decl.c revision 220150
1132747Skan/* Process declarations and variables for C++ compiler. 290287Sobrien Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3169699Skan 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. 451412Sobrien Contributed by Michael Tiemann (tiemann@cygnus.com) 518334Speter 6132747SkanThis file is part of GCC. 718334Speter 8132747SkanGCC is free software; you can redistribute it and/or modify 918334Speterit under the terms of the GNU General Public License as published by 1018334Speterthe Free Software Foundation; either version 2, or (at your option) 1118334Speterany later version. 1218334Speter 13132747SkanGCC is distributed in the hope that it will be useful, 1418334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of 1518334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1618334SpeterGNU General Public License for more details. 1718334Speter 1818334SpeterYou should have received a copy of the GNU General Public License 19132747Skanalong with GCC; see the file COPYING. If not, write to 20169699Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor, 21169699SkanBoston, MA 02110-1301, USA. */ 2218334Speter 2318334Speter 24132747Skan/* Process declarations and symbol lookup for C++ front end. 2518334Speter Also constructs types; the standard scalar types at initialization, 2618334Speter and structure, union, array and enum types when they are declared. */ 2718334Speter 2818334Speter/* ??? not all decl nodes are given the most useful possible 2918334Speter line numbers. For example, the CONST_DECLs for enum values. */ 3018334Speter 3118334Speter#include "config.h" 3251412Sobrien#include "system.h" 33132747Skan#include "coretypes.h" 34132747Skan#include "tm.h" 3518334Speter#include "tree.h" 3618334Speter#include "rtl.h" 3790287Sobrien#include "expr.h" 3818334Speter#include "flags.h" 3918334Speter#include "cp-tree.h" 4090287Sobrien#include "tree-inline.h" 4118334Speter#include "decl.h" 4251412Sobrien#include "output.h" 4351412Sobrien#include "except.h" 4451412Sobrien#include "toplev.h" 45117410Skan#include "hashtab.h" 4690287Sobrien#include "tm_p.h" 4790287Sobrien#include "target.h" 4890287Sobrien#include "c-common.h" 4996292Sobrien#include "c-pragma.h" 5090287Sobrien#include "diagnostic.h" 51117410Skan#include "debug.h" 52117410Skan#include "timevar.h" 53169699Skan#include "tree-flow.h" 5418334Speter 55169699Skanstatic tree grokparms (cp_parameter_declarator *, tree *); 56132747Skanstatic const char *redeclaration_error_message (tree, tree); 5790288Sobrien 58132747Skanstatic int decl_jump_unsafe (tree); 59132747Skanstatic void require_complete_types_for_parms (tree); 60132747Skanstatic int ambi_op_p (enum tree_code); 61132747Skanstatic int unary_op_p (enum tree_code); 62132747Skanstatic void push_local_name (tree); 63132747Skanstatic tree grok_reference_init (tree, tree, tree, tree *); 64169699Skanstatic tree grokvardecl (tree, tree, const cp_decl_specifier_seq *, 65169699Skan int, int, tree); 66132747Skanstatic void record_unknown_type (tree, const char *); 67169699Skanstatic tree builtin_function_1 (const char *, tree, tree, 68169699Skan enum built_in_function code, 69169699Skan enum built_in_class cl, const char *, 70132747Skan tree); 71132747Skanstatic tree build_library_fn_1 (tree, enum tree_code, tree); 72132747Skanstatic int member_function_or_else (tree, tree, enum overload_flags); 73132747Skanstatic void bad_specifiers (tree, const char *, int, int, int, int, 74132747Skan int); 75132747Skanstatic void check_for_uninitialized_const_var (tree); 76132747Skanstatic hashval_t typename_hash (const void *); 77132747Skanstatic int typename_compare (const void *, const void *); 78132747Skanstatic tree local_variable_p_walkfn (tree *, int *, void *); 79132747Skanstatic tree record_builtin_java_type (const char *, int); 80169699Skanstatic const char *tag_name (enum tag_types); 81169699Skanstatic tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool); 82132747Skanstatic int walk_namespaces_r (tree, walk_namespaces_fn, void *); 83132747Skanstatic void maybe_deduce_size_from_array_init (tree, tree); 84132747Skanstatic void layout_var_decl (tree); 85132747Skanstatic void maybe_commonize_var (tree); 86122192Skanstatic tree check_initializer (tree, tree, int, tree *); 87132747Skanstatic void make_rtl_for_nonlocal_decl (tree, tree, const char *); 88132747Skanstatic void save_function_data (tree); 89132747Skanstatic void check_function_type (tree, tree); 90132747Skanstatic void finish_constructor_body (void); 91132747Skanstatic void begin_destructor_body (void); 92132747Skanstatic void finish_destructor_body (void); 93132747Skanstatic tree create_array_type_for_decl (tree, tree, tree); 94132747Skanstatic tree get_atexit_node (void); 95132747Skanstatic tree get_dso_handle_node (void); 96132747Skanstatic tree start_cleanup_fn (void); 97132747Skanstatic void end_cleanup_fn (void); 98132747Skanstatic tree cp_make_fname_decl (tree, int); 99132747Skanstatic void initialize_predefined_identifiers (void); 100169699Skanstatic tree check_special_function_return_type 101132747Skan (special_function_kind, tree, tree); 102132747Skanstatic tree push_cp_library_fn (enum tree_code, tree); 103132747Skanstatic tree build_cp_library_fn (tree, enum tree_code, tree); 104132747Skanstatic void store_parm_decls (tree); 105117410Skanstatic void initialize_local_var (tree, tree); 106117410Skanstatic void expand_static_init (tree, tree); 107117410Skanstatic tree next_initializable_field (tree); 10818334Speter 10990287Sobrien/* The following symbols are subsumed in the cp_global_trees array, and 11090287Sobrien listed here individually for documentation purposes. 11118334Speter 11290287Sobrien C++ extensions 11390287Sobrien tree wchar_decl_node; 11418334Speter 11590287Sobrien tree vtable_entry_type; 11690287Sobrien tree delta_type_node; 11790287Sobrien tree __t_desc_type_node; 11818334Speter 119132747Skan tree class_type_node; 12090287Sobrien tree unknown_type_node; 12118334Speter 12290287Sobrien Array type `vtable_entry_type[]' 12318334Speter 12490287Sobrien tree vtbl_type_node; 12590287Sobrien tree vtbl_ptr_type_node; 12618334Speter 12790287Sobrien Namespaces, 12818334Speter 12990287Sobrien tree std_node; 13090287Sobrien tree abi_node; 13151412Sobrien 13290287Sobrien A FUNCTION_DECL which can call `abort'. Not necessarily the 13390287Sobrien one that the user will declare, but sufficient to be called 13490287Sobrien by routines that want to abort the program. 13518334Speter 13690287Sobrien tree abort_fndecl; 13718334Speter 13890287Sobrien The FUNCTION_DECL for the default `::operator delete'. 13918334Speter 14090287Sobrien tree global_delete_fndecl; 14151412Sobrien 14290287Sobrien Used by RTTI 14390287Sobrien tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; 144169699Skan tree tinfo_var_id; */ 14518334Speter 14690287Sobrientree cp_global_trees[CPTI_MAX]; 14718334Speter 14851412Sobrien/* Indicates that there is a type value in some namespace, although 14990287Sobrien that is not necessarily in scope at the moment. */ 15051412Sobrien 151132747Skantree global_type_node; 15251412Sobrien 153132747Skan/* The node that holds the "name" of the global scope. */ 154132747Skantree global_scope_name; 15551412Sobrien 15690287Sobrien#define local_names cp_function_chain->x_local_names 15751412Sobrien 15818334Speter/* A list of objects which have constructors or destructors 15918334Speter which reside in the global scope. The decl is stored in 16018334Speter the TREE_VALUE slot and the initializer is stored 16118334Speter in the TREE_PURPOSE slot. */ 16218334Spetertree static_aggregates; 16318334Speter 16418334Speter/* -- end of C++ */ 16518334Speter 16690287Sobrien/* A node for the integer constants 2, and 3. */ 16718334Speter 16890287Sobrientree integer_two_node, integer_three_node; 16918334Speter 170169699Skan/* Used only for jumps to as-yet undefined labels, since jumps to 171169699Skan defined labels can have their validity checked immediately. */ 172169699Skan 173169699Skanstruct named_label_use_entry GTY(()) 174169699Skan{ 175169699Skan struct named_label_use_entry *next; 176169699Skan /* The binding level to which this entry is *currently* attached. 177169699Skan This is initially the binding level in which the goto appeared, 178169699Skan but is modified as scopes are closed. */ 179169699Skan struct cp_binding_level *binding_level; 180169699Skan /* The head of the names list that was current when the goto appeared, 181169699Skan or the inner scope popped. These are the decls that will *not* be 182169699Skan skipped when jumping to the label. */ 183169699Skan tree names_in_scope; 184169699Skan /* The location of the goto, for error reporting. */ 185169699Skan location_t o_goto_locus; 186169699Skan /* True if an OpenMP structured block scope has been closed since 187169699Skan the goto appeared. This means that the branch from the label will 188169699Skan illegally exit an OpenMP scope. */ 189169699Skan bool in_omp_scope; 190169699Skan}; 191169699Skan 19290287Sobrien/* A list of all LABEL_DECLs in the function that have names. Here so 19390287Sobrien we can clear out their names' definitions at the end of the 19490287Sobrien function, and so we can check the validity of jumps to these labels. */ 19518334Speter 196169699Skanstruct named_label_entry GTY(()) 19790287Sobrien{ 198169699Skan /* The decl itself. */ 199169699Skan tree label_decl; 200169699Skan 201169699Skan /* The binding level to which the label is *currently* attached. 202169699Skan This is initially set to the binding level in which the label 203169699Skan is defined, but is modified as scopes are closed. */ 204117410Skan struct cp_binding_level *binding_level; 205169699Skan /* The head of the names list that was current when the label was 206169699Skan defined, or the inner scope popped. These are the decls that will 207169699Skan be skipped when jumping to the label. */ 20890287Sobrien tree names_in_scope; 209169699Skan /* A tree list of all decls from all binding levels that would be 210169699Skan crossed by a backward branch to the label. */ 21190287Sobrien tree bad_decls; 212169699Skan 213169699Skan /* A list of uses of the label, before the label is defined. */ 214169699Skan struct named_label_use_entry *uses; 215169699Skan 216169699Skan /* The following bits are set after the label is defined, and are 217169699Skan updated as scopes are popped. They indicate that a backward jump 218169699Skan to the label will illegally enter a scope of the given flavor. */ 219169699Skan bool in_try_scope; 220169699Skan bool in_catch_scope; 221169699Skan bool in_omp_scope; 22290287Sobrien}; 22318334Speter 22490287Sobrien#define named_labels cp_function_chain->x_named_labels 22518334Speter 22690287Sobrien/* The number of function bodies which we are currently processing. 22790287Sobrien (Zero if we are at namespace scope, one inside the body of a 22890287Sobrien function, two inside the body of a function in a local class, etc.) */ 22990287Sobrienint function_depth; 23051412Sobrien 23190287Sobrien/* States indicating how grokdeclarator() should handle declspecs marked 23290287Sobrien with __attribute__((deprecated)). An object declared as 23390287Sobrien __attribute__((deprecated)) suppresses warnings of uses of other 23490287Sobrien deprecated items. */ 235169699Skan 23690287Sobrienenum deprecated_states { 23790287Sobrien DEPRECATED_NORMAL, 23890287Sobrien DEPRECATED_SUPPRESS 23990287Sobrien}; 24018334Speter 24190287Sobrienstatic enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 24290287Sobrien 24318334Speter 24496292Sobrien/* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or 24596292Sobrien UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the 24696292Sobrien time the VAR_DECL was declared, the type was incomplete. */ 24796292Sobrien 248117410Skanstatic GTY(()) tree incomplete_vars; 24918334Speter 25090287Sobrien/* Returns the kind of template specialization we are currently 25190287Sobrien processing, given that it's declaration contained N_CLASS_SCOPES 25290287Sobrien explicit scope qualifications. */ 25390287Sobrien 25490287Sobrientmpl_spec_kind 255132747Skancurrent_tmpl_spec_kind (int n_class_scopes) 25690287Sobrien{ 25790287Sobrien int n_template_parm_scopes = 0; 25890287Sobrien int seen_specialization_p = 0; 25990287Sobrien int innermost_specialization_p = 0; 260117410Skan struct cp_binding_level *b; 26190287Sobrien 26290287Sobrien /* Scan through the template parameter scopes. */ 263169699Skan for (b = current_binding_level; 264169699Skan b->kind == sk_template_parms; 265132747Skan b = b->level_chain) 26690287Sobrien { 26790287Sobrien /* If we see a specialization scope inside a parameter scope, 26890287Sobrien then something is wrong. That corresponds to a declaration 26990287Sobrien like: 27090287Sobrien 27190287Sobrien template <class T> template <> ... 27290287Sobrien 273117410Skan which is always invalid since [temp.expl.spec] forbids the 27490287Sobrien specialization of a class member template if the enclosing 27590287Sobrien class templates are not explicitly specialized as well. */ 276132747Skan if (b->explicit_spec_p) 27790287Sobrien { 27890287Sobrien if (n_template_parm_scopes == 0) 27990287Sobrien innermost_specialization_p = 1; 28090287Sobrien else 28190287Sobrien seen_specialization_p = 1; 28290287Sobrien } 28390287Sobrien else if (seen_specialization_p == 1) 28490287Sobrien return tsk_invalid_member_spec; 28590287Sobrien 28690287Sobrien ++n_template_parm_scopes; 28790287Sobrien } 28890287Sobrien 28990287Sobrien /* Handle explicit instantiations. */ 29090287Sobrien if (processing_explicit_instantiation) 29190287Sobrien { 29290287Sobrien if (n_template_parm_scopes != 0) 29390287Sobrien /* We've seen a template parameter list during an explicit 29490287Sobrien instantiation. For example: 29590287Sobrien 29690287Sobrien template <class T> template void f(int); 29790287Sobrien 29890287Sobrien This is erroneous. */ 29990287Sobrien return tsk_invalid_expl_inst; 30090287Sobrien else 30190287Sobrien return tsk_expl_inst; 30290287Sobrien } 30390287Sobrien 30490287Sobrien if (n_template_parm_scopes < n_class_scopes) 30590287Sobrien /* We've not seen enough template headers to match all the 30690287Sobrien specialized classes present. For example: 30790287Sobrien 308169699Skan template <class T> void R<T>::S<T>::f(int); 30990287Sobrien 310117410Skan This is invalid; there needs to be one set of template 31190287Sobrien parameters for each class. */ 31290287Sobrien return tsk_insufficient_parms; 31390287Sobrien else if (n_template_parm_scopes == n_class_scopes) 31490287Sobrien /* We're processing a non-template declaration (even though it may 31590287Sobrien be a member of a template class.) For example: 31690287Sobrien 317169699Skan template <class T> void S<T>::f(int); 31890287Sobrien 31990287Sobrien The `class T' maches the `S<T>', leaving no template headers 32090287Sobrien corresponding to the `f'. */ 32190287Sobrien return tsk_none; 32290287Sobrien else if (n_template_parm_scopes > n_class_scopes + 1) 32390287Sobrien /* We've got too many template headers. For example: 32490287Sobrien 325169699Skan template <> template <class T> void f (T); 32690287Sobrien 32790287Sobrien There need to be more enclosing classes. */ 32890287Sobrien return tsk_excessive_parms; 32990287Sobrien else 33090287Sobrien /* This must be a template. It's of the form: 33190287Sobrien 332169699Skan template <class T> template <class U> void S<T>::f(U); 33390287Sobrien 33490287Sobrien This is a specialization if the innermost level was a 33590287Sobrien specialization; otherwise it's just a definition of the 33690287Sobrien template. */ 33790287Sobrien return innermost_specialization_p ? tsk_expl_spec : tsk_template; 33818334Speter} 33918334Speter 34090287Sobrien/* Exit the current scope. */ 34190287Sobrien 34290287Sobrienvoid 343132747Skanfinish_scope (void) 34490287Sobrien{ 34590287Sobrien poplevel (0, 0, 0); 34690287Sobrien} 34790287Sobrien 34890287Sobrien/* When a label goes out of scope, check to see if that label was used 34990287Sobrien in a valid manner, and issue any appropriate warnings or errors. */ 35090287Sobrien 35190287Sobrienstatic void 352132747Skanpop_label (tree label, tree old_value) 35390287Sobrien{ 354132747Skan if (!processing_template_decl) 35590287Sobrien { 35690287Sobrien if (DECL_INITIAL (label) == NULL_TREE) 35790287Sobrien { 358132747Skan location_t location; 359132747Skan 360169699Skan error ("label %q+D used but not defined", label); 361169699Skan#ifdef USE_MAPPED_LOCATION 362169699Skan location = input_location; /* FIXME want (input_filename, (line)0) */ 363169699Skan#else 364169699Skan location.file = input_filename; 365132747Skan location.line = 0; 366169699Skan#endif 36790287Sobrien /* Avoid crashing later. */ 368132747Skan define_label (location, DECL_NAME (label)); 36990287Sobrien } 370169699Skan else if (!TREE_USED (label)) 371169699Skan warning (OPT_Wunused_label, "label %q+D defined but not used", label); 37290287Sobrien } 37390287Sobrien 37490287Sobrien SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value); 37590287Sobrien} 37690287Sobrien 37790287Sobrien/* At the end of a function, all labels declared within the function 37890287Sobrien go out of scope. BLOCK is the top-level block for the 37990287Sobrien function. */ 38090287Sobrien 381169699Skanstatic int 382169699Skanpop_labels_1 (void **slot, void *data) 383169699Skan{ 384169699Skan struct named_label_entry *ent = (struct named_label_entry *) *slot; 385169699Skan tree block = (tree) data; 386169699Skan 387169699Skan pop_label (ent->label_decl, NULL_TREE); 388169699Skan 389169699Skan /* Put the labels into the "variables" of the top-level block, 390169699Skan so debugger can see them. */ 391169699Skan TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block); 392169699Skan BLOCK_VARS (block) = ent->label_decl; 393169699Skan 394169699Skan htab_clear_slot (named_labels, slot); 395169699Skan 396169699Skan return 1; 397169699Skan} 398169699Skan 39990287Sobrienstatic void 400132747Skanpop_labels (tree block) 40190287Sobrien{ 402169699Skan if (named_labels) 403169699Skan { 404169699Skan htab_traverse (named_labels, pop_labels_1, block); 405169699Skan named_labels = NULL; 406169699Skan } 407169699Skan} 40890287Sobrien 409169699Skan/* At the end of a block with local labels, restore the outer definition. */ 410169699Skan 411169699Skanstatic void 412169699Skanpop_local_label (tree label, tree old_value) 413169699Skan{ 414169699Skan struct named_label_entry dummy; 415169699Skan void **slot; 416169699Skan 417169699Skan pop_label (label, old_value); 418169699Skan 419169699Skan dummy.label_decl = label; 420169699Skan slot = htab_find_slot (named_labels, &dummy, NO_INSERT); 421169699Skan htab_clear_slot (named_labels, slot); 422169699Skan} 423169699Skan 424169699Skan/* The following two routines are used to interface to Objective-C++. 425169699Skan The binding level is purposely treated as an opaque type. */ 426169699Skan 427169699Skanvoid * 428169699Skanobjc_get_current_scope (void) 429169699Skan{ 430169699Skan return current_binding_level; 431169699Skan} 432169699Skan 433169699Skan/* The following routine is used by the NeXT-style SJLJ exceptions; 434169699Skan variables get marked 'volatile' so as to not be clobbered by 435169699Skan _setjmp()/_longjmp() calls. All variables in the current scope, 436169699Skan as well as parent scopes up to (but not including) ENCLOSING_BLK 437169699Skan shall be thusly marked. */ 438169699Skan 439169699Skanvoid 440169699Skanobjc_mark_locals_volatile (void *enclosing_blk) 441169699Skan{ 442169699Skan struct cp_binding_level *scope; 443169699Skan 444169699Skan for (scope = current_binding_level; 445169699Skan scope && scope != enclosing_blk; 446169699Skan scope = scope->level_chain) 44790287Sobrien { 448169699Skan tree decl; 449169699Skan 450169699Skan for (decl = scope->names; decl; decl = TREE_CHAIN (decl)) 451169699Skan objc_volatilize_decl (decl); 452169699Skan 453169699Skan /* Do not climb up past the current function. */ 454169699Skan if (scope->kind == sk_function_parms) 455169699Skan break; 45690287Sobrien } 457169699Skan} 45890287Sobrien 459169699Skan/* Update data for defined and undefined labels when leaving a scope. */ 460169699Skan 461169699Skanstatic int 462169699Skanpoplevel_named_label_1 (void **slot, void *data) 463169699Skan{ 464169699Skan struct named_label_entry *ent = (struct named_label_entry *) *slot; 465169699Skan struct cp_binding_level *bl = (struct cp_binding_level *) data; 466169699Skan struct cp_binding_level *obl = bl->level_chain; 467169699Skan 468169699Skan if (ent->binding_level == bl) 469169699Skan { 470169699Skan tree decl; 471169699Skan 472169699Skan for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl)) 473169699Skan if (decl_jump_unsafe (decl)) 474169699Skan ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls); 475169699Skan 476169699Skan ent->binding_level = obl; 477169699Skan ent->names_in_scope = obl->names; 478169699Skan switch (bl->kind) 479169699Skan { 480169699Skan case sk_try: 481169699Skan ent->in_try_scope = true; 482169699Skan break; 483169699Skan case sk_catch: 484169699Skan ent->in_catch_scope = true; 485169699Skan break; 486169699Skan case sk_omp: 487169699Skan ent->in_omp_scope = true; 488169699Skan break; 489169699Skan default: 490169699Skan break; 491169699Skan } 492169699Skan } 493169699Skan else if (ent->uses) 494169699Skan { 495169699Skan struct named_label_use_entry *use; 496169699Skan 497169699Skan for (use = ent->uses; use ; use = use->next) 498169699Skan if (use->binding_level == bl) 499169699Skan { 500169699Skan use->binding_level = obl; 501169699Skan use->names_in_scope = obl->names; 502169699Skan if (bl->kind == sk_omp) 503169699Skan use->in_omp_scope = true; 504169699Skan } 505169699Skan } 506169699Skan 507169699Skan return 1; 50890287Sobrien} 50990287Sobrien 51018334Speter/* Exit a binding level. 51118334Speter Pop the level off, and restore the state of the identifier-decl mappings 51218334Speter that were in effect when this level was entered. 51318334Speter 51418334Speter If KEEP == 1, this level had explicit declarations, so 51518334Speter and create a "block" (a BLOCK node) for the level 51618334Speter to record its declarations and subblocks for symbol table output. 51718334Speter 51818334Speter If FUNCTIONBODY is nonzero, this level is the body of a function, 51918334Speter so create a block as if KEEP were set and also clear out all 52018334Speter label names. 52118334Speter 52218334Speter If REVERSE is nonzero, reverse the order of decls before putting 52318334Speter them into the BLOCK. */ 52418334Speter 52518334Spetertree 526132747Skanpoplevel (int keep, int reverse, int functionbody) 52718334Speter{ 528132747Skan tree link; 52918334Speter /* The chain of decls was accumulated in reverse order. 53018334Speter Put it into forward order, just for cleanliness. */ 53118334Speter tree decls; 53218334Speter int tmp = functionbody; 53390287Sobrien int real_functionbody; 53490287Sobrien tree subblocks; 535169699Skan tree block; 53618334Speter tree decl; 53752290Sobrien int leaving_for_scope; 538132747Skan scope_kind kind; 53918334Speter 540117410Skan timevar_push (TV_NAME_LOOKUP); 541169699Skan restart: 542117410Skan 543169699Skan block = NULL_TREE; 54452290Sobrien 545169699Skan gcc_assert (current_binding_level->kind != sk_class); 546169699Skan 547132747Skan real_functionbody = (current_binding_level->kind == sk_cleanup 54890287Sobrien ? ((functionbody = 0), tmp) : functionbody); 54990287Sobrien subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; 55090287Sobrien 551169699Skan gcc_assert (!VEC_length(cp_class_binding, 552169699Skan current_binding_level->class_shadowed)); 55352290Sobrien 55452290Sobrien /* We used to use KEEP == 2 to indicate that the new block should go 55552290Sobrien at the beginning of the list of blocks at this binding level, 55652290Sobrien rather than the end. This hack is no longer used. */ 557169699Skan gcc_assert (keep == 0 || keep == 1); 55852290Sobrien 559132747Skan if (current_binding_level->keep) 56018334Speter keep = 1; 56118334Speter 56290287Sobrien /* Any uses of undefined labels, and any defined labels, now operate 56390287Sobrien under constraints of next binding contour. */ 564169699Skan if (cfun && !functionbody && named_labels) 565169699Skan htab_traverse (named_labels, poplevel_named_label_1, 566169699Skan current_binding_level); 56790287Sobrien 56818334Speter /* Get the decls in the order they were written. 56918334Speter Usually current_binding_level->names is in reverse order. 57018334Speter But parameter decls were previously put in forward order. */ 57118334Speter 57218334Speter if (reverse) 57318334Speter current_binding_level->names 57418334Speter = decls = nreverse (current_binding_level->names); 57518334Speter else 57618334Speter decls = current_binding_level->names; 57718334Speter 57818334Speter /* If there were any declarations or structure tags in that level, 57918334Speter or if this level is a function body, 58018334Speter create a BLOCK to record them for the life of this function. */ 58118334Speter block = NULL_TREE; 58290287Sobrien if (keep == 1 || functionbody) 58318334Speter block = make_node (BLOCK); 58418334Speter if (block != NULL_TREE) 58518334Speter { 58690287Sobrien BLOCK_VARS (block) = decls; 58790287Sobrien BLOCK_SUBBLOCKS (block) = subblocks; 58818334Speter } 58918334Speter 59018334Speter /* In each subblock, record that this is its superior. */ 59118334Speter if (keep >= 0) 59218334Speter for (link = subblocks; link; link = TREE_CHAIN (link)) 59318334Speter BLOCK_SUPERCONTEXT (link) = block; 59418334Speter 59552290Sobrien /* We still support the old for-scope rules, whereby the variables 59652290Sobrien in a for-init statement were in scope after the for-statement 597132747Skan ended. We only use the new rules if flag_new_for_scope is 59852290Sobrien nonzero. */ 59990287Sobrien leaving_for_scope 600132747Skan = current_binding_level->kind == sk_for && flag_new_for_scope == 1; 60118334Speter 602169699Skan /* Before we remove the declarations first check for unused variables. */ 603169699Skan if (warn_unused_variable 604169699Skan && !processing_template_decl) 605169699Skan for (decl = getdecls (); decl; decl = TREE_CHAIN (decl)) 606169699Skan if (TREE_CODE (decl) == VAR_DECL 607169699Skan && ! TREE_USED (decl) 608169699Skan && ! DECL_IN_SYSTEM_HEADER (decl) 609169699Skan && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)) 610169699Skan warning (OPT_Wunused_variable, "unused variable %q+D", decl); 611169699Skan 61252290Sobrien /* Remove declarations for all the DECLs in this level. */ 61352290Sobrien for (link = decls; link; link = TREE_CHAIN (link)) 61418334Speter { 61590287Sobrien if (leaving_for_scope && TREE_CODE (link) == VAR_DECL 616169699Skan && DECL_NAME (link)) 61718334Speter { 618169699Skan tree name = DECL_NAME (link); 619169699Skan cxx_binding *ob; 62052290Sobrien tree ns_binding; 62152290Sobrien 622169699Skan ob = outer_binding (name, 623169699Skan IDENTIFIER_BINDING (name), 624169699Skan /*class_p=*/true); 625169699Skan if (!ob) 626169699Skan ns_binding = IDENTIFIER_NAMESPACE_VALUE (name); 62751412Sobrien else 62852290Sobrien ns_binding = NULL_TREE; 62951412Sobrien 630169699Skan if (ob && ob->scope == current_binding_level->level_chain) 63152290Sobrien /* We have something like: 63290287Sobrien 633169699Skan int i; 634169699Skan for (int i; ;); 63590287Sobrien 63652290Sobrien and we are leaving the `for' scope. There's no reason to 63752290Sobrien keep the binding of the inner `i' in this case. */ 638169699Skan pop_binding (name, link); 639169699Skan else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL)) 640132747Skan || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL)) 64152290Sobrien /* Here, we have something like: 64251412Sobrien 64352290Sobrien typedef int I; 64451412Sobrien 64552290Sobrien void f () { 64652290Sobrien for (int I; ;); 64752290Sobrien } 64852290Sobrien 64952290Sobrien We must pop the for-scope binding so we know what's a 65052290Sobrien type and what isn't. */ 651169699Skan pop_binding (name, link); 65252290Sobrien else 65351412Sobrien { 65452290Sobrien /* Mark this VAR_DECL as dead so that we can tell we left it 65552290Sobrien there only for backward compatibility. */ 65652290Sobrien DECL_DEAD_FOR_LOCAL (link) = 1; 65790287Sobrien 658132747Skan /* Keep track of what should have happened when we 65952290Sobrien popped the binding. */ 660169699Skan if (ob && ob->value) 661169699Skan { 662169699Skan SET_DECL_SHADOWED_FOR_VAR (link, ob->value); 663169699Skan DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1; 664169699Skan } 66552290Sobrien 66652290Sobrien /* Add it to the list of dead variables in the next 66752290Sobrien outermost binding to that we can remove these when we 66852290Sobrien leave that binding. */ 66952290Sobrien current_binding_level->level_chain->dead_vars_from_for 67052290Sobrien = tree_cons (NULL_TREE, link, 67152290Sobrien current_binding_level->level_chain-> 67252290Sobrien dead_vars_from_for); 67352290Sobrien 674117410Skan /* Although we don't pop the cxx_binding, we do clear 675132747Skan its SCOPE since the scope is going away now. */ 676169699Skan IDENTIFIER_BINDING (name)->scope 677169699Skan = current_binding_level->level_chain; 67851412Sobrien } 67951412Sobrien } 68090287Sobrien else 68118334Speter { 682169699Skan tree name; 683169699Skan 68452290Sobrien /* Remove the binding. */ 68552290Sobrien decl = link; 686169699Skan 68752290Sobrien if (TREE_CODE (decl) == TREE_LIST) 68852290Sobrien decl = TREE_VALUE (decl); 689169699Skan name = decl; 690169699Skan 691169699Skan if (TREE_CODE (name) == OVERLOAD) 692169699Skan name = OVL_FUNCTION (name); 693169699Skan 694169699Skan gcc_assert (DECL_P (name)); 695169699Skan pop_binding (DECL_NAME (name), decl); 69618334Speter } 69752290Sobrien } 69818334Speter 69952290Sobrien /* Remove declarations for any `for' variables from inner scopes 70052290Sobrien that we kept around. */ 70152290Sobrien for (link = current_binding_level->dead_vars_from_for; 70252290Sobrien link; link = TREE_CHAIN (link)) 70352290Sobrien pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link)); 70418334Speter 70552290Sobrien /* Restore the IDENTIFIER_TYPE_VALUEs. */ 70652290Sobrien for (link = current_binding_level->type_shadowed; 70752290Sobrien link; link = TREE_CHAIN (link)) 70852290Sobrien SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); 70990287Sobrien 71090287Sobrien /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */ 71190287Sobrien for (link = current_binding_level->shadowed_labels; 71290287Sobrien link; 71390287Sobrien link = TREE_CHAIN (link)) 714169699Skan pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link)); 71590287Sobrien 71652290Sobrien /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs 71752290Sobrien list if a `using' declaration put them there. The debugging 71852290Sobrien back-ends won't understand OVERLOAD, so we remove them here. 71952290Sobrien Because the BLOCK_VARS are (temporarily) shared with 72052290Sobrien CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have 72152290Sobrien popped all the bindings. */ 72252290Sobrien if (block) 72352290Sobrien { 72452290Sobrien tree* d; 72551412Sobrien 72652290Sobrien for (d = &BLOCK_VARS (block); *d; ) 72718334Speter { 72852290Sobrien if (TREE_CODE (*d) == TREE_LIST) 72952290Sobrien *d = TREE_CHAIN (*d); 73052290Sobrien else 73152290Sobrien d = &TREE_CHAIN (*d); 73218334Speter } 73318334Speter } 73418334Speter 73518334Speter /* If the level being exited is the top level of a function, 73618334Speter check over all the labels. */ 73718334Speter if (functionbody) 73818334Speter { 73990287Sobrien /* Since this is the top level block of a function, the vars are 74090287Sobrien the function's parameters. Don't leave them in the BLOCK 74190287Sobrien because they are found in the FUNCTION_DECL instead. */ 74218334Speter BLOCK_VARS (block) = 0; 74390287Sobrien pop_labels (block); 74418334Speter } 74518334Speter 746132747Skan kind = current_binding_level->kind; 747169699Skan if (kind == sk_cleanup) 748169699Skan { 749169699Skan tree stmt; 75018334Speter 751169699Skan /* If this is a temporary binding created for a cleanup, then we'll 752169699Skan have pushed a statement list level. Pop that, create a new 753169699Skan BIND_EXPR for the block, and insert it into the stream. */ 754169699Skan stmt = pop_stmt_list (current_binding_level->statement_list); 755169699Skan stmt = c_build_bind_expr (block, stmt); 756169699Skan add_stmt (stmt); 757169699Skan } 758169699Skan 759132747Skan leave_scope (); 76018334Speter if (functionbody) 76118334Speter DECL_INITIAL (current_function_decl) = block; 76218334Speter else if (block) 76390287Sobrien current_binding_level->blocks 76490287Sobrien = chainon (current_binding_level->blocks, block); 76590287Sobrien 76618334Speter /* If we did not make a block for the level just exited, 76718334Speter any blocks made for inner levels 76818334Speter (since they cannot be recorded as subblocks in that level) 76918334Speter must be carried forward so they will later become subblocks 77018334Speter of something else. */ 77118334Speter else if (subblocks) 77252290Sobrien current_binding_level->blocks 77352290Sobrien = chainon (current_binding_level->blocks, subblocks); 77418334Speter 77590287Sobrien /* Each and every BLOCK node created here in `poplevel' is important 77690287Sobrien (e.g. for proper debugging information) so if we created one 77790287Sobrien earlier, mark it as "used". */ 77890287Sobrien if (block) 77990287Sobrien TREE_USED (block) = 1; 78090287Sobrien 781169699Skan /* All temporary bindings created for cleanups are popped silently. */ 782132747Skan if (kind == sk_cleanup) 783169699Skan goto restart; 78490287Sobrien 785117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block); 78618334Speter} 78718334Speter 78818334Speter/* Insert BLOCK at the end of the list of subblocks of the 78918334Speter current binding level. This is used when a BIND_EXPR is expanded, 79018334Speter to handle the BLOCK node inside the BIND_EXPR. */ 79118334Speter 79218334Spetervoid 793132747Skaninsert_block (tree block) 79418334Speter{ 79518334Speter TREE_USED (block) = 1; 79618334Speter current_binding_level->blocks 79718334Speter = chainon (current_binding_level->blocks, block); 79818334Speter} 79918334Speter 80052290Sobrien/* Walk all the namespaces contained NAMESPACE, including NAMESPACE 80152290Sobrien itself, calling F for each. The DATA is passed to F as well. */ 80252290Sobrien 80352290Sobrienstatic int 804132747Skanwalk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data) 80552290Sobrien{ 80652290Sobrien int result = 0; 807169699Skan tree current = NAMESPACE_LEVEL (namespace)->namespaces; 80852290Sobrien 80952290Sobrien result |= (*f) (namespace, data); 81052290Sobrien 811117410Skan for (; current; current = TREE_CHAIN (current)) 812117410Skan result |= walk_namespaces_r (current, f, data); 81352290Sobrien 81452290Sobrien return result; 81552290Sobrien} 81652290Sobrien 81752290Sobrien/* Walk all the namespaces, calling F for each. The DATA is passed to 81852290Sobrien F as well. */ 81952290Sobrien 82052290Sobrienint 821132747Skanwalk_namespaces (walk_namespaces_fn f, void* data) 82252290Sobrien{ 82352290Sobrien return walk_namespaces_r (global_namespace, f, data); 82452290Sobrien} 82552290Sobrien 82652290Sobrien/* Call wrapup_globals_declarations for the globals in NAMESPACE. If 82752290Sobrien DATA is non-NULL, this is the last time we will call 82852290Sobrien wrapup_global_declarations for this NAMESPACE. */ 82952290Sobrien 83052290Sobrienint 831132747Skanwrapup_globals_for_namespace (tree namespace, void* data) 83252290Sobrien{ 833117410Skan struct cp_binding_level *level = NAMESPACE_LEVEL (namespace); 834169699Skan VEC(tree,gc) *statics = level->static_decls; 835169699Skan tree *vec = VEC_address (tree, statics); 836169699Skan int len = VEC_length (tree, statics); 83752290Sobrien int last_time = (data != 0); 83852290Sobrien 83952290Sobrien if (last_time) 84052290Sobrien { 84152290Sobrien check_global_declarations (vec, len); 842169699Skan emit_debug_global_declarations (vec, len); 84352290Sobrien return 0; 84452290Sobrien } 84552290Sobrien 84652290Sobrien /* Write out any globals that need to be output. */ 847117410Skan return wrapup_global_declarations (vec, len); 84852290Sobrien} 84952290Sobrien 85018334Speter 85190287Sobrien/* In C++, you don't have to write `struct S' to refer to `S'; you 85290287Sobrien can just use `S'. We accomplish this by creating a TYPE_DECL as 85390287Sobrien if the user had written `typedef struct S S'. Create and return 85490287Sobrien the TYPE_DECL for TYPE. */ 85590287Sobrien 85690287Sobrientree 857132747Skancreate_implicit_typedef (tree name, tree type) 85890287Sobrien{ 85990287Sobrien tree decl; 86090287Sobrien 86190287Sobrien decl = build_decl (TYPE_DECL, name, type); 86290287Sobrien DECL_ARTIFICIAL (decl) = 1; 86390287Sobrien /* There are other implicit type declarations, like the one *within* 86490287Sobrien a class that allows you to write `S::S'. We must distinguish 86590287Sobrien amongst these. */ 86690287Sobrien SET_DECL_IMPLICIT_TYPEDEF_P (decl); 86790287Sobrien TYPE_NAME (type) = decl; 86890287Sobrien 86990287Sobrien return decl; 87090287Sobrien} 87190287Sobrien 87290287Sobrien/* Remember a local name for name-mangling purposes. */ 87390287Sobrien 87490287Sobrienstatic void 875132747Skanpush_local_name (tree decl) 87690287Sobrien{ 87790287Sobrien size_t i, nelts; 87890287Sobrien tree t, name; 87990287Sobrien 880117410Skan timevar_push (TV_NAME_LOOKUP); 88190287Sobrien 88290287Sobrien name = DECL_NAME (decl); 88390287Sobrien 884169699Skan nelts = VEC_length (tree, local_names); 88590287Sobrien for (i = 0; i < nelts; i++) 88690287Sobrien { 887169699Skan t = VEC_index (tree, local_names, i); 88890287Sobrien if (DECL_NAME (t) == name) 88990287Sobrien { 89090287Sobrien if (!DECL_LANG_SPECIFIC (decl)) 89190287Sobrien retrofit_lang_decl (decl); 892117410Skan DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1; 89390287Sobrien if (DECL_LANG_SPECIFIC (t)) 89490287Sobrien DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1; 89590287Sobrien else 89690287Sobrien DECL_DISCRIMINATOR (decl) = 1; 89790287Sobrien 898169699Skan VEC_replace (tree, local_names, i, decl); 899132747Skan timevar_pop (TV_NAME_LOOKUP); 900132747Skan return; 90190287Sobrien } 90290287Sobrien } 90390287Sobrien 904169699Skan VEC_safe_push (tree, gc, local_names, decl); 905117410Skan timevar_pop (TV_NAME_LOOKUP); 90690287Sobrien} 90718334Speter 90818334Speter/* Subroutine of duplicate_decls: return truthvalue of whether 90918334Speter or not types of these decls match. 91018334Speter 91118334Speter For C++, we must compare the parameter list so that `int' can match 91218334Speter `int&' in a parameter position, but `int&' is not confused with 91318334Speter `const int&'. */ 91451412Sobrien 91518334Speterint 916132747Skandecls_match (tree newdecl, tree olddecl) 91718334Speter{ 91818334Speter int types_match; 91918334Speter 92052290Sobrien if (newdecl == olddecl) 92152290Sobrien return 1; 92252290Sobrien 92352290Sobrien if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) 92452290Sobrien /* If the two DECLs are not even the same kind of thing, we're not 92552290Sobrien interested in their types. */ 92652290Sobrien return 0; 92752290Sobrien 92852290Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 92918334Speter { 93018334Speter tree f1 = TREE_TYPE (newdecl); 93118334Speter tree f2 = TREE_TYPE (olddecl); 93218334Speter tree p1 = TYPE_ARG_TYPES (f1); 93318334Speter tree p2 = TYPE_ARG_TYPES (f2); 93418334Speter 93590287Sobrien if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 93690287Sobrien && ! (DECL_EXTERN_C_P (newdecl) 93790287Sobrien && DECL_EXTERN_C_P (olddecl))) 93851412Sobrien return 0; 93951412Sobrien 94018334Speter if (TREE_CODE (f1) != TREE_CODE (f2)) 941169699Skan return 0; 94218334Speter 94352290Sobrien if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) 94418334Speter { 94590287Sobrien if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl) 94690287Sobrien && (DECL_BUILT_IN (olddecl) 94790287Sobrien#ifndef NO_IMPLICIT_EXTERN_C 948169699Skan || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl)) 949169699Skan || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl)) 95090287Sobrien#endif 95190287Sobrien )) 95218334Speter { 95318334Speter types_match = self_promoting_args_p (p1); 95418334Speter if (p1 == void_list_node) 95518334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 95618334Speter } 95790287Sobrien#ifndef NO_IMPLICIT_EXTERN_C 95890287Sobrien else if (p1 == NULL_TREE 95990287Sobrien && (DECL_EXTERN_C_P (olddecl) 960169699Skan && DECL_IN_SYSTEM_HEADER (olddecl) 961169699Skan && !DECL_CLASS_SCOPE_P (olddecl)) 96290287Sobrien && (DECL_EXTERN_C_P (newdecl) 963169699Skan && DECL_IN_SYSTEM_HEADER (newdecl) 964169699Skan && !DECL_CLASS_SCOPE_P (newdecl))) 96518334Speter { 96618334Speter types_match = self_promoting_args_p (p2); 96718334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 96818334Speter } 96990287Sobrien#endif 97018334Speter else 97152290Sobrien types_match = compparms (p1, p2); 97218334Speter } 97318334Speter else 97418334Speter types_match = 0; 97518334Speter } 97652290Sobrien else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 97718334Speter { 978132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) 979132747Skan != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))) 980132747Skan return 0; 981132747Skan 98251412Sobrien if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), 98351412Sobrien DECL_TEMPLATE_PARMS (olddecl))) 98451412Sobrien return 0; 98590287Sobrien 98651412Sobrien if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 987132747Skan types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)), 988132747Skan TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))); 98951412Sobrien else 99051412Sobrien types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl), 99151412Sobrien DECL_TEMPLATE_RESULT (newdecl)); 99218334Speter } 99318334Speter else 99418334Speter { 995146906Skan /* Need to check scope for variable declaration (VAR_DECL). 996146906Skan For typedef (TYPE_DECL), scope is ignored. */ 997146906Skan if (TREE_CODE (newdecl) == VAR_DECL 998169699Skan && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 999169699Skan /* [dcl.link] 1000169699Skan Two declarations for an object with C language linkage 1001169699Skan with the same name (ignoring the namespace that qualify 1002169699Skan it) that appear in different namespace scopes refer to 1003169699Skan the same object. */ 1004169699Skan && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl))) 1005146906Skan return 0; 1006146906Skan 100718334Speter if (TREE_TYPE (newdecl) == error_mark_node) 100818334Speter types_match = TREE_TYPE (olddecl) == error_mark_node; 100918334Speter else if (TREE_TYPE (olddecl) == NULL_TREE) 101018334Speter types_match = TREE_TYPE (newdecl) == NULL_TREE; 101118334Speter else if (TREE_TYPE (newdecl) == NULL_TREE) 101218334Speter types_match = 0; 101318334Speter else 101452290Sobrien types_match = comptypes (TREE_TYPE (newdecl), 101552290Sobrien TREE_TYPE (olddecl), 101652290Sobrien COMPARE_REDECLARATION); 101718334Speter } 101818334Speter 101918334Speter return types_match; 102018334Speter} 102118334Speter 102218334Speter/* If NEWDECL is `static' and an `extern' was seen previously, 102390287Sobrien warn about it. OLDDECL is the previous declaration. 102418334Speter 102518334Speter Note that this does not apply to the C++ case of declaring 102618334Speter a variable `extern const' and then later `const'. 102718334Speter 102818334Speter Don't complain about built-in functions, since they are beyond 102918334Speter the user's control. */ 103018334Speter 1031132747Skanvoid 1032132747Skanwarn_extern_redeclared_static (tree newdecl, tree olddecl) 103318334Speter{ 103490287Sobrien tree name; 103590287Sobrien 103690287Sobrien if (TREE_CODE (newdecl) == TYPE_DECL 103790287Sobrien || TREE_CODE (newdecl) == TEMPLATE_DECL 1038132747Skan || TREE_CODE (newdecl) == CONST_DECL 1039132747Skan || TREE_CODE (newdecl) == NAMESPACE_DECL) 104018334Speter return; 104118334Speter 104290287Sobrien /* Don't get confused by static member functions; that's a different 104390287Sobrien use of `static'. */ 104490287Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL 104590287Sobrien && DECL_STATIC_FUNCTION_P (newdecl)) 104690287Sobrien return; 104790287Sobrien 104890287Sobrien /* If the old declaration was `static', or the new one isn't, then 104990287Sobrien then everything is OK. */ 105090287Sobrien if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) 105190287Sobrien return; 105290287Sobrien 105390287Sobrien /* It's OK to declare a builtin function as `static'. */ 105490287Sobrien if (TREE_CODE (olddecl) == FUNCTION_DECL 105590287Sobrien && DECL_ARTIFICIAL (olddecl)) 105690287Sobrien return; 105790287Sobrien 105818334Speter name = DECL_ASSEMBLER_NAME (newdecl); 1059169699Skan pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl); 1060169699Skan pedwarn ("previous declaration of %q+D", olddecl); 106118334Speter} 106218334Speter 1063169699Skan/* NEW_DECL is a redeclaration of OLD_DECL; both are functions or 1064169699Skan function templates. If their exception specifications do not 1065169699Skan match, issue an a diagnostic. */ 1066169699Skan 1067169699Skanstatic void 1068169699Skancheck_redeclaration_exception_specification (tree new_decl, 1069169699Skan tree old_decl) 1070169699Skan{ 1071169699Skan tree new_type; 1072169699Skan tree old_type; 1073169699Skan tree new_exceptions; 1074169699Skan tree old_exceptions; 1075169699Skan 1076169699Skan new_type = TREE_TYPE (new_decl); 1077169699Skan new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type); 1078169699Skan old_type = TREE_TYPE (old_decl); 1079169699Skan old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type); 1080169699Skan 1081169699Skan /* [except.spec] 1082169699Skan 1083169699Skan If any declaration of a function has an exception-specification, 1084169699Skan all declarations, including the definition and an explicit 1085169699Skan specialization, of that function shall have an 1086169699Skan exception-specification with the same set of type-ids. */ 1087169699Skan if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl)) 1088169699Skan && ! DECL_IS_BUILTIN (old_decl) 1089169699Skan && flag_exceptions 1090169699Skan && !comp_except_specs (new_exceptions, old_exceptions, 1091169699Skan /*exact=*/true)) 1092169699Skan { 1093169699Skan error ("declaration of %qF throws different exceptions", new_decl); 1094169699Skan error ("from previous declaration %q+F", old_decl); 1095169699Skan } 1096169699Skan} 1097169699Skan 1098132747Skan/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations. 1099132747Skan If the redeclaration is invalid, a diagnostic is issued, and the 1100132747Skan error_mark_node is returned. Otherwise, OLDDECL is returned. 110118334Speter 1102132747Skan If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is 1103169699Skan returned. 110418334Speter 1105169699Skan NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */ 1106169699Skan 1107132747Skantree 1108169699Skanduplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend) 110918334Speter{ 111018334Speter unsigned olddecl_uid = DECL_UID (olddecl); 1111169699Skan int olddecl_friend = 0, types_match = 0, hidden_friend = 0; 111251412Sobrien int new_defines_function = 0; 1113169699Skan tree new_template; 111418334Speter 111551412Sobrien if (newdecl == olddecl) 1116132747Skan return olddecl; 111718334Speter 111818334Speter types_match = decls_match (newdecl, olddecl); 111918334Speter 112018334Speter /* If either the type of the new decl or the type of the old decl is an 112118334Speter error_mark_node, then that implies that we have already issued an 112218334Speter error (earlier) for some bogus type specification, and in that case, 112318334Speter it is rather pointless to harass the user with yet more error message 112451412Sobrien about the same declaration, so just pretend the types match here. */ 112551412Sobrien if (TREE_TYPE (newdecl) == error_mark_node 112651412Sobrien || TREE_TYPE (olddecl) == error_mark_node) 1127169699Skan return error_mark_node; 112890287Sobrien 112990287Sobrien if (DECL_P (olddecl) 113090287Sobrien && TREE_CODE (newdecl) == FUNCTION_DECL 113190287Sobrien && TREE_CODE (olddecl) == FUNCTION_DECL 113290287Sobrien && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl))) 113390287Sobrien { 113490287Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 113590287Sobrien && DECL_UNINLINABLE (newdecl) 113690287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 113790287Sobrien /* Already warned elsewhere. */; 113890287Sobrien else if (DECL_DECLARED_INLINE_P (olddecl) 113990287Sobrien && DECL_UNINLINABLE (olddecl) 114090287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 114190287Sobrien /* Already warned. */; 114290287Sobrien else if (DECL_DECLARED_INLINE_P (newdecl) 114390287Sobrien && DECL_UNINLINABLE (olddecl) 114490287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 114590287Sobrien { 1146169699Skan warning (OPT_Wattributes, "function %q+D redeclared as inline", 1147169699Skan newdecl); 1148169699Skan warning (OPT_Wattributes, "previous declaration of %q+D " 1149169699Skan "with attribute noinline", olddecl); 115090287Sobrien } 115190287Sobrien else if (DECL_DECLARED_INLINE_P (olddecl) 115290287Sobrien && DECL_UNINLINABLE (newdecl) 115390287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 115490287Sobrien { 1155169699Skan warning (OPT_Wattributes, "function %q+D redeclared with " 1156169699Skan "attribute noinline", newdecl); 1157169699Skan warning (OPT_Wattributes, "previous declaration of %q+D was inline", 1158169699Skan olddecl); 115990287Sobrien } 116090287Sobrien } 116190287Sobrien 1162117410Skan /* Check for redeclaration and other discrepancies. */ 116351412Sobrien if (TREE_CODE (olddecl) == FUNCTION_DECL 116490287Sobrien && DECL_ARTIFICIAL (olddecl)) 116518334Speter { 1166169699Skan gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl)); 116790287Sobrien if (TREE_CODE (newdecl) != FUNCTION_DECL) 116818334Speter { 1169169699Skan /* Avoid warnings redeclaring built-ins which have not been 1170169699Skan explicitly declared. */ 1171169699Skan if (DECL_ANTICIPATED (olddecl)) 1172169699Skan return NULL_TREE; 117396292Sobrien 117490287Sobrien /* If you declare a built-in or predefined function name as static, 117590287Sobrien the old definition is overridden, but optionally warn this was a 117690287Sobrien bad choice of name. */ 117790287Sobrien if (! TREE_PUBLIC (newdecl)) 117890287Sobrien { 1179169699Skan warning (OPT_Wshadow, "shadowing %s function %q#D", 1180169699Skan DECL_BUILT_IN (olddecl) ? "built-in" : "library", 1181169699Skan olddecl); 118290287Sobrien /* Discard the old built-in function. */ 1183132747Skan return NULL_TREE; 118490287Sobrien } 118590287Sobrien /* If the built-in is not ansi, then programs can override 118690287Sobrien it even globally without an error. */ 118790287Sobrien else if (! DECL_BUILT_IN (olddecl)) 1188169699Skan warning (0, "library function %q#D redeclared as non-function %q#D", 1189169699Skan olddecl, newdecl); 119090287Sobrien else 119190287Sobrien { 1192169699Skan error ("declaration of %q#D", newdecl); 1193169699Skan error ("conflicts with built-in declaration %q#D", 1194169699Skan olddecl); 119590287Sobrien } 1196132747Skan return NULL_TREE; 119718334Speter } 119890287Sobrien else if (!types_match) 119918334Speter { 1200169699Skan /* Avoid warnings redeclaring built-ins which have not been 1201169699Skan explicitly declared. */ 1202169699Skan if (DECL_ANTICIPATED (olddecl)) 1203169699Skan { 1204169699Skan /* Deal with fileptr_type_node. FILE type is not known 1205169699Skan at the time we create the builtins. */ 1206169699Skan tree t1, t2; 1207169699Skan 1208169699Skan for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1209169699Skan t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 1210169699Skan t1 || t2; 1211169699Skan t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) 1212169699Skan if (!t1 || !t2) 1213169699Skan break; 1214169699Skan else if (TREE_VALUE (t2) == fileptr_type_node) 1215169699Skan { 1216169699Skan tree t = TREE_VALUE (t1); 1217169699Skan 1218169699Skan if (TREE_CODE (t) == POINTER_TYPE 1219169699Skan && TYPE_NAME (TREE_TYPE (t)) 1220169699Skan && DECL_NAME (TYPE_NAME (TREE_TYPE (t))) 1221169699Skan == get_identifier ("FILE") 1222169699Skan && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2))) 1223169699Skan { 1224169699Skan tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 1225169699Skan 1226169699Skan TYPE_ARG_TYPES (TREE_TYPE (olddecl)) 1227169699Skan = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); 1228169699Skan types_match = decls_match (newdecl, olddecl); 1229169699Skan if (types_match) 1230169699Skan return duplicate_decls (newdecl, olddecl, 1231169699Skan newdecl_is_friend); 1232169699Skan TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs; 1233169699Skan } 1234169699Skan } 1235169699Skan else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) 1236169699Skan break; 1237169699Skan } 123896292Sobrien else if ((DECL_EXTERN_C_P (newdecl) 1239122192Skan && DECL_EXTERN_C_P (olddecl)) 1240122192Skan || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1241122192Skan TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 124218334Speter { 124390287Sobrien /* A near match; override the builtin. */ 124490287Sobrien 124590287Sobrien if (TREE_PUBLIC (newdecl)) 124618334Speter { 1247169699Skan warning (0, "new declaration %q#D", newdecl); 1248169699Skan warning (0, "ambiguates built-in declaration %q#D", 1249169699Skan olddecl); 125090287Sobrien } 1251169699Skan else 1252169699Skan warning (OPT_Wshadow, "shadowing %s function %q#D", 1253169699Skan DECL_BUILT_IN (olddecl) ? "built-in" : "library", 1254169699Skan olddecl); 125518334Speter } 125690287Sobrien else 125790287Sobrien /* Discard the old built-in function. */ 1258132747Skan return NULL_TREE; 125996292Sobrien 126096292Sobrien /* Replace the old RTL to avoid problems with inlining. */ 1261169699Skan COPY_DECL_RTL (newdecl, olddecl); 126290287Sobrien } 1263169699Skan /* Even if the types match, prefer the new declarations type for 1264169699Skan built-ins which have not been explicitly declared, for 1265169699Skan exception lists, etc... */ 1266117410Skan else if (DECL_ANTICIPATED (olddecl)) 1267132747Skan { 1268132747Skan tree type = TREE_TYPE (newdecl); 1269132747Skan tree attribs = (*targetm.merge_type_attributes) 1270132747Skan (TREE_TYPE (olddecl), type); 127118334Speter 1272132747Skan type = cp_build_type_attribute_variant (type, attribs); 1273132747Skan TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type; 1274132747Skan } 1275132747Skan 1276122192Skan /* Whether or not the builtin can throw exceptions has no 1277122192Skan bearing on this declarator. */ 1278122192Skan TREE_NOTHROW (olddecl) = 0; 1279122192Skan 128090287Sobrien if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) 128190287Sobrien { 128290287Sobrien /* If a builtin function is redeclared as `static', merge 128390287Sobrien the declarations, but make the original one static. */ 128490287Sobrien DECL_THIS_STATIC (olddecl) = 1; 128590287Sobrien TREE_PUBLIC (olddecl) = 0; 128690287Sobrien 128790287Sobrien /* Make the old declaration consistent with the new one so 128890287Sobrien that all remnants of the builtin-ness of this function 128990287Sobrien will be banished. */ 129090287Sobrien SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 1291169699Skan COPY_DECL_RTL (newdecl, olddecl); 129218334Speter } 129318334Speter } 129418334Speter else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 129518334Speter { 129651412Sobrien if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl) 129751412Sobrien && TREE_CODE (newdecl) != TYPE_DECL 129851412Sobrien && ! (TREE_CODE (newdecl) == TEMPLATE_DECL 129951412Sobrien && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)) 130051412Sobrien || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl) 130151412Sobrien && TREE_CODE (olddecl) != TYPE_DECL 130251412Sobrien && ! (TREE_CODE (olddecl) == TEMPLATE_DECL 130351412Sobrien && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) 130451412Sobrien == TYPE_DECL)))) 130551412Sobrien { 130651412Sobrien /* We do nothing special here, because C++ does such nasty 130751412Sobrien things with TYPE_DECLs. Instead, just let the TYPE_DECL 130851412Sobrien get shadowed, and know that if we need to find a TYPE_DECL 130951412Sobrien for a given name, we can look in the IDENTIFIER_TYPE_VALUE 131051412Sobrien slot of the identifier. */ 1311132747Skan return NULL_TREE; 131251412Sobrien } 131351412Sobrien 131418334Speter if ((TREE_CODE (newdecl) == FUNCTION_DECL 131551412Sobrien && DECL_FUNCTION_TEMPLATE_P (olddecl)) 131618334Speter || (TREE_CODE (olddecl) == FUNCTION_DECL 131751412Sobrien && DECL_FUNCTION_TEMPLATE_P (newdecl))) 1318132747Skan return NULL_TREE; 131951412Sobrien 1320169699Skan error ("%q#D redeclared as different kind of symbol", newdecl); 132118334Speter if (TREE_CODE (olddecl) == TREE_LIST) 132218334Speter olddecl = TREE_VALUE (olddecl); 1323169699Skan error ("previous declaration of %q+#D", olddecl); 132418334Speter 1325169699Skan return error_mark_node; 132618334Speter } 132718334Speter else if (!types_match) 132818334Speter { 132990287Sobrien if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) 133052290Sobrien /* These are certainly not duplicate declarations; they're 133152290Sobrien from different scopes. */ 1332132747Skan return NULL_TREE; 133352290Sobrien 133418334Speter if (TREE_CODE (newdecl) == TEMPLATE_DECL) 133518334Speter { 133618334Speter /* The name of a class template may not be declared to refer to 133718334Speter any other template, class, function, object, namespace, value, 133851412Sobrien or type in the same scope. */ 133951412Sobrien if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL 134051412Sobrien || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 134118334Speter { 1342169699Skan error ("declaration of template %q#D", newdecl); 1343169699Skan error ("conflicts with previous declaration %q+#D", olddecl); 134418334Speter } 134551412Sobrien else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL 134651412Sobrien && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL 134751412Sobrien && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))), 134852290Sobrien TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)))) 134951412Sobrien && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), 1350117410Skan DECL_TEMPLATE_PARMS (olddecl)) 1351117410Skan /* Template functions can be disambiguated by 1352117410Skan return type. */ 1353117410Skan && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)), 1354117410Skan TREE_TYPE (TREE_TYPE (olddecl)))) 135551412Sobrien { 1356169699Skan error ("new declaration %q#D", newdecl); 1357169699Skan error ("ambiguates old declaration %q+#D", olddecl); 135851412Sobrien } 1359132747Skan return NULL_TREE; 136018334Speter } 136118334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 136218334Speter { 136390287Sobrien if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) 136418334Speter { 1365169699Skan error ("declaration of C function %q#D conflicts with", 1366169699Skan newdecl); 1367169699Skan error ("previous declaration %q+#D here", olddecl); 136818334Speter } 136918334Speter else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 137052290Sobrien TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 137118334Speter { 1372169699Skan error ("new declaration %q#D", newdecl); 1373169699Skan error ("ambiguates old declaration %q+#D", olddecl); 1374169699Skan return error_mark_node; 137518334Speter } 137618334Speter else 1377132747Skan return NULL_TREE; 137818334Speter } 1379161660Skan else 138018334Speter { 1381169699Skan error ("conflicting declaration %q#D", newdecl); 1382169699Skan error ("%q+D has a previous declaration as %q#D", olddecl, olddecl); 1383169699Skan return error_mark_node; 138418334Speter } 138518334Speter } 138690287Sobrien else if (TREE_CODE (newdecl) == FUNCTION_DECL 138751412Sobrien && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) 138851412Sobrien && (!DECL_TEMPLATE_INFO (newdecl) 138990287Sobrien || (DECL_TI_TEMPLATE (newdecl) 139051412Sobrien != DECL_TI_TEMPLATE (olddecl)))) 139151412Sobrien || (DECL_TEMPLATE_SPECIALIZATION (newdecl) 139251412Sobrien && (!DECL_TEMPLATE_INFO (olddecl) 139390287Sobrien || (DECL_TI_TEMPLATE (olddecl) 139451412Sobrien != DECL_TI_TEMPLATE (newdecl)))))) 139551412Sobrien /* It's OK to have a template specialization and a non-template 139651412Sobrien with the same type, or to have specializations of two 139751412Sobrien different templates with the same type. Note that if one is a 139851412Sobrien specialization, and the other is an instantiation of the same 139951412Sobrien template, that we do not exit at this point. That situation 140051412Sobrien can occur if we instantiate a template class, and then 1401117410Skan specialize one of its methods. This situation is valid, but 140251412Sobrien the declarations must be merged in the usual way. */ 1403132747Skan return NULL_TREE; 140490287Sobrien else if (TREE_CODE (newdecl) == FUNCTION_DECL 140590287Sobrien && ((DECL_TEMPLATE_INSTANTIATION (olddecl) 140651412Sobrien && !DECL_USE_TEMPLATE (newdecl)) 140751412Sobrien || (DECL_TEMPLATE_INSTANTIATION (newdecl) 140851412Sobrien && !DECL_USE_TEMPLATE (olddecl)))) 140951412Sobrien /* One of the declarations is a template instantiation, and the 141051412Sobrien other is not a template at all. That's OK. */ 1411132747Skan return NULL_TREE; 1412146906Skan else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 1413146906Skan { 1414146906Skan /* In [namespace.alias] we have: 1415169699Skan 1416169699Skan In a declarative region, a namespace-alias-definition can be 1417146906Skan used to redefine a namespace-alias declared in that declarative 1418146906Skan region to refer only to the namespace to which it already 1419146906Skan refers. 1420169699Skan 1421146906Skan Therefore, if we encounter a second alias directive for the same 1422146906Skan alias, we can just ignore the second directive. */ 1423146906Skan if (DECL_NAMESPACE_ALIAS (newdecl) 1424169699Skan && (DECL_NAMESPACE_ALIAS (newdecl) 1425146906Skan == DECL_NAMESPACE_ALIAS (olddecl))) 1426146906Skan return olddecl; 1427146906Skan /* [namespace.alias] 1428132747Skan 1429169699Skan A namespace-name or namespace-alias shall not be declared as 1430146906Skan the name of any other entity in the same declarative region. 1431146906Skan A namespace-name defined at global scope shall not be 1432169699Skan declared as the name of any other entity in any global scope 1433146906Skan of the program. */ 1434169699Skan error ("declaration of namespace %qD conflicts with", newdecl); 1435169699Skan error ("previous declaration of namespace %q+D here", olddecl); 1436146906Skan return error_mark_node; 1437146906Skan } 143818334Speter else 143918334Speter { 144052290Sobrien const char *errmsg = redeclaration_error_message (newdecl, olddecl); 144118334Speter if (errmsg) 144218334Speter { 144390287Sobrien error (errmsg, newdecl); 144418334Speter if (DECL_NAME (olddecl) != NULL_TREE) 1445169699Skan error ((DECL_INITIAL (olddecl) && namespace_bindings_p ()) 1446169699Skan ? "%q+#D previously defined here" 1447169699Skan : "%q+#D previously declared here", olddecl); 1448132747Skan return error_mark_node; 144918334Speter } 145018334Speter else if (TREE_CODE (olddecl) == FUNCTION_DECL 145118334Speter && DECL_INITIAL (olddecl) != NULL_TREE 145218334Speter && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE 145318334Speter && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE) 145418334Speter { 145518334Speter /* Prototype decl follows defn w/o prototype. */ 1456169699Skan warning (0, "prototype for %q+#D", newdecl); 1457169699Skan warning (0, "%Jfollows non-prototype definition here", olddecl); 145818334Speter } 1459169699Skan else if ((TREE_CODE (olddecl) == FUNCTION_DECL 1460169699Skan || TREE_CODE (olddecl) == VAR_DECL) 146118334Speter && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) 146218334Speter { 1463169699Skan /* [dcl.link] 1464169699Skan If two declarations of the same function or object 1465169699Skan specify different linkage-specifications ..., the program 1466169699Skan is ill-formed.... Except for functions with C++ linkage, 1467169699Skan a function declaration without a linkage specification 1468169699Skan shall not precede the first linkage specification for 1469169699Skan that function. A function can be declared without a 1470169699Skan linkage specification after an explicit linkage 1471169699Skan specification has been seen; the linkage explicitly 1472169699Skan specified in the earlier declaration is not affected by 1473169699Skan such a function declaration. 1474169699Skan 1475169699Skan DR 563 raises the question why the restrictions on 1476169699Skan functions should not also apply to objects. Older 1477169699Skan versions of G++ silently ignore the linkage-specification 1478169699Skan for this example: 1479169699Skan 1480169699Skan namespace N { 1481169699Skan extern int i; 1482169699Skan extern "C" int i; 1483169699Skan } 1484169699Skan 1485169699Skan which is clearly wrong. Therefore, we now treat objects 1486169699Skan like functions. */ 148790287Sobrien if (current_lang_depth () == 0) 1488169699Skan { 1489169699Skan /* There is no explicit linkage-specification, so we use 1490169699Skan the linkage from the previous declaration. */ 1491169699Skan if (!DECL_LANG_SPECIFIC (newdecl)) 1492169699Skan retrofit_lang_decl (newdecl); 1493169699Skan SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 1494169699Skan } 149518334Speter else 149618334Speter { 1497169699Skan error ("previous declaration of %q+#D with %qL linkage", 1498169699Skan olddecl, DECL_LANGUAGE (olddecl)); 1499169699Skan error ("conflicts with new declaration with %qL linkage", 1500169699Skan DECL_LANGUAGE (newdecl)); 150118334Speter } 150218334Speter } 150318334Speter 150451412Sobrien if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) 150551412Sobrien ; 150651412Sobrien else if (TREE_CODE (olddecl) == FUNCTION_DECL) 150718334Speter { 150818334Speter tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 150918334Speter tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); 151018334Speter int i = 1; 151118334Speter 151218334Speter if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE) 151318334Speter t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2); 151490287Sobrien 151518334Speter for (; t1 && t1 != void_list_node; 151618334Speter t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) 151718334Speter if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) 151818334Speter { 151918334Speter if (1 == simple_cst_equal (TREE_PURPOSE (t1), 152018334Speter TREE_PURPOSE (t2))) 152118334Speter { 1522169699Skan pedwarn ("default argument given for parameter %d of %q#D", 152396292Sobrien i, newdecl); 1524169699Skan pedwarn ("after previous specification in %q+#D", olddecl); 152518334Speter } 152618334Speter else 152718334Speter { 1528169699Skan error ("default argument given for parameter %d of %q#D", 1529169699Skan i, newdecl); 1530169699Skan error ("after previous specification in %q+#D", 153118334Speter olddecl); 153218334Speter } 153318334Speter } 153418334Speter 1535169699Skan if (DECL_DECLARED_INLINE_P (newdecl) 153690287Sobrien && ! DECL_DECLARED_INLINE_P (olddecl) 153751412Sobrien && TREE_ADDRESSABLE (olddecl) && warn_inline) 153818334Speter { 1539169699Skan warning (0, "%q#D was used before it was declared inline", newdecl); 1540169699Skan warning (0, "%Jprevious non-inline declaration here", olddecl); 154118334Speter } 154218334Speter } 154318334Speter } 154418334Speter 154596292Sobrien /* Do not merge an implicit typedef with an explicit one. In: 154696292Sobrien 154796292Sobrien class A; 154896292Sobrien ... 154996292Sobrien typedef class A A __attribute__ ((foo)); 155096292Sobrien 155196292Sobrien the attribute should apply only to the typedef. */ 155296292Sobrien if (TREE_CODE (olddecl) == TYPE_DECL 155396292Sobrien && (DECL_IMPLICIT_TYPEDEF_P (olddecl) 155496292Sobrien || DECL_IMPLICIT_TYPEDEF_P (newdecl))) 1555132747Skan return NULL_TREE; 155696292Sobrien 155718334Speter /* If new decl is `static' and an `extern' was seen previously, 155818334Speter warn about it. */ 155918334Speter warn_extern_redeclared_static (newdecl, olddecl); 156018334Speter 156151412Sobrien /* We have committed to returning 1 at this point. */ 156218334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 156318334Speter { 156418334Speter /* Now that functions must hold information normally held 156518334Speter by field decls, there is extra work to do so that 156618334Speter declaration information does not get destroyed during 156718334Speter definition. */ 156818334Speter if (DECL_VINDEX (olddecl)) 156918334Speter DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); 157018334Speter if (DECL_CONTEXT (olddecl)) 157118334Speter DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 157218334Speter DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); 157318334Speter DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 157490287Sobrien DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); 157551412Sobrien DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); 1576171835Skan DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl); 157790287Sobrien DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); 157890287Sobrien if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK) 157990287Sobrien SET_OVERLOADED_OPERATOR_CODE 158090287Sobrien (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl)); 158151412Sobrien new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; 158290287Sobrien 158351412Sobrien /* Optionally warn about more than one declaration for the same 1584169699Skan name, but don't warn about a function declaration followed by a 1585169699Skan definition. */ 158651412Sobrien if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) 158751412Sobrien && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) 1588117410Skan /* Don't warn about extern decl followed by definition. */ 158951412Sobrien && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) 1590117410Skan /* Don't warn about friends, let add_friend take care of it. */ 1591169699Skan && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))) 159251412Sobrien { 1593169699Skan warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl); 1594169699Skan warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl); 159551412Sobrien } 159618334Speter } 159718334Speter 159818334Speter /* Deal with C++: must preserve virtual function table size. */ 159918334Speter if (TREE_CODE (olddecl) == TYPE_DECL) 160018334Speter { 1601132747Skan tree newtype = TREE_TYPE (newdecl); 1602132747Skan tree oldtype = TREE_TYPE (olddecl); 160318334Speter 160418334Speter if (newtype != error_mark_node && oldtype != error_mark_node 160518334Speter && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) 1606117410Skan CLASSTYPE_FRIEND_CLASSES (newtype) 1607117410Skan = CLASSTYPE_FRIEND_CLASSES (oldtype); 1608132747Skan 160990287Sobrien DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); 161018334Speter } 161118334Speter 161251412Sobrien /* Copy all the DECL_... slots specified in the new decl 161351412Sobrien except for any that we copy here from the old type. */ 161490287Sobrien DECL_ATTRIBUTES (newdecl) 161590287Sobrien = (*targetm.merge_decl_attributes) (olddecl, newdecl); 161618334Speter 161751412Sobrien if (TREE_CODE (newdecl) == TEMPLATE_DECL) 161818334Speter { 1619169699Skan tree old_result; 1620169699Skan tree new_result; 1621169699Skan old_result = DECL_TEMPLATE_RESULT (olddecl); 1622169699Skan new_result = DECL_TEMPLATE_RESULT (newdecl); 1623169699Skan TREE_TYPE (olddecl) = TREE_TYPE (old_result); 162490287Sobrien DECL_TEMPLATE_SPECIALIZATIONS (olddecl) 162552290Sobrien = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), 162652290Sobrien DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); 162790287Sobrien 1628169699Skan if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 1629169699Skan { 1630169699Skan DECL_INLINE (old_result) 1631169699Skan |= DECL_INLINE (new_result); 1632169699Skan DECL_DECLARED_INLINE_P (old_result) 1633169699Skan |= DECL_DECLARED_INLINE_P (new_result); 1634169699Skan check_redeclaration_exception_specification (newdecl, olddecl); 1635169699Skan } 1636169699Skan 163790287Sobrien /* If the new declaration is a definition, update the file and 163890287Sobrien line information on the declaration. */ 1639169699Skan if (DECL_INITIAL (old_result) == NULL_TREE 1640169699Skan && DECL_INITIAL (new_result) != NULL_TREE) 164190287Sobrien { 1642169699Skan DECL_SOURCE_LOCATION (olddecl) 1643169699Skan = DECL_SOURCE_LOCATION (old_result) 1644117410Skan = DECL_SOURCE_LOCATION (newdecl); 1645132747Skan if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 1646169699Skan DECL_ARGUMENTS (old_result) 1647169699Skan = DECL_ARGUMENTS (new_result); 164890287Sobrien } 164990287Sobrien 1650132747Skan return olddecl; 165118334Speter } 165290287Sobrien 165318334Speter if (types_match) 165418334Speter { 165518334Speter /* Automatically handles default parameters. */ 165618334Speter tree oldtype = TREE_TYPE (olddecl); 165718334Speter tree newtype; 165818334Speter 165918334Speter /* Merge the data types specified in the two decls. */ 166096292Sobrien newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); 166118334Speter 166296292Sobrien /* If merge_types produces a non-typedef type, just use the old type. */ 166390287Sobrien if (TREE_CODE (newdecl) == TYPE_DECL 166490287Sobrien && newtype == DECL_ORIGINAL_TYPE (newdecl)) 166590287Sobrien newtype = oldtype; 166690287Sobrien 166718334Speter if (TREE_CODE (newdecl) == VAR_DECL) 1668117410Skan { 1669117410Skan DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); 1670117410Skan DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); 1671169699Skan DECL_NONTRIVIALLY_INITIALIZED_P (newdecl) 1672169699Skan |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl); 1673132747Skan DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl) 1674132747Skan |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl); 1675169699Skan 1676169699Skan /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */ 1677169699Skan if (DECL_LANG_SPECIFIC (olddecl) 1678169699Skan && CP_DECL_THREADPRIVATE_P (olddecl)) 1679169699Skan { 1680169699Skan /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */ 1681169699Skan if (!DECL_LANG_SPECIFIC (newdecl)) 1682169699Skan retrofit_lang_decl (newdecl); 1683169699Skan 1684169699Skan DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl); 1685169699Skan CP_DECL_THREADPRIVATE_P (newdecl) = 1; 1686169699Skan } 1687117410Skan } 1688117410Skan 168996292Sobrien /* Do this after calling `merge_types' so that default 169018334Speter parameters don't confuse us. */ 1691169699Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL) 1692169699Skan check_redeclaration_exception_specification (newdecl, olddecl); 169318334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; 169418334Speter 1695169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1696169699Skan check_default_args (newdecl); 1697169699Skan 169818334Speter /* Lay the type out, unless already done. */ 169952290Sobrien if (! same_type_p (newtype, oldtype) 170051412Sobrien && TREE_TYPE (newdecl) != error_mark_node 170151412Sobrien && !(processing_template_decl && uses_template_parms (newdecl))) 170218334Speter layout_type (TREE_TYPE (newdecl)); 170318334Speter 170451412Sobrien if ((TREE_CODE (newdecl) == VAR_DECL 170551412Sobrien || TREE_CODE (newdecl) == PARM_DECL 170651412Sobrien || TREE_CODE (newdecl) == RESULT_DECL 170751412Sobrien || TREE_CODE (newdecl) == FIELD_DECL 170851412Sobrien || TREE_CODE (newdecl) == TYPE_DECL) 170951412Sobrien && !(processing_template_decl && uses_template_parms (newdecl))) 171018334Speter layout_decl (newdecl, 0); 171118334Speter 171218334Speter /* Merge the type qualifiers. */ 171318334Speter if (TREE_READONLY (newdecl)) 171418334Speter TREE_READONLY (olddecl) = 1; 171518334Speter if (TREE_THIS_VOLATILE (newdecl)) 171618334Speter TREE_THIS_VOLATILE (olddecl) = 1; 1717169699Skan if (TREE_NOTHROW (newdecl)) 1718169699Skan TREE_NOTHROW (olddecl) = 1; 171918334Speter 1720169699Skan /* Merge deprecatedness. */ 1721169699Skan if (TREE_DEPRECATED (newdecl)) 1722169699Skan TREE_DEPRECATED (olddecl) = 1; 1723169699Skan 172418334Speter /* Merge the initialization information. */ 172518334Speter if (DECL_INITIAL (newdecl) == NULL_TREE 172618334Speter && DECL_INITIAL (olddecl) != NULL_TREE) 172718334Speter { 172818334Speter DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1729117410Skan DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 173090287Sobrien if (CAN_HAVE_FULL_LANG_DECL_P (newdecl) 173190287Sobrien && DECL_LANG_SPECIFIC (newdecl) 173252290Sobrien && DECL_LANG_SPECIFIC (olddecl)) 1733132747Skan { 1734132747Skan DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 1735169699Skan DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 1736132747Skan } 173718334Speter } 173818334Speter 173918334Speter /* Merge the section attribute. 1740169699Skan We want to issue an error if the sections conflict but that must be 174118334Speter done later in decl_attributes since we are called before attributes 174218334Speter are assigned. */ 174318334Speter if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 174418334Speter DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 174518334Speter 174690287Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 174790287Sobrien { 174890287Sobrien DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 174990287Sobrien |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1750132747Skan DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1751132747Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1752132747Skan TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1753132747Skan TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl); 1754132747Skan DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1755132747Skan DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 175696292Sobrien /* Keep the old RTL. */ 175796292Sobrien COPY_DECL_RTL (olddecl, newdecl); 175890287Sobrien } 1759169699Skan else if (TREE_CODE (newdecl) == VAR_DECL 176096292Sobrien && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) 176196292Sobrien { 176296292Sobrien /* Keep the old RTL. We cannot keep the old RTL if the old 176396292Sobrien declaration was for an incomplete object and the new 176496292Sobrien declaration is not since many attributes of the RTL will 176596292Sobrien change. */ 176696292Sobrien COPY_DECL_RTL (olddecl, newdecl); 176796292Sobrien } 176818334Speter } 176918334Speter /* If cannot merge, then use the new type and qualifiers, 177018334Speter and don't preserve the old rtl. */ 177118334Speter else 177218334Speter { 177318334Speter /* Clean out any memory we had of the old declaration. */ 177418334Speter tree oldstatic = value_member (olddecl, static_aggregates); 177518334Speter if (oldstatic) 177618334Speter TREE_VALUE (oldstatic) = error_mark_node; 177718334Speter 177818334Speter TREE_TYPE (olddecl) = TREE_TYPE (newdecl); 177918334Speter TREE_READONLY (olddecl) = TREE_READONLY (newdecl); 178018334Speter TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); 178118334Speter TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); 178218334Speter } 178318334Speter 178418334Speter /* Merge the storage class information. */ 178596292Sobrien merge_weak (newdecl, olddecl); 178696292Sobrien 178751412Sobrien DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl); 178890287Sobrien DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); 178918334Speter TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 179018334Speter TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); 179118334Speter if (! DECL_EXTERNAL (olddecl)) 179218334Speter DECL_EXTERNAL (newdecl) = 0; 179390287Sobrien 1794169699Skan new_template = NULL_TREE; 179551412Sobrien if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) 179618334Speter { 179718334Speter DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); 179818334Speter DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); 179951412Sobrien DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); 180090287Sobrien DECL_TEMPLATE_INSTANTIATED (newdecl) 180152290Sobrien |= DECL_TEMPLATE_INSTANTIATED (olddecl); 1802169699Skan 1803169699Skan /* If the OLDDECL is an instantiation and/or specialization, 1804169699Skan then the NEWDECL must be too. But, it may not yet be marked 1805169699Skan as such if the caller has created NEWDECL, but has not yet 1806169699Skan figured out that it is a redeclaration. */ 1807169699Skan if (!DECL_USE_TEMPLATE (newdecl)) 1808169699Skan DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl); 1809169699Skan 181051412Sobrien /* Don't really know how much of the language-specific 181151412Sobrien values we should copy from old to new. */ 181251412Sobrien DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); 1813169699Skan DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 1814117410Skan DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2; 181551412Sobrien DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); 1816169699Skan DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl); 1817169699Skan if (DECL_TEMPLATE_INFO (newdecl)) 1818169699Skan new_template = DECL_TI_TEMPLATE (newdecl); 181952290Sobrien DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); 182090287Sobrien DECL_INITIALIZED_IN_CLASS_P (newdecl) 1821169699Skan |= DECL_INITIALIZED_IN_CLASS_P (olddecl); 182251412Sobrien olddecl_friend = DECL_FRIEND_P (olddecl); 1823169699Skan hidden_friend = (DECL_ANTICIPATED (olddecl) 1824169699Skan && DECL_HIDDEN_FRIEND_P (olddecl) 1825169699Skan && newdecl_is_friend); 182652290Sobrien 182752290Sobrien /* Only functions have DECL_BEFRIENDING_CLASSES. */ 182852290Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL 182952290Sobrien || DECL_FUNCTION_TEMPLATE_P (newdecl)) 1830117410Skan { 1831117410Skan DECL_BEFRIENDING_CLASSES (newdecl) 1832117410Skan = chainon (DECL_BEFRIENDING_CLASSES (newdecl), 1833117410Skan DECL_BEFRIENDING_CLASSES (olddecl)); 1834117410Skan /* DECL_THUNKS is only valid for virtual functions, 1835117410Skan otherwise it is a DECL_FRIEND_CONTEXT. */ 1836117410Skan if (DECL_VIRTUAL_P (newdecl)) 1837117410Skan DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl); 1838117410Skan } 183918334Speter } 184018334Speter 184118334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 184218334Speter { 184390287Sobrien if (DECL_TEMPLATE_INSTANTIATION (olddecl) 184490287Sobrien && !DECL_TEMPLATE_INSTANTIATION (newdecl)) 184551412Sobrien { 184651412Sobrien /* If newdecl is not a specialization, then it is not a 184751412Sobrien template-related function at all. And that means that we 1848132747Skan should have exited above, returning 0. */ 1849169699Skan gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl)); 185051412Sobrien 185190287Sobrien if (TREE_USED (olddecl)) 185251412Sobrien /* From [temp.expl.spec]: 185390287Sobrien 185451412Sobrien If a template, a member template or the member of a class 185551412Sobrien template is explicitly specialized then that 185651412Sobrien specialization shall be declared before the first use of 185751412Sobrien that specialization that would cause an implicit 185851412Sobrien instantiation to take place, in every translation unit in 185951412Sobrien which such a use occurs. */ 1860169699Skan error ("explicit specialization of %qD after first use", 186151412Sobrien olddecl); 186251412Sobrien 186351412Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); 1864117410Skan 1865169699Skan /* Don't propagate visibility from the template to the 1866169699Skan specialization here. We'll do that in determine_visibility if 1867169699Skan appropriate. */ 1868169699Skan DECL_VISIBILITY_SPECIFIED (olddecl) = 0; 1869169699Skan 1870117410Skan /* [temp.expl.spec/14] We don't inline explicit specialization 1871117410Skan just because the primary template says so. */ 187251412Sobrien } 1873117410Skan else 1874117410Skan { 1875117410Skan if (DECL_PENDING_INLINE_INFO (newdecl) == 0) 1876117410Skan DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl); 187718334Speter 1878117410Skan DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); 187918334Speter 1880169699Skan /* If either decl says `inline', this fn is inline, unless 1881117410Skan its definition was passed already. */ 1882117410Skan if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE) 1883117410Skan DECL_INLINE (olddecl) = 1; 1884117410Skan DECL_INLINE (newdecl) = DECL_INLINE (olddecl); 188590287Sobrien 1886117410Skan DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 1887117410Skan = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 1888117410Skan } 1889117410Skan 189090287Sobrien /* Preserve abstractness on cloned [cd]tors. */ 189190287Sobrien DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl); 189290287Sobrien 189318334Speter if (! types_match) 189418334Speter { 189590287Sobrien SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 189690287Sobrien COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); 1897169699Skan COPY_DECL_RTL (newdecl, olddecl); 189851412Sobrien } 189951412Sobrien if (! types_match || new_defines_function) 190051412Sobrien { 190190287Sobrien /* These need to be copied so that the names are available. 190290287Sobrien Note that if the types do match, we'll preserve inline 190390287Sobrien info and other bits, but if not, we won't. */ 190418334Speter DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); 190518334Speter DECL_RESULT (olddecl) = DECL_RESULT (newdecl); 190618334Speter } 190718334Speter if (new_defines_function) 190818334Speter /* If defining a function declared with other language 190918334Speter linkage, use the previously declared language linkage. */ 191090287Sobrien SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 191190287Sobrien else if (types_match) 191218334Speter { 191318334Speter /* If redeclaring a builtin function, and not a definition, 191418334Speter it stays built in. */ 191518334Speter if (DECL_BUILT_IN (olddecl)) 191618334Speter { 191790287Sobrien DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 191818334Speter DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 191918334Speter /* If we're keeping the built-in definition, keep the rtl, 192018334Speter regardless of declaration matches. */ 1921169699Skan COPY_DECL_RTL (olddecl, newdecl); 192218334Speter } 192318334Speter 192418334Speter DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 192518334Speter /* Don't clear out the arguments if we're redefining a function. */ 192618334Speter if (DECL_ARGUMENTS (olddecl)) 192718334Speter DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 192818334Speter } 192918334Speter } 193090287Sobrien else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 193190287Sobrien NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); 193218334Speter 193318334Speter /* Now preserve various other info from the definition. */ 193418334Speter TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); 193518334Speter TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); 193618334Speter DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 193790287Sobrien COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 193818334Speter 1939169699Skan /* Warn about conflicting visibility specifications. */ 1940169699Skan if (DECL_VISIBILITY_SPECIFIED (olddecl) 1941169699Skan && DECL_VISIBILITY_SPECIFIED (newdecl) 1942169699Skan && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1943132747Skan { 1944169699Skan warning (OPT_Wattributes, "%q+D: visibility attribute ignored " 1945169699Skan "because it", newdecl); 1946169699Skan warning (OPT_Wattributes, "%Jconflicts with previous " 1947169699Skan "declaration here", olddecl); 1948169699Skan } 1949169699Skan /* Choose the declaration which specified visibility. */ 1950169699Skan if (DECL_VISIBILITY_SPECIFIED (olddecl)) 1951169699Skan { 1952132747Skan DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1953169699Skan DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 1954132747Skan } 1955169699Skan /* Init priority used to be merged from newdecl to olddecl by the memcpy, 1956169699Skan so keep this behavior. */ 1957169699Skan if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl)) 1958169699Skan { 1959169699Skan SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl)); 1960169699Skan DECL_HAS_INIT_PRIORITY_P (olddecl) = 1; 1961169699Skan } 1962132747Skan 1963169699Skan /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced 1964169699Skan with that from NEWDECL below. */ 1965169699Skan if (DECL_LANG_SPECIFIC (olddecl)) 1966169699Skan { 1967169699Skan gcc_assert (DECL_LANG_SPECIFIC (olddecl) 1968169699Skan != DECL_LANG_SPECIFIC (newdecl)); 1969169699Skan ggc_free (DECL_LANG_SPECIFIC (olddecl)); 1970169699Skan } 1971169699Skan 197218334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 197318334Speter { 197418334Speter int function_size; 197518334Speter 1976169699Skan function_size = sizeof (struct tree_decl_common); 197718334Speter 197890287Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 197990287Sobrien (char *) newdecl + sizeof (struct tree_common), 198090287Sobrien function_size - sizeof (struct tree_common)); 198118334Speter 1982169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1983169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1984169699Skan sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common)); 1985169699Skan if (new_template) 1986132747Skan /* If newdecl is a template instantiation, it is possible that 1987132747Skan the following sequence of events has occurred: 198851412Sobrien 1989132747Skan o A friend function was declared in a class template. The 1990132747Skan class template was instantiated. 199151412Sobrien 1992132747Skan o The instantiation of the friend declaration was 1993132747Skan recorded on the instantiation list, and is newdecl. 199451412Sobrien 1995132747Skan o Later, however, instantiate_class_template called pushdecl 1996132747Skan on the newdecl to perform name injection. But, pushdecl in 1997132747Skan turn called duplicate_decls when it discovered that another 1998132747Skan declaration of a global function with the same name already 1999132747Skan existed. 200051412Sobrien 2001132747Skan o Here, in duplicate_decls, we decided to clobber newdecl. 200251412Sobrien 2003132747Skan If we're going to do that, we'd better make sure that 2004132747Skan olddecl, and not newdecl, is on the list of 2005132747Skan instantiations so that if we try to do the instantiation 2006132747Skan again we won't get the clobbered declaration. */ 2007169699Skan reregister_specialization (newdecl, 2008169699Skan new_template, 2009132747Skan olddecl); 201018334Speter } 201118334Speter else 201218334Speter { 2013169699Skan size_t size = tree_code_size (TREE_CODE (olddecl)); 201490287Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 201590287Sobrien (char *) newdecl + sizeof (struct tree_common), 2016169699Skan sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 2017169699Skan switch (TREE_CODE (olddecl)) 2018169699Skan { 2019169699Skan case LABEL_DECL: 2020169699Skan case VAR_DECL: 2021169699Skan case RESULT_DECL: 2022169699Skan case PARM_DECL: 2023169699Skan case FIELD_DECL: 2024169699Skan case TYPE_DECL: 2025169699Skan case CONST_DECL: 2026169699Skan { 2027169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 2028169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 2029169699Skan size - sizeof (struct tree_decl_common) 2030169699Skan + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 2031169699Skan } 2032169699Skan break; 2033169699Skan default: 2034169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 2035169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 2036169699Skan sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common) 2037169699Skan + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 2038169699Skan break; 2039169699Skan } 204018334Speter } 204118334Speter DECL_UID (olddecl) = olddecl_uid; 204218334Speter if (olddecl_friend) 204318334Speter DECL_FRIEND_P (olddecl) = 1; 2044169699Skan if (hidden_friend) 2045169699Skan { 2046169699Skan DECL_ANTICIPATED (olddecl) = 1; 2047169699Skan DECL_HIDDEN_FRIEND_P (olddecl) = 1; 2048169699Skan } 204918334Speter 205051412Sobrien /* NEWDECL contains the merged attribute lists. 205151412Sobrien Update OLDDECL to be the same. */ 205290287Sobrien DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); 205351412Sobrien 2054119270Skan /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 2055132747Skan so that encode_section_info has a chance to look at the new decl 2056132747Skan flags and attributes. */ 2057169699Skan if (DECL_RTL_SET_P (olddecl) 2058119270Skan && (TREE_CODE (olddecl) == FUNCTION_DECL 2059119270Skan || (TREE_CODE (olddecl) == VAR_DECL 2060119270Skan && TREE_STATIC (olddecl)))) 2061169699Skan make_decl_rtl (olddecl); 2062119270Skan 2063169699Skan /* The NEWDECL will no longer be needed. Because every out-of-class 2064169699Skan declaration of a member results in a call to duplicate_decls, 2065169699Skan freeing these nodes represents in a significant savings. */ 2066169699Skan ggc_free (newdecl); 2067169699Skan 2068132747Skan return olddecl; 206918334Speter} 207018334Speter 207118334Speter/* Return zero if the declaration NEWDECL is valid 207218334Speter when the declaration OLDDECL (assumed to be for the same name) 207318334Speter has already been seen. 207418334Speter Otherwise return an error message format string with a %s 207518334Speter where the identifier should go. */ 207618334Speter 207752290Sobrienstatic const char * 2078132747Skanredeclaration_error_message (tree newdecl, tree olddecl) 207918334Speter{ 208018334Speter if (TREE_CODE (newdecl) == TYPE_DECL) 208118334Speter { 208218334Speter /* Because C++ can put things into name space for free, 208318334Speter constructs like "typedef struct foo { ... } foo" 208418334Speter would look like an erroneous redeclaration. */ 208552290Sobrien if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) 2086169699Skan return NULL; 208718334Speter else 2088169699Skan return "redefinition of %q#D"; 208918334Speter } 209018334Speter else if (TREE_CODE (newdecl) == FUNCTION_DECL) 209118334Speter { 209218334Speter /* If this is a pure function, its olddecl will actually be 209318334Speter the original initialization to `0' (which we force to call 209418334Speter abort()). Don't complain about redefinition in this case. */ 2095169699Skan if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl) 2096169699Skan && DECL_INITIAL (olddecl) == NULL_TREE) 2097169699Skan return NULL; 209818334Speter 209951412Sobrien /* If both functions come from different namespaces, this is not 2100117410Skan a redeclaration - this is a conflict with a used function. */ 210151412Sobrien if (DECL_NAMESPACE_SCOPE_P (olddecl) 2102146906Skan && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl) 2103146906Skan && ! decls_match (olddecl, newdecl)) 2104169699Skan return "%qD conflicts with used function"; 210551412Sobrien 210618334Speter /* We'll complain about linkage mismatches in 2107169699Skan warn_extern_redeclared_static. */ 210818334Speter 210951412Sobrien /* Defining the same name twice is no good. */ 211018334Speter if (DECL_INITIAL (olddecl) != NULL_TREE 211118334Speter && DECL_INITIAL (newdecl) != NULL_TREE) 211218334Speter { 211318334Speter if (DECL_NAME (olddecl) == NULL_TREE) 2114169699Skan return "%q#D not declared in class"; 211518334Speter else 2116169699Skan return "redefinition of %q#D"; 211718334Speter } 2118169699Skan return NULL; 211918334Speter } 212018334Speter else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 212118334Speter { 2122132747Skan tree nt, ot; 2123132747Skan 2124132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 2125132747Skan { 2126132747Skan if (COMPLETE_TYPE_P (TREE_TYPE (newdecl)) 2127132747Skan && COMPLETE_TYPE_P (TREE_TYPE (olddecl))) 2128169699Skan return "redefinition of %q#D"; 2129132747Skan return NULL; 2130132747Skan } 2131132747Skan 2132132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL 2133169699Skan || (DECL_TEMPLATE_RESULT (newdecl) 2134132747Skan == DECL_TEMPLATE_RESULT (olddecl))) 2135132747Skan return NULL; 2136132747Skan 2137132747Skan nt = DECL_TEMPLATE_RESULT (newdecl); 2138132747Skan if (DECL_TEMPLATE_INFO (nt)) 2139132747Skan nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt)); 2140132747Skan ot = DECL_TEMPLATE_RESULT (olddecl); 2141132747Skan if (DECL_TEMPLATE_INFO (ot)) 2142132747Skan ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot)); 2143132747Skan if (DECL_INITIAL (nt) && DECL_INITIAL (ot)) 2144169699Skan return "redefinition of %q#D"; 2145132747Skan 2146132747Skan return NULL; 214718334Speter } 2148169699Skan else if (TREE_CODE (newdecl) == VAR_DECL 2149169699Skan && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl) 2150169699Skan && (! DECL_LANG_SPECIFIC (olddecl) 2151169699Skan || ! CP_DECL_THREADPRIVATE_P (olddecl) 2152169699Skan || DECL_THREAD_LOCAL_P (newdecl))) 2153169699Skan { 2154169699Skan /* Only variables can be thread-local, and all declarations must 2155169699Skan agree on this property. */ 2156169699Skan if (DECL_THREAD_LOCAL_P (newdecl)) 2157169699Skan return "thread-local declaration of %q#D follows " 2158169699Skan "non-thread-local declaration"; 2159169699Skan else 2160169699Skan return "non-thread-local declaration of %q#D follows " 2161169699Skan "thread-local declaration"; 2162169699Skan } 216390287Sobrien else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) 216418334Speter { 2165171835Skan /* The objects have been declared at namespace scope. If either 2166171835Skan is a member of an anonymous union, then this is an invalid 2167171835Skan redeclaration. For example: 2168171835Skan 2169171835Skan int i; 2170171835Skan union { int i; }; 2171171835Skan 2172171835Skan is invalid. */ 2173171835Skan if (DECL_ANON_UNION_VAR_P (newdecl) 2174171835Skan || DECL_ANON_UNION_VAR_P (olddecl)) 2175171835Skan return "redeclaration of %q#D"; 2176171835Skan /* If at least one declaration is a reference, there is no 2177171835Skan conflict. For example: 2178171835Skan 2179171835Skan int i = 3; 2180171835Skan extern int i; 2181171835Skan 2182171835Skan is valid. */ 218318334Speter if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) 2184169699Skan return NULL; 218518334Speter /* Reject two definitions. */ 2186169699Skan return "redefinition of %q#D"; 218718334Speter } 218818334Speter else 218918334Speter { 219018334Speter /* Objects declared with block scope: */ 219118334Speter /* Reject two definitions, and reject a definition 219218334Speter together with an external reference. */ 219318334Speter if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) 2194169699Skan return "redeclaration of %q#D"; 2195169699Skan return NULL; 219618334Speter } 219718334Speter} 219818334Speter 2199169699Skan/* Hash and equality functions for the named_label table. */ 2200169699Skan 2201169699Skanstatic hashval_t 2202169699Skannamed_label_entry_hash (const void *data) 2203169699Skan{ 2204169699Skan const struct named_label_entry *ent = (const struct named_label_entry *) data; 2205169699Skan return DECL_UID (ent->label_decl); 2206169699Skan} 2207169699Skan 2208169699Skanstatic int 2209169699Skannamed_label_entry_eq (const void *a, const void *b) 2210169699Skan{ 2211169699Skan const struct named_label_entry *ent_a = (const struct named_label_entry *) a; 2212169699Skan const struct named_label_entry *ent_b = (const struct named_label_entry *) b; 2213169699Skan return ent_a->label_decl == ent_b->label_decl; 2214169699Skan} 2215169699Skan 221690287Sobrien/* Create a new label, named ID. */ 221718334Speter 221890287Sobrienstatic tree 2219132747Skanmake_label_decl (tree id, int local_p) 222018334Speter{ 2221169699Skan struct named_label_entry *ent; 2222169699Skan void **slot; 222390287Sobrien tree decl; 222418334Speter 222590287Sobrien decl = build_decl (LABEL_DECL, id, void_type_node); 222618334Speter 222790287Sobrien DECL_CONTEXT (decl) = current_function_decl; 222890287Sobrien DECL_MODE (decl) = VOIDmode; 222990287Sobrien C_DECLARED_LABEL_FLAG (decl) = local_p; 223090287Sobrien 223190287Sobrien /* Say where one reference is to the label, for the sake of the 223290287Sobrien error if it is not defined. */ 2233132747Skan DECL_SOURCE_LOCATION (decl) = input_location; 223490287Sobrien 223590287Sobrien /* Record the fact that this identifier is bound to this label. */ 223690287Sobrien SET_IDENTIFIER_LABEL_VALUE (id, decl); 223790287Sobrien 2238169699Skan /* Create the label htab for the function on demand. */ 2239169699Skan if (!named_labels) 2240169699Skan named_labels = htab_create_ggc (13, named_label_entry_hash, 2241169699Skan named_label_entry_eq, NULL); 224290287Sobrien 2243169699Skan /* Record this label on the list of labels used in this function. 2244169699Skan We do this before calling make_label_decl so that we get the 2245169699Skan IDENTIFIER_LABEL_VALUE before the new label is declared. */ 2246169699Skan ent = GGC_CNEW (struct named_label_entry); 2247169699Skan ent->label_decl = decl; 224890287Sobrien 2249169699Skan slot = htab_find_slot (named_labels, ent, INSERT); 2250169699Skan gcc_assert (*slot == NULL); 2251169699Skan *slot = ent; 2252169699Skan 2253169699Skan return decl; 225490287Sobrien} 225518334Speter 225690287Sobrien/* Look for a label named ID in the current function. If one cannot 225790287Sobrien be found, create one. (We keep track of used, but undefined, 225890287Sobrien labels, and complain about them at the end of a function.) */ 225990287Sobrien 226090287Sobrientree 2261132747Skanlookup_label (tree id) 226290287Sobrien{ 226390287Sobrien tree decl; 226490287Sobrien 2265117410Skan timevar_push (TV_NAME_LOOKUP); 226690287Sobrien /* You can't use labels at global scope. */ 226790287Sobrien if (current_function_decl == NULL_TREE) 226818334Speter { 2269169699Skan error ("label %qE referenced outside of any function", id); 2270117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 227118334Speter } 227218334Speter 227390287Sobrien /* See if we've already got this label. */ 227490287Sobrien decl = IDENTIFIER_LABEL_VALUE (id); 227590287Sobrien if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl) 2276117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 227718334Speter 227890287Sobrien decl = make_label_decl (id, /*local_p=*/0); 2279117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 228090287Sobrien} 228118334Speter 228290287Sobrien/* Declare a local label named ID. */ 228318334Speter 228490287Sobrientree 2285132747Skandeclare_local_label (tree id) 228690287Sobrien{ 2287169699Skan tree decl, shadow; 228818334Speter 228990287Sobrien /* Add a new entry to the SHADOWED_LABELS list so that when we leave 2290169699Skan this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */ 2291169699Skan shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, 2292169699Skan current_binding_level->shadowed_labels); 2293169699Skan current_binding_level->shadowed_labels = shadow; 2294169699Skan 229590287Sobrien decl = make_label_decl (id, /*local_p=*/1); 2296169699Skan TREE_VALUE (shadow) = decl; 229790287Sobrien 229818334Speter return decl; 229918334Speter} 230018334Speter 230190287Sobrien/* Returns nonzero if it is ill-formed to jump past the declaration of 230290287Sobrien DECL. Returns 2 if it's also a real problem. */ 230318334Speter 230490287Sobrienstatic int 2305132747Skandecl_jump_unsafe (tree decl) 230690287Sobrien{ 2307169699Skan if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl) 2308169699Skan || TREE_TYPE (decl) == error_mark_node) 230990287Sobrien return 0; 231018334Speter 2311169699Skan if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) 2312169699Skan || DECL_NONTRIVIALLY_INITIALIZED_P (decl)) 2313169699Skan return 2; 2314169699Skan 2315169699Skan if (pod_type_p (TREE_TYPE (decl))) 231690287Sobrien return 0; 231790287Sobrien 2318169699Skan /* The POD stuff is just pedantry; why should it matter if the class 231990287Sobrien contains a field of pointer to member type? */ 232090287Sobrien return 1; 232190287Sobrien} 232290287Sobrien 2323169699Skan/* A subroutine of check_previous_goto_1 to identify a branch to the user. */ 2324169699Skan 2325169699Skanstatic void 2326169699Skanidentify_goto (tree decl, const location_t *locus) 2327169699Skan{ 2328169699Skan if (decl) 2329169699Skan pedwarn ("jump to label %qD", decl); 2330169699Skan else 2331169699Skan pedwarn ("jump to case label"); 2332169699Skan if (locus) 2333169699Skan pedwarn ("%H from here", locus); 2334169699Skan} 2335169699Skan 233690287Sobrien/* Check that a single previously seen jump to a newly defined label 233790287Sobrien is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for 233890287Sobrien the jump context; NAMES are the names in scope in LEVEL at the jump 2339169699Skan context; LOCUS is the source position of the jump or 0. Returns 2340169699Skan true if all is well. */ 234190287Sobrien 2342169699Skanstatic bool 2343169699Skancheck_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names, 2344169699Skan bool exited_omp, const location_t *locus) 234518334Speter{ 2346169699Skan struct cp_binding_level *b; 2347169699Skan bool identified = false, saw_eh = false, saw_omp = false; 2348169699Skan 2349169699Skan if (exited_omp) 235090287Sobrien { 2351169699Skan identify_goto (decl, locus); 2352169699Skan error (" exits OpenMP structured block"); 2353169699Skan identified = saw_omp = true; 2354169699Skan } 2355169699Skan 2356169699Skan for (b = current_binding_level; b ; b = b->level_chain) 2357169699Skan { 2358169699Skan tree new_decls, old_decls = (b == level ? names : NULL_TREE); 2359169699Skan 2360169699Skan for (new_decls = b->names; new_decls != old_decls; 236190287Sobrien new_decls = TREE_CHAIN (new_decls)) 236290287Sobrien { 236390287Sobrien int problem = decl_jump_unsafe (new_decls); 236490287Sobrien if (! problem) 236590287Sobrien continue; 236618334Speter 2367169699Skan if (!identified) 236890287Sobrien { 2369169699Skan identify_goto (decl, locus); 2370169699Skan identified = true; 237190287Sobrien } 237290287Sobrien if (problem > 1) 2373169699Skan error (" crosses initialization of %q+#D", new_decls); 237490287Sobrien else 2375169699Skan pedwarn (" enters scope of non-POD %q+#D", new_decls); 237690287Sobrien } 237790287Sobrien 237890287Sobrien if (b == level) 237990287Sobrien break; 2380169699Skan if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh) 238190287Sobrien { 2382169699Skan if (!identified) 238390287Sobrien { 2384169699Skan identify_goto (decl, locus); 2385169699Skan identified = true; 238690287Sobrien } 2387132747Skan if (b->kind == sk_try) 238890287Sobrien error (" enters try block"); 238990287Sobrien else 239090287Sobrien error (" enters catch block"); 2391169699Skan saw_eh = true; 239290287Sobrien } 2393169699Skan if (b->kind == sk_omp && !saw_omp) 2394169699Skan { 2395169699Skan if (!identified) 2396169699Skan { 2397169699Skan identify_goto (decl, locus); 2398169699Skan identified = true; 2399169699Skan } 2400169699Skan error (" enters OpenMP structured block"); 2401169699Skan saw_omp = true; 2402169699Skan } 240390287Sobrien } 2404169699Skan 2405169699Skan return !identified; 240690287Sobrien} 240790287Sobrien 240890287Sobrienstatic void 2409169699Skancheck_previous_goto (tree decl, struct named_label_use_entry *use) 241090287Sobrien{ 2411169699Skan check_previous_goto_1 (decl, use->binding_level, 2412169699Skan use->names_in_scope, use->in_omp_scope, 2413169699Skan &use->o_goto_locus); 241490287Sobrien} 241590287Sobrien 2416169699Skanstatic bool 2417132747Skancheck_switch_goto (struct cp_binding_level* level) 241890287Sobrien{ 2419169699Skan return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL); 242090287Sobrien} 242190287Sobrien 242290287Sobrien/* Check that a new jump to a label DECL is OK. Called by 242390287Sobrien finish_goto_stmt. */ 242490287Sobrien 242590287Sobrienvoid 2426132747Skancheck_goto (tree decl) 242790287Sobrien{ 2428169699Skan struct named_label_entry *ent, dummy; 2429169699Skan bool saw_catch = false, identified = false; 243090287Sobrien tree bad; 243190287Sobrien 2432169699Skan /* We can't know where a computed goto is jumping. 2433169699Skan So we assume that it's OK. */ 2434169699Skan if (TREE_CODE (decl) != LABEL_DECL) 243590287Sobrien return; 243690287Sobrien 2437169699Skan /* We didn't record any information about this label when we created it, 2438169699Skan and there's not much point since it's trivial to analyze as a return. */ 2439169699Skan if (decl == cdtor_label) 2440169699Skan return; 2441169699Skan 2442169699Skan dummy.label_decl = decl; 2443169699Skan ent = (struct named_label_entry *) htab_find (named_labels, &dummy); 2444169699Skan gcc_assert (ent != NULL); 2445169699Skan 244690287Sobrien /* If the label hasn't been defined yet, defer checking. */ 244790287Sobrien if (! DECL_INITIAL (decl)) 244890287Sobrien { 2449169699Skan struct named_label_use_entry *new_use; 2450169699Skan 2451169699Skan /* Don't bother creating another use if the last goto had the 2452169699Skan same data, and will therefore create the same set of errors. */ 2453169699Skan if (ent->uses 2454169699Skan && ent->uses->names_in_scope == current_binding_level->names) 2455169699Skan return; 2456169699Skan 2457169699Skan new_use = GGC_NEW (struct named_label_use_entry); 2458169699Skan new_use->binding_level = current_binding_level; 2459169699Skan new_use->names_in_scope = current_binding_level->names; 2460169699Skan new_use->o_goto_locus = input_location; 2461169699Skan new_use->in_omp_scope = false; 2462169699Skan 2463169699Skan new_use->next = ent->uses; 2464169699Skan ent->uses = new_use; 246590287Sobrien return; 246690287Sobrien } 246790287Sobrien 2468169699Skan if (ent->in_try_scope || ent->in_catch_scope 2469169699Skan || ent->in_omp_scope || ent->bad_decls) 247090287Sobrien { 2471169699Skan pedwarn ("jump to label %q+D", decl); 247290287Sobrien pedwarn (" from here"); 2473169699Skan identified = true; 247490287Sobrien } 247590287Sobrien 2476169699Skan for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad)) 247790287Sobrien { 247890287Sobrien tree b = TREE_VALUE (bad); 247990287Sobrien int u = decl_jump_unsafe (b); 248090287Sobrien 248190287Sobrien if (u > 1 && DECL_ARTIFICIAL (b)) 2482169699Skan { 2483169699Skan /* Can't skip init of __exception_info. */ 2484169699Skan error ("%J enters catch block", b); 2485169699Skan saw_catch = true; 2486169699Skan } 248790287Sobrien else if (u > 1) 2488169699Skan error (" skips initialization of %q+#D", b); 248990287Sobrien else 2490169699Skan pedwarn (" enters scope of non-POD %q+#D", b); 249190287Sobrien } 249290287Sobrien 2493169699Skan if (ent->in_try_scope) 249490287Sobrien error (" enters try block"); 2495169699Skan else if (ent->in_catch_scope && !saw_catch) 249690287Sobrien error (" enters catch block"); 2497169699Skan 2498169699Skan if (ent->in_omp_scope) 2499169699Skan error (" enters OpenMP structured block"); 2500169699Skan else if (flag_openmp) 2501169699Skan { 2502169699Skan struct cp_binding_level *b; 2503169699Skan for (b = current_binding_level; b ; b = b->level_chain) 2504169699Skan { 2505169699Skan if (b == ent->binding_level) 2506169699Skan break; 2507169699Skan if (b->kind == sk_omp) 2508169699Skan { 2509169699Skan if (!identified) 2510169699Skan { 2511169699Skan pedwarn ("jump to label %q+D", decl); 2512169699Skan pedwarn (" from here"); 2513169699Skan identified = true; 2514169699Skan } 2515169699Skan error (" exits OpenMP structured block"); 2516169699Skan break; 2517169699Skan } 2518169699Skan } 2519169699Skan } 252018334Speter} 252118334Speter 2522169699Skan/* Check that a return is ok wrt OpenMP structured blocks. 2523169699Skan Called by finish_return_stmt. Returns true if all is well. */ 2524169699Skan 2525169699Skanbool 2526169699Skancheck_omp_return (void) 2527169699Skan{ 2528169699Skan struct cp_binding_level *b; 2529169699Skan for (b = current_binding_level; b ; b = b->level_chain) 2530169699Skan if (b->kind == sk_omp) 2531169699Skan { 2532169699Skan error ("invalid exit from OpenMP structured block"); 2533169699Skan return false; 2534169699Skan } 2535169699Skan return true; 2536169699Skan} 2537169699Skan 253818334Speter/* Define a label, specifying the location in the source file. 2539122192Skan Return the LABEL_DECL node for the label. */ 254018334Speter 254118334Spetertree 2542132747Skandefine_label (location_t location, tree name) 254318334Speter{ 2544169699Skan struct named_label_entry *ent, dummy; 2545132747Skan struct cp_binding_level *p; 2546169699Skan tree decl; 254718334Speter 2548117410Skan timevar_push (TV_NAME_LOOKUP); 254951412Sobrien 2550169699Skan decl = lookup_label (name); 2551169699Skan 2552169699Skan dummy.label_decl = decl; 2553169699Skan ent = (struct named_label_entry *) htab_find (named_labels, &dummy); 2554169699Skan gcc_assert (ent != NULL); 2555169699Skan 255690287Sobrien /* After labels, make any new cleanups in the function go into their 255718334Speter own new (temporary) binding contour. */ 2558169699Skan for (p = current_binding_level; 2559169699Skan p->kind != sk_function_parms; 2560132747Skan p = p->level_chain) 256190287Sobrien p->more_cleanups_ok = 0; 256218334Speter 256318334Speter if (name == get_identifier ("wchar_t")) 256490287Sobrien pedwarn ("label named wchar_t"); 256518334Speter 256618334Speter if (DECL_INITIAL (decl) != NULL_TREE) 2567169699Skan { 2568169699Skan error ("duplicate label %qD", decl); 2569169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 2570169699Skan } 257118334Speter else 257218334Speter { 2573169699Skan struct named_label_use_entry *use; 2574169699Skan 257518334Speter /* Mark label as having been defined. */ 257618334Speter DECL_INITIAL (decl) = error_mark_node; 257718334Speter /* Say where in the source. */ 2578132747Skan DECL_SOURCE_LOCATION (decl) = location; 2579169699Skan 2580169699Skan ent->binding_level = current_binding_level; 2581169699Skan ent->names_in_scope = current_binding_level->names; 2582169699Skan 2583169699Skan for (use = ent->uses; use ; use = use->next) 2584169699Skan check_previous_goto (decl, use); 2585169699Skan ent->uses = NULL; 258618334Speter } 2587122192Skan 2588169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 258918334Speter} 259018334Speter 259118334Speterstruct cp_switch 259218334Speter{ 2593117410Skan struct cp_binding_level *level; 259418334Speter struct cp_switch *next; 259590287Sobrien /* The SWITCH_STMT being built. */ 259690287Sobrien tree switch_stmt; 259790287Sobrien /* A splay-tree mapping the low element of a case range to the high 259890287Sobrien element, or NULL_TREE if there is no high element. Used to 259990287Sobrien determine whether or not a new case label duplicates an old case 260090287Sobrien label. We need a tree, rather than simply a hash table, because 260190287Sobrien of the GNU case range extension. */ 260290287Sobrien splay_tree cases; 260318334Speter}; 260418334Speter 260590287Sobrien/* A stack of the currently active switch statements. The innermost 260690287Sobrien switch statement is on the top of the stack. There is no need to 260790287Sobrien mark the stack for garbage collection because it is only active 260890287Sobrien during the processing of the body of a function, and we never 260990287Sobrien collect at that point. */ 261090287Sobrien 261118334Speterstatic struct cp_switch *switch_stack; 261218334Speter 261390287Sobrien/* Called right after a switch-statement condition is parsed. 261490287Sobrien SWITCH_STMT is the switch statement being parsed. */ 261590287Sobrien 261618334Spetervoid 2617132747Skanpush_switch (tree switch_stmt) 261818334Speter{ 2619169699Skan struct cp_switch *p = XNEW (struct cp_switch); 262018334Speter p->level = current_binding_level; 262118334Speter p->next = switch_stack; 262290287Sobrien p->switch_stmt = switch_stmt; 262390287Sobrien p->cases = splay_tree_new (case_compare, NULL, NULL); 262418334Speter switch_stack = p; 262518334Speter} 262618334Speter 262718334Spetervoid 2628132747Skanpop_switch (void) 262918334Speter{ 2630169699Skan struct cp_switch *cs = switch_stack; 2631169699Skan location_t switch_location; 263290287Sobrien 2633169699Skan /* Emit warnings as needed. */ 2634169699Skan if (EXPR_HAS_LOCATION (cs->switch_stmt)) 2635169699Skan switch_location = EXPR_LOCATION (cs->switch_stmt); 2636169699Skan else 2637169699Skan switch_location = input_location; 2638169699Skan if (!processing_template_decl) 2639169699Skan c_do_switch_warnings (cs->cases, switch_location, 2640169699Skan SWITCH_STMT_TYPE (cs->switch_stmt), 2641169699Skan SWITCH_STMT_COND (cs->switch_stmt)); 2642169699Skan 264390287Sobrien splay_tree_delete (cs->cases); 264418334Speter switch_stack = switch_stack->next; 264590287Sobrien free (cs); 264618334Speter} 264718334Speter 264890287Sobrien/* Note that we've seen a definition of a case label, and complain if this 264990287Sobrien is a bad place for one. */ 265051412Sobrien 265190287Sobrientree 2652132747Skanfinish_case_label (tree low_value, tree high_value) 265318334Speter{ 265490287Sobrien tree cond, r; 2655132747Skan struct cp_binding_level *p; 265618334Speter 265790287Sobrien if (processing_template_decl) 265818334Speter { 265990287Sobrien tree label; 266090287Sobrien 266190287Sobrien /* For templates, just add the case label; we'll do semantic 266290287Sobrien analysis at instantiation-time. */ 266390287Sobrien label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); 266490287Sobrien return add_stmt (build_case_label (low_value, high_value, label)); 266518334Speter } 266618334Speter 266790287Sobrien /* Find the condition on which this switch statement depends. */ 2668169699Skan cond = SWITCH_STMT_COND (switch_stack->switch_stmt); 266990287Sobrien if (cond && TREE_CODE (cond) == TREE_LIST) 267090287Sobrien cond = TREE_VALUE (cond); 267190287Sobrien 2672169699Skan if (!check_switch_goto (switch_stack->level)) 2673169699Skan return error_mark_node; 267490287Sobrien 2675169699Skan r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond), 2676169699Skan low_value, high_value); 267790287Sobrien 267890287Sobrien /* After labels, make any new cleanups in the function go into their 267918334Speter own new (temporary) binding contour. */ 2680169699Skan for (p = current_binding_level; 2681169699Skan p->kind != sk_function_parms; 2682132747Skan p = p->level_chain) 268390287Sobrien p->more_cleanups_ok = 0; 268418334Speter 268590287Sobrien return r; 268618334Speter} 268718334Speter 268852290Sobrien/* Hash a TYPENAME_TYPE. K is really of type `tree'. */ 268952290Sobrien 2690117410Skanstatic hashval_t 2691132747Skantypename_hash (const void* k) 269252290Sobrien{ 2693117410Skan hashval_t hash; 2694117410Skan tree t = (tree) k; 269552290Sobrien 2696117410Skan hash = (htab_hash_pointer (TYPE_CONTEXT (t)) 2697117410Skan ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t)))); 269852290Sobrien 269952290Sobrien return hash; 270052290Sobrien} 270152290Sobrien 2702169699Skantypedef struct typename_info { 2703169699Skan tree scope; 2704169699Skan tree name; 2705169699Skan tree template_id; 2706169699Skan bool enum_p; 2707169699Skan bool class_p; 2708169699Skan} typename_info; 2709169699Skan 271052290Sobrien/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */ 271152290Sobrien 2712117410Skanstatic int 2713132747Skantypename_compare (const void * k1, const void * k2) 271452290Sobrien{ 271552290Sobrien tree t1; 2716169699Skan const typename_info *t2; 271752290Sobrien 271852290Sobrien t1 = (tree) k1; 2719169699Skan t2 = (const typename_info *) k2; 272090287Sobrien 2721169699Skan return (DECL_NAME (TYPE_NAME (t1)) == t2->name 2722169699Skan && TYPE_CONTEXT (t1) == t2->scope 2723169699Skan && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id 2724169699Skan && TYPENAME_IS_ENUM_P (t1) == t2->enum_p 2725169699Skan && TYPENAME_IS_CLASS_P (t1) == t2->class_p); 272652290Sobrien} 272752290Sobrien 272852290Sobrien/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is 2729169699Skan the type of `T', NAME is the IDENTIFIER_NODE for `t'. 273052290Sobrien 273152290Sobrien Returns the new TYPENAME_TYPE. */ 273252290Sobrien 2733117410Skanstatic GTY ((param_is (union tree_node))) htab_t typename_htab; 2734117410Skan 2735132747Skanstatic tree 2736169699Skanbuild_typename_type (tree context, tree name, tree fullname, 2737169699Skan enum tag_types tag_type) 273852290Sobrien{ 273952290Sobrien tree t; 274052290Sobrien tree d; 2741169699Skan typename_info ti; 2742132747Skan void **e; 2743169699Skan hashval_t hash; 274452290Sobrien 2745117410Skan if (typename_htab == NULL) 2746169699Skan typename_htab = htab_create_ggc (61, &typename_hash, 2747169699Skan &typename_compare, NULL); 274852290Sobrien 2749169699Skan ti.scope = FROB_CONTEXT (context); 2750169699Skan ti.name = name; 2751169699Skan ti.template_id = fullname; 2752169699Skan ti.enum_p = tag_type == enum_type; 2753169699Skan ti.class_p = (tag_type == class_type 2754169699Skan || tag_type == record_type 2755169699Skan || tag_type == union_type); 2756169699Skan hash = (htab_hash_pointer (ti.scope) 2757169699Skan ^ htab_hash_pointer (ti.name)); 275852290Sobrien 275952290Sobrien /* See if we already have this type. */ 2760169699Skan e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT); 2761117410Skan if (*e) 2762117410Skan t = (tree) *e; 276352290Sobrien else 2764169699Skan { 2765169699Skan /* Build the TYPENAME_TYPE. */ 2766169699Skan t = make_aggr_type (TYPENAME_TYPE); 2767169699Skan TYPE_CONTEXT (t) = ti.scope; 2768169699Skan TYPENAME_TYPE_FULLNAME (t) = ti.template_id; 2769169699Skan TYPENAME_IS_ENUM_P (t) = ti.enum_p; 2770169699Skan TYPENAME_IS_CLASS_P (t) = ti.class_p; 277152290Sobrien 2772169699Skan /* Build the corresponding TYPE_DECL. */ 2773169699Skan d = build_decl (TYPE_DECL, name, t); 2774169699Skan TYPE_NAME (TREE_TYPE (d)) = d; 2775169699Skan TYPE_STUB_DECL (TREE_TYPE (d)) = d; 2776169699Skan DECL_CONTEXT (d) = FROB_CONTEXT (context); 2777169699Skan DECL_ARTIFICIAL (d) = 1; 2778169699Skan 2779169699Skan /* Store it in the hash table. */ 2780169699Skan *e = t; 2781169699Skan } 2782169699Skan 278352290Sobrien return t; 278452290Sobrien} 278552290Sobrien 2786169699Skan/* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag 2787169699Skan provided to name the type. Returns an appropriate type, unless an 2788169699Skan error occurs, in which case error_mark_node is returned. If we 2789169699Skan locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we 2790169699Skan return that, rather than the _TYPE it corresponds to, in other 2791169699Skan cases we look through the type decl. If TF_ERROR is set, complain 2792169699Skan about errors, otherwise be quiet. */ 279390287Sobrien 279452290Sobrientree 2795169699Skanmake_typename_type (tree context, tree name, enum tag_types tag_type, 2796169699Skan tsubst_flags_t complain) 279751412Sobrien{ 279851412Sobrien tree fullname; 2799169699Skan tree t; 2800169699Skan bool want_template; 280151412Sobrien 2802132747Skan if (name == error_mark_node 2803132747Skan || context == NULL_TREE 2804132747Skan || context == error_mark_node) 2805132747Skan return error_mark_node; 2806132747Skan 280790287Sobrien if (TYPE_P (name)) 280852290Sobrien { 280990287Sobrien if (!(TYPE_LANG_SPECIFIC (name) 281090287Sobrien && (CLASSTYPE_IS_TEMPLATE (name) 281152290Sobrien || CLASSTYPE_USE_TEMPLATE (name)))) 281252290Sobrien name = TYPE_IDENTIFIER (name); 281352290Sobrien else 281452290Sobrien /* Create a TEMPLATE_ID_EXPR for the type. */ 281552290Sobrien name = build_nt (TEMPLATE_ID_EXPR, 281652290Sobrien CLASSTYPE_TI_TEMPLATE (name), 281752290Sobrien CLASSTYPE_TI_ARGS (name)); 281852290Sobrien } 281951412Sobrien else if (TREE_CODE (name) == TYPE_DECL) 282051412Sobrien name = DECL_NAME (name); 282151412Sobrien 282251412Sobrien fullname = name; 282351412Sobrien 282451412Sobrien if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 282551412Sobrien { 282651412Sobrien name = TREE_OPERAND (name, 0); 282751412Sobrien if (TREE_CODE (name) == TEMPLATE_DECL) 282851412Sobrien name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); 2829169699Skan else if (TREE_CODE (name) == OVERLOAD) 2830169699Skan { 2831169699Skan error ("%qD is not a type", name); 2832169699Skan return error_mark_node; 2833169699Skan } 283451412Sobrien } 283590287Sobrien if (TREE_CODE (name) == TEMPLATE_DECL) 283690287Sobrien { 2837169699Skan error ("%qD used without template parameters", name); 283890287Sobrien return error_mark_node; 283990287Sobrien } 2840169699Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 2841169699Skan gcc_assert (TYPE_P (context)); 284252290Sobrien 2843169699Skan /* When the CONTEXT is a dependent type, NAME could refer to a 2844169699Skan dependent base class of CONTEXT. So we cannot peek inside it, 2845169699Skan even if CONTEXT is a currently open scope. */ 2846169699Skan if (dependent_type_p (context)) 2847169699Skan return build_typename_type (context, name, fullname, tag_type); 2848169699Skan 2849169699Skan if (!IS_AGGR_TYPE (context)) 285051412Sobrien { 2851169699Skan if (complain & tf_error) 2852169699Skan error ("%q#T is not a class", context); 2853169699Skan return error_mark_node; 285451412Sobrien } 2855169699Skan 2856169699Skan want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR; 2857169699Skan 2858169699Skan /* We should only set WANT_TYPE when we're a nested typename type. 2859169699Skan Then we can give better diagnostics if we find a non-type. */ 2860169699Skan t = lookup_field (context, name, 0, /*want_type=*/true); 2861169699Skan if (!t) 286252290Sobrien { 286396292Sobrien if (complain & tf_error) 2864169699Skan error (want_template ? "no class template named %q#T in %q#T" 2865169699Skan : "no type named %q#T in %q#T", name, context); 286652290Sobrien return error_mark_node; 286752290Sobrien } 2868169699Skan 2869169699Skan if (want_template && !DECL_CLASS_TEMPLATE_P (t)) 2870169699Skan { 2871169699Skan if (complain & tf_error) 2872169699Skan error ("%<typename %T::%D%> names %q#T, which is not a class template", 2873169699Skan context, name, t); 2874169699Skan return error_mark_node; 2875169699Skan } 2876169699Skan if (!want_template && TREE_CODE (t) != TYPE_DECL) 2877169699Skan { 2878169699Skan if (complain & tf_error) 2879169699Skan error ("%<typename %T::%D%> names %q#T, which is not a type", 2880169699Skan context, name, t); 2881169699Skan return error_mark_node; 2882169699Skan } 2883169699Skan 2884169699Skan if (complain & tf_error) 2885169699Skan perform_or_defer_access_check (TYPE_BINFO (context), t, t); 288690287Sobrien 2887169699Skan if (want_template) 2888169699Skan return lookup_template_class (t, TREE_OPERAND (fullname, 1), 2889169699Skan NULL_TREE, context, 2890169699Skan /*entering_scope=*/0, 2891169699Skan tf_warning_or_error | tf_user); 2892169699Skan 2893169699Skan if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) 2894169699Skan t = TREE_TYPE (t); 2895169699Skan 2896169699Skan return t; 289751412Sobrien} 289851412Sobrien 2899169699Skan/* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name 2900169699Skan can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs, 2901169699Skan in which case error_mark_node is returned. 290290287Sobrien 2903169699Skan If PARM_LIST is non-NULL, also make sure that the template parameter 2904169699Skan list of TEMPLATE_DECL matches. 2905169699Skan 2906169699Skan If COMPLAIN zero, don't complain about any errors that occur. */ 2907169699Skan 290890287Sobrientree 2909169699Skanmake_unbound_class_template (tree context, tree name, tree parm_list, 2910169699Skan tsubst_flags_t complain) 291190287Sobrien{ 291290287Sobrien tree t; 291390287Sobrien tree d; 291490287Sobrien 291590287Sobrien if (TYPE_P (name)) 291690287Sobrien name = TYPE_IDENTIFIER (name); 291790287Sobrien else if (DECL_P (name)) 291890287Sobrien name = DECL_NAME (name); 2919169699Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 292090287Sobrien 2921132747Skan if (!dependent_type_p (context) 292290287Sobrien || currently_open_class (context)) 292390287Sobrien { 292490287Sobrien tree tmpl = NULL_TREE; 292590287Sobrien 292690287Sobrien if (IS_AGGR_TYPE (context)) 2927132747Skan tmpl = lookup_field (context, name, 0, false); 292890287Sobrien 292990287Sobrien if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) 293090287Sobrien { 2931117410Skan if (complain & tf_error) 2932169699Skan error ("no class template named %q#T in %q#T", name, context); 293390287Sobrien return error_mark_node; 293490287Sobrien } 2935169699Skan 2936169699Skan if (parm_list 2937169699Skan && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list)) 2938169699Skan { 2939169699Skan if (complain & tf_error) 2940169699Skan { 2941169699Skan error ("template parameters do not match template"); 2942169699Skan error ("%q+D declared here", tmpl); 2943169699Skan } 2944169699Skan return error_mark_node; 2945169699Skan } 2946169699Skan 2947117410Skan if (complain & tf_error) 2948169699Skan perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl); 294990287Sobrien 295090287Sobrien return tmpl; 295190287Sobrien } 295290287Sobrien 295390287Sobrien /* Build the UNBOUND_CLASS_TEMPLATE. */ 295490287Sobrien t = make_aggr_type (UNBOUND_CLASS_TEMPLATE); 295590287Sobrien TYPE_CONTEXT (t) = FROB_CONTEXT (context); 295690287Sobrien TREE_TYPE (t) = NULL_TREE; 295790287Sobrien 295890287Sobrien /* Build the corresponding TEMPLATE_DECL. */ 295990287Sobrien d = build_decl (TEMPLATE_DECL, name, t); 296090287Sobrien TYPE_NAME (TREE_TYPE (d)) = d; 296190287Sobrien TYPE_STUB_DECL (TREE_TYPE (d)) = d; 296290287Sobrien DECL_CONTEXT (d) = FROB_CONTEXT (context); 296390287Sobrien DECL_ARTIFICIAL (d) = 1; 2964169699Skan DECL_TEMPLATE_PARMS (d) = parm_list; 296590287Sobrien 296690287Sobrien return t; 296790287Sobrien} 296890287Sobrien 2969132747Skan 297051412Sobrien 297118334Speter/* Push the declarations of builtin types into the namespace. 2972132747Skan RID_INDEX is the index of the builtin type in the array 2973132747Skan RID_POINTERS. NAME is the name used when looking up the builtin 2974132747Skan type. TYPE is the _TYPE node for the builtin type. */ 297518334Speter 297690287Sobrienvoid 2977169699Skanrecord_builtin_type (enum rid rid_index, 2978169699Skan const char* name, 2979169699Skan tree type) 298018334Speter{ 298118334Speter tree rname = NULL_TREE, tname = NULL_TREE; 298251412Sobrien tree tdecl = NULL_TREE; 298318334Speter 298418334Speter if ((int) rid_index < (int) RID_MAX) 298518334Speter rname = ridpointers[(int) rid_index]; 298618334Speter if (name) 298718334Speter tname = get_identifier (name); 298818334Speter 2989132747Skan /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be 2990132747Skan eliminated. Built-in types should not be looked up name; their 2991132747Skan names are keywords that the parser can recognize. However, there 2992132747Skan is code in c-common.c that uses identifier_global_value to look 2993132747Skan up built-in types by name. */ 299418334Speter if (tname) 299518334Speter { 2996132747Skan tdecl = build_decl (TYPE_DECL, tname, type); 2997132747Skan DECL_ARTIFICIAL (tdecl) = 1; 2998132747Skan SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl); 299918334Speter } 3000132747Skan if (rname) 300118334Speter { 3002132747Skan if (!tdecl) 300318334Speter { 3004132747Skan tdecl = build_decl (TYPE_DECL, rname, type); 3005132747Skan DECL_ARTIFICIAL (tdecl) = 1; 300618334Speter } 3007132747Skan SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl); 300818334Speter } 3009132747Skan 3010132747Skan if (!TYPE_NAME (type)) 3011132747Skan TYPE_NAME (type) = tdecl; 3012132747Skan 3013132747Skan if (tdecl) 3014169699Skan debug_hooks->type_decl (tdecl, 0); 301551412Sobrien} 301618334Speter 301751412Sobrien/* Record one of the standard Java types. 301851412Sobrien * Declare it as having the given NAME. 301951412Sobrien * If SIZE > 0, it is the size of one of the integral types; 302051412Sobrien * otherwise it is the negative of the size of one of the other types. */ 302151412Sobrien 302251412Sobrienstatic tree 3023132747Skanrecord_builtin_java_type (const char* name, int size) 302451412Sobrien{ 302551412Sobrien tree type, decl; 302651412Sobrien if (size > 0) 302751412Sobrien type = make_signed_type (size); 302851412Sobrien else if (size > -32) 3029117410Skan { /* "__java_char" or ""__java_boolean". */ 303051412Sobrien type = make_unsigned_type (-size); 303151412Sobrien /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/ 303218334Speter } 303351412Sobrien else 3034117410Skan { /* "__java_float" or ""__java_double". */ 303551412Sobrien type = make_node (REAL_TYPE); 303651412Sobrien TYPE_PRECISION (type) = - size; 303751412Sobrien layout_type (type); 303851412Sobrien } 303951412Sobrien record_builtin_type (RID_MAX, name, type); 304051412Sobrien decl = TYPE_NAME (type); 304152290Sobrien 304252290Sobrien /* Suppress generate debug symbol entries for these types, 304352290Sobrien since for normal C++ they are just clutter. 3044117410Skan However, push_lang_context undoes this if extern "Java" is seen. */ 304551412Sobrien DECL_IGNORED_P (decl) = 1; 304652290Sobrien 304751412Sobrien TYPE_FOR_JAVA (type) = 1; 304851412Sobrien return type; 304918334Speter} 305018334Speter 3051117410Skan/* Push a type into the namespace so that the back-ends ignore it. */ 305251412Sobrien 305318334Speterstatic void 3054132747Skanrecord_unknown_type (tree type, const char* name) 305518334Speter{ 305651412Sobrien tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type)); 305751412Sobrien /* Make sure the "unknown type" typedecl gets ignored for debug info. */ 305851412Sobrien DECL_IGNORED_P (decl) = 1; 305951412Sobrien TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 306051412Sobrien TYPE_SIZE (type) = TYPE_SIZE (void_type_node); 306151412Sobrien TYPE_ALIGN (type) = 1; 306290287Sobrien TYPE_USER_ALIGN (type) = 0; 306351412Sobrien TYPE_MODE (type) = TYPE_MODE (void_type_node); 306490287Sobrien} 306518334Speter 3066169699Skan/* A string for which we should create an IDENTIFIER_NODE at 306790287Sobrien startup. */ 306851412Sobrien 306990287Sobrientypedef struct predefined_identifier 307090287Sobrien{ 307190287Sobrien /* The name of the identifier. */ 307290287Sobrien const char *const name; 307390287Sobrien /* The place where the IDENTIFIER_NODE should be stored. */ 307490287Sobrien tree *const node; 3075117410Skan /* Nonzero if this is the name of a constructor or destructor. */ 307690287Sobrien const int ctor_or_dtor_p; 307790287Sobrien} predefined_identifier; 307890287Sobrien 307990287Sobrien/* Create all the predefined identifiers. */ 308090287Sobrien 308118334Speterstatic void 3082132747Skaninitialize_predefined_identifiers (void) 308318334Speter{ 308490287Sobrien const predefined_identifier *pid; 308518334Speter 308690287Sobrien /* A table of identifiers to create at startup. */ 308790287Sobrien static const predefined_identifier predefined_identifiers[] = { 308890287Sobrien { "C++", &lang_name_cplusplus, 0 }, 308990287Sobrien { "C", &lang_name_c, 0 }, 309090287Sobrien { "Java", &lang_name_java, 0 }, 3091169699Skan /* Some of these names have a trailing space so that it is 3092169699Skan impossible for them to conflict with names written by users. */ 3093169699Skan { "__ct ", &ctor_identifier, 1 }, 3094169699Skan { "__base_ctor ", &base_ctor_identifier, 1 }, 3095169699Skan { "__comp_ctor ", &complete_ctor_identifier, 1 }, 3096169699Skan { "__dt ", &dtor_identifier, 1 }, 3097169699Skan { "__comp_dtor ", &complete_dtor_identifier, 1 }, 3098169699Skan { "__base_dtor ", &base_dtor_identifier, 1 }, 3099169699Skan { "__deleting_dtor ", &deleting_dtor_identifier, 1 }, 310090287Sobrien { IN_CHARGE_NAME, &in_charge_identifier, 0 }, 310190287Sobrien { "nelts", &nelts_identifier, 0 }, 310290287Sobrien { THIS_NAME, &this_identifier, 0 }, 310390287Sobrien { VTABLE_DELTA_NAME, &delta_identifier, 0 }, 310490287Sobrien { VTABLE_PFN_NAME, &pfn_identifier, 0 }, 310590287Sobrien { "_vptr", &vptr_identifier, 0 }, 310690287Sobrien { "__vtt_parm", &vtt_parm_identifier, 0 }, 3107132747Skan { "::", &global_scope_name, 0 }, 310890287Sobrien { "std", &std_identifier, 0 }, 310990287Sobrien { NULL, NULL, 0 } 311090287Sobrien }; 311190287Sobrien 311290287Sobrien for (pid = predefined_identifiers; pid->name; ++pid) 311390287Sobrien { 311490287Sobrien *pid->node = get_identifier (pid->name); 311590287Sobrien if (pid->ctor_or_dtor_p) 311690287Sobrien IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1; 311790287Sobrien } 311818334Speter} 311918334Speter 312018334Speter/* Create the predefined scalar types of C, 312118334Speter and some nodes representing standard constants (0, 1, (void *)0). 312218334Speter Initialize the global binding level. 312318334Speter Make definitions for built-in primitive functions. */ 312418334Speter 312518334Spetervoid 3126132747Skancxx_init_decl_processing (void) 312718334Speter{ 312890287Sobrien tree void_ftype; 312990287Sobrien tree void_ftype_ptr; 313018334Speter 3131169699Skan build_common_tree_nodes (flag_signed_char, false); 3132169699Skan 313390287Sobrien /* Create all the identifiers we need. */ 313490287Sobrien initialize_predefined_identifiers (); 313518334Speter 313690287Sobrien /* Create the global variables. */ 313790287Sobrien push_to_top_level (); 313890287Sobrien 3139132747Skan current_function_decl = NULL_TREE; 3140132747Skan current_binding_level = NULL; 3141117410Skan /* Enter the global namespace. */ 3142169699Skan gcc_assert (global_namespace == NULL_TREE); 3143132747Skan global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name, 3144169699Skan void_type_node); 3145169699Skan TREE_PUBLIC (global_namespace) = 1; 3146132747Skan begin_scope (sk_namespace, global_namespace); 3147132747Skan 314851412Sobrien current_lang_name = NULL_TREE; 314951412Sobrien 315090287Sobrien /* Adjust various flags based on command-line settings. */ 3151117410Skan if (!flag_permissive) 315252290Sobrien flag_pedantic_errors = 1; 315390287Sobrien if (!flag_no_inline) 315490287Sobrien { 315590287Sobrien flag_inline_trees = 1; 315690287Sobrien flag_no_inline = 1; 315790287Sobrien } 315890287Sobrien if (flag_inline_functions) 3159169699Skan flag_inline_trees = 2; 316018334Speter 3161104769Skan /* Force minimum function alignment if using the least significant 3162104769Skan bit of function pointers to store the virtual bit. */ 3163104769Skan if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn 3164104769Skan && force_align_functions_log < 1) 3165104769Skan force_align_functions_log = 1; 3166104769Skan 316718334Speter /* Initially, C. */ 316818334Speter current_lang_name = lang_name_c; 316918334Speter 317090287Sobrien /* Create the `std' namespace. */ 317190287Sobrien push_namespace (std_identifier); 317290287Sobrien std_node = current_namespace; 317390287Sobrien pop_namespace (); 317418334Speter 317590287Sobrien c_common_nodes_and_builtins (); 317618334Speter 317751412Sobrien java_byte_type_node = record_builtin_java_type ("__java_byte", 8); 317851412Sobrien java_short_type_node = record_builtin_java_type ("__java_short", 16); 317951412Sobrien java_int_type_node = record_builtin_java_type ("__java_int", 32); 318051412Sobrien java_long_type_node = record_builtin_java_type ("__java_long", 64); 318151412Sobrien java_float_type_node = record_builtin_java_type ("__java_float", -32); 318251412Sobrien java_double_type_node = record_builtin_java_type ("__java_double", -64); 318351412Sobrien java_char_type_node = record_builtin_java_type ("__java_char", -16); 318451412Sobrien java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1); 318551412Sobrien 3186169699Skan integer_two_node = build_int_cst (NULL_TREE, 2); 3187169699Skan integer_three_node = build_int_cst (NULL_TREE, 3); 318818334Speter 318918334Speter record_builtin_type (RID_BOOL, "bool", boolean_type_node); 3190132747Skan truthvalue_type_node = boolean_type_node; 3191132747Skan truthvalue_false_node = boolean_false_node; 3192132747Skan truthvalue_true_node = boolean_true_node; 319318334Speter 319490287Sobrien empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); 319518334Speter 319651412Sobrien#if 0 319790287Sobrien record_builtin_type (RID_MAX, NULL, string_type_node); 319851412Sobrien#endif 319918334Speter 320090287Sobrien delta_type_node = ptrdiff_type_node; 320190287Sobrien vtable_index_type = ptrdiff_type_node; 320218334Speter 320390287Sobrien vtt_parm_type = build_pointer_type (const_ptr_type_node); 320490287Sobrien void_ftype = build_function_type (void_type_node, void_list_node); 320590287Sobrien void_ftype_ptr = build_function_type (void_type_node, 320690287Sobrien tree_cons (NULL_TREE, 3207169699Skan ptr_type_node, 320890287Sobrien void_list_node)); 320951412Sobrien void_ftype_ptr 321090287Sobrien = build_exception_variant (void_ftype_ptr, empty_except_spec); 321151412Sobrien 321218334Speter /* C++ extensions */ 321318334Speter 321418334Speter unknown_type_node = make_node (UNKNOWN_TYPE); 321551412Sobrien record_unknown_type (unknown_type_node, "unknown type"); 321651412Sobrien 321718334Speter /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ 321818334Speter TREE_TYPE (unknown_type_node) = unknown_type_node; 321951412Sobrien 322051412Sobrien /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same 322151412Sobrien result. */ 322218334Speter TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; 322318334Speter TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; 322418334Speter 322590287Sobrien { 322690287Sobrien /* Make sure we get a unique function type, so we can give 322790287Sobrien its pointer type a name. (This wins for gdb.) */ 322890287Sobrien tree vfunc_type = make_node (FUNCTION_TYPE); 322990287Sobrien TREE_TYPE (vfunc_type) = integer_type_node; 323090287Sobrien TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; 323190287Sobrien layout_type (vfunc_type); 323218334Speter 323390287Sobrien vtable_entry_type = build_pointer_type (vfunc_type); 323490287Sobrien } 323518334Speter record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type); 323618334Speter 323718334Speter vtbl_type_node 323852290Sobrien = build_cplus_array_type (vtable_entry_type, NULL_TREE); 323918334Speter layout_type (vtbl_type_node); 324052290Sobrien vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); 324190287Sobrien record_builtin_type (RID_MAX, NULL, vtbl_type_node); 324252290Sobrien vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); 324352290Sobrien layout_type (vtbl_ptr_type_node); 324490287Sobrien record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); 324518334Speter 324690287Sobrien push_namespace (get_identifier ("__cxxabiv1")); 324790287Sobrien abi_node = current_namespace; 324890287Sobrien pop_namespace (); 324960970Sobrien 325051412Sobrien global_type_node = make_node (LANG_TYPE); 325151412Sobrien record_unknown_type (global_type_node, "global type"); 325218334Speter 325318334Speter /* Now, C++. */ 325418334Speter current_lang_name = lang_name_cplusplus; 325518334Speter 325651412Sobrien { 3257132747Skan tree bad_alloc_id; 3258132747Skan tree bad_alloc_type_node; 3259132747Skan tree bad_alloc_decl; 3260132747Skan tree newtype, deltype; 326190287Sobrien tree ptr_ftype_sizetype; 326290287Sobrien 326390287Sobrien push_namespace (std_identifier); 3264132747Skan bad_alloc_id = get_identifier ("bad_alloc"); 3265132747Skan bad_alloc_type_node = make_aggr_type (RECORD_TYPE); 3266132747Skan TYPE_CONTEXT (bad_alloc_type_node) = current_namespace; 3267169699Skan bad_alloc_decl 3268132747Skan = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node); 3269132747Skan DECL_CONTEXT (bad_alloc_decl) = current_namespace; 3270132747Skan TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl; 327190287Sobrien pop_namespace (); 3272169699Skan 3273169699Skan ptr_ftype_sizetype 327490287Sobrien = build_function_type (ptr_type_node, 327590287Sobrien tree_cons (NULL_TREE, 3276110631Skan size_type_node, 327790287Sobrien void_list_node)); 327851412Sobrien newtype = build_exception_variant 327990287Sobrien (ptr_ftype_sizetype, add_exception_specifier 328090287Sobrien (NULL_TREE, bad_alloc_type_node, -1)); 328190287Sobrien deltype = build_exception_variant (void_ftype_ptr, empty_except_spec); 328290287Sobrien push_cp_library_fn (NEW_EXPR, newtype); 328390287Sobrien push_cp_library_fn (VEC_NEW_EXPR, newtype); 328490287Sobrien global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype); 328590287Sobrien push_cp_library_fn (VEC_DELETE_EXPR, deltype); 328651412Sobrien } 328718334Speter 328818334Speter abort_fndecl 328990287Sobrien = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype); 329018334Speter 329118334Speter /* Perform other language dependent initializations. */ 329218334Speter init_class_processing (); 329390287Sobrien init_rtti_processing (); 329418334Speter 329551412Sobrien if (flag_exceptions) 329618334Speter init_exception_processing (); 329718334Speter 329851412Sobrien if (! supports_one_only ()) 329951412Sobrien flag_weak = 0; 330051412Sobrien 330190287Sobrien make_fname_decl = cp_make_fname_decl; 330290287Sobrien start_fname_decls (); 330318334Speter 330451412Sobrien /* Show we use EH for cleanups. */ 3305169699Skan if (flag_exceptions) 3306169699Skan using_eh_for_cleanups (); 330718334Speter} 330818334Speter 330990287Sobrien/* Generate an initializer for a function naming variable from 3310132747Skan NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is 3311132747Skan filled in with the type of the init. */ 331218334Speter 331390287Sobrientree 3314132747Skancp_fname_init (const char* name, tree *type_p) 331518334Speter{ 331690287Sobrien tree domain = NULL_TREE; 331790287Sobrien tree type; 331890287Sobrien tree init = NULL_TREE; 331990287Sobrien size_t length = 0; 332090287Sobrien 332190287Sobrien if (name) 332290287Sobrien { 332390287Sobrien length = strlen (name); 332490287Sobrien domain = build_index_type (size_int (length)); 332590287Sobrien init = build_string (length + 1, name); 332690287Sobrien } 3327169699Skan 332890287Sobrien type = build_qualified_type (char_type_node, TYPE_QUAL_CONST); 332990287Sobrien type = build_cplus_array_type (type, domain); 333090287Sobrien 3331132747Skan *type_p = type; 3332169699Skan 333390287Sobrien if (init) 333490287Sobrien TREE_TYPE (init) = type; 333590287Sobrien else 3336132747Skan init = error_mark_node; 3337169699Skan 333890287Sobrien return init; 333951412Sobrien} 334018334Speter 334190287Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 334290287Sobrien decl, NAME is the initialization string and TYPE_DEP indicates whether 334390287Sobrien NAME depended on the type of the function. We make use of that to detect 334490287Sobrien __PRETTY_FUNCTION__ inside a template fn. This is being done 3345169699Skan lazily at the point of first use, so we mustn't push the decl now. */ 334690287Sobrien 334790287Sobrienstatic tree 3348132747Skancp_make_fname_decl (tree id, int type_dep) 334990287Sobrien{ 335090287Sobrien const char *const name = (type_dep && processing_template_decl 3351132747Skan ? NULL : fname_as_string (type_dep)); 3352132747Skan tree type; 3353132747Skan tree init = cp_fname_init (name, &type); 3354132747Skan tree decl = build_decl (VAR_DECL, id, type); 335590287Sobrien 3356169699Skan if (name) 3357169699Skan free ((char *) name); 3358169699Skan 3359132747Skan /* As we're using pushdecl_with_scope, we must set the context. */ 336090287Sobrien DECL_CONTEXT (decl) = current_function_decl; 336190287Sobrien DECL_PRETTY_FUNCTION_P (decl) = type_dep; 3362169699Skan 336390287Sobrien TREE_STATIC (decl) = 1; 336490287Sobrien TREE_READONLY (decl) = 1; 336590287Sobrien DECL_ARTIFICIAL (decl) = 1; 3366169699Skan 336790287Sobrien TREE_USED (decl) = 1; 336890287Sobrien 3369132747Skan if (current_function_decl) 3370132747Skan { 3371132747Skan struct cp_binding_level *b = current_binding_level; 3372132747Skan while (b->level_chain->kind != sk_function_parms) 3373132747Skan b = b->level_chain; 3374169699Skan pushdecl_with_scope (decl, b, /*is_friend=*/false); 3375169699Skan cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE, 3376169699Skan LOOKUP_ONLYCONVERTING); 3377132747Skan } 3378132747Skan else 3379132747Skan pushdecl_top_level_and_finish (decl, init); 3380169699Skan 338190287Sobrien return decl; 338290287Sobrien} 338390287Sobrien 338496292Sobrien/* Make a definition for a builtin function named NAME in the current 338596292Sobrien namespace, whose data type is TYPE and whose context is CONTEXT. 338696292Sobrien TYPE should be a function type with argument types. 338790287Sobrien 338890287Sobrien CLASS and CODE tell later passes how to compile calls to this function. 338990287Sobrien See tree.h for possible values. 339090287Sobrien 339190287Sobrien If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, 3392117410Skan the name to be called if we can't opencode the function. 3393117410Skan If ATTRS is nonzero, use that for the function's attribute 3394117410Skan list. */ 339518334Speter 339696292Sobrienstatic tree 3397132747Skanbuiltin_function_1 (const char* name, 3398169699Skan tree type, 3399169699Skan tree context, 3400169699Skan enum built_in_function code, 3401169699Skan enum built_in_class class, 3402169699Skan const char* libname, 3403169699Skan tree attrs) 340418334Speter{ 340590287Sobrien tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type); 340690287Sobrien DECL_BUILT_IN_CLASS (decl) = class; 340790287Sobrien DECL_FUNCTION_CODE (decl) = code; 340896292Sobrien DECL_CONTEXT (decl) = context; 340918334Speter 341090287Sobrien pushdecl (decl); 341190287Sobrien 341218334Speter /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME, 341318334Speter we cannot change DECL_ASSEMBLER_NAME until we have installed this 341418334Speter function in the namespace. */ 341590287Sobrien if (libname) 341690287Sobrien SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname)); 341790287Sobrien 3418169699Skan /* A function in the user's namespace should have an explicit 3419169699Skan declaration before it is used. Mark the built-in function as 3420169699Skan anticipated but not actually declared. */ 342190287Sobrien if (name[0] != '_' || name[1] != '_') 342290287Sobrien DECL_ANTICIPATED (decl) = 1; 342390287Sobrien 342490287Sobrien /* Possibly apply some default attributes to this built-in function. */ 3425117410Skan if (attrs) 3426117410Skan decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 3427117410Skan else 3428117410Skan decl_attributes (&decl, NULL_TREE, 0); 342990287Sobrien 343018334Speter return decl; 343118334Speter} 343290287Sobrien 343396292Sobrien/* Entry point for the benefit of c_common_nodes_and_builtins. 343496292Sobrien 3435132747Skan Make a definition for a builtin function named NAME and whose data type 343696292Sobrien is TYPE. TYPE should be a function type with argument types. This 343796292Sobrien function places the anticipated declaration in the global namespace 343896292Sobrien and additionally in the std namespace if appropriate. 343996292Sobrien 344096292Sobrien CLASS and CODE tell later passes how to compile calls to this function. 344196292Sobrien See tree.h for possible values. 344296292Sobrien 344396292Sobrien If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, 3444117410Skan the name to be called if we can't opencode the function. 344596292Sobrien 3446117410Skan If ATTRS is nonzero, use that for the function's attribute 3447117410Skan list. */ 3448117410Skan 344996292Sobrientree 3450132747Skanbuiltin_function (const char* name, 3451169699Skan tree type, 3452169699Skan int code, 3453169699Skan enum built_in_class cl, 3454169699Skan const char* libname, 3455169699Skan tree attrs) 345696292Sobrien{ 345796292Sobrien /* All builtins that don't begin with an '_' should additionally 345896292Sobrien go in the 'std' namespace. */ 345996292Sobrien if (name[0] != '_') 346096292Sobrien { 346196292Sobrien push_namespace (std_identifier); 3462169699Skan builtin_function_1 (name, type, std_node, code, cl, libname, attrs); 346396292Sobrien pop_namespace (); 346496292Sobrien } 346596292Sobrien 3466117410Skan return builtin_function_1 (name, type, NULL_TREE, code, 3467169699Skan cl, libname, attrs); 346896292Sobrien} 346996292Sobrien 347090287Sobrien/* Generate a FUNCTION_DECL with the typical flags for a runtime library 347190287Sobrien function. Not called directly. */ 347290287Sobrien 347390287Sobrienstatic tree 3474132747Skanbuild_library_fn_1 (tree name, enum tree_code operator_code, tree type) 347590287Sobrien{ 347690287Sobrien tree fn = build_lang_decl (FUNCTION_DECL, name, type); 347790287Sobrien DECL_EXTERNAL (fn) = 1; 347890287Sobrien TREE_PUBLIC (fn) = 1; 347990287Sobrien DECL_ARTIFICIAL (fn) = 1; 348090287Sobrien SET_OVERLOADED_OPERATOR_CODE (fn, operator_code); 348190287Sobrien SET_DECL_LANGUAGE (fn, lang_c); 3482169699Skan /* Runtime library routines are, by definition, available in an 3483169699Skan external shared object. */ 3484169699Skan DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT; 3485169699Skan DECL_VISIBILITY_SPECIFIED (fn) = 1; 348690287Sobrien return fn; 348790287Sobrien} 348890287Sobrien 348990287Sobrien/* Returns the _DECL for a library function with C linkage. 349090287Sobrien We assume that such functions never throw; if this is incorrect, 349190287Sobrien callers should unset TREE_NOTHROW. */ 349290287Sobrien 349390287Sobrientree 3494132747Skanbuild_library_fn (tree name, tree type) 349590287Sobrien{ 3496169699Skan tree fn = build_library_fn_1 (name, ERROR_MARK, type); 3497169699Skan TREE_NOTHROW (fn) = 1; 3498169699Skan return fn; 349990287Sobrien} 350090287Sobrien 350190287Sobrien/* Returns the _DECL for a library function with C++ linkage. */ 350290287Sobrien 350390287Sobrienstatic tree 3504132747Skanbuild_cp_library_fn (tree name, enum tree_code operator_code, tree type) 350590287Sobrien{ 350690287Sobrien tree fn = build_library_fn_1 (name, operator_code, type); 350790287Sobrien TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type); 350890287Sobrien DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); 350990287Sobrien SET_DECL_LANGUAGE (fn, lang_cplusplus); 351090287Sobrien return fn; 351190287Sobrien} 351290287Sobrien 351390287Sobrien/* Like build_library_fn, but takes a C string instead of an 351490287Sobrien IDENTIFIER_NODE. */ 351590287Sobrien 351690287Sobrientree 3517132747Skanbuild_library_fn_ptr (const char* name, tree type) 351890287Sobrien{ 351990287Sobrien return build_library_fn (get_identifier (name), type); 352090287Sobrien} 352190287Sobrien 352290287Sobrien/* Like build_cp_library_fn, but takes a C string instead of an 352390287Sobrien IDENTIFIER_NODE. */ 352490287Sobrien 352590287Sobrientree 3526132747Skanbuild_cp_library_fn_ptr (const char* name, tree type) 352790287Sobrien{ 352890287Sobrien return build_cp_library_fn (get_identifier (name), ERROR_MARK, type); 352990287Sobrien} 353090287Sobrien 353190287Sobrien/* Like build_library_fn, but also pushes the function so that we will 353290287Sobrien be able to find it via IDENTIFIER_GLOBAL_VALUE. */ 353390287Sobrien 353490287Sobrientree 3535132747Skanpush_library_fn (tree name, tree type) 353690287Sobrien{ 353790287Sobrien tree fn = build_library_fn (name, type); 353890287Sobrien pushdecl_top_level (fn); 353990287Sobrien return fn; 354090287Sobrien} 354190287Sobrien 354290287Sobrien/* Like build_cp_library_fn, but also pushes the function so that it 354390287Sobrien will be found by normal lookup. */ 354490287Sobrien 354590287Sobrienstatic tree 3546132747Skanpush_cp_library_fn (enum tree_code operator_code, tree type) 354790287Sobrien{ 354890287Sobrien tree fn = build_cp_library_fn (ansi_opname (operator_code), 354990287Sobrien operator_code, 355090287Sobrien type); 355190287Sobrien pushdecl (fn); 355290287Sobrien return fn; 355390287Sobrien} 355490287Sobrien 355590287Sobrien/* Like push_library_fn, but takes a TREE_LIST of parm types rather than 355690287Sobrien a FUNCTION_TYPE. */ 355790287Sobrien 355890287Sobrientree 3559132747Skanpush_void_library_fn (tree name, tree parmtypes) 356090287Sobrien{ 356190287Sobrien tree type = build_function_type (void_type_node, parmtypes); 356290287Sobrien return push_library_fn (name, type); 356390287Sobrien} 356490287Sobrien 356590287Sobrien/* Like push_library_fn, but also note that this function throws 356690287Sobrien and does not return. Used for __throw_foo and the like. */ 356790287Sobrien 356890287Sobrientree 3569132747Skanpush_throw_library_fn (tree name, tree type) 357090287Sobrien{ 357190287Sobrien tree fn = push_library_fn (name, type); 357290287Sobrien TREE_THIS_VOLATILE (fn) = 1; 357390287Sobrien TREE_NOTHROW (fn) = 0; 357490287Sobrien return fn; 357590287Sobrien} 357618334Speter 357752290Sobrien/* When we call finish_struct for an anonymous union, we create 357852290Sobrien default copy constructors and such. But, an anonymous union 357952290Sobrien shouldn't have such things; this function undoes the damage to the 358052290Sobrien anonymous union type T. 358118334Speter 358252290Sobrien (The reason that we create the synthesized methods is that we don't 358352290Sobrien distinguish `union { int i; }' from `typedef union { int i; } U'. 358452290Sobrien The first is an anonymous union; the second is just an ordinary 358552290Sobrien union type.) */ 358618334Speter 358718334Spetervoid 3588132747Skanfixup_anonymous_aggr (tree t) 358952290Sobrien{ 359052290Sobrien tree *q; 359152290Sobrien 3592132747Skan /* Wipe out memory of synthesized methods. */ 359352290Sobrien TYPE_HAS_CONSTRUCTOR (t) = 0; 359452290Sobrien TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; 359552290Sobrien TYPE_HAS_INIT_REF (t) = 0; 359652290Sobrien TYPE_HAS_CONST_INIT_REF (t) = 0; 359752290Sobrien TYPE_HAS_ASSIGN_REF (t) = 0; 359852290Sobrien TYPE_HAS_CONST_ASSIGN_REF (t) = 0; 359952290Sobrien 360052290Sobrien /* Splice the implicitly generated functions out of the TYPE_METHODS 360152290Sobrien list. */ 360252290Sobrien q = &TYPE_METHODS (t); 360352290Sobrien while (*q) 360452290Sobrien { 360552290Sobrien if (DECL_ARTIFICIAL (*q)) 360652290Sobrien *q = TREE_CHAIN (*q); 360752290Sobrien else 360852290Sobrien q = &TREE_CHAIN (*q); 360952290Sobrien } 361052290Sobrien 361190287Sobrien /* ISO C++ 9.5.3. Anonymous unions may not have function members. */ 361252290Sobrien if (TYPE_METHODS (t)) 3613132747Skan error ("%Jan anonymous union cannot have function members", 3614132747Skan TYPE_MAIN_DECL (t)); 361590287Sobrien 361690287Sobrien /* Anonymous aggregates cannot have fields with ctors, dtors or complex 361790287Sobrien assignment operators (because they cannot have these methods themselves). 361890287Sobrien For anonymous unions this is already checked because they are not allowed 361990287Sobrien in any union, otherwise we have to check it. */ 362090287Sobrien if (TREE_CODE (t) != UNION_TYPE) 362190287Sobrien { 362290287Sobrien tree field, type; 362390287Sobrien 362490287Sobrien for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 362590287Sobrien if (TREE_CODE (field) == FIELD_DECL) 362690287Sobrien { 362790287Sobrien type = TREE_TYPE (field); 362890287Sobrien if (CLASS_TYPE_P (type)) 362990287Sobrien { 3630169699Skan if (TYPE_NEEDS_CONSTRUCTING (type)) 3631169699Skan error ("member %q+#D with constructor not allowed " 3632169699Skan "in anonymous aggregate", field); 363390287Sobrien if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 3634169699Skan error ("member %q+#D with destructor not allowed " 3635169699Skan "in anonymous aggregate", field); 363690287Sobrien if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) 3637169699Skan error ("member %q+#D with copy assignment operator " 3638169699Skan "not allowed in anonymous aggregate", field); 363990287Sobrien } 364090287Sobrien } 364190287Sobrien } 364252290Sobrien} 364352290Sobrien 364452290Sobrien/* Make sure that a declaration with no declarator is well-formed, i.e. 3645132747Skan just declares a tagged type or anonymous union. 364652290Sobrien 3647132747Skan Returns the type declared; or NULL_TREE if none. */ 364852290Sobrien 364952290Sobrientree 3650169699Skancheck_tag_decl (cp_decl_specifier_seq *declspecs) 365118334Speter{ 3652169699Skan int saw_friend = declspecs->specs[(int)ds_friend] != 0; 3653169699Skan int saw_typedef = declspecs->specs[(int)ds_typedef] != 0; 3654132747Skan /* If a class, struct, or enum type is declared by the DECLSPECS 3655132747Skan (i.e, if a class-specifier, enum-specifier, or non-typename 3656132747Skan elaborated-type-specifier appears in the DECLSPECS), 3657132747Skan DECLARED_TYPE is set to the corresponding type. */ 3658132747Skan tree declared_type = NULL_TREE; 3659132747Skan bool error_p = false; 366018334Speter 3661169699Skan if (declspecs->multiple_types_p) 3662169699Skan error ("multiple types in one declaration"); 3663169699Skan else if (declspecs->redefined_builtin_type) 366418334Speter { 3665169699Skan if (!in_system_header) 3666169699Skan pedwarn ("redeclaration of C++ built-in type %qT", 3667169699Skan declspecs->redefined_builtin_type); 3668169699Skan return NULL_TREE; 366918334Speter } 367018334Speter 3671169699Skan if (declspecs->type 3672169699Skan && TYPE_P (declspecs->type) 3673169699Skan && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE 3674169699Skan && IS_AGGR_TYPE (declspecs->type)) 3675169699Skan || TREE_CODE (declspecs->type) == ENUMERAL_TYPE)) 3676169699Skan declared_type = declspecs->type; 3677169699Skan else if (declspecs->type == error_mark_node) 3678169699Skan error_p = true; 3679132747Skan if (declared_type == NULL_TREE && ! saw_friend && !error_p) 368052290Sobrien pedwarn ("declaration does not declare anything"); 368190287Sobrien /* Check for an anonymous union. */ 3682132747Skan else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type)) 3683132747Skan && TYPE_ANONYMOUS_P (declared_type)) 368452290Sobrien { 368590287Sobrien /* 7/3 In a simple-declaration, the optional init-declarator-list 3686169699Skan can be omitted only when declaring a class (clause 9) or 3687169699Skan enumeration (7.2), that is, when the decl-specifier-seq contains 3688169699Skan either a class-specifier, an elaborated-type-specifier with 3689169699Skan a class-key (9.1), or an enum-specifier. In these cases and 3690169699Skan whenever a class-specifier or enum-specifier is present in the 3691169699Skan decl-specifier-seq, the identifiers in these specifiers are among 3692169699Skan the names being declared by the declaration (as class-name, 3693169699Skan enum-names, or enumerators, depending on the syntax). In such 3694169699Skan cases, and except for the declaration of an unnamed bit-field (9.6), 3695169699Skan the decl-specifier-seq shall introduce one or more names into the 3696169699Skan program, or shall redeclare a name introduced by a previous 3697169699Skan declaration. [Example: 3698169699Skan enum { }; // ill-formed 3699169699Skan typedef class { }; // ill-formed 3700169699Skan --end example] */ 370190287Sobrien if (saw_typedef) 3702169699Skan { 3703169699Skan error ("missing type-name in typedef-declaration"); 3704169699Skan return NULL_TREE; 3705169699Skan } 370652290Sobrien /* Anonymous unions are objects, so they can have specifiers. */; 3707132747Skan SET_ANON_AGGR_TYPE_P (declared_type); 370890287Sobrien 3709169699Skan if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 3710132747Skan && !in_system_header) 371190287Sobrien pedwarn ("ISO C++ prohibits anonymous structs"); 371252290Sobrien } 371352290Sobrien 3714169699Skan else 371552290Sobrien { 3716169699Skan if (declspecs->specs[(int)ds_inline] 3717169699Skan || declspecs->specs[(int)ds_virtual]) 3718169699Skan error ("%qs can only be specified for functions", 3719169699Skan declspecs->specs[(int)ds_inline] 3720169699Skan ? "inline" : "virtual"); 3721169699Skan else if (saw_friend 3722169699Skan && (!current_class_type 3723169699Skan || current_scope () != current_class_type)) 3724169699Skan error ("%<friend%> can only be specified inside a class"); 3725169699Skan else if (declspecs->specs[(int)ds_explicit]) 3726169699Skan error ("%<explicit%> can only be specified for constructors"); 3727169699Skan else if (declspecs->storage_class) 3728169699Skan error ("a storage class can only be specified for objects " 3729169699Skan "and functions"); 3730169699Skan else if (declspecs->specs[(int)ds_const] 3731169699Skan || declspecs->specs[(int)ds_volatile] 3732169699Skan || declspecs->specs[(int)ds_restrict] 3733169699Skan || declspecs->specs[(int)ds_thread]) 3734169699Skan error ("qualifiers can only be specified for objects " 3735169699Skan "and functions"); 373652290Sobrien } 373752290Sobrien 3738132747Skan return declared_type; 373952290Sobrien} 374052290Sobrien 374152290Sobrien/* Called when a declaration is seen that contains no names to declare. 374252290Sobrien If its type is a reference to a structure, union or enum inherited 374352290Sobrien from a containing scope, shadow that tag name for the current scope 374452290Sobrien with a forward reference. 374552290Sobrien If its type defines a new named structure or union 374652290Sobrien or defines an enum, it is valid but we need not do anything here. 374752290Sobrien Otherwise, it is an error. 374852290Sobrien 374952290Sobrien C++: may have to grok the declspecs to learn about static, 3750169699Skan complain for anonymous unions. 375152290Sobrien 3752132747Skan Returns the TYPE declared -- or NULL_TREE if none. */ 3753132747Skan 3754132747Skantree 3755169699Skanshadow_tag (cp_decl_specifier_seq *declspecs) 375652290Sobrien{ 375752290Sobrien tree t = check_tag_decl (declspecs); 375852290Sobrien 3759132747Skan if (!t) 3760132747Skan return NULL_TREE; 376152290Sobrien 3762169699Skan if (declspecs->attributes) 3763169699Skan { 3764169699Skan warning (0, "attribute ignored in declaration of %q+#T", t); 3765169699Skan warning (0, "attribute for %q+#T must follow the %qs keyword", 3766169699Skan t, class_key_or_enum_as_string (t)); 3767132747Skan 3768169699Skan } 3769169699Skan 3770169699Skan if (maybe_process_partial_specialization (t) == error_mark_node) 3771169699Skan return NULL_TREE; 3772169699Skan 377318334Speter /* This is where the variables in an anonymous union are 377418334Speter declared. An anonymous union declaration looks like: 377518334Speter union { ... } ; 377618334Speter because there is no declarator after the union, the parser 377718334Speter sends that declaration here. */ 3778132747Skan if (ANON_AGGR_TYPE_P (t)) 377918334Speter { 378090287Sobrien fixup_anonymous_aggr (t); 378151412Sobrien 378218334Speter if (TYPE_FIELDS (t)) 378318334Speter { 3784169699Skan tree decl = grokdeclarator (/*declarator=*/NULL, 3785169699Skan declspecs, NORMAL, 0, NULL); 378618334Speter finish_anon_union (decl); 378718334Speter } 378818334Speter } 3789132747Skan 3790132747Skan return t; 379118334Speter} 379218334Speter 379318334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 379418334Speter 379518334Spetertree 3796169699Skangroktypename (cp_decl_specifier_seq *type_specifiers, 3797169699Skan const cp_declarator *declarator) 379818334Speter{ 3799169699Skan tree attrs; 380090287Sobrien tree type; 3801169699Skan attrs = type_specifiers->attributes; 3802169699Skan type_specifiers->attributes = NULL_TREE; 3803169699Skan type = grokdeclarator (declarator, type_specifiers, TYPENAME, 0, &attrs); 380490287Sobrien if (attrs) 380590287Sobrien cplus_decl_attributes (&type, attrs, 0); 380690287Sobrien return type; 380718334Speter} 380818334Speter 380918334Speter/* Decode a declarator in an ordinary declaration or data definition. 381018334Speter This is called as soon as the type information and variable name 381118334Speter have been parsed, before parsing the initializer if any. 381218334Speter Here we create the ..._DECL node, fill in its type, 381318334Speter and put it on the list of decls for the current context. 381418334Speter The ..._DECL node is returned as the value. 381518334Speter 381618334Speter Exception: for arrays where the length is not specified, 381718334Speter the type is left null, to be filled in by `cp_finish_decl'. 381818334Speter 381918334Speter Function definitions do not come here; they go to start_function 382018334Speter instead. However, external and forward declarations of functions 382118334Speter do go through here. Structure field declarations are done by 382218334Speter grokfield and not through here. */ 382318334Speter 382418334Spetertree 3825169699Skanstart_decl (const cp_declarator *declarator, 3826169699Skan cp_decl_specifier_seq *declspecs, 3827169699Skan int initialized, 3828169699Skan tree attributes, 3829169699Skan tree prefix_attributes, 3830169699Skan tree *pushed_scope_p) 383118334Speter{ 383290287Sobrien tree decl; 3833132747Skan tree type, tem; 383418334Speter tree context; 3835169699Skan bool was_public; 383618334Speter 3837169699Skan *pushed_scope_p = NULL_TREE; 383818334Speter 383990287Sobrien /* An object declared as __attribute__((deprecated)) suppresses 384090287Sobrien warnings of uses of other deprecated items. */ 384190287Sobrien if (lookup_attribute ("deprecated", attributes)) 384290287Sobrien deprecated_state = DEPRECATED_SUPPRESS; 384352290Sobrien 384490287Sobrien attributes = chainon (attributes, prefix_attributes); 384590287Sobrien 384651412Sobrien decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, 384790287Sobrien &attributes); 384890287Sobrien 384990287Sobrien deprecated_state = DEPRECATED_NORMAL; 385090287Sobrien 3851169699Skan if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE 3852169699Skan || decl == error_mark_node) 3853146906Skan return error_mark_node; 385418334Speter 385518334Speter type = TREE_TYPE (decl); 385618334Speter 385790287Sobrien context = DECL_CONTEXT (decl); 385818334Speter 3859169699Skan if (context) 386018334Speter { 3861169699Skan *pushed_scope_p = push_scope (context); 3862169699Skan 3863169699Skan /* We are only interested in class contexts, later. */ 3864169699Skan if (TREE_CODE (context) == NAMESPACE_DECL) 3865169699Skan context = NULL_TREE; 386618334Speter } 386718334Speter 386818334Speter if (initialized) 386918334Speter /* Is it valid for this decl to have an initializer at all? 387018334Speter If not, set INITIALIZED to zero, which will indirectly 387118334Speter tell `cp_finish_decl' to ignore the initializer once it is parsed. */ 387218334Speter switch (TREE_CODE (decl)) 387318334Speter { 387418334Speter case TYPE_DECL: 3875169699Skan error ("typedef %qD is initialized (use __typeof__ instead)", decl); 3876169699Skan return error_mark_node; 387718334Speter 387818334Speter case FUNCTION_DECL: 3879169699Skan error ("function %q#D is initialized like a variable", decl); 3880169699Skan return error_mark_node; 388118334Speter 388218334Speter default: 388390287Sobrien break; 388418334Speter } 388518334Speter 388618334Speter if (initialized) 388718334Speter { 388818334Speter if (! toplevel_bindings_p () 388918334Speter && DECL_EXTERNAL (decl)) 3890169699Skan warning (0, "declaration of %q#D has %<extern%> and is initialized", 3891169699Skan decl); 389218334Speter DECL_EXTERNAL (decl) = 0; 389351412Sobrien if (toplevel_bindings_p ()) 389418334Speter TREE_STATIC (decl) = 1; 389518334Speter } 389618334Speter 389752290Sobrien /* Set attributes here so if duplicate decl, will have proper attributes. */ 389890287Sobrien cplus_decl_attributes (&decl, attributes, 0); 389952290Sobrien 3900169699Skan /* Dllimported symbols cannot be defined. Static data members (which 3901169699Skan can be initialized in-class and dllimported) go through grokfield, 3902169699Skan not here, so we don't need to exclude those decls when checking for 3903169699Skan a definition. */ 3904169699Skan if (initialized && DECL_DLLIMPORT_P (decl)) 3905169699Skan { 3906169699Skan error ("definition of %q#D is marked %<dllimport%>", decl); 3907169699Skan DECL_DLLIMPORT_P (decl) = 0; 3908169699Skan } 3909169699Skan 391096292Sobrien /* If #pragma weak was used, mark the decl weak now. */ 3911169699Skan maybe_apply_pragma_weak (decl); 391296292Sobrien 391390287Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 391490287Sobrien && DECL_DECLARED_INLINE_P (decl) 391590287Sobrien && DECL_UNINLINABLE (decl) 391690287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 3917169699Skan warning (0, "inline function %q+D given attribute noinline", decl); 391890287Sobrien 391990287Sobrien if (context && COMPLETE_TYPE_P (complete_type (context))) 392018334Speter { 392118334Speter if (TREE_CODE (decl) == VAR_DECL) 392218334Speter { 3923132747Skan tree field = lookup_field (context, DECL_NAME (decl), 0, false); 392418334Speter if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL) 3925169699Skan error ("%q#D is not a static member of %q#T", decl, context); 392651412Sobrien else 392751412Sobrien { 392851412Sobrien if (DECL_CONTEXT (field) != context) 392951412Sobrien { 3930132747Skan if (!same_type_p (DECL_CONTEXT (field), context)) 3931169699Skan pedwarn ("ISO C++ does not permit %<%T::%D%> " 3932169699Skan "to be defined as %<%T::%D%>", 3933132747Skan DECL_CONTEXT (field), DECL_NAME (decl), 3934132747Skan context, DECL_NAME (decl)); 393551412Sobrien DECL_CONTEXT (decl) = DECL_CONTEXT (field); 393651412Sobrien } 3937169699Skan if (processing_specialization 3938169699Skan && template_class_depth (context) == 0 3939169699Skan && CLASSTYPE_TEMPLATE_SPECIALIZATION (context)) 3940169699Skan error ("template header not allowed in member definition " 3941169699Skan "of explicitly specialized class"); 394251412Sobrien /* Static data member are tricky; an in-class initialization 394351412Sobrien still doesn't provide a definition, so the in-class 394451412Sobrien declaration will have DECL_EXTERNAL set, but will have an 394551412Sobrien initialization. Thus, duplicate_decls won't warn 394651412Sobrien about this situation, and so we check here. */ 3947169699Skan if (initialized && DECL_INITIALIZED_IN_CLASS_P (field)) 3948169699Skan error ("duplicate initialization of %qD", decl); 3949169699Skan if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false)) 395051412Sobrien decl = field; 395151412Sobrien } 395218334Speter } 395318334Speter else 395418334Speter { 3955132747Skan tree field = check_classfn (context, decl, 3956169699Skan (processing_template_decl 3957169699Skan > template_class_depth (context)) 3958169699Skan ? current_template_parms 3959169699Skan : NULL_TREE); 3960169699Skan if (field && duplicate_decls (decl, field, 3961169699Skan /*newdecl_is_friend=*/false)) 396218334Speter decl = field; 396318334Speter } 396418334Speter 396518334Speter /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ 396651412Sobrien DECL_IN_AGGR_P (decl) = 0; 3967169699Skan /* Do not mark DECL as an explicit specialization if it was not 3968169699Skan already marked as an instantiation; a declaration should 3969169699Skan never be marked as a specialization unless we know what 3970169699Skan template is being specialized. */ 3971169699Skan if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 397252290Sobrien { 397352290Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (decl); 3974169699Skan 397552290Sobrien /* [temp.expl.spec] An explicit specialization of a static data 397652290Sobrien member of a template is a definition if the declaration 397752290Sobrien includes an initializer; otherwise, it is a declaration. 397818334Speter 397952290Sobrien We check for processing_specialization so this only applies 398052290Sobrien to the new specialization syntax. */ 3981169699Skan if (!initialized && processing_specialization) 398252290Sobrien DECL_EXTERNAL (decl) = 1; 398352290Sobrien } 398452290Sobrien 398551412Sobrien if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)) 3986169699Skan pedwarn ("declaration of %q#D outside of class is not definition", 3987169699Skan decl); 398818334Speter } 398918334Speter 3990169699Skan was_public = TREE_PUBLIC (decl); 3991169699Skan 399290287Sobrien /* Enter this declaration into the symbol table. */ 399390287Sobrien tem = maybe_push_decl (decl); 399418334Speter 399551412Sobrien if (processing_template_decl) 399690287Sobrien tem = push_template_decl (tem); 3997132747Skan if (tem == error_mark_node) 3998132747Skan return error_mark_node; 399951412Sobrien 400051412Sobrien /* Tell the back-end to use or not use .common as appropriate. If we say 400151412Sobrien -fconserve-space, we want this to save .data space, at the expense of 400251412Sobrien wrong semantics. If we say -fno-conserve-space, we want this to 400351412Sobrien produce errors about redefs; to do this we force variables into the 400451412Sobrien data segment. */ 4005169699Skan if (flag_conserve_space 4006169699Skan && TREE_CODE (tem) == VAR_DECL 4007169699Skan && TREE_PUBLIC (tem) 4008169699Skan && !DECL_THREAD_LOCAL_P (tem) 4009169699Skan && !have_global_bss_p ()) 4010169699Skan DECL_COMMON (tem) = 1; 401190287Sobrien 4012169699Skan if (TREE_CODE (tem) == VAR_DECL 4013169699Skan && DECL_NAMESPACE_SCOPE_P (tem) && !TREE_PUBLIC (tem) && !was_public 4014169699Skan && !DECL_THIS_STATIC (tem) && !DECL_ARTIFICIAL (tem)) 4015169699Skan { 4016169699Skan /* This is a const variable with implicit 'static'. Set 4017169699Skan DECL_THIS_STATIC so we can tell it from variables that are 4018169699Skan !TREE_PUBLIC because of the anonymous namespace. */ 4019169699Skan gcc_assert (cp_type_readonly (TREE_TYPE (tem))); 4020169699Skan DECL_THIS_STATIC (tem) = 1; 4021169699Skan } 402218334Speter 4023169699Skan if (!processing_template_decl && TREE_CODE (tem) == VAR_DECL) 4024169699Skan start_decl_1 (tem, initialized); 4025169699Skan 402618334Speter return tem; 402718334Speter} 402818334Speter 402951412Sobrienvoid 4030169699Skanstart_decl_1 (tree decl, bool initialized) 403118334Speter{ 4032169699Skan tree type; 403318334Speter 4034169699Skan gcc_assert (!processing_template_decl); 4035169699Skan 4036169699Skan if (error_operand_p (decl)) 403752290Sobrien return; 403852290Sobrien 4039169699Skan gcc_assert (TREE_CODE (decl) == VAR_DECL); 4040169699Skan type = TREE_TYPE (decl); 4041169699Skan 404251412Sobrien if (initialized) 404351412Sobrien /* Is it valid for this decl to have an initializer at all? 404451412Sobrien If not, set INITIALIZED to zero, which will indirectly 404551412Sobrien tell `cp_finish_decl' to ignore the initializer once it is parsed. */ 404618334Speter { 404751412Sobrien /* Don't allow initializations for incomplete types except for 404851412Sobrien arrays which might be completed by the initialization. */ 404990287Sobrien if (COMPLETE_TYPE_P (complete_type (type))) 405051412Sobrien ; /* A complete type is ok. */ 405151412Sobrien else if (TREE_CODE (type) != ARRAY_TYPE) 405251412Sobrien { 4053169699Skan error ("variable %q#D has initializer but incomplete type", decl); 405451412Sobrien initialized = 0; 405551412Sobrien type = TREE_TYPE (decl) = error_mark_node; 405651412Sobrien } 405790287Sobrien else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) 405851412Sobrien { 405951412Sobrien if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) 4060169699Skan error ("elements of array %q#D have incomplete type", decl); 406151412Sobrien /* else we already gave an error in start_decl. */ 406251412Sobrien initialized = 0; 406351412Sobrien } 406418334Speter } 4065169699Skan else if (IS_AGGR_TYPE (type) 4066169699Skan && ! DECL_EXTERNAL (decl)) 406718334Speter { 4068169699Skan if (!COMPLETE_TYPE_P (complete_type (type))) 406918334Speter { 4070169699Skan error ("aggregate %q#D has incomplete type and cannot be defined", 407151412Sobrien decl); 407251412Sobrien /* Change the type so that assemble_variable will give 407351412Sobrien DECL an rtl we can live with: (mem (const_int 0)). */ 407451412Sobrien type = TREE_TYPE (decl) = error_mark_node; 407518334Speter } 407618334Speter else 407718334Speter { 407851412Sobrien /* If any base type in the hierarchy of TYPE needs a constructor, 407951412Sobrien then we set initialized to 1. This way any nodes which are 408051412Sobrien created for the purposes of initializing this aggregate 408151412Sobrien will live as long as it does. This is necessary for global 408251412Sobrien aggregates which do not have their initializers processed until 408351412Sobrien the end of the file. */ 408451412Sobrien initialized = TYPE_NEEDS_CONSTRUCTING (type); 408518334Speter } 408618334Speter } 408751412Sobrien 4088132747Skan /* Create a new scope to hold this declaration if necessary. 4089132747Skan Whether or not a new scope is necessary cannot be determined 4090132747Skan until after the type has been completed; if the type is a 4091132747Skan specialization of a class template it is not until after 4092132747Skan instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR 4093132747Skan will be set correctly. */ 4094132747Skan maybe_push_cleanup_level (type); 409551412Sobrien} 409651412Sobrien 4097122192Skan/* Handle initialization of references. DECL, TYPE, and INIT have the 4098122192Skan same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry, 4099122192Skan but will be set to a new CLEANUP_STMT if a temporary is created 4100132747Skan that must be destroyed subsequently. 410151412Sobrien 4102122192Skan Returns an initializer expression to use to initialize DECL, or 4103122192Skan NULL if the initialization can be performed statically. 4104122192Skan 410551412Sobrien Quotes on semantics can be found in ARM 8.4.3. */ 4106132747Skan 410790287Sobrienstatic tree 4108122192Skangrok_reference_init (tree decl, tree type, tree init, tree *cleanup) 410918334Speter{ 411018334Speter tree tmp; 411118334Speter 411218334Speter if (init == NULL_TREE) 411318334Speter { 411418334Speter if ((DECL_LANG_SPECIFIC (decl) == 0 411518334Speter || DECL_IN_AGGR_P (decl) == 0) 411618334Speter && ! DECL_THIS_EXTERN (decl)) 4117169699Skan error ("%qD declared as reference but not initialized", decl); 411890287Sobrien return NULL_TREE; 411918334Speter } 412018334Speter 412190287Sobrien if (TREE_CODE (init) == CONSTRUCTOR) 412218334Speter { 4123169699Skan error ("ISO C++ forbids use of initializer list to " 4124169699Skan "initialize reference %qD", decl); 412590287Sobrien return NULL_TREE; 412618334Speter } 412718334Speter 412818334Speter if (TREE_CODE (init) == TREE_LIST) 4129132747Skan init = build_x_compound_expr_from_list (init, "initializer"); 413018334Speter 413118334Speter if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE 413218334Speter && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) 4133132747Skan /* Note: default conversion is only called in very special cases. */ 4134132747Skan init = decay_conversion (init); 413518334Speter 413690287Sobrien /* Convert INIT to the reference type TYPE. This may involve the 413790287Sobrien creation of a temporary, whose lifetime must be the same as that 4138169699Skan of the reference. If so, a DECL_EXPR for the temporary will be 4139169699Skan added just after the DECL_EXPR for DECL. That's why we don't set 414090287Sobrien DECL_INITIAL for local references (instead assigning to them 414190287Sobrien explicitly); we need to allow the temporary to be initialized 414290287Sobrien first. */ 4143122192Skan tmp = initialize_reference (type, init, decl, cleanup); 414418334Speter 414518334Speter if (tmp == error_mark_node) 414690287Sobrien return NULL_TREE; 414790287Sobrien else if (tmp == NULL_TREE) 414818334Speter { 4149169699Skan error ("cannot initialize %qT from %qT", type, TREE_TYPE (init)); 415090287Sobrien return NULL_TREE; 415118334Speter } 415218334Speter 415390287Sobrien if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp)) 415490287Sobrien return tmp; 415518334Speter 415690287Sobrien DECL_INITIAL (decl) = tmp; 415718334Speter 415890287Sobrien return NULL_TREE; 415918334Speter} 416018334Speter 4161169699Skan/* Designated initializers in arrays are not supported in GNU C++. 4162169699Skan The parser cannot detect this error since it does not know whether 4163169699Skan a given brace-enclosed initializer is for a class type or for an 4164169699Skan array. This function checks that CE does not use a designated 4165169699Skan initializer. If it does, an error is issued. Returns true if CE 4166169699Skan is valid, i.e., does not have a designated initializer. */ 4167169699Skan 4168169699Skanstatic bool 4169169699Skancheck_array_designated_initializer (const constructor_elt *ce) 4170169699Skan{ 4171169699Skan /* Designated initializers for array elements arenot supported. */ 4172169699Skan if (ce->index) 4173169699Skan { 4174169699Skan /* The parser only allows identifiers as designated 4175169699Skan intializers. */ 4176169699Skan gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE); 4177169699Skan error ("name %qD used in a GNU-style designated " 4178169699Skan "initializer for an array", ce->index); 4179169699Skan return false; 4180169699Skan } 4181169699Skan 4182169699Skan return true; 4183169699Skan} 4184169699Skan 418590287Sobrien/* When parsing `int a[] = {1, 2};' we don't know the size of the 418690287Sobrien array until we finish parsing the initializer. If that's the 418790287Sobrien situation we're in, update DECL accordingly. */ 418852290Sobrien 418952290Sobrienstatic void 4190132747Skanmaybe_deduce_size_from_array_init (tree decl, tree init) 419152290Sobrien{ 419252290Sobrien tree type = TREE_TYPE (decl); 419352290Sobrien 419490287Sobrien if (TREE_CODE (type) == ARRAY_TYPE 419590287Sobrien && TYPE_DOMAIN (type) == NULL_TREE 419690287Sobrien && TREE_CODE (decl) != TYPE_DECL) 419790287Sobrien { 419890287Sobrien /* do_default is really a C-ism to deal with tentative definitions. 419990287Sobrien But let's leave it here to ease the eventual merge. */ 420090287Sobrien int do_default = !DECL_EXTERNAL (decl); 420190287Sobrien tree initializer = init ? init : DECL_INITIAL (decl); 4202169699Skan int failure = 0; 420352290Sobrien 4204169699Skan /* Check that there are no designated initializers in INIT, as 4205169699Skan those are not supported in GNU C++, and as the middle-end 4206169699Skan will crash if presented with a non-numeric designated 4207169699Skan initializer. */ 4208169699Skan if (initializer && TREE_CODE (initializer) == CONSTRUCTOR) 4209169699Skan { 4210169699Skan VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initializer); 4211169699Skan constructor_elt *ce; 4212169699Skan HOST_WIDE_INT i; 4213169699Skan for (i = 0; 4214169699Skan VEC_iterate (constructor_elt, v, i, ce); 4215169699Skan ++i) 4216169699Skan if (!check_array_designated_initializer (ce)) 4217169699Skan failure = 1; 4218169699Skan } 421918334Speter 4220169699Skan if (!failure) 422190287Sobrien { 4222169699Skan failure = cp_complete_array_type (&TREE_TYPE (decl), initializer, 4223169699Skan do_default); 4224169699Skan if (failure == 1) 4225169699Skan { 4226169699Skan error ("initializer fails to determine size of %qD", decl); 4227169699Skan TREE_TYPE (decl) = error_mark_node; 4228169699Skan } 4229169699Skan else if (failure == 2) 4230169699Skan { 4231169699Skan if (do_default) 4232169699Skan { 4233169699Skan error ("array size missing in %qD", decl); 4234169699Skan TREE_TYPE (decl) = error_mark_node; 4235169699Skan } 4236169699Skan /* If a `static' var's size isn't known, make it extern as 4237169699Skan well as static, so it does not get allocated. If it's not 4238169699Skan `static', then don't mark it extern; finish_incomplete_decl 4239169699Skan will give it a default size and it will get allocated. */ 4240169699Skan else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 4241169699Skan DECL_EXTERNAL (decl) = 1; 4242169699Skan } 4243169699Skan else if (failure == 3) 4244169699Skan { 4245169699Skan error ("zero-size array %qD", decl); 4246169699Skan TREE_TYPE (decl) = error_mark_node; 4247169699Skan } 424890287Sobrien } 424918334Speter 4250169699Skan cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl); 425118334Speter 425290287Sobrien layout_decl (decl, 0); 425390287Sobrien } 425490287Sobrien} 425518334Speter 425690287Sobrien/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue 425790287Sobrien any appropriate error messages regarding the layout. */ 425818334Speter 425990287Sobrienstatic void 4260132747Skanlayout_var_decl (tree decl) 426118334Speter{ 4262169699Skan tree type; 426318334Speter 4264169699Skan type = TREE_TYPE (decl); 4265169699Skan if (type == error_mark_node) 4266169699Skan return; 4267169699Skan 426890287Sobrien /* If we haven't already layed out this declaration, do so now. 426990287Sobrien Note that we must not call complete type for an external object 427090287Sobrien because it's type might involve templates that we are not 4271132747Skan supposed to instantiate yet. (And it's perfectly valid to say 427290287Sobrien `extern X x' for some incomplete type `X'.) */ 427390287Sobrien if (!DECL_EXTERNAL (decl)) 427490287Sobrien complete_type (type); 4275169699Skan if (!DECL_SIZE (decl) 4276117410Skan && TREE_TYPE (decl) != error_mark_node 4277117410Skan && (COMPLETE_TYPE_P (type) 4278169699Skan || (TREE_CODE (type) == ARRAY_TYPE 4279117410Skan && !TYPE_DOMAIN (type) 4280117410Skan && COMPLETE_TYPE_P (TREE_TYPE (type))))) 428190287Sobrien layout_decl (decl, 0); 428290287Sobrien 428390287Sobrien if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) 428418334Speter { 428590287Sobrien /* An automatic variable with an incomplete type: that is an error. 428690287Sobrien Don't talk about array types here, since we took care of that 428790287Sobrien message in grokdeclarator. */ 4288169699Skan error ("storage size of %qD isn't known", decl); 428990287Sobrien TREE_TYPE (decl) = error_mark_node; 429018334Speter } 429190287Sobrien#if 0 429290287Sobrien /* Keep this code around in case we later want to control debug info 429390287Sobrien based on whether a type is "used". (jason 1999-11-11) */ 429418334Speter 429590287Sobrien else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype)) 429690287Sobrien /* Let debugger know it should output info for this type. */ 429790287Sobrien note_debug_info_needed (ttype); 429818334Speter 429990287Sobrien if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) 430090287Sobrien note_debug_info_needed (DECL_CONTEXT (decl)); 430190287Sobrien#endif 430251412Sobrien 430390287Sobrien if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 430490287Sobrien && DECL_SIZE (decl) != NULL_TREE 430590287Sobrien && ! TREE_CONSTANT (DECL_SIZE (decl))) 430651412Sobrien { 430790287Sobrien if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 430890287Sobrien constant_expression_warning (DECL_SIZE (decl)); 430990287Sobrien else 4310169699Skan error ("storage size of %qD isn't constant", decl); 431151412Sobrien } 431290287Sobrien} 431318334Speter 431490287Sobrien/* If a local static variable is declared in an inline function, or if 431590287Sobrien we have a weak definition, we must endeavor to create only one 431690287Sobrien instance of the variable at link-time. */ 431718334Speter 431890287Sobrienstatic void 4319132747Skanmaybe_commonize_var (tree decl) 432090287Sobrien{ 432190287Sobrien /* Static data in a function with comdat linkage also has comdat 432290287Sobrien linkage. */ 432390287Sobrien if (TREE_STATIC (decl) 432490287Sobrien /* Don't mess with __FUNCTION__. */ 432590287Sobrien && ! DECL_ARTIFICIAL (decl) 4326117410Skan && DECL_FUNCTION_SCOPE_P (decl) 4327117410Skan /* Unfortunately, import_export_decl has not always been called 4328117410Skan before the function is processed, so we cannot simply check 4329169699Skan DECL_COMDAT. */ 4330117410Skan && (DECL_COMDAT (DECL_CONTEXT (decl)) 4331117410Skan || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl)) 4332117410Skan || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl))) 4333117410Skan && TREE_PUBLIC (DECL_CONTEXT (decl))))) 433418334Speter { 4335117410Skan if (flag_weak) 433651412Sobrien { 4337117410Skan /* With weak symbols, we simply make the variable COMDAT; 4338117410Skan that will cause copies in multiple translations units to 4339117410Skan be merged. */ 4340117410Skan comdat_linkage (decl); 4341117410Skan } 4342117410Skan else 4343117410Skan { 4344117410Skan if (DECL_INITIAL (decl) == NULL_TREE 4345117410Skan || DECL_INITIAL (decl) == error_mark_node) 434651412Sobrien { 4347117410Skan /* Without weak symbols, we can use COMMON to merge 4348117410Skan uninitialized variables. */ 434990287Sobrien TREE_PUBLIC (decl) = 1; 435090287Sobrien DECL_COMMON (decl) = 1; 435190287Sobrien } 4352117410Skan else 435390287Sobrien { 4354117410Skan /* While for initialized variables, we must use internal 4355117410Skan linkage -- which means that multiple copies will not 4356117410Skan be merged. */ 4357117410Skan TREE_PUBLIC (decl) = 0; 4358117410Skan DECL_COMMON (decl) = 0; 4359169699Skan warning (0, "sorry: semantics of inline function static " 4360169699Skan "data %q+#D are wrong (you'll wind up " 4361169699Skan "with multiple copies)", decl); 4362169699Skan warning (0, "%J you can work around this by removing " 4363169699Skan "the initializer", 4364132747Skan decl); 436590287Sobrien } 436651412Sobrien } 436751412Sobrien } 436890287Sobrien else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl)) 436990287Sobrien /* Set it up again; we might have set DECL_INITIAL since the last 437090287Sobrien time. */ 437190287Sobrien comdat_linkage (decl); 437290287Sobrien} 437351412Sobrien 437490287Sobrien/* Issue an error message if DECL is an uninitialized const variable. */ 437551412Sobrien 437690287Sobrienstatic void 4377132747Skancheck_for_uninitialized_const_var (tree decl) 437890287Sobrien{ 437990287Sobrien tree type = TREE_TYPE (decl); 438018334Speter 438190287Sobrien /* ``Unless explicitly declared extern, a const object does not have 438290287Sobrien external linkage and must be initialized. ($8.4; $12.1)'' ARM 438390287Sobrien 7.1.6 */ 438490287Sobrien if (TREE_CODE (decl) == VAR_DECL 438590287Sobrien && TREE_CODE (type) != REFERENCE_TYPE 438690287Sobrien && CP_TYPE_CONST_P (type) 438790287Sobrien && !TYPE_NEEDS_CONSTRUCTING (type) 438890287Sobrien && !DECL_INITIAL (decl)) 4389169699Skan error ("uninitialized const %qD", decl); 439090287Sobrien} 439118334Speter 4392169699Skan 4393169699Skan/* Structure holding the current initializer being processed by reshape_init. 4394169699Skan CUR is a pointer to the current element being processed, END is a pointer 4395169699Skan after the last element present in the initializer. */ 4396169699Skantypedef struct reshape_iterator_t 4397169699Skan{ 4398169699Skan constructor_elt *cur; 4399169699Skan constructor_elt *end; 4400169699Skan} reshape_iter; 4401169699Skan 4402169699Skanstatic tree reshape_init_r (tree, reshape_iter *, bool); 4403169699Skan 4404117410Skan/* FIELD is a FIELD_DECL or NULL. In the former case, the value 4405117410Skan returned is the next FIELD_DECL (possibly FIELD itself) that can be 4406117410Skan initialized. If there are no more such fields, the return value 4407117410Skan will be NULL. */ 440818334Speter 440990287Sobrienstatic tree 4410117410Skannext_initializable_field (tree field) 441190287Sobrien{ 4412117410Skan while (field 4413117410Skan && (TREE_CODE (field) != FIELD_DECL 4414117410Skan || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field)) 4415117410Skan || DECL_ARTIFICIAL (field))) 4416117410Skan field = TREE_CHAIN (field); 441790287Sobrien 4418117410Skan return field; 4419117410Skan} 442090287Sobrien 4421169699Skan/* Subroutine of reshape_init_array and reshape_init_vector, which does 4422169699Skan the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an 4423169699Skan INTEGER_CST representing the size of the array minus one (the maximum index), 4424169699Skan or NULL_TREE if the array was declared without specifying the size. D is 4425169699Skan the iterator within the constructor. */ 4426146906Skan 4427169699Skanstatic tree 4428169699Skanreshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d) 4429146906Skan{ 4430169699Skan tree new_init; 4431146906Skan bool sized_array_p = (max_index != NULL_TREE); 4432146906Skan unsigned HOST_WIDE_INT max_index_cst = 0; 4433146906Skan unsigned HOST_WIDE_INT index; 4434146906Skan 4435169699Skan /* The initializer for an array is always a CONSTRUCTOR. */ 4436169699Skan new_init = build_constructor (NULL_TREE, NULL); 4437169699Skan 4438146906Skan if (sized_array_p) 4439146906Skan { 4440169699Skan /* Minus 1 is used for zero sized arrays. */ 4441169699Skan if (integer_all_onesp (max_index)) 4442169699Skan return new_init; 4443169699Skan 4444146906Skan if (host_integerp (max_index, 1)) 4445146906Skan max_index_cst = tree_low_cst (max_index, 1); 4446146906Skan /* sizetype is sign extended, not zero extended. */ 4447146906Skan else 4448169699Skan max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index), 4449169699Skan 1); 4450146906Skan } 4451146906Skan 4452146906Skan /* Loop until there are no more initializers. */ 4453146906Skan for (index = 0; 4454169699Skan d->cur != d->end && (!sized_array_p || index <= max_index_cst); 4455146906Skan ++index) 4456146906Skan { 4457169699Skan tree elt_init; 4458146906Skan 4459169699Skan check_array_designated_initializer (d->cur); 4460169699Skan elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false); 4461169699Skan if (elt_init == error_mark_node) 4462169699Skan return error_mark_node; 4463169699Skan CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), NULL_TREE, elt_init); 4464146906Skan } 4465146906Skan 4466169699Skan return new_init; 4467146906Skan} 4468146906Skan 4469169699Skan/* Subroutine of reshape_init_r, processes the initializers for arrays. 4470169699Skan Parameters are the same of reshape_init_r. */ 447190287Sobrien 4472169699Skanstatic tree 4473169699Skanreshape_init_array (tree type, reshape_iter *d) 4474169699Skan{ 4475169699Skan tree max_index = NULL_TREE; 447690287Sobrien 4477169699Skan gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 4478117410Skan 4479169699Skan if (TYPE_DOMAIN (type)) 4480169699Skan max_index = array_type_nelts (type); 4481169699Skan 4482169699Skan return reshape_init_array_1 (TREE_TYPE (type), max_index, d); 4483169699Skan} 4484169699Skan 4485169699Skan/* Subroutine of reshape_init_r, processes the initializers for vectors. 4486169699Skan Parameters are the same of reshape_init_r. */ 4487169699Skan 4488117410Skanstatic tree 4489169699Skanreshape_init_vector (tree type, reshape_iter *d) 4490117410Skan{ 4491169699Skan tree max_index = NULL_TREE; 4492169699Skan tree rtype; 4493169699Skan 4494169699Skan gcc_assert (TREE_CODE (type) == VECTOR_TYPE); 4495169699Skan 4496169699Skan if (COMPOUND_LITERAL_P (d->cur->value)) 4497169699Skan { 4498169699Skan tree value = d->cur->value; 4499169699Skan if (!same_type_p (TREE_TYPE (value), type)) 4500169699Skan { 4501169699Skan error ("invalid type %qT as initializer for a vector of type %qT", 4502169699Skan TREE_TYPE (d->cur->value), type); 4503169699Skan value = error_mark_node; 4504169699Skan } 4505169699Skan ++d->cur; 4506169699Skan return value; 4507169699Skan } 4508169699Skan 4509169699Skan /* For a vector, the representation type is a struct 4510169699Skan containing a single member which is an array of the 4511169699Skan appropriate size. */ 4512169699Skan rtype = TYPE_DEBUG_REPRESENTATION_TYPE (type); 4513169699Skan if (rtype && TYPE_DOMAIN (TREE_TYPE (TYPE_FIELDS (rtype)))) 4514169699Skan max_index = array_type_nelts (TREE_TYPE (TYPE_FIELDS (rtype))); 4515169699Skan 4516169699Skan return reshape_init_array_1 (TREE_TYPE (type), max_index, d); 4517169699Skan} 4518169699Skan 4519169699Skan/* Subroutine of reshape_init_r, processes the initializers for classes 4520169699Skan or union. Parameters are the same of reshape_init_r. */ 4521169699Skan 4522169699Skanstatic tree 4523169699Skanreshape_init_class (tree type, reshape_iter *d, bool first_initializer_p) 4524169699Skan{ 4525169699Skan tree field; 4526117410Skan tree new_init; 4527117410Skan 4528169699Skan gcc_assert (CLASS_TYPE_P (type)); 4529117410Skan 4530169699Skan /* The initializer for a class is always a CONSTRUCTOR. */ 4531169699Skan new_init = build_constructor (NULL_TREE, NULL); 4532169699Skan field = next_initializable_field (TYPE_FIELDS (type)); 453318334Speter 4534169699Skan if (!field) 4535117410Skan { 4536169699Skan /* [dcl.init.aggr] 4537169699Skan 4538169699Skan An initializer for an aggregate member that is an 4539169699Skan empty class shall have the form of an empty 4540169699Skan initializer-list {}. */ 4541169699Skan if (!first_initializer_p) 4542169699Skan { 4543169699Skan error ("initializer for %qT must be brace-enclosed", type); 4544169699Skan return error_mark_node; 4545169699Skan } 4546169699Skan return new_init; 4547117410Skan } 4548169699Skan 4549169699Skan /* Loop through the initializable fields, gathering initializers. */ 4550169699Skan while (d->cur != d->end) 4551117410Skan { 4552169699Skan tree field_init; 4553169699Skan 4554169699Skan /* Handle designated initializers, as an extension. */ 4555169699Skan if (d->cur->index) 4556169699Skan { 4557169699Skan field = lookup_field_1 (type, d->cur->index, /*want_type=*/false); 4558169699Skan 4559169699Skan if (!field || TREE_CODE (field) != FIELD_DECL) 4560169699Skan { 4561169699Skan error ("%qT has no non-static data member named %qD", type, 4562169699Skan d->cur->index); 4563169699Skan return error_mark_node; 4564169699Skan } 4565169699Skan } 4566169699Skan 4567169699Skan /* If we processed all the member of the class, we are done. */ 4568169699Skan if (!field) 4569169699Skan break; 4570169699Skan 4571169699Skan field_init = reshape_init_r (TREE_TYPE (field), d, 4572169699Skan /*first_initializer_p=*/false); 4573169699Skan CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init); 4574169699Skan 4575169699Skan /* [dcl.init.aggr] 4576169699Skan 4577169699Skan When a union is initialized with a brace-enclosed 4578169699Skan initializer, the braces shall only contain an 4579169699Skan initializer for the first member of the union. */ 4580169699Skan if (TREE_CODE (type) == UNION_TYPE) 4581169699Skan break; 4582169699Skan 4583169699Skan field = next_initializable_field (TREE_CHAIN (field)); 4584117410Skan } 4585117410Skan 4586169699Skan return new_init; 4587169699Skan} 4588169699Skan 4589169699Skan/* Subroutine of reshape_init, which processes a single initializer (part of 4590169699Skan a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the 4591169699Skan iterator within the CONSTRUCTOR which points to the initializer to process. 4592169699Skan FIRST_INITIALIZER_P is true if this is the first initializer of the 4593169699Skan CONSTRUCTOR node. */ 4594169699Skan 4595169699Skanstatic tree 4596169699Skanreshape_init_r (tree type, reshape_iter *d, bool first_initializer_p) 4597169699Skan{ 4598169699Skan tree init = d->cur->value; 4599169699Skan 4600117410Skan /* A non-aggregate type is always initialized with a single 4601117410Skan initializer. */ 4602117410Skan if (!CP_AGGREGATE_TYPE_P (type)) 4603169699Skan { 4604169699Skan /* It is invalid to initialize a non-aggregate type with a 4605169699Skan brace-enclosed initializer. 4606169699Skan We need to check for BRACE_ENCLOSED_INITIALIZER_P here because 4607169699Skan of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is 4608169699Skan a CONSTRUCTOR (with a record type). */ 4609169699Skan if (TREE_CODE (init) == CONSTRUCTOR 4610169699Skan && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */ 4611169699Skan { 4612169699Skan error ("braces around scalar initializer for type %qT", type); 4613169699Skan init = error_mark_node; 4614169699Skan } 4615117410Skan 4616169699Skan d->cur++; 4617169699Skan return init; 4618169699Skan } 4619169699Skan 4620117410Skan /* [dcl.init.aggr] 4621117410Skan 4622117410Skan All implicit type conversions (clause _conv_) are considered when 4623117410Skan initializing the aggregate member with an initializer from an 4624117410Skan initializer-list. If the initializer can initialize a member, 4625117410Skan the member is initialized. Otherwise, if the member is itself a 4626117410Skan non-empty subaggregate, brace elision is assumed and the 4627117410Skan initializer is considered for the initialization of the first 4628117410Skan member of the subaggregate. */ 4629169699Skan if (TREE_CODE (init) != CONSTRUCTOR 4630169699Skan && can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL)) 4631117410Skan { 4632169699Skan d->cur++; 4633169699Skan return init; 4634117410Skan } 4635117410Skan 4636169699Skan /* [dcl.init.string] 4637169699Skan 4638169699Skan A char array (whether plain char, signed char, or unsigned char) 4639169699Skan can be initialized by a string-literal (optionally enclosed in 4640169699Skan braces); a wchar_t array can be initialized by a wide 4641169699Skan string-literal (optionally enclosed in braces). */ 4642169699Skan if (TREE_CODE (type) == ARRAY_TYPE 4643117410Skan && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))) 4644117410Skan { 4645169699Skan tree str_init = init; 4646117410Skan 4647169699Skan /* Strip one level of braces if and only if they enclose a single 4648169699Skan element (as allowed by [dcl.init.string]). */ 4649169699Skan if (!first_initializer_p 4650169699Skan && TREE_CODE (str_init) == CONSTRUCTOR 4651169699Skan && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1) 4652169699Skan { 4653169699Skan str_init = VEC_index (constructor_elt, 4654169699Skan CONSTRUCTOR_ELTS (str_init), 0)->value; 4655169699Skan } 4656169699Skan 4657169699Skan /* If it's a string literal, then it's the initializer for the array 4658169699Skan as a whole. Otherwise, continue with normal initialization for 4659169699Skan array types (one value per array element). */ 4660169699Skan if (TREE_CODE (str_init) == STRING_CST) 4661169699Skan { 4662169699Skan d->cur++; 4663169699Skan return str_init; 4664169699Skan } 4665117410Skan } 4666169699Skan 4667169699Skan /* The following cases are about aggregates. If we are not within a full 4668169699Skan initializer already, and there is not a CONSTRUCTOR, it means that there 4669169699Skan is a missing set of braces (that is, we are processing the case for 4670169699Skan which reshape_init exists). */ 4671169699Skan if (!first_initializer_p) 4672117410Skan { 4673169699Skan if (TREE_CODE (init) == CONSTRUCTOR) 467418334Speter { 4675169699Skan if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init))) 4676169699Skan /* There is no need to reshape pointer-to-member function 4677169699Skan initializers, as they are always constructed correctly 4678169699Skan by the front end. */ 4679169699Skan ; 4680169699Skan else if (COMPOUND_LITERAL_P (init)) 4681169699Skan /* For a nested compound literal, there is no need to reshape since 4682169699Skan brace elision is not allowed. Even if we decided to allow it, 4683169699Skan we should add a call to reshape_init in finish_compound_literal, 4684169699Skan before calling digest_init, so changing this code would still 4685169699Skan not be necessary. */ 4686169699Skan gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init)); 4687169699Skan else 4688117410Skan { 4689169699Skan ++d->cur; 4690169699Skan gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); 4691169699Skan return reshape_init (type, init); 4692117410Skan } 4693169699Skan } 4694117410Skan 4695169699Skan warning (OPT_Wmissing_braces, "missing braces around initializer for %qT", 4696169699Skan type); 4697169699Skan } 4698117410Skan 4699169699Skan /* Dispatch to specialized routines. */ 4700169699Skan if (CLASS_TYPE_P (type)) 4701169699Skan return reshape_init_class (type, d, first_initializer_p); 4702169699Skan else if (TREE_CODE (type) == ARRAY_TYPE) 4703169699Skan return reshape_init_array (type, d); 4704169699Skan else if (TREE_CODE (type) == VECTOR_TYPE) 4705169699Skan return reshape_init_vector (type, d); 4706169699Skan else 4707169699Skan gcc_unreachable(); 4708169699Skan} 4709117410Skan 4710169699Skan/* Undo the brace-elision allowed by [dcl.init.aggr] in a 4711169699Skan brace-enclosed aggregate initializer. 4712117410Skan 4713169699Skan INIT is the CONSTRUCTOR containing the list of initializers describing 4714169699Skan a brace-enclosed initializer for an entity of the indicated aggregate TYPE. 4715169699Skan It may not presently match the shape of the TYPE; for example: 4716117410Skan 4717169699Skan struct S { int a; int b; }; 4718169699Skan struct S a[] = { 1, 2, 3, 4 }; 4719117410Skan 4720169699Skan Here INIT will hold a VEC of four elements, rather than a 4721169699Skan VEC of two elements, each itself a VEC of two elements. This 4722169699Skan routine transforms INIT from the former form into the latter. The 4723169699Skan revised CONSTRUCTOR node is returned. */ 472418334Speter 4725169699Skantree 4726169699Skanreshape_init (tree type, tree init) 4727169699Skan{ 4728169699Skan VEC(constructor_elt, gc) *v; 4729169699Skan reshape_iter d; 4730169699Skan tree new_init; 4731117410Skan 4732169699Skan gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); 4733169699Skan 4734169699Skan v = CONSTRUCTOR_ELTS (init); 4735169699Skan 4736169699Skan /* An empty constructor does not need reshaping, and it is always a valid 4737169699Skan initializer. */ 4738169699Skan if (VEC_empty (constructor_elt, v)) 4739169699Skan return init; 4740169699Skan 4741169699Skan /* Recurse on this CONSTRUCTOR. */ 4742169699Skan d.cur = VEC_index (constructor_elt, v, 0); 4743169699Skan d.end = d.cur + VEC_length (constructor_elt, v); 4744169699Skan 4745169699Skan new_init = reshape_init_r (type, &d, true); 4746169699Skan if (new_init == error_mark_node) 4747169699Skan return error_mark_node; 4748169699Skan 4749169699Skan /* Make sure all the element of the constructor were used. Otherwise, 4750169699Skan issue an error about exceeding initializers. */ 4751169699Skan if (d.cur != d.end) 4752169699Skan error ("too many initializers for %qT", type); 4753169699Skan 4754117410Skan return new_init; 4755117410Skan} 4756117410Skan 4757117410Skan/* Verify INIT (the initializer for DECL), and record the 4758122192Skan initialization in DECL_INITIAL, if appropriate. CLEANUP is as for 4759122192Skan grok_reference_init. 4760117410Skan 4761117410Skan If the return value is non-NULL, it is an expression that must be 4762117410Skan evaluated dynamically to initialize DECL. */ 4763117410Skan 4764117410Skanstatic tree 4765122192Skancheck_initializer (tree decl, tree init, int flags, tree *cleanup) 4766117410Skan{ 4767117410Skan tree type = TREE_TYPE (decl); 4768132747Skan tree init_code = NULL; 4769117410Skan 4770117410Skan /* Things that are going to be initialized need to have complete 4771117410Skan type. */ 4772117410Skan TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); 4773117410Skan 4774117410Skan if (type == error_mark_node) 4775117410Skan /* We will have already complained. */ 4776169699Skan return NULL_TREE; 4777169699Skan 4778169699Skan if (TREE_CODE (type) == ARRAY_TYPE) 4779117410Skan { 4780169699Skan tree element_type = TREE_TYPE (type); 4781169699Skan 4782169699Skan /* The array type itself need not be complete, because the 4783169699Skan initializer may tell us how many elements are in the array. 4784169699Skan But, the elements of the array must be complete. */ 4785169699Skan if (!COMPLETE_TYPE_P (complete_type (element_type))) 4786169699Skan { 4787169699Skan error ("elements of array %q#D have incomplete type", decl); 4788169699Skan return NULL_TREE; 4789169699Skan } 4790169699Skan /* It is not valid to initialize an a VLA. */ 4791169699Skan if (init 4792169699Skan && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) 4793169699Skan || !TREE_CONSTANT (TYPE_SIZE (element_type)))) 4794169699Skan { 4795169699Skan error ("variable-sized object %qD may not be initialized", decl); 4796169699Skan return NULL_TREE; 4797169699Skan } 4798117410Skan } 4799169699Skan else if (!COMPLETE_TYPE_P (type)) 4800117410Skan { 4801169699Skan error ("%qD has incomplete type", decl); 4802169699Skan TREE_TYPE (decl) = error_mark_node; 4803169699Skan return NULL_TREE; 4804117410Skan } 4805169699Skan else 4806169699Skan /* There is no way to make a variable-sized class type in GNU C++. */ 4807169699Skan gcc_assert (TREE_CONSTANT (TYPE_SIZE (type))); 4808169699Skan 4809169699Skan if (!CP_AGGREGATE_TYPE_P (type) 4810169699Skan && init && BRACE_ENCLOSED_INITIALIZER_P (init) 4811169699Skan && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1) 4812117410Skan { 4813169699Skan error ("scalar object %qD requires one element in initializer", decl); 4814117410Skan TREE_TYPE (decl) = error_mark_node; 4815169699Skan return NULL_TREE; 4816117410Skan } 4817117410Skan 481890287Sobrien if (TREE_CODE (decl) == CONST_DECL) 481918334Speter { 4820169699Skan gcc_assert (TREE_CODE (type) != REFERENCE_TYPE); 482118334Speter 482218334Speter DECL_INITIAL (decl) = init; 482318334Speter 4824169699Skan gcc_assert (init != NULL_TREE); 482518334Speter init = NULL_TREE; 482618334Speter } 482790287Sobrien else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE) 4828122192Skan init = grok_reference_init (decl, type, init, cleanup); 4829117410Skan else if (init) 483090287Sobrien { 4831169699Skan /* Do not reshape constructors of vectors (they don't need to be 4832169699Skan reshaped. */ 4833169699Skan if (TREE_CODE (init) == CONSTRUCTOR 4834169699Skan && !COMPOUND_LITERAL_P (init) 4835169699Skan && !TREE_TYPE (init)) /* ptrmemfunc */ 4836117410Skan { 4837169699Skan init = reshape_init (type, init); 4838132747Skan 4839132747Skan if ((*targetm.vector_opaque_p) (type)) 4840132747Skan { 4841132747Skan error ("opaque vector types cannot be initialized"); 4842132747Skan init = error_mark_node; 4843132747Skan } 4844117410Skan } 4845102804Skan 4846117410Skan /* If DECL has an array type without a specific bound, deduce the 4847117410Skan array size from the initializer. */ 4848117410Skan maybe_deduce_size_from_array_init (decl, init); 4849117410Skan type = TREE_TYPE (decl); 4850169699Skan if (type == error_mark_node) 4851169699Skan return NULL_TREE; 4852117410Skan 485318334Speter if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type)) 485418334Speter { 485518334Speter if (TREE_CODE (type) == ARRAY_TYPE) 4856117410Skan goto initialize_aggr; 4857169699Skan else if (TREE_CODE (init) == CONSTRUCTOR) 485818334Speter { 485918334Speter if (TYPE_NON_AGGREGATE_CLASS (type)) 486018334Speter { 4861169699Skan error ("%qD must be initialized by constructor, " 4862169699Skan "not by %<{...}%>", 4863117410Skan decl); 486418334Speter init = error_mark_node; 486518334Speter } 486618334Speter else 486718334Speter goto dont_use_constructor; 486818334Speter } 4869117410Skan else 4870117410Skan { 4871117410Skan int saved_stmts_are_full_exprs_p; 4872117410Skan 4873117410Skan initialize_aggr: 4874117410Skan saved_stmts_are_full_exprs_p = 0; 4875117410Skan if (building_stmt_tree ()) 4876117410Skan { 4877117410Skan saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); 4878117410Skan current_stmt_tree ()->stmts_are_full_exprs_p = 1; 4879117410Skan } 4880117410Skan init = build_aggr_init (decl, init, flags); 4881117410Skan if (building_stmt_tree ()) 4882117410Skan current_stmt_tree ()->stmts_are_full_exprs_p = 4883117410Skan saved_stmts_are_full_exprs_p; 4884117410Skan return init; 4885117410Skan } 488618334Speter } 488718334Speter else 488818334Speter { 488918334Speter dont_use_constructor: 489018334Speter if (TREE_CODE (init) != TREE_VEC) 4891132747Skan { 4892132747Skan init_code = store_init_value (decl, init); 4893169699Skan if (pedantic && TREE_CODE (type) == ARRAY_TYPE 4894169699Skan && DECL_INITIAL (decl) 4895169699Skan && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST 4896169699Skan && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl))) 4897169699Skan warning (0, "array %qD initialized by parenthesized string literal %qE", 4898169699Skan decl, DECL_INITIAL (decl)); 4899132747Skan init = NULL; 4900132747Skan } 490118334Speter } 490218334Speter } 490318334Speter else if (DECL_EXTERNAL (decl)) 490418334Speter ; 4905117410Skan else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type)) 4906117410Skan goto initialize_aggr; 4907117410Skan else if (IS_AGGR_TYPE (type)) 490818334Speter { 490990287Sobrien tree core_type = strip_array_types (type); 491090287Sobrien 4911117410Skan if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)) 4912169699Skan error ("structure %qD with uninitialized const members", decl); 4913117410Skan if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) 4914169699Skan error ("structure %qD with uninitialized reference members", decl); 491518334Speter 491652290Sobrien check_for_uninitialized_const_var (decl); 491718334Speter } 491852290Sobrien else 491952290Sobrien check_for_uninitialized_const_var (decl); 492018334Speter 4921117410Skan if (init && init != error_mark_node) 4922169699Skan init_code = build2 (INIT_EXPR, type, decl, init); 4923117410Skan 4924132747Skan return init_code; 492590287Sobrien} 492618334Speter 492790287Sobrien/* If DECL is not a local variable, give it RTL. */ 492818334Speter 492990287Sobrienstatic void 4930132747Skanmake_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec) 493190287Sobrien{ 493290287Sobrien int toplev = toplevel_bindings_p (); 493390287Sobrien int defer_p; 4934169699Skan const char *filename; 493590287Sobrien 4936169699Skan /* Set the DECL_ASSEMBLER_NAME for the object. */ 4937169699Skan if (asmspec) 4938169699Skan { 4939169699Skan /* The `register' keyword, when used together with an 4940169699Skan asm-specification, indicates that the variable should be 4941169699Skan placed in a particular register. */ 4942169699Skan if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)) 4943169699Skan { 4944169699Skan set_user_assembler_name (decl, asmspec); 4945169699Skan DECL_HARD_REGISTER (decl) = 1; 4946169699Skan } 4947169699Skan else 4948169699Skan { 4949169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 4950169699Skan && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 4951169699Skan set_builtin_user_assembler_name (decl, asmspec); 4952169699Skan set_user_assembler_name (decl, asmspec); 4953169699Skan } 4954169699Skan } 4955169699Skan 495690287Sobrien /* Handle non-variables up front. */ 495790287Sobrien if (TREE_CODE (decl) != VAR_DECL) 495818334Speter { 4959169699Skan rest_of_decl_compilation (decl, toplev, at_eof); 496090287Sobrien return; 496190287Sobrien } 496218334Speter 496390287Sobrien /* If we see a class member here, it should be a static data 496490287Sobrien member. */ 496590287Sobrien if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) 496690287Sobrien { 4967169699Skan gcc_assert (TREE_STATIC (decl)); 496890287Sobrien /* An in-class declaration of a static data member should be 496990287Sobrien external; it is only a declaration, and not a definition. */ 497090287Sobrien if (init == NULL_TREE) 4971169699Skan gcc_assert (DECL_EXTERNAL (decl)); 497290287Sobrien } 497318334Speter 497490287Sobrien /* We don't create any RTL for local variables. */ 497590287Sobrien if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) 497690287Sobrien return; 497790287Sobrien 497890287Sobrien /* We defer emission of local statics until the corresponding 4979169699Skan DECL_EXPR is expanded. */ 498090287Sobrien defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl); 498190287Sobrien 498290287Sobrien /* We try to defer namespace-scope static constants so that they are 498390287Sobrien not emitted into the object file unnecessarily. */ 4984169699Skan filename = input_filename; 498590287Sobrien if (!DECL_VIRTUAL_P (decl) 498690287Sobrien && TREE_READONLY (decl) 498790287Sobrien && DECL_INITIAL (decl) != NULL_TREE 498890287Sobrien && DECL_INITIAL (decl) != error_mark_node 4989169699Skan && filename != NULL 499090287Sobrien && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)) 499190287Sobrien && toplev 499290287Sobrien && !TREE_PUBLIC (decl)) 499390287Sobrien { 499496292Sobrien /* Fool with the linkage of static consts according to #pragma 499596292Sobrien interface. */ 4996169699Skan struct c_fileinfo *finfo = get_fileinfo (filename); 4997169699Skan if (!finfo->interface_unknown && !TREE_PUBLIC (decl)) 499818334Speter { 499990287Sobrien TREE_PUBLIC (decl) = 1; 5000169699Skan DECL_EXTERNAL (decl) = finfo->interface_only; 500118334Speter } 500218334Speter 500390287Sobrien defer_p = 1; 500418334Speter } 500596292Sobrien /* Likewise for template instantiations. */ 5006169699Skan else if (DECL_LANG_SPECIFIC (decl) 5007169699Skan && DECL_IMPLICIT_INSTANTIATION (decl)) 500896292Sobrien defer_p = 1; 500918334Speter 501090287Sobrien /* If we're not deferring, go ahead and assemble the variable. */ 5011169699Skan if (!defer_p) 5012169699Skan rest_of_decl_compilation (decl, toplev, at_eof); 501390287Sobrien} 501490287Sobrien 501590287Sobrien/* Generate code to initialize DECL (a local variable). */ 501690287Sobrien 5017117410Skanstatic void 5018132747Skaninitialize_local_var (tree decl, tree init) 501990287Sobrien{ 502090287Sobrien tree type = TREE_TYPE (decl); 5021117410Skan tree cleanup; 502290287Sobrien 5023169699Skan gcc_assert (TREE_CODE (decl) == VAR_DECL 5024169699Skan || TREE_CODE (decl) == RESULT_DECL); 5025169699Skan gcc_assert (!TREE_STATIC (decl)); 502690287Sobrien 5027117410Skan if (DECL_SIZE (decl) == NULL_TREE) 502818334Speter { 502990287Sobrien /* If we used it already as memory, it must stay in memory. */ 503090287Sobrien DECL_INITIAL (decl) = NULL_TREE; 503190287Sobrien TREE_ADDRESSABLE (decl) = TREE_USED (decl); 503218334Speter } 503318334Speter 503490287Sobrien if (DECL_SIZE (decl) && type != error_mark_node) 503518334Speter { 503690287Sobrien int already_used; 503718334Speter 503890287Sobrien /* Compute and store the initial value. */ 503990287Sobrien already_used = TREE_USED (decl) || TREE_USED (type); 504018334Speter 5041117410Skan /* Perform the initialization. */ 5042117410Skan if (init) 504351412Sobrien { 504490287Sobrien int saved_stmts_are_full_exprs_p; 504518334Speter 5046169699Skan gcc_assert (building_stmt_tree ()); 504790287Sobrien saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); 504890287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 1; 5049117410Skan finish_expr_stmt (init); 505090287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 505190287Sobrien saved_stmts_are_full_exprs_p; 505251412Sobrien } 505351412Sobrien 505490287Sobrien /* Set this to 0 so we can tell whether an aggregate which was 505590287Sobrien initialized was ever used. Don't do this if it has a 505690287Sobrien destructor, so we don't complain about the 'resource 505790287Sobrien allocation is initialization' idiom. Now set 505890287Sobrien attribute((unused)) on types so decls of that type will be 505990287Sobrien marked used. (see TREE_USED, above.) */ 506090287Sobrien if (TYPE_NEEDS_CONSTRUCTING (type) 506190287Sobrien && ! already_used 506290287Sobrien && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) 506390287Sobrien && DECL_NAME (decl)) 506490287Sobrien TREE_USED (decl) = 0; 506590287Sobrien else if (already_used) 506690287Sobrien TREE_USED (decl) = 1; 506790287Sobrien } 506851412Sobrien 5069117410Skan /* Generate a cleanup, if necessary. */ 5070117410Skan cleanup = cxx_maybe_build_cleanup (decl); 507196292Sobrien if (DECL_SIZE (decl) && cleanup) 507290287Sobrien finish_decl_cleanup (decl, cleanup); 507390287Sobrien} 507490287Sobrien 5075169699Skan/* DECL is a VAR_DECL for a compiler-generated variable with static 5076169699Skan storage duration (like a virtual table) whose initializer is a 5077169699Skan compile-time constant. INIT must be either a TREE_LIST of values, 5078169699Skan or a CONSTRUCTOR. Initialize the variable and provide it to the 5079169699Skan back end. */ 5080169699Skan 5081169699Skanvoid 5082169699Skaninitialize_artificial_var (tree decl, tree init) 5083169699Skan{ 5084169699Skan gcc_assert (DECL_ARTIFICIAL (decl)); 5085169699Skan if (TREE_CODE (init) == TREE_LIST) 5086169699Skan init = build_constructor_from_list (NULL_TREE, init); 5087169699Skan gcc_assert (TREE_CODE (init) == CONSTRUCTOR); 5088169699Skan DECL_INITIAL (decl) = init; 5089169699Skan DECL_INITIALIZED_P (decl) = 1; 5090169699Skan determine_visibility (decl); 5091169699Skan layout_var_decl (decl); 5092169699Skan maybe_commonize_var (decl); 5093169699Skan make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL); 5094169699Skan} 5095169699Skan 5096171835Skan/* INIT is the initializer for a variable, as represented by the 5097171835Skan parser. Returns true iff INIT is value-dependent. */ 5098171835Skan 5099171835Skanstatic bool 5100171835Skanvalue_dependent_init_p (tree init) 5101171835Skan{ 5102171835Skan if (TREE_CODE (init) == TREE_LIST) 5103171835Skan /* A parenthesized initializer, e.g.: int i (3, 2); ? */ 5104171835Skan return any_value_dependent_elements_p (init); 5105171835Skan else if (TREE_CODE (init) == CONSTRUCTOR) 5106171835Skan /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */ 5107171835Skan { 5108171835Skan VEC(constructor_elt, gc) *elts; 5109171835Skan size_t nelts; 5110171835Skan size_t i; 5111171835Skan 5112171835Skan elts = CONSTRUCTOR_ELTS (init); 5113171835Skan nelts = VEC_length (constructor_elt, elts); 5114171835Skan for (i = 0; i < nelts; ++i) 5115171835Skan if (value_dependent_init_p (VEC_index (constructor_elt, 5116171835Skan elts, i)->value)) 5117171835Skan return true; 5118171835Skan } 5119171835Skan else 5120171835Skan /* It must be a simple expression, e.g., int i = 3; */ 5121171835Skan return value_dependent_expression_p (init); 5122171835Skan 5123171835Skan return false; 5124171835Skan} 5125171835Skan 512690287Sobrien/* Finish processing of a declaration; 512790287Sobrien install its line number and initial value. 512890287Sobrien If the length of an array type is not known before, 512990287Sobrien it must be determined now, from the initial value, or it is an error. 513090287Sobrien 5131169699Skan INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is 5132169699Skan true, then INIT is an integral constant expression. 513390287Sobrien 513490287Sobrien FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 513590287Sobrien if the (init) syntax was used. */ 513690287Sobrien 513790287Sobrienvoid 5138169699Skancp_finish_decl (tree decl, tree init, bool init_const_expr_p, 5139169699Skan tree asmspec_tree, int flags) 514090287Sobrien{ 5141122192Skan tree type; 5142122192Skan tree cleanup; 514390287Sobrien const char *asmspec = NULL; 514490287Sobrien int was_readonly = 0; 5145146906Skan bool var_definition_p = false; 5146169699Skan int saved_processing_template_decl; 514790287Sobrien 5148132747Skan if (decl == error_mark_node) 5149132747Skan return; 5150132747Skan else if (! decl) 515190287Sobrien { 515290287Sobrien if (init) 515390287Sobrien error ("assignment (not initialization) in declaration"); 515490287Sobrien return; 515590287Sobrien } 515690287Sobrien 5157169699Skan gcc_assert (TREE_CODE (decl) != RESULT_DECL); 5158169699Skan /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ 5159169699Skan gcc_assert (TREE_CODE (decl) != PARM_DECL); 5160132747Skan 5161169699Skan type = TREE_TYPE (decl); 5162169699Skan if (type == error_mark_node) 5163169699Skan return; 5164169699Skan 5165122192Skan /* Assume no cleanup is required. */ 5166122192Skan cleanup = NULL_TREE; 5167169699Skan saved_processing_template_decl = processing_template_decl; 5168122192Skan 516990287Sobrien /* If a name was specified, get the string. */ 5170117410Skan if (global_scope_p (current_binding_level)) 517196292Sobrien asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 5172169699Skan if (asmspec_tree && asmspec_tree != error_mark_node) 517396292Sobrien asmspec = TREE_STRING_POINTER (asmspec_tree); 517490287Sobrien 517590287Sobrien if (current_class_type 517690287Sobrien && CP_DECL_CONTEXT (decl) == current_class_type 517790287Sobrien && TYPE_BEING_DEFINED (current_class_type) 517890287Sobrien && (DECL_INITIAL (decl) || init)) 517990287Sobrien DECL_INITIALIZED_IN_CLASS_P (decl) = 1; 518090287Sobrien 5181169699Skan if (processing_template_decl) 518290287Sobrien { 5183169699Skan bool type_dependent_p; 518490287Sobrien 5185169699Skan /* Add this declaration to the statement-tree. */ 5186169699Skan if (at_function_scope_p ()) 5187169699Skan add_decl_expr (decl); 518890287Sobrien 5189169699Skan type_dependent_p = dependent_type_p (type); 519090287Sobrien 5191169699Skan if (init && init_const_expr_p) 5192169699Skan { 5193169699Skan DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 5194169699Skan if (DECL_INTEGRAL_CONSTANT_VAR_P (decl)) 5195169699Skan TREE_CONSTANT (decl) = 1; 5196169699Skan } 519790287Sobrien 5198171835Skan /* Generally, initializers in templates are expanded when the 5199171835Skan template is instantiated. But, if DECL is an integral 5200171835Skan constant static data member, then it can be used in future 5201171835Skan integral constant expressions, and its value must be 5202171835Skan available. */ 5203171835Skan if (!(init 5204171835Skan && DECL_CLASS_SCOPE_P (decl) 5205171835Skan && DECL_INTEGRAL_CONSTANT_VAR_P (decl) 5206171835Skan && !type_dependent_p 5207171835Skan && !value_dependent_init_p (init))) 5208169699Skan { 5209169699Skan if (init) 5210169699Skan DECL_INITIAL (decl) = init; 5211169699Skan if (TREE_CODE (decl) == VAR_DECL 5212169699Skan && !DECL_PRETTY_FUNCTION_P (decl) 5213169699Skan && !type_dependent_p) 5214169699Skan maybe_deduce_size_from_array_init (decl, init); 5215169699Skan goto finish_end; 5216169699Skan } 521790287Sobrien 5218169699Skan init = fold_non_dependent_expr (init); 5219169699Skan processing_template_decl = 0; 522090287Sobrien } 522190287Sobrien 522290287Sobrien /* Take care of TYPE_DECLs up front. */ 522390287Sobrien if (TREE_CODE (decl) == TYPE_DECL) 522490287Sobrien { 522590287Sobrien if (type != error_mark_node 522690287Sobrien && IS_AGGR_TYPE (type) && DECL_NAME (decl)) 522718334Speter { 522890287Sobrien if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) 5229169699Skan warning (0, "shadowing previous type declaration of %q#D", decl); 5230132747Skan set_identifier_type_value (DECL_NAME (decl), decl); 523118334Speter } 523218334Speter 523390287Sobrien /* If we have installed this as the canonical typedef for this 523490287Sobrien type, and that type has not been defined yet, delay emitting 523590287Sobrien the debug information for it, as we will emit it later. */ 523690287Sobrien if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl 523790287Sobrien && !COMPLETE_TYPE_P (TREE_TYPE (decl))) 523890287Sobrien TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 523918334Speter 5240169699Skan rest_of_decl_compilation (decl, DECL_CONTEXT (decl) == NULL_TREE, 5241169699Skan at_eof); 524290287Sobrien goto finish_end; 524390287Sobrien } 524451412Sobrien 5245169699Skan /* A reference will be modified here, as it is initialized. */ 5246169699Skan if (! DECL_EXTERNAL (decl) 5247169699Skan && TREE_READONLY (decl) 5248169699Skan && TREE_CODE (type) == REFERENCE_TYPE) 524990287Sobrien { 525090287Sobrien was_readonly = 1; 5251169699Skan TREE_READONLY (decl) = 0; 525290287Sobrien } 525351412Sobrien 5254132747Skan if (TREE_CODE (decl) == VAR_DECL) 525590287Sobrien { 5256117410Skan /* Only PODs can have thread-local storage. Other types may require 5257117410Skan various kinds of non-trivial initialization. */ 5258169699Skan if (DECL_THREAD_LOCAL_P (decl) && !pod_type_p (TREE_TYPE (decl))) 5259169699Skan error ("%qD cannot be thread-local because it has non-POD type %qT", 5260117410Skan decl, TREE_TYPE (decl)); 5261169699Skan /* If this is a local variable that will need a mangled name, 5262169699Skan register it now. We must do this before processing the 5263169699Skan initializer for the variable, since the initialization might 5264169699Skan require a guard variable, and since the mangled name of the 5265169699Skan guard variable will depend on the mangled name of this 5266169699Skan variable. */ 5267169699Skan if (!processing_template_decl 5268169699Skan && DECL_FUNCTION_SCOPE_P (decl) 5269169699Skan && TREE_STATIC (decl) 5270169699Skan && !DECL_ARTIFICIAL (decl)) 5271169699Skan push_local_name (decl); 5272117410Skan /* Convert the initializer to the type of DECL, if we have not 5273117410Skan already initialized DECL. */ 5274117410Skan if (!DECL_INITIALIZED_P (decl) 5275117410Skan /* If !DECL_EXTERNAL then DECL is being defined. In the 5276117410Skan case of a static data member initialized inside the 5277117410Skan class-specifier, there can be an initializer even if DECL 5278117410Skan is *not* defined. */ 5279117410Skan && (!DECL_EXTERNAL (decl) || init)) 5280117410Skan { 5281169699Skan if (init) 5282169699Skan { 5283169699Skan DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1; 5284169699Skan if (init_const_expr_p) 5285169699Skan { 5286169699Skan DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 5287169699Skan if (DECL_INTEGRAL_CONSTANT_VAR_P (decl)) 5288169699Skan TREE_CONSTANT (decl) = 1; 5289169699Skan } 5290169699Skan } 5291122192Skan init = check_initializer (decl, init, flags, &cleanup); 5292117410Skan /* Thread-local storage cannot be dynamically initialized. */ 5293169699Skan if (DECL_THREAD_LOCAL_P (decl) && init) 5294117410Skan { 5295169699Skan error ("%qD is thread-local and so cannot be dynamically " 5296117410Skan "initialized", decl); 5297117410Skan init = NULL_TREE; 5298117410Skan } 5299169699Skan 5300169699Skan /* Check that the initializer for a static data member was a 5301169699Skan constant. Although we check in the parser that the 5302169699Skan initializer is an integral constant expression, we do not 5303169699Skan simplify division-by-zero at the point at which it 5304169699Skan occurs. Therefore, in: 5305169699Skan 5306169699Skan struct S { static const int i = 7 / 0; }; 5307169699Skan 5308169699Skan we issue an error at this point. It would 5309169699Skan probably be better to forbid division by zero in 5310169699Skan integral constant expressions. */ 5311161660Skan if (DECL_EXTERNAL (decl) && init) 5312161660Skan { 5313169699Skan error ("%qD cannot be initialized by a non-constant expression" 5314161660Skan " when being declared", decl); 5315161660Skan DECL_INITIALIZED_IN_CLASS_P (decl) = 0; 5316161660Skan init = NULL_TREE; 5317161660Skan } 5318169699Skan 5319117410Skan /* Handle: 5320169699Skan 5321117410Skan [dcl.init] 5322169699Skan 5323117410Skan The memory occupied by any object of static storage 5324117410Skan duration is zero-initialized at program startup before 5325117410Skan any other initialization takes place. 5326169699Skan 5327117410Skan We cannot create an appropriate initializer until after 5328117410Skan the type of DECL is finalized. If DECL_INITIAL is set, 5329117410Skan then the DECL is statically initialized, and any 5330117410Skan necessary zero-initialization has already been performed. */ 5331117410Skan if (TREE_STATIC (decl) && !DECL_INITIAL (decl)) 5332117410Skan DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl), 5333117410Skan /*nelts=*/NULL_TREE, 5334117410Skan /*static_storage_p=*/true); 5335117410Skan /* Remember that the initialization for this variable has 5336117410Skan taken place. */ 5337117410Skan DECL_INITIALIZED_P (decl) = 1; 5338146906Skan /* This declaration is the definition of this variable, 5339146906Skan unless we are initializing a static data member within 5340146906Skan the class specifier. */ 5341146906Skan if (!DECL_EXTERNAL (decl)) 5342146906Skan var_definition_p = true; 5343117410Skan } 5344117410Skan /* If the variable has an array type, lay out the type, even if 5345117410Skan there is no initializer. It is valid to index through the 5346117410Skan array, and we must get TYPE_ALIGN set correctly on the array 5347117410Skan type. */ 5348117410Skan else if (TREE_CODE (type) == ARRAY_TYPE) 5349117410Skan layout_type (type); 5350117410Skan } 535151412Sobrien 535290287Sobrien /* Add this declaration to the statement-tree. This needs to happen 5353169699Skan after the call to check_initializer so that the DECL_EXPR for a 5354169699Skan reference temp is added before the DECL_EXPR for the reference itself. */ 5355132747Skan if (at_function_scope_p ()) 5356169699Skan add_decl_expr (decl); 535718334Speter 5358169699Skan /* Let the middle end know about variables and functions -- but not 5359169699Skan static data members in uninstantiated class templates. */ 5360169699Skan if (!saved_processing_template_decl 5361169699Skan && (TREE_CODE (decl) == VAR_DECL 5362169699Skan || TREE_CODE (decl) == FUNCTION_DECL)) 536390287Sobrien { 536490287Sobrien if (TREE_CODE (decl) == VAR_DECL) 5365169699Skan { 5366169699Skan layout_var_decl (decl); 5367169699Skan maybe_commonize_var (decl); 5368169699Skan } 536918334Speter 537090287Sobrien make_rtl_for_nonlocal_decl (decl, init, asmspec); 537151412Sobrien 5372169699Skan /* Check for abstractness of the type. Notice that there is no 5373169699Skan need to strip array types here since the check for those types 5374169699Skan is already done within create_array_type_for_decl. */ 537590287Sobrien if (TREE_CODE (type) == FUNCTION_TYPE 537690287Sobrien || TREE_CODE (type) == METHOD_TYPE) 5377169699Skan abstract_virtuals_error (decl, TREE_TYPE (type)); 537890287Sobrien else 5379132747Skan abstract_virtuals_error (decl, type); 538051412Sobrien 5381169699Skan /* This needs to happen after the linkage is set. */ 5382169699Skan determine_visibility (decl); 5383169699Skan 5384169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 5385117410Skan || TREE_TYPE (decl) == error_mark_node) 5386117410Skan /* No initialization required. */ 538790287Sobrien ; 538890287Sobrien else if (DECL_EXTERNAL (decl) 538990287Sobrien && ! (DECL_LANG_SPECIFIC (decl) 539090287Sobrien && DECL_NOT_REALLY_EXTERN (decl))) 539190287Sobrien { 539290287Sobrien if (init) 539390287Sobrien DECL_INITIAL (decl) = init; 539490287Sobrien } 5395117410Skan else 539690287Sobrien { 5397117410Skan /* A variable definition. */ 5398117410Skan if (DECL_FUNCTION_SCOPE_P (decl)) 539951412Sobrien { 5400117410Skan /* Initialize the local variable. */ 5401117410Skan if (processing_template_decl) 5402169699Skan DECL_INITIAL (decl) = init; 5403117410Skan else if (!TREE_STATIC (decl)) 5404117410Skan initialize_local_var (decl, init); 540518334Speter } 5406117410Skan 5407146906Skan /* If a variable is defined, and then a subsequent 5408169699Skan definition with external linkage is encountered, we will 5409146906Skan get here twice for the same variable. We want to avoid 5410146906Skan calling expand_static_init more than once. For variables 5411146906Skan that are not static data members, we can call 5412146906Skan expand_static_init only when we actually process the 5413146906Skan initializer. It is not legal to redeclare a static data 5414146906Skan member, so this issue does not arise in that case. */ 5415146906Skan if (var_definition_p && TREE_STATIC (decl)) 5416171835Skan { 5417171835Skan /* If a TREE_READONLY variable needs initialization 5418171835Skan at runtime, it is no longer readonly and we need to 5419171835Skan avoid MEM_READONLY_P being set on RTL created for it. */ 5420171835Skan if (init) 5421171835Skan { 5422171835Skan if (TREE_READONLY (decl)) 5423171835Skan TREE_READONLY (decl) = 0; 5424171835Skan was_readonly = 0; 5425171835Skan } 5426171835Skan expand_static_init (decl, init); 5427171835Skan } 542890287Sobrien } 542918334Speter } 543018334Speter 5431122192Skan /* If a CLEANUP_STMT was created to destroy a temporary bound to a 5432122192Skan reference, insert it in the statement-tree now. */ 5433122192Skan if (cleanup) 5434169699Skan push_cleanup (decl, cleanup, false); 5435122192Skan 543618334Speter finish_end: 5437169699Skan processing_template_decl = saved_processing_template_decl; 543818334Speter 543990287Sobrien if (was_readonly) 544090287Sobrien TREE_READONLY (decl) = 1; 5441132747Skan 5442132747Skan /* If this was marked 'used', be sure it will be output. */ 5443132747Skan if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 5444169699Skan mark_decl_referenced (decl); 544590287Sobrien} 544618334Speter 5447132747Skan/* This is here for a midend callback from c-common.c. */ 544890287Sobrien 544990287Sobrienvoid 5450132747Skanfinish_decl (tree decl, tree init, tree asmspec_tree) 545190287Sobrien{ 5452169699Skan cp_finish_decl (decl, init, /*init_const_expr_p=*/false, asmspec_tree, 0); 545390287Sobrien} 545490287Sobrien 545590287Sobrien/* Returns a declaration for a VAR_DECL as if: 545690287Sobrien 545790287Sobrien extern "C" TYPE NAME; 545890287Sobrien 545990287Sobrien had been seen. Used to create compiler-generated global 546090287Sobrien variables. */ 546190287Sobrien 5462169699Skanstatic tree 5463132747Skandeclare_global_var (tree name, tree type) 546490287Sobrien{ 546590287Sobrien tree decl; 546690287Sobrien 546790287Sobrien push_to_top_level (); 546890287Sobrien decl = build_decl (VAR_DECL, name, type); 546990287Sobrien TREE_PUBLIC (decl) = 1; 547090287Sobrien DECL_EXTERNAL (decl) = 1; 547190287Sobrien DECL_ARTIFICIAL (decl) = 1; 5472169699Skan /* If the user has explicitly declared this variable (perhaps 5473169699Skan because the code we are compiling is part of a low-level runtime 5474169699Skan library), then it is possible that our declaration will be merged 5475169699Skan with theirs by pushdecl. */ 5476169699Skan decl = pushdecl (decl); 5477169699Skan finish_decl (decl, NULL_TREE, NULL_TREE); 547890287Sobrien pop_from_top_level (); 547990287Sobrien 548090287Sobrien return decl; 548190287Sobrien} 548290287Sobrien 548390287Sobrien/* Returns a pointer to the `atexit' function. Note that if 5484117410Skan FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new 548590287Sobrien `__cxa_atexit' function specified in the IA64 C++ ABI. */ 548690287Sobrien 548790287Sobrienstatic tree 5488132747Skanget_atexit_node (void) 548990287Sobrien{ 549090287Sobrien tree atexit_fndecl; 549190287Sobrien tree arg_types; 549290287Sobrien tree fn_type; 549390287Sobrien tree fn_ptr_type; 549490287Sobrien const char *name; 5495169699Skan bool use_aeabi_atexit; 549690287Sobrien 549790287Sobrien if (atexit_node) 549890287Sobrien return atexit_node; 549990287Sobrien 550090287Sobrien if (flag_use_cxa_atexit) 550118334Speter { 550290287Sobrien /* The declaration for `__cxa_atexit' is: 550318334Speter 550490287Sobrien int __cxa_atexit (void (*)(void *), void *, void *) 550518334Speter 550690287Sobrien We build up the argument types and then then function type 550790287Sobrien itself. */ 550890287Sobrien 5509169699Skan use_aeabi_atexit = targetm.cxx.use_aeabi_atexit (); 551090287Sobrien /* First, build the pointer-to-function type for the first 551190287Sobrien argument. */ 551290287Sobrien arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); 551390287Sobrien fn_type = build_function_type (void_type_node, arg_types); 551490287Sobrien fn_ptr_type = build_pointer_type (fn_type); 551590287Sobrien /* Then, build the rest of the argument types. */ 551690287Sobrien arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); 5517169699Skan if (use_aeabi_atexit) 5518169699Skan { 5519169699Skan arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); 5520169699Skan arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); 5521169699Skan } 5522169699Skan else 5523169699Skan { 5524169699Skan arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); 5525169699Skan arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); 5526169699Skan } 552790287Sobrien /* And the final __cxa_atexit type. */ 552890287Sobrien fn_type = build_function_type (integer_type_node, arg_types); 552990287Sobrien fn_ptr_type = build_pointer_type (fn_type); 5530169699Skan if (use_aeabi_atexit) 5531169699Skan name = "__aeabi_atexit"; 5532169699Skan else 5533169699Skan name = "__cxa_atexit"; 553418334Speter } 553590287Sobrien else 553690287Sobrien { 553790287Sobrien /* The declaration for `atexit' is: 553818334Speter 5539169699Skan int atexit (void (*)()); 554090287Sobrien 554190287Sobrien We build up the argument types and then then function type 554290287Sobrien itself. */ 554390287Sobrien fn_type = build_function_type (void_type_node, void_list_node); 554490287Sobrien fn_ptr_type = build_pointer_type (fn_type); 554590287Sobrien arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node); 554690287Sobrien /* Build the final atexit type. */ 554790287Sobrien fn_type = build_function_type (integer_type_node, arg_types); 554890287Sobrien name = "atexit"; 554990287Sobrien } 555090287Sobrien 555190287Sobrien /* Now, build the function declaration. */ 555290287Sobrien push_lang_context (lang_name_c); 555390287Sobrien atexit_fndecl = build_library_fn_ptr (name, fn_type); 555490287Sobrien mark_used (atexit_fndecl); 555590287Sobrien pop_lang_context (); 5556132747Skan atexit_node = decay_conversion (atexit_fndecl); 555790287Sobrien 555890287Sobrien return atexit_node; 555990287Sobrien} 556090287Sobrien 556190287Sobrien/* Returns the __dso_handle VAR_DECL. */ 556290287Sobrien 556390287Sobrienstatic tree 5564132747Skanget_dso_handle_node (void) 556590287Sobrien{ 556690287Sobrien if (dso_handle_node) 556790287Sobrien return dso_handle_node; 556890287Sobrien 556990287Sobrien /* Declare the variable. */ 557090287Sobrien dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), 557190287Sobrien ptr_type_node); 557290287Sobrien 557390287Sobrien return dso_handle_node; 557490287Sobrien} 557590287Sobrien 557690287Sobrien/* Begin a new function with internal linkage whose job will be simply 557790287Sobrien to destroy some particular variable. */ 557890287Sobrien 5579132747Skanstatic GTY(()) int start_cleanup_cnt; 5580132747Skan 558190287Sobrienstatic tree 5582132747Skanstart_cleanup_fn (void) 558390287Sobrien{ 558490287Sobrien char name[32]; 558590287Sobrien tree parmtypes; 558690287Sobrien tree fntype; 558790287Sobrien tree fndecl; 558890287Sobrien 558990287Sobrien push_to_top_level (); 559090287Sobrien 559190287Sobrien /* No need to mangle this. */ 559290287Sobrien push_lang_context (lang_name_c); 559390287Sobrien 559490287Sobrien /* Build the parameter-types. */ 559590287Sobrien parmtypes = void_list_node; 559690287Sobrien /* Functions passed to __cxa_atexit take an additional parameter. 559790287Sobrien We'll just ignore it. After we implement the new calling 559890287Sobrien convention for destructors, we can eliminate the use of 559990287Sobrien additional cleanup functions entirely in the -fnew-abi case. */ 560090287Sobrien if (flag_use_cxa_atexit) 560190287Sobrien parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes); 560290287Sobrien /* Build the function type itself. */ 560390287Sobrien fntype = build_function_type (void_type_node, parmtypes); 560490287Sobrien /* Build the name of the function. */ 5605132747Skan sprintf (name, "__tcf_%d", start_cleanup_cnt++); 560690287Sobrien /* Build the function declaration. */ 560790287Sobrien fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); 560890287Sobrien /* It's a function with internal linkage, generated by the 560990287Sobrien compiler. */ 561090287Sobrien TREE_PUBLIC (fndecl) = 0; 561190287Sobrien DECL_ARTIFICIAL (fndecl) = 1; 561290287Sobrien /* Make the function `inline' so that it is only emitted if it is 561390287Sobrien actually needed. It is unlikely that it will be inlined, since 561490287Sobrien it is only called via a function pointer, but we avoid unnecessary 561590287Sobrien emissions this way. */ 561690287Sobrien DECL_INLINE (fndecl) = 1; 5617132747Skan DECL_DECLARED_INLINE_P (fndecl) = 1; 5618132747Skan DECL_INTERFACE_KNOWN (fndecl) = 1; 561990287Sobrien /* Build the parameter. */ 562090287Sobrien if (flag_use_cxa_atexit) 562118334Speter { 562290287Sobrien tree parmdecl; 562390287Sobrien 5624117410Skan parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node); 562590287Sobrien DECL_CONTEXT (parmdecl) = fndecl; 562690287Sobrien TREE_USED (parmdecl) = 1; 562790287Sobrien DECL_ARGUMENTS (fndecl) = parmdecl; 562818334Speter } 562918334Speter 563090287Sobrien pushdecl (fndecl); 5631169699Skan start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED); 563290287Sobrien 563390287Sobrien pop_lang_context (); 563490287Sobrien 563590287Sobrien return current_function_decl; 563618334Speter} 563718334Speter 563890287Sobrien/* Finish the cleanup function begun by start_cleanup_fn. */ 563951412Sobrien 564090287Sobrienstatic void 5641132747Skanend_cleanup_fn (void) 564290287Sobrien{ 5643132747Skan expand_or_defer_fn (finish_function (0)); 564490287Sobrien 564590287Sobrien pop_from_top_level (); 564690287Sobrien} 564790287Sobrien 564890287Sobrien/* Generate code to handle the destruction of DECL, an object with 564990287Sobrien static storage duration. */ 565090287Sobrien 5651169699Skantree 5652132747Skanregister_dtor_fn (tree decl) 565318334Speter{ 565490287Sobrien tree cleanup; 565590287Sobrien tree compound_stmt; 565690287Sobrien tree args; 565790287Sobrien tree fcall; 565890287Sobrien 565990287Sobrien if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 5660169699Skan return void_zero_node; 566190287Sobrien 566290287Sobrien /* Call build_cleanup before we enter the anonymous function so that 566390287Sobrien any access checks will be done relative to the current scope, 566490287Sobrien rather than the scope of the anonymous function. */ 566590287Sobrien build_cleanup (decl); 566690287Sobrien 566790287Sobrien /* Now start the function. */ 566890287Sobrien cleanup = start_cleanup_fn (); 566990287Sobrien 567090287Sobrien /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer 567190287Sobrien to the original function, rather than the anonymous one. That 567290287Sobrien will make the back-end think that nested functions are in use, 567390287Sobrien which causes confusion. */ 5674169699Skan 5675132747Skan push_deferring_access_checks (dk_no_check); 567690287Sobrien fcall = build_cleanup (decl); 5677132747Skan pop_deferring_access_checks (); 567890287Sobrien 567990287Sobrien /* Create the body of the anonymous function. */ 5680169699Skan compound_stmt = begin_compound_stmt (BCS_FN_BODY); 568190287Sobrien finish_expr_stmt (fcall); 5682132747Skan finish_compound_stmt (compound_stmt); 568390287Sobrien end_cleanup_fn (); 568490287Sobrien 568590287Sobrien /* Call atexit with the cleanup function. */ 5686117410Skan cxx_mark_addressable (cleanup); 5687132747Skan mark_used (cleanup); 568890287Sobrien cleanup = build_unary_op (ADDR_EXPR, cleanup, 0); 568990287Sobrien if (flag_use_cxa_atexit) 569090287Sobrien { 5691169699Skan args = tree_cons (NULL_TREE, 569296292Sobrien build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0), 569396292Sobrien NULL_TREE); 5694169699Skan if (targetm.cxx.use_aeabi_atexit ()) 5695169699Skan { 5696169699Skan args = tree_cons (NULL_TREE, cleanup, args); 5697169699Skan args = tree_cons (NULL_TREE, null_pointer_node, args); 5698169699Skan } 5699169699Skan else 5700169699Skan { 5701169699Skan args = tree_cons (NULL_TREE, null_pointer_node, args); 5702169699Skan args = tree_cons (NULL_TREE, cleanup, args); 5703169699Skan } 570490287Sobrien } 570590287Sobrien else 570690287Sobrien args = tree_cons (NULL_TREE, cleanup, NULL_TREE); 5707169699Skan return build_function_call (get_atexit_node (), args); 570818334Speter} 570918334Speter 5710117410Skan/* DECL is a VAR_DECL with static storage duration. INIT, if present, 5711117410Skan is its initializer. Generate code to handle the construction 5712117410Skan and destruction of DECL. */ 5713117410Skan 5714117410Skanstatic void 5715132747Skanexpand_static_init (tree decl, tree init) 571618334Speter{ 5717169699Skan gcc_assert (TREE_CODE (decl) == VAR_DECL); 5718169699Skan gcc_assert (TREE_STATIC (decl)); 5719117410Skan 5720117410Skan /* Some variables require no initialization. */ 5721169699Skan if (!init 5722117410Skan && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) 5723117410Skan && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 5724117410Skan return; 5725117410Skan 5726169699Skan if (DECL_FUNCTION_SCOPE_P (decl)) 572718334Speter { 572818334Speter /* Emit code to perform this initialization but once. */ 5729169699Skan tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE; 5730169699Skan tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE; 5731169699Skan tree guard, guard_addr, guard_addr_list; 5732169699Skan tree acquire_fn, release_fn, abort_fn; 5733169699Skan tree flag, begin; 573418334Speter 573552290Sobrien /* Emit code to perform this initialization but once. This code 573652290Sobrien looks like: 573752290Sobrien 5738169699Skan static <type> guard; 5739169699Skan if (!guard.first_byte) { 5740169699Skan if (__cxa_guard_acquire (&guard)) { 5741169699Skan bool flag = false; 5742169699Skan try { 5743169699Skan // Do initialization. 5744169699Skan flag = true; __cxa_guard_release (&guard); 5745169699Skan // Register variable for destruction at end of program. 5746169699Skan } catch { 5747169699Skan if (!flag) __cxa_guard_abort (&guard); 5748169699Skan } 574952290Sobrien } 575052290Sobrien 5751169699Skan Note that the `flag' variable is only set to 1 *after* the 575252290Sobrien initialization is complete. This ensures that an exception, 575352290Sobrien thrown during the construction, will cause the variable to 575452290Sobrien reinitialized when we pass through this code again, as per: 575590287Sobrien 575652290Sobrien [stmt.dcl] 575752290Sobrien 575852290Sobrien If the initialization exits by throwing an exception, the 575952290Sobrien initialization is not complete, so it will be tried again 576052290Sobrien the next time control enters the declaration. 576152290Sobrien 5762169699Skan This process should be thread-safe, too; multiple threads 5763169699Skan should not be able to initialize the variable more than 5764169699Skan once. */ 576552290Sobrien 576690287Sobrien /* Create the guard variable. */ 576790287Sobrien guard = get_guard (decl); 576890287Sobrien 5769169699Skan /* This optimization isn't safe on targets with relaxed memory 5770169699Skan consistency. On such targets we force synchronization in 5771169699Skan __cxa_guard_acquire. */ 5772169699Skan if (!targetm.relaxed_ordering || !flag_threadsafe_statics) 5773169699Skan { 5774169699Skan /* Begin the conditional initialization. */ 5775169699Skan if_stmt = begin_if_stmt (); 5776169699Skan finish_if_stmt_cond (get_guard_cond (guard), if_stmt); 5777169699Skan then_clause = begin_compound_stmt (BCS_NO_SCOPE); 5778169699Skan } 577951412Sobrien 5780169699Skan if (flag_threadsafe_statics) 5781169699Skan { 5782169699Skan guard_addr = build_address (guard); 5783169699Skan guard_addr_list = build_tree_list (NULL_TREE, guard_addr); 578490287Sobrien 5785169699Skan acquire_fn = get_identifier ("__cxa_guard_acquire"); 5786169699Skan release_fn = get_identifier ("__cxa_guard_release"); 5787169699Skan abort_fn = get_identifier ("__cxa_guard_abort"); 5788169699Skan if (!get_global_value_if_present (acquire_fn, &acquire_fn)) 5789169699Skan { 5790169699Skan tree argtypes = tree_cons (NULL_TREE, TREE_TYPE (guard_addr), 5791169699Skan void_list_node); 5792169699Skan tree vfntype = build_function_type (void_type_node, argtypes); 5793169699Skan acquire_fn = push_library_fn 5794169699Skan (acquire_fn, build_function_type (integer_type_node, argtypes)); 5795169699Skan release_fn = push_library_fn (release_fn, vfntype); 5796169699Skan abort_fn = push_library_fn (abort_fn, vfntype); 5797169699Skan } 5798169699Skan else 5799169699Skan { 5800169699Skan release_fn = identifier_global_value (release_fn); 5801169699Skan abort_fn = identifier_global_value (abort_fn); 5802169699Skan } 5803169699Skan 5804169699Skan inner_if_stmt = begin_if_stmt (); 5805169699Skan finish_if_stmt_cond (build_call (acquire_fn, guard_addr_list), 5806169699Skan inner_if_stmt); 5807169699Skan 5808169699Skan inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE); 5809169699Skan begin = get_target_expr (boolean_false_node); 5810169699Skan flag = TARGET_EXPR_SLOT (begin); 5811169699Skan 5812169699Skan TARGET_EXPR_CLEANUP (begin) 5813169699Skan = build3 (COND_EXPR, void_type_node, flag, 5814169699Skan void_zero_node, 5815169699Skan build_call (abort_fn, guard_addr_list)); 5816169699Skan CLEANUP_EH_ONLY (begin) = 1; 5817169699Skan 5818169699Skan /* Do the initialization itself. */ 5819169699Skan init = add_stmt_to_compound (begin, init); 5820169699Skan init = add_stmt_to_compound 5821169699Skan (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node)); 5822169699Skan init = add_stmt_to_compound 5823169699Skan (init, build_call (release_fn, guard_addr_list)); 5824169699Skan } 582590287Sobrien else 5826169699Skan init = add_stmt_to_compound (init, set_guard (guard)); 582718334Speter 582852290Sobrien /* Use atexit to register a function for destroying this static 582952290Sobrien variable. */ 5830169699Skan init = add_stmt_to_compound (init, register_dtor_fn (decl)); 583152290Sobrien 5832169699Skan finish_expr_stmt (init); 583351412Sobrien 5834169699Skan if (flag_threadsafe_statics) 5835169699Skan { 5836169699Skan finish_compound_stmt (inner_then_clause); 5837169699Skan finish_then_clause (inner_if_stmt); 5838169699Skan finish_if_stmt (inner_if_stmt); 5839169699Skan } 584051412Sobrien 5841169699Skan if (!targetm.relaxed_ordering || !flag_threadsafe_statics) 5842169699Skan { 5843169699Skan finish_compound_stmt (then_clause); 5844169699Skan finish_then_clause (if_stmt); 5845169699Skan finish_if_stmt (if_stmt); 5846169699Skan } 584718334Speter } 584818334Speter else 5849169699Skan static_aggregates = tree_cons (init, decl, static_aggregates); 585018334Speter} 585190287Sobrien 585218334Speter 585318334Speter/* Make TYPE a complete type based on INITIAL_VALUE. 585418334Speter Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 5855169699Skan 2 if there was no information (in which case assume 0 if DO_DEFAULT), 5856169699Skan 3 if the initializer list is empty (in pedantic mode). */ 585718334Speter 585818334Speterint 5859169699Skancp_complete_array_type (tree *ptype, tree initial_value, bool do_default) 586018334Speter{ 5861169699Skan int failure; 5862169699Skan tree type, elt_type; 586390287Sobrien 586418334Speter if (initial_value) 586518334Speter { 586690287Sobrien /* An array of character type can be initialized from a 5867169699Skan brace-enclosed string constant. 5868169699Skan 5869169699Skan FIXME: this code is duplicated from reshape_init. Probably 5870169699Skan we should just call reshape_init here? */ 5871169699Skan if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype))) 587290287Sobrien && TREE_CODE (initial_value) == CONSTRUCTOR 5873169699Skan && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value))) 587418334Speter { 5875169699Skan VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value); 5876169699Skan tree value = VEC_index (constructor_elt, v, 0)->value; 587790287Sobrien 5878169699Skan if (TREE_CODE (value) == STRING_CST 5879169699Skan && VEC_length (constructor_elt, v) == 1) 5880169699Skan initial_value = value; 588118334Speter } 588218334Speter } 588318334Speter 5884169699Skan failure = complete_array_type (ptype, initial_value, do_default); 588518334Speter 5886169699Skan /* We can create the array before the element type is complete, which 5887169699Skan means that we didn't have these two bits set in the original type 5888169699Skan either. In completing the type, we are expected to propagate these 5889169699Skan bits. See also complete_type which does the same thing for arrays 5890169699Skan of fixed size. */ 5891169699Skan type = *ptype; 5892169699Skan if (TYPE_DOMAIN (type)) 589318334Speter { 5894169699Skan elt_type = TREE_TYPE (type); 5895169699Skan TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type); 5896169699Skan TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 5897169699Skan = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type); 589818334Speter } 589918334Speter 5900169699Skan return failure; 590118334Speter} 590218334Speter 590318334Speter/* Return zero if something is declared to be a member of type 590418334Speter CTYPE when in the context of CUR_TYPE. STRING is the error 590518334Speter message to print in that case. Otherwise, quietly return 1. */ 590651412Sobrien 590718334Speterstatic int 5908132747Skanmember_function_or_else (tree ctype, tree cur_type, enum overload_flags flags) 590918334Speter{ 591018334Speter if (ctype && ctype != cur_type) 591118334Speter { 591290287Sobrien if (flags == DTOR_FLAG) 5913169699Skan error ("destructor for alien class %qT cannot be a member", ctype); 591490287Sobrien else 5915169699Skan error ("constructor for alien class %qT cannot be a member", ctype); 591618334Speter return 0; 591718334Speter } 591818334Speter return 1; 591918334Speter} 592018334Speter 592118334Speter/* Subroutine of `grokdeclarator'. */ 592218334Speter 592318334Speter/* Generate errors possibly applicable for a given set of specifiers. 592418334Speter This is for ARM $7.1.2. */ 592551412Sobrien 592618334Speterstatic void 5927132747Skanbad_specifiers (tree object, 5928169699Skan const char* type, 5929169699Skan int virtualp, 5930169699Skan int quals, 5931169699Skan int inlinep, 5932169699Skan int friendp, 5933169699Skan int raises) 593418334Speter{ 593518334Speter if (virtualp) 5936169699Skan error ("%qD declared as a %<virtual%> %s", object, type); 593718334Speter if (inlinep) 5938169699Skan error ("%qD declared as an %<inline%> %s", object, type); 593918334Speter if (quals) 5940169699Skan error ("%<const%> and %<volatile%> function specifiers on " 5941169699Skan "%qD invalid in %s declaration", 5942169699Skan object, type); 594318334Speter if (friendp) 5944169699Skan error ("%q+D declared as a friend", object); 594590287Sobrien if (raises 594690287Sobrien && (TREE_CODE (object) == TYPE_DECL 594790287Sobrien || (!TYPE_PTRFN_P (TREE_TYPE (object)) 594890287Sobrien && !TYPE_REFFN_P (TREE_TYPE (object)) 594990287Sobrien && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) 5950169699Skan error ("%q+D declared with an exception specification", object); 595118334Speter} 595218334Speter 5953169699Skan/* DECL is a member function or static data member and is presently 5954169699Skan being defined. Check that the definition is taking place in a 5955169699Skan valid namespace. */ 5956169699Skan 5957169699Skanstatic void 5958169699Skancheck_class_member_definition_namespace (tree decl) 5959169699Skan{ 5960169699Skan /* These checks only apply to member functions and static data 5961169699Skan members. */ 5962169699Skan gcc_assert (TREE_CODE (decl) == FUNCTION_DECL 5963169699Skan || TREE_CODE (decl) == VAR_DECL); 5964169699Skan /* We check for problems with specializations in pt.c in 5965169699Skan check_specialization_namespace, where we can issue better 5966169699Skan diagnostics. */ 5967169699Skan if (processing_specialization) 5968169699Skan return; 5969169699Skan /* There are no restrictions on the placement of 5970169699Skan explicit instantiations. */ 5971169699Skan if (processing_explicit_instantiation) 5972169699Skan return; 5973169699Skan /* [class.mfct] 5974169699Skan 5975169699Skan A member function definition that appears outside of the 5976169699Skan class definition shall appear in a namespace scope enclosing 5977169699Skan the class definition. 5978169699Skan 5979169699Skan [class.static.data] 5980169699Skan 5981169699Skan The definition for a static data member shall appear in a 5982169699Skan namespace scope enclosing the member's class definition. */ 5983169699Skan if (!is_ancestor (current_namespace, DECL_CONTEXT (decl))) 5984169699Skan pedwarn ("definition of %qD is not in namespace enclosing %qT", 5985169699Skan decl, DECL_CONTEXT (decl)); 5986169699Skan} 5987169699Skan 5988169699Skan/* Build a PARM_DECL for the "this" parameter. TYPE is the 5989169699Skan METHOD_TYPE for a non-static member function; QUALS are the 5990169699Skan cv-qualifiers that apply to the function. */ 5991169699Skan 5992169699Skantree 5993169699Skanbuild_this_parm (tree type, cp_cv_quals quals) 5994169699Skan{ 5995169699Skan tree this_type; 5996169699Skan tree qual_type; 5997169699Skan tree parm; 5998169699Skan cp_cv_quals this_quals; 5999169699Skan 6000169699Skan this_type = TREE_VALUE (TYPE_ARG_TYPES (type)); 6001169699Skan /* The `this' parameter is implicitly `const'; it cannot be 6002169699Skan assigned to. */ 6003169699Skan this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST; 6004169699Skan qual_type = cp_build_qualified_type (this_type, this_quals); 6005169699Skan parm = build_artificial_parm (this_identifier, qual_type); 6006169699Skan cp_apply_type_quals_to_decl (this_quals, parm); 6007169699Skan return parm; 6008169699Skan} 6009169699Skan 601018334Speter/* CTYPE is class type, or null if non-class. 601118334Speter TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE 601218334Speter or METHOD_TYPE. 601318334Speter DECLARATOR is the function's name. 6014132747Skan PARMS is a chain of PARM_DECLs for the function. 601518334Speter VIRTUALP is truthvalue of whether the function is virtual or not. 601618334Speter FLAGS are to be passed through to `grokclassfn'. 601718334Speter QUALS are qualifiers indicating whether the function is `const' 601818334Speter or `volatile'. 601918334Speter RAISES is a list of exceptions that this function can raise. 602018334Speter CHECK is 1 if we must find this method in CTYPE, 0 if we should 602190287Sobrien not look, and -1 if we should not call `grokclassfn' at all. 602251412Sobrien 6023169699Skan SFK is the kind of special function (if any) for the new function. 6024169699Skan 602552290Sobrien Returns `NULL_TREE' if something goes wrong, after issuing 602652290Sobrien applicable error messages. */ 602752290Sobrien 602818334Speterstatic tree 6029169699Skangrokfndecl (tree ctype, 6030169699Skan tree type, 6031169699Skan tree declarator, 6032132747Skan tree parms, 6033169699Skan tree orig_declarator, 6034169699Skan int virtualp, 6035169699Skan enum overload_flags flags, 6036169699Skan cp_cv_quals quals, 6037169699Skan tree raises, 6038169699Skan int check, 6039169699Skan int friendp, 6040169699Skan int publicp, 6041169699Skan int inlinep, 6042169699Skan special_function_kind sfk, 6043169699Skan bool funcdef_flag, 6044169699Skan int template_count, 6045169699Skan tree in_namespace, 6046169699Skan tree* attrlist) 604718334Speter{ 604890287Sobrien tree decl; 604918334Speter int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; 605051412Sobrien tree t; 605118334Speter 605218334Speter if (raises) 605390287Sobrien type = build_exception_variant (type, raises); 605451412Sobrien 605518334Speter decl = build_lang_decl (FUNCTION_DECL, declarator, type); 6056169699Skan if (TREE_CODE (type) == METHOD_TYPE) 6057169699Skan { 6058169699Skan tree parm; 6059169699Skan parm = build_this_parm (type, quals); 6060169699Skan TREE_CHAIN (parm) = parms; 6061169699Skan parms = parm; 6062169699Skan } 6063132747Skan DECL_ARGUMENTS (decl) = parms; 6064117410Skan /* Propagate volatile out from type to decl. */ 606518334Speter if (TYPE_VOLATILE (type)) 606651412Sobrien TREE_THIS_VOLATILE (decl) = 1; 606718334Speter 6068169699Skan if (friendp 6069169699Skan && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) 6070169699Skan { 6071169699Skan if (funcdef_flag) 6072169699Skan error 6073169699Skan ("defining explicit specialization %qD in friend declaration", 6074169699Skan orig_declarator); 6075169699Skan else 6076169699Skan { 6077169699Skan tree fns = TREE_OPERAND (orig_declarator, 0); 6078169699Skan tree args = TREE_OPERAND (orig_declarator, 1); 6079169699Skan 6080169699Skan if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 6081169699Skan { 6082169699Skan /* Something like `template <class T> friend void f<T>()'. */ 6083169699Skan error ("invalid use of template-id %qD in declaration " 6084169699Skan "of primary template", 6085169699Skan orig_declarator); 6086169699Skan return NULL_TREE; 6087169699Skan } 6088169699Skan 6089169699Skan 6090169699Skan /* A friend declaration of the form friend void f<>(). Record 6091169699Skan the information in the TEMPLATE_ID_EXPR. */ 6092169699Skan SET_DECL_IMPLICIT_INSTANTIATION (decl); 6093169699Skan 6094169699Skan if (TREE_CODE (fns) == COMPONENT_REF) 6095169699Skan { 6096169699Skan /* Due to bison parser ickiness, we will have already looked 6097169699Skan up an operator_name or PFUNCNAME within the current class 6098169699Skan (see template_id in parse.y). If the current class contains 6099169699Skan such a name, we'll get a COMPONENT_REF here. Undo that. */ 6100169699Skan 6101169699Skan gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0)) 6102169699Skan == current_class_type); 6103169699Skan fns = TREE_OPERAND (fns, 1); 6104169699Skan } 6105169699Skan gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE 6106169699Skan || TREE_CODE (fns) == OVERLOAD); 6107169699Skan DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE); 6108169699Skan 6109169699Skan for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) 6110169699Skan if (TREE_PURPOSE (t) 6111169699Skan && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) 6112169699Skan { 6113169699Skan error ("default arguments are not allowed in declaration " 6114169699Skan "of friend template specialization %qD", 6115169699Skan decl); 6116169699Skan return NULL_TREE; 6117169699Skan } 6118169699Skan 6119169699Skan if (inlinep) 6120169699Skan { 6121169699Skan error ("%<inline%> is not allowed in declaration of friend " 6122169699Skan "template specialization %qD", 6123169699Skan decl); 6124169699Skan return NULL_TREE; 6125169699Skan } 6126169699Skan } 6127169699Skan } 6128169699Skan 612952290Sobrien /* If this decl has namespace scope, set that up. */ 613051412Sobrien if (in_namespace) 613152290Sobrien set_decl_namespace (decl, in_namespace, friendp); 613290287Sobrien else if (!ctype) 613352290Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 613451412Sobrien 613552290Sobrien /* `main' and builtins have implicit 'C' linkage. */ 613652290Sobrien if ((MAIN_NAME_P (declarator) 613752290Sobrien || (IDENTIFIER_LENGTH (declarator) > 10 613852290Sobrien && IDENTIFIER_POINTER (declarator)[0] == '_' 613952290Sobrien && IDENTIFIER_POINTER (declarator)[1] == '_' 614052290Sobrien && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)) 614152290Sobrien && current_lang_name == lang_name_cplusplus 614252290Sobrien && ctype == NULL_TREE 614352290Sobrien /* NULL_TREE means global namespace. */ 614452290Sobrien && DECL_CONTEXT (decl) == NULL_TREE) 614590287Sobrien SET_DECL_LANGUAGE (decl, lang_c); 614652290Sobrien 614718334Speter /* Should probably propagate const out from type to decl I bet (mrs). */ 614818334Speter if (staticp) 614918334Speter { 615018334Speter DECL_STATIC_FUNCTION_P (decl) = 1; 615118334Speter DECL_CONTEXT (decl) = ctype; 615218334Speter } 615318334Speter 615451412Sobrien if (ctype) 6155169699Skan { 6156169699Skan DECL_CONTEXT (decl) = ctype; 6157169699Skan if (funcdef_flag) 6158169699Skan check_class_member_definition_namespace (decl); 6159169699Skan } 616018334Speter 616152290Sobrien if (ctype == NULL_TREE && DECL_MAIN_P (decl)) 616218334Speter { 616352290Sobrien if (processing_template_decl) 6164169699Skan error ("cannot declare %<::main%> to be a template"); 616518334Speter if (inlinep) 6166169699Skan error ("cannot declare %<::main%> to be inline"); 616790287Sobrien if (!publicp) 6168169699Skan error ("cannot declare %<::main%> to be static"); 616918334Speter inlinep = 0; 617018334Speter publicp = 1; 617118334Speter } 617252290Sobrien 617352290Sobrien /* Members of anonymous types and local classes have no linkage; make 6174169699Skan them internal. If a typedef is made later, this will be changed. */ 617590287Sobrien if (ctype && (TYPE_ANONYMOUS_P (ctype) 617690287Sobrien || decl_function_context (TYPE_MAIN_DECL (ctype)))) 617752290Sobrien publicp = 0; 617852290Sobrien 617952290Sobrien if (publicp) 618052290Sobrien { 618152290Sobrien /* [basic.link]: A name with no linkage (notably, the name of a class 618252290Sobrien or enumeration declared in a local scope) shall not be used to 618352290Sobrien declare an entity with linkage. 618452290Sobrien 6185132747Skan Only check this for public decls for now. See core 319, 389. */ 6186169699Skan t = no_linkage_check (TREE_TYPE (decl), 6187169699Skan /*relaxed_p=*/false); 618852290Sobrien if (t) 618952290Sobrien { 619090287Sobrien if (TYPE_ANONYMOUS_P (t)) 619152290Sobrien { 619290287Sobrien if (DECL_EXTERN_C_P (decl)) 619352290Sobrien /* Allow this; it's pretty common in C. */; 619452290Sobrien else 619590287Sobrien { 6196169699Skan pedwarn ("non-local function %q#D uses anonymous type", 619790287Sobrien decl); 619890287Sobrien if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) 6199169699Skan pedwarn ("%q+#D does not refer to the unqualified " 6200169699Skan "type, so it is not used for linkage", 6201169699Skan TYPE_NAME (t)); 620290287Sobrien } 620352290Sobrien } 620452290Sobrien else 6205169699Skan pedwarn ("non-local function %q#D uses local type %qT", decl, t); 620652290Sobrien } 620752290Sobrien } 620852290Sobrien 620951412Sobrien TREE_PUBLIC (decl) = publicp; 621018334Speter if (! publicp) 621151412Sobrien { 621251412Sobrien DECL_INTERFACE_KNOWN (decl) = 1; 621351412Sobrien DECL_NOT_REALLY_EXTERN (decl) = 1; 621451412Sobrien } 621518334Speter 621690287Sobrien /* If the declaration was declared inline, mark it as such. */ 621718334Speter if (inlinep) 621890287Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 621990287Sobrien /* We inline functions that are explicitly declared inline, or, when 622090287Sobrien the user explicitly asks us to, all functions. */ 6221132747Skan if (DECL_DECLARED_INLINE_P (decl) 6222132747Skan || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag)) 622390287Sobrien DECL_INLINE (decl) = 1; 622418334Speter 622518334Speter DECL_EXTERNAL (decl) = 1; 6226169699Skan if (quals && TREE_CODE (type) == FUNCTION_TYPE) 622718334Speter { 6228169699Skan error ("%smember function %qD cannot have cv-qualifier", 6229169699Skan (ctype ? "static " : "non-"), decl); 6230169699Skan quals = TYPE_UNQUALIFIED; 623118334Speter } 623218334Speter 6233169699Skan if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)) 6234169699Skan && !grok_op_properties (decl, /*complain=*/true)) 6235169699Skan return NULL_TREE; 623618334Speter 623790287Sobrien if (ctype && decl_function_context (decl)) 623851412Sobrien DECL_NO_STATIC_CHAIN (decl) = 1; 623951412Sobrien 624052290Sobrien if (funcdef_flag) 624152290Sobrien /* Make the init_value nonzero so pushdecl knows this is not 624252290Sobrien tentative. error_mark_node is replaced later with the BLOCK. */ 624352290Sobrien DECL_INITIAL (decl) = error_mark_node; 624452290Sobrien 624590287Sobrien if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) 624690287Sobrien TREE_NOTHROW (decl) = 1; 624790287Sobrien 624818334Speter /* Caller will do the rest of this. */ 624918334Speter if (check < 0) 625018334Speter return decl; 625118334Speter 625290287Sobrien if (ctype != NULL_TREE) 6253169699Skan { 6254169699Skan if (sfk == sfk_constructor) 6255169699Skan DECL_CONSTRUCTOR_P (decl) = 1; 625618334Speter 6257169699Skan grokclassfn (ctype, decl, flags); 6258169699Skan } 6259169699Skan 626090287Sobrien decl = check_explicit_specialization (orig_declarator, decl, 626190287Sobrien template_count, 6262169699Skan 2 * funcdef_flag + 626390287Sobrien 4 * (friendp != 0)); 626490287Sobrien if (decl == error_mark_node) 626590287Sobrien return NULL_TREE; 626651412Sobrien 6267169699Skan if (attrlist) 6268169699Skan { 6269169699Skan cplus_decl_attributes (&decl, *attrlist, 0); 6270169699Skan *attrlist = NULL_TREE; 6271169699Skan } 6272169699Skan 6273169699Skan /* Check main's type after attributes have been applied. */ 6274169699Skan if (ctype == NULL_TREE && DECL_MAIN_P (decl) 6275169699Skan && !same_type_p (TREE_TYPE (TREE_TYPE (decl)), 6276169699Skan integer_type_node)) 6277169699Skan { 6278169699Skan tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl)); 6279169699Skan tree newtype; 6280169699Skan error ("%<::main%> must return %<int%>"); 6281169699Skan newtype = build_function_type (integer_type_node, oldtypeargs); 6282169699Skan TREE_TYPE (decl) = newtype; 6283169699Skan } 6284169699Skan 628590287Sobrien if (ctype != NULL_TREE 628690287Sobrien && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl)) 628790287Sobrien && check) 628890287Sobrien { 628990287Sobrien tree old_decl; 629070639Sobrien 6291132747Skan old_decl = check_classfn (ctype, decl, 6292169699Skan (processing_template_decl 6293169699Skan > template_class_depth (ctype)) 6294169699Skan ? current_template_parms 6295169699Skan : NULL_TREE); 629690287Sobrien if (old_decl) 629790287Sobrien { 6298132747Skan tree ok; 6299169699Skan tree pushed_scope; 6300132747Skan 6301169699Skan if (TREE_CODE (old_decl) == TEMPLATE_DECL) 6302169699Skan /* Because grokfndecl is always supposed to return a 6303169699Skan FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT 6304169699Skan here. We depend on our callers to figure out that its 6305169699Skan really a template that's being returned. */ 6306169699Skan old_decl = DECL_TEMPLATE_RESULT (old_decl); 6307169699Skan 6308169699Skan if (DECL_STATIC_FUNCTION_P (old_decl) 6309169699Skan && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 6310169699Skan /* Remove the `this' parm added by grokclassfn. 6311169699Skan XXX Isn't this done in start_function, too? */ 6312169699Skan revert_static_member_fn (decl); 6313169699Skan if (DECL_ARTIFICIAL (old_decl)) 6314169699Skan error ("definition of implicitly-declared %qD", old_decl); 6315169699Skan 631690287Sobrien /* Since we've smashed OLD_DECL to its 631790287Sobrien DECL_TEMPLATE_RESULT, we must do the same to DECL. */ 631890287Sobrien if (TREE_CODE (decl) == TEMPLATE_DECL) 631990287Sobrien decl = DECL_TEMPLATE_RESULT (decl); 632051412Sobrien 632190287Sobrien /* Attempt to merge the declarations. This can fail, in 6322117410Skan the case of some invalid specialization declarations. */ 6323169699Skan pushed_scope = push_scope (ctype); 6324169699Skan ok = duplicate_decls (decl, old_decl, friendp); 6325169699Skan if (pushed_scope) 6326169699Skan pop_scope (pushed_scope); 6327132747Skan if (!ok) 6328132747Skan { 6329169699Skan error ("no %q#D member function declared in class %qT", 6330132747Skan decl, ctype); 6331132747Skan return NULL_TREE; 6332132747Skan } 633390287Sobrien return old_decl; 633451412Sobrien } 633518334Speter } 633618334Speter 633790287Sobrien if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) 633890287Sobrien return NULL_TREE; 633918334Speter 634090287Sobrien if (ctype == NULL_TREE || check) 634190287Sobrien return decl; 634252290Sobrien 634390287Sobrien if (virtualp) 634490287Sobrien DECL_VIRTUAL_P (decl) = 1; 634518334Speter 634618334Speter return decl; 634718334Speter} 634818334Speter 6349169699Skan/* DECL is a VAR_DECL for a static data member. Set flags to reflect 6350169699Skan the linkage that DECL will receive in the object file. */ 6351117410Skan 6352169699Skanstatic void 6353169699Skanset_linkage_for_static_data_member (tree decl) 6354169699Skan{ 6355169699Skan /* A static data member always has static storage duration and 6356169699Skan external linkage. Note that static data members are forbidden in 6357169699Skan local classes -- the only situation in which a class has 6358169699Skan non-external linkage. */ 6359169699Skan TREE_PUBLIC (decl) = 1; 6360169699Skan TREE_STATIC (decl) = 1; 6361169699Skan /* For non-template classes, static data members are always put 6362169699Skan out in exactly those files where they are defined, just as 6363169699Skan with ordinary namespace-scope variables. */ 6364169699Skan if (!processing_template_decl) 6365169699Skan DECL_INTERFACE_KNOWN (decl) = 1; 6366169699Skan} 6367169699Skan 6368169699Skan/* Create a VAR_DECL named NAME with the indicated TYPE. 6369169699Skan 6370117410Skan If SCOPE is non-NULL, it is the class type or namespace containing 6371117410Skan the variable. If SCOPE is NULL, the variable should is created in 6372117410Skan the innermost enclosings scope. */ 6373117410Skan 637418334Speterstatic tree 6375132747Skangrokvardecl (tree type, 6376169699Skan tree name, 6377169699Skan const cp_decl_specifier_seq *declspecs, 6378169699Skan int initialized, 6379169699Skan int constp, 6380169699Skan tree scope) 638118334Speter{ 638218334Speter tree decl; 6383161660Skan tree explicit_scope; 638418334Speter 6385169699Skan gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE); 6386117410Skan 6387161660Skan /* Compute the scope in which to place the variable, but remember 6388169699Skan whether or not that scope was explicitly specified by the user. */ 6389161660Skan explicit_scope = scope; 6390117410Skan if (!scope) 639118334Speter { 6392117410Skan /* An explicit "extern" specifier indicates a namespace-scope 6393117410Skan variable. */ 6394169699Skan if (declspecs->storage_class == sc_extern) 6395117410Skan scope = current_namespace; 6396117410Skan else if (!at_function_scope_p ()) 6397169699Skan scope = current_scope (); 639818334Speter } 6399117410Skan 6400117410Skan if (scope 6401117410Skan && (/* If the variable is a namespace-scope variable declared in a 6402117410Skan template, we need DECL_LANG_SPECIFIC. */ 6403117410Skan (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl) 6404117410Skan /* Similarly for namespace-scope variables with language linkage 6405117410Skan other than C++. */ 6406169699Skan || (TREE_CODE (scope) == NAMESPACE_DECL 6407117410Skan && current_lang_name != lang_name_cplusplus) 6408117410Skan /* Similarly for static data members. */ 6409117410Skan || TYPE_P (scope))) 6410117410Skan decl = build_lang_decl (VAR_DECL, name, type); 641118334Speter else 6412117410Skan decl = build_decl (VAR_DECL, name, type); 641352290Sobrien 6414161660Skan if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL) 6415161660Skan set_decl_namespace (decl, explicit_scope, 0); 6416117410Skan else 6417169699Skan DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 641852290Sobrien 6419169699Skan if (declspecs->storage_class == sc_extern) 642018334Speter { 642118334Speter DECL_THIS_EXTERN (decl) = 1; 642218334Speter DECL_EXTERNAL (decl) = !initialized; 642318334Speter } 642418334Speter 642551412Sobrien if (DECL_CLASS_SCOPE_P (decl)) 642618334Speter { 6427169699Skan set_linkage_for_static_data_member (decl); 6428169699Skan /* This function is only called with out-of-class definitions. */ 642918334Speter DECL_EXTERNAL (decl) = 0; 6430169699Skan check_class_member_definition_namespace (decl); 643118334Speter } 643218334Speter /* At top level, either `static' or no s.c. makes a definition 643318334Speter (perhaps tentative), and absence of `static' makes it public. */ 643418334Speter else if (toplevel_bindings_p ()) 643518334Speter { 6436169699Skan TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static 643718334Speter && (DECL_THIS_EXTERN (decl) || ! constp)); 643818334Speter TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); 643918334Speter } 644018334Speter /* Not at top level, only `static' makes a static definition. */ 644118334Speter else 644218334Speter { 6443169699Skan TREE_STATIC (decl) = declspecs->storage_class == sc_static; 644418334Speter TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); 644518334Speter } 644652290Sobrien 6447169699Skan if (declspecs->specs[(int)ds_thread]) 6448117410Skan { 6449117410Skan if (targetm.have_tls) 6450169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 6451117410Skan else 6452117410Skan /* A mere warning is sure to result in improper semantics 6453117410Skan at runtime. Don't bother to allow this to compile. */ 6454117410Skan error ("thread-local storage not supported for this target"); 6455117410Skan } 6456117410Skan 645752290Sobrien if (TREE_PUBLIC (decl)) 645852290Sobrien { 645952290Sobrien /* [basic.link]: A name with no linkage (notably, the name of a class 646052290Sobrien or enumeration declared in a local scope) shall not be used to 646152290Sobrien declare an entity with linkage. 646252290Sobrien 646352290Sobrien Only check this for public decls for now. */ 6464169699Skan tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false); 646552290Sobrien if (t) 646652290Sobrien { 646790287Sobrien if (TYPE_ANONYMOUS_P (t)) 6468169699Skan { 6469169699Skan if (DECL_EXTERN_C_P (decl)) 6470169699Skan /* Allow this; it's pretty common in C. */ 6471169699Skan ; 6472169699Skan else 6473169699Skan { 6474169699Skan /* DRs 132, 319 and 389 seem to indicate types with 6475169699Skan no linkage can only be used to declare extern "C" 6476169699Skan entities. Since it's not always an error in the 6477169699Skan ISO C++ 90 Standard, we only issue a warning. */ 6478169699Skan warning (0, "non-local variable %q#D uses anonymous type", 6479169699Skan decl); 6480169699Skan if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) 6481169699Skan warning (0, "%q+#D does not refer to the unqualified " 6482169699Skan "type, so it is not used for linkage", 6483169699Skan TYPE_NAME (t)); 6484169699Skan } 6485169699Skan } 648652290Sobrien else 6487169699Skan warning (0, "non-local variable %q#D uses local type %qT", decl, t); 648852290Sobrien } 648952290Sobrien } 6490169699Skan else 6491169699Skan DECL_INTERFACE_KNOWN (decl) = 1; 649252290Sobrien 649318334Speter return decl; 649418334Speter} 649518334Speter 649652290Sobrien/* Create and return a canonical pointer to member function type, for 649752290Sobrien TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ 649818334Speter 649918334Spetertree 6500117410Skanbuild_ptrmemfunc_type (tree type) 650118334Speter{ 6502132747Skan tree field, fields; 650318334Speter tree t; 650490287Sobrien tree unqualified_variant = NULL_TREE; 650518334Speter 650690287Sobrien if (type == error_mark_node) 650790287Sobrien return type; 650890287Sobrien 650918334Speter /* If a canonical type already exists for this type, use it. We use 651018334Speter this method instead of type_hash_canon, because it only does a 651118334Speter simple equality check on the list of field members. */ 651218334Speter 651318334Speter if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type))) 651418334Speter return t; 651518334Speter 651690287Sobrien /* Make sure that we always have the unqualified pointer-to-member 651790287Sobrien type first. */ 651890287Sobrien if (cp_type_quals (type) != TYPE_UNQUALIFIED) 651990287Sobrien unqualified_variant 652090287Sobrien = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); 652118334Speter 652290287Sobrien t = make_aggr_type (RECORD_TYPE); 6523169699Skan xref_basetypes (t, NULL_TREE); 6524169699Skan 652551412Sobrien /* Let the front-end know this is a pointer to member function... */ 652618334Speter TYPE_PTRMEMFUNC_FLAG (t) = 1; 652751412Sobrien /* ... and not really an aggregate. */ 652852290Sobrien SET_IS_AGGR_TYPE (t, 0); 652918334Speter 6530132747Skan field = build_decl (FIELD_DECL, pfn_identifier, type); 6531132747Skan fields = field; 6532169699Skan 6533132747Skan field = build_decl (FIELD_DECL, delta_identifier, delta_type_node); 6534132747Skan TREE_CHAIN (field) = fields; 6535132747Skan fields = field; 6536169699Skan 6537132747Skan finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node); 653818334Speter 653918334Speter /* Zap out the name so that the back-end will give us the debugging 654018334Speter information for this anonymous RECORD_TYPE. */ 654118334Speter TYPE_NAME (t) = NULL_TREE; 654218334Speter 654390287Sobrien /* If this is not the unqualified form of this pointer-to-member 654490287Sobrien type, set the TYPE_MAIN_VARIANT for this type to be the 654590287Sobrien unqualified type. Since they are actually RECORD_TYPEs that are 654690287Sobrien not variants of each other, we must do this manually. */ 654790287Sobrien if (cp_type_quals (type) != TYPE_UNQUALIFIED) 654890287Sobrien { 654990287Sobrien t = build_qualified_type (t, cp_type_quals (type)); 655090287Sobrien TYPE_MAIN_VARIANT (t) = unqualified_variant; 655190287Sobrien TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant); 655290287Sobrien TYPE_NEXT_VARIANT (unqualified_variant) = t; 655390287Sobrien } 655490287Sobrien 655590287Sobrien /* Cache this pointer-to-member type so that we can find it again 655690287Sobrien later. */ 655718334Speter TYPE_SET_PTRMEMFUNC_TYPE (type, t); 655818334Speter 655918334Speter return t; 656018334Speter} 656118334Speter 6562117410Skan/* Create and return a pointer to data member type. */ 6563117410Skan 6564117410Skantree 6565117410Skanbuild_ptrmem_type (tree class_type, tree member_type) 6566117410Skan{ 6567132747Skan if (TREE_CODE (member_type) == METHOD_TYPE) 6568132747Skan { 6569132747Skan tree arg_types; 6570132747Skan 6571132747Skan arg_types = TYPE_ARG_TYPES (member_type); 6572169699Skan class_type = (cp_build_qualified_type 6573132747Skan (class_type, 6574132747Skan cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types))))); 6575169699Skan member_type 6576169699Skan = build_method_type_directly (class_type, 6577132747Skan TREE_TYPE (member_type), 6578132747Skan TREE_CHAIN (arg_types)); 6579132747Skan return build_ptrmemfunc_type (build_pointer_type (member_type)); 6580132747Skan } 6581132747Skan else 6582132747Skan { 6583169699Skan gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE); 6584132747Skan return build_offset_type (class_type, member_type); 6585132747Skan } 6586117410Skan} 6587117410Skan 658852290Sobrien/* DECL is a VAR_DECL defined in-class, whose TYPE is also given. 658952290Sobrien Check to see that the definition is valid. Issue appropriate error 659052290Sobrien messages. Return 1 if the definition is particularly bad, or 0 659152290Sobrien otherwise. */ 659252290Sobrien 659352290Sobrienint 6594132747Skancheck_static_variable_definition (tree decl, tree type) 659552290Sobrien{ 659652290Sobrien /* Motion 10 at San Diego: If a static const integral data member is 659752290Sobrien initialized with an integral constant expression, the initializer 659852290Sobrien may appear either in the declaration (within the class), or in 659952290Sobrien the definition, but not both. If it appears in the class, the 660052290Sobrien member is a member constant. The file-scope definition is always 660152290Sobrien required. */ 6602110631Skan if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE) 660352290Sobrien { 6604169699Skan error ("invalid in-class initialization of static data member " 6605169699Skan "of non-integral type %qT", 6606110631Skan type); 660752290Sobrien /* If we just return the declaration, crashes will sometimes 6608169699Skan occur. We therefore return void_type_node, as if this were a 660952290Sobrien friend declaration, to cause callers to completely ignore 661052290Sobrien this declaration. */ 661152290Sobrien return 1; 661252290Sobrien } 661352290Sobrien else if (!CP_TYPE_CONST_P (type)) 6614169699Skan error ("ISO C++ forbids in-class initialization of non-const " 6615169699Skan "static member %qD", 6616169699Skan decl); 661752290Sobrien else if (pedantic && !INTEGRAL_TYPE_P (type)) 6618169699Skan pedwarn ("ISO C++ forbids initialization of member constant " 6619169699Skan "%qD of non-integral type %qT", decl, type); 662052290Sobrien 662152290Sobrien return 0; 662252290Sobrien} 662352290Sobrien 662490287Sobrien/* Given the SIZE (i.e., number of elements) in an array, compute an 662590287Sobrien appropriate index type for the array. If non-NULL, NAME is the 662690287Sobrien name of the thing being declared. */ 662790287Sobrien 662890287Sobrientree 6629132747Skancompute_array_index_type (tree name, tree size) 663090287Sobrien{ 6631169699Skan tree type; 663290287Sobrien tree itype; 663390287Sobrien 6634169699Skan if (error_operand_p (size)) 6635169699Skan return error_mark_node; 6636169699Skan 6637169699Skan type = TREE_TYPE (size); 6638132747Skan /* The array bound must be an integer type. */ 6639132747Skan if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type)) 664090287Sobrien { 6641132747Skan if (name) 6642169699Skan error ("size of array %qD has non-integral type %qT", name, type); 6643132747Skan else 6644169699Skan error ("size of array has non-integral type %qT", type); 6645132747Skan size = integer_one_node; 6646132747Skan type = TREE_TYPE (size); 664790287Sobrien } 664890287Sobrien 6649132747Skan if (abi_version_at_least (2) 6650132747Skan /* We should only handle value dependent expressions specially. */ 6651132747Skan ? value_dependent_expression_p (size) 6652132747Skan /* But for abi-1, we handled all instances in templates. This 6653132747Skan effects the manglings produced. */ 6654132747Skan : processing_template_decl) 6655132747Skan return build_index_type (build_min (MINUS_EXPR, sizetype, 6656132747Skan size, integer_one_node)); 6657132747Skan 6658117410Skan /* The size might be the result of a cast. */ 665990287Sobrien STRIP_TYPE_NOPS (size); 666090287Sobrien 666190287Sobrien /* It might be a const variable or enumeration constant. */ 6662169699Skan size = integral_constant_value (size); 666390287Sobrien 666490287Sobrien /* Normally, the array-bound will be a constant. */ 666590287Sobrien if (TREE_CODE (size) == INTEGER_CST) 666690287Sobrien { 666790287Sobrien /* Check to see if the array bound overflowed. Make that an 666890287Sobrien error, no matter how generous we're being. */ 666990287Sobrien int old_flag_pedantic_errors = flag_pedantic_errors; 667090287Sobrien int old_pedantic = pedantic; 667190287Sobrien pedantic = flag_pedantic_errors = 1; 667290287Sobrien constant_expression_warning (size); 667390287Sobrien pedantic = old_pedantic; 667490287Sobrien flag_pedantic_errors = old_flag_pedantic_errors; 667590287Sobrien 667690287Sobrien /* An array must have a positive number of elements. */ 667790287Sobrien if (INT_CST_LT (size, integer_zero_node)) 667890287Sobrien { 667990287Sobrien if (name) 6680169699Skan error ("size of array %qD is negative", name); 668190287Sobrien else 668290287Sobrien error ("size of array is negative"); 668390287Sobrien size = integer_one_node; 668490287Sobrien } 6685132747Skan /* As an extension we allow zero-sized arrays. We always allow 6686132747Skan them in system headers because glibc uses them. */ 668790287Sobrien else if (integer_zerop (size) && pedantic && !in_system_header) 668890287Sobrien { 668990287Sobrien if (name) 6690169699Skan pedwarn ("ISO C++ forbids zero-size array %qD", name); 669190287Sobrien else 669290287Sobrien pedwarn ("ISO C++ forbids zero-size array"); 669390287Sobrien } 669490287Sobrien } 669590287Sobrien else if (TREE_CONSTANT (size)) 669690287Sobrien { 669790287Sobrien /* `(int) &fn' is not a valid array bound. */ 669890287Sobrien if (name) 6699169699Skan error ("size of array %qD is not an integral constant-expression", 6700169699Skan name); 670190287Sobrien else 670290287Sobrien error ("size of array is not an integral constant-expression"); 6703169699Skan size = integer_one_node; 670490287Sobrien } 6705132747Skan else if (pedantic) 6706132747Skan { 6707132747Skan if (name) 6708169699Skan pedwarn ("ISO C++ forbids variable-size array %qD", name); 6709132747Skan else 6710132747Skan pedwarn ("ISO C++ forbids variable-size array"); 6711132747Skan } 671290287Sobrien 6713132747Skan if (processing_template_decl && !TREE_CONSTANT (size)) 6714132747Skan /* A variable sized array. */ 6715132747Skan itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node); 6716132747Skan else 671790287Sobrien { 6718169699Skan HOST_WIDE_INT saved_processing_template_decl; 6719169699Skan 6720132747Skan /* Compute the index of the largest element in the array. It is 6721169699Skan one less than the number of elements in the array. We save 6722169699Skan and restore PROCESSING_TEMPLATE_DECL so that computations in 6723169699Skan cp_build_binary_op will be appropriately folded. */ 6724169699Skan saved_processing_template_decl = processing_template_decl; 6725169699Skan processing_template_decl = 0; 6726169699Skan itype = cp_build_binary_op (MINUS_EXPR, 6727169699Skan cp_convert (ssizetype, size), 6728169699Skan cp_convert (ssizetype, integer_one_node)); 6729169699Skan itype = fold (itype); 6730169699Skan processing_template_decl = saved_processing_template_decl; 6731169699Skan 6732132747Skan if (!TREE_CONSTANT (itype)) 6733132747Skan /* A variable sized array. */ 6734132747Skan itype = variable_size (itype); 6735132747Skan /* Make sure that there was no overflow when creating to a signed 6736169699Skan index type. (For example, on a 32-bit machine, an array with 6737169699Skan size 2^32 - 1 is too big.) */ 6738169699Skan else if (TREE_CODE (itype) == INTEGER_CST 6739169699Skan && TREE_OVERFLOW (itype)) 674090287Sobrien { 6741132747Skan error ("overflow in array dimension"); 6742132747Skan TREE_OVERFLOW (itype) = 0; 674390287Sobrien } 674490287Sobrien } 674590287Sobrien 674690287Sobrien /* Create and return the appropriate index type. */ 674790287Sobrien return build_index_type (itype); 674890287Sobrien} 674990287Sobrien 6750132747Skan/* Returns the scope (if any) in which the entity declared by 6751132747Skan DECLARATOR will be located. If the entity was declared with an 6752132747Skan unqualified name, NULL_TREE is returned. */ 6753132747Skan 6754132747Skantree 6755169699Skanget_scope_of_declarator (const cp_declarator *declarator) 6756132747Skan{ 6757169699Skan while (declarator && declarator->kind != cdk_id) 6758169699Skan declarator = declarator->declarator; 6759132747Skan 6760169699Skan /* If the declarator-id is a SCOPE_REF, the scope in which the 6761169699Skan declaration occurs is the first operand. */ 6762169699Skan if (declarator 6763169699Skan && declarator->u.id.qualifying_scope) 6764169699Skan return declarator->u.id.qualifying_scope; 6765132747Skan 6766169699Skan /* Otherwise, the declarator is not a qualified name; the entity will 6767169699Skan be declared in the current scope. */ 6768169699Skan return NULL_TREE; 6769132747Skan} 6770132747Skan 677190287Sobrien/* Returns an ARRAY_TYPE for an array with SIZE elements of the 677290287Sobrien indicated TYPE. If non-NULL, NAME is the NAME of the declaration 677390287Sobrien with this type. */ 677490287Sobrien 677590287Sobrienstatic tree 6776132747Skancreate_array_type_for_decl (tree name, tree type, tree size) 677790287Sobrien{ 677890287Sobrien tree itype = NULL_TREE; 677990287Sobrien const char* error_msg; 678090287Sobrien 678190287Sobrien /* If things have already gone awry, bail now. */ 678290287Sobrien if (type == error_mark_node || size == error_mark_node) 678390287Sobrien return error_mark_node; 678490287Sobrien 678590287Sobrien /* Assume that everything will go OK. */ 678690287Sobrien error_msg = NULL; 678790287Sobrien 678890287Sobrien /* There are some types which cannot be array elements. */ 678990287Sobrien switch (TREE_CODE (type)) 679090287Sobrien { 679190287Sobrien case VOID_TYPE: 679290287Sobrien error_msg = "array of void"; 679390287Sobrien break; 679490287Sobrien 679590287Sobrien case FUNCTION_TYPE: 679690287Sobrien error_msg = "array of functions"; 679790287Sobrien break; 679890287Sobrien 679990287Sobrien case REFERENCE_TYPE: 680090287Sobrien error_msg = "array of references"; 680190287Sobrien break; 680290287Sobrien 680390287Sobrien case METHOD_TYPE: 680490287Sobrien error_msg = "array of function members"; 680590287Sobrien break; 680690287Sobrien 680790287Sobrien default: 680890287Sobrien break; 680990287Sobrien } 681090287Sobrien 681190287Sobrien /* If something went wrong, issue an error-message and return. */ 681290287Sobrien if (error_msg) 681390287Sobrien { 681490287Sobrien if (name) 6815169699Skan error ("declaration of %qD as %s", name, error_msg); 681690287Sobrien else 681790287Sobrien error ("creating %s", error_msg); 681890287Sobrien 681990287Sobrien return error_mark_node; 682090287Sobrien } 682190287Sobrien 682290287Sobrien /* [dcl.array] 682390287Sobrien 682490287Sobrien The constant expressions that specify the bounds of the arrays 682590287Sobrien can be omitted only for the first member of the sequence. */ 682690287Sobrien if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) 682790287Sobrien { 682890287Sobrien if (name) 6829169699Skan error ("declaration of %qD as multidimensional array must " 6830169699Skan "have bounds for all dimensions except the first", 6831169699Skan name); 683290287Sobrien else 6833169699Skan error ("multidimensional array must have bounds for all " 6834169699Skan "dimensions except the first"); 683590287Sobrien 683690287Sobrien return error_mark_node; 683790287Sobrien } 683890287Sobrien 683990287Sobrien /* Figure out the index type for the array. */ 684090287Sobrien if (size) 684190287Sobrien itype = compute_array_index_type (name, size); 684290287Sobrien 6843169699Skan /* [dcl.array] 6844169699Skan T is called the array element type; this type shall not be [...] an 6845169699Skan abstract class type. */ 6846169699Skan abstract_virtuals_error (name, type); 6847169699Skan 684890287Sobrien return build_cplus_array_type (type, itype); 684990287Sobrien} 685090287Sobrien 685190287Sobrien/* Check that it's OK to declare a function with the indicated TYPE. 685290287Sobrien SFK indicates the kind of special function (if any) that this 685390287Sobrien function is. OPTYPE is the type given in a conversion operator 6854169699Skan declaration, or the class type for a constructor/destructor. 6855169699Skan Returns the actual return type of the function; that 685690287Sobrien may be different than TYPE if an error occurs, or for certain 685790287Sobrien special functions. */ 685890287Sobrien 685990287Sobrienstatic tree 6860132747Skancheck_special_function_return_type (special_function_kind sfk, 6861169699Skan tree type, 6862169699Skan tree optype) 686390287Sobrien{ 686490287Sobrien switch (sfk) 686590287Sobrien { 686690287Sobrien case sfk_constructor: 686790287Sobrien if (type) 686890287Sobrien error ("return type specification for constructor invalid"); 686990287Sobrien 6870169699Skan if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype)) 6871169699Skan type = build_pointer_type (optype); 6872169699Skan else 6873169699Skan type = void_type_node; 687490287Sobrien break; 687590287Sobrien 687690287Sobrien case sfk_destructor: 687790287Sobrien if (type) 687890287Sobrien error ("return type specification for destructor invalid"); 6879169699Skan /* We can't use the proper return type here because we run into 6880169699Skan problems with ambiguous bases and covariant returns. 6881169699Skan Java classes are left unchanged because (void *) isn't a valid 6882169699Skan Java type, and we don't want to change the Java ABI. */ 6883169699Skan if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype)) 6884169699Skan type = build_pointer_type (void_type_node); 6885169699Skan else 6886169699Skan type = void_type_node; 688790287Sobrien break; 688890287Sobrien 688990287Sobrien case sfk_conversion: 689090287Sobrien if (type && !same_type_p (type, optype)) 6891169699Skan error ("operator %qT declared to return %qT", optype, type); 689290287Sobrien else if (type) 6893169699Skan pedwarn ("return type specified for %<operator %T%>", optype); 689490287Sobrien type = optype; 689590287Sobrien break; 689690287Sobrien 689790287Sobrien default: 6898169699Skan gcc_unreachable (); 689990287Sobrien } 690090287Sobrien 690190287Sobrien return type; 690290287Sobrien} 690390287Sobrien 6904146906Skan/* A variable or data member (whose unqualified name is IDENTIFIER) 6905146906Skan has been declared with the indicated TYPE. If the TYPE is not 6906146906Skan acceptable, issue an error message and return a type to use for 6907169699Skan error-recovery purposes. */ 6908146906Skan 6909146906Skantree 6910146906Skancheck_var_type (tree identifier, tree type) 6911146906Skan{ 6912146906Skan if (VOID_TYPE_P (type)) 6913146906Skan { 6914146906Skan if (!identifier) 6915146906Skan error ("unnamed variable or field declared void"); 6916146906Skan else if (TREE_CODE (identifier) == IDENTIFIER_NODE) 6917146906Skan { 6918169699Skan gcc_assert (!IDENTIFIER_OPNAME_P (identifier)); 6919169699Skan error ("variable or field %qE declared void", identifier); 6920146906Skan } 6921146906Skan else 6922146906Skan error ("variable or field declared void"); 6923169699Skan type = error_mark_node; 6924146906Skan } 6925169699Skan 6926146906Skan return type; 6927146906Skan} 6928146906Skan 6929132747Skan/* Given declspecs and a declarator (abstract or otherwise), determine 6930132747Skan the name and type of the object declared and construct a DECL node 6931132747Skan for it. 693218334Speter 693318334Speter DECLSPECS is a chain of tree_list nodes whose value fields 693418334Speter are the storage classes and type specifiers. 693518334Speter 693618334Speter DECL_CONTEXT says which syntactic context this declaration is in: 693718334Speter NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 693818334Speter FUNCDEF for a function definition. Like NORMAL but a few different 693918334Speter error messages in each case. Return value may be zero meaning 694018334Speter this definition is too screwy to try to parse. 694118334Speter MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to 694218334Speter handle member functions (which have FIELD context). 694318334Speter Return value may be zero meaning this definition is too screwy to 694418334Speter try to parse. 694518334Speter PARM for a parameter declaration (either within a function prototype 694618334Speter or before a function body). Make a PARM_DECL, or return void_type_node. 694718334Speter CATCHPARM for a parameter declaration before a catch clause. 694818334Speter TYPENAME if for a typename (in a cast or sizeof). 694918334Speter Don't make a DECL node; just return the ..._TYPE node. 695018334Speter FIELD for a struct or union field; make a FIELD_DECL. 695118334Speter BITFIELD for a field with specified width. 695218334Speter INITIALIZED is 1 if the decl has an initializer. 695318334Speter 695490287Sobrien ATTRLIST is a pointer to the list of attributes, which may be NULL 695590287Sobrien if there are none; *ATTRLIST may be modified if attributes from inside 695690287Sobrien the declarator should be applied to the declaration. 695752290Sobrien 6958132747Skan When this function is called, scoping variables (such as 6959132747Skan CURRENT_CLASS_TYPE) should reflect the scope in which the 6960132747Skan declaration occurs, not the scope in which the new declaration will 6961132747Skan be placed. For example, on: 696218334Speter 6963132747Skan void S::f() { ... } 696418334Speter 6965132747Skan when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE 6966169699Skan should not be `S'. 696718334Speter 6968169699Skan Returns a DECL (if a declarator is present), a TYPE (if there is no 6969169699Skan declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an 6970169699Skan error occurs. */ 6971169699Skan 697218334Spetertree 6973169699Skangrokdeclarator (const cp_declarator *declarator, 6974169699Skan const cp_decl_specifier_seq *declspecs, 6975169699Skan enum decl_context decl_context, 6976169699Skan int initialized, 6977169699Skan tree* attrlist) 697818334Speter{ 697918334Speter tree type = NULL_TREE; 698018334Speter int longlong = 0; 698118334Speter int virtualp, explicitp, friendp, inlinep, staticp; 698218334Speter int explicit_int = 0; 698318334Speter int explicit_char = 0; 698451412Sobrien int defaulted_int = 0; 6985169699Skan tree dependent_name = NULL_TREE; 6986169699Skan 698718334Speter tree typedef_decl = NULL_TREE; 6988169699Skan const char *name = NULL; 698918334Speter tree typedef_type = NULL_TREE; 6990169699Skan /* True if this declarator is a function definition. */ 6991169699Skan bool funcdef_flag = false; 6992169699Skan cp_declarator_kind innermost_code = cdk_error; 699318334Speter int bitfield = 0; 699451412Sobrien#if 0 699551412Sobrien /* See the code below that used this. */ 699690287Sobrien tree decl_attr = NULL_TREE; 699751412Sobrien#endif 699818334Speter 699918334Speter /* Keep track of what sort of function is being processed 700018334Speter so that we can warn about default return values, or explicit 700118334Speter return values which do not match prescribed defaults. */ 700290287Sobrien special_function_kind sfk = sfk_none; 700318334Speter 700418334Speter tree dname = NULL_TREE; 700518334Speter tree ctor_return_type = NULL_TREE; 700618334Speter enum overload_flags flags = NO_SPECIAL; 7007169699Skan /* cv-qualifiers that apply to the declarator, for a declaration of 7008169699Skan a member function. */ 7009169699Skan cp_cv_quals memfn_quals = TYPE_UNQUALIFIED; 7010169699Skan /* cv-qualifiers that apply to the type specified by the DECLSPECS. */ 7011169699Skan int type_quals; 701251412Sobrien tree raises = NULL_TREE; 701351412Sobrien int template_count = 0; 701490287Sobrien tree returned_attrs = NULL_TREE; 7015132747Skan tree parms = NULL_TREE; 7016169699Skan const cp_declarator *id_declarator; 7017169699Skan /* The unqualified name of the declarator; either an 7018169699Skan IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */ 7019169699Skan tree unqualified_id; 7020169699Skan /* The class type, if any, in which this entity is located, 7021169699Skan or NULL_TREE if none. Note that this value may be different from 7022169699Skan the current class type; for example if an attempt is made to declare 7023169699Skan "A::f" inside "B", this value will be "A". */ 7024169699Skan tree ctype = current_class_type; 7025169699Skan /* The NAMESPACE_DECL for the namespace in which this entity is 7026169699Skan located. If an unqualified name is used to declare the entity, 7027169699Skan this value will be NULL_TREE, even if the entity is located at 7028169699Skan namespace scope. */ 7029169699Skan tree in_namespace = NULL_TREE; 7030169699Skan cp_storage_class storage_class; 7031169699Skan bool unsigned_p, signed_p, short_p, long_p, thread_p; 7032169699Skan bool type_was_error_mark_node = false; 703318334Speter 7034169699Skan signed_p = declspecs->specs[(int)ds_signed]; 7035169699Skan unsigned_p = declspecs->specs[(int)ds_unsigned]; 7036169699Skan short_p = declspecs->specs[(int)ds_short]; 7037169699Skan long_p = declspecs->specs[(int)ds_long]; 7038169699Skan longlong = declspecs->specs[(int)ds_long] >= 2; 7039169699Skan thread_p = declspecs->specs[(int)ds_thread]; 7040169699Skan 704118334Speter if (decl_context == FUNCDEF) 7042169699Skan funcdef_flag = true, decl_context = NORMAL; 704318334Speter else if (decl_context == MEMFUNCDEF) 7044169699Skan funcdef_flag = true, decl_context = FIELD; 704518334Speter else if (decl_context == BITFIELD) 704618334Speter bitfield = 1, decl_context = FIELD; 704718334Speter 704818334Speter /* Look inside a declarator for the name being declared 704918334Speter and get it as a string, for an error message. */ 7050169699Skan for (id_declarator = declarator; 7051169699Skan id_declarator; 7052169699Skan id_declarator = id_declarator->declarator) 7053169699Skan { 7054169699Skan if (id_declarator->kind != cdk_id) 7055169699Skan innermost_code = id_declarator->kind; 705618334Speter 7057169699Skan switch (id_declarator->kind) 7058169699Skan { 7059169699Skan case cdk_function: 7060169699Skan if (id_declarator->declarator 7061169699Skan && id_declarator->declarator->kind == cdk_id) 706218334Speter { 7063169699Skan sfk = id_declarator->declarator->u.id.sfk; 7064169699Skan if (sfk == sfk_destructor) 7065169699Skan flags = DTOR_FLAG; 706618334Speter } 7067169699Skan break; 706818334Speter 7069169699Skan case cdk_id: 7070169699Skan { 7071169699Skan tree qualifying_scope = id_declarator->u.id.qualifying_scope; 7072169699Skan tree decl = id_declarator->u.id.unqualified_name; 7073169699Skan if (!decl) 7074169699Skan break; 7075169699Skan if (qualifying_scope) 707618334Speter { 7077169699Skan if (at_function_scope_p ()) 7078169699Skan { 7079169699Skan /* [dcl.meaning] 708090287Sobrien 7081169699Skan A declarator-id shall not be qualified except 7082169699Skan for ... 708351412Sobrien 7084169699Skan None of the cases are permitted in block 7085169699Skan scope. */ 7086169699Skan if (qualifying_scope == global_namespace) 7087169699Skan error ("invalid use of qualified-name %<::%D%>", 7088169699Skan decl); 7089169699Skan else if (TYPE_P (qualifying_scope)) 7090169699Skan error ("invalid use of qualified-name %<%T::%D%>", 7091169699Skan qualifying_scope, decl); 7092169699Skan else 7093169699Skan error ("invalid use of qualified-name %<%D::%D%>", 7094169699Skan qualifying_scope, decl); 7095169699Skan return error_mark_node; 7096169699Skan } 7097169699Skan else if (TYPE_P (qualifying_scope)) 709852290Sobrien { 7099169699Skan ctype = qualifying_scope; 7100169699Skan if (innermost_code != cdk_function 7101169699Skan && current_class_type 7102169699Skan && !UNIQUELY_DERIVED_FROM_P (ctype, 7103169699Skan current_class_type)) 7104169699Skan { 7105169699Skan error ("type %qT is not derived from type %qT", 7106169699Skan ctype, current_class_type); 7107169699Skan return error_mark_node; 7108169699Skan } 710952290Sobrien } 7110169699Skan else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL) 7111169699Skan in_namespace = qualifying_scope; 711218334Speter } 7113169699Skan switch (TREE_CODE (decl)) 711418334Speter { 7115169699Skan case BIT_NOT_EXPR: 7116169699Skan { 7117169699Skan tree type; 711818334Speter 7119169699Skan if (innermost_code != cdk_function) 712051412Sobrien { 7121169699Skan error ("declaration of %qD as non-function", decl); 7122169699Skan return error_mark_node; 712351412Sobrien } 7124169699Skan else if (!qualifying_scope 7125169699Skan && !(current_class_type && at_class_scope_p ())) 7126169699Skan { 7127169699Skan error ("declaration of %qD as non-member", decl); 7128169699Skan return error_mark_node; 7129169699Skan } 7130169699Skan 7131169699Skan type = TREE_OPERAND (decl, 0); 7132169699Skan name = IDENTIFIER_POINTER (constructor_name (type)); 7133169699Skan dname = decl; 713451412Sobrien } 7135169699Skan break; 713618334Speter 7137169699Skan case TEMPLATE_ID_EXPR: 713851412Sobrien { 7139169699Skan tree fns = TREE_OPERAND (decl, 0); 7140132747Skan 7141169699Skan dname = fns; 7142169699Skan if (TREE_CODE (dname) != IDENTIFIER_NODE) 714351412Sobrien { 7144169699Skan gcc_assert (is_overloaded_fn (dname)); 7145169699Skan dname = DECL_NAME (get_first_fn (dname)); 714651412Sobrien } 714751412Sobrien } 7148169699Skan /* Fall through. */ 714951412Sobrien 7150169699Skan case IDENTIFIER_NODE: 7151169699Skan if (TREE_CODE (decl) == IDENTIFIER_NODE) 7152169699Skan dname = decl; 715351412Sobrien 7154169699Skan if (C_IS_RESERVED_WORD (dname)) 7155169699Skan { 7156169699Skan error ("declarator-id missing; using reserved word %qD", 7157169699Skan dname); 7158169699Skan name = IDENTIFIER_POINTER (dname); 7159169699Skan } 7160169699Skan else if (!IDENTIFIER_TYPENAME_P (dname)) 7161169699Skan name = IDENTIFIER_POINTER (dname); 7162169699Skan else 7163169699Skan { 7164169699Skan gcc_assert (flags == NO_SPECIAL); 7165169699Skan flags = TYPENAME_FLAG; 7166169699Skan ctor_return_type = TREE_TYPE (dname); 7167169699Skan sfk = sfk_conversion; 7168169699Skan if (is_typename_at_global_scope (dname)) 7169169699Skan name = IDENTIFIER_POINTER (dname); 7170169699Skan else 7171169699Skan name = "<invalid operator>"; 7172169699Skan } 7173169699Skan break; 717451412Sobrien 7175169699Skan default: 7176169699Skan gcc_unreachable (); 7177169699Skan } 717851412Sobrien break; 717951412Sobrien 7180169699Skan case cdk_array: 7181169699Skan case cdk_pointer: 7182169699Skan case cdk_reference: 7183169699Skan case cdk_ptrmem: 7184117410Skan break; 7185117410Skan 7186169699Skan case cdk_error: 7187107601Sobrien return error_mark_node; 7188169699Skan 718951412Sobrien default: 7190169699Skan gcc_unreachable (); 719151412Sobrien } 7192169699Skan } 7193169699Skan if (id_declarator->kind == cdk_id) 7194169699Skan break; 7195169699Skan } 719618334Speter 7197169699Skan /* [dcl.fct.edf] 719818334Speter 7199169699Skan The declarator in a function-definition shall have the form 7200169699Skan D1 ( parameter-declaration-clause) ... */ 7201169699Skan if (funcdef_flag && innermost_code != cdk_function) 7202169699Skan { 7203169699Skan error ("function definition does not declare parameters"); 7204169699Skan return error_mark_node; 7205169699Skan } 720618334Speter 720718334Speter if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG) 7208169699Skan && innermost_code != cdk_function 7209169699Skan && ! (ctype && !declspecs->any_specifiers_p)) 721018334Speter { 7211169699Skan error ("declaration of %qD as non-function", dname); 7212169699Skan return error_mark_node; 721318334Speter } 721418334Speter 721518334Speter /* Anything declared one level down from the top level 721618334Speter must be one of the parameters of a function 721718334Speter (because the body is at least two levels down). */ 721818334Speter 721918334Speter /* This heuristic cannot be applied to C++ nodes! Fixed, however, 722018334Speter by not allowing C++ class definitions to specify their parameters 722118334Speter with xdecls (must be spec.d in the parmlist). 722218334Speter 722318334Speter Since we now wait to push a class scope until we are sure that 722418334Speter we are in a legitimate method context, we must set oldcname 722518334Speter explicitly (since current_class_name is not yet alive). 722618334Speter 722718334Speter We also want to avoid calling this a PARM if it is in a namespace. */ 722818334Speter 722990287Sobrien if (decl_context == NORMAL && !toplevel_bindings_p ()) 723018334Speter { 7231117410Skan struct cp_binding_level *b = current_binding_level; 723218334Speter current_binding_level = b->level_chain; 723318334Speter if (current_binding_level != 0 && toplevel_bindings_p ()) 723418334Speter decl_context = PARM; 723518334Speter current_binding_level = b; 723618334Speter } 723718334Speter 723890287Sobrien if (name == NULL) 723990287Sobrien name = decl_context == PARM ? "parameter" : "type name"; 724090287Sobrien 7241169699Skan /* If there were multiple types specified in the decl-specifier-seq, 7242169699Skan issue an error message. */ 7243169699Skan if (declspecs->multiple_types_p) 7244169699Skan { 7245169699Skan error ("two or more data types in declaration of %qs", name); 7246169699Skan return error_mark_node; 7247169699Skan } 724818334Speter 7249169699Skan /* Extract the basic type from the decl-specifier-seq. */ 7250169699Skan type = declspecs->type; 7251169699Skan if (type == error_mark_node) 725218334Speter { 7253169699Skan type = NULL_TREE; 7254169699Skan type_was_error_mark_node = true; 725518334Speter } 7256169699Skan /* If the entire declaration is itself tagged as deprecated then 7257169699Skan suppress reports of deprecated items. */ 7258169699Skan if (type && TREE_DEPRECATED (type) 7259169699Skan && deprecated_state != DEPRECATED_SUPPRESS) 7260169699Skan warn_deprecated_use (type); 7261169699Skan if (type && TREE_CODE (type) == TYPE_DECL) 7262169699Skan { 7263169699Skan typedef_decl = type; 7264169699Skan type = TREE_TYPE (typedef_decl); 7265169699Skan } 7266169699Skan /* No type at all: default to `int', and set DEFAULTED_INT 7267169699Skan because it was not a user-defined typedef. */ 7268169699Skan if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p)) 7269169699Skan { 7270169699Skan /* These imply 'int'. */ 7271169699Skan type = integer_type_node; 7272169699Skan defaulted_int = 1; 7273169699Skan } 7274169699Skan /* Gather flags. */ 7275169699Skan explicit_int = declspecs->explicit_int_p; 7276169699Skan explicit_char = declspecs->explicit_char_p; 727718334Speter 727896292Sobrien#if 0 727996292Sobrien /* See the code below that used this. */ 728096292Sobrien if (typedef_decl) 728196292Sobrien decl_attr = DECL_ATTRIBUTES (typedef_decl); 728296292Sobrien#endif 728318334Speter typedef_type = type; 728418334Speter 728518334Speter 7286169699Skan if (sfk != sfk_conversion) 7287169699Skan ctor_return_type = ctype; 728818334Speter 728990287Sobrien if (sfk != sfk_none) 729090287Sobrien type = check_special_function_return_type (sfk, type, 729190287Sobrien ctor_return_type); 729290287Sobrien else if (type == NULL_TREE) 729318334Speter { 729490287Sobrien int is_main; 729590287Sobrien 729618334Speter explicit_int = -1; 729752290Sobrien 729890287Sobrien /* We handle `main' specially here, because 'main () { }' is so 729990287Sobrien common. With no options, it is allowed. With -Wreturn-type, 730090287Sobrien it is a warning. It is only an error with -pedantic-errors. */ 730190287Sobrien is_main = (funcdef_flag 730290287Sobrien && dname && MAIN_NAME_P (dname) 730390287Sobrien && ctype == NULL_TREE 730490287Sobrien && in_namespace == NULL_TREE 730590287Sobrien && current_namespace == global_namespace); 730652290Sobrien 7307169699Skan if (type_was_error_mark_node) 7308169699Skan /* We've already issued an error, don't complain more. */; 7309169699Skan else if (in_system_header || flag_ms_extensions) 731090287Sobrien /* Allow it, sigh. */; 731190287Sobrien else if (pedantic || ! is_main) 7312169699Skan pedwarn ("ISO C++ forbids declaration of %qs with no type", name); 731390287Sobrien else if (warn_return_type) 7314169699Skan warning (0, "ISO C++ forbids declaration of %qs with no type", name); 731590287Sobrien 731690287Sobrien type = integer_type_node; 731718334Speter } 7318169699Skan 731918334Speter ctype = NULL_TREE; 732018334Speter 732118334Speter /* Now process the modifiers that were specified 732218334Speter and check for invalid combinations. */ 732318334Speter 732418334Speter /* Long double is a special combination. */ 7325169699Skan if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node) 732618334Speter { 7327169699Skan long_p = false; 732890287Sobrien type = build_qualified_type (long_double_type_node, 732990287Sobrien cp_type_quals (type)); 733018334Speter } 733118334Speter 733218334Speter /* Check all other uses of type modifiers. */ 733318334Speter 7334169699Skan if (unsigned_p || signed_p || long_p || short_p) 733518334Speter { 733618334Speter int ok = 0; 733718334Speter 7338169699Skan if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE) 7339169699Skan error ("%<signed%> or %<unsigned%> invalid for %qs", name); 7340169699Skan else if (signed_p && unsigned_p) 7341169699Skan error ("%<signed%> and %<unsigned%> specified together for %qs", name); 7342169699Skan else if (longlong && TREE_CODE (type) != INTEGER_TYPE) 7343169699Skan error ("%<long long%> invalid for %qs", name); 7344169699Skan else if (long_p && TREE_CODE (type) == REAL_TYPE) 7345169699Skan error ("%<long%> invalid for %qs", name); 7346169699Skan else if (short_p && TREE_CODE (type) == REAL_TYPE) 7347169699Skan error ("%<short%> invalid for %qs", name); 7348169699Skan else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE) 7349169699Skan error ("%<long%> or %<short%> invalid for %qs", name); 7350169699Skan else if ((long_p || short_p) && explicit_char) 7351169699Skan error ("%<long%> or %<short%> specified with char for %qs", name); 7352169699Skan else if (long_p && short_p) 7353169699Skan error ("%<long%> and %<short%> specified together for %qs", name); 735418334Speter else 735518334Speter { 735618334Speter ok = 1; 735751412Sobrien if (!explicit_int && !defaulted_int && !explicit_char && pedantic) 735818334Speter { 7359169699Skan pedwarn ("long, short, signed or unsigned used invalidly for %qs", 736018334Speter name); 736118334Speter if (flag_pedantic_errors) 736218334Speter ok = 0; 736318334Speter } 736418334Speter } 736518334Speter 736618334Speter /* Discard the type modifiers if they are invalid. */ 736718334Speter if (! ok) 736818334Speter { 7369169699Skan unsigned_p = false; 7370169699Skan signed_p = false; 7371169699Skan long_p = false; 7372169699Skan short_p = false; 737318334Speter longlong = 0; 737418334Speter } 737518334Speter } 737618334Speter 737718334Speter /* Decide whether an integer type is signed or not. 737818334Speter Optionally treat bitfields as signed by default. */ 7379169699Skan if (unsigned_p 738090287Sobrien /* [class.bit] 738190287Sobrien 738290287Sobrien It is implementation-defined whether a plain (neither 738390287Sobrien explicitly signed or unsigned) char, short, int, or long 738490287Sobrien bit-field is signed or unsigned. 738590287Sobrien 738690287Sobrien Naturally, we extend this to long long as well. Note that 738790287Sobrien this does not include wchar_t. */ 738890287Sobrien || (bitfield && !flag_signed_bitfields 7389169699Skan && !signed_p 739090287Sobrien /* A typedef for plain `int' without `signed' can be 739190287Sobrien controlled just like plain `int', but a typedef for 739290287Sobrien `signed int' cannot be so controlled. */ 739390287Sobrien && !(typedef_decl 739490287Sobrien && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) 7395169699Skan && TREE_CODE (type) == INTEGER_TYPE 739690287Sobrien && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) 739718334Speter { 739818334Speter if (longlong) 739918334Speter type = long_long_unsigned_type_node; 7400169699Skan else if (long_p) 740118334Speter type = long_unsigned_type_node; 7402169699Skan else if (short_p) 740318334Speter type = short_unsigned_type_node; 740418334Speter else if (type == char_type_node) 740518334Speter type = unsigned_char_type_node; 740618334Speter else if (typedef_decl) 7407117410Skan type = c_common_unsigned_type (type); 740818334Speter else 740918334Speter type = unsigned_type_node; 741018334Speter } 7411169699Skan else if (signed_p && type == char_type_node) 741218334Speter type = signed_char_type_node; 741318334Speter else if (longlong) 741418334Speter type = long_long_integer_type_node; 7415169699Skan else if (long_p) 741618334Speter type = long_integer_type_node; 7417169699Skan else if (short_p) 741818334Speter type = short_integer_type_node; 741918334Speter 7420169699Skan if (declspecs->specs[(int)ds_complex]) 742151412Sobrien { 7422169699Skan if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) 7423169699Skan error ("complex invalid for %qs", name); 742451412Sobrien /* If we just have "complex", it is equivalent to 742551412Sobrien "complex double", but if any modifiers at all are specified it is 742651412Sobrien the complex form of TYPE. E.g, "complex short" is 742751412Sobrien "complex short int". */ 742851412Sobrien 7429169699Skan else if (defaulted_int && ! longlong 7430169699Skan && ! (long_p || short_p || signed_p || unsigned_p)) 743151412Sobrien type = complex_double_type_node; 743251412Sobrien else if (type == integer_type_node) 743351412Sobrien type = complex_integer_type_node; 743451412Sobrien else if (type == float_type_node) 743551412Sobrien type = complex_float_type_node; 743651412Sobrien else if (type == double_type_node) 743751412Sobrien type = complex_double_type_node; 743851412Sobrien else if (type == long_double_type_node) 743951412Sobrien type = complex_long_double_type_node; 744051412Sobrien else 744151412Sobrien type = build_complex_type (type); 744251412Sobrien } 744351412Sobrien 744496292Sobrien type_quals = TYPE_UNQUALIFIED; 7445169699Skan if (declspecs->specs[(int)ds_const]) 744696292Sobrien type_quals |= TYPE_QUAL_CONST; 7447169699Skan if (declspecs->specs[(int)ds_volatile]) 744896292Sobrien type_quals |= TYPE_QUAL_VOLATILE; 7449169699Skan if (declspecs->specs[(int)ds_restrict]) 745096292Sobrien type_quals |= TYPE_QUAL_RESTRICT; 745196292Sobrien if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED) 7452169699Skan error ("qualifiers are not allowed on declaration of %<operator %T%>", 7453169699Skan ctor_return_type); 745451412Sobrien 7455169699Skan if (TREE_CODE (type) == FUNCTION_TYPE 7456169699Skan && type_quals != TYPE_UNQUALIFIED) 7457169699Skan { 7458169699Skan /* This was an error in C++98 (cv-qualifiers cannot be added to 7459169699Skan a function type), but DR 295 makes the code well-formed by 7460169699Skan dropping the extra qualifiers. */ 7461169699Skan if (pedantic) 7462169699Skan { 7463169699Skan tree bad_type = build_qualified_type (type, type_quals); 7464169699Skan pedwarn ("ignoring %qV qualifiers added to function type %qT", 7465169699Skan bad_type, type); 7466169699Skan } 7467169699Skan type_quals = TYPE_UNQUALIFIED; 7468169699Skan } 746996292Sobrien type_quals |= cp_type_quals (type); 747096292Sobrien type = cp_build_qualified_type_real 747196292Sobrien (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl) 7472169699Skan ? tf_ignore_bad_quals : 0) | tf_warning_or_error)); 747396292Sobrien /* We might have ignored or rejected some of the qualifiers. */ 747496292Sobrien type_quals = cp_type_quals (type); 7475169699Skan 747618334Speter staticp = 0; 7477169699Skan inlinep = !! declspecs->specs[(int)ds_inline]; 7478169699Skan virtualp = !! declspecs->specs[(int)ds_virtual]; 7479169699Skan explicitp = !! declspecs->specs[(int)ds_explicit]; 748018334Speter 7481169699Skan storage_class = declspecs->storage_class; 7482169699Skan if (storage_class == sc_static) 748318334Speter staticp = 1 + (decl_context == FIELD); 748418334Speter 748518334Speter if (virtualp && staticp == 2) 748618334Speter { 7487169699Skan error ("member %qD cannot be declared both virtual and static", dname); 7488169699Skan storage_class = sc_none; 748918334Speter staticp = 0; 749018334Speter } 7491169699Skan friendp = !! declspecs->specs[(int)ds_friend]; 749218334Speter 7493169699Skan if (dependent_name && !friendp) 749496292Sobrien { 7495169699Skan error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name); 7496169699Skan return error_mark_node; 749796292Sobrien } 749818334Speter 7499169699Skan /* Issue errors about use of storage classes for parameters. */ 7500169699Skan if (decl_context == PARM) 750118334Speter { 7502169699Skan if (declspecs->specs[(int)ds_typedef]) 750318334Speter { 7504169699Skan error ("typedef declaration invalid in parameter declaration"); 7505169699Skan return error_mark_node; 750618334Speter } 7507169699Skan else if (storage_class == sc_static 7508169699Skan || storage_class == sc_extern 7509169699Skan || thread_p) 7510169699Skan error ("storage class specifiers invalid in parameter declarations"); 751118334Speter } 751218334Speter 751318334Speter /* Give error if `virtual' is used outside of class declaration. */ 751418334Speter if (virtualp 751518334Speter && (current_class_name == NULL_TREE || decl_context != FIELD)) 751618334Speter { 751718334Speter error ("virtual outside class declaration"); 751818334Speter virtualp = 0; 751918334Speter } 752018334Speter 752118334Speter /* Static anonymous unions are dealt with here. */ 752218334Speter if (staticp && decl_context == TYPENAME 7523169699Skan && declspecs->type 7524169699Skan && ANON_AGGR_TYPE_P (declspecs->type)) 752518334Speter decl_context = FIELD; 752618334Speter 752718334Speter /* Warn about storage classes that are invalid for certain 752818334Speter kinds of declarations (parameters, typenames, etc.). */ 7529169699Skan if (thread_p 7530169699Skan && ((storage_class 7531169699Skan && storage_class != sc_extern 7532169699Skan && storage_class != sc_static) 7533169699Skan || declspecs->specs[(int)ds_typedef])) 753418334Speter { 7535169699Skan error ("multiple storage classes in declaration of %qs", name); 7536169699Skan thread_p = false; 7537169699Skan } 7538169699Skan if (declspecs->conflicting_specifiers_p) 7539169699Skan { 7540169699Skan error ("conflicting specifiers in declaration of %qs", name); 7541169699Skan storage_class = sc_none; 7542169699Skan } 7543169699Skan else if (decl_context != NORMAL 7544169699Skan && ((storage_class != sc_none 7545169699Skan && storage_class != sc_mutable) 7546169699Skan || thread_p)) 7547169699Skan { 754818334Speter if ((decl_context == PARM || decl_context == CATCHPARM) 7549169699Skan && (storage_class == sc_register 7550169699Skan || storage_class == sc_auto)) 755118334Speter ; 7552169699Skan else if (declspecs->specs[(int)ds_typedef]) 755351412Sobrien ; 755418334Speter else if (decl_context == FIELD 7555132747Skan /* C++ allows static class elements. */ 7556169699Skan && storage_class == sc_static) 755790287Sobrien /* C++ also allows inlines and signed and unsigned elements, 755890287Sobrien but in those cases we don't come in here. */ 755918334Speter ; 756018334Speter else 756118334Speter { 756218334Speter if (decl_context == FIELD) 7563169699Skan error ("storage class specified for %qs", name); 756418334Speter else 756590287Sobrien { 756690287Sobrien if (decl_context == PARM || decl_context == CATCHPARM) 7567169699Skan error ("storage class specified for parameter %qs", name); 756890287Sobrien else 756990287Sobrien error ("storage class specified for typename"); 757090287Sobrien } 7571169699Skan if (storage_class == sc_register 7572169699Skan || storage_class == sc_auto 7573169699Skan || storage_class == sc_extern 7574169699Skan || thread_p) 7575169699Skan storage_class = sc_none; 757618334Speter } 757718334Speter } 7578169699Skan else if (storage_class == sc_extern && initialized 7579169699Skan && !funcdef_flag) 758018334Speter { 758118334Speter if (toplevel_bindings_p ()) 758218334Speter { 758318334Speter /* It's common practice (and completely valid) to have a const 758418334Speter be initialized and declared extern. */ 758552290Sobrien if (!(type_quals & TYPE_QUAL_CONST)) 7586169699Skan warning (0, "%qs initialized and declared %<extern%>", name); 758718334Speter } 758818334Speter else 7589169699Skan error ("%qs has both %<extern%> and initializer", name); 759018334Speter } 7591169699Skan else if (storage_class == sc_extern && funcdef_flag 759218334Speter && ! toplevel_bindings_p ()) 7593169699Skan error ("nested function %qs declared %<extern%>", name); 759418334Speter else if (toplevel_bindings_p ()) 759518334Speter { 7596169699Skan if (storage_class == sc_auto) 7597169699Skan error ("top-level declaration of %qs specifies %<auto%>", name); 759818334Speter } 7599169699Skan else if (thread_p 7600169699Skan && storage_class != sc_extern 7601169699Skan && storage_class != sc_static) 7602117410Skan { 7603169699Skan error ("function-scope %qs implicitly auto and declared %<__thread%>", 7604117410Skan name); 7605169699Skan thread_p = false; 7606117410Skan } 760718334Speter 7608169699Skan if (storage_class && friendp) 760951412Sobrien error ("storage class specifiers invalid in friend function declarations"); 761051412Sobrien 7611169699Skan if (!id_declarator) 7612169699Skan unqualified_id = NULL_TREE; 7613169699Skan else 7614169699Skan { 7615169699Skan unqualified_id = id_declarator->u.id.unqualified_name; 7616169699Skan switch (TREE_CODE (unqualified_id)) 7617169699Skan { 7618169699Skan case BIT_NOT_EXPR: 7619169699Skan unqualified_id 7620169699Skan = constructor_name (TREE_OPERAND (unqualified_id, 0)); 7621169699Skan break; 7622132747Skan 7623169699Skan case IDENTIFIER_NODE: 7624169699Skan case TEMPLATE_ID_EXPR: 7625169699Skan break; 762618334Speter 7627169699Skan default: 7628169699Skan gcc_unreachable (); 7629169699Skan } 7630169699Skan } 7631169699Skan 7632169699Skan /* Determine the type of the entity declared by recurring on the 7633169699Skan declarator. */ 7634169699Skan for (; declarator; declarator = declarator->declarator) 763518334Speter { 7636169699Skan const cp_declarator *inner_declarator; 7637169699Skan tree attrs; 763818334Speter 763951412Sobrien if (type == error_mark_node) 7640169699Skan return error_mark_node; 764152290Sobrien 7642169699Skan attrs = declarator->attributes; 7643169699Skan if (attrs) 764418334Speter { 7645169699Skan int attr_flags; 764690287Sobrien 7647169699Skan attr_flags = 0; 7648169699Skan if (declarator == NULL || declarator->kind == cdk_id) 7649169699Skan attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 7650169699Skan if (declarator->kind == cdk_function) 7651169699Skan attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 7652169699Skan if (declarator->kind == cdk_array) 7653169699Skan attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 7654169699Skan returned_attrs = decl_attributes (&type, 7655169699Skan chainon (returned_attrs, attrs), 7656169699Skan attr_flags); 7657169699Skan } 765852290Sobrien 7659169699Skan if (declarator->kind == cdk_id) 7660169699Skan break; 766118334Speter 7662169699Skan inner_declarator = declarator->declarator; 766352290Sobrien 7664169699Skan switch (declarator->kind) 7665169699Skan { 7666169699Skan case cdk_array: 7667169699Skan type = create_array_type_for_decl (dname, type, 7668169699Skan declarator->u.array.bounds); 766918334Speter break; 767018334Speter 7671169699Skan case cdk_function: 767218334Speter { 767318334Speter tree arg_types; 767418334Speter int funcdecl_p; 767518334Speter 767618334Speter /* Declaring a function type. 767718334Speter Make sure we have a valid type for the function to return. */ 767818334Speter 767952290Sobrien /* We now know that the TYPE_QUALS don't apply to the 7680169699Skan decl, but to its return type. */ 768152290Sobrien type_quals = TYPE_UNQUALIFIED; 768218334Speter 768318334Speter /* Warn about some types functions can't return. */ 768418334Speter 768518334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 768618334Speter { 7687169699Skan error ("%qs declared as function returning a function", name); 768818334Speter type = integer_type_node; 768918334Speter } 769018334Speter if (TREE_CODE (type) == ARRAY_TYPE) 769118334Speter { 7692169699Skan error ("%qs declared as function returning an array", name); 769318334Speter type = integer_type_node; 769418334Speter } 769518334Speter 769618334Speter /* Pick up type qualifiers which should be applied to `this'. */ 7697169699Skan memfn_quals = declarator->u.function.qualifiers; 769818334Speter 769951412Sobrien /* Pick up the exception specifications. */ 7700169699Skan raises = declarator->u.function.exception_specification; 770151412Sobrien 770218334Speter /* Say it's a definition only for the CALL_EXPR 770318334Speter closest to the identifier. */ 7704169699Skan funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id; 770590287Sobrien 770618334Speter if (ctype == NULL_TREE 770718334Speter && decl_context == FIELD 770818334Speter && funcdecl_p 770918334Speter && (friendp == 0 || dname == current_class_name)) 771018334Speter ctype = current_class_type; 771118334Speter 7712169699Skan if (ctype && (sfk == sfk_constructor 7713169699Skan || sfk == sfk_destructor)) 771418334Speter { 771518334Speter /* We are within a class's scope. If our declarator name 771618334Speter is the same as the class name, and we are defining 771718334Speter a function, then it is a constructor/destructor, and 771818334Speter therefore returns a void type. */ 771918334Speter 7720169699Skan /* ISO C++ 12.4/2. A destructor may not be declared 7721169699Skan const or volatile. A destructor may not be 7722169699Skan static. 7723169699Skan 7724169699Skan ISO C++ 12.1. A constructor may not be declared 7725169699Skan const or volatile. A constructor may not be 7726169699Skan virtual. A constructor may not be static. */ 7727169699Skan if (staticp == 2) 7728169699Skan error ((flags == DTOR_FLAG) 7729169699Skan ? "destructor cannot be static member function" 7730169699Skan : "constructor cannot be static member function"); 7731169699Skan if (memfn_quals) 773218334Speter { 7733169699Skan error ((flags == DTOR_FLAG) 7734169699Skan ? "destructors may not be cv-qualified" 7735169699Skan : "constructors may not be cv-qualified"); 7736169699Skan memfn_quals = TYPE_UNQUALIFIED; 773718334Speter } 7738169699Skan 7739169699Skan if (decl_context == FIELD 7740169699Skan && !member_function_or_else (ctype, 7741169699Skan current_class_type, 7742169699Skan flags)) 7743169699Skan return error_mark_node; 7744169699Skan 7745169699Skan if (flags != DTOR_FLAG) 774618334Speter { 7747169699Skan /* It's a constructor. */ 774818334Speter if (explicitp == 1) 774918334Speter explicitp = 2; 775018334Speter if (virtualp) 775118334Speter { 775218334Speter pedwarn ("constructors cannot be declared virtual"); 775318334Speter virtualp = 0; 775418334Speter } 7755169699Skan if (decl_context == FIELD 7756169699Skan && sfk != sfk_constructor) 7757169699Skan return error_mark_node; 775818334Speter } 775918334Speter if (decl_context == FIELD) 776018334Speter staticp = 0; 776118334Speter } 776218334Speter else if (friendp) 776318334Speter { 776418334Speter if (initialized) 7765169699Skan error ("can't initialize friend function %qs", name); 776618334Speter if (virtualp) 776718334Speter { 776818334Speter /* Cannot be both friend and virtual. */ 776918334Speter error ("virtual functions cannot be friends"); 777018334Speter friendp = 0; 777118334Speter } 777218334Speter if (decl_context == NORMAL) 777318334Speter error ("friend declaration not in class definition"); 777418334Speter if (current_function_decl && funcdef_flag) 7775169699Skan error ("can't define friend function %qs in a local " 7776169699Skan "class definition", 7777169699Skan name); 777818334Speter } 777918334Speter 7780169699Skan arg_types = grokparms (declarator->u.function.parameters, 7781169699Skan &parms); 778218334Speter 7783169699Skan if (inner_declarator 7784169699Skan && inner_declarator->kind == cdk_id 7785169699Skan && inner_declarator->u.id.sfk == sfk_destructor 7786169699Skan && arg_types != void_list_node) 778718334Speter { 7788169699Skan error ("destructors may not have parameters"); 7789169699Skan arg_types = void_list_node; 7790169699Skan parms = NULL_TREE; 779118334Speter } 779218334Speter 779351412Sobrien type = build_function_type (type, arg_types); 779418334Speter } 779518334Speter break; 779618334Speter 7797169699Skan case cdk_pointer: 7798169699Skan case cdk_reference: 7799169699Skan case cdk_ptrmem: 780018334Speter /* Filter out pointers-to-references and references-to-references. 780118334Speter We can get these if a TYPE_DECL is used. */ 780218334Speter 780318334Speter if (TREE_CODE (type) == REFERENCE_TYPE) 780418334Speter { 7805169699Skan error (declarator->kind == cdk_reference 7806169699Skan ? "cannot declare reference to %q#T" 7807169699Skan : "cannot declare pointer to %q#T", type); 780818334Speter type = TREE_TYPE (type); 780918334Speter } 7810169699Skan else if (VOID_TYPE_P (type)) 7811169699Skan { 7812169699Skan if (declarator->kind == cdk_reference) 7813169699Skan error ("cannot declare reference to %q#T", type); 7814169699Skan else if (declarator->kind == cdk_ptrmem) 7815169699Skan error ("cannot declare pointer to %q#T member", type); 7816169699Skan } 781718334Speter 781852290Sobrien /* We now know that the TYPE_QUALS don't apply to the decl, 781952290Sobrien but to the target of the pointer. */ 782052290Sobrien type_quals = TYPE_UNQUALIFIED; 782118334Speter 7822169699Skan if (declarator->kind == cdk_ptrmem 7823169699Skan && (TREE_CODE (type) == FUNCTION_TYPE || memfn_quals)) 782418334Speter { 7825169699Skan memfn_quals |= cp_type_quals (type); 7826169699Skan type = build_memfn_type (type, 7827169699Skan declarator->u.pointer.class_type, 7828169699Skan memfn_quals); 7829169699Skan memfn_quals = TYPE_UNQUALIFIED; 7830169699Skan } 7831169699Skan 7832169699Skan if (declarator->kind == cdk_reference) 7833169699Skan { 7834117410Skan if (!VOID_TYPE_P (type)) 783552290Sobrien type = build_reference_type (type); 783618334Speter } 783718334Speter else if (TREE_CODE (type) == METHOD_TYPE) 783852290Sobrien type = build_ptrmemfunc_type (build_pointer_type (type)); 7839169699Skan else if (declarator->kind == cdk_ptrmem) 7840169699Skan { 7841169699Skan gcc_assert (TREE_CODE (declarator->u.pointer.class_type) 7842169699Skan != NAMESPACE_DECL); 7843169699Skan if (declarator->u.pointer.class_type == error_mark_node) 7844169699Skan /* We will already have complained. */ 7845169699Skan type = error_mark_node; 7846169699Skan else 7847169699Skan type = build_ptrmem_type (declarator->u.pointer.class_type, 7848169699Skan type); 7849169699Skan } 785018334Speter else 785118334Speter type = build_pointer_type (type); 785218334Speter 785318334Speter /* Process a list of type modifier keywords (such as 785418334Speter const or volatile) that were given inside the `*' or `&'. */ 785518334Speter 7856169699Skan if (declarator->u.pointer.qualifiers) 785718334Speter { 7858169699Skan type 7859169699Skan = cp_build_qualified_type (type, 7860169699Skan declarator->u.pointer.qualifiers); 786196292Sobrien type_quals = cp_type_quals (type); 786218334Speter } 786318334Speter ctype = NULL_TREE; 786418334Speter break; 786518334Speter 7866169699Skan case cdk_error: 7867169699Skan break; 786818334Speter 7869169699Skan default: 7870169699Skan gcc_unreachable (); 7871169699Skan } 7872169699Skan } 787351412Sobrien 7874169699Skan if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR 7875169699Skan && TREE_CODE (type) != FUNCTION_TYPE 7876169699Skan && TREE_CODE (type) != METHOD_TYPE) 7877169699Skan { 7878169699Skan error ("template-id %qD used as a declarator", 7879169699Skan unqualified_id); 7880169699Skan unqualified_id = dname; 7881169699Skan } 788218334Speter 7883169699Skan /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly 7884169699Skan qualified with a class-name, turn it into a METHOD_TYPE, unless 7885169699Skan we know that the function is static. We take advantage of this 7886169699Skan opportunity to do other processing that pertains to entities 7887169699Skan explicitly declared to be class members. Note that if DECLARATOR 7888169699Skan is non-NULL, we know it is a cdk_id declarator; otherwise, we 7889169699Skan would not have exited the loop above. */ 7890169699Skan if (declarator 7891169699Skan && declarator->u.id.qualifying_scope 7892169699Skan && TYPE_P (declarator->u.id.qualifying_scope)) 7893169699Skan { 7894169699Skan tree t; 789518334Speter 7896169699Skan ctype = declarator->u.id.qualifying_scope; 7897169699Skan ctype = TYPE_MAIN_VARIANT (ctype); 7898169699Skan t = ctype; 7899169699Skan while (t != NULL_TREE && CLASS_TYPE_P (t)) 7900169699Skan { 7901169699Skan /* You're supposed to have one `template <...>' for every 7902169699Skan template class, but you don't need one for a full 7903169699Skan specialization. For example: 790490287Sobrien 7905169699Skan template <class T> struct S{}; 7906169699Skan template <> struct S<int> { void f(); }; 7907169699Skan void S<int>::f () {} 790818334Speter 7909169699Skan is correct; there shouldn't be a `template <>' for the 7910169699Skan definition of `S<int>::f'. */ 7911169699Skan if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t) 7912169699Skan && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t))) 7913169699Skan /* T is an explicit (not partial) specialization. All 7914169699Skan containing classes must therefore also be explicitly 7915169699Skan specialized. */ 7916169699Skan break; 7917169699Skan if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t)) 7918169699Skan && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) 7919169699Skan template_count += 1; 792018334Speter 7921169699Skan t = TYPE_MAIN_DECL (t); 7922169699Skan t = DECL_CONTEXT (t); 7923169699Skan } 792418334Speter 7925169699Skan if (ctype == current_class_type) 7926169699Skan { 7927169699Skan if (friendp) 7928169699Skan pedwarn ("member functions are implicitly friends of their class"); 7929169699Skan else 7930169699Skan pedwarn ("extra qualification %<%T::%> on member %qs", 7931169699Skan ctype, name); 7932169699Skan } 7933169699Skan else if (/* If the qualifying type is already complete, then we 7934169699Skan can skip the following checks. */ 7935169699Skan !COMPLETE_TYPE_P (ctype) 7936169699Skan && (/* If the function is being defined, then 7937169699Skan qualifying type must certainly be complete. */ 7938169699Skan funcdef_flag 7939169699Skan /* A friend declaration of "T::f" is OK, even if 7940169699Skan "T" is a template parameter. But, if this 7941169699Skan function is not a friend, the qualifying type 7942169699Skan must be a class. */ 7943169699Skan || (!friendp && !CLASS_TYPE_P (ctype)) 7944169699Skan /* For a declaration, the type need not be 7945169699Skan complete, if either it is dependent (since there 7946169699Skan is no meaningful definition of complete in that 7947169699Skan case) or the qualifying class is currently being 7948169699Skan defined. */ 7949169699Skan || !(dependent_type_p (ctype) 7950169699Skan || currently_open_class (ctype))) 7951169699Skan /* Check that the qualifying type is complete. */ 7952169699Skan && !complete_type_or_else (ctype, NULL_TREE)) 7953169699Skan return error_mark_node; 7954169699Skan else if (TREE_CODE (type) == FUNCTION_TYPE) 7955169699Skan { 7956169699Skan tree sname = declarator->u.id.unqualified_name; 795718334Speter 7958169699Skan if (current_class_type 7959169699Skan && (!friendp || funcdef_flag)) 7960169699Skan { 7961169699Skan error (funcdef_flag 7962169699Skan ? "cannot define member function %<%T::%s%> within %<%T%>" 7963169699Skan : "cannot declare member function %<%T::%s%> within %<%T%>", 7964169699Skan ctype, name, current_class_type); 7965169699Skan return error_mark_node; 7966169699Skan } 796718334Speter 7968169699Skan if (TREE_CODE (sname) == IDENTIFIER_NODE 7969169699Skan && NEW_DELETE_OPNAME_P (sname)) 7970169699Skan /* Overloaded operator new and operator delete 7971169699Skan are always static functions. */ 7972169699Skan ; 7973169699Skan else 7974169699Skan type = build_memfn_type (type, ctype, memfn_quals); 797518334Speter } 7976169699Skan else if (declspecs->specs[(int)ds_typedef] 7977169699Skan && current_class_type) 7978169699Skan { 7979169699Skan error ("cannot declare member %<%T::%s%> within %qT", 7980169699Skan ctype, name, current_class_type); 7981169699Skan return error_mark_node; 7982169699Skan } 798318334Speter } 798418334Speter 7985169699Skan /* Now TYPE has the actual type. */ 7986169699Skan 798790287Sobrien if (returned_attrs) 798852290Sobrien { 798990287Sobrien if (attrlist) 799090287Sobrien *attrlist = chainon (returned_attrs, *attrlist); 799152290Sobrien else 799290287Sobrien attrlist = &returned_attrs; 799352290Sobrien } 799452290Sobrien 799590287Sobrien /* Did array size calculations overflow? */ 799690287Sobrien 799790287Sobrien if (TREE_CODE (type) == ARRAY_TYPE 799890287Sobrien && COMPLETE_TYPE_P (type) 7999169699Skan && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 8000169699Skan && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 800118334Speter { 8002169699Skan error ("size of array %qs is too large", name); 800390287Sobrien /* If we proceed with the array type as it is, we'll eventually 800490287Sobrien crash in tree_low_cst(). */ 800590287Sobrien type = error_mark_node; 800690287Sobrien } 800790287Sobrien 8008117410Skan if ((decl_context == FIELD || decl_context == PARM) 8009169699Skan && !processing_template_decl 8010169699Skan && variably_modified_type_p (type, NULL_TREE)) 8011107601Sobrien { 8012117410Skan if (decl_context == FIELD) 8013169699Skan error ("data member may not have variably modified type %qT", type); 8014117410Skan else 8015169699Skan error ("parameter may not have variably modified type %qT", type); 8016107601Sobrien type = error_mark_node; 8017107601Sobrien } 8018107601Sobrien 801990287Sobrien if (explicitp == 1 || (explicitp && friendp)) 802090287Sobrien { 802190287Sobrien /* [dcl.fct.spec] The explicit specifier shall only be used in 8022169699Skan declarations of constructors within a class definition. */ 8023169699Skan error ("only declarations of constructors can be %<explicit%>"); 802418334Speter explicitp = 0; 802518334Speter } 802618334Speter 8027169699Skan if (storage_class == sc_mutable) 802818334Speter { 8029117410Skan if (decl_context != FIELD || friendp) 803018334Speter { 8031169699Skan error ("non-member %qs cannot be declared %<mutable%>", name); 8032169699Skan storage_class = sc_none; 803318334Speter } 8034169699Skan else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef]) 8035169699Skan { 8036169699Skan error ("non-object member %qs cannot be declared %<mutable%>", name); 8037169699Skan storage_class = sc_none; 8038169699Skan } 803990287Sobrien else if (TREE_CODE (type) == FUNCTION_TYPE 8040169699Skan || TREE_CODE (type) == METHOD_TYPE) 8041169699Skan { 8042169699Skan error ("function %qs cannot be declared %<mutable%>", name); 8043169699Skan storage_class = sc_none; 8044169699Skan } 804518334Speter else if (staticp) 804618334Speter { 8047169699Skan error ("static %qs cannot be declared %<mutable%>", name); 8048169699Skan storage_class = sc_none; 804918334Speter } 805090287Sobrien else if (type_quals & TYPE_QUAL_CONST) 805190287Sobrien { 8052169699Skan error ("const %qs cannot be declared %<mutable%>", name); 8053169699Skan storage_class = sc_none; 805490287Sobrien } 805518334Speter } 805618334Speter 805752290Sobrien /* If this is declaring a typedef name, return a TYPE_DECL. */ 8058169699Skan if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME) 805918334Speter { 806018334Speter tree decl; 806118334Speter 806218334Speter /* Note that the grammar rejects storage classes 806318334Speter in typenames, fields or parameters. */ 806451412Sobrien if (current_lang_name == lang_name_java) 806551412Sobrien TYPE_FOR_JAVA (type) = 1; 806618334Speter 8067169699Skan /* This declaration: 8068169699Skan 8069169699Skan typedef void f(int) const; 8070169699Skan 8071169699Skan declares a function type which is not a member of any 8072169699Skan particular class, but which is cv-qualified; for 8073169699Skan example "f S::*" declares a pointer to a const-qualified 8074169699Skan member function of S. We record the cv-qualification in the 8075169699Skan function type. */ 8076169699Skan if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE) 8077169699Skan type = cp_build_qualified_type (type, memfn_quals); 8078169699Skan 807951412Sobrien if (decl_context == FIELD) 8080169699Skan decl = build_lang_decl (TYPE_DECL, unqualified_id, type); 808151412Sobrien else 8082169699Skan decl = build_decl (TYPE_DECL, unqualified_id, type); 8083169699Skan if (id_declarator && declarator->u.id.qualifying_scope) 8084169699Skan error ("%Jtypedef name may not be a nested-name-specifier", decl); 8085169699Skan 8086169699Skan if (decl_context != FIELD) 808751412Sobrien { 808890287Sobrien if (!current_function_decl) 808990287Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 8090169699Skan else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl) 8091169699Skan || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P 8092169699Skan (current_function_decl))) 8093169699Skan /* The TYPE_DECL is "abstract" because there will be 8094169699Skan clones of this constructor/destructor, and there will 8095169699Skan be copies of this TYPE_DECL generated in those 8096169699Skan clones. */ 8097169699Skan DECL_ABSTRACT (decl) = 1; 809851412Sobrien } 8099169699Skan else if (constructor_name_p (unqualified_id, current_class_type)) 8100169699Skan pedwarn ("ISO C++ forbids nested type %qD with same name " 8101169699Skan "as enclosing class", 8102169699Skan unqualified_id); 8103169699Skan 810490287Sobrien /* If the user declares "typedef struct {...} foo" then the 810590287Sobrien struct will have an anonymous name. Fill that name in now. 810690287Sobrien Nothing can refer to it, so nothing needs know about the name 810790287Sobrien change. */ 810818334Speter if (type != error_mark_node 8109169699Skan && unqualified_id 811018334Speter && TYPE_NAME (type) 811118334Speter && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL 811290287Sobrien && TYPE_ANONYMOUS_P (type) 811396292Sobrien /* Don't do this if there are attributes. */ 811496292Sobrien && (!attrlist || !*attrlist) 811590287Sobrien && cp_type_quals (type) == TYPE_UNQUALIFIED) 811618334Speter { 811752290Sobrien tree oldname = TYPE_NAME (type); 811852290Sobrien tree t; 811952290Sobrien 812051412Sobrien /* Replace the anonymous name with the real name everywhere. */ 812152290Sobrien for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 812252290Sobrien if (TYPE_NAME (t) == oldname) 812352290Sobrien TYPE_NAME (t) = decl; 812418334Speter 812518334Speter if (TYPE_LANG_SPECIFIC (type)) 812618334Speter TYPE_WAS_ANONYMOUS (type) = 1; 812718334Speter 812852290Sobrien /* If this is a typedef within a template class, the nested 812952290Sobrien type is a (non-primary) template. The name for the 813052290Sobrien template needs updating as well. */ 813152290Sobrien if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) 813290287Sobrien DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) 813352290Sobrien = TYPE_IDENTIFIER (type); 813452290Sobrien 813552290Sobrien /* FIXME remangle member functions; member functions of a 813652290Sobrien type with external linkage have external linkage. */ 813718334Speter } 813818334Speter 8139169699Skan /* Any qualifiers on a function type typedef have already been 8140169699Skan dealt with. */ 8141169699Skan if (memfn_quals && !ctype && TREE_CODE (type) == FUNCTION_TYPE) 8142169699Skan memfn_quals = TYPE_UNQUALIFIED; 814318334Speter 8144169699Skan if (signed_p 814518334Speter || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 814618334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 814718334Speter 8148169699Skan bad_specifiers (decl, "type", virtualp, 8149169699Skan memfn_quals != TYPE_UNQUALIFIED, 815051412Sobrien inlinep, friendp, raises != NULL_TREE); 815151412Sobrien 815218334Speter return decl; 815318334Speter } 815418334Speter 815518334Speter /* Detect the case of an array type of unspecified size 815618334Speter which came, as such, direct from a typedef name. 815796292Sobrien We must copy the type, so that the array's domain can be 815896292Sobrien individually set by the object's initializer. */ 815918334Speter 816096292Sobrien if (type && typedef_type 816196292Sobrien && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) 816290287Sobrien && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) 816396292Sobrien type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); 816418334Speter 816590287Sobrien /* Detect where we're using a typedef of function type to declare a 8166132747Skan function. PARMS will not be set, so we must create it now. */ 8167169699Skan 816890287Sobrien if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) 816990287Sobrien { 817090287Sobrien tree decls = NULL_TREE; 817190287Sobrien tree args; 817290287Sobrien 817390287Sobrien for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) 817490287Sobrien { 8175117410Skan tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args)); 817690287Sobrien 817790287Sobrien TREE_CHAIN (decl) = decls; 817890287Sobrien decls = decl; 817990287Sobrien } 8180169699Skan 8181132747Skan parms = nreverse (decls); 8182169699Skan 8183169699Skan if (decl_context != TYPENAME) 8184169699Skan { 8185169699Skan /* A cv-qualifier-seq shall only be part of the function type 8186169699Skan for a non-static member function. [8.3.5/4 dcl.fct] */ 8187169699Skan if (cp_type_quals (type) != TYPE_UNQUALIFIED 8188169699Skan && (current_class_type == NULL_TREE || staticp) ) 8189169699Skan { 8190169699Skan error ("qualified function types cannot be used to declare %s functions", 8191169699Skan (staticp? "static member" : "free")); 8192169699Skan type = TYPE_MAIN_VARIANT (type); 8193169699Skan } 8194169699Skan 8195169699Skan /* The qualifiers on the function type become the qualifiers on 8196169699Skan the non-static member function. */ 8197169699Skan memfn_quals |= cp_type_quals (type); 8198169699Skan } 819990287Sobrien } 820090287Sobrien 820118334Speter /* If this is a type name (such as, in a cast or sizeof), 820218334Speter compute the type and return it now. */ 820318334Speter 820418334Speter if (decl_context == TYPENAME) 820518334Speter { 820618334Speter /* Note that the grammar rejects storage classes 820718334Speter in typenames, fields or parameters. */ 820852290Sobrien if (type_quals != TYPE_UNQUALIFIED) 820990287Sobrien type_quals = TYPE_UNQUALIFIED; 821018334Speter 821118334Speter /* Special case: "friend class foo" looks like a TYPENAME context. */ 821218334Speter if (friendp) 821318334Speter { 821452290Sobrien if (type_quals != TYPE_UNQUALIFIED) 821518334Speter { 821690287Sobrien error ("type qualifiers specified for friend class declaration"); 821752290Sobrien type_quals = TYPE_UNQUALIFIED; 821818334Speter } 821918334Speter if (inlinep) 822018334Speter { 8221169699Skan error ("%<inline%> specified for friend class declaration"); 822218334Speter inlinep = 0; 822318334Speter } 822418334Speter 8225117410Skan if (!current_aggr) 822690287Sobrien { 8227117410Skan /* Don't allow friend declaration without a class-key. */ 822890287Sobrien if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 8229117410Skan pedwarn ("template parameters cannot be friends"); 823090287Sobrien else if (TREE_CODE (type) == TYPENAME_TYPE) 8231169699Skan pedwarn ("friend declaration requires class-key, " 8232169699Skan "i.e. %<friend class %T::%D%>", 8233117410Skan TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type)); 823490287Sobrien else 8235169699Skan pedwarn ("friend declaration requires class-key, " 8236169699Skan "i.e. %<friend %#T%>", 8237117410Skan type); 823890287Sobrien } 823990287Sobrien 824018334Speter /* Only try to do this stuff if we didn't already give up. */ 824118334Speter if (type != integer_type_node) 824218334Speter { 824318334Speter /* A friendly class? */ 824418334Speter if (current_class_type) 8245132747Skan make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type), 8246132747Skan /*complain=*/true); 824718334Speter else 8248169699Skan error ("trying to make class %qT a friend of global scope", 8249169699Skan type); 825090287Sobrien 825118334Speter type = void_type_node; 825218334Speter } 825318334Speter } 8254169699Skan else if (memfn_quals) 825518334Speter { 825618334Speter if (ctype == NULL_TREE) 825718334Speter { 825890287Sobrien if (TREE_CODE (type) != METHOD_TYPE) 8259169699Skan error ("invalid qualifiers on non-member function type"); 826090287Sobrien else 8261169699Skan ctype = TYPE_METHOD_BASETYPE (type); 826218334Speter } 826390287Sobrien if (ctype) 8264169699Skan type = build_memfn_type (type, ctype, memfn_quals); 826518334Speter } 826618334Speter 826718334Speter return type; 826818334Speter } 8269169699Skan else if (unqualified_id == NULL_TREE && decl_context != PARM 827018334Speter && decl_context != CATCHPARM 827118334Speter && TREE_CODE (type) != UNION_TYPE 827218334Speter && ! bitfield) 827318334Speter { 8274169699Skan error ("abstract declarator %qT used as declaration", type); 8275146906Skan return error_mark_node; 827618334Speter } 827718334Speter 8278146906Skan /* Only functions may be declared using an operator-function-id. */ 8279169699Skan if (unqualified_id 8280169699Skan && IDENTIFIER_OPNAME_P (unqualified_id) 8281146906Skan && TREE_CODE (type) != FUNCTION_TYPE 8282146906Skan && TREE_CODE (type) != METHOD_TYPE) 828318334Speter { 8284169699Skan error ("declaration of %qD as non-function", unqualified_id); 8285146906Skan return error_mark_node; 828618334Speter } 828718334Speter 8288146906Skan /* We don't check parameter types here because we can emit a better 8289146906Skan error message later. */ 8290146906Skan if (decl_context != PARM) 8291169699Skan { 8292169699Skan type = check_var_type (unqualified_id, type); 8293169699Skan if (type == error_mark_node) 8294169699Skan return error_mark_node; 8295169699Skan } 8296146906Skan 829718334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 829818334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 829918334Speter 830052290Sobrien if (decl_context == PARM || decl_context == CATCHPARM) 830152290Sobrien { 830252290Sobrien if (ctype || in_namespace) 8303169699Skan error ("cannot use %<::%> in parameter declaration"); 830452290Sobrien 830552290Sobrien /* A parameter declared as an array of T is really a pointer to T. 830652290Sobrien One declared as a function is really a pointer to a function. 830752290Sobrien One declared as a member is really a pointer to member. */ 830852290Sobrien 830952290Sobrien if (TREE_CODE (type) == ARRAY_TYPE) 831052290Sobrien { 831152290Sobrien /* Transfer const-ness of array into that of type pointed to. */ 831252290Sobrien type = build_pointer_type (TREE_TYPE (type)); 831352290Sobrien type_quals = TYPE_UNQUALIFIED; 831452290Sobrien } 831552290Sobrien else if (TREE_CODE (type) == FUNCTION_TYPE) 831652290Sobrien type = build_pointer_type (type); 831752290Sobrien } 831890287Sobrien 831918334Speter { 8320132747Skan tree decl; 832118334Speter 832218334Speter if (decl_context == PARM) 832318334Speter { 8324169699Skan decl = cp_build_parm_decl (unqualified_id, type); 832518334Speter 8326169699Skan bad_specifiers (decl, "parameter", virtualp, 8327169699Skan memfn_quals != TYPE_UNQUALIFIED, 832818334Speter inlinep, friendp, raises != NULL_TREE); 832918334Speter } 833018334Speter else if (decl_context == FIELD) 833118334Speter { 8332132747Skan /* The C99 flexible array extension. */ 8333132747Skan if (!staticp && TREE_CODE (type) == ARRAY_TYPE 8334132747Skan && TYPE_DOMAIN (type) == NULL_TREE) 8335132747Skan { 8336132747Skan tree itype = compute_array_index_type (dname, integer_zero_node); 8337132747Skan type = build_cplus_array_type (TREE_TYPE (type), itype); 8338132747Skan } 8339132747Skan 834018334Speter if (type == error_mark_node) 834118334Speter { 834218334Speter /* Happens when declaring arrays of sizes which 834318334Speter are error_mark_node, for example. */ 834418334Speter decl = NULL_TREE; 834518334Speter } 834652290Sobrien else if (in_namespace && !friendp) 834752290Sobrien { 834852290Sobrien /* Something like struct S { int N::j; }; */ 8349169699Skan error ("invalid use of %<::%>"); 8350169699Skan return error_mark_node; 835152290Sobrien } 835218334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 835318334Speter { 835418334Speter int publicp = 0; 835551412Sobrien tree function_context; 835618334Speter 835718334Speter if (friendp == 0) 835818334Speter { 835918334Speter if (ctype == NULL_TREE) 836018334Speter ctype = current_class_type; 836118334Speter 836218334Speter if (ctype == NULL_TREE) 836318334Speter { 8364169699Skan error ("can't make %qD into a method -- not in a class", 8365169699Skan unqualified_id); 8366169699Skan return error_mark_node; 836718334Speter } 836818334Speter 836918334Speter /* ``A union may [ ... ] not [ have ] virtual functions.'' 837018334Speter ARM 9.5 */ 837118334Speter if (virtualp && TREE_CODE (ctype) == UNION_TYPE) 837218334Speter { 8373169699Skan error ("function %qD declared virtual inside a union", 8374169699Skan unqualified_id); 8375169699Skan return error_mark_node; 837618334Speter } 837718334Speter 8378169699Skan if (NEW_DELETE_OPNAME_P (unqualified_id)) 837918334Speter { 838018334Speter if (virtualp) 838118334Speter { 8382169699Skan error ("%qD cannot be declared virtual, since it " 8383169699Skan "is always static", 8384169699Skan unqualified_id); 838518334Speter virtualp = 0; 838618334Speter } 838718334Speter } 838818334Speter else if (staticp < 2) 8389169699Skan type = build_memfn_type (type, ctype, memfn_quals); 839018334Speter } 839118334Speter 8392169699Skan /* Check that the name used for a destructor makes sense. */ 8393169699Skan if (sfk == sfk_destructor) 8394169699Skan { 8395169699Skan if (!ctype) 8396169699Skan { 8397169699Skan gcc_assert (friendp); 8398169699Skan error ("expected qualified name in friend declaration " 8399169699Skan "for destructor %qD", 8400169699Skan id_declarator->u.id.unqualified_name); 8401169699Skan return error_mark_node; 8402169699Skan } 8403169699Skan 8404169699Skan if (!same_type_p (TREE_OPERAND 8405169699Skan (id_declarator->u.id.unqualified_name, 0), 8406169699Skan ctype)) 8407169699Skan { 8408169699Skan error ("declaration of %qD as member of %qT", 8409169699Skan id_declarator->u.id.unqualified_name, ctype); 8410169699Skan return error_mark_node; 8411169699Skan } 8412169699Skan } 8413169699Skan 841418334Speter /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ 841590287Sobrien function_context = (ctype != NULL_TREE) ? 841690287Sobrien decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; 841751412Sobrien publicp = (! friendp || ! staticp) 841851412Sobrien && function_context == NULL_TREE; 841990287Sobrien decl = grokfndecl (ctype, type, 8420169699Skan TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR 8421169699Skan ? unqualified_id : dname, 8422132747Skan parms, 8423169699Skan unqualified_id, 8424169699Skan virtualp, flags, memfn_quals, raises, 842551412Sobrien friendp ? -1 : 0, friendp, publicp, inlinep, 8426169699Skan sfk, 8427169699Skan funcdef_flag, template_count, in_namespace, attrlist); 842818334Speter if (decl == NULL_TREE) 8429169699Skan return error_mark_node; 843051412Sobrien#if 0 843151412Sobrien /* This clobbers the attrs stored in `decl' from `attrlist'. */ 843290287Sobrien /* The decl and setting of decl_attr is also turned off. */ 843390287Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 843451412Sobrien#endif 843518334Speter 843652290Sobrien /* [class.conv.ctor] 843752290Sobrien 843852290Sobrien A constructor declared without the function-specifier 843952290Sobrien explicit that can be called with a single parameter 844052290Sobrien specifies a conversion from the type of its first 844152290Sobrien parameter to the type of its class. Such a constructor 844252290Sobrien is called a converting constructor. */ 844318334Speter if (explicitp == 2) 844418334Speter DECL_NONCONVERTING_P (decl) = 1; 844552290Sobrien else if (DECL_CONSTRUCTOR_P (decl)) 844652290Sobrien { 844752290Sobrien /* The constructor can be called with exactly one 844852290Sobrien parameter if there is at least one parameter, and 844952290Sobrien any subsequent parameters have default arguments. 845090287Sobrien Ignore any compiler-added parms. */ 845190287Sobrien tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl); 845252290Sobrien 845352290Sobrien if (arg_types == void_list_node 845490287Sobrien || (arg_types 845590287Sobrien && TREE_CHAIN (arg_types) 845652290Sobrien && TREE_CHAIN (arg_types) != void_list_node 845752290Sobrien && !TREE_PURPOSE (TREE_CHAIN (arg_types)))) 845852290Sobrien DECL_NONCONVERTING_P (decl) = 1; 845952290Sobrien } 846018334Speter } 846118334Speter else if (TREE_CODE (type) == METHOD_TYPE) 846218334Speter { 846318334Speter /* We only get here for friend declarations of 846418334Speter members of other classes. */ 846518334Speter /* All method decls are public, so tell grokfndecl to set 846618334Speter TREE_PUBLIC, also. */ 8467122192Skan decl = grokfndecl (ctype, type, 8468169699Skan TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR 8469169699Skan ? unqualified_id : dname, 8470132747Skan parms, 8471169699Skan unqualified_id, 8472169699Skan virtualp, flags, memfn_quals, raises, 8473169699Skan friendp ? -1 : 0, friendp, 1, 0, sfk, 8474169699Skan funcdef_flag, template_count, in_namespace, 8475169699Skan attrlist); 847618334Speter if (decl == NULL_TREE) 8477169699Skan return error_mark_node; 847818334Speter } 8479132747Skan else if (!staticp && !dependent_type_p (type) 848090287Sobrien && !COMPLETE_TYPE_P (complete_type (type)) 848118334Speter && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0)) 848218334Speter { 8483169699Skan if (unqualified_id) 8484169699Skan error ("field %qD has incomplete type", unqualified_id); 848518334Speter else 8486169699Skan error ("name %qT has incomplete type", type); 848718334Speter 848818334Speter /* If we're instantiating a template, tell them which 848918334Speter instantiation made the field's type be incomplete. */ 849018334Speter if (current_class_type 849118334Speter && TYPE_NAME (current_class_type) 849251412Sobrien && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)) 8493169699Skan && declspecs->type 8494169699Skan && declspecs->type == type) 8495169699Skan error (" in instantiation of template %qT", 8496169699Skan current_class_type); 849718334Speter 8498169699Skan return error_mark_node; 849918334Speter } 850018334Speter else 850118334Speter { 850218334Speter if (friendp) 850318334Speter { 8504169699Skan error ("%qE is neither function nor member function; " 8505169699Skan "cannot be declared friend", unqualified_id); 850618334Speter friendp = 0; 850718334Speter } 850818334Speter decl = NULL_TREE; 850918334Speter } 851018334Speter 851118334Speter if (friendp) 851218334Speter { 851318334Speter /* Friends are treated specially. */ 851418334Speter if (ctype == current_class_type) 8515169699Skan ; /* We already issued a pedwarn. */ 8516169699Skan else if (decl && DECL_NAME (decl)) 8517132747Skan { 8518132747Skan if (template_class_depth (current_class_type) == 0) 8519132747Skan { 8520132747Skan decl = check_explicit_specialization 8521169699Skan (unqualified_id, decl, template_count, 8522169699Skan 2 * funcdef_flag + 4); 8523132747Skan if (decl == error_mark_node) 8524132747Skan return error_mark_node; 8525132747Skan } 8526169699Skan 8527169699Skan decl = do_friend (ctype, unqualified_id, decl, 8528169699Skan *attrlist, flags, 8529169699Skan funcdef_flag); 8530132747Skan return decl; 8531132747Skan } 8532132747Skan else 8533169699Skan return error_mark_node; 853418334Speter } 853518334Speter 8536132747Skan /* Structure field. It may not be a function, except for C++. */ 853718334Speter 853818334Speter if (decl == NULL_TREE) 853918334Speter { 854018334Speter if (initialized) 854118334Speter { 854251412Sobrien if (!staticp) 854351412Sobrien { 854451412Sobrien /* An attempt is being made to initialize a non-static 854551412Sobrien member. But, from [class.mem]: 854690287Sobrien 854751412Sobrien 4 A member-declarator can contain a 854851412Sobrien constant-initializer only if it declares a static 854951412Sobrien member (_class.static_) of integral or enumeration 855090287Sobrien type, see _class.static.data_. 855151412Sobrien 855251412Sobrien This used to be relatively common practice, but 855351412Sobrien the rest of the compiler does not correctly 855451412Sobrien handle the initialization unless the member is 855551412Sobrien static so we make it static below. */ 8556169699Skan pedwarn ("ISO C++ forbids initialization of member %qD", 8557169699Skan unqualified_id); 8558169699Skan pedwarn ("making %qD static", unqualified_id); 855951412Sobrien staticp = 1; 856051412Sobrien } 856151412Sobrien 856252290Sobrien if (uses_template_parms (type)) 856352290Sobrien /* We'll check at instantiation time. */ 856452290Sobrien ; 8565169699Skan else if (check_static_variable_definition (unqualified_id, 856652290Sobrien type)) 856752290Sobrien /* If we just return the declaration, crashes 856852290Sobrien will sometimes occur. We therefore return 856952290Sobrien void_type_node, as if this was a friend 857052290Sobrien declaration, to cause callers to completely 857152290Sobrien ignore this declaration. */ 8572169699Skan return error_mark_node; 857318334Speter } 857418334Speter 857551412Sobrien if (staticp) 857618334Speter { 857790287Sobrien /* C++ allows static class members. All other work 857890287Sobrien for this is done by grokfield. */ 8579169699Skan decl = build_lang_decl (VAR_DECL, unqualified_id, type); 8580169699Skan set_linkage_for_static_data_member (decl); 8581169699Skan /* Even if there is an in-class initialization, DECL 8582169699Skan is considered undefined until an out-of-class 8583169699Skan definition is provided. */ 8584169699Skan DECL_EXTERNAL (decl) = 1; 8585169699Skan 8586169699Skan if (thread_p) 8587169699Skan { 8588169699Skan if (targetm.have_tls) 8589169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 8590169699Skan else 8591169699Skan /* A mere warning is sure to result in improper 8592169699Skan semantics at runtime. Don't bother to allow this to 8593169699Skan compile. */ 8594169699Skan error ("thread-local storage not supported for this target"); 8595169699Skan } 859618334Speter } 859718334Speter else 859818334Speter { 8599169699Skan decl = build_decl (FIELD_DECL, unqualified_id, type); 860090287Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 8601169699Skan if (storage_class == sc_mutable) 860218334Speter { 860318334Speter DECL_MUTABLE_P (decl) = 1; 8604169699Skan storage_class = sc_none; 860518334Speter } 860618334Speter } 860718334Speter 8608169699Skan bad_specifiers (decl, "field", virtualp, 8609169699Skan memfn_quals != TYPE_UNQUALIFIED, 861018334Speter inlinep, friendp, raises != NULL_TREE); 861118334Speter } 861218334Speter } 8613132747Skan else if (TREE_CODE (type) == FUNCTION_TYPE 8614132747Skan || TREE_CODE (type) == METHOD_TYPE) 861518334Speter { 861651412Sobrien tree original_name; 861718334Speter int publicp = 0; 861818334Speter 8619169699Skan if (!unqualified_id) 8620169699Skan return error_mark_node; 862118334Speter 8622169699Skan if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) 862351412Sobrien original_name = dname; 862451412Sobrien else 8625169699Skan original_name = unqualified_id; 862651412Sobrien 8627169699Skan if (storage_class == sc_auto) 8628169699Skan error ("storage class %<auto%> invalid for function %qs", name); 8629169699Skan else if (storage_class == sc_register) 8630169699Skan error ("storage class %<register%> invalid for function %qs", name); 8631169699Skan else if (thread_p) 8632169699Skan error ("storage class %<__thread%> invalid for function %qs", name); 863318334Speter 863418334Speter /* Function declaration not at top level. 863518334Speter Storage classes other than `extern' are not allowed 863618334Speter and `extern' makes no difference. */ 863718334Speter if (! toplevel_bindings_p () 8638169699Skan && (storage_class == sc_static 8639169699Skan || declspecs->specs[(int)ds_inline]) 864018334Speter && pedantic) 864118334Speter { 8642169699Skan if (storage_class == sc_static) 8643169699Skan pedwarn ("%<static%> specified invalid for function %qs " 8644169699Skan "declared out of global scope", name); 864518334Speter else 8646169699Skan pedwarn ("%<inline%> specifier invalid for function %qs " 8647169699Skan "declared out of global scope", name); 864818334Speter } 864990287Sobrien 865018334Speter if (ctype == NULL_TREE) 865118334Speter { 865218334Speter if (virtualp) 865318334Speter { 8654169699Skan error ("virtual non-class function %qs", name); 865518334Speter virtualp = 0; 865618334Speter } 865718334Speter } 8658132747Skan else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2 8659132747Skan && !NEW_DELETE_OPNAME_P (original_name)) 8660169699Skan type = build_method_type_directly (ctype, 8661132747Skan TREE_TYPE (type), 8662132747Skan TYPE_ARG_TYPES (type)); 866318334Speter 866451412Sobrien /* Record presence of `static'. */ 866518334Speter publicp = (ctype != NULL_TREE 8666169699Skan || storage_class == sc_extern 8667169699Skan || storage_class != sc_static); 866818334Speter 8669169699Skan decl = grokfndecl (ctype, type, original_name, parms, unqualified_id, 8670169699Skan virtualp, flags, memfn_quals, raises, 867151412Sobrien 1, friendp, 8672169699Skan publicp, inlinep, sfk, funcdef_flag, 8673169699Skan template_count, in_namespace, attrlist); 867418334Speter if (decl == NULL_TREE) 8675169699Skan return error_mark_node; 867618334Speter 867718334Speter if (staticp == 1) 867818334Speter { 8679117410Skan int invalid_static = 0; 868018334Speter 868118334Speter /* Don't allow a static member function in a class, and forbid 868218334Speter declaring main to be static. */ 868318334Speter if (TREE_CODE (type) == METHOD_TYPE) 868418334Speter { 8685169699Skan pedwarn ("cannot declare member function %qD to have " 8686169699Skan "static linkage", decl); 8687117410Skan invalid_static = 1; 868818334Speter } 868918334Speter else if (current_function_decl) 869018334Speter { 869118334Speter /* FIXME need arm citation */ 869218334Speter error ("cannot declare static function inside another function"); 8693117410Skan invalid_static = 1; 869418334Speter } 869518334Speter 8696117410Skan if (invalid_static) 869718334Speter { 869818334Speter staticp = 0; 8699169699Skan storage_class = sc_none; 870018334Speter } 870118334Speter } 870218334Speter } 870318334Speter else 870418334Speter { 870518334Speter /* It's a variable. */ 870618334Speter 870718334Speter /* An uninitialized decl with `extern' is a reference. */ 8708169699Skan decl = grokvardecl (type, unqualified_id, 8709169699Skan declspecs, 871090287Sobrien initialized, 871190287Sobrien (type_quals & TYPE_QUAL_CONST) != 0, 8712117410Skan ctype ? ctype : in_namespace); 8713169699Skan bad_specifiers (decl, "variable", virtualp, 8714169699Skan memfn_quals != TYPE_UNQUALIFIED, 871518334Speter inlinep, friendp, raises != NULL_TREE); 871618334Speter 871718334Speter if (ctype) 871818334Speter { 871918334Speter DECL_CONTEXT (decl) = ctype; 872018334Speter if (staticp == 1) 872118334Speter { 8722169699Skan pedwarn ("%<static%> may not be used when defining " 8723169699Skan "(as opposed to declaring) a static data member"); 8724169699Skan staticp = 0; 8725169699Skan storage_class = sc_none; 872618334Speter } 8727169699Skan if (storage_class == sc_register && TREE_STATIC (decl)) 872818334Speter { 8729169699Skan error ("static member %qD declared %<register%>", decl); 8730169699Skan storage_class = sc_none; 873118334Speter } 8732169699Skan if (storage_class == sc_extern && pedantic) 873318334Speter { 8734169699Skan pedwarn ("cannot explicitly declare member %q#D to have " 8735169699Skan "extern linkage", 8736169699Skan decl); 8737169699Skan storage_class = sc_none; 873818334Speter } 873918334Speter } 874018334Speter } 874118334Speter 874218334Speter /* Record `register' declaration for warnings on & 874318334Speter and in case doing stupid register allocation. */ 874418334Speter 8745169699Skan if (storage_class == sc_register) 874618334Speter DECL_REGISTER (decl) = 1; 8747169699Skan else if (storage_class == sc_extern) 874818334Speter DECL_THIS_EXTERN (decl) = 1; 8749169699Skan else if (storage_class == sc_static) 875018334Speter DECL_THIS_STATIC (decl) = 1; 875118334Speter 875290287Sobrien /* Record constancy and volatility. There's no need to do this 875390287Sobrien when processing a template; we'll do this for the instantiated 875490287Sobrien declaration based on the type of DECL. */ 875590287Sobrien if (!processing_template_decl) 8756169699Skan cp_apply_type_quals_to_decl (type_quals, decl); 875718334Speter 875818334Speter return decl; 875918334Speter } 876018334Speter} 876118334Speter 876252290Sobrien/* Subroutine of start_function. Ensure that each of the parameter 876352290Sobrien types (as listed in PARMS) is complete, as is required for a 876452290Sobrien function definition. */ 876518334Speter 876618334Speterstatic void 8767132747Skanrequire_complete_types_for_parms (tree parms) 876818334Speter{ 876990287Sobrien for (; parms; parms = TREE_CHAIN (parms)) 877018334Speter { 8771169699Skan if (dependent_type_p (TREE_TYPE (parms))) 8772169699Skan continue; 8773169699Skan if (!VOID_TYPE_P (TREE_TYPE (parms)) 8774169699Skan && complete_type_or_else (TREE_TYPE (parms), parms)) 8775117410Skan { 8776169699Skan relayout_decl (parms); 8777117410Skan DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms)); 8778117410Skan } 8779169699Skan else 8780169699Skan /* grokparms or complete_type_or_else will have already issued 8781169699Skan an error. */ 8782169699Skan TREE_TYPE (parms) = error_mark_node; 878318334Speter } 878418334Speter} 878518334Speter 8786117410Skan/* Returns nonzero if T is a local variable. */ 878752290Sobrien 878890287Sobrienint 8789132747Skanlocal_variable_p (tree t) 879052290Sobrien{ 879190287Sobrien if ((TREE_CODE (t) == VAR_DECL 879252290Sobrien /* A VAR_DECL with a context that is a _TYPE is a static data 879352290Sobrien member. */ 879452290Sobrien && !TYPE_P (CP_DECL_CONTEXT (t)) 879552290Sobrien /* Any other non-local variable must be at namespace scope. */ 879690287Sobrien && !DECL_NAMESPACE_SCOPE_P (t)) 879752290Sobrien || (TREE_CODE (t) == PARM_DECL)) 879890287Sobrien return 1; 879952290Sobrien 880090287Sobrien return 0; 880152290Sobrien} 880252290Sobrien 8803117410Skan/* Returns nonzero if T is an automatic local variable or a label. 880490287Sobrien (These are the declarations that need to be remapped when the code 880590287Sobrien containing them is duplicated.) */ 880690287Sobrien 880790287Sobrienint 8808132747Skannonstatic_local_decl_p (tree t) 880990287Sobrien{ 881090287Sobrien return ((local_variable_p (t) && !TREE_STATIC (t)) 881190287Sobrien || TREE_CODE (t) == LABEL_DECL 881290287Sobrien || TREE_CODE (t) == RESULT_DECL); 881390287Sobrien} 881490287Sobrien 881590287Sobrien/* Like local_variable_p, but suitable for use as a tree-walking 881690287Sobrien function. */ 881790287Sobrien 881890287Sobrienstatic tree 8819169699Skanlocal_variable_p_walkfn (tree *tp, int *walk_subtrees, 8820169699Skan void *data ATTRIBUTE_UNUSED) 882190287Sobrien{ 8822169699Skan if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) 8823169699Skan return *tp; 8824169699Skan else if (TYPE_P (*tp)) 8825169699Skan *walk_subtrees = 0; 8826169699Skan 8827169699Skan return NULL_TREE; 882890287Sobrien} 882990287Sobrien 8830169699Skan 883152290Sobrien/* Check that ARG, which is a default-argument expression for a 8832117410Skan parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if 883352290Sobrien something goes wrong. DECL may also be a _TYPE node, rather than a 883452290Sobrien DECL, if there is no DECL available. */ 883552290Sobrien 883652290Sobrientree 8837132747Skancheck_default_argument (tree decl, tree arg) 883852290Sobrien{ 883952290Sobrien tree var; 884052290Sobrien tree decl_type; 884152290Sobrien 884252290Sobrien if (TREE_CODE (arg) == DEFAULT_ARG) 884352290Sobrien /* We get a DEFAULT_ARG when looking at an in-class declaration 884452290Sobrien with a default argument. Ignore the argument for now; we'll 884552290Sobrien deal with it after the class is complete. */ 884652290Sobrien return arg; 884752290Sobrien 884852290Sobrien if (TYPE_P (decl)) 884952290Sobrien { 885052290Sobrien decl_type = decl; 885152290Sobrien decl = NULL_TREE; 885252290Sobrien } 885352290Sobrien else 885452290Sobrien decl_type = TREE_TYPE (decl); 885552290Sobrien 885690287Sobrien if (arg == error_mark_node 885752290Sobrien || decl == error_mark_node 885852290Sobrien || TREE_TYPE (arg) == error_mark_node 885952290Sobrien || decl_type == error_mark_node) 886052290Sobrien /* Something already went wrong. There's no need to check 886152290Sobrien further. */ 886252290Sobrien return error_mark_node; 886352290Sobrien 886452290Sobrien /* [dcl.fct.default] 886590287Sobrien 886652290Sobrien A default argument expression is implicitly converted to the 886752290Sobrien parameter type. */ 886852290Sobrien if (!TREE_TYPE (arg) 8869169699Skan || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL)) 887052290Sobrien { 887152290Sobrien if (decl) 8872169699Skan error ("default argument for %q#D has type %qT", 8873169699Skan decl, TREE_TYPE (arg)); 887452290Sobrien else 8875169699Skan error ("default argument for parameter of type %qT has type %qT", 8876169699Skan decl_type, TREE_TYPE (arg)); 887752290Sobrien 887852290Sobrien return error_mark_node; 887952290Sobrien } 888052290Sobrien 888152290Sobrien /* [dcl.fct.default] 888252290Sobrien 888352290Sobrien Local variables shall not be used in default argument 888490287Sobrien expressions. 888552290Sobrien 888652290Sobrien The keyword `this' shall not be used in a default argument of a 888752290Sobrien member function. */ 888890287Sobrien var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn, 888990287Sobrien NULL); 889052290Sobrien if (var) 889152290Sobrien { 8892169699Skan error ("default argument %qE uses local variable %qD", arg, var); 889352290Sobrien return error_mark_node; 889452290Sobrien } 889552290Sobrien 889652290Sobrien /* All is well. */ 889752290Sobrien return arg; 889852290Sobrien} 889952290Sobrien 890018334Speter/* Decode the list of parameter types for a function type. 890118334Speter Given the list of things declared inside the parens, 890218334Speter return a list of types. 890318334Speter 8904169699Skan If this parameter does not end with an ellipsis, we append 8905169699Skan void_list_node. 890618334Speter 8907132747Skan *PARMS is set to the chain of PARM_DECLs created. */ 890818334Speter 890918334Speterstatic tree 8910169699Skangrokparms (cp_parameter_declarator *first_parm, tree *parms) 891118334Speter{ 891218334Speter tree result = NULL_TREE; 891318334Speter tree decls = NULL_TREE; 8914169699Skan int ellipsis = !first_parm || first_parm->ellipsis_p; 8915169699Skan cp_parameter_declarator *parm; 891690287Sobrien int any_error = 0; 891718334Speter 8918169699Skan for (parm = first_parm; parm != NULL; parm = parm->next) 891918334Speter { 892090287Sobrien tree type = NULL_TREE; 8921169699Skan tree init = parm->default_argument; 8922169699Skan tree attrs; 8923169699Skan tree decl; 892418334Speter 8925169699Skan if (parm == no_parameters) 8926169699Skan break; 892718334Speter 8928169699Skan attrs = parm->decl_specifiers.attributes; 8929169699Skan parm->decl_specifiers.attributes = NULL_TREE; 8930169699Skan decl = grokdeclarator (parm->declarator, &parm->decl_specifiers, 893190287Sobrien PARM, init != NULL_TREE, &attrs); 893290287Sobrien if (! decl || TREE_TYPE (decl) == error_mark_node) 8933169699Skan continue; 893418334Speter 893590287Sobrien if (attrs) 893690287Sobrien cplus_decl_attributes (&decl, attrs, 0); 893752290Sobrien 893890287Sobrien type = TREE_TYPE (decl); 893990287Sobrien if (VOID_TYPE_P (type)) 8940169699Skan { 8941169699Skan if (same_type_p (type, void_type_node) 8942169699Skan && DECL_SELF_REFERENCE_P (type) 8943169699Skan && !DECL_NAME (decl) && !result && !parm->next && !ellipsis) 8944169699Skan /* this is a parmlist of `(void)', which is ok. */ 8945169699Skan break; 8946169699Skan cxx_incomplete_type_error (decl, type); 894790287Sobrien /* It's not a good idea to actually create parameters of 894890287Sobrien type `void'; other parts of the compiler assume that a 894990287Sobrien void type terminates the parameter list. */ 895090287Sobrien type = error_mark_node; 895190287Sobrien TREE_TYPE (decl) = error_mark_node; 8952169699Skan } 895352290Sobrien 895490287Sobrien if (type != error_mark_node) 895590287Sobrien { 895690287Sobrien /* Top-level qualifiers on the parameters are 895790287Sobrien ignored for function types. */ 8958132747Skan type = cp_build_qualified_type (type, 0); 895990287Sobrien if (TREE_CODE (type) == METHOD_TYPE) 896090287Sobrien { 8961169699Skan error ("parameter %qD invalidly declared method type", decl); 896290287Sobrien type = build_pointer_type (type); 896390287Sobrien TREE_TYPE (decl) = type; 896490287Sobrien } 896590287Sobrien else if (abstract_virtuals_error (decl, type)) 8966117410Skan any_error = 1; /* Seems like a good idea. */ 896790287Sobrien else if (POINTER_TYPE_P (type)) 896890287Sobrien { 896990287Sobrien /* [dcl.fct]/6, parameter types cannot contain pointers 897090287Sobrien (references) to arrays of unknown bound. */ 897190287Sobrien tree t = TREE_TYPE (type); 897290287Sobrien int ptr = TYPE_PTR_P (type); 897318334Speter 8974169699Skan while (1) 8975169699Skan { 8976169699Skan if (TYPE_PTR_P (t)) 8977169699Skan ptr = 1; 8978169699Skan else if (TREE_CODE (t) != ARRAY_TYPE) 8979169699Skan break; 8980169699Skan else if (!TYPE_DOMAIN (t)) 8981169699Skan break; 8982169699Skan t = TREE_TYPE (t); 8983169699Skan } 898490287Sobrien if (TREE_CODE (t) == ARRAY_TYPE) 8985169699Skan error ("parameter %qD includes %s to array of unknown " 8986169699Skan "bound %qT", 8987169699Skan decl, ptr ? "pointer" : "reference", t); 898890287Sobrien } 898918334Speter 8990169699Skan if (any_error) 8991169699Skan init = NULL_TREE; 8992169699Skan else if (init && !processing_template_decl) 899390287Sobrien init = check_default_argument (decl, init); 899490287Sobrien } 899518334Speter 899690287Sobrien TREE_CHAIN (decl) = decls; 899790287Sobrien decls = decl; 899890287Sobrien result = tree_cons (init, type, result); 899918334Speter } 900090287Sobrien decls = nreverse (decls); 900190287Sobrien result = nreverse (result); 900290287Sobrien if (!ellipsis) 900390287Sobrien result = chainon (result, void_list_node); 9004132747Skan *parms = decls; 900518334Speter 900618334Speter return result; 900718334Speter} 900851412Sobrien 900918334Speter 901090287Sobrien/* D is a constructor or overloaded `operator='. 901160970Sobrien 901290287Sobrien Let T be the class in which D is declared. Then, this function 901390287Sobrien returns: 901490287Sobrien 901590287Sobrien -1 if D's is an ill-formed constructor or copy assignment operator 901690287Sobrien whose first parameter is of type `T'. 901790287Sobrien 0 if D is not a copy constructor or copy assignment 901890287Sobrien operator. 901990287Sobrien 1 if D is a copy constructor or copy assignment operator whose 902090287Sobrien first parameter is a reference to const qualified T. 902190287Sobrien 2 if D is a copy constructor or copy assignment operator whose 902290287Sobrien first parameter is a reference to non-const qualified T. 902390287Sobrien 902490287Sobrien This function can be used as a predicate. Positive values indicate 9025117410Skan a copy constructor and nonzero values indicate a copy assignment 902690287Sobrien operator. */ 902790287Sobrien 902851412Sobrienint 9029132747Skancopy_fn_p (tree d) 903051412Sobrien{ 903190287Sobrien tree args; 903290287Sobrien tree arg_type; 903390287Sobrien int result = 1; 903451412Sobrien 9035169699Skan gcc_assert (DECL_FUNCTION_MEMBER_P (d)); 9036169699Skan 9037169699Skan if (TREE_CODE (d) == TEMPLATE_DECL 9038169699Skan || (DECL_TEMPLATE_INFO (d) 9039169699Skan && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d)))) 904090287Sobrien /* Instantiations of template member functions are never copy 904190287Sobrien functions. Note that member functions of templated classes are 904290287Sobrien represented as template functions internally, and we must 904390287Sobrien accept those as copy functions. */ 904490287Sobrien return 0; 9045169699Skan 904690287Sobrien args = FUNCTION_FIRST_USER_PARMTYPE (d); 904790287Sobrien if (!args) 904890287Sobrien return 0; 904951412Sobrien 905090287Sobrien arg_type = TREE_VALUE (args); 9051169699Skan if (arg_type == error_mark_node) 9052169699Skan return 0; 905318334Speter 905490287Sobrien if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) 905518334Speter { 905690287Sobrien /* Pass by value copy assignment operator. */ 905790287Sobrien result = -1; 905818334Speter } 905990287Sobrien else if (TREE_CODE (arg_type) == REFERENCE_TYPE 906090287Sobrien && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) 906160970Sobrien { 906290287Sobrien if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) 906390287Sobrien result = 2; 906460970Sobrien } 906590287Sobrien else 906690287Sobrien return 0; 9067169699Skan 906890287Sobrien args = TREE_CHAIN (args); 906960970Sobrien 907090287Sobrien if (args && args != void_list_node && !TREE_PURPOSE (args)) 907190287Sobrien /* There are more non-optional args. */ 907290287Sobrien return 0; 907370639Sobrien 907490287Sobrien return result; 907590287Sobrien} 907652290Sobrien 907790287Sobrien/* Remember any special properties of member function DECL. */ 907890287Sobrien 9079132747Skanvoid grok_special_member_properties (tree decl) 908090287Sobrien{ 9081169699Skan tree class_type; 9082169699Skan 9083169699Skan if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 9084169699Skan return; 9085169699Skan 9086169699Skan class_type = DECL_CONTEXT (decl); 9087169699Skan if (DECL_CONSTRUCTOR_P (decl)) 908818334Speter { 908990287Sobrien int ctor = copy_fn_p (decl); 9090169699Skan 9091169699Skan TYPE_HAS_CONSTRUCTOR (class_type) = 1; 9092169699Skan 909390287Sobrien if (ctor > 0) 909490287Sobrien { 909590287Sobrien /* [class.copy] 9096169699Skan 9097169699Skan A non-template constructor for class X is a copy 9098169699Skan constructor if its first parameter is of type X&, const 9099169699Skan X&, volatile X& or const volatile X&, and either there 9100169699Skan are no other parameters or else all other parameters have 9101169699Skan default arguments. */ 9102169699Skan TYPE_HAS_INIT_REF (class_type) = 1; 910390287Sobrien if (ctor > 1) 9104169699Skan TYPE_HAS_CONST_INIT_REF (class_type) = 1; 910590287Sobrien } 910690287Sobrien else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) 9107169699Skan TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1; 910818334Speter } 910990287Sobrien else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR) 911090287Sobrien { 911190287Sobrien /* [class.copy] 9112169699Skan 9113169699Skan A non-template assignment operator for class X is a copy 9114169699Skan assignment operator if its parameter is of type X, X&, const 9115169699Skan X&, volatile X& or const volatile X&. */ 9116169699Skan 911790287Sobrien int assop = copy_fn_p (decl); 9118169699Skan 911990287Sobrien if (assop) 912090287Sobrien { 9121169699Skan TYPE_HAS_ASSIGN_REF (class_type) = 1; 912290287Sobrien if (assop != 1) 9123169699Skan TYPE_HAS_CONST_ASSIGN_REF (class_type) = 1; 912490287Sobrien } 912590287Sobrien } 912690287Sobrien} 912752290Sobrien 912890287Sobrien/* Check a constructor DECL has the correct form. Complains 912990287Sobrien if the class has a constructor of the form X(X). */ 913052290Sobrien 913190287Sobrienint 9132132747Skangrok_ctor_properties (tree ctype, tree decl) 913390287Sobrien{ 913490287Sobrien int ctor_parm = copy_fn_p (decl); 913590287Sobrien 913690287Sobrien if (ctor_parm < 0) 913718334Speter { 913890287Sobrien /* [class.copy] 9139169699Skan 9140169699Skan A declaration of a constructor for a class X is ill-formed if 9141169699Skan its first parameter is of type (optionally cv-qualified) X 9142169699Skan and either there are no other parameters or else all other 9143169699Skan parameters have default arguments. 9144169699Skan 9145169699Skan We *don't* complain about member template instantiations that 9146169699Skan have this form, though; they can occur as we try to decide 9147169699Skan what constructor to use during overload resolution. Since 9148169699Skan overload resolution will never prefer such a constructor to 9149169699Skan the non-template copy constructor (which is either explicitly 9150169699Skan or implicitly defined), there's no need to worry about their 9151169699Skan existence. Theoretically, they should never even be 9152169699Skan instantiated, but that's hard to forestall. */ 9153169699Skan error ("invalid constructor; you probably meant %<%T (const %T&)%>", 915451412Sobrien ctype, ctype); 915551412Sobrien return 0; 915618334Speter } 9157169699Skan 915818334Speter return 1; 915918334Speter} 916018334Speter 916190287Sobrien/* An operator with this code is unary, but can also be binary. */ 916251412Sobrien 916318334Speterstatic int 9164132747Skanambi_op_p (enum tree_code code) 916518334Speter{ 916690287Sobrien return (code == INDIRECT_REF 916790287Sobrien || code == ADDR_EXPR 9168169699Skan || code == UNARY_PLUS_EXPR 916990287Sobrien || code == NEGATE_EXPR 917090287Sobrien || code == PREINCREMENT_EXPR 917190287Sobrien || code == PREDECREMENT_EXPR); 917218334Speter} 917318334Speter 917418334Speter/* An operator with this name can only be unary. */ 917551412Sobrien 917618334Speterstatic int 9177132747Skanunary_op_p (enum tree_code code) 917818334Speter{ 917990287Sobrien return (code == TRUTH_NOT_EXPR 918090287Sobrien || code == BIT_NOT_EXPR 918190287Sobrien || code == COMPONENT_REF 918290287Sobrien || code == TYPE_EXPR); 918318334Speter} 918418334Speter 9185169699Skan/* DECL is a declaration for an overloaded operator. If COMPLAIN is true, 9186132747Skan errors are issued for invalid declarations. */ 918751412Sobrien 9188132747Skanbool 9189161660Skangrok_op_properties (tree decl, bool complain) 919018334Speter{ 919118334Speter tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); 919290287Sobrien tree argtype; 919318334Speter int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); 919418334Speter tree name = DECL_NAME (decl); 919590287Sobrien enum tree_code operator_code; 919690287Sobrien int arity; 9197161660Skan bool ellipsis_p; 9198161660Skan tree class_type; 919918334Speter 9200169699Skan /* Count the number of arguments and check for ellipsis. */ 920190287Sobrien for (argtype = argtypes, arity = 0; 920290287Sobrien argtype && argtype != void_list_node; 920390287Sobrien argtype = TREE_CHAIN (argtype)) 920490287Sobrien ++arity; 9205161660Skan ellipsis_p = !argtype; 920690287Sobrien 9207161660Skan class_type = DECL_CONTEXT (decl); 9208161660Skan if (class_type && !CLASS_TYPE_P (class_type)) 9209161660Skan class_type = NULL_TREE; 9210169699Skan 921190287Sobrien if (DECL_CONV_FN_P (decl)) 921290287Sobrien operator_code = TYPE_EXPR; 921390287Sobrien else 921490287Sobrien do 921590287Sobrien { 921690287Sobrien#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ 921790287Sobrien if (ansi_opname (CODE) == name) \ 921890287Sobrien { \ 921990287Sobrien operator_code = (CODE); \ 922090287Sobrien break; \ 922190287Sobrien } \ 922290287Sobrien else if (ansi_assopname (CODE) == name) \ 922390287Sobrien { \ 922490287Sobrien operator_code = (CODE); \ 922590287Sobrien DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \ 922690287Sobrien break; \ 922790287Sobrien } 922890287Sobrien 922990287Sobrien#include "operators.def" 923090287Sobrien#undef DEF_OPERATOR 923190287Sobrien 9232169699Skan gcc_unreachable (); 923390287Sobrien } 923490287Sobrien while (0); 9235169699Skan gcc_assert (operator_code != LAST_CPLUS_TREE_CODE); 923690287Sobrien SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); 923790287Sobrien 9238161660Skan if (class_type) 9239161660Skan switch (operator_code) 9240161660Skan { 9241161660Skan case NEW_EXPR: 9242161660Skan TYPE_HAS_NEW_OPERATOR (class_type) = 1; 9243161660Skan break; 924490287Sobrien 9245161660Skan case DELETE_EXPR: 9246161660Skan TYPE_GETS_DELETE (class_type) |= 1; 9247161660Skan break; 924890287Sobrien 9249161660Skan case VEC_NEW_EXPR: 9250161660Skan TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1; 9251161660Skan break; 925290287Sobrien 9253161660Skan case VEC_DELETE_EXPR: 9254161660Skan TYPE_GETS_DELETE (class_type) |= 2; 9255161660Skan break; 925690287Sobrien 9257161660Skan default: 9258161660Skan break; 9259161660Skan } 926018334Speter 9261169699Skan /* [basic.std.dynamic.allocation]/1: 9262169699Skan 9263169699Skan A program is ill-formed if an allocation function is declared 9264169699Skan in a namespace scope other than global scope or declared static 9265169699Skan in global scope. 9266169699Skan 9267169699Skan The same also holds true for deallocation functions. */ 9268169699Skan if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR 9269169699Skan || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) 9270169699Skan { 9271169699Skan if (DECL_NAMESPACE_SCOPE_P (decl)) 9272169699Skan { 9273169699Skan if (CP_DECL_CONTEXT (decl) != global_namespace) 9274169699Skan { 9275169699Skan error ("%qD may not be declared within a namespace", decl); 9276169699Skan return false; 9277169699Skan } 9278169699Skan else if (!TREE_PUBLIC (decl)) 9279169699Skan { 9280169699Skan error ("%qD may not be declared as static", decl); 9281169699Skan return false; 9282169699Skan } 9283169699Skan } 9284169699Skan } 9285169699Skan 928690287Sobrien if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR) 9287132747Skan TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); 928890287Sobrien else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) 9289132747Skan TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); 929018334Speter else 929118334Speter { 929218334Speter /* An operator function must either be a non-static member function 929318334Speter or have at least one parameter of a class, a reference to a class, 929418334Speter an enumeration, or a reference to an enumeration. 13.4.0.6 */ 929518334Speter if (! methodp || DECL_STATIC_FUNCTION_P (decl)) 929618334Speter { 929790287Sobrien if (operator_code == TYPE_EXPR 929890287Sobrien || operator_code == CALL_EXPR 929990287Sobrien || operator_code == COMPONENT_REF 930090287Sobrien || operator_code == ARRAY_REF 930190287Sobrien || operator_code == NOP_EXPR) 9302169699Skan { 9303169699Skan error ("%qD must be a nonstatic member function", decl); 9304169699Skan return false; 9305169699Skan } 930618334Speter else 930718334Speter { 9308132747Skan tree p; 930918334Speter 931018334Speter if (DECL_STATIC_FUNCTION_P (decl)) 9311169699Skan { 9312169699Skan error ("%qD must be either a non-static member " 9313169699Skan "function or a non-member function", decl); 9314169699Skan return false; 9315169699Skan } 931618334Speter 9317132747Skan for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p)) 9318132747Skan { 9319132747Skan tree arg = non_reference (TREE_VALUE (p)); 9320169699Skan if (arg == error_mark_node) 9321169699Skan return false; 9322169699Skan 9323132747Skan /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used 9324132747Skan because these checks are performed even on 9325132747Skan template functions. */ 9326132747Skan if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE) 9327132747Skan break; 9328132747Skan } 932918334Speter 9330132747Skan if (!p || p == void_list_node) 9331132747Skan { 9332169699Skan if (complain) 9333169699Skan error ("%qD must have an argument of class or " 9334169699Skan "enumerated type", decl); 9335169699Skan return false; 9336132747Skan } 933718334Speter } 933818334Speter } 933990287Sobrien 9340132747Skan /* There are no restrictions on the arguments to an overloaded 9341132747Skan "operator ()". */ 934290287Sobrien if (operator_code == CALL_EXPR) 9343169699Skan return true; 934418334Speter 9345161660Skan /* Warn about conversion operators that will never be used. */ 9346169699Skan if (IDENTIFIER_TYPENAME_P (name) 9347161660Skan && ! DECL_TEMPLATE_INFO (decl) 9348161660Skan && warn_conversion 9349161660Skan /* Warn only declaring the function; there is no need to 9350161660Skan warn again about out-of-class definitions. */ 9351161660Skan && class_type == current_class_type) 935218334Speter { 935318334Speter tree t = TREE_TYPE (name); 9354161660Skan int ref = (TREE_CODE (t) == REFERENCE_TYPE); 9355161660Skan const char *what = 0; 9356169699Skan 9357161660Skan if (ref) 9358161660Skan t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); 9359169699Skan 9360161660Skan if (TREE_CODE (t) == VOID_TYPE) 9361161660Skan what = "void"; 9362161660Skan else if (class_type) 936318334Speter { 9364161660Skan if (t == class_type) 936518334Speter what = "the same type"; 936651412Sobrien /* Don't force t to be complete here. */ 936718334Speter else if (IS_AGGR_TYPE (t) 936890287Sobrien && COMPLETE_TYPE_P (t) 9369161660Skan && DERIVED_FROM_P (t, class_type)) 937018334Speter what = "a base class"; 9371161660Skan } 937218334Speter 9373161660Skan if (what) 9374169699Skan warning (OPT_Wconversion, "conversion to %s%s will never use a type " 9375169699Skan "conversion operator", 9376161660Skan ref ? "a reference to " : "", what); 937718334Speter } 9378161660Skan 937990287Sobrien if (operator_code == COND_EXPR) 938018334Speter { 938190287Sobrien /* 13.4.0.3 */ 938290287Sobrien error ("ISO C++ prohibits overloading operator ?:"); 9383169699Skan return false; 938418334Speter } 9385161660Skan else if (ellipsis_p) 9386169699Skan { 9387169699Skan error ("%qD must not have variable number of arguments", decl); 9388169699Skan return false; 9389169699Skan } 939090287Sobrien else if (ambi_op_p (operator_code)) 939118334Speter { 939290287Sobrien if (arity == 1) 939390287Sobrien /* We pick the one-argument operator codes by default, so 939490287Sobrien we don't have to change anything. */ 939590287Sobrien ; 939690287Sobrien else if (arity == 2) 939718334Speter { 939890287Sobrien /* If we thought this was a unary operator, we now know 939990287Sobrien it to be a binary operator. */ 940090287Sobrien switch (operator_code) 940190287Sobrien { 940290287Sobrien case INDIRECT_REF: 940390287Sobrien operator_code = MULT_EXPR; 940490287Sobrien break; 940590287Sobrien 940690287Sobrien case ADDR_EXPR: 940790287Sobrien operator_code = BIT_AND_EXPR; 940890287Sobrien break; 940990287Sobrien 9410169699Skan case UNARY_PLUS_EXPR: 941190287Sobrien operator_code = PLUS_EXPR; 941290287Sobrien break; 941390287Sobrien 941490287Sobrien case NEGATE_EXPR: 941590287Sobrien operator_code = MINUS_EXPR; 941690287Sobrien break; 941790287Sobrien 941890287Sobrien case PREINCREMENT_EXPR: 941990287Sobrien operator_code = POSTINCREMENT_EXPR; 942090287Sobrien break; 942190287Sobrien 942290287Sobrien case PREDECREMENT_EXPR: 942390287Sobrien operator_code = POSTDECREMENT_EXPR; 942490287Sobrien break; 942590287Sobrien 942690287Sobrien default: 9427169699Skan gcc_unreachable (); 942890287Sobrien } 942990287Sobrien 943090287Sobrien SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); 943190287Sobrien 943290287Sobrien if ((operator_code == POSTINCREMENT_EXPR 943390287Sobrien || operator_code == POSTDECREMENT_EXPR) 943451412Sobrien && ! processing_template_decl 943552290Sobrien && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node)) 943618334Speter { 943718334Speter if (methodp) 9438169699Skan error ("postfix %qD must take %<int%> as its argument", 9439169699Skan decl); 944018334Speter else 9441169699Skan error ("postfix %qD must take %<int%> as its second " 9442169699Skan "argument", decl); 9443169699Skan return false; 944418334Speter } 944518334Speter } 944618334Speter else 944718334Speter { 944818334Speter if (methodp) 9449169699Skan error ("%qD must take either zero or one argument", decl); 945018334Speter else 9451169699Skan error ("%qD must take either one or two arguments", decl); 9452169699Skan return false; 945318334Speter } 945451412Sobrien 945551412Sobrien /* More Effective C++ rule 6. */ 945651412Sobrien if (warn_ecpp 945790287Sobrien && (operator_code == POSTINCREMENT_EXPR 945890287Sobrien || operator_code == POSTDECREMENT_EXPR 945990287Sobrien || operator_code == PREINCREMENT_EXPR 946090287Sobrien || operator_code == PREDECREMENT_EXPR)) 946151412Sobrien { 946251412Sobrien tree arg = TREE_VALUE (argtypes); 946351412Sobrien tree ret = TREE_TYPE (TREE_TYPE (decl)); 946451412Sobrien if (methodp || TREE_CODE (arg) == REFERENCE_TYPE) 946551412Sobrien arg = TREE_TYPE (arg); 946651412Sobrien arg = TYPE_MAIN_VARIANT (arg); 946790287Sobrien if (operator_code == PREINCREMENT_EXPR 946890287Sobrien || operator_code == PREDECREMENT_EXPR) 946951412Sobrien { 947051412Sobrien if (TREE_CODE (ret) != REFERENCE_TYPE 947152290Sobrien || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), 947252290Sobrien arg)) 9473169699Skan warning (OPT_Weffc__, "prefix %qD should return %qT", decl, 9474169699Skan build_reference_type (arg)); 947551412Sobrien } 947651412Sobrien else 947751412Sobrien { 947852290Sobrien if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) 9479169699Skan warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg); 948051412Sobrien } 948151412Sobrien } 948218334Speter } 948390287Sobrien else if (unary_op_p (operator_code)) 948418334Speter { 948590287Sobrien if (arity != 1) 948618334Speter { 948718334Speter if (methodp) 9488169699Skan error ("%qD must take %<void%>", decl); 948918334Speter else 9490169699Skan error ("%qD must take exactly one argument", decl); 9491169699Skan return false; 949218334Speter } 949318334Speter } 949490287Sobrien else /* if (binary_op_p (operator_code)) */ 949518334Speter { 949690287Sobrien if (arity != 2) 949718334Speter { 949818334Speter if (methodp) 9499169699Skan error ("%qD must take exactly one argument", decl); 950018334Speter else 9501169699Skan error ("%qD must take exactly two arguments", decl); 9502169699Skan return false; 950318334Speter } 950451412Sobrien 950551412Sobrien /* More Effective C++ rule 7. */ 950651412Sobrien if (warn_ecpp 950790287Sobrien && (operator_code == TRUTH_ANDIF_EXPR 950890287Sobrien || operator_code == TRUTH_ORIF_EXPR 950990287Sobrien || operator_code == COMPOUND_EXPR)) 9510169699Skan warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments", 9511169699Skan decl); 951218334Speter } 951318334Speter 951451412Sobrien /* Effective C++ rule 23. */ 951551412Sobrien if (warn_ecpp 951690287Sobrien && arity == 2 951796292Sobrien && !DECL_ASSIGNMENT_OPERATOR_P (decl) 951890287Sobrien && (operator_code == PLUS_EXPR 951990287Sobrien || operator_code == MINUS_EXPR 952090287Sobrien || operator_code == TRUNC_DIV_EXPR 952196292Sobrien || operator_code == MULT_EXPR 952296292Sobrien || operator_code == TRUNC_MOD_EXPR) 952351412Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE) 9524169699Skan warning (OPT_Weffc__, "%qD should return by value", decl); 952551412Sobrien 952690287Sobrien /* [over.oper]/8 */ 952790287Sobrien for (; argtypes && argtypes != void_list_node; 9528169699Skan argtypes = TREE_CHAIN (argtypes)) 9529169699Skan if (TREE_PURPOSE (argtypes)) 9530169699Skan { 9531169699Skan TREE_PURPOSE (argtypes) = NULL_TREE; 9532169699Skan if (operator_code == POSTINCREMENT_EXPR 953390287Sobrien || operator_code == POSTDECREMENT_EXPR) 9534169699Skan { 9535169699Skan if (pedantic) 9536169699Skan pedwarn ("%qD cannot have default arguments", decl); 9537169699Skan } 9538169699Skan else 9539169699Skan { 9540169699Skan error ("%qD cannot have default arguments", decl); 9541169699Skan return false; 9542169699Skan } 9543169699Skan } 954418334Speter } 9545169699Skan return true; 954618334Speter} 954718334Speter 9548169699Skan/* Return a string giving the keyword associate with CODE. */ 9549169699Skan 955052290Sobrienstatic const char * 9551132747Skantag_name (enum tag_types code) 955252290Sobrien{ 955352290Sobrien switch (code) 955452290Sobrien { 955552290Sobrien case record_type: 955652290Sobrien return "struct"; 955752290Sobrien case class_type: 955852290Sobrien return "class"; 955952290Sobrien case union_type: 9560161660Skan return "union"; 956152290Sobrien case enum_type: 956252290Sobrien return "enum"; 9563161660Skan case typename_type: 9564161660Skan return "typename"; 956552290Sobrien default: 9566169699Skan gcc_unreachable (); 956752290Sobrien } 956852290Sobrien} 956952290Sobrien 9570117410Skan/* Name lookup in an elaborated-type-specifier (after the keyword 9571132747Skan indicated by TAG_CODE) has found the TYPE_DECL DECL. If the 9572117410Skan elaborated-type-specifier is invalid, issue a diagnostic and return 9573132747Skan error_mark_node; otherwise, return the *_TYPE to which it referred. 9574132747Skan If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */ 9575117410Skan 9576132747Skantree 9577117410Skancheck_elaborated_type_specifier (enum tag_types tag_code, 9578132747Skan tree decl, 9579132747Skan bool allow_template_p) 9580117410Skan{ 9581132747Skan tree type; 9582117410Skan 9583132747Skan /* In the case of: 9584117410Skan 9585132747Skan struct S { struct S *p; }; 9586132747Skan 9587132747Skan name lookup will find the TYPE_DECL for the implicit "S::S" 9588132747Skan typedef. Adjust for that here. */ 9589132747Skan if (DECL_SELF_REFERENCE_P (decl)) 9590132747Skan decl = TYPE_NAME (TREE_TYPE (decl)); 9591132747Skan 9592132747Skan type = TREE_TYPE (decl); 9593132747Skan 9594169699Skan /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P 9595169699Skan is false for this case as well. */ 9596169699Skan if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 9597169699Skan { 9598169699Skan error ("using template type parameter %qT after %qs", 9599169699Skan type, tag_name (tag_code)); 9600169699Skan return error_mark_node; 9601169699Skan } 9602169699Skan /* [dcl.type.elab] 9603132747Skan 9604132747Skan If the identifier resolves to a typedef-name or a template 9605132747Skan type-parameter, the elaborated-type-specifier is ill-formed. 9606132747Skan 9607132747Skan In other words, the only legitimate declaration to use in the 9608132747Skan elaborated type specifier is the implicit typedef created when 9609132747Skan the type is declared. */ 9610169699Skan else if (!DECL_IMPLICIT_TYPEDEF_P (decl) 9611169699Skan && tag_code != typename_type) 9612117410Skan { 9613169699Skan error ("using typedef-name %qD after %qs", decl, tag_name (tag_code)); 9614169699Skan error ("%q+D has a previous declaration here", decl); 9615132747Skan return error_mark_node; 9616117410Skan } 9617132747Skan else if (TREE_CODE (type) != RECORD_TYPE 9618132747Skan && TREE_CODE (type) != UNION_TYPE 9619161660Skan && tag_code != enum_type 9620161660Skan && tag_code != typename_type) 9621132747Skan { 9622169699Skan error ("%qT referred to as %qs", type, tag_name (tag_code)); 9623169699Skan error ("%q+T has a previous declaration here", type); 9624132747Skan return error_mark_node; 9625132747Skan } 9626132747Skan else if (TREE_CODE (type) != ENUMERAL_TYPE 9627132747Skan && tag_code == enum_type) 9628132747Skan { 9629169699Skan error ("%qT referred to as enum", type); 9630169699Skan error ("%q+T has a previous declaration here", type); 9631132747Skan return error_mark_node; 9632132747Skan } 9633132747Skan else if (!allow_template_p 9634132747Skan && TREE_CODE (type) == RECORD_TYPE 9635132747Skan && CLASSTYPE_IS_TEMPLATE (type)) 9636132747Skan { 9637132747Skan /* If a class template appears as elaborated type specifier 9638132747Skan without a template header such as: 9639117410Skan 9640132747Skan template <class T> class C {}; 9641132747Skan void f(class C); // No template header here 9642132747Skan 9643132747Skan then the required template argument is missing. */ 9644169699Skan error ("template argument required for %<%s %T%>", 9645132747Skan tag_name (tag_code), 9646132747Skan DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))); 9647132747Skan return error_mark_node; 9648132747Skan } 9649132747Skan 9650132747Skan return type; 9651117410Skan} 9652117410Skan 9653169699Skan/* Lookup NAME in elaborate type specifier in scope according to 9654169699Skan SCOPE and issue diagnostics if necessary. 9655169699Skan Return *_TYPE node upon success, NULL_TREE when the NAME is not 9656169699Skan found, and ERROR_MARK_NODE for type error. */ 9657169699Skan 9658169699Skanstatic tree 9659169699Skanlookup_and_check_tag (enum tag_types tag_code, tree name, 9660169699Skan tag_scope scope, bool template_header_p) 9661169699Skan{ 9662169699Skan tree t; 9663169699Skan tree decl; 9664169699Skan if (scope == ts_global) 9665169699Skan { 9666169699Skan /* First try ordinary name lookup, ignoring hidden class name 9667169699Skan injected via friend declaration. */ 9668169699Skan decl = lookup_name_prefer_type (name, 2); 9669169699Skan /* If that fails, the name will be placed in the smallest 9670169699Skan non-class, non-function-prototype scope according to 3.3.1/5. 9671169699Skan We may already have a hidden name declared as friend in this 9672169699Skan scope. So lookup again but not ignoring hidden names. 9673169699Skan If we find one, that name will be made visible rather than 9674169699Skan creating a new tag. */ 9675169699Skan if (!decl) 9676169699Skan decl = lookup_type_scope (name, ts_within_enclosing_non_class); 9677169699Skan } 9678169699Skan else 9679169699Skan decl = lookup_type_scope (name, scope); 9680169699Skan 9681169699Skan if (decl && DECL_CLASS_TEMPLATE_P (decl)) 9682169699Skan decl = DECL_TEMPLATE_RESULT (decl); 9683169699Skan 9684169699Skan if (decl && TREE_CODE (decl) == TYPE_DECL) 9685169699Skan { 9686169699Skan /* Look for invalid nested type: 9687169699Skan class C { 9688169699Skan class C {}; 9689169699Skan }; */ 9690169699Skan if (scope == ts_current && DECL_SELF_REFERENCE_P (decl)) 9691169699Skan { 9692169699Skan error ("%qD has the same name as the class in which it is " 9693169699Skan "declared", 9694169699Skan decl); 9695169699Skan return error_mark_node; 9696169699Skan } 9697169699Skan 9698169699Skan /* Two cases we need to consider when deciding if a class 9699169699Skan template is allowed as an elaborated type specifier: 9700169699Skan 1. It is a self reference to its own class. 9701169699Skan 2. It comes with a template header. 9702169699Skan 9703169699Skan For example: 9704169699Skan 9705169699Skan template <class T> class C { 9706169699Skan class C *c1; // DECL_SELF_REFERENCE_P is true 9707169699Skan class D; 9708169699Skan }; 9709169699Skan template <class U> class C; // template_header_p is true 9710169699Skan template <class T> class C<T>::D { 9711169699Skan class C *c2; // DECL_SELF_REFERENCE_P is true 9712169699Skan }; */ 9713169699Skan 9714169699Skan t = check_elaborated_type_specifier (tag_code, 9715169699Skan decl, 9716169699Skan template_header_p 9717169699Skan | DECL_SELF_REFERENCE_P (decl)); 9718169699Skan return t; 9719169699Skan } 9720169699Skan else 9721169699Skan return NULL_TREE; 9722169699Skan} 9723169699Skan 9724132747Skan/* Get the struct, enum or union (TAG_CODE says which) with tag NAME. 972518334Speter Define the tag as a forward-reference if it is not defined. 972618334Speter 9727132747Skan If a declaration is given, process it here, and report an error if 9728132747Skan multiple declarations are not identical. 972918334Speter 9730169699Skan SCOPE is TS_CURRENT when this is also a definition. Only look in 973118334Speter the current frame for the name (since C++ allows new names in any 9732169699Skan scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend 9733169699Skan declaration. Only look beginning from the current scope outward up 9734169699Skan till the nearest non-class scope. Otherwise it is TS_GLOBAL. 973518334Speter 9736132747Skan TEMPLATE_HEADER_P is true when this declaration is preceded by 9737132747Skan a set of template parameters. */ 9738132747Skan 973918334Spetertree 9740132747Skanxref_tag (enum tag_types tag_code, tree name, 9741169699Skan tag_scope scope, bool template_header_p) 974218334Speter{ 974318334Speter enum tree_code code; 9744132747Skan tree t; 974552290Sobrien tree context = NULL_TREE; 974618334Speter 9747117410Skan timevar_push (TV_NAME_LOOKUP); 974851412Sobrien 9749169699Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 9750132747Skan 975118334Speter switch (tag_code) 975218334Speter { 975318334Speter case record_type: 975418334Speter case class_type: 975518334Speter code = RECORD_TYPE; 975618334Speter break; 975718334Speter case union_type: 975818334Speter code = UNION_TYPE; 975918334Speter break; 976018334Speter case enum_type: 976118334Speter code = ENUMERAL_TYPE; 976218334Speter break; 976318334Speter default: 9764169699Skan gcc_unreachable (); 976518334Speter } 976618334Speter 9767169699Skan /* In case of anonymous name, xref_tag is only called to 9768169699Skan make type node and push name. Name lookup is not required. */ 9769169699Skan if (ANON_AGGRNAME_P (name)) 9770169699Skan t = NULL_TREE; 977118334Speter else 9772169699Skan t = lookup_and_check_tag (tag_code, name, 9773169699Skan scope, template_header_p); 977490287Sobrien 9775169699Skan if (t == error_mark_node) 9776169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 9777132747Skan 9778169699Skan if (scope != ts_current && t && current_class_type 9779169699Skan && template_class_depth (current_class_type) 9780169699Skan && template_header_p) 9781169699Skan { 9782169699Skan /* Since SCOPE is not TS_CURRENT, we are not looking at a 9783169699Skan definition of this tag. Since, in addition, we are currently 9784169699Skan processing a (member) template declaration of a template 9785169699Skan class, we must be very careful; consider: 978651412Sobrien 9787169699Skan template <class X> 9788169699Skan struct S1 978951412Sobrien 9790169699Skan template <class U> 9791169699Skan struct S2 9792169699Skan { template <class V> 9793169699Skan friend struct S1; }; 9794132747Skan 9795169699Skan Here, the S2::S1 declaration should not be confused with the 9796169699Skan outer declaration. In particular, the inner version should 9797169699Skan have a template parameter of level 2, not level 1. This 9798169699Skan would be particularly important if the member declaration 9799169699Skan were instead: 980052290Sobrien 9801169699Skan template <class V = U> friend struct S1; 980252290Sobrien 9803169699Skan say, when we should tsubst into `U' when instantiating 9804169699Skan S2. On the other hand, when presented with: 980552290Sobrien 9806169699Skan template <class T> 9807169699Skan struct S1 { 9808169699Skan template <class U> 9809169699Skan struct S2 {}; 9810169699Skan template <class U> 9811169699Skan friend struct S2; 9812169699Skan }; 981352290Sobrien 9814169699Skan we must find the inner binding eventually. We 9815169699Skan accomplish this by making sure that the new type we 9816169699Skan create to represent this declaration has the right 9817169699Skan TYPE_CONTEXT. */ 9818169699Skan context = TYPE_CONTEXT (t); 9819169699Skan t = NULL_TREE; 982018334Speter } 982118334Speter 9822132747Skan if (! t) 982318334Speter { 982418334Speter /* If no such tag is yet defined, create a forward-reference node 982518334Speter and record it as the "definition". 982618334Speter When a real declaration of this type is found, 982718334Speter the forward-reference will be altered into a real type. */ 982818334Speter if (code == ENUMERAL_TYPE) 982918334Speter { 9830169699Skan error ("use of enum %q#D without previous declaration", name); 9831132747Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 983218334Speter } 983318334Speter else 983418334Speter { 9835132747Skan t = make_aggr_type (code); 9836132747Skan TYPE_CONTEXT (t) = context; 9837169699Skan t = pushtag (name, t, scope); 983818334Speter } 983918334Speter } 984018334Speter else 984118334Speter { 9842169699Skan if (template_header_p && IS_AGGR_TYPE (t)) 9843169699Skan { 9844169699Skan if (!redeclare_class_template (t, current_template_parms)) 9845169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 9846169699Skan } 9847169699Skan else if (!processing_template_decl 9848132747Skan && CLASS_TYPE_P (t) 9849132747Skan && CLASSTYPE_IS_TEMPLATE (t)) 9850132747Skan { 9851169699Skan error ("redeclaration of %qT as a non-template", t); 9852169699Skan error ("previous declaration %q+D", t); 9853169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 9854132747Skan } 9855169699Skan 9856169699Skan /* Make injected friend class visible. */ 9857169699Skan if (scope != ts_within_enclosing_non_class 9858169699Skan && hidden_name_p (TYPE_NAME (t))) 9859169699Skan { 9860169699Skan DECL_ANTICIPATED (TYPE_NAME (t)) = 0; 9861169699Skan DECL_FRIEND_P (TYPE_NAME (t)) = 0; 9862169699Skan 9863169699Skan if (TYPE_TEMPLATE_INFO (t)) 9864169699Skan { 9865169699Skan DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0; 9866169699Skan DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0; 9867169699Skan } 9868169699Skan } 986918334Speter } 987018334Speter 9871132747Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 987218334Speter} 987318334Speter 987451412Sobrientree 9875169699Skanxref_tag_from_type (tree old, tree id, tag_scope scope) 987651412Sobrien{ 9877117410Skan enum tag_types tag_kind; 987851412Sobrien 987951412Sobrien if (TREE_CODE (old) == RECORD_TYPE) 9880117410Skan tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type); 988151412Sobrien else 9882117410Skan tag_kind = union_type; 988351412Sobrien 988451412Sobrien if (id == NULL_TREE) 988551412Sobrien id = TYPE_IDENTIFIER (old); 988651412Sobrien 9887169699Skan return xref_tag (tag_kind, id, scope, false); 988851412Sobrien} 988951412Sobrien 9890169699Skan/* Create the binfo hierarchy for REF with (possibly NULL) base list 9891169699Skan BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an 9892169699Skan access_* node, and the TREE_VALUE is the type of the base-class. 9893169699Skan Non-NULL TREE_TYPE indicates virtual inheritance. 9894169699Skan 9895169699Skan Returns true if the binfo heirarchy was successfully created, 9896169699Skan false if an error was detected. */ 989752290Sobrien 9898169699Skanbool 9899132747Skanxref_basetypes (tree ref, tree base_list) 990018334Speter{ 9901132747Skan tree *basep; 9902169699Skan tree binfo, base_binfo; 9903169699Skan unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */ 9904169699Skan unsigned max_bases = 0; /* Maximum direct bases. */ 9905132747Skan int i; 9906169699Skan tree default_access; 9907169699Skan tree igo_prev; /* Track Inheritance Graph Order. */ 990818334Speter 9909132747Skan if (ref == error_mark_node) 9910169699Skan return false; 9911132747Skan 9912169699Skan /* The base of a derived class is private by default, all others are 9913169699Skan public. */ 9914169699Skan default_access = (TREE_CODE (ref) == RECORD_TYPE 9915169699Skan && CLASSTYPE_DECLARED_CLASS (ref) 9916169699Skan ? access_private_node : access_public_node); 991718334Speter 991852290Sobrien /* First, make sure that any templates in base-classes are 991952290Sobrien instantiated. This ensures that if we call ourselves recursively 992052290Sobrien we do not get confused about which classes are marked and which 992152290Sobrien are not. */ 9922169699Skan basep = &base_list; 9923169699Skan while (*basep) 9924132747Skan { 9925132747Skan tree basetype = TREE_VALUE (*basep); 9926169699Skan 9927132747Skan if (!(processing_template_decl && uses_template_parms (basetype)) 9928132747Skan && !complete_type_or_else (basetype, NULL)) 9929132747Skan /* An incomplete type. Remove it from the list. */ 9930132747Skan *basep = TREE_CHAIN (*basep); 9931132747Skan else 9932169699Skan { 9933169699Skan max_bases++; 9934169699Skan if (TREE_TYPE (*basep)) 9935169699Skan max_vbases++; 9936169699Skan if (CLASS_TYPE_P (basetype)) 9937169699Skan max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype)); 9938169699Skan basep = &TREE_CHAIN (*basep); 9939169699Skan } 9940132747Skan } 994152290Sobrien 9942169699Skan TYPE_MARKED_P (ref) = 1; 9943169699Skan 9944169699Skan /* The binfo slot should be empty, unless this is an (ill-formed) 9945169699Skan redefinition. */ 9946169699Skan gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref)); 9947169699Skan gcc_assert (TYPE_MAIN_VARIANT (ref) == ref); 9948169699Skan 9949169699Skan binfo = make_tree_binfo (max_bases); 9950169699Skan 9951169699Skan TYPE_BINFO (ref) = binfo; 9952169699Skan BINFO_OFFSET (binfo) = size_zero_node; 9953169699Skan BINFO_TYPE (binfo) = ref; 9954169699Skan 9955169699Skan if (max_bases) 995618334Speter { 9957169699Skan BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases); 9958169699Skan /* An aggregate cannot have baseclasses. */ 9959169699Skan CLASSTYPE_NON_AGGREGATE (ref) = 1; 9960169699Skan 9961169699Skan if (TREE_CODE (ref) == UNION_TYPE) 9962169699Skan { 9963169699Skan error ("derived union %qT invalid", ref); 9964169699Skan return false; 9965169699Skan } 9966169699Skan } 9967169699Skan 9968169699Skan if (max_bases > 1) 9969169699Skan { 9970169699Skan if (TYPE_FOR_JAVA (ref)) 9971169699Skan { 9972169699Skan error ("Java class %qT cannot have multiple bases", ref); 9973169699Skan return false; 9974169699Skan } 9975169699Skan } 9976169699Skan 9977169699Skan if (max_vbases) 9978169699Skan { 9979169699Skan CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases); 9980169699Skan 9981169699Skan if (TYPE_FOR_JAVA (ref)) 9982169699Skan { 9983169699Skan error ("Java class %qT cannot have virtual bases", ref); 9984169699Skan return false; 9985169699Skan } 9986169699Skan } 9987169699Skan 9988169699Skan for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list)) 9989169699Skan { 9990169699Skan tree access = TREE_PURPOSE (base_list); 9991169699Skan int via_virtual = TREE_TYPE (base_list) != NULL_TREE; 9992169699Skan tree basetype = TREE_VALUE (base_list); 9993169699Skan 9994169699Skan if (access == access_default_node) 9995169699Skan access = default_access; 9996169699Skan 9997169699Skan if (TREE_CODE (basetype) == TYPE_DECL) 9998169699Skan basetype = TREE_TYPE (basetype); 9999169699Skan if (TREE_CODE (basetype) != RECORD_TYPE 10000169699Skan && TREE_CODE (basetype) != TYPENAME_TYPE 10001169699Skan && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM 10002169699Skan && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM) 1000318334Speter { 10004169699Skan error ("base type %qT fails to be a struct or class type", 10005169699Skan basetype); 10006169699Skan return false; 1000718334Speter } 10008169699Skan 10009169699Skan if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0)) 10010169699Skan TYPE_FOR_JAVA (ref) = 1; 10011169699Skan 10012169699Skan base_binfo = NULL_TREE; 10013169699Skan if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype)) 10014132747Skan { 10015169699Skan base_binfo = TYPE_BINFO (basetype); 10016169699Skan /* The original basetype could have been a typedef'd type. */ 10017169699Skan basetype = BINFO_TYPE (base_binfo); 10018169699Skan 10019169699Skan /* Inherit flags from the base. */ 10020169699Skan TYPE_HAS_NEW_OPERATOR (ref) 10021169699Skan |= TYPE_HAS_NEW_OPERATOR (basetype); 10022169699Skan TYPE_HAS_ARRAY_NEW_OPERATOR (ref) 10023169699Skan |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); 10024169699Skan TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); 10025169699Skan TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype); 10026169699Skan CLASSTYPE_DIAMOND_SHAPED_P (ref) 10027169699Skan |= CLASSTYPE_DIAMOND_SHAPED_P (basetype); 10028169699Skan CLASSTYPE_REPEATED_BASE_P (ref) 10029169699Skan |= CLASSTYPE_REPEATED_BASE_P (basetype); 10030132747Skan } 1003118334Speter 10032169699Skan /* We must do this test after we've seen through a typedef 10033169699Skan type. */ 10034169699Skan if (TYPE_MARKED_P (basetype)) 10035169699Skan { 10036169699Skan if (basetype == ref) 10037169699Skan error ("recursive type %qT undefined", basetype); 10038169699Skan else 10039169699Skan error ("duplicate base type %qT invalid", basetype); 10040169699Skan return false; 10041169699Skan } 10042169699Skan TYPE_MARKED_P (basetype) = 1; 10043169699Skan 10044169699Skan base_binfo = copy_binfo (base_binfo, basetype, ref, 10045169699Skan &igo_prev, via_virtual); 10046169699Skan if (!BINFO_INHERITANCE_CHAIN (base_binfo)) 10047169699Skan BINFO_INHERITANCE_CHAIN (base_binfo) = binfo; 10048169699Skan 10049169699Skan BINFO_BASE_APPEND (binfo, base_binfo); 10050169699Skan BINFO_BASE_ACCESS_APPEND (binfo, access); 1005152290Sobrien } 1005252290Sobrien 10053169699Skan if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1)) 10054169699Skan /* If we have space in the vbase vector, we must have shared at 10055169699Skan least one of them, and are therefore diamond shaped. */ 10056169699Skan CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1; 10057169699Skan 1005818334Speter /* Unmark all the types. */ 10059169699Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 10060169699Skan TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; 10061169699Skan TYPE_MARKED_P (ref) = 0; 10062169699Skan 10063169699Skan /* Now see if we have a repeated base type. */ 10064169699Skan if (!CLASSTYPE_REPEATED_BASE_P (ref)) 10065132747Skan { 10066169699Skan for (base_binfo = binfo; base_binfo; 10067169699Skan base_binfo = TREE_CHAIN (base_binfo)) 10068132747Skan { 10069169699Skan if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) 10070169699Skan { 10071169699Skan CLASSTYPE_REPEATED_BASE_P (ref) = 1; 10072169699Skan break; 10073169699Skan } 10074169699Skan TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1; 10075132747Skan } 10076169699Skan for (base_binfo = binfo; base_binfo; 10077169699Skan base_binfo = TREE_CHAIN (base_binfo)) 10078169699Skan if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) 10079169699Skan TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; 10080169699Skan else 10081169699Skan break; 10082132747Skan } 10083169699Skan 10084169699Skan return true; 1008590287Sobrien} 1008652290Sobrien 1008718334Speter 1008818334Speter/* Begin compiling the definition of an enumeration type. 10089169699Skan NAME is its name. 1009018334Speter Returns the type object, as yet incomplete. 1009118334Speter Also records info about it so that build_enumerator 1009218334Speter may be used to declare the individual values as they are read. */ 1009318334Speter 1009418334Spetertree 10095132747Skanstart_enum (tree name) 1009618334Speter{ 10097169699Skan tree enumtype; 1009818334Speter 10099169699Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 10100169699Skan 1010118334Speter /* If this is the real definition for a previous forward reference, 1010218334Speter fill in the contents in the same object that used to be the 1010318334Speter forward reference. */ 1010418334Speter 10105169699Skan enumtype = lookup_and_check_tag (enum_type, name, 10106169699Skan /*tag_scope=*/ts_current, 10107169699Skan /*template_header_p=*/false); 1010818334Speter 1010918334Speter if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE) 1011090287Sobrien { 10111169699Skan error ("multiple definition of %q#T", enumtype); 10112132747Skan error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype)); 1011390287Sobrien /* Clear out TYPE_VALUES, and start again. */ 1011490287Sobrien TYPE_VALUES (enumtype) = NULL_TREE; 1011590287Sobrien } 1011618334Speter else 1011718334Speter { 10118169699Skan /* In case of error, make a dummy enum to allow parsing to 10119169699Skan continue. */ 10120169699Skan if (enumtype == error_mark_node) 10121169699Skan name = make_anon_name (); 10122169699Skan 1012318334Speter enumtype = make_node (ENUMERAL_TYPE); 10124169699Skan enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current); 1012518334Speter } 1012618334Speter 1012718334Speter return enumtype; 1012818334Speter} 1012918334Speter 1013018334Speter/* After processing and defining all the values of an enumeration type, 1013118334Speter install their decls in the enumeration type and finish it off. 1013290287Sobrien ENUMTYPE is the type object and VALUES a list of name-value pairs. */ 1013318334Speter 1013490287Sobrienvoid 10135132747Skanfinish_enum (tree enumtype) 1013618334Speter{ 10137132747Skan tree values; 10138132747Skan tree decl; 10139132747Skan tree value; 1014090287Sobrien tree minnode; 1014190287Sobrien tree maxnode; 1014290287Sobrien tree t; 1014390287Sobrien bool unsignedp; 10144169699Skan bool use_short_enum; 1014590287Sobrien int lowprec; 10146169699Skan int highprec; 1014790287Sobrien int precision; 10148132747Skan integer_type_kind itk; 10149132747Skan tree underlying_type = NULL_TREE; 1015018334Speter 1015190287Sobrien /* We built up the VALUES in reverse order. */ 1015290287Sobrien TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); 1015390287Sobrien 10154117410Skan /* For an enum defined in a template, just set the type of the values; 10155117410Skan all further processing is postponed until the template is 10156117410Skan instantiated. We need to set the type so that tsubst of a CONST_DECL 10157117410Skan works. */ 1015890287Sobrien if (processing_template_decl) 1015918334Speter { 10160169699Skan for (values = TYPE_VALUES (enumtype); 10161169699Skan values; 10162132747Skan values = TREE_CHAIN (values)) 10163132747Skan TREE_TYPE (TREE_VALUE (values)) = enumtype; 10164117410Skan if (at_function_scope_p ()) 1016590287Sobrien add_stmt (build_min (TAG_DEFN, enumtype)); 1016690287Sobrien return; 1016790287Sobrien } 1016852290Sobrien 10169132747Skan /* Determine the minimum and maximum values of the enumerators. */ 1017090287Sobrien if (TYPE_VALUES (enumtype)) 1017190287Sobrien { 1017290287Sobrien minnode = maxnode = NULL_TREE; 1017352290Sobrien 10174169699Skan for (values = TYPE_VALUES (enumtype); 10175169699Skan values; 10176132747Skan values = TREE_CHAIN (values)) 1017790287Sobrien { 10178132747Skan decl = TREE_VALUE (values); 1017952290Sobrien 10180117410Skan /* [dcl.enum]: Following the closing brace of an enum-specifier, 10181117410Skan each enumerator has the type of its enumeration. Prior to the 10182117410Skan closing brace, the type of each enumerator is the type of its 10183117410Skan initializing value. */ 10184117410Skan TREE_TYPE (decl) = enumtype; 1018552290Sobrien 10186132747Skan /* Update the minimum and maximum values, if appropriate. */ 10187132747Skan value = DECL_INITIAL (decl); 10188169699Skan if (value == error_mark_node) 10189169699Skan value = integer_zero_node; 10190117410Skan /* Figure out what the minimum and maximum values of the 10191117410Skan enumerators are. */ 1019290287Sobrien if (!minnode) 1019390287Sobrien minnode = maxnode = value; 1019490287Sobrien else if (tree_int_cst_lt (maxnode, value)) 1019590287Sobrien maxnode = value; 1019690287Sobrien else if (tree_int_cst_lt (value, minnode)) 1019790287Sobrien minnode = value; 1019818334Speter } 1019918334Speter } 1020018334Speter else 10201132747Skan /* [dcl.enum] 10202132747Skan 10203132747Skan If the enumerator-list is empty, the underlying type is as if 10204132747Skan the enumeration had a single enumerator with value 0. */ 1020590287Sobrien minnode = maxnode = integer_zero_node; 1020618334Speter 1020790287Sobrien /* Compute the number of bits require to represent all values of the 1020890287Sobrien enumeration. We must do this before the type of MINNODE and 1020990287Sobrien MAXNODE are transformed, since min_precision relies on the 1021090287Sobrien TREE_TYPE of the value it is passed. */ 1021190287Sobrien unsignedp = tree_int_cst_sgn (minnode) >= 0; 1021290287Sobrien lowprec = min_precision (minnode, unsignedp); 1021390287Sobrien highprec = min_precision (maxnode, unsignedp); 1021490287Sobrien precision = MAX (lowprec, highprec); 1021518334Speter 10216132747Skan /* Determine the underlying type of the enumeration. 10217132747Skan 10218132747Skan [dcl.enum] 10219132747Skan 10220132747Skan The underlying type of an enumeration is an integral type that 10221132747Skan can represent all the enumerator values defined in the 10222132747Skan enumeration. It is implementation-defined which integral type is 10223132747Skan used as the underlying type for an enumeration except that the 10224132747Skan underlying type shall not be larger than int unless the value of 10225169699Skan an enumerator cannot fit in an int or unsigned int. 10226132747Skan 10227132747Skan We use "int" or an "unsigned int" as the underlying type, even if 10228132747Skan a smaller integral type would work, unless the user has 10229169699Skan explicitly requested that we use the smallest possible type. The 10230169699Skan user can request that for all enumerations with a command line 10231169699Skan flag, or for just one enumeration with an attribute. */ 10232169699Skan 10233169699Skan use_short_enum = flag_short_enums 10234169699Skan || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype)); 10235169699Skan 10236169699Skan for (itk = (use_short_enum ? itk_char : itk_int); 10237169699Skan itk != itk_none; 10238132747Skan itk++) 1023951412Sobrien { 10240132747Skan underlying_type = integer_types[itk]; 10241132747Skan if (TYPE_PRECISION (underlying_type) >= precision 10242169699Skan && TYPE_UNSIGNED (underlying_type) == unsignedp) 10243132747Skan break; 10244132747Skan } 10245132747Skan if (itk == itk_none) 10246132747Skan { 10247132747Skan /* DR 377 10248132747Skan 10249132747Skan IF no integral type can represent all the enumerator values, the 10250132747Skan enumeration is ill-formed. */ 10251117410Skan error ("no integral type can represent all of the enumerator values " 10252169699Skan "for %qT", enumtype); 10253117410Skan precision = TYPE_PRECISION (long_long_integer_type_node); 10254132747Skan underlying_type = integer_types[itk_unsigned_long_long]; 1025551412Sobrien } 1025690287Sobrien 10257169699Skan /* Compute the minium and maximum values for the type. 10258132747Skan 10259132747Skan [dcl.enum] 10260132747Skan 10261132747Skan For an enumeration where emin is the smallest enumerator and emax 10262132747Skan is the largest, the values of the enumeration are the values of the 10263132747Skan underlying type in the range bmin to bmax, where bmin and bmax are, 10264132747Skan respectively, the smallest and largest values of the smallest bit- 10265132747Skan field that can store emin and emax. */ 10266146906Skan 10267146906Skan /* The middle-end currently assumes that types with TYPE_PRECISION 10268146906Skan narrower than their underlying type are suitably zero or sign 10269146906Skan extended to fill their mode. g++ doesn't make these guarantees. 10270146906Skan Until the middle-end can represent such paradoxical types, we 10271169699Skan set the TYPE_PRECISION to the width of the underlying type. */ 10272146906Skan TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type); 10273146906Skan 10274132747Skan set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp); 1027551412Sobrien 10276132747Skan /* [dcl.enum] 10277169699Skan 10278132747Skan The value of sizeof() applied to an enumeration type, an object 10279132747Skan of an enumeration type, or an enumerator, is the value of sizeof() 10280132747Skan applied to the underlying type. */ 10281132747Skan TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type); 10282132747Skan TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type); 10283132747Skan TYPE_MODE (enumtype) = TYPE_MODE (underlying_type); 10284132747Skan TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type); 10285132747Skan TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type); 10286169699Skan TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type); 1028718334Speter 10288132747Skan /* Convert each of the enumerators to the type of the underlying 10289132747Skan type of the enumeration. */ 10290132747Skan for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) 10291132747Skan { 10292169699Skan location_t saved_location; 10293169699Skan 10294132747Skan decl = TREE_VALUE (values); 10295169699Skan saved_location = input_location; 10296169699Skan input_location = DECL_SOURCE_LOCATION (decl); 10297132747Skan value = perform_implicit_conversion (underlying_type, 10298132747Skan DECL_INITIAL (decl)); 10299169699Skan input_location = saved_location; 10300169699Skan 10301169699Skan /* Do not clobber shared ints. */ 10302169699Skan value = copy_node (value); 10303169699Skan 10304132747Skan TREE_TYPE (value) = enumtype; 10305132747Skan DECL_INITIAL (decl) = value; 10306132747Skan TREE_VALUE (values) = value; 10307132747Skan } 10308119270Skan 1030990287Sobrien /* Fix up all variant types of this enum type. */ 1031090287Sobrien for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) 1031190287Sobrien { 1031290287Sobrien TYPE_VALUES (t) = TYPE_VALUES (enumtype); 1031390287Sobrien TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype); 1031490287Sobrien TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype); 1031590287Sobrien TYPE_SIZE (t) = TYPE_SIZE (enumtype); 1031690287Sobrien TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype); 1031790287Sobrien TYPE_MODE (t) = TYPE_MODE (enumtype); 1031890287Sobrien TYPE_PRECISION (t) = TYPE_PRECISION (enumtype); 1031990287Sobrien TYPE_ALIGN (t) = TYPE_ALIGN (enumtype); 1032090287Sobrien TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype); 10321169699Skan TYPE_UNSIGNED (t) = TYPE_UNSIGNED (enumtype); 1032252290Sobrien } 1032318334Speter 1032490287Sobrien /* Finish debugging output for this type. */ 1032590287Sobrien rest_of_type_compilation (enumtype, namespace_bindings_p ()); 1032618334Speter} 1032718334Speter 1032852290Sobrien/* Build and install a CONST_DECL for an enumeration constant of the 1032990287Sobrien enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. 1033018334Speter Assignment of sequential values by default is handled here. */ 1033118334Speter 1033290287Sobrienvoid 10333132747Skanbuild_enumerator (tree name, tree value, tree enumtype) 1033418334Speter{ 1033590287Sobrien tree decl; 1033652290Sobrien tree context; 1033790287Sobrien tree type; 1033818334Speter 10339169699Skan /* If the VALUE was erroneous, pretend it wasn't there; that will 10340169699Skan result in the enum being assigned the next value in sequence. */ 10341169699Skan if (value == error_mark_node) 10342169699Skan value = NULL_TREE; 10343169699Skan 1034418334Speter /* Remove no-op casts from the value. */ 1034518334Speter if (value) 1034618334Speter STRIP_TYPE_NOPS (value); 1034718334Speter 1034890287Sobrien if (! processing_template_decl) 1034990287Sobrien { 1035090287Sobrien /* Validate and default VALUE. */ 1035190287Sobrien if (value != NULL_TREE) 1035290287Sobrien { 10353169699Skan value = integral_constant_value (value); 1035418334Speter 1035590287Sobrien if (TREE_CODE (value) == INTEGER_CST) 1035690287Sobrien { 10357132747Skan value = perform_integral_promotions (value); 1035890287Sobrien constant_expression_warning (value); 1035990287Sobrien } 1036090287Sobrien else 1036190287Sobrien { 10362169699Skan error ("enumerator value for %qD not integer constant", name); 1036390287Sobrien value = NULL_TREE; 1036490287Sobrien } 1036590287Sobrien } 1036618334Speter 1036790287Sobrien /* Default based on previous value. */ 10368132747Skan if (value == NULL_TREE) 1036990287Sobrien { 1037090287Sobrien if (TYPE_VALUES (enumtype)) 1037190287Sobrien { 10372169699Skan HOST_WIDE_INT hi; 10373169699Skan unsigned HOST_WIDE_INT lo; 10374169699Skan tree prev_value; 10375169699Skan bool overflowed; 10376169699Skan 10377169699Skan /* The next value is the previous value plus one. We can 10378169699Skan safely assume that the previous value is an INTEGER_CST. 10379169699Skan add_double doesn't know the type of the target expression, 10380169699Skan so we must check with int_fits_type_p as well. */ 1038190287Sobrien prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); 10382169699Skan overflowed = add_double (TREE_INT_CST_LOW (prev_value), 10383169699Skan TREE_INT_CST_HIGH (prev_value), 10384169699Skan 1, 0, &lo, &hi); 10385169699Skan value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi); 10386169699Skan overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value)); 1038790287Sobrien 10388169699Skan if (overflowed) 10389169699Skan { 10390169699Skan error ("overflow in enumeration values at %qD", name); 10391169699Skan value = error_mark_node; 10392169699Skan } 1039390287Sobrien } 1039490287Sobrien else 1039590287Sobrien value = integer_zero_node; 1039690287Sobrien } 1039790287Sobrien 1039890287Sobrien /* Remove no-op casts from the value. */ 10399117410Skan STRIP_TYPE_NOPS (value); 1040090287Sobrien } 1040118334Speter 1040218334Speter /* C++ associates enums with global, function, or class declarations. */ 1040390287Sobrien context = current_scope (); 1040490287Sobrien 1040590287Sobrien /* Build the actual enumeration constant. Note that the enumeration 1040690287Sobrien constants have the type of their initializers until the 1040790287Sobrien enumeration is complete: 1040890287Sobrien 1040990287Sobrien [ dcl.enum ] 1041090287Sobrien 1041190287Sobrien Following the closing brace of an enum-specifier, each enumer- 1041290287Sobrien ator has the type of its enumeration. Prior to the closing 1041390287Sobrien brace, the type of each enumerator is the type of its 1041490287Sobrien initializing value. 1041590287Sobrien 1041690287Sobrien In finish_enum we will reset the type. Of course, if we're 10417117410Skan processing a template, there may be no value. */ 1041890287Sobrien type = value ? TREE_TYPE (value) : NULL_TREE; 1041990287Sobrien 1042090287Sobrien if (context && context == current_class_type) 1042190287Sobrien /* This enum declaration is local to the class. We need the full 1042290287Sobrien lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */ 1042390287Sobrien decl = build_lang_decl (CONST_DECL, name, type); 1042490287Sobrien else 1042590287Sobrien /* It's a global enum, or it's local to a function. (Note local to 1042652290Sobrien a function could mean local to a class method. */ 1042790287Sobrien decl = build_decl (CONST_DECL, name, type); 1042818334Speter 1042990287Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (context); 10430169699Skan TREE_CONSTANT (decl) = 1; 10431169699Skan TREE_INVARIANT (decl) = 1; 10432169699Skan TREE_READONLY (decl) = 1; 1043390287Sobrien DECL_INITIAL (decl) = value; 1043418334Speter 1043590287Sobrien if (context && context == current_class_type) 1043690287Sobrien /* In something like `struct S { enum E { i = 7 }; };' we put `i' 10437132747Skan on the TYPE_FIELDS list for `S'. (That's so that you can say 10438132747Skan things like `S::i' later.) */ 1043990287Sobrien finish_member_declaration (decl); 1044090287Sobrien else 1044196292Sobrien pushdecl (decl); 1044218334Speter 1044390287Sobrien /* Add this enumeration constant to the list for this type. */ 1044490287Sobrien TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); 1044518334Speter} 1044618334Speter 1044718334Speter 1044890287Sobrien/* We're defining DECL. Make sure that it's type is OK. */ 1044951412Sobrien 1045090287Sobrienstatic void 10451132747Skancheck_function_type (tree decl, tree current_function_parms) 1045290287Sobrien{ 1045390287Sobrien tree fntype = TREE_TYPE (decl); 1045490287Sobrien tree return_type = complete_type (TREE_TYPE (fntype)); 1045590287Sobrien 1045690287Sobrien /* In a function definition, arg types must be complete. */ 1045790287Sobrien require_complete_types_for_parms (current_function_parms); 1045890287Sobrien 10459169699Skan if (dependent_type_p (return_type)) 10460169699Skan return; 1046190287Sobrien if (!COMPLETE_OR_VOID_TYPE_P (return_type)) 1046290287Sobrien { 10463169699Skan tree args = TYPE_ARG_TYPES (fntype); 1046490287Sobrien 10465169699Skan error ("return type %q#T is incomplete", return_type); 10466169699Skan 10467169699Skan /* Make it return void instead. */ 1046890287Sobrien if (TREE_CODE (fntype) == METHOD_TYPE) 10469169699Skan fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)), 10470169699Skan void_type_node, 10471169699Skan TREE_CHAIN (args)); 1047290287Sobrien else 10473169699Skan fntype = build_function_type (void_type_node, args); 1047490287Sobrien TREE_TYPE (decl) 1047590287Sobrien = build_exception_variant (fntype, 10476169699Skan TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl))); 1047790287Sobrien } 1047890287Sobrien else 1047990287Sobrien abstract_virtuals_error (decl, TREE_TYPE (fntype)); 1048090287Sobrien} 1048190287Sobrien 1048218334Speter/* Create the FUNCTION_DECL for a function definition. 1048318334Speter DECLSPECS and DECLARATOR are the parts of the declaration; 1048418334Speter they describe the function's name and the type it returns, 1048518334Speter but twisted together in a fashion that parallels the syntax of C. 1048618334Speter 1048790287Sobrien FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the 1048890287Sobrien DECLARATOR is really the DECL for the function we are about to 1048990287Sobrien process and that DECLSPECS should be ignored), SF_INCLASS_INLINE 1049090287Sobrien indicating that the function is an inline defined in-class. 1049190287Sobrien 1049218334Speter This function creates a binding context for the function body 1049318334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 1049418334Speter 1049518334Speter For C++, we must first check whether that datum makes any sense. 1049618334Speter For example, "class A local_a(1,2);" means that variable local_a 1049718334Speter is an aggregate of type A, which should have a constructor 1049890287Sobrien applied to it with the argument list [1, 2]. */ 1049918334Speter 10500169699Skanvoid 10501169699Skanstart_preparsed_function (tree decl1, tree attrs, int flags) 1050218334Speter{ 1050318334Speter tree ctype = NULL_TREE; 1050418334Speter tree fntype; 1050518334Speter tree restype; 1050618334Speter int doing_friend = 0; 10507117410Skan struct cp_binding_level *bl; 1050890287Sobrien tree current_function_parms; 10509169699Skan struct c_fileinfo *finfo 10510169699Skan = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1))); 10511169699Skan bool honor_interface; 1051218334Speter 1051318334Speter /* Sanity check. */ 10514169699Skan gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE); 10515169699Skan gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE); 1051618334Speter 10517169699Skan fntype = TREE_TYPE (decl1); 10518169699Skan if (TREE_CODE (fntype) == METHOD_TYPE) 10519169699Skan ctype = TYPE_METHOD_BASETYPE (fntype); 1052018334Speter 10521169699Skan /* ISO C++ 11.4/5. A friend function defined in a class is in 10522169699Skan the (lexical) scope of the class in which it is defined. */ 10523169699Skan if (!ctype && DECL_FRIEND_P (decl1)) 1052418334Speter { 10525169699Skan ctype = DECL_FRIEND_CONTEXT (decl1); 1052618334Speter 10527169699Skan /* CTYPE could be null here if we're dealing with a template; 10528169699Skan for example, `inline friend float foo()' inside a template 10529169699Skan will have no CTYPE set. */ 10530169699Skan if (ctype && TREE_CODE (ctype) != RECORD_TYPE) 10531169699Skan ctype = NULL_TREE; 10532169699Skan else 10533169699Skan doing_friend = 1; 1053418334Speter } 1053518334Speter 1053690287Sobrien if (DECL_DECLARED_INLINE_P (decl1) 1053790287Sobrien && lookup_attribute ("noinline", attrs)) 10538169699Skan warning (0, "inline function %q+D given attribute noinline", decl1); 1053990287Sobrien 1054090287Sobrien if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) 1054190287Sobrien /* This is a constructor, we must ensure that any default args 1054290287Sobrien introduced by this definition are propagated to the clones 1054390287Sobrien now. The clones are used directly in overload resolution. */ 1054490287Sobrien adjust_clone_args (decl1); 1054590287Sobrien 1054690287Sobrien /* Sometimes we don't notice that a function is a static member, and 1054790287Sobrien build a METHOD_TYPE for it. Fix that up now. */ 1054890287Sobrien if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) 1054990287Sobrien && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE) 1055090287Sobrien { 1055190287Sobrien revert_static_member_fn (decl1); 1055290287Sobrien ctype = NULL_TREE; 1055390287Sobrien } 1055490287Sobrien 1055552290Sobrien /* Set up current_class_type, and enter the scope of the class, if 1055652290Sobrien appropriate. */ 1055752290Sobrien if (ctype) 10558132747Skan push_nested_class (ctype); 1055952290Sobrien else if (DECL_STATIC_FUNCTION_P (decl1)) 10560132747Skan push_nested_class (DECL_CONTEXT (decl1)); 1056152290Sobrien 1056252290Sobrien /* Now that we have entered the scope of the class, we must restore 1056352290Sobrien the bindings for any template parameters surrounding DECL1, if it 1056452290Sobrien is an inline member template. (Order is important; consider the 1056552290Sobrien case where a template parameter has the same name as a field of 1056652290Sobrien the class.) It is not until after this point that 1056752290Sobrien PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ 1056890287Sobrien if (flags & SF_INCLASS_INLINE) 1056952290Sobrien maybe_begin_member_template_processing (decl1); 1057052290Sobrien 1057190287Sobrien /* Effective C++ rule 15. */ 1057290287Sobrien if (warn_ecpp 1057390287Sobrien && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR 1057490287Sobrien && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE) 10575169699Skan warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>"); 1057690287Sobrien 1057790287Sobrien /* Make the init_value nonzero so pushdecl knows this is not tentative. 1057890287Sobrien error_mark_node is replaced below (in poplevel) with the BLOCK. */ 1057990287Sobrien if (!DECL_INITIAL (decl1)) 1058090287Sobrien DECL_INITIAL (decl1) = error_mark_node; 1058190287Sobrien 1058290287Sobrien /* This function exists in static storage. 1058390287Sobrien (This does not mean `static' in the C sense!) */ 1058490287Sobrien TREE_STATIC (decl1) = 1; 1058590287Sobrien 1058690287Sobrien /* We must call push_template_decl after current_class_type is set 1058790287Sobrien up. (If we are processing inline definitions after exiting a 1058890287Sobrien class scope, current_class_type will be NULL_TREE until set above 1058990287Sobrien by push_nested_class.) */ 1059090287Sobrien if (processing_template_decl) 10591161660Skan { 10592169699Skan /* FIXME: Handle error_mark_node more gracefully. */ 10593161660Skan tree newdecl1 = push_template_decl (decl1); 10594161660Skan if (newdecl1 != error_mark_node) 10595161660Skan decl1 = newdecl1; 10596161660Skan } 1059790287Sobrien 1059852290Sobrien /* We are now in the scope of the function being defined. */ 1059918334Speter current_function_decl = decl1; 1060052290Sobrien 1060151412Sobrien /* Save the parm names or decls from this function's declarator 1060251412Sobrien where store_parm_decls will find them. */ 10603132747Skan current_function_parms = DECL_ARGUMENTS (decl1); 1060418334Speter 1060590287Sobrien /* Make sure the parameter and return types are reasonable. When 1060690287Sobrien you declare a function, these types can be incomplete, but they 1060790287Sobrien must be complete when you define the function. */ 10608169699Skan check_function_type (decl1, current_function_parms); 1060990287Sobrien 1061090287Sobrien /* Build the return declaration for the function. */ 1061190287Sobrien restype = TREE_TYPE (fntype); 1061296292Sobrien /* Promote the value to int before returning it. */ 1061396292Sobrien if (c_promoting_integer_type_p (restype)) 1061496292Sobrien restype = type_promotes_to (restype); 1061596292Sobrien if (DECL_RESULT (decl1) == NULL_TREE) 1061618334Speter { 10617169699Skan tree resdecl; 10618169699Skan 10619169699Skan resdecl = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); 10620169699Skan DECL_ARTIFICIAL (resdecl) = 1; 10621169699Skan DECL_IGNORED_P (resdecl) = 1; 10622169699Skan DECL_RESULT (decl1) = resdecl; 10623169699Skan 10624169699Skan cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl); 1062518334Speter } 1062618334Speter 1062790287Sobrien /* Initialize RTL machinery. We cannot do this until 1062890287Sobrien CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this 1062990287Sobrien even when processing a template; this is how we get 1063090287Sobrien CFUN set up, and our per-function variables initialized. 1063190287Sobrien FIXME factor out the non-RTL stuff. */ 1063290287Sobrien bl = current_binding_level; 10633132747Skan allocate_struct_function (decl1); 1063490287Sobrien current_binding_level = bl; 1063551412Sobrien 1063690287Sobrien /* Even though we're inside a function body, we still don't want to 1063790287Sobrien call expand_expr to calculate the size of a variable-sized array. 1063890287Sobrien We haven't necessarily assigned RTL to all variables yet, so it's 1063990287Sobrien not safe to try to expand expressions involving them. */ 1064090287Sobrien cfun->x_dont_save_pending_sizes_p = 1; 1064118334Speter 1064290287Sobrien /* Start the statement-tree, start the tree now. */ 10643169699Skan DECL_SAVED_TREE (decl1) = push_stmt_list (); 1064418334Speter 1064590287Sobrien /* Let the user know we're compiling this function. */ 1064690287Sobrien announce_function (decl1); 1064752290Sobrien 1064818334Speter /* Record the decl so that the function name is defined. 1064918334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 1065018334Speter use the old decl. */ 1065190287Sobrien if (!processing_template_decl && !(flags & SF_PRE_PARSED)) 1065218334Speter { 1065351412Sobrien /* A specialization is not used to guide overload resolution. */ 10654132747Skan if (!DECL_FUNCTION_MEMBER_P (decl1) 10655169699Skan && !(DECL_USE_TEMPLATE (decl1) && 10656132747Skan PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1)))) 10657132747Skan { 10658132747Skan tree olddecl = pushdecl (decl1); 10659132747Skan 10660132747Skan if (olddecl == error_mark_node) 10661132747Skan /* If something went wrong when registering the declaration, 10662132747Skan use DECL1; we have to have a FUNCTION_DECL to use when 10663132747Skan parsing the body of the function. */ 10664132747Skan ; 10665132747Skan else 10666132747Skan /* Otherwise, OLDDECL is either a previous declaration of 10667132747Skan the same function or DECL1 itself. */ 10668132747Skan decl1 = olddecl; 10669132747Skan } 1067051412Sobrien else 1067151412Sobrien { 10672117410Skan /* We need to set the DECL_CONTEXT. */ 1067351412Sobrien if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) 1067451412Sobrien DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); 1067551412Sobrien } 1067618334Speter fntype = TREE_TYPE (decl1); 10677169699Skan 10678169699Skan /* If #pragma weak applies, mark the decl appropriately now. 10679169699Skan The pragma only applies to global functions. Because 10680169699Skan determining whether or not the #pragma applies involves 10681169699Skan computing the mangled name for the declaration, we cannot 10682169699Skan apply the pragma until after we have merged this declaration 10683169699Skan with any previous declarations; if the original declaration 10684169699Skan has a linkage specification, that specification applies to 10685169699Skan the definition as well, and may affect the mangled name. */ 10686169699Skan if (!DECL_CONTEXT (decl1)) 10687169699Skan maybe_apply_pragma_weak (decl1); 1068818334Speter } 1068918334Speter 1069090287Sobrien /* Reset these in case the call to pushdecl changed them. */ 1069151412Sobrien current_function_decl = decl1; 1069290287Sobrien cfun->decl = decl1; 1069351412Sobrien 1069490287Sobrien /* If we are (erroneously) defining a function that we have already 1069590287Sobrien defined before, wipe out what we knew before. */ 10696117410Skan if (!DECL_PENDING_INLINE_P (decl1)) 10697117410Skan DECL_SAVED_FUNCTION_DATA (decl1) = NULL; 1069890287Sobrien 1069990287Sobrien if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) 1070090287Sobrien { 1070190287Sobrien /* We know that this was set up by `grokclassfn'. We do not 1070290287Sobrien wait until `store_parm_decls', since evil parse errors may 1070390287Sobrien never get us to that point. Here we keep the consistency 1070490287Sobrien between `current_class_type' and `current_class_ptr'. */ 1070590287Sobrien tree t = DECL_ARGUMENTS (decl1); 1070690287Sobrien 10707169699Skan gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL); 10708169699Skan gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE); 1070990287Sobrien 1071090287Sobrien cp_function_chain->x_current_class_ref 1071190287Sobrien = build_indirect_ref (t, NULL); 1071290287Sobrien cp_function_chain->x_current_class_ptr = t; 1071390287Sobrien 1071490287Sobrien /* Constructors and destructors need to know whether they're "in 1071590287Sobrien charge" of initializing virtual base classes. */ 1071690287Sobrien t = TREE_CHAIN (t); 1071790287Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) 1071890287Sobrien { 1071990287Sobrien current_in_charge_parm = t; 1072090287Sobrien t = TREE_CHAIN (t); 1072190287Sobrien } 1072290287Sobrien if (DECL_HAS_VTT_PARM_P (decl1)) 1072390287Sobrien { 10724169699Skan gcc_assert (DECL_NAME (t) == vtt_parm_identifier); 1072590287Sobrien current_vtt_parm = t; 1072690287Sobrien } 1072790287Sobrien } 1072890287Sobrien 10729169699Skan honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1) 10730169699Skan /* Implicitly-defined methods (like the 10731169699Skan destructor for a class in which no destructor 10732169699Skan is explicitly declared) must not be defined 10733169699Skan until their definition is needed. So, we 10734169699Skan ignore interface specifications for 10735169699Skan compiler-generated functions. */ 10736169699Skan && !DECL_ARTIFICIAL (decl1)); 10737169699Skan 1073818334Speter if (DECL_INTERFACE_KNOWN (decl1)) 1073918334Speter { 1074090287Sobrien tree ctx = decl_function_context (decl1); 1074151412Sobrien 1074218334Speter if (DECL_NOT_REALLY_EXTERN (decl1)) 1074318334Speter DECL_EXTERNAL (decl1) = 0; 1074451412Sobrien 1074590287Sobrien if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx) 1074651412Sobrien && TREE_PUBLIC (ctx)) 1074751412Sobrien /* This is a function in a local class in an extern inline 1074851412Sobrien function. */ 1074951412Sobrien comdat_linkage (decl1); 1075018334Speter } 1075118334Speter /* If this function belongs to an interface, it is public. 1075218334Speter If it belongs to someone else's interface, it is also external. 1075352290Sobrien This only affects inlines and template instantiations. */ 10754169699Skan else if (!finfo->interface_unknown && honor_interface) 1075518334Speter { 10756169699Skan if (DECL_DECLARED_INLINE_P (decl1) 1075790287Sobrien || DECL_TEMPLATE_INSTANTIATION (decl1) 1075851412Sobrien || processing_template_decl) 1075952290Sobrien { 1076052290Sobrien DECL_EXTERNAL (decl1) 10761169699Skan = (finfo->interface_only 10762169699Skan || (DECL_DECLARED_INLINE_P (decl1) 1076390287Sobrien && ! flag_implement_inlines 1076452290Sobrien && !DECL_VINDEX (decl1))); 1076552290Sobrien 1076652290Sobrien /* For WIN32 we also want to put these in linkonce sections. */ 1076752290Sobrien maybe_make_one_only (decl1); 1076852290Sobrien } 1076918334Speter else 1077018334Speter DECL_EXTERNAL (decl1) = 0; 1077118334Speter DECL_INTERFACE_KNOWN (decl1) = 1; 10772169699Skan /* If this function is in an interface implemented in this file, 10773169699Skan make sure that the backend knows to emit this function 10774169699Skan here. */ 10775169699Skan if (!DECL_EXTERNAL (decl1)) 10776169699Skan mark_needed (decl1); 1077718334Speter } 10778169699Skan else if (finfo->interface_unknown && finfo->interface_only 10779169699Skan && honor_interface) 1078052290Sobrien { 1078152290Sobrien /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma 10782169699Skan interface, we will have both finfo->interface_unknown and 10783169699Skan finfo->interface_only set. In that case, we don't want to 10784169699Skan use the normal heuristics because someone will supply a 10785169699Skan #pragma implementation elsewhere, and deducing it here would 10786169699Skan produce a conflict. */ 1078752290Sobrien comdat_linkage (decl1); 1078852290Sobrien DECL_EXTERNAL (decl1) = 0; 1078952290Sobrien DECL_INTERFACE_KNOWN (decl1) = 1; 1079052290Sobrien DECL_DEFER_OUTPUT (decl1) = 1; 1079152290Sobrien } 1079218334Speter else 1079318334Speter { 1079418334Speter /* This is a definition, not a reference. 1079518334Speter So clear DECL_EXTERNAL. */ 1079618334Speter DECL_EXTERNAL (decl1) = 0; 1079718334Speter 10798169699Skan if ((DECL_DECLARED_INLINE_P (decl1) 1079990287Sobrien || DECL_TEMPLATE_INSTANTIATION (decl1)) 1080051412Sobrien && ! DECL_INTERFACE_KNOWN (decl1) 1080151412Sobrien /* Don't try to defer nested functions for now. */ 1080290287Sobrien && ! decl_function_context (decl1)) 1080318334Speter DECL_DEFER_OUTPUT (decl1) = 1; 1080418334Speter else 1080551412Sobrien DECL_INTERFACE_KNOWN (decl1) = 1; 1080618334Speter } 1080718334Speter 10808169699Skan /* Determine the ELF visibility attribute for the function. We must not 10809169699Skan do this before calling "pushdecl", as we must allow "duplicate_decls" 10810169699Skan to merge any attributes appropriately. We also need to wait until 10811169699Skan linkage is set. */ 10812169699Skan if (!DECL_CLONED_FUNCTION_P (decl1)) 10813169699Skan determine_visibility (decl1); 10814169699Skan 10815132747Skan begin_scope (sk_function_parms, decl1); 1081618334Speter 1081751412Sobrien ++function_depth; 1081851412Sobrien 10819169699Skan if (DECL_DESTRUCTOR_P (decl1) 10820169699Skan || (DECL_CONSTRUCTOR_P (decl1) 10821169699Skan && targetm.cxx.cdtor_returns_this ())) 1082218334Speter { 10823169699Skan cdtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); 10824169699Skan DECL_CONTEXT (cdtor_label) = current_function_decl; 1082518334Speter } 1082618334Speter 1082790287Sobrien start_fname_decls (); 10828169699Skan 1082990287Sobrien store_parm_decls (current_function_parms); 10830169699Skan} 1083190287Sobrien 10832169699Skan 10833169699Skan/* Like start_preparsed_function, except that instead of a 10834169699Skan FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR. 10835169699Skan 10836169699Skan Returns 1 on success. If the DECLARATOR is not suitable for a function 10837169699Skan (it defines a datum instead), we return 0, which tells 10838169699Skan yyparse to report a parse error. */ 10839169699Skan 10840169699Skanint 10841169699Skanstart_function (cp_decl_specifier_seq *declspecs, 10842169699Skan const cp_declarator *declarator, 10843169699Skan tree attrs) 10844169699Skan{ 10845169699Skan tree decl1; 10846169699Skan 10847169699Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs); 10848169699Skan /* If the declarator is not suitable for a function definition, 10849169699Skan cause a syntax error. */ 10850169699Skan if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) 10851169699Skan return 0; 10852169699Skan 10853169699Skan if (DECL_MAIN_P (decl1)) 10854169699Skan /* main must return int. grokfndecl should have corrected it 10855169699Skan (and issued a diagnostic) if the user got it wrong. */ 10856169699Skan gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)), 10857169699Skan integer_type_node)); 10858169699Skan 10859169699Skan start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT); 10860169699Skan 1086118334Speter return 1; 1086218334Speter} 1086318334Speter 10864169699Skan/* Returns true iff an EH_SPEC_BLOCK should be created in the body of 10865169699Skan FN. */ 10866169699Skan 10867169699Skanstatic bool 10868169699Skanuse_eh_spec_block (tree fn) 10869169699Skan{ 10870169699Skan return (flag_exceptions && flag_enforce_eh_specs 10871169699Skan && !processing_template_decl 10872169699Skan && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)) 10873169699Skan /* We insert the EH_SPEC_BLOCK only in the original 10874169699Skan function; then, it is copied automatically to the 10875169699Skan clones. */ 10876169699Skan && !DECL_CLONED_FUNCTION_P (fn) 10877169699Skan /* Implicitly-generated constructors and destructors have 10878169699Skan exception specifications. However, those specifications 10879169699Skan are the union of the possible exceptions specified by the 10880169699Skan constructors/destructors for bases and members, so no 10881169699Skan unallowed exception will ever reach this function. By 10882169699Skan not creating the EH_SPEC_BLOCK we save a little memory, 10883169699Skan and we avoid spurious warnings about unreachable 10884169699Skan code. */ 10885169699Skan && !DECL_ARTIFICIAL (fn)); 10886169699Skan} 10887169699Skan 1088818334Speter/* Store the parameter declarations into the current function declaration. 1088918334Speter This is called after parsing the parameter declarations, before 1089018334Speter digesting the body of the function. 1089118334Speter 1089218334Speter Also install to binding contour return value identifier, if any. */ 1089318334Speter 1089490287Sobrienstatic void 10895132747Skanstore_parm_decls (tree current_function_parms) 1089618334Speter{ 10897132747Skan tree fndecl = current_function_decl; 10898132747Skan tree parm; 1089918334Speter 1090018334Speter /* This is a chain of any other decls that came in among the parm 1090118334Speter declarations. If a parm is declared with enum {foo, bar} x; 1090218334Speter then CONST_DECLs for foo and bar are put here. */ 1090318334Speter tree nonparms = NULL_TREE; 1090418334Speter 1090590287Sobrien if (current_function_parms) 1090618334Speter { 1090718334Speter /* This case is when the function was defined with an ANSI prototype. 1090818334Speter The parms already have decls, so we need not do anything here 1090918334Speter except record them as in effect 1091018334Speter and complain if any redundant old-style parm decls were written. */ 1091118334Speter 1091290287Sobrien tree specparms = current_function_parms; 1091390287Sobrien tree next; 1091418334Speter 1091518334Speter /* Must clear this because it might contain TYPE_DECLs declared 1091690287Sobrien at class level. */ 10917132747Skan current_binding_level->names = NULL; 1091851412Sobrien 1091990287Sobrien /* If we're doing semantic analysis, then we'll call pushdecl 1092090287Sobrien for each of these. We must do them in reverse order so that 1092190287Sobrien they end in the correct forward order. */ 1092290287Sobrien specparms = nreverse (specparms); 1092390287Sobrien 1092490287Sobrien for (parm = specparms; parm; parm = next) 1092518334Speter { 1092618334Speter next = TREE_CHAIN (parm); 1092718334Speter if (TREE_CODE (parm) == PARM_DECL) 1092818334Speter { 1092990287Sobrien if (DECL_NAME (parm) == NULL_TREE 1093090287Sobrien || TREE_CODE (parm) != VOID_TYPE) 1093190287Sobrien pushdecl (parm); 1093218334Speter else 10933169699Skan error ("parameter %qD declared void", parm); 1093418334Speter } 1093518334Speter else 1093618334Speter { 1093718334Speter /* If we find an enum constant or a type tag, 1093818334Speter put it aside for the moment. */ 1093918334Speter TREE_CHAIN (parm) = NULL_TREE; 1094018334Speter nonparms = chainon (nonparms, parm); 1094118334Speter } 1094218334Speter } 1094318334Speter 1094490287Sobrien /* Get the decls in their original chain order and record in the 1094590287Sobrien function. This is all and only the PARM_DECLs that were 1094690287Sobrien pushed into scope by the loop above. */ 1094718334Speter DECL_ARGUMENTS (fndecl) = getdecls (); 1094818334Speter } 1094918334Speter else 1095018334Speter DECL_ARGUMENTS (fndecl) = NULL_TREE; 1095118334Speter 1095218334Speter /* Now store the final chain of decls for the arguments 1095318334Speter as the decl-chain of the current lexical scope. 1095418334Speter Put the enumerators in as well, at the front so that 1095518334Speter DECL_ARGUMENTS is not modified. */ 10956132747Skan current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl)); 1095718334Speter 10958169699Skan if (use_eh_spec_block (current_function_decl)) 1095990287Sobrien current_eh_spec_block = begin_eh_spec_block (); 1096090287Sobrien} 1096152290Sobrien 1096290287Sobrien 1096390287Sobrien/* We have finished doing semantic analysis on DECL, but have not yet 1096490287Sobrien generated RTL for its body. Save away our current state, so that 1096590287Sobrien when we want to generate RTL later we know what to do. */ 1096618334Speter 1096790287Sobrienstatic void 10968132747Skansave_function_data (tree decl) 1096990287Sobrien{ 10970117410Skan struct language_function *f; 1097151412Sobrien 1097290287Sobrien /* Save the language-specific per-function data so that we can 1097390287Sobrien get it back when we really expand this function. */ 10974169699Skan gcc_assert (!DECL_PENDING_INLINE_P (decl)); 1097551412Sobrien 1097690287Sobrien /* Make a copy. */ 10977169699Skan f = GGC_NEW (struct language_function); 10978117410Skan memcpy (f, cp_function_chain, sizeof (struct language_function)); 1097990287Sobrien DECL_SAVED_FUNCTION_DATA (decl) = f; 1098051412Sobrien 1098190287Sobrien /* Clear out the bits we don't need. */ 10982169699Skan f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE; 1098390287Sobrien f->bindings = NULL; 1098490287Sobrien f->x_local_names = NULL; 1098518334Speter} 1098618334Speter 1098751412Sobrien 10988169699Skan/* Set the return value of the constructor (if present). */ 10989169699Skan 1099090287Sobrienstatic void 10991169699Skanfinish_constructor_body (void) 1099218334Speter{ 10993169699Skan tree val; 10994169699Skan tree exprstmt; 1099518334Speter 10996169699Skan if (targetm.cxx.cdtor_returns_this ()) 10997169699Skan { 10998169699Skan /* Any return from a constructor will end up here. */ 10999169699Skan add_stmt (build_stmt (LABEL_EXPR, cdtor_label)); 1100018334Speter 11001169699Skan val = DECL_ARGUMENTS (current_function_decl); 11002169699Skan val = build2 (MODIFY_EXPR, TREE_TYPE (val), 11003169699Skan DECL_RESULT (current_function_decl), val); 11004169699Skan /* Return the address of the object. */ 11005169699Skan exprstmt = build_stmt (RETURN_EXPR, val); 11006169699Skan add_stmt (exprstmt); 11007169699Skan } 1100818334Speter} 1100918334Speter 1101090287Sobrien/* Do all the processing for the beginning of a destructor; set up the 1101190287Sobrien vtable pointers and cleanups for bases and members. */ 1101260970Sobrien 1101360970Sobrienstatic void 11014132747Skanbegin_destructor_body (void) 1101560970Sobrien{ 1101690287Sobrien tree compound_stmt; 1101760970Sobrien 11018169699Skan /* If the CURRENT_CLASS_TYPE is incomplete, we will have already 11019169699Skan issued an error message. We still want to try to process the 11020169699Skan body of the function, but initialize_vtbl_ptrs will crash if 11021169699Skan TYPE_BINFO is NULL. */ 11022169699Skan if (COMPLETE_TYPE_P (current_class_type)) 11023169699Skan { 11024169699Skan compound_stmt = begin_compound_stmt (0); 11025169699Skan /* Make all virtual function table pointers in non-virtual base 11026169699Skan classes point to CURRENT_CLASS_TYPE's virtual function 11027169699Skan tables. */ 11028169699Skan initialize_vtbl_ptrs (current_class_ptr); 11029169699Skan finish_compound_stmt (compound_stmt); 1103060970Sobrien 11031169699Skan /* And insert cleanups for our bases and members so that they 11032169699Skan will be properly destroyed if we throw. */ 11033169699Skan push_base_cleanups (); 11034169699Skan } 1103560970Sobrien} 1103660970Sobrien 1103790287Sobrien/* At the end of every destructor we generate code to delete the object if 1103890287Sobrien necessary. Do that now. */ 1103960970Sobrien 1104060970Sobrienstatic void 11041132747Skanfinish_destructor_body (void) 1104260970Sobrien{ 1104390287Sobrien tree exprstmt; 1104460970Sobrien 11045107601Sobrien /* Any return from a destructor will end up here; that way all base 11046107601Sobrien and member cleanups will be run when the function returns. */ 11047169699Skan add_stmt (build_stmt (LABEL_EXPR, cdtor_label)); 11048107601Sobrien 1104990287Sobrien /* In a virtual destructor, we must call delete. */ 1105090287Sobrien if (DECL_VIRTUAL_P (current_function_decl)) 1105160970Sobrien { 1105290287Sobrien tree if_stmt; 11053117410Skan tree virtual_size = cxx_sizeof (current_class_type); 1105460970Sobrien 1105590287Sobrien /* [class.dtor] 1105660970Sobrien 1105790287Sobrien At the point of definition of a virtual destructor (including 1105890287Sobrien an implicit definition), non-placement operator delete shall 1105990287Sobrien be looked up in the scope of the destructor's class and if 1106090287Sobrien found shall be accessible and unambiguous. */ 11061169699Skan exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr, 11062169699Skan virtual_size, 11063169699Skan /*global_p=*/false, 11064169699Skan /*placement=*/NULL_TREE, 11065169699Skan /*alloc_fn=*/NULL_TREE); 1106660970Sobrien 1106790287Sobrien if_stmt = begin_if_stmt (); 11068169699Skan finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node, 11069169699Skan current_in_charge_parm, 11070169699Skan integer_one_node), 1107190287Sobrien if_stmt); 1107290287Sobrien finish_expr_stmt (exprstmt); 1107390287Sobrien finish_then_clause (if_stmt); 11074169699Skan finish_if_stmt (if_stmt); 1107560970Sobrien } 11076169699Skan 11077169699Skan if (targetm.cxx.cdtor_returns_this ()) 11078169699Skan { 11079169699Skan tree val; 11080169699Skan 11081169699Skan val = DECL_ARGUMENTS (current_function_decl); 11082169699Skan val = build2 (MODIFY_EXPR, TREE_TYPE (val), 11083169699Skan DECL_RESULT (current_function_decl), val); 11084169699Skan /* Return the address of the object. */ 11085169699Skan exprstmt = build_stmt (RETURN_EXPR, val); 11086169699Skan add_stmt (exprstmt); 11087169699Skan } 1108890287Sobrien} 1108960970Sobrien 1109090287Sobrien/* Do the necessary processing for the beginning of a function body, which 1109190287Sobrien in this case includes member-initializers, but not the catch clauses of 1109290287Sobrien a function-try-block. Currently, this means opening a binding level 11093169699Skan for the member-initializers (in a ctor) and member cleanups (in a dtor). */ 1109460970Sobrien 1109590287Sobrientree 11096132747Skanbegin_function_body (void) 1109790287Sobrien{ 1109890287Sobrien tree stmt; 1109960970Sobrien 11100169699Skan if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 11101169699Skan return NULL_TREE; 11102169699Skan 1110390287Sobrien if (processing_template_decl) 1110490287Sobrien /* Do nothing now. */; 1110590287Sobrien else 1110690287Sobrien /* Always keep the BLOCK node associated with the outermost pair of 1110790287Sobrien curly braces of a function. These are needed for correct 1110890287Sobrien operation of dwarfout.c. */ 11109132747Skan keep_next_level (true); 1111060970Sobrien 11111169699Skan stmt = begin_compound_stmt (BCS_FN_BODY); 1111260970Sobrien 1111390287Sobrien if (processing_template_decl) 1111490287Sobrien /* Do nothing now. */; 1111590287Sobrien else if (DECL_DESTRUCTOR_P (current_function_decl)) 1111690287Sobrien begin_destructor_body (); 1111760970Sobrien 1111890287Sobrien return stmt; 1111990287Sobrien} 1112060970Sobrien 1112190287Sobrien/* Do the processing for the end of a function body. Currently, this means 1112290287Sobrien closing out the cleanups for fully-constructed bases and members, and in 1112390287Sobrien the case of the destructor, deleting the object if desired. Again, this 1112490287Sobrien is only meaningful for [cd]tors, since they are the only functions where 1112590287Sobrien there is a significant distinction between the main body and any 1112690287Sobrien function catch clauses. Handling, say, main() return semantics here 1112790287Sobrien would be wrong, as flowing off the end of a function catch clause for 1112890287Sobrien main() would also need to return 0. */ 1112960970Sobrien 1113090287Sobrienvoid 11131132747Skanfinish_function_body (tree compstmt) 1113290287Sobrien{ 11133169699Skan if (compstmt == NULL_TREE) 11134169699Skan return; 11135169699Skan 11136107601Sobrien /* Close the block. */ 11137132747Skan finish_compound_stmt (compstmt); 1113860970Sobrien 1113990287Sobrien if (processing_template_decl) 1114090287Sobrien /* Do nothing now. */; 1114190287Sobrien else if (DECL_CONSTRUCTOR_P (current_function_decl)) 1114290287Sobrien finish_constructor_body (); 1114390287Sobrien else if (DECL_DESTRUCTOR_P (current_function_decl)) 1114490287Sobrien finish_destructor_body (); 11145169699Skan} 1114660970Sobrien 11147169699Skan/* Given a function, returns the BLOCK corresponding to the outermost level 11148169699Skan of curly braces, skipping the artificial block created for constructor 11149169699Skan initializers. */ 11150169699Skan 11151169699Skanstatic tree 11152169699Skanouter_curly_brace_block (tree fndecl) 11153169699Skan{ 11154169699Skan tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl)); 11155169699Skan if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 11156169699Skan /* Skip the artificial function body block. */ 11157169699Skan block = BLOCK_SUBBLOCKS (block); 11158169699Skan return block; 11159169699Skan} 11160169699Skan 1116118334Speter/* Finish up a function declaration and compile that function 1116218334Speter all the way to assembler language output. The free the storage 1116318334Speter for the function definition. 1116418334Speter 1116590287Sobrien FLAGS is a bitwise or of the following values: 1116652290Sobrien 2 - INCLASS_INLINE 1116752290Sobrien We just finished processing the body of an in-class inline 1116852290Sobrien function definition. (This processing will have taken place 1116990287Sobrien after the class definition is complete.) */ 1117018334Speter 1117190287Sobrientree 11172132747Skanfinish_function (int flags) 1117318334Speter{ 11174132747Skan tree fndecl = current_function_decl; 1117518334Speter tree fntype, ctype = NULL_TREE; 1117652290Sobrien int inclass_inline = (flags & 2) != 0; 1117790287Sobrien int nested; 1117818334Speter 1117918334Speter /* When we get some parse errors, we can end up without a 1118018334Speter current_function_decl, so cope. */ 1118118334Speter if (fndecl == NULL_TREE) 1118290287Sobrien return error_mark_node; 1118318334Speter 11184117410Skan if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) 11185117410Skan && DECL_VIRTUAL_P (fndecl) 11186117410Skan && !processing_template_decl) 11187117410Skan { 11188117410Skan tree fnclass = DECL_CONTEXT (fndecl); 11189117410Skan if (fndecl == CLASSTYPE_KEY_METHOD (fnclass)) 11190117410Skan keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes); 11191117410Skan } 11192117410Skan 1119390287Sobrien nested = function_depth > 1; 1119418334Speter fntype = TREE_TYPE (fndecl); 1119518334Speter 1119690287Sobrien /* TREE_READONLY (fndecl) = 1; 1119790287Sobrien This caused &foo to be of type ptr-to-const-function 1119890287Sobrien which then got a warning when stored in a ptr-to-function variable. */ 1119918334Speter 11200169699Skan gcc_assert (building_stmt_tree ()); 11201169699Skan 1120290287Sobrien /* For a cloned function, we've already got all the code we need; 1120390287Sobrien there's no need to add any extra bits. */ 1120490287Sobrien if (!DECL_CLONED_FUNCTION_P (fndecl)) 1120518334Speter { 1120690287Sobrien if (DECL_MAIN_P (current_function_decl)) 1120751412Sobrien { 11208169699Skan tree stmt; 11209169699Skan 11210169699Skan /* Make it so that `main' always returns 0 by default (or 11211169699Skan 1 for VMS). */ 11212102804Skan#if VMS_TARGET 11213169699Skan stmt = finish_return_stmt (integer_one_node); 1121418334Speter#else 11215169699Skan stmt = finish_return_stmt (integer_zero_node); 1121618334Speter#endif 11217169699Skan /* Hack. We don't want the middle-end to warn that this 11218169699Skan return is unreachable, so put the statement on the 11219169699Skan special line 0. */ 11220169699Skan#ifdef USE_MAPPED_LOCATION 11221169699Skan SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION); 11222169699Skan#else 11223169699Skan annotate_with_file_line (stmt, input_filename, 0); 11224169699Skan#endif 1122551412Sobrien } 1122618334Speter 11227169699Skan if (use_eh_spec_block (current_function_decl)) 1122890287Sobrien finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS 1122990287Sobrien (TREE_TYPE (current_function_decl)), 1123090287Sobrien current_eh_spec_block); 1123190287Sobrien } 1123218334Speter 11233169699Skan /* If we're saving up tree structure, tie off the function now. */ 11234169699Skan DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 11235169699Skan 11236132747Skan finish_fname_decls (); 11237132747Skan 11238117410Skan /* If this function can't throw any exceptions, remember that. */ 11239117410Skan if (!processing_template_decl 11240117410Skan && !cp_function_chain->can_throw 11241169699Skan && !flag_non_call_exceptions 11242169699Skan && !DECL_REPLACEABLE_P (fndecl)) 11243117410Skan TREE_NOTHROW (fndecl) = 1; 11244117410Skan 1124590287Sobrien /* This must come after expand_function_end because cleanups might 1124690287Sobrien have declarations (from inline functions) that need to go into 1124790287Sobrien this function's blocks. */ 11248169699Skan 1124990287Sobrien /* If the current binding level isn't the outermost binding level 1125090287Sobrien for this function, either there is a bug, or we have experienced 1125190287Sobrien syntax errors and the statement tree is malformed. */ 11252132747Skan if (current_binding_level->kind != sk_function_parms) 1125390287Sobrien { 1125490287Sobrien /* Make sure we have already experienced errors. */ 11255169699Skan gcc_assert (errorcount); 1125618334Speter 1125790287Sobrien /* Throw away the broken statement tree and extra binding 11258169699Skan levels. */ 11259169699Skan DECL_SAVED_TREE (fndecl) = alloc_stmt_list (); 1126018334Speter 11261132747Skan while (current_binding_level->kind != sk_function_parms) 1126251412Sobrien { 11263132747Skan if (current_binding_level->kind == sk_class) 1126490287Sobrien pop_nested_class (); 1126590287Sobrien else 1126690287Sobrien poplevel (0, 0, 0); 1126751412Sobrien } 1126818334Speter } 1126918334Speter poplevel (1, 0, 1); 1127018334Speter 11271132747Skan /* Statements should always be full-expressions at the outermost set 11272132747Skan of curly braces for a function. */ 11273169699Skan gcc_assert (stmts_are_full_exprs_p ()); 11274132747Skan 11275169699Skan /* Set up the named return value optimization, if we can. Candidate 11276220150Smm variables are selected in check_return_expr. */ 1127790287Sobrien if (current_function_return_value) 1127818334Speter { 1127990287Sobrien tree r = current_function_return_value; 11280117410Skan tree outer; 11281117410Skan 1128290287Sobrien if (r != error_mark_node 11283117410Skan /* This is only worth doing for fns that return in memory--and 11284117410Skan simpler, since we don't have to worry about promoted modes. */ 11285132747Skan && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl) 11286117410Skan /* Only allow this for variables declared in the outer scope of 11287117410Skan the function so we know that their lifetime always ends with a 11288117410Skan return; see g++.dg/opt/nrv6.C. We could be more flexible if 11289117410Skan we were to do this optimization in tree-ssa. */ 11290169699Skan && (outer = outer_curly_brace_block (fndecl)) 11291169699Skan && chain_member (r, BLOCK_VARS (outer))) 11292169699Skan finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl)); 11293169699Skan 11294169699Skan current_function_return_value = NULL_TREE; 1129518334Speter } 1129618334Speter 1129790287Sobrien /* Remember that we were in class scope. */ 1129890287Sobrien if (current_class_name) 1129990287Sobrien ctype = current_class_type; 1130090287Sobrien 1130118334Speter /* Must mark the RESULT_DECL as being in this function. */ 1130251412Sobrien DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 1130318334Speter 1130418334Speter /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point 1130518334Speter to the FUNCTION_DECL node itself. */ 1130618334Speter BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 1130718334Speter 1130890287Sobrien /* Save away current state, if appropriate. */ 1130990287Sobrien if (!processing_template_decl) 1131090287Sobrien save_function_data (fndecl); 1131118334Speter 1131296292Sobrien /* Complain if there's just no return statement. */ 1131396292Sobrien if (warn_return_type 1131496292Sobrien && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE 11315132747Skan && !dependent_type_p (TREE_TYPE (fntype)) 1131696292Sobrien && !current_function_returns_value && !current_function_returns_null 1131796292Sobrien /* Don't complain if we abort or throw. */ 1131896292Sobrien && !current_function_returns_abnormally 1131996292Sobrien && !DECL_NAME (DECL_RESULT (fndecl)) 1132096292Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 1132196292Sobrien inline function, as we might never be compiled separately. */ 11322169699Skan && (DECL_INLINE (fndecl) || processing_template_decl) 11323169699Skan /* Structor return values (if any) are set by the compiler. */ 11324169699Skan && !DECL_CONSTRUCTOR_P (fndecl) 11325169699Skan && !DECL_DESTRUCTOR_P (fndecl)) 11326169699Skan warning (OPT_Wreturn_type, "no return statement in function returning non-void"); 11327132747Skan 11328169699Skan /* Store the end of the function, so that we get good line number 11329169699Skan info for the epilogue. */ 11330169699Skan cfun->function_end_locus = input_location; 11331169699Skan 11332169699Skan /* Genericize before inlining. */ 11333169699Skan if (!processing_template_decl) 11334169699Skan { 11335169699Skan struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl); 11336169699Skan cp_genericize (fndecl); 11337169699Skan /* Clear out the bits we don't need. */ 11338169699Skan f->x_current_class_ptr = NULL; 11339169699Skan f->x_current_class_ref = NULL; 11340169699Skan f->x_eh_spec_block = NULL; 11341169699Skan f->x_in_charge_parm = NULL; 11342169699Skan f->x_vtt_parm = NULL; 11343169699Skan f->x_return_value = NULL; 11344169699Skan f->bindings = NULL; 11345169699Skan f->extern_decl_map = NULL; 11346169699Skan 11347169699Skan /* Handle attribute((warn_unused_result)). Relies on gimple input. */ 11348169699Skan c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); 11349169699Skan } 11350169699Skan /* Clear out the bits we don't need. */ 11351169699Skan local_names = NULL; 11352169699Skan 11353132747Skan /* We're leaving the context of this function, so zap cfun. It's still in 11354169699Skan DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */ 1135590287Sobrien cfun = NULL; 11356132747Skan current_function_decl = NULL; 1135718334Speter 11358117410Skan /* If this is an in-class inline definition, we may have to pop the 1135990287Sobrien bindings for the template parameters that we added in 1136090287Sobrien maybe_begin_member_template_processing when start_function was 1136190287Sobrien called. */ 1136290287Sobrien if (inclass_inline) 1136390287Sobrien maybe_end_member_template_processing (); 1136418334Speter 1136590287Sobrien /* Leave the scope of the class. */ 1136690287Sobrien if (ctype) 1136790287Sobrien pop_nested_class (); 1136818334Speter 1136951412Sobrien --function_depth; 1137051412Sobrien 1137190287Sobrien /* Clean up. */ 1137218334Speter if (! nested) 1137390287Sobrien /* Let the error reporting routines know that we're outside a 1137490287Sobrien function. For a nested function, this value is used in 11375117410Skan cxx_pop_function_context and then reset via pop_function_context. */ 1137690287Sobrien current_function_decl = NULL_TREE; 1137718334Speter 1137890287Sobrien return fndecl; 1137918334Speter} 1138018334Speter 1138118334Speter/* Create the FUNCTION_DECL for a function definition. 1138218334Speter DECLSPECS and DECLARATOR are the parts of the declaration; 1138318334Speter they describe the return type and the name of the function, 1138418334Speter but twisted together in a fashion that parallels the syntax of C. 1138518334Speter 1138618334Speter This function creates a binding context for the function body 1138718334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 1138818334Speter 1138918334Speter Returns a FUNCTION_DECL on success. 1139018334Speter 1139118334Speter If the DECLARATOR is not suitable for a function (it defines a datum 1139218334Speter instead), we return 0, which tells yyparse to report a parse error. 1139318334Speter 1139418334Speter May return void_type_node indicating that this method is actually 1139518334Speter a friend. See grokfield for more details. 1139618334Speter 1139718334Speter Came here with a `.pushlevel' . 1139818334Speter 1139918334Speter DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING 1140018334Speter CHANGES TO CODE IN `grokfield'. */ 1140151412Sobrien 1140218334Spetertree 11403169699Skanstart_method (cp_decl_specifier_seq *declspecs, 11404169699Skan const cp_declarator *declarator, tree attrlist) 1140518334Speter{ 1140651412Sobrien tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, 1140790287Sobrien &attrlist); 1140818334Speter 11409132747Skan if (fndecl == error_mark_node) 11410132747Skan return error_mark_node; 1141118334Speter 11412132747Skan if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL) 11413132747Skan { 11414132747Skan error ("invalid member function declaration"); 11415132747Skan return error_mark_node; 11416132747Skan } 11417132747Skan 1141890287Sobrien if (attrlist) 1141990287Sobrien cplus_decl_attributes (&fndecl, attrlist, 0); 1142090287Sobrien 1142118334Speter /* Pass friends other than inline friend functions back. */ 1142251412Sobrien if (fndecl == void_type_node) 1142318334Speter return fndecl; 1142418334Speter 1142518334Speter if (DECL_IN_AGGR_P (fndecl)) 1142618334Speter { 11427169699Skan if (DECL_CONTEXT (fndecl) 11428169699Skan && TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL) 11429169699Skan error ("%qD is already defined in class %qT", fndecl, 11430169699Skan DECL_CONTEXT (fndecl)); 11431169699Skan return error_mark_node; 1143218334Speter } 1143318334Speter 1143452290Sobrien check_template_shadow (fndecl); 1143552290Sobrien 1143690287Sobrien DECL_DECLARED_INLINE_P (fndecl) = 1; 1143718334Speter if (flag_default_inline) 1143818334Speter DECL_INLINE (fndecl) = 1; 1143918334Speter 1144052290Sobrien /* We process method specializations in finish_struct_1. */ 1144152290Sobrien if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) 11442132747Skan { 11443132747Skan fndecl = push_template_decl (fndecl); 11444132747Skan if (fndecl == error_mark_node) 11445132747Skan return fndecl; 11446132747Skan } 1144718334Speter 1144818334Speter if (! DECL_FRIEND_P (fndecl)) 1144918334Speter { 1145018334Speter if (TREE_CHAIN (fndecl)) 1145118334Speter { 1145218334Speter fndecl = copy_node (fndecl); 1145318334Speter TREE_CHAIN (fndecl) = NULL_TREE; 1145418334Speter } 1145518334Speter } 1145618334Speter 11457169699Skan finish_decl (fndecl, NULL_TREE, NULL_TREE); 1145818334Speter 11459132747Skan /* Make a place for the parms. */ 11460132747Skan begin_scope (sk_function_parms, fndecl); 1146190287Sobrien 1146218334Speter DECL_IN_AGGR_P (fndecl) = 1; 1146318334Speter return fndecl; 1146418334Speter} 1146518334Speter 1146618334Speter/* Go through the motions of finishing a function definition. 1146718334Speter We don't compile this method until after the whole class has 1146818334Speter been processed. 1146918334Speter 1147018334Speter FINISH_METHOD must return something that looks as though it 1147118334Speter came from GROKFIELD (since we are defining a method, after all). 1147218334Speter 1147318334Speter This is called after parsing the body of the function definition. 1147418334Speter STMTS is the chain of statements that makes up the function body. 1147518334Speter 1147618334Speter DECL is the ..._DECL that `start_method' provided. */ 1147718334Speter 1147818334Spetertree 11479132747Skanfinish_method (tree decl) 1148018334Speter{ 11481132747Skan tree fndecl = decl; 1148218334Speter tree old_initial; 1148318334Speter 11484132747Skan tree link; 1148518334Speter 1148651412Sobrien if (decl == void_type_node) 1148718334Speter return decl; 1148818334Speter 1148918334Speter old_initial = DECL_INITIAL (fndecl); 1149018334Speter 1149118334Speter /* Undo the level for the parms (from start_method). 1149218334Speter This is like poplevel, but it causes nothing to be 1149318334Speter saved. Saving information here confuses symbol-table 1149418334Speter output routines. Besides, this information will 1149518334Speter be correctly output when this method is actually 1149618334Speter compiled. */ 1149718334Speter 1149818334Speter /* Clear out the meanings of the local variables of this level; 1149918334Speter also record in each decl which block it belongs to. */ 1150018334Speter 1150118334Speter for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) 1150218334Speter { 1150318334Speter if (DECL_NAME (link) != NULL_TREE) 1150452290Sobrien pop_binding (DECL_NAME (link), link); 11505169699Skan gcc_assert (TREE_CODE (link) != FUNCTION_DECL); 1150618334Speter DECL_CONTEXT (link) = NULL_TREE; 1150718334Speter } 1150818334Speter 1150918334Speter poplevel (0, 0, 0); 1151018334Speter 1151118334Speter DECL_INITIAL (fndecl) = old_initial; 1151218334Speter 1151318334Speter /* We used to check if the context of FNDECL was different from 1151418334Speter current_class_type as another way to get inside here. This didn't work 1151518334Speter for String.cc in libg++. */ 1151618334Speter if (DECL_FRIEND_P (fndecl)) 1151718334Speter { 11518169699Skan VEC_safe_push (tree, gc, CLASSTYPE_INLINE_FRIENDS (current_class_type), 11519169699Skan fndecl); 1152018334Speter decl = void_type_node; 1152118334Speter } 1152218334Speter 1152318334Speter return decl; 1152418334Speter} 1152518334Speter 1152618334Speter 1152796292Sobrien/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that 1152896292Sobrien we can lay it out later, when and if its type becomes complete. */ 1152996292Sobrien 1153018334Spetervoid 11531132747Skanmaybe_register_incomplete_var (tree var) 1153218334Speter{ 11533169699Skan gcc_assert (TREE_CODE (var) == VAR_DECL); 1153418334Speter 1153596292Sobrien /* Keep track of variables with incomplete types. */ 11536169699Skan if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 1153796292Sobrien && DECL_EXTERNAL (var)) 1153818334Speter { 1153996292Sobrien tree inner_type = TREE_TYPE (var); 11540169699Skan 1154196292Sobrien while (TREE_CODE (inner_type) == ARRAY_TYPE) 1154296292Sobrien inner_type = TREE_TYPE (inner_type); 1154396292Sobrien inner_type = TYPE_MAIN_VARIANT (inner_type); 11544169699Skan 1154596292Sobrien if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) 1154696292Sobrien /* RTTI TD entries are created while defining the type_info. */ 1154796292Sobrien || (TYPE_LANG_SPECIFIC (inner_type) 1154896292Sobrien && TYPE_BEING_DEFINED (inner_type))) 1154996292Sobrien incomplete_vars = tree_cons (inner_type, var, incomplete_vars); 1155090287Sobrien } 1155196292Sobrien} 1155290287Sobrien 1155396292Sobrien/* Called when a class type (given by TYPE) is defined. If there are 1155496292Sobrien any existing VAR_DECLs whose type hsa been completed by this 1155596292Sobrien declaration, update them now. */ 1155696292Sobrien 1155796292Sobrienvoid 11558132747Skancomplete_vars (tree type) 1155996292Sobrien{ 1156096292Sobrien tree *list = &incomplete_vars; 1156196292Sobrien 11562169699Skan gcc_assert (CLASS_TYPE_P (type)); 11563169699Skan while (*list) 1156490287Sobrien { 1156596292Sobrien if (same_type_p (type, TREE_PURPOSE (*list))) 1156618334Speter { 1156796292Sobrien tree var = TREE_VALUE (*list); 11568169699Skan tree type = TREE_TYPE (var); 1156996292Sobrien /* Complete the type of the variable. The VAR_DECL itself 1157096292Sobrien will be laid out in expand_expr. */ 11571169699Skan complete_type (type); 11572169699Skan cp_apply_type_quals_to_decl (cp_type_quals (type), var); 1157396292Sobrien /* Remove this entry from the list. */ 1157496292Sobrien *list = TREE_CHAIN (*list); 1157518334Speter } 1157618334Speter else 1157796292Sobrien list = &TREE_CHAIN (*list); 1157818334Speter } 11579169699Skan 11580169699Skan /* Check for pending declarations which may have abstract type. */ 11581169699Skan complete_type_check_abstract (type); 1158218334Speter} 1158318334Speter 1158490287Sobrien/* If DECL is of a type which needs a cleanup, build that cleanup 1158590287Sobrien here. */ 1158618334Speter 1158790287Sobrientree 11588132747Skancxx_maybe_build_cleanup (tree decl) 1158918334Speter{ 1159018334Speter tree type = TREE_TYPE (decl); 1159190287Sobrien 1159290287Sobrien if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 1159318334Speter { 1159490287Sobrien int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR; 1159518334Speter tree rval; 11596169699Skan bool has_vbases = (TREE_CODE (type) == RECORD_TYPE 11597169699Skan && CLASSTYPE_VBASECLASSES (type)); 1159818334Speter 1159918334Speter if (TREE_CODE (type) == ARRAY_TYPE) 1160018334Speter rval = decl; 1160118334Speter else 1160218334Speter { 11603117410Skan cxx_mark_addressable (decl); 1160418334Speter rval = build_unary_op (ADDR_EXPR, decl, 0); 1160518334Speter } 1160618334Speter 1160718334Speter /* Optimize for space over speed here. */ 11608169699Skan if (!has_vbases || flag_expensive_optimizations) 1160918334Speter flags |= LOOKUP_NONVIRTUAL; 1161018334Speter 1161190287Sobrien rval = build_delete (TREE_TYPE (rval), rval, 1161290287Sobrien sfk_complete_destructor, flags, 0); 1161318334Speter 1161418334Speter return rval; 1161518334Speter } 1161696292Sobrien return NULL_TREE; 1161718334Speter} 1161818334Speter 1161990287Sobrien/* When a stmt has been parsed, this function is called. */ 1162018334Speter 1162118334Spetervoid 11622132747Skanfinish_stmt (void) 1162318334Speter{ 1162418334Speter} 1162518334Speter 1162690287Sobrien/* DECL was originally constructed as a non-static member function, 1162790287Sobrien but turned out to be static. Update it accordingly. */ 1162818334Speter 1162918334Spetervoid 11630132747Skanrevert_static_member_fn (tree decl) 1163118334Speter{ 1163218334Speter tree tmp; 1163390287Sobrien tree function = TREE_TYPE (decl); 1163490287Sobrien tree args = TYPE_ARG_TYPES (function); 1163518334Speter 1163690287Sobrien if (cp_type_quals (TREE_TYPE (TREE_VALUE (args))) 1163752290Sobrien != TYPE_UNQUALIFIED) 11638169699Skan error ("static member function %q#D declared with type qualifiers", decl); 1163918334Speter 1164018334Speter args = TREE_CHAIN (args); 1164118334Speter tmp = build_function_type (TREE_TYPE (function), args); 1164290287Sobrien tmp = build_qualified_type (tmp, cp_type_quals (function)); 1164318334Speter tmp = build_exception_variant (tmp, 1164418334Speter TYPE_RAISES_EXCEPTIONS (function)); 1164590287Sobrien TREE_TYPE (decl) = tmp; 1164690287Sobrien if (DECL_ARGUMENTS (decl)) 1164790287Sobrien DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl)); 1164890287Sobrien DECL_STATIC_FUNCTION_P (decl) = 1; 1164918334Speter} 1165018334Speter 1165190287Sobrien/* Initialize the variables used during compilation of a C++ 1165290287Sobrien function. */ 1165390287Sobrien 11654117410Skanvoid 11655132747Skancxx_push_function_context (struct function * f) 1165618334Speter{ 11657169699Skan struct language_function *p = GGC_CNEW (struct language_function); 11658117410Skan f->language = p; 1165918334Speter 1166090287Sobrien /* Whenever we start a new function, we destroy temporaries in the 1166190287Sobrien usual way. */ 1166290287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 1; 11663132747Skan 11664132747Skan if (f->decl) 11665132747Skan { 11666132747Skan tree fn = f->decl; 11667132747Skan 11668132747Skan if (DECL_SAVED_FUNCTION_DATA (fn)) 11669132747Skan { 11670132747Skan /* If we already parsed this function, and we're just expanding it 11671132747Skan now, restore saved state. */ 11672132747Skan *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn); 11673132747Skan 11674132747Skan /* We don't need the saved data anymore. Unless this is an inline 11675132747Skan function; we need the named return value info for 11676169699Skan declare_return_variable. */ 11677132747Skan if (! DECL_INLINE (fn)) 11678132747Skan DECL_SAVED_FUNCTION_DATA (fn) = NULL; 11679132747Skan } 11680132747Skan } 1168190287Sobrien} 1168218334Speter 1168390287Sobrien/* Free the language-specific parts of F, now that we've finished 1168490287Sobrien compiling the function. */ 1168518334Speter 11686117410Skanvoid 11687132747Skancxx_pop_function_context (struct function * f) 1168818334Speter{ 1168990287Sobrien f->language = 0; 1169090287Sobrien} 1169118334Speter 11692117410Skan/* Return which tree structure is used by T, or TS_CP_GENERIC if T is 11693117410Skan one of the language-independent trees. */ 1169418334Speter 11695117410Skanenum cp_tree_node_structure_enum 11696132747Skancp_tree_node_structure (union lang_tree_node * t) 1169790287Sobrien{ 11698117410Skan switch (TREE_CODE (&t->generic)) 1169990287Sobrien { 11700132747Skan case DEFAULT_ARG: return TS_CP_DEFAULT_ARG; 11701117410Skan case IDENTIFIER_NODE: return TS_CP_IDENTIFIER; 11702117410Skan case OVERLOAD: return TS_CP_OVERLOAD; 11703117410Skan case TEMPLATE_PARM_INDEX: return TS_CP_TPI; 11704169699Skan case TINST_LEVEL: return TS_CP_TINST_LEVEL; 11705117410Skan case PTRMEM_CST: return TS_CP_PTRMEM; 11706169699Skan case BASELINK: return TS_CP_BASELINK; 11707117410Skan default: return TS_CP_GENERIC; 1170890287Sobrien } 1170990287Sobrien} 1171018334Speter 1171190287Sobrien/* Build the void_list_node (void_type_node having been created). */ 1171290287Sobrientree 11713132747Skanbuild_void_list_node (void) 1171418334Speter{ 1171590287Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 1171690287Sobrien return t; 1171718334Speter} 1171890287Sobrien 11719169699Skanbool 11720132747Skancp_missing_noreturn_ok_p (tree decl) 1172190287Sobrien{ 1172290287Sobrien /* A missing noreturn is ok for the `main' function. */ 1172390287Sobrien return DECL_MAIN_P (decl); 1172490287Sobrien} 11725117410Skan 11726169699Skan/* Return the COMDAT group into which DECL should be placed. */ 11727169699Skan 11728169699Skanconst char * 11729169699Skancxx_comdat_group (tree decl) 11730169699Skan{ 11731169699Skan tree name; 11732169699Skan 11733169699Skan /* Virtual tables, construction virtual tables, and virtual table 11734169699Skan tables all go in a single COMDAT group, named after the primary 11735169699Skan virtual table. */ 11736169699Skan if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl)) 11737169699Skan name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl))); 11738169699Skan /* For all other DECLs, the COMDAT group is the mangled name of the 11739169699Skan declaration itself. */ 11740169699Skan else 11741169699Skan { 11742169699Skan while (DECL_THUNK_P (decl)) 11743169699Skan { 11744169699Skan /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk 11745169699Skan into the same section as the target function. In that case 11746169699Skan we must return target's name. */ 11747169699Skan tree target = THUNK_TARGET (decl); 11748169699Skan if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target) 11749169699Skan && DECL_SECTION_NAME (target) != NULL 11750169699Skan && DECL_ONE_ONLY (target)) 11751169699Skan decl = target; 11752169699Skan else 11753169699Skan break; 11754169699Skan } 11755169699Skan name = DECL_ASSEMBLER_NAME (decl); 11756169699Skan } 11757169699Skan 11758169699Skan return IDENTIFIER_POINTER (name); 11759169699Skan} 11760169699Skan 11761117410Skan#include "gt-cp-decl.h" 11762