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 (!prototype_p (f2) && 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 (!prototype_p (f1)
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);
1014 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1015 || targetm.comp_type_attributes (TREE_TYPE (newdecl),
1016 TREE_TYPE (olddecl)) != 0);
1021 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1023 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1024 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1027 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1028 DECL_TEMPLATE_PARMS (olddecl)))
1031 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1032 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1033 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1035 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1036 DECL_TEMPLATE_RESULT (newdecl));
1040 /* Need to check scope for variable declaration (VAR_DECL).
1041 For typedef (TYPE_DECL), scope is ignored. */
1042 if (TREE_CODE (newdecl) == VAR_DECL
1043 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1045 Two declarations for an object with C language linkage
1046 with the same name (ignoring the namespace that qualify
1047 it) that appear in different namespace scopes refer to
1049 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1052 if (TREE_TYPE (newdecl) == error_mark_node)
1053 types_match = TREE_TYPE (olddecl) == error_mark_node;
1054 else if (TREE_TYPE (olddecl) == NULL_TREE)
1055 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1056 else if (TREE_TYPE (newdecl) == NULL_TREE)
1059 types_match = comptypes (TREE_TYPE (newdecl),
1060 TREE_TYPE (olddecl),
1061 COMPARE_REDECLARATION);
1067 /* If NEWDECL is `static' and an `extern' was seen previously,
1068 warn about it. OLDDECL is the previous declaration.
1070 Note that this does not apply to the C++ case of declaring
1071 a variable `extern const' and then later `const'.
1073 Don't complain about built-in functions, since they are beyond
1074 the user's control. */
1077 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1079 if (TREE_CODE (newdecl) == TYPE_DECL
1080 || TREE_CODE (newdecl) == TEMPLATE_DECL
1081 || TREE_CODE (newdecl) == CONST_DECL
1082 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1085 /* Don't get confused by static member functions; that's a different
1087 if (TREE_CODE (newdecl) == FUNCTION_DECL
1088 && DECL_STATIC_FUNCTION_P (newdecl))
1091 /* If the old declaration was `static', or the new one isn't, then
1092 then everything is OK. */
1093 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1096 /* It's OK to declare a builtin function as `static'. */
1097 if (TREE_CODE (olddecl) == FUNCTION_DECL
1098 && DECL_ARTIFICIAL (olddecl))
1101 permerror (input_location, "%qD was declared %<extern%> and later %<static%>", newdecl);
1102 permerror (input_location, "previous declaration of %q+D", olddecl);
1105 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1106 function templates. If their exception specifications do not
1107 match, issue a diagnostic. */
1110 check_redeclaration_exception_specification (tree new_decl,
1115 tree new_exceptions;
1116 tree old_exceptions;
1118 new_type = TREE_TYPE (new_decl);
1119 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1120 old_type = TREE_TYPE (old_decl);
1121 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1125 If any declaration of a function has an exception-specification,
1126 all declarations, including the definition and an explicit
1127 specialization, of that function shall have an
1128 exception-specification with the same set of type-ids. */
1129 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1130 && ! DECL_IS_BUILTIN (old_decl)
1132 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1134 error ("declaration of %qF has a different exception specifier",
1136 error ("from previous declaration %q+F", old_decl);
1140 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1141 Otherwise issue diagnostics. */
1144 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1146 old_decl = STRIP_TEMPLATE (old_decl);
1147 new_decl = STRIP_TEMPLATE (new_decl);
1148 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1149 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1151 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1152 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1154 if (TREE_CODE (old_decl) == FUNCTION_DECL && DECL_BUILT_IN (old_decl))
1156 /* Hide a built-in declaration. */
1157 DECL_DECLARED_CONSTEXPR_P (old_decl)
1158 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1161 error ("redeclaration %qD differs in %<constexpr%>", new_decl);
1162 error ("from previous declaration %q+D", old_decl);
1166 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1167 && lookup_attribute ("gnu_inline", \
1168 DECL_ATTRIBUTES (fn)))
1170 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1171 If the redeclaration is invalid, a diagnostic is issued, and the
1172 error_mark_node is returned. Otherwise, OLDDECL is returned.
1174 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1177 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1180 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1182 unsigned olddecl_uid = DECL_UID (olddecl);
1183 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1184 int new_defines_function = 0;
1185 tree new_template_info;
1187 if (newdecl == olddecl)
1190 types_match = decls_match (newdecl, olddecl);
1192 /* If either the type of the new decl or the type of the old decl is an
1193 error_mark_node, then that implies that we have already issued an
1194 error (earlier) for some bogus type specification, and in that case,
1195 it is rather pointless to harass the user with yet more error message
1196 about the same declaration, so just pretend the types match here. */
1197 if (TREE_TYPE (newdecl) == error_mark_node
1198 || TREE_TYPE (olddecl) == error_mark_node)
1199 return error_mark_node;
1201 if (DECL_P (olddecl)
1202 && TREE_CODE (newdecl) == FUNCTION_DECL
1203 && TREE_CODE (olddecl) == FUNCTION_DECL
1204 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1206 if (DECL_DECLARED_INLINE_P (newdecl)
1207 && DECL_UNINLINABLE (newdecl)
1208 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1209 /* Already warned elsewhere. */;
1210 else if (DECL_DECLARED_INLINE_P (olddecl)
1211 && DECL_UNINLINABLE (olddecl)
1212 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1213 /* Already warned. */;
1214 else if (DECL_DECLARED_INLINE_P (newdecl)
1215 && DECL_UNINLINABLE (olddecl)
1216 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1218 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1220 warning (OPT_Wattributes, "previous declaration of %q+D "
1221 "with attribute noinline", olddecl);
1223 else if (DECL_DECLARED_INLINE_P (olddecl)
1224 && DECL_UNINLINABLE (newdecl)
1225 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1227 warning (OPT_Wattributes, "function %q+D redeclared with "
1228 "attribute noinline", newdecl);
1229 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1234 /* Check for redeclaration and other discrepancies. */
1235 if (TREE_CODE (olddecl) == FUNCTION_DECL
1236 && DECL_ARTIFICIAL (olddecl))
1238 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1239 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1241 /* Avoid warnings redeclaring built-ins which have not been
1242 explicitly declared. */
1243 if (DECL_ANTICIPATED (olddecl))
1246 /* If you declare a built-in or predefined function name as static,
1247 the old definition is overridden, but optionally warn this was a
1248 bad choice of name. */
1249 if (! TREE_PUBLIC (newdecl))
1251 warning (OPT_Wshadow,
1252 DECL_BUILT_IN (olddecl)
1253 ? G_("shadowing built-in function %q#D")
1254 : G_("shadowing library function %q#D"), olddecl);
1255 /* Discard the old built-in function. */
1258 /* If the built-in is not ansi, then programs can override
1259 it even globally without an error. */
1260 else if (! DECL_BUILT_IN (olddecl))
1261 warning (0, "library function %q#D redeclared as non-function %q#D",
1265 error ("declaration of %q#D", newdecl);
1266 error ("conflicts with built-in declaration %q#D",
1271 else if (!types_match)
1273 /* Avoid warnings redeclaring built-ins which have not been
1274 explicitly declared. */
1275 if (DECL_ANTICIPATED (olddecl))
1277 /* Deal with fileptr_type_node. FILE type is not known
1278 at the time we create the builtins. */
1281 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1282 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1284 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1287 else if (TREE_VALUE (t2) == fileptr_type_node)
1289 tree t = TREE_VALUE (t1);
1291 if (TREE_CODE (t) == POINTER_TYPE
1292 && TYPE_NAME (TREE_TYPE (t))
1293 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1294 == get_identifier ("FILE")
1295 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1297 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1299 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1300 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1301 types_match = decls_match (newdecl, olddecl);
1303 return duplicate_decls (newdecl, olddecl,
1305 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1308 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1311 else if ((DECL_EXTERN_C_P (newdecl)
1312 && DECL_EXTERN_C_P (olddecl))
1313 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1314 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1316 /* A near match; override the builtin. */
1318 if (TREE_PUBLIC (newdecl))
1320 warning (0, "new declaration %q#D", newdecl);
1321 warning (0, "ambiguates built-in declaration %q#D",
1325 warning (OPT_Wshadow,
1326 DECL_BUILT_IN (olddecl)
1327 ? G_("shadowing built-in function %q#D")
1328 : G_("shadowing library function %q#D"), olddecl);
1331 /* Discard the old built-in function. */
1334 /* Replace the old RTL to avoid problems with inlining. */
1335 COPY_DECL_RTL (newdecl, olddecl);
1337 /* Even if the types match, prefer the new declarations type for
1338 built-ins which have not been explicitly declared, for
1339 exception lists, etc... */
1340 else if (DECL_IS_BUILTIN (olddecl))
1342 tree type = TREE_TYPE (newdecl);
1343 tree attribs = (*targetm.merge_type_attributes)
1344 (TREE_TYPE (olddecl), type);
1346 type = cp_build_type_attribute_variant (type, attribs);
1347 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1350 /* If a function is explicitly declared "throw ()", propagate that to
1351 the corresponding builtin. */
1352 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1353 && DECL_ANTICIPATED (olddecl)
1354 && TREE_NOTHROW (newdecl)
1355 && !TREE_NOTHROW (olddecl)
1356 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != NULL_TREE
1357 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != olddecl
1359 TREE_NOTHROW (built_in_decls [DECL_FUNCTION_CODE (olddecl)]) = 1;
1361 /* Whether or not the builtin can throw exceptions has no
1362 bearing on this declarator. */
1363 TREE_NOTHROW (olddecl) = 0;
1365 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1367 /* If a builtin function is redeclared as `static', merge
1368 the declarations, but make the original one static. */
1369 DECL_THIS_STATIC (olddecl) = 1;
1370 TREE_PUBLIC (olddecl) = 0;
1372 /* Make the old declaration consistent with the new one so
1373 that all remnants of the builtin-ness of this function
1374 will be banished. */
1375 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1376 COPY_DECL_RTL (newdecl, olddecl);
1379 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1381 /* C++ Standard, 3.3, clause 4:
1382 "[Note: a namespace name or a class template name must be unique
1383 in its declarative region (7.3.2, clause 14). ]" */
1384 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1385 && TREE_CODE (newdecl) != NAMESPACE_DECL
1386 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1387 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1388 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1389 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1391 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1392 && TREE_CODE (newdecl) != TYPE_DECL)
1393 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1394 && TREE_CODE (olddecl) != TYPE_DECL))
1396 /* We do nothing special here, because C++ does such nasty
1397 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1398 get shadowed, and know that if we need to find a TYPE_DECL
1399 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1400 slot of the identifier. */
1404 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1405 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1406 || (TREE_CODE (olddecl) == FUNCTION_DECL
1407 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1411 error ("%q#D redeclared as different kind of symbol", newdecl);
1412 if (TREE_CODE (olddecl) == TREE_LIST)
1413 olddecl = TREE_VALUE (olddecl);
1414 error ("previous declaration of %q+#D", olddecl);
1416 return error_mark_node;
1418 else if (!types_match)
1420 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1421 /* These are certainly not duplicate declarations; they're
1422 from different scopes. */
1425 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1427 /* The name of a class template may not be declared to refer to
1428 any other template, class, function, object, namespace, value,
1429 or type in the same scope. */
1430 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1431 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1433 error ("declaration of template %q#D", newdecl);
1434 error ("conflicts with previous declaration %q+#D", olddecl);
1436 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1437 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1438 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1439 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1440 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1441 DECL_TEMPLATE_PARMS (olddecl))
1442 /* Template functions can be disambiguated by
1444 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1445 TREE_TYPE (TREE_TYPE (olddecl))))
1447 error ("new declaration %q#D", newdecl);
1448 error ("ambiguates old declaration %q+#D", olddecl);
1452 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1454 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1456 error ("declaration of C function %q#D conflicts with",
1458 error ("previous declaration %q+#D here", olddecl);
1460 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1461 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1463 error ("new declaration %q#D", newdecl);
1464 error ("ambiguates old declaration %q+#D", olddecl);
1465 return error_mark_node;
1472 error ("conflicting declaration %q#D", newdecl);
1473 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1474 return error_mark_node;
1477 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1478 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1479 && (!DECL_TEMPLATE_INFO (newdecl)
1480 || (DECL_TI_TEMPLATE (newdecl)
1481 != DECL_TI_TEMPLATE (olddecl))))
1482 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1483 && (!DECL_TEMPLATE_INFO (olddecl)
1484 || (DECL_TI_TEMPLATE (olddecl)
1485 != DECL_TI_TEMPLATE (newdecl))))))
1486 /* It's OK to have a template specialization and a non-template
1487 with the same type, or to have specializations of two
1488 different templates with the same type. Note that if one is a
1489 specialization, and the other is an instantiation of the same
1490 template, that we do not exit at this point. That situation
1491 can occur if we instantiate a template class, and then
1492 specialize one of its methods. This situation is valid, but
1493 the declarations must be merged in the usual way. */
1495 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1496 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1497 && !DECL_USE_TEMPLATE (newdecl))
1498 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1499 && !DECL_USE_TEMPLATE (olddecl))))
1500 /* One of the declarations is a template instantiation, and the
1501 other is not a template at all. That's OK. */
1503 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1505 /* In [namespace.alias] we have:
1507 In a declarative region, a namespace-alias-definition can be
1508 used to redefine a namespace-alias declared in that declarative
1509 region to refer only to the namespace to which it already
1512 Therefore, if we encounter a second alias directive for the same
1513 alias, we can just ignore the second directive. */
1514 if (DECL_NAMESPACE_ALIAS (newdecl)
1515 && (DECL_NAMESPACE_ALIAS (newdecl)
1516 == DECL_NAMESPACE_ALIAS (olddecl)))
1518 /* [namespace.alias]
1520 A namespace-name or namespace-alias shall not be declared as
1521 the name of any other entity in the same declarative region.
1522 A namespace-name defined at global scope shall not be
1523 declared as the name of any other entity in any global scope
1525 error ("declaration of namespace %qD conflicts with", newdecl);
1526 error ("previous declaration of namespace %q+D here", olddecl);
1527 return error_mark_node;
1531 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1534 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1535 if (DECL_NAME (olddecl) != NULL_TREE)
1536 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1537 ? "%q+#D previously defined here"
1538 : "%q+#D previously declared here", olddecl);
1539 return error_mark_node;
1541 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1542 && DECL_INITIAL (olddecl) != NULL_TREE
1543 && !prototype_p (TREE_TYPE (olddecl))
1544 && prototype_p (TREE_TYPE (newdecl)))
1546 /* Prototype decl follows defn w/o prototype. */
1547 warning_at (input_location, 0, "prototype for %q+#D", newdecl);
1548 warning_at (DECL_SOURCE_LOCATION (olddecl), 0,
1549 "follows non-prototype definition here");
1551 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1552 || TREE_CODE (olddecl) == VAR_DECL)
1553 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1556 If two declarations of the same function or object
1557 specify different linkage-specifications ..., the program
1558 is ill-formed.... Except for functions with C++ linkage,
1559 a function declaration without a linkage specification
1560 shall not precede the first linkage specification for
1561 that function. A function can be declared without a
1562 linkage specification after an explicit linkage
1563 specification has been seen; the linkage explicitly
1564 specified in the earlier declaration is not affected by
1565 such a function declaration.
1567 DR 563 raises the question why the restrictions on
1568 functions should not also apply to objects. Older
1569 versions of G++ silently ignore the linkage-specification
1577 which is clearly wrong. Therefore, we now treat objects
1579 if (current_lang_depth () == 0)
1581 /* There is no explicit linkage-specification, so we use
1582 the linkage from the previous declaration. */
1583 if (!DECL_LANG_SPECIFIC (newdecl))
1584 retrofit_lang_decl (newdecl);
1585 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1589 error ("previous declaration of %q+#D with %qL linkage",
1590 olddecl, DECL_LANGUAGE (olddecl));
1591 error ("conflicts with new declaration with %qL linkage",
1592 DECL_LANGUAGE (newdecl));
1596 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1598 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1600 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1601 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1604 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1605 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1607 for (; t1 && t1 != void_list_node;
1608 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1609 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1611 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1614 permerror (input_location, "default argument given for parameter %d of %q#D",
1616 permerror (input_location, "after previous specification in %q+#D", olddecl);
1620 error ("default argument given for parameter %d of %q#D",
1622 error ("after previous specification in %q+#D",
1629 /* Do not merge an implicit typedef with an explicit one. In:
1633 typedef class A A __attribute__ ((foo));
1635 the attribute should apply only to the typedef. */
1636 if (TREE_CODE (olddecl) == TYPE_DECL
1637 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1638 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1641 /* If new decl is `static' and an `extern' was seen previously,
1643 warn_extern_redeclared_static (newdecl, olddecl);
1645 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1646 return error_mark_node;
1648 /* We have committed to returning 1 at this point. */
1649 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1651 /* Now that functions must hold information normally held
1652 by field decls, there is extra work to do so that
1653 declaration information does not get destroyed during
1655 if (DECL_VINDEX (olddecl))
1656 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1657 if (DECL_CONTEXT (olddecl))
1658 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1659 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1660 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1661 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1662 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1663 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1664 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1665 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1666 SET_OVERLOADED_OPERATOR_CODE
1667 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1668 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1670 /* Optionally warn about more than one declaration for the same
1671 name, but don't warn about a function declaration followed by a
1673 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1674 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1675 /* Don't warn about extern decl followed by definition. */
1676 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1677 /* Don't warn about friends, let add_friend take care of it. */
1678 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1680 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1681 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1684 if (DECL_DELETED_FN (newdecl))
1686 error ("deleted definition of %qD", newdecl);
1687 error ("after previous declaration %q+D", olddecl);
1691 /* Deal with C++: must preserve virtual function table size. */
1692 if (TREE_CODE (olddecl) == TYPE_DECL)
1694 tree newtype = TREE_TYPE (newdecl);
1695 tree oldtype = TREE_TYPE (olddecl);
1697 if (newtype != error_mark_node && oldtype != error_mark_node
1698 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1699 CLASSTYPE_FRIEND_CLASSES (newtype)
1700 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1702 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1705 /* Copy all the DECL_... slots specified in the new decl
1706 except for any that we copy here from the old type. */
1707 DECL_ATTRIBUTES (newdecl)
1708 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1710 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1714 old_result = DECL_TEMPLATE_RESULT (olddecl);
1715 new_result = DECL_TEMPLATE_RESULT (newdecl);
1716 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1717 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1718 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1719 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1721 DECL_ATTRIBUTES (old_result)
1722 = (*targetm.merge_decl_attributes) (old_result, new_result);
1724 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1726 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1727 && DECL_INITIAL (new_result))
1729 if (DECL_INITIAL (old_result))
1730 DECL_UNINLINABLE (old_result) = 1;
1732 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1733 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1734 DECL_NOT_REALLY_EXTERN (old_result)
1735 = DECL_NOT_REALLY_EXTERN (new_result);
1736 DECL_INTERFACE_KNOWN (old_result)
1737 = DECL_INTERFACE_KNOWN (new_result);
1738 DECL_DECLARED_INLINE_P (old_result)
1739 = DECL_DECLARED_INLINE_P (new_result);
1740 DECL_DISREGARD_INLINE_LIMITS (old_result)
1741 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1746 DECL_DECLARED_INLINE_P (old_result)
1747 |= DECL_DECLARED_INLINE_P (new_result);
1748 DECL_DISREGARD_INLINE_LIMITS (old_result)
1749 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1750 check_redeclaration_exception_specification (newdecl, olddecl);
1754 /* If the new declaration is a definition, update the file and
1755 line information on the declaration, and also make
1756 the old declaration the same definition. */
1757 if (DECL_INITIAL (new_result) != NULL_TREE)
1759 DECL_SOURCE_LOCATION (olddecl)
1760 = DECL_SOURCE_LOCATION (old_result)
1761 = DECL_SOURCE_LOCATION (newdecl);
1762 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1763 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1766 DECL_ARGUMENTS (old_result)
1767 = DECL_ARGUMENTS (new_result);
1768 for (parm = DECL_ARGUMENTS (old_result); parm;
1769 parm = DECL_CHAIN (parm))
1770 DECL_CONTEXT (parm) = old_result;
1779 /* Automatically handles default parameters. */
1780 tree oldtype = TREE_TYPE (olddecl);
1783 /* Merge the data types specified in the two decls. */
1784 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1786 /* If merge_types produces a non-typedef type, just use the old type. */
1787 if (TREE_CODE (newdecl) == TYPE_DECL
1788 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1791 if (TREE_CODE (newdecl) == VAR_DECL)
1793 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1794 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1795 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1796 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1797 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1798 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1800 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1801 if (DECL_LANG_SPECIFIC (olddecl)
1802 && CP_DECL_THREADPRIVATE_P (olddecl))
1804 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1805 if (!DECL_LANG_SPECIFIC (newdecl))
1806 retrofit_lang_decl (newdecl);
1808 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1809 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1813 /* Do this after calling `merge_types' so that default
1814 parameters don't confuse us. */
1815 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1816 check_redeclaration_exception_specification (newdecl, olddecl);
1817 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1819 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1820 check_default_args (newdecl);
1822 /* Lay the type out, unless already done. */
1823 if (! same_type_p (newtype, oldtype)
1824 && TREE_TYPE (newdecl) != error_mark_node
1825 && !(processing_template_decl && uses_template_parms (newdecl)))
1826 layout_type (TREE_TYPE (newdecl));
1828 if ((TREE_CODE (newdecl) == VAR_DECL
1829 || TREE_CODE (newdecl) == PARM_DECL
1830 || TREE_CODE (newdecl) == RESULT_DECL
1831 || TREE_CODE (newdecl) == FIELD_DECL
1832 || TREE_CODE (newdecl) == TYPE_DECL)
1833 && !(processing_template_decl && uses_template_parms (newdecl)))
1834 layout_decl (newdecl, 0);
1836 /* Merge the type qualifiers. */
1837 if (TREE_READONLY (newdecl))
1838 TREE_READONLY (olddecl) = 1;
1839 if (TREE_THIS_VOLATILE (newdecl))
1840 TREE_THIS_VOLATILE (olddecl) = 1;
1841 if (TREE_NOTHROW (newdecl))
1842 TREE_NOTHROW (olddecl) = 1;
1844 /* Merge deprecatedness. */
1845 if (TREE_DEPRECATED (newdecl))
1846 TREE_DEPRECATED (olddecl) = 1;
1848 /* Preserve function specific target and optimization options */
1849 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1851 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1852 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1853 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1854 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1856 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1857 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1858 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1859 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1862 /* Merge the initialization information. */
1863 if (DECL_INITIAL (newdecl) == NULL_TREE
1864 && DECL_INITIAL (olddecl) != NULL_TREE)
1866 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1867 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1868 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1870 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1871 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1875 /* Merge the section attribute.
1876 We want to issue an error if the sections conflict but that must be
1877 done later in decl_attributes since we are called before attributes
1879 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1880 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1882 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1884 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1885 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1886 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1887 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1888 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1889 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1890 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1891 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1892 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1893 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
1894 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
1895 /* Keep the old RTL. */
1896 COPY_DECL_RTL (olddecl, newdecl);
1898 else if (TREE_CODE (newdecl) == VAR_DECL
1899 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1901 /* Keep the old RTL. We cannot keep the old RTL if the old
1902 declaration was for an incomplete object and the new
1903 declaration is not since many attributes of the RTL will
1905 COPY_DECL_RTL (olddecl, newdecl);
1908 /* If cannot merge, then use the new type and qualifiers,
1909 and don't preserve the old rtl. */
1912 /* Clean out any memory we had of the old declaration. */
1913 tree oldstatic = value_member (olddecl, static_aggregates);
1915 TREE_VALUE (oldstatic) = error_mark_node;
1917 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1918 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1919 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1920 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1923 /* Merge the storage class information. */
1924 merge_weak (newdecl, olddecl);
1926 if (DECL_ONE_ONLY (olddecl))
1927 DECL_COMDAT_GROUP (newdecl) = DECL_COMDAT_GROUP (olddecl);
1929 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1930 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1931 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1932 if (! DECL_EXTERNAL (olddecl))
1933 DECL_EXTERNAL (newdecl) = 0;
1935 new_template_info = NULL_TREE;
1936 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1938 bool new_redefines_gnu_inline = false;
1940 if (new_defines_function
1941 && ((DECL_INTERFACE_KNOWN (olddecl)
1942 && TREE_CODE (olddecl) == FUNCTION_DECL)
1943 || (TREE_CODE (olddecl) == TEMPLATE_DECL
1944 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1945 == FUNCTION_DECL))))
1949 if (TREE_CODE (fn) == TEMPLATE_DECL)
1950 fn = DECL_TEMPLATE_RESULT (olddecl);
1952 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
1955 if (!new_redefines_gnu_inline)
1957 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1958 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1959 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1961 DECL_TEMPLATE_INSTANTIATED (newdecl)
1962 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1963 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
1965 /* If the OLDDECL is an instantiation and/or specialization,
1966 then the NEWDECL must be too. But, it may not yet be marked
1967 as such if the caller has created NEWDECL, but has not yet
1968 figured out that it is a redeclaration. */
1969 if (!DECL_USE_TEMPLATE (newdecl))
1970 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1972 /* Don't really know how much of the language-specific
1973 values we should copy from old to new. */
1974 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1975 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1976 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1977 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1979 if (LANG_DECL_HAS_MIN (newdecl))
1981 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
1982 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
1983 if (DECL_TEMPLATE_INFO (newdecl))
1984 new_template_info = DECL_TEMPLATE_INFO (newdecl);
1985 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1987 /* Only functions have these fields. */
1988 if (TREE_CODE (newdecl) == FUNCTION_DECL
1989 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1991 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1992 olddecl_friend = DECL_FRIEND_P (olddecl);
1993 hidden_friend = (DECL_ANTICIPATED (olddecl)
1994 && DECL_HIDDEN_FRIEND_P (olddecl)
1995 && newdecl_is_friend);
1996 DECL_BEFRIENDING_CLASSES (newdecl)
1997 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1998 DECL_BEFRIENDING_CLASSES (olddecl));
1999 /* DECL_THUNKS is only valid for virtual functions,
2000 otherwise it is a DECL_FRIEND_CONTEXT. */
2001 if (DECL_VIRTUAL_P (newdecl))
2002 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
2004 /* Only variables have this field. */
2005 else if (TREE_CODE (newdecl) == VAR_DECL
2006 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2007 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2010 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2014 /* Merge parameter attributes. */
2015 tree oldarg, newarg;
2016 for (oldarg = DECL_ARGUMENTS(olddecl),
2017 newarg = DECL_ARGUMENTS(newdecl);
2019 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2020 DECL_ATTRIBUTES (newarg)
2021 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2022 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2025 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2026 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2028 /* If newdecl is not a specialization, then it is not a
2029 template-related function at all. And that means that we
2030 should have exited above, returning 0. */
2031 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2033 if (DECL_ODR_USED (olddecl))
2034 /* From [temp.expl.spec]:
2036 If a template, a member template or the member of a class
2037 template is explicitly specialized then that
2038 specialization shall be declared before the first use of
2039 that specialization that would cause an implicit
2040 instantiation to take place, in every translation unit in
2041 which such a use occurs. */
2042 error ("explicit specialization of %qD after first use",
2045 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2047 /* Don't propagate visibility from the template to the
2048 specialization here. We'll do that in determine_visibility if
2050 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2052 /* [temp.expl.spec/14] We don't inline explicit specialization
2053 just because the primary template says so. */
2055 else if (new_defines_function && DECL_INITIAL (olddecl))
2057 /* Never inline re-defined extern inline functions.
2058 FIXME: this could be better handled by keeping both
2059 function as separate declarations. */
2060 DECL_UNINLINABLE (newdecl) = 1;
2064 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2065 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2067 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2069 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2070 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2072 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2073 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2074 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2075 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2078 /* Preserve abstractness on cloned [cd]tors. */
2079 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
2081 /* Update newdecl's parms to point at olddecl. */
2082 for (parm = DECL_ARGUMENTS (newdecl); parm;
2083 parm = DECL_CHAIN (parm))
2084 DECL_CONTEXT (parm) = olddecl;
2088 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2089 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2090 COPY_DECL_RTL (newdecl, olddecl);
2092 if (! types_match || new_defines_function)
2094 /* These need to be copied so that the names are available.
2095 Note that if the types do match, we'll preserve inline
2096 info and other bits, but if not, we won't. */
2097 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2098 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2100 if (new_defines_function)
2101 /* If defining a function declared with other language
2102 linkage, use the previously declared language linkage. */
2103 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2104 else if (types_match)
2106 /* If redeclaring a builtin function, and not a definition,
2107 it stays built in. */
2108 if (DECL_BUILT_IN (olddecl))
2110 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2111 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2112 /* If we're keeping the built-in definition, keep the rtl,
2113 regardless of declaration matches. */
2114 COPY_DECL_RTL (olddecl, newdecl);
2117 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2118 /* Don't clear out the arguments if we're just redeclaring a
2120 if (DECL_ARGUMENTS (olddecl))
2121 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2124 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2125 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2127 /* Now preserve various other info from the definition. */
2128 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2129 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2130 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2131 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2133 /* Warn about conflicting visibility specifications. */
2134 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2135 && DECL_VISIBILITY_SPECIFIED (newdecl)
2136 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2138 warning_at (input_location, OPT_Wattributes,
2139 "%q+D: visibility attribute ignored because it", newdecl);
2140 warning_at (DECL_SOURCE_LOCATION (olddecl), OPT_Wattributes,
2141 "conflicts with previous declaration here");
2143 /* Choose the declaration which specified visibility. */
2144 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2146 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2147 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2149 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2150 so keep this behavior. */
2151 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2153 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2154 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2156 /* Likewise for DECL_USER_ALIGN and DECL_PACKED. */
2157 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2158 if (TREE_CODE (newdecl) == FIELD_DECL)
2159 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2161 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2162 with that from NEWDECL below. */
2163 if (DECL_LANG_SPECIFIC (olddecl))
2165 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2166 != DECL_LANG_SPECIFIC (newdecl));
2167 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2170 /* Merge the USED information. */
2171 if (TREE_USED (olddecl))
2172 TREE_USED (newdecl) = 1;
2173 else if (TREE_USED (newdecl))
2174 TREE_USED (olddecl) = 1;
2175 if (TREE_CODE (newdecl) == VAR_DECL)
2177 if (DECL_READ_P (olddecl))
2178 DECL_READ_P (newdecl) = 1;
2179 else if (DECL_READ_P (newdecl))
2180 DECL_READ_P (olddecl) = 1;
2182 if (DECL_PRESERVE_P (olddecl))
2183 DECL_PRESERVE_P (newdecl) = 1;
2184 else if (DECL_PRESERVE_P (newdecl))
2185 DECL_PRESERVE_P (olddecl) = 1;
2187 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2191 function_size = sizeof (struct tree_decl_common);
2193 memcpy ((char *) olddecl + sizeof (struct tree_common),
2194 (char *) newdecl + sizeof (struct tree_common),
2195 function_size - sizeof (struct tree_common));
2197 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2198 (char *) newdecl + sizeof (struct tree_decl_common),
2199 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2200 if (new_template_info)
2201 /* If newdecl is a template instantiation, it is possible that
2202 the following sequence of events has occurred:
2204 o A friend function was declared in a class template. The
2205 class template was instantiated.
2207 o The instantiation of the friend declaration was
2208 recorded on the instantiation list, and is newdecl.
2210 o Later, however, instantiate_class_template called pushdecl
2211 on the newdecl to perform name injection. But, pushdecl in
2212 turn called duplicate_decls when it discovered that another
2213 declaration of a global function with the same name already
2216 o Here, in duplicate_decls, we decided to clobber newdecl.
2218 If we're going to do that, we'd better make sure that
2219 olddecl, and not newdecl, is on the list of
2220 instantiations so that if we try to do the instantiation
2221 again we won't get the clobbered declaration. */
2222 reregister_specialization (newdecl,
2228 size_t size = tree_code_size (TREE_CODE (olddecl));
2229 memcpy ((char *) olddecl + sizeof (struct tree_common),
2230 (char *) newdecl + sizeof (struct tree_common),
2231 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2232 switch (TREE_CODE (olddecl))
2242 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2243 (char *) newdecl + sizeof (struct tree_decl_common),
2244 size - sizeof (struct tree_decl_common)
2245 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2249 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2250 (char *) newdecl + sizeof (struct tree_decl_common),
2251 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2252 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2256 DECL_UID (olddecl) = olddecl_uid;
2258 DECL_FRIEND_P (olddecl) = 1;
2261 DECL_ANTICIPATED (olddecl) = 1;
2262 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2265 /* NEWDECL contains the merged attribute lists.
2266 Update OLDDECL to be the same. */
2267 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2269 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2270 so that encode_section_info has a chance to look at the new decl
2271 flags and attributes. */
2272 if (DECL_RTL_SET_P (olddecl)
2273 && (TREE_CODE (olddecl) == FUNCTION_DECL
2274 || (TREE_CODE (olddecl) == VAR_DECL
2275 && TREE_STATIC (olddecl))))
2276 make_decl_rtl (olddecl);
2278 /* The NEWDECL will no longer be needed. Because every out-of-class
2279 declaration of a member results in a call to duplicate_decls,
2280 freeing these nodes represents in a significant savings. */
2286 /* Return zero if the declaration NEWDECL is valid
2287 when the declaration OLDDECL (assumed to be for the same name)
2288 has already been seen.
2289 Otherwise return an error message format string with a %s
2290 where the identifier should go. */
2293 redeclaration_error_message (tree newdecl, tree olddecl)
2295 if (TREE_CODE (newdecl) == TYPE_DECL)
2297 /* Because C++ can put things into name space for free,
2298 constructs like "typedef struct foo { ... } foo"
2299 would look like an erroneous redeclaration. */
2300 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2303 return G_("redefinition of %q#D");
2305 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2307 /* If this is a pure function, its olddecl will actually be
2308 the original initialization to `0' (which we force to call
2309 abort()). Don't complain about redefinition in this case. */
2310 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2311 && DECL_INITIAL (olddecl) == NULL_TREE)
2314 /* If both functions come from different namespaces, this is not
2315 a redeclaration - this is a conflict with a used function. */
2316 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2317 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2318 && ! decls_match (olddecl, newdecl))
2319 return G_("%qD conflicts with used function");
2321 /* We'll complain about linkage mismatches in
2322 warn_extern_redeclared_static. */
2324 /* Defining the same name twice is no good. */
2325 if (DECL_INITIAL (olddecl) != NULL_TREE
2326 && DECL_INITIAL (newdecl) != NULL_TREE)
2328 if (DECL_NAME (olddecl) == NULL_TREE)
2329 return G_("%q#D not declared in class");
2330 else if (!GNU_INLINE_P (olddecl)
2331 || GNU_INLINE_P (newdecl))
2332 return G_("redefinition of %q#D");
2335 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2337 bool olda = GNU_INLINE_P (olddecl);
2338 bool newa = GNU_INLINE_P (newdecl);
2343 return G_("%q+D redeclared inline with "
2344 "%<gnu_inline%> attribute");
2346 return G_("%q+D redeclared inline without "
2347 "%<gnu_inline%> attribute");
2353 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2357 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2359 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2360 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2361 return G_("redefinition of %q#D");
2365 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2366 || (DECL_TEMPLATE_RESULT (newdecl)
2367 == DECL_TEMPLATE_RESULT (olddecl)))
2370 nt = DECL_TEMPLATE_RESULT (newdecl);
2371 if (DECL_TEMPLATE_INFO (nt))
2372 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2373 ot = DECL_TEMPLATE_RESULT (olddecl);
2374 if (DECL_TEMPLATE_INFO (ot))
2375 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2376 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2377 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2378 return G_("redefinition of %q#D");
2380 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2382 bool olda = GNU_INLINE_P (ot);
2383 bool newa = GNU_INLINE_P (nt);
2388 return G_("%q+D redeclared inline with "
2389 "%<gnu_inline%> attribute");
2391 return G_("%q+D redeclared inline without "
2392 "%<gnu_inline%> attribute");
2396 /* Core issue #226 (C++0x):
2398 If a friend function template declaration specifies a
2399 default template-argument, that declaration shall be a
2400 definition and shall be the only declaration of the
2401 function template in the translation unit. */
2402 if ((cxx_dialect != cxx98)
2403 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2404 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2405 /*is_primary=*/1, /*is_partial=*/0,
2406 /*is_friend_decl=*/2))
2407 return G_("redeclaration of friend %q#D "
2408 "may not have default template arguments");
2412 else if (TREE_CODE (newdecl) == VAR_DECL
2413 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2414 && (! DECL_LANG_SPECIFIC (olddecl)
2415 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2416 || DECL_THREAD_LOCAL_P (newdecl)))
2418 /* Only variables can be thread-local, and all declarations must
2419 agree on this property. */
2420 if (DECL_THREAD_LOCAL_P (newdecl))
2421 return G_("thread-local declaration of %q#D follows "
2422 "non-thread-local declaration");
2424 return G_("non-thread-local declaration of %q#D follows "
2425 "thread-local declaration");
2427 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2429 /* The objects have been declared at namespace scope. If either
2430 is a member of an anonymous union, then this is an invalid
2431 redeclaration. For example:
2437 if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2438 || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
2439 return G_("redeclaration of %q#D");
2440 /* If at least one declaration is a reference, there is no
2441 conflict. For example:
2447 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2449 /* Reject two definitions. */
2450 return G_("redefinition of %q#D");
2454 /* Objects declared with block scope: */
2455 /* Reject two definitions, and reject a definition
2456 together with an external reference. */
2457 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2458 return G_("redeclaration of %q#D");
2463 /* Hash and equality functions for the named_label table. */
2466 named_label_entry_hash (const void *data)
2468 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2469 return DECL_UID (ent->label_decl);
2473 named_label_entry_eq (const void *a, const void *b)
2475 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2476 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2477 return ent_a->label_decl == ent_b->label_decl;
2480 /* Create a new label, named ID. */
2483 make_label_decl (tree id, int local_p)
2485 struct named_label_entry *ent;
2489 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2491 DECL_CONTEXT (decl) = current_function_decl;
2492 DECL_MODE (decl) = VOIDmode;
2493 C_DECLARED_LABEL_FLAG (decl) = local_p;
2495 /* Say where one reference is to the label, for the sake of the
2496 error if it is not defined. */
2497 DECL_SOURCE_LOCATION (decl) = input_location;
2499 /* Record the fact that this identifier is bound to this label. */
2500 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2502 /* Create the label htab for the function on demand. */
2504 named_labels = htab_create_ggc (13, named_label_entry_hash,
2505 named_label_entry_eq, NULL);
2507 /* Record this label on the list of labels used in this function.
2508 We do this before calling make_label_decl so that we get the
2509 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2510 ent = ggc_alloc_cleared_named_label_entry ();
2511 ent->label_decl = decl;
2513 slot = htab_find_slot (named_labels, ent, INSERT);
2514 gcc_assert (*slot == NULL);
2520 /* Look for a label named ID in the current function. If one cannot
2521 be found, create one. (We keep track of used, but undefined,
2522 labels, and complain about them at the end of a function.) */
2525 lookup_label (tree id)
2529 timevar_push (TV_NAME_LOOKUP);
2530 /* You can't use labels at global scope. */
2531 if (current_function_decl == NULL_TREE)
2533 error ("label %qE referenced outside of any function", id);
2534 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2537 /* See if we've already got this label. */
2538 decl = IDENTIFIER_LABEL_VALUE (id);
2539 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2540 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2542 decl = make_label_decl (id, /*local_p=*/0);
2543 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2546 /* Declare a local label named ID. */
2549 declare_local_label (tree id)
2552 cp_label_binding *bind;
2554 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2555 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2556 bind = VEC_safe_push (cp_label_binding, gc,
2557 current_binding_level->shadowed_labels, NULL);
2558 bind->prev_value = IDENTIFIER_LABEL_VALUE (id);
2560 decl = make_label_decl (id, /*local_p=*/1);
2566 /* Returns nonzero if it is ill-formed to jump past the declaration of
2567 DECL. Returns 2 if it's also a real problem. */
2570 decl_jump_unsafe (tree decl)
2572 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
2573 with automatic storage duration is not in scope to a point where it is
2574 in scope is ill-formed unless the variable has scalar type, class type
2575 with a trivial default constructor and a trivial destructor, a
2576 cv-qualified version of one of these types, or an array of one of the
2577 preceding types and is declared without an initializer (8.5). */
2578 tree type = TREE_TYPE (decl);
2580 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2581 || type == error_mark_node)
2584 type = strip_array_types (type);
2586 if (type_has_nontrivial_default_init (TREE_TYPE (decl))
2587 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2590 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2596 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2599 identify_goto (tree decl, const location_t *locus)
2602 permerror (input_location, "jump to label %qD", decl);
2604 permerror (input_location, "jump to case label");
2606 permerror (*locus, " from here");
2609 /* Check that a single previously seen jump to a newly defined label
2610 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2611 the jump context; NAMES are the names in scope in LEVEL at the jump
2612 context; LOCUS is the source position of the jump or 0. Returns
2613 true if all is well. */
2616 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2617 bool exited_omp, const location_t *locus)
2619 struct cp_binding_level *b;
2620 bool identified = false, saw_eh = false, saw_omp = false;
2624 identify_goto (decl, locus);
2625 error (" exits OpenMP structured block");
2626 identified = saw_omp = true;
2629 for (b = current_binding_level; b ; b = b->level_chain)
2631 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2633 for (new_decls = b->names; new_decls != old_decls;
2634 new_decls = DECL_CHAIN (new_decls))
2636 int problem = decl_jump_unsafe (new_decls);
2642 identify_goto (decl, locus);
2646 error (" crosses initialization of %q+#D", new_decls);
2648 permerror (input_location, " enters scope of %q+#D which has "
2649 "non-trivial destructor", new_decls);
2654 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2658 identify_goto (decl, locus);
2661 if (b->kind == sk_try)
2662 error (" enters try block");
2664 error (" enters catch block");
2667 if (b->kind == sk_omp && !saw_omp)
2671 identify_goto (decl, locus);
2674 error (" enters OpenMP structured block");
2683 check_previous_goto (tree decl, struct named_label_use_entry *use)
2685 check_previous_goto_1 (decl, use->binding_level,
2686 use->names_in_scope, use->in_omp_scope,
2687 &use->o_goto_locus);
2691 check_switch_goto (struct cp_binding_level* level)
2693 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2696 /* Check that a new jump to a label DECL is OK. Called by
2697 finish_goto_stmt. */
2700 check_goto (tree decl)
2702 struct named_label_entry *ent, dummy;
2703 bool saw_catch = false, identified = false;
2707 /* We can't know where a computed goto is jumping.
2708 So we assume that it's OK. */
2709 if (TREE_CODE (decl) != LABEL_DECL)
2712 /* We didn't record any information about this label when we created it,
2713 and there's not much point since it's trivial to analyze as a return. */
2714 if (decl == cdtor_label)
2717 dummy.label_decl = decl;
2718 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2719 gcc_assert (ent != NULL);
2721 /* If the label hasn't been defined yet, defer checking. */
2722 if (! DECL_INITIAL (decl))
2724 struct named_label_use_entry *new_use;
2726 /* Don't bother creating another use if the last goto had the
2727 same data, and will therefore create the same set of errors. */
2729 && ent->uses->names_in_scope == current_binding_level->names)
2732 new_use = ggc_alloc_named_label_use_entry ();
2733 new_use->binding_level = current_binding_level;
2734 new_use->names_in_scope = current_binding_level->names;
2735 new_use->o_goto_locus = input_location;
2736 new_use->in_omp_scope = false;
2738 new_use->next = ent->uses;
2739 ent->uses = new_use;
2743 if (ent->in_try_scope || ent->in_catch_scope
2744 || ent->in_omp_scope || !VEC_empty (tree, ent->bad_decls))
2746 permerror (input_location, "jump to label %q+D", decl);
2747 permerror (input_location, " from here");
2751 FOR_EACH_VEC_ELT (tree, ent->bad_decls, ix, bad)
2753 int u = decl_jump_unsafe (bad);
2755 if (u > 1 && DECL_ARTIFICIAL (bad))
2757 /* Can't skip init of __exception_info. */
2758 error_at (DECL_SOURCE_LOCATION (bad), " enters catch block");
2762 error (" skips initialization of %q+#D", bad);
2764 permerror (input_location, " enters scope of %q+#D which has "
2765 "non-trivial destructor", bad);
2768 if (ent->in_try_scope)
2769 error (" enters try block");
2770 else if (ent->in_catch_scope && !saw_catch)
2771 error (" enters catch block");
2773 if (ent->in_omp_scope)
2774 error (" enters OpenMP structured block");
2775 else if (flag_openmp)
2777 struct cp_binding_level *b;
2778 for (b = current_binding_level; b ; b = b->level_chain)
2780 if (b == ent->binding_level)
2782 if (b->kind == sk_omp)
2786 permerror (input_location, "jump to label %q+D", decl);
2787 permerror (input_location, " from here");
2790 error (" exits OpenMP structured block");
2797 /* Check that a return is ok wrt OpenMP structured blocks.
2798 Called by finish_return_stmt. Returns true if all is well. */
2801 check_omp_return (void)
2803 struct cp_binding_level *b;
2804 for (b = current_binding_level; b ; b = b->level_chain)
2805 if (b->kind == sk_omp)
2807 error ("invalid exit from OpenMP structured block");
2813 /* Define a label, specifying the location in the source file.
2814 Return the LABEL_DECL node for the label. */
2817 define_label (location_t location, tree name)
2819 struct named_label_entry *ent, dummy;
2820 struct cp_binding_level *p;
2823 timevar_push (TV_NAME_LOOKUP);
2825 decl = lookup_label (name);
2827 dummy.label_decl = decl;
2828 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2829 gcc_assert (ent != NULL);
2831 /* After labels, make any new cleanups in the function go into their
2832 own new (temporary) binding contour. */
2833 for (p = current_binding_level;
2834 p->kind != sk_function_parms;
2836 p->more_cleanups_ok = 0;
2838 if (name == get_identifier ("wchar_t"))
2839 permerror (input_location, "label named wchar_t");
2841 if (DECL_INITIAL (decl) != NULL_TREE)
2843 error ("duplicate label %qD", decl);
2844 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2848 struct named_label_use_entry *use;
2850 /* Mark label as having been defined. */
2851 DECL_INITIAL (decl) = error_mark_node;
2852 /* Say where in the source. */
2853 DECL_SOURCE_LOCATION (decl) = location;
2855 ent->binding_level = current_binding_level;
2856 ent->names_in_scope = current_binding_level->names;
2858 for (use = ent->uses; use ; use = use->next)
2859 check_previous_goto (decl, use);
2863 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2868 struct cp_binding_level *level;
2869 struct cp_switch *next;
2870 /* The SWITCH_STMT being built. */
2872 /* A splay-tree mapping the low element of a case range to the high
2873 element, or NULL_TREE if there is no high element. Used to
2874 determine whether or not a new case label duplicates an old case
2875 label. We need a tree, rather than simply a hash table, because
2876 of the GNU case range extension. */
2880 /* A stack of the currently active switch statements. The innermost
2881 switch statement is on the top of the stack. There is no need to
2882 mark the stack for garbage collection because it is only active
2883 during the processing of the body of a function, and we never
2884 collect at that point. */
2886 static struct cp_switch *switch_stack;
2888 /* Called right after a switch-statement condition is parsed.
2889 SWITCH_STMT is the switch statement being parsed. */
2892 push_switch (tree switch_stmt)
2894 struct cp_switch *p = XNEW (struct cp_switch);
2895 p->level = current_binding_level;
2896 p->next = switch_stack;
2897 p->switch_stmt = switch_stmt;
2898 p->cases = splay_tree_new (case_compare, NULL, NULL);
2905 struct cp_switch *cs = switch_stack;
2906 location_t switch_location;
2908 /* Emit warnings as needed. */
2909 switch_location = EXPR_LOC_OR_HERE (cs->switch_stmt);
2910 if (!processing_template_decl)
2911 c_do_switch_warnings (cs->cases, switch_location,
2912 SWITCH_STMT_TYPE (cs->switch_stmt),
2913 SWITCH_STMT_COND (cs->switch_stmt));
2915 splay_tree_delete (cs->cases);
2916 switch_stack = switch_stack->next;
2920 /* Note that we've seen a definition of a case label, and complain if this
2921 is a bad place for one. */
2924 finish_case_label (location_t loc, tree low_value, tree high_value)
2927 struct cp_binding_level *p;
2929 if (processing_template_decl)
2933 /* For templates, just add the case label; we'll do semantic
2934 analysis at instantiation-time. */
2935 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
2936 return add_stmt (build_case_label (loc, low_value, high_value, label));
2939 /* Find the condition on which this switch statement depends. */
2940 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2941 if (cond && TREE_CODE (cond) == TREE_LIST)
2942 cond = TREE_VALUE (cond);
2944 if (!check_switch_goto (switch_stack->level))
2945 return error_mark_node;
2948 low_value = cxx_constant_value (low_value);
2950 high_value = cxx_constant_value (high_value);
2952 r = c_add_case_label (loc, switch_stack->cases, cond,
2953 SWITCH_STMT_TYPE (switch_stack->switch_stmt),
2954 low_value, high_value);
2956 /* After labels, make any new cleanups in the function go into their
2957 own new (temporary) binding contour. */
2958 for (p = current_binding_level;
2959 p->kind != sk_function_parms;
2961 p->more_cleanups_ok = 0;
2966 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2969 typename_hash (const void* k)
2972 const_tree const t = (const_tree) k;
2974 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2975 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2980 typedef struct typename_info {
2988 /* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
2989 really of type `typename_info*' */
2992 typename_compare (const void * k1, const void * k2)
2994 const_tree const t1 = (const_tree) k1;
2995 const typename_info *const t2 = (const typename_info *) k2;
2997 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2998 && TYPE_CONTEXT (t1) == t2->scope
2999 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3000 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3001 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3004 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3005 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3007 Returns the new TYPENAME_TYPE. */
3009 static GTY ((param_is (union tree_node))) htab_t typename_htab;
3012 build_typename_type (tree context, tree name, tree fullname,
3013 enum tag_types tag_type)
3021 if (typename_htab == NULL)
3022 typename_htab = htab_create_ggc (61, &typename_hash,
3023 &typename_compare, NULL);
3025 ti.scope = FROB_CONTEXT (context);
3027 ti.template_id = fullname;
3028 ti.enum_p = tag_type == enum_type;
3029 ti.class_p = (tag_type == class_type
3030 || tag_type == record_type
3031 || tag_type == union_type);
3032 hash = (htab_hash_pointer (ti.scope)
3033 ^ htab_hash_pointer (ti.name));
3035 /* See if we already have this type. */
3036 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
3041 /* Build the TYPENAME_TYPE. */
3042 t = cxx_make_type (TYPENAME_TYPE);
3043 TYPE_CONTEXT (t) = ti.scope;
3044 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3045 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3046 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3048 /* Build the corresponding TYPE_DECL. */
3049 d = build_decl (input_location, TYPE_DECL, name, t);
3050 TYPE_NAME (TREE_TYPE (d)) = d;
3051 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3052 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3053 DECL_ARTIFICIAL (d) = 1;
3055 /* Store it in the hash table. */
3058 /* TYPENAME_TYPEs must always be compared structurally, because
3059 they may or may not resolve down to another type depending on
3060 the currently open classes. */
3061 SET_TYPE_STRUCTURAL_EQUALITY (t);
3067 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3068 provided to name the type. Returns an appropriate type, unless an
3069 error occurs, in which case error_mark_node is returned. If we
3070 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3071 return that, rather than the _TYPE it corresponds to, in other
3072 cases we look through the type decl. If TF_ERROR is set, complain
3073 about errors, otherwise be quiet. */
3076 make_typename_type (tree context, tree name, enum tag_types tag_type,
3077 tsubst_flags_t complain)
3083 if (name == error_mark_node
3084 || context == NULL_TREE
3085 || context == error_mark_node)
3086 return error_mark_node;
3090 if (!(TYPE_LANG_SPECIFIC (name)
3091 && (CLASSTYPE_IS_TEMPLATE (name)
3092 || CLASSTYPE_USE_TEMPLATE (name))))
3093 name = TYPE_IDENTIFIER (name);
3095 /* Create a TEMPLATE_ID_EXPR for the type. */
3096 name = build_nt (TEMPLATE_ID_EXPR,
3097 CLASSTYPE_TI_TEMPLATE (name),
3098 CLASSTYPE_TI_ARGS (name));
3100 else if (TREE_CODE (name) == TYPE_DECL)
3101 name = DECL_NAME (name);
3105 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3107 name = TREE_OPERAND (name, 0);
3108 if (TREE_CODE (name) == TEMPLATE_DECL)
3109 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3110 else if (TREE_CODE (name) == OVERLOAD)
3112 error ("%qD is not a type", name);
3113 return error_mark_node;
3116 if (TREE_CODE (name) == TEMPLATE_DECL)
3118 error ("%qD used without template parameters", name);
3119 return error_mark_node;
3121 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3122 gcc_assert (TYPE_P (context));
3124 if (!MAYBE_CLASS_TYPE_P (context))
3126 if (complain & tf_error)
3127 error ("%q#T is not a class", context);
3128 return error_mark_node;
3131 /* When the CONTEXT is a dependent type, NAME could refer to a
3132 dependent base class of CONTEXT. But look inside it anyway
3133 if CONTEXT is a currently open scope, in case it refers to a
3134 member of the current instantiation or a non-dependent base;
3135 lookup will stop when we hit a dependent base. */
3136 if (!dependent_scope_p (context))
3137 /* We should only set WANT_TYPE when we're a nested typename type.
3138 Then we can give better diagnostics if we find a non-type. */
3139 t = lookup_field (context, name, 2, /*want_type=*/true);
3143 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3144 return build_typename_type (context, name, fullname, tag_type);
3146 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3150 if (complain & tf_error)
3151 error (want_template ? "no class template named %q#T in %q#T"
3152 : "no type named %q#T in %q#T", name, context);
3153 return error_mark_node;
3156 /* Pull out the template from an injected-class-name (or multiple). */
3158 t = maybe_get_template_decl_from_type_decl (t);
3160 if (TREE_CODE (t) == TREE_LIST)
3162 if (complain & tf_error)
3164 error ("lookup of %qT in %qT is ambiguous", name, context);
3165 print_candidates (t);
3167 return error_mark_node;
3170 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
3172 if (complain & tf_error)
3173 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3175 return error_mark_node;
3177 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3179 if (complain & tf_error)
3180 error ("%<typename %T::%D%> names %q#T, which is not a type",
3182 return error_mark_node;
3185 if (complain & tf_error)
3186 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
3188 /* If we are currently parsing a template and if T is a typedef accessed
3189 through CONTEXT then we need to remember and check access of T at
3190 template instantiation time. */
3191 add_typedef_to_current_template_for_access_check (t, context, input_location);
3194 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3196 /*entering_scope=*/0,
3197 tf_warning_or_error | tf_user);
3199 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3205 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3206 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3207 in which case error_mark_node is returned.
3209 If PARM_LIST is non-NULL, also make sure that the template parameter
3210 list of TEMPLATE_DECL matches.
3212 If COMPLAIN zero, don't complain about any errors that occur. */
3215 make_unbound_class_template (tree context, tree name, tree parm_list,
3216 tsubst_flags_t complain)
3222 name = TYPE_IDENTIFIER (name);
3223 else if (DECL_P (name))
3224 name = DECL_NAME (name);
3225 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3227 if (!dependent_type_p (context)
3228 || currently_open_class (context))
3230 tree tmpl = NULL_TREE;
3232 if (MAYBE_CLASS_TYPE_P (context))
3233 tmpl = lookup_field (context, name, 0, false);
3235 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3236 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3238 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
3240 if (complain & tf_error)
3241 error ("no class template named %q#T in %q#T", name, context);
3242 return error_mark_node;
3246 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3248 if (complain & tf_error)
3250 error ("template parameters do not match template");
3251 error ("%q+D declared here", tmpl);
3253 return error_mark_node;
3256 if (complain & tf_error)
3257 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
3262 /* Build the UNBOUND_CLASS_TEMPLATE. */
3263 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3264 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3265 TREE_TYPE (t) = NULL_TREE;
3266 SET_TYPE_STRUCTURAL_EQUALITY (t);
3268 /* Build the corresponding TEMPLATE_DECL. */
3269 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3270 TYPE_NAME (TREE_TYPE (d)) = d;
3271 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3272 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3273 DECL_ARTIFICIAL (d) = 1;
3274 DECL_TEMPLATE_PARMS (d) = parm_list;
3281 /* Push the declarations of builtin types into the namespace.
3282 RID_INDEX is the index of the builtin type in the array
3283 RID_POINTERS. NAME is the name used when looking up the builtin
3284 type. TYPE is the _TYPE node for the builtin type. */
3287 record_builtin_type (enum rid rid_index,
3291 tree rname = NULL_TREE, tname = NULL_TREE;
3292 tree tdecl = NULL_TREE;
3294 if ((int) rid_index < (int) RID_MAX)
3295 rname = ridpointers[(int) rid_index];
3297 tname = get_identifier (name);
3299 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3300 eliminated. Built-in types should not be looked up name; their
3301 names are keywords that the parser can recognize. However, there
3302 is code in c-common.c that uses identifier_global_value to look
3303 up built-in types by name. */
3306 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3307 DECL_ARTIFICIAL (tdecl) = 1;
3308 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3314 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3315 DECL_ARTIFICIAL (tdecl) = 1;
3317 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3320 if (!TYPE_NAME (type))
3321 TYPE_NAME (type) = tdecl;
3324 debug_hooks->type_decl (tdecl, 0);
3327 /* Record one of the standard Java types.
3328 * Declare it as having the given NAME.
3329 * If SIZE > 0, it is the size of one of the integral types;
3330 * otherwise it is the negative of the size of one of the other types. */
3333 record_builtin_java_type (const char* name, int size)
3338 type = build_nonstandard_integer_type (size, 0);
3339 type = build_distinct_type_copy (type);
3341 else if (size > -32)
3344 /* "__java_char" or ""__java_boolean". */
3345 type = build_nonstandard_integer_type (-size, 1);
3346 type = build_distinct_type_copy (type);
3347 /* Get the signed type cached and attached to the unsigned type,
3348 so it doesn't get garbage-collected at "random" times,
3349 causing potential codegen differences out of different UIDs
3350 and different alias set numbers. */
3351 stype = build_nonstandard_integer_type (-size, 0);
3352 stype = build_distinct_type_copy (stype);
3353 TREE_CHAIN (type) = stype;
3354 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3357 { /* "__java_float" or ""__java_double". */
3358 type = make_node (REAL_TYPE);
3359 TYPE_PRECISION (type) = - size;
3362 record_builtin_type (RID_MAX, name, type);
3363 decl = TYPE_NAME (type);
3365 /* Suppress generate debug symbol entries for these types,
3366 since for normal C++ they are just clutter.
3367 However, push_lang_context undoes this if extern "Java" is seen. */
3368 DECL_IGNORED_P (decl) = 1;
3370 TYPE_FOR_JAVA (type) = 1;
3374 /* Push a type into the namespace so that the back ends ignore it. */
3377 record_unknown_type (tree type, const char* name)
3379 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3380 TYPE_DECL, get_identifier (name), type));
3381 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3382 DECL_IGNORED_P (decl) = 1;
3383 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3384 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3385 TYPE_ALIGN (type) = 1;
3386 TYPE_USER_ALIGN (type) = 0;
3387 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3390 /* A string for which we should create an IDENTIFIER_NODE at
3393 typedef struct predefined_identifier
3395 /* The name of the identifier. */
3396 const char *const name;
3397 /* The place where the IDENTIFIER_NODE should be stored. */
3399 /* Nonzero if this is the name of a constructor or destructor. */
3400 const int ctor_or_dtor_p;
3401 } predefined_identifier;
3403 /* Create all the predefined identifiers. */
3406 initialize_predefined_identifiers (void)
3408 const predefined_identifier *pid;
3410 /* A table of identifiers to create at startup. */
3411 static const predefined_identifier predefined_identifiers[] = {
3412 { "C++", &lang_name_cplusplus, 0 },
3413 { "C", &lang_name_c, 0 },
3414 { "Java", &lang_name_java, 0 },
3415 /* Some of these names have a trailing space so that it is
3416 impossible for them to conflict with names written by users. */
3417 { "__ct ", &ctor_identifier, 1 },
3418 { "__base_ctor ", &base_ctor_identifier, 1 },
3419 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3420 { "__dt ", &dtor_identifier, 1 },
3421 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3422 { "__base_dtor ", &base_dtor_identifier, 1 },
3423 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3424 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3425 { "nelts", &nelts_identifier, 0 },
3426 { THIS_NAME, &this_identifier, 0 },
3427 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3428 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3429 { "_vptr", &vptr_identifier, 0 },
3430 { "__vtt_parm", &vtt_parm_identifier, 0 },
3431 { "::", &global_scope_name, 0 },
3432 { "std", &std_identifier, 0 },
3436 for (pid = predefined_identifiers; pid->name; ++pid)
3438 *pid->node = get_identifier (pid->name);
3439 if (pid->ctor_or_dtor_p)
3440 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3444 /* Create the predefined scalar types of C,
3445 and some nodes representing standard constants (0, 1, (void *)0).
3446 Initialize the global binding level.
3447 Make definitions for built-in primitive functions. */
3450 cxx_init_decl_processing (void)
3453 tree void_ftype_ptr;
3455 build_common_tree_nodes (flag_signed_char);
3457 /* Create all the identifiers we need. */
3458 initialize_predefined_identifiers ();
3460 /* Create the global variables. */
3461 push_to_top_level ();
3463 current_function_decl = NULL_TREE;
3464 current_binding_level = NULL;
3465 /* Enter the global namespace. */
3466 gcc_assert (global_namespace == NULL_TREE);
3467 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3469 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
3470 TREE_PUBLIC (global_namespace) = 1;
3471 begin_scope (sk_namespace, global_namespace);
3473 current_lang_name = NULL_TREE;
3475 if (flag_visibility_ms_compat)
3476 default_visibility = VISIBILITY_HIDDEN;
3479 current_lang_name = lang_name_c;
3481 /* Create the `std' namespace. */
3482 push_namespace (std_identifier);
3483 std_node = current_namespace;
3486 c_common_nodes_and_builtins ();
3488 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3489 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3490 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3491 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3492 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3493 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3494 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3495 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3497 integer_two_node = build_int_cst (NULL_TREE, 2);
3499 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3500 truthvalue_type_node = boolean_type_node;
3501 truthvalue_false_node = boolean_false_node;
3502 truthvalue_true_node = boolean_true_node;
3504 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3505 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
3506 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
3509 record_builtin_type (RID_MAX, NULL, string_type_node);
3512 delta_type_node = ptrdiff_type_node;
3513 vtable_index_type = ptrdiff_type_node;
3515 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3516 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
3517 void_ftype_ptr = build_function_type_list (void_type_node,
3518 ptr_type_node, NULL_TREE);
3520 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3522 /* C++ extensions */
3524 unknown_type_node = make_node (LANG_TYPE);
3525 record_unknown_type (unknown_type_node, "unknown type");
3527 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3528 TREE_TYPE (unknown_type_node) = unknown_type_node;
3530 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3532 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3533 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3535 init_list_type_node = make_node (LANG_TYPE);
3536 record_unknown_type (init_list_type_node, "init list");
3539 /* Make sure we get a unique function type, so we can give
3540 its pointer type a name. (This wins for gdb.) */
3541 tree vfunc_type = make_node (FUNCTION_TYPE);
3542 TREE_TYPE (vfunc_type) = integer_type_node;
3543 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3544 layout_type (vfunc_type);
3546 vtable_entry_type = build_pointer_type (vfunc_type);
3548 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3551 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3552 layout_type (vtbl_type_node);
3553 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3554 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3555 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3556 layout_type (vtbl_ptr_type_node);
3557 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3559 push_namespace (get_identifier ("__cxxabiv1"));
3560 abi_node = current_namespace;
3563 global_type_node = make_node (LANG_TYPE);
3564 record_unknown_type (global_type_node, "global type");
3567 current_lang_name = lang_name_cplusplus;
3570 tree newtype, deltype;
3571 tree ptr_ftype_sizetype;
3575 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
3576 if (cxx_dialect == cxx98)
3579 tree bad_alloc_type_node;
3580 tree bad_alloc_decl;
3582 push_namespace (std_identifier);
3583 bad_alloc_id = get_identifier ("bad_alloc");
3584 bad_alloc_type_node = make_class_type (RECORD_TYPE);
3585 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3587 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3588 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3592 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
3595 new_eh_spec = noexcept_false_spec;
3597 newtype = build_exception_variant (ptr_ftype_sizetype, new_eh_spec);
3598 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3599 push_cp_library_fn (NEW_EXPR, newtype);
3600 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3601 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3602 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3604 nullptr_type_node = make_node (NULLPTR_TYPE);
3605 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
3606 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
3607 TYPE_UNSIGNED (nullptr_type_node) = 1;
3608 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
3609 SET_TYPE_MODE (nullptr_type_node, Pmode);
3610 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
3611 nullptr_node = build_int_cst (nullptr_type_node, 0);
3615 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3617 /* Perform other language dependent initializations. */
3618 init_class_processing ();
3619 init_rtti_processing ();
3620 init_template_processing ();
3622 if (flag_exceptions)
3623 init_exception_processing ();
3625 if (! supports_one_only ())
3628 make_fname_decl = cp_make_fname_decl;
3629 start_fname_decls ();
3631 /* Show we use EH for cleanups. */
3632 if (flag_exceptions)
3633 using_eh_for_cleanups ();
3636 /* Generate an initializer for a function naming variable from
3637 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3638 filled in with the type of the init. */
3641 cp_fname_init (const char* name, tree *type_p)
3643 tree domain = NULL_TREE;
3645 tree init = NULL_TREE;
3650 length = strlen (name);
3651 domain = build_index_type (size_int (length));
3652 init = build_string (length + 1, name);
3655 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3656 type = build_cplus_array_type (type, domain);
3661 TREE_TYPE (init) = type;
3663 init = error_mark_node;
3668 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
3669 the decl, LOC is the location to give the decl, NAME is the
3670 initialization string and TYPE_DEP indicates whether NAME depended
3671 on the type of the function. We make use of that to detect
3672 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
3673 at the point of first use, so we mustn't push the decl now. */
3676 cp_make_fname_decl (location_t loc, tree id, int type_dep)
3678 const char *const name = (type_dep && processing_template_decl
3679 ? NULL : fname_as_string (type_dep));
3681 tree init = cp_fname_init (name, &type);
3682 tree decl = build_decl (loc, VAR_DECL, id, type);
3685 free (CONST_CAST (char *, name));
3687 /* As we're using pushdecl_with_scope, we must set the context. */
3688 DECL_CONTEXT (decl) = current_function_decl;
3689 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3691 TREE_STATIC (decl) = 1;
3692 TREE_READONLY (decl) = 1;
3693 DECL_ARTIFICIAL (decl) = 1;
3695 TREE_USED (decl) = 1;
3697 if (current_function_decl)
3699 struct cp_binding_level *b = current_binding_level;
3700 if (b->kind == sk_function_parms)
3701 return error_mark_node;
3702 while (b->level_chain->kind != sk_function_parms)
3704 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3705 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3706 LOOKUP_ONLYCONVERTING);
3709 pushdecl_top_level_and_finish (decl, init);
3715 builtin_function_1 (tree decl, tree context, bool is_global)
3717 tree id = DECL_NAME (decl);
3718 const char *name = IDENTIFIER_POINTER (id);
3720 retrofit_lang_decl (decl);
3722 DECL_ARTIFICIAL (decl) = 1;
3723 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
3724 SET_DECL_LANGUAGE (decl, lang_c);
3725 /* Runtime library routines are, by definition, available in an
3726 external shared object. */
3727 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
3728 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3730 DECL_CONTEXT (decl) = context;
3733 pushdecl_top_level (decl);
3737 /* A function in the user's namespace should have an explicit
3738 declaration before it is used. Mark the built-in function as
3739 anticipated but not actually declared. */
3740 if (name[0] != '_' || name[1] != '_')
3741 DECL_ANTICIPATED (decl) = 1;
3742 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
3744 size_t len = strlen (name);
3746 /* Treat __*_chk fortification functions as anticipated as well,
3747 unless they are __builtin_*. */
3748 if (len > strlen ("___chk")
3749 && memcmp (name + len - strlen ("_chk"),
3750 "_chk", strlen ("_chk") + 1) == 0)
3751 DECL_ANTICIPATED (decl) = 1;
3758 cxx_builtin_function (tree decl)
3760 tree id = DECL_NAME (decl);
3761 const char *name = IDENTIFIER_POINTER (id);
3762 /* All builtins that don't begin with an '_' should additionally
3763 go in the 'std' namespace. */
3766 tree decl2 = copy_node(decl);
3767 push_namespace (std_identifier);
3768 builtin_function_1 (decl2, std_node, false);
3772 return builtin_function_1 (decl, NULL_TREE, false);
3775 /* Like cxx_builtin_function, but guarantee the function is added to the global
3776 scope. This is to allow function specific options to add new machine
3777 dependent builtins when the target ISA changes via attribute((target(...)))
3778 which saves space on program startup if the program does not use non-generic
3782 cxx_builtin_function_ext_scope (tree decl)
3785 tree id = DECL_NAME (decl);
3786 const char *name = IDENTIFIER_POINTER (id);
3787 /* All builtins that don't begin with an '_' should additionally
3788 go in the 'std' namespace. */
3791 tree decl2 = copy_node(decl);
3792 push_namespace (std_identifier);
3793 builtin_function_1 (decl2, std_node, true);
3797 return builtin_function_1 (decl, NULL_TREE, true);
3800 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3801 function. Not called directly. */
3804 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3806 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3807 DECL_EXTERNAL (fn) = 1;
3808 TREE_PUBLIC (fn) = 1;
3809 DECL_ARTIFICIAL (fn) = 1;
3810 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3811 SET_DECL_LANGUAGE (fn, lang_c);
3812 /* Runtime library routines are, by definition, available in an
3813 external shared object. */
3814 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3815 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3819 /* Returns the _DECL for a library function with C linkage.
3820 We assume that such functions never throw; if this is incorrect,
3821 callers should unset TREE_NOTHROW. */
3824 build_library_fn (tree name, tree type)
3826 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3827 TREE_NOTHROW (fn) = 1;
3831 /* Returns the _DECL for a library function with C++ linkage. */
3834 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3836 tree fn = build_library_fn_1 (name, operator_code, type);
3837 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3838 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3839 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3843 /* Like build_library_fn, but takes a C string instead of an
3847 build_library_fn_ptr (const char* name, tree type)
3849 return build_library_fn (get_identifier (name), type);
3852 /* Like build_cp_library_fn, but takes a C string instead of an
3856 build_cp_library_fn_ptr (const char* name, tree type)
3858 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3861 /* Like build_library_fn, but also pushes the function so that we will
3862 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
3863 may throw exceptions listed in RAISES. */
3866 push_library_fn (tree name, tree type, tree raises)
3871 type = build_exception_variant (type, raises);
3873 fn = build_library_fn (name, type);
3874 pushdecl_top_level (fn);
3878 /* Like build_cp_library_fn, but also pushes the function so that it
3879 will be found by normal lookup. */
3882 push_cp_library_fn (enum tree_code operator_code, tree type)
3884 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3891 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3895 push_void_library_fn (tree name, tree parmtypes)
3897 tree type = build_function_type (void_type_node, parmtypes);
3898 return push_library_fn (name, type, NULL_TREE);
3901 /* Like push_library_fn, but also note that this function throws
3902 and does not return. Used for __throw_foo and the like. */
3905 push_throw_library_fn (tree name, tree type)
3907 tree fn = push_library_fn (name, type, NULL_TREE);
3908 TREE_THIS_VOLATILE (fn) = 1;
3909 TREE_NOTHROW (fn) = 0;
3913 /* When we call finish_struct for an anonymous union, we create
3914 default copy constructors and such. But, an anonymous union
3915 shouldn't have such things; this function undoes the damage to the
3916 anonymous union type T.
3918 (The reason that we create the synthesized methods is that we don't
3919 distinguish `union { int i; }' from `typedef union { int i; } U'.
3920 The first is an anonymous union; the second is just an ordinary
3924 fixup_anonymous_aggr (tree t)
3928 /* Wipe out memory of synthesized methods. */
3929 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
3930 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3931 TYPE_HAS_COPY_CTOR (t) = 0;
3932 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
3933 TYPE_HAS_COPY_ASSIGN (t) = 0;
3934 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
3936 /* Splice the implicitly generated functions out of the TYPE_METHODS
3938 q = &TYPE_METHODS (t);
3941 if (DECL_ARTIFICIAL (*q))
3942 *q = TREE_CHAIN (*q);
3944 q = &DECL_CHAIN (*q);
3947 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3948 if (TYPE_METHODS (t))
3950 tree decl = TYPE_MAIN_DECL (t);
3952 if (TREE_CODE (t) != UNION_TYPE)
3953 error_at (DECL_SOURCE_LOCATION (decl),
3954 "an anonymous struct cannot have function members");
3956 error_at (DECL_SOURCE_LOCATION (decl),
3957 "an anonymous union cannot have function members");
3960 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3961 assignment operators (because they cannot have these methods themselves).
3962 For anonymous unions this is already checked because they are not allowed
3963 in any union, otherwise we have to check it. */
3964 if (TREE_CODE (t) != UNION_TYPE)
3968 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3969 if (TREE_CODE (field) == FIELD_DECL)
3971 type = TREE_TYPE (field);
3972 if (CLASS_TYPE_P (type))
3974 if (TYPE_NEEDS_CONSTRUCTING (type))
3975 error ("member %q+#D with constructor not allowed "
3976 "in anonymous aggregate", field);
3977 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3978 error ("member %q+#D with destructor not allowed "
3979 "in anonymous aggregate", field);
3980 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3981 error ("member %q+#D with copy assignment operator "
3982 "not allowed in anonymous aggregate", field);
3988 /* Make sure that a declaration with no declarator is well-formed, i.e.
3989 just declares a tagged type or anonymous union.
3991 Returns the type declared; or NULL_TREE if none. */
3994 check_tag_decl (cp_decl_specifier_seq *declspecs)
3996 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3997 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3998 /* If a class, struct, or enum type is declared by the DECLSPECS
3999 (i.e, if a class-specifier, enum-specifier, or non-typename
4000 elaborated-type-specifier appears in the DECLSPECS),
4001 DECLARED_TYPE is set to the corresponding type. */
4002 tree declared_type = NULL_TREE;
4003 bool error_p = false;
4005 if (declspecs->multiple_types_p)
4006 error ("multiple types in one declaration");
4007 else if (declspecs->redefined_builtin_type)
4009 if (!in_system_header)
4010 permerror (input_location, "redeclaration of C++ built-in type %qT",
4011 declspecs->redefined_builtin_type);
4016 && TYPE_P (declspecs->type)
4017 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4018 && MAYBE_CLASS_TYPE_P (declspecs->type))
4019 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4020 declared_type = declspecs->type;
4021 else if (declspecs->type == error_mark_node)
4023 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4024 permerror (input_location, "declaration does not declare anything");
4025 /* Check for an anonymous union. */
4026 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4027 && TYPE_ANONYMOUS_P (declared_type))
4029 /* 7/3 In a simple-declaration, the optional init-declarator-list
4030 can be omitted only when declaring a class (clause 9) or
4031 enumeration (7.2), that is, when the decl-specifier-seq contains
4032 either a class-specifier, an elaborated-type-specifier with
4033 a class-key (9.1), or an enum-specifier. In these cases and
4034 whenever a class-specifier or enum-specifier is present in the
4035 decl-specifier-seq, the identifiers in these specifiers are among
4036 the names being declared by the declaration (as class-name,
4037 enum-names, or enumerators, depending on the syntax). In such
4038 cases, and except for the declaration of an unnamed bit-field (9.6),
4039 the decl-specifier-seq shall introduce one or more names into the
4040 program, or shall redeclare a name introduced by a previous
4041 declaration. [Example:
4042 enum { }; // ill-formed
4043 typedef class { }; // ill-formed
4047 error ("missing type-name in typedef-declaration");
4050 /* Anonymous unions are objects, so they can have specifiers. */;
4051 SET_ANON_AGGR_TYPE_P (declared_type);
4053 if (TREE_CODE (declared_type) != UNION_TYPE && !in_system_header)
4054 pedwarn (input_location, OPT_pedantic, "ISO C++ prohibits anonymous structs");
4059 if (declspecs->specs[(int)ds_inline]
4060 || declspecs->specs[(int)ds_virtual])
4061 error ("%qs can only be specified for functions",
4062 declspecs->specs[(int)ds_inline]
4063 ? "inline" : "virtual");
4065 && (!current_class_type
4066 || current_scope () != current_class_type))
4067 error ("%<friend%> can only be specified inside a class");
4068 else if (declspecs->specs[(int)ds_explicit])
4069 error ("%<explicit%> can only be specified for constructors");
4070 else if (declspecs->storage_class)
4071 error ("a storage class can only be specified for objects "
4073 else if (declspecs->specs[(int)ds_const]
4074 || declspecs->specs[(int)ds_volatile]
4075 || declspecs->specs[(int)ds_restrict]
4076 || declspecs->specs[(int)ds_thread])
4077 error ("qualifiers can only be specified for objects "
4079 else if (saw_typedef)
4080 warning (0, "%<typedef%> was ignored in this declaration");
4081 else if (declspecs->specs[(int) ds_constexpr])
4082 error ("%<constexpr%> cannot be used for type declarations");
4085 return declared_type;
4088 /* Called when a declaration is seen that contains no names to declare.
4089 If its type is a reference to a structure, union or enum inherited
4090 from a containing scope, shadow that tag name for the current scope
4091 with a forward reference.
4092 If its type defines a new named structure or union
4093 or defines an enum, it is valid but we need not do anything here.
4094 Otherwise, it is an error.
4096 C++: may have to grok the declspecs to learn about static,
4097 complain for anonymous unions.
4099 Returns the TYPE declared -- or NULL_TREE if none. */
4102 shadow_tag (cp_decl_specifier_seq *declspecs)
4104 tree t = check_tag_decl (declspecs);
4109 if (declspecs->attributes)
4111 warning (0, "attribute ignored in declaration of %q+#T", t);
4112 warning (0, "attribute for %q+#T must follow the %qs keyword",
4113 t, class_key_or_enum_as_string (t));
4117 if (maybe_process_partial_specialization (t) == error_mark_node)
4120 /* This is where the variables in an anonymous union are
4121 declared. An anonymous union declaration looks like: