OSDN Git Service

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