OSDN Git Service

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