OSDN Git Service

1763c41aaf500d9ff92795457e17a48c211946ba
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl.c
1 /* Process declarations and variables for C++ compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003  Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* Process declarations and symbol lookup for C++ front end.
25    Also constructs types; the standard scalar types at initialization,
26    and structure, union, array and enum types when they are declared.  */
27
28 /* ??? not all decl nodes are given the most useful possible
29    line numbers.  For example, the CONST_DECLs for enum values.  */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "lex.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "diagnostic.h"
52 #include "debug.h"
53 #include "timevar.h"
54
55 static tree grokparms (tree);
56 static const char *redeclaration_error_message (tree, tree);
57
58 static void push_binding_level (struct cp_binding_level *, int,
59                                 int);
60 static void pop_binding_level (void);
61 static void suspend_binding_level (void);
62 static void resume_binding_level (struct cp_binding_level *);
63 static struct cp_binding_level *make_binding_level (void);
64 static void declare_namespace_level (void);
65 static int decl_jump_unsafe (tree);
66 static void storedecls (tree);
67 static void require_complete_types_for_parms (tree);
68 static int ambi_op_p (enum tree_code);
69 static int unary_op_p (enum tree_code);
70 static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
71 static tree lookup_tag_reverse (tree, tree);
72 static void push_local_name (tree);
73 static void warn_extern_redeclared_static (tree, tree);
74 static tree grok_reference_init (tree, tree, tree);
75 static tree grokfndecl (tree, tree, tree, tree, int,
76                         enum overload_flags, tree,
77                         tree, int, int, int, int, int, int, tree);
78 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
79 static tree follow_tag_typedef (tree);
80 static tree lookup_tag (enum tree_code, tree,
81                         struct cp_binding_level *, int);
82 static void set_identifier_type_value_with_scope
83         (tree, tree, struct cp_binding_level *);
84 static void record_unknown_type (tree, const char *);
85 static tree builtin_function_1 (const char *, tree, tree, int,
86                                 enum built_in_class, const char *,
87                                 tree);
88 static tree build_library_fn_1 (tree, enum tree_code, tree);
89 static int member_function_or_else (tree, tree, enum overload_flags);
90 static void bad_specifiers (tree, const char *, int, int, int, int,
91                             int);
92 static tree maybe_process_template_type_declaration 
93         (tree, int, struct cp_binding_level*);
94 static void check_for_uninitialized_const_var (tree);
95 static hashval_t typename_hash (const void *);
96 static int typename_compare (const void *, const void *);
97 static void push_binding (tree, tree, struct cp_binding_level*);
98 static int add_binding (tree, tree);
99 static void pop_binding (tree, tree);
100 static tree local_variable_p_walkfn (tree *, int *, void *);
101 static tree 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     /* An array of static functions and variables (for namespaces only) */
331     varray_type static_decls;
332
333     /* A chain of VTABLE_DECL nodes.  */
334     tree vtables; 
335
336     /* A list of structure, union and enum definitions, for looking up
337        tag names.
338        It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
339        or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
340        or ENUMERAL_TYPE node.
341
342        C++: the TREE_VALUE nodes can be simple types for
343        component_bindings.  */
344     tree tags;
345
346     /* A list of USING_DECL nodes.  */
347     tree usings;
348
349     /* A list of used namespaces. PURPOSE is the namespace,
350        VALUE the common ancestor with this binding_level's namespace.  */
351     tree using_directives;
352
353     /* If this binding level is the binding level for a class, then
354        class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
355        is the name of an entity bound in the class.  The TREE_TYPE is
356        the DECL bound by this name in the class.  */
357     tree class_shadowed;
358
359     /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
360        is used for all binding levels. In addition the TREE_VALUE is the
361        IDENTIFIER_TYPE_VALUE before we entered the class.  */
362     tree type_shadowed;
363
364     /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
365        label in this scope.  The TREE_PURPOSE is the previous value of
366        the IDENTIFIER_LABEL VALUE.  */
367     tree shadowed_labels;
368
369     /* For each level (except not the global one),
370        a chain of BLOCK nodes for all the levels
371        that were entered and exited one level down.  */
372     tree blocks;
373
374     /* The _TYPE node for this level, if parm_flag == 2.  */
375     tree this_class;
376
377     /* The binding level which this one is contained in (inherits from).  */
378     struct cp_binding_level *level_chain;
379
380     /* List of VAR_DECLS saved from a previous for statement.
381        These would be dead in ISO-conforming code, but might
382        be referenced in ARM-era code.  These are stored in a
383        TREE_LIST; the TREE_VALUE is the actual declaration.  */
384     tree dead_vars_from_for;
385
386     /* 1 for the level that holds the parameters of a function.
387        2 for the level that holds a class declaration.  */
388     unsigned parm_flag : 2;
389
390     /* 1 means make a BLOCK for this level regardless of all else.
391        2 for temporary binding contours created by the compiler.  */
392     unsigned keep : 2;
393
394     /* Nonzero if this level "doesn't exist" for tags.  */
395     unsigned tag_transparent : 1;
396
397     /* Nonzero if this level can safely have additional
398        cleanup-needing variables added to it.  */
399     unsigned more_cleanups_ok : 1;
400     unsigned have_cleanups : 1;
401
402     /* Nonzero if this scope is for storing the decls for template
403        parameters and generic decls; these decls will be discarded and
404        replaced with a TEMPLATE_DECL.  */
405     unsigned template_parms_p : 1;
406
407     /* Nonzero if this scope corresponds to the `<>' in a
408        `template <>' clause.  Whenever this flag is set,
409        TEMPLATE_PARMS_P will be set as well.  */
410     unsigned template_spec_p : 1;
411
412     /* This is set for a namespace binding level.  */
413     unsigned namespace_p : 1;
414
415     /* True if this level is that of a for-statement where we need to
416        worry about ambiguous (ARM or ISO) scope rules.  */
417     unsigned is_for_scope : 1;
418
419     /* True if this level corresponds to a TRY block.  Currently this
420        information is only available while building the tree structure.  */
421     unsigned is_try_scope : 1;
422
423     /* True if this level corresponds to a CATCH block.  Currently this
424        information is only available while building the tree structure.  */
425     unsigned is_catch_scope : 1;
426
427     /* Three bits left for this word.  */
428
429     /* Binding depth at which this level began.  */
430     unsigned binding_depth;
431   };
432
433 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
434
435 /* The binding level currently in effect.  */
436
437 #define current_binding_level                   \
438   (cfun && cp_function_chain->bindings          \
439    ? cp_function_chain->bindings                \
440    : scope_chain->bindings)
441
442 /* The binding level of the current class, if any.  */
443
444 #define class_binding_level scope_chain->class_bindings
445
446 /* A chain of binding_level structures awaiting reuse.  */
447
448 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
449
450 /* The outermost binding level, for names of file scope.
451    This is created when the compiler is started and exists
452    through the entire run.  */
453
454 static GTY(()) struct cp_binding_level *global_binding_level;
455
456 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
457
458 static int keep_next_level_flag;
459
460 /* A TREE_LIST of VAR_DECLs.  The TREE_PURPOSE is a RECORD_TYPE or
461    UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type.  At the
462    time the VAR_DECL was declared, the type was incomplete.  */
463
464 static GTY(()) tree incomplete_vars;
465
466 #if defined(DEBUG_BINDING_LEVELS)
467 static int binding_depth = 0;
468 static int is_class_level = 0;
469
470 static void
471 indent (void)
472 {
473   register unsigned i;
474
475   for (i = 0; i < binding_depth*2; i++)
476     putc (' ', stderr);
477 }
478 #endif /* defined(DEBUG_BINDING_LEVELS) */
479
480 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
481
482 static void
483 push_binding_level (struct cp_binding_level *newlevel, 
484                     int tag_transparent, 
485                     int keep)
486 {
487   /* Add this level to the front of the chain (stack) of levels that
488      are active.  */
489   memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
490   newlevel->level_chain = current_binding_level;
491   current_binding_level = newlevel;
492   newlevel->tag_transparent = tag_transparent;
493   newlevel->more_cleanups_ok = 1;
494
495   newlevel->keep = keep;
496 #if defined(DEBUG_BINDING_LEVELS)
497   newlevel->binding_depth = binding_depth;
498   indent ();
499   fprintf (stderr, "push %s level 0x%08x line %d\n",
500            (is_class_level) ? "class" : "block", newlevel, lineno);
501   is_class_level = 0;
502   binding_depth++;
503 #endif /* defined(DEBUG_BINDING_LEVELS) */
504 }
505
506 /* Find the innermost enclosing class scope, and reset
507    CLASS_BINDING_LEVEL appropriately.  */
508
509 static void
510 find_class_binding_level (void)
511 {
512   struct cp_binding_level *level = current_binding_level;
513
514   while (level && level->parm_flag != 2)
515     level = level->level_chain;
516   if (level && level->parm_flag == 2)
517     class_binding_level = level;
518   else
519     class_binding_level = 0;
520 }
521
522 static void
523 pop_binding_level (void)
524 {
525   if (global_binding_level)
526     {
527       /* Cannot pop a level, if there are none left to pop.  */
528       if (current_binding_level == global_binding_level)
529         abort ();
530     }
531   /* Pop the current level, and free the structure for reuse.  */
532 #if defined(DEBUG_BINDING_LEVELS)
533   binding_depth--;
534   indent ();
535   fprintf (stderr, "pop  %s level 0x%08x line %d\n",
536           (is_class_level) ? "class" : "block",
537           current_binding_level, lineno);
538   if (is_class_level != (current_binding_level == class_binding_level))
539     {
540       indent ();
541       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
542     }
543   is_class_level = 0;
544 #endif /* defined(DEBUG_BINDING_LEVELS) */
545   {
546     register struct cp_binding_level *level = current_binding_level;
547     current_binding_level = current_binding_level->level_chain;
548     level->level_chain = free_binding_level;
549 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
550     if (level->binding_depth != binding_depth)
551       abort ();
552 #endif /* defined(DEBUG_BINDING_LEVELS) */
553     free_binding_level = level;
554     find_class_binding_level ();
555   }
556 }
557
558 static void
559 suspend_binding_level (void)
560 {
561   if (class_binding_level)
562     current_binding_level = class_binding_level;
563
564   if (global_binding_level)
565     {
566       /* Cannot suspend a level, if there are none left to suspend.  */
567       if (current_binding_level == global_binding_level)
568         abort ();
569     }
570   /* Suspend the current level.  */
571 #if defined(DEBUG_BINDING_LEVELS)
572   binding_depth--;
573   indent ();
574   fprintf (stderr, "suspend  %s level 0x%08x line %d\n",
575           (is_class_level) ? "class" : "block",
576           current_binding_level, lineno);
577   if (is_class_level != (current_binding_level == class_binding_level))
578     {
579       indent ();
580       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
581     }
582   is_class_level = 0;
583 #endif /* defined(DEBUG_BINDING_LEVELS) */
584   current_binding_level = current_binding_level->level_chain;
585   find_class_binding_level ();
586 }
587
588 static void
589 resume_binding_level (struct cp_binding_level* b)
590 {
591   /* Resuming binding levels is meant only for namespaces,
592      and those cannot nest into classes.  */
593   my_friendly_assert(!class_binding_level, 386);
594   /* Also, resuming a non-directly nested namespace is a no-no.  */
595   my_friendly_assert(b->level_chain == current_binding_level, 386);
596   current_binding_level = b;
597 #if defined(DEBUG_BINDING_LEVELS)
598   b->binding_depth = binding_depth;
599   indent ();
600   fprintf (stderr, "resume %s level 0x%08x line %d\n",
601            (is_class_level) ? "class" : "block", b, lineno);
602   is_class_level = 0;
603   binding_depth++;
604 #endif /* defined(DEBUG_BINDING_LEVELS) */
605 }
606 \f
607 /* Create a new `struct cp_binding_level'.  */
608
609 static
610 struct cp_binding_level *
611 make_binding_level (void)
612 {
613   /* NOSTRICT */
614   return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
615 }
616
617 /* Nonzero if we are currently in the global binding level.  */
618
619 int
620 global_bindings_p (void)
621 {
622   return current_binding_level == global_binding_level;
623 }
624
625 /* Return the innermost binding level that is not for a class scope.  */
626
627 static struct cp_binding_level *
628 innermost_nonclass_level (void)
629 {
630   struct cp_binding_level *b;
631
632   b = current_binding_level;
633   while (b->parm_flag == 2)
634     b = b->level_chain;
635
636   return b;
637 }
638
639 /* Nonzero if we are currently in a toplevel binding level.  This
640    means either the global binding level or a namespace in a toplevel
641    binding level.  Since there are no non-toplevel namespace levels,
642    this really means any namespace or template parameter level.  We
643    also include a class whose context is toplevel.  */
644
645 int
646 toplevel_bindings_p (void)
647 {
648   struct cp_binding_level *b = innermost_nonclass_level ();
649
650   return b->namespace_p || b->template_parms_p;
651 }
652
653 /* Nonzero if this is a namespace scope, or if we are defining a class
654    which is itself at namespace scope, or whose enclosing class is
655    such a class, etc.  */
656
657 int
658 namespace_bindings_p (void)
659 {
660   struct cp_binding_level *b = innermost_nonclass_level ();
661
662   return b->namespace_p;
663 }
664
665 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
666    unconditionally.  Otherwise, use the normal logic to decide whether
667    or not to create a BLOCK.  */
668
669 void
670 keep_next_level (int keep)
671 {
672   keep_next_level_flag = keep;
673 }
674
675 /* Nonzero if the current level needs to have a BLOCK made.  */
676
677 int
678 kept_level_p (void)
679 {
680   return (current_binding_level->blocks != NULL_TREE
681           || current_binding_level->keep
682           || current_binding_level->names != NULL_TREE
683           || (current_binding_level->tags != NULL_TREE
684               && !current_binding_level->tag_transparent));
685 }
686
687 static void
688 declare_namespace_level (void)
689 {
690   current_binding_level->namespace_p = 1;
691 }
692
693 /* Returns nonzero if this scope was created to store template
694    parameters.  */
695
696 int
697 template_parm_scope_p (void)
698 {
699   return current_binding_level->template_parms_p;
700 }
701
702 /* Returns the kind of template specialization we are currently
703    processing, given that it's declaration contained N_CLASS_SCOPES
704    explicit scope qualifications.  */
705
706 tmpl_spec_kind
707 current_tmpl_spec_kind (int n_class_scopes)
708 {
709   int n_template_parm_scopes = 0;
710   int seen_specialization_p = 0;
711   int innermost_specialization_p = 0;
712   struct cp_binding_level *b;
713
714   /* Scan through the template parameter scopes.  */
715   for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
716     {
717       /* If we see a specialization scope inside a parameter scope,
718          then something is wrong.  That corresponds to a declaration
719          like:
720
721             template <class T> template <> ...
722
723          which is always invalid since [temp.expl.spec] forbids the
724          specialization of a class member template if the enclosing
725          class templates are not explicitly specialized as well.  */
726       if (b->template_spec_p)
727         {
728           if (n_template_parm_scopes == 0)
729             innermost_specialization_p = 1;
730           else
731             seen_specialization_p = 1;
732         }
733       else if (seen_specialization_p == 1)
734         return tsk_invalid_member_spec;
735
736       ++n_template_parm_scopes;
737     }
738
739   /* Handle explicit instantiations.  */
740   if (processing_explicit_instantiation)
741     {
742       if (n_template_parm_scopes != 0)
743         /* We've seen a template parameter list during an explicit
744            instantiation.  For example:
745
746              template <class T> template void f(int);
747
748            This is erroneous.  */
749         return tsk_invalid_expl_inst;
750       else
751         return tsk_expl_inst;
752     }
753
754   if (n_template_parm_scopes < n_class_scopes)
755     /* We've not seen enough template headers to match all the
756        specialized classes present.  For example:
757
758          template <class T> void R<T>::S<T>::f(int);
759
760        This is invalid; there needs to be one set of template
761        parameters for each class.  */
762     return tsk_insufficient_parms;
763   else if (n_template_parm_scopes == n_class_scopes)
764     /* We're processing a non-template declaration (even though it may
765        be a member of a template class.)  For example:
766
767          template <class T> void S<T>::f(int);
768
769        The `class T' maches the `S<T>', leaving no template headers
770        corresponding to the `f'.  */
771     return tsk_none;
772   else if (n_template_parm_scopes > n_class_scopes + 1)
773     /* We've got too many template headers.  For example:
774
775          template <> template <class T> void f (T);
776
777        There need to be more enclosing classes.  */
778     return tsk_excessive_parms;
779   else
780     /* This must be a template.  It's of the form:
781
782          template <class T> template <class U> void S<T>::f(U);
783
784        This is a specialization if the innermost level was a
785        specialization; otherwise it's just a definition of the
786        template.  */
787     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
788 }
789
790 void
791 set_class_shadows (tree shadows)
792 {
793   class_binding_level->class_shadowed = shadows;
794 }
795
796 /* Enter a new binding level.
797    If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
798    not for that of tags.  */
799
800 void
801 pushlevel (int tag_transparent)
802 {
803   struct cp_binding_level *newlevel;
804
805   if (cfun && !doing_semantic_analysis_p ())
806     return;
807
808   /* Reuse or create a struct for this binding level.  */
809 #if defined(DEBUG_BINDING_LEVELS)
810   if (0)
811 #else /* !defined(DEBUG_BINDING_LEVELS) */
812   if (free_binding_level)
813 #endif /* !defined(DEBUG_BINDING_LEVELS) */
814     {
815       newlevel = free_binding_level;
816       free_binding_level = free_binding_level->level_chain;
817     }
818   else
819     newlevel = make_binding_level ();
820
821   push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
822   keep_next_level_flag = 0;
823 }
824
825 /* We're defining an object of type TYPE.  If it needs a cleanup, but
826    we're not allowed to add any more objects with cleanups to the current
827    scope, create a new binding level.  */
828
829 void
830 maybe_push_cleanup_level (tree type)
831 {
832   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
833       && current_binding_level->more_cleanups_ok == 0)
834     {
835       keep_next_level (2);
836       pushlevel (1);
837       clear_last_expr ();
838       add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
839     }
840 }
841   
842 /* Enter a new scope.  The KIND indicates what kind of scope is being
843    created.  */
844
845 void
846 begin_scope (scope_kind sk)
847 {
848   pushlevel (0);
849
850   switch (sk)
851     {
852     case sk_block:
853       break;
854
855     case sk_try:
856       current_binding_level->is_try_scope = 1;
857       break;
858
859     case sk_catch:
860       current_binding_level->is_catch_scope = 1;
861       break;
862
863     case sk_for:
864       current_binding_level->is_for_scope = 1;
865       break;
866
867     case sk_template_spec:
868       current_binding_level->template_spec_p = 1;
869       /* Fall through.  */
870
871     case sk_template_parms:
872       current_binding_level->template_parms_p = 1;
873       break;
874
875     default:
876       abort ();
877     }
878 }
879
880 /* Exit the current scope.  */
881
882 void
883 finish_scope (void)
884 {
885   poplevel (0, 0, 0);
886 }
887
888 /* For a binding between a name and an entity at a block scope,
889    this is the `struct cp_binding_level' for the block.  */
890 #define BINDING_LEVEL(NODE) \
891   (((struct tree_binding*)(NODE))->scope.level)
892
893 /* A free list of CPLUS_BINDING nodes, connected by their
894    TREE_CHAINs.  */
895
896 static GTY((deletable (""))) tree free_bindings;
897
898 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
899    level at which this declaration is being bound.  */
900
901 static void
902 push_binding (tree id, 
903               tree decl,
904               struct cp_binding_level* level)
905 {
906   tree binding;
907
908   if (free_bindings)
909     {
910       binding = free_bindings;
911       free_bindings = TREE_CHAIN (binding);
912     }
913   else
914     binding = make_node (CPLUS_BINDING);
915
916   /* Now, fill in the binding information.  */
917   BINDING_VALUE (binding) = decl;
918   BINDING_TYPE (binding) = NULL_TREE;
919   BINDING_LEVEL (binding) = level;
920   INHERITED_VALUE_BINDING_P (binding) = 0;
921   LOCAL_BINDING_P (binding) = (level != class_binding_level);
922   BINDING_HAS_LEVEL_P (binding) = 1;
923
924   /* And put it on the front of the list of bindings for ID.  */
925   TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
926   IDENTIFIER_BINDING (id) = binding;
927 }
928
929 /* ID is already bound in the current scope.  But, DECL is an
930    additional binding for ID in the same scope.  This is the `struct
931    stat' hack whereby a non-typedef class-name or enum-name can be
932    bound at the same level as some other kind of entity.  It's the
933    responsibility of the caller to check that inserting this name is
934    valid here.  Returns nonzero if the new binding was successful.  */
935 static int
936 add_binding (tree id, tree decl)
937 {
938   tree binding = IDENTIFIER_BINDING (id);
939   int ok = 1;
940
941   timevar_push (TV_NAME_LOOKUP);
942   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
943     /* The new name is the type name.  */
944     BINDING_TYPE (binding) = decl;
945   else if (!BINDING_VALUE (binding))
946     /* This situation arises when push_class_level_binding moves an
947        inherited type-binding out of the way to make room for a new
948        value binding.  */
949     BINDING_VALUE (binding) = decl;
950   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
951            && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
952     {
953       /* The old binding was a type name.  It was placed in
954          BINDING_VALUE because it was thought, at the point it was
955          declared, to be the only entity with such a name.  Move the
956          type name into the type slot; it is now hidden by the new
957          binding.  */
958       BINDING_TYPE (binding) = BINDING_VALUE (binding);
959       BINDING_VALUE (binding) = decl;
960       INHERITED_VALUE_BINDING_P (binding) = 0;
961     }
962   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
963            && TREE_CODE (decl) == TYPE_DECL
964            && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
965            && (same_type_p (TREE_TYPE (decl),
966                             TREE_TYPE (BINDING_VALUE (binding)))
967                /* If either type involves template parameters, we must
968                   wait until instantiation.  */
969                || uses_template_parms (TREE_TYPE (decl))
970                || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
971     /* We have two typedef-names, both naming the same type to have
972        the same name.  This is OK because of:
973
974          [dcl.typedef]
975
976          In a given scope, a typedef specifier can be used to redefine
977          the name of any type declared in that scope to refer to the
978          type to which it already refers.  */
979     ok = 0;
980   /* There can be two block-scope declarations of the same variable,
981      so long as they are `extern' declarations.  */
982   else if (TREE_CODE (decl) == VAR_DECL
983            && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
984            && DECL_EXTERNAL (decl)
985            && DECL_EXTERNAL (BINDING_VALUE (binding)))
986     {
987       duplicate_decls (decl, BINDING_VALUE (binding));
988       ok = 0;
989     }
990   else
991     {
992       error ("declaration of `%#D'", decl);
993       cp_error_at ("conflicts with previous declaration `%#D'",
994                    BINDING_VALUE (binding));
995       ok = 0;
996     }
997
998   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
999 }
1000
1001 /* Add DECL to the list of things declared in B.  */
1002
1003 static void
1004 add_decl_to_level (tree decl, 
1005                    struct cp_binding_level* b)
1006 {
1007   if (TREE_CODE (decl) == NAMESPACE_DECL 
1008       && !DECL_NAMESPACE_ALIAS (decl))
1009     {
1010       TREE_CHAIN (decl) = b->namespaces;
1011       b->namespaces = decl;
1012     }
1013   else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1014     {
1015       TREE_CHAIN (decl) = b->vtables;
1016       b->vtables = decl;
1017     }
1018   else       
1019     {
1020       /* We build up the list in reverse order, and reverse it later if
1021          necessary.  */
1022       TREE_CHAIN (decl) = b->names;
1023       b->names = decl;
1024       b->names_size++;
1025
1026       /* If appropriate, add decl to separate list of statics */
1027       if (b->namespace_p)
1028         if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1029             || (TREE_CODE (decl) == FUNCTION_DECL
1030                 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1031           VARRAY_PUSH_TREE (b->static_decls, decl);
1032     }
1033 }
1034
1035 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1036    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1037    doesn't really belong to this binding level, that it got here
1038    through a using-declaration.  */
1039
1040 void
1041 push_local_binding (tree id, tree decl, int flags)
1042 {
1043   struct cp_binding_level *b;
1044
1045   /* Skip over any local classes.  This makes sense if we call
1046      push_local_binding with a friend decl of a local class.  */
1047   b = current_binding_level;
1048   while (b->parm_flag == 2)
1049     b = b->level_chain;
1050
1051   if (lookup_name_current_level (id))
1052     {
1053       /* Supplement the existing binding.  */
1054       if (!add_binding (id, decl))
1055         /* It didn't work.  Something else must be bound at this
1056            level.  Do not add DECL to the list of things to pop
1057            later.  */
1058         return;
1059     }
1060   else
1061     /* Create a new binding.  */
1062     push_binding (id, decl, b);
1063
1064   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1065     /* We must put the OVERLOAD into a TREE_LIST since the
1066        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1067        decls that got here through a using-declaration.  */
1068     decl = build_tree_list (NULL_TREE, decl);
1069
1070   /* And put DECL on the list of things declared by the current
1071      binding level.  */
1072   add_decl_to_level (decl, b);
1073 }
1074
1075 /* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1076    binding was successful.  */
1077
1078 int
1079 push_class_binding (tree id, tree decl)
1080 {
1081   int result = 1;
1082   tree binding = IDENTIFIER_BINDING (id);
1083   tree context;
1084
1085   timevar_push (TV_NAME_LOOKUP);
1086   /* Note that we declared this value so that we can issue an error if
1087      this is an invalid redeclaration of a name already used for some
1088      other purpose.  */
1089   note_name_declared_in_class (id, decl);
1090
1091   if (binding && BINDING_LEVEL (binding) == class_binding_level)
1092     /* Supplement the existing binding.  */
1093     result = add_binding (id, decl);
1094   else
1095     /* Create a new binding.  */
1096     push_binding (id, decl, class_binding_level);
1097
1098   /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1099      class-level declaration.  Note that we do not use DECL here
1100      because of the possibility of the `struct stat' hack; if DECL is
1101      a class-name or enum-name we might prefer a field-name, or some
1102      such.  */
1103   IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1104
1105   /* If this is a binding from a base class, mark it as such.  */
1106   binding = IDENTIFIER_BINDING (id);
1107   if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1108     {
1109       if (TREE_CODE (decl) == OVERLOAD)
1110         context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1111       else
1112         {
1113           my_friendly_assert (DECL_P (decl), 0);
1114           context = context_for_name_lookup (decl);
1115         }
1116
1117       if (is_properly_derived_from (current_class_type, context))
1118         INHERITED_VALUE_BINDING_P (binding) = 1;
1119       else
1120         INHERITED_VALUE_BINDING_P (binding) = 0;
1121     }
1122   else if (BINDING_VALUE (binding) == decl)
1123     /* We only encounter a TREE_LIST when push_class_decls detects an
1124        ambiguity.  Such an ambiguity can be overridden by a definition
1125        in this class.  */
1126     INHERITED_VALUE_BINDING_P (binding) = 1;
1127
1128   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1129 }
1130
1131 /* Remove the binding for DECL which should be the innermost binding
1132    for ID.  */
1133
1134 static void
1135 pop_binding (tree id, tree decl)
1136 {
1137   tree binding;
1138
1139   if (id == NULL_TREE)
1140     /* It's easiest to write the loops that call this function without
1141        checking whether or not the entities involved have names.  We
1142        get here for such an entity.  */
1143     return;
1144
1145   /* Get the innermost binding for ID.  */
1146   binding = IDENTIFIER_BINDING (id);
1147
1148   /* The name should be bound.  */
1149   my_friendly_assert (binding != NULL_TREE, 0);
1150
1151   /* The DECL will be either the ordinary binding or the type
1152      binding for this identifier.  Remove that binding.  */
1153   if (BINDING_VALUE (binding) == decl)
1154     BINDING_VALUE (binding) = NULL_TREE;
1155   else if (BINDING_TYPE (binding) == decl)
1156     BINDING_TYPE (binding) = NULL_TREE;
1157   else
1158     abort ();
1159
1160   if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1161     {
1162       /* We're completely done with the innermost binding for this
1163          identifier.  Unhook it from the list of bindings.  */
1164       IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1165
1166       /* Add it to the free list.  */
1167       TREE_CHAIN (binding) = free_bindings;
1168       free_bindings = binding;
1169
1170       /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1171          it.  */
1172       BINDING_LEVEL (binding) = NULL;
1173     }
1174 }
1175
1176 /* When a label goes out of scope, check to see if that label was used
1177    in a valid manner, and issue any appropriate warnings or errors.  */
1178
1179 static void
1180 pop_label (tree label, tree old_value)
1181 {
1182   if (!processing_template_decl && doing_semantic_analysis_p ())
1183     {
1184       if (DECL_INITIAL (label) == NULL_TREE)
1185         {
1186           cp_error_at ("label `%D' used but not defined", label);
1187           /* Avoid crashing later.  */
1188           define_label (input_filename, 1, DECL_NAME (label));
1189         }
1190       else if (warn_unused_label && !TREE_USED (label))
1191         cp_warning_at ("label `%D' defined but not used", label);
1192     }
1193
1194   SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1195 }
1196
1197 /* At the end of a function, all labels declared within the function
1198    go out of scope.  BLOCK is the top-level block for the
1199    function.  */
1200
1201 static void
1202 pop_labels (tree block)
1203 {
1204   struct named_label_list *link;
1205
1206   /* Clear out the definitions of all label names, since their scopes
1207      end here.  */
1208   for (link = named_labels; link; link = link->next)
1209     {
1210       pop_label (link->label_decl, link->old_value);
1211       /* Put the labels into the "variables" of the top-level block,
1212          so debugger can see them.  */
1213       TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1214       BLOCK_VARS (block) = link->label_decl;
1215     }
1216
1217   named_labels = NULL;
1218 }
1219
1220 /* Exit a binding level.
1221    Pop the level off, and restore the state of the identifier-decl mappings
1222    that were in effect when this level was entered.
1223
1224    If KEEP == 1, this level had explicit declarations, so
1225    and create a "block" (a BLOCK node) for the level
1226    to record its declarations and subblocks for symbol table output.
1227
1228    If FUNCTIONBODY is nonzero, this level is the body of a function,
1229    so create a block as if KEEP were set and also clear out all
1230    label names.
1231
1232    If REVERSE is nonzero, reverse the order of decls before putting
1233    them into the BLOCK.  */
1234
1235 tree
1236 poplevel (int keep, int reverse, int functionbody)
1237 {
1238   register tree link;
1239   /* The chain of decls was accumulated in reverse order.
1240      Put it into forward order, just for cleanliness.  */
1241   tree decls;
1242   int tmp = functionbody;
1243   int real_functionbody;
1244   tree tags;
1245   tree subblocks;
1246   tree block = NULL_TREE;
1247   tree decl;
1248   int leaving_for_scope;
1249
1250   timevar_push (TV_NAME_LOOKUP);
1251   if (cfun && !doing_semantic_analysis_p ())
1252     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1253
1254   my_friendly_assert (current_binding_level->parm_flag != 2,
1255                       19990916);
1256
1257   real_functionbody = (current_binding_level->keep == 2
1258                        ? ((functionbody = 0), tmp) : functionbody);
1259   tags = functionbody >= 0 ? current_binding_level->tags : 0;
1260   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1261
1262   my_friendly_assert (!current_binding_level->class_shadowed,
1263                       19990414);
1264
1265   /* We used to use KEEP == 2 to indicate that the new block should go
1266      at the beginning of the list of blocks at this binding level,
1267      rather than the end.  This hack is no longer used.  */
1268   my_friendly_assert (keep == 0 || keep == 1, 0);
1269
1270   if (current_binding_level->keep == 1)
1271     keep = 1;
1272
1273   /* Any uses of undefined labels, and any defined labels, now operate
1274      under constraints of next binding contour.  */
1275   if (cfun && !functionbody)
1276     {
1277       struct cp_binding_level *level_chain;
1278       level_chain = current_binding_level->level_chain;
1279       if (level_chain)
1280         {
1281           struct named_label_use_list *uses;
1282           struct named_label_list *labels;
1283           for (labels = named_labels; labels; labels = labels->next)
1284             if (labels->binding_level == current_binding_level)
1285               {
1286                 tree decl;
1287                 if (current_binding_level->is_try_scope)
1288                   labels->in_try_scope = 1;
1289                 if (current_binding_level->is_catch_scope)
1290                   labels->in_catch_scope = 1;
1291                 for (decl = labels->names_in_scope; decl;
1292                      decl = TREE_CHAIN (decl))
1293                   if (decl_jump_unsafe (decl))
1294                     labels->bad_decls = tree_cons (NULL_TREE, decl,
1295                                                    labels->bad_decls);
1296                 labels->binding_level = level_chain;
1297                 labels->names_in_scope = level_chain->names;
1298               }
1299
1300           for (uses = named_label_uses; uses; uses = uses->next)
1301             if (uses->binding_level == current_binding_level)
1302               {
1303                 uses->binding_level = level_chain;
1304                 uses->names_in_scope = level_chain->names;
1305               }
1306         }
1307     }
1308
1309   /* Get the decls in the order they were written.
1310      Usually current_binding_level->names is in reverse order.
1311      But parameter decls were previously put in forward order.  */
1312
1313   if (reverse)
1314     current_binding_level->names
1315       = decls = nreverse (current_binding_level->names);
1316   else
1317     decls = current_binding_level->names;
1318
1319   /* Output any nested inline functions within this block
1320      if they weren't already output.  */
1321   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1322     if (TREE_CODE (decl) == FUNCTION_DECL
1323         && ! TREE_ASM_WRITTEN (decl)
1324         && DECL_INITIAL (decl) != NULL_TREE
1325         && TREE_ADDRESSABLE (decl)
1326         && decl_function_context (decl) == current_function_decl)
1327       {
1328         /* If this decl was copied from a file-scope decl
1329            on account of a block-scope extern decl,
1330            propagate TREE_ADDRESSABLE to the file-scope decl.  */
1331         if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1332           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1333         else
1334           {
1335             push_function_context ();
1336             output_inline_function (decl);
1337             pop_function_context ();
1338           }
1339       }
1340
1341   /* When not in function-at-a-time mode, expand_end_bindings will
1342      warn about unused variables.  But, in function-at-a-time mode
1343      expand_end_bindings is not passed the list of variables in the
1344      current scope, and therefore no warning is emitted.  So, we
1345      explicitly warn here.  */
1346   if (!processing_template_decl)
1347     warn_about_unused_variables (getdecls ());
1348
1349   /* If there were any declarations or structure tags in that level,
1350      or if this level is a function body,
1351      create a BLOCK to record them for the life of this function.  */
1352   block = NULL_TREE;
1353   if (keep == 1 || functionbody)
1354     block = make_node (BLOCK);
1355   if (block != NULL_TREE)
1356     {
1357       BLOCK_VARS (block) = decls;
1358       BLOCK_SUBBLOCKS (block) = subblocks;
1359     }
1360
1361   /* In each subblock, record that this is its superior.  */
1362   if (keep >= 0)
1363     for (link = subblocks; link; link = TREE_CHAIN (link))
1364       BLOCK_SUPERCONTEXT (link) = block;
1365
1366   /* We still support the old for-scope rules, whereby the variables
1367      in a for-init statement were in scope after the for-statement
1368      ended.  We only use the new rules if flag_new_for_scope is
1369      nonzero.  */
1370   leaving_for_scope
1371     = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1372
1373   /* Remove declarations for all the DECLs in this level.  */
1374   for (link = decls; link; link = TREE_CHAIN (link))
1375     {
1376       if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1377           && DECL_NAME (link))
1378         {
1379           tree outer_binding
1380             = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1381           tree ns_binding;
1382
1383           if (!outer_binding)
1384             ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1385           else
1386             ns_binding = NULL_TREE;
1387
1388           if (outer_binding
1389               && (BINDING_LEVEL (outer_binding)
1390                   == current_binding_level->level_chain))
1391             /* We have something like:
1392
1393                  int i;
1394                  for (int i; ;);
1395
1396                and we are leaving the `for' scope.  There's no reason to
1397                keep the binding of the inner `i' in this case.  */
1398             pop_binding (DECL_NAME (link), link);
1399           else if ((outer_binding
1400                     && (TREE_CODE (BINDING_VALUE (outer_binding))
1401                         == TYPE_DECL))
1402                    || (ns_binding
1403                        && TREE_CODE (ns_binding) == TYPE_DECL))
1404             /* Here, we have something like:
1405
1406                  typedef int I;
1407
1408                  void f () {
1409                    for (int I; ;);
1410                  }
1411
1412                We must pop the for-scope binding so we know what's a
1413                type and what isn't.  */
1414             pop_binding (DECL_NAME (link), link);
1415           else
1416             {
1417               /* Mark this VAR_DECL as dead so that we can tell we left it
1418                  there only for backward compatibility.  */
1419               DECL_DEAD_FOR_LOCAL (link) = 1;
1420
1421               /* Keep track of what should of have happenned when we
1422                  popped the binding.  */
1423               if (outer_binding && BINDING_VALUE (outer_binding))
1424                 DECL_SHADOWED_FOR_VAR (link)
1425                   = BINDING_VALUE (outer_binding);
1426
1427               /* Add it to the list of dead variables in the next
1428                  outermost binding to that we can remove these when we
1429                  leave that binding.  */
1430               current_binding_level->level_chain->dead_vars_from_for
1431                 = tree_cons (NULL_TREE, link,
1432                              current_binding_level->level_chain->
1433                              dead_vars_from_for);
1434
1435               /* Although we don't pop the CPLUS_BINDING, we do clear
1436                  its BINDING_LEVEL since the level is going away now.  */
1437               BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1438                 = 0;
1439             }
1440         }
1441       else
1442         {
1443           /* Remove the binding.  */
1444           decl = link;
1445           if (TREE_CODE (decl) == TREE_LIST)
1446             decl = TREE_VALUE (decl);
1447           if (DECL_P (decl))
1448             pop_binding (DECL_NAME (decl), decl);
1449           else if (TREE_CODE (decl) == OVERLOAD)
1450             pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1451           else
1452             abort ();
1453         }
1454     }
1455
1456   /* Remove declarations for any `for' variables from inner scopes
1457      that we kept around.  */
1458   for (link = current_binding_level->dead_vars_from_for;
1459        link; link = TREE_CHAIN (link))
1460     pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1461
1462   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1463   for (link = current_binding_level->type_shadowed;
1464        link; link = TREE_CHAIN (link))
1465     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1466
1467   /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1468   for (link = current_binding_level->shadowed_labels;
1469        link;
1470        link = TREE_CHAIN (link))
1471     pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1472
1473   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1474      list if a `using' declaration put them there.  The debugging
1475      back-ends won't understand OVERLOAD, so we remove them here.
1476      Because the BLOCK_VARS are (temporarily) shared with
1477      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1478      popped all the bindings.  */
1479   if (block)
1480     {
1481       tree* d;
1482
1483       for (d = &BLOCK_VARS (block); *d; )
1484         {
1485           if (TREE_CODE (*d) == TREE_LIST)
1486             *d = TREE_CHAIN (*d);
1487           else
1488             d = &TREE_CHAIN (*d);
1489         }
1490     }
1491
1492   /* If the level being exited is the top level of a function,
1493      check over all the labels.  */
1494   if (functionbody)
1495     {
1496       /* Since this is the top level block of a function, the vars are
1497          the function's parameters.  Don't leave them in the BLOCK
1498          because they are found in the FUNCTION_DECL instead.  */
1499       BLOCK_VARS (block) = 0;
1500       pop_labels (block);
1501     }
1502
1503   tmp = current_binding_level->keep;
1504
1505   pop_binding_level ();
1506   if (functionbody)
1507     DECL_INITIAL (current_function_decl) = block;
1508   else if (block)
1509     current_binding_level->blocks
1510       = chainon (current_binding_level->blocks, block);
1511
1512   /* If we did not make a block for the level just exited,
1513      any blocks made for inner levels
1514      (since they cannot be recorded as subblocks in that level)
1515      must be carried forward so they will later become subblocks
1516      of something else.  */
1517   else if (subblocks)
1518     current_binding_level->blocks
1519       = chainon (current_binding_level->blocks, subblocks);
1520
1521   /* Each and every BLOCK node created here in `poplevel' is important
1522      (e.g. for proper debugging information) so if we created one
1523      earlier, mark it as "used".  */
1524   if (block)
1525     TREE_USED (block) = 1;
1526
1527   /* Take care of compiler's internal binding structures.  */
1528   if (tmp == 2)
1529     {
1530       tree scope_stmts;
1531
1532       scope_stmts
1533         = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1534       if (block)
1535         {
1536           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1537           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1538         }
1539
1540       block = poplevel (keep, reverse, functionbody);
1541     }
1542
1543   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1544 }
1545
1546 /* Delete the node BLOCK from the current binding level.
1547    This is used for the block inside a stmt expr ({...})
1548    so that the block can be reinserted where appropriate.  */
1549
1550 void
1551 delete_block (tree block)
1552 {
1553   tree t;
1554   if (current_binding_level->blocks == block)
1555     current_binding_level->blocks = TREE_CHAIN (block);
1556   for (t = current_binding_level->blocks; t;)
1557     {
1558       if (TREE_CHAIN (t) == block)
1559         TREE_CHAIN (t) = TREE_CHAIN (block);
1560       else
1561         t = TREE_CHAIN (t);
1562     }
1563   TREE_CHAIN (block) = NULL_TREE;
1564   /* Clear TREE_USED which is always set by poplevel.
1565      The flag is set again if insert_block is called.  */
1566   TREE_USED (block) = 0;
1567 }
1568
1569 /* Insert BLOCK at the end of the list of subblocks of the
1570    current binding level.  This is used when a BIND_EXPR is expanded,
1571    to handle the BLOCK node inside the BIND_EXPR.  */
1572
1573 void
1574 insert_block (tree block)
1575 {
1576   TREE_USED (block) = 1;
1577   current_binding_level->blocks
1578     = chainon (current_binding_level->blocks, block);
1579 }
1580
1581 /* Set the BLOCK node for the innermost scope
1582    (the one we are currently in).  */
1583
1584 void
1585 set_block (tree block ATTRIBUTE_UNUSED )
1586 {
1587   /* The RTL expansion machinery requires us to provide this callback,
1588      but it is not applicable in function-at-a-time mode.  */
1589   my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1590 }
1591
1592 /* Do a pushlevel for class declarations.  */
1593
1594 void
1595 pushlevel_class (void)
1596 {
1597   register struct cp_binding_level *newlevel;
1598
1599   /* Reuse or create a struct for this binding level.  */
1600 #if defined(DEBUG_BINDING_LEVELS)
1601   if (0)
1602 #else /* !defined(DEBUG_BINDING_LEVELS) */
1603   if (free_binding_level)
1604 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1605     {
1606       newlevel = free_binding_level;
1607       free_binding_level = free_binding_level->level_chain;
1608     }
1609   else
1610     newlevel = make_binding_level ();
1611
1612 #if defined(DEBUG_BINDING_LEVELS)
1613   is_class_level = 1;
1614 #endif /* defined(DEBUG_BINDING_LEVELS) */
1615
1616   push_binding_level (newlevel, 0, 0);
1617
1618   class_binding_level = current_binding_level;
1619   class_binding_level->parm_flag = 2;
1620   class_binding_level->this_class = current_class_type;
1621 }
1622
1623 /* ...and a poplevel for class declarations.  */
1624
1625 void
1626 poplevel_class (void)
1627 {
1628   register struct cp_binding_level *level = class_binding_level;
1629   tree shadowed;
1630
1631   timevar_push (TV_NAME_LOOKUP);
1632   my_friendly_assert (level != 0, 354);
1633
1634   /* If we're leaving a toplevel class, don't bother to do the setting
1635      of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1636      shouldn't even be used when current_class_type isn't set, and second,
1637      if we don't touch it here, we're able to use the cache effect if the
1638      next time we're entering a class scope, it is the same class.  */
1639   if (current_class_depth != 1)
1640     {
1641       struct cp_binding_level* b;
1642
1643       /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1644       for (shadowed = level->class_shadowed;
1645            shadowed;
1646            shadowed = TREE_CHAIN (shadowed))
1647         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1648
1649       /* Find the next enclosing class, and recreate
1650          IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1651       b = level->level_chain;
1652       while (b && b->parm_flag != 2)
1653         b = b->level_chain;
1654
1655       if (b)
1656         for (shadowed = b->class_shadowed;
1657              shadowed;
1658              shadowed = TREE_CHAIN (shadowed))
1659           {
1660             tree t;
1661
1662             t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1663             while (t && BINDING_LEVEL (t) != b)
1664               t = TREE_CHAIN (t);
1665
1666             if (t)
1667               IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1668                 = BINDING_VALUE (t);
1669           }
1670     }
1671   else
1672     /* Remember to save what IDENTIFIER's were bound in this scope so we
1673        can recover from cache misses.  */
1674     {
1675       previous_class_type = current_class_type;
1676       previous_class_values = class_binding_level->class_shadowed;
1677     }
1678   for (shadowed = level->type_shadowed;
1679        shadowed;
1680        shadowed = TREE_CHAIN (shadowed))
1681     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1682
1683   /* Remove the bindings for all of the class-level declarations.  */
1684   for (shadowed = level->class_shadowed;
1685        shadowed;
1686        shadowed = TREE_CHAIN (shadowed))
1687     pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1688
1689   /* Now, pop out of the binding level which we created up in the
1690      `pushlevel_class' routine.  */
1691 #if defined(DEBUG_BINDING_LEVELS)
1692   is_class_level = 1;
1693 #endif /* defined(DEBUG_BINDING_LEVELS) */
1694
1695   pop_binding_level ();
1696   timevar_pop (TV_NAME_LOOKUP);
1697 }
1698
1699 /* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1700    for any names in enclosing classes.  */
1701
1702 void
1703 clear_identifier_class_values (void)
1704 {
1705   tree t;
1706
1707   if (!class_binding_level)
1708     return;
1709
1710   for (t = class_binding_level->class_shadowed;
1711        t;
1712        t = TREE_CHAIN (t))
1713     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1714 }
1715
1716 /* Returns nonzero if T is a virtual function table.  */
1717
1718 int
1719 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1720 {
1721   return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1722 }
1723
1724 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1725    functions.  */
1726
1727 int
1728 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1729 {
1730   return (TREE_CODE (t) == TYPE_DECL
1731           && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1732           && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1733 }
1734
1735 /* Return the declarations that are members of the namespace NS.  */
1736
1737 tree
1738 cp_namespace_decls (tree ns)
1739 {
1740   return NAMESPACE_LEVEL (ns)->names;
1741 }
1742
1743 struct walk_globals_data {
1744   walk_globals_pred p;
1745   walk_globals_fn f;
1746   void *data;
1747 };
1748
1749 /* Walk the vtable declarations in NAMESPACE.  Whenever one is found
1750    for which P returns nonzero, call F with its address.  If any call
1751    to F returns a nonzero value, return a nonzero value.  */
1752
1753 static int
1754 walk_vtables_r (tree namespace, void* data)
1755 {
1756   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1757   walk_globals_fn f = wgd->f;
1758   void *d = wgd->data;
1759   tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1760   int result = 0;
1761
1762   for (; decl ; decl = TREE_CHAIN (decl))
1763     result |= (*f) (&decl, d);
1764
1765   return result;
1766 }
1767
1768 /* Walk the vtable declarations.  Whenever one is found for which P
1769    returns nonzero, call F with its address.  If any call to F
1770    returns a nonzero value, return a nonzero value.  */
1771 bool
1772 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1773 {    
1774   struct walk_globals_data wgd;
1775   wgd.p = p;    
1776   wgd.f = f;
1777   wgd.data = data;
1778
1779   return walk_namespaces (walk_vtables_r, &wgd);
1780 }
1781
1782 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1783    itself, calling F for each.  The DATA is passed to F as well.  */
1784
1785 static int
1786 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1787 {
1788   int result = 0;
1789   tree current = NAMESPACE_LEVEL (namespace)->namespaces;     
1790
1791   result |= (*f) (namespace, data);
1792
1793   for (; current; current = TREE_CHAIN (current))
1794     result |= walk_namespaces_r (current, f, data);
1795
1796   return result;
1797 }
1798
1799 /* Walk all the namespaces, calling F for each.  The DATA is passed to
1800    F as well.  */
1801
1802 int
1803 walk_namespaces (walk_namespaces_fn f, void* data)
1804 {
1805   return walk_namespaces_r (global_namespace, f, data);
1806 }
1807
1808 /* Walk the global declarations in NAMESPACE.  Whenever one is found
1809    for which P returns nonzero, call F with its address.  If any call
1810    to F returns a nonzero value, return a nonzero value.  */
1811
1812 static int
1813 walk_globals_r (tree namespace, void* data)
1814 {
1815   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1816   walk_globals_pred p = wgd->p;
1817   walk_globals_fn f = wgd->f;
1818   void *d = wgd->data;
1819   tree *t;
1820   int result = 0;
1821
1822   t = &NAMESPACE_LEVEL (namespace)->names;
1823
1824   while (*t)
1825     {
1826       tree glbl = *t;
1827
1828       if ((*p) (glbl, d))
1829         result |= (*f) (t, d);
1830
1831       /* If F changed *T, then *T still points at the next item to
1832          examine.  */
1833       if (*t == glbl)
1834         t = &TREE_CHAIN (*t);
1835     }
1836
1837   return result;
1838 }
1839
1840 /* Walk the global declarations.  Whenever one is found for which P
1841    returns true, call F with its address.  If any call to F
1842    returns true, return true.  */
1843
1844 bool
1845 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1846 {
1847   struct walk_globals_data wgd;
1848   wgd.p = p;
1849   wgd.f = f;
1850   wgd.data = data;
1851
1852   return walk_namespaces (walk_globals_r, &wgd);
1853 }
1854
1855 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
1856    DATA is non-NULL, this is the last time we will call
1857    wrapup_global_declarations for this NAMESPACE.  */
1858
1859 int
1860 wrapup_globals_for_namespace (tree namespace, void* data)
1861 {
1862   struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1863   varray_type statics = level->static_decls;
1864   tree *vec = &VARRAY_TREE (statics, 0);
1865   int len = VARRAY_ACTIVE_SIZE (statics);
1866   int last_time = (data != 0);
1867
1868   if (last_time)
1869     {
1870       check_global_declarations (vec, len);
1871       return 0;
1872     }
1873
1874   /* Write out any globals that need to be output.  */
1875   return wrapup_global_declarations (vec, len);
1876 }
1877
1878 \f
1879 /* For debugging.  */
1880 static int no_print_functions = 0;
1881 static int no_print_builtins = 0;
1882
1883 void
1884 print_binding_level (struct cp_binding_level* lvl)
1885 {
1886   tree t;
1887   int i = 0, len;
1888   fprintf (stderr, " blocks=");
1889   fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1890   if (lvl->tag_transparent)
1891     fprintf (stderr, " tag-transparent");
1892   if (lvl->more_cleanups_ok)
1893     fprintf (stderr, " more-cleanups-ok");
1894   if (lvl->have_cleanups)
1895     fprintf (stderr, " have-cleanups");
1896   fprintf (stderr, "\n");
1897   if (lvl->names)
1898     {
1899       fprintf (stderr, " names:\t");
1900       /* We can probably fit 3 names to a line?  */
1901       for (t = lvl->names; t; t = TREE_CHAIN (t))
1902         {
1903           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1904             continue;
1905           if (no_print_builtins
1906               && (TREE_CODE (t) == TYPE_DECL)
1907               && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1908             continue;
1909
1910           /* Function decls tend to have longer names.  */
1911           if (TREE_CODE (t) == FUNCTION_DECL)
1912             len = 3;
1913           else
1914             len = 2;
1915           i += len;
1916           if (i > 6)
1917             {
1918               fprintf (stderr, "\n\t");
1919               i = len;
1920             }
1921           print_node_brief (stderr, "", t, 0);
1922           if (t == error_mark_node)
1923             break;
1924         }
1925       if (i)
1926         fprintf (stderr, "\n");
1927     }
1928   if (lvl->tags)
1929     {
1930       fprintf (stderr, " tags:\t");
1931       i = 0;
1932       for (t = lvl->tags; t; t = TREE_CHAIN (t))
1933         {
1934           if (TREE_PURPOSE (t) == NULL_TREE)
1935             len = 3;
1936           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1937             len = 2;
1938           else
1939             len = 4;
1940           i += len;
1941           if (i > 5)
1942             {
1943               fprintf (stderr, "\n\t");
1944               i = len;
1945             }
1946           if (TREE_PURPOSE (t) == NULL_TREE)
1947             {
1948               print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1949               fprintf (stderr, ">");
1950             }
1951           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1952             print_node_brief (stderr, "", TREE_VALUE (t), 0);
1953           else
1954             {
1955               print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1956               print_node_brief (stderr, "", TREE_VALUE (t), 0);
1957               fprintf (stderr, ">");
1958             }
1959         }
1960       if (i)
1961         fprintf (stderr, "\n");
1962     }
1963   if (lvl->class_shadowed)
1964     {
1965       fprintf (stderr, " class-shadowed:");
1966       for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1967         {
1968           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1969         }
1970       fprintf (stderr, "\n");
1971     }
1972   if (lvl->type_shadowed)
1973     {
1974       fprintf (stderr, " type-shadowed:");
1975       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1976         {
1977           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1978         }
1979       fprintf (stderr, "\n");
1980     }
1981 }
1982
1983 void
1984 print_other_binding_stack (struct cp_binding_level *stack)
1985 {
1986   struct cp_binding_level *level;
1987   for (level = stack; level != global_binding_level; level = level->level_chain)
1988     {
1989       fprintf (stderr, "binding level ");
1990       fprintf (stderr, HOST_PTR_PRINTF, level);
1991       fprintf (stderr, "\n");
1992       print_binding_level (level);
1993     }
1994 }
1995
1996 void
1997 print_binding_stack (void)
1998 {
1999   struct cp_binding_level *b;
2000   fprintf (stderr, "current_binding_level=");
2001   fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2002   fprintf (stderr, "\nclass_binding_level=");
2003   fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2004   fprintf (stderr, "\nglobal_binding_level=");
2005   fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2006   fprintf (stderr, "\n");
2007   if (class_binding_level)
2008     {
2009       for (b = class_binding_level; b; b = b->level_chain)
2010         if (b == current_binding_level)
2011           break;
2012       if (b)
2013         b = class_binding_level;
2014       else
2015         b = current_binding_level;
2016     }
2017   else
2018     b = current_binding_level;
2019   print_other_binding_stack (b);
2020   fprintf (stderr, "global:\n");
2021   print_binding_level (global_binding_level);
2022 }
2023
2024 /* Namespace binding access routines: The namespace_bindings field of
2025    the identifier is polymorphic, with three possible values:
2026    NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2027    indicating the BINDING_VALUE of global_namespace.  */
2028
2029 /* Check whether the a binding for the name to scope is known.
2030    Assumes that the bindings of the name are already a list
2031    of bindings. Returns the binding found, or NULL_TREE.  */
2032
2033 static tree
2034 find_binding (tree name, tree scope)
2035 {
2036   tree iter, prev = NULL_TREE;
2037
2038   timevar_push (TV_NAME_LOOKUP);
2039   scope = ORIGINAL_NAMESPACE (scope);
2040
2041   for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2042        iter = TREE_CHAIN (iter))
2043     {
2044       my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2045       if (BINDING_SCOPE (iter) == scope)
2046         {
2047           /* Move binding found to the front of the list, so
2048              subsequent lookups will find it faster.  */
2049           if (prev)
2050             {
2051               TREE_CHAIN (prev) = TREE_CHAIN (iter);
2052               TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2053               IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2054             }
2055           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, iter);
2056         }
2057       prev = iter;
2058     }
2059   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2060 }
2061
2062 /* Always returns a binding for name in scope. If the
2063    namespace_bindings is not a list, convert it to one first.
2064    If no binding is found, make a new one.  */
2065
2066 tree
2067 binding_for_name (tree name, tree scope)
2068 {
2069   tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2070   tree result;
2071
2072   scope = ORIGINAL_NAMESPACE (scope);
2073
2074   if (b && TREE_CODE (b) != CPLUS_BINDING)
2075     {
2076       /* Get rid of optimization for global scope.  */
2077       IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2078       BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2079       b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2080     }
2081   if (b && (result = find_binding (name, scope)))
2082     return result;
2083   /* Not found, make a new one.  */
2084   result = make_node (CPLUS_BINDING);
2085   TREE_CHAIN (result) = b;
2086   IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2087   BINDING_SCOPE (result) = scope;
2088   BINDING_TYPE (result) = NULL_TREE;
2089   BINDING_VALUE (result) = NULL_TREE;
2090   return result;
2091 }
2092
2093 /* Return the binding value for name in scope, considering that
2094    namespace_binding may or may not be a list of CPLUS_BINDINGS.  */
2095
2096 tree
2097 namespace_binding (tree name, tree scope)
2098 {
2099   tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2100   if (b == NULL_TREE)
2101     return NULL_TREE;
2102   if (scope == NULL_TREE)
2103     scope = global_namespace;
2104   if (TREE_CODE (b) != CPLUS_BINDING)
2105     return (scope == global_namespace) ? b : NULL_TREE;
2106   name = find_binding (name,scope);
2107   if (name == NULL_TREE)
2108     return name;
2109   return BINDING_VALUE (name);
2110 }
2111
2112 /* Set the binding value for name in scope. If modifying the binding
2113    of global_namespace is attempted, try to optimize it.  */
2114
2115 void
2116 set_namespace_binding (tree name, tree scope, tree val)
2117 {
2118   tree b;
2119
2120   timevar_push (TV_NAME_LOOKUP);
2121   if (scope == NULL_TREE)
2122     scope = global_namespace;
2123
2124   if (scope == global_namespace)
2125     {
2126       b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2127       if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2128         {
2129           IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2130           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
2131         }
2132     }
2133   b = binding_for_name (name, scope);
2134   BINDING_VALUE (b) = val;
2135   timevar_pop (TV_NAME_LOOKUP);
2136 }
2137
2138 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2139    select a name that is unique to this compilation unit.  */
2140
2141 void
2142 push_namespace (tree name)
2143 {
2144   tree d = NULL_TREE;
2145   int need_new = 1;
2146   int implicit_use = 0;
2147   int global = 0;
2148
2149   timevar_push (TV_NAME_LOOKUP);
2150   
2151   if (!global_namespace)
2152     {
2153       /* This must be ::.  */
2154       my_friendly_assert (name == get_identifier ("::"), 377);
2155       global = 1;
2156     }
2157   else if (!name)
2158     {
2159       /* The name of anonymous namespace is unique for the translation
2160          unit.  */
2161       if (!anonymous_namespace_name)
2162         anonymous_namespace_name = get_file_function_name ('N');
2163       name = anonymous_namespace_name;
2164       d = IDENTIFIER_NAMESPACE_VALUE (name);
2165       if (d)
2166         /* Reopening anonymous namespace.  */
2167         need_new = 0;
2168       implicit_use = 1;
2169     }
2170   else
2171     {
2172       /* Check whether this is an extended namespace definition.  */
2173       d = IDENTIFIER_NAMESPACE_VALUE (name);
2174       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2175         {
2176           need_new = 0;
2177           if (DECL_NAMESPACE_ALIAS (d))
2178             {
2179               error ("namespace alias `%D' not allowed here, assuming `%D'",
2180                         d, DECL_NAMESPACE_ALIAS (d));
2181               d = DECL_NAMESPACE_ALIAS (d);
2182             }
2183         }
2184     }
2185
2186   if (need_new)
2187     {
2188       /* Make a new namespace, binding the name to it.  */
2189       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2190       /* The global namespace is not pushed, and the global binding
2191          level is set elsewhere.  */
2192       if (!global)
2193         {
2194           DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2195           d = pushdecl (d);
2196           pushlevel (0);
2197           declare_namespace_level ();
2198           NAMESPACE_LEVEL (d) = current_binding_level;
2199           VARRAY_TREE_INIT (current_binding_level->static_decls,
2200                             name != std_identifier ? 10 : 200,
2201                             "Static declarations");
2202         }
2203     }
2204   else
2205     resume_binding_level (NAMESPACE_LEVEL (d));
2206
2207   if (implicit_use)
2208     do_using_directive (d);
2209   /* Enter the name space.  */
2210   current_namespace = d;
2211
2212   timevar_pop (TV_NAME_LOOKUP);
2213 }
2214
2215 /* Pop from the scope of the current namespace.  */
2216
2217 void
2218 pop_namespace (void)
2219 {
2220   my_friendly_assert (current_namespace != global_namespace, 20010801);
2221   current_namespace = CP_DECL_CONTEXT (current_namespace);
2222   /* The binding level is not popped, as it might be re-opened later.  */
2223   suspend_binding_level ();
2224 }
2225
2226 /* Push into the scope of the namespace NS, even if it is deeply
2227    nested within another namespace.  */
2228
2229 void
2230 push_nested_namespace (tree ns)
2231 {
2232   if (ns == global_namespace)
2233     push_to_top_level ();
2234   else
2235     {
2236       push_nested_namespace (CP_DECL_CONTEXT (ns));
2237       push_namespace (DECL_NAME (ns));
2238     }
2239 }
2240
2241 /* Pop back from the scope of the namespace NS, which was previously
2242    entered with push_nested_namespace.  */
2243
2244 void
2245 pop_nested_namespace (tree ns)
2246 {
2247   timevar_push (TV_NAME_LOOKUP);
2248   while (ns != global_namespace)
2249     {
2250       pop_namespace ();
2251       ns = CP_DECL_CONTEXT (ns);
2252     }
2253
2254   pop_from_top_level ();
2255   timevar_pop (TV_NAME_LOOKUP);
2256 }
2257
2258 \f
2259 /* Allocate storage for saving a C++ binding.  */
2260 #define cxx_saved_binding_make() \
2261   (ggc_alloc (sizeof (cxx_saved_binding)))
2262
2263 struct cxx_saved_binding GTY(())
2264 {
2265   /* Link that chains saved C++ bindings for a given name into a stack.  */
2266   cxx_saved_binding *previous;
2267   /* The name of the current binding.  */
2268   tree identifier;
2269   /* The binding we're saving.  */
2270   tree binding;
2271   tree class_value;
2272   tree real_type_value;
2273 };
2274
2275 /* Subroutines for reverting temporarily to top-level for instantiation
2276    of templates and such.  We actually need to clear out the class- and
2277    local-value slots of all identifiers, so that only the global values
2278    are at all visible.  Simply setting current_binding_level to the global
2279    scope isn't enough, because more binding levels may be pushed.  */
2280 struct saved_scope *scope_chain;
2281
2282 static cxx_saved_binding *
2283 store_bindings (tree names, cxx_saved_binding *old_bindings)
2284 {
2285   tree t;
2286   cxx_saved_binding *search_bindings = old_bindings;
2287
2288   timevar_push (TV_NAME_LOOKUP);
2289   for (t = names; t; t = TREE_CHAIN (t))
2290     {
2291       tree id;
2292       cxx_saved_binding *saved;
2293       cxx_saved_binding *t1;
2294
2295       if (TREE_CODE (t) == TREE_LIST)
2296         id = TREE_PURPOSE (t);
2297       else
2298         id = DECL_NAME (t);
2299
2300       if (!id
2301           /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2302              we have no IDENTIFIER_BINDING if we have left the class
2303              scope, but cached the class-level declarations.  */
2304           || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2305         continue;
2306
2307       for (t1 = search_bindings; t1; t1 = t1->previous)
2308         if (t1->identifier == id)
2309           goto skip_it;
2310
2311       my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2312       saved = cxx_saved_binding_make ();
2313       saved->previous = old_bindings;
2314       saved->identifier = id;
2315       saved->binding = IDENTIFIER_BINDING (id);
2316       saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2317       saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2318       IDENTIFIER_BINDING (id) = NULL_TREE;
2319       IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2320       old_bindings = saved;
2321     skip_it:
2322       ;
2323     }
2324   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2325 }
2326
2327 void
2328 maybe_push_to_top_level (int pseudo)
2329 {
2330   struct saved_scope *s;
2331   struct cp_binding_level *b;
2332   cxx_saved_binding *old_bindings;
2333   int need_pop;
2334
2335   timevar_push (TV_NAME_LOOKUP);
2336   s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2337
2338   b = scope_chain ? current_binding_level : 0;
2339
2340   /* If we're in the middle of some function, save our state.  */
2341   if (cfun)
2342     {
2343       need_pop = 1;
2344       push_function_context_to (NULL_TREE);
2345     }
2346   else
2347     need_pop = 0;
2348
2349   old_bindings = NULL;
2350   if (scope_chain && previous_class_type)
2351     old_bindings = store_bindings (previous_class_values, old_bindings);
2352
2353   /* Have to include global_binding_level, because class-level decls
2354      aren't listed anywhere useful.  */
2355   for (; b; b = b->level_chain)
2356     {
2357       tree t;
2358
2359       /* Template IDs are inserted into the global level. If they were
2360          inserted into namespace level, finish_file wouldn't find them
2361          when doing pending instantiations. Therefore, don't stop at
2362          namespace level, but continue until :: .  */
2363       if (b == global_binding_level || (pseudo && b->template_parms_p))
2364         break;
2365
2366       old_bindings = store_bindings (b->names, old_bindings);
2367       /* We also need to check class_shadowed to save class-level type
2368          bindings, since pushclass doesn't fill in b->names.  */
2369       if (b->parm_flag == 2)
2370         old_bindings = store_bindings (b->class_shadowed, old_bindings);
2371
2372       /* Unwind type-value slots back to top level.  */
2373       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2374         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2375     }
2376   s->prev = scope_chain;
2377   s->old_bindings = old_bindings;
2378   s->bindings = b;
2379   s->need_pop_function_context = need_pop;
2380   s->function_decl = current_function_decl;
2381   s->last_parms = last_function_parms;
2382   s->check_access = flag_access_control;
2383
2384   scope_chain = s;
2385   current_function_decl = NULL_TREE;
2386   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2387   current_lang_name = lang_name_cplusplus;
2388   current_namespace = global_namespace;
2389   timevar_pop (TV_NAME_LOOKUP);
2390 }
2391
2392 void
2393 push_to_top_level (void)
2394 {
2395   maybe_push_to_top_level (0);
2396 }
2397
2398 void
2399 pop_from_top_level (void)
2400 {
2401   struct saved_scope *s = scope_chain;
2402   cxx_saved_binding *saved;
2403
2404   timevar_push (TV_NAME_LOOKUP); 
2405   /* Clear out class-level bindings cache.  */
2406   if (previous_class_type)
2407     invalidate_class_lookup_cache ();
2408
2409   current_lang_base = 0;
2410
2411   scope_chain = s->prev;
2412   for (saved = s->old_bindings; saved; saved = saved->previous)
2413     {
2414       tree id = saved->identifier;
2415
2416       IDENTIFIER_BINDING (id) = saved->binding;
2417       IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2418       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2419     }
2420
2421   /* If we were in the middle of compiling a function, restore our
2422      state.  */
2423   if (s->need_pop_function_context)
2424     pop_function_context_from (NULL_TREE);
2425   current_function_decl = s->function_decl;
2426   last_function_parms = s->last_parms;
2427   timevar_pop (TV_NAME_LOOKUP);
2428 }
2429 \f
2430 /* Push a definition of struct, union or enum tag "name".
2431    into binding_level "b".   "type" should be the type node,
2432    We assume that the tag "name" is not already defined.
2433
2434    Note that the definition may really be just a forward reference.
2435    In that case, the TYPE_SIZE will be a NULL_TREE.
2436
2437    C++ gratuitously puts all these tags in the name space.  */
2438
2439 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2440    record the shadowed value for this binding contour.  TYPE is
2441    the type that ID maps to.  */
2442
2443 static void
2444 set_identifier_type_value_with_scope (tree id, 
2445                                       tree type, 
2446                                       struct cp_binding_level* b)
2447 {
2448   if (!b->namespace_p)
2449     {
2450       /* Shadow the marker, not the real thing, so that the marker
2451          gets restored later.  */
2452       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2453       b->type_shadowed
2454         = tree_cons (id, old_type_value, b->type_shadowed);
2455     }
2456   else
2457     {
2458       tree binding = binding_for_name (id, current_namespace);
2459       BINDING_TYPE (binding) = type;
2460       /* Store marker instead of real type.  */
2461       type = global_type_node;
2462     }
2463   SET_IDENTIFIER_TYPE_VALUE (id, type);
2464 }
2465
2466 /* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2467
2468 void
2469 set_identifier_type_value (tree id, tree type)
2470 {
2471   set_identifier_type_value_with_scope (id, type, current_binding_level);
2472 }
2473
2474 /* Return the type associated with id.  */
2475
2476 tree
2477 identifier_type_value (tree id)
2478 {
2479   timevar_push (TV_NAME_LOOKUP);
2480   /* There is no type with that name, anywhere.  */
2481   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2482     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2483   /* This is not the type marker, but the real thing.  */
2484   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2485     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2486   /* Have to search for it. It must be on the global level, now.
2487      Ask lookup_name not to return non-types. */
2488   id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2489   if (id)
2490     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2491   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2492 }
2493
2494 /* Pop off extraneous binding levels left over due to syntax errors.
2495
2496    We don't pop past namespaces, as they might be valid.  */
2497
2498 void
2499 pop_everything (void)
2500 {
2501 #ifdef DEBUG_BINDING_LEVELS
2502   fprintf (stderr, "XXX entering pop_everything ()\n");
2503 #endif
2504   while (!toplevel_bindings_p ())
2505     {
2506       if (current_binding_level->parm_flag == 2)
2507         pop_nested_class ();
2508       else
2509         poplevel (0, 0, 0);
2510     }
2511 #ifdef DEBUG_BINDING_LEVELS
2512   fprintf (stderr, "XXX leaving pop_everything ()\n");
2513 #endif
2514 }
2515
2516 /* The type TYPE is being declared.  If it is a class template, or a
2517    specialization of a class template, do any processing required and
2518    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2519    being declared a friend.  B is the binding level at which this TYPE
2520    should be bound.
2521
2522    Returns the TYPE_DECL for TYPE, which may have been altered by this
2523    processing.  */
2524
2525 static tree
2526 maybe_process_template_type_declaration (tree type, 
2527                                          int globalize, 
2528                                          struct cp_binding_level* b)
2529 {
2530   tree decl = TYPE_NAME (type);
2531
2532   if (processing_template_parmlist)
2533     /* You can't declare a new template type in a template parameter
2534        list.  But, you can declare a non-template type:
2535
2536          template <class A*> struct S;
2537
2538        is a forward-declaration of `A'.  */
2539     ;
2540   else
2541     {
2542       maybe_check_template_type (type);
2543
2544       my_friendly_assert (IS_AGGR_TYPE (type)
2545                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2546
2547
2548       if (processing_template_decl)
2549         {
2550           /* This may change after the call to
2551              push_template_decl_real, but we want the original value.  */
2552           tree name = DECL_NAME (decl);
2553
2554           decl = push_template_decl_real (decl, globalize);
2555           /* If the current binding level is the binding level for the
2556              template parameters (see the comment in
2557              begin_template_parm_list) and the enclosing level is a class
2558              scope, and we're not looking at a friend, push the
2559              declaration of the member class into the class scope.  In the
2560              friend case, push_template_decl will already have put the
2561              friend into global scope, if appropriate.  */
2562           if (TREE_CODE (type) != ENUMERAL_TYPE
2563               && !globalize && b->template_parms_p
2564               && b->level_chain->parm_flag == 2)
2565             {
2566               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2567               /* Put this tag on the list of tags for the class, since
2568                  that won't happen below because B is not the class
2569                  binding level, but is instead the pseudo-global level.  */
2570               b->level_chain->tags =
2571                 tree_cons (name, type, b->level_chain->tags);
2572               if (!COMPLETE_TYPE_P (current_class_type))
2573                 {
2574                   maybe_add_class_template_decl_list (current_class_type,
2575                                                       type, /*friend_p=*/0);
2576                   CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2577                 }
2578             }
2579         }
2580     }
2581
2582   return decl;
2583 }
2584
2585 /* In C++, you don't have to write `struct S' to refer to `S'; you
2586    can just use `S'.  We accomplish this by creating a TYPE_DECL as
2587    if the user had written `typedef struct S S'.  Create and return
2588    the TYPE_DECL for TYPE.  */
2589
2590 tree
2591 create_implicit_typedef (tree name, tree type)
2592 {
2593   tree decl;
2594
2595   decl = build_decl (TYPE_DECL, name, type);
2596   DECL_ARTIFICIAL (decl) = 1;
2597   /* There are other implicit type declarations, like the one *within*
2598      a class that allows you to write `S::S'.  We must distinguish
2599      amongst these.  */
2600   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2601   TYPE_NAME (type) = decl;
2602
2603   return decl;
2604 }
2605
2606 /* Remember a local name for name-mangling purposes.  */
2607
2608 static void
2609 push_local_name (tree decl)
2610 {
2611   size_t i, nelts;
2612   tree t, name;
2613
2614   timevar_push (TV_NAME_LOOKUP);
2615   if (!local_names)
2616     VARRAY_TREE_INIT (local_names, 8, "local_names");
2617
2618   name = DECL_NAME (decl);
2619
2620   nelts = VARRAY_ACTIVE_SIZE (local_names);
2621   for (i = 0; i < nelts; i++)
2622     {
2623       t = VARRAY_TREE (local_names, i);
2624       if (DECL_NAME (t) == name)
2625         {
2626           if (!DECL_LANG_SPECIFIC (decl))
2627             retrofit_lang_decl (decl);
2628           DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2629           if (DECL_LANG_SPECIFIC (t))
2630             DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2631           else
2632             DECL_DISCRIMINATOR (decl) = 1;
2633
2634           VARRAY_TREE (local_names, i) = decl;
2635           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
2636         }
2637     }
2638
2639   VARRAY_PUSH_TREE (local_names, decl);
2640   timevar_pop (TV_NAME_LOOKUP);
2641 }
2642
2643 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2644    Normally put it into the inner-most non-tag-transparent scope,
2645    but if GLOBALIZE is true, put it in the inner-most non-class scope.
2646    The latter is needed for implicit declarations.  */
2647
2648 void
2649 pushtag (tree name, tree type, int globalize)
2650 {
2651   register struct cp_binding_level *b;
2652
2653   timevar_push (TV_NAME_LOOKUP);
2654   b = current_binding_level;
2655   while (b->tag_transparent
2656          || (b->parm_flag == 2
2657              && (globalize
2658                  /* We may be defining a new type in the initializer
2659                     of a static member variable. We allow this when
2660                     not pedantic, and it is particularly useful for
2661                     type punning via an anonymous union.  */
2662                  || COMPLETE_TYPE_P (b->this_class))))
2663     b = b->level_chain;
2664
2665   b->tags = tree_cons (name, type, b->tags);
2666
2667   if (name)
2668     {
2669       /* Do C++ gratuitous typedefing.  */
2670       if (IDENTIFIER_TYPE_VALUE (name) != type)
2671         {
2672           register tree d = NULL_TREE;
2673           int in_class = 0;
2674           tree context = TYPE_CONTEXT (type);
2675
2676           if (! context)
2677             {
2678               tree cs = current_scope ();
2679
2680               if (! globalize)
2681                 context = cs;
2682               else if (cs != NULL_TREE && TYPE_P (cs))
2683                 /* When declaring a friend class of a local class, we want
2684                    to inject the newly named class into the scope
2685                    containing the local class, not the namespace scope.  */
2686                 context = decl_function_context (get_type_decl (cs));
2687             }
2688           if (!context)
2689             context = current_namespace;
2690
2691           if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2692               || b->parm_flag == 2)
2693             in_class = 1;
2694
2695           if (current_lang_name == lang_name_java)
2696             TYPE_FOR_JAVA (type) = 1;
2697
2698           d = create_implicit_typedef (name, type);
2699           DECL_CONTEXT (d) = FROB_CONTEXT (context);
2700           if (! in_class)
2701             set_identifier_type_value_with_scope (name, type, b);
2702
2703           d = maybe_process_template_type_declaration (type,
2704                                                        globalize, b);
2705
2706           if (b->parm_flag == 2)
2707             {
2708               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2709                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2710                    class.  But if it's a member template class, we
2711                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
2712                    is done later.  */
2713                 finish_member_declaration (d);
2714               else
2715                 pushdecl_class_level (d);
2716             }
2717           else
2718             d = pushdecl_with_scope (d, b);
2719
2720           /* FIXME what if it gets a name from typedef?  */
2721           if (ANON_AGGRNAME_P (name))
2722             DECL_IGNORED_P (d) = 1;
2723
2724           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2725
2726           /* If this is a local class, keep track of it.  We need this
2727              information for name-mangling, and so that it is possible to find
2728              all function definitions in a translation unit in a convenient
2729              way.  (It's otherwise tricky to find a member function definition
2730              it's only pointed to from within a local class.)  */
2731           if (TYPE_CONTEXT (type)
2732               && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2733               && !processing_template_decl)
2734             VARRAY_PUSH_TREE (local_classes, type);
2735         }
2736       if (b->parm_flag == 2)
2737         {
2738           if (!COMPLETE_TYPE_P (current_class_type))
2739             {
2740               maybe_add_class_template_decl_list (current_class_type,
2741                                                   type, /*friend_p=*/0);
2742               CLASSTYPE_TAGS (current_class_type) = b->tags;
2743             }
2744         }
2745     }
2746
2747   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2748     /* Use the canonical TYPE_DECL for this node.  */
2749     TYPE_STUB_DECL (type) = TYPE_NAME (type);
2750   else
2751     {
2752       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2753          will be the tagged type we just added to the current
2754          binding level.  This fake NULL-named TYPE_DECL node helps
2755          dwarfout.c to know when it needs to output a
2756          representation of a tagged type, and it also gives us a
2757          convenient place to record the "scope start" address for
2758          the tagged type.  */
2759
2760       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2761       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2762     }
2763   timevar_pop (TV_NAME_LOOKUP);
2764 }
2765
2766 /* Counter used to create anonymous type names.  */
2767
2768 static GTY(()) int anon_cnt;
2769
2770 /* Return an IDENTIFIER which can be used as a name for
2771    anonymous structs and unions.  */
2772
2773 tree
2774 make_anon_name (void)
2775 {
2776   char buf[32];
2777
2778   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2779   return get_identifier (buf);
2780 }
2781
2782 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2783    This keeps dbxout from getting confused.  */
2784
2785 void
2786 clear_anon_tags (void)
2787 {
2788   register struct cp_binding_level *b;
2789   register tree tags;
2790   static int last_cnt = 0;
2791
2792   /* Fast out if no new anon names were declared.  */
2793   if (last_cnt == anon_cnt)
2794     return;
2795
2796   b = current_binding_level;
2797   while (b->tag_transparent)
2798     b = b->level_chain;
2799   tags = b->tags;
2800   while (tags)
2801     {
2802       /* A NULL purpose means we have already processed all tags
2803          from here to the end of the list.  */
2804       if (TREE_PURPOSE (tags) == NULL_TREE)
2805         break;
2806       if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2807         TREE_PURPOSE (tags) = NULL_TREE;
2808       tags = TREE_CHAIN (tags);
2809     }
2810   last_cnt = anon_cnt;
2811 }
2812 \f
2813 /* Subroutine of duplicate_decls: return truthvalue of whether
2814    or not types of these decls match.
2815
2816    For C++, we must compare the parameter list so that `int' can match
2817    `int&' in a parameter position, but `int&' is not confused with
2818    `const int&'.  */
2819
2820 int
2821 decls_match (tree newdecl, tree olddecl)
2822 {
2823   int types_match;
2824
2825   if (newdecl == olddecl)
2826     return 1;
2827
2828   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2829     /* If the two DECLs are not even the same kind of thing, we're not
2830        interested in their types.  */
2831     return 0;
2832
2833   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2834     {
2835       tree f1 = TREE_TYPE (newdecl);
2836       tree f2 = TREE_TYPE (olddecl);
2837       tree p1 = TYPE_ARG_TYPES (f1);
2838       tree p2 = TYPE_ARG_TYPES (f2);
2839
2840       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2841           && ! (DECL_EXTERN_C_P (newdecl)
2842                 && DECL_EXTERN_C_P (olddecl)))
2843         return 0;
2844
2845       if (TREE_CODE (f1) != TREE_CODE (f2))
2846         return 0;
2847
2848       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2849         {
2850           if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2851               && (DECL_BUILT_IN (olddecl)
2852 #ifndef NO_IMPLICIT_EXTERN_C
2853                   || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2854                   || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2855 #endif
2856               ))
2857             {
2858               types_match = self_promoting_args_p (p1);
2859               if (p1 == void_list_node)
2860                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2861             }
2862 #ifndef NO_IMPLICIT_EXTERN_C
2863           else if (p1 == NULL_TREE
2864                    && (DECL_EXTERN_C_P (olddecl)
2865                        && DECL_IN_SYSTEM_HEADER (olddecl)
2866                        && !DECL_CLASS_SCOPE_P (olddecl))
2867                    && (DECL_EXTERN_C_P (newdecl)
2868                        && DECL_IN_SYSTEM_HEADER (newdecl)
2869                        && !DECL_CLASS_SCOPE_P (newdecl)))
2870             {
2871               types_match = self_promoting_args_p (p2);
2872               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2873             }
2874 #endif
2875           else
2876             types_match = compparms (p1, p2);
2877         }
2878       else
2879         types_match = 0;
2880     }
2881   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2882     {
2883       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2884                                 DECL_TEMPLATE_PARMS (olddecl)))
2885         return 0;
2886
2887       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2888           != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2889         return 0;
2890
2891       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2892         types_match = 1;
2893       else
2894         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2895                                    DECL_TEMPLATE_RESULT (newdecl));
2896     }
2897   else
2898     {
2899       if (TREE_TYPE (newdecl) == error_mark_node)
2900         types_match = TREE_TYPE (olddecl) == error_mark_node;
2901       else if (TREE_TYPE (olddecl) == NULL_TREE)
2902         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2903       else if (TREE_TYPE (newdecl) == NULL_TREE)
2904         types_match = 0;
2905       else
2906         types_match = comptypes (TREE_TYPE (newdecl),
2907                                  TREE_TYPE (olddecl),
2908                                  COMPARE_REDECLARATION);
2909     }
2910
2911   return types_match;
2912 }
2913
2914 /* If NEWDECL is `static' and an `extern' was seen previously,
2915    warn about it.  OLDDECL is the previous declaration.
2916
2917    Note that this does not apply to the C++ case of declaring
2918    a variable `extern const' and then later `const'.
2919
2920    Don't complain about built-in functions, since they are beyond
2921    the user's control.  */
2922
2923 static void
2924 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2925 {
2926   static const char *const explicit_extern_static_warning
2927     = "`%D' was declared `extern' and later `static'";
2928   static const char *const implicit_extern_static_warning
2929     = "`%D' was declared implicitly `extern' and later `static'";
2930
2931   tree name;
2932
2933   if (TREE_CODE (newdecl) == TYPE_DECL
2934       || TREE_CODE (newdecl) == TEMPLATE_DECL
2935       || TREE_CODE (newdecl) == CONST_DECL)
2936     return;
2937
2938   /* Don't get confused by static member functions; that's a different
2939      use of `static'.  */
2940   if (TREE_CODE (newdecl) == FUNCTION_DECL
2941       && DECL_STATIC_FUNCTION_P (newdecl))
2942     return;
2943
2944   /* If the old declaration was `static', or the new one isn't, then
2945      then everything is OK.  */
2946   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2947     return;
2948
2949   /* It's OK to declare a builtin function as `static'.  */
2950   if (TREE_CODE (olddecl) == FUNCTION_DECL
2951       && DECL_ARTIFICIAL (olddecl))
2952     return;
2953
2954   name = DECL_ASSEMBLER_NAME (newdecl);
2955   pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2956               ? implicit_extern_static_warning
2957               : explicit_extern_static_warning, newdecl);
2958   cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2959 }
2960
2961 /* Handle when a new declaration NEWDECL has the same name as an old
2962    one OLDDECL in the same binding contour.  Prints an error message
2963    if appropriate.
2964
2965    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2966    Otherwise, return 0.  */
2967
2968 int
2969 duplicate_decls (tree newdecl, tree olddecl)
2970 {
2971   unsigned olddecl_uid = DECL_UID (olddecl);
2972   int olddecl_friend = 0, types_match = 0;
2973   int new_defines_function = 0;
2974
2975   if (newdecl == olddecl)
2976     return 1;
2977
2978   types_match = decls_match (newdecl, olddecl);
2979
2980   /* If either the type of the new decl or the type of the old decl is an
2981      error_mark_node, then that implies that we have already issued an
2982      error (earlier) for some bogus type specification, and in that case,
2983      it is rather pointless to harass the user with yet more error message
2984      about the same declaration, so just pretend the types match here.  */
2985   if (TREE_TYPE (newdecl) == error_mark_node
2986       || TREE_TYPE (olddecl) == error_mark_node)
2987     types_match = 1;
2988
2989   if (DECL_P (olddecl)
2990       && TREE_CODE (newdecl) == FUNCTION_DECL
2991       && TREE_CODE (olddecl) == FUNCTION_DECL
2992       && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2993     {
2994       if (DECL_DECLARED_INLINE_P (newdecl)
2995           && DECL_UNINLINABLE (newdecl)
2996           && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2997         /* Already warned elsewhere.  */;
2998       else if (DECL_DECLARED_INLINE_P (olddecl)
2999                && DECL_UNINLINABLE (olddecl)
3000                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
3001         /* Already warned.  */;
3002       else if (DECL_DECLARED_INLINE_P (newdecl)
3003                && DECL_UNINLINABLE (olddecl)
3004                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
3005         {
3006           warning ("%Hfunction '%D' redeclared as inline",
3007                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
3008           warning ("%Hprevious declaration of '%D' with attribute noinline",
3009                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
3010         }
3011       else if (DECL_DECLARED_INLINE_P (olddecl)
3012                && DECL_UNINLINABLE (newdecl)
3013                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
3014         {
3015           warning ("%Hfunction '%D' redeclared with attribute noinline",
3016                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
3017           warning ("%Hprevious declaration of '%D' was inline",
3018                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
3019         }
3020     }
3021
3022   /* Check for redeclaration and other discrepancies.  */
3023   if (TREE_CODE (olddecl) == FUNCTION_DECL
3024       && DECL_ARTIFICIAL (olddecl))
3025     {
3026       if (TREE_CODE (newdecl) != FUNCTION_DECL)
3027         {
3028           /* Avoid warnings redeclaring anticipated built-ins.  */
3029           if (DECL_ANTICIPATED (olddecl))
3030             return 0;
3031
3032           /* If you declare a built-in or predefined function name as static,
3033              the old definition is overridden, but optionally warn this was a
3034              bad choice of name.  */
3035           if (! TREE_PUBLIC (newdecl))
3036             {
3037               if (warn_shadow)
3038                 warning ("shadowing %s function `%#D'",
3039                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3040                             olddecl);
3041               /* Discard the old built-in function.  */
3042               return 0;
3043             }
3044           /* If the built-in is not ansi, then programs can override
3045              it even globally without an error.  */
3046           else if (! DECL_BUILT_IN (olddecl))
3047             warning ("library function `%#D' redeclared as non-function `%#D'",
3048                         olddecl, newdecl);
3049           else
3050             {
3051               error ("declaration of `%#D'", newdecl);
3052               error ("conflicts with built-in declaration `%#D'",
3053                         olddecl);
3054             }
3055           return 0;
3056         }
3057       else if (!types_match)
3058         {
3059           /* Avoid warnings redeclaring anticipated built-ins.  */
3060           if (DECL_ANTICIPATED (olddecl))
3061             ;  /* Do nothing yet.  */
3062           else if ((DECL_EXTERN_C_P (newdecl)
3063                && DECL_EXTERN_C_P (olddecl))
3064               || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3065                             TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3066             {
3067               /* A near match; override the builtin.  */
3068
3069               if (TREE_PUBLIC (newdecl))
3070                 {
3071                   warning ("new declaration `%#D'", newdecl);
3072                   warning ("ambiguates built-in declaration `%#D'",
3073                               olddecl);
3074                 }
3075               else if (warn_shadow)
3076                 warning ("shadowing %s function `%#D'",
3077                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3078                             olddecl);
3079             }
3080           else
3081             /* Discard the old built-in function.  */
3082             return 0;
3083
3084           /* Replace the old RTL to avoid problems with inlining.  */
3085           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3086         }
3087
3088       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3089         {
3090           /* If a builtin function is redeclared as `static', merge
3091              the declarations, but make the original one static.  */
3092           DECL_THIS_STATIC (olddecl) = 1;
3093           TREE_PUBLIC (olddecl) = 0;
3094
3095           /* Make the old declaration consistent with the new one so
3096              that all remnants of the builtin-ness of this function
3097              will be banished.  */
3098           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3099           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3100         }
3101     }
3102   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3103     {
3104       if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3105            && TREE_CODE (newdecl) != TYPE_DECL
3106            && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3107                  && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3108           || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3109               && TREE_CODE (olddecl) != TYPE_DECL
3110               && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3111                     && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3112                         == TYPE_DECL))))
3113         {
3114           /* We do nothing special here, because C++ does such nasty
3115              things with TYPE_DECLs.  Instead, just let the TYPE_DECL
3116              get shadowed, and know that if we need to find a TYPE_DECL
3117              for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3118              slot of the identifier.  */
3119           return 0;
3120         }
3121
3122       if ((TREE_CODE (newdecl) == FUNCTION_DECL
3123            && DECL_FUNCTION_TEMPLATE_P (olddecl))
3124           || (TREE_CODE (olddecl) == FUNCTION_DECL
3125               && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3126         return 0;
3127
3128       error ("`%#D' redeclared as different kind of symbol", newdecl);
3129       if (TREE_CODE (olddecl) == TREE_LIST)
3130         olddecl = TREE_VALUE (olddecl);
3131       cp_error_at ("previous declaration of `%#D'", olddecl);
3132
3133       /* New decl is completely inconsistent with the old one =>
3134          tell caller to replace the old one.  */
3135
3136       return 0;
3137     }
3138   else if (!types_match)
3139     {
3140       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3141         /* These are certainly not duplicate declarations; they're
3142            from different scopes.  */
3143         return 0;
3144
3145       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3146         {
3147           /* The name of a class template may not be declared to refer to
3148              any other template, class, function, object, namespace, value,
3149              or type in the same scope.  */
3150           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3151               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3152             {
3153               error ("declaration of template `%#D'", newdecl);
3154               cp_error_at ("conflicts with previous declaration `%#D'",
3155                            olddecl);
3156             }
3157           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3158                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3159                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3160                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3161                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3162                                            DECL_TEMPLATE_PARMS (olddecl))
3163                    /* Template functions can be disambiguated by
3164                       return type.  */
3165                    && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3166                                    TREE_TYPE (TREE_TYPE (olddecl))))
3167             {
3168               error ("new declaration `%#D'", newdecl);
3169               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3170             }
3171           return 0;
3172         }
3173       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3174         {
3175           if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3176             {
3177               error ("declaration of C function `%#D' conflicts with",
3178                         newdecl);
3179               cp_error_at ("previous declaration `%#D' here", olddecl);
3180             }
3181           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3182                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3183             {
3184               error ("new declaration `%#D'", newdecl);
3185               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3186             }
3187           else
3188             return 0;
3189         }
3190
3191       /* Already complained about this, so don't do so again.  */
3192       else if (current_class_type == NULL_TREE
3193           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3194         {
3195           error ("conflicting types for `%#D'", newdecl);
3196           cp_error_at ("previous declaration as `%#D'", olddecl);
3197         }
3198     }
3199   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3200             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3201                  && (!DECL_TEMPLATE_INFO (newdecl)
3202                      || (DECL_TI_TEMPLATE (newdecl)
3203                          != DECL_TI_TEMPLATE (olddecl))))
3204                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3205                     && (!DECL_TEMPLATE_INFO (olddecl)
3206                         || (DECL_TI_TEMPLATE (olddecl)
3207                             != DECL_TI_TEMPLATE (newdecl))))))
3208     /* It's OK to have a template specialization and a non-template
3209        with the same type, or to have specializations of two
3210        different templates with the same type.  Note that if one is a
3211        specialization, and the other is an instantiation of the same
3212        template, that we do not exit at this point.  That situation
3213        can occur if we instantiate a template class, and then
3214        specialize one of its methods.  This situation is valid, but
3215        the declarations must be merged in the usual way.  */
3216     return 0;
3217   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3218            && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3219                 && !DECL_USE_TEMPLATE (newdecl))
3220                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3221                    && !DECL_USE_TEMPLATE (olddecl))))
3222     /* One of the declarations is a template instantiation, and the
3223        other is not a template at all.  That's OK.  */
3224     return 0;
3225   else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3226            && DECL_NAMESPACE_ALIAS (newdecl)
3227            && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3228     /* Redeclaration of namespace alias, ignore it.  */
3229     return 1;
3230   else
3231     {
3232       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3233       if (errmsg)
3234         {
3235           error (errmsg, newdecl);
3236           if (DECL_NAME (olddecl) != NULL_TREE)
3237             cp_error_at ((DECL_INITIAL (olddecl)
3238                           && namespace_bindings_p ())
3239                          ? "`%#D' previously defined here"
3240                          : "`%#D' previously declared here", olddecl);
3241           return 0;
3242         }
3243       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3244                && DECL_INITIAL (olddecl) != NULL_TREE
3245                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3246                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3247         {
3248           /* Prototype decl follows defn w/o prototype.  */
3249           cp_warning_at ("prototype for `%#D'", newdecl);
3250           cp_warning_at ("follows non-prototype definition here", olddecl);
3251         }
3252       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3253                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3254         {
3255           /* extern "C" int foo ();
3256              int foo () { bar (); }
3257              is OK.  */
3258           if (current_lang_depth () == 0)
3259             SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3260           else
3261             {
3262               cp_error_at ("previous declaration of `%#D' with %L linkage",
3263                            olddecl, DECL_LANGUAGE (olddecl));
3264               error ("conflicts with new declaration with %L linkage",
3265                         DECL_LANGUAGE (newdecl));
3266             }
3267         }
3268
3269       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3270         ;
3271       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3272         {
3273           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3274           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3275           int i = 1;
3276
3277           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3278             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3279
3280           for (; t1 && t1 != void_list_node;
3281                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3282             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3283               {
3284                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3285                                            TREE_PURPOSE (t2)))
3286                   {
3287                     pedwarn ("default argument given for parameter %d of `%#D'",
3288                              i, newdecl);
3289                     cp_pedwarn_at ("after previous specification in `%#D'",
3290                                    olddecl);
3291                   }
3292                 else
3293                   {
3294                     error ("default argument given for parameter %d of `%#D'",
3295                               i, newdecl);
3296                     cp_error_at ("after previous specification in `%#D'",
3297                                  olddecl);
3298                   }
3299               }
3300
3301           if (DECL_DECLARED_INLINE_P (newdecl) 
3302               && ! DECL_DECLARED_INLINE_P (olddecl)
3303               && TREE_ADDRESSABLE (olddecl) && warn_inline)
3304             {
3305               warning ("`%#D' was used before it was declared inline",
3306                           newdecl);
3307               cp_warning_at ("previous non-inline declaration here",
3308                              olddecl);
3309             }
3310         }
3311     }
3312
3313   /* Do not merge an implicit typedef with an explicit one.  In:
3314
3315        class A;
3316        ...
3317        typedef class A A __attribute__ ((foo));
3318
3319      the attribute should apply only to the typedef.  */
3320   if (TREE_CODE (olddecl) == TYPE_DECL
3321       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3322           || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3323     return 0;
3324
3325   /* If new decl is `static' and an `extern' was seen previously,
3326      warn about it.  */
3327   warn_extern_redeclared_static (newdecl, olddecl);
3328
3329   /* We have committed to returning 1 at this point.  */
3330   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3331     {
3332       /* Now that functions must hold information normally held
3333          by field decls, there is extra work to do so that
3334          declaration information does not get destroyed during
3335          definition.  */
3336       if (DECL_VINDEX (olddecl))
3337         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3338       if (DECL_CONTEXT (olddecl))
3339         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3340       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3341       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3342       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3343       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3344       DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3345       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3346       if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3347         SET_OVERLOADED_OPERATOR_CODE
3348           (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3349       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3350
3351       /* Optionally warn about more than one declaration for the same
3352          name, but don't warn about a function declaration followed by a
3353          definition.  */
3354       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3355           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3356           /* Don't warn about extern decl followed by definition.  */
3357           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3358           /* Don't warn about friends, let add_friend take care of it.  */
3359           && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3360         {
3361           warning ("redundant redeclaration of `%D' in same scope", newdecl);
3362           cp_warning_at ("previous declaration of `%D'", olddecl);
3363         }
3364     }
3365
3366   /* Deal with C++: must preserve virtual function table size.  */
3367   if (TREE_CODE (olddecl) == TYPE_DECL)
3368     {
3369       register tree newtype = TREE_TYPE (newdecl);
3370       register tree oldtype = TREE_TYPE (olddecl);
3371
3372       if (newtype != error_mark_node && oldtype != error_mark_node
3373           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3374         CLASSTYPE_FRIEND_CLASSES (newtype)
3375           = CLASSTYPE_FRIEND_CLASSES (oldtype);
3376 \
3377       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3378     }
3379
3380   /* Copy all the DECL_... slots specified in the new decl
3381      except for any that we copy here from the old type.  */
3382   DECL_ATTRIBUTES (newdecl)
3383     = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3384
3385   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3386     {
3387       TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3388       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3389         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3390                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3391
3392       /* If the new declaration is a definition, update the file and
3393          line information on the declaration.  */
3394       if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3395           && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3396         {
3397           DECL_SOURCE_LOCATION (olddecl) 
3398             = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3399             = DECL_SOURCE_LOCATION (newdecl);
3400         }
3401
3402       return 1;
3403     }
3404
3405   if (types_match)
3406     {
3407       /* Automatically handles default parameters.  */
3408       tree oldtype = TREE_TYPE (olddecl);
3409       tree newtype;
3410
3411       /* Merge the data types specified in the two decls.  */
3412       newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3413
3414       /* If merge_types produces a non-typedef type, just use the old type.  */
3415       if (TREE_CODE (newdecl) == TYPE_DECL
3416           && newtype == DECL_ORIGINAL_TYPE (newdecl))
3417         newtype = oldtype;
3418
3419       if (TREE_CODE (newdecl) == VAR_DECL)
3420         {
3421           DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3422           DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3423         }
3424
3425       /* Do this after calling `merge_types' so that default
3426          parameters don't confuse us.  */
3427       else if (TREE_CODE (newdecl) == FUNCTION_DECL
3428           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3429               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3430         {
3431           TREE_TYPE (newdecl) = build_exception_variant (newtype,
3432                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3433           TREE_TYPE (olddecl) = build_exception_variant (newtype,
3434                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
3435
3436           if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3437               && DECL_SOURCE_LINE (olddecl) != 0
3438               && flag_exceptions
3439               && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3440                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3441             {
3442               error ("declaration of `%F' throws different exceptions",
3443                         newdecl);
3444               cp_error_at ("than previous declaration `%F'", olddecl);
3445             }
3446         }
3447       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3448
3449       /* Lay the type out, unless already done.  */
3450       if (! same_type_p (newtype, oldtype)
3451           && TREE_TYPE (newdecl) != error_mark_node
3452           && !(processing_template_decl && uses_template_parms (newdecl)))
3453         layout_type (TREE_TYPE (newdecl));
3454
3455       if ((TREE_CODE (newdecl) == VAR_DECL
3456            || TREE_CODE (newdecl) == PARM_DECL
3457            || TREE_CODE (newdecl) == RESULT_DECL
3458            || TREE_CODE (newdecl) == FIELD_DECL
3459            || TREE_CODE (newdecl) == TYPE_DECL)
3460           && !(processing_template_decl && uses_template_parms (newdecl)))
3461         layout_decl (newdecl, 0);
3462
3463       /* Merge the type qualifiers.  */
3464       if (TREE_READONLY (newdecl))
3465         TREE_READONLY (olddecl) = 1;
3466       if (TREE_THIS_VOLATILE (newdecl))
3467         TREE_THIS_VOLATILE (olddecl) = 1;
3468
3469       /* Merge the initialization information.  */
3470       if (DECL_INITIAL (newdecl) == NULL_TREE
3471           && DECL_INITIAL (olddecl) != NULL_TREE)
3472         {
3473           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3474           DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3475           if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3476               && DECL_LANG_SPECIFIC (newdecl)
3477               && DECL_LANG_SPECIFIC (olddecl))
3478             DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3479         }
3480
3481       /* Merge the section attribute.
3482          We want to issue an error if the sections conflict but that must be
3483          done later in decl_attributes since we are called before attributes
3484          are assigned.  */
3485       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3486         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3487
3488       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3489         {
3490           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3491             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3492           DECL_NO_LIMIT_STACK (newdecl)
3493             |= DECL_NO_LIMIT_STACK (olddecl);
3494           /* Keep the old RTL.  */
3495           COPY_DECL_RTL (olddecl, newdecl);
3496         }
3497       else if (TREE_CODE (newdecl) == VAR_DECL 
3498                && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3499         {
3500           /* Keep the old RTL.  We cannot keep the old RTL if the old
3501              declaration was for an incomplete object and the new
3502              declaration is not since many attributes of the RTL will
3503              change.  */
3504           COPY_DECL_RTL (olddecl, newdecl);
3505         }
3506     }
3507   /* If cannot merge, then use the new type and qualifiers,
3508      and don't preserve the old rtl.  */
3509   else
3510     {
3511       /* Clean out any memory we had of the old declaration.  */
3512       tree oldstatic = value_member (olddecl, static_aggregates);
3513       if (oldstatic)
3514         TREE_VALUE (oldstatic) = error_mark_node;
3515
3516       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3517       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3518       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3519       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3520     }
3521
3522   /* Merge the storage class information.  */
3523   merge_weak (newdecl, olddecl);
3524
3525   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3526   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3527   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3528   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3529   if (! DECL_EXTERNAL (olddecl))
3530     DECL_EXTERNAL (newdecl) = 0;
3531
3532   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3533     {
3534       DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3535       DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3536       DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3537       DECL_TEMPLATE_INSTANTIATED (newdecl)
3538         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3539       /* Don't really know how much of the language-specific
3540          values we should copy from old to new.  */
3541       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3542       DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 
3543         DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3544       DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3545       DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3546       DECL_INITIALIZED_IN_CLASS_P (newdecl)
3547         |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3548       olddecl_friend = DECL_FRIEND_P (olddecl);
3549
3550       /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3551       if (TREE_CODE (newdecl) == FUNCTION_DECL
3552           || DECL_FUNCTION_TEMPLATE_P (newdecl))
3553         {
3554           DECL_BEFRIENDING_CLASSES (newdecl)
3555             = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3556                        DECL_BEFRIENDING_CLASSES (olddecl));
3557           /* DECL_THUNKS is only valid for virtual functions,
3558              otherwise it is a DECL_FRIEND_CONTEXT.  */
3559           if (DECL_VIRTUAL_P (newdecl))
3560             DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3561         }
3562     }
3563
3564   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3565     {
3566       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3567           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3568         {
3569           /* If newdecl is not a specialization, then it is not a
3570              template-related function at all.  And that means that we
3571              should have exited above, returning 0.  */
3572           my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3573                               0);
3574
3575           if (TREE_USED (olddecl))
3576             /* From [temp.expl.spec]:
3577
3578                If a template, a member template or the member of a class
3579                template is explicitly specialized then that
3580                specialization shall be declared before the first use of
3581                that specialization that would cause an implicit
3582                instantiation to take place, in every translation unit in
3583                which such a use occurs.  */
3584             error ("explicit specialization of %D after first use",
3585                       olddecl);
3586
3587           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3588
3589           /* [temp.expl.spec/14] We don't inline explicit specialization
3590              just because the primary template says so.  */
3591         }
3592       else
3593         {
3594           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3595             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3596
3597           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3598
3599           /* If either decl says `inline', this fn is inline, unless 
3600              its definition was passed already.  */
3601           if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3602             DECL_INLINE (olddecl) = 1;
3603           DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3604
3605           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3606             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3607         }
3608
3609       /* Preserve abstractness on cloned [cd]tors.  */
3610       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3611
3612       if (! types_match)
3613         {
3614           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3615           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3616           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3617         }
3618       if (! types_match || new_defines_function)
3619         {
3620           /* These need to be copied so that the names are available.
3621              Note that if the types do match, we'll preserve inline
3622              info and other bits, but if not, we won't.  */
3623           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3624           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3625         }
3626       if (new_defines_function)
3627         /* If defining a function declared with other language
3628            linkage, use the previously declared language linkage.  */
3629         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3630       else if (types_match)
3631         {
3632           /* If redeclaring a builtin function, and not a definition,
3633              it stays built in.  */
3634           if (DECL_BUILT_IN (olddecl))
3635             {
3636               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3637               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3638               /* If we're keeping the built-in definition, keep the rtl,
3639                  regardless of declaration matches.  */
3640               SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3641             }
3642           else
3643             DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3644
3645           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3646           /* Don't clear out the arguments if we're redefining a function.  */
3647           if (DECL_ARGUMENTS (olddecl))
3648             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3649         }
3650     }
3651   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3652     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3653
3654   /* Now preserve various other info from the definition.  */
3655   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3656   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3657   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3658   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3659
3660   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3661     {
3662       int function_size;
3663
3664       function_size = sizeof (struct tree_decl);
3665
3666       memcpy ((char *) olddecl + sizeof (struct tree_common),
3667               (char *) newdecl + sizeof (struct tree_common),
3668               function_size - sizeof (struct tree_common));
3669
3670       if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3671         {
3672           /* If newdecl is a template instantiation, it is possible that
3673              the following sequence of events has occurred:
3674
3675              o A friend function was declared in a class template.  The
3676              class template was instantiated.
3677
3678              o The instantiation of the friend declaration was
3679              recorded on the instantiation list, and is newdecl.
3680
3681              o Later, however, instantiate_class_template called pushdecl
3682              on the newdecl to perform name injection.  But, pushdecl in
3683              turn called duplicate_decls when it discovered that another
3684              declaration of a global function with the same name already
3685              existed.
3686
3687              o Here, in duplicate_decls, we decided to clobber newdecl.
3688
3689              If we're going to do that, we'd better make sure that
3690              olddecl, and not newdecl, is on the list of
3691              instantiations so that if we try to do the instantiation
3692              again we won't get the clobbered declaration.  */
3693
3694           tree tmpl = DECL_TI_TEMPLATE (newdecl);
3695           tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3696
3697           for (; decls; decls = TREE_CHAIN (decls))
3698             if (TREE_VALUE (decls) == newdecl)
3699               TREE_VALUE (decls) = olddecl;
3700         }
3701     }
3702   else
3703     {
3704       memcpy ((char *) olddecl + sizeof (struct tree_common),
3705               (char *) newdecl + sizeof (struct tree_common),
3706               sizeof (struct tree_decl) - sizeof (struct tree_common)
3707               + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3708     }
3709
3710   DECL_UID (olddecl) = olddecl_uid;
3711   if (olddecl_friend)
3712     DECL_FRIEND_P (olddecl) = 1;
3713
3714   /* NEWDECL contains the merged attribute lists.
3715      Update OLDDECL to be the same.  */
3716   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3717
3718   return 1;
3719 }
3720
3721 /* Record a decl-node X as belonging to the current lexical scope.
3722    Check for errors (such as an incompatible declaration for the same
3723    name already seen in the same scope).
3724
3725    Returns either X or an old decl for the same name.
3726    If an old decl is returned, it may have been smashed
3727    to agree with what X says.  */
3728
3729 tree
3730 pushdecl (tree x)
3731 {
3732   register tree t;
3733   register tree name;
3734   int need_new_binding;
3735
3736   timevar_push (TV_NAME_LOOKUP);
3737   /* We shouldn't be calling pushdecl when we're generating RTL for a
3738      function that we already did semantic analysis on previously.  */
3739   my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3740                       19990913);
3741
3742   need_new_binding = 1;
3743
3744   if (DECL_TEMPLATE_PARM_P (x))
3745     /* Template parameters have no context; they are not X::T even
3746        when declared within a class or namespace.  */
3747     ;
3748   else
3749     {
3750       if (current_function_decl && x != current_function_decl
3751           /* A local declaration for a function doesn't constitute
3752              nesting.  */
3753           && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3754           /* A local declaration for an `extern' variable is in the
3755              scope of the current namespace, not the current
3756              function.  */
3757           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3758           && !DECL_CONTEXT (x))
3759         DECL_CONTEXT (x) = current_function_decl;
3760
3761       /* If this is the declaration for a namespace-scope function,
3762          but the declaration itself is in a local scope, mark the
3763          declaration.  */
3764       if (TREE_CODE (x) == FUNCTION_DECL
3765           && DECL_NAMESPACE_SCOPE_P (x)
3766           && current_function_decl
3767           && x != current_function_decl)
3768         DECL_LOCAL_FUNCTION_P (x) = 1;
3769     }
3770
3771   name = DECL_NAME (x);
3772   if (name)
3773     {
3774       int different_binding_level = 0;
3775
3776       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3777         name = TREE_OPERAND (name, 0);
3778
3779       /* In case this decl was explicitly namespace-qualified, look it
3780          up in its namespace context.  */
3781       if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3782           && namespace_bindings_p ())
3783         t = namespace_binding (name, DECL_CONTEXT (x));
3784       else
3785         t = lookup_name_current_level (name);
3786
3787       /* [basic.link] If there is a visible declaration of an entity
3788          with linkage having the same name and type, ignoring entities
3789          declared outside the innermost enclosing namespace scope, the
3790          block scope declaration declares that same entity and
3791          receives the linkage of the previous declaration.  */
3792       if (! t && current_function_decl && x != current_function_decl
3793           && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3794           && DECL_EXTERNAL (x))
3795         {
3796           /* Look in block scope.  */
3797           t = IDENTIFIER_VALUE (name);
3798           /* Or in the innermost namespace.  */
3799           if (! t)
3800             t = namespace_binding (name, DECL_CONTEXT (x));
3801           /* Does it have linkage?  Note that if this isn't a DECL, it's an
3802              OVERLOAD, which is OK.  */
3803           if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3804             t = NULL_TREE;
3805           if (t)
3806             different_binding_level = 1;
3807         }
3808
3809       /* If we are declaring a function, and the result of name-lookup
3810          was an OVERLOAD, look for an overloaded instance that is
3811          actually the same as the function we are declaring.  (If
3812          there is one, we have to merge our declaration with the
3813          previous declaration.)  */
3814       if (t && TREE_CODE (t) == OVERLOAD)
3815         {
3816           tree match;
3817
3818           if (TREE_CODE (x) == FUNCTION_DECL)
3819             for (match = t; match; match = OVL_NEXT (match))
3820               {
3821                 if (decls_match (OVL_CURRENT (match), x))
3822                   break;
3823               }
3824           else
3825             /* Just choose one.  */
3826             match = t;
3827
3828           if (match)
3829             t = OVL_CURRENT (match);
3830           else
3831             t = NULL_TREE;
3832         }
3833
3834       if (t == error_mark_node)
3835         {
3836           /* error_mark_node is 0 for a while during initialization!  */
3837           t = NULL_TREE;
3838           cp_error_at ("`%#D' used prior to declaration", x);
3839         }
3840       else if (t != NULL_TREE)
3841         {
3842           if (different_binding_level)
3843             {
3844               if (decls_match (x, t))
3845                 /* The standard only says that the local extern
3846                    inherits linkage from the previous decl; in
3847                    particular, default args are not shared.  It would
3848                    be nice to propagate inlining info, though.  FIXME.  */
3849                 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3850             }
3851           else if (TREE_CODE (t) == PARM_DECL)
3852             {
3853               if (DECL_CONTEXT (t) == NULL_TREE)
3854                 /* This is probaby caused by too many errors, but calling
3855                    abort will say that if errors have occurred.  */
3856                 abort ();
3857
3858               /* Check for duplicate params.  */
3859               if (duplicate_decls (x, t))
3860                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3861             }
3862           else if ((DECL_EXTERN_C_FUNCTION_P (x)
3863                     || DECL_FUNCTION_TEMPLATE_P (x))
3864                    && is_overloaded_fn (t))
3865             /* Don't do anything just yet.  */;
3866           else if (t == wchar_decl_node)
3867             {
3868               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3869                 pedwarn ("redeclaration of `wchar_t' as `%T'",
3870                             TREE_TYPE (x));
3871
3872               /* Throw away the redeclaration.  */
3873               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3874             }
3875           else if (TREE_CODE (t) != TREE_CODE (x))
3876             {
3877               if (duplicate_decls (x, t))
3878                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3879             }
3880           else if (duplicate_decls (x, t))
3881             {
3882               if (TREE_CODE (t) == TYPE_DECL)
3883                 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3884               else if (TREE_CODE (t) == FUNCTION_DECL)
3885                 check_default_args (t);
3886
3887               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3888             }
3889           else if (DECL_MAIN_P (x))
3890             {
3891               /* A redeclaration of main, but not a duplicate of the
3892                  previous one.
3893
3894                  [basic.start.main]
3895
3896                  This function shall not be overloaded.  */
3897               cp_error_at ("invalid redeclaration of `%D'", t);
3898               error ("as `%D'", x);
3899               /* We don't try to push this declaration since that
3900                  causes a crash.  */
3901               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3902             }
3903         }
3904
3905       check_template_shadow (x);
3906
3907       /* If this is a function conjured up by the backend, massage it
3908          so it looks friendly.  */
3909       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3910         {
3911           retrofit_lang_decl (x);
3912           SET_DECL_LANGUAGE (x, lang_c);
3913         }
3914
3915       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3916         {
3917           t = push_overloaded_decl (x, PUSH_LOCAL);
3918           if (t != x)
3919             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3920           if (!namespace_bindings_p ())
3921             /* We do not need to create a binding for this name;
3922                push_overloaded_decl will have already done so if
3923                necessary.  */
3924             need_new_binding = 0;
3925         }
3926       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3927         {
3928           t = push_overloaded_decl (x, PUSH_GLOBAL);
3929           if (t == x)
3930             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3931           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3932         }
3933
3934       /* If declaring a type as a typedef, copy the type (unless we're
3935          at line 0), and install this TYPE_DECL as the new type's typedef
3936          name.  See the extensive comment in ../c-decl.c (pushdecl).  */
3937       if (TREE_CODE (x) == TYPE_DECL)
3938         {
3939           tree type = TREE_TYPE (x);
3940           if (DECL_SOURCE_LINE (x) == 0)
3941             {
3942               if (TYPE_NAME (type) == 0)
3943                 TYPE_NAME (type) = x;
3944             }
3945           else if (type != error_mark_node && TYPE_NAME (type) != x
3946                    /* We don't want to copy the type when all we're
3947                       doing is making a TYPE_DECL for the purposes of
3948                       inlining.  */
3949                    && (!TYPE_NAME (type)
3950                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3951             {
3952               DECL_ORIGINAL_TYPE (x) = type;
3953               type = build_type_copy (type);
3954               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3955               TYPE_NAME (type) = x;
3956               TREE_TYPE (x) = type;
3957             }
3958
3959           if (type != error_mark_node
3960               && TYPE_NAME (type)
3961               && TYPE_IDENTIFIER (type))
3962             set_identifier_type_value_with_scope (DECL_NAME (x), type,
3963                                                   current_binding_level);
3964
3965         }
3966
3967       /* Multiple external decls of the same identifier ought to match.
3968
3969          We get warnings about inline functions where they are defined.
3970          We get warnings about other functions from push_overloaded_decl.
3971
3972          Avoid duplicate warnings where they are used.  */
3973       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3974         {
3975           tree decl;
3976
3977           decl = IDENTIFIER_NAMESPACE_VALUE (name);
3978           if (decl && TREE_CODE (decl) == OVERLOAD)
3979             decl = OVL_FUNCTION (decl);
3980
3981           if (decl && decl != error_mark_node
3982               && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3983               /* If different sort of thing, we already gave an error.  */
3984               && TREE_CODE (decl) == TREE_CODE (x)
3985               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3986             {
3987               pedwarn ("type mismatch with previous external decl", x);
3988               cp_pedwarn_at ("previous external decl of `%#D'", decl);
3989             }
3990         }
3991
3992       /* This name is new in its binding level.
3993          Install the new declaration and return it.  */
3994       if (namespace_bindings_p ())
3995         {
3996           /* Install a global value.  */
3997
3998           /* If the first global decl has external linkage,
3999              warn if we later see static one.  */
4000           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
4001             TREE_PUBLIC (name) = 1;
4002
4003           /* Bind the name for the entity.  */
4004           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
4005                 && t != NULL_TREE)
4006               && (TREE_CODE (x) == TYPE_DECL
4007                   || TREE_CODE (x) == VAR_DECL
4008                   || TREE_CODE (x) == ALIAS_DECL
4009                   || TREE_CODE (x) == NAMESPACE_DECL
4010                   || TREE_CODE (x) == CONST_DECL
4011                   || TREE_CODE (x) == TEMPLATE_DECL))
4012             SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
4013
4014           /* Don't forget if the function was used via an implicit decl.  */
4015           if (IDENTIFIER_IMPLICIT_DECL (name)
4016               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
4017             TREE_USED (x) = 1;
4018
4019           /* Don't forget if its address was taken in that way.  */
4020           if (IDENTIFIER_IMPLICIT_DECL (name)
4021               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
4022             TREE_ADDRESSABLE (x) = 1;
4023
4024           /* Warn about mismatches against previous implicit decl.  */
4025           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4026               /* If this real decl matches the implicit, don't complain.  */
4027               && ! (TREE_CODE (x) == FUNCTION_DECL
4028                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
4029             warning
4030               ("`%D' was previously implicitly declared to return `int'", x);
4031
4032           /* If new decl is `static' and an `extern' was seen previously,
4033              warn about it.  */
4034           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4035             warn_extern_redeclared_static (x, t);
4036         }
4037       else
4038         {
4039           /* Here to install a non-global value.  */
4040           tree oldlocal = IDENTIFIER_VALUE (name);
4041           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
4042
4043           if (need_new_binding)
4044             {
4045               push_local_binding (name, x, 0);
4046               /* Because push_local_binding will hook X on to the
4047                  current_binding_level's name list, we don't want to
4048                  do that again below.  */
4049               need_new_binding = 0;
4050             }
4051
4052           /* If this is a TYPE_DECL, push it into the type value slot.  */
4053           if (TREE_CODE (x) == TYPE_DECL)
4054             set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4055                                                   current_binding_level);
4056
4057           /* Clear out any TYPE_DECL shadowed by a namespace so that
4058              we won't think this is a type.  The C struct hack doesn't
4059              go through namespaces.  */
4060           if (TREE_CODE (x) == NAMESPACE_DECL)
4061             set_identifier_type_value_with_scope (name, NULL_TREE,
4062                                                   current_binding_level);
4063
4064           if (oldlocal)
4065             {
4066               tree d = oldlocal;
4067
4068               while (oldlocal
4069                      && TREE_CODE (oldlocal) == VAR_DECL
4070                      && DECL_DEAD_FOR_LOCAL (oldlocal))
4071                 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4072
4073               if (oldlocal == NULL_TREE)
4074                 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4075             }
4076
4077           /* If this is an extern function declaration, see if we
4078              have a global definition or declaration for the function.  */
4079           if (oldlocal == NULL_TREE
4080               && DECL_EXTERNAL (x)
4081               && oldglobal != NULL_TREE
4082               && TREE_CODE (x) == FUNCTION_DECL
4083               && TREE_CODE (oldglobal) == FUNCTION_DECL)
4084             {
4085               /* We have one.  Their types must agree.  */
4086               if (decls_match (x, oldglobal))
4087                 /* OK */;
4088               else
4089                 {
4090                   warning ("extern declaration of `%#D' doesn't match", x);
4091                   cp_warning_at ("global declaration `%#D'", oldglobal);
4092                 }
4093             }
4094           /* If we have a local external declaration,
4095              and no file-scope declaration has yet been seen,
4096              then if we later have a file-scope decl it must not be static.  */
4097           if (oldlocal == NULL_TREE
4098               && oldglobal == NULL_TREE
4099               && DECL_EXTERNAL (x)
4100               && TREE_PUBLIC (x))
4101             TREE_PUBLIC (name) = 1;
4102
4103           /* Warn if shadowing an argument at the top level of the body.  */
4104           if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4105               /* Inline decls shadow nothing.  */
4106               && !DECL_FROM_INLINE (x)
4107               && TREE_CODE (oldlocal) == PARM_DECL
4108               /* Don't check the `this' parameter.  */
4109               && !DECL_ARTIFICIAL (oldlocal))
4110             {
4111               bool err = false;
4112
4113               /* Don't complain if it's from an enclosing function.  */
4114               if (DECL_CONTEXT (oldlocal) == current_function_decl
4115                   && TREE_CODE (x) != PARM_DECL)
4116                 {
4117                   /* Go to where the parms should be and see if we find
4118                      them there.  */
4119                   struct cp_binding_level *b = current_binding_level->level_chain;
4120
4121                   /* Skip the ctor/dtor cleanup level.  */
4122                   b = b->level_chain;
4123
4124                   /* ARM $8.3 */
4125                   if (b->parm_flag == 1)
4126                     {
4127                       error ("declaration of `%#D' shadows a parameter",
4128                                 name);
4129                       err = true;
4130                     }
4131                 }
4132
4133               if (warn_shadow && !err)
4134                 shadow_warning ("a parameter", name, oldlocal);
4135             }
4136
4137           /* Maybe warn if shadowing something else.  */
4138           else if (warn_shadow && !DECL_EXTERNAL (x)
4139               /* No shadow warnings for internally generated vars.  */
4140               && ! DECL_ARTIFICIAL (x)
4141               /* No shadow warnings for vars made for inlining.  */
4142               && ! DECL_FROM_INLINE (x))
4143             {
4144               if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4145                        && current_class_ptr
4146                        && !TREE_STATIC (name))
4147                 warning ("declaration of `%s' shadows a member of `this'",
4148                             IDENTIFIER_POINTER (name));
4149               else if (oldlocal != NULL_TREE
4150                        && TREE_CODE (oldlocal) == VAR_DECL)
4151                 shadow_warning ("a previous local", name, oldlocal);
4152               else if (oldglobal != NULL_TREE
4153                        && TREE_CODE (oldglobal) == VAR_DECL)
4154                 /* XXX shadow warnings in outer-more namespaces */
4155                 shadow_warning ("a global declaration", name, oldglobal);
4156             }
4157         }
4158
4159       if (TREE_CODE (x) == FUNCTION_DECL)
4160         check_default_args (x);
4161
4162       if (TREE_CODE (x) == VAR_DECL)
4163         maybe_register_incomplete_var (x);
4164     }
4165
4166   if (need_new_binding)
4167     add_decl_to_level (x,
4168                        DECL_NAMESPACE_SCOPE_P (x)
4169                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4170                        : current_binding_level);
4171
4172   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4173 }
4174
4175 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4176    caller to set DECL_CONTEXT properly.  */
4177
4178 static tree
4179 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4180 {
4181   register struct cp_binding_level *b;
4182   tree function_decl = current_function_decl;
4183
4184   timevar_push (TV_NAME_LOOKUP);
4185   current_function_decl = NULL_TREE;
4186   if (level->parm_flag == 2)
4187     {
4188       b = class_binding_level;
4189       class_binding_level = level;
4190       pushdecl_class_level (x);
4191       class_binding_level = b;
4192     }
4193   else
4194     {
4195       b = current_binding_level;
4196       current_binding_level = level;
4197       x = pushdecl (x);
4198       current_binding_level = b;
4199     }
4200   current_function_decl = function_decl;
4201   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4202 }
4203
4204 /* Like pushdecl, only it places X in the current namespace,
4205    if appropriate.  */
4206
4207 tree
4208 pushdecl_namespace_level (tree x)
4209 {
4210   register struct cp_binding_level *b = current_binding_level;
4211   register tree t;
4212
4213   timevar_push (TV_NAME_LOOKUP);
4214   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4215
4216   /* Now, the type_shadowed stack may screw us.  Munge it so it does
4217      what we want.  */
4218   if (TREE_CODE (x) == TYPE_DECL)
4219     {
4220       tree name = DECL_NAME (x);
4221       tree newval;
4222       tree *ptr = (tree *)0;
4223       for (; b != global_binding_level; b = b->level_chain)
4224         {
4225           tree shadowed = b->type_shadowed;
4226           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4227             if (TREE_PURPOSE (shadowed) == name)
4228               {
4229                 ptr = &TREE_VALUE (shadowed);
4230                 /* Can't break out of the loop here because sometimes
4231                    a binding level will have duplicate bindings for
4232                    PT names.  It's gross, but I haven't time to fix it.  */
4233               }
4234         }
4235       newval = TREE_TYPE (x);
4236       if (ptr == (tree *)0)
4237         {
4238           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4239              up here if this is changed to an assertion.  --KR  */
4240           SET_IDENTIFIER_TYPE_VALUE (name, newval);
4241         }
4242       else
4243         {
4244           *ptr = newval;
4245         }
4246     }
4247   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4248 }
4249
4250 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4251    if appropriate.  */
4252
4253 tree
4254 pushdecl_top_level (tree x)
4255 {
4256   timevar_push (TV_NAME_LOOKUP);
4257   push_to_top_level ();
4258   x = pushdecl_namespace_level (x);
4259   pop_from_top_level ();
4260   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4261 }
4262
4263 /* Make the declaration of X appear in CLASS scope.  */
4264
4265 void
4266 pushdecl_class_level (tree x)
4267 {
4268   tree name;
4269
4270   timevar_push (TV_NAME_LOOKUP);
4271   /* Get the name of X.  */
4272   if (TREE_CODE (x) == OVERLOAD)
4273     name = DECL_NAME (get_first_fn (x));
4274   else
4275     name = DECL_NAME (x);
4276
4277   if (name)
4278     {
4279       push_class_level_binding (name, x);
4280       if (TREE_CODE (x) == TYPE_DECL)
4281         set_identifier_type_value (name, TREE_TYPE (x));
4282     }
4283   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4284     {
4285       /* If X is an anonymous aggregate, all of its members are
4286          treated as if they were members of the class containing the
4287          aggregate, for naming purposes.  */
4288       tree f;
4289
4290       for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4291         pushdecl_class_level (f);
4292     }
4293   timevar_pop (TV_NAME_LOOKUP);
4294 }
4295
4296 /* Enter DECL into the symbol table, if that's appropriate.  Returns
4297    DECL, or a modified version thereof.  */
4298
4299 tree
4300 maybe_push_decl (tree decl)
4301 {
4302   tree type = TREE_TYPE (decl);
4303
4304   /* Add this decl to the current binding level, but not if it comes
4305      from another scope, e.g. a static member variable.  TEM may equal
4306      DECL or it may be a previous decl of the same name.  */
4307   if (decl == error_mark_node
4308       || (TREE_CODE (decl) != PARM_DECL
4309           && DECL_CONTEXT (decl) != NULL_TREE
4310           /* Definitions of namespace members outside their namespace are
4311              possible.  */
4312           && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4313       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4314       || TREE_CODE (type) == UNKNOWN_TYPE
4315       /* The declaration of a template specialization does not affect
4316          the functions available for overload resolution, so we do not
4317          call pushdecl.  */
4318       || (TREE_CODE (decl) == FUNCTION_DECL
4319           && DECL_TEMPLATE_SPECIALIZATION (decl)))
4320     return decl;
4321   else
4322     return pushdecl (decl);
4323 }
4324
4325 /* Make the declaration(s) of X appear in CLASS scope
4326    under the name NAME.  */
4327
4328 void
4329 push_class_level_binding (tree name, tree x)
4330 {
4331   tree binding;
4332   timevar_push (TV_NAME_LOOKUP);
4333   /* The class_binding_level will be NULL if x is a template
4334      parameter name in a member template.  */
4335   if (!class_binding_level)
4336     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
4337
4338   /* Make sure that this new member does not have the same name
4339      as a template parameter.  */
4340   if (TYPE_BEING_DEFINED (current_class_type))
4341     check_template_shadow (x);
4342
4343   /* If this declaration shadows a declaration from an enclosing
4344      class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4345      we leave this class.  Record the shadowed declaration here.  */
4346   binding = IDENTIFIER_BINDING (name);
4347   if (binding
4348       && ((TREE_CODE (x) == OVERLOAD
4349            && BINDING_VALUE (binding)
4350            && is_overloaded_fn (BINDING_VALUE (binding)))
4351           || INHERITED_VALUE_BINDING_P (binding)))
4352     {
4353       tree shadow;
4354       tree old_decl;
4355
4356       /* If the old binding was from a base class, and was for a tag
4357          name, slide it over to make room for the new binding.  The
4358          old binding is still visible if explicitly qualified with a
4359          class-key.  */
4360       if (INHERITED_VALUE_BINDING_P (binding)
4361           && BINDING_VALUE (binding)
4362           && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4363           && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4364           && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4365         {
4366           old_decl = BINDING_TYPE (binding);
4367           BINDING_TYPE (binding) = BINDING_VALUE (binding);
4368           BINDING_VALUE (binding) = NULL_TREE;
4369           INHERITED_VALUE_BINDING_P (binding) = 0;
4370         }
4371       else
4372         old_decl = BINDING_VALUE (binding);
4373
4374       /* Find the previous binding of name on the class-shadowed
4375          list, and update it.  */
4376       for (shadow = class_binding_level->class_shadowed;
4377            shadow;
4378            shadow = TREE_CHAIN (shadow))
4379         if (TREE_PURPOSE (shadow) == name
4380             && TREE_TYPE (shadow) == old_decl)
4381           {
4382             BINDING_VALUE (binding) = x;
4383             INHERITED_VALUE_BINDING_P (binding) = 0;
4384             TREE_TYPE (shadow) = x;
4385             IDENTIFIER_CLASS_VALUE (name) = x;
4386             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
4387           }
4388     }
4389
4390   /* If we didn't replace an existing binding, put the binding on the
4391      stack of bindings for the identifier, and update the shadowed list.  */
4392   if (push_class_binding (name, x))
4393     {
4394       class_binding_level->class_shadowed
4395         = tree_cons (name, NULL,
4396                      class_binding_level->class_shadowed);
4397       /* Record the value we are binding NAME to so that we can know
4398          what to pop later.  */
4399       TREE_TYPE (class_binding_level->class_shadowed) = x;
4400     }
4401   timevar_pop (TV_NAME_LOOKUP);
4402 }
4403
4404 /* Insert another USING_DECL into the current binding level, returning
4405    this declaration. If this is a redeclaration, do nothing, and
4406    return NULL_TREE if this not in namespace scope (in namespace
4407    scope, a using decl might extend any previous bindings).  */
4408
4409 tree
4410 push_using_decl (tree scope, tree name)
4411 {
4412   tree decl;
4413
4414   timevar_push (TV_NAME_LOOKUP);
4415   my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4416   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4417   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4418     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4419       break;
4420   if (decl)
4421     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4422                             namespace_bindings_p () ? decl : NULL_TREE);
4423   decl = build_lang_decl (USING_DECL, name, void_type_node);
4424   DECL_INITIAL (decl) = scope;
4425   TREE_CHAIN (decl) = current_binding_level->usings;
4426   current_binding_level->usings = decl;
4427   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4428 }
4429
4430 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4431    changed (i.e. there was already a directive), or the fresh
4432    TREE_LIST otherwise.  */
4433
4434 tree
4435 push_using_directive (tree used)
4436 {
4437   tree ud = current_binding_level->using_directives;
4438   tree iter, ancestor;
4439
4440   timevar_push (TV_NAME_LOOKUP);
4441   /* Check if we already have this.  */
4442   if (purpose_member (used, ud) != NULL_TREE)
4443     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4444
4445   ancestor = namespace_ancestor (current_decl_namespace (), used);
4446   ud = current_binding_level->using_directives;
4447   ud = tree_cons (used, ancestor, ud);
4448   current_binding_level->using_directives = ud;
4449
4450   /* Recursively add all namespaces used.  */
4451   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4452     push_using_directive (TREE_PURPOSE (iter));
4453
4454   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4455 }
4456
4457 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4458    other definitions already in place.  We get around this by making
4459    the value of the identifier point to a list of all the things that
4460    want to be referenced by that name.  It is then up to the users of
4461    that name to decide what to do with that list.
4462
4463    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4464    DECL_TEMPLATE_RESULT.  It is dealt with the same way.
4465
4466    FLAGS is a bitwise-or of the following values:
4467      PUSH_LOCAL: Bind DECL in the current scope, rather than at
4468                  namespace scope.
4469      PUSH_USING: DECL is being pushed as the result of a using
4470                  declaration.
4471
4472    The value returned may be a previous declaration if we guessed wrong
4473    about what language DECL should belong to (C or C++).  Otherwise,
4474    it's always DECL (and never something that's not a _DECL).  */
4475
4476 tree
4477 push_overloaded_decl (tree decl, int flags)
4478 {
4479   tree name = DECL_NAME (decl);
4480   tree old;
4481   tree new_binding;
4482   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4483
4484   timevar_push (TV_NAME_LOOKUP);
4485   if (doing_global)
4486     old = namespace_binding (name, DECL_CONTEXT (decl));
4487   else
4488     old = lookup_name_current_level (name);
4489
4490   if (old)
4491     {
4492       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4493         {
4494           tree t = TREE_TYPE (old);
4495           if (IS_AGGR_TYPE (t) && warn_shadow
4496               && (! DECL_IN_SYSTEM_HEADER (decl)
4497                   || ! DECL_IN_SYSTEM_HEADER (old)))
4498             warning ("`%#D' hides constructor for `%#T'", decl, t);
4499           old = NULL_TREE;
4500         }
4501       else if (is_overloaded_fn (old))
4502         {
4503           tree tmp;
4504
4505           for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4506             {
4507               tree fn = OVL_CURRENT (tmp);
4508
4509               if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4510                   && !(flags & PUSH_USING)
4511                   && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4512                                 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4513                 error ("`%#D' conflicts with previous using declaration `%#D'",
4514                           decl, fn);
4515
4516               if (duplicate_decls (decl, fn))
4517                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4518             }
4519         }
4520       else if (old == error_mark_node)
4521         /* Ignore the undefined symbol marker.  */
4522         old = NULL_TREE;
4523       else
4524         {
4525           cp_error_at ("previous non-function declaration `%#D'", old);
4526           error ("conflicts with function declaration `%#D'", decl);
4527           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4528         }
4529     }
4530
4531   if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4532     {
4533       if (old && TREE_CODE (old) != OVERLOAD)
4534         new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4535       else
4536         new_binding = ovl_cons (decl, old);
4537       if (flags & PUSH_USING)
4538         OVL_USED (new_binding) = 1;
4539     }
4540   else
4541     /* NAME is not ambiguous.  */
4542     new_binding = decl;
4543
4544   if (doing_global)
4545     set_namespace_binding (name, current_namespace, new_binding);
4546   else
4547     {
4548       /* We only create an OVERLOAD if there was a previous binding at
4549          this level, or if decl is a template. In the former case, we
4550          need to remove the old binding and replace it with the new
4551          binding.  We must also run through the NAMES on the binding
4552          level where the name was bound to update the chain.  */
4553
4554       if (TREE_CODE (new_binding) == OVERLOAD && old)
4555         {
4556           tree *d;
4557
4558           for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4559                *d;
4560                d = &TREE_CHAIN (*d))
4561             if (*d == old
4562                 || (TREE_CODE (*d) == TREE_LIST
4563                     && TREE_VALUE (*d) == old))
4564               {
4565                 if (TREE_CODE (*d) == TREE_LIST)
4566                   /* Just replace the old binding with the new.  */
4567                   TREE_VALUE (*d) = new_binding;
4568                 else
4569                   /* Build a TREE_LIST to wrap the OVERLOAD.  */
4570                   *d = tree_cons (NULL_TREE, new_binding,
4571                                   TREE_CHAIN (*d));
4572
4573                 /* And update the CPLUS_BINDING node.  */
4574                 BINDING_VALUE (IDENTIFIER_BINDING (name))
4575                   = new_binding;
4576                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4577               }
4578
4579           /* We should always find a previous binding in this case.  */
4580           abort ();
4581         }
4582
4583       /* Install the new binding.  */
4584       push_local_binding (name, new_binding, flags);
4585     }
4586
4587   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4588 }
4589 \f
4590 /* Generate an implicit declaration for identifier FUNCTIONID
4591    as a function of type int ().  Print a warning if appropriate.  */
4592
4593 tree
4594 implicitly_declare (tree functionid)
4595 {
4596   register tree decl;
4597
4598   /* We used to reuse an old implicit decl here,
4599      but this loses with inline functions because it can clobber
4600      the saved decl chains.  */
4601   decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4602
4603   DECL_EXTERNAL (decl) = 1;
4604   TREE_PUBLIC (decl) = 1;
4605
4606   /* ISO standard says implicit declarations are in the innermost block.
4607      So we record the decl in the standard fashion.  */
4608   pushdecl (decl);
4609   rest_of_decl_compilation (decl, NULL, 0, 0);
4610
4611   if (warn_implicit
4612       /* Only one warning per identifier.  */
4613       && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4614     {
4615       pedwarn ("implicit declaration of function `%#D'", decl);
4616     }
4617
4618   SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4619
4620   return decl;
4621 }
4622
4623 /* Return zero if the declaration NEWDECL is valid
4624    when the declaration OLDDECL (assumed to be for the same name)
4625    has already been seen.
4626    Otherwise return an error message format string with a %s
4627    where the identifier should go.  */
4628
4629 static const char *
4630 redeclaration_error_message (tree newdecl, tree olddecl)
4631 {
4632   if (TREE_CODE (newdecl) == TYPE_DECL)
4633     {
4634       /* Because C++ can put things into name space for free,
4635          constructs like "typedef struct foo { ... } foo"
4636          would look like an erroneous redeclaration.  */
4637       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4638         return 0;
4639       else
4640         return "redefinition of `%#D'";
4641     }
4642   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4643     {
4644       /* If this is a pure function, its olddecl will actually be
4645          the original initialization to `0' (which we force to call
4646          abort()).  Don't complain about redefinition in this case.  */
4647       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4648         return 0;
4649
4650       /* If both functions come from different namespaces, this is not
4651          a redeclaration - this is a conflict with a used function.  */
4652       if (DECL_NAMESPACE_SCOPE_P (olddecl)
4653           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4654         return "`%D' conflicts with used function";
4655
4656       /* We'll complain about linkage mismatches in
4657          warn_extern_redeclared_static.  */
4658
4659       /* Defining the same name twice is no good.  */
4660       if (DECL_INITIAL (olddecl) != NULL_TREE
4661           && DECL_INITIAL (newdecl) != NULL_TREE)
4662         {
4663           if (DECL_NAME (olddecl) == NULL_TREE)
4664             return "`%#D' not declared in class";
4665           else
4666             return "redefinition of `%#D'";
4667         }
4668       return 0;
4669     }
4670   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4671     {
4672       if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4673            && (DECL_TEMPLATE_RESULT (newdecl)
4674                != DECL_TEMPLATE_RESULT (olddecl))
4675            && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4676            && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4677           || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4678               && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4679               && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4680         return "redefinition of `%#D'";
4681       return 0;
4682     }
4683   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4684     {
4685       /* Objects declared at top level:  */
4686       /* If at least one is a reference, it's ok.  */
4687       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4688         return 0;
4689       /* Reject two definitions.  */
4690       return "redefinition of `%#D'";
4691     }
4692   else
4693     {
4694       /* Objects declared with block scope:  */
4695       /* Reject two definitions, and reject a definition
4696          together with an external reference.  */
4697       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4698         return "redeclaration of `%#D'";
4699       return 0;
4700     }
4701 }
4702 \f
4703 /* Create a new label, named ID.  */
4704
4705 static tree
4706 make_label_decl (tree id, int local_p)
4707 {
4708   tree decl;
4709
4710   decl = build_decl (LABEL_DECL, id, void_type_node);
4711   if (expanding_p)
4712     /* Make sure every label has an rtx.  */
4713     label_rtx (decl);
4714
4715   DECL_CONTEXT (decl) = current_function_decl;
4716   DECL_MODE (decl) = VOIDmode;
4717   C_DECLARED_LABEL_FLAG (decl) = local_p;
4718
4719   /* Say where one reference is to the label, for the sake of the
4720      error if it is not defined.  */
4721   DECL_SOURCE_LINE (decl) = lineno;
4722   DECL_SOURCE_FILE (decl) = input_filename;
4723
4724   /* Record the fact that this identifier is bound to this label.  */
4725   SET_IDENTIFIER_LABEL_VALUE (id, decl);
4726
4727   return decl;
4728 }
4729
4730 /* Record this label on the list of used labels so that we can check
4731    at the end of the function to see whether or not the label was
4732    actually defined, and so we can check when the label is defined whether
4733    this use is valid.  */
4734
4735 static void
4736 use_label (tree decl)
4737 {
4738   if (named_label_uses == NULL
4739       || named_label_uses->names_in_scope != current_binding_level->names
4740       || named_label_uses->label_decl != decl)
4741     {
4742       struct named_label_use_list *new_ent;
4743       new_ent = ((struct named_label_use_list *)
4744                  ggc_alloc (sizeof (struct named_label_use_list)));
4745       new_ent->label_decl = decl;
4746       new_ent->names_in_scope = current_binding_level->names;
4747       new_ent->binding_level = current_binding_level;
4748       new_ent->lineno_o_goto = lineno;
4749       new_ent->filename_o_goto = input_filename;
4750       new_ent->next = named_label_uses;
4751       named_label_uses = new_ent;
4752     }
4753 }
4754
4755 /* Look for a label named ID in the current function.  If one cannot
4756    be found, create one.  (We keep track of used, but undefined,
4757    labels, and complain about them at the end of a function.)  */
4758
4759 tree
4760 lookup_label (tree id)
4761 {
4762   tree decl;
4763   struct named_label_list *ent;
4764
4765   timevar_push (TV_NAME_LOOKUP);
4766   /* You can't use labels at global scope.  */
4767   if (current_function_decl == NULL_TREE)
4768     {
4769       error ("label `%s' referenced outside of any function",
4770              IDENTIFIER_POINTER (id));
4771       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4772     }
4773
4774   /* See if we've already got this label.  */
4775   decl = IDENTIFIER_LABEL_VALUE (id);
4776   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4777     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4778
4779   /* Record this label on the list of labels used in this function.
4780      We do this before calling make_label_decl so that we get the
4781      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
4782   ent = ((struct named_label_list *)
4783          ggc_alloc_cleared (sizeof (struct named_label_list)));
4784   ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4785   ent->next = named_labels;
4786   named_labels = ent;
4787
4788   /* We need a new label.  */
4789   decl = make_label_decl (id, /*local_p=*/0);
4790
4791   /* Now fill in the information we didn't have before.  */
4792   ent->label_decl = decl;
4793
4794   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4795 }
4796
4797 /* Declare a local label named ID.  */
4798
4799 tree
4800 declare_local_label (tree id)
4801 {
4802   tree decl;
4803
4804   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4805      this scope we can restore the old value of
4806      IDENTIFIER_TYPE_VALUE.  */
4807   current_binding_level->shadowed_labels
4808     = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4809                  current_binding_level->shadowed_labels);
4810   /* Look for the label.  */
4811   decl = make_label_decl (id, /*local_p=*/1);
4812   /* Now fill in the information we didn't have before.  */
4813   TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4814
4815   return decl;
4816 }
4817
4818 /* Returns nonzero if it is ill-formed to jump past the declaration of
4819    DECL.  Returns 2 if it's also a real problem.  */
4820
4821 static int
4822 decl_jump_unsafe (tree decl)
4823 {
4824   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4825     return 0;
4826
4827   if (DECL_INITIAL (decl) == NULL_TREE
4828       && pod_type_p (TREE_TYPE (decl)))
4829     return 0;
4830
4831   /* This is really only important if we're crossing an initialization.
4832      The POD stuff is just pedantry; why should it matter if the class
4833      contains a field of pointer to member type?  */
4834   if (DECL_INITIAL (decl)
4835       || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4836     return 2;
4837   return 1;
4838 }
4839
4840 /* Check that a single previously seen jump to a newly defined label
4841    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4842    the jump context; NAMES are the names in scope in LEVEL at the jump
4843    context; FILE and LINE are the source position of the jump or 0.  */
4844
4845 static void
4846 check_previous_goto_1 (tree decl,
4847                        struct cp_binding_level* level,
4848                        tree names,
4849                        const char* file,
4850                        int line)
4851 {
4852   int identified = 0;
4853   int saw_eh = 0;
4854   struct cp_binding_level *b = current_binding_level;
4855   for (; b; b = b->level_chain)
4856     {
4857       tree new_decls = b->names;
4858       tree old_decls = (b == level ? names : NULL_TREE);
4859       for (; new_decls != old_decls;
4860            new_decls = TREE_CHAIN (new_decls))
4861         {
4862           int problem = decl_jump_unsafe (new_decls);
4863           if (! problem)
4864             continue;
4865
4866           if (! identified)
4867             {
4868               if (decl)
4869                 pedwarn ("jump to label `%D'", decl);
4870               else
4871                 pedwarn ("jump to case label");
4872
4873               if (file)
4874                 pedwarn_with_file_and_line (file, line, "  from here");
4875               identified = 1;
4876             }
4877
4878           if (problem > 1)
4879             cp_error_at ("  crosses initialization of `%#D'",
4880                          new_decls);
4881           else
4882             cp_pedwarn_at ("  enters scope of non-POD `%#D'",
4883                            new_decls);
4884         }
4885
4886       if (b == level)
4887         break;
4888       if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4889         {
4890           if (! identified)
4891             {
4892               if (decl)
4893                 pedwarn ("jump to label `%D'", decl);
4894               else
4895                 pedwarn ("jump to case label");
4896
4897               if (file)
4898                 pedwarn_with_file_and_line (file, line, "  from here");
4899               identified = 1;
4900             }
4901           if (b->is_try_scope)
4902             error ("  enters try block");
4903           else
4904             error ("  enters catch block");
4905           saw_eh = 1;
4906         }
4907     }
4908 }
4909
4910 static void
4911 check_previous_goto (struct named_label_use_list* use)
4912 {
4913   check_previous_goto_1 (use->label_decl, use->binding_level,
4914                          use->names_in_scope, use->filename_o_goto,
4915                          use->lineno_o_goto);
4916 }
4917
4918 static void
4919 check_switch_goto (struct cp_binding_level* level)
4920 {
4921   check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
4922 }
4923
4924 /* Check that any previously seen jumps to a newly defined label DECL
4925    are OK.  Called by define_label.  */
4926
4927 static void
4928 check_previous_gotos (tree decl)
4929 {
4930   struct named_label_use_list **usep;
4931
4932   if (! TREE_USED (decl))
4933     return;
4934
4935   for (usep = &named_label_uses; *usep; )
4936     {
4937       struct named_label_use_list *use = *usep;
4938       if (use->label_decl == decl)
4939         {
4940           check_previous_goto (use);
4941           *usep = use->next;
4942         }
4943       else
4944         usep = &(use->next);
4945     }
4946 }
4947
4948 /* Check that a new jump to a label DECL is OK.  Called by
4949    finish_goto_stmt.  */
4950
4951 void
4952 check_goto (tree decl)
4953 {
4954   int identified = 0;
4955   tree bad;
4956   struct named_label_list *lab;
4957
4958   /* We can't know where a computed goto is jumping.  So we assume
4959      that it's OK.  */
4960   if (! DECL_P (decl))
4961     return;
4962
4963   /* If the label hasn't been defined yet, defer checking.  */
4964   if (! DECL_INITIAL (decl))
4965     {
4966       use_label (decl);
4967       return;
4968     }
4969
4970   for (lab = named_labels; lab; lab = lab->next)
4971     if (decl == lab->label_decl)
4972       break;
4973
4974   /* If the label is not on named_labels it's a gcc local label, so
4975      it must be in an outer scope, so jumping to it is always OK.  */
4976   if (lab == 0)
4977     return;
4978
4979   if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4980       && !identified)
4981     {
4982       cp_pedwarn_at ("jump to label `%D'", decl);
4983       pedwarn ("  from here");
4984       identified = 1;
4985     }
4986
4987   for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4988     {
4989       tree b = TREE_VALUE (bad);
4990       int u = decl_jump_unsafe (b);
4991
4992       if (u > 1 && DECL_ARTIFICIAL (b))
4993         /* Can't skip init of __exception_info.  */
4994         cp_error_at ("  enters catch block", b);
4995       else if (u > 1)
4996         cp_error_at ("  skips initialization of `%#D'", b);
4997       else
4998         cp_pedwarn_at ("  enters scope of non-POD `%#D'", b);
4999     }
5000
5001   if (lab->in_try_scope)
5002     error ("  enters try block");
5003   else if (lab->in_catch_scope)
5004     error ("  enters catch block");
5005 }
5006
5007 /* Define a label, specifying the location in the source file.
5008    Return the LABEL_DECL node for the label, if the definition is valid.
5009    Otherwise return 0.  */
5010
5011 tree
5012 define_label (const char* filename, int line, tree name)
5013 {
5014   tree decl = lookup_label (name);
5015   struct named_label_list *ent;
5016   register struct cp_binding_level *p;
5017
5018   timevar_push (TV_NAME_LOOKUP);
5019   for (ent = named_labels; ent; ent = ent->next)
5020     if (ent->label_decl == decl)
5021       break;
5022
5023   /* After labels, make any new cleanups in the function go into their
5024      own new (temporary) binding contour.  */
5025   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5026     p->more_cleanups_ok = 0;
5027
5028   if (name == get_identifier ("wchar_t"))
5029     pedwarn ("label named wchar_t");
5030
5031   if (DECL_INITIAL (decl) != NULL_TREE)
5032     {
5033       error ("duplicate label `%D'", decl);
5034       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5035     }
5036   else
5037     {
5038       /* Mark label as having been defined.  */
5039       DECL_INITIAL (decl) = error_mark_node;
5040       /* Say where in the source.  */
5041       DECL_SOURCE_FILE (decl) = filename;
5042       DECL_SOURCE_LINE (decl) = line;
5043       if (ent)
5044         {
5045           ent->names_in_scope = current_binding_level->names;
5046           ent->binding_level = current_binding_level;
5047         }
5048       check_previous_gotos (decl);
5049       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5050     }
5051   timevar_pop (TV_NAME_LOOKUP);
5052 }
5053
5054 struct cp_switch
5055 {
5056   struct cp_binding_level *level;
5057   struct cp_switch *next;
5058   /* The SWITCH_STMT being built.  */
5059   tree switch_stmt;
5060   /* A splay-tree mapping the low element of a case range to the high
5061      element, or NULL_TREE if there is no high element.  Used to
5062      determine whether or not a new case label duplicates an old case
5063      label.  We need a tree, rather than simply a hash table, because
5064      of the GNU case range extension.  */
5065   splay_tree cases;
5066 };
5067
5068 /* A stack of the currently active switch statements.  The innermost
5069    switch statement is on the top of the stack.  There is no need to
5070    mark the stack for garbage collection because it is only active
5071    during the processing of the body of a function, and we never
5072    collect at that point.  */
5073
5074 static struct cp_switch *switch_stack;
5075
5076 /* Called right after a switch-statement condition is parsed.
5077    SWITCH_STMT is the switch statement being parsed.  */
5078
5079 void
5080 push_switch (tree switch_stmt)
5081 {
5082   struct cp_switch *p
5083     = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
5084   p->level = current_binding_level;
5085   p->next = switch_stack;
5086   p->switch_stmt = switch_stmt;
5087   p->cases = splay_tree_new (case_compare, NULL, NULL);
5088   switch_stack = p;
5089 }
5090
5091 void
5092 pop_switch (void)
5093 {
5094   struct cp_switch *cs;
5095
5096   cs = switch_stack;
5097   splay_tree_delete (cs->cases);
5098   switch_stack = switch_stack->next;
5099   free (cs);
5100 }
5101
5102 /* Note that we've seen a definition of a case label, and complain if this
5103    is a bad place for one.  */
5104
5105 tree
5106 finish_case_label (tree low_value, tree high_value)
5107 {
5108   tree cond, r;
5109   register struct cp_binding_level *p;
5110
5111   if (! switch_stack)
5112     {
5113       if (high_value)
5114         error ("case label not within a switch statement");
5115       else if (low_value)
5116         error ("case label `%E' not within a switch statement",
5117                   low_value);
5118       else
5119         error ("`default' label not within a switch statement");
5120       return NULL_TREE;
5121     }
5122
5123   if (processing_template_decl)
5124     {
5125       tree label;
5126
5127       /* For templates, just add the case label; we'll do semantic
5128          analysis at instantiation-time.  */
5129       label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5130       return add_stmt (build_case_label (low_value, high_value, label));
5131     }
5132
5133   /* Find the condition on which this switch statement depends.  */
5134   cond = SWITCH_COND (switch_stack->switch_stmt);
5135   if (cond && TREE_CODE (cond) == TREE_LIST)
5136     cond = TREE_VALUE (cond);
5137
5138   r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5139
5140   check_switch_goto (switch_stack->level);
5141
5142   /* After labels, make any new cleanups in the function go into their
5143      own new (temporary) binding contour.  */
5144   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5145     p->more_cleanups_ok = 0;
5146
5147   return r;
5148 }
5149 \f
5150 /* Return the list of declarations of the current level.
5151    Note that this list is in reverse order unless/until
5152    you nreverse it; and when you do nreverse it, you must
5153    store the result back using `storedecls' or you will lose.  */
5154
5155 tree
5156 getdecls (void)
5157 {
5158   return current_binding_level->names;
5159 }
5160
5161 /* Return the list of type-tags (for structs, etc) of the current level.  */
5162
5163 tree
5164 gettags (void)
5165 {
5166   return current_binding_level->tags;
5167 }
5168
5169 /* Store the list of declarations of the current level.
5170    This is done for the parameter declarations of a function being defined,
5171    after they are modified in the light of any missing parameters.  */
5172
5173 static void
5174 storedecls (tree decls)
5175 {
5176   current_binding_level->names = decls;
5177 }
5178
5179 /* Similarly, store the list of tags of the current level.  */
5180
5181 void
5182 storetags (tree tags)
5183 {
5184   current_binding_level->tags = tags;
5185 }
5186 \f
5187 /* Return the type that should be used when TYPE's name is preceded
5188    by a tag such as 'struct' or 'union', or null if the name cannot
5189    be used in this way.
5190
5191    For example, when processing the third line of:
5192
5193         struct A;
5194         typedef struct A A;
5195         struct A;
5196
5197    lookup of A will find the typedef.  Given A's typedef, this function
5198    will return the type associated with "struct A".  For the tag to be
5199    anything other than TYPE, TYPE must be a typedef whose original type
5200    has the same name and context as TYPE itself.
5201
5202    It is not valid for a typedef of an anonymous type to be used with
5203    an explicit tag:
5204
5205        typedef struct { ... } B;
5206        struct B;
5207
5208    Return null for this case.  */
5209
5210 static tree
5211 follow_tag_typedef (tree type)
5212 {
5213   tree original;
5214
5215   original = original_type (type);
5216   if (! TYPE_NAME (original))
5217     return NULL_TREE;
5218   if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5219       && (CP_DECL_CONTEXT (TYPE_NAME (original))
5220           == CP_DECL_CONTEXT (TYPE_NAME (type)))
5221       && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5222     return original;
5223   else
5224     return NULL_TREE;
5225 }
5226
5227 /* Given NAME, an IDENTIFIER_NODE,
5228    return the structure (or union or enum) definition for that name.
5229    Searches binding levels from BINDING_LEVEL up to the global level.
5230    If THISLEVEL_ONLY is nonzero, searches only the specified context
5231    (but skips any tag-transparent contexts to find one that is
5232    meaningful for tags).
5233    FORM says which kind of type the caller wants;
5234    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5235    If the wrong kind of type is found, and it's not a template, an error is
5236    reported.  */
5237
5238 static tree
5239 lookup_tag (enum tree_code form,
5240             tree name,
5241             struct cp_binding_level* binding_level,
5242             int thislevel_only)
5243 {
5244   register struct cp_binding_level *level;
5245   /* Nonzero if, we should look past a template parameter level, even
5246      if THISLEVEL_ONLY.  */
5247   int allow_template_parms_p = 1;
5248
5249   timevar_push (TV_NAME_LOOKUP);
5250   for (level = binding_level; level; level = level->level_chain)
5251     {
5252       register tree tail;
5253       if (ANON_AGGRNAME_P (name))
5254         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5255           {
5256             /* There's no need for error checking here, because
5257                anon names are unique throughout the compilation.  */
5258             if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5259               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5260           }
5261       else if (level->namespace_p)
5262         /* Do namespace lookup.  */
5263         for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5264           {
5265             tree old = binding_for_name (name, tail);
5266
5267             /* If we just skipped past a template parameter level,
5268                even though THISLEVEL_ONLY, and we find a template
5269                class declaration, then we use the _TYPE node for the
5270                template.  See the example below.  */
5271             if (thislevel_only && !allow_template_parms_p
5272                 && old && BINDING_VALUE (old)
5273                 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5274               old = TREE_TYPE (BINDING_VALUE (old));
5275             else
5276               old = BINDING_TYPE (old);
5277
5278             if (old)
5279               {
5280                 /* We've found something at this binding level.  If it is
5281                    a typedef, extract the tag it refers to.  Lookup fails
5282                    if the typedef doesn't refer to a taggable type.  */
5283                 old = follow_tag_typedef (old);
5284                 if (!old)
5285                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5286                 if (TREE_CODE (old) != form
5287                     && (form == ENUMERAL_TYPE
5288                         || TREE_CODE (old) == ENUMERAL_TYPE))
5289                   {
5290                     error ("`%#D' redeclared as %C", old, form);
5291                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5292                   }
5293                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5294               }
5295             if (thislevel_only || tail == global_namespace)
5296               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5297           }
5298       else
5299         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5300           {
5301             if (TREE_PURPOSE (tail) == name)
5302               {
5303                 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5304                 
5305                 if (code != form
5306                     && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5307                   {
5308                     /* Definition isn't the kind we were looking for.  */
5309                     error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5310                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5311                   }
5312                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5313               }
5314           }
5315       if (thislevel_only && ! level->tag_transparent)
5316         {
5317           if (level->template_parms_p && allow_template_parms_p)
5318             {
5319               /* We must deal with cases like this:
5320
5321                    template <class T> struct S;
5322                    template <class T> struct S {};
5323
5324                  When looking up `S', for the second declaration, we
5325                  would like to find the first declaration.  But, we
5326                  are in the pseudo-global level created for the
5327                  template parameters, rather than the (surrounding)
5328                  namespace level.  Thus, we keep going one more level,
5329                  even though THISLEVEL_ONLY is nonzero.  */
5330               allow_template_parms_p = 0;
5331               continue;
5332             }
5333           else
5334             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5335         }
5336     }
5337   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5338 }
5339
5340 /* Given a type, find the tag that was defined for it and return the tag name.
5341    Otherwise return 0.  However, the value can never be 0
5342    in the cases in which this is used.
5343
5344    C++: If NAME is nonzero, this is the new name to install.  This is
5345    done when replacing anonymous tags with real tag names.  */
5346
5347 static tree
5348 lookup_tag_reverse (tree type, tree name)
5349 {
5350   register struct cp_binding_level *level;
5351
5352   timevar_push (TV_NAME_LOOKUP);
5353   for (level = current_binding_level; level; level = level->level_chain)
5354     {
5355       register tree tail;
5356       for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5357         {
5358           if (TREE_VALUE (tail) == type)
5359             {
5360               if (name)
5361                 TREE_PURPOSE (tail) = name;
5362               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_PURPOSE (tail));
5363             }
5364         }
5365     }
5366   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5367 }
5368 \f
5369 /* Look up NAME in the NAMESPACE.  */
5370
5371 tree
5372 lookup_namespace_name (tree namespace, tree name)
5373 {
5374   tree val;
5375   tree template_id = NULL_TREE;
5376
5377   timevar_push (TV_NAME_LOOKUP);
5378   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5379
5380   if (TREE_CODE (name) == NAMESPACE_DECL)
5381     /* This happens for A::B<int> when B is a namespace.  */
5382     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5383   else if (TREE_CODE (name) == TEMPLATE_DECL)
5384     {
5385       /* This happens for A::B where B is a template, and there are no
5386          template arguments.  */
5387       error ("invalid use of `%D'", name);
5388       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5389     }
5390
5391   namespace = ORIGINAL_NAMESPACE (namespace);
5392
5393   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5394     {
5395       template_id = name;
5396       name = TREE_OPERAND (name, 0);
5397       if (TREE_CODE (name) == OVERLOAD)
5398         name = DECL_NAME (OVL_CURRENT (name));
5399       else if (DECL_P (name))
5400         name = DECL_NAME (name);
5401     }
5402
5403   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5404
5405   val = make_node (CPLUS_BINDING);
5406   if (!qualified_lookup_using_namespace (name, namespace, val, 0))
5407     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5408
5409   if (BINDING_VALUE (val))
5410     {
5411       val = BINDING_VALUE (val);
5412
5413       if (template_id)
5414         {
5415           if (DECL_CLASS_TEMPLATE_P (val))
5416             val = lookup_template_class (val,
5417                                          TREE_OPERAND (template_id, 1),
5418                                          /*in_decl=*/NULL_TREE,
5419                                          /*context=*/NULL_TREE,
5420                                          /*entering_scope=*/0,
5421                                          tf_error | tf_warning);
5422           else if (DECL_FUNCTION_TEMPLATE_P (val)
5423                    || TREE_CODE (val) == OVERLOAD)
5424             val = lookup_template_function (val,
5425                                             TREE_OPERAND (template_id, 1));
5426           else
5427             {
5428               error ("`%D::%D' is not a template",
5429                         namespace, name);
5430               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5431             }
5432         }
5433
5434       /* If we have a single function from a using decl, pull it out.  */
5435       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5436         val = OVL_FUNCTION (val);
5437
5438       /* Ignore built-in functions that haven't been prototyped yet.  */
5439       if (!val || !DECL_P(val)
5440           || !DECL_LANG_SPECIFIC(val)
5441           || !DECL_ANTICIPATED (val))
5442         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5443     }
5444
5445   error ("`%D' undeclared in namespace `%D'", name, namespace);
5446   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5447 }
5448
5449 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5450
5451 static hashval_t
5452 typename_hash (const void* k)
5453 {
5454   hashval_t hash;
5455   tree t = (tree) k;
5456
5457   hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5458           ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5459
5460   return hash;
5461 }
5462
5463 /* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5464
5465 static int
5466 typename_compare (const void * k1, const void * k2)
5467 {
5468   tree t1;
5469   tree t2;
5470   tree d1;
5471   tree d2;
5472
5473   t1 = (tree) k1;
5474   t2 = (tree) k2;
5475   d1 = TYPE_NAME (t1);
5476   d2 = TYPE_NAME (t2);
5477
5478   return (DECL_NAME (d1) == DECL_NAME (d2)
5479           && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5480           && ((TREE_TYPE (t1) != NULL_TREE)
5481               == (TREE_TYPE (t2) != NULL_TREE))
5482           && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5483           && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5484 }
5485
5486 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5487    the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5488    is non-NULL, this type is being created by the implicit typename
5489    extension, and BASE_TYPE is a type named `t' in some base class of
5490    `T' which depends on template parameters.
5491
5492    Returns the new TYPENAME_TYPE.  */
5493
5494 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5495
5496 tree
5497 build_typename_type (tree context, tree name, tree fullname)
5498 {
5499   tree t;
5500   tree d;
5501   PTR *e;
5502
5503   if (typename_htab == NULL)
5504     {
5505       typename_htab = htab_create_ggc (61, &typename_hash, 
5506                                        &typename_compare, NULL);
5507     }
5508
5509   /* Build the TYPENAME_TYPE.  */
5510   t = make_aggr_type (TYPENAME_TYPE);
5511   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5512   TYPENAME_TYPE_FULLNAME (t) = fullname;
5513
5514   /* Build the corresponding TYPE_DECL.  */
5515   d = build_decl (TYPE_DECL, name, t);
5516   TYPE_NAME (TREE_TYPE (d)) = d;
5517   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5518   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5519   DECL_ARTIFICIAL (d) = 1;
5520
5521   /* See if we already have this type.  */
5522   e = htab_find_slot (typename_htab, t, INSERT);
5523   if (*e)
5524     t = (tree) *e;
5525   else
5526     *e = t;
5527
5528   return t;
5529 }
5530
5531 /* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5532    unless an error occurs, in which case error_mark_node is returned.
5533    If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5534    set, we return that, rather than the _TYPE it corresponds to, in
5535    other cases we look through the type decl.  If TF_ERROR is set,
5536    complain about errors, otherwise be quiet.  */
5537
5538 tree
5539 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5540 {
5541   tree fullname;
5542
5543   if (TYPE_P (name))
5544     {
5545       if (!(TYPE_LANG_SPECIFIC (name)
5546             && (CLASSTYPE_IS_TEMPLATE (name)
5547                 || CLASSTYPE_USE_TEMPLATE (name))))
5548         name = TYPE_IDENTIFIER (name);
5549       else
5550         /* Create a TEMPLATE_ID_EXPR for the type.  */
5551         name = build_nt (TEMPLATE_ID_EXPR,
5552                          CLASSTYPE_TI_TEMPLATE (name),
5553                          CLASSTYPE_TI_ARGS (name));
5554     }
5555   else if (TREE_CODE (name) == TYPE_DECL)
5556     name = DECL_NAME (name);
5557
5558   fullname = name;
5559
5560   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5561     {
5562       name = TREE_OPERAND (name, 0);
5563       if (TREE_CODE (name) == TEMPLATE_DECL)
5564         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5565     }
5566   if (TREE_CODE (name) == TEMPLATE_DECL)
5567     {
5568       error ("`%D' used without template parameters", name);
5569       return error_mark_node;
5570     }
5571   if (TREE_CODE (name) != IDENTIFIER_NODE)
5572     abort ();
5573
5574   if (TREE_CODE (context) == NAMESPACE_DECL)
5575     {
5576       /* We can get here from typename_sub0 in the explicit_template_type
5577          expansion.  Just fail.  */
5578       if (complain & tf_error)
5579         error ("no class template named `%#T' in `%#T'",
5580                   name, context);
5581       return error_mark_node;
5582     }
5583
5584   if (! uses_template_parms (context)
5585       || currently_open_class (context))
5586     {
5587       if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5588         {
5589           tree tmpl = NULL_TREE;
5590           if (IS_AGGR_TYPE (context))
5591             tmpl = lookup_field (context, name, 0, false);
5592           if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5593             {
5594               if (complain & tf_error)
5595                 error ("no class template named `%#T' in `%#T'",
5596                           name, context);
5597               return error_mark_node;
5598             }
5599
5600           if (complain & tf_error)
5601             {
5602               if (complain & tf_parsing)
5603                 perform_or_defer_access_check (context, tmpl);
5604               else
5605                 enforce_access (context, tmpl);
5606             }
5607
5608           return lookup_template_class (tmpl,
5609                                         TREE_OPERAND (fullname, 1),
5610                                         NULL_TREE, context,
5611                                         /*entering_scope=*/0,
5612                                         tf_error | tf_warning);
5613         }
5614       else
5615         {
5616           tree t;
5617
5618           if (!IS_AGGR_TYPE (context))
5619             {
5620               if (complain & tf_error)
5621                 error ("no type named `%#T' in `%#T'", name, context);
5622               return error_mark_node;
5623             }
5624
5625           t = lookup_field (context, name, 0, true);
5626           if (t)
5627             {
5628               if (TREE_CODE (t) != TYPE_DECL)
5629                 {
5630                   if (complain & tf_error)
5631                     error ("no type named `%#T' in `%#T'", name, context);
5632                   return error_mark_node;
5633                 }
5634
5635               if (complain & tf_error)
5636                 {
5637                   if (complain & tf_parsing)
5638                     perform_or_defer_access_check (context, t);
5639                   else
5640                     enforce_access (context, t);
5641                 }
5642
5643               if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5644                 t = TREE_TYPE (t);
5645               
5646               return t;
5647             }
5648         }
5649     }
5650
5651   /* If the CONTEXT is not a template type, then either the field is
5652      there now or its never going to be.  */
5653   if (!uses_template_parms (context))
5654     {
5655       if (complain & tf_error)
5656         error ("no type named `%#T' in `%#T'", name, context);
5657       return error_mark_node;
5658     }
5659
5660   return build_typename_type (context, name, fullname);
5661 }
5662
5663 /* Resolve `CONTEXT::template NAME'.  Returns an appropriate type,
5664    unless an error occurs, in which case error_mark_node is returned.
5665    If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5666    corresponds to.  If COMPLAIN zero, don't complain about any errors
5667    that occur.  */
5668
5669 tree
5670 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5671 {
5672   tree t;
5673   tree d;
5674
5675   if (TYPE_P (name))
5676     name = TYPE_IDENTIFIER (name);
5677   else if (DECL_P (name))
5678     name = DECL_NAME (name);
5679   if (TREE_CODE (name) != IDENTIFIER_NODE)
5680     abort ();
5681
5682   if (!uses_template_parms (context)
5683       || currently_open_class (context))
5684     {
5685       tree tmpl = NULL_TREE;
5686
5687       if (IS_AGGR_TYPE (context))
5688         tmpl = lookup_field (context, name, 0, false);
5689
5690       if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5691         {
5692           if (complain & tf_error)
5693             error ("no class template named `%#T' in `%#T'", name, context);
5694           return error_mark_node;
5695         }
5696       
5697       if (complain & tf_error)
5698         {
5699           if (complain & tf_parsing)
5700             perform_or_defer_access_check (context, tmpl);
5701           else
5702             enforce_access (context, tmpl);
5703         }
5704
5705       return tmpl;
5706     }
5707
5708   /* Build the UNBOUND_CLASS_TEMPLATE.  */
5709   t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5710   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5711   TREE_TYPE (t) = NULL_TREE;
5712
5713   /* Build the corresponding TEMPLATE_DECL.  */
5714   d = build_decl (TEMPLATE_DECL, name, t);
5715   TYPE_NAME (TREE_TYPE (d)) = d;
5716   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5717   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5718   DECL_ARTIFICIAL (d) = 1;
5719
5720   return t;
5721 }
5722
5723 /* Select the right _DECL from multiple choices.  */
5724
5725 static tree
5726 select_decl (tree binding, int flags)
5727 {
5728   tree val;
5729   val = BINDING_VALUE (binding);
5730
5731   timevar_push (TV_NAME_LOOKUP);
5732   if (LOOKUP_NAMESPACES_ONLY (flags))
5733     {
5734       /* We are not interested in types.  */
5735       if (val && TREE_CODE (val) == NAMESPACE_DECL)
5736         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5737       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5738     }
5739
5740   /* If we could have a type and
5741      we have nothing or we need a type and have none.  */
5742   if (BINDING_TYPE (binding)
5743       && (!val || ((flags & LOOKUP_PREFER_TYPES)
5744                    && TREE_CODE (val) != TYPE_DECL)))
5745     val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5746   /* Don't return non-types if we really prefer types.  */
5747   else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
5748            && (TREE_CODE (val) != TEMPLATE_DECL
5749                || !DECL_CLASS_TEMPLATE_P (val)))
5750     val = NULL_TREE;
5751
5752   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5753 }
5754
5755 /* Unscoped lookup of a global: iterate over current namespaces,
5756    considering using-directives.  If SPACESP is non-NULL, store a list
5757    of the namespaces we've considered in it.  */
5758
5759 tree
5760 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5761 {
5762   tree b = make_node (CPLUS_BINDING);
5763   tree initial = current_decl_namespace ();
5764   tree scope = initial;
5765   tree siter;
5766   struct cp_binding_level *level;
5767   tree val = NULL_TREE;
5768
5769   timevar_push (TV_NAME_LOOKUP);
5770   if (spacesp)
5771     *spacesp = NULL_TREE;
5772
5773   for (; !val; scope = CP_DECL_CONTEXT (scope))
5774     {
5775       if (spacesp)
5776         *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5777       val = binding_for_name (name, scope);
5778
5779       /* Ignore anticipated built-in functions.  */
5780       if (val && BINDING_VALUE (val)
5781           && DECL_P (BINDING_VALUE (val))
5782           && DECL_LANG_SPECIFIC (BINDING_VALUE (val))
5783           && DECL_ANTICIPATED (BINDING_VALUE (val)))
5784         {
5785           BINDING_VALUE (b) = NULL_TREE;
5786           BINDING_TYPE (b) = NULL_TREE;
5787         }
5788       else
5789         {
5790           /* Initialize binding for this context.  */
5791           BINDING_VALUE (b) = BINDING_VALUE (val);
5792           BINDING_TYPE (b) = BINDING_TYPE (val);
5793         }
5794
5795       /* Add all _DECLs seen through local using-directives.  */
5796       for (level = current_binding_level;
5797            !level->namespace_p;
5798            level = level->level_chain)
5799         if (!lookup_using_namespace (name, b, level->using_directives,
5800                                      scope, flags, spacesp))
5801           /* Give up because of error.  */
5802           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5803
5804       /* Add all _DECLs seen through global using-directives.  */
5805       /* XXX local and global using lists should work equally.  */
5806       siter = initial;
5807       while (1)
5808         {
5809           if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
5810                                        scope, flags, spacesp))
5811             /* Give up because of error.  */
5812             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5813           if (siter == scope) break;
5814           siter = CP_DECL_CONTEXT (siter);
5815         }
5816
5817       val = select_decl (b, flags);
5818       if (scope == global_namespace)
5819         break;
5820     }
5821   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5822 }
5823
5824 /* Combine prefer_type and namespaces_only into flags.  */
5825
5826 static int
5827 lookup_flags (int prefer_type, int namespaces_only)
5828 {
5829   if (namespaces_only)
5830     return LOOKUP_PREFER_NAMESPACES;
5831   if (prefer_type > 1)
5832     return LOOKUP_PREFER_TYPES;
5833   if (prefer_type > 0)
5834     return LOOKUP_PREFER_BOTH;
5835   return 0;
5836 }
5837
5838 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5839    ignore it or not.  Subroutine of lookup_name_real.  */
5840
5841 static tree
5842 qualify_lookup (tree val, int flags)
5843 {
5844   if (val == NULL_TREE)
5845     return val;
5846   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5847     return val;
5848   if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5849     return val;
5850   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5851     return NULL_TREE;
5852   return val;
5853 }
5854
5855 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5856    or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
5857    bindings.  
5858
5859    Returns a DECL (or OVERLOAD, or BASELINK) representing the
5860    declaration found.  */
5861
5862 tree
5863 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5864 {
5865   if (TREE_CODE (scope) == NAMESPACE_DECL)
5866     {
5867       tree val;
5868
5869       val = make_node (CPLUS_BINDING);
5870       flags |= LOOKUP_COMPLAIN;
5871       if (is_type_p)
5872         flags |= LOOKUP_PREFER_TYPES;
5873       if (!qualified_lookup_using_namespace (name, scope, val, flags))
5874         return NULL_TREE;
5875       return select_decl (val, flags);
5876     }
5877   else
5878     return lookup_member (scope, name, 0, is_type_p);
5879 }
5880
5881 /* Check to see whether or not DECL is a variable that would have been
5882    in scope under the ARM, but is not in scope under the ANSI/ISO
5883    standard.  If so, issue an error message.  If name lookup would
5884    work in both cases, but return a different result, this function
5885    returns the result of ANSI/ISO lookup.  Otherwise, it returns
5886    DECL.  */
5887
5888 tree
5889 check_for_out_of_scope_variable (tree decl)
5890 {
5891   tree shadowed;
5892
5893   /* We only care about out of scope variables.  */
5894   if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5895     return decl;
5896
5897   shadowed = DECL_SHADOWED_FOR_VAR (decl);
5898   while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5899          && DECL_DEAD_FOR_LOCAL (shadowed))
5900     shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5901   if (!shadowed)
5902     shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5903   if (shadowed)
5904     {
5905       if (!DECL_ERROR_REPORTED (decl))
5906         {
5907           warning ("name lookup of `%D' changed",
5908                       DECL_NAME (decl));
5909           cp_warning_at ("  matches this `%D' under ISO standard rules",
5910                          shadowed);
5911           cp_warning_at ("  matches this `%D' under old rules", decl);
5912           DECL_ERROR_REPORTED (decl) = 1;
5913         }
5914       return shadowed;
5915     }
5916
5917   /* If we have already complained about this declaration, there's no
5918      need to do it again.  */
5919   if (DECL_ERROR_REPORTED (decl))
5920     return decl;
5921
5922   DECL_ERROR_REPORTED (decl) = 1;
5923   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5924     {
5925       error ("name lookup of `%D' changed for new ISO `for' scoping",
5926              DECL_NAME (decl));
5927       cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
5928       return error_mark_node;
5929     }
5930   else
5931     {
5932       pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5933                DECL_NAME (decl));
5934       cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
5935     }
5936
5937   return decl;
5938 }
5939
5940 /* Look up NAME in the current binding level and its superiors in the
5941    namespace of variables, functions and typedefs.  Return a ..._DECL
5942    node of some kind representing its definition if there is only one
5943    such declaration, or return a TREE_LIST with all the overloaded
5944    definitions if there are many, or return 0 if it is undefined.
5945
5946    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5947    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5948    Otherwise we prefer non-TYPE_DECLs.
5949
5950    If NONCLASS is nonzero, we don't look for the NAME in class scope,
5951    using IDENTIFIER_CLASS_VALUE.  */
5952
5953 tree
5954 lookup_name_real (tree name, 
5955                   int prefer_type, 
5956                   int nonclass, 
5957                   int namespaces_only,
5958                   int flags)
5959 {
5960   tree t;
5961   tree val = NULL_TREE;
5962
5963   timevar_push (TV_NAME_LOOKUP);
5964   /* Conversion operators are handled specially because ordinary
5965      unqualified name lookup will not find template conversion
5966      operators.  */
5967   if (IDENTIFIER_TYPENAME_P (name)) 
5968     {
5969       struct cp_binding_level *level;
5970
5971       for (level = current_binding_level; 
5972            level && !level->namespace_p; 
5973            level = level->level_chain)
5974         {
5975           tree class_type;
5976           tree operators;
5977           
5978           /* A conversion operator can only be declared in a class 
5979              scope.  */
5980           if (level->parm_flag != 2)
5981             continue;
5982           
5983           /* Lookup the conversion operator in the class.  */
5984           class_type = level->this_class;
5985           operators = lookup_fnfields (class_type, name, /*protect=*/0);
5986           if (operators)
5987             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5988         }
5989
5990       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5991     }
5992
5993   flags |= lookup_flags (prefer_type, namespaces_only);
5994
5995   /* First, look in non-namespace scopes.  */
5996
5997   if (current_class_type == NULL_TREE)
5998     nonclass = 1;
5999
6000   for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
6001     {
6002       tree binding;
6003
6004       if (!LOCAL_BINDING_P (t) && nonclass)
6005         /* We're not looking for class-scoped bindings, so keep going.  */
6006         continue;
6007
6008       /* If this is the kind of thing we're looking for, we're done.  */
6009       if (qualify_lookup (BINDING_VALUE (t), flags))
6010         binding = BINDING_VALUE (t);
6011       else if ((flags & LOOKUP_PREFER_TYPES)
6012                && qualify_lookup (BINDING_TYPE (t), flags))
6013         binding = BINDING_TYPE (t);
6014       else
6015         binding = NULL_TREE;
6016
6017       if (binding)
6018         {
6019           val = binding;
6020           break;
6021         }
6022     }
6023
6024   /* Now lookup in namespace scopes.  */
6025   if (!val)
6026     {
6027       t = unqualified_namespace_lookup (name, flags, 0);
6028       if (t)
6029         val = t;
6030     }
6031
6032   if (val)
6033     {
6034       /* If we have a single function from a using decl, pull it out.  */
6035       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
6036         val = OVL_FUNCTION (val);
6037     }
6038
6039   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6040 }
6041
6042 tree
6043 lookup_name_nonclass (tree name)
6044 {
6045   return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
6046 }
6047
6048 tree
6049 lookup_function_nonclass (tree name, tree args)
6050 {
6051   return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
6052 }
6053
6054 tree
6055 lookup_name (tree name, int prefer_type)
6056 {
6057   return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
6058 }
6059
6060 /* Similar to `lookup_name' but look only in the innermost non-class
6061    binding level.  */
6062
6063 tree
6064 lookup_name_current_level (tree name)
6065 {
6066   struct cp_binding_level *b;
6067   tree t = NULL_TREE;
6068
6069   timevar_push (TV_NAME_LOOKUP);
6070   b = current_binding_level;
6071   while (b->parm_flag == 2)
6072     b = b->level_chain;
6073
6074   if (b->namespace_p)
6075     {
6076       t = IDENTIFIER_NAMESPACE_VALUE (name);
6077
6078       /* extern "C" function() */
6079       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6080         t = TREE_VALUE (t);
6081     }
6082   else if (IDENTIFIER_BINDING (name)
6083            && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6084     {
6085       while (1)
6086         {
6087           if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6088             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
6089
6090           if (b->keep == 2)
6091             b = b->level_chain;
6092           else
6093             break;
6094         }
6095     }
6096
6097   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6098 }
6099
6100 /* Like lookup_name_current_level, but for types.  */
6101
6102 tree
6103 lookup_type_current_level (tree name)
6104 {
6105   register tree t = NULL_TREE;
6106
6107   timevar_push (TV_NAME_LOOKUP);
6108   my_friendly_assert (! current_binding_level->namespace_p, 980716);
6109
6110   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6111       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6112     {
6113       struct cp_binding_level *b = current_binding_level;
6114       while (1)
6115         {
6116           if (purpose_member (name, b->type_shadowed))
6117             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
6118                                     REAL_IDENTIFIER_TYPE_VALUE (name));
6119           if (b->keep == 2)
6120             b = b->level_chain;
6121           else
6122             break;
6123         }
6124     }
6125
6126   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6127 }
6128
6129 \f
6130 /* Push the declarations of builtin types into the namespace.
6131    RID_INDEX is the index of the builtin type
6132    in the array RID_POINTERS.  NAME is the name used when looking
6133    up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
6134
6135 void
6136 record_builtin_type (enum rid rid_index, 
6137                      const char* name, 
6138                      tree type)
6139 {
6140   tree rname = NULL_TREE, tname = NULL_TREE;
6141   tree tdecl = NULL_TREE;
6142
6143   if ((int) rid_index < (int) RID_MAX)
6144     rname = ridpointers[(int) rid_index];
6145   if (name)
6146     tname = get_identifier (name);
6147
6148   if (tname)
6149     {
6150       tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6151       set_identifier_type_value (tname, NULL_TREE);
6152       if ((int) rid_index < (int) RID_MAX)
6153         /* Built-in types live in the global namespace.  */
6154         SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6155     }
6156   if (rname != NULL_TREE)
6157     {
6158       if (tname != NULL_TREE)
6159         {
6160           set_identifier_type_value (rname, NULL_TREE);
6161           SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6162         }
6163       else
6164         {
6165           tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6166           set_identifier_type_value (rname, NULL_TREE);
6167         }
6168     }
6169 }
6170
6171 /* Record one of the standard Java types.
6172  * Declare it as having the given NAME.
6173  * If SIZE > 0, it is the size of one of the integral types;
6174  * otherwise it is the negative of the size of one of the other types.  */
6175
6176 static tree
6177 record_builtin_java_type (const char* name, int size)
6178 {
6179   tree type, decl;
6180   if (size > 0)
6181     type = make_signed_type (size);
6182   else if (size > -32)
6183     { /* "__java_char" or ""__java_boolean".  */
6184       type = make_unsigned_type (-size);
6185       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6186     }
6187   else
6188     { /* "__java_float" or ""__java_double".  */
6189       type = make_node (REAL_TYPE);
6190       TYPE_PRECISION (type) = - size;
6191       layout_type (type);
6192     }
6193   record_builtin_type (RID_MAX, name, type);
6194   decl = TYPE_NAME (type);
6195
6196   /* Suppress generate debug symbol entries for these types,
6197      since for normal C++ they are just clutter.
6198      However, push_lang_context undoes this if extern "Java" is seen.  */
6199   DECL_IGNORED_P (decl) = 1;
6200
6201   TYPE_FOR_JAVA (type) = 1;
6202   return type;
6203 }
6204
6205 /* Push a type into the namespace so that the back-ends ignore it.  */
6206
6207 static void
6208 record_unknown_type (tree type, const char* name)
6209 {
6210   tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6211   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6212   DECL_IGNORED_P (decl) = 1;
6213   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6214   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6215   TYPE_ALIGN (type) = 1;
6216   TYPE_USER_ALIGN (type) = 0;
6217   TYPE_MODE (type) = TYPE_MODE (void_type_node);
6218 }
6219
6220 /* An string for which we should create an IDENTIFIER_NODE at
6221    startup.  */
6222
6223 typedef struct predefined_identifier
6224 {
6225   /* The name of the identifier.  */
6226   const char *const name;
6227   /* The place where the IDENTIFIER_NODE should be stored.  */
6228   tree *const node;
6229   /* Nonzero if this is the name of a constructor or destructor.  */
6230   const int ctor_or_dtor_p;
6231 } predefined_identifier;
6232
6233 /* Create all the predefined identifiers.  */
6234
6235 static void
6236 initialize_predefined_identifiers (void)
6237 {
6238   const predefined_identifier *pid;
6239
6240   /* A table of identifiers to create at startup.  */
6241   static const predefined_identifier predefined_identifiers[] = {
6242     { "C++", &lang_name_cplusplus, 0 },
6243     { "C", &lang_name_c, 0 },
6244     { "Java", &lang_name_java, 0 },
6245     { CTOR_NAME, &ctor_identifier, 1 },
6246     { "__base_ctor", &base_ctor_identifier, 1 },
6247     { "__comp_ctor", &complete_ctor_identifier, 1 },
6248     { DTOR_NAME, &dtor_identifier, 1 },
6249     { "__comp_dtor", &complete_dtor_identifier, 1 },
6250     { "__base_dtor", &base_dtor_identifier, 1 },
6251     { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6252     { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6253     { "nelts", &nelts_identifier, 0 },
6254     { THIS_NAME, &this_identifier, 0 },
6255     { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6256     { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6257     { "_vptr", &vptr_identifier, 0 },
6258     { "__vtt_parm", &vtt_parm_identifier, 0 },
6259     { "std", &std_identifier, 0 },
6260     { NULL, NULL, 0 }
6261   };
6262
6263   for (pid = predefined_identifiers; pid->name; ++pid)
6264     {
6265       *pid->node = get_identifier (pid->name);
6266       if (pid->ctor_or_dtor_p)
6267         IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6268     }
6269 }
6270
6271 /* Create the predefined scalar types of C,
6272    and some nodes representing standard constants (0, 1, (void *)0).
6273    Initialize the global binding level.
6274    Make definitions for built-in primitive functions.  */
6275
6276 void
6277 cxx_init_decl_processing (void)
6278 {
6279   tree void_ftype;
6280   tree void_ftype_ptr;
6281
6282   /* Create all the identifiers we need.  */
6283   initialize_predefined_identifiers ();
6284
6285   /* Fill in back-end hooks.  */
6286   lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6287
6288   /* Create the global variables.  */
6289   push_to_top_level ();
6290
6291   /* Enter the global namespace.  */
6292   my_friendly_assert (global_namespace == NULL_TREE, 375);
6293   push_namespace (get_identifier ("::"));
6294   global_namespace = current_namespace;
6295   current_lang_name = NULL_TREE;
6296
6297   /* Adjust various flags based on command-line settings.  */
6298   if (! flag_permissive && ! pedantic)
6299     flag_pedantic_errors = 1;
6300   if (!flag_no_inline)
6301     {
6302       flag_inline_trees = 1;
6303       flag_no_inline = 1;
6304     }
6305   if (flag_inline_functions)
6306     {
6307       flag_inline_trees = 2;
6308       flag_inline_functions = 0;
6309     }
6310
6311   /* Force minimum function alignment if using the least significant
6312      bit of function pointers to store the virtual bit.  */
6313   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6314       && force_align_functions_log < 1)
6315     force_align_functions_log = 1;
6316
6317   /* Initially, C.  */
6318   current_lang_name = lang_name_c;
6319
6320   current_function_decl = NULL_TREE;
6321   current_binding_level = NULL_BINDING_LEVEL;
6322   free_binding_level = NULL_BINDING_LEVEL;
6323
6324   build_common_tree_nodes (flag_signed_char);
6325
6326   error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6327   TREE_TYPE (error_mark_list) = error_mark_node;
6328
6329   /* Make the binding_level structure for global names.  */
6330   pushlevel (0);
6331   global_binding_level = current_binding_level;
6332   /* The global level is the namespace level of ::.  */
6333   NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6334   declare_namespace_level ();
6335
6336   VARRAY_TREE_INIT (global_binding_level->static_decls,
6337                     200,
6338                     "Static declarations");
6339
6340   /* Create the `std' namespace.  */
6341   push_namespace (std_identifier);
6342   std_node = current_namespace;
6343   pop_namespace ();
6344
6345   c_common_nodes_and_builtins ();
6346
6347   java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6348   java_short_type_node = record_builtin_java_type ("__java_short", 16);
6349   java_int_type_node = record_builtin_java_type ("__java_int", 32);
6350   java_long_type_node = record_builtin_java_type ("__java_long", 64);
6351   java_float_type_node = record_builtin_java_type ("__java_float", -32);
6352   java_double_type_node = record_builtin_java_type ("__java_double", -64);
6353   java_char_type_node = record_builtin_java_type ("__java_char", -16);
6354   java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6355
6356   integer_two_node = build_int_2 (2, 0);
6357   TREE_TYPE (integer_two_node) = integer_type_node;
6358   integer_three_node = build_int_2 (3, 0);
6359   TREE_TYPE (integer_three_node) = integer_type_node;
6360
6361   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6362   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6363   TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6364   TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6365   TYPE_PRECISION (boolean_type_node) = 1;
6366   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6367   boolean_false_node = build_int_2 (0, 0);
6368   TREE_TYPE (boolean_false_node) = boolean_type_node;
6369   boolean_true_node = build_int_2 (1, 0);
6370   TREE_TYPE (boolean_true_node) = boolean_type_node;
6371
6372   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6373
6374 #if 0
6375   record_builtin_type (RID_MAX, NULL, string_type_node);
6376 #endif
6377
6378   delta_type_node = ptrdiff_type_node;
6379   vtable_index_type = ptrdiff_type_node;
6380
6381   vtt_parm_type = build_pointer_type (const_ptr_type_node);
6382   void_ftype = build_function_type (void_type_node, void_list_node);
6383   void_ftype_ptr = build_function_type (void_type_node,
6384                                         tree_cons (NULL_TREE,
6385                                                    ptr_type_node, 
6386                                                    void_list_node));
6387   void_ftype_ptr
6388     = build_exception_variant (void_ftype_ptr, empty_except_spec);
6389
6390   /* C++ extensions */
6391
6392   unknown_type_node = make_node (UNKNOWN_TYPE);
6393   record_unknown_type (unknown_type_node, "unknown type");
6394
6395   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
6396   TREE_TYPE (unknown_type_node) = unknown_type_node;
6397
6398   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6399      result.  */
6400   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6401   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6402
6403   {
6404     /* Make sure we get a unique function type, so we can give
6405        its pointer type a name.  (This wins for gdb.) */
6406     tree vfunc_type = make_node (FUNCTION_TYPE);
6407     TREE_TYPE (vfunc_type) = integer_type_node;
6408     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6409     layout_type (vfunc_type);
6410
6411     vtable_entry_type = build_pointer_type (vfunc_type);
6412   }
6413   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6414
6415   vtbl_type_node
6416     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6417   layout_type (vtbl_type_node);
6418   vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6419   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6420   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6421   layout_type (vtbl_ptr_type_node);
6422   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6423
6424   push_namespace (get_identifier ("__cxxabiv1"));
6425   abi_node = current_namespace;
6426   pop_namespace ();
6427
6428   global_type_node = make_node (LANG_TYPE);
6429   record_unknown_type (global_type_node, "global type");
6430
6431   /* Now, C++.  */
6432   current_lang_name = lang_name_cplusplus;
6433
6434   {
6435     tree bad_alloc_type_node, newtype, deltype;
6436     tree ptr_ftype_sizetype;
6437
6438     push_namespace (std_identifier);
6439     bad_alloc_type_node 
6440       = xref_tag (class_type, get_identifier ("bad_alloc"), 
6441                   /*attributes=*/NULL_TREE, 1);
6442     pop_namespace ();
6443     ptr_ftype_sizetype 
6444       = build_function_type (ptr_type_node,
6445                              tree_cons (NULL_TREE,
6446                                         size_type_node,
6447                                         void_list_node));
6448     newtype = build_exception_variant
6449       (ptr_ftype_sizetype, add_exception_specifier
6450        (NULL_TREE, bad_alloc_type_node, -1));
6451     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6452     push_cp_library_fn (NEW_EXPR, newtype);
6453     push_cp_library_fn (VEC_NEW_EXPR, newtype);
6454     global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6455     push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6456   }
6457
6458   abort_fndecl
6459     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6460
6461   /* Perform other language dependent initializations.  */
6462   init_class_processing ();
6463   init_search_processing ();
6464   init_rtti_processing ();
6465
6466   if (flag_exceptions)
6467     init_exception_processing ();
6468
6469   if (! supports_one_only ())
6470     flag_weak = 0;
6471
6472   make_fname_decl = cp_make_fname_decl;
6473   start_fname_decls ();
6474
6475   /* Show we use EH for cleanups.  */
6476   using_eh_for_cleanups ();
6477
6478   /* Maintain consistency.  Perhaps we should just complain if they
6479      say -fwritable-strings?  */
6480   if (flag_writable_strings)
6481     flag_const_strings = 0;
6482 }
6483
6484 /* Generate an initializer for a function naming variable from
6485    NAME. NAME may be NULL, in which case we generate a special
6486    ERROR_MARK node which should be replaced later.  */
6487
6488 tree
6489 cp_fname_init (const char* name)
6490 {
6491   tree domain = NULL_TREE;
6492   tree type;
6493   tree init = NULL_TREE;
6494   size_t length = 0;
6495
6496   if (name)
6497     {
6498       length = strlen (name);
6499       domain = build_index_type (size_int (length));
6500       init = build_string (length + 1, name);
6501     }
6502   
6503   type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6504   type = build_cplus_array_type (type, domain);
6505
6506   if (init)
6507     TREE_TYPE (init) = type;
6508   else
6509     /* We don't know the value until instantiation time. Make
6510        something which will be digested now, but replaced later.  */
6511     init = build (ERROR_MARK, type);
6512   
6513   return init;
6514 }
6515
6516 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6517    decl, NAME is the initialization string and TYPE_DEP indicates whether
6518    NAME depended on the type of the function. We make use of that to detect
6519    __PRETTY_FUNCTION__ inside a template fn. This is being done
6520    lazily at the point of first use, so we musn't push the decl now.  */
6521
6522 static tree
6523 cp_make_fname_decl (tree id, int type_dep)
6524 {
6525   const char *const name = (type_dep && processing_template_decl
6526                       ? NULL : fname_as_string (type_dep));
6527   tree init = cp_fname_init (name);
6528   tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6529
6530   /* As we're using pushdecl_with_scope, we must set the context.  */
6531   DECL_CONTEXT (decl) = current_function_decl;
6532   DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6533       
6534   TREE_STATIC (decl) = 1;
6535   TREE_READONLY (decl) = 1;
6536   DECL_ARTIFICIAL (decl) = 1;
6537   DECL_INITIAL (decl) = init;
6538   
6539   TREE_USED (decl) = 1;
6540
6541   if (current_function_decl)
6542     {
6543       struct cp_binding_level *b = current_binding_level;
6544       while (b->level_chain->parm_flag == 0)
6545         b = b->level_chain;
6546       pushdecl_with_scope (decl, b);
6547     }   
6548
6549   cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6550       
6551   return decl;
6552 }
6553
6554 /* Make a definition for a builtin function named NAME in the current
6555    namespace, whose data type is TYPE and whose context is CONTEXT.
6556    TYPE should be a function type with argument types.
6557
6558    CLASS and CODE tell later passes how to compile calls to this function.
6559    See tree.h for possible values.
6560
6561    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6562    the name to be called if we can't opencode the function.
6563    If ATTRS is nonzero, use that for the function's attribute
6564    list.  */
6565
6566 static tree
6567 builtin_function_1 (const char* name,
6568                     tree type,
6569                     tree context,
6570                     int code,
6571                     enum built_in_class class,
6572                     const char* libname,
6573                     tree attrs)
6574 {
6575   tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6576   DECL_BUILT_IN_CLASS (decl) = class;
6577   DECL_FUNCTION_CODE (decl) = code;
6578   DECL_CONTEXT (decl) = context;
6579
6580   pushdecl (decl);
6581
6582   /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6583      we cannot change DECL_ASSEMBLER_NAME until we have installed this
6584      function in the namespace.  */
6585   if (libname)
6586     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6587   make_decl_rtl (decl, NULL);
6588
6589   /* Warn if a function in the namespace for users
6590      is used without an occasion to consider it declared.  */
6591   if (name[0] != '_' || name[1] != '_')
6592     DECL_ANTICIPATED (decl) = 1;
6593
6594   /* Possibly apply some default attributes to this built-in function.  */
6595   if (attrs)
6596     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6597   else
6598     decl_attributes (&decl, NULL_TREE, 0);
6599
6600   return decl;
6601 }
6602
6603 /* Entry point for the benefit of c_common_nodes_and_builtins.
6604
6605    Make a defintion for a builtin function named NAME and whose data type
6606    is TYPE.  TYPE should be a function type with argument types.  This
6607    function places the anticipated declaration in the global namespace
6608    and additionally in the std namespace if appropriate.
6609
6610    CLASS and CODE tell later passes how to compile calls to this function.
6611    See tree.h for possible values.
6612
6613    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6614    the name to be called if we can't opencode the function.
6615
6616    If ATTRS is nonzero, use that for the function's attribute
6617    list.  */
6618
6619 tree
6620 builtin_function (const char* name,
6621                   tree type,
6622                   int code,
6623                   enum built_in_class class,
6624                   const char* libname,
6625                   tree attrs)
6626 {
6627   /* All builtins that don't begin with an '_' should additionally
6628      go in the 'std' namespace.  */
6629   if (name[0] != '_')
6630     {
6631       push_namespace (std_identifier);
6632       builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6633       pop_namespace ();
6634     }
6635
6636   return builtin_function_1 (name, type, NULL_TREE, code,
6637                              class, libname, attrs);
6638 }
6639
6640 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6641    function.  Not called directly.  */
6642
6643 static tree
6644 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6645 {
6646   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6647   DECL_EXTERNAL (fn) = 1;
6648   TREE_PUBLIC (fn) = 1;
6649   DECL_ARTIFICIAL (fn) = 1;
6650   TREE_NOTHROW (fn) = 1;
6651   SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6652   SET_DECL_LANGUAGE (fn, lang_c);
6653   return fn;
6654 }
6655
6656 /* Returns the _DECL for a library function with C linkage.
6657    We assume that such functions never throw; if this is incorrect,
6658    callers should unset TREE_NOTHROW.  */
6659
6660 tree
6661 build_library_fn (tree name, tree type)
6662 {
6663   return build_library_fn_1 (name, ERROR_MARK, type);
6664 }
6665
6666 /* Returns the _DECL for a library function with C++ linkage.  */
6667
6668 static tree
6669 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6670 {
6671   tree fn = build_library_fn_1 (name, operator_code, type);
6672   TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6673   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6674   SET_DECL_LANGUAGE (fn, lang_cplusplus);
6675   set_mangled_name_for_decl (fn);
6676   return fn;
6677 }
6678
6679 /* Like build_library_fn, but takes a C string instead of an
6680    IDENTIFIER_NODE.  */
6681
6682 tree
6683 build_library_fn_ptr (const char* name, tree type)
6684 {
6685   return build_library_fn (get_identifier (name), type);
6686 }
6687
6688 /* Like build_cp_library_fn, but takes a C string instead of an
6689    IDENTIFIER_NODE.  */
6690
6691 tree
6692 build_cp_library_fn_ptr (const char* name, tree type)
6693 {
6694   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6695 }
6696
6697 /* Like build_library_fn, but also pushes the function so that we will
6698    be able to find it via IDENTIFIER_GLOBAL_VALUE.  */
6699
6700 tree
6701 push_library_fn (tree name, tree type)
6702 {
6703   tree fn = build_library_fn (name, type);
6704   pushdecl_top_level (fn);
6705   return fn;
6706 }
6707
6708 /* Like build_cp_library_fn, but also pushes the function so that it
6709    will be found by normal lookup.  */
6710
6711 static tree
6712 push_cp_library_fn (enum tree_code operator_code, tree type)
6713 {
6714   tree fn = build_cp_library_fn (ansi_opname (operator_code),
6715                                  operator_code,
6716                                  type);
6717   pushdecl (fn);
6718   return fn;
6719 }
6720
6721 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6722    a FUNCTION_TYPE.  */
6723
6724 tree
6725 push_void_library_fn (tree name, tree parmtypes)
6726 {
6727   tree type = build_function_type (void_type_node, parmtypes);
6728   return push_library_fn (name, type);
6729 }
6730
6731 /* Like push_library_fn, but also note that this function throws
6732    and does not return.  Used for __throw_foo and the like.  */
6733
6734 tree
6735 push_throw_library_fn (tree name, tree type)
6736 {
6737   tree fn = push_library_fn (name, type);
6738   TREE_THIS_VOLATILE (fn) = 1;
6739   TREE_NOTHROW (fn) = 0;
6740   return fn;
6741 }
6742
6743 /* Apply default attributes to a function, if a system function with default
6744    attributes.  */
6745
6746 void
6747 cxx_insert_default_attributes (tree decl)
6748 {
6749   if (!DECL_EXTERN_C_FUNCTION_P (decl))
6750     return;
6751   if (!TREE_PUBLIC (decl))
6752     return;
6753   c_common_insert_default_attributes (decl);
6754 }
6755 \f
6756 /* When we call finish_struct for an anonymous union, we create
6757    default copy constructors and such.  But, an anonymous union
6758    shouldn't have such things; this function undoes the damage to the
6759    anonymous union type T.
6760
6761    (The reason that we create the synthesized methods is that we don't
6762    distinguish `union { int i; }' from `typedef union { int i; } U'.
6763    The first is an anonymous union; the second is just an ordinary
6764    union type.)  */
6765
6766 void
6767 fixup_anonymous_aggr (tree t)
6768 {
6769   tree *q;
6770
6771   /* Wipe out memory of synthesized methods */
6772   TYPE_HAS_CONSTRUCTOR (t) = 0;
6773   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6774   TYPE_HAS_INIT_REF (t) = 0;
6775   TYPE_HAS_CONST_INIT_REF (t) = 0;
6776   TYPE_HAS_ASSIGN_REF (t) = 0;
6777   TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6778
6779   /* Splice the implicitly generated functions out of the TYPE_METHODS
6780      list.  */
6781   q = &TYPE_METHODS (t);
6782   while (*q)
6783     {
6784       if (DECL_ARTIFICIAL (*q))
6785         *q = TREE_CHAIN (*q);
6786       else
6787         q = &TREE_CHAIN (*q);
6788     }
6789
6790   /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
6791   if (TYPE_METHODS (t))
6792     cp_error_at ("an anonymous union cannot have function members", t);
6793
6794   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6795      assignment operators (because they cannot have these methods themselves).
6796      For anonymous unions this is already checked because they are not allowed
6797      in any union, otherwise we have to check it.  */
6798   if (TREE_CODE (t) != UNION_TYPE)
6799     {
6800       tree field, type;
6801
6802       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6803         if (TREE_CODE (field) == FIELD_DECL)
6804           {
6805             type = TREE_TYPE (field);
6806             if (CLASS_TYPE_P (type))
6807               {
6808                 if (TYPE_NEEDS_CONSTRUCTING (type))
6809                   cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6810                                field);
6811                 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6812                   cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6813                                field);
6814                 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6815                   cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6816                                field);
6817               }
6818           }
6819     }
6820 }
6821
6822 /* Make sure that a declaration with no declarator is well-formed, i.e.
6823    just declares a tagged type or anonymous union.
6824
6825    Returns the type declared; or NULL_TREE if none.  */
6826
6827 tree
6828 check_tag_decl (tree declspecs)
6829 {
6830   int found_type = 0;
6831   int saw_friend = 0;
6832   int saw_typedef = 0;
6833   tree ob_modifier = NULL_TREE;
6834   register tree link;
6835   /* If a class, struct, or enum type is declared by the DECLSPECS
6836      (i.e, if a class-specifier, enum-specifier, or non-typename
6837      elaborated-type-specifier appears in the DECLSPECS),
6838      DECLARED_TYPE is set to the corresponding type.  */
6839   tree declared_type = NULL_TREE;
6840   bool error_p = false;
6841
6842   for (link = declspecs; link; link = TREE_CHAIN (link))
6843     {
6844       tree value = TREE_VALUE (link);
6845
6846       if (TYPE_P (value)
6847           || TREE_CODE (value) == TYPE_DECL
6848           || (TREE_CODE (value) == IDENTIFIER_NODE
6849               && IDENTIFIER_GLOBAL_VALUE (value)
6850               && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6851         {
6852           ++found_type;
6853
6854           if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6855             {
6856               if (! in_system_header)
6857                 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6858               return NULL_TREE;
6859             }
6860
6861           if (TYPE_P (value)
6862               && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6863                   || TREE_CODE (value) == ENUMERAL_TYPE))
6864             {
6865               my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6866               declared_type = value;
6867             }
6868         }
6869       else if (value == ridpointers[(int) RID_TYPEDEF])
6870         saw_typedef = 1;
6871       else if (value == ridpointers[(int) RID_FRIEND])
6872         {
6873           if (current_class_type == NULL_TREE
6874               || current_scope () != current_class_type)
6875             ob_modifier = value;
6876           else
6877             saw_friend = 1;
6878         }
6879       else if (value == ridpointers[(int) RID_STATIC]
6880                || value == ridpointers[(int) RID_EXTERN]
6881                || value == ridpointers[(int) RID_AUTO]
6882                || value == ridpointers[(int) RID_REGISTER]
6883                || value == ridpointers[(int) RID_INLINE]
6884                || value == ridpointers[(int) RID_VIRTUAL]
6885                || value == ridpointers[(int) RID_CONST]
6886                || value == ridpointers[(int) RID_VOLATILE]
6887                || value == ridpointers[(int) RID_EXPLICIT]
6888                || value == ridpointers[(int) RID_THREAD])
6889         ob_modifier = value;
6890       else if (value == error_mark_node)
6891         error_p = true;
6892     }
6893
6894   if (found_type > 1)
6895     error ("multiple types in one declaration");
6896
6897   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6898     pedwarn ("declaration does not declare anything");
6899   /* Check for an anonymous union.  */
6900   else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6901            && TYPE_ANONYMOUS_P (declared_type))
6902     {
6903       /* 7/3 In a simple-declaration, the optional init-declarator-list
6904          can be omitted only when declaring a class (clause 9) or
6905          enumeration (7.2), that is, when the decl-specifier-seq contains
6906          either a class-specifier, an elaborated-type-specifier with
6907          a class-key (9.1), or an enum-specifier.  In these cases and
6908          whenever a class-specifier or enum-specifier is present in the
6909          decl-specifier-seq, the identifiers in these specifiers are among
6910          the names being declared by the declaration (as class-name,
6911          enum-names, or enumerators, depending on the syntax).  In such
6912          cases, and except for the declaration of an unnamed bit-field (9.6),
6913          the decl-specifier-seq shall introduce one or more names into the
6914          program, or shall redeclare a name introduced by a previous
6915          declaration.  [Example:
6916              enum { };            // ill-formed
6917              typedef class { };   // ill-formed
6918          --end example]  */
6919       if (saw_typedef)
6920         {
6921           error ("missing type-name in typedef-declaration");
6922           return NULL_TREE;
6923         }
6924       /* Anonymous unions are objects, so they can have specifiers.  */;
6925       SET_ANON_AGGR_TYPE_P (declared_type);
6926
6927       if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 
6928           && !in_system_header)
6929         pedwarn ("ISO C++ prohibits anonymous structs");
6930     }
6931
6932   else if (ob_modifier)
6933     {
6934       if (ob_modifier == ridpointers[(int) RID_INLINE]
6935           || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6936         error ("`%D' can only be specified for functions", ob_modifier);
6937       else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6938         error ("`%D' can only be specified inside a class", ob_modifier);
6939       else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6940         error ("`%D' can only be specified for constructors",
6941                   ob_modifier);
6942       else
6943         error ("`%D' can only be specified for objects and functions",
6944                   ob_modifier);
6945     }
6946
6947   return declared_type;
6948 }
6949
6950 /* Called when a declaration is seen that contains no names to declare.
6951    If its type is a reference to a structure, union or enum inherited
6952    from a containing scope, shadow that tag name for the current scope
6953    with a forward reference.
6954    If its type defines a new named structure or union
6955    or defines an enum, it is valid but we need not do anything here.
6956    Otherwise, it is an error.
6957
6958    C++: may have to grok the declspecs to learn about static,
6959    complain for anonymous unions.  
6960
6961    Returns the TYPE declared -- or NULL_TREE if none.  */
6962
6963 tree
6964 shadow_tag (tree declspecs)
6965 {
6966   tree t = check_tag_decl (declspecs);
6967
6968   if (!t)
6969     return NULL_TREE;
6970
6971   maybe_process_partial_specialization (t);
6972
6973   /* This is where the variables in an anonymous union are
6974      declared.  An anonymous union declaration looks like:
6975      union { ... } ;
6976      because there is no declarator after the union, the parser
6977      sends that declaration here.  */
6978   if (ANON_AGGR_TYPE_P (t))
6979     {
6980       fixup_anonymous_aggr (t);
6981
6982       if (TYPE_FIELDS (t))
6983         {
6984           tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6985                                       NULL);
6986           finish_anon_union (decl);
6987         }
6988     }
6989
6990   return t;
6991 }
6992 \f
6993 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
6994
6995 tree
6996 groktypename (tree typename)
6997 {
6998   tree specs, attrs;
6999   tree type;
7000   if (TREE_CODE (typename) != TREE_LIST)
7001     return typename;
7002   split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
7003   type = grokdeclarator (TREE_VALUE (typename), specs,
7004                          TYPENAME, 0, &attrs);
7005   if (attrs)
7006     cplus_decl_attributes (&type, attrs, 0);
7007   return type;
7008 }
7009
7010 /* Decode a declarator in an ordinary declaration or data definition.
7011    This is called as soon as the type information and variable name
7012    have been parsed, before parsing the initializer if any.
7013    Here we create the ..._DECL node, fill in its type,
7014    and put it on the list of decls for the current context.
7015    The ..._DECL node is returned as the value.
7016
7017    Exception: for arrays where the length is not specified,
7018    the type is left null, to be filled in by `cp_finish_decl'.
7019
7020    Function definitions do not come here; they go to start_function
7021    instead.  However, external and forward declarations of functions
7022    do go through here.  Structure field declarations are done by
7023    grokfield and not through here.  */
7024
7025 tree
7026 start_decl (tree declarator, 
7027             tree declspecs, 
7028             int initialized, 
7029             tree attributes, 
7030             tree prefix_attributes)
7031 {
7032   tree decl;
7033   register tree type, tem;
7034   tree context;
7035
7036   /* This should only be done once on the top most decl.  */
7037   if (have_extern_spec)
7038     {
7039       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
7040                              declspecs);
7041       have_extern_spec = false;
7042     }
7043
7044   /* An object declared as __attribute__((deprecated)) suppresses
7045      warnings of uses of other deprecated items.  */
7046   if (lookup_attribute ("deprecated", attributes))
7047     deprecated_state = DEPRECATED_SUPPRESS;
7048
7049   attributes = chainon (attributes, prefix_attributes);
7050
7051   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
7052                          &attributes);
7053
7054   deprecated_state = DEPRECATED_NORMAL;
7055
7056   if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
7057     return NULL_TREE;
7058
7059   type = TREE_TYPE (decl);
7060
7061   if (type == error_mark_node)
7062     return NULL_TREE;
7063
7064   context = DECL_CONTEXT (decl);
7065
7066   if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7067       && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7068     {
7069       /* When parsing the initializer, lookup should use the object's
7070          namespace.  */
7071       push_decl_namespace (context);
7072     }
7073
7074   /* We are only interested in class contexts, later.  */
7075   if (context && TREE_CODE (context) == NAMESPACE_DECL)
7076     context = NULL_TREE;
7077
7078   if (initialized)
7079     /* Is it valid for this decl to have an initializer at all?
7080        If not, set INITIALIZED to zero, which will indirectly
7081        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7082     switch (TREE_CODE (decl))
7083       {
7084       case TYPE_DECL:
7085         error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
7086         initialized = 0;
7087         break;
7088
7089       case FUNCTION_DECL:
7090         error ("function `%#D' is initialized like a variable", decl);
7091         initialized = 0;
7092         break;
7093
7094       default:
7095         break;
7096       }
7097
7098   if (initialized)
7099     {
7100       if (! toplevel_bindings_p ()
7101           && DECL_EXTERNAL (decl))
7102         warning ("declaration of `%#D' has `extern' and is initialized",
7103                     decl);
7104       DECL_EXTERNAL (decl) = 0;
7105       if (toplevel_bindings_p ())
7106         TREE_STATIC (decl) = 1;
7107
7108       /* Tell `pushdecl' this is an initialized decl
7109          even though we don't yet have the initializer expression.
7110          Also tell `cp_finish_decl' it may store the real initializer.  */
7111       DECL_INITIAL (decl) = error_mark_node;
7112     }
7113
7114   /* Set attributes here so if duplicate decl, will have proper attributes.  */
7115   cplus_decl_attributes (&decl, attributes, 0);
7116
7117   /* If #pragma weak was used, mark the decl weak now.  */
7118   if (current_binding_level == global_binding_level)
7119     maybe_apply_pragma_weak (decl);
7120
7121   if (TREE_CODE (decl) == FUNCTION_DECL
7122       && DECL_DECLARED_INLINE_P (decl)
7123       && DECL_UNINLINABLE (decl)
7124       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7125     warning ("%Hinline function '%D' given attribute noinline",
7126              &DECL_SOURCE_LOCATION (decl), decl);
7127
7128   if (context && COMPLETE_TYPE_P (complete_type (context)))
7129     {
7130       push_nested_class (context);
7131
7132       if (TREE_CODE (decl) == VAR_DECL)
7133         {
7134           tree field = lookup_field (context, DECL_NAME (decl), 0, false);
7135           if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7136             error ("`%#D' is not a static member of `%#T'", decl, context);
7137           else
7138             {
7139               if (DECL_CONTEXT (field) != context)
7140                 {
7141                   if (!same_type_p (DECL_CONTEXT (field), context))
7142                     pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7143                              DECL_CONTEXT (field), DECL_NAME (decl),
7144                              context, DECL_NAME (decl));
7145                   DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7146                 }
7147               /* Static data member are tricky; an in-class initialization
7148                  still doesn't provide a definition, so the in-class
7149                  declaration will have DECL_EXTERNAL set, but will have an
7150                  initialization.  Thus, duplicate_decls won't warn
7151                  about this situation, and so we check here.  */
7152               if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7153                 error ("duplicate initialization of %D", decl);
7154               if (duplicate_decls (decl, field))
7155                 decl = field;
7156             }
7157         }
7158       else
7159         {
7160           tree field = check_classfn (context, decl);
7161           if (field && duplicate_decls (decl, field))
7162             decl = field;
7163         }
7164
7165       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7166       DECL_IN_AGGR_P (decl) = 0;
7167       if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7168           || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7169         {
7170           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7171           /* [temp.expl.spec] An explicit specialization of a static data
7172              member of a template is a definition if the declaration
7173              includes an initializer; otherwise, it is a declaration.
7174
7175              We check for processing_specialization so this only applies
7176              to the new specialization syntax.  */
7177           if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7178             DECL_EXTERNAL (decl) = 1;
7179         }
7180
7181       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7182         pedwarn ("declaration of `%#D' outside of class is not definition",
7183                     decl);
7184     }
7185
7186   /* Enter this declaration into the symbol table.  */
7187   tem = maybe_push_decl (decl);
7188
7189   if (processing_template_decl)
7190     tem = push_template_decl (tem);
7191
7192 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7193   /* Tell the back-end to use or not use .common as appropriate.  If we say
7194      -fconserve-space, we want this to save .data space, at the expense of
7195      wrong semantics.  If we say -fno-conserve-space, we want this to
7196      produce errors about redefs; to do this we force variables into the
7197      data segment.  */
7198   DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7199                         || !DECL_THREAD_LOCAL (tem))
7200                        && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7201 #endif
7202
7203   if (! processing_template_decl)
7204     start_decl_1 (tem);
7205
7206   return tem;
7207 }
7208
7209 void
7210 start_decl_1 (tree decl)
7211 {
7212   tree type = TREE_TYPE (decl);
7213   int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7214
7215   if (type == error_mark_node)
7216     return;
7217
7218   maybe_push_cleanup_level (type);
7219
7220   if (initialized)
7221     /* Is it valid for this decl to have an initializer at all?
7222        If not, set INITIALIZED to zero, which will indirectly
7223        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7224     {
7225       /* Don't allow initializations for incomplete types except for
7226          arrays which might be completed by the initialization.  */
7227       if (COMPLETE_TYPE_P (complete_type (type)))
7228         ;                       /* A complete type is ok.  */
7229       else if (TREE_CODE (type) != ARRAY_TYPE)
7230         {
7231           error ("variable `%#D' has initializer but incomplete type",
7232                     decl);
7233           initialized = 0;
7234           type = TREE_TYPE (decl) = error_mark_node;
7235         }
7236       else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7237         {
7238           if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7239             error ("elements of array `%#D' have incomplete type", decl);
7240           /* else we already gave an error in start_decl.  */
7241           initialized = 0;
7242         }
7243     }
7244
7245   if (!initialized
7246       && TREE_CODE (decl) != TYPE_DECL
7247       && TREE_CODE (decl) != TEMPLATE_DECL
7248       && type != error_mark_node
7249       && IS_AGGR_TYPE (type)
7250       && ! DECL_EXTERNAL (decl))
7251     {
7252       if ((! processing_template_decl || ! uses_template_parms (type))
7253           && !COMPLETE_TYPE_P (complete_type (type)))
7254         {
7255           error ("aggregate `%#D' has incomplete type and cannot be defined",
7256                  decl);
7257           /* Change the type so that assemble_variable will give
7258              DECL an rtl we can live with: (mem (const_int 0)).  */
7259           type = TREE_TYPE (decl) = error_mark_node;
7260         }
7261       else
7262         {
7263           /* If any base type in the hierarchy of TYPE needs a constructor,
7264              then we set initialized to 1.  This way any nodes which are
7265              created for the purposes of initializing this aggregate
7266              will live as long as it does.  This is necessary for global
7267              aggregates which do not have their initializers processed until
7268              the end of the file.  */
7269           initialized = TYPE_NEEDS_CONSTRUCTING (type);
7270         }
7271     }
7272
7273   if (! initialized)
7274     DECL_INITIAL (decl) = NULL_TREE;
7275 }
7276
7277 /* Handle initialization of references.
7278    These three arguments are from `cp_finish_decl', and have the
7279    same meaning here that they do there.
7280
7281    Quotes on semantics can be found in ARM 8.4.3.  */
7282
7283 static tree
7284 grok_reference_init (tree decl, tree type, tree init)
7285 {
7286   tree tmp;
7287
7288   if (init == NULL_TREE)
7289     {
7290       if ((DECL_LANG_SPECIFIC (decl) == 0
7291            || DECL_IN_AGGR_P (decl) == 0)
7292           && ! DECL_THIS_EXTERN (decl))
7293         error ("`%D' declared as reference but not initialized", decl);
7294       return NULL_TREE;
7295     }
7296
7297   if (TREE_CODE (init) == CONSTRUCTOR)
7298     {
7299       error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7300       return NULL_TREE;
7301     }
7302
7303   if (TREE_CODE (init) == TREE_LIST)
7304     init = build_compound_expr (init);
7305
7306   if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7307     init = convert_from_reference (init);
7308
7309   if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7310       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7311     {
7312       /* Note: default conversion is only called in very special cases.  */
7313       init = default_conversion (init);
7314     }
7315
7316   /* Convert INIT to the reference type TYPE.  This may involve the
7317      creation of a temporary, whose lifetime must be the same as that
7318      of the reference.  If so, a DECL_STMT for the temporary will be
7319      added just after the DECL_STMT for DECL.  That's why we don't set
7320      DECL_INITIAL for local references (instead assigning to them
7321      explicitly); we need to allow the temporary to be initialized
7322      first.  */
7323   tmp = initialize_reference (type, init, decl);
7324
7325   if (tmp == error_mark_node)
7326     return NULL_TREE;
7327   else if (tmp == NULL_TREE)
7328     {
7329       error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7330       return NULL_TREE;
7331     }
7332
7333   if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7334     return tmp;
7335
7336   DECL_INITIAL (decl) = tmp;
7337
7338   return NULL_TREE;
7339 }
7340
7341 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7342    array until we finish parsing the initializer.  If that's the
7343    situation we're in, update DECL accordingly.  */
7344
7345 static void
7346 maybe_deduce_size_from_array_init (tree decl, tree init)
7347 {
7348   tree type = TREE_TYPE (decl);
7349
7350   if (TREE_CODE (type) == ARRAY_TYPE
7351       && TYPE_DOMAIN (type) == NULL_TREE
7352       && TREE_CODE (decl) != TYPE_DECL)
7353     {
7354       /* do_default is really a C-ism to deal with tentative definitions.
7355          But let's leave it here to ease the eventual merge.  */
7356       int do_default = !DECL_EXTERNAL (decl);
7357       tree initializer = init ? init : DECL_INITIAL (decl);
7358       int failure = complete_array_type (type, initializer, do_default);
7359
7360       if (failure == 1)
7361         error ("initializer fails to determine size of `%D'", decl);
7362
7363       if (failure == 2)
7364         {
7365           if (do_default)
7366             error ("array size missing in `%D'", decl);
7367           /* If a `static' var's size isn't known, make it extern as
7368              well as static, so it does not get allocated.  If it's not
7369              `static', then don't mark it extern; finish_incomplete_decl
7370              will give it a default size and it will get allocated.  */
7371           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7372             DECL_EXTERNAL (decl) = 1;
7373         }
7374
7375       if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7376           && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7377                               integer_zero_node))
7378         error ("zero-size array `%D'", decl);
7379
7380       layout_decl (decl, 0);
7381     }
7382 }
7383
7384 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7385    any appropriate error messages regarding the layout.  */
7386
7387 static void
7388 layout_var_decl (tree decl)
7389 {
7390   tree type = TREE_TYPE (decl);
7391 #if 0
7392   tree ttype = target_type (type);
7393 #endif
7394
7395   /* If we haven't already layed out this declaration, do so now.
7396      Note that we must not call complete type for an external object
7397      because it's type might involve templates that we are not
7398      supposed to isntantiate yet.  (And it's perfectly valid to say
7399      `extern X x' for some incomplete type `X'.)  */
7400   if (!DECL_EXTERNAL (decl))
7401     complete_type (type);
7402   if (!DECL_SIZE (decl) 
7403       && TREE_TYPE (decl) != error_mark_node
7404       && (COMPLETE_TYPE_P (type)
7405           || (TREE_CODE (type) == ARRAY_TYPE 
7406               && !TYPE_DOMAIN (type)
7407               && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7408     layout_decl (decl, 0);
7409
7410   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7411     {
7412       /* An automatic variable with an incomplete type: that is an error.
7413          Don't talk about array types here, since we took care of that
7414          message in grokdeclarator.  */
7415       error ("storage size of `%D' isn't known", decl);
7416       TREE_TYPE (decl) = error_mark_node;
7417     }
7418 #if 0
7419   /* Keep this code around in case we later want to control debug info
7420      based on whether a type is "used".  (jason 1999-11-11) */
7421
7422   else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7423     /* Let debugger know it should output info for this type.  */
7424     note_debug_info_needed (ttype);
7425
7426   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7427     note_debug_info_needed (DECL_CONTEXT (decl));
7428 #endif
7429
7430   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7431       && DECL_SIZE (decl) != NULL_TREE
7432       && ! TREE_CONSTANT (DECL_SIZE (decl)))
7433     {
7434       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7435         constant_expression_warning (DECL_SIZE (decl));
7436       else
7437         error ("storage size of `%D' isn't constant", decl);
7438     }
7439
7440   if (TREE_STATIC (decl)
7441       && !DECL_ARTIFICIAL (decl)
7442       && current_function_decl
7443       && DECL_CONTEXT (decl) == current_function_decl)
7444     push_local_name (decl);
7445 }
7446
7447 /* If a local static variable is declared in an inline function, or if
7448    we have a weak definition, we must endeavor to create only one
7449    instance of the variable at link-time.  */
7450
7451 static void
7452 maybe_commonize_var (tree decl)
7453 {
7454   /* Static data in a function with comdat linkage also has comdat
7455      linkage.  */
7456   if (TREE_STATIC (decl)
7457       /* Don't mess with __FUNCTION__.  */
7458       && ! DECL_ARTIFICIAL (decl)
7459       && current_function_decl
7460       && DECL_CONTEXT (decl) == current_function_decl
7461       && (DECL_DECLARED_INLINE_P (current_function_decl)
7462           || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7463       && TREE_PUBLIC (current_function_decl))
7464     {
7465       /* If flag_weak, we don't need to mess with this, as we can just
7466          make the function weak, and let it refer to its unique local
7467          copy.  This works because we don't allow the function to be
7468          inlined.  */
7469       if (! flag_weak)
7470         {
7471           if (DECL_INTERFACE_KNOWN (current_function_decl))
7472             {
7473               TREE_PUBLIC (decl) = 1;
7474               DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7475             }
7476           else if (DECL_INITIAL (decl) == NULL_TREE
7477                    || DECL_INITIAL (decl) == error_mark_node)
7478             {
7479               TREE_PUBLIC (decl) = 1;
7480               DECL_COMMON (decl) = 1;
7481             }
7482           /* else we lose. We can only do this if we can use common,
7483              which we can't if it has been initialized.  */
7484
7485           if (!TREE_PUBLIC (decl))
7486             {
7487               cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7488               cp_warning_at ("  you can work around this by removing the initializer", decl);
7489             }
7490         }
7491       else
7492         comdat_linkage (decl);
7493     }
7494   else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7495     /* Set it up again; we might have set DECL_INITIAL since the last
7496        time.  */
7497     comdat_linkage (decl);
7498 }
7499
7500 /* Issue an error message if DECL is an uninitialized const variable.  */
7501
7502 static void
7503 check_for_uninitialized_const_var (tree decl)
7504 {
7505   tree type = TREE_TYPE (decl);
7506
7507   /* ``Unless explicitly declared extern, a const object does not have
7508      external linkage and must be initialized. ($8.4; $12.1)'' ARM
7509      7.1.6 */
7510   if (TREE_CODE (decl) == VAR_DECL
7511       && TREE_CODE (type) != REFERENCE_TYPE
7512       && CP_TYPE_CONST_P (type)
7513       && !TYPE_NEEDS_CONSTRUCTING (type)
7514       && !DECL_INITIAL (decl))
7515     error ("uninitialized const `%D'", decl);
7516 }
7517
7518 /* FIELD is a FIELD_DECL or NULL.  In the former case, the value
7519    returned is the next FIELD_DECL (possibly FIELD itself) that can be
7520    initialized.  If there are no more such fields, the return value
7521    will be NULL.  */
7522
7523 static tree
7524 next_initializable_field (tree field)
7525 {
7526   while (field
7527          && (TREE_CODE (field) != FIELD_DECL
7528              || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7529              || DECL_ARTIFICIAL (field)))
7530     field = TREE_CHAIN (field);
7531
7532   return field;
7533 }
7534
7535 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7536    brace-enclosed aggregate initializer.
7537
7538    *INITP is one of a list of initializers describing a brace-enclosed
7539    initializer for an entity of the indicated aggregate TYPE.  It may
7540    not presently match the shape of the TYPE; for example:
7541    
7542      struct S { int a; int b; };
7543      struct S a[] = { 1, 2, 3, 4 };
7544
7545    Here *INITP will point to TREE_LIST of four elements, rather than a
7546    list of two elements, each itself a list of two elements.  This
7547    routine transforms INIT from the former form into the latter.  The
7548    revised initializer is returned.  */
7549
7550 static tree
7551 reshape_init (tree type, tree *initp)
7552 {
7553   tree inits;
7554   tree old_init;
7555   tree old_init_value;
7556   tree new_init;
7557   bool brace_enclosed_p;
7558
7559   old_init = *initp;
7560   old_init_value = (TREE_CODE (*initp) == TREE_LIST
7561                     ? TREE_VALUE (*initp) : old_init);
7562
7563   /* For some parse errors, OLD_INIT_VALUE may be NULL.  */
7564   if (!old_init_value)
7565     {
7566       my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7567       TREE_VALUE (old_init) = error_mark_node;
7568       return old_init;
7569     }
7570
7571   /* If the initializer is brace-enclosed, pull initializers from the
7572      enclosed elements.  Advance past the brace-enclosed initializer
7573      now.  */
7574   if (TREE_CODE (old_init_value) == CONSTRUCTOR 
7575       && TREE_HAS_CONSTRUCTOR (old_init_value))
7576     {
7577       *initp = TREE_CHAIN (old_init);
7578       TREE_CHAIN (old_init) = NULL_TREE;
7579       inits = CONSTRUCTOR_ELTS (old_init_value);
7580       initp = &inits;
7581       brace_enclosed_p = true;
7582     }
7583   else
7584     {
7585       inits = NULL_TREE;
7586       brace_enclosed_p = false;
7587     }
7588
7589   /* A non-aggregate type is always initialized with a single
7590      initializer.  */
7591   if (!CP_AGGREGATE_TYPE_P (type))
7592       {
7593         *initp = TREE_CHAIN (old_init);
7594         TREE_CHAIN (old_init) = NULL_TREE;
7595         /* It is invalid to initialize a non-aggregate type with a
7596            brace-enclosed initializer.  */
7597         if (brace_enclosed_p)
7598           {
7599             error ("brace-enclosed initializer used to initialize `%T'",
7600                    type);
7601             if (TREE_CODE (old_init) == TREE_LIST)
7602               TREE_VALUE (old_init) = error_mark_node;
7603             else
7604               old_init = error_mark_node;
7605           }
7606         
7607         return old_init;
7608       }
7609
7610   /* [dcl.init.aggr]
7611
7612      All implicit type conversions (clause _conv_) are considered when
7613      initializing the aggregate member with an initializer from an
7614      initializer-list.  If the initializer can initialize a member,
7615      the member is initialized.  Otherwise, if the member is itself a
7616      non-empty subaggregate, brace elision is assumed and the
7617      initializer is considered for the initialization of the first
7618      member of the subaggregate.  */
7619   if (CLASS_TYPE_P (type) 
7620       && !brace_enclosed_p
7621       && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7622     {
7623       *initp = TREE_CHAIN (old_init);
7624       TREE_CHAIN (old_init) = NULL_TREE;
7625       return old_init;
7626     }
7627
7628   if (TREE_CODE (old_init_value) == STRING_CST
7629       && TREE_CODE (type) == ARRAY_TYPE
7630       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7631     {
7632       /* [dcl.init.string]
7633
7634          A char array (whether plain char, signed char, or unsigned char)
7635          can be initialized by a string-literal (optionally enclosed in
7636          braces); a wchar_t array can be initialized by a wide
7637          string-literal (optionally enclosed in braces).  */
7638       new_init = old_init;
7639       /* Move past the initializer.  */
7640       *initp = TREE_CHAIN (old_init);
7641       TREE_CHAIN (old_init) = NULL_TREE;
7642     }
7643   else
7644     {
7645       /* Build a CONSTRUCTOR to hold the contents of the aggregate.  */  
7646       new_init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
7647       TREE_HAS_CONSTRUCTOR (new_init) = 1;
7648
7649       if (CLASS_TYPE_P (type))
7650         {
7651           tree field;
7652
7653           field = next_initializable_field (TYPE_FIELDS (type));
7654
7655           if (!field)
7656             {
7657               /* [dcl.init.aggr]
7658               
7659                  An initializer for an aggregate member that is an
7660                  empty class shall have the form of an empty
7661                  initializer-list {}.  */
7662               if (!brace_enclosed_p)
7663                 error ("initializer for `%T' must be brace-enclosed",
7664                        type);
7665             }
7666           else
7667             {
7668               /* Loop through the initializable fields, gathering
7669                  initializers.  */
7670               /* FIXME support non-trivial labeled initializers.  */
7671               while (*initp && field)
7672                 {
7673                   tree field_init;
7674
7675                   field_init = reshape_init (TREE_TYPE (field), initp);
7676                   TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7677                   CONSTRUCTOR_ELTS (new_init) = field_init;
7678                   /* [dcl.init.aggr] 
7679
7680                      When a union  is  initialized with a brace-enclosed
7681                      initializer, the braces shall only contain an
7682                      initializer for the first member of the union.  */
7683                   if (TREE_CODE (type) == UNION_TYPE)
7684                     break;
7685                   field = next_initializable_field (TREE_CHAIN (field));
7686                 }
7687             }
7688         }
7689       else if (TREE_CODE (type) == ARRAY_TYPE)
7690         {
7691           tree index;
7692           tree max_index;
7693
7694           /* If the bound of the array is known, take no more initializers
7695              than are allowed.  */
7696           max_index = (TYPE_DOMAIN (type) 
7697                        ? array_type_nelts (type) : NULL_TREE);
7698           /* Loop through the array elements, gathering initializers.  */
7699           for (index = size_zero_node;
7700                *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7701                index = size_binop (PLUS_EXPR, index, size_one_node))
7702             {
7703               tree element_init;
7704
7705               element_init = reshape_init (TREE_TYPE (type), initp);
7706               TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7707               CONSTRUCTOR_ELTS (new_init) = element_init;
7708               if (TREE_PURPOSE (element_init))
7709                 index = TREE_PURPOSE (element_init);
7710             }
7711         }
7712       else
7713         abort ();
7714
7715       /* The initializers were placed in reverse order in the
7716          CONSTRUCTOR.  */
7717       CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7718
7719       if (TREE_CODE (old_init) == TREE_LIST)
7720         new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7721     }
7722
7723   /* If this was a brace-enclosed initializer and all of the
7724      initializers were not used up, there is a problem.  */
7725   if (brace_enclosed_p && *initp)
7726     error ("too many initializers for `%T'", type);
7727
7728   return new_init;
7729 }
7730
7731 /* Verify INIT (the initializer for DECL), and record the
7732    initialization in DECL_INITIAL, if appropriate.  
7733
7734    If the return value is non-NULL, it is an expression that must be
7735    evaluated dynamically to initialize DECL.  */
7736
7737 static tree
7738 check_initializer (tree decl, tree init, int flags)
7739 {
7740   tree type = TREE_TYPE (decl);
7741
7742   /* If `start_decl' didn't like having an initialization, ignore it now.  */
7743   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7744     init = NULL_TREE;
7745
7746   /* If an initializer is present, DECL_INITIAL has been
7747      error_mark_node, to indicate that an as-of-yet unevaluated
7748      initialization will occur.  From now on, DECL_INITIAL reflects
7749      the static initialization -- if any -- of DECL.  */
7750   DECL_INITIAL (decl) = NULL_TREE;
7751
7752   /* Things that are going to be initialized need to have complete
7753      type.  */
7754   TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7755
7756   if (type == error_mark_node)
7757     /* We will have already complained.  */
7758     init = NULL_TREE;
7759   else if (init && COMPLETE_TYPE_P (type) 
7760            && !TREE_CONSTANT (TYPE_SIZE (type)))
7761     {
7762       error ("variable-sized object `%D' may not be initialized", decl);
7763       init = NULL_TREE;
7764     }
7765   else if (TREE_CODE (type) == ARRAY_TYPE
7766            && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7767     {
7768       error ("elements of array `%#D' have incomplete type", decl);
7769       init = NULL_TREE;
7770     }
7771   else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7772     {
7773       error ("`%D' has incomplete type", decl);
7774       TREE_TYPE (decl) = error_mark_node;
7775       init = NULL_TREE;
7776     }
7777
7778   if (TREE_CODE (decl) == CONST_DECL)
7779     {
7780       my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7781
7782       DECL_INITIAL (decl) = init;
7783
7784       my_friendly_assert (init != NULL_TREE, 149);
7785       init = NULL_TREE;
7786     }
7787   else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7788     init = grok_reference_init (decl, type, init);
7789   else if (init)
7790     {
7791       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7792         {
7793           /* [dcl.init] paragraph 13,
7794              If T is a scalar type, then a declaration of the form
7795              T x = { a };
7796              is equivalent to
7797              T x = a;
7798              
7799              reshape_init will complain about the extra braces,
7800              and doesn't do anything useful in the case where TYPE is
7801              scalar, so just don't call it.  */
7802           if (CP_AGGREGATE_TYPE_P (type))
7803             init = reshape_init (type, &init);
7804
7805           if ((*targetm.vector_opaque_p) (type))
7806             {
7807               error ("opaque vector types cannot be initialized");
7808               init = error_mark_node;
7809             }
7810         }
7811
7812       /* If DECL has an array type without a specific bound, deduce the
7813          array size from the initializer.  */
7814       maybe_deduce_size_from_array_init (decl, init);
7815       type = TREE_TYPE (decl);
7816       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7817         TREE_TYPE (init) = type;
7818
7819       if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7820         {
7821           if (TREE_CODE (type) == ARRAY_TYPE)
7822             goto initialize_aggr;
7823           else if (TREE_CODE (init) == CONSTRUCTOR
7824                    && TREE_HAS_CONSTRUCTOR (init))
7825             {
7826               if (TYPE_NON_AGGREGATE_CLASS (type))
7827                 {
7828                   error ("`%D' must be initialized by constructor, not by `{...}'",
7829                          decl);
7830                   init = error_mark_node;
7831                 }
7832               else
7833                 goto dont_use_constructor;
7834             }
7835           else
7836             {
7837               int saved_stmts_are_full_exprs_p;
7838
7839             initialize_aggr:
7840               saved_stmts_are_full_exprs_p = 0;
7841               if (building_stmt_tree ())
7842                 {
7843                   saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7844                   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7845                 }
7846               init = build_aggr_init (decl, init, flags);
7847               if (building_stmt_tree ())
7848                 current_stmt_tree ()->stmts_are_full_exprs_p =
7849                   saved_stmts_are_full_exprs_p;
7850               return init;
7851             }
7852         }
7853       else
7854         {
7855         dont_use_constructor:
7856           if (TREE_CODE (init) != TREE_VEC)
7857             init = store_init_value (decl, init);
7858         }
7859     }
7860   else if (DECL_EXTERNAL (decl))
7861     ;
7862   else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7863     goto initialize_aggr;
7864   else if (IS_AGGR_TYPE (type))
7865     {
7866       tree core_type = strip_array_types (type);
7867
7868       if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7869         error ("structure `%D' with uninitialized const members", decl);
7870       if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7871         error ("structure `%D' with uninitialized reference members",
7872                decl);
7873
7874       check_for_uninitialized_const_var (decl);
7875     }
7876   else
7877     check_for_uninitialized_const_var (decl);
7878
7879   if (init && init != error_mark_node)
7880     init = build (INIT_EXPR, type, decl, init);
7881
7882   return init;
7883 }
7884
7885 /* If DECL is not a local variable, give it RTL.  */
7886
7887 static void
7888 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7889 {
7890   int toplev = toplevel_bindings_p ();
7891   int defer_p;
7892
7893   /* Handle non-variables up front.  */
7894   if (TREE_CODE (decl) != VAR_DECL)
7895     {
7896       rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7897       return;
7898     }
7899
7900   /* If we see a class member here, it should be a static data
7901      member.  */
7902   if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7903     {
7904       my_friendly_assert (TREE_STATIC (decl), 19990828);
7905       /* An in-class declaration of a static data member should be
7906          external; it is only a declaration, and not a definition.  */
7907       if (init == NULL_TREE)
7908         my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7909     }
7910
7911   /* Set the DECL_ASSEMBLER_NAME for the variable.  */
7912   if (asmspec)
7913     {
7914       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7915       /* The `register' keyword, when used together with an
7916          asm-specification, indicates that the variable should be
7917          placed in a particular register.  */
7918       if (DECL_REGISTER (decl))
7919         DECL_C_HARD_REGISTER (decl) = 1;
7920     }
7921
7922   /* We don't create any RTL for local variables.  */
7923   if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7924     return;
7925
7926   /* We defer emission of local statics until the corresponding
7927      DECL_STMT is expanded.  */
7928   defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7929
7930   /* We try to defer namespace-scope static constants so that they are
7931      not emitted into the object file unnecessarily.  */
7932   if (!DECL_VIRTUAL_P (decl)
7933       && TREE_READONLY (decl)
7934       && DECL_INITIAL (decl) != NULL_TREE
7935       && DECL_INITIAL (decl) != error_mark_node
7936       && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7937       && toplev
7938       && !TREE_PUBLIC (decl))
7939     {
7940       /* Fool with the linkage of static consts according to #pragma
7941          interface.  */
7942       if (!interface_unknown && !TREE_PUBLIC (decl))
7943         {
7944           TREE_PUBLIC (decl) = 1;
7945           DECL_EXTERNAL (decl) = interface_only;
7946         }
7947
7948       defer_p = 1;
7949     }
7950   /* Likewise for template instantiations.  */
7951   else if (DECL_COMDAT (decl))
7952     defer_p = 1;
7953
7954   /* If we're deferring the variable, we only need to make RTL if
7955      there's an ASMSPEC.  Otherwise, we'll lazily create it later when
7956      we need it.  (There's no way to lazily create RTL for things that
7957      have assembly specs because the information about the specifier
7958      isn't stored in the tree, yet)  */
7959   if (defer_p && asmspec)
7960     make_decl_rtl (decl, asmspec);
7961   /* If we're not deferring, go ahead and assemble the variable.  */
7962   else if (!defer_p)
7963     rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7964 }
7965
7966 /* The old ARM scoping rules injected variables declared in the
7967    initialization statement of a for-statement into the surrounding
7968    scope.  We support this usage, in order to be backward-compatible.
7969    DECL is a just-declared VAR_DECL; if necessary inject its
7970    declaration into the surrounding scope.  */
7971
7972 void
7973 maybe_inject_for_scope_var (tree decl)
7974 {
7975   timevar_push (TV_NAME_LOOKUP);
7976   if (!DECL_NAME (decl))
7977     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
7978   
7979   /* Declarations of __FUNCTION__ and its ilk appear magically when
7980      the variable is first used.  If that happens to be inside a
7981      for-loop, we don't want to do anything special.  */
7982   if (DECL_PRETTY_FUNCTION_P (decl))
7983     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
7984
7985   if (current_binding_level->is_for_scope)
7986     {
7987       struct cp_binding_level *outer
7988         = current_binding_level->level_chain;
7989
7990       /* Check to see if the same name is already bound at the outer
7991          level, either because it was directly declared, or because a
7992          dead for-decl got preserved.  In either case, the code would
7993          not have been valid under the ARM scope rules, so clear
7994          is_for_scope for the current_binding_level.
7995
7996          Otherwise, we need to preserve the temp slot for decl to last
7997          into the outer binding level.  */
7998
7999       tree outer_binding
8000         = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
8001
8002       if (outer_binding && BINDING_LEVEL (outer_binding) == outer
8003           && (TREE_CODE (BINDING_VALUE (outer_binding))
8004               == VAR_DECL)
8005           && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
8006         {
8007           BINDING_VALUE (outer_binding)
8008             = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
8009           current_binding_level->is_for_scope = 0;
8010         }
8011     }
8012   timevar_pop (TV_NAME_LOOKUP);
8013 }
8014
8015 /* Generate code to initialize DECL (a local variable).  */
8016
8017 static void
8018 initialize_local_var (tree decl, tree init)
8019 {
8020   tree type = TREE_TYPE (decl);
8021
8022   my_friendly_assert (TREE_CODE (decl) == VAR_DECL
8023                       || TREE_CODE (decl) == RESULT_DECL, 
8024                       20021010);
8025   my_friendly_assert (!TREE_STATIC (decl), 20021010);
8026
8027   if (DECL_SIZE (decl) == NULL_TREE)
8028     {
8029       /* If we used it already as memory, it must stay in memory.  */
8030       DECL_INITIAL (decl) = NULL_TREE;
8031       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8032     }
8033
8034   if (DECL_SIZE (decl) && type != error_mark_node)
8035     {
8036       int already_used;
8037
8038       /* Compute and store the initial value.  */
8039       already_used = TREE_USED (decl) || TREE_USED (type);
8040
8041       /* Perform the initialization.  */
8042       if (init)
8043         {
8044           int saved_stmts_are_full_exprs_p;
8045
8046           my_friendly_assert (building_stmt_tree (), 20000906);
8047           saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8048           current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8049           finish_expr_stmt (init);
8050           current_stmt_tree ()->stmts_are_full_exprs_p =
8051             saved_stmts_are_full_exprs_p;
8052         }
8053
8054       /* Set this to 0 so we can tell whether an aggregate which was
8055          initialized was ever used.  Don't do this if it has a
8056          destructor, so we don't complain about the 'resource
8057          allocation is initialization' idiom.  Now set
8058          attribute((unused)) on types so decls of that type will be
8059          marked used. (see TREE_USED, above.)  */
8060       if (TYPE_NEEDS_CONSTRUCTING (type)
8061           && ! already_used
8062           && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8063           && DECL_NAME (decl))
8064         TREE_USED (decl) = 0;
8065       else if (already_used)
8066         TREE_USED (decl) = 1;
8067     }
8068
8069   /* Generate a cleanup, if necessary.  */
8070   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8071     {
8072       tree cleanup;
8073
8074       /* Compute the cleanup.  */
8075       cleanup = cxx_maybe_build_cleanup (decl);
8076       
8077       /* Record the cleanup required for this declaration.  */
8078       if (DECL_SIZE (decl) && cleanup)
8079         finish_decl_cleanup (decl, cleanup);
8080     }
8081 }
8082
8083 /* Finish processing of a declaration;
8084    install its line number and initial value.
8085    If the length of an array type is not known before,
8086    it must be determined now, from the initial value, or it is an error.
8087
8088    INIT holds the value of an initializer that should be allowed to escape
8089    the normal rules.
8090
8091    FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8092    if the (init) syntax was used.  */
8093
8094 void
8095 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8096 {
8097   register tree type;
8098   tree ttype = NULL_TREE;
8099   const char *asmspec = NULL;
8100   int was_readonly = 0;
8101
8102   if (! decl)
8103     {
8104       if (init)
8105         error ("assignment (not initialization) in declaration");
8106       return;
8107     }
8108
8109   /* If a name was specified, get the string.  */
8110   if (current_binding_level == global_binding_level)
8111     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8112   if (asmspec_tree)
8113     asmspec = TREE_STRING_POINTER (asmspec_tree);
8114
8115   if (init && TREE_CODE (init) == NAMESPACE_DECL)
8116     {
8117       error ("cannot initialize `%D' to namespace `%D'",
8118                 decl, init);
8119       init = NULL_TREE;
8120     }
8121
8122   if (current_class_type
8123       && CP_DECL_CONTEXT (decl) == current_class_type
8124       && TYPE_BEING_DEFINED (current_class_type)
8125       && (DECL_INITIAL (decl) || init))
8126     DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8127
8128   if (TREE_CODE (decl) == VAR_DECL
8129       && DECL_CONTEXT (decl)
8130       && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8131       && DECL_CONTEXT (decl) != current_namespace
8132       && init)
8133     {
8134       /* Leave the namespace of the object.  */
8135       pop_decl_namespace ();
8136     }
8137
8138   type = TREE_TYPE (decl);
8139
8140   if (type == error_mark_node)
8141     return;
8142
8143   if (TYPE_HAS_MUTABLE_P (type))
8144     TREE_READONLY (decl) = 0;
8145
8146   if (processing_template_decl)
8147     {
8148       /* Add this declaration to the statement-tree.  */
8149       if (at_function_scope_p ()
8150           && TREE_CODE (decl) != RESULT_DECL)
8151         add_decl_stmt (decl);
8152
8153       if (init && DECL_INITIAL (decl))
8154         DECL_INITIAL (decl) = init;
8155       goto finish_end0;
8156     }
8157
8158   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
8159   my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8160
8161   /* Take care of TYPE_DECLs up front.  */
8162   if (TREE_CODE (decl) == TYPE_DECL)
8163     {
8164       if (type != error_mark_node
8165           && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8166         {
8167           if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8168             warning ("shadowing previous type declaration of `%#D'", decl);
8169           set_identifier_type_value (DECL_NAME (decl), type);
8170           CLASSTYPE_GOT_SEMICOLON (type) = 1;
8171         }
8172
8173       /* If we have installed this as the canonical typedef for this
8174          type, and that type has not been defined yet, delay emitting
8175          the debug information for it, as we will emit it later.  */
8176       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8177           && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8178         TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8179
8180       rest_of_decl_compilation (decl, NULL,
8181                                 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8182       goto finish_end;
8183     }
8184
8185   if (TREE_CODE (decl) != FUNCTION_DECL)
8186     ttype = target_type (type);
8187
8188   if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8189       && TYPE_NEEDS_CONSTRUCTING (type))
8190     {
8191       /* Currently, GNU C++ puts constants in text space, making them
8192          impossible to initialize.  In the future, one would hope for
8193          an operating system which understood the difference between
8194          initialization and the running of a program.  */
8195       was_readonly = 1;
8196       TREE_READONLY (decl) = 0;
8197     }
8198
8199   if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8200     {
8201       /* This must override the asm specifier which was placed by
8202          grokclassfn.  Lay this out fresh.  */
8203       SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8204       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8205       make_decl_rtl (decl, asmspec);
8206     }
8207   else if (TREE_CODE (decl) == RESULT_DECL)
8208     init = check_initializer (decl, init, flags);
8209   else if (TREE_CODE (decl) == VAR_DECL)
8210     {
8211       /* Only PODs can have thread-local storage.  Other types may require
8212          various kinds of non-trivial initialization.  */
8213       if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8214         error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8215                decl, TREE_TYPE (decl));
8216       /* Convert the initializer to the type of DECL, if we have not
8217          already initialized DECL.  */
8218       if (!DECL_INITIALIZED_P (decl)
8219           /* If !DECL_EXTERNAL then DECL is being defined.  In the
8220              case of a static data member initialized inside the
8221              class-specifier, there can be an initializer even if DECL
8222              is *not* defined.  */
8223           && (!DECL_EXTERNAL (decl) || init))
8224         {
8225           init = check_initializer (decl, init, flags);
8226           /* Thread-local storage cannot be dynamically initialized.  */
8227           if (DECL_THREAD_LOCAL (decl) && init)
8228             {
8229               error ("`%D' is thread-local and so cannot be dynamically "
8230                      "initialized", decl);
8231               init = NULL_TREE;
8232             }
8233           /* Handle:
8234              
8235              [dcl.init]
8236              
8237              The memory occupied by any object of static storage
8238              duration is zero-initialized at program startup before
8239              any other initialization takes place.
8240              
8241              We cannot create an appropriate initializer until after
8242              the type of DECL is finalized.  If DECL_INITIAL is set,
8243              then the DECL is statically initialized, and any
8244              necessary zero-initialization has already been performed.  */
8245           if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8246             DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8247                                                    /*nelts=*/NULL_TREE,
8248                                                    /*static_storage_p=*/true);
8249           /* Remember that the initialization for this variable has
8250              taken place.  */
8251           DECL_INITIALIZED_P (decl) = 1;
8252         }
8253       /* If the variable has an array type, lay out the type, even if
8254          there is no initializer.  It is valid to index through the
8255          array, and we must get TYPE_ALIGN set correctly on the array
8256          type.  */
8257       else if (TREE_CODE (type) == ARRAY_TYPE)
8258         layout_type (type);
8259     }
8260
8261   /* Add this declaration to the statement-tree.  This needs to happen
8262      after the call to check_initializer so that the DECL_STMT for a
8263      reference temp is added before the DECL_STMT for the reference itself.  */
8264   if (building_stmt_tree ()
8265       && at_function_scope_p ()
8266       && TREE_CODE (decl) != RESULT_DECL)
8267     add_decl_stmt (decl);
8268
8269   if (TREE_CODE (decl) == VAR_DECL)
8270     layout_var_decl (decl);
8271
8272   /* Output the assembler code and/or RTL code for variables and functions,
8273      unless the type is an undefined structure or union.
8274      If not, it will get done when the type is completed.  */
8275   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8276       || TREE_CODE (decl) == RESULT_DECL)
8277     {
8278       if (TREE_CODE (decl) == VAR_DECL)
8279         maybe_commonize_var (decl);
8280
8281       make_rtl_for_nonlocal_decl (decl, init, asmspec);
8282
8283       if (TREE_CODE (type) == FUNCTION_TYPE
8284           || TREE_CODE (type) == METHOD_TYPE)
8285         abstract_virtuals_error (decl,
8286                                  strip_array_types (TREE_TYPE (type)));
8287       else
8288         abstract_virtuals_error (decl, strip_array_types (type));
8289
8290       if (TREE_CODE (decl) == FUNCTION_DECL 
8291           || TREE_TYPE (decl) == error_mark_node)
8292         /* No initialization required.  */
8293         ;
8294       else if (DECL_EXTERNAL (decl)
8295                && ! (DECL_LANG_SPECIFIC (decl)
8296                      && DECL_NOT_REALLY_EXTERN (decl)))
8297         {
8298           if (init)
8299             DECL_INITIAL (decl) = init;
8300         }
8301       else
8302         {
8303           /* A variable definition.  */
8304           if (DECL_FUNCTION_SCOPE_P (decl))
8305             {
8306               /* This is a local declaration.  */
8307               if (doing_semantic_analysis_p ())
8308                 maybe_inject_for_scope_var (decl);
8309               /* Initialize the local variable.  */
8310               if (processing_template_decl)
8311                 {
8312                   if (init || DECL_INITIAL (decl) == error_mark_node)
8313                     DECL_INITIAL (decl) = init;
8314                 }
8315               else if (!TREE_STATIC (decl))
8316                 initialize_local_var (decl, init);
8317             }
8318
8319           if (TREE_STATIC (decl))
8320             expand_static_init (decl, init);
8321         }
8322     finish_end0:
8323
8324       /* Undo call to `pushclass' that was done in `start_decl'
8325          due to initialization of qualified member variable.
8326          I.e., Foo::x = 10;  */
8327       {
8328         tree context = CP_DECL_CONTEXT (decl);
8329         if (context
8330             && TYPE_P (context)
8331             && (TREE_CODE (decl) == VAR_DECL
8332                 /* We also have a pushclass done that we need to undo here
8333                    if we're at top level and declare a method.  */
8334                 || TREE_CODE (decl) == FUNCTION_DECL)
8335             /* If size hasn't been set, we're still defining it,
8336                and therefore inside the class body; don't pop
8337                the binding level..  */
8338             && COMPLETE_TYPE_P (context)
8339             && context == current_class_type)
8340           pop_nested_class ();
8341       }
8342     }
8343
8344  finish_end:
8345
8346   if (was_readonly)
8347     TREE_READONLY (decl) = 1;
8348 }
8349
8350 /* This is here for a midend callback from c-common.c */
8351
8352 void
8353 finish_decl (tree decl, tree init, tree asmspec_tree)
8354 {
8355   cp_finish_decl (decl, init, asmspec_tree, 0);
8356 }
8357
8358 /* Returns a declaration for a VAR_DECL as if:
8359
8360      extern "C" TYPE NAME;
8361
8362    had been seen.  Used to create compiler-generated global
8363    variables.  */
8364
8365 tree
8366 declare_global_var (tree name, tree type)
8367 {
8368   tree decl;
8369
8370   push_to_top_level ();
8371   decl = build_decl (VAR_DECL, name, type);
8372   TREE_PUBLIC (decl) = 1;
8373   DECL_EXTERNAL (decl) = 1;
8374   DECL_ARTIFICIAL (decl) = 1;
8375   pushdecl (decl);
8376   cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8377   pop_from_top_level ();
8378
8379   return decl;
8380 }
8381
8382 /* Returns a pointer to the `atexit' function.  Note that if
8383    FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8384    `__cxa_atexit' function specified in the IA64 C++ ABI.  */
8385
8386 static tree
8387 get_atexit_node (void)
8388 {
8389   tree atexit_fndecl;
8390   tree arg_types;
8391   tree fn_type;
8392   tree fn_ptr_type;
8393   const char *name;
8394
8395   if (atexit_node)
8396     return atexit_node;
8397
8398   if (flag_use_cxa_atexit)
8399     {
8400       /* The declaration for `__cxa_atexit' is:
8401
8402            int __cxa_atexit (void (*)(void *), void *, void *)
8403
8404          We build up the argument types and then then function type
8405          itself.  */
8406
8407       /* First, build the pointer-to-function type for the first
8408          argument.  */
8409       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8410       fn_type = build_function_type (void_type_node, arg_types);
8411       fn_ptr_type = build_pointer_type (fn_type);
8412       /* Then, build the rest of the argument types.  */
8413       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8414       arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8415       arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8416       /* And the final __cxa_atexit type.  */
8417       fn_type = build_function_type (integer_type_node, arg_types);
8418       fn_ptr_type = build_pointer_type (fn_type);
8419       name = "__cxa_atexit";
8420     }
8421   else
8422     {
8423       /* The declaration for `atexit' is:
8424
8425            int atexit (void (*)());
8426
8427          We build up the argument types and then then function type
8428          itself.  */
8429       fn_type = build_function_type (void_type_node, void_list_node);
8430       fn_ptr_type = build_pointer_type (fn_type);
8431       arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8432       /* Build the final atexit type.  */
8433       fn_type = build_function_type (integer_type_node, arg_types);
8434       name = "atexit";
8435     }
8436
8437   /* Now, build the function declaration.  */
8438   push_lang_context (lang_name_c);
8439   atexit_fndecl = build_library_fn_ptr (name, fn_type);
8440   mark_used (atexit_fndecl);
8441   pop_lang_context ();
8442   atexit_node = default_conversion (atexit_fndecl);
8443
8444   return atexit_node;
8445 }
8446
8447 /* Returns the __dso_handle VAR_DECL.  */
8448
8449 static tree
8450 get_dso_handle_node (void)
8451 {
8452   if (dso_handle_node)
8453     return dso_handle_node;
8454
8455   /* Declare the variable.  */
8456   dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8457                                         ptr_type_node);
8458
8459   return dso_handle_node;
8460 }
8461
8462 /* Begin a new function with internal linkage whose job will be simply
8463    to destroy some particular variable.  */
8464
8465 static tree
8466 start_cleanup_fn (void)
8467 {
8468   static int counter = 0;
8469   int old_interface_only = interface_only;
8470   int old_interface_unknown = interface_unknown;
8471   char name[32];
8472   tree parmtypes;
8473   tree fntype;
8474   tree fndecl;
8475
8476   push_to_top_level ();
8477
8478   /* No need to mangle this.  */
8479   push_lang_context (lang_name_c);
8480
8481   interface_only = 0;
8482   interface_unknown = 1;
8483
8484   /* Build the parameter-types.  */
8485   parmtypes = void_list_node;
8486   /* Functions passed to __cxa_atexit take an additional parameter.
8487      We'll just ignore it.  After we implement the new calling
8488      convention for destructors, we can eliminate the use of
8489      additional cleanup functions entirely in the -fnew-abi case.  */
8490   if (flag_use_cxa_atexit)
8491     parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8492   /* Build the function type itself.  */
8493   fntype = build_function_type (void_type_node, parmtypes);
8494   /* Build the name of the function.  */
8495   sprintf (name, "__tcf_%d", counter++);
8496   /* Build the function declaration.  */
8497   fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8498   /* It's a function with internal linkage, generated by the
8499      compiler.  */
8500   TREE_PUBLIC (fndecl) = 0;
8501   DECL_ARTIFICIAL (fndecl) = 1;
8502   /* Make the function `inline' so that it is only emitted if it is
8503      actually needed.  It is unlikely that it will be inlined, since
8504      it is only called via a function pointer, but we avoid unnecessary
8505      emissions this way.  */
8506   DECL_INLINE (fndecl) = 1;
8507   /* Build the parameter.  */
8508   if (flag_use_cxa_atexit)
8509     {
8510       tree parmdecl;
8511
8512       parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8513       DECL_CONTEXT (parmdecl) = fndecl;
8514       TREE_USED (parmdecl) = 1;
8515       DECL_ARGUMENTS (fndecl) = parmdecl;
8516     }
8517
8518   pushdecl (fndecl);
8519   start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8520
8521   interface_unknown = old_interface_unknown;
8522   interface_only = old_interface_only;
8523
8524   pop_lang_context ();
8525
8526   return current_function_decl;
8527 }
8528
8529 /* Finish the cleanup function begun by start_cleanup_fn.  */
8530
8531 static void
8532 end_cleanup_fn (void)
8533 {
8534   expand_body (finish_function (0));
8535
8536   pop_from_top_level ();
8537 }
8538
8539 /* Generate code to handle the destruction of DECL, an object with
8540    static storage duration.  */
8541
8542 void
8543 register_dtor_fn (tree decl)
8544 {
8545   tree cleanup;
8546   tree compound_stmt;
8547   tree args;
8548   tree fcall;
8549   int saved_flag_access_control;
8550
8551   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8552     return;
8553
8554   /* Call build_cleanup before we enter the anonymous function so that
8555      any access checks will be done relative to the current scope,
8556      rather than the scope of the anonymous function.  */
8557   build_cleanup (decl);
8558
8559   /* Now start the function.  */
8560   cleanup = start_cleanup_fn ();
8561
8562   /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
8563      to the original function, rather than the anonymous one.  That
8564      will make the back-end think that nested functions are in use,
8565      which causes confusion.  */
8566   saved_flag_access_control = flag_access_control;
8567   scope_chain->check_access = flag_access_control = 0;
8568   fcall = build_cleanup (decl);
8569   scope_chain->check_access = flag_access_control = saved_flag_access_control;
8570
8571   /* Create the body of the anonymous function.  */
8572   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8573   finish_expr_stmt (fcall);
8574   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8575   end_cleanup_fn ();
8576
8577   /* Call atexit with the cleanup function.  */
8578   cxx_mark_addressable (cleanup);
8579   cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8580   if (flag_use_cxa_atexit)
8581     {
8582       args = tree_cons (NULL_TREE, 
8583                         build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8584                         NULL_TREE);
8585       args = tree_cons (NULL_TREE, null_pointer_node, args);
8586       args = tree_cons (NULL_TREE, cleanup, args);
8587     }
8588   else
8589     args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8590   finish_expr_stmt (build_function_call (get_atexit_node (), args));
8591 }
8592
8593 /* DECL is a VAR_DECL with static storage duration.  INIT, if present,
8594    is its initializer.  Generate code to handle the construction
8595    and destruction of DECL.  */
8596
8597 static void
8598 expand_static_init (tree decl, tree init)
8599 {
8600   tree oldstatic;
8601
8602   my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8603   my_friendly_assert (TREE_STATIC (decl), 20021010);
8604
8605   /* Some variables require no initialization.  */
8606   if (!init 
8607       && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8608       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8609     return;
8610
8611   oldstatic = value_member (decl, static_aggregates);
8612
8613   if (oldstatic)
8614     {
8615       if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8616         error ("multiple initializations given for `%D'", decl);
8617     }
8618   else if (! toplevel_bindings_p ())
8619     {
8620       /* Emit code to perform this initialization but once.  */
8621       tree if_stmt;
8622       tree then_clause;
8623       tree assignment;
8624       tree guard;
8625       tree guard_init;
8626
8627       /* Emit code to perform this initialization but once.  This code
8628          looks like:
8629
8630            static int guard = 0;
8631            if (!guard) {
8632              // Do initialization.
8633              guard = 1;
8634              // Register variable for destruction at end of program.
8635            }
8636
8637          Note that the `temp' variable is only set to 1 *after* the
8638          initialization is complete.  This ensures that an exception,
8639          thrown during the construction, will cause the variable to
8640          reinitialized when we pass through this code again, as per:
8641
8642            [stmt.dcl]
8643
8644            If the initialization exits by throwing an exception, the
8645            initialization is not complete, so it will be tried again
8646            the next time control enters the declaration.
8647
8648          In theory, this process should be thread-safe, too; multiple
8649          threads should not be able to initialize the variable more
8650          than once.  We don't yet attempt to ensure thread-safety.  */
8651
8652       /* Create the guard variable.  */
8653       guard = get_guard (decl);
8654
8655       /* Begin the conditional initialization.  */
8656       if_stmt = begin_if_stmt ();
8657       finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8658       then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8659
8660       /* Do the initialization itself.  */
8661       assignment = init ? init : NULL_TREE;
8662
8663       /* Once the assignment is complete, set TEMP to 1.  Since the
8664          construction of the static object is complete at this point,
8665          we want to make sure TEMP is set to 1 even if a temporary
8666          constructed during the initialization throws an exception
8667          when it is destroyed.  So, we combine the initialization and
8668          the assignment to TEMP into a single expression, ensuring
8669          that when we call finish_expr_stmt the cleanups will not be
8670          run until after TEMP is set to 1.  */
8671       guard_init = set_guard (guard);
8672       if (assignment)
8673         {
8674           assignment = tree_cons (NULL_TREE, assignment,
8675                                   build_tree_list (NULL_TREE,
8676                                                    guard_init));
8677           assignment = build_compound_expr (assignment);
8678         }
8679       else
8680         assignment = guard_init;
8681       finish_expr_stmt (assignment);
8682
8683       /* Use atexit to register a function for destroying this static
8684          variable.  */
8685       register_dtor_fn (decl);
8686
8687       finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8688       finish_then_clause (if_stmt);
8689       finish_if_stmt ();
8690     }
8691   else
8692     static_aggregates = tree_cons (init, decl, static_aggregates);
8693 }
8694
8695 /* Finish the declaration of a catch-parameter.  */
8696
8697 tree
8698 start_handler_parms (tree declspecs, tree declarator)
8699 {
8700   tree decl;
8701   if (declspecs)
8702     {
8703       decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8704                              1, NULL);
8705       if (decl == NULL_TREE)
8706         error ("invalid catch parameter");
8707     }
8708   else
8709     decl = NULL_TREE;
8710
8711   return decl;
8712 }
8713
8714 \f
8715 /* Make TYPE a complete type based on INITIAL_VALUE.
8716    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8717    2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
8718
8719 int
8720 complete_array_type (tree type, tree initial_value, int do_default)
8721 {
8722   register tree maxindex = NULL_TREE;
8723   int value = 0;
8724
8725   if (initial_value)
8726     {
8727       /* An array of character type can be initialized from a
8728          brace-enclosed string constant.  */
8729       if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8730           && TREE_CODE (initial_value) == CONSTRUCTOR
8731           && CONSTRUCTOR_ELTS (initial_value)
8732           && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8733               == STRING_CST)
8734           && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8735         initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8736
8737       /* Note MAXINDEX is really the maximum index, one less than the
8738          size.  */
8739       if (TREE_CODE (initial_value) == STRING_CST)
8740         {
8741           int eltsize
8742             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8743           maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8744                                    / eltsize) - 1, 0);
8745         }
8746       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8747         {
8748           tree elts = CONSTRUCTOR_ELTS (initial_value);
8749
8750           maxindex = ssize_int (-1);
8751           for (; elts; elts = TREE_CHAIN (elts))
8752             {
8753               if (TREE_PURPOSE (elts))
8754                 maxindex = TREE_PURPOSE (elts);
8755               else
8756                 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8757             }
8758           maxindex = copy_node (maxindex);
8759         }
8760       else
8761         {
8762           /* Make an error message unless that happened already.  */
8763           if (initial_value != error_mark_node)
8764             value = 1;
8765           else
8766             initial_value = NULL_TREE;
8767
8768           /* Prevent further error messages.  */
8769           maxindex = build_int_2 (0, 0);
8770         }
8771     }
8772
8773   if (!maxindex)
8774     {
8775       if (do_default)
8776         maxindex = build_int_2 (0, 0);
8777       value = 2;
8778     }
8779
8780   if (maxindex)
8781     {
8782       tree itype;
8783       tree domain;
8784
8785       domain = build_index_type (maxindex);
8786       TYPE_DOMAIN (type) = domain;
8787
8788       if (! TREE_TYPE (maxindex))
8789         TREE_TYPE (maxindex) = domain;
8790       if (initial_value)
8791         itype = TREE_TYPE (initial_value);
8792       else
8793         itype = NULL;
8794       if (itype && !TYPE_DOMAIN (itype))
8795         TYPE_DOMAIN (itype) = domain;
8796       /* The type of the main variant should never be used for arrays
8797          of different sizes.  It should only ever be completed with the
8798          size of the array.  */
8799       if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8800         TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8801     }
8802
8803   /* Lay out the type now that we can get the real answer.  */
8804
8805   layout_type (type);
8806
8807   return value;
8808 }
8809 \f
8810 /* Return zero if something is declared to be a member of type
8811    CTYPE when in the context of CUR_TYPE.  STRING is the error
8812    message to print in that case.  Otherwise, quietly return 1.  */
8813
8814 static int
8815 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8816 {
8817   if (ctype && ctype != cur_type)
8818     {
8819       if (flags == DTOR_FLAG)
8820         error ("destructor for alien class `%T' cannot be a member",
8821                   ctype);
8822       else
8823         error ("constructor for alien class `%T' cannot be a member",
8824                   ctype);
8825       return 0;
8826     }
8827   return 1;
8828 }
8829 \f
8830 /* Subroutine of `grokdeclarator'.  */
8831
8832 /* Generate errors possibly applicable for a given set of specifiers.
8833    This is for ARM $7.1.2.  */
8834
8835 static void
8836 bad_specifiers (tree object,
8837                 const char* type,
8838                 int virtualp,
8839                 int quals,
8840                 int inlinep,
8841                 int friendp,
8842                 int raises)
8843 {
8844   if (virtualp)
8845     error ("`%D' declared as a `virtual' %s", object, type);
8846   if (inlinep)
8847     error ("`%D' declared as an `inline' %s", object, type);
8848   if (quals)
8849     error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8850               object, type);
8851   if (friendp)
8852     cp_error_at ("`%D' declared as a friend", object);
8853   if (raises
8854       && (TREE_CODE (object) == TYPE_DECL
8855           || (!TYPE_PTRFN_P (TREE_TYPE (object))
8856               && !TYPE_REFFN_P (TREE_TYPE (object))
8857               && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8858     cp_error_at ("`%D' declared with an exception specification", object);
8859 }
8860
8861 /* CTYPE is class type, or null if non-class.
8862    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8863    or METHOD_TYPE.
8864    DECLARATOR is the function's name.
8865    VIRTUALP is truthvalue of whether the function is virtual or not.
8866    FLAGS are to be passed through to `grokclassfn'.
8867    QUALS are qualifiers indicating whether the function is `const'
8868    or `volatile'.
8869    RAISES is a list of exceptions that this function can raise.
8870    CHECK is 1 if we must find this method in CTYPE, 0 if we should
8871    not look, and -1 if we should not call `grokclassfn' at all.
8872
8873    Returns `NULL_TREE' if something goes wrong, after issuing
8874    applicable error messages.  */
8875
8876 static tree
8877 grokfndecl (tree ctype, 
8878             tree type,
8879             tree declarator,
8880             tree orig_declarator,
8881             int virtualp,
8882             enum overload_flags flags,
8883             tree quals, 
8884             tree raises,
8885             int check, 
8886             int friendp, 
8887             int publicp, 
8888             int inlinep, 
8889             int funcdef_flag, 
8890             int template_count,
8891             tree in_namespace)
8892 {
8893   tree decl;
8894   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8895   int has_default_arg = 0;
8896   tree t;
8897
8898   if (raises)
8899     type = build_exception_variant (type, raises);
8900
8901   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8902   /* Propagate volatile out from type to decl.  */
8903   if (TYPE_VOLATILE (type))
8904     TREE_THIS_VOLATILE (decl) = 1;
8905
8906   /* If this decl has namespace scope, set that up.  */
8907   if (in_namespace)
8908     set_decl_namespace (decl, in_namespace, friendp);
8909   else if (!ctype)
8910     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8911
8912   /* `main' and builtins have implicit 'C' linkage.  */
8913   if ((MAIN_NAME_P (declarator)
8914        || (IDENTIFIER_LENGTH (declarator) > 10
8915            && IDENTIFIER_POINTER (declarator)[0] == '_'
8916            && IDENTIFIER_POINTER (declarator)[1] == '_'
8917            && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8918       && current_lang_name == lang_name_cplusplus
8919       && ctype == NULL_TREE
8920       /* NULL_TREE means global namespace.  */
8921       && DECL_CONTEXT (decl) == NULL_TREE)
8922     SET_DECL_LANGUAGE (decl, lang_c);
8923
8924   /* Should probably propagate const out from type to decl I bet (mrs).  */
8925   if (staticp)
8926     {
8927       DECL_STATIC_FUNCTION_P (decl) = 1;
8928       DECL_CONTEXT (decl) = ctype;
8929     }
8930
8931   if (ctype)
8932     DECL_CONTEXT (decl) = ctype;
8933
8934   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8935     {
8936       if (processing_template_decl)
8937         error ("cannot declare `::main' to be a template");
8938       if (inlinep)
8939         error ("cannot declare `::main' to be inline");
8940       if (!publicp)
8941         error ("cannot declare `::main' to be static");
8942       if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8943                         integer_type_node))
8944         error ("`main' must return `int'");
8945       inlinep = 0;
8946       publicp = 1;
8947     }
8948
8949   /* Members of anonymous types and local classes have no linkage; make
8950      them internal.  */
8951   /* FIXME what if it gets a name from typedef?  */
8952   if (ctype && (TYPE_ANONYMOUS_P (ctype)
8953                 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8954     publicp = 0;
8955
8956   if (publicp)
8957     {
8958       /* [basic.link]: A name with no linkage (notably, the name of a class
8959          or enumeration declared in a local scope) shall not be used to
8960          declare an entity with linkage.
8961
8962          Only check this for public decls for now.  */
8963       t = no_linkage_check (TREE_TYPE (decl));
8964       if (t)
8965         {
8966           if (TYPE_ANONYMOUS_P (t))
8967             {
8968               if (DECL_EXTERN_C_P (decl))
8969                 /* Allow this; it's pretty common in C.  */;
8970               else
8971                 {
8972                   pedwarn ("non-local function `%#D' uses anonymous type",
8973                               decl);
8974                   if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8975                     cp_pedwarn_at ("\
8976 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8977                                 TYPE_NAME (t));
8978                 }
8979             }
8980           else
8981             pedwarn ("non-local function `%#D' uses local type `%T'",
8982                         decl, t);
8983         }
8984     }
8985
8986   TREE_PUBLIC (decl) = publicp;
8987   if (! publicp)
8988     {
8989       DECL_INTERFACE_KNOWN (decl) = 1;
8990       DECL_NOT_REALLY_EXTERN (decl) = 1;
8991     }
8992
8993   DID_INLINE_FUNC (decl) = 0;
8994   /* If the declaration was declared inline, mark it as such.  */
8995   if (inlinep)
8996     DECL_DECLARED_INLINE_P (decl) = 1;
8997   /* We inline functions that are explicitly declared inline, or, when
8998      the user explicitly asks us to, all functions.  */
8999   if (DECL_DECLARED_INLINE_P (decl))
9000     DECL_INLINE (decl) = 1;
9001   if (flag_inline_trees == 2 && !DECL_INLINE (decl))
9002     {
9003       DID_INLINE_FUNC (decl) = 1;
9004       DECL_INLINE (decl) = 1;
9005     }
9006
9007   DECL_EXTERNAL (decl) = 1;
9008   if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
9009     {
9010       error ("%smember function `%D' cannot have `%T' method qualifier",
9011                 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
9012       quals = NULL_TREE;
9013     }
9014
9015   if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
9016     grok_op_properties (decl, friendp);
9017
9018   if (ctype && decl_function_context (decl))
9019     DECL_NO_STATIC_CHAIN (decl) = 1;
9020
9021   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9022     if (TREE_PURPOSE (t)
9023         && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
9024       {
9025         has_default_arg = 1;
9026         break;
9027       }
9028
9029   if (friendp
9030       && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9031     {
9032       if (funcdef_flag)
9033         error
9034           ("defining explicit specialization `%D' in friend declaration",
9035            orig_declarator);
9036       else
9037         {
9038           tree fns = TREE_OPERAND (orig_declarator, 0);
9039           tree args = TREE_OPERAND (orig_declarator, 1);
9040
9041           if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9042             {
9043               /* Something like `template <class T> friend void f<T>()'.  */
9044               error ("invalid use of template-id `%D' in declaration of primary template",
9045                         orig_declarator);
9046               return NULL_TREE;
9047             }
9048
9049
9050           /* A friend declaration of the form friend void f<>().  Record
9051              the information in the TEMPLATE_ID_EXPR.  */
9052           SET_DECL_IMPLICIT_INSTANTIATION (decl);
9053
9054           if (TREE_CODE (fns) == COMPONENT_REF)
9055             {
9056               /* Due to bison parser ickiness, we will have already looked
9057                  up an operator_name or PFUNCNAME within the current class
9058                  (see template_id in parse.y). If the current class contains
9059                  such a name, we'll get a COMPONENT_REF here. Undo that.  */
9060
9061               my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
9062                                   == current_class_type, 20001120);
9063               fns = TREE_OPERAND (fns, 1);
9064             }
9065           my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
9066                               || TREE_CODE (fns) == LOOKUP_EXPR
9067                               || TREE_CODE (fns) == OVERLOAD, 20001120);
9068           DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
9069
9070           if (has_default_arg)
9071             {
9072               error ("default arguments are not allowed in declaration of friend template specialization `%D'",
9073                         decl);
9074               return NULL_TREE;
9075             }
9076
9077           if (inlinep)
9078             {
9079               error ("`inline' is not allowed in declaration of friend template specialization `%D'",
9080                         decl);
9081               return NULL_TREE;
9082             }
9083         }
9084     }
9085
9086   if (funcdef_flag)
9087     /* Make the init_value nonzero so pushdecl knows this is not
9088        tentative.  error_mark_node is replaced later with the BLOCK.  */
9089     DECL_INITIAL (decl) = error_mark_node;
9090
9091   if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9092     TREE_NOTHROW (decl) = 1;
9093
9094   /* Caller will do the rest of this.  */
9095   if (check < 0)
9096     return decl;
9097
9098   if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
9099     DECL_CONSTRUCTOR_P (decl) = 1;
9100
9101   /* Function gets the ugly name, field gets the nice one.  This call
9102      may change the type of the function (because of default
9103      parameters)!  */
9104   if (ctype != NULL_TREE)
9105     grokclassfn (ctype, decl, flags, quals);
9106
9107   decl = check_explicit_specialization (orig_declarator, decl,
9108                                         template_count,
9109                                         2 * (funcdef_flag != 0) +
9110                                         4 * (friendp != 0));
9111   if (decl == error_mark_node)
9112     return NULL_TREE;
9113
9114   if (ctype != NULL_TREE
9115       && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9116       && check)
9117     {
9118       tree old_decl;
9119
9120       old_decl = check_classfn (ctype, decl);
9121
9122       if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9123         /* Because grokfndecl is always supposed to return a
9124            FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9125            here.  We depend on our callers to figure out that its
9126            really a template that's being returned.  */
9127         old_decl = DECL_TEMPLATE_RESULT (old_decl);
9128
9129       if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9130           && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9131         {
9132           /* Remove the `this' parm added by grokclassfn.
9133              XXX Isn't this done in start_function, too?  */
9134           revert_static_member_fn (decl);
9135           last_function_parms = TREE_CHAIN (last_function_parms);
9136         }
9137       if (old_decl && DECL_ARTIFICIAL (old_decl))
9138         error ("definition of implicitly-declared `%D'", old_decl);
9139
9140       if (old_decl)
9141         {
9142           /* Since we've smashed OLD_DECL to its
9143              DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9144           if (TREE_CODE (decl) == TEMPLATE_DECL)
9145             decl = DECL_TEMPLATE_RESULT (decl);
9146
9147           /* Attempt to merge the declarations.  This can fail, in
9148              the case of some invalid specialization declarations.  */
9149           push_scope (ctype);
9150           if (!duplicate_decls (decl, old_decl))
9151             error ("no `%#D' member function declared in class `%T'",
9152                       decl, ctype);
9153           pop_scope (ctype);
9154           return old_decl;
9155         }
9156     }
9157
9158   if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9159     return NULL_TREE;
9160
9161   if (ctype == NULL_TREE || check)
9162     return decl;
9163
9164   if (virtualp)
9165     DECL_VIRTUAL_P (decl) = 1;
9166
9167   return decl;
9168 }
9169
9170 /* Create a VAR_DECL named NAME with the indicated TYPE.  
9171
9172    If SCOPE is non-NULL, it is the class type or namespace containing
9173    the variable.  If SCOPE is NULL, the variable should is created in
9174    the innermost enclosings scope.  */
9175
9176 static tree
9177 grokvardecl (tree type,
9178              tree name,
9179              RID_BIT_TYPE * specbits_in,
9180              int initialized,
9181              int constp,
9182              tree scope)
9183 {
9184   tree decl;
9185   RID_BIT_TYPE specbits;
9186
9187   my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, 
9188                       20020808);
9189
9190   specbits = *specbits_in;
9191
9192   /* Compute the scope in which to place the variable.  */
9193   if (!scope)
9194     {
9195       /* An explicit "extern" specifier indicates a namespace-scope
9196          variable.  */
9197       if (RIDBIT_SETP (RID_EXTERN, specbits))
9198         scope = current_namespace;
9199       else if (!at_function_scope_p ())
9200         {
9201           scope = current_scope ();
9202           if (!scope)
9203             scope = current_namespace;
9204         }
9205     }
9206
9207   if (scope
9208       && (/* If the variable is a namespace-scope variable declared in a
9209              template, we need DECL_LANG_SPECIFIC.  */
9210           (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9211           /* Similarly for namespace-scope variables with language linkage
9212              other than C++.  */
9213           || (TREE_CODE (scope) == NAMESPACE_DECL 
9214               && current_lang_name != lang_name_cplusplus)
9215           /* Similarly for static data members.  */
9216           || TYPE_P (scope)))
9217     decl = build_lang_decl (VAR_DECL, name, type);
9218   else
9219     decl = build_decl (VAR_DECL, name, type);
9220
9221   if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9222     set_decl_namespace (decl, scope, 0);
9223   else
9224     DECL_CONTEXT (decl) = scope;
9225
9226   if (name && scope && current_lang_name != lang_name_c)
9227     /* We can't mangle lazily here because we don't have any
9228        way to recover whether or not a variable was `extern
9229        "C"' later.  */
9230     mangle_decl (decl);
9231
9232   if (RIDBIT_SETP (RID_EXTERN, specbits))
9233     {
9234       DECL_THIS_EXTERN (decl) = 1;
9235       DECL_EXTERNAL (decl) = !initialized;
9236     }
9237
9238   /* In class context, static means one per class,
9239      public access, and static storage.  */
9240   if (DECL_CLASS_SCOPE_P (decl))
9241     {
9242       TREE_PUBLIC (decl) = 1;
9243       TREE_STATIC (decl) = 1;
9244       DECL_EXTERNAL (decl) = 0;
9245     }
9246   /* At top level, either `static' or no s.c. makes a definition
9247      (perhaps tentative), and absence of `static' makes it public.  */
9248   else if (toplevel_bindings_p ())
9249     {
9250       TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9251                             && (DECL_THIS_EXTERN (decl) || ! constp));
9252       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9253     }
9254   /* Not at top level, only `static' makes a static definition.  */
9255   else
9256     {
9257       TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9258       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9259     }
9260
9261   if (RIDBIT_SETP (RID_THREAD, specbits))
9262     {
9263       if (targetm.have_tls)
9264         DECL_THREAD_LOCAL (decl) = 1;
9265       else
9266         /* A mere warning is sure to result in improper semantics
9267            at runtime.  Don't bother to allow this to compile.  */
9268         error ("thread-local storage not supported for this target");
9269     }
9270
9271   if (TREE_PUBLIC (decl))
9272     {
9273       /* [basic.link]: A name with no linkage (notably, the name of a class
9274          or enumeration declared in a local scope) shall not be used to
9275          declare an entity with linkage.
9276
9277          Only check this for public decls for now.  */
9278       tree t = no_linkage_check (TREE_TYPE (decl));
9279       if (t)
9280         {
9281           if (TYPE_ANONYMOUS_P (t))
9282             /* Ignore for now; `enum { foo } e' is pretty common.  */;
9283           else
9284             pedwarn ("non-local variable `%#D' uses local type `%T'",
9285                         decl, t);
9286         }
9287     }
9288
9289   return decl;
9290 }
9291
9292 /* Create and return a canonical pointer to member function type, for
9293    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9294
9295 tree
9296 build_ptrmemfunc_type (tree type)
9297 {
9298   tree field, fields;
9299   tree t;
9300   tree unqualified_variant = NULL_TREE;
9301
9302   if (type == error_mark_node)
9303     return type;
9304
9305   /* If a canonical type already exists for this type, use it.  We use
9306      this method instead of type_hash_canon, because it only does a
9307      simple equality check on the list of field members.  */
9308
9309   if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9310     return t;
9311
9312   /* Make sure that we always have the unqualified pointer-to-member
9313      type first.  */
9314   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9315     unqualified_variant
9316       = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9317
9318   t = make_aggr_type (RECORD_TYPE);
9319   /* Let the front-end know this is a pointer to member function...  */
9320   TYPE_PTRMEMFUNC_FLAG (t) = 1;
9321   /* ... and not really an aggregate.  */
9322   SET_IS_AGGR_TYPE (t, 0);
9323
9324   field = build_decl (FIELD_DECL, pfn_identifier, type);
9325   fields = field;
9326   
9327   field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9328   TREE_CHAIN (field) = fields;
9329   fields = field;
9330   
9331   finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9332
9333   /* Zap out the name so that the back-end will give us the debugging
9334      information for this anonymous RECORD_TYPE.  */
9335   TYPE_NAME (t) = NULL_TREE;
9336
9337   /* If this is not the unqualified form of this pointer-to-member
9338      type, set the TYPE_MAIN_VARIANT for this type to be the
9339      unqualified type.  Since they are actually RECORD_TYPEs that are
9340      not variants of each other, we must do this manually.  */
9341   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9342     {
9343       t = build_qualified_type (t, cp_type_quals (type));
9344       TYPE_MAIN_VARIANT (t) = unqualified_variant;
9345       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9346       TYPE_NEXT_VARIANT (unqualified_variant) = t;
9347     }
9348
9349   /* Cache this pointer-to-member type so that we can find it again
9350      later.  */
9351   TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9352
9353   /* Seems to be wanted.  */
9354   CLASSTYPE_GOT_SEMICOLON (t) = 1;
9355
9356   return t;
9357 }
9358
9359 /* Create and return a pointer to data member type.  */
9360
9361 tree
9362 build_ptrmem_type (tree class_type, tree member_type)
9363 {
9364   return build_pointer_type (build_offset_type (class_type, member_type));
9365 }
9366
9367 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9368    Check to see that the definition is valid.  Issue appropriate error
9369    messages.  Return 1 if the definition is particularly bad, or 0
9370    otherwise.  */
9371
9372 int
9373 check_static_variable_definition (tree decl, tree type)
9374 {
9375   /* Motion 10 at San Diego: If a static const integral data member is
9376      initialized with an integral constant expression, the initializer
9377      may appear either in the declaration (within the class), or in
9378      the definition, but not both.  If it appears in the class, the
9379      member is a member constant.  The file-scope definition is always
9380      required.  */
9381   if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9382     {
9383       error ("invalid in-class initialization of static data member of non-integral type `%T'",
9384              type);
9385       /* If we just return the declaration, crashes will sometimes
9386          occur.  We therefore return void_type_node, as if this was a
9387          friend declaration, to cause callers to completely ignore
9388          this declaration.  */
9389       return 1;
9390     }
9391   else if (!CP_TYPE_CONST_P (type))
9392     error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9393               decl);
9394   else if (pedantic && !INTEGRAL_TYPE_P (type))
9395     pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9396
9397   return 0;
9398 }
9399
9400 /* Given the SIZE (i.e., number of elements) in an array, compute an
9401    appropriate index type for the array.  If non-NULL, NAME is the
9402    name of the thing being declared.  */
9403
9404 tree
9405 compute_array_index_type (tree name, tree size)
9406 {
9407   tree itype;
9408
9409   /* If this involves a template parameter, it will be a constant at
9410      instantiation time, but we don't know what the value is yet.
9411      Even if no template parameters are involved, we may an expression
9412      that is not a constant; we don't even simplify `1 + 2' when
9413      processing a template.  */
9414   if (processing_template_decl)
9415     {
9416       /* Resolve a qualified reference to an enumerator or static
9417          const data member of ours.  */
9418       if (TREE_CODE (size) == SCOPE_REF
9419           && TREE_OPERAND (size, 0) == current_class_type)
9420         {
9421           tree t = lookup_field (current_class_type,
9422                                  TREE_OPERAND (size, 1), 0, false);
9423           if (t)
9424             size = t;
9425         }
9426
9427       return build_index_type (build_min (MINUS_EXPR, sizetype,
9428                                           size, integer_one_node));
9429     }
9430
9431   /* The size might be the result of a cast.  */
9432   STRIP_TYPE_NOPS (size);
9433
9434   /* It might be a const variable or enumeration constant.  */
9435   size = decl_constant_value (size);
9436
9437   /* The array bound must be an integer type.  */
9438   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9439       && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9440       && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9441     {
9442       if (name)
9443         error ("size of array `%D' has non-integer type", name);
9444       else
9445         error ("size of array has non-integer type");
9446       size = integer_one_node;
9447     }
9448
9449   /* Normally, the array-bound will be a constant.  */
9450   if (TREE_CODE (size) == INTEGER_CST)
9451     {
9452       /* Check to see if the array bound overflowed.  Make that an
9453          error, no matter how generous we're being.  */
9454       int old_flag_pedantic_errors = flag_pedantic_errors;
9455       int old_pedantic = pedantic;
9456       pedantic = flag_pedantic_errors = 1;
9457       constant_expression_warning (size);
9458       pedantic = old_pedantic;
9459       flag_pedantic_errors = old_flag_pedantic_errors;
9460
9461       /* An array must have a positive number of elements.  */
9462       if (INT_CST_LT (size, integer_zero_node))
9463         {
9464           if (name)
9465             error ("size of array `%D' is negative", name);
9466           else
9467             error ("size of array is negative");
9468           size = integer_one_node;
9469         }
9470       /* Except that an extension we allow zero-sized arrays.  We
9471          always allow them in system headers because glibc uses
9472          them.  */
9473       else if (integer_zerop (size) && pedantic && !in_system_header)
9474         {
9475           if (name)
9476             pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9477           else
9478             pedwarn ("ISO C++ forbids zero-size array");
9479         }
9480     }
9481   else if (TREE_CONSTANT (size))
9482     {
9483       /* `(int) &fn' is not a valid array bound.  */
9484       if (name)
9485         error ("size of array `%D' is not an integral constant-expression",
9486                   name);
9487       else
9488         error ("size of array is not an integral constant-expression");
9489     }
9490
9491   /* Compute the index of the largest element in the array.  It is
9492      one less than the number of elements in the array.  */
9493   itype
9494     = fold (cp_build_binary_op (MINUS_EXPR,
9495                                 cp_convert (ssizetype, size),
9496                                 cp_convert (ssizetype,
9497                                             integer_one_node)));
9498
9499   /* Check for variable-sized arrays.  We allow such things as an
9500      extension, even though they are not allowed in ANSI/ISO C++.  */
9501   if (!TREE_CONSTANT (itype))
9502     {
9503       if (pedantic)
9504         {
9505           if (name)
9506             pedwarn ("ISO C++ forbids variable-size array `%D'",
9507                         name);
9508           else
9509             pedwarn ("ISO C++ forbids variable-size array");
9510         }
9511
9512       /* Create a variable-sized array index type.  */
9513       itype = variable_size (itype);
9514     }
9515   /* Make sure that there was no overflow when creating to a signed
9516      index type.  (For example, on a 32-bit machine, an array with
9517      size 2^32 - 1 is too big.)  */
9518   else if (TREE_OVERFLOW (itype))
9519     {
9520       error ("overflow in array dimension");
9521       TREE_OVERFLOW (itype) = 0;
9522     }
9523
9524   /* Create and return the appropriate index type.  */
9525   return build_index_type (itype);
9526 }
9527
9528 /* Returns the scope (if any) in which the entity declared by
9529    DECLARATOR will be located.  If the entity was declared with an
9530    unqualified name, NULL_TREE is returned.  */
9531
9532 tree
9533 get_scope_of_declarator (tree declarator)
9534 {
9535   if (!declarator)
9536     return NULL_TREE;
9537   
9538   switch (TREE_CODE (declarator))
9539     {
9540     case CALL_EXPR:
9541     case ARRAY_REF:
9542     case INDIRECT_REF:
9543     case ADDR_EXPR:
9544       /* For any of these, the main declarator is the first operand.  */
9545       return get_scope_of_declarator (TREE_OPERAND
9546                                       (declarator, 0));
9547
9548     case SCOPE_REF:
9549       /* For a pointer-to-member, continue descending.  */
9550       if (TREE_CODE (TREE_OPERAND (declarator, 1))
9551           == INDIRECT_REF)
9552         return get_scope_of_declarator (TREE_OPERAND
9553                                         (declarator, 1));
9554       /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9555          which the declaration occurs is the first operand.  */
9556       return TREE_OPERAND (declarator, 0);
9557
9558     case TREE_LIST:
9559       /* Attributes to be applied. The declarator is TREE_VALUE.  */
9560       return get_scope_of_declarator (TREE_VALUE (declarator));
9561       
9562     default:
9563       /* Otherwise, we have a declarator-id which is not a qualified
9564          name; the entity will be declared in the current scope.  */
9565       return NULL_TREE;
9566     }
9567 }
9568
9569 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9570    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
9571    with this type.  */
9572
9573 static tree
9574 create_array_type_for_decl (tree name, tree type, tree size)
9575 {
9576   tree itype = NULL_TREE;
9577   const char* error_msg;
9578
9579   /* If things have already gone awry, bail now.  */
9580   if (type == error_mark_node || size == error_mark_node)
9581     return error_mark_node;
9582
9583   /* Assume that everything will go OK.  */
9584   error_msg = NULL;
9585
9586   /* There are some types which cannot be array elements.  */
9587   switch (TREE_CODE (type))
9588     {
9589     case VOID_TYPE:
9590       error_msg = "array of void";
9591       break;
9592
9593     case FUNCTION_TYPE:
9594       error_msg = "array of functions";
9595       break;
9596
9597     case REFERENCE_TYPE:
9598       error_msg = "array of references";
9599       break;
9600
9601     case OFFSET_TYPE:
9602       error_msg = "array of data members";
9603       break;
9604
9605     case METHOD_TYPE:
9606       error_msg = "array of function members";
9607       break;
9608
9609     default:
9610       break;
9611     }
9612
9613   /* If something went wrong, issue an error-message and return.  */
9614   if (error_msg)
9615     {
9616       if (name)
9617         error ("declaration of `%D' as %s", name, error_msg);
9618       else
9619         error ("creating %s", error_msg);
9620
9621       return error_mark_node;
9622     }
9623
9624   /* [dcl.array]
9625
9626      The constant expressions that specify the bounds of the arrays
9627      can be omitted only for the first member of the sequence.  */
9628   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9629     {
9630       if (name)
9631         error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9632                   name);
9633       else
9634         error ("multidimensional array must have bounds for all dimensions except the first");
9635
9636       return error_mark_node;
9637     }
9638
9639   /* Figure out the index type for the array.  */
9640   if (size)
9641     itype = compute_array_index_type (name, size);
9642
9643   return build_cplus_array_type (type, itype);
9644 }
9645
9646 /* Check that it's OK to declare a function with the indicated TYPE.
9647    SFK indicates the kind of special function (if any) that this
9648    function is.  OPTYPE is the type given in a conversion operator
9649    declaration.  Returns the actual return type of the function; that
9650    may be different than TYPE if an error occurs, or for certain
9651    special functions.  */
9652
9653 static tree
9654 check_special_function_return_type (special_function_kind sfk,
9655                                     tree type,
9656                                     tree optype)
9657 {
9658   switch (sfk)
9659     {
9660     case sfk_constructor:
9661       if (type)
9662         error ("return type specification for constructor invalid");
9663
9664       type = void_type_node;
9665       break;
9666
9667     case sfk_destructor:
9668       if (type)
9669         error ("return type specification for destructor invalid");
9670       type = void_type_node;
9671       break;
9672
9673     case sfk_conversion:
9674       if (type && !same_type_p (type, optype))
9675         error ("operator `%T' declared to return `%T'", optype, type);
9676       else if (type)
9677         pedwarn ("return type specified for `operator %T'",  optype);
9678       type = optype;
9679       break;
9680
9681     default:
9682       abort ();
9683       break;
9684     }
9685
9686   return type;
9687 }
9688
9689 /* Given declspecs and a declarator (abstract or otherwise), determine
9690    the name and type of the object declared and construct a DECL node
9691    for it.
9692
9693    DECLSPECS is a chain of tree_list nodes whose value fields
9694     are the storage classes and type specifiers.
9695
9696    DECL_CONTEXT says which syntactic context this declaration is in:
9697      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9698      FUNCDEF for a function definition.  Like NORMAL but a few different
9699       error messages in each case.  Return value may be zero meaning
9700       this definition is too screwy to try to parse.
9701      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
9702       handle member functions (which have FIELD context).
9703       Return value may be zero meaning this definition is too screwy to
9704       try to parse.
9705      PARM for a parameter declaration (either within a function prototype
9706       or before a function body).  Make a PARM_DECL, or return void_type_node.
9707      CATCHPARM for a parameter declaration before a catch clause.
9708      TYPENAME if for a typename (in a cast or sizeof).
9709       Don't make a DECL node; just return the ..._TYPE node.
9710      FIELD for a struct or union field; make a FIELD_DECL.
9711      BITFIELD for a field with specified width.
9712    INITIALIZED is 1 if the decl has an initializer.
9713
9714    ATTRLIST is a pointer to the list of attributes, which may be NULL
9715    if there are none; *ATTRLIST may be modified if attributes from inside
9716    the declarator should be applied to the declaration.
9717
9718    When this function is called, scoping variables (such as
9719    CURRENT_CLASS_TYPE) should reflect the scope in which the
9720    declaration occurs, not the scope in which the new declaration will
9721    be placed.  For example, on:
9722
9723      void S::f() { ... }
9724
9725    when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9726    should not be `S'.  */
9727
9728 tree
9729 grokdeclarator (tree declarator,
9730                 tree declspecs,
9731                 enum decl_context decl_context,
9732                 int initialized,
9733                 tree* attrlist)
9734 {
9735   RID_BIT_TYPE specbits;
9736   int nclasses = 0;
9737   tree spec;
9738   tree type = NULL_TREE;
9739   int longlong = 0;
9740   int type_quals;
9741   int virtualp, explicitp, friendp, inlinep, staticp;
9742   int explicit_int = 0;
9743   int explicit_char = 0;
9744   int defaulted_int = 0;
9745   int extern_langp = 0;
9746   tree dependant_name = NULL_TREE;
9747   
9748   tree typedef_decl = NULL_TREE;
9749   const char *name;
9750   tree typedef_type = NULL_TREE;
9751   int funcdef_flag = 0;
9752   enum tree_code innermost_code = ERROR_MARK;
9753   int bitfield = 0;
9754 #if 0
9755   /* See the code below that used this.  */
9756   tree decl_attr = NULL_TREE;
9757 #endif
9758
9759   /* Keep track of what sort of function is being processed
9760      so that we can warn about default return values, or explicit
9761      return values which do not match prescribed defaults.  */
9762   special_function_kind sfk = sfk_none;
9763
9764   tree dname = NULL_TREE;
9765   tree ctype = current_class_type;
9766   tree ctor_return_type = NULL_TREE;
9767   enum overload_flags flags = NO_SPECIAL;
9768   tree quals = NULL_TREE;
9769   tree raises = NULL_TREE;
9770   int template_count = 0;
9771   tree in_namespace = NULL_TREE;
9772   tree returned_attrs = NULL_TREE;
9773   tree scope = NULL_TREE;
9774
9775   RIDBIT_RESET_ALL (specbits);
9776   if (decl_context == FUNCDEF)
9777     funcdef_flag = 1, decl_context = NORMAL;
9778   else if (decl_context == MEMFUNCDEF)
9779     funcdef_flag = -1, decl_context = FIELD;
9780   else if (decl_context == BITFIELD)
9781     bitfield = 1, decl_context = FIELD;
9782
9783   /* Look inside a declarator for the name being declared
9784      and get it as a string, for an error message.  */
9785   {
9786     tree *next = &declarator;
9787     register tree decl;
9788     name = NULL;
9789
9790     while (next && *next)
9791       {
9792         decl = *next;
9793         switch (TREE_CODE (decl))
9794           {
9795           case TREE_LIST:
9796             /* For attributes.  */
9797             next = &TREE_VALUE (decl);
9798             break;
9799
9800           case COND_EXPR:
9801             ctype = NULL_TREE;
9802             next = &TREE_OPERAND (decl, 0);
9803             break;
9804
9805           case BIT_NOT_EXPR:    /* For C++ destructors!  */
9806             {
9807               tree name = TREE_OPERAND (decl, 0);
9808               tree rename = NULL_TREE;
9809
9810               my_friendly_assert (flags == NO_SPECIAL, 152);
9811               flags = DTOR_FLAG;
9812               sfk = sfk_destructor;
9813               if (TYPE_P (name))
9814                 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9815               my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9816               if (ctype == NULL_TREE)
9817                 {
9818                   if (current_class_type == NULL_TREE)
9819                     {
9820                       error ("destructors must be member functions");
9821                       flags = NO_SPECIAL;
9822                     }
9823                   else
9824                     {
9825                       tree t = constructor_name (current_class_type);
9826                       if (t != name)
9827                         rename = t;
9828                     }
9829                 }
9830               else
9831                 {
9832                   tree t = constructor_name (ctype);
9833                   if (t != name)
9834                     rename = t;
9835                 }
9836
9837               if (rename)
9838                 {
9839                   error ("destructor `%T' must match class name `%T'",
9840                             name, rename);
9841                   TREE_OPERAND (decl, 0) = rename;
9842                 }
9843               next = &name;
9844             }
9845             break;
9846
9847           case ADDR_EXPR:       /* C++ reference declaration */
9848             /* Fall through.  */
9849           case ARRAY_REF:
9850           case INDIRECT_REF:
9851             ctype = NULL_TREE;
9852             innermost_code = TREE_CODE (decl);
9853             next = &TREE_OPERAND (decl, 0);
9854             break;
9855
9856           case CALL_EXPR:
9857             innermost_code = TREE_CODE (decl);
9858             if (decl_context == FIELD && ctype == NULL_TREE)
9859               ctype = current_class_type;
9860             if (ctype
9861                 && TREE_OPERAND (decl, 0)
9862                 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9863                     && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9864                                            ctype)))
9865               TREE_OPERAND (decl, 0) = constructor_name (ctype);
9866             next = &TREE_OPERAND (decl, 0);
9867             decl = *next;
9868             if (ctype != NULL_TREE
9869                 && decl != NULL_TREE && flags != DTOR_FLAG
9870                 && constructor_name_p (decl, ctype))
9871               {
9872                 sfk = sfk_constructor;
9873                 ctor_return_type = ctype;
9874               }
9875             ctype = NULL_TREE;
9876             break;
9877
9878           case TEMPLATE_ID_EXPR:
9879               {
9880                 tree fns = TREE_OPERAND (decl, 0);
9881
9882                 if (TREE_CODE (fns) == LOOKUP_EXPR)
9883                   fns = TREE_OPERAND (fns, 0);
9884
9885                 dname = fns;
9886                 if (TREE_CODE (dname) == COMPONENT_REF)
9887                   dname = TREE_OPERAND (dname, 1);
9888                 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9889                   {
9890                     my_friendly_assert (is_overloaded_fn (dname),
9891                                         19990331);
9892                     dname = DECL_NAME (get_first_fn (dname));
9893                   }
9894               }
9895           /* Fall through.  */
9896
9897           case IDENTIFIER_NODE:
9898             if (TREE_CODE (decl) == IDENTIFIER_NODE)
9899               dname = decl;
9900
9901             next = 0;
9902
9903             if (C_IS_RESERVED_WORD (dname))
9904               {
9905                 error ("declarator-id missing; using reserved word `%D'",
9906                           dname);
9907                 name = IDENTIFIER_POINTER (dname);
9908               }
9909             else if (!IDENTIFIER_TYPENAME_P (dname))
9910               name = IDENTIFIER_POINTER (dname);
9911             else
9912               {
9913                 my_friendly_assert (flags == NO_SPECIAL, 154);
9914                 flags = TYPENAME_FLAG;
9915                 ctor_return_type = TREE_TYPE (dname);
9916                 sfk = sfk_conversion;
9917                 if (IDENTIFIER_GLOBAL_VALUE (dname)
9918                     && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9919                         == TYPE_DECL))
9920                   name = IDENTIFIER_POINTER (dname);
9921                 else
9922                   name = "<invalid operator>";
9923               }
9924             break;
9925
9926             /* C++ extension */
9927           case SCOPE_REF:
9928             {
9929               /* Perform error checking, and decide on a ctype.  */
9930               tree cname = TREE_OPERAND (decl, 0);
9931               if (cname == NULL_TREE)
9932                 ctype = NULL_TREE;
9933               else if (TREE_CODE (cname) == NAMESPACE_DECL)
9934                 {
9935                   ctype = NULL_TREE;
9936                   in_namespace = TREE_OPERAND (decl, 0);
9937                 }
9938               else if (! is_aggr_type (cname, 1))
9939                 ctype = NULL_TREE;
9940               /* Must test TREE_OPERAND (decl, 1), in case user gives
9941                  us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
9942               else if (TREE_OPERAND (decl, 1)
9943                        && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9944                 ctype = cname;
9945               else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9946                        || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9947                 {
9948                   /* This might be declaring a member of a template
9949                      parm to be a friend.  */
9950                   ctype = cname;
9951                   dependant_name = TREE_OPERAND (decl, 1);
9952                 }
9953               else if (ctype == NULL_TREE)
9954                 ctype = cname;
9955               else if (TREE_COMPLEXITY (decl) == current_class_depth)
9956                 ;
9957               else
9958                 {
9959                   if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9960                     {
9961                       error ("type `%T' is not derived from type `%T'",
9962                                 cname, ctype);
9963                       ctype = NULL_TREE;
9964                     }
9965                   else
9966                     ctype = cname;
9967                 }
9968
9969               /* It is valid to write:
9970
9971                    class C { void f(); };
9972                    typedef C D;
9973                    void D::f();
9974
9975                  The standard is not clear about whether `typedef const C D' is
9976                  legal; as of 2002-09-15 the committee is considering
9977                  that question.  EDG 3.0 allows that syntax.
9978                  Therefore, we do as well.  */
9979               if (ctype)
9980                 ctype = TYPE_MAIN_VARIANT (ctype);
9981               /* Update the declarator so that when we process it
9982                  again the correct type is present.  */
9983               TREE_OPERAND (decl, 0) = ctype;
9984
9985               if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9986                   && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9987                                          ctype))
9988                 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9989               next = &TREE_OPERAND (decl, 1);
9990               decl = *next;
9991               if (ctype)
9992                 {
9993                   if (TREE_CODE (decl) == IDENTIFIER_NODE
9994                       && constructor_name_p (decl, ctype))
9995                     {
9996                       sfk = sfk_constructor;
9997                       ctor_return_type = ctype;
9998                     }
9999                   else if (TREE_CODE (decl) == BIT_NOT_EXPR
10000                            && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
10001                            && constructor_name_p (TREE_OPERAND (decl, 0),
10002                                                   ctype))
10003                     {
10004                       sfk = sfk_destructor;
10005                       ctor_return_type = ctype;
10006                       flags = DTOR_FLAG;
10007                       TREE_OPERAND (decl, 0) = constructor_name (ctype);
10008                       next = &TREE_OPERAND (decl, 0);
10009                     }
10010                 }
10011             }
10012             break;
10013
10014           case ERROR_MARK:
10015             next = 0;
10016             break;
10017
10018           case TYPE_DECL:
10019             /* Parse error puts this typespec where
10020                a declarator should go.  */
10021             error ("`%T' specified as declarator-id", DECL_NAME (decl));
10022             if (TREE_TYPE (decl) == current_class_type)
10023               error ("  perhaps you want `%T' for a constructor",
10024                         current_class_name);
10025             dname = DECL_NAME (decl);
10026             name = IDENTIFIER_POINTER (dname);
10027
10028             /* Avoid giving two errors for this.  */
10029             IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
10030
10031             declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
10032             *next = dname;
10033             next = 0;
10034             break;
10035
10036           case BASELINK:
10037             next = &BASELINK_FUNCTIONS (decl);
10038             break;
10039
10040           case TEMPLATE_DECL:
10041             /* Sometimes, we see a template-name used as part of a 
10042                decl-specifier like in 
10043                   std::allocator alloc;
10044                Handle that gracefully.  */
10045             error ("invalid use of template-name '%E' in a declarator", decl);
10046             return error_mark_node;
10047             break;
10048             
10049           default:
10050             my_friendly_assert (0, 20020917);
10051           }
10052       }
10053   }
10054
10055   /* A function definition's declarator must have the form of
10056      a function declarator.  */
10057
10058   if (funcdef_flag && innermost_code != CALL_EXPR)
10059     return 0;
10060
10061   if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
10062       && innermost_code != CALL_EXPR
10063       && ! (ctype && declspecs == NULL_TREE))
10064     {
10065       error ("declaration of `%D' as non-function", dname);
10066       return void_type_node;
10067     }
10068
10069   /* Anything declared one level down from the top level
10070      must be one of the parameters of a function
10071      (because the body is at least two levels down).  */
10072
10073   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10074      by not allowing C++ class definitions to specify their parameters
10075      with xdecls (must be spec.d in the parmlist).
10076
10077      Since we now wait to push a class scope until we are sure that
10078      we are in a legitimate method context, we must set oldcname
10079      explicitly (since current_class_name is not yet alive).
10080
10081      We also want to avoid calling this a PARM if it is in a namespace.  */
10082
10083   if (decl_context == NORMAL && !toplevel_bindings_p ())
10084     {
10085       struct cp_binding_level *b = current_binding_level;
10086       current_binding_level = b->level_chain;
10087       if (current_binding_level != 0 && toplevel_bindings_p ())
10088         decl_context = PARM;
10089       current_binding_level = b;
10090     }
10091
10092   if (name == NULL)
10093     name = decl_context == PARM ? "parameter" : "type name";
10094
10095   /* Look through the decl specs and record which ones appear.
10096      Some typespecs are defined as built-in typenames.
10097      Others, the ones that are modifiers of other types,
10098      are represented by bits in SPECBITS: set the bits for
10099      the modifiers that appear.  Storage class keywords are also in SPECBITS.
10100
10101      If there is a typedef name or a type, store the type in TYPE.
10102      This includes builtin typedefs such as `int'.
10103
10104      Set EXPLICIT_INT if the type is `int' or `char' and did not
10105      come from a user typedef.
10106
10107      Set LONGLONG if `long' is mentioned twice.
10108
10109      For C++, constructors and destructors have their own fast treatment.  */
10110
10111   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10112     {
10113       register int i;
10114       register tree id;
10115
10116       /* Certain parse errors slip through.  For example,
10117          `int class;' is not caught by the parser. Try
10118          weakly to recover here.  */
10119       if (TREE_CODE (spec) != TREE_LIST)
10120         return 0;
10121
10122       id = TREE_VALUE (spec);
10123
10124       /* If the entire declaration is itself tagged as deprecated then
10125          suppress reports of deprecated items.  */
10126       if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10127         {
10128           if (deprecated_state != DEPRECATED_SUPPRESS)
10129             warn_deprecated_use (id);
10130         }
10131
10132       if (TREE_CODE (id) == IDENTIFIER_NODE)
10133         {
10134           if (id == ridpointers[(int) RID_INT]
10135               || id == ridpointers[(int) RID_CHAR]
10136               || id == ridpointers[(int) RID_BOOL]
10137               || id == ridpointers[(int) RID_WCHAR])
10138             {
10139               if (type)
10140                 {
10141                   if (id == ridpointers[(int) RID_BOOL])
10142                     error ("`bool' is now a keyword");
10143                   else
10144                     error ("extraneous `%T' ignored", id);
10145                 }
10146               else
10147                 {
10148                   if (id == ridpointers[(int) RID_INT])
10149                     explicit_int = 1;
10150                   else if (id == ridpointers[(int) RID_CHAR])
10151                     explicit_char = 1;
10152                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10153                 }
10154               goto found;
10155             }
10156           /* C++ aggregate types.  */
10157           if (IDENTIFIER_HAS_TYPE_VALUE (id))
10158             {
10159               if (type)
10160                 error ("multiple declarations `%T' and `%T'", type, id);
10161               else
10162                 type = IDENTIFIER_TYPE_VALUE (id);
10163               goto found;
10164             }
10165
10166           for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10167             {
10168               if (ridpointers[i] == id)
10169                 {
10170                   if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10171                     {
10172                       if (pedantic && ! in_system_header && warn_long_long)
10173                         pedwarn ("ISO C++ does not support `long long'");
10174                       if (longlong)
10175                         error ("`long long long' is too long for GCC");
10176                       else
10177                         longlong = 1;
10178                     }
10179                   else if (RIDBIT_SETP (i, specbits))
10180                     pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10181
10182                   /* Diagnose "__thread extern" or "__thread static".  */
10183                   if (RIDBIT_SETP (RID_THREAD, specbits))
10184                     {
10185                       if (i == (int)RID_EXTERN)
10186                         error ("`__thread' before `extern'");
10187                       else if (i == (int)RID_STATIC)
10188                         error ("`__thread' before `static'");
10189                     }
10190
10191                   if (i == (int)RID_EXTERN
10192                       && TREE_PURPOSE (spec) == error_mark_node)
10193                     /* This extern was part of a language linkage.  */
10194                     extern_langp = 1;
10195
10196                   RIDBIT_SET (i, specbits);
10197                   goto found;
10198                 }
10199             }
10200         }
10201       else if (TREE_CODE (id) == TYPE_DECL)
10202         {
10203           if (type)
10204             error ("multiple declarations `%T' and `%T'", type,
10205                       TREE_TYPE (id));
10206           else
10207             {
10208               type = TREE_TYPE (id);
10209               TREE_VALUE (spec) = type;
10210               typedef_decl = id;
10211             }
10212           goto found;
10213         }
10214       if (type)
10215         error ("two or more data types in declaration of `%s'", name);
10216       else if (TREE_CODE (id) == IDENTIFIER_NODE)
10217         {
10218           register tree t = lookup_name (id, 1);
10219           if (!t || TREE_CODE (t) != TYPE_DECL)
10220             error ("`%s' fails to be a typedef or built in type",
10221                    IDENTIFIER_POINTER (id));
10222           else
10223             {
10224               type = TREE_TYPE (t);
10225               typedef_decl = t;
10226             }
10227         }
10228       else if (id != error_mark_node)
10229         /* Can't change CLASS nodes into RECORD nodes here!  */
10230         type = id;
10231
10232     found: ;
10233     }
10234
10235 #if 0
10236   /* See the code below that used this.  */
10237   if (typedef_decl)
10238     decl_attr = DECL_ATTRIBUTES (typedef_decl);
10239 #endif
10240   typedef_type = type;
10241
10242   /* No type at all: default to `int', and set DEFAULTED_INT
10243      because it was not a user-defined typedef.  */
10244
10245   if (type == NULL_TREE
10246       && (RIDBIT_SETP (RID_SIGNED, specbits)
10247           || RIDBIT_SETP (RID_UNSIGNED, specbits)
10248           || RIDBIT_SETP (RID_LONG, specbits)
10249           || RIDBIT_SETP (RID_SHORT, specbits)))
10250     {
10251       /* These imply 'int'.  */
10252       type = integer_type_node;
10253       defaulted_int = 1;
10254     }
10255
10256   if (sfk != sfk_none)
10257     type = check_special_function_return_type (sfk, type,
10258                                                ctor_return_type);
10259   else if (type == NULL_TREE)
10260     {
10261       int is_main;
10262
10263       explicit_int = -1;
10264
10265       /* We handle `main' specially here, because 'main () { }' is so
10266          common.  With no options, it is allowed.  With -Wreturn-type,
10267          it is a warning.  It is only an error with -pedantic-errors.  */
10268       is_main = (funcdef_flag
10269                  && dname && MAIN_NAME_P (dname)
10270                  && ctype == NULL_TREE
10271                  && in_namespace == NULL_TREE
10272                  && current_namespace == global_namespace);
10273
10274       if (in_system_header || flag_ms_extensions)
10275         /* Allow it, sigh.  */;
10276       else if (pedantic || ! is_main)
10277         pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10278                     name);
10279       else if (warn_return_type)
10280         warning ("ISO C++ forbids declaration of `%s' with no type",
10281                     name);
10282
10283       type = integer_type_node;
10284     }
10285   
10286   ctype = NULL_TREE;
10287
10288   /* Now process the modifiers that were specified
10289      and check for invalid combinations.  */
10290
10291   /* Long double is a special combination.  */
10292
10293   if (RIDBIT_SETP (RID_LONG, specbits)
10294       && TYPE_MAIN_VARIANT (type) == double_type_node)
10295     {
10296       RIDBIT_RESET (RID_LONG, specbits);
10297       type = build_qualified_type (long_double_type_node,
10298                                    cp_type_quals (type));
10299     }
10300
10301   /* Check all other uses of type modifiers.  */
10302
10303   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10304       || RIDBIT_SETP (RID_SIGNED, specbits)
10305       || RIDBIT_SETP (RID_LONG, specbits)
10306       || RIDBIT_SETP (RID_SHORT, specbits))
10307     {
10308       int ok = 0;
10309
10310       if (TREE_CODE (type) == REAL_TYPE)
10311         error ("short, signed or unsigned invalid for `%s'", name);
10312       else if (TREE_CODE (type) != INTEGER_TYPE)
10313         error ("long, short, signed or unsigned invalid for `%s'", name);
10314       else if (RIDBIT_SETP (RID_LONG, specbits)
10315                && RIDBIT_SETP (RID_SHORT, specbits))
10316         error ("long and short specified together for `%s'", name);
10317       else if ((RIDBIT_SETP (RID_LONG, specbits)
10318                 || RIDBIT_SETP (RID_SHORT, specbits))
10319                && explicit_char)
10320         error ("long or short specified with char for `%s'", name);
10321       else if ((RIDBIT_SETP (RID_LONG, specbits)
10322                 || RIDBIT_SETP (RID_SHORT, specbits))
10323                && TREE_CODE (type) == REAL_TYPE)
10324         error ("long or short specified with floating type for `%s'", name);
10325       else if (RIDBIT_SETP (RID_SIGNED, specbits)
10326                && RIDBIT_SETP (RID_UNSIGNED, specbits))
10327         error ("signed and unsigned given together for `%s'", name);
10328       else
10329         {
10330           ok = 1;
10331           if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10332             {
10333               pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10334                        name);
10335               if (flag_pedantic_errors)
10336                 ok = 0;
10337             }
10338         }
10339
10340       /* Discard the type modifiers if they are invalid.  */
10341       if (! ok)
10342         {
10343           RIDBIT_RESET (RID_UNSIGNED, specbits);
10344           RIDBIT_RESET (RID_SIGNED, specbits);
10345           RIDBIT_RESET (RID_LONG, specbits);
10346           RIDBIT_RESET (RID_SHORT, specbits);
10347           longlong = 0;
10348         }
10349     }
10350
10351   if (RIDBIT_SETP (RID_COMPLEX, specbits)
10352       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10353     {
10354       error ("complex invalid for `%s'", name);
10355       RIDBIT_RESET (RID_COMPLEX, specbits);
10356     }
10357
10358   /* Decide whether an integer type is signed or not.
10359      Optionally treat bitfields as signed by default.  */
10360   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10361       /* [class.bit]
10362
10363          It is implementation-defined whether a plain (neither
10364          explicitly signed or unsigned) char, short, int, or long
10365          bit-field is signed or unsigned.
10366
10367          Naturally, we extend this to long long as well.  Note that
10368          this does not include wchar_t.  */
10369       || (bitfield && !flag_signed_bitfields
10370           && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10371           /* A typedef for plain `int' without `signed' can be
10372              controlled just like plain `int', but a typedef for
10373              `signed int' cannot be so controlled.  */
10374           && !(typedef_decl
10375                && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10376           && (TREE_CODE (type) == INTEGER_TYPE
10377               || TREE_CODE (type) == CHAR_TYPE)
10378           && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10379     {
10380       if (longlong)
10381         type = long_long_unsigned_type_node;
10382       else if (RIDBIT_SETP (RID_LONG, specbits))
10383         type = long_unsigned_type_node;
10384       else if (RIDBIT_SETP (RID_SHORT, specbits))
10385         type = short_unsigned_type_node;
10386       else if (type == char_type_node)
10387         type = unsigned_char_type_node;
10388       else if (typedef_decl)
10389         type = c_common_unsigned_type (type);
10390       else
10391         type = unsigned_type_node;
10392     }
10393   else if (RIDBIT_SETP (RID_SIGNED, specbits)
10394            && type == char_type_node)
10395     type = signed_char_type_node;
10396   else if (longlong)
10397     type = long_long_integer_type_node;
10398   else if (RIDBIT_SETP (RID_LONG, specbits))
10399     type = long_integer_type_node;
10400   else if (RIDBIT_SETP (RID_SHORT, specbits))
10401     type = short_integer_type_node;
10402
10403   if (RIDBIT_SETP (RID_COMPLEX, specbits))
10404     {
10405       /* If we just have "complex", it is equivalent to
10406          "complex double", but if any modifiers at all are specified it is
10407          the complex form of TYPE.  E.g, "complex short" is
10408          "complex short int".  */
10409
10410       if (defaulted_int && ! longlong
10411           && ! (RIDBIT_SETP (RID_LONG, specbits)
10412                 || RIDBIT_SETP (RID_SHORT, specbits)
10413                 || RIDBIT_SETP (RID_SIGNED, specbits)
10414                 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10415         type = complex_double_type_node;
10416       else if (type == integer_type_node)
10417         type = complex_integer_type_node;
10418       else if (type == float_type_node)
10419         type = complex_float_type_node;
10420       else if (type == double_type_node)
10421         type = complex_double_type_node;
10422       else if (type == long_double_type_node)
10423         type = complex_long_double_type_node;
10424       else
10425         type = build_complex_type (type);
10426     }
10427
10428   type_quals = TYPE_UNQUALIFIED;
10429   if (RIDBIT_SETP (RID_CONST, specbits))
10430     type_quals |= TYPE_QUAL_CONST;
10431   if (RIDBIT_SETP (RID_VOLATILE, specbits))
10432     type_quals |= TYPE_QUAL_VOLATILE;
10433   if (RIDBIT_SETP (RID_RESTRICT, specbits))
10434     type_quals |= TYPE_QUAL_RESTRICT;
10435   if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10436     error ("qualifiers are not allowed on declaration of `operator %T'",
10437               ctor_return_type);
10438
10439   type_quals |= cp_type_quals (type);
10440   type = cp_build_qualified_type_real
10441     (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10442                          ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10443   /* We might have ignored or rejected some of the qualifiers.  */
10444   type_quals = cp_type_quals (type);
10445   
10446   staticp = 0;
10447   inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10448   virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10449   RIDBIT_RESET (RID_VIRTUAL, specbits);
10450   explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10451   RIDBIT_RESET (RID_EXPLICIT, specbits);
10452
10453   if (RIDBIT_SETP (RID_STATIC, specbits))
10454     staticp = 1 + (decl_context == FIELD);
10455
10456   if (virtualp && staticp == 2)
10457     {
10458       error ("member `%D' cannot be declared both virtual and static",
10459                 dname);
10460       staticp = 0;
10461     }
10462   friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10463   RIDBIT_RESET (RID_FRIEND, specbits);
10464
10465   if (dependant_name && !friendp)
10466     {
10467       error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10468       return void_type_node;
10469     }
10470   
10471   /* Warn if two storage classes are given. Default to `auto'.  */
10472
10473   if (RIDBIT_ANY_SET (specbits))
10474     {
10475       if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10476       if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10477       if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10478       if (decl_context == PARM && nclasses > 0)
10479         error ("storage class specifiers invalid in parameter declarations");
10480       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10481         {
10482           if (decl_context == PARM)
10483             error ("typedef declaration invalid in parameter declaration");
10484           nclasses++;
10485         }
10486       if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10487       if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10488       if (!nclasses && !friendp && extern_langp)
10489         nclasses++;
10490     }
10491
10492   /* Give error if `virtual' is used outside of class declaration.  */
10493   if (virtualp
10494       && (current_class_name == NULL_TREE || decl_context != FIELD))
10495     {
10496       error ("virtual outside class declaration");
10497       virtualp = 0;
10498     }
10499
10500   /* Static anonymous unions are dealt with here.  */
10501   if (staticp && decl_context == TYPENAME
10502       && TREE_CODE (declspecs) == TREE_LIST
10503       && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10504     decl_context = FIELD;
10505
10506   /* Warn about storage classes that are invalid for certain
10507      kinds of declarations (parameters, typenames, etc.).  */
10508
10509   /* "static __thread" and "extern __thread" are allowed.  */
10510   if (nclasses == 2
10511       && RIDBIT_SETP (RID_THREAD, specbits)
10512       && (RIDBIT_SETP (RID_EXTERN, specbits)
10513           || RIDBIT_SETP (RID_STATIC, specbits)))
10514     nclasses = 1;
10515     
10516   if (nclasses > 1)
10517     error ("multiple storage classes in declaration of `%s'", name);
10518   else if (decl_context != NORMAL && nclasses > 0)
10519     {
10520       if ((decl_context == PARM || decl_context == CATCHPARM)
10521           && (RIDBIT_SETP (RID_REGISTER, specbits)
10522               || RIDBIT_SETP (RID_AUTO, specbits)))
10523         ;
10524       else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10525         ;
10526       else if (decl_context == FIELD
10527                /* C++ allows static class elements  */
10528                && RIDBIT_SETP (RID_STATIC, specbits))
10529         /* C++ also allows inlines and signed and unsigned elements,
10530            but in those cases we don't come in here.  */
10531         ;
10532       else
10533         {
10534           if (decl_context == FIELD)
10535             {
10536               tree tmp = NULL_TREE;
10537               register int op = 0;
10538
10539               if (declarator)
10540                 {
10541                   /* Avoid trying to get an operand off an identifier node.  */
10542                   if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10543                     tmp = declarator;
10544                   else
10545                     tmp = TREE_OPERAND (declarator, 0);
10546                   op = IDENTIFIER_OPNAME_P (tmp);
10547                   if (IDENTIFIER_TYPENAME_P (tmp))
10548                     {
10549                       if (IDENTIFIER_GLOBAL_VALUE (tmp)
10550                           && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10551                               == TYPE_DECL))
10552                         name = IDENTIFIER_POINTER (tmp);
10553                       else
10554                         name = "<invalid operator>";
10555                     }
10556                 }
10557               error ("storage class specified for %s `%s'",
10558                      op ? "member operator" : "field",
10559                      name);
10560             }
10561           else
10562             {
10563               if (decl_context == PARM || decl_context == CATCHPARM)
10564                 error ("storage class specified for parameter `%s'", name);
10565               else
10566                 error ("storage class specified for typename");
10567             }
10568           RIDBIT_RESET (RID_REGISTER, specbits);
10569           RIDBIT_RESET (RID_AUTO, specbits);
10570           RIDBIT_RESET (RID_EXTERN, specbits);
10571           RIDBIT_RESET (RID_THREAD, specbits);
10572         }
10573     }
10574   else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10575     {
10576       if (toplevel_bindings_p ())
10577         {
10578           /* It's common practice (and completely valid) to have a const
10579              be initialized and declared extern.  */
10580           if (!(type_quals & TYPE_QUAL_CONST))
10581             warning ("`%s' initialized and declared `extern'", name);
10582         }
10583       else
10584         error ("`%s' has both `extern' and initializer", name);
10585     }
10586   else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10587            && ! toplevel_bindings_p ())
10588     error ("nested function `%s' declared `extern'", name);
10589   else if (toplevel_bindings_p ())
10590     {
10591       if (RIDBIT_SETP (RID_AUTO, specbits))
10592         error ("top-level declaration of `%s' specifies `auto'", name);
10593     }
10594   else if (RIDBIT_SETP (RID_THREAD, specbits)
10595            && !RIDBIT_SETP (RID_EXTERN, specbits)
10596            && !RIDBIT_SETP (RID_STATIC, specbits))
10597     {
10598       error ("function-scope `%s' implicitly auto and declared `__thread'",
10599              name);
10600       RIDBIT_RESET (RID_THREAD, specbits);
10601     }
10602
10603   if (nclasses > 0 && friendp)
10604     error ("storage class specifiers invalid in friend function declarations");
10605
10606   scope = get_scope_of_declarator (declarator);
10607
10608   /* Now figure out the structure of the declarator proper.
10609      Descend through it, creating more complex types, until we reach
10610      the declared identifier (or NULL_TREE, in an abstract declarator).  */
10611
10612   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10613          && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10614     {
10615       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10616          an INDIRECT_REF (for *...),
10617          a CALL_EXPR (for ...(...)),
10618          an identifier (for the name being declared)
10619          or a null pointer (for the place in an absolute declarator
10620          where the name was omitted).
10621          For the last two cases, we have just exited the loop.
10622
10623          For C++ it could also be
10624          a SCOPE_REF (for class :: ...).  In this case, we have converted
10625          sensible names to types, and those are the values we use to
10626          qualify the member name.
10627          an ADDR_EXPR (for &...),
10628          a BIT_NOT_EXPR (for destructors)
10629
10630          At this point, TYPE is the type of elements of an array,
10631          or for a function to return, or for a pointer to point to.
10632          After this sequence of ifs, TYPE is the type of the
10633          array or function or pointer, and DECLARATOR has had its
10634          outermost layer removed.  */
10635
10636       if (type == error_mark_node)
10637         {
10638           if (TREE_CODE (declarator) == SCOPE_REF)
10639             declarator = TREE_OPERAND (declarator, 1);
10640           else
10641             declarator = TREE_OPERAND (declarator, 0);
10642           continue;
10643         }
10644       if (quals != NULL_TREE
10645           && (declarator == NULL_TREE
10646               || TREE_CODE (declarator) != SCOPE_REF))
10647         {
10648           if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10649             ctype = TYPE_METHOD_BASETYPE (type);
10650           if (ctype != NULL_TREE)
10651             {
10652               tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10653               grok_method_quals (ctype, dummy, quals);
10654               type = TREE_TYPE (dummy);
10655               quals = NULL_TREE;
10656             }
10657         }
10658
10659       switch (TREE_CODE (declarator))
10660         {
10661         case TREE_LIST:
10662           {
10663             /* We encode a declarator with embedded attributes using
10664                a TREE_LIST.  */
10665             tree attrs = TREE_PURPOSE (declarator);
10666             tree inner_decl;
10667             int attr_flags;
10668
10669             declarator = TREE_VALUE (declarator);
10670             inner_decl = declarator;
10671             while (inner_decl != NULL_TREE
10672                    && TREE_CODE (inner_decl) == TREE_LIST)
10673               inner_decl = TREE_VALUE (inner_decl);
10674             attr_flags = 0;
10675             if (inner_decl == NULL_TREE
10676                 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10677               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10678             if (TREE_CODE (inner_decl) == CALL_EXPR)
10679               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10680             if (TREE_CODE (inner_decl) == ARRAY_REF)
10681               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10682             returned_attrs = decl_attributes (&type,
10683                                               chainon (returned_attrs, attrs),
10684                                               attr_flags);
10685           }
10686           break;
10687
10688         case ARRAY_REF:
10689           {
10690             register tree size;
10691
10692             size = TREE_OPERAND (declarator, 1);
10693
10694             /* VC++ spells a zero-sized array with [].  */
10695             if (size == NULL_TREE && decl_context == FIELD && ! staticp
10696                 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10697               size = integer_zero_node;
10698
10699             declarator = TREE_OPERAND (declarator, 0);
10700
10701             type = create_array_type_for_decl (dname, type, size);
10702
10703             ctype = NULL_TREE;
10704           }
10705           break;
10706
10707         case CALL_EXPR:
10708           {
10709             tree arg_types;
10710             int funcdecl_p;
10711             tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10712             tree inner_decl = TREE_OPERAND (declarator, 0);
10713
10714             /* Declaring a function type.
10715                Make sure we have a valid type for the function to return.  */
10716
10717             /* We now know that the TYPE_QUALS don't apply to the
10718                decl, but to its return type.  */
10719             type_quals = TYPE_UNQUALIFIED;
10720
10721             /* Warn about some types functions can't return.  */
10722
10723             if (TREE_CODE (type) == FUNCTION_TYPE)
10724               {
10725                 error ("`%s' declared as function returning a function", name);
10726                 type = integer_type_node;
10727               }
10728             if (TREE_CODE (type) == ARRAY_TYPE)
10729               {
10730                 error ("`%s' declared as function returning an array", name);
10731                 type = integer_type_node;
10732               }
10733
10734             if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10735               inner_decl = TREE_OPERAND (inner_decl, 1);
10736
10737             if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10738               inner_decl = dname;
10739
10740             /* Pick up type qualifiers which should be applied to `this'.  */
10741             quals = CALL_DECLARATOR_QUALS (declarator);
10742
10743             /* Pick up the exception specifications.  */
10744             raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10745
10746             /* Say it's a definition only for the CALL_EXPR
10747                closest to the identifier.  */
10748             funcdecl_p
10749               = inner_decl
10750               && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10751                   || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10752                   || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10753
10754             if (ctype == NULL_TREE
10755                 && decl_context == FIELD
10756                 && funcdecl_p
10757                 && (friendp == 0 || dname == current_class_name))
10758               ctype = current_class_type;
10759
10760             if (ctype && sfk == sfk_conversion)
10761               TYPE_HAS_CONVERSION (ctype) = 1;
10762             if (ctype && constructor_name_p (dname, ctype))
10763               {
10764                 /* We are within a class's scope. If our declarator name
10765                    is the same as the class name, and we are defining
10766                    a function, then it is a constructor/destructor, and
10767                    therefore returns a void type.  */
10768
10769                 if (flags == DTOR_FLAG)
10770                   {
10771                     /* ISO C++ 12.4/2.  A destructor may not be
10772                        declared const or volatile.  A destructor may
10773                        not be static.  */
10774                     if (staticp == 2)
10775                       error ("destructor cannot be static member function");
10776                     if (quals)
10777                       {
10778                         error ("destructors may not be `%s'",
10779                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10780                         quals = NULL_TREE;
10781                       }
10782                     if (decl_context == FIELD)
10783                       {
10784                         if (! member_function_or_else (ctype,
10785                                                        current_class_type,
10786                                                        flags))
10787                           return void_type_node;
10788                       }
10789                   }
10790                 else            /* It's a constructor.  */
10791                   {
10792                     if (explicitp == 1)
10793                       explicitp = 2;
10794                     /* ISO C++ 12.1.  A constructor may not be
10795                        declared const or volatile.  A constructor may
10796                        not be virtual.  A constructor may not be
10797                        static.  */
10798                     if (staticp == 2)
10799                       error ("constructor cannot be static member function");
10800                     if (virtualp)
10801                       {
10802                         pedwarn ("constructors cannot be declared virtual");
10803                         virtualp = 0;
10804                       }
10805                     if (quals)
10806                       {
10807                         error ("constructors may not be `%s'",
10808                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10809                         quals = NULL_TREE;
10810                       }
10811                     {
10812                       RID_BIT_TYPE tmp_bits;
10813                       memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10814                       RIDBIT_RESET (RID_INLINE, tmp_bits);
10815                       RIDBIT_RESET (RID_STATIC, tmp_bits);
10816                       if (RIDBIT_ANY_SET (tmp_bits))
10817                         error ("return value type specifier for constructor ignored");
10818                     }
10819                     if (decl_context == FIELD)
10820                       {
10821                         if (! member_function_or_else (ctype,
10822                                                        current_class_type,
10823                                                        flags))
10824                           return void_type_node;
10825                         TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10826                         if (sfk != sfk_constructor)
10827                           return NULL_TREE;
10828                       }
10829                   }
10830                 if (decl_context == FIELD)
10831                   staticp = 0;
10832               }
10833             else if (friendp)
10834               {
10835                 if (initialized)
10836                   error ("can't initialize friend function `%s'", name);
10837                 if (virtualp)
10838                   {
10839                     /* Cannot be both friend and virtual.  */
10840                     error ("virtual functions cannot be friends");
10841                     RIDBIT_RESET (RID_FRIEND, specbits);
10842                     friendp = 0;
10843                   }
10844                 if (decl_context == NORMAL)
10845                   error ("friend declaration not in class definition");
10846                 if (current_function_decl && funcdef_flag)
10847                   error ("can't define friend function `%s' in a local class definition",
10848                             name);
10849               }
10850
10851             /* Construct the function type and go to the next
10852                inner layer of declarator.  */
10853
10854             declarator = TREE_OPERAND (declarator, 0);
10855
10856             /* FIXME: This is where default args should be fully
10857                processed.  */
10858
10859             arg_types = grokparms (inner_parms);
10860
10861             if (declarator && flags == DTOR_FLAG)
10862               {
10863                 /* A destructor declared in the body of a class will
10864                    be represented as a BIT_NOT_EXPR.  But, we just
10865                    want the underlying IDENTIFIER.  */
10866                 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10867                   declarator = TREE_OPERAND (declarator, 0);
10868
10869                 if (arg_types != void_list_node)
10870                   {
10871                     error ("destructors may not have parameters");
10872                     arg_types = void_list_node;
10873                     last_function_parms = NULL_TREE;
10874                   }
10875               }
10876
10877             /* ANSI says that `const int foo ();'
10878                does not make the function foo const.  */
10879             type = build_function_type (type, arg_types);
10880           }
10881           break;
10882
10883         case ADDR_EXPR:
10884         case INDIRECT_REF:
10885           /* Filter out pointers-to-references and references-to-references.
10886              We can get these if a TYPE_DECL is used.  */
10887
10888           if (TREE_CODE (type) == REFERENCE_TYPE)
10889             {
10890               error (TREE_CODE (declarator) == ADDR_EXPR
10891                      ? "cannot declare reference to `%#T'"
10892                      : "cannot declare pointer to `%#T'", type);
10893               type = TREE_TYPE (type);
10894             }
10895           else if (VOID_TYPE_P (type)
10896                    && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10897             error (ctype ? "cannot declare pointer to `%#T' member"
10898                      : "cannot declare reference to `%#T'", type);
10899
10900           /* Merge any constancy or volatility into the target type
10901              for the pointer.  */
10902
10903           /* We now know that the TYPE_QUALS don't apply to the decl,
10904              but to the target of the pointer.  */
10905           type_quals = TYPE_UNQUALIFIED;
10906
10907           if (TREE_CODE (declarator) == ADDR_EXPR)
10908             {
10909               if (!VOID_TYPE_P (type))
10910                 type = build_reference_type (type);
10911             }
10912           else if (TREE_CODE (type) == METHOD_TYPE)
10913             type = build_ptrmemfunc_type (build_pointer_type (type));
10914           else if (ctype)
10915             type = build_ptrmem_type (ctype, type);
10916           else
10917             type = build_pointer_type (type);
10918
10919           /* Process a list of type modifier keywords (such as
10920              const or volatile) that were given inside the `*' or `&'.  */
10921
10922           if (TREE_TYPE (declarator))
10923             {
10924               register tree typemodlist;
10925               int erred = 0;
10926               int constp = 0;
10927               int volatilep = 0;
10928               int restrictp = 0;
10929               
10930               for (typemodlist = TREE_TYPE (declarator); typemodlist;
10931                    typemodlist = TREE_CHAIN (typemodlist))
10932                 {
10933                   tree qualifier = TREE_VALUE (typemodlist);
10934
10935                   if (qualifier == ridpointers[(int) RID_CONST])
10936                     {
10937                       constp++;
10938                       type_quals |= TYPE_QUAL_CONST;
10939                     }
10940                   else if (qualifier == ridpointers[(int) RID_VOLATILE])
10941                     {
10942                       volatilep++;
10943                       type_quals |= TYPE_QUAL_VOLATILE;
10944                     }
10945                   else if (qualifier == ridpointers[(int) RID_RESTRICT])
10946                     {
10947                       restrictp++;
10948                       type_quals |= TYPE_QUAL_RESTRICT;
10949                     }
10950                   else if (!erred)
10951                     {
10952                       erred = 1;
10953                       error ("invalid type modifier within pointer declarator");
10954                     }
10955                 }
10956               if (constp > 1)
10957                 pedwarn ("duplicate `const'");
10958               if (volatilep > 1)
10959                 pedwarn ("duplicate `volatile'");
10960               if (restrictp > 1)
10961                 pedwarn ("duplicate `restrict'");
10962               type = cp_build_qualified_type (type, type_quals);
10963               type_quals = cp_type_quals (type);
10964             }
10965           declarator = TREE_OPERAND (declarator, 0);
10966           ctype = NULL_TREE;
10967           break;
10968
10969         case SCOPE_REF:
10970           {
10971             /* We have converted type names to NULL_TREE if the
10972                name was bogus, or to a _TYPE node, if not.
10973
10974                The variable CTYPE holds the type we will ultimately
10975                resolve to.  The code here just needs to build
10976                up appropriate member types.  */
10977             tree sname = TREE_OPERAND (declarator, 1);
10978             tree t;
10979
10980             /* Destructors can have their visibilities changed as well.  */
10981             if (TREE_CODE (sname) == BIT_NOT_EXPR)
10982               sname = TREE_OPERAND (sname, 0);
10983
10984             if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10985               {
10986                 /* We had a reference to a global decl, or
10987                    perhaps we were given a non-aggregate typedef,
10988                    in which case we cleared this out, and should just
10989                    keep going as though it wasn't there.  */
10990                 declarator = sname;
10991                 continue;
10992               }
10993             ctype = TREE_OPERAND (declarator, 0);
10994
10995             t = ctype;
10996             while (t != NULL_TREE && CLASS_TYPE_P (t))
10997               {
10998                 /* You're supposed to have one `template <...>'
10999                    for every template class, but you don't need one
11000                    for a full specialization.  For example:
11001
11002                      template <class T> struct S{};
11003                      template <> struct S<int> { void f(); };
11004                      void S<int>::f () {}
11005
11006                    is correct; there shouldn't be a `template <>' for
11007                    the definition of `S<int>::f'.  */
11008                 if (CLASSTYPE_TEMPLATE_INFO (t)
11009                     && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
11010                         || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
11011                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
11012                   template_count += 1;
11013
11014                 t = TYPE_MAIN_DECL (t);
11015                 t = DECL_CONTEXT (t);
11016               }
11017
11018             if (sname == NULL_TREE)
11019               goto done_scoping;
11020
11021             if (TREE_CODE (sname) == IDENTIFIER_NODE)
11022               {
11023                 /* This is the `standard' use of the scoping operator:
11024                    basetype :: member .  */
11025
11026                 if (ctype == current_class_type)
11027                   {
11028                     /* class A {
11029                          void A::f ();
11030                        };
11031
11032                        Is this ill-formed?  */
11033
11034                     if (pedantic)
11035                       pedwarn ("extra qualification `%T::' on member `%s' ignored",
11036                                   ctype, name);
11037                   }
11038                 else if (TREE_CODE (type) == FUNCTION_TYPE)
11039                   {
11040                     if (current_class_type == NULL_TREE || friendp)
11041                       type = build_cplus_method_type (ctype, TREE_TYPE (type),
11042                                                       TYPE_ARG_TYPES (type));
11043                     else
11044                       {
11045                         error ("cannot declare member function `%T::%s' within `%T'",
11046                                   ctype, name, current_class_type);
11047                         return error_mark_node;
11048                       }
11049                   }
11050                 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
11051                          || COMPLETE_TYPE_P (complete_type (ctype)))
11052                   {
11053                     /* Have to move this code elsewhere in this function.
11054                        this code is used for i.e., typedef int A::M; M *pm;
11055
11056                        It is?  How? jason 10/2/94 */
11057
11058                     if (current_class_type)
11059                       {
11060                         error ("cannot declare member `%T::%s' within `%T'",
11061                                   ctype, name, current_class_type);
11062                         return void_type_node;
11063                       }
11064                   }
11065                 else
11066                   {
11067                     cxx_incomplete_type_error (NULL_TREE, ctype);
11068                     return error_mark_node;
11069                   }
11070
11071                 declarator = sname;
11072               }
11073             else if (TREE_CODE (sname) == SCOPE_REF)
11074               abort ();
11075             else
11076               {
11077               done_scoping:
11078                 declarator = TREE_OPERAND (declarator, 1);
11079                 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
11080                   /* In this case, we will deal with it later.  */
11081                   ;
11082                 else if (TREE_CODE (type) == FUNCTION_TYPE)
11083                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11084                                                   TYPE_ARG_TYPES (type));
11085               }
11086           }
11087           break;
11088
11089         case BIT_NOT_EXPR:
11090           declarator = TREE_OPERAND (declarator, 0);
11091           break;
11092
11093         case BASELINK:
11094           declarator = BASELINK_FUNCTIONS (declarator);
11095           break;
11096
11097         case RECORD_TYPE:
11098         case UNION_TYPE:
11099         case ENUMERAL_TYPE:
11100           declarator = NULL_TREE;
11101           break;
11102
11103         case ERROR_MARK:
11104           declarator = NULL_TREE;
11105           break;
11106
11107         default:
11108           abort ();
11109         }
11110     }
11111
11112   if (returned_attrs)
11113     {
11114       if (attrlist)
11115         *attrlist = chainon (returned_attrs, *attrlist);
11116       else
11117         attrlist = &returned_attrs;
11118     }
11119
11120   /* Now TYPE has the actual type.  */
11121
11122   /* Did array size calculations overflow?  */
11123
11124   if (TREE_CODE (type) == ARRAY_TYPE
11125       && COMPLETE_TYPE_P (type)
11126       && TREE_OVERFLOW (TYPE_SIZE (type)))
11127     {
11128       error ("size of array `%s' is too large", name);
11129       /* If we proceed with the array type as it is, we'll eventually
11130          crash in tree_low_cst().  */
11131       type = error_mark_node;
11132     }
11133
11134   if ((decl_context == FIELD || decl_context == PARM)
11135       && !processing_template_decl 
11136       && variably_modified_type_p (type))
11137     {
11138       if (decl_context == FIELD)
11139         error ("data member may not have variably modified type `%T'", type);
11140       else
11141         error ("parameter may not have variably modified type `%T'", type);
11142       type = error_mark_node;
11143     }
11144
11145   if (explicitp == 1 || (explicitp && friendp))
11146     {
11147       /* [dcl.fct.spec] The explicit specifier shall only be used in
11148          declarations of constructors within a class definition.  */
11149       error ("only declarations of constructors can be `explicit'");
11150       explicitp = 0;
11151     }
11152
11153   if (RIDBIT_SETP (RID_MUTABLE, specbits))
11154     {
11155       if (current_class_name == NULL_TREE || decl_context == PARM || friendp)
11156         {
11157           error ("non-member `%s' cannot be declared `mutable'", name);
11158           RIDBIT_RESET (RID_MUTABLE, specbits);
11159         }
11160       else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11161         {
11162           error ("non-object member `%s' cannot be declared `mutable'", name);
11163           RIDBIT_RESET (RID_MUTABLE, specbits);
11164         }
11165       else if (TREE_CODE (type) == FUNCTION_TYPE
11166                || TREE_CODE (type) == METHOD_TYPE)
11167         {
11168           error ("function `%s' cannot be declared `mutable'", name);
11169           RIDBIT_RESET (RID_MUTABLE, specbits);
11170         }
11171       else if (staticp)
11172         {
11173           error ("static `%s' cannot be declared `mutable'", name);
11174           RIDBIT_RESET (RID_MUTABLE, specbits);
11175         }
11176       else if (type_quals & TYPE_QUAL_CONST)
11177         {
11178           error ("const `%s' cannot be declared `mutable'", name);
11179           RIDBIT_RESET (RID_MUTABLE, specbits);
11180         }
11181     }
11182
11183   if (declarator == NULL_TREE
11184       || TREE_CODE (declarator) == IDENTIFIER_NODE
11185       || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11186           && (TREE_CODE (type) == FUNCTION_TYPE
11187               || TREE_CODE (type) == METHOD_TYPE)))
11188     /* OK */;
11189   else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11190     {
11191       error ("template-id `%D' used as a declarator", declarator);
11192       declarator = dname;
11193     }
11194   else
11195     /* Unexpected declarator format.  */
11196     abort ();
11197
11198   /* If this is declaring a typedef name, return a TYPE_DECL.  */
11199
11200   if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11201     {
11202       tree decl;
11203
11204       /* Note that the grammar rejects storage classes
11205          in typenames, fields or parameters.  */
11206       if (current_lang_name == lang_name_java)
11207         TYPE_FOR_JAVA (type) = 1;
11208
11209       if (decl_context == FIELD)
11210         {
11211           if (constructor_name_p (declarator, current_class_type))
11212             pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11213                         declarator);
11214           decl = build_lang_decl (TYPE_DECL, declarator, type);
11215         }
11216       else
11217         {
11218           decl = build_decl (TYPE_DECL, declarator, type);
11219           if (in_namespace || ctype)
11220             cp_error_at ("typedef name may not be a nested-name-specifier",
11221                          decl);
11222           if (!current_function_decl)
11223             DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11224         }
11225       
11226       /* If the user declares "typedef struct {...} foo" then the
11227          struct will have an anonymous name.  Fill that name in now.
11228          Nothing can refer to it, so nothing needs know about the name
11229          change.  */
11230       if (type != error_mark_node
11231           && declarator
11232           && TYPE_NAME (type)
11233           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11234           && TYPE_ANONYMOUS_P (type)
11235           /* Don't do this if there are attributes.  */
11236           && (!attrlist || !*attrlist)
11237           && cp_type_quals (type) == TYPE_UNQUALIFIED)
11238         {
11239           tree oldname = TYPE_NAME (type);
11240           tree t;
11241
11242           /* Replace the anonymous name with the real name everywhere.  */
11243           lookup_tag_reverse (type, declarator);
11244           for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11245             if (TYPE_NAME (t) == oldname)
11246               TYPE_NAME (t) = decl;
11247
11248           if (TYPE_LANG_SPECIFIC (type))
11249             TYPE_WAS_ANONYMOUS (type) = 1;
11250
11251           /* If this is a typedef within a template class, the nested
11252              type is a (non-primary) template.  The name for the
11253              template needs updating as well.  */
11254           if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11255             DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11256               = TYPE_IDENTIFIER (type);
11257
11258           /* FIXME remangle member functions; member functions of a
11259              type with external linkage have external linkage.  */
11260         }
11261
11262       if (quals)
11263         {
11264           if (ctype == NULL_TREE)
11265             {
11266               if (TREE_CODE (type) != METHOD_TYPE)
11267                 cp_error_at ("invalid type qualifier for non-member function type", decl);
11268               else
11269                 ctype = TYPE_METHOD_BASETYPE (type);
11270             }
11271           if (ctype != NULL_TREE)
11272             grok_method_quals (ctype, decl, quals);
11273         }
11274
11275       if (RIDBIT_SETP (RID_SIGNED, specbits)
11276           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11277         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11278
11279       bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11280                       inlinep, friendp, raises != NULL_TREE);
11281
11282       return decl;
11283     }
11284
11285   /* Detect the case of an array type of unspecified size
11286      which came, as such, direct from a typedef name.
11287      We must copy the type, so that the array's domain can be
11288      individually set by the object's initializer.  */
11289
11290   if (type && typedef_type
11291       && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11292       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11293     type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11294
11295   /* Detect where we're using a typedef of function type to declare a
11296      function. last_function_parms will not be set, so we must create
11297      it now.  */
11298   
11299   if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11300     {
11301       tree decls = NULL_TREE;
11302       tree args;
11303
11304       for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11305         {
11306           tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11307
11308           TREE_CHAIN (decl) = decls;
11309           decls = decl;
11310         }
11311       
11312       last_function_parms = nreverse (decls);
11313     }
11314
11315   /* If this is a type name (such as, in a cast or sizeof),
11316      compute the type and return it now.  */
11317
11318   if (decl_context == TYPENAME)
11319     {
11320       /* Note that the grammar rejects storage classes
11321          in typenames, fields or parameters.  */
11322       if (type_quals != TYPE_UNQUALIFIED)
11323         type_quals = TYPE_UNQUALIFIED;
11324
11325       /* Special case: "friend class foo" looks like a TYPENAME context.  */
11326       if (friendp)
11327         {
11328           if (type_quals != TYPE_UNQUALIFIED)
11329             {
11330               error ("type qualifiers specified for friend class declaration");
11331               type_quals = TYPE_UNQUALIFIED;
11332             }
11333           if (inlinep)
11334             {
11335               error ("`inline' specified for friend class declaration");
11336               inlinep = 0;
11337             }
11338
11339           if (!current_aggr)
11340             {
11341               /* Don't allow friend declaration without a class-key.  */
11342               if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11343                 pedwarn ("template parameters cannot be friends");
11344               else if (TREE_CODE (type) == TYPENAME_TYPE)
11345                 pedwarn ("friend declaration requires class-key, "
11346                          "i.e. `friend class %T::%D'",
11347                          TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11348               else
11349                 pedwarn ("friend declaration requires class-key, "
11350                          "i.e. `friend %#T'",
11351                          type);
11352             }
11353
11354           /* Only try to do this stuff if we didn't already give up.  */
11355           if (type != integer_type_node)
11356             {
11357               /* A friendly class?  */
11358               if (current_class_type)
11359                 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11360               else
11361                 error ("trying to make class `%T' a friend of global scope",
11362                           type);
11363
11364               type = void_type_node;
11365             }
11366         }
11367       else if (quals)
11368         {
11369           if (ctype == NULL_TREE)
11370             {
11371               if (TREE_CODE (type) != METHOD_TYPE)
11372                 error ("invalid qualifiers on non-member function type");
11373               else
11374                 ctype = TYPE_METHOD_BASETYPE (type);
11375             }
11376           if (ctype)
11377             {
11378               tree dummy = build_decl (TYPE_DECL, declarator, type);
11379               grok_method_quals (ctype, dummy, quals);
11380               type = TREE_TYPE (dummy);
11381             }
11382         }
11383
11384       return type;
11385     }
11386   else if (declarator == NULL_TREE && decl_context != PARM
11387            && decl_context != CATCHPARM
11388            && TREE_CODE (type) != UNION_TYPE
11389            && ! bitfield)
11390     {
11391       error ("abstract declarator `%T' used as declaration", type);
11392       declarator = make_anon_name ();
11393     }
11394
11395   /* `void' at top level (not within pointer)
11396      is allowed only in typedefs or type names.
11397      We don't complain about parms either, but that is because
11398      a better error message can be made later.  */
11399
11400   if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11401     {
11402       if (! declarator)
11403         error ("unnamed variable or field declared void");
11404       else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11405         {
11406           if (IDENTIFIER_OPNAME_P (declarator))
11407             abort ();
11408           else
11409             error ("variable or field `%s' declared void", name);
11410         }
11411       else
11412         error ("variable or field declared void");
11413       type = integer_type_node;
11414     }
11415
11416   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11417      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
11418
11419   if (decl_context == PARM || decl_context == CATCHPARM)
11420     {
11421       if (ctype || in_namespace)
11422         error ("cannot use `::' in parameter declaration");
11423
11424       /* A parameter declared as an array of T is really a pointer to T.
11425          One declared as a function is really a pointer to a function.
11426          One declared as a member is really a pointer to member.  */
11427
11428       if (TREE_CODE (type) == ARRAY_TYPE)
11429         {
11430           /* Transfer const-ness of array into that of type pointed to.  */
11431           type = build_pointer_type (TREE_TYPE (type));
11432           type_quals = TYPE_UNQUALIFIED;
11433         }
11434       else if (TREE_CODE (type) == FUNCTION_TYPE)
11435         type = build_pointer_type (type);
11436       else if (TREE_CODE (type) == OFFSET_TYPE)
11437         type = build_pointer_type (type);
11438     }
11439
11440   {
11441     register tree decl;
11442
11443     if (decl_context == PARM)
11444       {
11445         decl = cp_build_parm_decl (declarator, type);
11446
11447         bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11448                         inlinep, friendp, raises != NULL_TREE);
11449       }
11450     else if (decl_context == FIELD)
11451       {
11452         if (type == error_mark_node)
11453           {
11454             /* Happens when declaring arrays of sizes which
11455                are error_mark_node, for example.  */
11456             decl = NULL_TREE;
11457           }
11458         else if (in_namespace && !friendp)
11459           {
11460             /* Something like struct S { int N::j; };  */
11461             error ("invalid use of `::'");
11462             decl = NULL_TREE;
11463           }
11464         else if (TREE_CODE (type) == FUNCTION_TYPE)
11465           {
11466             int publicp = 0;
11467             tree function_context;
11468
11469             /* We catch the others as conflicts with the builtin
11470                typedefs.  */
11471             if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11472               {
11473                 error ("function `%D' cannot be declared friend",
11474                           declarator);
11475                 friendp = 0;
11476               }
11477
11478             if (friendp == 0)
11479               {
11480                 if (ctype == NULL_TREE)
11481                   ctype = current_class_type;
11482
11483                 if (ctype == NULL_TREE)
11484                   {
11485                     error ("can't make `%D' into a method -- not in a class",
11486                               declarator);
11487                     return void_type_node;
11488                   }
11489
11490                 /* ``A union may [ ... ] not [ have ] virtual functions.''
11491                    ARM 9.5 */
11492                 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11493                   {
11494                     error ("function `%D' declared virtual inside a union",
11495                               declarator);
11496                     return void_type_node;
11497                   }
11498
11499                 if (declarator == ansi_opname (NEW_EXPR)
11500                     || declarator == ansi_opname (VEC_NEW_EXPR)
11501                     || declarator == ansi_opname (DELETE_EXPR)
11502                     || declarator == ansi_opname (VEC_DELETE_EXPR))
11503                   {
11504                     if (virtualp)
11505                       {
11506                         error ("`%D' cannot be declared virtual, since it is always static",
11507                                   declarator);
11508                         virtualp = 0;
11509                       }
11510                   }
11511                 else if (staticp < 2)
11512                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11513                                                   TYPE_ARG_TYPES (type));
11514               }
11515
11516             /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
11517             function_context = (ctype != NULL_TREE) ?
11518               decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11519             publicp = (! friendp || ! staticp)
11520               && function_context == NULL_TREE;
11521             decl = grokfndecl (ctype, type,
11522                                TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11523                                ? declarator : dname,
11524                                declarator,
11525                                virtualp, flags, quals, raises,
11526                                friendp ? -1 : 0, friendp, publicp, inlinep,
11527                                funcdef_flag, template_count, in_namespace);
11528             if (decl == NULL_TREE)
11529               return decl;
11530 #if 0
11531             /* This clobbers the attrs stored in `decl' from `attrlist'.  */
11532             /* The decl and setting of decl_attr is also turned off.  */
11533             decl = build_decl_attribute_variant (decl, decl_attr);
11534 #endif
11535
11536             /* [class.conv.ctor]
11537
11538                A constructor declared without the function-specifier
11539                explicit that can be called with a single parameter
11540                specifies a conversion from the type of its first
11541                parameter to the type of its class.  Such a constructor
11542                is called a converting constructor.  */
11543             if (explicitp == 2)
11544               DECL_NONCONVERTING_P (decl) = 1;
11545             else if (DECL_CONSTRUCTOR_P (decl))
11546               {
11547                 /* The constructor can be called with exactly one
11548                    parameter if there is at least one parameter, and
11549                    any subsequent parameters have default arguments.
11550                    Ignore any compiler-added parms.  */
11551                 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11552
11553                 if (arg_types == void_list_node
11554                     || (arg_types
11555                         && TREE_CHAIN (arg_types)
11556                         && TREE_CHAIN (arg_types) != void_list_node
11557                         && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11558                   DECL_NONCONVERTING_P (decl) = 1;
11559               }
11560           }
11561         else if (TREE_CODE (type) == METHOD_TYPE)
11562           {
11563             /* We only get here for friend declarations of
11564                members of other classes.  */
11565             /* All method decls are public, so tell grokfndecl to set
11566                TREE_PUBLIC, also.  */
11567             decl = grokfndecl (ctype, type, declarator, declarator,
11568                                virtualp, flags, quals, raises,
11569                                friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11570                                template_count, in_namespace);
11571             if (decl == NULL_TREE)
11572               return NULL_TREE;
11573           }
11574         else if (!staticp && ! processing_template_decl
11575                  && !COMPLETE_TYPE_P (complete_type (type))
11576                  && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11577           {
11578             if (declarator)
11579               error ("field `%D' has incomplete type", declarator);
11580             else
11581               error ("name `%T' has incomplete type", type);
11582
11583             /* If we're instantiating a template, tell them which
11584                instantiation made the field's type be incomplete.  */
11585             if (current_class_type
11586                 && TYPE_NAME (current_class_type)
11587                 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11588                 && declspecs && TREE_VALUE (declspecs)
11589                 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11590               error ("  in instantiation of template `%T'",
11591                         current_class_type);
11592
11593             type = error_mark_node;
11594             decl = NULL_TREE;
11595           }
11596         else
11597           {
11598             if (friendp)
11599               {
11600                 error ("`%s' is neither function nor member function; cannot be declared friend",
11601                        IDENTIFIER_POINTER (declarator));
11602                 friendp = 0;
11603               }
11604             decl = NULL_TREE;
11605           }
11606
11607         if (friendp)
11608           {
11609             /* Friends are treated specially.  */
11610             if (ctype == current_class_type)
11611               warning ("member functions are implicitly friends of their class");
11612             else
11613               {
11614                 tree t = NULL_TREE;
11615                 if (decl && DECL_NAME (decl))
11616                   {
11617                     if (template_class_depth (current_class_type) == 0)
11618                       {
11619                         decl
11620                           = check_explicit_specialization
11621                           (declarator, decl,
11622                            template_count, 2 * (funcdef_flag != 0) + 4);
11623                         if (decl == error_mark_node)
11624                           return error_mark_node;
11625                       }
11626                     
11627                     t = do_friend (ctype, declarator, decl,
11628                                    last_function_parms, *attrlist,
11629                                    flags, quals, funcdef_flag);
11630                   }
11631                 if (t && funcdef_flag)
11632                   return t;
11633   
11634                 return void_type_node;
11635               }
11636           }
11637
11638         /* Structure field.  It may not be a function, except for C++ */
11639
11640         if (decl == NULL_TREE)
11641           {
11642             if (initialized)
11643               {
11644                 if (!staticp)
11645                   {
11646                     /* An attempt is being made to initialize a non-static
11647                        member.  But, from [class.mem]:
11648
11649                        4 A member-declarator can contain a
11650                        constant-initializer only if it declares a static
11651                        member (_class.static_) of integral or enumeration
11652                        type, see _class.static.data_.
11653
11654                        This used to be relatively common practice, but
11655                        the rest of the compiler does not correctly
11656                        handle the initialization unless the member is
11657                        static so we make it static below.  */
11658                     pedwarn ("ISO C++ forbids initialization of member `%D'",
11659                                 declarator);
11660                     pedwarn ("making `%D' static", declarator);
11661                     staticp = 1;
11662                   }
11663
11664                 if (uses_template_parms (type))
11665                   /* We'll check at instantiation time.  */
11666                   ;
11667                 else if (check_static_variable_definition (declarator,
11668                                                            type))
11669                   /* If we just return the declaration, crashes
11670                      will sometimes occur.  We therefore return
11671                      void_type_node, as if this was a friend
11672                      declaration, to cause callers to completely
11673                      ignore this declaration.  */
11674                   return void_type_node;
11675               }
11676
11677             /* 9.2p13 [class.mem] */
11678             if (constructor_name_p (declarator, current_class_type)
11679                 /* The standard does not allow non-static data members
11680                    here either, but we agreed at the 10/99 meeting
11681                    to change that in TC 1 so that they are allowed in
11682                    classes with no user-defined constructors.  */
11683                 && staticp)
11684               pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11685                           declarator);
11686
11687             if (staticp)
11688               {
11689                 /* C++ allows static class members.  All other work
11690                    for this is done by grokfield.  */
11691                 decl = build_lang_decl (VAR_DECL, declarator, type);
11692                 TREE_STATIC (decl) = 1;
11693                 /* In class context, 'static' means public access.  */
11694                 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11695               }
11696             else
11697               {
11698                 decl = build_decl (FIELD_DECL, declarator, type);
11699                 DECL_NONADDRESSABLE_P (decl) = bitfield;
11700                 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11701                   {
11702                     DECL_MUTABLE_P (decl) = 1;
11703                     RIDBIT_RESET (RID_MUTABLE, specbits);
11704                   }
11705               }
11706
11707             bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11708                             inlinep, friendp, raises != NULL_TREE);
11709           }
11710       }
11711     else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11712       {
11713         tree original_name;
11714         int publicp = 0;
11715
11716         if (! declarator)
11717           return NULL_TREE;
11718
11719         if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11720           original_name = dname;
11721         else
11722           original_name = declarator;
11723
11724         if (RIDBIT_SETP (RID_AUTO, specbits))
11725           error ("storage class `auto' invalid for function `%s'", name);
11726         else if (RIDBIT_SETP (RID_REGISTER, specbits))
11727           error ("storage class `register' invalid for function `%s'", name);
11728         else if (RIDBIT_SETP (RID_THREAD, specbits))
11729           error ("storage class `__thread' invalid for function `%s'", name);
11730
11731         /* Function declaration not at top level.
11732            Storage classes other than `extern' are not allowed
11733            and `extern' makes no difference.  */
11734         if (! toplevel_bindings_p ()
11735             && (RIDBIT_SETP (RID_STATIC, specbits)
11736                 || RIDBIT_SETP (RID_INLINE, specbits))
11737             && pedantic)
11738           {
11739             if (RIDBIT_SETP (RID_STATIC, specbits))
11740               pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11741             else
11742               pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11743           }
11744
11745         if (ctype == NULL_TREE)
11746           {
11747             if (virtualp)
11748               {
11749                 error ("virtual non-class function `%s'", name);
11750                 virtualp = 0;
11751               }
11752           }
11753         else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11754           type = build_cplus_method_type (ctype, TREE_TYPE (type),
11755                                           TYPE_ARG_TYPES (type));
11756
11757         /* Record presence of `static'.  */
11758         publicp = (ctype != NULL_TREE
11759                    || RIDBIT_SETP (RID_EXTERN, specbits)
11760                    || !RIDBIT_SETP (RID_STATIC, specbits));
11761
11762         decl = grokfndecl (ctype, type, original_name, declarator,
11763                            virtualp, flags, quals, raises,
11764                            1, friendp,
11765                            publicp, inlinep, funcdef_flag,
11766                            template_count, in_namespace);
11767         if (decl == NULL_TREE)
11768           return NULL_TREE;
11769
11770         if (staticp == 1)
11771           {
11772             int invalid_static = 0;
11773
11774             /* Don't allow a static member function in a class, and forbid
11775                declaring main to be static.  */
11776             if (TREE_CODE (type) == METHOD_TYPE)
11777               {
11778                 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11779                 invalid_static = 1;
11780               }
11781             else if (current_function_decl)
11782               {
11783                 /* FIXME need arm citation */
11784                 error ("cannot declare static function inside another function");
11785                 invalid_static = 1;
11786               }
11787
11788             if (invalid_static)
11789               {
11790                 staticp = 0;
11791                 RIDBIT_RESET (RID_STATIC, specbits);
11792               }
11793           }
11794       }
11795     else
11796       {
11797         /* It's a variable.  */
11798
11799         /* An uninitialized decl with `extern' is a reference.  */
11800         decl = grokvardecl (type, declarator, &specbits,
11801                             initialized,
11802                             (type_quals & TYPE_QUAL_CONST) != 0,
11803                             ctype ? ctype : in_namespace);
11804         bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11805                         inlinep, friendp, raises != NULL_TREE);
11806
11807         if (ctype)
11808           {
11809             DECL_CONTEXT (decl) = ctype;
11810             if (staticp == 1)
11811               {
11812                 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11813                 staticp = 0;
11814                 RIDBIT_RESET (RID_STATIC, specbits);
11815               }
11816             if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11817               {
11818                 error ("static member `%D' declared `register'", decl);
11819                 RIDBIT_RESET (RID_REGISTER, specbits);
11820               }
11821             if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11822               {
11823                 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11824                             decl);
11825                 RIDBIT_RESET (RID_EXTERN, specbits);
11826               }
11827           }
11828       }
11829
11830     my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11831
11832     /* Record `register' declaration for warnings on &
11833        and in case doing stupid register allocation.  */
11834
11835     if (RIDBIT_SETP (RID_REGISTER, specbits))
11836       DECL_REGISTER (decl) = 1;
11837
11838     if (RIDBIT_SETP (RID_EXTERN, specbits))
11839       DECL_THIS_EXTERN (decl) = 1;
11840
11841     if (RIDBIT_SETP (RID_STATIC, specbits))
11842       DECL_THIS_STATIC (decl) = 1;
11843
11844     /* Record constancy and volatility.  There's no need to do this
11845        when processing a template; we'll do this for the instantiated
11846        declaration based on the type of DECL.  */
11847     if (!processing_template_decl)
11848       c_apply_type_quals_to_decl (type_quals, decl);
11849
11850     return decl;
11851   }
11852 }
11853 \f
11854 /* Subroutine of start_function.  Ensure that each of the parameter
11855    types (as listed in PARMS) is complete, as is required for a
11856    function definition.  */
11857
11858 static void
11859 require_complete_types_for_parms (tree parms)
11860 {
11861   for (; parms; parms = TREE_CHAIN (parms))
11862     {
11863       if (VOID_TYPE_P (TREE_TYPE (parms)))
11864         /* grokparms will have already issued an error */
11865         TREE_TYPE (parms) = error_mark_node;
11866       else if (complete_type_or_else (TREE_TYPE (parms), parms))
11867         {
11868           layout_decl (parms, 0);
11869           DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11870         }
11871       else
11872         TREE_TYPE (parms) = error_mark_node;
11873     }
11874 }
11875
11876 /* Returns nonzero if T is a local variable.  */
11877
11878 int
11879 local_variable_p (tree t)
11880 {
11881   if ((TREE_CODE (t) == VAR_DECL
11882        /* A VAR_DECL with a context that is a _TYPE is a static data
11883           member.  */
11884        && !TYPE_P (CP_DECL_CONTEXT (t))
11885        /* Any other non-local variable must be at namespace scope.  */
11886        && !DECL_NAMESPACE_SCOPE_P (t))
11887       || (TREE_CODE (t) == PARM_DECL))
11888     return 1;
11889
11890   return 0;
11891 }
11892
11893 /* Returns nonzero if T is an automatic local variable or a label.
11894    (These are the declarations that need to be remapped when the code
11895    containing them is duplicated.)  */
11896
11897 int
11898 nonstatic_local_decl_p (tree t)
11899 {
11900   return ((local_variable_p (t) && !TREE_STATIC (t))
11901           || TREE_CODE (t) == LABEL_DECL
11902           || TREE_CODE (t) == RESULT_DECL);
11903 }
11904
11905 /* Like local_variable_p, but suitable for use as a tree-walking
11906    function.  */
11907
11908 static tree
11909 local_variable_p_walkfn (tree* tp,
11910                          int* walk_subtrees ATTRIBUTE_UNUSED ,
11911                          void* data ATTRIBUTE_UNUSED )
11912 {
11913   return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11914           ? *tp : NULL_TREE);
11915 }
11916
11917 /* Check that ARG, which is a default-argument expression for a
11918    parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
11919    something goes wrong.  DECL may also be a _TYPE node, rather than a
11920    DECL, if there is no DECL available.  */
11921
11922 tree
11923 check_default_argument (tree decl, tree arg)
11924 {
11925   tree var;
11926   tree decl_type;
11927
11928   if (TREE_CODE (arg) == DEFAULT_ARG)
11929     /* We get a DEFAULT_ARG when looking at an in-class declaration
11930        with a default argument.  Ignore the argument for now; we'll
11931        deal with it after the class is complete.  */
11932     return arg;
11933
11934   if (processing_template_decl || uses_template_parms (arg))
11935     /* We don't do anything checking until instantiation-time.  Note
11936        that there may be uninstantiated arguments even for an
11937        instantiated function, since default arguments are not
11938        instantiated until they are needed.  */
11939     return arg;
11940
11941   if (TYPE_P (decl))
11942     {
11943       decl_type = decl;
11944       decl = NULL_TREE;
11945     }
11946   else
11947     decl_type = TREE_TYPE (decl);
11948
11949   if (arg == error_mark_node
11950       || decl == error_mark_node
11951       || TREE_TYPE (arg) == error_mark_node
11952       || decl_type == error_mark_node)
11953     /* Something already went wrong.  There's no need to check
11954        further.  */
11955     return error_mark_node;
11956
11957   /* [dcl.fct.default]
11958
11959      A default argument expression is implicitly converted to the
11960      parameter type.  */
11961   if (!TREE_TYPE (arg)
11962       || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11963     {
11964       if (decl)
11965         error ("default argument for `%#D' has type `%T'",
11966                   decl, TREE_TYPE (arg));
11967       else
11968         error ("default argument for parameter of type `%T' has type `%T'",
11969                   decl_type, TREE_TYPE (arg));
11970
11971       return error_mark_node;
11972     }
11973
11974   /* [dcl.fct.default]
11975
11976      Local variables shall not be used in default argument
11977      expressions.
11978
11979      The keyword `this' shall not be used in a default argument of a
11980      member function.  */
11981   var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11982                                       NULL);
11983   if (var)
11984     {
11985       error ("default argument `%E' uses local variable `%D'",
11986                 arg, var);
11987       return error_mark_node;
11988     }
11989
11990   /* All is well.  */
11991   return arg;
11992 }
11993
11994 /* Decode the list of parameter types for a function type.
11995    Given the list of things declared inside the parens,
11996    return a list of types.
11997
11998    We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11999    flag. If unset, we append void_list_node. A parmlist declared
12000    as `(void)' is accepted as the empty parmlist.
12001
12002    Also set last_function_parms to the chain of PARM_DECLs.  */
12003
12004 static tree
12005 grokparms (tree first_parm)
12006 {
12007   tree result = NULL_TREE;
12008   tree decls = NULL_TREE;
12009   int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
12010   tree parm, chain;
12011   int any_error = 0;
12012
12013   my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
12014
12015   for (parm = first_parm; parm != NULL_TREE; parm = chain)
12016     {
12017       tree type = NULL_TREE;
12018       tree decl = TREE_VALUE (parm);
12019       tree init = TREE_PURPOSE (parm);
12020       tree specs, attrs;
12021
12022       chain = TREE_CHAIN (parm);
12023       /* @@ weak defense against parse errors.  */
12024       if (TREE_CODE (decl) != VOID_TYPE
12025           && TREE_CODE (decl) != TREE_LIST)
12026         {
12027           /* Give various messages as the need arises.  */
12028           if (TREE_CODE (decl) == STRING_CST)
12029             error ("invalid string constant `%E'", decl);
12030           else if (TREE_CODE (decl) == INTEGER_CST)
12031             error ("invalid integer constant in parameter list, did you forget to give parameter name?");
12032           continue;
12033         }
12034
12035       if (parm == void_list_node)
12036         break;
12037
12038       split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
12039       decl = grokdeclarator (TREE_VALUE (decl), specs,
12040                              PARM, init != NULL_TREE, &attrs);
12041       if (! decl || TREE_TYPE (decl) == error_mark_node)
12042         continue;
12043
12044       if (attrs)
12045         cplus_decl_attributes (&decl, attrs, 0);
12046
12047       type = TREE_TYPE (decl);
12048       if (VOID_TYPE_P (type))
12049         {
12050           if (same_type_p (type, void_type_node)
12051               && !DECL_NAME (decl) && !result && !chain && !ellipsis)
12052             /* this is a parmlist of `(void)', which is ok.  */
12053             break;
12054           cxx_incomplete_type_error (decl, type);
12055           /* It's not a good idea to actually create parameters of
12056              type `void'; other parts of the compiler assume that a
12057              void type terminates the parameter list.  */
12058           type = error_mark_node;
12059           TREE_TYPE (decl) = error_mark_node;
12060         }
12061
12062       if (type != error_mark_node)
12063         {
12064           /* Top-level qualifiers on the parameters are
12065              ignored for function types.  */
12066           type = TYPE_MAIN_VARIANT (type);
12067           if (TREE_CODE (type) == METHOD_TYPE)
12068             {
12069               error ("parameter `%D' invalidly declared method type", decl);
12070               type = build_pointer_type (type);
12071               TREE_TYPE (decl) = type;
12072             }
12073           else if (TREE_CODE (type) == OFFSET_TYPE)
12074             {
12075               error ("parameter `%D' invalidly declared offset type", decl);
12076               type = build_pointer_type (type);
12077               TREE_TYPE (decl) = type;
12078             }
12079           else if (abstract_virtuals_error (decl, type))
12080             any_error = 1;  /* Seems like a good idea.  */
12081           else if (POINTER_TYPE_P (type))
12082             {
12083               /* [dcl.fct]/6, parameter types cannot contain pointers
12084                  (references) to arrays of unknown bound.  */
12085               tree t = TREE_TYPE (type);
12086               int ptr = TYPE_PTR_P (type);
12087
12088               while (1)
12089                 {
12090                   if (TYPE_PTR_P (t))
12091                     ptr = 1;
12092                   else if (TREE_CODE (t) != ARRAY_TYPE)
12093                     break;
12094                   else if (!TYPE_DOMAIN (t))
12095                     break;
12096                   t = TREE_TYPE (t);
12097                 }
12098               if (TREE_CODE (t) == ARRAY_TYPE)
12099                 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12100                           decl, ptr ? "pointer" : "reference", t);
12101             }
12102
12103           if (!any_error && init)
12104             init = check_default_argument (decl, init);
12105           else
12106             init = NULL_TREE;
12107         }
12108
12109       TREE_CHAIN (decl) = decls;
12110       decls = decl;
12111       result = tree_cons (init, type, result);
12112     }
12113   decls = nreverse (decls);
12114   result = nreverse (result);
12115   if (!ellipsis)
12116     result = chainon (result, void_list_node);
12117   last_function_parms = decls;
12118
12119   return result;
12120 }
12121
12122 \f
12123 /* D is a constructor or overloaded `operator='.
12124
12125    Let T be the class in which D is declared. Then, this function
12126    returns:
12127
12128    -1 if D's is an ill-formed constructor or copy assignment operator
12129       whose first parameter is of type `T'.
12130    0  if D is not a copy constructor or copy assignment
12131       operator.
12132    1  if D is a copy constructor or copy assignment operator whose
12133       first parameter is a reference to const qualified T.
12134    2  if D is a copy constructor or copy assignment operator whose
12135       first parameter is a reference to non-const qualified T.
12136
12137    This function can be used as a predicate. Positive values indicate
12138    a copy constructor and nonzero values indicate a copy assignment
12139    operator.  */
12140
12141 int
12142 copy_fn_p (tree d)
12143 {
12144   tree args;
12145   tree arg_type;
12146   int result = 1;
12147   
12148   my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12149
12150   if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12151     /* Instantiations of template member functions are never copy
12152        functions.  Note that member functions of templated classes are
12153        represented as template functions internally, and we must
12154        accept those as copy functions.  */
12155     return 0;
12156     
12157   args = FUNCTION_FIRST_USER_PARMTYPE (d);
12158   if (!args)
12159     return 0;
12160
12161   arg_type = TREE_VALUE (args);
12162
12163   if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12164     {
12165       /* Pass by value copy assignment operator.  */
12166       result = -1;
12167     }
12168   else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12169            && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12170     {
12171       if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12172         result = 2;
12173     }
12174   else
12175     return 0;
12176   
12177   args = TREE_CHAIN (args);
12178
12179   if (args && args != void_list_node && !TREE_PURPOSE (args))
12180     /* There are more non-optional args.  */
12181     return 0;
12182
12183   return result;
12184 }
12185
12186 /* Remember any special properties of member function DECL.  */
12187
12188 void grok_special_member_properties (tree decl)
12189 {
12190   if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12191     ; /* Not special.  */
12192   else if (DECL_CONSTRUCTOR_P (decl))
12193     {
12194       int ctor = copy_fn_p (decl);
12195       
12196       if (ctor > 0)
12197         {
12198           /* [class.copy]
12199               
12200              A non-template constructor for class X is a copy
12201              constructor if its first parameter is of type X&, const
12202              X&, volatile X& or const volatile X&, and either there
12203              are no other parameters or else all other parameters have
12204              default arguments.  */
12205           TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12206           if (ctor > 1)
12207             TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12208         }
12209       else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12210         TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12211     }
12212   else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12213     {
12214       /* [class.copy]
12215           
12216          A non-template assignment operator for class X is a copy
12217          assignment operator if its parameter is of type X, X&, const
12218          X&, volatile X& or const volatile X&.  */
12219       
12220       int assop = copy_fn_p (decl);
12221       
12222       if (assop)
12223         {
12224           TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12225           if (assop != 1)
12226             TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12227           if (DECL_PURE_VIRTUAL_P (decl))
12228             TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12229         }
12230     }
12231 }
12232
12233 /* Check a constructor DECL has the correct form.  Complains
12234    if the class has a constructor of the form X(X).  */
12235
12236 int
12237 grok_ctor_properties (tree ctype, tree decl)
12238 {
12239   int ctor_parm = copy_fn_p (decl);
12240
12241   if (ctor_parm < 0)
12242     {
12243       /* [class.copy]
12244           
12245          A declaration of a constructor for a class X is ill-formed if
12246          its first parameter is of type (optionally cv-qualified) X
12247          and either there are no other parameters or else all other
12248          parameters have default arguments.
12249           
12250          We *don't* complain about member template instantiations that
12251          have this form, though; they can occur as we try to decide
12252          what constructor to use during overload resolution.  Since
12253          overload resolution will never prefer such a constructor to
12254          the non-template copy constructor (which is either explicitly
12255          or implicitly defined), there's no need to worry about their
12256          existence.  Theoretically, they should never even be
12257          instantiated, but that's hard to forestall.  */
12258       error ("invalid constructor; you probably meant `%T (const %T&)'",
12259                 ctype, ctype);
12260       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12261       return 0;
12262     }
12263   
12264   return 1;
12265 }
12266
12267 /* An operator with this code is unary, but can also be binary.  */
12268
12269 static int
12270 ambi_op_p (enum tree_code code)
12271 {
12272   return (code == INDIRECT_REF
12273           || code == ADDR_EXPR
12274           || code == CONVERT_EXPR
12275           || code == NEGATE_EXPR
12276           || code == PREINCREMENT_EXPR
12277           || code == PREDECREMENT_EXPR);
12278 }
12279
12280 /* An operator with this name can only be unary.  */
12281
12282 static int
12283 unary_op_p (enum tree_code code)
12284 {
12285   return (code == TRUTH_NOT_EXPR
12286           || code == BIT_NOT_EXPR
12287           || code == COMPONENT_REF
12288           || code == TYPE_EXPR);
12289 }
12290
12291 /* Do a little sanity-checking on how they declared their operator.  */
12292
12293 void
12294 grok_op_properties (tree decl, int friendp)
12295 {
12296   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12297   tree argtype;
12298   int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12299   tree name = DECL_NAME (decl);
12300   enum tree_code operator_code;
12301   int arity;
12302
12303   /* Count the number of arguments.  */
12304   for (argtype = argtypes, arity = 0;
12305        argtype && argtype != void_list_node;
12306        argtype = TREE_CHAIN (argtype))
12307     ++arity;
12308
12309   if (current_class_type == NULL_TREE)
12310     friendp = 1;
12311
12312   if (DECL_CONV_FN_P (decl))
12313     operator_code = TYPE_EXPR;
12314   else
12315     do
12316       {
12317 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)       \
12318         if (ansi_opname (CODE) == name)                         \
12319           {                                                     \
12320             operator_code = (CODE);                             \
12321             break;                                              \
12322           }                                                     \
12323         else if (ansi_assopname (CODE) == name)                 \
12324           {                                                     \
12325             operator_code = (CODE);                             \
12326             DECL_ASSIGNMENT_OPERATOR_P (decl) = 1;              \
12327             break;                                              \
12328           }
12329
12330 #include "operators.def"
12331 #undef DEF_OPERATOR
12332
12333         abort ();
12334       }
12335     while (0);
12336   my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12337   SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12338
12339   if (! friendp)
12340     {
12341       switch (operator_code)
12342         {
12343         case CALL_EXPR:
12344           TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12345           break;
12346
12347         case ARRAY_REF:
12348           TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12349           break;
12350
12351         case COMPONENT_REF:
12352         case MEMBER_REF:
12353           TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12354           break;
12355
12356         case NEW_EXPR:
12357           TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12358           break;
12359
12360         case DELETE_EXPR:
12361           TYPE_GETS_DELETE (current_class_type) |= 1;
12362           break;
12363
12364         case VEC_NEW_EXPR:
12365           TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12366           break;
12367
12368         case VEC_DELETE_EXPR:
12369           TYPE_GETS_DELETE (current_class_type) |= 2;
12370           break;
12371
12372         default:
12373           break;
12374         }
12375     }
12376
12377   if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12378     {
12379       /* When the compiler encounters the definition of A::operator new, it
12380          doesn't look at the class declaration to find out if it's static.  */
12381       if (methodp)
12382         revert_static_member_fn (decl);
12383
12384       TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12385     }
12386   else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12387     {
12388       if (methodp)
12389         revert_static_member_fn (decl);
12390
12391       TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12392     }
12393   else
12394     {
12395       /* An operator function must either be a non-static member function
12396          or have at least one parameter of a class, a reference to a class,
12397          an enumeration, or a reference to an enumeration.  13.4.0.6 */
12398       if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12399         {
12400           if (operator_code == TYPE_EXPR
12401               || operator_code == CALL_EXPR
12402               || operator_code == COMPONENT_REF
12403               || operator_code == ARRAY_REF
12404               || operator_code == NOP_EXPR)
12405             error ("`%D' must be a nonstatic member function", decl);
12406           else
12407             {
12408               tree p = argtypes;
12409
12410               if (DECL_STATIC_FUNCTION_P (decl))
12411                 error ("`%D' must be either a non-static member function or a non-member function", decl);
12412
12413               if (p)
12414                 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12415                   {
12416                     tree arg = TREE_VALUE (p);
12417                     if (TREE_CODE (arg) == REFERENCE_TYPE)
12418                       arg = TREE_TYPE (arg);
12419
12420                     /* This lets bad template code slip through.  */
12421                     if (IS_AGGR_TYPE (arg)
12422                         || TREE_CODE (arg) == ENUMERAL_TYPE
12423                         || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12424                         || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12425                       goto foundaggr;
12426                   }
12427               error
12428                 ("`%D' must have an argument of class or enumerated type",
12429                  decl);
12430             foundaggr:
12431               ;
12432             }
12433         }
12434
12435       if (operator_code == CALL_EXPR)
12436         return;                 /* No restrictions on args.  */
12437
12438       if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12439         {
12440           tree t = TREE_TYPE (name);
12441           if (! friendp)
12442             {
12443               int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12444               const char *what = 0;
12445
12446               if (ref)
12447                 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12448
12449               if (TREE_CODE (t) == VOID_TYPE)
12450                 what = "void";
12451               else if (t == current_class_type)
12452                 what = "the same type";
12453               /* Don't force t to be complete here.  */
12454               else if (IS_AGGR_TYPE (t)
12455                        && COMPLETE_TYPE_P (t)
12456                        && DERIVED_FROM_P (t, current_class_type))
12457                 what = "a base class";
12458
12459               if (what)
12460                 warning ("conversion to %s%s will never use a type conversion operator",
12461                          ref ? "a reference to " : "", what);
12462             }
12463         }
12464       if (operator_code == COND_EXPR)
12465         {
12466           /* 13.4.0.3 */
12467           error ("ISO C++ prohibits overloading operator ?:");
12468         }
12469       else if (ambi_op_p (operator_code))
12470         {
12471           if (arity == 1)
12472             /* We pick the one-argument operator codes by default, so
12473                we don't have to change anything.  */
12474             ;
12475           else if (arity == 2)
12476             {
12477               /* If we thought this was a unary operator, we now know
12478                  it to be a binary operator.  */
12479               switch (operator_code)
12480                 {
12481                 case INDIRECT_REF:
12482                   operator_code = MULT_EXPR;
12483                   break;
12484
12485                 case ADDR_EXPR:
12486                   operator_code = BIT_AND_EXPR;
12487                   break;
12488
12489                 case CONVERT_EXPR:
12490                   operator_code = PLUS_EXPR;
12491                   break;
12492
12493                 case NEGATE_EXPR:
12494                   operator_code = MINUS_EXPR;
12495                   break;
12496
12497                 case PREINCREMENT_EXPR:
12498                   operator_code = POSTINCREMENT_EXPR;
12499                   break;
12500
12501                 case PREDECREMENT_EXPR:
12502                   operator_code = POSTDECREMENT_EXPR;
12503                   break;
12504
12505                 default:
12506                   abort ();
12507                 }
12508
12509               SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12510
12511               if ((operator_code == POSTINCREMENT_EXPR
12512                    || operator_code == POSTDECREMENT_EXPR)
12513                   && ! processing_template_decl
12514                   && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12515                 {
12516                   if (methodp)
12517                     error ("postfix `%D' must take `int' as its argument",
12518                               decl);
12519                   else
12520                     error
12521                       ("postfix `%D' must take `int' as its second argument",
12522                        decl);
12523                 }
12524             }
12525           else
12526             {
12527               if (methodp)
12528                 error ("`%D' must take either zero or one argument", decl);
12529               else
12530                 error ("`%D' must take either one or two arguments", decl);
12531             }
12532
12533           /* More Effective C++ rule 6.  */
12534           if (warn_ecpp
12535               && (operator_code == POSTINCREMENT_EXPR
12536                   || operator_code == POSTDECREMENT_EXPR
12537                   || operator_code == PREINCREMENT_EXPR
12538                   || operator_code == PREDECREMENT_EXPR))
12539             {
12540               tree arg = TREE_VALUE (argtypes);
12541               tree ret = TREE_TYPE (TREE_TYPE (decl));
12542               if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12543                 arg = TREE_TYPE (arg);
12544               arg = TYPE_MAIN_VARIANT (arg);
12545               if (operator_code == PREINCREMENT_EXPR
12546                   || operator_code == PREDECREMENT_EXPR)
12547                 {
12548                   if (TREE_CODE (ret) != REFERENCE_TYPE
12549                       || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12550                                        arg))
12551                     warning ("prefix `%D' should return `%T'", decl,
12552                                 build_reference_type (arg));
12553                 }
12554               else
12555                 {
12556                   if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12557                     warning ("postfix `%D' should return `%T'", decl, arg);
12558                 }
12559             }
12560         }
12561       else if (unary_op_p (operator_code))
12562         {
12563           if (arity != 1)
12564             {
12565               if (methodp)
12566                 error ("`%D' must take `void'", decl);
12567               else
12568                 error ("`%D' must take exactly one argument", decl);
12569             }
12570         }
12571       else /* if (binary_op_p (operator_code)) */
12572         {
12573           if (arity != 2)
12574             {
12575               if (methodp)
12576                 error ("`%D' must take exactly one argument", decl);
12577               else
12578                 error ("`%D' must take exactly two arguments", decl);
12579             }
12580
12581           /* More Effective C++ rule 7.  */
12582           if (warn_ecpp
12583               && (operator_code == TRUTH_ANDIF_EXPR
12584                   || operator_code == TRUTH_ORIF_EXPR
12585                   || operator_code == COMPOUND_EXPR))
12586             warning ("user-defined `%D' always evaluates both arguments",
12587                         decl);
12588         }
12589
12590       /* Effective C++ rule 23.  */
12591       if (warn_ecpp
12592           && arity == 2
12593           && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12594           && (operator_code == PLUS_EXPR
12595               || operator_code == MINUS_EXPR
12596               || operator_code == TRUNC_DIV_EXPR
12597               || operator_code == MULT_EXPR
12598               || operator_code == TRUNC_MOD_EXPR)
12599           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12600         warning ("`%D' should return by value", decl);
12601
12602       /* [over.oper]/8 */
12603       for (; argtypes && argtypes != void_list_node;
12604           argtypes = TREE_CHAIN (argtypes))
12605         if (TREE_PURPOSE (argtypes))
12606           {
12607             TREE_PURPOSE (argtypes) = NULL_TREE;
12608             if (operator_code == POSTINCREMENT_EXPR
12609                 || operator_code == POSTDECREMENT_EXPR)
12610               {
12611                 if (pedantic)
12612                   pedwarn ("`%D' cannot have default arguments", decl);
12613               }
12614             else
12615               error ("`%D' cannot have default arguments", decl);
12616           }
12617
12618     }
12619 }
12620 \f
12621 static const char *
12622 tag_name (enum tag_types code)
12623 {
12624   switch (code)
12625     {
12626     case record_type:
12627       return "struct";
12628     case class_type:
12629       return "class";
12630     case union_type:
12631       return "union ";
12632     case enum_type:
12633       return "enum";
12634     default:
12635       abort ();
12636     }
12637 }
12638
12639 /* Get the struct, enum or union (CODE says which) with tag NAME.
12640    Define the tag as a forward-reference if it is not defined.
12641
12642    C++: If a class derivation is given, process it here, and report
12643    an error if multiple derivation declarations are not identical.
12644
12645    If this is a definition, come in through xref_tag and only look in
12646    the current frame for the name (since C++ allows new names in any
12647    scope.)  */
12648
12649 tree
12650 xref_tag (enum tag_types tag_code, tree name, tree attributes, 
12651           bool globalize)
12652 {
12653   enum tree_code code;
12654   register tree ref, t;
12655   struct cp_binding_level *b = current_binding_level;
12656   tree context = NULL_TREE;
12657
12658   timevar_push (TV_NAME_LOOKUP);
12659   switch (tag_code)
12660     {
12661     case record_type:
12662     case class_type:
12663       code = RECORD_TYPE;
12664       break;
12665     case union_type:
12666       code = UNION_TYPE;
12667       break;
12668     case enum_type:
12669       code = ENUMERAL_TYPE;
12670       break;
12671     default:
12672       abort ();
12673     }
12674
12675   /* If a cross reference is requested, look up the type
12676      already defined for this tag and return it.  */
12677   if (TYPE_P (name))
12678     {
12679       t = name;
12680       name = TYPE_IDENTIFIER (t);
12681     }
12682   else
12683     t = IDENTIFIER_TYPE_VALUE (name);
12684
12685   /* Warn about 'friend struct Inherited;' doing the wrong thing.  */
12686   if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12687     {
12688       static int explained;
12689       tree shadowed;
12690
12691       warning ("`%s %T' declares a new type at namespace scope",
12692                   tag_name (tag_code), name);
12693       if (!explained++)
12694         warning ("  names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12695                     tag_name (tag_code),
12696                     constructor_name (current_class_type),
12697                     TYPE_IDENTIFIER (t));
12698
12699       /* We need to remove the class scope binding for the
12700          TYPENAME_TYPE as otherwise poplevel_class gets confused.  */
12701       for (shadowed = b->class_shadowed;
12702            shadowed;
12703            shadowed = TREE_CHAIN (shadowed))
12704         if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12705           {
12706             TREE_PURPOSE (shadowed) = NULL_TREE;
12707             break;
12708           }
12709     }
12710
12711   if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12712       && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12713     t = NULL_TREE;
12714
12715   if (! globalize)
12716     {
12717       /* If we know we are defining this tag, only look it up in
12718          this scope and don't try to find it as a type.  */
12719       ref = lookup_tag (code, name, b, 1);
12720     }
12721   else
12722     {
12723       if (t)
12724         {
12725           ref = follow_tag_typedef (t);
12726
12727           /* [dcl.type.elab] If the identifier resolves to a
12728              typedef-name or a template type-parameter, the
12729              elaborated-type-specifier is ill-formed.  */
12730           if (!ref)
12731             {
12732               pedwarn ("using typedef-name `%D' after `%s'",
12733                        TYPE_NAME (t), tag_name (tag_code));
12734               ref = t;
12735             }
12736           else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12737             error ("using template type parameter `%T' after `%s'",
12738                    t, tag_name (tag_code));
12739         }
12740       else
12741         ref = lookup_tag (code, name, b, 0);
12742
12743       if (! ref)
12744         {
12745           /* Try finding it as a type declaration.  If that wins,
12746              use it.  */
12747           ref = lookup_name (name, 1);
12748
12749           if (ref != NULL_TREE
12750               && processing_template_decl
12751               && DECL_CLASS_TEMPLATE_P (ref)
12752               && template_class_depth (current_class_type) == 0)
12753             /* Since GLOBALIZE is true, we're declaring a global
12754                template, so we want this type.  */
12755             ref = DECL_TEMPLATE_RESULT (ref);
12756
12757           if (ref && TREE_CODE (ref) == TYPE_DECL
12758               && TREE_CODE (TREE_TYPE (ref)) == code)
12759             ref = TREE_TYPE (ref);
12760           else
12761             ref = NULL_TREE;
12762         }
12763
12764       if (ref && current_class_type
12765           && template_class_depth (current_class_type)
12766           && PROCESSING_REAL_TEMPLATE_DECL_P ())
12767         {
12768           /* Since GLOBALIZE is nonzero, we are not looking at a
12769              definition of this tag.  Since, in addition, we are currently
12770              processing a (member) template declaration of a template
12771              class, we must be very careful; consider:
12772
12773                template <class X>
12774                struct S1
12775
12776                template <class U>
12777                struct S2
12778                { template <class V>
12779                friend struct S1; };
12780
12781              Here, the S2::S1 declaration should not be confused with the
12782              outer declaration.  In particular, the inner version should
12783              have a template parameter of level 2, not level 1.  This
12784              would be particularly important if the member declaration
12785              were instead:
12786
12787                template <class V = U> friend struct S1;
12788
12789              say, when we should tsubst into `U' when instantiating
12790              S2.  On the other hand, when presented with:
12791
12792                  template <class T>
12793                  struct S1 {
12794                    template <class U>
12795                    struct S2 {};
12796                    template <class U>
12797                    friend struct S2;
12798                  };
12799
12800               we must find the inner binding eventually.  We
12801               accomplish this by making sure that the new type we
12802               create to represent this declaration has the right
12803               TYPE_CONTEXT.  */
12804           context = TYPE_CONTEXT (ref);
12805           ref = NULL_TREE;
12806         }
12807     }
12808
12809   if (! ref)
12810     {
12811       /* If no such tag is yet defined, create a forward-reference node
12812          and record it as the "definition".
12813          When a real declaration of this type is found,
12814          the forward-reference will be altered into a real type.  */
12815       if (code == ENUMERAL_TYPE)
12816         {
12817           error ("use of enum `%#D' without previous declaration", name);
12818
12819           ref = make_node (ENUMERAL_TYPE);
12820
12821           /* Give the type a default layout like unsigned int
12822              to avoid crashing if it does not get defined.  */
12823           TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12824           TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12825           TYPE_USER_ALIGN (ref) = 0;
12826           TREE_UNSIGNED (ref) = 1;
12827           TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12828           TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12829           TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12830
12831           /* Enable us to recognize when a type is created in class context.
12832              To do nested classes correctly, this should probably be cleared
12833              out when we leave this classes scope.  Currently this in only
12834              done in `start_enum'.  */
12835
12836           pushtag (name, ref, globalize);
12837         }
12838       else
12839         {
12840           struct cp_binding_level *old_b = class_binding_level;
12841
12842           ref = make_aggr_type (code);
12843           TYPE_CONTEXT (ref) = context;
12844
12845 #ifdef NONNESTED_CLASSES
12846           /* Class types don't nest the way enums do.  */
12847           class_binding_level = (struct cp_binding_level *)0;
12848 #endif
12849           pushtag (name, ref, globalize);
12850           class_binding_level = old_b;
12851         }
12852     }
12853   else
12854     {
12855       if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12856         redeclare_class_template (ref, current_template_parms);
12857     }
12858
12859   TYPE_ATTRIBUTES (ref) = attributes;
12860
12861   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
12862 }
12863
12864 tree
12865 xref_tag_from_type (tree old, tree id, int globalize)
12866 {
12867   enum tag_types tag_kind;
12868
12869   if (TREE_CODE (old) == RECORD_TYPE)
12870     tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12871   else
12872     tag_kind  = union_type;
12873
12874   if (id == NULL_TREE)
12875     id = TYPE_IDENTIFIER (old);
12876
12877   return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12878 }
12879
12880 /* REF is a type (named NAME), for which we have just seen some
12881    baseclasses.  BASE_LIST is a list of those baseclasses; the
12882    TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12883    the base-class.  TREE_VIA_VIRTUAL indicates virtual
12884    inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12885    struct, or union.  */
12886
12887 void
12888 xref_basetypes (tree ref, tree base_list)
12889 {
12890   /* In the declaration `A : X, Y, ... Z' we mark all the types
12891      (A, X, Y, ..., Z) so we can check for duplicates.  */
12892   tree *basep;
12893
12894   int i;
12895   enum tag_types tag_code;
12896
12897   if (TREE_CODE (ref) == UNION_TYPE)
12898     {
12899       error ("derived union `%T' invalid", ref);
12900       return;
12901     }
12902
12903   tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12904
12905   /* First, make sure that any templates in base-classes are
12906      instantiated.  This ensures that if we call ourselves recursively
12907      we do not get confused about which classes are marked and which
12908      are not.  */
12909   basep = &base_list; 
12910   while (*basep) 
12911     {
12912       tree basetype = TREE_VALUE (*basep);
12913       if (!(processing_template_decl && uses_template_parms (basetype))
12914           && !complete_type_or_else (basetype, NULL))
12915         /* An incomplete type.  Remove it from the list.  */
12916         *basep = TREE_CHAIN (*basep);
12917       else
12918         basep = &TREE_CHAIN (*basep);
12919     }
12920
12921   SET_CLASSTYPE_MARKED (ref);
12922   i = list_length (base_list);
12923   if (i)
12924     {
12925       tree binfo = TYPE_BINFO (ref);
12926       tree binfos = make_tree_vec (i);
12927       tree accesses = make_tree_vec (i);
12928       
12929       BINFO_BASETYPES (binfo) = binfos;
12930       BINFO_BASEACCESSES (binfo) = accesses;
12931   
12932       for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12933         {
12934           tree access = TREE_PURPOSE (base_list);
12935           int via_virtual = TREE_VIA_VIRTUAL (base_list);
12936           tree basetype = TREE_VALUE (base_list);
12937           tree base_binfo;
12938           
12939           if (access == access_default_node)
12940             /* The base of a derived struct is public by default.  */
12941             access = (tag_code == class_type
12942                       ? access_private_node : access_public_node);
12943           
12944           if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12945             basetype = TREE_TYPE (basetype);
12946           if (!basetype
12947               || (TREE_CODE (basetype) != RECORD_TYPE
12948                   && TREE_CODE (basetype) != TYPENAME_TYPE
12949                   && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12950                   && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12951             {
12952               error ("base type `%T' fails to be a struct or class type",
12953                      basetype);
12954               continue;
12955             }
12956           
12957           if (CLASSTYPE_MARKED (basetype))
12958             {
12959               if (basetype == ref)
12960                 error ("recursive type `%T' undefined", basetype);
12961               else
12962                 error ("duplicate base type `%T' invalid", basetype);
12963               continue;
12964             }
12965           
12966           if (TYPE_FOR_JAVA (basetype)
12967               && (current_lang_depth () == 0))
12968             TYPE_FOR_JAVA (ref) = 1;
12969           
12970           if (CLASS_TYPE_P (basetype))
12971             {
12972               base_binfo = TYPE_BINFO (basetype);
12973               /* This flag will be in the binfo of the base type, we must
12974                  clear it after copying the base binfos.  */
12975               BINFO_DEPENDENT_BASE_P (base_binfo)
12976                 = dependent_type_p (basetype);
12977             }
12978           else
12979             base_binfo = make_binfo (size_zero_node, basetype,
12980                                      NULL_TREE, NULL_TREE);
12981           
12982           TREE_VEC_ELT (binfos, i) = base_binfo;
12983           TREE_VEC_ELT (accesses, i) = access;
12984           /* This flag will be in the binfo of the base type, we must
12985              clear it after copying the base binfos.  */
12986           TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12987           
12988           SET_CLASSTYPE_MARKED (basetype);
12989           
12990           /* We are free to modify these bits because they are meaningless
12991              at top level, and BASETYPE is a top-level type.  */
12992           if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12993             {
12994               TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12995               /* Converting to a virtual base class requires looking
12996                  up the offset of the virtual base.  */
12997               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12998             }
12999           
13000           if (CLASS_TYPE_P (basetype))
13001             {
13002               TYPE_HAS_NEW_OPERATOR (ref)
13003                 |= TYPE_HAS_NEW_OPERATOR (basetype);
13004               TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13005                 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13006               TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13007               /* If the base-class uses multiple inheritance, so do we.  */
13008               TYPE_USES_MULTIPLE_INHERITANCE (ref)
13009                 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
13010               /* Likewise, if converting to a base of the base may require
13011                  code, then we may need to generate code to convert to a
13012                  base as well.  */
13013               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
13014                 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
13015             }
13016           i++;
13017         }
13018       if (i)
13019         TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
13020       else
13021         BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
13022       
13023       if (i > 1)
13024         {
13025           TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
13026           /* If there is more than one non-empty they cannot be at the same
13027              address.  */
13028           TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13029         }
13030     }
13031   
13032   /* Copy the base binfos, collect the virtual bases and set the
13033      inheritance order chain.  */
13034   copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
13035   CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
13036   
13037   /* Unmark all the types.  */
13038   while (i--)
13039     {
13040       tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
13041       
13042       CLEAR_CLASSTYPE_MARKED (basetype);
13043       if (CLASS_TYPE_P (basetype))
13044         {
13045           TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
13046           BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
13047         }
13048     }
13049   CLEAR_CLASSTYPE_MARKED (ref);
13050 }
13051
13052 \f
13053 /* Begin compiling the definition of an enumeration type.
13054    NAME is its name (or null if anonymous).
13055    Returns the type object, as yet incomplete.
13056    Also records info about it so that build_enumerator
13057    may be used to declare the individual values as they are read.  */
13058
13059 tree
13060 start_enum (tree name)
13061 {
13062   register tree enumtype = NULL_TREE;
13063   struct cp_binding_level *b = current_binding_level;
13064
13065   /* If this is the real definition for a previous forward reference,
13066      fill in the contents in the same object that used to be the
13067      forward reference.  */
13068
13069   if (name != NULL_TREE)
13070     enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13071
13072   if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13073     {
13074       error ("multiple definition of `%#T'", enumtype);
13075       cp_error_at ("previous definition here", enumtype);
13076       /* Clear out TYPE_VALUES, and start again.  */
13077       TYPE_VALUES (enumtype) = NULL_TREE;
13078     }
13079   else
13080     {
13081       enumtype = make_node (ENUMERAL_TYPE);
13082       pushtag (name, enumtype, 0);
13083     }
13084
13085   return enumtype;
13086 }
13087
13088 /* After processing and defining all the values of an enumeration type,
13089    install their decls in the enumeration type and finish it off.
13090    ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
13091
13092 void
13093 finish_enum (tree enumtype)
13094 {
13095   tree pair;
13096   tree minnode;
13097   tree maxnode;
13098   tree t;
13099   bool unsignedp;
13100   int lowprec;
13101   int highprec; 
13102   int precision;
13103
13104   /* We built up the VALUES in reverse order.  */
13105   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13106
13107   /* For an enum defined in a template, just set the type of the values;
13108      all further processing is postponed until the template is
13109      instantiated.  We need to set the type so that tsubst of a CONST_DECL
13110      works.  */
13111   if (processing_template_decl)
13112     {
13113       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13114         TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13115       if (at_function_scope_p ())
13116         add_stmt (build_min (TAG_DEFN, enumtype));
13117       return;
13118     }
13119
13120   if (TYPE_VALUES (enumtype))
13121     {
13122       minnode = maxnode = NULL_TREE;
13123
13124       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13125         {
13126           tree decl = TREE_VALUE (pair);
13127           tree value = DECL_INITIAL (decl);
13128
13129           /* [dcl.enum]: Following the closing brace of an enum-specifier,
13130              each enumerator has the type of its enumeration.  Prior to the
13131              closing brace, the type of each enumerator is the type of its
13132              initializing value.  */
13133           TREE_TYPE (decl) = enumtype;
13134
13135           /* Figure out what the minimum and maximum values of the
13136              enumerators are.  */
13137           if (!minnode)
13138             minnode = maxnode = value;
13139           else if (tree_int_cst_lt (maxnode, value))
13140             maxnode = value;
13141           else if (tree_int_cst_lt (value, minnode))
13142             minnode = value;
13143
13144           /* Set the TREE_TYPE for the values as well.  That's so that when
13145              we call decl_constant_value we get an entity of the right type
13146              (but with the constant value).  But first make a copy so we
13147              don't clobber shared INTEGER_CSTs.  */
13148           if (TREE_TYPE (value) != enumtype)
13149             {
13150               value = DECL_INITIAL (decl) = copy_node (value);
13151               TREE_TYPE (value) = enumtype;
13152             }
13153
13154           /* In addition, transform the TYPE_VALUES list to contain the
13155              values, rather than the CONST_DECLs for them.  */
13156           TREE_VALUE (pair) = value;
13157         }
13158     }
13159   else
13160     minnode = maxnode = integer_zero_node;
13161
13162   /* Compute the number of bits require to represent all values of the
13163      enumeration.  We must do this before the type of MINNODE and
13164      MAXNODE are transformed, since min_precision relies on the
13165      TREE_TYPE of the value it is passed.  */
13166   unsignedp = tree_int_cst_sgn (minnode) >= 0;
13167   lowprec = min_precision (minnode, unsignedp);
13168   highprec = min_precision (maxnode, unsignedp);
13169   precision = MAX (lowprec, highprec);
13170
13171   /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'.  */
13172   TYPE_SIZE (enumtype) = NULL_TREE;
13173   TYPE_PRECISION (enumtype) = precision;
13174   if (unsignedp)
13175     fixup_unsigned_type (enumtype);
13176   else
13177     fixup_signed_type (enumtype);
13178
13179   if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13180     /* Use the width of the narrowest normal C type which is wide
13181        enough.  */
13182     TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13183                                                 (precision, 1));
13184   else
13185     TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13186
13187   TYPE_SIZE (enumtype) = NULL_TREE;
13188   layout_type (enumtype);
13189
13190   /* Fix up all variant types of this enum type.  */
13191   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13192     {
13193       TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13194       TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13195       TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13196       TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13197       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13198       TYPE_MODE (t) = TYPE_MODE (enumtype);
13199       TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13200       TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13201       TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13202       TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13203     }
13204
13205   /* Finish debugging output for this type.  */
13206   rest_of_type_compilation (enumtype, namespace_bindings_p ());
13207 }
13208
13209 /* Build and install a CONST_DECL for an enumeration constant of the
13210    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13211    Assignment of sequential values by default is handled here.  */
13212
13213 void
13214 build_enumerator (tree name, tree value, tree enumtype)
13215 {
13216   tree decl;
13217   tree context;
13218   tree type;
13219
13220   /* Remove no-op casts from the value.  */
13221   if (value)
13222     STRIP_TYPE_NOPS (value);
13223
13224   if (! processing_template_decl)
13225     {
13226       /* Validate and default VALUE.  */
13227       if (value != NULL_TREE)
13228         {
13229           value = decl_constant_value (value);
13230
13231           if (TREE_CODE (value) == INTEGER_CST)
13232             {
13233               value = default_conversion (value);
13234               constant_expression_warning (value);
13235             }
13236           else
13237             {
13238               error ("enumerator value for `%D' not integer constant", name);
13239               value = NULL_TREE;
13240             }
13241         }
13242
13243       /* Default based on previous value.  */
13244       if (value == NULL_TREE)
13245         {
13246           tree prev_value;
13247
13248           if (TYPE_VALUES (enumtype))
13249             {
13250               /* The next value is the previous value ...  */
13251               prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13252               /* ... plus one.  */
13253               value = cp_build_binary_op (PLUS_EXPR,
13254                                           prev_value,
13255                                           integer_one_node);
13256
13257               if (tree_int_cst_lt (value, prev_value))
13258                 error ("overflow in enumeration values at `%D'", name);
13259             }
13260           else
13261             value = integer_zero_node;
13262         }
13263
13264       /* Remove no-op casts from the value.  */
13265       STRIP_TYPE_NOPS (value);
13266     }
13267
13268   /* C++ associates enums with global, function, or class declarations.  */
13269   context = current_scope ();
13270
13271   /* Build the actual enumeration constant.  Note that the enumeration
13272     constants have the type of their initializers until the
13273     enumeration is complete:
13274
13275       [ dcl.enum ]
13276
13277       Following the closing brace of an enum-specifier, each enumer-
13278       ator has the type of its enumeration.  Prior to the closing
13279       brace, the type of each enumerator is the type of its
13280       initializing value.
13281
13282     In finish_enum we will reset the type.  Of course, if we're
13283     processing a template, there may be no value.  */
13284   type = value ? TREE_TYPE (value) : NULL_TREE;
13285
13286   if (context && context == current_class_type)
13287     /* This enum declaration is local to the class.  We need the full
13288        lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
13289     decl = build_lang_decl (CONST_DECL, name, type);
13290   else
13291     /* It's a global enum, or it's local to a function.  (Note local to
13292       a function could mean local to a class method.  */
13293     decl = build_decl (CONST_DECL, name, type);
13294
13295   DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13296   DECL_INITIAL (decl) = value;
13297   TREE_READONLY (decl) = 1;
13298
13299   if (context && context == current_class_type)
13300     /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13301       on the TYPE_FIELDS list for `S'.  (That's so that you can say
13302       things like `S::i' later.)  */
13303     finish_member_declaration (decl);
13304   else
13305     pushdecl (decl);
13306
13307   /* Add this enumeration constant to the list for this type.  */
13308   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13309 }
13310
13311 \f
13312 /* We're defining DECL.  Make sure that it's type is OK.  */
13313
13314 static void
13315 check_function_type (tree decl, tree current_function_parms)
13316 {
13317   tree fntype = TREE_TYPE (decl);
13318   tree return_type = complete_type (TREE_TYPE (fntype));
13319
13320   /* In a function definition, arg types must be complete.  */
13321   require_complete_types_for_parms (current_function_parms);
13322
13323   if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13324     {
13325       error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13326
13327       /* Make it return void instead, but don't change the
13328          type of the DECL_RESULT, in case we have a named return value.  */
13329       if (TREE_CODE (fntype) == METHOD_TYPE)
13330         {
13331           tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13332           TREE_TYPE (decl)
13333             = build_cplus_method_type (ctype,
13334                                        void_type_node,
13335                                        FUNCTION_ARG_CHAIN (decl));
13336         }
13337       else
13338         TREE_TYPE (decl)
13339           = build_function_type (void_type_node,
13340                                  TYPE_ARG_TYPES (TREE_TYPE (decl)));
13341       TREE_TYPE (decl)
13342         = build_exception_variant (fntype,
13343                                    TYPE_RAISES_EXCEPTIONS (fntype));
13344     }
13345   else
13346     abstract_virtuals_error (decl, TREE_TYPE (fntype));
13347 }
13348
13349 /* Create the FUNCTION_DECL for a function definition.
13350    DECLSPECS and DECLARATOR are the parts of the declaration;
13351    they describe the function's name and the type it returns,
13352    but twisted together in a fashion that parallels the syntax of C.
13353
13354    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13355    DECLARATOR is really the DECL for the function we are about to
13356    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13357    indicating that the function is an inline defined in-class.
13358
13359    This function creates a binding context for the function body
13360    as well as setting up the FUNCTION_DECL in current_function_decl.
13361
13362    Returns 1 on success.  If the DECLARATOR is not suitable for a function
13363    (it defines a datum instead), we return 0, which tells
13364    yyparse to report a parse error.
13365
13366    For C++, we must first check whether that datum makes any sense.
13367    For example, "class A local_a(1,2);" means that variable local_a
13368    is an aggregate of type A, which should have a constructor
13369    applied to it with the argument list [1, 2].  */
13370
13371 int
13372 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13373 {
13374   tree decl1;
13375   tree ctype = NULL_TREE;
13376   tree fntype;
13377   tree restype;
13378   int doing_friend = 0;
13379   struct cp_binding_level *bl;
13380   tree current_function_parms;
13381
13382   /* Sanity check.  */
13383   my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13384   my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13385
13386   /* This should only be done once on the top most decl.  */
13387   if (have_extern_spec)
13388     {
13389       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13390       have_extern_spec = false;
13391     }
13392
13393   if (flags & SF_PRE_PARSED)
13394     {
13395       decl1 = declarator;
13396
13397       fntype = TREE_TYPE (decl1);
13398       if (TREE_CODE (fntype) == METHOD_TYPE)
13399         ctype = TYPE_METHOD_BASETYPE (fntype);
13400
13401       /* ISO C++ 11.4/5.  A friend function defined in a class is in
13402          the (lexical) scope of the class in which it is defined.  */
13403       if (!ctype && DECL_FRIEND_P (decl1))
13404         {
13405           ctype = DECL_FRIEND_CONTEXT (decl1);
13406
13407           /* CTYPE could be null here if we're dealing with a template;
13408              for example, `inline friend float foo()' inside a template
13409              will have no CTYPE set.  */
13410           if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13411             ctype = NULL_TREE;
13412           else
13413             doing_friend = 1;
13414         }
13415
13416       last_function_parms = DECL_ARGUMENTS (decl1);
13417     }
13418   else
13419     {
13420       decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13421       /* If the declarator is not suitable for a function definition,
13422          cause a syntax error.  */
13423       if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13424         return 0;
13425
13426       cplus_decl_attributes (&decl1, attrs, 0);
13427
13428       /* If #pragma weak was used, mark the decl weak now.  */
13429       if (current_binding_level == global_binding_level)
13430         maybe_apply_pragma_weak (decl1);
13431
13432       fntype = TREE_TYPE (decl1);
13433
13434       restype = TREE_TYPE (fntype);
13435       if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13436         {
13437           error ("semicolon missing after declaration of `%#T'", restype);
13438           shadow_tag (build_tree_list (NULL_TREE, restype));
13439           CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13440           if (TREE_CODE (fntype) == FUNCTION_TYPE)
13441             fntype = build_function_type (integer_type_node,
13442                                           TYPE_ARG_TYPES (fntype));
13443           else
13444             fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13445                                               integer_type_node,
13446                                               TYPE_ARG_TYPES (fntype));
13447           TREE_TYPE (decl1) = fntype;
13448         }
13449
13450       if (TREE_CODE (fntype) == METHOD_TYPE)
13451         ctype = TYPE_METHOD_BASETYPE (fntype);
13452       else if (DECL_MAIN_P (decl1))
13453         {
13454           /* If this doesn't return integer_type, complain.  */
13455           if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13456             {
13457               if (pedantic || warn_return_type)
13458                 pedwarn ("return type for `main' changed to `int'");
13459               TREE_TYPE (decl1) = fntype = default_function_type;
13460             }
13461         }
13462     }
13463
13464   if (DECL_DECLARED_INLINE_P (decl1)
13465       && lookup_attribute ("noinline", attrs))
13466     warning ("%Hinline function '%D' given attribute noinline",
13467              &DECL_SOURCE_LOCATION (decl1), decl1);
13468
13469   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13470     /* This is a constructor, we must ensure that any default args
13471        introduced by this definition are propagated to the clones
13472        now. The clones are used directly in overload resolution.  */
13473     adjust_clone_args (decl1);
13474
13475   /* Sometimes we don't notice that a function is a static member, and
13476      build a METHOD_TYPE for it.  Fix that up now.  */
13477   if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13478       && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13479     {
13480       revert_static_member_fn (decl1);
13481       last_function_parms = TREE_CHAIN (last_function_parms);
13482       ctype = NULL_TREE;
13483     }
13484
13485   /* Warn if function was previously implicitly declared
13486      (but not if we warned then).  */
13487   if (! warn_implicit
13488       && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13489     cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13490
13491   /* Set up current_class_type, and enter the scope of the class, if
13492      appropriate.  */
13493   if (ctype)
13494     push_nested_class (ctype);
13495   else if (DECL_STATIC_FUNCTION_P (decl1))
13496     push_nested_class (DECL_CONTEXT (decl1));
13497
13498   /* Now that we have entered the scope of the class, we must restore
13499      the bindings for any template parameters surrounding DECL1, if it
13500      is an inline member template.  (Order is important; consider the
13501      case where a template parameter has the same name as a field of
13502      the class.)  It is not until after this point that
13503      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
13504   if (flags & SF_INCLASS_INLINE)
13505     maybe_begin_member_template_processing (decl1);
13506
13507   /* Effective C++ rule 15.  */
13508   if (warn_ecpp
13509       && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13510       && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13511     warning ("`operator=' should return a reference to `*this'");
13512
13513   /* Make the init_value nonzero so pushdecl knows this is not tentative.
13514      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
13515   if (!DECL_INITIAL (decl1))
13516     DECL_INITIAL (decl1) = error_mark_node;
13517
13518   /* This function exists in static storage.
13519      (This does not mean `static' in the C sense!)  */
13520   TREE_STATIC (decl1) = 1;
13521
13522   /* We must call push_template_decl after current_class_type is set
13523      up.  (If we are processing inline definitions after exiting a
13524      class scope, current_class_type will be NULL_TREE until set above
13525      by push_nested_class.)  */
13526   if (processing_template_decl)
13527     decl1 = push_template_decl (decl1);
13528
13529   /* We are now in the scope of the function being defined.  */
13530   current_function_decl = decl1;
13531
13532   /* Save the parm names or decls from this function's declarator
13533      where store_parm_decls will find them.  */
13534   current_function_parms = last_function_parms;
13535
13536   /* Make sure the parameter and return types are reasonable.  When
13537      you declare a function, these types can be incomplete, but they
13538      must be complete when you define the function.  */
13539   if (! processing_template_decl)
13540     check_function_type (decl1, current_function_parms);
13541
13542   /* Build the return declaration for the function.  */
13543   restype = TREE_TYPE (fntype);
13544   /* Promote the value to int before returning it.  */
13545   if (c_promoting_integer_type_p (restype))
13546     restype = type_promotes_to (restype);
13547   if (DECL_RESULT (decl1) == NULL_TREE)
13548     {
13549       DECL_RESULT (decl1)
13550         = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13551       c_apply_type_quals_to_decl (cp_type_quals (restype),
13552                                   DECL_RESULT (decl1));
13553     }
13554
13555   /* Initialize RTL machinery.  We cannot do this until
13556      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
13557      even when processing a template; this is how we get
13558      CFUN set up, and our per-function variables initialized.
13559      FIXME factor out the non-RTL stuff.  */
13560   bl = current_binding_level;
13561   init_function_start (decl1, input_filename, lineno);
13562   current_binding_level = bl;
13563
13564   /* Even though we're inside a function body, we still don't want to
13565      call expand_expr to calculate the size of a variable-sized array.
13566      We haven't necessarily assigned RTL to all variables yet, so it's
13567      not safe to try to expand expressions involving them.  */
13568   immediate_size_expand = 0;
13569   cfun->x_dont_save_pending_sizes_p = 1;
13570
13571   /* Start the statement-tree, start the tree now.  */
13572   begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13573
13574   /* Don't double-count statements in templates.  */
13575   DECL_NUM_STMTS (decl1) = 0;
13576
13577   /* Let the user know we're compiling this function.  */
13578   announce_function (decl1);
13579
13580   /* Record the decl so that the function name is defined.
13581      If we already have a decl for this name, and it is a FUNCTION_DECL,
13582      use the old decl.  */
13583   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13584     {
13585       /* A specialization is not used to guide overload resolution.  */
13586       if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13587           && ! DECL_FUNCTION_MEMBER_P (decl1))
13588         decl1 = pushdecl (decl1);
13589       else
13590         {
13591           /* We need to set the DECL_CONTEXT.  */
13592           if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13593             DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13594           /* And make sure we have enough default args.  */
13595           check_default_args (decl1);
13596         }
13597       fntype = TREE_TYPE (decl1);
13598     }
13599
13600   /* Reset these in case the call to pushdecl changed them.  */
13601   current_function_decl = decl1;
13602   cfun->decl = decl1;
13603
13604   /* If we are (erroneously) defining a function that we have already
13605      defined before, wipe out what we knew before.  */
13606   if (!DECL_PENDING_INLINE_P (decl1))
13607     DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13608
13609   if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13610     {
13611       /* We know that this was set up by `grokclassfn'.  We do not
13612          wait until `store_parm_decls', since evil parse errors may
13613          never get us to that point.  Here we keep the consistency
13614          between `current_class_type' and `current_class_ptr'.  */
13615       tree t = DECL_ARGUMENTS (decl1);
13616
13617       my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13618                           162);
13619       my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13620                           19990811);
13621
13622       cp_function_chain->x_current_class_ref
13623         = build_indirect_ref (t, NULL);
13624       cp_function_chain->x_current_class_ptr = t;
13625
13626       /* Constructors and destructors need to know whether they're "in
13627          charge" of initializing virtual base classes.  */
13628       t = TREE_CHAIN (t);
13629       if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13630         {
13631           current_in_charge_parm = t;
13632           t = TREE_CHAIN (t);
13633         }
13634       if (DECL_HAS_VTT_PARM_P (decl1))
13635         {
13636           if (DECL_NAME (t) != vtt_parm_identifier)
13637             abort ();
13638           current_vtt_parm = t;
13639         }
13640     }
13641
13642   if (DECL_INTERFACE_KNOWN (decl1))
13643     {
13644       tree ctx = decl_function_context (decl1);
13645
13646       if (DECL_NOT_REALLY_EXTERN (decl1))
13647         DECL_EXTERNAL (decl1) = 0;
13648
13649       if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13650           && TREE_PUBLIC (ctx))
13651         /* This is a function in a local class in an extern inline
13652            function.  */
13653         comdat_linkage (decl1);
13654     }
13655   /* If this function belongs to an interface, it is public.
13656      If it belongs to someone else's interface, it is also external.
13657      This only affects inlines and template instantiations.  */
13658   else if (interface_unknown == 0
13659            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13660                || flag_alt_external_templates))
13661     {
13662       if (DECL_DECLARED_INLINE_P (decl1) 
13663           || DECL_TEMPLATE_INSTANTIATION (decl1)
13664           || processing_template_decl)
13665         {
13666           DECL_EXTERNAL (decl1)
13667             = (interface_only
13668                || (DECL_DECLARED_INLINE_P (decl1) 
13669                    && ! flag_implement_inlines
13670                    && !DECL_VINDEX (decl1)));
13671
13672           /* For WIN32 we also want to put these in linkonce sections.  */
13673           maybe_make_one_only (decl1);
13674         }
13675       else
13676         DECL_EXTERNAL (decl1) = 0;
13677       DECL_NOT_REALLY_EXTERN (decl1) = 0;
13678       DECL_INTERFACE_KNOWN (decl1) = 1;
13679     }
13680   else if (interface_unknown && interface_only
13681            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13682                || flag_alt_external_templates))
13683     {
13684       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13685          interface, we will have interface_only set but not
13686          interface_known.  In that case, we don't want to use the normal
13687          heuristics because someone will supply a #pragma implementation
13688          elsewhere, and deducing it here would produce a conflict.  */
13689       comdat_linkage (decl1);
13690       DECL_EXTERNAL (decl1) = 0;
13691       DECL_INTERFACE_KNOWN (decl1) = 1;
13692       DECL_DEFER_OUTPUT (decl1) = 1;
13693     }
13694   else
13695     {
13696       /* This is a definition, not a reference.
13697          So clear DECL_EXTERNAL.  */
13698       DECL_EXTERNAL (decl1) = 0;
13699
13700       if ((DECL_DECLARED_INLINE_P (decl1) 
13701            || DECL_TEMPLATE_INSTANTIATION (decl1))
13702           && ! DECL_INTERFACE_KNOWN (decl1)
13703           /* Don't try to defer nested functions for now.  */
13704           && ! decl_function_context (decl1))
13705         DECL_DEFER_OUTPUT (decl1) = 1;
13706       else
13707         DECL_INTERFACE_KNOWN (decl1) = 1;
13708     }
13709
13710   pushlevel (0);
13711   current_binding_level->parm_flag = 1;
13712
13713   ++function_depth;
13714
13715   if (DECL_DESTRUCTOR_P (decl1))
13716     {
13717       dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13718       DECL_CONTEXT (dtor_label) = current_function_decl;
13719     }
13720
13721   start_fname_decls ();
13722   
13723   store_parm_decls (current_function_parms);
13724
13725   return 1;
13726 }
13727 \f
13728 /* Store the parameter declarations into the current function declaration.
13729    This is called after parsing the parameter declarations, before
13730    digesting the body of the function.
13731
13732    Also install to binding contour return value identifier, if any.  */
13733
13734 static void
13735 store_parm_decls (tree current_function_parms)
13736 {
13737   register tree fndecl = current_function_decl;
13738   register tree parm;
13739
13740   /* This is a chain of any other decls that came in among the parm
13741      declarations.  If a parm is declared with  enum {foo, bar} x;
13742      then CONST_DECLs for foo and bar are put here.  */
13743   tree nonparms = NULL_TREE;
13744
13745   if (current_function_parms)
13746     {
13747       /* This case is when the function was defined with an ANSI prototype.
13748          The parms already have decls, so we need not do anything here
13749          except record them as in effect
13750          and complain if any redundant old-style parm decls were written.  */
13751
13752       tree specparms = current_function_parms;
13753       tree next;
13754
13755       /* Must clear this because it might contain TYPE_DECLs declared
13756              at class level.  */
13757       storedecls (NULL_TREE);
13758
13759       /* If we're doing semantic analysis, then we'll call pushdecl
13760              for each of these.  We must do them in reverse order so that
13761              they end in the correct forward order.  */
13762       specparms = nreverse (specparms);
13763
13764       for (parm = specparms; parm; parm = next)
13765         {
13766           next = TREE_CHAIN (parm);
13767           if (TREE_CODE (parm) == PARM_DECL)
13768             {
13769               if (DECL_NAME (parm) == NULL_TREE
13770                   || TREE_CODE (parm) != VOID_TYPE)
13771                 pushdecl (parm);
13772               else
13773                 error ("parameter `%D' declared void", parm);
13774             }
13775           else
13776             {
13777               /* If we find an enum constant or a type tag,
13778                  put it aside for the moment.  */
13779               TREE_CHAIN (parm) = NULL_TREE;
13780               nonparms = chainon (nonparms, parm);
13781             }
13782         }
13783
13784       /* Get the decls in their original chain order and record in the
13785          function.  This is all and only the PARM_DECLs that were
13786          pushed into scope by the loop above.  */
13787       DECL_ARGUMENTS (fndecl) = getdecls ();
13788       storetags (gettags ());
13789     }
13790   else
13791     DECL_ARGUMENTS (fndecl) = NULL_TREE;
13792
13793   /* Now store the final chain of decls for the arguments
13794      as the decl-chain of the current lexical scope.
13795      Put the enumerators in as well, at the front so that
13796      DECL_ARGUMENTS is not modified.  */
13797   storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13798
13799   /* Do the starting of the exception specifications, if we have any.  */
13800   if (flag_exceptions && !processing_template_decl
13801       && flag_enforce_eh_specs
13802       && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13803     current_eh_spec_block = begin_eh_spec_block ();
13804 }
13805
13806 \f
13807 /* We have finished doing semantic analysis on DECL, but have not yet
13808    generated RTL for its body.  Save away our current state, so that
13809    when we want to generate RTL later we know what to do.  */
13810
13811 static void
13812 save_function_data (tree decl)
13813 {
13814   struct language_function *f;
13815
13816   /* Save the language-specific per-function data so that we can
13817      get it back when we really expand this function.  */
13818   my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13819                       19990908);
13820
13821   /* Make a copy.  */
13822   f = ((struct language_function *)
13823        ggc_alloc (sizeof (struct language_function)));
13824   memcpy (f, cp_function_chain, sizeof (struct language_function));
13825   DECL_SAVED_FUNCTION_DATA (decl) = f;
13826
13827   /* Clear out the bits we don't need.  */
13828   f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13829   f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13830   f->x_named_label_uses = NULL;
13831   f->bindings = NULL;
13832   f->x_local_names = NULL;
13833
13834   /* When we get back here again, we will be expanding.  */
13835   f->x_expanding_p = 1;
13836
13837   /* If we've already decided that we cannot inline this function, we
13838      must remember that fact when we actually go to expand the
13839      function.  */
13840   if (current_function_cannot_inline)
13841     {
13842       f->cannot_inline = current_function_cannot_inline;
13843       DECL_INLINE (decl) = 0;
13844     }
13845 }
13846
13847 /* Add a note to mark the beginning of the main body of the constructor.
13848    This is used to set up the data structures for the cleanup regions for
13849    fully-constructed bases and members.  */
13850
13851 static void
13852 begin_constructor_body (void)
13853 {
13854 }
13855
13856 /* Add a note to mark the end of the main body of the constructor.  This is
13857    used to end the cleanup regions for fully-constructed bases and
13858    members.  */
13859
13860 static void
13861 finish_constructor_body (void)
13862 {
13863 }
13864
13865 /* Do all the processing for the beginning of a destructor; set up the
13866    vtable pointers and cleanups for bases and members.  */
13867
13868 static void
13869 begin_destructor_body (void)
13870 {
13871   tree if_stmt;
13872   tree compound_stmt;
13873
13874   /* If the dtor is empty, and we know there is not any possible
13875      way we could use any vtable entries, before they are possibly
13876      set by a base class dtor, we don't have to setup the vtables,
13877      as we know that any base class dtor will set up any vtables
13878      it needs.  We avoid MI, because one base class dtor can do a
13879      virtual dispatch to an overridden function that would need to
13880      have a non-related vtable set up, we cannot avoid setting up
13881      vtables in that case.  We could change this to see if there
13882      is just one vtable.
13883
13884      ??? In the destructor for a class, the vtables are set
13885      appropriately for that class.  There will be no non-related
13886      vtables.  jason 2001-12-11.  */
13887   if_stmt = begin_if_stmt ();
13888
13889   /* If it is not safe to avoid setting up the vtables, then
13890      someone will change the condition to be boolean_true_node.  
13891      (Actually, for now, we do not have code to set the condition
13892      appropriately, so we just assume that we always need to
13893      initialize the vtables.)  */
13894   finish_if_stmt_cond (boolean_true_node, if_stmt);
13895
13896   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13897
13898   /* Make all virtual function table pointers in non-virtual base
13899      classes point to CURRENT_CLASS_TYPE's virtual function
13900      tables.  */
13901   initialize_vtbl_ptrs (current_class_ptr);
13902
13903   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13904   finish_then_clause (if_stmt);
13905   finish_if_stmt ();
13906
13907   /* And insert cleanups for our bases and members so that they
13908      will be properly destroyed if we throw.  */
13909   push_base_cleanups ();
13910 }
13911
13912 /* At the end of every destructor we generate code to delete the object if
13913    necessary.  Do that now.  */
13914
13915 static void
13916 finish_destructor_body (void)
13917 {
13918   tree exprstmt;
13919
13920   /* Any return from a destructor will end up here; that way all base
13921      and member cleanups will be run when the function returns.  */
13922   add_stmt (build_stmt (LABEL_STMT, dtor_label));
13923
13924   /* In a virtual destructor, we must call delete.  */
13925   if (DECL_VIRTUAL_P (current_function_decl))
13926     {
13927       tree if_stmt;
13928       tree virtual_size = cxx_sizeof (current_class_type);
13929
13930       /* [class.dtor]
13931
13932       At the point of definition of a virtual destructor (including
13933       an implicit definition), non-placement operator delete shall
13934       be looked up in the scope of the destructor's class and if
13935       found shall be accessible and unambiguous.  */
13936       exprstmt = build_op_delete_call
13937         (DELETE_EXPR, current_class_ptr, virtual_size,
13938          LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13939
13940       if_stmt = begin_if_stmt ();
13941       finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13942                                   current_in_charge_parm,
13943                                   integer_one_node),
13944                            if_stmt);
13945       finish_expr_stmt (exprstmt);
13946       finish_then_clause (if_stmt);
13947       finish_if_stmt ();
13948     }
13949 }
13950
13951 /* Do the necessary processing for the beginning of a function body, which
13952    in this case includes member-initializers, but not the catch clauses of
13953    a function-try-block.  Currently, this means opening a binding level
13954    for the member-initializers (in a ctor) and member cleanups (in a dtor).
13955    In other functions, this isn't necessary, but it doesn't hurt.  */
13956
13957 tree
13958 begin_function_body (void)
13959 {
13960   tree stmt;
13961
13962   if (processing_template_decl)
13963     /* Do nothing now.  */;
13964   else
13965     /* Always keep the BLOCK node associated with the outermost pair of
13966        curly braces of a function.  These are needed for correct
13967        operation of dwarfout.c.  */
13968     keep_next_level (1);
13969
13970   stmt = begin_compound_stmt (0);
13971   COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13972
13973   if (processing_template_decl)
13974     /* Do nothing now.  */;
13975   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13976     begin_constructor_body ();
13977   else if (DECL_DESTRUCTOR_P (current_function_decl))
13978     begin_destructor_body ();
13979
13980   return stmt;
13981 }
13982
13983 /* Do the processing for the end of a function body.  Currently, this means
13984    closing out the cleanups for fully-constructed bases and members, and in
13985    the case of the destructor, deleting the object if desired.  Again, this
13986    is only meaningful for [cd]tors, since they are the only functions where
13987    there is a significant distinction between the main body and any
13988    function catch clauses.  Handling, say, main() return semantics here
13989    would be wrong, as flowing off the end of a function catch clause for
13990    main() would also need to return 0.  */
13991
13992 void
13993 finish_function_body (tree compstmt)
13994 {
13995   /* Close the block.  */
13996   finish_compound_stmt (0, compstmt);
13997
13998   if (processing_template_decl)
13999     /* Do nothing now.  */;
14000   else if (DECL_CONSTRUCTOR_P (current_function_decl))
14001     finish_constructor_body ();
14002   else if (DECL_DESTRUCTOR_P (current_function_decl))
14003     finish_destructor_body ();
14004 }  
14005
14006 /* Finish up a function declaration and compile that function
14007    all the way to assembler language output.  The free the storage
14008    for the function definition.
14009
14010    FLAGS is a bitwise or of the following values:
14011      2 - INCLASS_INLINE
14012        We just finished processing the body of an in-class inline
14013        function definition.  (This processing will have taken place
14014        after the class definition is complete.)  */
14015
14016 tree
14017 finish_function (int flags)
14018 {
14019   register tree fndecl = current_function_decl;
14020   tree fntype, ctype = NULL_TREE;
14021   int inclass_inline = (flags & 2) != 0;
14022   int nested;
14023
14024   /* When we get some parse errors, we can end up without a
14025      current_function_decl, so cope.  */
14026   if (fndecl == NULL_TREE)
14027     return error_mark_node;
14028
14029   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14030       && DECL_VIRTUAL_P (fndecl)
14031       && !processing_template_decl)
14032     {
14033       tree fnclass = DECL_CONTEXT (fndecl);
14034       if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14035         keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14036     }
14037
14038   nested = function_depth > 1;
14039   fntype = TREE_TYPE (fndecl);
14040
14041   /*  TREE_READONLY (fndecl) = 1;
14042       This caused &foo to be of type ptr-to-const-function
14043       which then got a warning when stored in a ptr-to-function variable.  */
14044
14045   my_friendly_assert (building_stmt_tree (), 20000911);
14046
14047   finish_fname_decls ();
14048   
14049   /* For a cloned function, we've already got all the code we need;
14050      there's no need to add any extra bits.  */
14051   if (!DECL_CLONED_FUNCTION_P (fndecl))
14052     {
14053       if (DECL_MAIN_P (current_function_decl))
14054         {
14055           /* Make it so that `main' always returns 0 by default.  */
14056 #if VMS_TARGET
14057           finish_return_stmt (integer_one_node);
14058 #else
14059           finish_return_stmt (integer_zero_node);
14060 #endif
14061         }
14062
14063       /* Finish dealing with exception specifiers.  */
14064       if (flag_exceptions && !processing_template_decl
14065           && flag_enforce_eh_specs
14066           && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14067         finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14068                               (TREE_TYPE (current_function_decl)),
14069                               current_eh_spec_block);
14070     }
14071
14072   /* If we're saving up tree structure, tie off the function now.  */
14073   finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14074
14075   /* This must come after expand_function_end because cleanups might
14076      have declarations (from inline functions) that need to go into
14077      this function's blocks.  */
14078   
14079   /* If the current binding level isn't the outermost binding level
14080      for this function, either there is a bug, or we have experienced
14081      syntax errors and the statement tree is malformed.  */
14082   if (current_binding_level->parm_flag != 1)
14083     {
14084       /* Make sure we have already experienced errors.  */
14085       if (errorcount == 0)
14086         abort ();
14087
14088       /* Throw away the broken statement tree and extra binding
14089          levels.  */
14090       DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14091
14092       while (current_binding_level->parm_flag != 1)
14093         {
14094           if (current_binding_level->parm_flag == 2)
14095             pop_nested_class ();
14096           else
14097             poplevel (0, 0, 0);
14098         }
14099     }
14100   poplevel (1, 0, 1);
14101
14102   /* Set up the named return value optimization, if we can.  Here, we
14103      eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14104      for the nrv.  genrtl_start_function and declare_return_variable
14105      handle making the nrv and RESULT_DECL share space.  */
14106   if (current_function_return_value)
14107     {
14108       tree r = current_function_return_value;
14109       /* This is only worth doing for fns that return in memory--and
14110          simpler, since we don't have to worry about promoted modes.  */
14111       if (r != error_mark_node
14112           && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl))))
14113         {
14114           DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14115           walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14116                                         nullify_returns_r, r);
14117         }
14118       else
14119         /* Clear it so genrtl_start_function and declare_return_variable
14120            know we're not optimizing.  */
14121         current_function_return_value = NULL_TREE;
14122     }
14123
14124   /* Remember that we were in class scope.  */
14125   if (current_class_name)
14126     ctype = current_class_type;
14127
14128   /* Must mark the RESULT_DECL as being in this function.  */
14129   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14130
14131   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14132      to the FUNCTION_DECL node itself.  */
14133   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14134
14135   /* Save away current state, if appropriate.  */
14136   if (!processing_template_decl)
14137     save_function_data (fndecl);
14138
14139   /* If this function calls `setjmp' it cannot be inlined.  When
14140      `longjmp' is called it is not guaranteed to restore the value of
14141      local variables that have been modified since the call to
14142      `setjmp'.  So, if were to inline this function into some caller
14143      `c', then when we `longjmp', we might not restore all variables
14144      in `c'.  (It might seem, at first blush, that there's no way for
14145      this function to modify local variables in `c', but their
14146      addresses may have been stored somewhere accessible to this
14147      function.)  */
14148   if (!processing_template_decl && calls_setjmp_p (fndecl))
14149     DECL_UNINLINABLE (fndecl) = 1;
14150
14151   /* Complain if there's just no return statement.  */
14152   if (warn_return_type
14153       && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14154       && !current_function_returns_value && !current_function_returns_null
14155       /* Don't complain if we abort or throw.  */
14156       && !current_function_returns_abnormally
14157       && !DECL_NAME (DECL_RESULT (fndecl))
14158       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
14159          inline function, as we might never be compiled separately.  */
14160       && (DECL_INLINE (fndecl) || processing_template_decl))
14161     warning ("no return statement in function returning non-void");
14162     
14163   /* Clear out memory we no longer need.  */
14164   free_after_parsing (cfun);
14165   /* Since we never call rest_of_compilation, we never clear
14166      CFUN.  Do so explicitly.  */
14167   free_after_compilation (cfun);
14168   cfun = NULL;
14169
14170   /* If this is an in-class inline definition, we may have to pop the
14171      bindings for the template parameters that we added in
14172      maybe_begin_member_template_processing when start_function was
14173      called.  */
14174   if (inclass_inline)
14175     maybe_end_member_template_processing ();
14176
14177   /* Leave the scope of the class.  */
14178   if (ctype)
14179     pop_nested_class ();
14180
14181   --function_depth;
14182
14183   /* Clean up.  */
14184   if (! nested)
14185     /* Let the error reporting routines know that we're outside a
14186        function.  For a nested function, this value is used in
14187        cxx_pop_function_context and then reset via pop_function_context.  */
14188     current_function_decl = NULL_TREE;
14189
14190   return fndecl;
14191 }
14192 \f
14193 /* Create the FUNCTION_DECL for a function definition.
14194    DECLSPECS and DECLARATOR are the parts of the declaration;
14195    they describe the return type and the name of the function,
14196    but twisted together in a fashion that parallels the syntax of C.
14197
14198    This function creates a binding context for the function body
14199    as well as setting up the FUNCTION_DECL in current_function_decl.
14200
14201    Returns a FUNCTION_DECL on success.
14202
14203    If the DECLARATOR is not suitable for a function (it defines a datum
14204    instead), we return 0, which tells yyparse to report a parse error.
14205
14206    May return void_type_node indicating that this method is actually
14207    a friend.  See grokfield for more details.
14208
14209    Came here with a `.pushlevel' .
14210
14211    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14212    CHANGES TO CODE IN `grokfield'.  */
14213
14214 tree
14215 start_method (tree declspecs, tree declarator, tree attrlist)
14216 {
14217   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14218                                 &attrlist);
14219
14220   if (fndecl == error_mark_node)
14221     return error_mark_node;
14222
14223   if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14224     {
14225       error ("invalid member function declaration");
14226       return error_mark_node;
14227     }
14228
14229   if (attrlist)
14230     cplus_decl_attributes (&fndecl, attrlist, 0);
14231
14232   /* Pass friends other than inline friend functions back.  */
14233   if (fndecl == void_type_node)
14234     return fndecl;
14235
14236   if (DECL_IN_AGGR_P (fndecl))
14237     {
14238       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14239         {
14240           if (DECL_CONTEXT (fndecl)
14241               && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14242             error ("`%D' is already defined in class `%T'", fndecl,
14243                       DECL_CONTEXT (fndecl));
14244         }
14245       return void_type_node;
14246     }
14247
14248   check_template_shadow (fndecl);
14249
14250   DECL_DECLARED_INLINE_P (fndecl) = 1;
14251
14252   DID_INLINE_FUNC (fndecl) = 0;
14253   if (flag_default_inline)
14254     DECL_INLINE (fndecl) = 1;
14255
14256   /* We process method specializations in finish_struct_1.  */
14257   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14258     fndecl = push_template_decl (fndecl);
14259
14260   if (! DECL_FRIEND_P (fndecl))
14261     {
14262       if (TREE_CHAIN (fndecl))
14263         {
14264           fndecl = copy_node (fndecl);
14265           TREE_CHAIN (fndecl) = NULL_TREE;
14266         }
14267       grok_special_member_properties (fndecl);
14268     }
14269
14270   cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14271
14272   /* Make a place for the parms */
14273   pushlevel (0);
14274   current_binding_level->parm_flag = 1;
14275
14276   DECL_IN_AGGR_P (fndecl) = 1;
14277   return fndecl;
14278 }
14279
14280 /* Go through the motions of finishing a function definition.
14281    We don't compile this method until after the whole class has
14282    been processed.
14283
14284    FINISH_METHOD must return something that looks as though it
14285    came from GROKFIELD (since we are defining a method, after all).
14286
14287    This is called after parsing the body of the function definition.
14288    STMTS is the chain of statements that makes up the function body.
14289
14290    DECL is the ..._DECL that `start_method' provided.  */
14291
14292 tree
14293 finish_method (tree decl)
14294 {
14295   register tree fndecl = decl;
14296   tree old_initial;
14297
14298   register tree link;
14299
14300   if (decl == void_type_node)
14301     return decl;
14302
14303   old_initial = DECL_INITIAL (fndecl);
14304
14305   /* Undo the level for the parms (from start_method).
14306      This is like poplevel, but it causes nothing to be
14307      saved.  Saving information here confuses symbol-table
14308      output routines.  Besides, this information will
14309      be correctly output when this method is actually
14310      compiled.  */
14311
14312   /* Clear out the meanings of the local variables of this level;
14313      also record in each decl which block it belongs to.  */
14314
14315   for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14316     {
14317       if (DECL_NAME (link) != NULL_TREE)
14318         pop_binding (DECL_NAME (link), link);
14319       my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14320       DECL_CONTEXT (link) = NULL_TREE;
14321     }
14322
14323   poplevel (0, 0, 0);
14324
14325   DECL_INITIAL (fndecl) = old_initial;
14326
14327   /* We used to check if the context of FNDECL was different from
14328      current_class_type as another way to get inside here.  This didn't work
14329      for String.cc in libg++.  */
14330   if (DECL_FRIEND_P (fndecl))
14331     {
14332       CLASSTYPE_INLINE_FRIENDS (current_class_type)
14333         = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14334       decl = void_type_node;
14335     }
14336
14337   return decl;
14338 }
14339 \f
14340
14341 /* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
14342    we can lay it out later, when and if its type becomes complete.  */
14343
14344 void
14345 maybe_register_incomplete_var (tree var)
14346 {
14347   my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14348
14349   /* Keep track of variables with incomplete types.  */
14350   if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 
14351       && DECL_EXTERNAL (var))
14352     {
14353       tree inner_type = TREE_TYPE (var);
14354       
14355       while (TREE_CODE (inner_type) == ARRAY_TYPE)
14356         inner_type = TREE_TYPE (inner_type);
14357       inner_type = TYPE_MAIN_VARIANT (inner_type);
14358       
14359       if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14360           /* RTTI TD entries are created while defining the type_info.  */
14361           || (TYPE_LANG_SPECIFIC (inner_type)
14362               && TYPE_BEING_DEFINED (inner_type)))
14363         incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14364     }
14365 }
14366
14367 /* Called when a class type (given by TYPE) is defined.  If there are
14368    any existing VAR_DECLs whose type hsa been completed by this
14369    declaration, update them now.  */
14370
14371 void
14372 complete_vars (tree type)
14373 {
14374   tree *list = &incomplete_vars;
14375
14376   my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14377   while (*list) 
14378     {
14379       if (same_type_p (type, TREE_PURPOSE (*list)))
14380         {
14381           tree var = TREE_VALUE (*list);
14382           /* Complete the type of the variable.  The VAR_DECL itself
14383              will be laid out in expand_expr.  */
14384           complete_type (TREE_TYPE (var));
14385           /* Remove this entry from the list.  */
14386           *list = TREE_CHAIN (*list);
14387         }
14388       else
14389         list = &TREE_CHAIN (*list);
14390     }
14391 }
14392
14393 /* If DECL is of a type which needs a cleanup, build that cleanup
14394    here.  */
14395
14396 tree
14397 cxx_maybe_build_cleanup (tree decl)
14398 {
14399   tree type = TREE_TYPE (decl);
14400
14401   if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14402     {
14403       int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14404       tree rval;
14405
14406       if (TREE_CODE (type) == ARRAY_TYPE)
14407         rval = decl;
14408       else
14409         {
14410           cxx_mark_addressable (decl);
14411           rval = build_unary_op (ADDR_EXPR, decl, 0);
14412         }
14413
14414       /* Optimize for space over speed here.  */
14415       if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14416           || flag_expensive_optimizations)
14417         flags |= LOOKUP_NONVIRTUAL;
14418
14419       rval = build_delete (TREE_TYPE (rval), rval,
14420                            sfk_complete_destructor, flags, 0);
14421
14422       if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14423           && ! TYPE_HAS_DESTRUCTOR (type))
14424         rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14425                                                build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14426
14427       return rval;
14428     }
14429   return NULL_TREE;
14430 }
14431 \f
14432 /* When a stmt has been parsed, this function is called.  */
14433
14434 void
14435 finish_stmt (void)
14436 {
14437   /* Always assume this statement was not an expression statement.  If
14438      it actually was an expression statement, its our callers
14439      responsibility to fix this up.  */
14440   last_expr_type = NULL_TREE;
14441 }
14442
14443 /* DECL was originally constructed as a non-static member function,
14444    but turned out to be static.  Update it accordingly.  */
14445
14446 void
14447 revert_static_member_fn (tree decl)
14448 {
14449   tree tmp;
14450   tree function = TREE_TYPE (decl);
14451   tree args = TYPE_ARG_TYPES (function);
14452
14453   if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14454       != TYPE_UNQUALIFIED)
14455     error ("static member function `%#D' declared with type qualifiers",
14456               decl);
14457
14458   args = TREE_CHAIN (args);
14459   tmp = build_function_type (TREE_TYPE (function), args);
14460   tmp = build_qualified_type (tmp, cp_type_quals (function));
14461   tmp = build_exception_variant (tmp,
14462                                  TYPE_RAISES_EXCEPTIONS (function));
14463   TREE_TYPE (decl) = tmp;
14464   if (DECL_ARGUMENTS (decl))
14465     DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14466   DECL_STATIC_FUNCTION_P (decl) = 1;
14467 }
14468
14469 /* Initialize the variables used during compilation of a C++
14470    function.  */
14471
14472 void
14473 cxx_push_function_context (struct function * f)
14474 {
14475   struct language_function *p
14476     = ((struct language_function *)
14477        ggc_alloc_cleared (sizeof (struct language_function)));
14478   f->language = p;
14479
14480   /* It takes an explicit call to expand_body to generate RTL for a
14481      function.  */
14482   expanding_p = 0;
14483
14484   /* Whenever we start a new function, we destroy temporaries in the
14485      usual way.  */
14486   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14487 }
14488
14489 /* Free the language-specific parts of F, now that we've finished
14490    compiling the function.  */
14491
14492 void
14493 cxx_pop_function_context (struct function * f)
14494 {
14495   f->language = 0;
14496 }
14497
14498 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14499    one of the language-independent trees.  */
14500
14501 enum cp_tree_node_structure_enum
14502 cp_tree_node_structure (union lang_tree_node * t)
14503 {
14504   switch (TREE_CODE (&t->generic))
14505     {
14506     case DEFAULT_ARG:           return TS_CP_DEFAULT_ARG;
14507     case IDENTIFIER_NODE:       return TS_CP_IDENTIFIER;
14508     case CPLUS_BINDING:         return TS_CP_BINDING;
14509     case OVERLOAD:              return TS_CP_OVERLOAD;
14510     case TEMPLATE_PARM_INDEX:   return TS_CP_TPI;
14511     case PTRMEM_CST:            return TS_CP_PTRMEM;
14512     case BASELINK:              return TS_CP_BASELINK;
14513     case WRAPPER:               return TS_CP_WRAPPER;
14514     case SRCLOC:                return TS_CP_SRCLOC;
14515     default:                    return TS_CP_GENERIC;
14516     }
14517 }
14518
14519 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14520    the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
14521
14522 tree
14523 identifier_global_value (tree t)
14524 {
14525   return IDENTIFIER_GLOBAL_VALUE (t);
14526 }
14527
14528 /* Build the void_list_node (void_type_node having been created).  */
14529 tree
14530 build_void_list_node (void)
14531 {
14532   tree t = build_tree_list (NULL_TREE, void_type_node);
14533   TREE_PARMLIST (t) = 1;
14534   return t;
14535 }
14536
14537 static int
14538 cp_missing_noreturn_ok_p (tree decl)
14539 {
14540   /* A missing noreturn is ok for the `main' function.  */
14541   return DECL_MAIN_P (decl);
14542 }
14543
14544 #include "gt-cp-decl.h"
14545 #include "gtype-cp.h"