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 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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"
40 #include "tree-inline.h"
50 #include "diagnostic.h"
53 #include "tree-flow.h"
55 static tree grokparms (cp_parameter_declarator *, tree *);
56 static const char *redeclaration_error_message (tree, tree);
58 static int decl_jump_unsafe (tree);
59 static void require_complete_types_for_parms (tree);
60 static int ambi_op_p (enum tree_code);
61 static int unary_op_p (enum tree_code);
62 static void push_local_name (tree);
63 static tree grok_reference_init (tree, tree, tree, tree *);
64 static tree grokfndecl (tree, tree, tree, tree, tree, int,
65 enum overload_flags, cp_cv_quals,
66 tree, int, int, int, int, int, int, tree,
68 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
70 static void record_unknown_type (tree, const char *);
71 static tree builtin_function_1 (const char *, tree, tree,
72 enum built_in_function code,
73 enum built_in_class cl, const char *,
75 static tree build_library_fn_1 (tree, enum tree_code, tree);
76 static int member_function_or_else (tree, tree, enum overload_flags);
77 static void bad_specifiers (tree, const char *, int, int, int, int,
79 static void check_for_uninitialized_const_var (tree);
80 static hashval_t typename_hash (const void *);
81 static int typename_compare (const void *, const void *);
82 static tree local_variable_p_walkfn (tree *, int *, void *);
83 static tree record_builtin_java_type (const char *, int);
84 static const char *tag_name (enum tag_types);
85 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
86 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
87 static int walk_globals_r (tree, void*);
88 static int walk_vtables_r (tree, void*);
89 static tree make_label_decl (tree, int);
90 static void use_label (tree);
91 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
93 static void check_previous_goto (struct named_label_use_list *);
94 static void check_switch_goto (struct cp_binding_level *);
95 static void check_previous_gotos (tree);
96 static void pop_label (tree, tree);
97 static void pop_labels (tree);
98 static void maybe_deduce_size_from_array_init (tree, tree);
99 static void layout_var_decl (tree);
100 static void maybe_commonize_var (tree);
101 static tree check_initializer (tree, tree, int, tree *);
102 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
103 static void save_function_data (tree);
104 static void check_function_type (tree, tree);
105 static void finish_constructor_body (void);
106 static void begin_destructor_body (void);
107 static void finish_destructor_body (void);
108 static tree create_array_type_for_decl (tree, tree, tree);
109 static tree get_atexit_node (void);
110 static tree get_dso_handle_node (void);
111 static tree start_cleanup_fn (void);
112 static void end_cleanup_fn (void);
113 static tree cp_make_fname_decl (tree, int);
114 static void initialize_predefined_identifiers (void);
115 static tree check_special_function_return_type
116 (special_function_kind, tree, tree);
117 static tree push_cp_library_fn (enum tree_code, tree);
118 static tree build_cp_library_fn (tree, enum tree_code, tree);
119 static void store_parm_decls (tree);
120 static void initialize_local_var (tree, tree);
121 static void expand_static_init (tree, tree);
122 static tree next_initializable_field (tree);
123 static tree reshape_init (tree, tree *);
125 /* Erroneous argument lists can use this *IFF* they do not modify it. */
126 tree error_mark_list;
128 /* The following symbols are subsumed in the cp_global_trees array, and
129 listed here individually for documentation purposes.
132 tree wchar_decl_node;
134 tree vtable_entry_type;
135 tree delta_type_node;
136 tree __t_desc_type_node;
137 tree ti_desc_type_node;
138 tree bltn_desc_type_node, ptr_desc_type_node;
139 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
140 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
141 tree ptm_desc_type_node;
142 tree base_desc_type_node;
144 tree class_type_node;
145 tree unknown_type_node;
147 Array type `vtable_entry_type[]'
150 tree vtbl_ptr_type_node;
157 A FUNCTION_DECL which can call `abort'. Not necessarily the
158 one that the user will declare, but sufficient to be called
159 by routines that want to abort the program.
163 The FUNCTION_DECL for the default `::operator delete'.
165 tree global_delete_fndecl;
168 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
173 tree cp_global_trees[CPTI_MAX];
175 /* Indicates that there is a type value in some namespace, although
176 that is not necessarily in scope at the moment. */
178 tree global_type_node;
180 /* The node that holds the "name" of the global scope. */
181 tree global_scope_name;
183 /* Used only for jumps to as-yet undefined labels, since jumps to
184 defined labels can have their validity checked immediately. */
186 struct named_label_use_list GTY(())
188 struct cp_binding_level *binding_level;
191 location_t o_goto_locus;
192 struct named_label_use_list *next;
195 #define named_label_uses cp_function_chain->x_named_label_uses
197 #define local_names cp_function_chain->x_local_names
199 /* A list of objects which have constructors or destructors
200 which reside in the global scope. The decl is stored in
201 the TREE_VALUE slot and the initializer is stored
202 in the TREE_PURPOSE slot. */
203 tree static_aggregates;
207 /* A node for the integer constants 2, and 3. */
209 tree integer_two_node, integer_three_node;
211 /* A list of all LABEL_DECLs in the function that have names. Here so
212 we can clear out their names' definitions at the end of the
213 function, and so we can check the validity of jumps to these labels. */
215 struct named_label_list GTY(())
217 struct cp_binding_level *binding_level;
222 struct named_label_list *next;
223 unsigned int in_try_scope : 1;
224 unsigned int in_catch_scope : 1;
227 #define named_labels cp_function_chain->x_named_labels
229 /* The number of function bodies which we are currently processing.
230 (Zero if we are at namespace scope, one inside the body of a
231 function, two inside the body of a function in a local class, etc.) */
234 /* States indicating how grokdeclarator() should handle declspecs marked
235 with __attribute__((deprecated)). An object declared as
236 __attribute__((deprecated)) suppresses warnings of uses of other
239 enum deprecated_states {
244 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
246 /* True if a declaration with an `extern' linkage specifier is being
248 bool have_extern_spec;
251 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
252 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
253 time the VAR_DECL was declared, the type was incomplete. */
255 static GTY(()) tree incomplete_vars;
257 /* Returns the kind of template specialization we are currently
258 processing, given that it's declaration contained N_CLASS_SCOPES
259 explicit scope qualifications. */
262 current_tmpl_spec_kind (int n_class_scopes)
264 int n_template_parm_scopes = 0;
265 int seen_specialization_p = 0;
266 int innermost_specialization_p = 0;
267 struct cp_binding_level *b;
269 /* Scan through the template parameter scopes. */
270 for (b = current_binding_level;
271 b->kind == sk_template_parms;
274 /* If we see a specialization scope inside a parameter scope,
275 then something is wrong. That corresponds to a declaration
278 template <class T> template <> ...
280 which is always invalid since [temp.expl.spec] forbids the
281 specialization of a class member template if the enclosing
282 class templates are not explicitly specialized as well. */
283 if (b->explicit_spec_p)
285 if (n_template_parm_scopes == 0)
286 innermost_specialization_p = 1;
288 seen_specialization_p = 1;
290 else if (seen_specialization_p == 1)
291 return tsk_invalid_member_spec;
293 ++n_template_parm_scopes;
296 /* Handle explicit instantiations. */
297 if (processing_explicit_instantiation)
299 if (n_template_parm_scopes != 0)
300 /* We've seen a template parameter list during an explicit
301 instantiation. For example:
303 template <class T> template void f(int);
305 This is erroneous. */
306 return tsk_invalid_expl_inst;
308 return tsk_expl_inst;
311 if (n_template_parm_scopes < n_class_scopes)
312 /* We've not seen enough template headers to match all the
313 specialized classes present. For example:
315 template <class T> void R<T>::S<T>::f(int);
317 This is invalid; there needs to be one set of template
318 parameters for each class. */
319 return tsk_insufficient_parms;
320 else if (n_template_parm_scopes == n_class_scopes)
321 /* We're processing a non-template declaration (even though it may
322 be a member of a template class.) For example:
324 template <class T> void S<T>::f(int);
326 The `class T' maches the `S<T>', leaving no template headers
327 corresponding to the `f'. */
329 else if (n_template_parm_scopes > n_class_scopes + 1)
330 /* We've got too many template headers. For example:
332 template <> template <class T> void f (T);
334 There need to be more enclosing classes. */
335 return tsk_excessive_parms;
337 /* This must be a template. It's of the form:
339 template <class T> template <class U> void S<T>::f(U);
341 This is a specialization if the innermost level was a
342 specialization; otherwise it's just a definition of the
344 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
347 /* Exit the current scope. */
355 /* When a label goes out of scope, check to see if that label was used
356 in a valid manner, and issue any appropriate warnings or errors. */
359 pop_label (tree label, tree old_value)
361 if (!processing_template_decl)
363 if (DECL_INITIAL (label) == NULL_TREE)
367 cp_error_at ("label %qD used but not defined", label);
368 #ifdef USE_MAPPED_LOCATION
369 location = input_location; /* FIXME want (input_filename, (line)0) */
371 location.file = input_filename;
374 /* Avoid crashing later. */
375 define_label (location, DECL_NAME (label));
377 else if (warn_unused_label && !TREE_USED (label))
378 cp_warning_at ("label %qD defined but not used", 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 (tree block)
391 struct named_label_list *link;
393 /* Clear out the definitions of all label names, since their scopes
395 for (link = named_labels; link; link = link->next)
397 pop_label (link->label_decl, link->old_value);
398 /* Put the labels into the "variables" of the top-level block,
399 so debugger can see them. */
400 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
401 BLOCK_VARS (block) = link->label_decl;
407 /* The following two routines are used to interface to Objective-C++.
408 The binding level is purposely treated as an opaque type. */
411 objc_get_current_scope (void)
413 return current_binding_level;
416 /* The following routine is used by the NeXT-style SJLJ exceptions;
417 variables get marked 'volatile' so as to not be clobbered by
418 _setjmp()/_longjmp() calls. All variables in the current scope,
419 as well as parent scopes up to (but not including) ENCLOSING_BLK
420 shall be thusly marked. */
423 objc_mark_locals_volatile (void *enclosing_blk)
425 struct cp_binding_level *scope;
427 for (scope = current_binding_level;
428 scope && scope != enclosing_blk && scope->kind == sk_block;
429 scope = scope->level_chain)
433 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
435 if (TREE_CODE (decl) == VAR_DECL)
437 DECL_REGISTER (decl) = 0;
438 TREE_THIS_VOLATILE (decl) = 1;
444 /* Exit a binding level.
445 Pop the level off, and restore the state of the identifier-decl mappings
446 that were in effect when this level was entered.
448 If KEEP == 1, this level had explicit declarations, so
449 and create a "block" (a BLOCK node) for the level
450 to record its declarations and subblocks for symbol table output.
452 If FUNCTIONBODY is nonzero, this level is the body of a function,
453 so create a block as if KEEP were set and also clear out all
456 If REVERSE is nonzero, reverse the order of decls before putting
457 them into the BLOCK. */
460 poplevel (int keep, int reverse, int functionbody)
463 /* The chain of decls was accumulated in reverse order.
464 Put it into forward order, just for cleanliness. */
466 int tmp = functionbody;
467 int real_functionbody;
471 int leaving_for_scope;
474 timevar_push (TV_NAME_LOOKUP);
479 gcc_assert (current_binding_level->kind != sk_class);
481 real_functionbody = (current_binding_level->kind == sk_cleanup
482 ? ((functionbody = 0), tmp) : functionbody);
483 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
485 gcc_assert (!VEC_length(cp_class_binding,
486 current_binding_level->class_shadowed));
488 /* We used to use KEEP == 2 to indicate that the new block should go
489 at the beginning of the list of blocks at this binding level,
490 rather than the end. This hack is no longer used. */
491 gcc_assert (keep == 0 || keep == 1);
493 if (current_binding_level->keep)
496 /* Any uses of undefined labels, and any defined labels, now operate
497 under constraints of next binding contour. */
498 if (cfun && !functionbody)
500 struct cp_binding_level *level_chain;
501 level_chain = current_binding_level->level_chain;
504 struct named_label_use_list *uses;
505 struct named_label_list *labels;
506 for (labels = named_labels; labels; labels = labels->next)
507 if (labels->binding_level == current_binding_level)
510 if (current_binding_level->kind == sk_try)
511 labels->in_try_scope = 1;
512 if (current_binding_level->kind == sk_catch)
513 labels->in_catch_scope = 1;
514 for (decl = labels->names_in_scope; decl;
515 decl = TREE_CHAIN (decl))
516 if (decl_jump_unsafe (decl))
517 labels->bad_decls = tree_cons (NULL_TREE, decl,
519 labels->binding_level = level_chain;
520 labels->names_in_scope = level_chain->names;
523 for (uses = named_label_uses; uses; uses = uses->next)
524 if (uses->binding_level == current_binding_level)
526 uses->binding_level = level_chain;
527 uses->names_in_scope = level_chain->names;
532 /* Get the decls in the order they were written.
533 Usually current_binding_level->names is in reverse order.
534 But parameter decls were previously put in forward order. */
537 current_binding_level->names
538 = decls = nreverse (current_binding_level->names);
540 decls = current_binding_level->names;
542 /* If there were any declarations or structure tags in that level,
543 or if this level is a function body,
544 create a BLOCK to record them for the life of this function. */
546 if (keep == 1 || functionbody)
547 block = make_node (BLOCK);
548 if (block != NULL_TREE)
550 BLOCK_VARS (block) = decls;
551 BLOCK_SUBBLOCKS (block) = subblocks;
554 /* In each subblock, record that this is its superior. */
556 for (link = subblocks; link; link = TREE_CHAIN (link))
557 BLOCK_SUPERCONTEXT (link) = block;
559 /* We still support the old for-scope rules, whereby the variables
560 in a for-init statement were in scope after the for-statement
561 ended. We only use the new rules if flag_new_for_scope is
564 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
566 /* Before we remove the declarations first check for unused variables. */
567 if (warn_unused_variable
568 && !processing_template_decl)
569 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
570 if (TREE_CODE (decl) == VAR_DECL
571 && ! TREE_USED (decl)
572 && ! DECL_IN_SYSTEM_HEADER (decl)
573 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
574 warning ("%Junused variable %qD", decl, decl);
576 /* Remove declarations for all the DECLs in this level. */
577 for (link = decls; link; link = TREE_CHAIN (link))
579 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
582 tree name = DECL_NAME (link);
586 ob = outer_binding (name,
587 IDENTIFIER_BINDING (name),
590 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
592 ns_binding = NULL_TREE;
594 if (ob && ob->scope == current_binding_level->level_chain)
595 /* We have something like:
600 and we are leaving the `for' scope. There's no reason to
601 keep the binding of the inner `i' in this case. */
602 pop_binding (name, link);
603 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
604 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
605 /* Here, we have something like:
613 We must pop the for-scope binding so we know what's a
614 type and what isn't. */
615 pop_binding (name, link);
618 /* Mark this VAR_DECL as dead so that we can tell we left it
619 there only for backward compatibility. */
620 DECL_DEAD_FOR_LOCAL (link) = 1;
622 /* Keep track of what should have happened when we
623 popped the binding. */
625 DECL_SHADOWED_FOR_VAR (link) = ob->value;
627 /* Add it to the list of dead variables in the next
628 outermost binding to that we can remove these when we
629 leave that binding. */
630 current_binding_level->level_chain->dead_vars_from_for
631 = tree_cons (NULL_TREE, link,
632 current_binding_level->level_chain->
635 /* Although we don't pop the cxx_binding, we do clear
636 its SCOPE since the scope is going away now. */
637 IDENTIFIER_BINDING (name)->scope
638 = current_binding_level->level_chain;
645 /* Remove the binding. */
648 if (TREE_CODE (decl) == TREE_LIST)
649 decl = TREE_VALUE (decl);
652 if (TREE_CODE (name) == OVERLOAD)
653 name = OVL_FUNCTION (name);
655 gcc_assert (DECL_P (name));
656 pop_binding (DECL_NAME (name), decl);
660 /* Remove declarations for any `for' variables from inner scopes
661 that we kept around. */
662 for (link = current_binding_level->dead_vars_from_for;
663 link; link = TREE_CHAIN (link))
664 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
666 /* Restore the IDENTIFIER_TYPE_VALUEs. */
667 for (link = current_binding_level->type_shadowed;
668 link; link = TREE_CHAIN (link))
669 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
671 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
672 for (link = current_binding_level->shadowed_labels;
674 link = TREE_CHAIN (link))
675 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
677 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
678 list if a `using' declaration put them there. The debugging
679 back-ends won't understand OVERLOAD, so we remove them here.
680 Because the BLOCK_VARS are (temporarily) shared with
681 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
682 popped all the bindings. */
687 for (d = &BLOCK_VARS (block); *d; )
689 if (TREE_CODE (*d) == TREE_LIST)
690 *d = TREE_CHAIN (*d);
692 d = &TREE_CHAIN (*d);
696 /* If the level being exited is the top level of a function,
697 check over all the labels. */
700 /* Since this is the top level block of a function, the vars are
701 the function's parameters. Don't leave them in the BLOCK
702 because they are found in the FUNCTION_DECL instead. */
703 BLOCK_VARS (block) = 0;
707 kind = current_binding_level->kind;
708 if (kind == sk_cleanup)
712 /* If this is a temporary binding created for a cleanup, then we'll
713 have pushed a statement list level. Pop that, create a new
714 BIND_EXPR for the block, and insert it into the stream. */
715 stmt = pop_stmt_list (current_binding_level->statement_list);
716 stmt = c_build_bind_expr (block, stmt);
722 DECL_INITIAL (current_function_decl) = block;
724 current_binding_level->blocks
725 = chainon (current_binding_level->blocks, block);
727 /* If we did not make a block for the level just exited,
728 any blocks made for inner levels
729 (since they cannot be recorded as subblocks in that level)
730 must be carried forward so they will later become subblocks
731 of something else. */
733 current_binding_level->blocks
734 = chainon (current_binding_level->blocks, subblocks);
736 /* Each and every BLOCK node created here in `poplevel' is important
737 (e.g. for proper debugging information) so if we created one
738 earlier, mark it as "used". */
740 TREE_USED (block) = 1;
742 /* All temporary bindings created for cleanups are popped silently. */
743 if (kind == sk_cleanup)
746 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
749 /* Delete the node BLOCK from the current binding level.
750 This is used for the block inside a stmt expr ({...})
751 so that the block can be reinserted where appropriate. */
754 delete_block (tree block)
757 if (current_binding_level->blocks == block)
758 current_binding_level->blocks = TREE_CHAIN (block);
759 for (t = current_binding_level->blocks; t;)
761 if (TREE_CHAIN (t) == block)
762 TREE_CHAIN (t) = TREE_CHAIN (block);
766 TREE_CHAIN (block) = NULL_TREE;
767 /* Clear TREE_USED which is always set by poplevel.
768 The flag is set again if insert_block is called. */
769 TREE_USED (block) = 0;
772 /* Insert BLOCK at the end of the list of subblocks of the
773 current binding level. This is used when a BIND_EXPR is expanded,
774 to handle the BLOCK node inside the BIND_EXPR. */
777 insert_block (tree block)
779 TREE_USED (block) = 1;
780 current_binding_level->blocks
781 = chainon (current_binding_level->blocks, block);
784 /* Returns nonzero if T is a virtual function table. */
787 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
789 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
792 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
796 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
798 return (TREE_CODE (t) == TYPE_DECL
799 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
800 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
803 struct walk_globals_data {
809 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
810 for which P returns nonzero, call F with its address. If any call
811 to F returns a nonzero value, return a nonzero value. */
814 walk_vtables_r (tree namespace, void* data)
816 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
817 walk_globals_fn f = wgd->f;
819 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
822 for (; decl ; decl = TREE_CHAIN (decl))
823 result |= (*f) (&decl, d);
828 /* Walk the vtable declarations. Whenever one is found for which P
829 returns nonzero, call F with its address. If any call to F
830 returns a nonzero value, return a nonzero value. */
832 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
834 struct walk_globals_data wgd;
839 return walk_namespaces (walk_vtables_r, &wgd);
842 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
843 itself, calling F for each. The DATA is passed to F as well. */
846 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
849 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
851 result |= (*f) (namespace, data);
853 for (; current; current = TREE_CHAIN (current))
854 result |= walk_namespaces_r (current, f, data);
859 /* Walk all the namespaces, calling F for each. The DATA is passed to
863 walk_namespaces (walk_namespaces_fn f, void* data)
865 return walk_namespaces_r (global_namespace, f, data);
868 /* Walk the global declarations in NAMESPACE. Whenever one is found
869 for which P returns nonzero, call F with its address. If any call
870 to F returns a nonzero value, return a nonzero value. */
873 walk_globals_r (tree namespace, void* data)
875 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
876 walk_globals_pred p = wgd->p;
877 walk_globals_fn f = wgd->f;
882 t = &NAMESPACE_LEVEL (namespace)->names;
889 result |= (*f) (t, d);
891 /* If F changed *T, then *T still points at the next item to
894 t = &TREE_CHAIN (*t);
900 /* Walk the global declarations. Whenever one is found for which P
901 returns true, call F with its address. If any call to F
902 returns true, return true. */
905 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
907 struct walk_globals_data wgd;
912 return walk_namespaces (walk_globals_r, &wgd);
915 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
916 DATA is non-NULL, this is the last time we will call
917 wrapup_global_declarations for this NAMESPACE. */
920 wrapup_globals_for_namespace (tree namespace, void* data)
922 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
923 varray_type statics = level->static_decls;
924 tree *vec = &VARRAY_TREE (statics, 0);
925 int len = VARRAY_ACTIVE_SIZE (statics);
926 int last_time = (data != 0);
930 check_global_declarations (vec, len);
934 /* Write out any globals that need to be output. */
935 return wrapup_global_declarations (vec, len);
939 /* In C++, you don't have to write `struct S' to refer to `S'; you
940 can just use `S'. We accomplish this by creating a TYPE_DECL as
941 if the user had written `typedef struct S S'. Create and return
942 the TYPE_DECL for TYPE. */
945 create_implicit_typedef (tree name, tree type)
949 decl = build_decl (TYPE_DECL, name, type);
950 DECL_ARTIFICIAL (decl) = 1;
951 /* There are other implicit type declarations, like the one *within*
952 a class that allows you to write `S::S'. We must distinguish
954 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
955 TYPE_NAME (type) = decl;
960 /* Remember a local name for name-mangling purposes. */
963 push_local_name (tree decl)
968 timevar_push (TV_NAME_LOOKUP);
970 VARRAY_TREE_INIT (local_names, 8, "local_names");
972 name = DECL_NAME (decl);
974 nelts = VARRAY_ACTIVE_SIZE (local_names);
975 for (i = 0; i < nelts; i++)
977 t = VARRAY_TREE (local_names, i);
978 if (DECL_NAME (t) == name)
980 if (!DECL_LANG_SPECIFIC (decl))
981 retrofit_lang_decl (decl);
982 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
983 if (DECL_LANG_SPECIFIC (t))
984 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
986 DECL_DISCRIMINATOR (decl) = 1;
988 VARRAY_TREE (local_names, i) = decl;
989 timevar_pop (TV_NAME_LOOKUP);
994 VARRAY_PUSH_TREE (local_names, decl);
995 timevar_pop (TV_NAME_LOOKUP);
998 /* Subroutine of duplicate_decls: return truthvalue of whether
999 or not types of these decls match.
1001 For C++, we must compare the parameter list so that `int' can match
1002 `int&' in a parameter position, but `int&' is not confused with
1006 decls_match (tree newdecl, tree olddecl)
1010 if (newdecl == olddecl)
1013 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1014 /* If the two DECLs are not even the same kind of thing, we're not
1015 interested in their types. */
1018 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1020 tree f1 = TREE_TYPE (newdecl);
1021 tree f2 = TREE_TYPE (olddecl);
1022 tree p1 = TYPE_ARG_TYPES (f1);
1023 tree p2 = TYPE_ARG_TYPES (f2);
1025 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1026 && ! (DECL_EXTERN_C_P (newdecl)
1027 && DECL_EXTERN_C_P (olddecl)))
1030 if (TREE_CODE (f1) != TREE_CODE (f2))
1033 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
1035 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
1036 && (DECL_BUILT_IN (olddecl)
1037 #ifndef NO_IMPLICIT_EXTERN_C
1038 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1039 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1043 types_match = self_promoting_args_p (p1);
1044 if (p1 == void_list_node)
1045 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1047 #ifndef NO_IMPLICIT_EXTERN_C
1048 else if (p1 == NULL_TREE
1049 && (DECL_EXTERN_C_P (olddecl)
1050 && DECL_IN_SYSTEM_HEADER (olddecl)
1051 && !DECL_CLASS_SCOPE_P (olddecl))
1052 && (DECL_EXTERN_C_P (newdecl)
1053 && DECL_IN_SYSTEM_HEADER (newdecl)
1054 && !DECL_CLASS_SCOPE_P (newdecl)))
1056 types_match = self_promoting_args_p (p2);
1057 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1061 types_match = compparms (p1, p2);
1066 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1068 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1069 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1072 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1073 DECL_TEMPLATE_PARMS (olddecl)))
1076 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1077 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1078 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1080 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1081 DECL_TEMPLATE_RESULT (newdecl));
1085 if (TREE_TYPE (newdecl) == error_mark_node)
1086 types_match = TREE_TYPE (olddecl) == error_mark_node;
1087 else if (TREE_TYPE (olddecl) == NULL_TREE)
1088 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1089 else if (TREE_TYPE (newdecl) == NULL_TREE)
1092 types_match = comptypes (TREE_TYPE (newdecl),
1093 TREE_TYPE (olddecl),
1094 COMPARE_REDECLARATION);
1100 /* If NEWDECL is `static' and an `extern' was seen previously,
1101 warn about it. OLDDECL is the previous declaration.
1103 Note that this does not apply to the C++ case of declaring
1104 a variable `extern const' and then later `const'.
1106 Don't complain about built-in functions, since they are beyond
1107 the user's control. */
1110 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1114 if (TREE_CODE (newdecl) == TYPE_DECL
1115 || TREE_CODE (newdecl) == TEMPLATE_DECL
1116 || TREE_CODE (newdecl) == CONST_DECL
1117 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1120 /* Don't get confused by static member functions; that's a different
1122 if (TREE_CODE (newdecl) == FUNCTION_DECL
1123 && DECL_STATIC_FUNCTION_P (newdecl))
1126 /* If the old declaration was `static', or the new one isn't, then
1127 then everything is OK. */
1128 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1131 /* It's OK to declare a builtin function as `static'. */
1132 if (TREE_CODE (olddecl) == FUNCTION_DECL
1133 && DECL_ARTIFICIAL (olddecl))
1136 name = DECL_ASSEMBLER_NAME (newdecl);
1137 pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl);
1138 cp_pedwarn_at ("previous declaration of %qD", olddecl);
1141 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1142 If the redeclaration is invalid, a diagnostic is issued, and the
1143 error_mark_node is returned. Otherwise, OLDDECL is returned.
1145 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1149 duplicate_decls (tree newdecl, tree olddecl)
1151 unsigned olddecl_uid = DECL_UID (olddecl);
1152 int olddecl_friend = 0, types_match = 0;
1153 int new_defines_function = 0;
1155 if (newdecl == olddecl)
1158 types_match = decls_match (newdecl, olddecl);
1160 /* If either the type of the new decl or the type of the old decl is an
1161 error_mark_node, then that implies that we have already issued an
1162 error (earlier) for some bogus type specification, and in that case,
1163 it is rather pointless to harass the user with yet more error message
1164 about the same declaration, so just pretend the types match here. */
1165 if (TREE_TYPE (newdecl) == error_mark_node
1166 || TREE_TYPE (olddecl) == error_mark_node)
1169 if (DECL_P (olddecl)
1170 && TREE_CODE (newdecl) == FUNCTION_DECL
1171 && TREE_CODE (olddecl) == FUNCTION_DECL
1172 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1174 if (DECL_DECLARED_INLINE_P (newdecl)
1175 && DECL_UNINLINABLE (newdecl)
1176 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1177 /* Already warned elsewhere. */;
1178 else if (DECL_DECLARED_INLINE_P (olddecl)
1179 && DECL_UNINLINABLE (olddecl)
1180 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1181 /* Already warned. */;
1182 else if (DECL_DECLARED_INLINE_P (newdecl)
1183 && DECL_UNINLINABLE (olddecl)
1184 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1186 warning ("%Jfunction %qD redeclared as inline", newdecl, newdecl);
1187 warning ("%Jprevious declaration of %qD with attribute noinline",
1190 else if (DECL_DECLARED_INLINE_P (olddecl)
1191 && DECL_UNINLINABLE (newdecl)
1192 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1194 warning ("%Jfunction %qD redeclared with attribute noinline",
1196 warning ("%Jprevious declaration of %qD was inline",
1201 /* Check for redeclaration and other discrepancies. */
1202 if (TREE_CODE (olddecl) == FUNCTION_DECL
1203 && DECL_ARTIFICIAL (olddecl))
1205 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1207 /* Avoid warnings redeclaring anticipated built-ins. */
1208 if (DECL_ANTICIPATED (olddecl))
1211 /* If you declare a built-in or predefined function name as static,
1212 the old definition is overridden, but optionally warn this was a
1213 bad choice of name. */
1214 if (! TREE_PUBLIC (newdecl))
1217 warning ("shadowing %s function %q#D",
1218 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1220 /* Discard the old built-in function. */
1223 /* If the built-in is not ansi, then programs can override
1224 it even globally without an error. */
1225 else if (! DECL_BUILT_IN (olddecl))
1226 warning ("library function %q#D redeclared as non-function %q#D",
1230 error ("declaration of %q#D", newdecl);
1231 error ("conflicts with built-in declaration %q#D",
1236 else if (!types_match)
1238 /* Avoid warnings redeclaring anticipated built-ins. */
1239 if (DECL_ANTICIPATED (olddecl))
1241 /* Deal with fileptr_type_node. FILE type is not known
1242 at the time we create the builtins. */
1245 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1246 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1248 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1251 else if (TREE_VALUE (t2) == fileptr_type_node)
1253 tree t = TREE_VALUE (t1);
1255 if (TREE_CODE (t) == POINTER_TYPE
1256 && TYPE_NAME (TREE_TYPE (t))
1257 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1258 == get_identifier ("FILE")
1259 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1261 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1263 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1264 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1265 types_match = decls_match (newdecl, olddecl);
1267 return duplicate_decls (newdecl, olddecl);
1268 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1271 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1274 else if ((DECL_EXTERN_C_P (newdecl)
1275 && DECL_EXTERN_C_P (olddecl))
1276 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1277 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1279 /* A near match; override the builtin. */
1281 if (TREE_PUBLIC (newdecl))
1283 warning ("new declaration %q#D", newdecl);
1284 warning ("ambiguates built-in declaration %q#D",
1287 else if (warn_shadow)
1288 warning ("shadowing %s function %q#D",
1289 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1293 /* Discard the old built-in function. */
1296 /* Replace the old RTL to avoid problems with inlining. */
1297 COPY_DECL_RTL (newdecl, olddecl);
1299 /* Even if the types match, prefer the new declarations type
1300 for anticipated built-ins, for exception lists, etc... */
1301 else if (DECL_ANTICIPATED (olddecl))
1303 tree type = TREE_TYPE (newdecl);
1304 tree attribs = (*targetm.merge_type_attributes)
1305 (TREE_TYPE (olddecl), type);
1307 type = cp_build_type_attribute_variant (type, attribs);
1308 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1311 /* Whether or not the builtin can throw exceptions has no
1312 bearing on this declarator. */
1313 TREE_NOTHROW (olddecl) = 0;
1315 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1317 /* If a builtin function is redeclared as `static', merge
1318 the declarations, but make the original one static. */
1319 DECL_THIS_STATIC (olddecl) = 1;
1320 TREE_PUBLIC (olddecl) = 0;
1322 /* Make the old declaration consistent with the new one so
1323 that all remnants of the builtin-ness of this function
1324 will be banished. */
1325 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1326 COPY_DECL_RTL (newdecl, olddecl);
1329 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1331 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1332 && TREE_CODE (newdecl) != TYPE_DECL
1333 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
1334 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
1335 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1336 && TREE_CODE (olddecl) != TYPE_DECL
1337 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
1338 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1341 /* We do nothing special here, because C++ does such nasty
1342 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1343 get shadowed, and know that if we need to find a TYPE_DECL
1344 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1345 slot of the identifier. */
1349 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1350 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1351 || (TREE_CODE (olddecl) == FUNCTION_DECL
1352 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1355 error ("%q#D redeclared as different kind of symbol", newdecl);
1356 if (TREE_CODE (olddecl) == TREE_LIST)
1357 olddecl = TREE_VALUE (olddecl);
1358 cp_error_at ("previous declaration of %q#D", olddecl);
1360 return error_mark_node;
1362 else if (!types_match)
1364 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1365 /* These are certainly not duplicate declarations; they're
1366 from different scopes. */
1369 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1371 /* The name of a class template may not be declared to refer to
1372 any other template, class, function, object, namespace, value,
1373 or type in the same scope. */
1374 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1375 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1377 error ("declaration of template %q#D", newdecl);
1378 cp_error_at ("conflicts with previous declaration %q#D",
1381 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1382 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1383 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1384 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1385 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1386 DECL_TEMPLATE_PARMS (olddecl))
1387 /* Template functions can be disambiguated by
1389 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1390 TREE_TYPE (TREE_TYPE (olddecl))))
1392 error ("new declaration %q#D", newdecl);
1393 cp_error_at ("ambiguates old declaration %q#D", olddecl);
1397 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1399 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1401 error ("declaration of C function %q#D conflicts with",
1403 cp_error_at ("previous declaration %q#D here", olddecl);
1405 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1406 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1408 error ("new declaration %q#D", newdecl);
1409 cp_error_at ("ambiguates old declaration %q#D", olddecl);
1416 error ("conflicting declaration %q#D", newdecl);
1417 cp_error_at ("%qD has a previous declaration as %q#D",
1419 return error_mark_node;
1422 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1423 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1424 && (!DECL_TEMPLATE_INFO (newdecl)
1425 || (DECL_TI_TEMPLATE (newdecl)
1426 != DECL_TI_TEMPLATE (olddecl))))
1427 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1428 && (!DECL_TEMPLATE_INFO (olddecl)
1429 || (DECL_TI_TEMPLATE (olddecl)
1430 != DECL_TI_TEMPLATE (newdecl))))))
1431 /* It's OK to have a template specialization and a non-template
1432 with the same type, or to have specializations of two
1433 different templates with the same type. Note that if one is a
1434 specialization, and the other is an instantiation of the same
1435 template, that we do not exit at this point. That situation
1436 can occur if we instantiate a template class, and then
1437 specialize one of its methods. This situation is valid, but
1438 the declarations must be merged in the usual way. */
1440 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1441 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1442 && !DECL_USE_TEMPLATE (newdecl))
1443 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1444 && !DECL_USE_TEMPLATE (olddecl))))
1445 /* One of the declarations is a template instantiation, and the
1446 other is not a template at all. That's OK. */
1448 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1450 /* In [namespace.alias] we have:
1452 In a declarative region, a namespace-alias-definition can be
1453 used to redefine a namespace-alias declared in that declarative
1454 region to refer only to the namespace to which it already
1457 Therefore, if we encounter a second alias directive for the same
1458 alias, we can just ignore the second directive. */
1459 if (DECL_NAMESPACE_ALIAS (newdecl)
1460 && (DECL_NAMESPACE_ALIAS (newdecl)
1461 == DECL_NAMESPACE_ALIAS (olddecl)))
1463 /* [namespace.alias]
1465 A namespace-name or namespace-alias shall not be declared as
1466 the name of any other entity in the same declarative region.
1467 A namespace-name defined at global scope shall not be
1468 declared as the name of any other entity in any global scope
1470 error ("declaration of namespace %qD conflicts with", newdecl);
1471 cp_error_at ("previous declaration of namespace %qD here", olddecl);
1472 return error_mark_node;
1476 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1479 error (errmsg, newdecl);
1480 if (DECL_NAME (olddecl) != NULL_TREE)
1481 cp_error_at ((DECL_INITIAL (olddecl)
1482 && namespace_bindings_p ())
1483 ? "%q#D previously defined here"
1484 : "%q#D previously declared here", olddecl);
1485 return error_mark_node;
1487 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1488 && DECL_INITIAL (olddecl) != NULL_TREE
1489 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1490 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1492 /* Prototype decl follows defn w/o prototype. */
1493 cp_warning_at ("prototype for %q#D", newdecl);
1494 warning ("%Jfollows non-prototype definition here", olddecl);
1496 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1497 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1499 /* extern "C" int foo ();
1500 int foo () { bar (); }
1502 if (current_lang_depth () == 0)
1503 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1506 cp_error_at ("previous declaration of %q#D with %qL linkage",
1507 olddecl, DECL_LANGUAGE (olddecl));
1508 error ("conflicts with new declaration with %qL linkage",
1509 DECL_LANGUAGE (newdecl));
1513 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1515 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1517 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1518 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1521 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1522 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1524 for (; t1 && t1 != void_list_node;
1525 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1526 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1528 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1531 pedwarn ("default argument given for parameter %d of %q#D",
1533 cp_pedwarn_at ("after previous specification in %q#D",
1538 error ("default argument given for parameter %d of %q#D",
1540 cp_error_at ("after previous specification in %q#D",
1545 if (DECL_DECLARED_INLINE_P (newdecl)
1546 && ! DECL_DECLARED_INLINE_P (olddecl)
1547 && TREE_ADDRESSABLE (olddecl) && warn_inline)
1549 warning ("%q#D was used before it was declared inline", newdecl);
1550 warning ("%Jprevious non-inline declaration here", olddecl);
1555 /* Do not merge an implicit typedef with an explicit one. In:
1559 typedef class A A __attribute__ ((foo));
1561 the attribute should apply only to the typedef. */
1562 if (TREE_CODE (olddecl) == TYPE_DECL
1563 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1564 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1567 /* If new decl is `static' and an `extern' was seen previously,
1569 warn_extern_redeclared_static (newdecl, olddecl);
1571 /* We have committed to returning 1 at this point. */
1572 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1574 /* Now that functions must hold information normally held
1575 by field decls, there is extra work to do so that
1576 declaration information does not get destroyed during
1578 if (DECL_VINDEX (olddecl))
1579 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1580 if (DECL_CONTEXT (olddecl))
1581 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1582 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1583 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1584 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1585 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1586 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1587 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1588 SET_OVERLOADED_OPERATOR_CODE
1589 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1590 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1592 /* Optionally warn about more than one declaration for the same
1593 name, but don't warn about a function declaration followed by a
1595 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1596 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1597 /* Don't warn about extern decl followed by definition. */
1598 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1599 /* Don't warn about friends, let add_friend take care of it. */
1600 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
1602 warning ("redundant redeclaration of %qD in same scope", newdecl);
1603 cp_warning_at ("previous declaration of %qD", olddecl);
1607 /* Deal with C++: must preserve virtual function table size. */
1608 if (TREE_CODE (olddecl) == TYPE_DECL)
1610 tree newtype = TREE_TYPE (newdecl);
1611 tree oldtype = TREE_TYPE (olddecl);
1613 if (newtype != error_mark_node && oldtype != error_mark_node
1614 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1615 CLASSTYPE_FRIEND_CLASSES (newtype)
1616 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1618 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1621 /* Copy all the DECL_... slots specified in the new decl
1622 except for any that we copy here from the old type. */
1623 DECL_ATTRIBUTES (newdecl)
1624 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1626 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1628 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
1629 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1630 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1631 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1633 /* If the new declaration is a definition, update the file and
1634 line information on the declaration. */
1635 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
1636 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
1638 DECL_SOURCE_LOCATION (olddecl)
1639 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
1640 = DECL_SOURCE_LOCATION (newdecl);
1641 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1642 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (olddecl))
1643 = DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (newdecl));
1646 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1648 DECL_INLINE (DECL_TEMPLATE_RESULT (olddecl))
1649 |= DECL_INLINE (DECL_TEMPLATE_RESULT (newdecl));
1650 DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (olddecl))
1651 |= DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (newdecl));
1659 /* Automatically handles default parameters. */
1660 tree oldtype = TREE_TYPE (olddecl);
1663 /* Merge the data types specified in the two decls. */
1664 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1666 /* If merge_types produces a non-typedef type, just use the old type. */
1667 if (TREE_CODE (newdecl) == TYPE_DECL
1668 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1671 if (TREE_CODE (newdecl) == VAR_DECL)
1673 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1674 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1675 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1676 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1679 /* Do this after calling `merge_types' so that default
1680 parameters don't confuse us. */
1681 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1682 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
1683 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
1685 TREE_TYPE (newdecl) = build_exception_variant (newtype,
1686 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
1687 TREE_TYPE (olddecl) = build_exception_variant (newtype,
1688 TYPE_RAISES_EXCEPTIONS (oldtype));
1690 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
1691 && ! DECL_IS_BUILTIN (olddecl)
1693 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
1694 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
1696 error ("declaration of %qF throws different exceptions",
1698 cp_error_at ("than previous declaration %qF", olddecl);
1701 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1703 /* Lay the type out, unless already done. */
1704 if (! same_type_p (newtype, oldtype)
1705 && TREE_TYPE (newdecl) != error_mark_node
1706 && !(processing_template_decl && uses_template_parms (newdecl)))
1707 layout_type (TREE_TYPE (newdecl));
1709 if ((TREE_CODE (newdecl) == VAR_DECL
1710 || TREE_CODE (newdecl) == PARM_DECL
1711 || TREE_CODE (newdecl) == RESULT_DECL
1712 || TREE_CODE (newdecl) == FIELD_DECL
1713 || TREE_CODE (newdecl) == TYPE_DECL)
1714 && !(processing_template_decl && uses_template_parms (newdecl)))
1715 layout_decl (newdecl, 0);
1717 /* Merge the type qualifiers. */
1718 if (TREE_READONLY (newdecl))
1719 TREE_READONLY (olddecl) = 1;
1720 if (TREE_THIS_VOLATILE (newdecl))
1721 TREE_THIS_VOLATILE (olddecl) = 1;
1722 if (TREE_NOTHROW (newdecl))
1723 TREE_NOTHROW (olddecl) = 1;
1725 /* Merge deprecatedness. */
1726 if (TREE_DEPRECATED (newdecl))
1727 TREE_DEPRECATED (olddecl) = 1;
1729 /* Merge the initialization information. */
1730 if (DECL_INITIAL (newdecl) == NULL_TREE
1731 && DECL_INITIAL (olddecl) != NULL_TREE)
1733 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1734 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1735 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1736 && DECL_LANG_SPECIFIC (newdecl)
1737 && DECL_LANG_SPECIFIC (olddecl))
1739 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1740 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1744 /* Merge the section attribute.
1745 We want to issue an error if the sections conflict but that must be
1746 done later in decl_attributes since we are called before attributes
1748 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1749 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1751 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1753 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1754 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1755 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1756 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1757 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1758 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1759 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1760 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1761 /* Keep the old RTL. */
1762 COPY_DECL_RTL (olddecl, newdecl);
1764 else if (TREE_CODE (newdecl) == VAR_DECL
1765 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1767 /* Keep the old RTL. We cannot keep the old RTL if the old
1768 declaration was for an incomplete object and the new
1769 declaration is not since many attributes of the RTL will
1771 COPY_DECL_RTL (olddecl, newdecl);
1774 /* If cannot merge, then use the new type and qualifiers,
1775 and don't preserve the old rtl. */
1778 /* Clean out any memory we had of the old declaration. */
1779 tree oldstatic = value_member (olddecl, static_aggregates);
1781 TREE_VALUE (oldstatic) = error_mark_node;
1783 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1784 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1785 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1786 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1789 /* Merge the storage class information. */
1790 merge_weak (newdecl, olddecl);
1792 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
1793 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1794 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1795 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1796 if (! DECL_EXTERNAL (olddecl))
1797 DECL_EXTERNAL (newdecl) = 0;
1799 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1801 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1802 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1803 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1804 DECL_TEMPLATE_INSTANTIATED (newdecl)
1805 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1806 /* Don't really know how much of the language-specific
1807 values we should copy from old to new. */
1808 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1809 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1810 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
1811 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1812 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1813 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1814 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1815 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1816 olddecl_friend = DECL_FRIEND_P (olddecl);
1818 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1819 if (TREE_CODE (newdecl) == FUNCTION_DECL
1820 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1822 DECL_BEFRIENDING_CLASSES (newdecl)
1823 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1824 DECL_BEFRIENDING_CLASSES (olddecl));
1825 /* DECL_THUNKS is only valid for virtual functions,
1826 otherwise it is a DECL_FRIEND_CONTEXT. */
1827 if (DECL_VIRTUAL_P (newdecl))
1828 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1832 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1834 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1835 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1837 /* If newdecl is not a specialization, then it is not a
1838 template-related function at all. And that means that we
1839 should have exited above, returning 0. */
1840 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1842 if (TREE_USED (olddecl))
1843 /* From [temp.expl.spec]:
1845 If a template, a member template or the member of a class
1846 template is explicitly specialized then that
1847 specialization shall be declared before the first use of
1848 that specialization that would cause an implicit
1849 instantiation to take place, in every translation unit in
1850 which such a use occurs. */
1851 error ("explicit specialization of %qD after first use",
1854 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1856 /* [temp.expl.spec/14] We don't inline explicit specialization
1857 just because the primary template says so. */
1861 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1862 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1864 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1866 /* If either decl says `inline', this fn is inline, unless
1867 its definition was passed already. */
1868 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1869 DECL_INLINE (olddecl) = 1;
1870 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1872 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1873 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1876 /* Preserve abstractness on cloned [cd]tors. */
1877 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1881 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1882 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
1883 COPY_DECL_RTL (newdecl, olddecl);
1885 if (! types_match || new_defines_function)
1887 /* These need to be copied so that the names are available.
1888 Note that if the types do match, we'll preserve inline
1889 info and other bits, but if not, we won't. */
1890 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1891 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
1893 if (new_defines_function)
1894 /* If defining a function declared with other language
1895 linkage, use the previously declared language linkage. */
1896 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1897 else if (types_match)
1899 /* If redeclaring a builtin function, and not a definition,
1900 it stays built in. */
1901 if (DECL_BUILT_IN (olddecl))
1903 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1904 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1905 /* If we're keeping the built-in definition, keep the rtl,
1906 regardless of declaration matches. */
1907 COPY_DECL_RTL (olddecl, newdecl);
1910 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1911 /* Don't clear out the arguments if we're redefining a function. */
1912 if (DECL_ARGUMENTS (olddecl))
1913 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1916 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1917 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
1919 /* Now preserve various other info from the definition. */
1920 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1921 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1922 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1923 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1925 /* Warn about conflicting visibility specifications. */
1926 if (DECL_VISIBILITY_SPECIFIED (olddecl)
1927 && DECL_VISIBILITY_SPECIFIED (newdecl)
1928 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1930 warning ("%J%qD: visibility attribute ignored because it",
1932 warning ("%Jconflicts with previous declaration here", olddecl);
1934 /* Choose the declaration which specified visibility. */
1935 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1937 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1938 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1941 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
1942 with that from NEWDECL below. */
1943 if (DECL_LANG_SPECIFIC (olddecl))
1945 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
1946 != DECL_LANG_SPECIFIC (newdecl));
1947 ggc_free (DECL_LANG_SPECIFIC (olddecl));
1950 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1954 function_size = sizeof (struct tree_decl);
1956 memcpy ((char *) olddecl + sizeof (struct tree_common),
1957 (char *) newdecl + sizeof (struct tree_common),
1958 function_size - sizeof (struct tree_common));
1960 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
1961 /* If newdecl is a template instantiation, it is possible that
1962 the following sequence of events has occurred:
1964 o A friend function was declared in a class template. The
1965 class template was instantiated.
1967 o The instantiation of the friend declaration was
1968 recorded on the instantiation list, and is newdecl.
1970 o Later, however, instantiate_class_template called pushdecl
1971 on the newdecl to perform name injection. But, pushdecl in
1972 turn called duplicate_decls when it discovered that another
1973 declaration of a global function with the same name already
1976 o Here, in duplicate_decls, we decided to clobber newdecl.
1978 If we're going to do that, we'd better make sure that
1979 olddecl, and not newdecl, is on the list of
1980 instantiations so that if we try to do the instantiation
1981 again we won't get the clobbered declaration. */
1982 reregister_specialization (newdecl,
1983 DECL_TI_TEMPLATE (newdecl),
1988 memcpy ((char *) olddecl + sizeof (struct tree_common),
1989 (char *) newdecl + sizeof (struct tree_common),
1990 sizeof (struct tree_decl) - sizeof (struct tree_common)
1991 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
1994 DECL_UID (olddecl) = olddecl_uid;
1996 DECL_FRIEND_P (olddecl) = 1;
1998 /* NEWDECL contains the merged attribute lists.
1999 Update OLDDECL to be the same. */
2000 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2002 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2003 so that encode_section_info has a chance to look at the new decl
2004 flags and attributes. */
2005 if (DECL_RTL_SET_P (olddecl)
2006 && (TREE_CODE (olddecl) == FUNCTION_DECL
2007 || (TREE_CODE (olddecl) == VAR_DECL
2008 && TREE_STATIC (olddecl))))
2009 make_decl_rtl (olddecl);
2011 /* The NEWDECL will no longer be needed. Because every out-of-class
2012 declaration of a member results in a call to duplicate_decls,
2013 freeing these nodes represents in a significant savings. */
2019 /* Return zero if the declaration NEWDECL is valid
2020 when the declaration OLDDECL (assumed to be for the same name)
2021 has already been seen.
2022 Otherwise return an error message format string with a %s
2023 where the identifier should go. */
2026 redeclaration_error_message (tree newdecl, tree olddecl)
2028 if (TREE_CODE (newdecl) == TYPE_DECL)
2030 /* Because C++ can put things into name space for free,
2031 constructs like "typedef struct foo { ... } foo"
2032 would look like an erroneous redeclaration. */
2033 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2036 return "redefinition of %q#D";
2038 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2040 /* If this is a pure function, its olddecl will actually be
2041 the original initialization to `0' (which we force to call
2042 abort()). Don't complain about redefinition in this case. */
2043 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
2046 /* If both functions come from different namespaces, this is not
2047 a redeclaration - this is a conflict with a used function. */
2048 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2049 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
2050 return "%qD conflicts with used function";
2052 /* We'll complain about linkage mismatches in
2053 warn_extern_redeclared_static. */
2055 /* Defining the same name twice is no good. */
2056 if (DECL_INITIAL (olddecl) != NULL_TREE
2057 && DECL_INITIAL (newdecl) != NULL_TREE)
2059 if (DECL_NAME (olddecl) == NULL_TREE)
2060 return "%q#D not declared in class";
2062 return "redefinition of %q#D";
2066 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2070 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2072 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2073 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2074 return "redefinition of %q#D";
2078 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2079 || (DECL_TEMPLATE_RESULT (newdecl)
2080 == DECL_TEMPLATE_RESULT (olddecl)))
2083 nt = DECL_TEMPLATE_RESULT (newdecl);
2084 if (DECL_TEMPLATE_INFO (nt))
2085 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2086 ot = DECL_TEMPLATE_RESULT (olddecl);
2087 if (DECL_TEMPLATE_INFO (ot))
2088 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2089 if (DECL_INITIAL (nt) && DECL_INITIAL (ot))
2090 return "redefinition of %q#D";
2094 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2096 /* Objects declared at top level: */
2097 /* If at least one is a reference, it's ok. */
2098 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2100 /* Reject two definitions. */
2101 return "redefinition of %q#D";
2105 /* Objects declared with block scope: */
2106 /* Reject two definitions, and reject a definition
2107 together with an external reference. */
2108 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2109 return "redeclaration of %q#D";
2114 /* Create a new label, named ID. */
2117 make_label_decl (tree id, int local_p)
2121 decl = build_decl (LABEL_DECL, id, void_type_node);
2123 DECL_CONTEXT (decl) = current_function_decl;
2124 DECL_MODE (decl) = VOIDmode;
2125 C_DECLARED_LABEL_FLAG (decl) = local_p;
2127 /* Say where one reference is to the label, for the sake of the
2128 error if it is not defined. */
2129 DECL_SOURCE_LOCATION (decl) = input_location;
2131 /* Record the fact that this identifier is bound to this label. */
2132 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2137 /* Record this label on the list of used labels so that we can check
2138 at the end of the function to see whether or not the label was
2139 actually defined, and so we can check when the label is defined whether
2140 this use is valid. */
2143 use_label (tree decl)
2145 if (named_label_uses == NULL
2146 || named_label_uses->names_in_scope != current_binding_level->names
2147 || named_label_uses->label_decl != decl)
2149 struct named_label_use_list *new_ent;
2150 new_ent = GGC_NEW (struct named_label_use_list);
2151 new_ent->label_decl = decl;
2152 new_ent->names_in_scope = current_binding_level->names;
2153 new_ent->binding_level = current_binding_level;
2154 new_ent->o_goto_locus = input_location;
2155 new_ent->next = named_label_uses;
2156 named_label_uses = new_ent;
2160 /* Look for a label named ID in the current function. If one cannot
2161 be found, create one. (We keep track of used, but undefined,
2162 labels, and complain about them at the end of a function.) */
2165 lookup_label (tree id)
2168 struct named_label_list *ent;
2170 timevar_push (TV_NAME_LOOKUP);
2171 /* You can't use labels at global scope. */
2172 if (current_function_decl == NULL_TREE)
2174 error ("label %qE referenced outside of any function", id);
2175 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2178 /* See if we've already got this label. */
2179 decl = IDENTIFIER_LABEL_VALUE (id);
2180 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2181 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2183 /* Record this label on the list of labels used in this function.
2184 We do this before calling make_label_decl so that we get the
2185 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2186 ent = GGC_CNEW (struct named_label_list);
2187 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
2188 ent->next = named_labels;
2191 /* We need a new label. */
2192 decl = make_label_decl (id, /*local_p=*/0);
2194 /* Now fill in the information we didn't have before. */
2195 ent->label_decl = decl;
2197 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2200 /* Declare a local label named ID. */
2203 declare_local_label (tree id)
2207 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2208 this scope we can restore the old value of
2209 IDENTIFIER_TYPE_VALUE. */
2210 current_binding_level->shadowed_labels
2211 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2212 current_binding_level->shadowed_labels);
2213 /* Look for the label. */
2214 decl = make_label_decl (id, /*local_p=*/1);
2215 /* Now fill in the information we didn't have before. */
2216 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
2221 /* Returns nonzero if it is ill-formed to jump past the declaration of
2222 DECL. Returns 2 if it's also a real problem. */
2225 decl_jump_unsafe (tree decl)
2227 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
2230 if (DECL_INITIAL (decl) == NULL_TREE
2231 && pod_type_p (TREE_TYPE (decl)))
2234 /* This is really only important if we're crossing an initialization.
2235 The POD stuff is just pedantry; why should it matter if the class
2236 contains a field of pointer to member type? */
2237 if (DECL_INITIAL (decl)
2238 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
2243 /* Check that a single previously seen jump to a newly defined label
2244 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2245 the jump context; NAMES are the names in scope in LEVEL at the jump
2246 context; FILE and LINE are the source position of the jump or 0. */
2249 check_previous_goto_1 (tree decl,
2250 struct cp_binding_level* level,
2251 tree names, const location_t *locus)
2255 struct cp_binding_level *b = current_binding_level;
2256 for (; b; b = b->level_chain)
2258 tree new_decls = b->names;
2259 tree old_decls = (b == level ? names : NULL_TREE);
2260 for (; new_decls != old_decls;
2261 new_decls = TREE_CHAIN (new_decls))
2263 int problem = decl_jump_unsafe (new_decls);
2270 pedwarn ("jump to label %qD", decl);
2272 pedwarn ("jump to case label");
2275 pedwarn ("%H from here", locus);
2280 cp_error_at (" crosses initialization of %q#D",
2283 cp_pedwarn_at (" enters scope of non-POD %q#D",
2289 if ((b->kind == sk_try || b->kind == sk_catch) && ! saw_eh)
2294 pedwarn ("jump to label %qD", decl);
2296 pedwarn ("jump to case label");
2299 pedwarn ("%H from here", locus);
2302 if (b->kind == sk_try)
2303 error (" enters try block");
2305 error (" enters catch block");
2312 check_previous_goto (struct named_label_use_list* use)
2314 check_previous_goto_1 (use->label_decl, use->binding_level,
2315 use->names_in_scope, &use->o_goto_locus);
2319 check_switch_goto (struct cp_binding_level* level)
2321 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
2324 /* Check that any previously seen jumps to a newly defined label DECL
2325 are OK. Called by define_label. */
2328 check_previous_gotos (tree decl)
2330 struct named_label_use_list **usep;
2332 if (! TREE_USED (decl))
2335 for (usep = &named_label_uses; *usep; )
2337 struct named_label_use_list *use = *usep;
2338 if (use->label_decl == decl)
2340 check_previous_goto (use);
2344 usep = &(use->next);
2348 /* Check that a new jump to a label DECL is OK. Called by
2349 finish_goto_stmt. */
2352 check_goto (tree decl)
2356 struct named_label_list *lab;
2358 /* We can't know where a computed goto is jumping. So we assume
2360 if (! DECL_P (decl))
2363 /* If the label hasn't been defined yet, defer checking. */
2364 if (! DECL_INITIAL (decl))
2370 for (lab = named_labels; lab; lab = lab->next)
2371 if (decl == lab->label_decl)
2374 /* If the label is not on named_labels it's a gcc local label, so
2375 it must be in an outer scope, so jumping to it is always OK. */
2379 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
2382 cp_pedwarn_at ("jump to label %qD", decl);
2383 pedwarn (" from here");
2387 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
2389 tree b = TREE_VALUE (bad);
2390 int u = decl_jump_unsafe (b);
2392 if (u > 1 && DECL_ARTIFICIAL (b))
2393 /* Can't skip init of __exception_info. */
2394 error ("%J enters catch block", b);
2396 cp_error_at (" skips initialization of %q#D", b);
2398 cp_pedwarn_at (" enters scope of non-POD %q#D", b);
2401 if (lab->in_try_scope)
2402 error (" enters try block");
2403 else if (lab->in_catch_scope)
2404 error (" enters catch block");
2407 /* Define a label, specifying the location in the source file.
2408 Return the LABEL_DECL node for the label. */
2411 define_label (location_t location, tree name)
2413 tree decl = lookup_label (name);
2414 struct named_label_list *ent;
2415 struct cp_binding_level *p;
2417 timevar_push (TV_NAME_LOOKUP);
2418 for (ent = named_labels; ent; ent = ent->next)
2419 if (ent->label_decl == decl)
2422 /* After labels, make any new cleanups in the function go into their
2423 own new (temporary) binding contour. */
2424 for (p = current_binding_level;
2425 p->kind != sk_function_parms;
2427 p->more_cleanups_ok = 0;
2429 if (name == get_identifier ("wchar_t"))
2430 pedwarn ("label named wchar_t");
2432 if (DECL_INITIAL (decl) != NULL_TREE)
2433 error ("duplicate label %qD", decl);
2436 /* Mark label as having been defined. */
2437 DECL_INITIAL (decl) = error_mark_node;
2438 /* Say where in the source. */
2439 DECL_SOURCE_LOCATION (decl) = location;
2442 ent->names_in_scope = current_binding_level->names;
2443 ent->binding_level = current_binding_level;
2445 check_previous_gotos (decl);
2448 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2453 struct cp_binding_level *level;
2454 struct cp_switch *next;
2455 /* The SWITCH_STMT being built. */
2457 /* A splay-tree mapping the low element of a case range to the high
2458 element, or NULL_TREE if there is no high element. Used to
2459 determine whether or not a new case label duplicates an old case
2460 label. We need a tree, rather than simply a hash table, because
2461 of the GNU case range extension. */
2465 /* A stack of the currently active switch statements. The innermost
2466 switch statement is on the top of the stack. There is no need to
2467 mark the stack for garbage collection because it is only active
2468 during the processing of the body of a function, and we never
2469 collect at that point. */
2471 static struct cp_switch *switch_stack;
2473 /* Called right after a switch-statement condition is parsed.
2474 SWITCH_STMT is the switch statement being parsed. */
2477 push_switch (tree switch_stmt)
2479 struct cp_switch *p = xmalloc (sizeof (struct cp_switch));
2480 p->level = current_binding_level;
2481 p->next = switch_stack;
2482 p->switch_stmt = switch_stmt;
2483 p->cases = splay_tree_new (case_compare, NULL, NULL);
2490 struct cp_switch *cs = switch_stack;
2492 /* Emit warnings as needed. */
2493 c_do_switch_warnings (cs->cases, cs->switch_stmt);
2495 splay_tree_delete (cs->cases);
2496 switch_stack = switch_stack->next;
2500 /* Note that we've seen a definition of a case label, and complain if this
2501 is a bad place for one. */
2504 finish_case_label (tree low_value, tree high_value)
2507 struct cp_binding_level *p;
2509 if (processing_template_decl)
2513 /* For templates, just add the case label; we'll do semantic
2514 analysis at instantiation-time. */
2515 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2516 return add_stmt (build_case_label (low_value, high_value, label));
2519 /* Find the condition on which this switch statement depends. */
2520 cond = SWITCH_COND (switch_stack->switch_stmt);
2521 if (cond && TREE_CODE (cond) == TREE_LIST)
2522 cond = TREE_VALUE (cond);
2524 r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond),
2525 low_value, high_value);
2527 check_switch_goto (switch_stack->level);
2529 /* After labels, make any new cleanups in the function go into their
2530 own new (temporary) binding contour. */
2531 for (p = current_binding_level;
2532 p->kind != sk_function_parms;
2534 p->more_cleanups_ok = 0;
2539 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2542 typename_hash (const void* k)
2547 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2548 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2553 typedef struct typename_info {
2561 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
2564 typename_compare (const void * k1, const void * k2)
2567 const typename_info *t2;
2570 t2 = (const typename_info *) k2;
2572 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2573 && TYPE_CONTEXT (t1) == t2->scope
2574 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2575 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2576 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2579 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2580 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2582 Returns the new TYPENAME_TYPE. */
2584 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2587 build_typename_type (tree context, tree name, tree fullname,
2588 enum tag_types tag_type)
2596 if (typename_htab == NULL)
2597 typename_htab = htab_create_ggc (61, &typename_hash,
2598 &typename_compare, NULL);
2600 ti.scope = FROB_CONTEXT (context);
2602 ti.template_id = fullname;
2603 ti.enum_p = tag_type == enum_type;
2604 ti.class_p = (tag_type == class_type
2605 || tag_type == record_type
2606 || tag_type == union_type);
2607 hash = (htab_hash_pointer (ti.scope)
2608 ^ htab_hash_pointer (ti.name));
2610 /* See if we already have this type. */
2611 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2616 /* Build the TYPENAME_TYPE. */
2617 t = make_aggr_type (TYPENAME_TYPE);
2618 TYPE_CONTEXT (t) = ti.scope;
2619 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2620 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2621 TYPENAME_IS_CLASS_P (t) = ti.class_p;
2623 /* Build the corresponding TYPE_DECL. */
2624 d = build_decl (TYPE_DECL, name, t);
2625 TYPE_NAME (TREE_TYPE (d)) = d;
2626 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2627 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2628 DECL_ARTIFICIAL (d) = 1;
2630 /* Store it in the hash table. */
2637 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2638 provided to name the type. Returns an appropriate type, unless an
2639 error occurs, in which case error_mark_node is returned. If we
2640 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2641 return that, rather than the _TYPE it corresponds to, in other
2642 cases we look through the type decl. If TF_ERROR is set, complain
2643 about errors, otherwise be quiet. */
2646 make_typename_type (tree context, tree name, enum tag_types tag_type,
2647 tsubst_flags_t complain)
2651 if (name == error_mark_node
2652 || context == NULL_TREE
2653 || context == error_mark_node)
2654 return error_mark_node;
2658 if (!(TYPE_LANG_SPECIFIC (name)
2659 && (CLASSTYPE_IS_TEMPLATE (name)
2660 || CLASSTYPE_USE_TEMPLATE (name))))
2661 name = TYPE_IDENTIFIER (name);
2663 /* Create a TEMPLATE_ID_EXPR for the type. */
2664 name = build_nt (TEMPLATE_ID_EXPR,
2665 CLASSTYPE_TI_TEMPLATE (name),
2666 CLASSTYPE_TI_ARGS (name));
2668 else if (TREE_CODE (name) == TYPE_DECL)
2669 name = DECL_NAME (name);
2673 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2675 name = TREE_OPERAND (name, 0);
2676 if (TREE_CODE (name) == TEMPLATE_DECL)
2677 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2679 if (TREE_CODE (name) == TEMPLATE_DECL)
2681 error ("%qD used without template parameters", name);
2682 return error_mark_node;
2684 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2685 gcc_assert (TYPE_P (context));
2687 if (!dependent_type_p (context)
2688 || currently_open_class (context))
2690 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
2692 tree tmpl = NULL_TREE;
2693 if (IS_AGGR_TYPE (context))
2694 tmpl = lookup_field (context, name, 0, false);
2695 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2697 if (complain & tf_error)
2698 error ("no class template named %q#T in %q#T",
2700 return error_mark_node;
2703 if (complain & tf_error)
2704 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
2706 return lookup_template_class (tmpl,
2707 TREE_OPERAND (fullname, 1),
2709 /*entering_scope=*/0,
2710 tf_error | tf_warning | tf_user);
2716 if (!IS_AGGR_TYPE (context))
2718 if (complain & tf_error)
2719 error ("no type named %q#T in %q#T", name, context);
2720 return error_mark_node;
2723 t = lookup_field (context, name, 0, true);
2726 if (TREE_CODE (t) != TYPE_DECL)
2728 if (complain & tf_error)
2729 error ("no type named %q#T in %q#T", name, context);
2730 return error_mark_node;
2733 if (complain & tf_error)
2734 perform_or_defer_access_check (TYPE_BINFO (context), t);
2736 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2744 /* If the CONTEXT is not a template type, then either the field is
2745 there now or its never going to be. */
2746 if (!dependent_type_p (context))
2748 if (complain & tf_error)
2749 error ("no type named %q#T in %q#T", name, context);
2750 return error_mark_node;
2753 return build_typename_type (context, name, fullname, tag_type);
2756 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
2757 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
2758 in which case error_mark_node is returned.
2760 If PARM_LIST is non-NULL, also make sure that the template parameter
2761 list of TEMPLATE_DECL matches.
2763 If COMPLAIN zero, don't complain about any errors that occur. */
2766 make_unbound_class_template (tree context, tree name, tree parm_list,
2767 tsubst_flags_t complain)
2773 name = TYPE_IDENTIFIER (name);
2774 else if (DECL_P (name))
2775 name = DECL_NAME (name);
2776 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2778 if (!dependent_type_p (context)
2779 || currently_open_class (context))
2781 tree tmpl = NULL_TREE;
2783 if (IS_AGGR_TYPE (context))
2784 tmpl = lookup_field (context, name, 0, false);
2786 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2788 if (complain & tf_error)
2789 error ("no class template named %q#T in %q#T", name, context);
2790 return error_mark_node;
2794 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
2796 if (complain & tf_error)
2798 error ("template parameters do not match template");
2799 cp_error_at ("%qD declared here", tmpl);
2801 return error_mark_node;
2804 if (complain & tf_error)
2805 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
2810 /* Build the UNBOUND_CLASS_TEMPLATE. */
2811 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2812 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2813 TREE_TYPE (t) = NULL_TREE;
2815 /* Build the corresponding TEMPLATE_DECL. */
2816 d = build_decl (TEMPLATE_DECL, name, t);
2817 TYPE_NAME (TREE_TYPE (d)) = d;
2818 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2819 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2820 DECL_ARTIFICIAL (d) = 1;
2821 DECL_TEMPLATE_PARMS (d) = parm_list;
2828 /* Push the declarations of builtin types into the namespace.
2829 RID_INDEX is the index of the builtin type in the array
2830 RID_POINTERS. NAME is the name used when looking up the builtin
2831 type. TYPE is the _TYPE node for the builtin type. */
2834 record_builtin_type (enum rid rid_index,
2838 tree rname = NULL_TREE, tname = NULL_TREE;
2839 tree tdecl = NULL_TREE;
2841 if ((int) rid_index < (int) RID_MAX)
2842 rname = ridpointers[(int) rid_index];
2844 tname = get_identifier (name);
2846 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
2847 eliminated. Built-in types should not be looked up name; their
2848 names are keywords that the parser can recognize. However, there
2849 is code in c-common.c that uses identifier_global_value to look
2850 up built-in types by name. */
2853 tdecl = build_decl (TYPE_DECL, tname, type);
2854 DECL_ARTIFICIAL (tdecl) = 1;
2855 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
2861 tdecl = build_decl (TYPE_DECL, rname, type);
2862 DECL_ARTIFICIAL (tdecl) = 1;
2864 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
2867 if (!TYPE_NAME (type))
2868 TYPE_NAME (type) = tdecl;
2871 debug_hooks->type_decl (tdecl, 0);
2874 /* Record one of the standard Java types.
2875 * Declare it as having the given NAME.
2876 * If SIZE > 0, it is the size of one of the integral types;
2877 * otherwise it is the negative of the size of one of the other types. */
2880 record_builtin_java_type (const char* name, int size)
2884 type = make_signed_type (size);
2885 else if (size > -32)
2886 { /* "__java_char" or ""__java_boolean". */
2887 type = make_unsigned_type (-size);
2888 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
2891 { /* "__java_float" or ""__java_double". */
2892 type = make_node (REAL_TYPE);
2893 TYPE_PRECISION (type) = - size;
2896 record_builtin_type (RID_MAX, name, type);
2897 decl = TYPE_NAME (type);
2899 /* Suppress generate debug symbol entries for these types,
2900 since for normal C++ they are just clutter.
2901 However, push_lang_context undoes this if extern "Java" is seen. */
2902 DECL_IGNORED_P (decl) = 1;
2904 TYPE_FOR_JAVA (type) = 1;
2908 /* Push a type into the namespace so that the back-ends ignore it. */
2911 record_unknown_type (tree type, const char* name)
2913 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
2914 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
2915 DECL_IGNORED_P (decl) = 1;
2916 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
2917 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
2918 TYPE_ALIGN (type) = 1;
2919 TYPE_USER_ALIGN (type) = 0;
2920 TYPE_MODE (type) = TYPE_MODE (void_type_node);
2923 /* An string for which we should create an IDENTIFIER_NODE at
2926 typedef struct predefined_identifier
2928 /* The name of the identifier. */
2929 const char *const name;
2930 /* The place where the IDENTIFIER_NODE should be stored. */
2932 /* Nonzero if this is the name of a constructor or destructor. */
2933 const int ctor_or_dtor_p;
2934 } predefined_identifier;
2936 /* Create all the predefined identifiers. */
2939 initialize_predefined_identifiers (void)
2941 const predefined_identifier *pid;
2943 /* A table of identifiers to create at startup. */
2944 static const predefined_identifier predefined_identifiers[] = {
2945 { "C++", &lang_name_cplusplus, 0 },
2946 { "C", &lang_name_c, 0 },
2947 { "Java", &lang_name_java, 0 },
2948 /* Some of these names have a trailing space so that it is
2949 impossible for them to conflict with names written by users. */
2950 { "__ct ", &ctor_identifier, 1 },
2951 { "__base_ctor ", &base_ctor_identifier, 1 },
2952 { "__comp_ctor ", &complete_ctor_identifier, 1 },
2953 { "__dt ", &dtor_identifier, 1 },
2954 { "__comp_dtor ", &complete_dtor_identifier, 1 },
2955 { "__base_dtor ", &base_dtor_identifier, 1 },
2956 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
2957 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
2958 { "nelts", &nelts_identifier, 0 },
2959 { THIS_NAME, &this_identifier, 0 },
2960 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
2961 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
2962 { "_vptr", &vptr_identifier, 0 },
2963 { "__vtt_parm", &vtt_parm_identifier, 0 },
2964 { "::", &global_scope_name, 0 },
2965 { "std", &std_identifier, 0 },
2969 for (pid = predefined_identifiers; pid->name; ++pid)
2971 *pid->node = get_identifier (pid->name);
2972 if (pid->ctor_or_dtor_p)
2973 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
2977 /* Create the predefined scalar types of C,
2978 and some nodes representing standard constants (0, 1, (void *)0).
2979 Initialize the global binding level.
2980 Make definitions for built-in primitive functions. */
2983 cxx_init_decl_processing (void)
2986 tree void_ftype_ptr;
2988 build_common_tree_nodes (flag_signed_char, false);
2990 /* Create all the identifiers we need. */
2991 initialize_predefined_identifiers ();
2993 /* Create the global variables. */
2994 push_to_top_level ();
2996 current_function_decl = NULL_TREE;
2997 current_binding_level = NULL;
2998 /* Enter the global namespace. */
2999 gcc_assert (global_namespace == NULL_TREE);
3000 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3002 begin_scope (sk_namespace, global_namespace);
3004 current_lang_name = NULL_TREE;
3006 /* Adjust various flags based on command-line settings. */
3007 if (!flag_permissive)
3008 flag_pedantic_errors = 1;
3009 if (!flag_no_inline)
3011 flag_inline_trees = 1;
3014 if (flag_inline_functions)
3015 flag_inline_trees = 2;
3017 /* Force minimum function alignment if using the least significant
3018 bit of function pointers to store the virtual bit. */
3019 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
3020 && force_align_functions_log < 1)
3021 force_align_functions_log = 1;
3024 current_lang_name = lang_name_c;
3026 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
3027 TREE_TYPE (error_mark_list) = error_mark_node;
3029 /* Create the `std' namespace. */
3030 push_namespace (std_identifier);
3031 std_node = current_namespace;
3034 c_common_nodes_and_builtins ();
3036 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3037 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3038 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3039 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3040 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3041 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3042 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3043 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3045 integer_two_node = build_int_cst (NULL_TREE, 2);
3046 integer_three_node = build_int_cst (NULL_TREE, 3);
3048 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3049 truthvalue_type_node = boolean_type_node;
3050 truthvalue_false_node = boolean_false_node;
3051 truthvalue_true_node = boolean_true_node;
3053 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3056 record_builtin_type (RID_MAX, NULL, string_type_node);
3059 delta_type_node = ptrdiff_type_node;
3060 vtable_index_type = ptrdiff_type_node;
3062 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3063 void_ftype = build_function_type (void_type_node, void_list_node);
3064 void_ftype_ptr = build_function_type (void_type_node,
3065 tree_cons (NULL_TREE,
3069 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3071 /* C++ extensions */
3073 unknown_type_node = make_node (UNKNOWN_TYPE);
3074 record_unknown_type (unknown_type_node, "unknown type");
3076 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3077 TREE_TYPE (unknown_type_node) = unknown_type_node;
3079 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3081 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3082 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3085 /* Make sure we get a unique function type, so we can give
3086 its pointer type a name. (This wins for gdb.) */
3087 tree vfunc_type = make_node (FUNCTION_TYPE);
3088 TREE_TYPE (vfunc_type) = integer_type_node;
3089 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3090 layout_type (vfunc_type);
3092 vtable_entry_type = build_pointer_type (vfunc_type);
3094 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3097 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3098 layout_type (vtbl_type_node);
3099 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3100 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3101 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3102 layout_type (vtbl_ptr_type_node);
3103 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3105 push_namespace (get_identifier ("__cxxabiv1"));
3106 abi_node = current_namespace;
3109 global_type_node = make_node (LANG_TYPE);
3110 record_unknown_type (global_type_node, "global type");
3113 current_lang_name = lang_name_cplusplus;
3117 tree bad_alloc_type_node;
3118 tree bad_alloc_decl;
3119 tree newtype, deltype;
3120 tree ptr_ftype_sizetype;
3122 push_namespace (std_identifier);
3123 bad_alloc_id = get_identifier ("bad_alloc");
3124 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3125 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3127 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3128 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3129 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
3133 = build_function_type (ptr_type_node,
3134 tree_cons (NULL_TREE,
3137 newtype = build_exception_variant
3138 (ptr_ftype_sizetype, add_exception_specifier
3139 (NULL_TREE, bad_alloc_type_node, -1));
3140 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3141 push_cp_library_fn (NEW_EXPR, newtype);
3142 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3143 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3144 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3148 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3150 /* Perform other language dependent initializations. */
3151 init_class_processing ();
3152 init_rtti_processing ();
3154 if (flag_exceptions)
3155 init_exception_processing ();
3157 if (! supports_one_only ())
3160 make_fname_decl = cp_make_fname_decl;
3161 start_fname_decls ();
3163 /* Show we use EH for cleanups. */
3164 if (flag_exceptions)
3165 using_eh_for_cleanups ();
3168 /* Generate an initializer for a function naming variable from
3169 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3170 filled in with the type of the init. */
3173 cp_fname_init (const char* name, tree *type_p)
3175 tree domain = NULL_TREE;
3177 tree init = NULL_TREE;
3182 length = strlen (name);
3183 domain = build_index_type (size_int (length));
3184 init = build_string (length + 1, name);
3187 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3188 type = build_cplus_array_type (type, domain);
3193 TREE_TYPE (init) = type;
3195 init = error_mark_node;
3200 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3201 decl, NAME is the initialization string and TYPE_DEP indicates whether
3202 NAME depended on the type of the function. We make use of that to detect
3203 __PRETTY_FUNCTION__ inside a template fn. This is being done
3204 lazily at the point of first use, so we mustn't push the decl now. */
3207 cp_make_fname_decl (tree id, int type_dep)
3209 const char *const name = (type_dep && processing_template_decl
3210 ? NULL : fname_as_string (type_dep));
3212 tree init = cp_fname_init (name, &type);
3213 tree decl = build_decl (VAR_DECL, id, type);
3216 free ((char *) name);
3218 /* As we're using pushdecl_with_scope, we must set the context. */
3219 DECL_CONTEXT (decl) = current_function_decl;
3220 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3222 TREE_STATIC (decl) = 1;
3223 TREE_READONLY (decl) = 1;
3224 DECL_ARTIFICIAL (decl) = 1;
3225 DECL_INITIAL (decl) = init;
3227 TREE_USED (decl) = 1;
3229 if (current_function_decl)
3231 struct cp_binding_level *b = current_binding_level;
3232 while (b->level_chain->kind != sk_function_parms)
3234 pushdecl_with_scope (decl, b);
3235 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
3238 pushdecl_top_level_and_finish (decl, init);
3243 /* Make a definition for a builtin function named NAME in the current
3244 namespace, whose data type is TYPE and whose context is CONTEXT.
3245 TYPE should be a function type with argument types.
3247 CLASS and CODE tell later passes how to compile calls to this function.
3248 See tree.h for possible values.
3250 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3251 the name to be called if we can't opencode the function.
3252 If ATTRS is nonzero, use that for the function's attribute
3256 builtin_function_1 (const char* name,
3259 enum built_in_function code,
3260 enum built_in_class class,
3261 const char* libname,
3264 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
3265 DECL_BUILT_IN_CLASS (decl) = class;
3266 DECL_FUNCTION_CODE (decl) = code;
3267 DECL_CONTEXT (decl) = context;
3271 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
3272 we cannot change DECL_ASSEMBLER_NAME until we have installed this
3273 function in the namespace. */
3275 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
3277 /* Warn if a function in the namespace for users
3278 is used without an occasion to consider it declared. */
3279 if (name[0] != '_' || name[1] != '_')
3280 DECL_ANTICIPATED (decl) = 1;
3282 /* Possibly apply some default attributes to this built-in function. */
3284 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
3286 decl_attributes (&decl, NULL_TREE, 0);
3291 /* Entry point for the benefit of c_common_nodes_and_builtins.
3293 Make a definition for a builtin function named NAME and whose data type
3294 is TYPE. TYPE should be a function type with argument types. This
3295 function places the anticipated declaration in the global namespace
3296 and additionally in the std namespace if appropriate.
3298 CLASS and CODE tell later passes how to compile calls to this function.
3299 See tree.h for possible values.
3301 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3302 the name to be called if we can't opencode the function.
3304 If ATTRS is nonzero, use that for the function's attribute
3308 builtin_function (const char* name,
3311 enum built_in_class cl,
3312 const char* libname,
3315 /* All builtins that don't begin with an '_' should additionally
3316 go in the 'std' namespace. */
3319 push_namespace (std_identifier);
3320 builtin_function_1 (name, type, std_node, code, cl, libname, attrs);
3324 return builtin_function_1 (name, type, NULL_TREE, code,
3325 cl, libname, attrs);
3328 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3329 function. Not called directly. */
3332 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3334 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3335 DECL_EXTERNAL (fn) = 1;
3336 TREE_PUBLIC (fn) = 1;
3337 DECL_ARTIFICIAL (fn) = 1;
3338 TREE_NOTHROW (fn) = 1;
3339 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3340 SET_DECL_LANGUAGE (fn, lang_c);
3341 /* Runtime library routines are, by definition, available in an
3342 external shared object. */
3343 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3344 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3348 /* Returns the _DECL for a library function with C linkage.
3349 We assume that such functions never throw; if this is incorrect,
3350 callers should unset TREE_NOTHROW. */
3353 build_library_fn (tree name, tree type)
3355 return build_library_fn_1 (name, ERROR_MARK, type);
3358 /* Returns the _DECL for a library function with C++ linkage. */
3361 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3363 tree fn = build_library_fn_1 (name, operator_code, type);
3364 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3365 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3366 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3370 /* Like build_library_fn, but takes a C string instead of an
3374 build_library_fn_ptr (const char* name, tree type)
3376 return build_library_fn (get_identifier (name), type);
3379 /* Like build_cp_library_fn, but takes a C string instead of an
3383 build_cp_library_fn_ptr (const char* name, tree type)
3385 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3388 /* Like build_library_fn, but also pushes the function so that we will
3389 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3392 push_library_fn (tree name, tree type)
3394 tree fn = build_library_fn (name, type);
3395 pushdecl_top_level (fn);
3399 /* Like build_cp_library_fn, but also pushes the function so that it
3400 will be found by normal lookup. */
3403 push_cp_library_fn (enum tree_code operator_code, tree type)
3405 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3412 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3416 push_void_library_fn (tree name, tree parmtypes)
3418 tree type = build_function_type (void_type_node, parmtypes);
3419 return push_library_fn (name, type);
3422 /* Like push_library_fn, but also note that this function throws
3423 and does not return. Used for __throw_foo and the like. */
3426 push_throw_library_fn (tree name, tree type)
3428 tree fn = push_library_fn (name, type);
3429 TREE_THIS_VOLATILE (fn) = 1;
3430 TREE_NOTHROW (fn) = 0;
3434 /* When we call finish_struct for an anonymous union, we create
3435 default copy constructors and such. But, an anonymous union
3436 shouldn't have such things; this function undoes the damage to the
3437 anonymous union type T.
3439 (The reason that we create the synthesized methods is that we don't
3440 distinguish `union { int i; }' from `typedef union { int i; } U'.
3441 The first is an anonymous union; the second is just an ordinary
3445 fixup_anonymous_aggr (tree t)
3449 /* Wipe out memory of synthesized methods. */
3450 TYPE_HAS_CONSTRUCTOR (t) = 0;
3451 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3452 TYPE_HAS_INIT_REF (t) = 0;
3453 TYPE_HAS_CONST_INIT_REF (t) = 0;
3454 TYPE_HAS_ASSIGN_REF (t) = 0;
3455 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3457 /* Splice the implicitly generated functions out of the TYPE_METHODS
3459 q = &TYPE_METHODS (t);
3462 if (DECL_ARTIFICIAL (*q))
3463 *q = TREE_CHAIN (*q);
3465 q = &TREE_CHAIN (*q);
3468 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3469 if (TYPE_METHODS (t))
3470 error ("%Jan anonymous union cannot have function members",
3471 TYPE_MAIN_DECL (t));
3473 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3474 assignment operators (because they cannot have these methods themselves).
3475 For anonymous unions this is already checked because they are not allowed
3476 in any union, otherwise we have to check it. */
3477 if (TREE_CODE (t) != UNION_TYPE)
3481 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3482 if (TREE_CODE (field) == FIELD_DECL)
3484 type = TREE_TYPE (field);
3485 if (CLASS_TYPE_P (type))
3487 if (TYPE_NEEDS_CONSTRUCTING (type))
3488 cp_error_at ("member %q#D with constructor not allowed "
3489 "in anonymous aggregate",
3491 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3492 cp_error_at ("member %q#D with destructor not allowed "
3493 "in anonymous aggregate",
3495 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3496 cp_error_at ("member %q#D with copy assignment operator "
3497 "not allowed in anonymous aggregate",
3504 /* Make sure that a declaration with no declarator is well-formed, i.e.
3505 just declares a tagged type or anonymous union.
3507 Returns the type declared; or NULL_TREE if none. */
3510 check_tag_decl (cp_decl_specifier_seq *declspecs)
3512 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3513 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3514 /* If a class, struct, or enum type is declared by the DECLSPECS
3515 (i.e, if a class-specifier, enum-specifier, or non-typename
3516 elaborated-type-specifier appears in the DECLSPECS),
3517 DECLARED_TYPE is set to the corresponding type. */
3518 tree declared_type = NULL_TREE;
3519 bool error_p = false;
3521 if (declspecs->multiple_types_p)
3522 error ("multiple types in one declaration");
3523 else if (declspecs->redefined_builtin_type)
3525 if (!in_system_header)
3526 pedwarn ("redeclaration of C++ built-in type %qT",
3527 declspecs->redefined_builtin_type);
3532 && TYPE_P (declspecs->type)
3533 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
3534 && IS_AGGR_TYPE (declspecs->type))
3535 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3536 declared_type = declspecs->type;
3537 else if (declspecs->type == error_mark_node)
3539 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
3540 pedwarn ("declaration does not declare anything");
3541 /* Check for an anonymous union. */
3542 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3543 && TYPE_ANONYMOUS_P (declared_type))
3545 /* 7/3 In a simple-declaration, the optional init-declarator-list
3546 can be omitted only when declaring a class (clause 9) or
3547 enumeration (7.2), that is, when the decl-specifier-seq contains
3548 either a class-specifier, an elaborated-type-specifier with
3549 a class-key (9.1), or an enum-specifier. In these cases and
3550 whenever a class-specifier or enum-specifier is present in the
3551 decl-specifier-seq, the identifiers in these specifiers are among
3552 the names being declared by the declaration (as class-name,
3553 enum-names, or enumerators, depending on the syntax). In such
3554 cases, and except for the declaration of an unnamed bit-field (9.6),
3555 the decl-specifier-seq shall introduce one or more names into the
3556 program, or shall redeclare a name introduced by a previous
3557 declaration. [Example:
3558 enum { }; // ill-formed
3559 typedef class { }; // ill-formed
3563 error ("missing type-name in typedef-declaration");
3566 /* Anonymous unions are objects, so they can have specifiers. */;
3567 SET_ANON_AGGR_TYPE_P (declared_type);
3569 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
3570 && !in_system_header)
3571 pedwarn ("ISO C++ prohibits anonymous structs");
3576 if (declspecs->specs[(int)ds_inline]
3577 || declspecs->specs[(int)ds_virtual])
3578 error ("%qs can only be specified for functions",
3579 declspecs->specs[(int)ds_inline]
3580 ? "inline" : "virtual");
3582 && (!current_class_type
3583 || current_scope () != current_class_type))
3584 error ("%<friend%> can only be specified inside a class");
3585 else if (declspecs->specs[(int)ds_explicit])
3586 error ("%<explicit%> can only be specified for constructors");
3587 else if (declspecs->storage_class)
3588 error ("a storage class can only be specified for objects "
3590 else if (declspecs->specs[(int)ds_const]
3591 || declspecs->specs[(int)ds_volatile]
3592 || declspecs->specs[(int)ds_restrict]
3593 || declspecs->specs[(int)ds_thread])
3594 error ("qualifiers can only be specified for objects "
3598 return declared_type;
3601 /* Called when a declaration is seen that contains no names to declare.
3602 If its type is a reference to a structure, union or enum inherited
3603 from a containing scope, shadow that tag name for the current scope
3604 with a forward reference.
3605 If its type defines a new named structure or union
3606 or defines an enum, it is valid but we need not do anything here.
3607 Otherwise, it is an error.
3609 C++: may have to grok the declspecs to learn about static,
3610 complain for anonymous unions.
3612 Returns the TYPE declared -- or NULL_TREE if none. */
3615 shadow_tag (cp_decl_specifier_seq *declspecs)
3617 tree t = check_tag_decl (declspecs);
3622 if (declspecs->attributes)
3624 cp_warning_at ("attribute ignored in declaration of %q#T", t);
3625 cp_warning_at ("attribute for %q#T must follow the %qs keyword",
3627 class_key_or_enum_as_string (t));
3631 maybe_process_partial_specialization (t);
3633 /* This is where the variables in an anonymous union are
3634 declared. An anonymous union declaration looks like:
3636 because there is no declarator after the union, the parser
3637 sends that declaration here. */
3638 if (ANON_AGGR_TYPE_P (t))
3640 fixup_anonymous_aggr (t);
3642 if (TYPE_FIELDS (t))
3644 tree decl = grokdeclarator (/*declarator=*/NULL,
3645 declspecs, NORMAL, 0, NULL);
3646 finish_anon_union (decl);
3653 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3656 groktypename (cp_decl_specifier_seq *type_specifiers,
3657 const cp_declarator *declarator)
3661 attrs = type_specifiers->attributes;
3662 type_specifiers->attributes = NULL_TREE;
3663 type = grokdeclarator (declarator, type_specifiers, TYPENAME, 0, &attrs);
3665 cplus_decl_attributes (&type, attrs, 0);
3669 /* Decode a declarator in an ordinary declaration or data definition.
3670 This is called as soon as the type information and variable name
3671 have been parsed, before parsing the initializer if any.
3672 Here we create the ..._DECL node, fill in its type,
3673 and put it on the list of decls for the current context.
3674 The ..._DECL node is returned as the value.
3676 Exception: for arrays where the length is not specified,
3677 the type is left null, to be filled in by `cp_finish_decl'.
3679 Function definitions do not come here; they go to start_function
3680 instead. However, external and forward declarations of functions
3681 do go through here. Structure field declarations are done by
3682 grokfield and not through here. */
3685 start_decl (const cp_declarator *declarator,
3686 cp_decl_specifier_seq *declspecs,
3689 tree prefix_attributes,
3696 *pop_scope_p = false;
3698 /* This should only be done once on the top most decl. */
3699 if (have_extern_spec)
3701 declspecs->storage_class = sc_extern;
3702 have_extern_spec = false;
3705 /* An object declared as __attribute__((deprecated)) suppresses
3706 warnings of uses of other deprecated items. */
3707 if (lookup_attribute ("deprecated", attributes))
3708 deprecated_state = DEPRECATED_SUPPRESS;
3710 attributes = chainon (attributes, prefix_attributes);
3712 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
3715 deprecated_state = DEPRECATED_NORMAL;
3717 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
3718 return error_mark_node;
3720 type = TREE_TYPE (decl);
3722 if (type == error_mark_node)
3723 return error_mark_node;
3725 context = DECL_CONTEXT (decl);
3728 *pop_scope_p = push_scope (context);
3730 /* We are only interested in class contexts, later. */
3731 if (context && TREE_CODE (context) == NAMESPACE_DECL)
3732 context = NULL_TREE;
3735 /* Is it valid for this decl to have an initializer at all?
3736 If not, set INITIALIZED to zero, which will indirectly
3737 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
3738 switch (TREE_CODE (decl))
3741 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3746 error ("function %q#D is initialized like a variable", decl);
3756 if (! toplevel_bindings_p ()
3757 && DECL_EXTERNAL (decl))
3758 warning ("declaration of %q#D has %<extern%> and is initialized",
3760 DECL_EXTERNAL (decl) = 0;
3761 if (toplevel_bindings_p ())
3762 TREE_STATIC (decl) = 1;
3764 /* Tell `pushdecl' this is an initialized decl
3765 even though we don't yet have the initializer expression.
3766 Also tell `cp_finish_decl' it may store the real initializer. */
3767 DECL_INITIAL (decl) = error_mark_node;
3770 /* Set attributes here so if duplicate decl, will have proper attributes. */
3771 cplus_decl_attributes (&decl, attributes, 0);
3773 /* If #pragma weak was used, mark the decl weak now. */
3774 if (global_scope_p (current_binding_level))
3775 maybe_apply_pragma_weak (decl);
3777 if (TREE_CODE (decl) == FUNCTION_DECL
3778 && DECL_DECLARED_INLINE_P (decl)
3779 && DECL_UNINLINABLE (decl)
3780 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3781 warning ("%Jinline function %qD given attribute noinline", decl, decl);
3783 if (context && COMPLETE_TYPE_P (complete_type (context)))
3785 if (TREE_CODE (decl) == VAR_DECL)
3787 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
3788 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
3789 error ("%q#D is not a static member of %q#T", decl, context);
3792 if (DECL_CONTEXT (field) != context)
3794 if (!same_type_p (DECL_CONTEXT (field), context))
3795 pedwarn ("ISO C++ does not permit %<%T::%D%> "
3796 "to be defined as %<%T::%D%>",
3797 DECL_CONTEXT (field), DECL_NAME (decl),
3798 context, DECL_NAME (decl));
3799 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
3801 if (processing_specialization
3802 && template_class_depth (context) == 0
3803 && CLASSTYPE_TEMPLATE_SPECIALIZATION (context))
3804 error ("template header not allowed in member definition "
3805 "of explicitly specialized class");
3806 /* Static data member are tricky; an in-class initialization
3807 still doesn't provide a definition, so the in-class
3808 declaration will have DECL_EXTERNAL set, but will have an
3809 initialization. Thus, duplicate_decls won't warn
3810 about this situation, and so we check here. */
3811 if (DECL_INITIAL (decl) && DECL_INITIAL (field))
3812 error ("duplicate initialization of %qD", decl);
3813 if (duplicate_decls (decl, field))
3819 tree field = check_classfn (context, decl,
3820 (processing_template_decl
3821 > template_class_depth (context))
3822 ? current_template_parms
3824 if (field && duplicate_decls (decl, field))
3828 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
3829 DECL_IN_AGGR_P (decl) = 0;
3830 if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
3831 || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
3833 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
3834 /* [temp.expl.spec] An explicit specialization of a static data
3835 member of a template is a definition if the declaration
3836 includes an initializer; otherwise, it is a declaration.
3838 We check for processing_specialization so this only applies
3839 to the new specialization syntax. */
3840 if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
3841 DECL_EXTERNAL (decl) = 1;
3844 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
3845 pedwarn ("declaration of %q#D outside of class is not definition",
3849 /* Enter this declaration into the symbol table. */
3850 tem = maybe_push_decl (decl);
3852 if (processing_template_decl)
3853 tem = push_template_decl (tem);
3854 if (tem == error_mark_node)
3855 return error_mark_node;
3857 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
3858 /* Tell the back-end to use or not use .common as appropriate. If we say
3859 -fconserve-space, we want this to save .data space, at the expense of
3860 wrong semantics. If we say -fno-conserve-space, we want this to
3861 produce errors about redefs; to do this we force variables into the
3863 DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
3864 || !DECL_THREAD_LOCAL (tem))
3865 && (flag_conserve_space || ! TREE_PUBLIC (tem)));
3868 if (! processing_template_decl)
3875 start_decl_1 (tree decl)
3877 tree type = TREE_TYPE (decl);
3878 int initialized = (DECL_INITIAL (decl) != NULL_TREE);
3880 if (type == error_mark_node)
3884 /* Is it valid for this decl to have an initializer at all?
3885 If not, set INITIALIZED to zero, which will indirectly
3886 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
3888 /* Don't allow initializations for incomplete types except for
3889 arrays which might be completed by the initialization. */
3890 if (COMPLETE_TYPE_P (complete_type (type)))
3891 ; /* A complete type is ok. */
3892 else if (TREE_CODE (type) != ARRAY_TYPE)
3894 error ("variable %q#D has initializer but incomplete type", decl);
3896 type = TREE_TYPE (decl) = error_mark_node;
3898 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
3900 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
3901 error ("elements of array %q#D have incomplete type", decl);
3902 /* else we already gave an error in start_decl. */
3908 && TREE_CODE (decl) != TYPE_DECL
3909 && TREE_CODE (decl) != TEMPLATE_DECL
3910 && type != error_mark_node
3911 && IS_AGGR_TYPE (type)
3912 && ! DECL_EXTERNAL (decl))
3914 if ((! processing_template_decl || ! uses_template_parms (type))
3915 && !COMPLETE_TYPE_P (complete_type (type)))
3917 error ("aggregate %q#D has incomplete type and cannot be defined",
3919 /* Change the type so that assemble_variable will give
3920 DECL an rtl we can live with: (mem (const_int 0)). */
3921 type = TREE_TYPE (decl) = error_mark_node;
3925 /* If any base type in the hierarchy of TYPE needs a constructor,
3926 then we set initialized to 1. This way any nodes which are
3927 created for the purposes of initializing this aggregate
3928 will live as long as it does. This is necessary for global
3929 aggregates which do not have their initializers processed until
3930 the end of the file. */
3931 initialized = TYPE_NEEDS_CONSTRUCTING (type);
3936 DECL_INITIAL (decl) = NULL_TREE;
3938 /* Create a new scope to hold this declaration if necessary.
3939 Whether or not a new scope is necessary cannot be determined
3940 until after the type has been completed; if the type is a
3941 specialization of a class template it is not until after
3942 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
3943 will be set correctly. */
3944 maybe_push_cleanup_level (type);
3947 /* Handle initialization of references. DECL, TYPE, and INIT have the
3948 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
3949 but will be set to a new CLEANUP_STMT if a temporary is created
3950 that must be destroyed subsequently.
3952 Returns an initializer expression to use to initialize DECL, or
3953 NULL if the initialization can be performed statically.
3955 Quotes on semantics can be found in ARM 8.4.3. */
3958 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
3962 if (init == NULL_TREE)
3964 if ((DECL_LANG_SPECIFIC (decl) == 0
3965 || DECL_IN_AGGR_P (decl) == 0)
3966 && ! DECL_THIS_EXTERN (decl))
3967 error ("%qD declared as reference but not initialized", decl);
3971 if (TREE_CODE (init) == CONSTRUCTOR)
3973 error ("ISO C++ forbids use of initializer list to "
3974 "initialize reference %qD", decl);
3978 if (TREE_CODE (init) == TREE_LIST)
3979 init = build_x_compound_expr_from_list (init, "initializer");
3981 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
3982 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
3983 /* Note: default conversion is only called in very special cases. */
3984 init = decay_conversion (init);
3986 /* Convert INIT to the reference type TYPE. This may involve the
3987 creation of a temporary, whose lifetime must be the same as that
3988 of the reference. If so, a DECL_EXPR for the temporary will be
3989 added just after the DECL_EXPR for DECL. That's why we don't set
3990 DECL_INITIAL for local references (instead assigning to them
3991 explicitly); we need to allow the temporary to be initialized
3993 tmp = initialize_reference (type, init, decl, cleanup);
3995 if (tmp == error_mark_node)
3997 else if (tmp == NULL_TREE)
3999 error ("cannot initialize %qT from %qT", type, TREE_TYPE (init));
4003 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
4006 DECL_INITIAL (decl) = tmp;
4011 /* When parsing `int a[] = {1, 2};' we don't know the size of the
4012 array until we finish parsing the initializer. If that's the
4013 situation we're in, update DECL accordingly. */
4016 maybe_deduce_size_from_array_init (tree decl, tree init)
4018 tree type = TREE_TYPE (decl);
4020 if (TREE_CODE (type) == ARRAY_TYPE
4021 && TYPE_DOMAIN (type) == NULL_TREE
4022 && TREE_CODE (decl) != TYPE_DECL)
4024 /* do_default is really a C-ism to deal with tentative definitions.
4025 But let's leave it here to ease the eventual merge. */
4026 int do_default = !DECL_EXTERNAL (decl);
4027 tree initializer = init ? init : DECL_INITIAL (decl);
4028 int failure = complete_array_type (type, initializer, do_default);
4031 error ("initializer fails to determine size of %qD", decl);
4036 error ("array size missing in %qD", decl);
4037 /* If a `static' var's size isn't known, make it extern as
4038 well as static, so it does not get allocated. If it's not
4039 `static', then don't mark it extern; finish_incomplete_decl
4040 will give it a default size and it will get allocated. */
4041 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4042 DECL_EXTERNAL (decl) = 1;
4045 if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
4046 && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4048 error ("zero-size array %qD", decl);
4050 layout_decl (decl, 0);
4054 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
4055 any appropriate error messages regarding the layout. */
4058 layout_var_decl (tree decl)
4060 tree type = TREE_TYPE (decl);
4062 tree ttype = target_type (type);
4065 /* If we haven't already layed out this declaration, do so now.
4066 Note that we must not call complete type for an external object
4067 because it's type might involve templates that we are not
4068 supposed to instantiate yet. (And it's perfectly valid to say
4069 `extern X x' for some incomplete type `X'.) */
4070 if (!DECL_EXTERNAL (decl))
4071 complete_type (type);
4072 if (!DECL_SIZE (decl)
4073 && TREE_TYPE (decl) != error_mark_node
4074 && (COMPLETE_TYPE_P (type)
4075 || (TREE_CODE (type) == ARRAY_TYPE
4076 && !TYPE_DOMAIN (type)
4077 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
4078 layout_decl (decl, 0);
4080 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
4082 /* An automatic variable with an incomplete type: that is an error.
4083 Don't talk about array types here, since we took care of that
4084 message in grokdeclarator. */
4085 error ("storage size of %qD isn't known", decl);
4086 TREE_TYPE (decl) = error_mark_node;
4089 /* Keep this code around in case we later want to control debug info
4090 based on whether a type is "used". (jason 1999-11-11) */
4092 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
4093 /* Let debugger know it should output info for this type. */
4094 note_debug_info_needed (ttype);
4096 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4097 note_debug_info_needed (DECL_CONTEXT (decl));
4100 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4101 && DECL_SIZE (decl) != NULL_TREE
4102 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4104 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4105 constant_expression_warning (DECL_SIZE (decl));
4107 error ("storage size of %qD isn't constant", decl);
4110 if (TREE_STATIC (decl)
4111 && !DECL_ARTIFICIAL (decl)
4112 && current_function_decl
4113 && DECL_CONTEXT (decl) == current_function_decl)
4114 push_local_name (decl);
4117 /* If a local static variable is declared in an inline function, or if
4118 we have a weak definition, we must endeavor to create only one
4119 instance of the variable at link-time. */
4122 maybe_commonize_var (tree decl)
4124 /* Static data in a function with comdat linkage also has comdat
4126 if (TREE_STATIC (decl)
4127 /* Don't mess with __FUNCTION__. */
4128 && ! DECL_ARTIFICIAL (decl)
4129 && DECL_FUNCTION_SCOPE_P (decl)
4130 /* Unfortunately, import_export_decl has not always been called
4131 before the function is processed, so we cannot simply check
4133 && (DECL_COMDAT (DECL_CONTEXT (decl))
4134 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
4135 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
4136 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
4140 /* With weak symbols, we simply make the variable COMDAT;
4141 that will cause copies in multiple translations units to