OSDN Git Service

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