OSDN Git Service

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