1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
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. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
33 #include "coretypes.h"
38 #include "tree-iterator.h"
39 #include "tree-inline.h"
47 #include "c-family/c-common.h"
48 #include "c-family/c-objc.h"
49 #include "c-family/c-pragma.h"
50 #include "diagnostic.h"
54 #include "pointer-set.h"
55 #include "splay-tree.h"
58 /* Possible cases of bad specifiers type used by bad_specifiers. */
60 BSP_VAR, /* variable */
61 BSP_PARM, /* parameter */
66 static tree grokparms (tree parmlist, tree *);
67 static const char *redeclaration_error_message (tree, tree);
69 static int decl_jump_unsafe (tree);
70 static void require_complete_types_for_parms (tree);
71 static int ambi_op_p (enum tree_code);
72 static int unary_op_p (enum tree_code);
73 static void push_local_name (tree);
74 static tree grok_reference_init (tree, tree, tree, tree *);
75 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
77 static void record_unknown_type (tree, const char *);
78 static tree builtin_function_1 (tree, tree, bool);
79 static tree build_library_fn_1 (tree, enum tree_code, tree);
80 static int member_function_or_else (tree, tree, enum overload_flags);
81 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
83 static void check_for_uninitialized_const_var (tree);
84 static hashval_t typename_hash (const void *);
85 static int typename_compare (const void *, const void *);
86 static tree local_variable_p_walkfn (tree *, int *, void *);
87 static tree record_builtin_java_type (const char *, int);
88 static const char *tag_name (enum tag_types);
89 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
90 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
91 static void maybe_deduce_size_from_array_init (tree, tree);
92 static void layout_var_decl (tree);
93 static tree check_initializer (tree, tree, int, tree *);
94 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
95 static void save_function_data (tree);
96 static void copy_type_enum (tree , tree);
97 static void check_function_type (tree, tree);
98 static void finish_constructor_body (void);
99 static void begin_destructor_body (void);
100 static void finish_destructor_body (void);
101 static void record_key_method_defined (tree);
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 (location_t, 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);
117 /* The following symbols are subsumed in the cp_global_trees array, and
118 listed here individually for documentation purposes.
121 tree wchar_decl_node;
123 tree vtable_entry_type;
124 tree delta_type_node;
125 tree __t_desc_type_node;
127 tree class_type_node;
128 tree unknown_type_node;
130 Array type `vtable_entry_type[]'
133 tree vtbl_ptr_type_node;
140 A FUNCTION_DECL which can call `abort'. Not necessarily the
141 one that the user will declare, but sufficient to be called
142 by routines that want to abort the program.
146 The FUNCTION_DECL for the default `::operator delete'.
148 tree global_delete_fndecl;
151 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
152 tree tinfo_var_id; */
154 tree cp_global_trees[CPTI_MAX];
156 /* Indicates that there is a type value in some namespace, although
157 that is not necessarily in scope at the moment. */
159 tree global_type_node;
161 /* The node that holds the "name" of the global scope. */
162 tree global_scope_name;
164 #define local_names cp_function_chain->x_local_names
166 /* A list of objects which have constructors or destructors
167 which reside in the global scope. The decl is stored in
168 the TREE_VALUE slot and the initializer is stored
169 in the TREE_PURPOSE slot. */
170 tree static_aggregates;
174 /* A node for the integer constant 2. */
176 tree integer_two_node;
178 /* Used only for jumps to as-yet undefined labels, since jumps to
179 defined labels can have their validity checked immediately. */
181 struct GTY(()) named_label_use_entry {
182 struct named_label_use_entry *next;
183 /* The binding level to which this entry is *currently* attached.
184 This is initially the binding level in which the goto appeared,
185 but is modified as scopes are closed. */
186 struct cp_binding_level *binding_level;
187 /* The head of the names list that was current when the goto appeared,
188 or the inner scope popped. These are the decls that will *not* be
189 skipped when jumping to the label. */
191 /* The location of the goto, for error reporting. */
192 location_t o_goto_locus;
193 /* True if an OpenMP structured block scope has been closed since
194 the goto appeared. This means that the branch from the label will
195 illegally exit an OpenMP scope. */
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. */
203 struct GTY(()) named_label_entry {
204 /* The decl itself. */
207 /* The binding level to which the label is *currently* attached.
208 This is initially set to the binding level in which the label
209 is defined, but is modified as scopes are closed. */
210 struct cp_binding_level *binding_level;
211 /* The head of the names list that was current when the label was
212 defined, or the inner scope popped. These are the decls that will
213 be skipped when jumping to the label. */
215 /* A vector of all decls from all binding levels that would be
216 crossed by a backward branch to the label. */
217 VEC(tree,gc) *bad_decls;
219 /* A list of uses of the label, before the label is defined. */
220 struct named_label_use_entry *uses;
222 /* The following bits are set after the label is defined, and are
223 updated as scopes are popped. They indicate that a backward jump
224 to the label will illegally enter a scope of the given flavor. */
230 #define named_labels cp_function_chain->x_named_labels
232 /* The number of function bodies which we are currently processing.
233 (Zero if we are at namespace scope, one inside the body of a
234 function, two inside the body of a function in a local class, etc.) */
237 /* To avoid unwanted recursion, finish_function defers all mark_used calls
238 encountered during its execution until it finishes. */
239 bool defer_mark_used_calls;
240 VEC(tree, gc) *deferred_mark_used_calls;
242 /* States indicating how grokdeclarator() should handle declspecs marked
243 with __attribute__((deprecated)). An object declared as
244 __attribute__((deprecated)) suppresses warnings of uses of other
246 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
249 /* A list of VAR_DECLs whose type was incomplete at the time the
250 variable was declared. */
252 typedef struct GTY(()) incomplete_var_d {
254 tree incomplete_type;
257 DEF_VEC_O(incomplete_var);
258 DEF_VEC_ALLOC_O(incomplete_var,gc);
260 static GTY(()) VEC(incomplete_var,gc) *incomplete_vars;
262 /* Returns the kind of template specialization we are currently
263 processing, given that it's declaration contained N_CLASS_SCOPES
264 explicit scope qualifications. */
267 current_tmpl_spec_kind (int n_class_scopes)
269 int n_template_parm_scopes = 0;
270 int seen_specialization_p = 0;
271 int innermost_specialization_p = 0;
272 struct cp_binding_level *b;
274 /* Scan through the template parameter scopes. */
275 for (b = current_binding_level;
276 b->kind == sk_template_parms;
279 /* If we see a specialization scope inside a parameter scope,
280 then something is wrong. That corresponds to a declaration
283 template <class T> template <> ...
285 which is always invalid since [temp.expl.spec] forbids the
286 specialization of a class member template if the enclosing
287 class templates are not explicitly specialized as well. */
288 if (b->explicit_spec_p)
290 if (n_template_parm_scopes == 0)
291 innermost_specialization_p = 1;
293 seen_specialization_p = 1;
295 else if (seen_specialization_p == 1)
296 return tsk_invalid_member_spec;
298 ++n_template_parm_scopes;
301 /* Handle explicit instantiations. */
302 if (processing_explicit_instantiation)
304 if (n_template_parm_scopes != 0)
305 /* We've seen a template parameter list during an explicit
306 instantiation. For example:
308 template <class T> template void f(int);
310 This is erroneous. */
311 return tsk_invalid_expl_inst;
313 return tsk_expl_inst;
316 if (n_template_parm_scopes < n_class_scopes)
317 /* We've not seen enough template headers to match all the
318 specialized classes present. For example:
320 template <class T> void R<T>::S<T>::f(int);
322 This is invalid; there needs to be one set of template
323 parameters for each class. */
324 return tsk_insufficient_parms;
325 else if (n_template_parm_scopes == n_class_scopes)
326 /* We're processing a non-template declaration (even though it may
327 be a member of a template class.) For example:
329 template <class T> void S<T>::f(int);
331 The `class T' matches the `S<T>', leaving no template headers
332 corresponding to the `f'. */
334 else if (n_template_parm_scopes > n_class_scopes + 1)
335 /* We've got too many template headers. For example:
337 template <> template <class T> void f (T);
339 There need to be more enclosing classes. */
340 return tsk_excessive_parms;
342 /* This must be a template. It's of the form:
344 template <class T> template <class U> void S<T>::f(U);
346 This is a specialization if the innermost level was a
347 specialization; otherwise it's just a definition of the
349 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
352 /* Exit the current scope. */
360 /* When a label goes out of scope, check to see if that label was used
361 in a valid manner, and issue any appropriate warnings or errors. */
364 pop_label (tree label, tree old_value)
366 if (!processing_template_decl)
368 if (DECL_INITIAL (label) == NULL_TREE)
372 error ("label %q+D used but not defined", label);
373 location = input_location; /* FIXME want (input_filename, (line)0) */
374 /* Avoid crashing later. */
375 define_label (location, DECL_NAME (label));
378 warn_for_unused_label (label);
381 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
384 /* At the end of a function, all labels declared within the function
385 go out of scope. BLOCK is the top-level block for the
389 pop_labels_1 (void **slot, void *data)
391 struct named_label_entry *ent = (struct named_label_entry *) *slot;
392 tree block = (tree) data;
394 pop_label (ent->label_decl, NULL_TREE);
396 /* Put the labels into the "variables" of the top-level block,
397 so debugger can see them. */
398 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
399 BLOCK_VARS (block) = ent->label_decl;
401 htab_clear_slot (named_labels, slot);
407 pop_labels (tree block)
411 htab_traverse (named_labels, pop_labels_1, block);
416 /* At the end of a block with local labels, restore the outer definition. */
419 pop_local_label (tree label, tree old_value)
421 struct named_label_entry dummy;
424 pop_label (label, old_value);
426 dummy.label_decl = label;
427 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
428 htab_clear_slot (named_labels, slot);
431 /* The following two routines are used to interface to Objective-C++.
432 The binding level is purposely treated as an opaque type. */
435 objc_get_current_scope (void)
437 return current_binding_level;
440 /* The following routine is used by the NeXT-style SJLJ exceptions;
441 variables get marked 'volatile' so as to not be clobbered by
442 _setjmp()/_longjmp() calls. All variables in the current scope,
443 as well as parent scopes up to (but not including) ENCLOSING_BLK
444 shall be thusly marked. */
447 objc_mark_locals_volatile (void *enclosing_blk)
449 struct cp_binding_level *scope;
451 for (scope = current_binding_level;
452 scope && scope != enclosing_blk;
453 scope = scope->level_chain)
457 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
458 objc_volatilize_decl (decl);
460 /* Do not climb up past the current function. */
461 if (scope->kind == sk_function_parms)
466 /* Update data for defined and undefined labels when leaving a scope. */
469 poplevel_named_label_1 (void **slot, void *data)
471 struct named_label_entry *ent = (struct named_label_entry *) *slot;
472 struct cp_binding_level *bl = (struct cp_binding_level *) data;
473 struct cp_binding_level *obl = bl->level_chain;
475 if (ent->binding_level == bl)
479 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
480 TREE_LISTs representing OVERLOADs, so be careful. */
481 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
483 : TREE_CHAIN (decl)))
484 if (decl_jump_unsafe (decl))
485 VEC_safe_push (tree, gc, ent->bad_decls, decl);
487 ent->binding_level = obl;
488 ent->names_in_scope = obl->names;
492 ent->in_try_scope = true;
495 ent->in_catch_scope = true;
498 ent->in_omp_scope = true;
506 struct named_label_use_entry *use;
508 for (use = ent->uses; use ; use = use->next)
509 if (use->binding_level == bl)
511 use->binding_level = obl;
512 use->names_in_scope = obl->names;
513 if (bl->kind == sk_omp)
514 use->in_omp_scope = true;
521 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
522 when errors were reported, except for -Werror-unused-but-set-*. */
523 static int unused_but_set_errorcount;
525 /* Exit a binding level.
526 Pop the level off, and restore the state of the identifier-decl mappings
527 that were in effect when this level was entered.
529 If KEEP == 1, this level had explicit declarations, so
530 and create a "block" (a BLOCK node) for the level
531 to record its declarations and subblocks for symbol table output.
533 If FUNCTIONBODY is nonzero, this level is the body of a function,
534 so create a block as if KEEP were set and also clear out all
537 If REVERSE is nonzero, reverse the order of decls before putting
538 them into the BLOCK. */
541 poplevel (int keep, int reverse, int functionbody)
544 /* The chain of decls was accumulated in reverse order.
545 Put it into forward order, just for cleanliness. */
550 int leaving_for_scope;
553 cp_label_binding *label_bind;
555 timevar_push (TV_NAME_LOOKUP);
560 gcc_assert (current_binding_level->kind != sk_class);
562 if (current_binding_level->kind == sk_cleanup)
564 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
566 gcc_assert (!VEC_length(cp_class_binding,
567 current_binding_level->class_shadowed));
569 /* We used to use KEEP == 2 to indicate that the new block should go
570 at the beginning of the list of blocks at this binding level,
571 rather than the end. This hack is no longer used. */
572 gcc_assert (keep == 0 || keep == 1);
574 if (current_binding_level->keep)
577 /* Any uses of undefined labels, and any defined labels, now operate
578 under constraints of next binding contour. */
579 if (cfun && !functionbody && named_labels)
580 htab_traverse (named_labels, poplevel_named_label_1,
581 current_binding_level);
583 /* Get the decls in the order they were written.
584 Usually current_binding_level->names is in reverse order.
585 But parameter decls were previously put in forward order. */
588 current_binding_level->names
589 = decls = nreverse (current_binding_level->names);
591 decls = current_binding_level->names;
593 /* If there were any declarations or structure tags in that level,
594 or if this level is a function body,
595 create a BLOCK to record them for the life of this function. */
597 if (keep == 1 || functionbody)
598 block = make_node (BLOCK);
599 if (block != NULL_TREE)
601 BLOCK_VARS (block) = decls;
602 BLOCK_SUBBLOCKS (block) = subblocks;
605 /* In each subblock, record that this is its superior. */
607 for (link = subblocks; link; link = BLOCK_CHAIN (link))
608 BLOCK_SUPERCONTEXT (link) = block;
610 /* We still support the old for-scope rules, whereby the variables
611 in a for-init statement were in scope after the for-statement
612 ended. We only use the new rules if flag_new_for_scope is
615 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
617 /* Before we remove the declarations first check for unused variables. */
618 if ((warn_unused_variable || warn_unused_but_set_variable)
619 && !processing_template_decl)
620 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
621 if (TREE_CODE (decl) == VAR_DECL
622 && (! TREE_USED (decl) || !DECL_READ_P (decl))
623 && ! DECL_IN_SYSTEM_HEADER (decl)
624 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
626 if (! TREE_USED (decl))
627 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
628 else if (DECL_CONTEXT (decl) == current_function_decl
629 && TREE_TYPE (decl) != error_mark_node
630 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
631 && errorcount == unused_but_set_errorcount
632 && (!CLASS_TYPE_P (TREE_TYPE (decl))
633 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
635 warning (OPT_Wunused_but_set_variable,
636 "variable %q+D set but not used", decl);
637 unused_but_set_errorcount = errorcount;
641 /* Remove declarations for all the DECLs in this level. */
642 for (link = decls; link; link = TREE_CHAIN (link))
644 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
647 tree name = DECL_NAME (link);
651 ob = outer_binding (name,
652 IDENTIFIER_BINDING (name),
655 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
657 ns_binding = NULL_TREE;
659 if (ob && ob->scope == current_binding_level->level_chain)
660 /* We have something like:
665 and we are leaving the `for' scope. There's no reason to
666 keep the binding of the inner `i' in this case. */
667 pop_binding (name, link);
668 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
669 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
670 /* Here, we have something like:
678 We must pop the for-scope binding so we know what's a
679 type and what isn't. */
680 pop_binding (name, link);
683 /* Mark this VAR_DECL as dead so that we can tell we left it
684 there only for backward compatibility. */
685 DECL_DEAD_FOR_LOCAL (link) = 1;
687 /* Keep track of what should have happened when we
688 popped the binding. */
691 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
692 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
695 /* Add it to the list of dead variables in the next
696 outermost binding to that we can remove these when we
697 leave that binding. */
698 VEC_safe_push (tree, gc,
699 current_binding_level->level_chain->dead_vars_from_for,
702 /* Although we don't pop the cxx_binding, we do clear
703 its SCOPE since the scope is going away now. */
704 IDENTIFIER_BINDING (name)->scope
705 = current_binding_level->level_chain;
712 /* Remove the binding. */
715 if (TREE_CODE (decl) == TREE_LIST)
716 decl = TREE_VALUE (decl);
719 if (TREE_CODE (name) == OVERLOAD)
720 name = OVL_FUNCTION (name);
722 gcc_assert (DECL_P (name));
723 pop_binding (DECL_NAME (name), decl);
727 /* Remove declarations for any `for' variables from inner scopes
728 that we kept around. */
729 FOR_EACH_VEC_ELT_REVERSE (tree, current_binding_level->dead_vars_from_for,
731 pop_binding (DECL_NAME (decl), decl);
733 /* Restore the IDENTIFIER_TYPE_VALUEs. */
734 for (link = current_binding_level->type_shadowed;
735 link; link = TREE_CHAIN (link))
736 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
738 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
739 FOR_EACH_VEC_ELT_REVERSE (cp_label_binding,
740 current_binding_level->shadowed_labels,
742 pop_local_label (label_bind->label, label_bind->prev_value);
744 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
745 list if a `using' declaration put them there. The debugging
746 back ends won't understand OVERLOAD, so we remove them here.
747 Because the BLOCK_VARS are (temporarily) shared with
748 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
749 popped all the bindings. */
754 for (d = &BLOCK_VARS (block); *d; )
756 if (TREE_CODE (*d) == TREE_LIST)
757 *d = TREE_CHAIN (*d);
759 d = &DECL_CHAIN (*d);
763 /* If the level being exited is the top level of a function,
764 check over all the labels. */
767 /* Since this is the top level block of a function, the vars are
768 the function's parameters. Don't leave them in the BLOCK
769 because they are found in the FUNCTION_DECL instead. */
770 BLOCK_VARS (block) = 0;
774 kind = current_binding_level->kind;
775 if (kind == sk_cleanup)
779 /* If this is a temporary binding created for a cleanup, then we'll
780 have pushed a statement list level. Pop that, create a new
781 BIND_EXPR for the block, and insert it into the stream. */
782 stmt = pop_stmt_list (current_binding_level->statement_list);
783 stmt = c_build_bind_expr (input_location, block, stmt);
790 /* The current function is being defined, so its DECL_INITIAL
791 should be error_mark_node. */
792 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
793 DECL_INITIAL (current_function_decl) = block;
796 current_binding_level->blocks
797 = chainon (current_binding_level->blocks, block);
799 /* If we did not make a block for the level just exited,
800 any blocks made for inner levels
801 (since they cannot be recorded as subblocks in that level)
802 must be carried forward so they will later become subblocks
803 of something else. */
805 current_binding_level->blocks
806 = chainon (current_binding_level->blocks, subblocks);
808 /* Each and every BLOCK node created here in `poplevel' is important
809 (e.g. for proper debugging information) so if we created one
810 earlier, mark it as "used". */
812 TREE_USED (block) = 1;
814 /* All temporary bindings created for cleanups are popped silently. */
815 if (kind == sk_cleanup)
818 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
821 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
822 itself, calling F for each. The DATA is passed to F as well. */
825 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
828 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
830 result |= (*f) (name_space, data);
832 for (; current; current = DECL_CHAIN (current))
833 result |= walk_namespaces_r (current, f, data);
838 /* Walk all the namespaces, calling F for each. The DATA is passed to
842 walk_namespaces (walk_namespaces_fn f, void* data)
844 return walk_namespaces_r (global_namespace, f, data);
847 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
848 DATA is non-NULL, this is the last time we will call
849 wrapup_global_declarations for this NAMESPACE. */
852 wrapup_globals_for_namespace (tree name_space, void* data)
854 struct cp_binding_level *level = NAMESPACE_LEVEL (name_space);
855 VEC(tree,gc) *statics = level->static_decls;
856 tree *vec = VEC_address (tree, statics);
857 int len = VEC_length (tree, statics);
858 int last_time = (data != 0);
862 check_global_declarations (vec, len);
863 emit_debug_global_declarations (vec, len);
867 /* Write out any globals that need to be output. */
868 return wrapup_global_declarations (vec, len);
872 /* In C++, you don't have to write `struct S' to refer to `S'; you
873 can just use `S'. We accomplish this by creating a TYPE_DECL as
874 if the user had written `typedef struct S S'. Create and return
875 the TYPE_DECL for TYPE. */
878 create_implicit_typedef (tree name, tree type)
882 decl = build_decl (input_location, TYPE_DECL, name, type);
883 DECL_ARTIFICIAL (decl) = 1;
884 /* There are other implicit type declarations, like the one *within*
885 a class that allows you to write `S::S'. We must distinguish
887 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
888 TYPE_NAME (type) = decl;
889 TYPE_STUB_DECL (type) = decl;
894 /* Remember a local name for name-mangling purposes. */
897 push_local_name (tree decl)
902 timevar_push (TV_NAME_LOOKUP);
904 name = DECL_NAME (decl);
906 nelts = VEC_length (tree, local_names);
907 for (i = 0; i < nelts; i++)
909 t = VEC_index (tree, local_names, i);
910 if (DECL_NAME (t) == name)
912 if (!DECL_LANG_SPECIFIC (decl))
913 retrofit_lang_decl (decl);
914 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
915 if (DECL_LANG_SPECIFIC (t))
916 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
918 DECL_DISCRIMINATOR (decl) = 1;
920 VEC_replace (tree, local_names, i, decl);
921 timevar_pop (TV_NAME_LOOKUP);
926 VEC_safe_push (tree, gc, local_names, decl);
927 timevar_pop (TV_NAME_LOOKUP);
930 /* Subroutine of duplicate_decls: return truthvalue of whether
931 or not types of these decls match.
933 For C++, we must compare the parameter list so that `int' can match
934 `int&' in a parameter position, but `int&' is not confused with
938 decls_match (tree newdecl, tree olddecl)
942 if (newdecl == olddecl)
945 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
946 /* If the two DECLs are not even the same kind of thing, we're not
947 interested in their types. */
950 if (TREE_CODE (newdecl) == FUNCTION_DECL)
952 tree f1 = TREE_TYPE (newdecl);
953 tree f2 = TREE_TYPE (olddecl);
954 tree p1 = TYPE_ARG_TYPES (f1);
955 tree p2 = TYPE_ARG_TYPES (f2);
957 /* Specializations of different templates are different functions
958 even if they have the same type. */
959 tree t1 = (DECL_USE_TEMPLATE (newdecl)
960 ? DECL_TI_TEMPLATE (newdecl)
962 tree t2 = (DECL_USE_TEMPLATE (olddecl)
963 ? DECL_TI_TEMPLATE (olddecl)
968 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
969 && ! (DECL_EXTERN_C_P (newdecl)
970 && DECL_EXTERN_C_P (olddecl)))
973 #ifdef NO_IMPLICIT_EXTERN_C
974 /* A new declaration doesn't match a built-in one unless it
975 is also extern "C". */
976 if (DECL_IS_BUILTIN (olddecl)
977 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
981 if (TREE_CODE (f1) != TREE_CODE (f2))
984 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
986 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
987 && (DECL_BUILT_IN (olddecl)
988 #ifndef NO_IMPLICIT_EXTERN_C
989 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
990 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
994 types_match = self_promoting_args_p (p1);
995 if (p1 == void_list_node)
996 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
998 #ifndef NO_IMPLICIT_EXTERN_C
999 else if (p1 == NULL_TREE
1000 && (DECL_EXTERN_C_P (olddecl)
1001 && DECL_IN_SYSTEM_HEADER (olddecl)
1002 && !DECL_CLASS_SCOPE_P (olddecl))
1003 && (DECL_EXTERN_C_P (newdecl)
1004 && DECL_IN_SYSTEM_HEADER (newdecl)
1005 && !DECL_CLASS_SCOPE_P (newdecl)))
1007 types_match = self_promoting_args_p (p2);
1008 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1012 types_match = compparms (p1, p2);
1017 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1019 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1020 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1023 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1024 DECL_TEMPLATE_PARMS (olddecl)))
1027 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1028 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1029 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1031 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1032 DECL_TEMPLATE_RESULT (newdecl));
1036 /* Need to check scope for variable declaration (VAR_DECL).
1037 For typedef (TYPE_DECL), scope is ignored. */
1038 if (TREE_CODE (newdecl) == VAR_DECL
1039 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1041 Two declarations for an object with C language linkage
1042 with the same name (ignoring the namespace that qualify
1043 it) that appear in different namespace scopes refer to
1045 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1048 if (TREE_TYPE (newdecl) == error_mark_node)
1049 types_match = TREE_TYPE (olddecl) == error_mark_node;
1050 else if (TREE_TYPE (olddecl) == NULL_TREE)
1051 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1052 else if (TREE_TYPE (newdecl) == NULL_TREE)
1055 types_match = comptypes (TREE_TYPE (newdecl),
1056 TREE_TYPE (olddecl),
1057 COMPARE_REDECLARATION);
1063 /* If NEWDECL is `static' and an `extern' was seen previously,
1064 warn about it. OLDDECL is the previous declaration.
1066 Note that this does not apply to the C++ case of declaring
1067 a variable `extern const' and then later `const'.
1069 Don't complain about built-in functions, since they are beyond
1070 the user's control. */
1073 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1075 if (TREE_CODE (newdecl) == TYPE_DECL
1076 || TREE_CODE (newdecl) == TEMPLATE_DECL
1077 || TREE_CODE (newdecl) == CONST_DECL
1078 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1081 /* Don't get confused by static member functions; that's a different
1083 if (TREE_CODE (newdecl) == FUNCTION_DECL
1084 && DECL_STATIC_FUNCTION_P (newdecl))
1087 /* If the old declaration was `static', or the new one isn't, then
1088 then everything is OK. */
1089 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1092 /* It's OK to declare a builtin function as `static'. */
1093 if (TREE_CODE (olddecl) == FUNCTION_DECL
1094 && DECL_ARTIFICIAL (olddecl))
1097 permerror (input_location, "%qD was declared %<extern%> and later %<static%>", newdecl);
1098 permerror (input_location, "previous declaration of %q+D", olddecl);
1101 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1102 function templates. If their exception specifications do not
1103 match, issue a diagnostic. */
1106 check_redeclaration_exception_specification (tree new_decl,
1111 tree new_exceptions;
1112 tree old_exceptions;
1114 new_type = TREE_TYPE (new_decl);
1115 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1116 old_type = TREE_TYPE (old_decl);
1117 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1121 If any declaration of a function has an exception-specification,
1122 all declarations, including the definition and an explicit
1123 specialization, of that function shall have an
1124 exception-specification with the same set of type-ids. */
1125 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1126 && ! DECL_IS_BUILTIN (old_decl)
1128 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1130 error ("declaration of %qF has a different exception specifier",
1132 error ("from previous declaration %q+F", old_decl);
1136 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1137 Otherwise issue diagnostics. */
1140 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1142 old_decl = STRIP_TEMPLATE (old_decl);
1143 new_decl = STRIP_TEMPLATE (new_decl);
1144 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1145 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1147 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1148 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1150 if (TREE_CODE (old_decl) == FUNCTION_DECL && DECL_BUILT_IN (old_decl))
1152 /* Hide a built-in declaration. */
1153 DECL_DECLARED_CONSTEXPR_P (old_decl)
1154 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1157 error ("redeclaration %qD differs in %<constexpr%>", new_decl);
1158 error ("from previous declaration %q+D", old_decl);
1162 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1163 && lookup_attribute ("gnu_inline", \
1164 DECL_ATTRIBUTES (fn)))
1166 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1167 If the redeclaration is invalid, a diagnostic is issued, and the
1168 error_mark_node is returned. Otherwise, OLDDECL is returned.
1170 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1173 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1176 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1178 unsigned olddecl_uid = DECL_UID (olddecl);
1179 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1180 int new_defines_function = 0;
1181 tree new_template_info;
1183 if (newdecl == olddecl)
1186 types_match = decls_match (newdecl, olddecl);
1188 /* If either the type of the new decl or the type of the old decl is an
1189 error_mark_node, then that implies that we have already issued an
1190 error (earlier) for some bogus type specification, and in that case,
1191 it is rather pointless to harass the user with yet more error message
1192 about the same declaration, so just pretend the types match here. */
1193 if (TREE_TYPE (newdecl) == error_mark_node
1194 || TREE_TYPE (olddecl) == error_mark_node)
1195 return error_mark_node;
1197 if (DECL_P (olddecl)
1198 && TREE_CODE (newdecl) == FUNCTION_DECL
1199 && TREE_CODE (olddecl) == FUNCTION_DECL
1200 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1202 if (DECL_DECLARED_INLINE_P (newdecl)
1203 && DECL_UNINLINABLE (newdecl)
1204 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1205 /* Already warned elsewhere. */;
1206 else if (DECL_DECLARED_INLINE_P (olddecl)
1207 && DECL_UNINLINABLE (olddecl)
1208 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1209 /* Already warned. */;
1210 else if (DECL_DECLARED_INLINE_P (newdecl)
1211 && DECL_UNINLINABLE (olddecl)
1212 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1214 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1216 warning (OPT_Wattributes, "previous declaration of %q+D "
1217 "with attribute noinline", olddecl);
1219 else if (DECL_DECLARED_INLINE_P (olddecl)
1220 && DECL_UNINLINABLE (newdecl)
1221 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1223 warning (OPT_Wattributes, "function %q+D redeclared with "
1224 "attribute noinline", newdecl);
1225 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1230 /* Check for redeclaration and other discrepancies. */
1231 if (TREE_CODE (olddecl) == FUNCTION_DECL
1232 && DECL_ARTIFICIAL (olddecl))
1234 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1235 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1237 /* Avoid warnings redeclaring built-ins which have not been
1238 explicitly declared. */
1239 if (DECL_ANTICIPATED (olddecl))
1242 /* If you declare a built-in or predefined function name as static,
1243 the old definition is overridden, but optionally warn this was a
1244 bad choice of name. */
1245 if (! TREE_PUBLIC (newdecl))
1247 warning (OPT_Wshadow,
1248 DECL_BUILT_IN (olddecl)
1249 ? G_("shadowing built-in function %q#D")
1250 : G_("shadowing library function %q#D"), olddecl);
1251 /* Discard the old built-in function. */
1254 /* If the built-in is not ansi, then programs can override
1255 it even globally without an error. */
1256 else if (! DECL_BUILT_IN (olddecl))
1257 warning (0, "library function %q#D redeclared as non-function %q#D",
1261 error ("declaration of %q#D", newdecl);
1262 error ("conflicts with built-in declaration %q#D",
1267 else if (!types_match)
1269 /* Avoid warnings redeclaring built-ins which have not been
1270 explicitly declared. */
1271 if (DECL_ANTICIPATED (olddecl))
1273 /* Deal with fileptr_type_node. FILE type is not known
1274 at the time we create the builtins. */
1277 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1278 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1280 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1283 else if (TREE_VALUE (t2) == fileptr_type_node)
1285 tree t = TREE_VALUE (t1);
1287 if (TREE_CODE (t) == POINTER_TYPE
1288 && TYPE_NAME (TREE_TYPE (t))
1289 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1290 == get_identifier ("FILE")
1291 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1293 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1295 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1296 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1297 types_match = decls_match (newdecl, olddecl);
1299 return duplicate_decls (newdecl, olddecl,
1301 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1304 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1307 else if ((DECL_EXTERN_C_P (newdecl)
1308 && DECL_EXTERN_C_P (olddecl))
1309 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1310 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1312 /* A near match; override the builtin. */
1314 if (TREE_PUBLIC (newdecl))
1316 warning (0, "new declaration %q#D", newdecl);
1317 warning (0, "ambiguates built-in declaration %q#D",
1321 warning (OPT_Wshadow,
1322 DECL_BUILT_IN (olddecl)
1323 ? G_("shadowing built-in function %q#D")
1324 : G_("shadowing library function %q#D"), olddecl);
1327 /* Discard the old built-in function. */
1330 /* Replace the old RTL to avoid problems with inlining. */
1331 COPY_DECL_RTL (newdecl, olddecl);
1333 /* Even if the types match, prefer the new declarations type for
1334 built-ins which have not been explicitly declared, for
1335 exception lists, etc... */
1336 else if (DECL_IS_BUILTIN (olddecl))
1338 tree type = TREE_TYPE (newdecl);
1339 tree attribs = (*targetm.merge_type_attributes)
1340 (TREE_TYPE (olddecl), type);
1342 type = cp_build_type_attribute_variant (type, attribs);
1343 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1346 /* If a function is explicitly declared "throw ()", propagate that to
1347 the corresponding builtin. */
1348 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1349 && DECL_ANTICIPATED (olddecl)
1350 && TREE_NOTHROW (newdecl)
1351 && !TREE_NOTHROW (olddecl)
1352 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != NULL_TREE
1353 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != olddecl
1355 TREE_NOTHROW (built_in_decls [DECL_FUNCTION_CODE (olddecl)]) = 1;
1357 /* Whether or not the builtin can throw exceptions has no
1358 bearing on this declarator. */
1359 TREE_NOTHROW (olddecl) = 0;
1361 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1363 /* If a builtin function is redeclared as `static', merge
1364 the declarations, but make the original one static. */
1365 DECL_THIS_STATIC (olddecl) = 1;
1366 TREE_PUBLIC (olddecl) = 0;
1368 /* Make the old declaration consistent with the new one so
1369 that all remnants of the builtin-ness of this function
1370 will be banished. */
1371 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1372 COPY_DECL_RTL (newdecl, olddecl);
1375 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1377 /* C++ Standard, 3.3, clause 4:
1378 "[Note: a namespace name or a class template name must be unique
1379 in its declarative region (7.3.2, clause 14). ]" */
1380 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1381 && TREE_CODE (newdecl) != NAMESPACE_DECL
1382 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1383 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1384 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1385 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1387 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1388 && TREE_CODE (newdecl) != TYPE_DECL)
1389 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1390 && TREE_CODE (olddecl) != TYPE_DECL))
1392 /* We do nothing special here, because C++ does such nasty
1393 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1394 get shadowed, and know that if we need to find a TYPE_DECL
1395 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1396 slot of the identifier. */
1400 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1401 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1402 || (TREE_CODE (olddecl) == FUNCTION_DECL
1403 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1407 error ("%q#D redeclared as different kind of symbol", newdecl);
1408 if (TREE_CODE (olddecl) == TREE_LIST)
1409 olddecl = TREE_VALUE (olddecl);
1410 error ("previous declaration of %q+#D", olddecl);
1412 return error_mark_node;
1414 else if (!types_match)
1416 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1417 /* These are certainly not duplicate declarations; they're
1418 from different scopes. */
1421 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1423 /* The name of a class template may not be declared to refer to
1424 any other template, class, function, object, namespace, value,
1425 or type in the same scope. */
1426 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1427 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1429 error ("declaration of template %q#D", newdecl);
1430 error ("conflicts with previous declaration %q+#D", olddecl);
1432 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1433 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1434 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1435 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1436 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1437 DECL_TEMPLATE_PARMS (olddecl))
1438 /* Template functions can be disambiguated by
1440 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1441 TREE_TYPE (TREE_TYPE (olddecl))))
1443 error ("new declaration %q#D", newdecl);
1444 error ("ambiguates old declaration %q+#D", olddecl);
1448 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1450 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1452 error ("declaration of C function %q#D conflicts with",
1454 error ("previous declaration %q+#D here", olddecl);
1456 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1457 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1459 error ("new declaration %q#D", newdecl);
1460 error ("ambiguates old declaration %q+#D", olddecl);
1461 return error_mark_node;
1468 error ("conflicting declaration %q#D", newdecl);
1469 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1470 return error_mark_node;
1473 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1474 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1475 && (!DECL_TEMPLATE_INFO (newdecl)
1476 || (DECL_TI_TEMPLATE (newdecl)
1477 != DECL_TI_TEMPLATE (olddecl))))
1478 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1479 && (!DECL_TEMPLATE_INFO (olddecl)
1480 || (DECL_TI_TEMPLATE (olddecl)
1481 != DECL_TI_TEMPLATE (newdecl))))))
1482 /* It's OK to have a template specialization and a non-template
1483 with the same type, or to have specializations of two
1484 different templates with the same type. Note that if one is a
1485 specialization, and the other is an instantiation of the same
1486 template, that we do not exit at this point. That situation
1487 can occur if we instantiate a template class, and then
1488 specialize one of its methods. This situation is valid, but
1489 the declarations must be merged in the usual way. */
1491 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1492 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1493 && !DECL_USE_TEMPLATE (newdecl))
1494 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1495 && !DECL_USE_TEMPLATE (olddecl))))
1496 /* One of the declarations is a template instantiation, and the
1497 other is not a template at all. That's OK. */
1499 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1501 /* In [namespace.alias] we have:
1503 In a declarative region, a namespace-alias-definition can be
1504 used to redefine a namespace-alias declared in that declarative
1505 region to refer only to the namespace to which it already
1508 Therefore, if we encounter a second alias directive for the same
1509 alias, we can just ignore the second directive. */
1510 if (DECL_NAMESPACE_ALIAS (newdecl)
1511 && (DECL_NAMESPACE_ALIAS (newdecl)
1512 == DECL_NAMESPACE_ALIAS (olddecl)))
1514 /* [namespace.alias]
1516 A namespace-name or namespace-alias shall not be declared as
1517 the name of any other entity in the same declarative region.
1518 A namespace-name defined at global scope shall not be
1519 declared as the name of any other entity in any global scope
1521 error ("declaration of namespace %qD conflicts with", newdecl);
1522 error ("previous declaration of namespace %q+D here", olddecl);
1523 return error_mark_node;
1527 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1530 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1531 if (DECL_NAME (olddecl) != NULL_TREE)
1532 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1533 ? "%q+#D previously defined here"
1534 : "%q+#D previously declared here", olddecl);
1535 return error_mark_node;
1537 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1538 && DECL_INITIAL (olddecl) != NULL_TREE
1539 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1540 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1542 /* Prototype decl follows defn w/o prototype. */
1543 warning_at (input_location, 0, "prototype for %q+#D", newdecl);
1544 warning_at (DECL_SOURCE_LOCATION (olddecl), 0,
1545 "follows non-prototype definition here");
1547 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1548 || TREE_CODE (olddecl) == VAR_DECL)
1549 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1552 If two declarations of the same function or object
1553 specify different linkage-specifications ..., the program
1554 is ill-formed.... Except for functions with C++ linkage,
1555 a function declaration without a linkage specification
1556 shall not precede the first linkage specification for
1557 that function. A function can be declared without a
1558 linkage specification after an explicit linkage
1559 specification has been seen; the linkage explicitly
1560 specified in the earlier declaration is not affected by
1561 such a function declaration.
1563 DR 563 raises the question why the restrictions on
1564 functions should not also apply to objects. Older
1565 versions of G++ silently ignore the linkage-specification
1573 which is clearly wrong. Therefore, we now treat objects
1575 if (current_lang_depth () == 0)
1577 /* There is no explicit linkage-specification, so we use
1578 the linkage from the previous declaration. */
1579 if (!DECL_LANG_SPECIFIC (newdecl))
1580 retrofit_lang_decl (newdecl);
1581 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1585 error ("previous declaration of %q+#D with %qL linkage",
1586 olddecl, DECL_LANGUAGE (olddecl));
1587 error ("conflicts with new declaration with %qL linkage",
1588 DECL_LANGUAGE (newdecl));
1592 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1594 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1596 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1597 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1600 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1601 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1603 for (; t1 && t1 != void_list_node;
1604 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1605 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1607 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1610 permerror (input_location, "default argument given for parameter %d of %q#D",
1612 permerror (input_location, "after previous specification in %q+#D", olddecl);
1616 error ("default argument given for parameter %d of %q#D",
1618 error ("after previous specification in %q+#D",
1625 /* Do not merge an implicit typedef with an explicit one. In:
1629 typedef class A A __attribute__ ((foo));
1631 the attribute should apply only to the typedef. */
1632 if (TREE_CODE (olddecl) == TYPE_DECL
1633 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1634 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1637 /* If new decl is `static' and an `extern' was seen previously,
1639 warn_extern_redeclared_static (newdecl, olddecl);
1641 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1642 return error_mark_node;
1644 /* We have committed to returning 1 at this point. */
1645 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1647 /* Now that functions must hold information normally held
1648 by field decls, there is extra work to do so that
1649 declaration information does not get destroyed during
1651 if (DECL_VINDEX (olddecl))
1652 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1653 if (DECL_CONTEXT (olddecl))
1654 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1655 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1656 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1657 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1658 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1659 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1660 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1661 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1662 SET_OVERLOADED_OPERATOR_CODE
1663 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1664 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1666 /* Optionally warn about more than one declaration for the same
1667 name, but don't warn about a function declaration followed by a
1669 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1670 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1671 /* Don't warn about extern decl followed by definition. */
1672 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1673 /* Don't warn about friends, let add_friend take care of it. */
1674 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1676 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1677 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1680 if (DECL_DELETED_FN (newdecl))
1682 error ("deleted definition of %qD", newdecl);
1683 error ("after previous declaration %q+D", olddecl);
1687 /* Deal with C++: must preserve virtual function table size. */
1688 if (TREE_CODE (olddecl) == TYPE_DECL)
1690 tree newtype = TREE_TYPE (newdecl);
1691 tree oldtype = TREE_TYPE (olddecl);
1693 if (newtype != error_mark_node && oldtype != error_mark_node
1694 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1695 CLASSTYPE_FRIEND_CLASSES (newtype)
1696 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1698 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1701 /* Copy all the DECL_... slots specified in the new decl
1702 except for any that we copy here from the old type. */
1703 DECL_ATTRIBUTES (newdecl)
1704 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1706 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1710 old_result = DECL_TEMPLATE_RESULT (olddecl);
1711 new_result = DECL_TEMPLATE_RESULT (newdecl);
1712 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1713 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1714 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1715 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1717 DECL_ATTRIBUTES (old_result)
1718 = (*targetm.merge_decl_attributes) (old_result, new_result);
1720 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1722 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1723 && DECL_INITIAL (new_result))
1725 if (DECL_INITIAL (old_result))
1726 DECL_UNINLINABLE (old_result) = 1;
1728 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1729 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1730 DECL_NOT_REALLY_EXTERN (old_result)
1731 = DECL_NOT_REALLY_EXTERN (new_result);
1732 DECL_INTERFACE_KNOWN (old_result)
1733 = DECL_INTERFACE_KNOWN (new_result);
1734 DECL_DECLARED_INLINE_P (old_result)
1735 = DECL_DECLARED_INLINE_P (new_result);
1736 DECL_DISREGARD_INLINE_LIMITS (old_result)
1737 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1742 DECL_DECLARED_INLINE_P (old_result)
1743 |= DECL_DECLARED_INLINE_P (new_result);
1744 DECL_DISREGARD_INLINE_LIMITS (old_result)
1745 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1746 check_redeclaration_exception_specification (newdecl, olddecl);
1750 /* If the new declaration is a definition, update the file and
1751 line information on the declaration, and also make
1752 the old declaration the same definition. */
1753 if (DECL_INITIAL (new_result) != NULL_TREE)
1755 DECL_SOURCE_LOCATION (olddecl)
1756 = DECL_SOURCE_LOCATION (old_result)
1757 = DECL_SOURCE_LOCATION (newdecl);
1758 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1759 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1762 DECL_ARGUMENTS (old_result)
1763 = DECL_ARGUMENTS (new_result);
1764 for (parm = DECL_ARGUMENTS (old_result); parm;
1765 parm = DECL_CHAIN (parm))
1766 DECL_CONTEXT (parm) = old_result;
1775 /* Automatically handles default parameters. */
1776 tree oldtype = TREE_TYPE (olddecl);
1779 /* Merge the data types specified in the two decls. */
1780 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1782 /* If merge_types produces a non-typedef type, just use the old type. */
1783 if (TREE_CODE (newdecl) == TYPE_DECL
1784 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1787 if (TREE_CODE (newdecl) == VAR_DECL)
1789 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1790 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1791 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1792 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1793 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1794 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1796 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1797 if (DECL_LANG_SPECIFIC (olddecl)
1798 && CP_DECL_THREADPRIVATE_P (olddecl))
1800 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1801 if (!DECL_LANG_SPECIFIC (newdecl))
1802 retrofit_lang_decl (newdecl);
1804 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1805 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1809 /* Do this after calling `merge_types' so that default
1810 parameters don't confuse us. */
1811 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1812 check_redeclaration_exception_specification (newdecl, olddecl);
1813 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1815 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1816 check_default_args (newdecl);
1818 /* Lay the type out, unless already done. */
1819 if (! same_type_p (newtype, oldtype)
1820 && TREE_TYPE (newdecl) != error_mark_node
1821 && !(processing_template_decl && uses_template_parms (newdecl)))
1822 layout_type (TREE_TYPE (newdecl));
1824 if ((TREE_CODE (newdecl) == VAR_DECL
1825 || TREE_CODE (newdecl) == PARM_DECL
1826 || TREE_CODE (newdecl) == RESULT_DECL
1827 || TREE_CODE (newdecl) == FIELD_DECL
1828 || TREE_CODE (newdecl) == TYPE_DECL)
1829 && !(processing_template_decl && uses_template_parms (newdecl)))
1830 layout_decl (newdecl, 0);
1832 /* Merge the type qualifiers. */
1833 if (TREE_READONLY (newdecl))
1834 TREE_READONLY (olddecl) = 1;
1835 if (TREE_THIS_VOLATILE (newdecl))
1836 TREE_THIS_VOLATILE (olddecl) = 1;
1837 if (TREE_NOTHROW (newdecl))
1838 TREE_NOTHROW (olddecl) = 1;
1840 /* Merge deprecatedness. */
1841 if (TREE_DEPRECATED (newdecl))
1842 TREE_DEPRECATED (olddecl) = 1;
1844 /* Preserve function specific target and optimization options */
1845 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1847 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1848 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1849 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1850 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1852 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1853 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1854 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1855 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1858 /* Merge the initialization information. */
1859 if (DECL_INITIAL (newdecl) == NULL_TREE
1860 && DECL_INITIAL (olddecl) != NULL_TREE)
1862 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1863 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1864 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1866 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1867 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1871 /* Merge the section attribute.
1872 We want to issue an error if the sections conflict but that must be
1873 done later in decl_attributes since we are called before attributes
1875 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1876 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1878 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1880 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1881 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1882 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1883 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1884 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1885 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1886 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1887 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1888 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1889 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
1890 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
1891 /* Keep the old RTL. */
1892 COPY_DECL_RTL (olddecl, newdecl);
1894 else if (TREE_CODE (newdecl) == VAR_DECL
1895 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1897 /* Keep the old RTL. We cannot keep the old RTL if the old
1898 declaration was for an incomplete object and the new
1899 declaration is not since many attributes of the RTL will
1901 COPY_DECL_RTL (olddecl, newdecl);
1904 /* If cannot merge, then use the new type and qualifiers,
1905 and don't preserve the old rtl. */
1908 /* Clean out any memory we had of the old declaration. */
1909 tree oldstatic = value_member (olddecl, static_aggregates);
1911 TREE_VALUE (oldstatic) = error_mark_node;
1913 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1914 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1915 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1916 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1919 /* Merge the storage class information. */
1920 merge_weak (newdecl, olddecl);
1922 if (DECL_ONE_ONLY (olddecl))
1923 DECL_COMDAT_GROUP (newdecl) = DECL_COMDAT_GROUP (olddecl);
1925 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1926 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1927 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1928 if (! DECL_EXTERNAL (olddecl))
1929 DECL_EXTERNAL (newdecl) = 0;
1931 new_template_info = NULL_TREE;
1932 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1934 bool new_redefines_gnu_inline = false;
1936 if (new_defines_function
1937 && ((DECL_INTERFACE_KNOWN (olddecl)
1938 && TREE_CODE (olddecl) == FUNCTION_DECL)
1939 || (TREE_CODE (olddecl) == TEMPLATE_DECL
1940 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1941 == FUNCTION_DECL))))
1945 if (TREE_CODE (fn) == TEMPLATE_DECL)
1946 fn = DECL_TEMPLATE_RESULT (olddecl);
1948 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
1951 if (!new_redefines_gnu_inline)
1953 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1954 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1955 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1957 DECL_TEMPLATE_INSTANTIATED (newdecl)
1958 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1959 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
1961 /* If the OLDDECL is an instantiation and/or specialization,
1962 then the NEWDECL must be too. But, it may not yet be marked
1963 as such if the caller has created NEWDECL, but has not yet
1964 figured out that it is a redeclaration. */
1965 if (!DECL_USE_TEMPLATE (newdecl))
1966 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1968 /* Don't really know how much of the language-specific
1969 values we should copy from old to new. */
1970 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1971 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1972 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1973 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1975 if (LANG_DECL_HAS_MIN (newdecl))
1977 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
1978 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
1979 if (DECL_TEMPLATE_INFO (newdecl))
1980 new_template_info = DECL_TEMPLATE_INFO (newdecl);
1981 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1983 /* Only functions have these fields. */
1984 if (TREE_CODE (newdecl) == FUNCTION_DECL
1985 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1987 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1988 olddecl_friend = DECL_FRIEND_P (olddecl);
1989 hidden_friend = (DECL_ANTICIPATED (olddecl)
1990 && DECL_HIDDEN_FRIEND_P (olddecl)
1991 && newdecl_is_friend);
1992 DECL_BEFRIENDING_CLASSES (newdecl)
1993 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1994 DECL_BEFRIENDING_CLASSES (olddecl));
1995 /* DECL_THUNKS is only valid for virtual functions,
1996 otherwise it is a DECL_FRIEND_CONTEXT. */
1997 if (DECL_VIRTUAL_P (newdecl))
1998 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
2000 /* Only variables have this field. */
2001 else if (TREE_CODE (newdecl) == VAR_DECL
2002 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2003 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2006 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2010 /* Merge parameter attributes. */
2011 tree oldarg, newarg;
2012 for (oldarg = DECL_ARGUMENTS(olddecl),
2013 newarg = DECL_ARGUMENTS(newdecl);
2015 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2016 DECL_ATTRIBUTES (newarg)
2017 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2018 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2021 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2022 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2024 /* If newdecl is not a specialization, then it is not a
2025 template-related function at all. And that means that we
2026 should have exited above, returning 0. */
2027 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2029 if (DECL_ODR_USED (olddecl))
2030 /* From [temp.expl.spec]:
2032 If a template, a member template or the member of a class
2033 template is explicitly specialized then that
2034 specialization shall be declared before the first use of
2035 that specialization that would cause an implicit
2036 instantiation to take place, in every translation unit in
2037 which such a use occurs. */
2038 error ("explicit specialization of %qD after first use",
2041 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2043 /* Don't propagate visibility from the template to the
2044 specialization here. We'll do that in determine_visibility if
2046 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2048 /* [temp.expl.spec/14] We don't inline explicit specialization
2049 just because the primary template says so. */
2051 else if (new_defines_function && DECL_INITIAL (olddecl))
2053 /* Never inline re-defined extern inline functions.
2054 FIXME: this could be better handled by keeping both
2055 function as separate declarations. */
2056 DECL_UNINLINABLE (newdecl) = 1;
2060 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2061 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2063 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2065 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2066 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2068 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2069 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2070 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2071 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2074 /* Preserve abstractness on cloned [cd]tors. */
2075 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
2077 /* Update newdecl's parms to point at olddecl. */
2078 for (parm = DECL_ARGUMENTS (newdecl); parm;
2079 parm = DECL_CHAIN (parm))
2080 DECL_CONTEXT (parm) = olddecl;
2084 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2085 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2086 COPY_DECL_RTL (newdecl, olddecl);
2088 if (! types_match || new_defines_function)
2090 /* These need to be copied so that the names are available.
2091 Note that if the types do match, we'll preserve inline
2092 info and other bits, but if not, we won't. */
2093 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2094 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2096 if (new_defines_function)
2097 /* If defining a function declared with other language
2098 linkage, use the previously declared language linkage. */
2099 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2100 else if (types_match)
2102 /* If redeclaring a builtin function, and not a definition,
2103 it stays built in. */
2104 if (DECL_BUILT_IN (olddecl))
2106 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2107 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2108 /* If we're keeping the built-in definition, keep the rtl,
2109 regardless of declaration matches. */
2110 COPY_DECL_RTL (olddecl, newdecl);
2113 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2114 /* Don't clear out the arguments if we're just redeclaring a
2116 if (DECL_ARGUMENTS (olddecl))
2117 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2120 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2121 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2123 /* Now preserve various other info from the definition. */
2124 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2125 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2126 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2127 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2129 /* Warn about conflicting visibility specifications. */
2130 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2131 && DECL_VISIBILITY_SPECIFIED (newdecl)
2132 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2134 warning_at (input_location, OPT_Wattributes,
2135 "%q+D: visibility attribute ignored because it", newdecl);
2136 warning_at (DECL_SOURCE_LOCATION (olddecl), OPT_Wattributes,
2137 "conflicts with previous declaration here");
2139 /* Choose the declaration which specified visibility. */
2140 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2142 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2143 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2145 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2146 so keep this behavior. */
2147 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2149 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2150 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2152 /* Likewise for DECL_USER_ALIGN and DECL_PACKED. */
2153 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2154 if (TREE_CODE (newdecl) == FIELD_DECL)
2155 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2157 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2158 with that from NEWDECL below. */
2159 if (DECL_LANG_SPECIFIC (olddecl))
2161 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2162 != DECL_LANG_SPECIFIC (newdecl));
2163 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2166 /* Merge the USED information. */
2167 if (TREE_USED (olddecl))
2168 TREE_USED (newdecl) = 1;
2169 else if (TREE_USED (newdecl))
2170 TREE_USED (olddecl) = 1;
2171 if (TREE_CODE (newdecl) == VAR_DECL)
2173 if (DECL_READ_P (olddecl))
2174 DECL_READ_P (newdecl) = 1;
2175 else if (DECL_READ_P (newdecl))
2176 DECL_READ_P (olddecl) = 1;
2178 if (DECL_PRESERVE_P (olddecl))
2179 DECL_PRESERVE_P (newdecl) = 1;
2180 else if (DECL_PRESERVE_P (newdecl))
2181 DECL_PRESERVE_P (olddecl) = 1;
2183 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2187 function_size = sizeof (struct tree_decl_common);
2189 memcpy ((char *) olddecl + sizeof (struct tree_common),
2190 (char *) newdecl + sizeof (struct tree_common),
2191 function_size - sizeof (struct tree_common));
2193 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2194 (char *) newdecl + sizeof (struct tree_decl_common),
2195 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2196 if (new_template_info)
2197 /* If newdecl is a template instantiation, it is possible that
2198 the following sequence of events has occurred:
2200 o A friend function was declared in a class template. The
2201 class template was instantiated.
2203 o The instantiation of the friend declaration was
2204 recorded on the instantiation list, and is newdecl.
2206 o Later, however, instantiate_class_template called pushdecl
2207 on the newdecl to perform name injection. But, pushdecl in
2208 turn called duplicate_decls when it discovered that another
2209 declaration of a global function with the same name already
2212 o Here, in duplicate_decls, we decided to clobber newdecl.
2214 If we're going to do that, we'd better make sure that
2215 olddecl, and not newdecl, is on the list of
2216 instantiations so that if we try to do the instantiation
2217 again we won't get the clobbered declaration. */
2218 reregister_specialization (newdecl,
2224 size_t size = tree_code_size (TREE_CODE (olddecl));
2225 memcpy ((char *) olddecl + sizeof (struct tree_common),
2226 (char *) newdecl + sizeof (struct tree_common),
2227 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2228 switch (TREE_CODE (olddecl))
2238 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2239 (char *) newdecl + sizeof (struct tree_decl_common),
2240 size - sizeof (struct tree_decl_common)
2241 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2245 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2246 (char *) newdecl + sizeof (struct tree_decl_common),
2247 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2248 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2252 DECL_UID (olddecl) = olddecl_uid;
2254 DECL_FRIEND_P (olddecl) = 1;
2257 DECL_ANTICIPATED (olddecl) = 1;
2258 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2261 /* NEWDECL contains the merged attribute lists.
2262 Update OLDDECL to be the same. */
2263 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2265 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2266 so that encode_section_info has a chance to look at the new decl
2267 flags and attributes. */
2268 if (DECL_RTL_SET_P (olddecl)
2269 && (TREE_CODE (olddecl) == FUNCTION_DECL
2270 || (TREE_CODE (olddecl) == VAR_DECL
2271 && TREE_STATIC (olddecl))))
2272 make_decl_rtl (olddecl);
2274 /* The NEWDECL will no longer be needed. Because every out-of-class
2275 declaration of a member results in a call to duplicate_decls,
2276 freeing these nodes represents in a significant savings. */
2282 /* Return zero if the declaration NEWDECL is valid
2283 when the declaration OLDDECL (assumed to be for the same name)
2284 has already been seen.
2285 Otherwise return an error message format string with a %s
2286 where the identifier should go. */
2289 redeclaration_error_message (tree newdecl, tree olddecl)
2291 if (TREE_CODE (newdecl) == TYPE_DECL)
2293 /* Because C++ can put things into name space for free,
2294 constructs like "typedef struct foo { ... } foo"
2295 would look like an erroneous redeclaration. */
2296 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2299 return G_("redefinition of %q#D");
2301 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2303 /* If this is a pure function, its olddecl will actually be
2304 the original initialization to `0' (which we force to call
2305 abort()). Don't complain about redefinition in this case. */
2306 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2307 && DECL_INITIAL (olddecl) == NULL_TREE)
2310 /* If both functions come from different namespaces, this is not
2311 a redeclaration - this is a conflict with a used function. */
2312 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2313 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2314 && ! decls_match (olddecl, newdecl))
2315 return G_("%qD conflicts with used function");
2317 /* We'll complain about linkage mismatches in
2318 warn_extern_redeclared_static. */
2320 /* Defining the same name twice is no good. */
2321 if (DECL_INITIAL (olddecl) != NULL_TREE
2322 && DECL_INITIAL (newdecl) != NULL_TREE)
2324 if (DECL_NAME (olddecl) == NULL_TREE)
2325 return G_("%q#D not declared in class");
2326 else if (!GNU_INLINE_P (olddecl)
2327 || GNU_INLINE_P (newdecl))
2328 return G_("redefinition of %q#D");
2331 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2333 bool olda = GNU_INLINE_P (olddecl);
2334 bool newa = GNU_INLINE_P (newdecl);
2339 return G_("%q+D redeclared inline with "
2340 "%<gnu_inline%> attribute");
2342 return G_("%q+D redeclared inline without "
2343 "%<gnu_inline%> attribute");
2349 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2353 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2355 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2356 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2357 return G_("redefinition of %q#D");
2361 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2362 || (DECL_TEMPLATE_RESULT (newdecl)
2363 == DECL_TEMPLATE_RESULT (olddecl)))
2366 nt = DECL_TEMPLATE_RESULT (newdecl);
2367 if (DECL_TEMPLATE_INFO (nt))
2368 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2369 ot = DECL_TEMPLATE_RESULT (olddecl);
2370 if (DECL_TEMPLATE_INFO (ot))
2371 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2372 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2373 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2374 return G_("redefinition of %q#D");
2376 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2378 bool olda = GNU_INLINE_P (ot);
2379 bool newa = GNU_INLINE_P (nt);
2384 return G_("%q+D redeclared inline with "
2385 "%<gnu_inline%> attribute");
2387 return G_("%q+D redeclared inline without "
2388 "%<gnu_inline%> attribute");
2392 /* Core issue #226 (C++0x):
2394 If a friend function template declaration specifies a
2395 default template-argument, that declaration shall be a
2396 definition and shall be the only declaration of the
2397 function template in the translation unit. */
2398 if ((cxx_dialect != cxx98)
2399 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2400 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2401 /*is_primary=*/1, /*is_partial=*/0,
2402 /*is_friend_decl=*/2))
2403 return G_("redeclaration of friend %q#D "
2404 "may not have default template arguments");
2408 else if (TREE_CODE (newdecl) == VAR_DECL
2409 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2410 && (! DECL_LANG_SPECIFIC (olddecl)
2411 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2412 || DECL_THREAD_LOCAL_P (newdecl)))
2414 /* Only variables can be thread-local, and all declarations must
2415 agree on this property. */
2416 if (DECL_THREAD_LOCAL_P (newdecl))
2417 return G_("thread-local declaration of %q#D follows "
2418 "non-thread-local declaration");
2420 return G_("non-thread-local declaration of %q#D follows "
2421 "thread-local declaration");
2423 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2425 /* The objects have been declared at namespace scope. If either
2426 is a member of an anonymous union, then this is an invalid
2427 redeclaration. For example:
2433 if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2434 || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
2435 return G_("redeclaration of %q#D");
2436 /* If at least one declaration is a reference, there is no
2437 conflict. For example:
2443 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2445 /* Reject two definitions. */
2446 return G_("redefinition of %q#D");
2450 /* Objects declared with block scope: */
2451 /* Reject two definitions, and reject a definition
2452 together with an external reference. */
2453 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2454 return G_("redeclaration of %q#D");
2459 /* Hash and equality functions for the named_label table. */
2462 named_label_entry_hash (const void *data)
2464 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2465 return DECL_UID (ent->label_decl);
2469 named_label_entry_eq (const void *a, const void *b)
2471 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2472 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2473 return ent_a->label_decl == ent_b->label_decl;
2476 /* Create a new label, named ID. */
2479 make_label_decl (tree id, int local_p)
2481 struct named_label_entry *ent;
2485 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2487 DECL_CONTEXT (decl) = current_function_decl;
2488 DECL_MODE (decl) = VOIDmode;
2489 C_DECLARED_LABEL_FLAG (decl) = local_p;
2491 /* Say where one reference is to the label, for the sake of the
2492 error if it is not defined. */
2493 DECL_SOURCE_LOCATION (decl) = input_location;
2495 /* Record the fact that this identifier is bound to this label. */
2496 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2498 /* Create the label htab for the function on demand. */
2500 named_labels = htab_create_ggc (13, named_label_entry_hash,
2501 named_label_entry_eq, NULL);
2503 /* Record this label on the list of labels used in this function.
2504 We do this before calling make_label_decl so that we get the
2505 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2506 ent = ggc_alloc_cleared_named_label_entry ();
2507 ent->label_decl = decl;
2509 slot = htab_find_slot (named_labels, ent, INSERT);
2510 gcc_assert (*slot == NULL);
2516 /* Look for a label named ID in the current function. If one cannot
2517 be found, create one. (We keep track of used, but undefined,
2518 labels, and complain about them at the end of a function.) */
2521 lookup_label (tree id)
2525 timevar_push (TV_NAME_LOOKUP);
2526 /* You can't use labels at global scope. */
2527 if (current_function_decl == NULL_TREE)
2529 error ("label %qE referenced outside of any function", id);
2530 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2533 /* See if we've already got this label. */
2534 decl = IDENTIFIER_LABEL_VALUE (id);
2535 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2536 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2538 decl = make_label_decl (id, /*local_p=*/0);
2539 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2542 /* Declare a local label named ID. */
2545 declare_local_label (tree id)
2548 cp_label_binding *bind;
2550 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2551 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2552 bind = VEC_safe_push (cp_label_binding, gc,
2553 current_binding_level->shadowed_labels, NULL);
2554 bind->prev_value = IDENTIFIER_LABEL_VALUE (id);
2556 decl = make_label_decl (id, /*local_p=*/1);
2562 /* Returns nonzero if it is ill-formed to jump past the declaration of
2563 DECL. Returns 2 if it's also a real problem. */
2566 decl_jump_unsafe (tree decl)
2568 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2569 with automatic storage duration is not in scope to a point where it is
2570 in scope is ill-formed unless the variable has scalar type, class type
2571 with a trivial default constructor and a trivial destructor, a
2572 cv-qualified version of one of these types, or an array of one of the
2573 preceding types and is declared without an initializer (8.5). */
2574 tree type = TREE_TYPE (decl);
2576 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2577 || type == error_mark_node)
2580 type = strip_array_types (type);
2582 if (type_has_nontrivial_default_init (TREE_TYPE (decl))
2583 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2586 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2592 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2595 identify_goto (tree decl, const location_t *locus)
2598 permerror (input_location, "jump to label %qD", decl);
2600 permerror (input_location, "jump to case label");
2602 permerror (*locus, " from here");
2605 /* Check that a single previously seen jump to a newly defined label
2606 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2607 the jump context; NAMES are the names in scope in LEVEL at the jump
2608 context; LOCUS is the source position of the jump or 0. Returns
2609 true if all is well. */
2612 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2613 bool exited_omp, const location_t *locus)
2615 struct cp_binding_level *b;
2616 bool identified = false, saw_eh = false, saw_omp = false;
2620 identify_goto (decl, locus);
2621 error (" exits OpenMP structured block");
2622 identified = saw_omp = true;
2625 for (b = current_binding_level; b ; b = b->level_chain)
2627 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2629 for (new_decls = b->names; new_decls != old_decls;
2630 new_decls = DECL_CHAIN (new_decls))
2632 int problem = decl_jump_unsafe (new_decls);
2638 identify_goto (decl, locus);
2642 error (" crosses initialization of %q+#D", new_decls);
2644 permerror (input_location, " enters scope of %q+#D which has "
2645 "non-trivial destructor", new_decls);
2650 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2654 identify_goto (decl, locus);
2657 if (b->kind == sk_try)
2658 error (" enters try block");
2660 error (" enters catch block");
2663 if (b->kind == sk_omp && !saw_omp)
2667 identify_goto (decl, locus);
2670 error (" enters OpenMP structured block");
2679 check_previous_goto (tree decl, struct named_label_use_entry *use)
2681 check_previous_goto_1 (decl, use->binding_level,
2682 use->names_in_scope, use->in_omp_scope,
2683 &use->o_goto_locus);
2687 check_switch_goto (struct cp_binding_level* level)
2689 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2692 /* Check that a new jump to a label DECL is OK. Called by
2693 finish_goto_stmt. */
2696 check_goto (tree decl)
2698 struct named_label_entry *ent, dummy;
2699 bool saw_catch = false, identified = false;
2703 /* We can't know where a computed goto is jumping.
2704 So we assume that it's OK. */
2705 if (TREE_CODE (decl) != LABEL_DECL)
2708 /* We didn't record any information about this label when we created it,
2709 and there's not much point since it's trivial to analyze as a return. */
2710 if (decl == cdtor_label)
2713 dummy.label_decl = decl;
2714 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2715 gcc_assert (ent != NULL);
2717 /* If the label hasn't been defined yet, defer checking. */
2718 if (! DECL_INITIAL (decl))
2720 struct named_label_use_entry *new_use;
2722 /* Don't bother creating another use if the last goto had the
2723 same data, and will therefore create the same set of errors. */
2725 && ent->uses->names_in_scope == current_binding_level->names)
2728 new_use = ggc_alloc_named_label_use_entry ();
2729 new_use->binding_level = current_binding_level;
2730 new_use->names_in_scope = current_binding_level->names;
2731 new_use->o_goto_locus = input_location;
2732 new_use->in_omp_scope = false;
2734 new_use->next = ent->uses;
2735 ent->uses = new_use;
2739 if (ent->in_try_scope || ent->in_catch_scope
2740 || ent->in_omp_scope || !VEC_empty (tree, ent->bad_decls))
2742 permerror (input_location, "jump to label %q+D", decl);
2743 permerror (input_location, " from here");
2747 FOR_EACH_VEC_ELT (tree, ent->bad_decls, ix, bad)
2749 int u = decl_jump_unsafe (bad);
2751 if (u > 1 && DECL_ARTIFICIAL (bad))
2753 /* Can't skip init of __exception_info. */
2754 error_at (DECL_SOURCE_LOCATION (bad), " enters catch block");
2758 error (" skips initialization of %q+#D", bad);
2760 permerror (input_location, " enters scope of %q+#D which has "
2761 "non-trivial destructor", bad);
2764 if (ent->in_try_scope)
2765 error (" enters try block");
2766 else if (ent->in_catch_scope && !saw_catch)
2767 error (" enters catch block");
2769 if (ent->in_omp_scope)
2770 error (" enters OpenMP structured block");
2771 else if (flag_openmp)
2773 struct cp_binding_level *b;
2774 for (b = current_binding_level; b ; b = b->level_chain)
2776 if (b == ent->binding_level)
2778 if (b->kind == sk_omp)
2782 permerror (input_location, "jump to label %q+D", decl);
2783 permerror (input_location, " from here");
2786 error (" exits OpenMP structured block");
2793 /* Check that a return is ok wrt OpenMP structured blocks.
2794 Called by finish_return_stmt. Returns true if all is well. */
2797 check_omp_return (void)
2799 struct cp_binding_level *b;
2800 for (b = current_binding_level; b ; b = b->level_chain)
2801 if (b->kind == sk_omp)
2803 error ("invalid exit from OpenMP structured block");
2809 /* Define a label, specifying the location in the source file.
2810 Return the LABEL_DECL node for the label. */
2813 define_label (location_t location, tree name)
2815 struct named_label_entry *ent, dummy;
2816 struct cp_binding_level *p;
2819 timevar_push (TV_NAME_LOOKUP);
2821 decl = lookup_label (name);
2823 dummy.label_decl = decl;
2824 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2825 gcc_assert (ent != NULL);
2827 /* After labels, make any new cleanups in the function go into their
2828 own new (temporary) binding contour. */
2829 for (p = current_binding_level;
2830 p->kind != sk_function_parms;
2832 p->more_cleanups_ok = 0;
2834 if (name == get_identifier ("wchar_t"))
2835 permerror (input_location, "label named wchar_t");
2837 if (DECL_INITIAL (decl) != NULL_TREE)
2839 error ("duplicate label %qD", decl);
2840 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2844 struct named_label_use_entry *use;
2846 /* Mark label as having been defined. */
2847 DECL_INITIAL (decl) = error_mark_node;
2848 /* Say where in the source. */
2849 DECL_SOURCE_LOCATION (decl) = location;
2851 ent->binding_level = current_binding_level;
2852 ent->names_in_scope = current_binding_level->names;
2854 for (use = ent->uses; use ; use = use->next)
2855 check_previous_goto (decl, use);
2859 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2864 struct cp_binding_level *level;
2865 struct cp_switch *next;
2866 /* The SWITCH_STMT being built. */
2868 /* A splay-tree mapping the low element of a case range to the high
2869 element, or NULL_TREE if there is no high element. Used to
2870 determine whether or not a new case label duplicates an old case
2871 label. We need a tree, rather than simply a hash table, because
2872 of the GNU case range extension. */
2876 /* A stack of the currently active switch statements. The innermost
2877 switch statement is on the top of the stack. There is no need to
2878 mark the stack for garbage collection because it is only active
2879 during the processing of the body of a function, and we never
2880 collect at that point. */
2882 static struct cp_switch *switch_stack;
2884 /* Called right after a switch-statement condition is parsed.
2885 SWITCH_STMT is the switch statement being parsed. */
2888 push_switch (tree switch_stmt)
2890 struct cp_switch *p = XNEW (struct cp_switch);
2891 p->level = current_binding_level;
2892 p->next = switch_stack;
2893 p->switch_stmt = switch_stmt;
2894 p->cases = splay_tree_new (case_compare, NULL, NULL);
2901 struct cp_switch *cs = switch_stack;
2902 location_t switch_location;
2904 /* Emit warnings as needed. */
2905 switch_location = EXPR_LOC_OR_HERE (cs->switch_stmt);
2906 if (!processing_template_decl)
2907 c_do_switch_warnings (cs->cases, switch_location,
2908 SWITCH_STMT_TYPE (cs->switch_stmt),
2909 SWITCH_STMT_COND (cs->switch_stmt));
2911 splay_tree_delete (cs->cases);
2912 switch_stack = switch_stack->next;
2916 /* Note that we've seen a definition of a case label, and complain if this
2917 is a bad place for one. */
2920 finish_case_label (location_t loc, tree low_value, tree high_value)
2923 struct cp_binding_level *p;
2925 if (processing_template_decl)
2929 /* For templates, just add the case label; we'll do semantic
2930 analysis at instantiation-time. */
2931 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
2932 return add_stmt (build_case_label (loc, low_value, high_value, label));
2935 /* Find the condition on which this switch statement depends. */
2936 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2937 if (cond && TREE_CODE (cond) == TREE_LIST)
2938 cond = TREE_VALUE (cond);
2940 if (!check_switch_goto (switch_stack->level))
2941 return error_mark_node;
2944 low_value = cxx_constant_value (low_value);
2946 high_value = cxx_constant_value (high_value);
2948 r = c_add_case_label (loc, switch_stack->cases, cond,
2949 SWITCH_STMT_TYPE (switch_stack->switch_stmt),
2950 low_value, high_value);
2952 /* After labels, make any new cleanups in the function go into their
2953 own new (temporary) binding contour. */
2954 for (p = current_binding_level;
2955 p->kind != sk_function_parms;
2957 p->more_cleanups_ok = 0;
2962 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2965 typename_hash (const void* k)
2968 const_tree const t = (const_tree) k;
2970 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2971 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2976 typedef struct typename_info {
2984 /* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
2985 really of type `typename_info*' */
2988 typename_compare (const void * k1, const void * k2)
2990 const_tree const t1 = (const_tree) k1;
2991 const typename_info *const t2 = (const typename_info *) k2;
2993 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2994 && TYPE_CONTEXT (t1) == t2->scope
2995 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2996 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2997 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3000 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3001 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3003 Returns the new TYPENAME_TYPE. */
3005 static GTY ((param_is (union tree_node))) htab_t typename_htab;
3008 build_typename_type (tree context, tree name, tree fullname,
3009 enum tag_types tag_type)
3017 if (typename_htab == NULL)
3018 typename_htab = htab_create_ggc (61, &typename_hash,
3019 &typename_compare, NULL);
3021 ti.scope = FROB_CONTEXT (context);
3023 ti.template_id = fullname;
3024 ti.enum_p = tag_type == enum_type;
3025 ti.class_p = (tag_type == class_type
3026 || tag_type == record_type
3027 || tag_type == union_type);
3028 hash = (htab_hash_pointer (ti.scope)
3029 ^ htab_hash_pointer (ti.name));
3031 /* See if we already have this type. */
3032 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
3037 /* Build the TYPENAME_TYPE. */
3038 t = cxx_make_type (TYPENAME_TYPE);
3039 TYPE_CONTEXT (t) = ti.scope;
3040 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3041 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3042 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3044 /* Build the corresponding TYPE_DECL. */
3045 d = build_decl (input_location, TYPE_DECL, name, t);
3046 TYPE_NAME (TREE_TYPE (d)) = d;
3047 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3048 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3049 DECL_ARTIFICIAL (d) = 1;
3051 /* Store it in the hash table. */
3054 /* TYPENAME_TYPEs must always be compared structurally, because
3055 they may or may not resolve down to another type depending on
3056 the currently open classes. */
3057 SET_TYPE_STRUCTURAL_EQUALITY (t);
3063 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3064 provided to name the type. Returns an appropriate type, unless an
3065 error occurs, in which case error_mark_node is returned. If we
3066 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3067 return that, rather than the _TYPE it corresponds to, in other
3068 cases we look through the type decl. If TF_ERROR is set, complain
3069 about errors, otherwise be quiet. */
3072 make_typename_type (tree context, tree name, enum tag_types tag_type,
3073 tsubst_flags_t complain)
3079 if (name == error_mark_node
3080 || context == NULL_TREE
3081 || context == error_mark_node)
3082 return error_mark_node;
3086 if (!(TYPE_LANG_SPECIFIC (name)
3087 && (CLASSTYPE_IS_TEMPLATE (name)
3088 || CLASSTYPE_USE_TEMPLATE (name))))
3089 name = TYPE_IDENTIFIER (name);
3091 /* Create a TEMPLATE_ID_EXPR for the type. */
3092 name = build_nt (TEMPLATE_ID_EXPR,
3093 CLASSTYPE_TI_TEMPLATE (name),
3094 CLASSTYPE_TI_ARGS (name));
3096 else if (TREE_CODE (name) == TYPE_DECL)
3097 name = DECL_NAME (name);
3101 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3103 name = TREE_OPERAND (name, 0);
3104 if (TREE_CODE (name) == TEMPLATE_DECL)
3105 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3106 else if (TREE_CODE (name) == OVERLOAD)
3108 error ("%qD is not a type", name);
3109 return error_mark_node;
3112 if (TREE_CODE (name) == TEMPLATE_DECL)
3114 error ("%qD used without template parameters", name);
3115 return error_mark_node;
3117 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3118 gcc_assert (TYPE_P (context));
3120 if (!MAYBE_CLASS_TYPE_P (context))
3122 if (complain & tf_error)
3123 error ("%q#T is not a class", context);
3124 return error_mark_node;
3127 /* When the CONTEXT is a dependent type, NAME could refer to a
3128 dependent base class of CONTEXT. But look inside it anyway
3129 if CONTEXT is a currently open scope, in case it refers to a
3130 member of the current instantiation or a non-dependent base;
3131 lookup will stop when we hit a dependent base. */
3132 if (!dependent_scope_p (context))
3133 /* We should only set WANT_TYPE when we're a nested typename type.
3134 Then we can give better diagnostics if we find a non-type. */
3135 t = lookup_field (context, name, 2, /*want_type=*/true);
3139 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3140 return build_typename_type (context, name, fullname, tag_type);
3142 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3146 if (complain & tf_error)
3147 error (want_template ? "no class template named %q#T in %q#T"
3148 : "no type named %q#T in %q#T", name, context);
3149 return error_mark_node;
3152 /* Pull out the template from an injected-class-name (or multiple). */
3154 t = maybe_get_template_decl_from_type_decl (t);
3156 if (TREE_CODE (t) == TREE_LIST)
3158 if (complain & tf_error)
3160 error ("lookup of %qT in %qT is ambiguous", name, context);
3161 print_candidates (t);
3163 return error_mark_node;
3166 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
3168 if (complain & tf_error)
3169 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3171 return error_mark_node;
3173 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3175 if (complain & tf_error)
3176 error ("%<typename %T::%D%> names %q#T, which is not a type",
3178 return error_mark_node;
3181 if (complain & tf_error)
3182 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
3184 /* If we are currently parsing a template and if T is a typedef accessed
3185 through CONTEXT then we need to remember and check access of T at
3186 template instantiation time. */
3187 add_typedef_to_current_template_for_access_check (t, context, input_location);
3190 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3192 /*entering_scope=*/0,
3193 tf_warning_or_error | tf_user);
3195 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3201 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3202 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3203 in which case error_mark_node is returned.
3205 If PARM_LIST is non-NULL, also make sure that the template parameter
3206 list of TEMPLATE_DECL matches.
3208 If COMPLAIN zero, don't complain about any errors that occur. */
3211 make_unbound_class_template (tree context, tree name, tree parm_list,
3212 tsubst_flags_t complain)
3218 name = TYPE_IDENTIFIER (name);
3219 else if (DECL_P (name))
3220 name = DECL_NAME (name);
3221 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3223 if (!dependent_type_p (context)
3224 || currently_open_class (context))
3226 tree tmpl = NULL_TREE;
3228 if (MAYBE_CLASS_TYPE_P (context))
3229 tmpl = lookup_field (context, name, 0, false);
3231 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3232 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3234 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
3236 if (complain & tf_error)
3237 error ("no class template named %q#T in %q#T", name, context);
3238 return error_mark_node;
3242 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3244 if (complain & tf_error)
3246 error ("template parameters do not match template");
3247 error ("%q+D declared here", tmpl);
3249 return error_mark_node;
3252 if (complain & tf_error)
3253 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
3258 /* Build the UNBOUND_CLASS_TEMPLATE. */
3259 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3260 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3261 TREE_TYPE (t) = NULL_TREE;
3262 SET_TYPE_STRUCTURAL_EQUALITY (t);
3264 /* Build the corresponding TEMPLATE_DECL. */
3265 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3266 TYPE_NAME (TREE_TYPE (d)) = d;
3267 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3268 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3269 DECL_ARTIFICIAL (d) = 1;
3270 DECL_TEMPLATE_PARMS (d) = parm_list;
3277 /* Push the declarations of builtin types into the namespace.
3278 RID_INDEX is the index of the builtin type in the array
3279 RID_POINTERS. NAME is the name used when looking up the builtin
3280 type. TYPE is the _TYPE node for the builtin type. */
3283 record_builtin_type (enum rid rid_index,
3287 tree rname = NULL_TREE, tname = NULL_TREE;
3288 tree tdecl = NULL_TREE;
3290 if ((int) rid_index < (int) RID_MAX)
3291 rname = ridpointers[(int) rid_index];
3293 tname = get_identifier (name);
3295 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3296 eliminated. Built-in types should not be looked up name; their
3297 names are keywords that the parser can recognize. However, there
3298 is code in c-common.c that uses identifier_global_value to look
3299 up built-in types by name. */
3302 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3303 DECL_ARTIFICIAL (tdecl) = 1;
3304 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3310 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3311 DECL_ARTIFICIAL (tdecl) = 1;
3313 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3316 if (!TYPE_NAME (type))
3317 TYPE_NAME (type) = tdecl;
3320 debug_hooks->type_decl (tdecl, 0);
3323 /* Record one of the standard Java types.
3324 * Declare it as having the given NAME.
3325 * If SIZE > 0, it is the size of one of the integral types;
3326 * otherwise it is the negative of the size of one of the other types. */
3329 record_builtin_java_type (const char* name, int size)
3333 type = build_nonstandard_integer_type (size, 0);
3334 else if (size > -32)
3337 /* "__java_char" or ""__java_boolean". */
3338 type = build_nonstandard_integer_type (-size, 1);
3339 /* Get the signed type cached and attached to the unsigned type,
3340 so it doesn't get garbage-collected at "random" times,
3341 causing potential codegen differences out of different UIDs
3342 and different alias set numbers. */
3343 stype = build_nonstandard_integer_type (-size, 0);
3344 TREE_CHAIN (type) = stype;
3345 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3348 { /* "__java_float" or ""__java_double". */
3349 type = make_node (REAL_TYPE);
3350 TYPE_PRECISION (type) = - size;
3353 record_builtin_type (RID_MAX, name, type);
3354 decl = TYPE_NAME (type);
3356 /* Suppress generate debug symbol entries for these types,
3357 since for normal C++ they are just clutter.
3358 However, push_lang_context undoes this if extern "Java" is seen. */
3359 DECL_IGNORED_P (decl) = 1;
3361 TYPE_FOR_JAVA (type) = 1;
3365 /* Push a type into the namespace so that the back ends ignore it. */
3368 record_unknown_type (tree type, const char* name)
3370 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3371 TYPE_DECL, get_identifier (name), type));
3372 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3373 DECL_IGNORED_P (decl) = 1;
3374 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3375 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3376 TYPE_ALIGN (type) = 1;
3377 TYPE_USER_ALIGN (type) = 0;
3378 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3381 /* A string for which we should create an IDENTIFIER_NODE at
3384 typedef struct predefined_identifier
3386 /* The name of the identifier. */
3387 const char *const name;
3388 /* The place where the IDENTIFIER_NODE should be stored. */
3390 /* Nonzero if this is the name of a constructor or destructor. */
3391 const int ctor_or_dtor_p;
3392 } predefined_identifier;
3394 /* Create all the predefined identifiers. */
3397 initialize_predefined_identifiers (void)
3399 const predefined_identifier *pid;
3401 /* A table of identifiers to create at startup. */
3402 static const predefined_identifier predefined_identifiers[] = {
3403 { "C++", &lang_name_cplusplus, 0 },
3404 { "C", &lang_name_c, 0 },
3405 { "Java", &lang_name_java, 0 },
3406 /* Some of these names have a trailing space so that it is
3407 impossible for them to conflict with names written by users. */
3408 { "__ct ", &ctor_identifier, 1 },
3409 { "__base_ctor ", &base_ctor_identifier, 1 },
3410 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3411 { "__dt ", &dtor_identifier, 1 },
3412 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3413 { "__base_dtor ", &base_dtor_identifier, 1 },
3414 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3415 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3416 { "nelts", &nelts_identifier, 0 },
3417 { THIS_NAME, &this_identifier, 0 },
3418 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3419 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3420 { "_vptr", &vptr_identifier, 0 },
3421 { "__vtt_parm", &vtt_parm_identifier, 0 },
3422 { "::", &global_scope_name, 0 },
3423 { "std", &std_identifier, 0 },
3427 for (pid = predefined_identifiers; pid->name; ++pid)
3429 *pid->node = get_identifier (pid->name);
3430 if (pid->ctor_or_dtor_p)
3431 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3435 /* Create the predefined scalar types of C,
3436 and some nodes representing standard constants (0, 1, (void *)0).
3437 Initialize the global binding level.
3438 Make definitions for built-in primitive functions. */
3441 cxx_init_decl_processing (void)
3444 tree void_ftype_ptr;
3446 build_common_tree_nodes (flag_signed_char);
3448 /* Create all the identifiers we need. */
3449 initialize_predefined_identifiers ();
3451 /* Create the global variables. */
3452 push_to_top_level ();
3454 current_function_decl = NULL_TREE;
3455 current_binding_level = NULL;
3456 /* Enter the global namespace. */
3457 gcc_assert (global_namespace == NULL_TREE);
3458 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3460 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3461 TREE_PUBLIC (global_namespace) = 1;
3462 begin_scope (sk_namespace, global_namespace);
3464 current_lang_name = NULL_TREE;
3466 if (flag_visibility_ms_compat)
3467 default_visibility = VISIBILITY_HIDDEN;
3470 current_lang_name = lang_name_c;
3472 /* Create the `std' namespace. */
3473 push_namespace (std_identifier);
3474 std_node = current_namespace;
3477 c_common_nodes_and_builtins ();
3479 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3480 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3481 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3482 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3483 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3484 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3485 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3486 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3488 integer_two_node = build_int_cst (NULL_TREE, 2);
3490 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3491 truthvalue_type_node = boolean_type_node;
3492 truthvalue_false_node = boolean_false_node;
3493 truthvalue_true_node = boolean_true_node;
3495 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3496 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
3497 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
3500 record_builtin_type (RID_MAX, NULL, string_type_node);
3503 delta_type_node = ptrdiff_type_node;
3504 vtable_index_type = ptrdiff_type_node;
3506 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3507 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
3508 void_ftype_ptr = build_function_type_list (void_type_node,
3509 ptr_type_node, NULL_TREE);
3511 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3513 /* C++ extensions */
3515 unknown_type_node = make_node (LANG_TYPE);
3516 record_unknown_type (unknown_type_node, "unknown type");
3518 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3519 TREE_TYPE (unknown_type_node) = unknown_type_node;
3521 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3523 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3524 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3526 init_list_type_node = make_node (LANG_TYPE);
3527 record_unknown_type (init_list_type_node, "init list");
3530 /* Make sure we get a unique function type, so we can give
3531 its pointer type a name. (This wins for gdb.) */
3532 tree vfunc_type = make_node (FUNCTION_TYPE);
3533 TREE_TYPE (vfunc_type) = integer_type_node;
3534 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3535 layout_type (vfunc_type);
3537 vtable_entry_type = build_pointer_type (vfunc_type);
3539 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3542 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3543 layout_type (vtbl_type_node);
3544 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3545 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3546 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3547 layout_type (vtbl_ptr_type_node);
3548 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3550 push_namespace (get_identifier ("__cxxabiv1"));
3551 abi_node = current_namespace;
3554 global_type_node = make_node (LANG_TYPE);
3555 record_unknown_type (global_type_node, "global type");
3558 current_lang_name = lang_name_cplusplus;
3561 tree newtype, deltype;
3562 tree ptr_ftype_sizetype;
3566 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
3567 if (cxx_dialect == cxx98)
3570 tree bad_alloc_type_node;
3571 tree bad_alloc_decl;
3573 push_namespace (std_identifier);
3574 bad_alloc_id = get_identifier ("bad_alloc");
3575 bad_alloc_type_node = make_class_type (RECORD_TYPE);
3576 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3578 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3579 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3583 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
3586 new_eh_spec = noexcept_false_spec;
3588 newtype = build_exception_variant (ptr_ftype_sizetype, new_eh_spec);
3589 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3590 push_cp_library_fn (NEW_EXPR, newtype);
3591 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3592 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3593 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3595 nullptr_type_node = make_node (NULLPTR_TYPE);
3596 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
3597 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
3598 TYPE_UNSIGNED (nullptr_type_node) = 1;
3599 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
3600 SET_TYPE_MODE (nullptr_type_node, Pmode);
3601 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
3602 nullptr_node = build_int_cst (nullptr_type_node, 0);
3606 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3608 /* Perform other language dependent initializations. */
3609 init_class_processing ();
3610 init_rtti_processing ();
3611 init_template_processing ();
3613 if (flag_exceptions)
3614 init_exception_processing ();
3616 if (! supports_one_only ())
3619 make_fname_decl = cp_make_fname_decl;
3620 start_fname_decls ();
3622 /* Show we use EH for cleanups. */
3623 if (flag_exceptions)
3624 using_eh_for_cleanups ();
3627 /* Generate an initializer for a function naming variable from
3628 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3629 filled in with the type of the init. */
3632 cp_fname_init (const char* name, tree *type_p)
3634 tree domain = NULL_TREE;
3636 tree init = NULL_TREE;
3641 length = strlen (name);
3642 domain = build_index_type (size_int (length));
3643 init = build_string (length + 1, name);
3646 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3647 type = build_cplus_array_type (type, domain);
3652 TREE_TYPE (init) = type;
3654 init = error_mark_node;
3659 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
3660 the decl, LOC is the location to give the decl, NAME is the
3661 initialization string and TYPE_DEP indicates whether NAME depended
3662 on the type of the function. We make use of that to detect
3663 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
3664 at the point of first use, so we mustn't push the decl now. */
3667 cp_make_fname_decl (location_t loc, tree id, int type_dep)
3669 const char *const name = (type_dep && processing_template_decl
3670 ? NULL : fname_as_string (type_dep));
3672 tree init = cp_fname_init (name, &type);
3673 tree decl = build_decl (loc, VAR_DECL, id, type);
3676 free (CONST_CAST (char *, name));
3678 /* As we're using pushdecl_with_scope, we must set the context. */
3679 DECL_CONTEXT (decl) = current_function_decl;
3680 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3682 TREE_STATIC (decl) = 1;
3683 TREE_READONLY (decl) = 1;
3684 DECL_ARTIFICIAL (decl) = 1;
3686 TREE_USED (decl) = 1;
3688 if (current_function_decl)
3690 struct cp_binding_level *b = current_binding_level;
3691 if (b->kind == sk_function_parms)
3692 return error_mark_node;
3693 while (b->level_chain->kind != sk_function_parms)
3695 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3696 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3697 LOOKUP_ONLYCONVERTING);
3700 pushdecl_top_level_and_finish (decl, init);
3706 builtin_function_1 (tree decl, tree context, bool is_global)
3708 tree id = DECL_NAME (decl);
3709 const char *name = IDENTIFIER_POINTER (id);
3711 retrofit_lang_decl (decl);
3713 DECL_ARTIFICIAL (decl) = 1;
3714 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3715 SET_DECL_LANGUAGE (decl, lang_c);
3716 /* Runtime library routines are, by definition, available in an
3717 external shared object. */
3718 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3719 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3721 DECL_CONTEXT (decl) = context;
3724 pushdecl_top_level (decl);
3728 /* A function in the user's namespace should have an explicit
3729 declaration before it is used. Mark the built-in function as
3730 anticipated but not actually declared. */
3731 if (name[0] != '_' || name[1] != '_')
3732 DECL_ANTICIPATED (decl) = 1;
3733 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
3735 size_t len = strlen (name);
3737 /* Treat __*_chk fortification functions as anticipated as well,
3738 unless they are __builtin_*. */
3739 if (len > strlen ("___chk")
3740 && memcmp (name + len - strlen ("_chk"),
3741 "_chk", strlen ("_chk") + 1) == 0)
3742 DECL_ANTICIPATED (decl) = 1;
3749 cxx_builtin_function (tree decl)
3751 tree id = DECL_NAME (decl);
3752 const char *name = IDENTIFIER_POINTER (id);
3753 /* All builtins that don't begin with an '_' should additionally
3754 go in the 'std' namespace. */
3757 tree decl2 = copy_node(decl);
3758 push_namespace (std_identifier);
3759 builtin_function_1 (decl2, std_node, false);
3763 return builtin_function_1 (decl, NULL_TREE, false);
3766 /* Like cxx_builtin_function, but guarantee the function is added to the global
3767 scope. This is to allow function specific options to add new machine
3768 dependent builtins when the target ISA changes via attribute((target(...)))
3769 which saves space on program startup if the program does not use non-generic
3773 cxx_builtin_function_ext_scope (tree decl)
3776 tree id = DECL_NAME (decl);
3777 const char *name = IDENTIFIER_POINTER (id);
3778 /* All builtins that don't begin with an '_' should additionally
3779 go in the 'std' namespace. */
3782 tree decl2 = copy_node(decl);
3783 push_namespace (std_identifier);
3784 builtin_function_1 (decl2, std_node, true);
3788 return builtin_function_1 (decl, NULL_TREE, true);
3791 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3792 function. Not called directly. */
3795 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3797 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3798 DECL_EXTERNAL (fn) = 1;
3799 TREE_PUBLIC (fn) = 1;
3800 DECL_ARTIFICIAL (fn) = 1;
3801 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3802 SET_DECL_LANGUAGE (fn, lang_c);
3803 /* Runtime library routines are, by definition, available in an
3804 external shared object. */
3805 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3806 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3810 /* Returns the _DECL for a library function with C linkage.
3811 We assume that such functions never throw; if this is incorrect,
3812 callers should unset TREE_NOTHROW. */
3815 build_library_fn (tree name, tree type)
3817 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3818 TREE_NOTHROW (fn) = 1;
3822 /* Returns the _DECL for a library function with C++ linkage. */
3825 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3827 tree fn = build_library_fn_1 (name, operator_code, type);
3828 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3829 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3830 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3834 /* Like build_library_fn, but takes a C string instead of an
3838 build_library_fn_ptr (const char* name, tree type)
3840 return build_library_fn (get_identifier (name), type);
3843 /* Like build_cp_library_fn, but takes a C string instead of an
3847 build_cp_library_fn_ptr (const char* name, tree type)
3849 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3852 /* Like build_library_fn, but also pushes the function so that we will
3853 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
3854 may throw exceptions listed in RAISES. */
3857 push_library_fn (tree name, tree type, tree raises)
3862 type = build_exception_variant (type, raises);
3864 fn = build_library_fn (name, type);
3865 pushdecl_top_level (fn);
3869 /* Like build_cp_library_fn, but also pushes the function so that it
3870 will be found by normal lookup. */
3873 push_cp_library_fn (enum tree_code operator_code, tree type)
3875 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3882 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3886 push_void_library_fn (tree name, tree parmtypes)
3888 tree type = build_function_type (void_type_node, parmtypes);
3889 return push_library_fn (name, type, NULL_TREE);
3892 /* Like push_library_fn, but also note that this function throws
3893 and does not return. Used for __throw_foo and the like. */
3896 push_throw_library_fn (tree name, tree type)
3898 tree fn = push_library_fn (name, type, NULL_TREE);
3899 TREE_THIS_VOLATILE (fn) = 1;
3900 TREE_NOTHROW (fn) = 0;
3904 /* When we call finish_struct for an anonymous union, we create
3905 default copy constructors and such. But, an anonymous union
3906 shouldn't have such things; this function undoes the damage to the
3907 anonymous union type T.
3909 (The reason that we create the synthesized methods is that we don't
3910 distinguish `union { int i; }' from `typedef union { int i; } U'.
3911 The first is an anonymous union; the second is just an ordinary
3915 fixup_anonymous_aggr (tree t)
3919 /* Wipe out memory of synthesized methods. */
3920 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
3921 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3922 TYPE_HAS_COPY_CTOR (t) = 0;
3923 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
3924 TYPE_HAS_COPY_ASSIGN (t) = 0;
3925 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
3927 /* Splice the implicitly generated functions out of the TYPE_METHODS
3929 q = &TYPE_METHODS (t);
3932 if (DECL_ARTIFICIAL (*q))
3933 *q = TREE_CHAIN (*q);
3935 q = &DECL_CHAIN (*q);
3938 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3939 if (TYPE_METHODS (t))
3941 tree decl = TYPE_MAIN_DECL (t);
3943 if (TREE_CODE (t) != UNION_TYPE)
3944 error_at (DECL_SOURCE_LOCATION (decl),
3945 "an anonymous struct cannot have function members");
3947 error_at (DECL_SOURCE_LOCATION (decl),
3948 "an anonymous union cannot have function members");
3951 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3952 assignment operators (because they cannot have these methods themselves).
3953 For anonymous unions this is already checked because they are not allowed
3954 in any union, otherwise we have to check it. */
3955 if (TREE_CODE (t) != UNION_TYPE)
3959 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3960 if (TREE_CODE (field) == FIELD_DECL)
3962 type = TREE_TYPE (field);
3963 if (CLASS_TYPE_P (type))
3965 if (TYPE_NEEDS_CONSTRUCTING (type))
3966 error ("member %q+#D with constructor not allowed "
3967 "in anonymous aggregate", field);
3968 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3969 error ("member %q+#D with destructor not allowed "
3970 "in anonymous aggregate", field);
3971 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3972 error ("member %q+#D with copy assignment operator "
3973 "not allowed in anonymous aggregate", field);
3979 /* Make sure that a declaration with no declarator is well-formed, i.e.
3980 just declares a tagged type or anonymous union.
3982 Returns the type declared; or NULL_TREE if none. */
3985 check_tag_decl (cp_decl_specifier_seq *declspecs)
3987 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3988 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3989 /* If a class, struct, or enum type is declared by the DECLSPECS
3990 (i.e, if a class-specifier, enum-specifier, or non-typename
3991 elaborated-type-specifier appears in the DECLSPECS),
3992 DECLARED_TYPE is set to the corresponding type. */
3993 tree declared_type = NULL_TREE;
3994 bool error_p = false;
3996 if (declspecs->multiple_types_p)
3997 error ("multiple types in one declaration");
3998 else if (declspecs->redefined_builtin_type)
4000 if (!in_system_header)
4001 permerror (input_location, "redeclaration of C++ built-in type %qT",
4002 declspecs->redefined_builtin_type);
4007 && TYPE_P (declspecs->type)
4008 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4009 && MAYBE_CLASS_TYPE_P (declspecs->type))
4010 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4011 declared_type = declspecs->type;
4012 else if (declspecs->type == error_mark_node)
4014 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4015 permerror (input_location, "declaration does not declare anything");
4016 /* Check for an anonymous union. */
4017 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4018 && TYPE_ANONYMOUS_P (declared_type))
4020 /* 7/3 In a simple-declaration, the optional init-declarator-list
4021 can be omitted only when declaring a class (clause 9) or
4022 enumeration (7.2), that is, when the decl-specifier-seq contains
4023 either a class-specifier, an elaborated-type-specifier with
4024 a class-key (9.1), or an enum-specifier. In these cases and
4025 whenever a class-specifier or enum-specifier is present in the
4026 decl-specifier-seq, the identifiers in these specifiers are among
4027 the names being declared by the declaration (as class-name,
4028 enum-names, or enumerators, depending on the syntax). In such
4029 cases, and except for the declaration of an unnamed bit-field (9.6),
4030 the decl-specifier-seq shall introduce one or more names into the
4031 program, or shall redeclare a name introduced by a previous
4032 declaration. [Example:
4033 enum { }; // ill-formed
4034 typedef class { }; // ill-formed
4038 error ("missing type-name in typedef-declaration");
4041 /* Anonymous unions are objects, so they can have specifiers. */;
4042 SET_ANON_AGGR_TYPE_P (declared_type);
4044 if (TREE_CODE (declared_type) != UNION_TYPE && !in_system_header)
4045 pedwarn (input_location, OPT_pedantic, "ISO C++ prohibits anonymous structs");
4050 if (declspecs->specs[(int)ds_inline]
4051 || declspecs->specs[(int)ds_virtual])
4052 error ("%qs can only be specified for functions",
4053 declspecs->specs[(int)ds_inline]
4054 ? "inline" : "virtual");
4056 && (!current_class_type
4057 || current_scope () != current_class_type))
4058 error ("%<friend%> can only be specified inside a class");
4059 else if (declspecs->specs[(int)ds_explicit])
4060 error ("%<explicit%> can only be specified for constructors");
4061 else if (declspecs->storage_class)
4062 error ("a storage class can only be specified for objects "
4064 else if (declspecs->specs[(int)ds_const]
4065 || declspecs->specs[(int)ds_volatile]
4066 || declspecs->specs[(int)ds_restrict]
4067 || declspecs->specs[(int)ds_thread])
4068 error ("qualifiers can only be specified for objects "
4070 else if (saw_typedef)
4071 warning (0, "%<typedef%> was ignored in this declaration");
4072 else if (declspecs->specs[(int) ds_constexpr])
4073 error ("%<constexpr%> cannot be used for type declarations");
4076 return declared_type;
4079 /* Called when a declaration is seen that contains no names to declare.
4080 If its type is a reference to a structure, union or enum inherited
4081 from a containing scope, shadow that tag name for the current scope
4082 with a forward reference.
4083 If its type defines a new named structure or union
4084 or defines an enum, it is valid but we need not do anything here.
4085 Otherwise, it is an error.
4087 C++: may have to grok the declspecs to learn about static,
4088 complain for anonymous unions.
4090 Returns the TYPE declared -- or NULL_TREE if none. */
4093 shadow_tag (cp_decl_specifier_seq *declspecs)
4095 tree t = check_tag_decl (declspecs);
4100 if (declspecs->attributes)
4102 warning (0, "attribute ignored in declaration of %q+#T", t);
4103 warning (0, "attribute for %q+#T must follow the %qs keyword",
4104 t, class_key_or_enum_as_string (t));
4108 if (maybe_process_partial_specialization (t) == error_mark_node)
4111 /* This is where the variables in an anonymous union are
4112 declared. An anonymous union declaration looks like:
4114 because there is no declarator after the union, the parser
4115 sends that declaration here. */
4116 if (ANON_AGGR_TYPE_P (t))
4118 fixup_anonymous_aggr (t);
4120 if (TYPE_FIELDS (t))
4122 tree decl = grokdeclarator (/*declarator=*/NULL,
4123 declspecs, NORMAL, 0, NULL);