OSDN Git Service

7a82ed6c511bad003215c4d37f8c86f5832f5ca8
[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       /* Only variables have this field.  */
1933       else if (TREE_CODE (newdecl) == VAR_DECL
1934                && VAR_HAD_UNKNOWN_BOUND (olddecl))
1935         SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
1936     }
1937
1938   if (TREE_CODE (newdecl) == FUNCTION_DECL)
1939     {
1940       tree parm;
1941
1942       /* Merge parameter attributes. */
1943       tree oldarg, newarg;
1944       for (oldarg = DECL_ARGUMENTS(olddecl), 
1945                newarg = DECL_ARGUMENTS(newdecl);
1946            oldarg && newarg;
1947            oldarg = TREE_CHAIN(oldarg), newarg = TREE_CHAIN(newarg)) {
1948           DECL_ATTRIBUTES (newarg)
1949               = (*targetm.merge_decl_attributes) (oldarg, newarg);
1950           DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
1951       }
1952       
1953       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1954           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1955         {
1956           /* If newdecl is not a specialization, then it is not a
1957              template-related function at all.  And that means that we
1958              should have exited above, returning 0.  */
1959           gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1960
1961           if (DECL_ODR_USED (olddecl))
1962             /* From [temp.expl.spec]:
1963
1964                If a template, a member template or the member of a class
1965                template is explicitly specialized then that
1966                specialization shall be declared before the first use of
1967                that specialization that would cause an implicit
1968                instantiation to take place, in every translation unit in
1969                which such a use occurs.  */
1970             error ("explicit specialization of %qD after first use",
1971                       olddecl);
1972
1973           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1974
1975           /* Don't propagate visibility from the template to the
1976              specialization here.  We'll do that in determine_visibility if
1977              appropriate.  */
1978           DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
1979
1980           /* [temp.expl.spec/14] We don't inline explicit specialization
1981              just because the primary template says so.  */
1982         }
1983       else if (new_defines_function && DECL_INITIAL (olddecl))
1984         {
1985           /* Never inline re-defined extern inline functions.
1986              FIXME: this could be better handled by keeping both
1987              function as separate declarations.  */
1988           DECL_UNINLINABLE (newdecl) = 1;
1989         }
1990       else
1991         {
1992           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1993             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1994
1995           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1996
1997           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1998             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1999
2000           DECL_DISREGARD_INLINE_LIMITS (newdecl)
2001             = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2002             = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2003                || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2004         }
2005
2006       /* Preserve abstractness on cloned [cd]tors.  */
2007       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
2008
2009       /* Update newdecl's parms to point at olddecl.  */
2010       for (parm = DECL_ARGUMENTS (newdecl); parm;
2011            parm = TREE_CHAIN (parm))
2012         DECL_CONTEXT (parm) = olddecl;
2013
2014       if (! types_match)
2015         {
2016           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2017           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2018           COPY_DECL_RTL (newdecl, olddecl);
2019         }
2020       if (! types_match || new_defines_function)
2021         {
2022           /* These need to be copied so that the names are available.
2023              Note that if the types do match, we'll preserve inline
2024              info and other bits, but if not, we won't.  */
2025           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2026           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2027         }
2028       if (new_defines_function)
2029         /* If defining a function declared with other language
2030            linkage, use the previously declared language linkage.  */
2031         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2032       else if (types_match)
2033         {
2034           /* If redeclaring a builtin function, and not a definition,
2035              it stays built in.  */
2036           if (DECL_BUILT_IN (olddecl))
2037             {
2038               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2039               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2040               /* If we're keeping the built-in definition, keep the rtl,
2041                  regardless of declaration matches.  */
2042               COPY_DECL_RTL (olddecl, newdecl);
2043             }
2044
2045           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2046           /* Don't clear out the arguments if we're just redeclaring a
2047              function.  */
2048           if (DECL_ARGUMENTS (olddecl))
2049             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2050         }
2051     }
2052   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2053     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2054
2055   /* Now preserve various other info from the definition.  */
2056   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2057   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2058   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2059   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2060
2061   /* Warn about conflicting visibility specifications.  */
2062   if (DECL_VISIBILITY_SPECIFIED (olddecl)
2063       && DECL_VISIBILITY_SPECIFIED (newdecl)
2064       && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2065     {
2066       warning_at (input_location, OPT_Wattributes,
2067                   "%q+D: visibility attribute ignored because it", newdecl);
2068       warning_at (DECL_SOURCE_LOCATION (olddecl), OPT_Wattributes,
2069                   "conflicts with previous declaration here");
2070     }
2071   /* Choose the declaration which specified visibility.  */
2072   if (DECL_VISIBILITY_SPECIFIED (olddecl))
2073     {
2074       DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2075       DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2076     }
2077   /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2078      so keep this behavior.  */
2079   if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2080     {
2081       SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2082       DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2083     }
2084
2085   /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2086      with that from NEWDECL below.  */
2087   if (DECL_LANG_SPECIFIC (olddecl))
2088     {
2089       gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2090                   != DECL_LANG_SPECIFIC (newdecl));
2091       ggc_free (DECL_LANG_SPECIFIC (olddecl));
2092     }
2093
2094   /* Merge the USED information.  */
2095   if (TREE_USED (olddecl))
2096     TREE_USED (newdecl) = 1;
2097   else if (TREE_USED (newdecl))
2098     TREE_USED (olddecl) = 1;
2099   if (DECL_PRESERVE_P (olddecl))
2100     DECL_PRESERVE_P (newdecl) = 1;
2101   else if (DECL_PRESERVE_P (newdecl))
2102     DECL_PRESERVE_P (olddecl) = 1;
2103
2104   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2105     {
2106       int function_size;
2107
2108       function_size = sizeof (struct tree_decl_common);
2109
2110       memcpy ((char *) olddecl + sizeof (struct tree_common),
2111               (char *) newdecl + sizeof (struct tree_common),
2112               function_size - sizeof (struct tree_common));
2113
2114       memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2115               (char *) newdecl + sizeof (struct tree_decl_common),
2116               sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2117       if (new_template_info)
2118         /* If newdecl is a template instantiation, it is possible that
2119            the following sequence of events has occurred:
2120
2121            o A friend function was declared in a class template.  The
2122            class template was instantiated.
2123
2124            o The instantiation of the friend declaration was
2125            recorded on the instantiation list, and is newdecl.
2126
2127            o Later, however, instantiate_class_template called pushdecl
2128            on the newdecl to perform name injection.  But, pushdecl in
2129            turn called duplicate_decls when it discovered that another
2130            declaration of a global function with the same name already
2131            existed.
2132
2133            o Here, in duplicate_decls, we decided to clobber newdecl.
2134
2135            If we're going to do that, we'd better make sure that
2136            olddecl, and not newdecl, is on the list of
2137            instantiations so that if we try to do the instantiation
2138            again we won't get the clobbered declaration.  */
2139         reregister_specialization (newdecl,
2140                                    new_template_info,
2141                                    olddecl);
2142     }
2143   else
2144     {
2145       size_t size = tree_code_size (TREE_CODE (olddecl));
2146       memcpy ((char *) olddecl + sizeof (struct tree_common),
2147               (char *) newdecl + sizeof (struct tree_common),
2148               sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2149       switch (TREE_CODE (olddecl))
2150         {
2151         case LABEL_DECL:
2152         case VAR_DECL:
2153         case RESULT_DECL:
2154         case PARM_DECL:
2155         case FIELD_DECL:
2156         case TYPE_DECL:
2157         case CONST_DECL:
2158           {
2159             memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2160                     (char *) newdecl + sizeof (struct tree_decl_common),
2161                     size - sizeof (struct tree_decl_common)
2162                     + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2163           }
2164           break;
2165         default:
2166           memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2167                   (char *) newdecl + sizeof (struct tree_decl_common),
2168                   sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2169                   + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2170           break;
2171         }
2172     }
2173   DECL_UID (olddecl) = olddecl_uid;
2174   if (olddecl_friend)
2175     DECL_FRIEND_P (olddecl) = 1;
2176   if (hidden_friend)
2177     {
2178       DECL_ANTICIPATED (olddecl) = 1;
2179       DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2180     }
2181
2182   /* NEWDECL contains the merged attribute lists.
2183      Update OLDDECL to be the same.  */
2184   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2185
2186   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2187     so that encode_section_info has a chance to look at the new decl
2188     flags and attributes.  */
2189   if (DECL_RTL_SET_P (olddecl)
2190       && (TREE_CODE (olddecl) == FUNCTION_DECL
2191           || (TREE_CODE (olddecl) == VAR_DECL
2192               && TREE_STATIC (olddecl))))
2193     make_decl_rtl (olddecl);
2194
2195   /* The NEWDECL will no longer be needed.  Because every out-of-class
2196      declaration of a member results in a call to duplicate_decls,
2197      freeing these nodes represents in a significant savings.  */
2198   ggc_free (newdecl);
2199
2200   return olddecl;
2201 }
2202 \f
2203 /* Return zero if the declaration NEWDECL is valid
2204    when the declaration OLDDECL (assumed to be for the same name)
2205    has already been seen.
2206    Otherwise return an error message format string with a %s
2207    where the identifier should go.  */
2208
2209 static const char *
2210 redeclaration_error_message (tree newdecl, tree olddecl)
2211 {
2212   if (TREE_CODE (newdecl) == TYPE_DECL)
2213     {
2214       /* Because C++ can put things into name space for free,
2215          constructs like "typedef struct foo { ... } foo"
2216          would look like an erroneous redeclaration.  */
2217       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2218         return NULL;
2219       else
2220         return G_("redefinition of %q#D");
2221     }
2222   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2223     {
2224       /* If this is a pure function, its olddecl will actually be
2225          the original initialization to `0' (which we force to call
2226          abort()).  Don't complain about redefinition in this case.  */
2227       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2228           && DECL_INITIAL (olddecl) == NULL_TREE)
2229         return NULL;
2230
2231       /* If both functions come from different namespaces, this is not
2232          a redeclaration - this is a conflict with a used function.  */
2233       if (DECL_NAMESPACE_SCOPE_P (olddecl)
2234           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2235           && ! decls_match (olddecl, newdecl))
2236         return G_("%qD conflicts with used function");
2237
2238       /* We'll complain about linkage mismatches in
2239          warn_extern_redeclared_static.  */
2240
2241       /* Defining the same name twice is no good.  */
2242       if (DECL_INITIAL (olddecl) != NULL_TREE
2243           && DECL_INITIAL (newdecl) != NULL_TREE)
2244         {
2245           if (DECL_NAME (olddecl) == NULL_TREE)
2246             return G_("%q#D not declared in class");
2247           else if (!GNU_INLINE_P (olddecl)
2248                    || GNU_INLINE_P (newdecl))
2249             return G_("redefinition of %q#D");
2250         }
2251
2252       if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2253         {
2254           bool olda = GNU_INLINE_P (olddecl);
2255           bool newa = GNU_INLINE_P (newdecl);
2256
2257           if (olda != newa)
2258             {
2259               if (newa)
2260                 return G_("%q+D redeclared inline with "
2261                           "%<gnu_inline%> attribute");
2262               else
2263                 return G_("%q+D redeclared inline without "
2264                           "%<gnu_inline%> attribute");
2265             }
2266         }
2267
2268       return NULL;
2269     }
2270   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2271     {
2272       tree nt, ot;
2273
2274       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2275         {
2276           if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2277               && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2278             return G_("redefinition of %q#D");
2279           return NULL;
2280         }
2281
2282       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2283           || (DECL_TEMPLATE_RESULT (newdecl)
2284               == DECL_TEMPLATE_RESULT (olddecl)))
2285         return NULL;
2286
2287       nt = DECL_TEMPLATE_RESULT (newdecl);
2288       if (DECL_TEMPLATE_INFO (nt))
2289         nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2290       ot = DECL_TEMPLATE_RESULT (olddecl);
2291       if (DECL_TEMPLATE_INFO (ot))
2292         ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2293       if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2294           && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2295         return G_("redefinition of %q#D");
2296
2297       if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2298         {
2299           bool olda = GNU_INLINE_P (ot);
2300           bool newa = GNU_INLINE_P (nt);
2301
2302           if (olda != newa)
2303             {
2304               if (newa)
2305                 return G_("%q+D redeclared inline with "
2306                           "%<gnu_inline%> attribute");
2307               else
2308                 return G_("%q+D redeclared inline without "
2309                           "%<gnu_inline%> attribute");
2310             }
2311         }
2312
2313       /* Core issue #226 (C++0x): 
2314            
2315            If a friend function template declaration specifies a
2316            default template-argument, that declaration shall be a
2317            definition and shall be the only declaration of the
2318            function template in the translation unit.  */
2319       if ((cxx_dialect != cxx98) 
2320           && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2321           && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl), 
2322                                        /*is_primary=*/1, /*is_partial=*/0,
2323                                        /*is_friend_decl=*/2))
2324         return G_("redeclaration of friend %q#D "
2325                   "may not have default template arguments");
2326
2327       return NULL;
2328     }
2329   else if (TREE_CODE (newdecl) == VAR_DECL
2330            && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2331            && (! DECL_LANG_SPECIFIC (olddecl)
2332                || ! CP_DECL_THREADPRIVATE_P (olddecl)
2333                || DECL_THREAD_LOCAL_P (newdecl)))
2334     {
2335       /* Only variables can be thread-local, and all declarations must
2336          agree on this property.  */
2337       if (DECL_THREAD_LOCAL_P (newdecl))
2338         return G_("thread-local declaration of %q#D follows "
2339                   "non-thread-local declaration");
2340       else
2341         return G_("non-thread-local declaration of %q#D follows "
2342                   "thread-local declaration");
2343     }
2344   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2345     {
2346       /* The objects have been declared at namespace scope.  If either
2347          is a member of an anonymous union, then this is an invalid
2348          redeclaration.  For example:
2349
2350            int i;
2351            union { int i; };
2352
2353            is invalid.  */
2354       if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2355           || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
2356         return G_("redeclaration of %q#D");
2357       /* If at least one declaration is a reference, there is no
2358          conflict.  For example:
2359
2360            int i = 3;
2361            extern int i;
2362
2363          is valid.  */
2364       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2365         return NULL;
2366       /* Reject two definitions.  */
2367       return G_("redefinition of %q#D");
2368     }
2369   else
2370     {
2371       /* Objects declared with block scope:  */
2372       /* Reject two definitions, and reject a definition
2373          together with an external reference.  */
2374       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2375         return G_("redeclaration of %q#D");
2376       return NULL;
2377     }
2378 }
2379 \f
2380 /* Hash and equality functions for the named_label table.  */
2381
2382 static hashval_t
2383 named_label_entry_hash (const void *data)
2384 {
2385   const struct named_label_entry *ent = (const struct named_label_entry *) data;
2386   return DECL_UID (ent->label_decl);
2387 }
2388
2389 static int
2390 named_label_entry_eq (const void *a, const void *b)
2391 {
2392   const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2393   const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2394   return ent_a->label_decl == ent_b->label_decl;
2395 }
2396
2397 /* Create a new label, named ID.  */
2398
2399 static tree
2400 make_label_decl (tree id, int local_p)
2401 {
2402   struct named_label_entry *ent;
2403   void **slot;
2404   tree decl;
2405
2406   decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2407
2408   DECL_CONTEXT (decl) = current_function_decl;
2409   DECL_MODE (decl) = VOIDmode;
2410   C_DECLARED_LABEL_FLAG (decl) = local_p;
2411
2412   /* Say where one reference is to the label, for the sake of the
2413      error if it is not defined.  */
2414   DECL_SOURCE_LOCATION (decl) = input_location;
2415
2416   /* Record the fact that this identifier is bound to this label.  */
2417   SET_IDENTIFIER_LABEL_VALUE (id, decl);
2418
2419   /* Create the label htab for the function on demand.  */
2420   if (!named_labels)
2421     named_labels = htab_create_ggc (13, named_label_entry_hash,
2422                                     named_label_entry_eq, NULL);
2423
2424   /* Record this label on the list of labels used in this function.
2425      We do this before calling make_label_decl so that we get the
2426      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
2427   ent = GGC_CNEW (struct named_label_entry);
2428   ent->label_decl = decl;
2429
2430   slot = htab_find_slot (named_labels, ent, INSERT);
2431   gcc_assert (*slot == NULL);
2432   *slot = ent;
2433
2434   return decl;
2435 }
2436
2437 /* Look for a label named ID in the current function.  If one cannot
2438    be found, create one.  (We keep track of used, but undefined,
2439    labels, and complain about them at the end of a function.)  */
2440
2441 tree
2442 lookup_label (tree id)
2443 {
2444   tree decl;
2445
2446   timevar_push (TV_NAME_LOOKUP);
2447   /* You can't use labels at global scope.  */
2448   if (current_function_decl == NULL_TREE)
2449     {
2450       error ("label %qE referenced outside of any function", id);
2451       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2452     }
2453
2454   /* See if we've already got this label.  */
2455   decl = IDENTIFIER_LABEL_VALUE (id);
2456   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2457     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2458
2459   decl = make_label_decl (id, /*local_p=*/0);
2460   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2461 }
2462
2463 /* Declare a local label named ID.  */
2464
2465 tree
2466 declare_local_label (tree id)
2467 {
2468   tree decl, shadow;
2469
2470   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2471      this scope we can restore the old value of IDENTIFIER_TYPE_VALUE.  */
2472   shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2473                       current_binding_level->shadowed_labels);
2474   current_binding_level->shadowed_labels = shadow;
2475
2476   decl = make_label_decl (id, /*local_p=*/1);
2477   TREE_VALUE (shadow) = decl;
2478
2479   return decl;
2480 }
2481
2482 /* Returns nonzero if it is ill-formed to jump past the declaration of
2483    DECL.  Returns 2 if it's also a real problem.  */
2484
2485 static int
2486 decl_jump_unsafe (tree decl)
2487 {
2488   /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2489      with automatic storage duration is not in scope to a point where it is
2490      in scope is ill-formed unless the variable has scalar type, class type
2491      with a trivial default constructor and a trivial destructor, a
2492      cv-qualified version of one of these types, or an array of one of the
2493      preceding types and is declared without an initializer (8.5).  */
2494   tree type = TREE_TYPE (decl);
2495
2496   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2497       || type == error_mark_node)
2498     return 0;
2499
2500   type = strip_array_types (type);
2501
2502   if (type_has_nontrivial_default_init (TREE_TYPE (decl))
2503       || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2504     return 2;
2505
2506   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2507     return 1;
2508
2509   return 0;
2510 }
2511
2512 /* A subroutine of check_previous_goto_1 to identify a branch to the user.  */
2513
2514 static void
2515 identify_goto (tree decl, const location_t *locus)
2516 {
2517   if (decl)
2518     permerror (input_location, "jump to label %qD", decl);
2519   else
2520     permerror (input_location, "jump to case label");
2521   if (locus)
2522     permerror (*locus, "  from here");
2523 }
2524
2525 /* Check that a single previously seen jump to a newly defined label
2526    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2527    the jump context; NAMES are the names in scope in LEVEL at the jump
2528    context; LOCUS is the source position of the jump or 0.  Returns
2529    true if all is well.  */
2530
2531 static bool
2532 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2533                        bool exited_omp, const location_t *locus)
2534 {
2535   struct cp_binding_level *b;
2536   bool identified = false, saw_eh = false, saw_omp = false;
2537
2538   if (exited_omp)
2539     {
2540       identify_goto (decl, locus);
2541       error ("  exits OpenMP structured block");
2542       identified = saw_omp = true;
2543     }
2544
2545   for (b = current_binding_level; b ; b = b->level_chain)
2546     {
2547       tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2548
2549       for (new_decls = b->names; new_decls != old_decls;
2550            new_decls = TREE_CHAIN (new_decls))
2551         {
2552           int problem = decl_jump_unsafe (new_decls);
2553           if (! problem)
2554             continue;
2555
2556           if (!identified)
2557             {
2558               identify_goto (decl, locus);
2559               identified = true;
2560             }
2561           if (problem > 1)
2562             error ("  crosses initialization of %q+#D", new_decls);
2563           else
2564             permerror (input_location, "  enters scope of %q+#D which has "
2565                        "non-trivial destructor", new_decls);
2566         }
2567
2568       if (b == level)
2569         break;
2570       if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2571         {
2572           if (!identified)
2573             {
2574               identify_goto (decl, locus);
2575               identified = true;
2576             }
2577           if (b->kind == sk_try)
2578             error ("  enters try block");
2579           else
2580             error ("  enters catch block");
2581           saw_eh = true;
2582         }
2583       if (b->kind == sk_omp && !saw_omp)
2584         {
2585           if (!identified)
2586             {
2587               identify_goto (decl, locus);
2588               identified = true;
2589             }
2590           error ("  enters OpenMP structured block");
2591           saw_omp = true;
2592         }
2593     }
2594
2595   return !identified;
2596 }
2597
2598 static void
2599 check_previous_goto (tree decl, struct named_label_use_entry *use)
2600 {
2601   check_previous_goto_1 (decl, use->binding_level,
2602                          use->names_in_scope, use->in_omp_scope,
2603                          &use->o_goto_locus);
2604 }
2605
2606 static bool
2607 check_switch_goto (struct cp_binding_level* level)
2608 {
2609   return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2610 }
2611
2612 /* Check that a new jump to a label DECL is OK.  Called by
2613    finish_goto_stmt.  */
2614
2615 void
2616 check_goto (tree decl)
2617 {
2618   struct named_label_entry *ent, dummy;
2619   bool saw_catch = false, identified = false;
2620   tree bad;
2621
2622   /* We can't know where a computed goto is jumping.
2623      So we assume that it's OK.  */
2624   if (TREE_CODE (decl) != LABEL_DECL)
2625     return;
2626
2627   /* We didn't record any information about this label when we created it,
2628      and there's not much point since it's trivial to analyze as a return.  */
2629   if (decl == cdtor_label)
2630     return;
2631
2632   dummy.label_decl = decl;
2633   ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2634   gcc_assert (ent != NULL);
2635
2636   /* If the label hasn't been defined yet, defer checking.  */
2637   if (! DECL_INITIAL (decl))
2638     {
2639       struct named_label_use_entry *new_use;
2640
2641       /* Don't bother creating another use if the last goto had the
2642          same data, and will therefore create the same set of errors.  */
2643       if (ent->uses
2644           && ent->uses->names_in_scope == current_binding_level->names)
2645         return;
2646
2647       new_use = GGC_NEW (struct named_label_use_entry);
2648       new_use->binding_level = current_binding_level;
2649       new_use->names_in_scope = current_binding_level->names;
2650       new_use->o_goto_locus = input_location;
2651       new_use->in_omp_scope = false;
2652
2653       new_use->next = ent->uses;
2654       ent->uses = new_use;
2655       return;
2656     }
2657
2658   if (ent->in_try_scope || ent->in_catch_scope
2659       || ent->in_omp_scope || ent->bad_decls)
2660     {
2661       permerror (input_location, "jump to label %q+D", decl);
2662       permerror (input_location, "  from here");
2663       identified = true;
2664     }
2665
2666   for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad))
2667     {
2668       tree b = TREE_VALUE (bad);
2669       int u = decl_jump_unsafe (b);
2670
2671       if (u > 1 && DECL_ARTIFICIAL (b))
2672         {
2673           /* Can't skip init of __exception_info.  */
2674           error_at (DECL_SOURCE_LOCATION (b), "  enters catch block");
2675           saw_catch = true;
2676         }
2677       else if (u > 1)
2678         error ("  skips initialization of %q+#D", b);
2679       else
2680         permerror (input_location, "  enters scope of %q+#D which has "
2681                    "non-trivial destructor", b);
2682     }
2683
2684   if (ent->in_try_scope)
2685     error ("  enters try block");
2686   else if (ent->in_catch_scope && !saw_catch)
2687     error ("  enters catch block");
2688
2689   if (ent->in_omp_scope)
2690     error ("  enters OpenMP structured block");
2691   else if (flag_openmp)
2692     {
2693       struct cp_binding_level *b;
2694       for (b = current_binding_level; b ; b = b->level_chain)
2695         {
2696           if (b == ent->binding_level)
2697             break;
2698           if (b->kind == sk_omp)
2699             {
2700               if (!identified)
2701                 {
2702                   permerror (input_location, "jump to label %q+D", decl);
2703                   permerror (input_location, "  from here");
2704                   identified = true;
2705                 }
2706               error ("  exits OpenMP structured block");
2707               break;
2708             }
2709         }
2710     }
2711 }
2712
2713 /* Check that a return is ok wrt OpenMP structured blocks.
2714    Called by finish_return_stmt.  Returns true if all is well.  */
2715
2716 bool
2717 check_omp_return (void)
2718 {
2719   struct cp_binding_level *b;
2720   for (b = current_binding_level; b ; b = b->level_chain)
2721     if (b->kind == sk_omp)
2722       {
2723         error ("invalid exit from OpenMP structured block");
2724         return false;
2725       }
2726   return true;
2727 }
2728
2729 /* Define a label, specifying the location in the source file.
2730    Return the LABEL_DECL node for the label.  */
2731
2732 tree
2733 define_label (location_t location, tree name)
2734 {
2735   struct named_label_entry *ent, dummy;
2736   struct cp_binding_level *p;
2737   tree decl;
2738
2739   timevar_push (TV_NAME_LOOKUP);
2740
2741   decl = lookup_label (name);
2742
2743   dummy.label_decl = decl;
2744   ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2745   gcc_assert (ent != NULL);
2746
2747   /* After labels, make any new cleanups in the function go into their
2748      own new (temporary) binding contour.  */
2749   for (p = current_binding_level;
2750        p->kind != sk_function_parms;
2751        p = p->level_chain)
2752     p->more_cleanups_ok = 0;
2753
2754   if (name == get_identifier ("wchar_t"))
2755     permerror (input_location, "label named wchar_t");
2756
2757   if (DECL_INITIAL (decl) != NULL_TREE)
2758     {
2759       error ("duplicate label %qD", decl);
2760       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2761     }
2762   else
2763     {
2764       struct named_label_use_entry *use;
2765
2766       /* Mark label as having been defined.  */
2767       DECL_INITIAL (decl) = error_mark_node;
2768       /* Say where in the source.  */
2769       DECL_SOURCE_LOCATION (decl) = location;
2770
2771       ent->binding_level = current_binding_level;
2772       ent->names_in_scope = current_binding_level->names;
2773
2774       for (use = ent->uses; use ; use = use->next)
2775         check_previous_goto (decl, use);
2776       ent->uses = NULL;
2777     }
2778
2779   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2780 }
2781
2782 struct cp_switch
2783 {
2784   struct cp_binding_level *level;
2785   struct cp_switch *next;
2786   /* The SWITCH_STMT being built.  */
2787   tree switch_stmt;
2788   /* A splay-tree mapping the low element of a case range to the high
2789      element, or NULL_TREE if there is no high element.  Used to
2790      determine whether or not a new case label duplicates an old case
2791      label.  We need a tree, rather than simply a hash table, because
2792      of the GNU case range extension.  */
2793   splay_tree cases;
2794 };
2795
2796 /* A stack of the currently active switch statements.  The innermost
2797    switch statement is on the top of the stack.  There is no need to
2798    mark the stack for garbage collection because it is only active
2799    during the processing of the body of a function, and we never
2800    collect at that point.  */
2801
2802 static struct cp_switch *switch_stack;
2803
2804 /* Called right after a switch-statement condition is parsed.
2805    SWITCH_STMT is the switch statement being parsed.  */
2806
2807 void
2808 push_switch (tree switch_stmt)
2809 {
2810   struct cp_switch *p = XNEW (struct cp_switch);
2811   p->level = current_binding_level;
2812   p->next = switch_stack;
2813   p->switch_stmt = switch_stmt;
2814   p->cases = splay_tree_new (case_compare, NULL, NULL);
2815   switch_stack = p;
2816 }
2817
2818 void
2819 pop_switch (void)
2820 {
2821   struct cp_switch *cs = switch_stack;
2822   location_t switch_location;
2823
2824   /* Emit warnings as needed.  */
2825   if (EXPR_HAS_LOCATION (cs->switch_stmt))
2826     switch_location = EXPR_LOCATION (cs->switch_stmt);
2827   else
2828     switch_location = input_location;
2829   if (!processing_template_decl)
2830     c_do_switch_warnings (cs->cases, switch_location,
2831                           SWITCH_STMT_TYPE (cs->switch_stmt),
2832                           SWITCH_STMT_COND (cs->switch_stmt));
2833
2834   splay_tree_delete (cs->cases);
2835   switch_stack = switch_stack->next;
2836   free (cs);
2837 }
2838
2839 /* Note that we've seen a definition of a case label, and complain if this
2840    is a bad place for one.  */
2841
2842 tree
2843 finish_case_label (location_t loc, tree low_value, tree high_value)
2844 {
2845   tree cond, r;
2846   struct cp_binding_level *p;
2847
2848   if (processing_template_decl)
2849     {
2850       tree label;
2851
2852       /* For templates, just add the case label; we'll do semantic
2853          analysis at instantiation-time.  */
2854       label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
2855       return add_stmt (build_case_label (loc, low_value, high_value, label));
2856     }
2857
2858   /* Find the condition on which this switch statement depends.  */
2859   cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2860   if (cond && TREE_CODE (cond) == TREE_LIST)
2861     cond = TREE_VALUE (cond);
2862
2863   if (!check_switch_goto (switch_stack->level))
2864     return error_mark_node;
2865
2866   r = c_add_case_label (loc, switch_stack->cases, cond,
2867                         SWITCH_STMT_TYPE (switch_stack->switch_stmt),
2868                         low_value, high_value);
2869
2870   /* After labels, make any new cleanups in the function go into their
2871      own new (temporary) binding contour.  */
2872   for (p = current_binding_level;
2873        p->kind != sk_function_parms;
2874        p = p->level_chain)
2875     p->more_cleanups_ok = 0;
2876
2877   return r;
2878 }
2879 \f
2880 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
2881
2882 static hashval_t
2883 typename_hash (const void* k)
2884 {
2885   hashval_t hash;
2886   const_tree const t = (const_tree) k;
2887
2888   hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2889           ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2890
2891   return hash;
2892 }
2893
2894 typedef struct typename_info {
2895   tree scope;
2896   tree name;
2897   tree template_id;
2898   bool enum_p;
2899   bool class_p;
2900 } typename_info;
2901
2902 /* Compare two TYPENAME_TYPEs.  K1 is really of type `tree', K2 is
2903    really of type `typename_info*'  */
2904
2905 static int
2906 typename_compare (const void * k1, const void * k2)
2907 {
2908   const_tree const t1 = (const_tree) k1;
2909   const typename_info *const t2 = (const typename_info *) k2;
2910
2911   return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2912           && TYPE_CONTEXT (t1) == t2->scope
2913           && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2914           && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2915           && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2916 }
2917
2918 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
2919    the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2920
2921    Returns the new TYPENAME_TYPE.  */
2922
2923 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2924
2925 static tree
2926 build_typename_type (tree context, tree name, tree fullname,
2927                      enum tag_types tag_type)
2928 {
2929   tree t;
2930   tree d;
2931   typename_info ti;
2932   void **e;
2933   hashval_t hash;
2934
2935   if (typename_htab == NULL)
2936     typename_htab = htab_create_ggc (61, &typename_hash,
2937                                      &typename_compare, NULL);
2938
2939   ti.scope = FROB_CONTEXT (context);
2940   ti.name = name;
2941   ti.template_id = fullname;
2942   ti.enum_p = tag_type == enum_type;
2943   ti.class_p = (tag_type == class_type
2944                 || tag_type == record_type
2945                 || tag_type == union_type);
2946   hash =  (htab_hash_pointer (ti.scope)
2947            ^ htab_hash_pointer (ti.name));
2948
2949   /* See if we already have this type.  */
2950   e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2951   if (*e)
2952     t = (tree) *e;
2953   else
2954     {
2955       /* Build the TYPENAME_TYPE.  */
2956       t = cxx_make_type (TYPENAME_TYPE);
2957       TYPE_CONTEXT (t) = ti.scope;
2958       TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2959       TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2960       TYPENAME_IS_CLASS_P (t) = ti.class_p;
2961
2962       /* Build the corresponding TYPE_DECL.  */
2963       d = build_decl (input_location, TYPE_DECL, name, t);
2964       TYPE_NAME (TREE_TYPE (d)) = d;
2965       TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2966       DECL_CONTEXT (d) = FROB_CONTEXT (context);
2967       DECL_ARTIFICIAL (d) = 1;
2968
2969       /* Store it in the hash table.  */
2970       *e = t;
2971
2972       /* TYPENAME_TYPEs must always be compared structurally, because
2973          they may or may not resolve down to another type depending on
2974          the currently open classes. */
2975       SET_TYPE_STRUCTURAL_EQUALITY (t);
2976     }
2977
2978   return t;
2979 }
2980
2981 /* Resolve `typename CONTEXT::NAME'.  TAG_TYPE indicates the tag
2982    provided to name the type.  Returns an appropriate type, unless an
2983    error occurs, in which case error_mark_node is returned.  If we
2984    locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2985    return that, rather than the _TYPE it corresponds to, in other
2986    cases we look through the type decl.  If TF_ERROR is set, complain
2987    about errors, otherwise be quiet.  */
2988
2989 tree
2990 make_typename_type (tree context, tree name, enum tag_types tag_type,
2991                     tsubst_flags_t complain)
2992 {
2993   tree fullname;
2994   tree t;
2995   bool want_template;
2996
2997   if (name == error_mark_node
2998       || context == NULL_TREE
2999       || context == error_mark_node)
3000     return error_mark_node;
3001
3002   if (TYPE_P (name))
3003     {
3004       if (!(TYPE_LANG_SPECIFIC (name)
3005             && (CLASSTYPE_IS_TEMPLATE (name)
3006                 || CLASSTYPE_USE_TEMPLATE (name))))
3007         name = TYPE_IDENTIFIER (name);
3008       else
3009         /* Create a TEMPLATE_ID_EXPR for the type.  */
3010         name = build_nt (TEMPLATE_ID_EXPR,
3011                          CLASSTYPE_TI_TEMPLATE (name),
3012                          CLASSTYPE_TI_ARGS (name));
3013     }
3014   else if (TREE_CODE (name) == TYPE_DECL)
3015     name = DECL_NAME (name);
3016
3017   fullname = name;
3018
3019   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3020     {
3021       name = TREE_OPERAND (name, 0);
3022       if (TREE_CODE (name) == TEMPLATE_DECL)
3023         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3024       else if (TREE_CODE (name) == OVERLOAD)
3025         {
3026           error ("%qD is not a type", name);
3027           return error_mark_node;
3028         }
3029     }
3030   if (TREE_CODE (name) == TEMPLATE_DECL)
3031     {
3032       error ("%qD used without template parameters", name);
3033       return error_mark_node;
3034     }
3035   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3036   gcc_assert (TYPE_P (context));
3037
3038   if (!MAYBE_CLASS_TYPE_P (context))
3039     {
3040       if (complain & tf_error)
3041         error ("%q#T is not a class", context);
3042       return error_mark_node;
3043     }
3044   
3045   /* When the CONTEXT is a dependent type,  NAME could refer to a
3046      dependent base class of CONTEXT.  But look inside it anyway
3047      if CONTEXT is a currently open scope, in case it refers to a
3048      member of the current instantiation or a non-dependent base;
3049      lookup will stop when we hit a dependent base.  */
3050   if (!dependent_scope_p (context))
3051     /* We should only set WANT_TYPE when we're a nested typename type.
3052        Then we can give better diagnostics if we find a non-type.  */
3053     t = lookup_field (context, name, 2, /*want_type=*/true);
3054   else
3055     t = NULL_TREE;
3056
3057   if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3058     return build_typename_type (context, name, fullname, tag_type);
3059
3060   want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3061   
3062   if (!t)
3063     {
3064       if (complain & tf_error)
3065         error (want_template ? "no class template named %q#T in %q#T"
3066                : "no type named %q#T in %q#T", name, context);
3067       return error_mark_node;
3068     }
3069   
3070   /* Pull out the template from an injected-class-name (or multiple).  */
3071   if (want_template)
3072     t = maybe_get_template_decl_from_type_decl (t);
3073
3074   if (TREE_CODE (t) == TREE_LIST)
3075     {
3076       if (complain & tf_error)
3077         {
3078           error ("lookup of %qT in %qT is ambiguous", name, context);
3079           print_candidates (t);
3080         }
3081       return error_mark_node;
3082     }
3083
3084   if (want_template && !DECL_CLASS_TEMPLATE_P (t))
3085     {
3086       if (complain & tf_error)
3087         error ("%<typename %T::%D%> names %q#T, which is not a class template",
3088                context, name, t);
3089       return error_mark_node;
3090     }
3091   if (!want_template && TREE_CODE (t) != TYPE_DECL)
3092     {
3093       if (complain & tf_error)
3094         error ("%<typename %T::%D%> names %q#T, which is not a type",
3095                context, name, t);
3096       return error_mark_node;
3097     }
3098   
3099   if (complain & tf_error)
3100     perform_or_defer_access_check (TYPE_BINFO (context), t, t);
3101
3102   /* If we are currently parsing a template and if T is a typedef accessed
3103      through CONTEXT then we need to remember and check access of T at
3104      template instantiation time.  */
3105   add_typedef_to_current_template_for_access_check (t, context, input_location);
3106
3107   if (want_template)
3108     return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3109                                   NULL_TREE, context,
3110                                   /*entering_scope=*/0,
3111                                   tf_warning_or_error | tf_user);
3112   
3113   if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3114     t = TREE_TYPE (t);
3115   
3116   return t;
3117 }
3118
3119 /* Resolve `CONTEXT::template NAME'.  Returns a TEMPLATE_DECL if the name
3120    can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3121    in which case error_mark_node is returned.
3122
3123    If PARM_LIST is non-NULL, also make sure that the template parameter
3124    list of TEMPLATE_DECL matches.
3125
3126    If COMPLAIN zero, don't complain about any errors that occur.  */
3127
3128 tree
3129 make_unbound_class_template (tree context, tree name, tree parm_list,
3130                              tsubst_flags_t complain)
3131 {
3132   tree t;
3133   tree d;
3134
3135   if (TYPE_P (name))
3136     name = TYPE_IDENTIFIER (name);
3137   else if (DECL_P (name))
3138     name = DECL_NAME (name);
3139   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3140
3141   if (!dependent_type_p (context)
3142       || currently_open_class (context))
3143     {
3144       tree tmpl = NULL_TREE;
3145
3146       if (MAYBE_CLASS_TYPE_P (context))
3147         tmpl = lookup_field (context, name, 0, false);
3148
3149       if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
3150         {
3151           if (complain & tf_error)
3152             error ("no class template named %q#T in %q#T", name, context);
3153           return error_mark_node;
3154         }
3155
3156       if (parm_list
3157           && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3158         {
3159           if (complain & tf_error)
3160             {
3161               error ("template parameters do not match template");
3162               error ("%q+D declared here", tmpl);
3163             }
3164           return error_mark_node;
3165         }
3166
3167       if (complain & tf_error)
3168         perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
3169
3170       return tmpl;
3171     }
3172
3173   /* Build the UNBOUND_CLASS_TEMPLATE.  */
3174   t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3175   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3176   TREE_TYPE (t) = NULL_TREE;
3177   SET_TYPE_STRUCTURAL_EQUALITY (t);
3178
3179   /* Build the corresponding TEMPLATE_DECL.  */
3180   d = build_decl (input_location, TEMPLATE_DECL, name, t);
3181   TYPE_NAME (TREE_TYPE (d)) = d;
3182   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3183   DECL_CONTEXT (d) = FROB_CONTEXT (context);
3184   DECL_ARTIFICIAL (d) = 1;
3185   DECL_TEMPLATE_PARMS (d) = parm_list;
3186
3187   return t;
3188 }
3189
3190 \f
3191
3192 /* Push the declarations of builtin types into the namespace.
3193    RID_INDEX is the index of the builtin type in the array
3194    RID_POINTERS.  NAME is the name used when looking up the builtin
3195    type.  TYPE is the _TYPE node for the builtin type.  */
3196
3197 void
3198 record_builtin_type (enum rid rid_index,
3199                      const char* name,
3200                      tree type)
3201 {
3202   tree rname = NULL_TREE, tname = NULL_TREE;
3203   tree tdecl = NULL_TREE;
3204
3205   if ((int) rid_index < (int) RID_MAX)
3206     rname = ridpointers[(int) rid_index];
3207   if (name)
3208     tname = get_identifier (name);
3209
3210   /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3211      eliminated.  Built-in types should not be looked up name; their
3212      names are keywords that the parser can recognize.  However, there
3213      is code in c-common.c that uses identifier_global_value to look
3214      up built-in types by name.  */
3215   if (tname)
3216     {
3217       tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3218       DECL_ARTIFICIAL (tdecl) = 1;
3219       SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3220     }
3221   if (rname)
3222     {
3223       if (!tdecl)
3224         {
3225           tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3226           DECL_ARTIFICIAL (tdecl) = 1;
3227         }
3228       SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3229     }
3230
3231   if (!TYPE_NAME (type))
3232     TYPE_NAME (type) = tdecl;
3233
3234   if (tdecl)
3235     debug_hooks->type_decl (tdecl, 0);
3236 }
3237
3238 /* Record one of the standard Java types.
3239  * Declare it as having the given NAME.
3240  * If SIZE > 0, it is the size of one of the integral types;
3241  * otherwise it is the negative of the size of one of the other types.  */
3242
3243 static tree
3244 record_builtin_java_type (const char* name, int size)
3245 {
3246   tree type, decl;
3247   if (size > 0)
3248     type = build_nonstandard_integer_type (size, 0);
3249   else if (size > -32)
3250     {
3251       tree stype;
3252       /* "__java_char" or ""__java_boolean".  */
3253       type = build_nonstandard_integer_type (-size, 1);
3254       /* Get the signed type cached and attached to the unsigned type,
3255          so it doesn't get garbage-collected at "random" times,
3256          causing potential codegen differences out of different UIDs
3257          and different alias set numbers.  */
3258       stype = build_nonstandard_integer_type (-size, 0);
3259       TREE_CHAIN (type) = stype;
3260       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3261     }
3262   else
3263     { /* "__java_float" or ""__java_double".  */
3264       type = make_node (REAL_TYPE);
3265       TYPE_PRECISION (type) = - size;
3266       layout_type (type);
3267     }
3268   record_builtin_type (RID_MAX, name, type);
3269   decl = TYPE_NAME (type);
3270
3271   /* Suppress generate debug symbol entries for these types,
3272      since for normal C++ they are just clutter.
3273      However, push_lang_context undoes this if extern "Java" is seen.  */
3274   DECL_IGNORED_P (decl) = 1;
3275
3276   TYPE_FOR_JAVA (type) = 1;
3277   return type;
3278 }
3279
3280 /* Push a type into the namespace so that the back ends ignore it.  */
3281
3282 static void
3283 record_unknown_type (tree type, const char* name)
3284 {
3285   tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3286                                     TYPE_DECL, get_identifier (name), type));
3287   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
3288   DECL_IGNORED_P (decl) = 1;
3289   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3290   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3291   TYPE_ALIGN (type) = 1;
3292   TYPE_USER_ALIGN (type) = 0;
3293   SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3294 }
3295
3296 /* A string for which we should create an IDENTIFIER_NODE at
3297    startup.  */
3298
3299 typedef struct predefined_identifier
3300 {
3301   /* The name of the identifier.  */
3302   const char *const name;
3303   /* The place where the IDENTIFIER_NODE should be stored.  */
3304   tree *const node;
3305   /* Nonzero if this is the name of a constructor or destructor.  */
3306   const int ctor_or_dtor_p;
3307 } predefined_identifier;
3308
3309 /* Create all the predefined identifiers.  */
3310
3311 static void
3312 initialize_predefined_identifiers (void)
3313 {
3314   const predefined_identifier *pid;
3315
3316   /* A table of identifiers to create at startup.  */
3317   static const predefined_identifier predefined_identifiers[] = {
3318     { "C++", &lang_name_cplusplus, 0 },
3319     { "C", &lang_name_c, 0 },
3320     { "Java", &lang_name_java, 0 },
3321     /* Some of these names have a trailing space so that it is
3322        impossible for them to conflict with names written by users.  */
3323     { "__ct ", &ctor_identifier, 1 },
3324     { "__base_ctor ", &base_ctor_identifier, 1 },
3325     { "__comp_ctor ", &complete_ctor_identifier, 1 },
3326     { "__dt ", &dtor_identifier, 1 },
3327     { "__comp_dtor ", &complete_dtor_identifier, 1 },
3328     { "__base_dtor ", &base_dtor_identifier, 1 },
3329     { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3330     { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3331     { "nelts", &nelts_identifier, 0 },
3332     { THIS_NAME, &this_identifier, 0 },
3333     { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3334     { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3335     { "_vptr", &vptr_identifier, 0 },
3336     { "__vtt_parm", &vtt_parm_identifier, 0 },
3337     { "::", &global_scope_name, 0 },
3338     { "std", &std_identifier, 0 },
3339     { NULL, NULL, 0 }
3340   };
3341
3342   for (pid = predefined_identifiers; pid->name; ++pid)
3343     {
3344       *pid->node = get_identifier (pid->name);
3345       if (pid->ctor_or_dtor_p)
3346         IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3347     }
3348 }
3349
3350 /* Create the predefined scalar types of C,
3351    and some nodes representing standard constants (0, 1, (void *)0).
3352    Initialize the global binding level.
3353    Make definitions for built-in primitive functions.  */
3354
3355 void
3356 cxx_init_decl_processing (void)
3357 {
3358   tree void_ftype;
3359   tree void_ftype_ptr;
3360
3361   build_common_tree_nodes (flag_signed_char, false);
3362
3363   /* Create all the identifiers we need.  */
3364   initialize_predefined_identifiers ();
3365
3366   /* Create the global variables.  */
3367   push_to_top_level ();
3368
3369   current_function_decl = NULL_TREE;
3370   current_binding_level = NULL;
3371   /* Enter the global namespace.  */
3372   gcc_assert (global_namespace == NULL_TREE);
3373   global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3374                                       void_type_node);
3375   TREE_PUBLIC (global_namespace) = 1;
3376   begin_scope (sk_namespace, global_namespace);
3377
3378   current_lang_name = NULL_TREE;
3379
3380   if (flag_visibility_ms_compat)
3381     default_visibility = VISIBILITY_HIDDEN;
3382
3383   /* Initially, C.  */
3384   current_lang_name = lang_name_c;
3385
3386   /* Create the `std' namespace.  */
3387   push_namespace (std_identifier);
3388   std_node = current_namespace;
3389   pop_namespace ();
3390
3391   c_common_nodes_and_builtins ();
3392
3393   java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3394   java_short_type_node = record_builtin_java_type ("__java_short", 16);
3395   java_int_type_node = record_builtin_java_type ("__java_int", 32);
3396   java_long_type_node = record_builtin_java_type ("__java_long", 64);
3397   java_float_type_node = record_builtin_java_type ("__java_float", -32);
3398   java_double_type_node = record_builtin_java_type ("__java_double", -64);
3399   java_char_type_node = record_builtin_java_type ("__java_char", -16);
3400   java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3401
3402   integer_two_node = build_int_cst (NULL_TREE, 2);
3403   integer_three_node = build_int_cst (NULL_TREE, 3);
3404
3405   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3406   truthvalue_type_node = boolean_type_node;
3407   truthvalue_false_node = boolean_false_node;
3408   truthvalue_true_node = boolean_true_node;
3409
3410   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3411
3412 #if 0
3413   record_builtin_type (RID_MAX, NULL, string_type_node);
3414 #endif
3415
3416   delta_type_node = ptrdiff_type_node;
3417   vtable_index_type = ptrdiff_type_node;
3418
3419   vtt_parm_type = build_pointer_type (const_ptr_type_node);
3420   void_ftype = build_function_type (void_type_node, void_list_node);
3421   void_ftype_ptr = build_function_type (void_type_node,
3422                                         tree_cons (NULL_TREE,
3423                                                    ptr_type_node,
3424                                                    void_list_node));
3425   void_ftype_ptr
3426     = build_exception_variant (void_ftype_ptr, empty_except_spec);
3427
3428   /* C++ extensions */
3429
3430   unknown_type_node = make_node (UNKNOWN_TYPE);
3431   record_unknown_type (unknown_type_node, "unknown type");
3432
3433   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
3434   TREE_TYPE (unknown_type_node) = unknown_type_node;
3435
3436   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3437      result.  */
3438   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3439   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3440
3441   init_list_type_node = make_node (UNKNOWN_TYPE);
3442   record_unknown_type (init_list_type_node, "init list");
3443
3444   {
3445     /* Make sure we get a unique function type, so we can give
3446        its pointer type a name.  (This wins for gdb.) */
3447     tree vfunc_type = make_node (FUNCTION_TYPE);
3448     TREE_TYPE (vfunc_type) = integer_type_node;
3449     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3450     layout_type (vfunc_type);
3451
3452     vtable_entry_type = build_pointer_type (vfunc_type);
3453   }
3454   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3455
3456   vtbl_type_node
3457     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3458   layout_type (vtbl_type_node);
3459   vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3460   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3461   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3462   layout_type (vtbl_ptr_type_node);
3463   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3464
3465   push_namespace (get_identifier ("__cxxabiv1"));
3466   abi_node = current_namespace;
3467   pop_namespace ();
3468
3469   global_type_node = make_node (LANG_TYPE);
3470   record_unknown_type (global_type_node, "global type");
3471
3472   /* Now, C++.  */
3473   current_lang_name = lang_name_cplusplus;
3474
3475   {
3476     tree bad_alloc_id;
3477     tree bad_alloc_type_node;
3478     tree bad_alloc_decl;
3479     tree newtype, deltype;
3480     tree ptr_ftype_sizetype;
3481
3482     push_namespace (std_identifier);
3483     bad_alloc_id = get_identifier ("bad_alloc");
3484     bad_alloc_type_node = make_class_type (RECORD_TYPE);
3485     TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3486     bad_alloc_decl
3487       = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3488     DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3489     pop_namespace ();
3490
3491     ptr_ftype_sizetype
3492       = build_function_type (ptr_type_node,
3493                              tree_cons (NULL_TREE,
3494                                         size_type_node,
3495                                         void_list_node));
3496     newtype = build_exception_variant
3497       (ptr_ftype_sizetype, add_exception_specifier
3498        (NULL_TREE, bad_alloc_type_node, -1));
3499     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3500     push_cp_library_fn (NEW_EXPR, newtype);
3501     push_cp_library_fn (VEC_NEW_EXPR, newtype);
3502     global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3503     push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3504   }
3505
3506   abort_fndecl
3507     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3508
3509   /* Perform other language dependent initializations.  */
3510   init_class_processing ();
3511   init_rtti_processing ();
3512   init_template_processing ();
3513
3514   if (flag_exceptions)
3515     init_exception_processing ();
3516
3517   if (! supports_one_only ())
3518     flag_weak = 0;
3519
3520   make_fname_decl = cp_make_fname_decl;
3521   start_fname_decls ();
3522
3523   /* Show we use EH for cleanups.  */
3524   if (flag_exceptions)
3525     using_eh_for_cleanups ();
3526 }
3527
3528 /* Generate an initializer for a function naming variable from
3529    NAME. NAME may be NULL, to indicate a dependent name.  TYPE_P is
3530    filled in with the type of the init.  */
3531
3532 tree
3533 cp_fname_init (const char* name, tree *type_p)
3534 {
3535   tree domain = NULL_TREE;
3536   tree type;
3537   tree init = NULL_TREE;
3538   size_t length = 0;
3539
3540   if (name)
3541     {
3542       length = strlen (name);
3543       domain = build_index_type (size_int (length));
3544       init = build_string (length + 1, name);
3545     }
3546
3547   type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3548   type = build_cplus_array_type (type, domain);
3549
3550   *type_p = type;
3551
3552   if (init)
3553     TREE_TYPE (init) = type;
3554   else
3555     init = error_mark_node;
3556
3557   return init;
3558 }
3559
3560 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
3561    the decl, LOC is the location to give the decl, NAME is the
3562    initialization string and TYPE_DEP indicates whether NAME depended
3563    on the type of the function. We make use of that to detect
3564    __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
3565    at the point of first use, so we mustn't push the decl now.  */
3566
3567 static tree
3568 cp_make_fname_decl (location_t loc, tree id, int type_dep)
3569 {
3570   const char *const name = (type_dep && processing_template_decl
3571                             ? NULL : fname_as_string (type_dep));
3572   tree type;
3573   tree init = cp_fname_init (name, &type);
3574   tree decl = build_decl (loc, VAR_DECL, id, type);
3575
3576   if (name)
3577     free (CONST_CAST (char *, name));
3578
3579   /* As we're using pushdecl_with_scope, we must set the context.  */
3580   DECL_CONTEXT (decl) = current_function_decl;
3581   DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3582
3583   TREE_STATIC (decl) = 1;
3584   TREE_READONLY (decl) = 1;
3585   DECL_ARTIFICIAL (decl) = 1;
3586
3587   TREE_USED (decl) = 1;
3588
3589   if (current_function_decl)
3590     {
3591       struct cp_binding_level *b = current_binding_level;
3592       while (b->level_chain->kind != sk_function_parms)
3593         b = b->level_chain;
3594       pushdecl_with_scope (decl, b, /*is_friend=*/false);
3595       cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3596                       LOOKUP_ONLYCONVERTING);
3597     }
3598   else
3599     pushdecl_top_level_and_finish (decl, init);
3600
3601   return decl;
3602 }
3603
3604 static tree
3605 builtin_function_1 (tree decl, tree context, bool is_global)
3606 {
3607   tree          id = DECL_NAME (decl);
3608   const char *name = IDENTIFIER_POINTER (id);
3609
3610   retrofit_lang_decl (decl);
3611
3612   DECL_ARTIFICIAL (decl) = 1;
3613   SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3614   SET_DECL_LANGUAGE (decl, lang_c);
3615   /* Runtime library routines are, by definition, available in an
3616      external shared object.  */
3617   DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3618   DECL_VISIBILITY_SPECIFIED (decl) = 1;
3619
3620   DECL_CONTEXT (decl) = context;
3621
3622   if (is_global)
3623     pushdecl_top_level (decl);
3624   else
3625     pushdecl (decl);
3626
3627   /* A function in the user's namespace should have an explicit
3628      declaration before it is used.  Mark the built-in function as
3629      anticipated but not actually declared.  */
3630   if (name[0] != '_' || name[1] != '_')
3631     DECL_ANTICIPATED (decl) = 1;
3632   else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
3633     {
3634       size_t len = strlen (name);
3635
3636       /* Treat __*_chk fortification functions as anticipated as well,
3637          unless they are __builtin_*.  */
3638       if (len > strlen ("___chk")
3639           && memcmp (name + len - strlen ("_chk"),
3640                      "_chk", strlen ("_chk") + 1) == 0)
3641         DECL_ANTICIPATED (decl) = 1;
3642     }
3643
3644   return decl;
3645 }
3646
3647 tree
3648 cxx_builtin_function (tree decl)
3649 {
3650   tree          id = DECL_NAME (decl);
3651   const char *name = IDENTIFIER_POINTER (id);
3652   /* All builtins that don't begin with an '_' should additionally
3653      go in the 'std' namespace.  */
3654   if (name[0] != '_')
3655     {
3656       tree decl2 = copy_node(decl);
3657       push_namespace (std_identifier);
3658       builtin_function_1 (decl2, std_node, false);
3659       pop_namespace ();
3660     }
3661
3662   return builtin_function_1 (decl, NULL_TREE, false);
3663 }
3664
3665 /* Like cxx_builtin_function, but guarantee the function is added to the global
3666    scope.  This is to allow function specific options to add new machine
3667    dependent builtins when the target ISA changes via attribute((target(...)))
3668    which saves space on program startup if the program does not use non-generic
3669    ISAs.  */
3670
3671 tree
3672 cxx_builtin_function_ext_scope (tree decl)
3673 {
3674
3675   tree          id = DECL_NAME (decl);
3676   const char *name = IDENTIFIER_POINTER (id);
3677   /* All builtins that don't begin with an '_' should additionally
3678      go in the 'std' namespace.  */
3679   if (name[0] != '_')
3680     {
3681       tree decl2 = copy_node(decl);
3682       push_namespace (std_identifier);
3683       builtin_function_1 (decl2, std_node, true);
3684       pop_namespace ();
3685     }
3686
3687   return builtin_function_1 (decl, NULL_TREE, true);
3688 }
3689
3690 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3691    function.  Not called directly.  */
3692
3693 static tree
3694 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3695 {
3696   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3697   DECL_EXTERNAL (fn) = 1;
3698   TREE_PUBLIC (fn) = 1;
3699   DECL_ARTIFICIAL (fn) = 1;
3700   SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3701   SET_DECL_LANGUAGE (fn, lang_c);
3702   /* Runtime library routines are, by definition, available in an
3703      external shared object.  */
3704   DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3705   DECL_VISIBILITY_SPECIFIED (fn) = 1;
3706   return fn;
3707 }
3708
3709 /* Returns the _DECL for a library function with C linkage.
3710    We assume that such functions never throw; if this is incorrect,
3711    callers should unset TREE_NOTHROW.  */
3712
3713 static tree
3714 build_library_fn (tree name, tree type)
3715 {
3716   tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3717   TREE_NOTHROW (fn) = 1;
3718   return fn;
3719 }
3720
3721 /* Returns the _DECL for a library function with C++ linkage.  */
3722
3723 static tree
3724 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3725 {
3726   tree fn = build_library_fn_1 (name, operator_code, type);
3727   TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3728   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3729   SET_DECL_LANGUAGE (fn, lang_cplusplus);
3730   return fn;
3731 }
3732
3733 /* Like build_library_fn, but takes a C string instead of an
3734    IDENTIFIER_NODE.  */
3735
3736 tree
3737 build_library_fn_ptr (const char* name, tree type)
3738 {
3739   return build_library_fn (get_identifier (name), type);
3740 }
3741
3742 /* Like build_cp_library_fn, but takes a C string instead of an
3743    IDENTIFIER_NODE.  */
3744
3745 tree
3746 build_cp_library_fn_ptr (const char* name, tree type)
3747 {
3748   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3749 }
3750
3751 /* Like build_library_fn, but also pushes the function so that we will
3752    be able to find it via IDENTIFIER_GLOBAL_VALUE.  Also, the function
3753    may throw exceptions listed in RAISES.  */
3754
3755 tree
3756 push_library_fn (tree name, tree type, tree raises)
3757 {
3758   tree fn;
3759
3760   if (raises)
3761     type = build_exception_variant (type, raises);
3762
3763   fn = build_library_fn (name, type);
3764   pushdecl_top_level (fn);
3765   return fn;
3766 }
3767
3768 /* Like build_cp_library_fn, but also pushes the function so that it
3769    will be found by normal lookup.  */
3770
3771 static tree
3772 push_cp_library_fn (enum tree_code operator_code, tree type)
3773 {
3774   tree fn = build_cp_library_fn (ansi_opname (operator_code),
3775                                  operator_code,
3776                                  type);
3777   pushdecl (fn);
3778   return fn;
3779 }
3780
3781 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3782    a FUNCTION_TYPE.  */
3783
3784 tree
3785 push_void_library_fn (tree name, tree parmtypes)
3786 {
3787   tree type = build_function_type (void_type_node, parmtypes);
3788   return push_library_fn (name, type, NULL_TREE);
3789 }
3790
3791 /* Like push_library_fn, but also note that this function throws
3792    and does not return.  Used for __throw_foo and the like.  */
3793
3794 tree
3795 push_throw_library_fn (tree name, tree type)
3796 {
3797   tree fn = push_library_fn (name, type, NULL_TREE);
3798   TREE_THIS_VOLATILE (fn) = 1;
3799   TREE_NOTHROW (fn) = 0;
3800   return fn;
3801 }
3802 \f
3803 /* When we call finish_struct for an anonymous union, we create
3804    default copy constructors and such.  But, an anonymous union
3805    shouldn't have such things; this function undoes the damage to the
3806    anonymous union type T.
3807
3808    (The reason that we create the synthesized methods is that we don't
3809    distinguish `union { int i; }' from `typedef union { int i; } U'.
3810    The first is an anonymous union; the second is just an ordinary
3811    union type.)  */
3812
3813 void
3814 fixup_anonymous_aggr (tree t)
3815 {
3816   tree *q;
3817
3818   /* Wipe out memory of synthesized methods.  */
3819   TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
3820   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3821   TYPE_HAS_INIT_REF (t) = 0;
3822   TYPE_HAS_CONST_INIT_REF (t) = 0;
3823   TYPE_HAS_ASSIGN_REF (t) = 0;
3824   TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3825
3826   /* Splice the implicitly generated functions out of the TYPE_METHODS
3827      list.  */
3828   q = &TYPE_METHODS (t);
3829   while (*q)
3830     {
3831       if (DECL_ARTIFICIAL (*q))
3832         *q = TREE_CHAIN (*q);
3833       else
3834         q = &TREE_CHAIN (*q);
3835     }
3836
3837   /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
3838   if (TYPE_METHODS (t))
3839     {
3840       tree decl = TYPE_MAIN_DECL (t);
3841
3842       if (TREE_CODE (t) != UNION_TYPE)
3843         error_at (DECL_SOURCE_LOCATION (decl), 
3844                   "an anonymous struct cannot have function members");
3845       else
3846         error_at (DECL_SOURCE_LOCATION (decl),
3847                   "an anonymous union cannot have function members");
3848     }
3849
3850   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3851      assignment operators (because they cannot have these methods themselves).
3852      For anonymous unions this is already checked because they are not allowed
3853      in any union, otherwise we have to check it.  */
3854   if (TREE_CODE (t) != UNION_TYPE)
3855     {
3856       tree field, type;
3857
3858       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3859         if (TREE_CODE (field) == FIELD_DECL)
3860           {
3861             type = TREE_TYPE (field);
3862             if (CLASS_TYPE_P (type))
3863               {
3864                 if (TYPE_NEEDS_CONSTRUCTING (type))
3865                   error ("member %q+#D with constructor not allowed "
3866                          "in anonymous aggregate", field);
3867                 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3868                   error ("member %q+#D with destructor not allowed "
3869                          "in anonymous aggregate", field);
3870                 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3871                   error ("member %q+#D with copy assignment operator "
3872                          "not allowed in anonymous aggregate", field);
3873               }
3874           }
3875     }
3876 }
3877
3878 /* Make sure that a declaration with no declarator is well-formed, i.e.
3879    just declares a tagged type or anonymous union.
3880
3881    Returns the type declared; or NULL_TREE if none.  */
3882
3883 tree
3884 check_tag_decl (cp_decl_specifier_seq *declspecs)
3885 {
3886   int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3887   int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3888   /* If a class, struct, or enum type is declared by the DECLSPECS
3889      (i.e, if a class-specifier, enum-specifier, or non-typename
3890      elaborated-type-specifier appears in the DECLSPECS),
3891      DECLARED_TYPE is set to the corresponding type.  */
3892   tree declared_type = NULL_TREE;
3893   bool error_p = false;
3894
3895   if (declspecs->multiple_types_p)
3896     error ("multiple types in one declaration");
3897   else if (declspecs->redefined_builtin_type)
3898     {
3899       if (!in_system_header)
3900         permerror (input_location, "redeclaration of C++ built-in type %qT",
3901                    declspecs->redefined_builtin_type);
3902       return NULL_TREE;
3903     }
3904
3905   if (declspecs->type
3906       && TYPE_P (declspecs->type)
3907       && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
3908            && MAYBE_CLASS_TYPE_P (declspecs->type))
3909           || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3910     declared_type = declspecs->type;
3911   else if (declspecs->type == error_mark_node)
3912     error_p = true;
3913   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
3914     permerror (input_location, "declaration does not declare anything");
3915   /* Check for an anonymous union.  */
3916   else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
3917            && TYPE_ANONYMOUS_P (declared_type))
3918     {
3919       /* 7/3 In a simple-declaration, the optional init-declarator-list
3920          can be omitted only when declaring a class (clause 9) or
3921          enumeration (7.2), that is, when the decl-specifier-seq contains
3922          either a class-specifier, an elaborated-type-specifier with
3923          a class-key (9.1), or an enum-specifier.  In these cases and
3924          whenever a class-specifier or enum-specifier is present in the
3925          decl-specifier-seq, the identifiers in these specifiers are among
3926          the names being declared by the declaration (as class-name,
3927          enum-names, or enumerators, depending on the syntax).  In such
3928          cases, and except for the declaration of an unnamed bit-field (9.6),
3929          the decl-specifier-seq shall introduce one or more names into the
3930          program, or shall redeclare a name introduced by a previous
3931          declaration.  [Example:
3932              enum { };                  // ill-formed
3933              typedef class { };         // ill-formed
3934          --end example]  */
3935       if (saw_typedef)
3936         {
3937           error ("missing type-name in typedef-declaration");
3938           return NULL_TREE;
3939         }
3940       /* Anonymous unions are objects, so they can have specifiers.  */;
3941       SET_ANON_AGGR_TYPE_P (declared_type);
3942
3943       if (TREE_CODE (declared_type) != UNION_TYPE && !in_system_header)
3944         pedwarn (input_location, OPT_pedantic, "ISO C++ prohibits anonymous structs");
3945     }
3946
3947   else
3948     {
3949       if (declspecs->specs[(int)ds_inline]
3950           || declspecs->specs[(int)ds_virtual])
3951         error ("%qs can only be specified for functions",
3952                declspecs->specs[(int)ds_inline]
3953                ? "inline" : "virtual");
3954       else if (saw_friend
3955                && (!current_class_type
3956                    || current_scope () != current_class_type))
3957         error ("%<friend%> can only be specified inside a class");
3958       else if (declspecs->specs[(int)ds_explicit])
3959         error ("%<explicit%> can only be specified for constructors");
3960       else if (declspecs->storage_class)
3961         error ("a storage class can only be specified for objects "
3962                "and functions");
3963       else if (declspecs->specs[(int)ds_const]
3964                || declspecs->specs[(int)ds_volatile]
3965                || declspecs->specs[(int)ds_restrict]
3966                || declspecs->specs[(int)ds_thread])
3967         error ("qualifiers can only be specified for objects "
3968                "and functions");
3969       else if (saw_typedef)
3970         warning (0, "%<typedef%> was ignored in this declaration");
3971       else if (declspecs->specs[(int) ds_constexpr])
3972         error ("%<constexpr> cannot be used for type declarations");
3973     }
3974
3975   return declared_type;
3976 }
3977
3978 /* Called when a declaration is seen that contains no names to declare.
3979    If its type is a reference to a structure, union or enum inherited
3980    from a containing scope, shadow that tag name for the current scope
3981    with a forward reference.
3982    If its type defines a new named structure or union
3983    or defines an enum, it is valid but we need not do anything here.
3984    Otherwise, it is an error.
3985
3986    C++: may have to grok the declspecs to learn about static,
3987    complain for anonymous unions.
3988
3989    Returns the TYPE declared -- or NULL_TREE if none.  */
3990
3991 tree
3992 shadow_tag (cp_decl_specifier_seq *declspecs)
3993 {
3994   tree t = check_tag_decl (declspecs);
3995
3996   if (!t)
3997     return NULL_TREE;
3998
3999   if (declspecs->attributes)
4000     {
4001       warning (0, "attribute ignored in declaration of %q+#T", t);
4002       warning (0, "attribute for %q+#T must follow the %qs keyword",
4003                t, class_key_or_enum_as_string (t));
4004
4005     }
4006
4007   if (maybe_process_partial_specialization (t) == error_mark_node)
4008     return NULL_TREE;
4009
4010   /* This is where the variables in an anonymous union are
4011      declared.  An anonymous union declaration looks like:
4012      union { ... } ;
4013      because there is no declarator after the union, the parser
4014      sends that declaration here.  */
4015   if (ANON_AGGR_TYPE_P (t))
4016     {
4017       fixup_anonymous_aggr (t);
4018
4019       if (TYPE_FIELDS (t))
4020         {
4021           tree decl = grokdeclarator (/*declarator=*/NULL,
4022                                       declspecs, NORMAL, 0, NULL);
4023           finish_anon_union (decl);
4024         }
4025     }
4026
4027   return t;
4028 }
4029 \f
4030 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
4031
4032 tree
4033 groktypename (cp_decl_specifier_seq *type_specifiers,
4034               const cp_declarator *declarator,
4035               bool is_template_arg)
4036 {
4037   tree attrs;
4038   tree type;
4039   enum decl_context context
4040     = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4041   attrs = type_specifiers->attributes;
4042   type_specifiers->attributes = NULL_TREE;
4043   type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4044   if (attrs && type != error_mark_node)
4045     {
4046       if (CLASS_TYPE_P (type))
4047         warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4048                  "outside of definition", type);
4049       else if (MAYBE_CLASS_TYPE_P (type))
4050         /* A template type parameter or other dependent type.  */
4051         warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4052                  "type %qT without an associated declaration", type);
4053       else
4054         cplus_decl_attributes (&type, attrs, 0);
4055     }
4056   return type;
4057 }
4058
4059 /* Process a DECLARATOR for a function-scope variable declaration,
4060    namespace-scope variable declaration, or function declaration.
4061    (Function definitions go through start_function; class member
4062    declarations appearing in the body of the class go through
4063    grokfield.)  The DECL corresponding to the DECLARATOR is returned.
4064    If an error occurs, the error_mark_node is returned instead.
4065    
4066    DECLSPECS are the decl-specifiers for the declaration.  INITIALIZED is
4067    SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4068    for an explicitly defaulted function, or SD_DELETED for an explicitly
4069    deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4070    implicitly initialized via a default constructor.  ATTRIBUTES and
4071    PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4072    *PUSHED_SCOPE_P is set to the scope entered in this function, if any; if
4073    set, the caller is responsible for calling pop_scope.  */
4074
4075 tree
4076 start_decl (const cp_declarator *declarator,
4077             cp_decl_specifier_seq *declspecs,
4078             int initialized,
4079             tree attributes,
4080             tree prefix_attributes,
4081             tree *pushed_scope_p)
4082 {
4083   tree decl;
4084   tree context;
4085   bool was_public;
4086   int flags;
4087
4088   *pushed_scope_p = NULL_TREE;
4089
4090   /* An object declared as __attribute__((deprecated)) suppresses
4091      warnings of uses of other deprecated items.  */
4092   if (lookup_attribute ("deprecated", attributes))
4093     deprecated_state = DEPRECATED_SUPPRESS;
4094
4095   attributes = chainon (attributes, prefix_attributes);
4096
4097   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4098                          &attributes);
4099
4100   deprecated_state = DEPRECATED_NORMAL;
4101
4102   if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
4103       || decl == error_mark_node)
4104     return error_mark_node;
4105
4106   context = DECL_CONTEXT (decl);
4107
4108   if (context)
4109     {
4110       *pushed_scope_p = push_scope (context);
4111
4112       /* We are only interested in class contexts, later.  */
4113       if (TREE_CODE (context) == NAMESPACE_DECL)
4114         context = NULL_TREE;
4115     }
4116
4117   if (initialized)
4118     /* Is it valid for this decl to have an initializer at all?
4119        If not, set INITIALIZED to zero, which will indirectly
4120        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
4121     switch (TREE_CODE (decl))
4122       {
4123       case TYPE_DECL: