OSDN Git Service

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