OSDN Git Service

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