OSDN Git Service

955fd0f0d538e8ca5441ea257cbba733f3088515
[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, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4    Free Software Foundation, Inc.
5    Contributed by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
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 "flags.h"
37 #include "cp-tree.h"
38 #include "tree-iterator.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "intl.h"
42 #include "output.h"
43 #include "toplev.h"
44 #include "hashtab.h"
45 #include "tm_p.h"
46 #include "target.h"
47 #include "c-family/c-common.h"
48 #include "c-family/c-objc.h"
49 #include "c-family/c-pragma.h"
50 #include "diagnostic.h"
51 #include "intl.h"
52 #include "debug.h"
53 #include "timevar.h"
54 #include "pointer-set.h"
55 #include "splay-tree.h"
56 #include "plugin.h"
57
58 /* Possible cases of bad specifiers type used by bad_specifiers. */
59 enum bad_spec_place {
60   BSP_VAR,    /* variable */
61   BSP_PARM,   /* parameter */
62   BSP_TYPE,   /* type */
63   BSP_FIELD   /* field */
64 };
65
66 static tree grokparms (tree parmlist, tree *);
67 static const char *redeclaration_error_message (tree, tree);
68
69 static int decl_jump_unsafe (tree);
70 static void require_complete_types_for_parms (tree);
71 static int ambi_op_p (enum tree_code);
72 static int unary_op_p (enum tree_code);
73 static void push_local_name (tree);
74 static tree grok_reference_init (tree, tree, tree, int);
75 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
76                          int, int, tree);
77 static int check_static_variable_definition (tree, tree);
78 static void record_unknown_type (tree, const char *);
79 static tree builtin_function_1 (tree, tree, bool);
80 static tree build_library_fn_1 (tree, enum tree_code, tree);
81 static int member_function_or_else (tree, tree, enum overload_flags);
82 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
83                             int);
84 static void check_for_uninitialized_const_var (tree);
85 static hashval_t typename_hash (const void *);
86 static int typename_compare (const void *, const void *);
87 static tree local_variable_p_walkfn (tree *, int *, void *);
88 static tree record_builtin_java_type (const char *, int);
89 static const char *tag_name (enum tag_types);
90 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
91 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
92 static void maybe_deduce_size_from_array_init (tree, tree);
93 static void layout_var_decl (tree);
94 static tree check_initializer (tree, tree, int, VEC(tree,gc) **);
95 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
96 static void save_function_data (tree);
97 static void copy_type_enum (tree , tree);
98 static void check_function_type (tree, tree);
99 static void finish_constructor_body (void);
100 static void begin_destructor_body (void);
101 static void finish_destructor_body (void);
102 static void record_key_method_defined (tree);
103 static tree create_array_type_for_decl (tree, tree, tree);
104 static tree get_atexit_node (void);
105 static tree get_dso_handle_node (void);
106 static tree start_cleanup_fn (void);
107 static void end_cleanup_fn (void);
108 static tree cp_make_fname_decl (location_t, tree, int);
109 static void initialize_predefined_identifiers (void);
110 static tree check_special_function_return_type
111         (special_function_kind, tree, tree);
112 static tree push_cp_library_fn (enum tree_code, tree);
113 static tree build_cp_library_fn (tree, enum tree_code, tree);
114 static void store_parm_decls (tree);
115 static void initialize_local_var (tree, tree);
116 static void expand_static_init (tree, tree);
117
118 /* The following symbols are subsumed in the cp_global_trees array, and
119    listed here individually for documentation purposes.
120
121    C++ extensions
122         tree wchar_decl_node;
123
124         tree vtable_entry_type;
125         tree delta_type_node;
126         tree __t_desc_type_node;
127
128         tree class_type_node;
129         tree unknown_type_node;
130
131    Array type `vtable_entry_type[]'
132
133         tree vtbl_type_node;
134         tree vtbl_ptr_type_node;
135
136    Namespaces,
137
138         tree std_node;
139         tree abi_node;
140
141    A FUNCTION_DECL which can call `abort'.  Not necessarily the
142    one that the user will declare, but sufficient to be called
143    by routines that want to abort the program.
144
145         tree abort_fndecl;
146
147    The FUNCTION_DECL for the default `::operator delete'.
148
149         tree global_delete_fndecl;
150
151    Used by RTTI
152         tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
153         tree tinfo_var_id;  */
154
155 tree cp_global_trees[CPTI_MAX];
156
157 /* Indicates that there is a type value in some namespace, although
158    that is not necessarily in scope at the moment.  */
159
160 tree global_type_node;
161
162 /* The node that holds the "name" of the global scope.  */
163 tree global_scope_name;
164
165 #define local_names cp_function_chain->x_local_names
166
167 /* A list of objects which have constructors or destructors
168    which reside in the global scope.  The decl is stored in
169    the TREE_VALUE slot and the initializer is stored
170    in the TREE_PURPOSE slot.  */
171 tree static_aggregates;
172
173 /* -- end of C++ */
174
175 /* A node for the integer constant 2.  */
176
177 tree integer_two_node;
178
179 /* Used only for jumps to as-yet undefined labels, since jumps to
180    defined labels can have their validity checked immediately.  */
181
182 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
183   struct named_label_use_entry *next;
184   /* The binding level to which this entry is *currently* attached.
185      This is initially the binding level in which the goto appeared,
186      but is modified as scopes are closed.  */
187   cp_binding_level *binding_level;
188   /* The head of the names list that was current when the goto appeared,
189      or the inner scope popped.  These are the decls that will *not* be
190      skipped when jumping to the label.  */
191   tree names_in_scope;
192   /* The location of the goto, for error reporting.  */
193   location_t o_goto_locus;
194   /* True if an OpenMP structured block scope has been closed since
195      the goto appeared.  This means that the branch from the label will
196      illegally exit an OpenMP scope.  */
197   bool in_omp_scope;
198 };
199
200 /* A list of all LABEL_DECLs in the function that have names.  Here so
201    we can clear out their names' definitions at the end of the
202    function, and so we can check the validity of jumps to these labels.  */
203
204 struct GTY(()) named_label_entry {
205   /* The decl itself.  */
206   tree label_decl;
207
208   /* The binding level to which the label is *currently* attached.
209      This is initially set to the binding level in which the label
210      is defined, but is modified as scopes are closed.  */
211   cp_binding_level *binding_level;
212   /* The head of the names list that was current when the label was
213      defined, or the inner scope popped.  These are the decls that will
214      be skipped when jumping to the label.  */
215   tree names_in_scope;
216   /* A vector of all decls from all binding levels that would be
217      crossed by a backward branch to the label.  */
218   VEC(tree,gc) *bad_decls;
219
220   /* A list of uses of the label, before the label is defined.  */
221   struct named_label_use_entry *uses;
222
223   /* The following bits are set after the label is defined, and are
224      updated as scopes are popped.  They indicate that a backward jump
225      to the label will illegally enter a scope of the given flavor.  */
226   bool in_try_scope;
227   bool in_catch_scope;
228   bool in_omp_scope;
229 };
230
231 #define named_labels cp_function_chain->x_named_labels
232 \f
233 /* The number of function bodies which we are currently processing.
234    (Zero if we are at namespace scope, one inside the body of a
235    function, two inside the body of a function in a local class, etc.)  */
236 int function_depth;
237
238 /* To avoid unwanted recursion, finish_function defers all mark_used calls
239    encountered during its execution until it finishes.  */
240 bool defer_mark_used_calls;
241 VEC(tree, gc) *deferred_mark_used_calls;
242
243 /* States indicating how grokdeclarator() should handle declspecs marked
244    with __attribute__((deprecated)).  An object declared as
245    __attribute__((deprecated)) suppresses warnings of uses of other
246    deprecated items.  */
247 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
248
249 \f
250 /* A list of VAR_DECLs whose type was incomplete at the time the
251    variable was declared.  */
252
253 typedef struct GTY(()) incomplete_var_d {
254   tree decl;
255   tree incomplete_type;
256 } incomplete_var;
257
258 DEF_VEC_O(incomplete_var);
259 DEF_VEC_ALLOC_O(incomplete_var,gc);
260
261 static GTY(()) VEC(incomplete_var,gc) *incomplete_vars;
262 \f
263 /* Returns the kind of template specialization we are currently
264    processing, given that it's declaration contained N_CLASS_SCOPES
265    explicit scope qualifications.  */
266
267 tmpl_spec_kind
268 current_tmpl_spec_kind (int n_class_scopes)
269 {
270   int n_template_parm_scopes = 0;
271   int seen_specialization_p = 0;
272   int innermost_specialization_p = 0;
273   cp_binding_level *b;
274
275   /* Scan through the template parameter scopes.  */
276   for (b = current_binding_level;
277        b->kind == sk_template_parms;
278        b = b->level_chain)
279     {
280       /* If we see a specialization scope inside a parameter scope,
281          then something is wrong.  That corresponds to a declaration
282          like:
283
284             template <class T> template <> ...
285
286          which is always invalid since [temp.expl.spec] forbids the
287          specialization of a class member template if the enclosing
288          class templates are not explicitly specialized as well.  */
289       if (b->explicit_spec_p)
290         {
291           if (n_template_parm_scopes == 0)
292             innermost_specialization_p = 1;
293           else
294             seen_specialization_p = 1;
295         }
296       else if (seen_specialization_p == 1)
297         return tsk_invalid_member_spec;
298
299       ++n_template_parm_scopes;
300     }
301
302   /* Handle explicit instantiations.  */
303   if (processing_explicit_instantiation)
304     {
305       if (n_template_parm_scopes != 0)
306         /* We've seen a template parameter list during an explicit
307            instantiation.  For example:
308
309              template <class T> template void f(int);
310
311            This is erroneous.  */
312         return tsk_invalid_expl_inst;
313       else
314         return tsk_expl_inst;
315     }
316
317   if (n_template_parm_scopes < n_class_scopes)
318     /* We've not seen enough template headers to match all the
319        specialized classes present.  For example:
320
321          template <class T> void R<T>::S<T>::f(int);
322
323        This is invalid; there needs to be one set of template
324        parameters for each class.  */
325     return tsk_insufficient_parms;
326   else if (n_template_parm_scopes == n_class_scopes)
327     /* We're processing a non-template declaration (even though it may
328        be a member of a template class.)  For example:
329
330          template <class T> void S<T>::f(int);
331
332        The `class T' matches the `S<T>', leaving no template headers
333        corresponding to the `f'.  */
334     return tsk_none;
335   else if (n_template_parm_scopes > n_class_scopes + 1)
336     /* We've got too many template headers.  For example:
337
338          template <> template <class T> void f (T);
339
340        There need to be more enclosing classes.  */
341     return tsk_excessive_parms;
342   else
343     /* This must be a template.  It's of the form:
344
345          template <class T> template <class U> void S<T>::f(U);
346
347        This is a specialization if the innermost level was a
348        specialization; otherwise it's just a definition of the
349        template.  */
350     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
351 }
352
353 /* Exit the current scope.  */
354
355 void
356 finish_scope (void)
357 {
358   poplevel (0, 0, 0);
359 }
360
361 /* When a label goes out of scope, check to see if that label was used
362    in a valid manner, and issue any appropriate warnings or errors.  */
363
364 static void
365 pop_label (tree label, tree old_value)
366 {
367   if (!processing_template_decl)
368     {
369       if (DECL_INITIAL (label) == NULL_TREE)
370         {
371           location_t location;
372
373           error ("label %q+D used but not defined", label);
374           location = input_location; /* FIXME want (input_filename, (line)0) */
375           /* Avoid crashing later.  */
376           define_label (location, DECL_NAME (label));
377         }
378       else 
379         warn_for_unused_label (label);
380     }
381
382   SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
383 }
384
385 /* At the end of a function, all labels declared within the function
386    go out of scope.  BLOCK is the top-level block for the
387    function.  */
388
389 static int
390 pop_labels_1 (void **slot, void *data)
391 {
392   struct named_label_entry *ent = (struct named_label_entry *) *slot;
393   tree block = (tree) data;
394
395   pop_label (ent->label_decl, NULL_TREE);
396
397   /* Put the labels into the "variables" of the top-level block,
398      so debugger can see them.  */
399   DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
400   BLOCK_VARS (block) = ent->label_decl;
401
402   htab_clear_slot (named_labels, slot);
403
404   return 1;
405 }
406
407 static void
408 pop_labels (tree block)
409 {
410   if (named_labels)
411     {
412       htab_traverse (named_labels, pop_labels_1, block);
413       named_labels = NULL;
414     }
415 }
416
417 /* At the end of a block with local labels, restore the outer definition.  */
418
419 static void
420 pop_local_label (tree label, tree old_value)
421 {
422   struct named_label_entry dummy;
423   void **slot;
424
425   pop_label (label, old_value);
426
427   dummy.label_decl = label;
428   slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
429   htab_clear_slot (named_labels, slot);
430 }
431
432 /* The following two routines are used to interface to Objective-C++.
433    The binding level is purposely treated as an opaque type.  */
434
435 void *
436 objc_get_current_scope (void)
437 {
438   return current_binding_level;
439 }
440
441 /* The following routine is used by the NeXT-style SJLJ exceptions;
442    variables get marked 'volatile' so as to not be clobbered by
443    _setjmp()/_longjmp() calls.  All variables in the current scope,
444    as well as parent scopes up to (but not including) ENCLOSING_BLK
445    shall be thusly marked.  */
446
447 void
448 objc_mark_locals_volatile (void *enclosing_blk)
449 {
450   cp_binding_level *scope;
451
452   for (scope = current_binding_level;
453        scope && scope != enclosing_blk;
454        scope = scope->level_chain)
455     {
456       tree decl;
457
458       for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
459         objc_volatilize_decl (decl);
460
461       /* Do not climb up past the current function.  */
462       if (scope->kind == sk_function_parms)
463         break;
464     }
465 }
466
467 /* Update data for defined and undefined labels when leaving a scope.  */
468
469 static int
470 poplevel_named_label_1 (void **slot, void *data)
471 {
472   struct named_label_entry *ent = (struct named_label_entry *) *slot;
473   cp_binding_level *bl = (cp_binding_level *) data;
474   cp_binding_level *obl = bl->level_chain;
475
476   if (ent->binding_level == bl)
477     {
478       tree decl;
479
480       /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
481          TREE_LISTs representing OVERLOADs, so be careful.  */
482       for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
483                                                      ? DECL_CHAIN (decl)
484                                                      : TREE_CHAIN (decl)))
485         if (decl_jump_unsafe (decl))
486           VEC_safe_push (tree, gc, ent->bad_decls, decl);
487
488       ent->binding_level = obl;
489       ent->names_in_scope = obl->names;
490       switch (bl->kind)
491         {
492         case sk_try:
493           ent->in_try_scope = true;
494           break;
495         case sk_catch:
496           ent->in_catch_scope = true;
497           break;
498         case sk_omp:
499           ent->in_omp_scope = true;
500           break;
501         default:
502           break;
503         }
504     }
505   else if (ent->uses)
506     {
507       struct named_label_use_entry *use;
508
509       for (use = ent->uses; use ; use = use->next)
510         if (use->binding_level == bl)
511           {
512             use->binding_level = obl;
513             use->names_in_scope = obl->names;
514             if (bl->kind == sk_omp)
515               use->in_omp_scope = true;
516           }
517     }
518
519   return 1;
520 }
521
522 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
523    when errors were reported, except for -Werror-unused-but-set-*.  */
524 static int unused_but_set_errorcount;
525
526 /* Exit a binding level.
527    Pop the level off, and restore the state of the identifier-decl mappings
528    that were in effect when this level was entered.
529
530    If KEEP == 1, this level had explicit declarations, so
531    and create a "block" (a BLOCK node) for the level
532    to record its declarations and subblocks for symbol table output.
533
534    If FUNCTIONBODY is nonzero, this level is the body of a function,
535    so create a block as if KEEP were set and also clear out all
536    label names.
537
538    If REVERSE is nonzero, reverse the order of decls before putting
539    them into the BLOCK.  */
540
541 tree
542 poplevel (int keep, int reverse, int functionbody)
543 {
544   tree link;
545   /* The chain of decls was accumulated in reverse order.
546      Put it into forward order, just for cleanliness.  */
547   tree decls;
548   tree subblocks;
549   tree block;
550   tree decl;
551   int leaving_for_scope;
552   scope_kind kind;
553   unsigned ix;
554   cp_label_binding *label_bind;
555
556   timevar_start (TV_NAME_LOOKUP);
557  restart:
558
559   block = NULL_TREE;
560
561   gcc_assert (current_binding_level->kind != sk_class);
562
563   if (current_binding_level->kind == sk_cleanup)
564     functionbody = 0;
565   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
566
567   gcc_assert (!VEC_length(cp_class_binding,
568                           current_binding_level->class_shadowed));
569
570   /* We used to use KEEP == 2 to indicate that the new block should go
571      at the beginning of the list of blocks at this binding level,
572      rather than the end.  This hack is no longer used.  */
573   gcc_assert (keep == 0 || keep == 1);
574
575   if (current_binding_level->keep)
576     keep = 1;
577
578   /* Any uses of undefined labels, and any defined labels, now operate
579      under constraints of next binding contour.  */
580   if (cfun && !functionbody && named_labels)
581     htab_traverse (named_labels, poplevel_named_label_1,
582                    current_binding_level);
583
584   /* Get the decls in the order they were written.
585      Usually current_binding_level->names is in reverse order.
586      But parameter decls were previously put in forward order.  */
587
588   if (reverse)
589     current_binding_level->names
590       = decls = nreverse (current_binding_level->names);
591   else
592     decls = current_binding_level->names;
593
594   /* If there were any declarations or structure tags in that level,
595      or if this level is a function body,
596      create a BLOCK to record them for the life of this function.  */
597   block = NULL_TREE;
598   if (keep == 1 || functionbody)
599     block = make_node (BLOCK);
600   if (block != NULL_TREE)
601     {
602       BLOCK_VARS (block) = decls;
603       BLOCK_SUBBLOCKS (block) = subblocks;
604     }
605
606   /* In each subblock, record that this is its superior.  */
607   if (keep >= 0)
608     for (link = subblocks; link; link = BLOCK_CHAIN (link))
609       BLOCK_SUPERCONTEXT (link) = block;
610
611   /* We still support the old for-scope rules, whereby the variables
612      in a for-init statement were in scope after the for-statement
613      ended.  We only use the new rules if flag_new_for_scope is
614      nonzero.  */
615   leaving_for_scope
616     = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
617
618   /* Before we remove the declarations first check for unused variables.  */
619   if ((warn_unused_variable || warn_unused_but_set_variable)
620       && !processing_template_decl)
621     for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
622       if (TREE_CODE (decl) == VAR_DECL
623           && (! TREE_USED (decl) || !DECL_READ_P (decl))
624           && ! DECL_IN_SYSTEM_HEADER (decl)
625           && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
626         {
627           if (! TREE_USED (decl))
628             warning (OPT_Wunused_variable, "unused variable %q+D", decl);
629           else if (DECL_CONTEXT (decl) == current_function_decl
630                    && TREE_TYPE (decl) != error_mark_node
631                    && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
632                    && errorcount == unused_but_set_errorcount
633                    && (!CLASS_TYPE_P (TREE_TYPE (decl))
634                        || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
635             {
636               warning (OPT_Wunused_but_set_variable,
637                        "variable %q+D set but not used", decl); 
638               unused_but_set_errorcount = errorcount;
639             }
640         }
641
642   /* Remove declarations for all the DECLs in this level.  */
643   for (link = decls; link; link = TREE_CHAIN (link))
644     {
645       if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
646           /* It's hard to make this ARM compatibility hack play nicely with
647              lambdas, and it really isn't necessary in C++11 mode.  */
648           && cxx_dialect < cxx0x
649           && DECL_NAME (link))
650         {
651           tree name = DECL_NAME (link);
652           cxx_binding *ob;
653           tree ns_binding;
654
655           ob = outer_binding (name,
656                               IDENTIFIER_BINDING (name),
657                               /*class_p=*/true);
658           if (!ob)
659             ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
660           else
661             ns_binding = NULL_TREE;
662
663           if (ob && ob->scope == current_binding_level->level_chain)
664             /* We have something like:
665
666                  int i;
667                  for (int i; ;);
668
669                and we are leaving the `for' scope.  There's no reason to
670                keep the binding of the inner `i' in this case.  */
671             pop_binding (name, link);
672           else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
673                    || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
674             /* Here, we have something like:
675
676                  typedef int I;
677
678                  void f () {
679                    for (int I; ;);
680                  }
681
682                We must pop the for-scope binding so we know what's a
683                type and what isn't.  */
684             pop_binding (name, link);
685           else
686             {
687               /* Mark this VAR_DECL as dead so that we can tell we left it
688                  there only for backward compatibility.  */
689               DECL_DEAD_FOR_LOCAL (link) = 1;
690
691               /* Keep track of what should have happened when we
692                  popped the binding.  */
693               if (ob && ob->value)
694                 {
695                   SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
696                   DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
697                 }
698
699               /* Add it to the list of dead variables in the next
700                  outermost binding to that we can remove these when we
701                  leave that binding.  */
702               VEC_safe_push (tree, gc,
703                              current_binding_level->level_chain->dead_vars_from_for,
704                              link);
705
706               /* Although we don't pop the cxx_binding, we do clear
707                  its SCOPE since the scope is going away now.  */
708               IDENTIFIER_BINDING (name)->scope
709                 = current_binding_level->level_chain;
710             }
711         }
712       else
713         {
714           tree name;
715
716           /* Remove the binding.  */
717           decl = link;
718
719           if (TREE_CODE (decl) == TREE_LIST)
720             decl = TREE_VALUE (decl);
721           name = decl;
722
723           if (TREE_CODE (name) == OVERLOAD)
724             name = OVL_FUNCTION (name);
725
726           gcc_assert (DECL_P (name));
727           pop_binding (DECL_NAME (name), decl);
728         }
729     }
730
731   /* Remove declarations for any `for' variables from inner scopes
732      that we kept around.  */
733   FOR_EACH_VEC_ELT_REVERSE (tree, current_binding_level->dead_vars_from_for,
734                             ix, decl)
735     pop_binding (DECL_NAME (decl), decl);
736
737   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
738   for (link = current_binding_level->type_shadowed;
739        link; link = TREE_CHAIN (link))
740     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
741
742   /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
743   FOR_EACH_VEC_ELT_REVERSE (cp_label_binding,
744                             current_binding_level->shadowed_labels,
745                             ix, label_bind)
746     pop_local_label (label_bind->label, label_bind->prev_value);
747
748   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
749      list if a `using' declaration put them there.  The debugging
750      back ends won't understand OVERLOAD, so we remove them here.
751      Because the BLOCK_VARS are (temporarily) shared with
752      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
753      popped all the bindings.  */
754   if (block)
755     {
756       tree* d;
757
758       for (d = &BLOCK_VARS (block); *d; )
759         {
760           if (TREE_CODE (*d) == TREE_LIST)
761             *d = TREE_CHAIN (*d);
762           else
763             d = &DECL_CHAIN (*d);
764         }
765     }
766
767   /* If the level being exited is the top level of a function,
768      check over all the labels.  */
769   if (functionbody)
770     {
771       /* Since this is the top level block of a function, the vars are
772          the function's parameters.  Don't leave them in the BLOCK
773          because they are found in the FUNCTION_DECL instead.  */
774       BLOCK_VARS (block) = 0;
775       pop_labels (block);
776     }
777
778   kind = current_binding_level->kind;
779   if (kind == sk_cleanup)
780     {
781       tree stmt;
782
783       /* If this is a temporary binding created for a cleanup, then we'll
784          have pushed a statement list level.  Pop that, create a new
785          BIND_EXPR for the block, and insert it into the stream.  */
786       stmt = pop_stmt_list (current_binding_level->statement_list);
787       stmt = c_build_bind_expr (input_location, block, stmt);
788       add_stmt (stmt);
789     }
790
791   leave_scope ();
792   if (functionbody)
793     {
794       /* The current function is being defined, so its DECL_INITIAL
795          should be error_mark_node.  */
796       gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
797       DECL_INITIAL (current_function_decl) = block;
798     }
799   else if (block)
800     current_binding_level->blocks
801       = block_chainon (current_binding_level->blocks, block);
802
803   /* If we did not make a block for the level just exited,
804      any blocks made for inner levels
805      (since they cannot be recorded as subblocks in that level)
806      must be carried forward so they will later become subblocks
807      of something else.  */
808   else if (subblocks)
809     current_binding_level->blocks
810       = block_chainon (current_binding_level->blocks, subblocks);
811
812   /* Each and every BLOCK node created here in `poplevel' is important
813      (e.g. for proper debugging information) so if we created one
814      earlier, mark it as "used".  */
815   if (block)
816     TREE_USED (block) = 1;
817
818   /* All temporary bindings created for cleanups are popped silently.  */
819   if (kind == sk_cleanup)
820     goto restart;
821
822   timevar_stop (TV_NAME_LOOKUP);
823   return block;
824 }
825
826 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
827    itself, calling F for each.  The DATA is passed to F as well.  */
828
829 static int
830 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
831 {
832   int result = 0;
833   tree current = NAMESPACE_LEVEL (name_space)->namespaces;
834
835   result |= (*f) (name_space, data);
836
837   for (; current; current = DECL_CHAIN (current))
838     result |= walk_namespaces_r (current, f, data);
839
840   return result;
841 }
842
843 /* Walk all the namespaces, calling F for each.  The DATA is passed to
844    F as well.  */
845
846 int
847 walk_namespaces (walk_namespaces_fn f, void* data)
848 {
849   return walk_namespaces_r (global_namespace, f, data);
850 }
851
852 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
853    DATA is non-NULL, this is the last time we will call
854    wrapup_global_declarations for this NAMESPACE.  */
855
856 int
857 wrapup_globals_for_namespace (tree name_space, void* data)
858 {
859   cp_binding_level *level = NAMESPACE_LEVEL (name_space);
860   VEC(tree,gc) *statics = level->static_decls;
861   tree *vec = VEC_address (tree, statics);
862   int len = VEC_length (tree, statics);
863   int last_time = (data != 0);
864
865   if (last_time)
866     {
867       check_global_declarations (vec, len);
868       emit_debug_global_declarations (vec, len);
869       return 0;
870     }
871
872   /* Write out any globals that need to be output.  */
873   return wrapup_global_declarations (vec, len);
874 }
875
876 \f
877 /* In C++, you don't have to write `struct S' to refer to `S'; you
878    can just use `S'.  We accomplish this by creating a TYPE_DECL as
879    if the user had written `typedef struct S S'.  Create and return
880    the TYPE_DECL for TYPE.  */
881
882 tree
883 create_implicit_typedef (tree name, tree type)
884 {
885   tree decl;
886
887   decl = build_decl (input_location, TYPE_DECL, name, type);
888   DECL_ARTIFICIAL (decl) = 1;
889   /* There are other implicit type declarations, like the one *within*
890      a class that allows you to write `S::S'.  We must distinguish
891      amongst these.  */
892   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
893   TYPE_NAME (type) = decl;
894   TYPE_STUB_DECL (type) = decl;
895
896   return decl;
897 }
898
899 /* Remember a local name for name-mangling purposes.  */
900
901 static void
902 push_local_name (tree decl)
903 {
904   size_t i, nelts;
905   tree t, name;
906
907   timevar_start (TV_NAME_LOOKUP);
908
909   name = DECL_NAME (decl);
910
911   nelts = VEC_length (tree, local_names);
912   for (i = 0; i < nelts; i++)
913     {
914       t = VEC_index (tree, local_names, i);
915       if (DECL_NAME (t) == name)
916         {
917           if (!DECL_LANG_SPECIFIC (decl))
918             retrofit_lang_decl (decl);
919           DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
920           if (DECL_DISCRIMINATOR_SET_P (t))
921             DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
922           else
923             DECL_DISCRIMINATOR (decl) = 1;
924
925           VEC_replace (tree, local_names, i, decl);
926           timevar_stop (TV_NAME_LOOKUP);
927           return;
928         }
929     }
930
931   VEC_safe_push (tree, gc, local_names, decl);
932   timevar_stop (TV_NAME_LOOKUP);
933 }
934 \f
935 /* Subroutine of duplicate_decls: return truthvalue of whether
936    or not types of these decls match.
937
938    For C++, we must compare the parameter list so that `int' can match
939    `int&' in a parameter position, but `int&' is not confused with
940    `const int&'.  */
941
942 int
943 decls_match (tree newdecl, tree olddecl)
944 {
945   int types_match;
946
947   if (newdecl == olddecl)
948     return 1;
949
950   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
951     /* If the two DECLs are not even the same kind of thing, we're not
952        interested in their types.  */
953     return 0;
954
955   gcc_assert (DECL_P (newdecl));
956
957   if (TREE_CODE (newdecl) == FUNCTION_DECL)
958     {
959       tree f1 = TREE_TYPE (newdecl);
960       tree f2 = TREE_TYPE (olddecl);
961       tree p1 = TYPE_ARG_TYPES (f1);
962       tree p2 = TYPE_ARG_TYPES (f2);
963
964       /* Specializations of different templates are different functions
965          even if they have the same type.  */
966       tree t1 = (DECL_USE_TEMPLATE (newdecl)
967                  ? DECL_TI_TEMPLATE (newdecl)
968                  : NULL_TREE);
969       tree t2 = (DECL_USE_TEMPLATE (olddecl)
970                  ? DECL_TI_TEMPLATE (olddecl)
971                  : NULL_TREE);
972       if (t1 != t2)
973         return 0;
974
975       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
976           && ! (DECL_EXTERN_C_P (newdecl)
977                 && DECL_EXTERN_C_P (olddecl)))
978         return 0;
979
980 #ifdef NO_IMPLICIT_EXTERN_C
981       /* A new declaration doesn't match a built-in one unless it
982          is also extern "C".  */
983       if (DECL_IS_BUILTIN (olddecl)
984           && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
985         return 0;
986 #endif
987
988       if (TREE_CODE (f1) != TREE_CODE (f2))
989         return 0;
990
991       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
992         {
993           if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
994               && (DECL_BUILT_IN (olddecl)
995 #ifndef NO_IMPLICIT_EXTERN_C
996                   || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
997                   || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
998 #endif
999               ))
1000             {
1001               types_match = self_promoting_args_p (p1);
1002               if (p1 == void_list_node)
1003                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1004             }
1005 #ifndef NO_IMPLICIT_EXTERN_C
1006           else if (!prototype_p (f1)
1007                    && (DECL_EXTERN_C_P (olddecl)
1008                        && DECL_IN_SYSTEM_HEADER (olddecl)
1009                        && !DECL_CLASS_SCOPE_P (olddecl))
1010                    && (DECL_EXTERN_C_P (newdecl)
1011                        && DECL_IN_SYSTEM_HEADER (newdecl)
1012                        && !DECL_CLASS_SCOPE_P (newdecl)))
1013             {
1014               types_match = self_promoting_args_p (p2);
1015               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1016             }
1017 #endif
1018           else
1019             types_match =
1020               compparms (p1, p2)
1021               && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1022                   || comp_type_attributes (TREE_TYPE (newdecl),
1023                                            TREE_TYPE (olddecl)) != 0);
1024         }
1025       else
1026         types_match = 0;
1027     }
1028   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1029     {
1030       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1031           != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1032         return 0;
1033
1034       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1035                                 DECL_TEMPLATE_PARMS (olddecl)))
1036         return 0;
1037
1038       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1039         types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1040                                    TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1041       else
1042         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1043                                    DECL_TEMPLATE_RESULT (newdecl));
1044     }
1045   else
1046     {
1047       /* Need to check scope for variable declaration (VAR_DECL).
1048          For typedef (TYPE_DECL), scope is ignored.  */
1049       if (TREE_CODE (newdecl) == VAR_DECL
1050           && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1051           /* [dcl.link]
1052              Two declarations for an object with C language linkage
1053              with the same name (ignoring the namespace that qualify
1054              it) that appear in different namespace scopes refer to
1055              the same object.  */
1056           && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1057         return 0;
1058
1059       if (TREE_TYPE (newdecl) == error_mark_node)
1060         types_match = TREE_TYPE (olddecl) == error_mark_node;
1061       else if (TREE_TYPE (olddecl) == NULL_TREE)
1062         types_match = TREE_TYPE (newdecl) == NULL_TREE;
1063       else if (TREE_TYPE (newdecl) == NULL_TREE)
1064         types_match = 0;
1065       else
1066         types_match = comptypes (TREE_TYPE (newdecl),
1067                                  TREE_TYPE (olddecl),
1068                                  COMPARE_REDECLARATION);
1069     }
1070
1071   return types_match;
1072 }
1073
1074 /* If NEWDECL is `static' and an `extern' was seen previously,
1075    warn about it.  OLDDECL is the previous declaration.
1076
1077    Note that this does not apply to the C++ case of declaring
1078    a variable `extern const' and then later `const'.
1079
1080    Don't complain about built-in functions, since they are beyond
1081    the user's control.  */
1082
1083 void
1084 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1085 {
1086   if (TREE_CODE (newdecl) == TYPE_DECL
1087       || TREE_CODE (newdecl) == TEMPLATE_DECL
1088       || TREE_CODE (newdecl) == CONST_DECL
1089       || TREE_CODE (newdecl) == NAMESPACE_DECL)
1090     return;
1091
1092   /* Don't get confused by static member functions; that's a different
1093      use of `static'.  */
1094   if (TREE_CODE (newdecl) == FUNCTION_DECL
1095       && DECL_STATIC_FUNCTION_P (newdecl))
1096     return;
1097
1098   /* If the old declaration was `static', or the new one isn't, then
1099      everything is OK.  */
1100   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1101     return;
1102
1103   /* It's OK to declare a builtin function as `static'.  */
1104   if (TREE_CODE (olddecl) == FUNCTION_DECL
1105       && DECL_ARTIFICIAL (olddecl))
1106     return;
1107
1108   permerror (input_location, "%qD was declared %<extern%> and later %<static%>", newdecl);
1109   permerror (input_location, "previous declaration of %q+D", olddecl);
1110 }
1111
1112 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1113    function templates.  If their exception specifications do not
1114    match, issue a diagnostic.  */
1115
1116 static void
1117 check_redeclaration_exception_specification (tree new_decl,
1118                                              tree old_decl)
1119 {
1120   tree new_type;
1121   tree old_type;
1122   tree new_exceptions;
1123   tree old_exceptions;
1124
1125   new_type = TREE_TYPE (new_decl);
1126   new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1127   old_type = TREE_TYPE (old_decl);
1128   old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1129
1130   /* [except.spec]
1131
1132      If any declaration of a function has an exception-specification,
1133      all declarations, including the definition and an explicit
1134      specialization, of that function shall have an
1135      exception-specification with the same set of type-ids.  */
1136   if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1137       && ! DECL_IS_BUILTIN (old_decl)
1138       && flag_exceptions
1139       && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1140     {
1141       error ("declaration of %qF has a different exception specifier",
1142              new_decl);
1143       error ("from previous declaration %q+F", old_decl);
1144     }
1145 }
1146
1147 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1148    Otherwise issue diagnostics.  */
1149
1150 static bool
1151 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1152 {
1153   old_decl = STRIP_TEMPLATE (old_decl);
1154   new_decl = STRIP_TEMPLATE (new_decl);
1155   if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1156       || !VAR_OR_FUNCTION_DECL_P (new_decl))
1157     return true;
1158   if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1159       == DECL_DECLARED_CONSTEXPR_P (new_decl))
1160     return true;
1161   if (TREE_CODE (old_decl) == FUNCTION_DECL && DECL_BUILT_IN (old_decl))
1162     {
1163       /* Hide a built-in declaration.  */
1164       DECL_DECLARED_CONSTEXPR_P (old_decl)
1165         = DECL_DECLARED_CONSTEXPR_P (new_decl);
1166       return true;
1167     }
1168   error ("redeclaration %qD differs in %<constexpr%>", new_decl);
1169   error ("from previous declaration %q+D", old_decl);
1170   return false;
1171 }
1172
1173 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn)                   \
1174                           && lookup_attribute ("gnu_inline",            \
1175                                                DECL_ATTRIBUTES (fn)))
1176
1177 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1178    If the redeclaration is invalid, a diagnostic is issued, and the
1179    error_mark_node is returned.  Otherwise, OLDDECL is returned.
1180
1181    If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1182    returned.
1183
1184    NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend.  */
1185
1186 tree
1187 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1188 {
1189   unsigned olddecl_uid = DECL_UID (olddecl);
1190   int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1191   int new_defines_function = 0;
1192   tree new_template_info;
1193
1194   if (newdecl == olddecl)
1195     return olddecl;
1196
1197   types_match = decls_match (newdecl, olddecl);
1198
1199   /* If either the type of the new decl or the type of the old decl is an
1200      error_mark_node, then that implies that we have already issued an
1201      error (earlier) for some bogus type specification, and in that case,
1202      it is rather pointless to harass the user with yet more error message
1203      about the same declaration, so just pretend the types match here.  */
1204   if (TREE_TYPE (newdecl) == error_mark_node
1205       || TREE_TYPE (olddecl) == error_mark_node)
1206     return error_mark_node;
1207
1208   if (UDLIT_OPER_P (DECL_NAME (newdecl))
1209       && UDLIT_OPER_P (DECL_NAME (olddecl)))
1210     {
1211       if (TREE_CODE (newdecl) == TEMPLATE_DECL
1212           && TREE_CODE (olddecl) != TEMPLATE_DECL
1213           && check_raw_literal_operator (olddecl))
1214         error ("literal operator template %q+D conflicts with"
1215                " raw literal operator %qD", newdecl, olddecl);
1216       else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1217                && TREE_CODE (olddecl) == TEMPLATE_DECL
1218                && check_raw_literal_operator (newdecl))
1219         error ("raw literal operator %q+D conflicts with"
1220                " literal operator template %qD", newdecl, olddecl);
1221     }
1222
1223   if (DECL_P (olddecl)
1224       && TREE_CODE (newdecl) == FUNCTION_DECL
1225       && TREE_CODE (olddecl) == FUNCTION_DECL
1226       && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1227     {
1228       if (DECL_DECLARED_INLINE_P (newdecl)
1229           && DECL_UNINLINABLE (newdecl)
1230           && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1231         /* Already warned elsewhere.  */;
1232       else if (DECL_DECLARED_INLINE_P (olddecl)
1233                && DECL_UNINLINABLE (olddecl)
1234                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1235         /* Already warned.  */;
1236       else if (DECL_DECLARED_INLINE_P (newdecl)
1237                && DECL_UNINLINABLE (olddecl)
1238                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1239         {
1240           warning (OPT_Wattributes, "function %q+D redeclared as inline",
1241                    newdecl);
1242           warning (OPT_Wattributes, "previous declaration of %q+D "
1243                    "with attribute noinline", olddecl);
1244         }
1245       else if (DECL_DECLARED_INLINE_P (olddecl)
1246                && DECL_UNINLINABLE (newdecl)
1247                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1248         {
1249           warning (OPT_Wattributes, "function %q+D redeclared with "
1250                    "attribute noinline", newdecl);
1251           warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1252                    olddecl);
1253         }
1254     }
1255
1256   /* Check for redeclaration and other discrepancies.  */
1257   if (TREE_CODE (olddecl) == FUNCTION_DECL
1258       && DECL_ARTIFICIAL (olddecl))
1259     {
1260       gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1261       if (TREE_CODE (newdecl) != FUNCTION_DECL)
1262         {
1263           /* Avoid warnings redeclaring built-ins which have not been
1264              explicitly declared.  */
1265           if (DECL_ANTICIPATED (olddecl))
1266             return NULL_TREE;
1267
1268           /* If you declare a built-in or predefined function name as static,
1269              the old definition is overridden, but optionally warn this was a
1270              bad choice of name.  */
1271           if (! TREE_PUBLIC (newdecl))
1272             {
1273               warning (OPT_Wshadow, 
1274                        DECL_BUILT_IN (olddecl)
1275                        ? G_("shadowing built-in function %q#D")
1276                        : G_("shadowing library function %q#D"), olddecl);
1277               /* Discard the old built-in function.  */
1278               return NULL_TREE;
1279             }
1280           /* If the built-in is not ansi, then programs can override
1281              it even globally without an error.  */
1282           else if (! DECL_BUILT_IN (olddecl))
1283             warning (0, "library function %q#D redeclared as non-function %q#D",
1284                      olddecl, newdecl);
1285           else
1286             {
1287               error ("declaration of %q#D", newdecl);
1288               error ("conflicts with built-in declaration %q#D",
1289                      olddecl);
1290             }
1291           return NULL_TREE;
1292         }
1293       else if (!types_match)
1294         {
1295           /* Avoid warnings redeclaring built-ins which have not been
1296              explicitly declared.  */
1297           if (DECL_ANTICIPATED (olddecl))
1298             {
1299               /* Deal with fileptr_type_node.  FILE type is not known
1300                  at the time we create the builtins.  */
1301               tree t1, t2;
1302
1303               for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1304                    t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1305                    t1 || t2;
1306                    t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1307                 if (!t1 || !t2)
1308                   break;
1309                 else if (TREE_VALUE (t2) == fileptr_type_node)
1310                   {
1311                     tree t = TREE_VALUE (t1);
1312
1313                     if (TREE_CODE (t) == POINTER_TYPE
1314                         && TYPE_NAME (TREE_TYPE (t))
1315                         && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1316                            == get_identifier ("FILE")
1317                         && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1318                       {
1319                         tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1320
1321                         TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1322                           = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1323                         types_match = decls_match (newdecl, olddecl);
1324                         if (types_match)
1325                           return duplicate_decls (newdecl, olddecl,
1326                                                   newdecl_is_friend);
1327                         TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1328                       }
1329                   }
1330                 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1331                   break;
1332             }
1333           else if ((DECL_EXTERN_C_P (newdecl)
1334                     && DECL_EXTERN_C_P (olddecl))
1335                    || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1336                                  TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1337             {
1338               /* A near match; override the builtin.  */
1339
1340               if (TREE_PUBLIC (newdecl))
1341                 {
1342                   warning (0, "new declaration %q#D", newdecl);
1343                   warning (0, "ambiguates built-in declaration %q#D",
1344                            olddecl);
1345                 }
1346               else
1347                 warning (OPT_Wshadow, 
1348                          DECL_BUILT_IN (olddecl)
1349                          ? G_("shadowing built-in function %q#D")
1350                          : G_("shadowing library function %q#D"), olddecl);
1351             }
1352           else
1353             /* Discard the old built-in function.  */
1354             return NULL_TREE;
1355
1356           /* Replace the old RTL to avoid problems with inlining.  */
1357           COPY_DECL_RTL (newdecl, olddecl);
1358         }
1359       /* Even if the types match, prefer the new declarations type for
1360          built-ins which have not been explicitly declared, for
1361          exception lists, etc...  */
1362       else if (DECL_IS_BUILTIN (olddecl))
1363         {
1364           tree type = TREE_TYPE (newdecl);
1365           tree attribs = (*targetm.merge_type_attributes)
1366             (TREE_TYPE (olddecl), type);
1367
1368           type = cp_build_type_attribute_variant (type, attribs);
1369           TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1370         }
1371
1372       /* If a function is explicitly declared "throw ()", propagate that to
1373          the corresponding builtin.  */
1374       if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1375           && DECL_ANTICIPATED (olddecl)
1376           && TREE_NOTHROW (newdecl)
1377           && !TREE_NOTHROW (olddecl))
1378         {
1379           enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1380           tree tmpdecl = builtin_decl_explicit (fncode);
1381           if (tmpdecl && tmpdecl != olddecl && types_match)
1382             TREE_NOTHROW (tmpdecl)  = 1;
1383         }
1384
1385       /* Whether or not the builtin can throw exceptions has no
1386          bearing on this declarator.  */
1387       TREE_NOTHROW (olddecl) = 0;
1388
1389       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1390         {
1391           /* If a builtin function is redeclared as `static', merge
1392              the declarations, but make the original one static.  */
1393           DECL_THIS_STATIC (olddecl) = 1;
1394           TREE_PUBLIC (olddecl) = 0;
1395
1396           /* Make the old declaration consistent with the new one so
1397              that all remnants of the builtin-ness of this function
1398              will be banished.  */
1399           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1400           COPY_DECL_RTL (newdecl, olddecl);
1401         }
1402     }
1403   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1404     {
1405       /* C++ Standard, 3.3, clause 4:
1406          "[Note: a namespace name or a class template name must be unique
1407          in its declarative region (7.3.2, clause 14). ]"  */
1408       if (TREE_CODE (olddecl) != NAMESPACE_DECL
1409           && TREE_CODE (newdecl) != NAMESPACE_DECL
1410           && (TREE_CODE (olddecl) != TEMPLATE_DECL
1411               || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1412           && (TREE_CODE (newdecl) != TEMPLATE_DECL
1413               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1414         {
1415           if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1416                && TREE_CODE (newdecl) != TYPE_DECL)
1417               || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1418                   && TREE_CODE (olddecl) != TYPE_DECL))
1419             {
1420               /* We do nothing special here, because C++ does such nasty
1421                  things with TYPE_DECLs.  Instead, just let the TYPE_DECL
1422                  get shadowed, and know that if we need to find a TYPE_DECL
1423                  for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1424                  slot of the identifier.  */
1425               return NULL_TREE;
1426             }
1427             
1428             if ((TREE_CODE (newdecl) == FUNCTION_DECL
1429                  && DECL_FUNCTION_TEMPLATE_P (olddecl))
1430                 || (TREE_CODE (olddecl) == FUNCTION_DECL
1431                     && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1432               return NULL_TREE;
1433         }
1434
1435       error ("%q#D redeclared as different kind of symbol", newdecl);
1436       if (TREE_CODE (olddecl) == TREE_LIST)
1437         olddecl = TREE_VALUE (olddecl);
1438       error ("previous declaration of %q+#D", olddecl);
1439
1440       return error_mark_node;
1441     }
1442   else if (!types_match)
1443     {
1444       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1445         /* These are certainly not duplicate declarations; they're
1446            from different scopes.  */
1447         return NULL_TREE;
1448
1449       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1450         {
1451           /* The name of a class template may not be declared to refer to
1452              any other template, class, function, object, namespace, value,
1453              or type in the same scope.  */
1454           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1455               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1456             {
1457               error ("declaration of template %q#D", newdecl);
1458               error ("conflicts with previous declaration %q+#D", olddecl);
1459             }
1460           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1461                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1462                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1463                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1464                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1465                                            DECL_TEMPLATE_PARMS (olddecl))
1466                    /* Template functions can be disambiguated by
1467                       return type.  */
1468                    && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1469                                    TREE_TYPE (TREE_TYPE (olddecl))))
1470             {
1471               error ("new declaration %q#D", newdecl);
1472               error ("ambiguates old declaration %q+#D", olddecl);
1473             }
1474           return NULL_TREE;
1475         }
1476       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1477         {
1478           if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1479             {
1480               error ("declaration of C function %q#D conflicts with",
1481                      newdecl);
1482               error ("previous declaration %q+#D here", olddecl);
1483               return NULL_TREE;
1484             }
1485           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1486                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1487             {
1488               error ("new declaration %q#D", newdecl);
1489               error ("ambiguates old declaration %q+#D", olddecl);
1490               return error_mark_node;
1491             }
1492           else
1493             return NULL_TREE;
1494         }
1495       else
1496         {
1497           error ("conflicting declaration %q#D", newdecl);
1498           error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1499           return error_mark_node;
1500         }
1501     }
1502   else if (TREE_CODE (newdecl) == FUNCTION_DECL
1503             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1504                  && (!DECL_TEMPLATE_INFO (newdecl)
1505                      || (DECL_TI_TEMPLATE (newdecl)
1506                          != DECL_TI_TEMPLATE (olddecl))))
1507                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1508                     && (!DECL_TEMPLATE_INFO (olddecl)
1509                         || (DECL_TI_TEMPLATE (olddecl)
1510                             != DECL_TI_TEMPLATE (newdecl))))))
1511     /* It's OK to have a template specialization and a non-template
1512        with the same type, or to have specializations of two
1513        different templates with the same type.  Note that if one is a
1514        specialization, and the other is an instantiation of the same
1515        template, that we do not exit at this point.  That situation
1516        can occur if we instantiate a template class, and then
1517        specialize one of its methods.  This situation is valid, but
1518        the declarations must be merged in the usual way.  */
1519     return NULL_TREE;
1520   else if (TREE_CODE (newdecl) == FUNCTION_DECL
1521            && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1522                 && !DECL_USE_TEMPLATE (newdecl))
1523                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1524                    && !DECL_USE_TEMPLATE (olddecl))))
1525     /* One of the declarations is a template instantiation, and the
1526        other is not a template at all.  That's OK.  */
1527     return NULL_TREE;
1528   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1529     {
1530       /* In [namespace.alias] we have:
1531
1532            In a declarative region, a namespace-alias-definition can be
1533            used to redefine a namespace-alias declared in that declarative
1534            region to refer only to the namespace to which it already
1535            refers.
1536
1537          Therefore, if we encounter a second alias directive for the same
1538          alias, we can just ignore the second directive.  */
1539       if (DECL_NAMESPACE_ALIAS (newdecl)
1540           && (DECL_NAMESPACE_ALIAS (newdecl)
1541               == DECL_NAMESPACE_ALIAS (olddecl)))
1542         return olddecl;
1543       /* [namespace.alias]
1544
1545          A namespace-name or namespace-alias shall not be declared as
1546          the name of any other entity in the same declarative region.
1547          A namespace-name defined at global scope shall not be
1548          declared as the name of any other entity in any global scope
1549          of the program.  */
1550       error ("declaration of namespace %qD conflicts with", newdecl);
1551       error ("previous declaration of namespace %q+D here", olddecl);
1552       return error_mark_node;
1553     }
1554   else
1555     {
1556       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1557       if (errmsg)
1558         {
1559           error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1560           if (DECL_NAME (olddecl) != NULL_TREE)
1561             error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1562                    ? G_("%q+#D previously defined here")
1563                    : G_("%q+#D previously declared here"), olddecl);
1564           return error_mark_node;
1565         }
1566       else if (TREE_CODE (olddecl) == FUNCTION_DECL
1567                && DECL_INITIAL (olddecl) != NULL_TREE
1568                && !prototype_p (TREE_TYPE (olddecl))
1569                && prototype_p (TREE_TYPE (newdecl)))
1570         {
1571           /* Prototype decl follows defn w/o prototype.  */
1572           warning_at (input_location, 0, "prototype for %q+#D", newdecl);
1573           warning_at (DECL_SOURCE_LOCATION (olddecl), 0,
1574                       "follows non-prototype definition here");
1575         }
1576       else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1577                 || TREE_CODE (olddecl) == VAR_DECL)
1578                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1579         {
1580           /* [dcl.link]
1581              If two declarations of the same function or object
1582              specify different linkage-specifications ..., the program
1583              is ill-formed.... Except for functions with C++ linkage,
1584              a function declaration without a linkage specification
1585              shall not precede the first linkage specification for
1586              that function.  A function can be declared without a
1587              linkage specification after an explicit linkage
1588              specification has been seen; the linkage explicitly
1589              specified in the earlier declaration is not affected by
1590              such a function declaration.
1591
1592              DR 563 raises the question why the restrictions on
1593              functions should not also apply to objects.  Older
1594              versions of G++ silently ignore the linkage-specification
1595              for this example:
1596
1597                namespace N { 
1598                  extern int i;
1599                  extern "C" int i;
1600                }
1601
1602              which is clearly wrong.  Therefore, we now treat objects
1603              like functions.  */
1604           if (current_lang_depth () == 0)
1605             {
1606               /* There is no explicit linkage-specification, so we use
1607                  the linkage from the previous declaration.  */
1608               if (!DECL_LANG_SPECIFIC (newdecl))
1609                 retrofit_lang_decl (newdecl);
1610               SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1611             }
1612           else
1613             {
1614               error ("previous declaration of %q+#D with %qL linkage",
1615                      olddecl, DECL_LANGUAGE (olddecl));
1616               error ("conflicts with new declaration with %qL linkage",
1617                      DECL_LANGUAGE (newdecl));
1618             }
1619         }
1620
1621       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1622         ;
1623       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1624         {
1625           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1626           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1627           int i = 1;
1628
1629           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1630             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1631
1632           for (; t1 && t1 != void_list_node;
1633                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1634             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1635               {
1636                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1637                                            TREE_PURPOSE (t2)))
1638                   {
1639                     permerror (input_location, "default argument given for parameter %d of %q#D",
1640                                i, newdecl);
1641                     permerror (input_location, "after previous specification in %q+#D", olddecl);
1642                   }
1643                 else
1644                   {
1645                     error ("default argument given for parameter %d of %q#D",
1646                            i, newdecl);
1647                     error ("after previous specification in %q+#D",
1648                                  olddecl);
1649                   }
1650               }
1651         }
1652     }
1653
1654   /* Do not merge an implicit typedef with an explicit one.  In:
1655
1656        class A;
1657        ...
1658        typedef class A A __attribute__ ((foo));
1659
1660      the attribute should apply only to the typedef.  */
1661   if (TREE_CODE (olddecl) == TYPE_DECL
1662       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1663           || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1664     return NULL_TREE;
1665
1666   /* If new decl is `static' and an `extern' was seen previously,
1667      warn about it.  */
1668   warn_extern_redeclared_static (newdecl, olddecl);
1669
1670   if (!validate_constexpr_redeclaration (olddecl, newdecl))
1671     return error_mark_node;
1672
1673   /* We have committed to returning 1 at this point.  */
1674   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1675     {
1676       /* Now that functions must hold information normally held
1677          by field decls, there is extra work to do so that
1678          declaration information does not get destroyed during
1679          definition.  */
1680       if (DECL_VINDEX (olddecl))
1681         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1682       if (DECL_CONTEXT (olddecl))
1683         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1684       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1685       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1686       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1687       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1688       DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1689       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1690       if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1691         SET_OVERLOADED_OPERATOR_CODE
1692           (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1693       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1694
1695       /* Optionally warn about more than one declaration for the same
1696          name, but don't warn about a function declaration followed by a
1697          definition.  */
1698       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1699           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1700           /* Don't warn about extern decl followed by definition.  */
1701           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1702           /* Don't warn about friends, let add_friend take care of it.  */
1703           && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1704           /* Don't warn about declaration followed by specialization.  */
1705           && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1706               || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1707         {
1708           warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1709           warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1710         }
1711
1712       if (DECL_DELETED_FN (newdecl))
1713         {
1714           error ("deleted definition of %qD", newdecl);
1715           error ("after previous declaration %q+D", olddecl);
1716         }
1717       DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1718     }
1719
1720   /* Deal with C++: must preserve virtual function table size.  */
1721   if (TREE_CODE (olddecl) == TYPE_DECL)
1722     {
1723       tree newtype = TREE_TYPE (newdecl);
1724       tree oldtype = TREE_TYPE (olddecl);
1725
1726       if (newtype != error_mark_node && oldtype != error_mark_node
1727           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1728         CLASSTYPE_FRIEND_CLASSES (newtype)
1729           = CLASSTYPE_FRIEND_CLASSES (oldtype);
1730
1731       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1732     }
1733
1734   /* Copy all the DECL_... slots specified in the new decl
1735      except for any that we copy here from the old type.  */
1736   DECL_ATTRIBUTES (newdecl)
1737     = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1738
1739   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1740     {
1741       tree old_result;
1742       tree new_result;
1743       old_result = DECL_TEMPLATE_RESULT (olddecl);
1744       new_result = DECL_TEMPLATE_RESULT (newdecl);
1745       TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1746       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1747         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1748                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1749
1750       DECL_ATTRIBUTES (old_result)
1751         = (*targetm.merge_decl_attributes) (old_result, new_result);
1752
1753       if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1754         {
1755           if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1756               && DECL_INITIAL (new_result))
1757             {
1758               if (DECL_INITIAL (old_result))
1759                 DECL_UNINLINABLE (old_result) = 1;
1760               else
1761                 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1762               DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1763               DECL_NOT_REALLY_EXTERN (old_result)
1764                 = DECL_NOT_REALLY_EXTERN (new_result);
1765               DECL_INTERFACE_KNOWN (old_result)
1766                 = DECL_INTERFACE_KNOWN (new_result);
1767               DECL_DECLARED_INLINE_P (old_result)
1768                 = DECL_DECLARED_INLINE_P (new_result);
1769               DECL_DISREGARD_INLINE_LIMITS (old_result)
1770                 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1771
1772             }
1773           else
1774             {
1775               DECL_DECLARED_INLINE_P (old_result)
1776                 |= DECL_DECLARED_INLINE_P (new_result);
1777               DECL_DISREGARD_INLINE_LIMITS (old_result)
1778                 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1779               check_redeclaration_exception_specification (newdecl, olddecl);
1780             }
1781         }
1782
1783       /* If the new declaration is a definition, update the file and
1784          line information on the declaration, and also make
1785          the old declaration the same definition.  */
1786       if (DECL_INITIAL (new_result) != NULL_TREE)
1787         {
1788           DECL_SOURCE_LOCATION (olddecl)
1789             = DECL_SOURCE_LOCATION (old_result)
1790             = DECL_SOURCE_LOCATION (newdecl);
1791           DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1792           if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1793             {
1794               tree parm;
1795               DECL_ARGUMENTS (old_result)
1796                 = DECL_ARGUMENTS (new_result);
1797               for (parm = DECL_ARGUMENTS (old_result); parm;
1798                    parm = DECL_CHAIN (parm))
1799                 DECL_CONTEXT (parm) = old_result;
1800             }
1801         }
1802
1803       return olddecl;
1804     }
1805
1806   if (types_match)
1807     {
1808       /* Automatically handles default parameters.  */
1809       tree oldtype = TREE_TYPE (olddecl);
1810       tree newtype;
1811
1812       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1813         maybe_instantiate_noexcept (olddecl);
1814
1815       /* Merge the data types specified in the two decls.  */
1816       newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1817
1818       /* For typedefs use the old type, as the new type's DECL_NAME points
1819          at newdecl, which will be ggc_freed.  */
1820       if (TREE_CODE (newdecl) == TYPE_DECL)
1821         newtype = oldtype;
1822
1823       if (TREE_CODE (newdecl) == VAR_DECL)
1824         {
1825           DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1826           DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1827           DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1828             |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1829           DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1830             |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1831
1832           /* Merge the threadprivate attribute from OLDDECL into NEWDECL.  */
1833           if (DECL_LANG_SPECIFIC (olddecl)
1834               && CP_DECL_THREADPRIVATE_P (olddecl))
1835             {
1836               /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed.  */
1837               if (!DECL_LANG_SPECIFIC (newdecl))
1838                 retrofit_lang_decl (newdecl);
1839
1840               DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1841               CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1842             }
1843         }
1844
1845       /* Do this after calling `merge_types' so that default
1846          parameters don't confuse us.  */
1847       else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1848         check_redeclaration_exception_specification (newdecl, olddecl);
1849       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1850
1851       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1852         check_default_args (newdecl);
1853
1854       /* Lay the type out, unless already done.  */
1855       if (! same_type_p (newtype, oldtype)
1856           && TREE_TYPE (newdecl) != error_mark_node
1857           && !(processing_template_decl && uses_template_parms (newdecl)))
1858         layout_type (TREE_TYPE (newdecl));
1859
1860       if ((TREE_CODE (newdecl) == VAR_DECL
1861            || TREE_CODE (newdecl) == PARM_DECL
1862            || TREE_CODE (newdecl) == RESULT_DECL
1863            || TREE_CODE (newdecl) == FIELD_DECL
1864            || TREE_CODE (newdecl) == TYPE_DECL)
1865           && !(processing_template_decl && uses_template_parms (newdecl)))
1866         layout_decl (newdecl, 0);
1867
1868       /* Merge the type qualifiers.  */
1869       if (TREE_READONLY (newdecl))
1870         TREE_READONLY (olddecl) = 1;
1871       if (TREE_THIS_VOLATILE (newdecl))
1872         TREE_THIS_VOLATILE (olddecl) = 1;
1873       if (TREE_NOTHROW (newdecl))
1874         TREE_NOTHROW (olddecl) = 1;
1875
1876       /* Merge deprecatedness.  */
1877       if (TREE_DEPRECATED (newdecl))
1878         TREE_DEPRECATED (olddecl) = 1;
1879
1880       /* Preserve function specific target and optimization options */
1881       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1882         {
1883           if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1884               && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1885             DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1886               = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1887
1888           if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1889               && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1890             DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1891               = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1892         }
1893
1894       /* Merge the initialization information.  */
1895       if (DECL_INITIAL (newdecl) == NULL_TREE
1896           && DECL_INITIAL (olddecl) != NULL_TREE)
1897         {
1898           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1899           DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1900           if (TREE_CODE (newdecl) == FUNCTION_DECL)
1901             {
1902               DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1903               DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1904             }
1905         }
1906
1907       /* Merge the section attribute.
1908          We want to issue an error if the sections conflict but that must be
1909          done later in decl_attributes since we are called before attributes
1910          are assigned.  */
1911       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1912         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1913
1914       if (TREE_CODE (newdecl) == FUNCTION_DECL)
1915         {
1916           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1917             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1918           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1919           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1920           TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1921           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1922           DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1923           DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1924           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1925           DECL_LOOPING_CONST_OR_PURE_P (newdecl) 
1926             |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
1927           /* Keep the old RTL.  */
1928           COPY_DECL_RTL (olddecl, newdecl);
1929         }
1930       else if (TREE_CODE (newdecl) == VAR_DECL
1931                && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1932         {
1933           /* Keep the old RTL.  We cannot keep the old RTL if the old
1934              declaration was for an incomplete object and the new
1935              declaration is not since many attributes of the RTL will
1936              change.  */
1937           COPY_DECL_RTL (olddecl, newdecl);
1938         }
1939     }
1940   /* If cannot merge, then use the new type and qualifiers,
1941      and don't preserve the old rtl.  */
1942   else
1943     {
1944       /* Clean out any memory we had of the old declaration.  */
1945       tree oldstatic = value_member (olddecl, static_aggregates);
1946       if (oldstatic)
1947         TREE_VALUE (oldstatic) = error_mark_node;
1948
1949       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1950       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1951       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1952       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1953     }
1954
1955   /* Merge the storage class information.  */
1956   merge_weak (newdecl, olddecl);
1957
1958   if (DECL_ONE_ONLY (olddecl))
1959     DECL_COMDAT_GROUP (newdecl) = DECL_COMDAT_GROUP (olddecl);
1960
1961   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1962   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1963   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1964   if (! DECL_EXTERNAL (olddecl))
1965     DECL_EXTERNAL (newdecl) = 0;
1966
1967   new_template_info = NULL_TREE;
1968   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1969     {
1970       bool new_redefines_gnu_inline = false;
1971
1972       if (new_defines_function
1973           && ((DECL_INTERFACE_KNOWN (olddecl)
1974                && TREE_CODE (olddecl) == FUNCTION_DECL)
1975               || (TREE_CODE (olddecl) == TEMPLATE_DECL
1976                   && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1977                       == FUNCTION_DECL))))
1978         {
1979           tree fn = olddecl;
1980
1981           if (TREE_CODE (fn) == TEMPLATE_DECL)
1982             fn = DECL_TEMPLATE_RESULT (olddecl);
1983
1984           new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
1985         }
1986
1987       if (!new_redefines_gnu_inline)
1988         {
1989           DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1990           DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1991           DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1992         }
1993       DECL_TEMPLATE_INSTANTIATED (newdecl)
1994         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1995       DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
1996
1997       /* If the OLDDECL is an instantiation and/or specialization,
1998          then the NEWDECL must be too.  But, it may not yet be marked
1999          as such if the caller has created NEWDECL, but has not yet
2000          figured out that it is a redeclaration.  */
2001       if (!DECL_USE_TEMPLATE (newdecl))
2002         DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2003
2004       /* Don't really know how much of the language-specific
2005          values we should copy from old to new.  */
2006       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2007       DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2008       DECL_INITIALIZED_IN_CLASS_P (newdecl)
2009         |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2010
2011       if (LANG_DECL_HAS_MIN (newdecl))
2012         {
2013           DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2014             DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2015           if (DECL_TEMPLATE_INFO (newdecl))
2016             new_template_info = DECL_TEMPLATE_INFO (newdecl);
2017           DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2018         }
2019       /* Only functions have these fields.  */
2020       if (TREE_CODE (newdecl) == FUNCTION_DECL
2021           || DECL_FUNCTION_TEMPLATE_P (newdecl))
2022         {
2023           DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2024           olddecl_friend = DECL_FRIEND_P (olddecl);
2025           hidden_friend = (DECL_ANTICIPATED (olddecl)
2026                            && DECL_HIDDEN_FRIEND_P (olddecl)
2027                            && newdecl_is_friend);
2028           DECL_BEFRIENDING_CLASSES (newdecl)
2029             = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2030                        DECL_BEFRIENDING_CLASSES (olddecl));
2031           /* DECL_THUNKS is only valid for virtual functions,
2032              otherwise it is a DECL_FRIEND_CONTEXT.  */
2033           if (DECL_VIRTUAL_P (newdecl))
2034             DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
2035         }
2036       /* Only variables have this field.  */
2037       else if (TREE_CODE (newdecl) == VAR_DECL
2038                && VAR_HAD_UNKNOWN_BOUND (olddecl))
2039         SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2040     }
2041
2042   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2043     {
2044       tree parm;
2045
2046       /* Merge parameter attributes. */
2047       tree oldarg, newarg;
2048       for (oldarg = DECL_ARGUMENTS(olddecl), 
2049                newarg = DECL_ARGUMENTS(newdecl);
2050            oldarg && newarg;
2051            oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2052           DECL_ATTRIBUTES (newarg)
2053               = (*targetm.merge_decl_attributes) (oldarg, newarg);
2054           DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2055       }
2056       
2057       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2058           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2059         {
2060           /* If newdecl is not a specialization, then it is not a
2061              template-related function at all.  And that means that we
2062              should have exited above, returning 0.  */
2063           gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2064
2065           if (DECL_ODR_USED (olddecl))
2066             /* From [temp.expl.spec]:
2067
2068                If a template, a member template or the member of a class
2069                template is explicitly specialized then that
2070                specialization shall be declared before the first use of
2071                that specialization that would cause an implicit
2072                instantiation to take place, in every translation unit in
2073                which such a use occurs.  */
2074             error ("explicit specialization of %qD after first use",
2075                       olddecl);
2076
2077           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2078
2079           /* Don't propagate visibility from the template to the
2080              specialization here.  We'll do that in determine_visibility if
2081              appropriate.  */
2082           DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2083
2084           /* [temp.expl.spec/14] We don't inline explicit specialization
2085              just because the primary template says so.  */
2086
2087           /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2088              the always_inline attribute.  */
2089           if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2090               && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2091             {
2092               if (DECL_DECLARED_INLINE_P (newdecl))
2093                 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2094               else
2095                 DECL_ATTRIBUTES (newdecl)
2096                   = remove_attribute ("always_inline",
2097                                       DECL_ATTRIBUTES (newdecl));
2098             }
2099         }
2100       else if (new_defines_function && DECL_INITIAL (olddecl))
2101         {
2102           /* Never inline re-defined extern inline functions.
2103              FIXME: this could be better handled by keeping both
2104              function as separate declarations.  */
2105           DECL_UNINLINABLE (newdecl) = 1;
2106         }
2107       else
2108         {
2109           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2110             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2111
2112           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2113
2114           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2115             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2116
2117           DECL_DISREGARD_INLINE_LIMITS (newdecl)
2118             = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2119             = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2120                || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2121         }
2122
2123       /* Preserve abstractness on cloned [cd]tors.  */
2124       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
2125
2126       /* Update newdecl's parms to point at olddecl.  */
2127       for (parm = DECL_ARGUMENTS (newdecl); parm;
2128            parm = DECL_CHAIN (parm))
2129         DECL_CONTEXT (parm) = olddecl;
2130
2131       if (! types_match)
2132         {
2133           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2134           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2135           COPY_DECL_RTL (newdecl, olddecl);
2136         }
2137       if (! types_match || new_defines_function)
2138         {
2139           /* These need to be copied so that the names are available.
2140              Note that if the types do match, we'll preserve inline
2141              info and other bits, but if not, we won't.  */
2142           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2143           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2144         }
2145       if (new_defines_function)
2146         /* If defining a function declared with other language
2147            linkage, use the previously declared language linkage.  */
2148         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2149       else if (types_match)
2150         {
2151           /* If redeclaring a builtin function, and not a definition,
2152              it stays built in.  */
2153           if (DECL_BUILT_IN (olddecl))
2154             {
2155               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2156               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2157               /* If we're keeping the built-in definition, keep the rtl,
2158                  regardless of declaration matches.  */
2159               COPY_DECL_RTL (olddecl, newdecl);
2160               if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2161                 {
2162                   enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2163                   switch (fncode)
2164                     {
2165                       /* If a compatible prototype of these builtin functions
2166                          is seen, assume the runtime implements it with the
2167                          expected semantics.  */
2168                     case BUILT_IN_STPCPY:
2169                       if (builtin_decl_explicit_p (fncode))
2170                         set_builtin_decl_implicit_p (fncode, true);
2171                       break;
2172                     default:
2173                       break;
2174                     }
2175                 }
2176             }
2177
2178           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2179           /* Don't clear out the arguments if we're just redeclaring a
2180              function.  */
2181           if (DECL_ARGUMENTS (olddecl))
2182             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2183         }
2184     }
2185   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2186     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2187
2188   /* Now preserve various other info from the definition.  */
2189   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2190   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2191   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2192   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2193
2194   /* Warn about conflicting visibility specifications.  */
2195   if (DECL_VISIBILITY_SPECIFIED (olddecl)
2196       && DECL_VISIBILITY_SPECIFIED (newdecl)
2197       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2198     {
2199       warning_at (input_location, OPT_Wattributes,
2200                   "%q+D: visibility attribute ignored because it", newdecl);
2201       warning_at (DECL_SOURCE_LOCATION (olddecl), OPT_Wattributes,
2202                   "conflicts with previous declaration here");
2203     }
2204   /* Choose the declaration which specified visibility.  */
2205   if (DECL_VISIBILITY_SPECIFIED (olddecl))
2206     {
2207       DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2208       DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2209     }
2210   /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2211      so keep this behavior.  */
2212   if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2213     {
2214       SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2215       DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2216     }
2217   /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED.  */
2218   if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2219     {
2220       DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2221       DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2222     }
2223   DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2224   if (TREE_CODE (newdecl) == FIELD_DECL)
2225     DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2226
2227   /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2228      with that from NEWDECL below.  */
2229   if (DECL_LANG_SPECIFIC (olddecl))
2230     {
2231       gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2232                   != DECL_LANG_SPECIFIC (newdecl));
2233       ggc_free (DECL_LANG_SPECIFIC (olddecl));
2234     }
2235
2236   /* Merge the USED information.  */
2237   if (TREE_USED (olddecl))
2238     TREE_USED (newdecl) = 1;
2239   else if (TREE_USED (newdecl))
2240     TREE_USED (olddecl) = 1;
2241   if (TREE_CODE (newdecl) == VAR_DECL)
2242     {
2243       if (DECL_READ_P (olddecl))
2244         DECL_READ_P (newdecl) = 1;
2245       else if (DECL_READ_P (newdecl))
2246         DECL_READ_P (olddecl) = 1;
2247     }
2248   if (DECL_PRESERVE_P (olddecl))
2249     DECL_PRESERVE_P (newdecl) = 1;
2250   else if (DECL_PRESERVE_P (newdecl))
2251     DECL_PRESERVE_P (olddecl) = 1;
2252
2253   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2254     {
2255       int function_size;
2256
2257       function_size = sizeof (struct tree_decl_common);
2258
2259       memcpy ((char *) olddecl + sizeof (struct tree_common),
2260               (char *) newdecl + sizeof (struct tree_common),
2261               function_size - sizeof (struct tree_common));
2262
2263       memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2264               (char *) newdecl + sizeof (struct tree_decl_common),
2265               sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2266       if (new_template_info)
2267         /* If newdecl is a template instantiation, it is possible that
2268            the following sequence of events has occurred:
2269
2270            o A friend function was declared in a class template.  The
2271            class template was instantiated.
2272
2273            o The instantiation of the friend declaration was
2274            recorded on the instantiation list, and is newdecl.
2275
2276            o Later, however, instantiate_class_template called pushdecl
2277            on the newdecl to perform name injection.  But, pushdecl in
2278            turn called duplicate_decls when it discovered that another
2279            declaration of a global function with the same name already
2280            existed.
2281
2282            o Here, in duplicate_decls, we decided to clobber newdecl.
2283
2284            If we're going to do that, we'd better make sure that
2285            olddecl, and not newdecl, is on the list of
2286            instantiations so that if we try to do the instantiation
2287            again we won't get the clobbered declaration.  */
2288         reregister_specialization (newdecl,
2289                                    new_template_info,
2290                                    olddecl);
2291     }
2292   else
2293     {
2294       size_t size = tree_code_size (TREE_CODE (olddecl));
2295       memcpy ((char *) olddecl + sizeof (struct tree_common),
2296               (char *) newdecl + sizeof (struct tree_common),
2297               sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2298       switch (TREE_CODE (olddecl))
2299         {
2300         case LABEL_DECL:
2301         case VAR_DECL:
2302         case RESULT_DECL:
2303         case PARM_DECL:
2304         case FIELD_DECL:
2305         case TYPE_DECL:
2306         case CONST_DECL:
2307           {
2308             memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2309                     (char *) newdecl + sizeof (struct tree_decl_common),
2310                     size - sizeof (struct tree_decl_common)
2311                     + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2312           }
2313           break;
2314         default:
2315           memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2316                   (char *) newdecl + sizeof (struct tree_decl_common),
2317                   sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2318                   + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2319           break;
2320         }
2321     }
2322   DECL_UID (olddecl) = olddecl_uid;
2323   if (olddecl_friend)
2324     DECL_FRIEND_P (olddecl) = 1;
2325   if (hidden_friend)
2326     {
2327       DECL_ANTICIPATED (olddecl) = 1;
2328       DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2329     }
2330
2331   /* NEWDECL contains the merged attribute lists.
2332      Update OLDDECL to be the same.  */
2333   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2334
2335   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2336     so that encode_section_info has a chance to look at the new decl
2337     flags and attributes.  */
2338   if (DECL_RTL_SET_P (olddecl)
2339       && (TREE_CODE (olddecl) == FUNCTION_DECL
2340           || (TREE_CODE (olddecl) == VAR_DECL
2341               && TREE_STATIC (olddecl))))
2342     make_decl_rtl (olddecl);
2343
2344   /* The NEWDECL will no longer be needed.  Because every out-of-class
2345      declaration of a member results in a call to duplicate_decls,
2346      freeing these nodes represents in a significant savings.  */
2347   ggc_free (newdecl);
2348
2349   return olddecl;
2350 }
2351 \f
2352 /* Return zero if the declaration NEWDECL is valid
2353    when the declaration OLDDECL (assumed to be for the same name)
2354    has already been seen.
2355    Otherwise return an error message format string with a %s
2356    where the identifier should go.  */
2357
2358 static const char *
2359 redeclaration_error_message (tree newdecl, tree olddecl)
2360 {
2361   if (TREE_CODE (newdecl) == TYPE_DECL)
2362     {
2363       /* Because C++ can put things into name space for free,
2364          constructs like "typedef struct foo { ... } foo"
2365          would look like an erroneous redeclaration.  */
2366       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2367         return NULL;
2368       else
2369         return G_("redefinition of %q#D");
2370     }
2371   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2372     {
2373       /* If this is a pure function, its olddecl will actually be
2374          the original initialization to `0' (which we force to call
2375          abort()).  Don't complain about redefinition in this case.  */
2376       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2377           && DECL_INITIAL (olddecl) == NULL_TREE)
2378         return NULL;
2379
2380       /* If both functions come from different namespaces, this is not
2381          a redeclaration - this is a conflict with a used function.  */
2382       if (DECL_NAMESPACE_SCOPE_P (olddecl)
2383           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2384           && ! decls_match (olddecl, newdecl))
2385         return G_("%qD conflicts with used function");
2386
2387       /* We'll complain about linkage mismatches in
2388          warn_extern_redeclared_static.  */
2389
2390       /* Defining the same name twice is no good.  */
2391       if (DECL_INITIAL (olddecl) != NULL_TREE
2392           && DECL_INITIAL (newdecl) != NULL_TREE)
2393         {
2394           if (DECL_NAME (olddecl) == NULL_TREE)
2395             return G_("%q#D not declared in class");
2396           else if (!GNU_INLINE_P (olddecl)
2397                    || GNU_INLINE_P (newdecl))
2398             return G_("redefinition of %q#D");
2399         }
2400
2401       if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2402         {
2403           bool olda = GNU_INLINE_P (olddecl);
2404           bool newa = GNU_INLINE_P (newdecl);
2405
2406           if (olda != newa)
2407             {
2408               if (newa)
2409                 return G_("%q+D redeclared inline with "
2410                           "%<gnu_inline%> attribute");
2411               else
2412                 return G_("%q+D redeclared inline without "
2413                           "%<gnu_inline%> attribute");
2414             }
2415         }
2416
2417       return NULL;
2418     }
2419   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2420     {
2421       tree nt, ot;
2422
2423       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2424         {
2425           if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2426               && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2427             return G_("redefinition of %q#D");
2428           return NULL;
2429         }
2430
2431       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2432           || (DECL_TEMPLATE_RESULT (newdecl)
2433               == DECL_TEMPLATE_RESULT (olddecl)))
2434         return NULL;
2435
2436       nt = DECL_TEMPLATE_RESULT (newdecl);
2437       if (DECL_TEMPLATE_INFO (nt))
2438         nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2439       ot = DECL_TEMPLATE_RESULT (olddecl);
2440       if (DECL_TEMPLATE_INFO (ot))
2441         ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2442       if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2443           && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2444         return G_("redefinition of %q#D");
2445
2446       if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2447         {
2448           bool olda = GNU_INLINE_P (ot);
2449           bool newa = GNU_INLINE_P (nt);
2450
2451           if (olda != newa)
2452             {
2453               if (newa)
2454                 return G_("%q+D redeclared inline with "
2455                           "%<gnu_inline%> attribute");
2456               else
2457                 return G_("%q+D redeclared inline without "
2458                           "%<gnu_inline%> attribute");
2459             }
2460         }
2461
2462       /* Core issue #226 (C++0x): 
2463            
2464            If a friend function template declaration specifies a
2465            default template-argument, that declaration shall be a
2466            definition and shall be the only declaration of the
2467            function template in the translation unit.  */
2468       if ((cxx_dialect != cxx98) 
2469           && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2470           && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl), 
2471                                        /*is_primary=*/1, /*is_partial=*/0,
2472                                        /*is_friend_decl=*/2))
2473         return G_("redeclaration of friend %q#D "
2474                   "may not have default template arguments");
2475
2476       return NULL;
2477     }
2478   else if (TREE_CODE (newdecl) == VAR_DECL
2479            && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2480            && (! DECL_LANG_SPECIFIC (olddecl)
2481                || ! CP_DECL_THREADPRIVATE_P (olddecl)
2482                || DECL_THREAD_LOCAL_P (newdecl)))
2483     {
2484       /* Only variables can be thread-local, and all declarations must
2485          agree on this property.  */
2486       if (DECL_THREAD_LOCAL_P (newdecl))
2487         return G_("thread-local declaration of %q#D follows "
2488                   "non-thread-local declaration");
2489       else
2490         return G_("non-thread-local declaration of %q#D follows "
2491                   "thread-local declaration");
2492     }
2493   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2494     {
2495       /* The objects have been declared at namespace scope.  If either
2496          is a member of an anonymous union, then this is an invalid
2497          redeclaration.  For example:
2498
2499            int i;
2500            union { int i; };
2501
2502            is invalid.  */
2503       if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2504           || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
2505         return G_("redeclaration of %q#D");
2506       /* If at least one declaration is a reference, there is no
2507          conflict.  For example:
2508
2509            int i = 3;
2510            extern int i;
2511
2512          is valid.  */
2513       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2514         return NULL;
2515       /* Reject two definitions.  */
2516       return G_("redefinition of %q#D");
2517     }
2518   else
2519     {
2520       /* Objects declared with block scope:  */
2521       /* Reject two definitions, and reject a definition
2522          together with an external reference.  */
2523       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2524         return G_("redeclaration of %q#D");
2525       return NULL;
2526     }
2527 }
2528 \f
2529 /* Hash and equality functions for the named_label table.  */
2530
2531 static hashval_t
2532 named_label_entry_hash (const void *data)
2533 {
2534   const struct named_label_entry *ent = (const struct named_label_entry *) data;
2535   return DECL_UID (ent->label_decl);
2536 }
2537
2538 static int
2539 named_label_entry_eq (const void *a, const void *b)
2540 {
2541   const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2542   const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2543   return ent_a->label_decl == ent_b->label_decl;
2544 }
2545
2546 /* Create a new label, named ID.  */
2547
2548 static tree
2549 make_label_decl (tree id, int local_p)
2550 {
2551   struct named_label_entry *ent;
2552   void **slot;
2553   tree decl;
2554
2555   decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2556
2557   DECL_CONTEXT (decl) = current_function_decl;
2558   DECL_MODE (decl) = VOIDmode;
2559   C_DECLARED_LABEL_FLAG (decl) = local_p;
2560
2561   /* Say where one reference is to the label, for the sake of the
2562      error if it is not defined.  */
2563   DECL_SOURCE_LOCATION (decl) = input_location;
2564
2565   /* Record the fact that this identifier is bound to this label.  */
2566   SET_IDENTIFIER_LABEL_VALUE (id, decl);
2567
2568   /* Create the label htab for the function on demand.  */
2569   if (!named_labels)
2570     named_labels = htab_create_ggc (13, named_label_entry_hash,
2571                                     named_label_entry_eq, NULL);
2572
2573   /* Record this label on the list of labels used in this function.
2574      We do this before calling make_label_decl so that we get the
2575      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
2576   ent = ggc_alloc_cleared_named_label_entry ();
2577   ent->label_decl = decl;
2578
2579   slot = htab_find_slot (named_labels, ent, INSERT);
2580   gcc_assert (*slot == NULL);
2581   *slot = ent;
2582
2583   return decl;
2584 }
2585
2586 /* Look for a label named ID in the current function.  If one cannot
2587    be found, create one.  (We keep track of used, but undefined,
2588    labels, and complain about them at the end of a function.)  */
2589
2590 static tree
2591 lookup_label_1 (tree id)
2592 {
2593   tree decl;
2594
2595   /* You can't use labels at global scope.  */
2596   if (current_function_decl == NULL_TREE)
2597     {
2598       error ("label %qE referenced outside of any function", id);
2599       return NULL_TREE;
2600     }
2601
2602   /* See if we've already got this label.  */
2603   decl = IDENTIFIER_LABEL_VALUE (id);
2604   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2605     return decl;
2606
2607   decl = make_label_decl (id, /*local_p=*/0);
2608   return decl;
2609 }
2610
2611 /* Wrapper for lookup_label_1.  */
2612
2613 tree
2614 lookup_label (tree id)
2615 {
2616   tree ret;
2617   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2618   ret = lookup_label_1 (id);
2619   timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2620   return ret;
2621 }
2622
2623 /* Declare a local label named ID.  */
2624
2625 tree
2626 declare_local_label (tree id)
2627 {
2628   tree decl;
2629   cp_label_binding *bind;
2630
2631   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2632      this scope we can restore the old value of IDENTIFIER_TYPE_VALUE.  */
2633   bind = VEC_safe_push (cp_label_binding, gc,
2634                         current_binding_level->shadowed_labels, NULL);
2635   bind->prev_value = IDENTIFIER_LABEL_VALUE (id);
2636
2637   decl = make_label_decl (id, /*local_p=*/1);
2638   bind->label = decl;
2639
2640   return decl;
2641 }
2642
2643 /* Returns nonzero if it is ill-formed to jump past the declaration of
2644    DECL.  Returns 2 if it's also a real problem.  */
2645
2646 static int
2647 decl_jump_unsafe (tree decl)
2648 {
2649   /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2650      with automatic storage duration is not in scope to a point where it is
2651      in scope is ill-formed unless the variable has scalar type, class type
2652      with a trivial default constructor and a trivial destructor, a
2653      cv-qualified version of one of these types, or an array of one of the
2654      preceding types and is declared without an initializer (8.5).  */
2655   tree type = TREE_TYPE (decl);
2656
2657   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2658       || type == error_mark_node)
2659     return 0;
2660
2661   type = strip_array_types (type);
2662
2663   if (type_has_nontrivial_default_init (TREE_TYPE (decl))
2664       || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2665     return 2;
2666
2667   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2668     return 1;
2669
2670   return 0;
2671 }
2672
2673 /* A subroutine of check_previous_goto_1 to identify a branch to the user.  */
2674
2675 static void
2676 identify_goto (tree decl, const location_t *locus)
2677 {
2678   if (decl)
2679     permerror (input_location, "jump to label %qD", decl);
2680   else
2681     permerror (input_location, "jump to case label");
2682   if (locus)
2683     permerror (*locus, "  from here");
2684 }
2685
2686 /* Check that a single previously seen jump to a newly defined label
2687    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2688    the jump context; NAMES are the names in scope in LEVEL at the jump
2689    context; LOCUS is the source position of the jump or 0.  Returns
2690    true if all is well.  */
2691
2692 static bool
2693 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
2694                        bool exited_omp, const location_t *locus)
2695 {
2696   cp_binding_level *b;
2697   bool identified = false, saw_eh = false, saw_omp = false;
2698
2699   if (exited_omp)
2700     {
2701       identify_goto (decl, locus);
2702       error ("  exits OpenMP structured block");
2703       identified = saw_omp = true;
2704     }
2705
2706   for (b = current_binding_level; b ; b = b->level_chain)
2707     {
2708       tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2709
2710       for (new_decls = b->names; new_decls != old_decls;
2711            new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
2712                         : TREE_CHAIN (new_decls)))
2713         {
2714           int problem = decl_jump_unsafe (new_decls);
2715           if (! problem)
2716             continue;
2717
2718           if (!identified)
2719             {
2720               identify_goto (decl, locus);
2721               identified = true;
2722             }
2723           if (problem > 1)
2724             error ("  crosses initialization of %q+#D", new_decls);
2725           else
2726             permerror (input_location, "  enters scope of %q+#D which has "
2727                        "non-trivial destructor", new_decls);
2728         }
2729
2730       if (b == level)
2731         break;
2732       if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2733         {
2734           if (!identified)
2735             {
2736               identify_goto (decl, locus);
2737               identified = true;
2738             }
2739           if (b->kind == sk_try)
2740             error ("  enters try block");
2741           else
2742             error ("  enters catch block");
2743           saw_eh = true;
2744         }
2745       if (b->kind == sk_omp && !saw_omp)
2746         {
2747           if (!identified)
2748             {
2749               identify_goto (decl, locus);
2750               identified = true;
2751             }
2752           error ("  enters OpenMP structured block");
2753           saw_omp = true;
2754         }
2755     }
2756
2757   return !identified;
2758 }
2759
2760 static void
2761 check_previous_goto (tree decl, struct named_label_use_entry *use)
2762 {
2763   check_previous_goto_1 (decl, use->binding_level,
2764                          use->names_in_scope, use->in_omp_scope,
2765                          &use->o_goto_locus);
2766 }
2767
2768 static bool
2769 check_switch_goto (cp_binding_level* level)
2770 {
2771   return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2772 }
2773
2774 /* Check that a new jump to a label DECL is OK.  Called by
2775    finish_goto_stmt.  */
2776
2777 void
2778 check_goto (tree decl)
2779 {
2780   struct named_label_entry *ent, dummy;
2781   bool saw_catch = false, identified = false;
2782   tree bad;
2783   unsigned ix;
2784
2785   /* We can't know where a computed goto is jumping.
2786      So we assume that it's OK.  */
2787   if (TREE_CODE (decl) != LABEL_DECL)
2788     return;
2789
2790   /* We didn't record any information about this label when we created it,
2791      and there's not much point since it's trivial to analyze as a return.  */
2792   if (decl == cdtor_label)
2793     return;
2794
2795   dummy.label_decl = decl;
2796   ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2797   gcc_assert (ent != NULL);
2798
2799   /* If the label hasn't been defined yet, defer checking.  */
2800   if (! DECL_INITIAL (decl))
2801     {
2802       struct named_label_use_entry *new_use;
2803
2804       /* Don't bother creating another use if the last goto had the
2805          same data, and will therefore create the same set of errors.  */
2806       if (ent->uses
2807           && ent->uses->names_in_scope == current_binding_level->names)
2808         return;
2809
2810       new_use = ggc_alloc_named_label_use_entry ();
2811       new_use->binding_level = current_binding_level;
2812       new_use->names_in_scope = current_binding_level->names;
2813       new_use->o_goto_locus = input_location;
2814       new_use->in_omp_scope = false;
2815
2816       new_use->next = ent->uses;
2817       ent->uses = new_use;
2818       return;
2819     }
2820
2821   if (ent->in_try_scope || ent->in_catch_scope
2822       || ent->in_omp_scope || !VEC_empty (tree, ent->bad_decls))
2823     {
2824       permerror (input_location, "jump to label %q+D", decl);
2825       permerror (input_location, "  from here");
2826       identified = true;
2827     }
2828
2829   FOR_EACH_VEC_ELT (tree, ent->bad_decls, ix, bad)
2830     {
2831       int u = decl_jump_unsafe (bad);
2832
2833       if (u > 1 && DECL_ARTIFICIAL (bad))
2834         {
2835           /* Can't skip init of __exception_info.  */
2836           error_at (DECL_SOURCE_LOCATION (bad), "  enters catch block");
2837           saw_catch = true;
2838         }
2839       else if (u > 1)
2840         error ("  skips initialization of %q+#D", bad);
2841       else
2842         permerror (input_location, "  enters scope of %q+#D which has "
2843                    "non-trivial destructor", bad);
2844     }
2845
2846   if (ent->in_try_scope)
2847     error ("  enters try block");
2848   else if (ent->in_catch_scope && !saw_catch)
2849     error ("  enters catch block");
2850
2851   if (ent->in_omp_scope)
2852     error ("  enters OpenMP structured block");
2853   else if (flag_openmp)
2854     {
2855       cp_binding_level *b;
2856       for (b = current_binding_level; b ; b = b->level_chain)
2857         {
2858           if (b == ent->binding_level)
2859             break;
2860           if (b->kind == sk_omp)
2861             {
2862               if (!identified)
2863                 {
2864                   permerror (input_location, "jump to label %q+D", decl);
2865                   permerror (input_location, "  from here");
2866                   identified = true;
2867                 }
2868               error ("  exits OpenMP structured block");
2869               break;
2870             }
2871         }
2872     }
2873 }
2874
2875 /* Check that a return is ok wrt OpenMP structured blocks.
2876    Called by finish_return_stmt.  Returns true if all is well.  */
2877
2878 bool
2879 check_omp_return (void)
2880 {
2881   cp_binding_level *b;
2882   for (b = current_binding_level; b ; b = b->level_chain)
2883     if (b->kind == sk_omp)
2884       {
2885         error ("invalid exit from OpenMP structured block");
2886         return false;
2887       }
2888     else if (b->kind == sk_function_parms)
2889       break;
2890   return true;
2891 }
2892
2893 /* Define a label, specifying the location in the source file.
2894    Return the LABEL_DECL node for the label.  */
2895
2896 static tree
2897 define_label_1 (location_t location, tree name)
2898 {
2899   struct named_label_entry *ent, dummy;
2900   cp_binding_level *p;
2901   tree decl;
2902
2903   decl = lookup_label (name);
2904
2905   dummy.label_decl = decl;
2906   ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2907   gcc_assert (ent != NULL);
2908
2909   /* After labels, make any new cleanups in the function go into their
2910      own new (temporary) binding contour.  */
2911   for (p = current_binding_level;
2912        p->kind != sk_function_parms;
2913        p = p->level_chain)
2914     p->more_cleanups_ok = 0;
2915
2916   if (name == get_identifier ("wchar_t"))
2917     permerror (input_location, "label named wchar_t");
2918
2919   if (DECL_INITIAL (decl) != NULL_TREE)
2920     {
2921       error ("duplicate label %qD", decl);
2922       return error_mark_node;
2923     }
2924   else
2925     {
2926       struct named_label_use_entry *use;
2927
2928       /* Mark label as having been defined.  */
2929       DECL_INITIAL (decl) = error_mark_node;
2930       /* Say where in the source.  */
2931       DECL_SOURCE_LOCATION (decl) = location;
2932
2933       ent->binding_level = current_binding_level;
2934       ent->names_in_scope = current_binding_level->names;
2935
2936       for (use = ent->uses; use ; use = use->next)
2937         check_previous_goto (decl, use);
2938       ent->uses = NULL;
2939     }
2940
2941   return decl;
2942 }
2943
2944 /* Wrapper for define_label_1.  */
2945
2946 tree
2947 define_label (location_t location, tree name)
2948 {
2949   tree ret;
2950   bool running = timevar_cond_start (TV_NAME_LOOKUP);
2951   ret = define_label_1 (location, name);
2952   timevar_cond_stop (TV_NAME_LOOKUP, running);
2953   return ret;
2954 }
2955
2956
2957 struct cp_switch
2958 {
2959   cp_binding_level *level;
2960   struct cp_switch *next;
2961   /* The SWITCH_STMT being built.  */
2962   tree switch_stmt;
2963   /* A splay-tree mapping the low element of a case range to the high
2964      element, or NULL_TREE if there is no high element.  Used to
2965      determine whether or not a new case label duplicates an old case
2966      label.  We need a tree, rather than simply a hash table, because
2967      of the GNU case range extension.  */
2968   splay_tree cases;
2969 };
2970
2971 /* A stack of the currently active switch statements.  The innermost
2972    switch statement is on the top of the stack.  There is no need to
2973    mark the stack for garbage collection because it is only active
2974    during the processing of the body of a function, and we never
2975    collect at that point.  */
2976
2977 static struct cp_switch *switch_stack;
2978
2979 /* Called right after a switch-statement condition is parsed.
2980    SWITCH_STMT is the switch statement being parsed.  */
2981
2982 void
2983 push_switch (tree switch_stmt)
2984 {
2985   struct cp_switch *p = XNEW (struct cp_switch);
2986   p->level = current_binding_level;
2987   p->next = switch_stack;
2988   p->switch_stmt = switch_stmt;
2989   p->cases = splay_tree_new (case_compare, NULL, NULL);
2990   switch_stack = p;
2991 }
2992
2993 void
2994 pop_switch (void)
2995 {
2996   struct cp_switch *cs = switch_stack;
2997   location_t switch_location;
2998
2999   /* Emit warnings as needed.  */
3000   switch_location = EXPR_LOC_OR_HERE (cs->switch_stmt);
3001   if (!processing_template_decl)
3002     c_do_switch_warnings (cs->cases, switch_location,
3003                           SWITCH_STMT_TYPE (cs->switch_stmt),
3004                           SWITCH_STMT_COND (cs->switch_stmt));
3005
3006   splay_tree_delete (cs->cases);
3007   switch_stack = switch_stack->next;
3008   free (cs);
3009 }
3010
3011 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3012    condition.  Note that if TYPE and VALUE are already integral we don't
3013    really do the conversion because the language-independent
3014    warning/optimization code will work better that way.  */
3015
3016 static tree
3017 case_conversion (tree type, tree value)
3018 {
3019   if (value == NULL_TREE)
3020     return value;
3021
3022   if (cxx_dialect >= cxx0x
3023       && (SCOPED_ENUM_P (type)
3024           || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3025     {
3026       if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3027         type = type_promotes_to (type);
3028       value = perform_implicit_conversion (type, value, tf_warning_or_error);
3029     }
3030   return cxx_constant_value (value);
3031 }
3032
3033 /* Note that we've seen a definition of a case label, and complain if this
3034    is a bad place for one.  */
3035
3036 tree
3037 finish_case_label (location_t loc, tree low_value, tree high_value)
3038 {
3039   tree cond, r;
3040   cp_binding_level *p;
3041   tree type;
3042
3043   if (processing_template_decl)
3044     {
3045       tree label;
3046
3047       /* For templates, just add the case label; we'll do semantic
3048          analysis at instantiation-time.  */
3049       label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3050       return add_stmt (build_case_label (low_value, high_value, label));
3051     }
3052
3053   /* Find the condition on which this switch statement depends.  */
3054   cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3055   if (cond && TREE_CODE (cond) == TREE_LIST)
3056     cond = TREE_VALUE (cond);
3057
3058   if (!check_switch_goto (switch_stack->level))
3059     return error_mark_node;
3060
3061   type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3062
3063   low_value = case_conversion (type, low_value);
3064   high_value = case_conversion (type, high_value);
3065
3066   r = c_add_case_label (loc, switch_stack->cases, cond, type,
3067                         low_value, high_value);
3068
3069   /* After labels, make any new cleanups in the function go into their
3070      own new (temporary) binding contour.  */
3071   for (p = current_binding_level;
3072        p->kind != sk_function_parms;
3073        p = p->level_chain)
3074     p->more_cleanups_ok = 0;
3075
3076   return r;
3077 }
3078 \f
3079 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
3080
3081 static hashval_t
3082 typename_hash (const void* k)
3083 {
3084   hashval_t hash;
3085   const_tree const t = (const_tree) k;
3086
3087   hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3088           ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
3089
3090   return hash;
3091 }
3092
3093 typedef struct typename_info {
3094   tree scope;
3095   tree name;
3096   tree template_id;
3097   bool enum_p;
3098   bool class_p;
3099 } typename_info;
3100
3101 /* Compare two TYPENAME_TYPEs.  K1 is really of type `tree', K2 is
3102    really of type `typename_info*'  */
3103
3104 static int
3105 typename_compare (const void * k1, const void * k2)
3106 {
3107   const_tree const t1 = (const_tree) k1;
3108   const typename_info *const t2 = (const typename_info *) k2;
3109
3110   return (DECL_NAME (TYPE_NAME (t1)) == t2->name
3111           && TYPE_CONTEXT (t1) == t2->scope
3112           && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3113           && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3114           && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3115 }
3116
3117 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
3118    the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3119
3120    Returns the new TYPENAME_TYPE.  */
3121
3122 static GTY ((param_is (union tree_node))) htab_t typename_htab;
3123
3124 static tree
3125 build_typename_type (tree context, tree name, tree fullname,
3126                      enum tag_types tag_type)
3127 {
3128   tree t;
3129   tree d;
3130   typename_info ti;
3131   void **e;
3132   hashval_t hash;
3133
3134   if (typename_htab == NULL)
3135     typename_htab = htab_create_ggc (61, &typename_hash,
3136                                      &typename_compare, NULL);
3137
3138   ti.scope = FROB_CONTEXT (context);
3139   ti.name = name;
3140   ti.template_id = fullname;
3141   ti.enum_p = tag_type == enum_type;
3142   ti.class_p = (tag_type == class_type
3143                 || tag_type == record_type
3144                 || tag_type == union_type);
3145   hash =  (htab_hash_pointer (ti.scope)
3146            ^ htab_hash_pointer (ti.name));
3147
3148   /* See if we already have this type.  */
3149   e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
3150   if (*e)
3151     t = (tree) *e;
3152   else
3153     {
3154       /* Build the TYPENAME_TYPE.  */
3155       t = cxx_make_type (TYPENAME_TYPE);
3156       TYPE_CONTEXT (t) = ti.scope;
3157       TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3158       TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3159       TYPENAME_IS_CLASS_P (t) = ti.class_p;
3160
3161       /* Build the corresponding TYPE_DECL.  */
3162       d = build_decl (input_location, TYPE_DECL, name, t);
3163       TYPE_NAME (TREE_TYPE (d)) = d;
3164       TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3165       DECL_CONTEXT (d) = FROB_CONTEXT (context);
3166       DECL_ARTIFICIAL (d) = 1;
3167
3168       /* Store it in the hash table.  */
3169       *e = t;
3170
3171       /* TYPENAME_TYPEs must always be compared structurally, because
3172          they may or may not resolve down to another type depending on
3173          the currently open classes. */
3174       SET_TYPE_STRUCTURAL_EQUALITY (t);
3175     }
3176
3177   return t;
3178 }
3179
3180 /* Resolve `typename CONTEXT::NAME'.  TAG_TYPE indicates the tag
3181    provided to name the type.  Returns an appropriate type, unless an
3182    error occurs, in which case error_mark_node is returned.  If we
3183    locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3184    return that, rather than the _TYPE it corresponds to, in other
3185    cases we look through the type decl.  If TF_ERROR is set, complain
3186    about errors, otherwise be quiet.  */
3187
3188 tree
3189 make_typename_type (tree context, tree name, enum tag_types tag_type,
3190                     tsubst_flags_t complain)
3191 {
3192   tree fullname;
3193   tree t;
3194   bool want_template;
3195
3196   if (name == error_mark_node
3197       || context == NULL_TREE
3198       || context == error_mark_node)
3199     return error_mark_node;
3200
3201   if (TYPE_P (name))
3202     {
3203       if (!(TYPE_LANG_SPECIFIC (name)
3204             && (CLASSTYPE_IS_TEMPLATE (name)
3205                 || CLASSTYPE_USE_TEMPLATE (name))))
3206         name = TYPE_IDENTIFIER (name);
3207       else
3208         /* Create a TEMPLATE_ID_EXPR for the type.  */
3209         name = build_nt (TEMPLATE_ID_EXPR,
3210                          CLASSTYPE_TI_TEMPLATE (name),
3211                          CLASSTYPE_TI_ARGS (name));
3212     }
3213   else if (TREE_CODE (name) == TYPE_DECL)
3214     name = DECL_NAME (name);
3215
3216   fullname = name;
3217
3218   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3219     {
3220       name = TREE_OPERAND (name, 0);
3221       if (TREE_CODE (name) == TEMPLATE_DECL)
3222         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3223       else if (TREE_CODE (name) == OVERLOAD)
3224         {
3225           error ("%qD is not a type", name);
3226           return error_mark_node;
3227         }
3228     }
3229   if (TREE_CODE (name) == TEMPLATE_DECL)
3230     {
3231       error ("%qD used without template parameters", name);
3232       return error_mark_node;
3233     }
3234   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3235   gcc_assert (TYPE_P (context));
3236
3237   if (!MAYBE_CLASS_TYPE_P (context))
3238     {
3239       if (complain & tf_error)
3240         error ("%q#T is not a class", context);
3241       return error_mark_node;
3242     }
3243   
3244   /* When the CONTEXT is a dependent type,  NAME could refer to a
3245      dependent base class of CONTEXT.  But look inside it anyway
3246      if CONTEXT is a currently open scope, in case it refers to a
3247      member of the current instantiation or a non-dependent base;
3248      lookup will stop when we hit a dependent base.  */
3249   if (!dependent_scope_p (context))
3250     /* We should only set WANT_TYPE when we're a nested typename type.
3251        Then we can give better diagnostics if we find a non-type.  */
3252     t = lookup_field (context, name, 2, /*want_type=*/true);
3253   else
3254     t = NULL_TREE;
3255
3256   if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3257     return build_typename_type (context, name, fullname, tag_type);
3258
3259   want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3260   
3261   if (!t)
3262     {
3263       if (complain & tf_error)
3264         error (want_template ? G_("no class template named %q#T in %q#T")
3265                : G_("no type named %q#T in %q#T"), name, context);
3266       return error_mark_node;
3267     }
3268   
3269   /* Pull out the template from an injected-class-name (or multiple).  */
3270   if (want_template)
3271     t = maybe_get_template_decl_from_type_decl (t);
3272
3273   if (TREE_CODE (t) == TREE_LIST)
3274     {
3275       if (complain & tf_error)
3276         {
3277           error ("lookup of %qT in %qT is ambiguous", name, context);
3278           print_candidates (t);
3279         }
3280       return error_mark_node;
3281     }
3282
3283   if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3284     {
3285       if (complain & tf_error)
3286         error ("%<typename %T::%D%> names %q#T, which is not a class template",
3287                context, name, t);
3288       return error_mark_node;
3289     }
3290   if (!want_template && TREE_CODE (t) != TYPE_DECL)
3291     {
3292       if (complain & tf_error)
3293         error ("%<typename %T::%D%> names %q#T, which is not a type",
3294                context, name, t);
3295       return error_mark_node;
3296     }
3297   
3298   if (complain & tf_error)
3299     perform_or_defer_access_check (TYPE_BINFO (context), t, t);
3300
3301   /* If we are currently parsing a template and if T is a typedef accessed
3302      through CONTEXT then we need to remember and check access of T at
3303      template instantiation time.  */
3304   add_typedef_to_current_template_for_access_check (t, context, input_location);
3305
3306   if (want_template)
3307     return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3308                                   NULL_TREE, context,
3309                                   /*entering_scope=*/0,
3310                                   tf_warning_or_error | tf_user);
3311   
3312   if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3313     t = TREE_TYPE (t);
3314   
3315   return t;
3316 }
3317
3318 /* Resolve `CONTEXT::template NAME'.  Returns a TEMPLATE_DECL if the name
3319    can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3320    in which case error_mark_node is returned.
3321
3322    If PARM_LIST is non-NULL, also make sure that the template parameter
3323    list of TEMPLATE_DECL matches.
3324
3325    If COMPLAIN zero, don't complain about any errors that occur.  */
3326
3327 tree
3328 make_unbound_class_template (tree context, tree name, tree parm_list,
3329                              tsubst_flags_t complain)
3330 {
3331   tree t;
3332   tree d;
3333
3334   if (TYPE_P (name))
3335     name = TYPE_IDENTIFIER (name);
3336   else if (DECL_P (name))
3337     name = DECL_NAME (name);
3338   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3339
3340   if (!dependent_type_p (context)
3341       || currently_open_class (context))
3342     {
3343       tree tmpl = NULL_TREE;
3344
3345       if (MAYBE_CLASS_TYPE_P (context))
3346         tmpl = lookup_field (context, name, 0, false);
3347
3348       if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3349         tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3350
3351       if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3352         {
3353           if (complain & tf_error)
3354             error ("no class template named %q#T in %q#T", name, context);
3355           return error_mark_node;
3356         }
3357
3358       if (parm_list
3359           && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3360         {
3361           if (complain & tf_error)
3362             {
3363               error ("template parameters do not match template");
3364               error ("%q+D declared here", tmpl);
3365             }
3366           return error_mark_node;
3367         }
3368
3369       if (complain & tf_error)
3370         perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
3371
3372       return tmpl;
3373     }
3374
3375   /* Build the UNBOUND_CLASS_TEMPLATE.  */
3376   t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3377   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3378   TREE_TYPE (t) = NULL_TREE;
3379   SET_TYPE_STRUCTURAL_EQUALITY (t);
3380
3381   /* Build the corresponding TEMPLATE_DECL.  */
3382   d = build_decl (input_location, TEMPLATE_DECL, name, t);
3383   TYPE_NAME (TREE_TYPE (d)) = d;
3384   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3385   DECL_CONTEXT (d) = FROB_CONTEXT (context);
3386   DECL_ARTIFICIAL (d) = 1;
3387   DECL_TEMPLATE_PARMS (d) = parm_list;
3388
3389   return t;
3390 }
3391
3392 \f
3393
3394 /* Push the declarations of builtin types into the namespace.
3395    RID_INDEX is the index of the builtin type in the array
3396    RID_POINTERS.  NAME is the name used when looking up the builtin
3397    type.  TYPE is the _TYPE node for the builtin type.  */
3398
3399 void
3400 record_builtin_type (enum rid rid_index,
3401                      const char* name,
3402                      tree type)
3403 {
3404   tree rname = NULL_TREE, tname = NULL_TREE;
3405   tree tdecl = NULL_TREE;
3406
3407   if ((int) rid_index < (int) RID_MAX)
3408     rname = ridpointers[(int) rid_index];
3409   if (name)
3410     tname = get_identifier (name);
3411
3412   /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3413      eliminated.  Built-in types should not be looked up name; their
3414      names are keywords that the parser can recognize.  However, there
3415      is code in c-common.c that uses identifier_global_value to look
3416      up built-in types by name.  */
3417   if (tname)
3418     {
3419       tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3420       DECL_ARTIFICIAL (tdecl) = 1;
3421       SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3422     }
3423   if (rname)
3424     {
3425       if (!tdecl)
3426         {
3427           tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3428           DECL_ARTIFICIAL (tdecl) = 1;
3429         }
3430       SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3431     }
3432
3433   if (!TYPE_NAME (type))
3434     TYPE_NAME (type) = tdecl;
3435
3436   if (tdecl)
3437     debug_hooks->type_decl (tdecl, 0);
3438 }
3439
3440 /* Record one of the standard Java types.
3441  * Declare it as having the given NAME.
3442  * If SIZE > 0, it is the size of one of the integral types;
3443  * otherwise it is the negative of the size of one of the other types.  */
3444
3445 static tree
3446 record_builtin_java_type (const char* name, int size)
3447 {
3448   tree type, decl;
3449   if (size > 0)
3450     {
3451       type = build_nonstandard_integer_type (size, 0);
3452       type = build_distinct_type_copy (type);
3453     }
3454   else if (size > -32)
3455     {
3456       tree stype;
3457       /* "__java_char" or ""__java_boolean".  */
3458       type = build_nonstandard_integer_type (-size, 1);
3459       type = build_distinct_type_copy (type);
3460       /* Get the signed type cached and attached to the unsigned type,
3461          so it doesn't get garbage-collected at "random" times,
3462          causing potential codegen differences out of different UIDs
3463          and different alias set numbers.  */
3464       stype = build_nonstandard_integer_type (-size, 0);
3465       stype = build_distinct_type_copy (stype);
3466       TREE_CHAIN (type) = stype;
3467       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3468     }
3469   else
3470     { /* "__java_float" or ""__java_double".  */
3471       type = make_node (REAL_TYPE);
3472       TYPE_PRECISION (type) = - size;
3473       layout_type (type);
3474     }
3475   record_builtin_type (RID_MAX, name, type);
3476   decl = TYPE_NAME (type);
3477
3478   /* Suppress generate debug symbol entries for these types,
3479   &nbs