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. */ 235260919Spfg/* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 236260919Spfg/* An object declared as __attribute__((unavailable)) suppresses 237260919Spfg any reports of being declared with unavailable or deprecated 238260919Spfg items. */ 239260919Spfg/* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 240169699Skan 24190287Sobrienenum deprecated_states { 24290287Sobrien DEPRECATED_NORMAL, 24390287Sobrien DEPRECATED_SUPPRESS 244260919Spfg /* APPLE LOCAL "unavailable" attribute (radar 2809697) */ 245260919Spfg , DEPRECATED_UNAVAILABLE_SUPPRESS 24690287Sobrien}; 24718334Speter 24890287Sobrienstatic enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 24990287Sobrien 25018334Speter 25196292Sobrien/* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or 25296292Sobrien UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the 25396292Sobrien time the VAR_DECL was declared, the type was incomplete. */ 25496292Sobrien 255117410Skanstatic GTY(()) tree incomplete_vars; 25618334Speter 25790287Sobrien/* Returns the kind of template specialization we are currently 25890287Sobrien processing, given that it's declaration contained N_CLASS_SCOPES 25990287Sobrien explicit scope qualifications. */ 26090287Sobrien 26190287Sobrientmpl_spec_kind 262132747Skancurrent_tmpl_spec_kind (int n_class_scopes) 26390287Sobrien{ 26490287Sobrien int n_template_parm_scopes = 0; 26590287Sobrien int seen_specialization_p = 0; 26690287Sobrien int innermost_specialization_p = 0; 267117410Skan struct cp_binding_level *b; 26890287Sobrien 26990287Sobrien /* Scan through the template parameter scopes. */ 270169699Skan for (b = current_binding_level; 271169699Skan b->kind == sk_template_parms; 272132747Skan b = b->level_chain) 27390287Sobrien { 27490287Sobrien /* If we see a specialization scope inside a parameter scope, 27590287Sobrien then something is wrong. That corresponds to a declaration 27690287Sobrien like: 27790287Sobrien 27890287Sobrien template <class T> template <> ... 27990287Sobrien 280117410Skan which is always invalid since [temp.expl.spec] forbids the 28190287Sobrien specialization of a class member template if the enclosing 28290287Sobrien class templates are not explicitly specialized as well. */ 283132747Skan if (b->explicit_spec_p) 28490287Sobrien { 28590287Sobrien if (n_template_parm_scopes == 0) 28690287Sobrien innermost_specialization_p = 1; 28790287Sobrien else 28890287Sobrien seen_specialization_p = 1; 28990287Sobrien } 29090287Sobrien else if (seen_specialization_p == 1) 29190287Sobrien return tsk_invalid_member_spec; 29290287Sobrien 29390287Sobrien ++n_template_parm_scopes; 29490287Sobrien } 29590287Sobrien 29690287Sobrien /* Handle explicit instantiations. */ 29790287Sobrien if (processing_explicit_instantiation) 29890287Sobrien { 29990287Sobrien if (n_template_parm_scopes != 0) 30090287Sobrien /* We've seen a template parameter list during an explicit 30190287Sobrien instantiation. For example: 30290287Sobrien 30390287Sobrien template <class T> template void f(int); 30490287Sobrien 30590287Sobrien This is erroneous. */ 30690287Sobrien return tsk_invalid_expl_inst; 30790287Sobrien else 30890287Sobrien return tsk_expl_inst; 30990287Sobrien } 31090287Sobrien 31190287Sobrien if (n_template_parm_scopes < n_class_scopes) 31290287Sobrien /* We've not seen enough template headers to match all the 31390287Sobrien specialized classes present. For example: 31490287Sobrien 315169699Skan template <class T> void R<T>::S<T>::f(int); 31690287Sobrien 317117410Skan This is invalid; there needs to be one set of template 31890287Sobrien parameters for each class. */ 31990287Sobrien return tsk_insufficient_parms; 32090287Sobrien else if (n_template_parm_scopes == n_class_scopes) 32190287Sobrien /* We're processing a non-template declaration (even though it may 32290287Sobrien be a member of a template class.) For example: 32390287Sobrien 324169699Skan template <class T> void S<T>::f(int); 32590287Sobrien 32690287Sobrien The `class T' maches the `S<T>', leaving no template headers 32790287Sobrien corresponding to the `f'. */ 32890287Sobrien return tsk_none; 32990287Sobrien else if (n_template_parm_scopes > n_class_scopes + 1) 33090287Sobrien /* We've got too many template headers. For example: 33190287Sobrien 332169699Skan template <> template <class T> void f (T); 33390287Sobrien 33490287Sobrien There need to be more enclosing classes. */ 33590287Sobrien return tsk_excessive_parms; 33690287Sobrien else 33790287Sobrien /* This must be a template. It's of the form: 33890287Sobrien 339169699Skan template <class T> template <class U> void S<T>::f(U); 34090287Sobrien 34190287Sobrien This is a specialization if the innermost level was a 34290287Sobrien specialization; otherwise it's just a definition of the 34390287Sobrien template. */ 34490287Sobrien return innermost_specialization_p ? tsk_expl_spec : tsk_template; 34518334Speter} 34618334Speter 34790287Sobrien/* Exit the current scope. */ 34890287Sobrien 34990287Sobrienvoid 350132747Skanfinish_scope (void) 35190287Sobrien{ 35290287Sobrien poplevel (0, 0, 0); 35390287Sobrien} 35490287Sobrien 35590287Sobrien/* When a label goes out of scope, check to see if that label was used 35690287Sobrien in a valid manner, and issue any appropriate warnings or errors. */ 35790287Sobrien 35890287Sobrienstatic void 359132747Skanpop_label (tree label, tree old_value) 36090287Sobrien{ 361132747Skan if (!processing_template_decl) 36290287Sobrien { 36390287Sobrien if (DECL_INITIAL (label) == NULL_TREE) 36490287Sobrien { 365132747Skan location_t location; 366132747Skan 367169699Skan error ("label %q+D used but not defined", label); 368169699Skan#ifdef USE_MAPPED_LOCATION 369169699Skan location = input_location; /* FIXME want (input_filename, (line)0) */ 370169699Skan#else 371169699Skan location.file = input_filename; 372132747Skan location.line = 0; 373169699Skan#endif 37490287Sobrien /* Avoid crashing later. */ 375132747Skan define_label (location, DECL_NAME (label)); 37690287Sobrien } 377169699Skan else if (!TREE_USED (label)) 378169699Skan warning (OPT_Wunused_label, "label %q+D defined but not used", label); 37990287Sobrien } 38090287Sobrien 38190287Sobrien SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value); 38290287Sobrien} 38390287Sobrien 38490287Sobrien/* At the end of a function, all labels declared within the function 38590287Sobrien go out of scope. BLOCK is the top-level block for the 38690287Sobrien function. */ 38790287Sobrien 388169699Skanstatic int 389169699Skanpop_labels_1 (void **slot, void *data) 390169699Skan{ 391169699Skan struct named_label_entry *ent = (struct named_label_entry *) *slot; 392169699Skan tree block = (tree) data; 393169699Skan 394169699Skan pop_label (ent->label_decl, NULL_TREE); 395169699Skan 396169699Skan /* Put the labels into the "variables" of the top-level block, 397169699Skan so debugger can see them. */ 398169699Skan TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block); 399169699Skan BLOCK_VARS (block) = ent->label_decl; 400169699Skan 401169699Skan htab_clear_slot (named_labels, slot); 402169699Skan 403169699Skan return 1; 404169699Skan} 405169699Skan 40690287Sobrienstatic void 407132747Skanpop_labels (tree block) 40890287Sobrien{ 409169699Skan if (named_labels) 410169699Skan { 411169699Skan htab_traverse (named_labels, pop_labels_1, block); 412169699Skan named_labels = NULL; 413169699Skan } 414169699Skan} 41590287Sobrien 416169699Skan/* At the end of a block with local labels, restore the outer definition. */ 417169699Skan 418169699Skanstatic void 419169699Skanpop_local_label (tree label, tree old_value) 420169699Skan{ 421169699Skan struct named_label_entry dummy; 422169699Skan void **slot; 423169699Skan 424169699Skan pop_label (label, old_value); 425169699Skan 426169699Skan dummy.label_decl = label; 427169699Skan slot = htab_find_slot (named_labels, &dummy, NO_INSERT); 428169699Skan htab_clear_slot (named_labels, slot); 429169699Skan} 430169699Skan 431169699Skan/* The following two routines are used to interface to Objective-C++. 432169699Skan The binding level is purposely treated as an opaque type. */ 433169699Skan 434169699Skanvoid * 435169699Skanobjc_get_current_scope (void) 436169699Skan{ 437169699Skan return current_binding_level; 438169699Skan} 439169699Skan 440169699Skan/* The following routine is used by the NeXT-style SJLJ exceptions; 441169699Skan variables get marked 'volatile' so as to not be clobbered by 442169699Skan _setjmp()/_longjmp() calls. All variables in the current scope, 443169699Skan as well as parent scopes up to (but not including) ENCLOSING_BLK 444169699Skan shall be thusly marked. */ 445169699Skan 446169699Skanvoid 447169699Skanobjc_mark_locals_volatile (void *enclosing_blk) 448169699Skan{ 449169699Skan struct cp_binding_level *scope; 450169699Skan 451169699Skan for (scope = current_binding_level; 452169699Skan scope && scope != enclosing_blk; 453169699Skan scope = scope->level_chain) 45490287Sobrien { 455169699Skan tree decl; 456169699Skan 457169699Skan for (decl = scope->names; decl; decl = TREE_CHAIN (decl)) 458169699Skan objc_volatilize_decl (decl); 459169699Skan 460169699Skan /* Do not climb up past the current function. */ 461169699Skan if (scope->kind == sk_function_parms) 462169699Skan break; 46390287Sobrien } 464169699Skan} 46590287Sobrien 466169699Skan/* Update data for defined and undefined labels when leaving a scope. */ 467169699Skan 468169699Skanstatic int 469169699Skanpoplevel_named_label_1 (void **slot, void *data) 470169699Skan{ 471169699Skan struct named_label_entry *ent = (struct named_label_entry *) *slot; 472169699Skan struct cp_binding_level *bl = (struct cp_binding_level *) data; 473169699Skan struct cp_binding_level *obl = bl->level_chain; 474169699Skan 475169699Skan if (ent->binding_level == bl) 476169699Skan { 477169699Skan tree decl; 478169699Skan 479169699Skan for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl)) 480169699Skan if (decl_jump_unsafe (decl)) 481169699Skan ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls); 482169699Skan 483169699Skan ent->binding_level = obl; 484169699Skan ent->names_in_scope = obl->names; 485169699Skan switch (bl->kind) 486169699Skan { 487169699Skan case sk_try: 488169699Skan ent->in_try_scope = true; 489169699Skan break; 490169699Skan case sk_catch: 491169699Skan ent->in_catch_scope = true; 492169699Skan break; 493169699Skan case sk_omp: 494169699Skan ent->in_omp_scope = true; 495169699Skan break; 496169699Skan default: 497169699Skan break; 498169699Skan } 499169699Skan } 500169699Skan else if (ent->uses) 501169699Skan { 502169699Skan struct named_label_use_entry *use; 503169699Skan 504169699Skan for (use = ent->uses; use ; use = use->next) 505169699Skan if (use->binding_level == bl) 506169699Skan { 507169699Skan use->binding_level = obl; 508169699Skan use->names_in_scope = obl->names; 509169699Skan if (bl->kind == sk_omp) 510169699Skan use->in_omp_scope = true; 511169699Skan } 512169699Skan } 513169699Skan 514169699Skan return 1; 51590287Sobrien} 51690287Sobrien 51718334Speter/* Exit a binding level. 51818334Speter Pop the level off, and restore the state of the identifier-decl mappings 51918334Speter that were in effect when this level was entered. 52018334Speter 52118334Speter If KEEP == 1, this level had explicit declarations, so 52218334Speter and create a "block" (a BLOCK node) for the level 52318334Speter to record its declarations and subblocks for symbol table output. 52418334Speter 52518334Speter If FUNCTIONBODY is nonzero, this level is the body of a function, 52618334Speter so create a block as if KEEP were set and also clear out all 52718334Speter label names. 52818334Speter 52918334Speter If REVERSE is nonzero, reverse the order of decls before putting 53018334Speter them into the BLOCK. */ 53118334Speter 53218334Spetertree 533132747Skanpoplevel (int keep, int reverse, int functionbody) 53418334Speter{ 535132747Skan tree link; 53618334Speter /* The chain of decls was accumulated in reverse order. 53718334Speter Put it into forward order, just for cleanliness. */ 53818334Speter tree decls; 53918334Speter int tmp = functionbody; 54090287Sobrien int real_functionbody; 54190287Sobrien tree subblocks; 542169699Skan tree block; 54318334Speter tree decl; 54452290Sobrien int leaving_for_scope; 545132747Skan scope_kind kind; 54618334Speter 547117410Skan timevar_push (TV_NAME_LOOKUP); 548169699Skan restart: 549117410Skan 550169699Skan block = NULL_TREE; 55152290Sobrien 552169699Skan gcc_assert (current_binding_level->kind != sk_class); 553169699Skan 554132747Skan real_functionbody = (current_binding_level->kind == sk_cleanup 55590287Sobrien ? ((functionbody = 0), tmp) : functionbody); 55690287Sobrien subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; 55790287Sobrien 558169699Skan gcc_assert (!VEC_length(cp_class_binding, 559169699Skan current_binding_level->class_shadowed)); 56052290Sobrien 56152290Sobrien /* We used to use KEEP == 2 to indicate that the new block should go 56252290Sobrien at the beginning of the list of blocks at this binding level, 56352290Sobrien rather than the end. This hack is no longer used. */ 564169699Skan gcc_assert (keep == 0 || keep == 1); 56552290Sobrien 566132747Skan if (current_binding_level->keep) 56718334Speter keep = 1; 56818334Speter 56990287Sobrien /* Any uses of undefined labels, and any defined labels, now operate 57090287Sobrien under constraints of next binding contour. */ 571169699Skan if (cfun && !functionbody && named_labels) 572169699Skan htab_traverse (named_labels, poplevel_named_label_1, 573169699Skan current_binding_level); 57490287Sobrien 57518334Speter /* Get the decls in the order they were written. 57618334Speter Usually current_binding_level->names is in reverse order. 57718334Speter But parameter decls were previously put in forward order. */ 57818334Speter 57918334Speter if (reverse) 58018334Speter current_binding_level->names 58118334Speter = decls = nreverse (current_binding_level->names); 58218334Speter else 58318334Speter decls = current_binding_level->names; 58418334Speter 58518334Speter /* If there were any declarations or structure tags in that level, 58618334Speter or if this level is a function body, 58718334Speter create a BLOCK to record them for the life of this function. */ 58818334Speter block = NULL_TREE; 58990287Sobrien if (keep == 1 || functionbody) 59018334Speter block = make_node (BLOCK); 59118334Speter if (block != NULL_TREE) 59218334Speter { 59390287Sobrien BLOCK_VARS (block) = decls; 59490287Sobrien BLOCK_SUBBLOCKS (block) = subblocks; 59518334Speter } 59618334Speter 59718334Speter /* In each subblock, record that this is its superior. */ 59818334Speter if (keep >= 0) 59918334Speter for (link = subblocks; link; link = TREE_CHAIN (link)) 60018334Speter BLOCK_SUPERCONTEXT (link) = block; 60118334Speter 60252290Sobrien /* We still support the old for-scope rules, whereby the variables 60352290Sobrien in a for-init statement were in scope after the for-statement 604132747Skan ended. We only use the new rules if flag_new_for_scope is 60552290Sobrien nonzero. */ 60690287Sobrien leaving_for_scope 607132747Skan = current_binding_level->kind == sk_for && flag_new_for_scope == 1; 60818334Speter 609169699Skan /* Before we remove the declarations first check for unused variables. */ 610169699Skan if (warn_unused_variable 611169699Skan && !processing_template_decl) 612169699Skan for (decl = getdecls (); decl; decl = TREE_CHAIN (decl)) 613169699Skan if (TREE_CODE (decl) == VAR_DECL 614169699Skan && ! TREE_USED (decl) 615169699Skan && ! DECL_IN_SYSTEM_HEADER (decl) 616169699Skan && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)) 617169699Skan warning (OPT_Wunused_variable, "unused variable %q+D", decl); 618169699Skan 61952290Sobrien /* Remove declarations for all the DECLs in this level. */ 62052290Sobrien for (link = decls; link; link = TREE_CHAIN (link)) 62118334Speter { 62290287Sobrien if (leaving_for_scope && TREE_CODE (link) == VAR_DECL 623169699Skan && DECL_NAME (link)) 62418334Speter { 625169699Skan tree name = DECL_NAME (link); 626169699Skan cxx_binding *ob; 62752290Sobrien tree ns_binding; 62852290Sobrien 629169699Skan ob = outer_binding (name, 630169699Skan IDENTIFIER_BINDING (name), 631169699Skan /*class_p=*/true); 632169699Skan if (!ob) 633169699Skan ns_binding = IDENTIFIER_NAMESPACE_VALUE (name); 63451412Sobrien else 63552290Sobrien ns_binding = NULL_TREE; 63651412Sobrien 637169699Skan if (ob && ob->scope == current_binding_level->level_chain) 63852290Sobrien /* We have something like: 63990287Sobrien 640169699Skan int i; 641169699Skan for (int i; ;); 64290287Sobrien 64352290Sobrien and we are leaving the `for' scope. There's no reason to 64452290Sobrien keep the binding of the inner `i' in this case. */ 645169699Skan pop_binding (name, link); 646169699Skan else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL)) 647132747Skan || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL)) 64852290Sobrien /* Here, we have something like: 64951412Sobrien 65052290Sobrien typedef int I; 65151412Sobrien 65252290Sobrien void f () { 65352290Sobrien for (int I; ;); 65452290Sobrien } 65552290Sobrien 65652290Sobrien We must pop the for-scope binding so we know what's a 65752290Sobrien type and what isn't. */ 658169699Skan pop_binding (name, link); 65952290Sobrien else 66051412Sobrien { 66152290Sobrien /* Mark this VAR_DECL as dead so that we can tell we left it 66252290Sobrien there only for backward compatibility. */ 66352290Sobrien DECL_DEAD_FOR_LOCAL (link) = 1; 66490287Sobrien 665132747Skan /* Keep track of what should have happened when we 66652290Sobrien popped the binding. */ 667169699Skan if (ob && ob->value) 668169699Skan { 669169699Skan SET_DECL_SHADOWED_FOR_VAR (link, ob->value); 670169699Skan DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1; 671169699Skan } 67252290Sobrien 67352290Sobrien /* Add it to the list of dead variables in the next 67452290Sobrien outermost binding to that we can remove these when we 67552290Sobrien leave that binding. */ 67652290Sobrien current_binding_level->level_chain->dead_vars_from_for 67752290Sobrien = tree_cons (NULL_TREE, link, 67852290Sobrien current_binding_level->level_chain-> 67952290Sobrien dead_vars_from_for); 68052290Sobrien 681117410Skan /* Although we don't pop the cxx_binding, we do clear 682132747Skan its SCOPE since the scope is going away now. */ 683169699Skan IDENTIFIER_BINDING (name)->scope 684169699Skan = current_binding_level->level_chain; 68551412Sobrien } 68651412Sobrien } 68790287Sobrien else 68818334Speter { 689169699Skan tree name; 690169699Skan 69152290Sobrien /* Remove the binding. */ 69252290Sobrien decl = link; 693169699Skan 69452290Sobrien if (TREE_CODE (decl) == TREE_LIST) 69552290Sobrien decl = TREE_VALUE (decl); 696169699Skan name = decl; 697169699Skan 698169699Skan if (TREE_CODE (name) == OVERLOAD) 699169699Skan name = OVL_FUNCTION (name); 700169699Skan 701169699Skan gcc_assert (DECL_P (name)); 702169699Skan pop_binding (DECL_NAME (name), decl); 70318334Speter } 70452290Sobrien } 70518334Speter 70652290Sobrien /* Remove declarations for any `for' variables from inner scopes 70752290Sobrien that we kept around. */ 70852290Sobrien for (link = current_binding_level->dead_vars_from_for; 70952290Sobrien link; link = TREE_CHAIN (link)) 71052290Sobrien pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link)); 71118334Speter 71252290Sobrien /* Restore the IDENTIFIER_TYPE_VALUEs. */ 71352290Sobrien for (link = current_binding_level->type_shadowed; 71452290Sobrien link; link = TREE_CHAIN (link)) 71552290Sobrien SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); 71690287Sobrien 71790287Sobrien /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */ 71890287Sobrien for (link = current_binding_level->shadowed_labels; 71990287Sobrien link; 72090287Sobrien link = TREE_CHAIN (link)) 721169699Skan pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link)); 72290287Sobrien 72352290Sobrien /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs 72452290Sobrien list if a `using' declaration put them there. The debugging 72552290Sobrien back-ends won't understand OVERLOAD, so we remove them here. 72652290Sobrien Because the BLOCK_VARS are (temporarily) shared with 72752290Sobrien CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have 72852290Sobrien popped all the bindings. */ 72952290Sobrien if (block) 73052290Sobrien { 73152290Sobrien tree* d; 73251412Sobrien 73352290Sobrien for (d = &BLOCK_VARS (block); *d; ) 73418334Speter { 73552290Sobrien if (TREE_CODE (*d) == TREE_LIST) 73652290Sobrien *d = TREE_CHAIN (*d); 73752290Sobrien else 73852290Sobrien d = &TREE_CHAIN (*d); 73918334Speter } 74018334Speter } 74118334Speter 74218334Speter /* If the level being exited is the top level of a function, 74318334Speter check over all the labels. */ 74418334Speter if (functionbody) 74518334Speter { 74690287Sobrien /* Since this is the top level block of a function, the vars are 74790287Sobrien the function's parameters. Don't leave them in the BLOCK 74890287Sobrien because they are found in the FUNCTION_DECL instead. */ 74918334Speter BLOCK_VARS (block) = 0; 75090287Sobrien pop_labels (block); 75118334Speter } 75218334Speter 753132747Skan kind = current_binding_level->kind; 754169699Skan if (kind == sk_cleanup) 755169699Skan { 756169699Skan tree stmt; 75718334Speter 758169699Skan /* If this is a temporary binding created for a cleanup, then we'll 759169699Skan have pushed a statement list level. Pop that, create a new 760169699Skan BIND_EXPR for the block, and insert it into the stream. */ 761169699Skan stmt = pop_stmt_list (current_binding_level->statement_list); 762169699Skan stmt = c_build_bind_expr (block, stmt); 763169699Skan add_stmt (stmt); 764169699Skan } 765169699Skan 766132747Skan leave_scope (); 76718334Speter if (functionbody) 768260140Spfg { 769260140Spfg /* The current function is being defined, so its DECL_INITIAL 770260140Spfg should be error_mark_node. */ 771260140Spfg gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node); 772260140Spfg DECL_INITIAL (current_function_decl) = block; 773260140Spfg } 77418334Speter else if (block) 77590287Sobrien current_binding_level->blocks 77690287Sobrien = chainon (current_binding_level->blocks, block); 77790287Sobrien 77818334Speter /* If we did not make a block for the level just exited, 77918334Speter any blocks made for inner levels 78018334Speter (since they cannot be recorded as subblocks in that level) 78118334Speter must be carried forward so they will later become subblocks 78218334Speter of something else. */ 78318334Speter else if (subblocks) 78452290Sobrien current_binding_level->blocks 78552290Sobrien = chainon (current_binding_level->blocks, subblocks); 78618334Speter 78790287Sobrien /* Each and every BLOCK node created here in `poplevel' is important 78890287Sobrien (e.g. for proper debugging information) so if we created one 78990287Sobrien earlier, mark it as "used". */ 79090287Sobrien if (block) 79190287Sobrien TREE_USED (block) = 1; 79290287Sobrien 793169699Skan /* All temporary bindings created for cleanups are popped silently. */ 794132747Skan if (kind == sk_cleanup) 795169699Skan goto restart; 79690287Sobrien 797117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block); 79818334Speter} 79918334Speter 80018334Speter/* Insert BLOCK at the end of the list of subblocks of the 80118334Speter current binding level. This is used when a BIND_EXPR is expanded, 80218334Speter to handle the BLOCK node inside the BIND_EXPR. */ 80318334Speter 80418334Spetervoid 805132747Skaninsert_block (tree block) 80618334Speter{ 80718334Speter TREE_USED (block) = 1; 80818334Speter current_binding_level->blocks 80918334Speter = chainon (current_binding_level->blocks, block); 81018334Speter} 81118334Speter 81252290Sobrien/* Walk all the namespaces contained NAMESPACE, including NAMESPACE 81352290Sobrien itself, calling F for each. The DATA is passed to F as well. */ 81452290Sobrien 81552290Sobrienstatic int 816132747Skanwalk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data) 81752290Sobrien{ 81852290Sobrien int result = 0; 819169699Skan tree current = NAMESPACE_LEVEL (namespace)->namespaces; 82052290Sobrien 82152290Sobrien result |= (*f) (namespace, data); 82252290Sobrien 823117410Skan for (; current; current = TREE_CHAIN (current)) 824117410Skan result |= walk_namespaces_r (current, f, data); 82552290Sobrien 82652290Sobrien return result; 82752290Sobrien} 82852290Sobrien 82952290Sobrien/* Walk all the namespaces, calling F for each. The DATA is passed to 83052290Sobrien F as well. */ 83152290Sobrien 83252290Sobrienint 833132747Skanwalk_namespaces (walk_namespaces_fn f, void* data) 83452290Sobrien{ 83552290Sobrien return walk_namespaces_r (global_namespace, f, data); 83652290Sobrien} 83752290Sobrien 83852290Sobrien/* Call wrapup_globals_declarations for the globals in NAMESPACE. If 83952290Sobrien DATA is non-NULL, this is the last time we will call 84052290Sobrien wrapup_global_declarations for this NAMESPACE. */ 84152290Sobrien 84252290Sobrienint 843132747Skanwrapup_globals_for_namespace (tree namespace, void* data) 84452290Sobrien{ 845117410Skan struct cp_binding_level *level = NAMESPACE_LEVEL (namespace); 846169699Skan VEC(tree,gc) *statics = level->static_decls; 847169699Skan tree *vec = VEC_address (tree, statics); 848169699Skan int len = VEC_length (tree, statics); 84952290Sobrien int last_time = (data != 0); 85052290Sobrien 85152290Sobrien if (last_time) 85252290Sobrien { 85352290Sobrien check_global_declarations (vec, len); 854169699Skan emit_debug_global_declarations (vec, len); 85552290Sobrien return 0; 85652290Sobrien } 85752290Sobrien 85852290Sobrien /* Write out any globals that need to be output. */ 859117410Skan return wrapup_global_declarations (vec, len); 86052290Sobrien} 86152290Sobrien 86218334Speter 86390287Sobrien/* In C++, you don't have to write `struct S' to refer to `S'; you 86490287Sobrien can just use `S'. We accomplish this by creating a TYPE_DECL as 86590287Sobrien if the user had written `typedef struct S S'. Create and return 86690287Sobrien the TYPE_DECL for TYPE. */ 86790287Sobrien 86890287Sobrientree 869132747Skancreate_implicit_typedef (tree name, tree type) 87090287Sobrien{ 87190287Sobrien tree decl; 87290287Sobrien 87390287Sobrien decl = build_decl (TYPE_DECL, name, type); 87490287Sobrien DECL_ARTIFICIAL (decl) = 1; 87590287Sobrien /* There are other implicit type declarations, like the one *within* 87690287Sobrien a class that allows you to write `S::S'. We must distinguish 87790287Sobrien amongst these. */ 87890287Sobrien SET_DECL_IMPLICIT_TYPEDEF_P (decl); 87990287Sobrien TYPE_NAME (type) = decl; 88090287Sobrien 88190287Sobrien return decl; 88290287Sobrien} 88390287Sobrien 88490287Sobrien/* Remember a local name for name-mangling purposes. */ 88590287Sobrien 88690287Sobrienstatic void 887132747Skanpush_local_name (tree decl) 88890287Sobrien{ 88990287Sobrien size_t i, nelts; 89090287Sobrien tree t, name; 89190287Sobrien 892117410Skan timevar_push (TV_NAME_LOOKUP); 89390287Sobrien 89490287Sobrien name = DECL_NAME (decl); 89590287Sobrien 896169699Skan nelts = VEC_length (tree, local_names); 89790287Sobrien for (i = 0; i < nelts; i++) 89890287Sobrien { 899169699Skan t = VEC_index (tree, local_names, i); 90090287Sobrien if (DECL_NAME (t) == name) 90190287Sobrien { 90290287Sobrien if (!DECL_LANG_SPECIFIC (decl)) 90390287Sobrien retrofit_lang_decl (decl); 904117410Skan DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1; 90590287Sobrien if (DECL_LANG_SPECIFIC (t)) 90690287Sobrien DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1; 90790287Sobrien else 90890287Sobrien DECL_DISCRIMINATOR (decl) = 1; 90990287Sobrien 910169699Skan VEC_replace (tree, local_names, i, decl); 911132747Skan timevar_pop (TV_NAME_LOOKUP); 912132747Skan return; 91390287Sobrien } 91490287Sobrien } 91590287Sobrien 916169699Skan VEC_safe_push (tree, gc, local_names, decl); 917117410Skan timevar_pop (TV_NAME_LOOKUP); 91890287Sobrien} 91918334Speter 92018334Speter/* Subroutine of duplicate_decls: return truthvalue of whether 92118334Speter or not types of these decls match. 92218334Speter 92318334Speter For C++, we must compare the parameter list so that `int' can match 92418334Speter `int&' in a parameter position, but `int&' is not confused with 92518334Speter `const int&'. */ 92651412Sobrien 92718334Speterint 928132747Skandecls_match (tree newdecl, tree olddecl) 92918334Speter{ 93018334Speter int types_match; 93118334Speter 93252290Sobrien if (newdecl == olddecl) 93352290Sobrien return 1; 93452290Sobrien 93552290Sobrien if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) 93652290Sobrien /* If the two DECLs are not even the same kind of thing, we're not 93752290Sobrien interested in their types. */ 93852290Sobrien return 0; 93952290Sobrien 94052290Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 94118334Speter { 94218334Speter tree f1 = TREE_TYPE (newdecl); 94318334Speter tree f2 = TREE_TYPE (olddecl); 94418334Speter tree p1 = TYPE_ARG_TYPES (f1); 94518334Speter tree p2 = TYPE_ARG_TYPES (f2); 94618334Speter 94790287Sobrien if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 94890287Sobrien && ! (DECL_EXTERN_C_P (newdecl) 94990287Sobrien && DECL_EXTERN_C_P (olddecl))) 95051412Sobrien return 0; 95151412Sobrien 95218334Speter if (TREE_CODE (f1) != TREE_CODE (f2)) 953169699Skan return 0; 95418334Speter 95552290Sobrien if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) 95618334Speter { 95790287Sobrien if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl) 95890287Sobrien && (DECL_BUILT_IN (olddecl) 95990287Sobrien#ifndef NO_IMPLICIT_EXTERN_C 960169699Skan || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl)) 961169699Skan || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl)) 96290287Sobrien#endif 96390287Sobrien )) 96418334Speter { 96518334Speter types_match = self_promoting_args_p (p1); 96618334Speter if (p1 == void_list_node) 96718334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 96818334Speter } 96990287Sobrien#ifndef NO_IMPLICIT_EXTERN_C 97090287Sobrien else if (p1 == NULL_TREE 97190287Sobrien && (DECL_EXTERN_C_P (olddecl) 972169699Skan && DECL_IN_SYSTEM_HEADER (olddecl) 973169699Skan && !DECL_CLASS_SCOPE_P (olddecl)) 97490287Sobrien && (DECL_EXTERN_C_P (newdecl) 975169699Skan && DECL_IN_SYSTEM_HEADER (newdecl) 976169699Skan && !DECL_CLASS_SCOPE_P (newdecl))) 97718334Speter { 97818334Speter types_match = self_promoting_args_p (p2); 97918334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 98018334Speter } 98190287Sobrien#endif 98218334Speter else 98352290Sobrien types_match = compparms (p1, p2); 98418334Speter } 98518334Speter else 98618334Speter types_match = 0; 98718334Speter } 98852290Sobrien else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 98918334Speter { 990132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) 991132747Skan != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))) 992132747Skan return 0; 993132747Skan 99451412Sobrien if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), 99551412Sobrien DECL_TEMPLATE_PARMS (olddecl))) 99651412Sobrien return 0; 99790287Sobrien 99851412Sobrien if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 999132747Skan types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)), 1000132747Skan TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))); 100151412Sobrien else 100251412Sobrien types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl), 100351412Sobrien DECL_TEMPLATE_RESULT (newdecl)); 100418334Speter } 100518334Speter else 100618334Speter { 1007146906Skan /* Need to check scope for variable declaration (VAR_DECL). 1008146906Skan For typedef (TYPE_DECL), scope is ignored. */ 1009146906Skan if (TREE_CODE (newdecl) == VAR_DECL 1010169699Skan && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 1011169699Skan /* [dcl.link] 1012169699Skan Two declarations for an object with C language linkage 1013169699Skan with the same name (ignoring the namespace that qualify 1014169699Skan it) that appear in different namespace scopes refer to 1015169699Skan the same object. */ 1016169699Skan && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl))) 1017146906Skan return 0; 1018146906Skan 101918334Speter if (TREE_TYPE (newdecl) == error_mark_node) 102018334Speter types_match = TREE_TYPE (olddecl) == error_mark_node; 102118334Speter else if (TREE_TYPE (olddecl) == NULL_TREE) 102218334Speter types_match = TREE_TYPE (newdecl) == NULL_TREE; 102318334Speter else if (TREE_TYPE (newdecl) == NULL_TREE) 102418334Speter types_match = 0; 102518334Speter else 102652290Sobrien types_match = comptypes (TREE_TYPE (newdecl), 102752290Sobrien TREE_TYPE (olddecl), 102852290Sobrien COMPARE_REDECLARATION); 102918334Speter } 103018334Speter 103118334Speter return types_match; 103218334Speter} 103318334Speter 103418334Speter/* If NEWDECL is `static' and an `extern' was seen previously, 103590287Sobrien warn about it. OLDDECL is the previous declaration. 103618334Speter 103718334Speter Note that this does not apply to the C++ case of declaring 103818334Speter a variable `extern const' and then later `const'. 103918334Speter 104018334Speter Don't complain about built-in functions, since they are beyond 104118334Speter the user's control. */ 104218334Speter 1043132747Skanvoid 1044132747Skanwarn_extern_redeclared_static (tree newdecl, tree olddecl) 104518334Speter{ 104690287Sobrien tree name; 104790287Sobrien 104890287Sobrien if (TREE_CODE (newdecl) == TYPE_DECL 104990287Sobrien || TREE_CODE (newdecl) == TEMPLATE_DECL 1050132747Skan || TREE_CODE (newdecl) == CONST_DECL 1051132747Skan || TREE_CODE (newdecl) == NAMESPACE_DECL) 105218334Speter return; 105318334Speter 105490287Sobrien /* Don't get confused by static member functions; that's a different 105590287Sobrien use of `static'. */ 105690287Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL 105790287Sobrien && DECL_STATIC_FUNCTION_P (newdecl)) 105890287Sobrien return; 105990287Sobrien 106090287Sobrien /* If the old declaration was `static', or the new one isn't, then 106190287Sobrien then everything is OK. */ 106290287Sobrien if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) 106390287Sobrien return; 106490287Sobrien 106590287Sobrien /* It's OK to declare a builtin function as `static'. */ 106690287Sobrien if (TREE_CODE (olddecl) == FUNCTION_DECL 106790287Sobrien && DECL_ARTIFICIAL (olddecl)) 106890287Sobrien return; 106990287Sobrien 107018334Speter name = DECL_ASSEMBLER_NAME (newdecl); 1071169699Skan pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl); 1072169699Skan pedwarn ("previous declaration of %q+D", olddecl); 107318334Speter} 107418334Speter 1075169699Skan/* NEW_DECL is a redeclaration of OLD_DECL; both are functions or 1076169699Skan function templates. If their exception specifications do not 1077169699Skan match, issue an a diagnostic. */ 1078169699Skan 1079169699Skanstatic void 1080169699Skancheck_redeclaration_exception_specification (tree new_decl, 1081169699Skan tree old_decl) 1082169699Skan{ 1083169699Skan tree new_type; 1084169699Skan tree old_type; 1085169699Skan tree new_exceptions; 1086169699Skan tree old_exceptions; 1087169699Skan 1088169699Skan new_type = TREE_TYPE (new_decl); 1089169699Skan new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type); 1090169699Skan old_type = TREE_TYPE (old_decl); 1091169699Skan old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type); 1092169699Skan 1093169699Skan /* [except.spec] 1094169699Skan 1095169699Skan If any declaration of a function has an exception-specification, 1096169699Skan all declarations, including the definition and an explicit 1097169699Skan specialization, of that function shall have an 1098169699Skan exception-specification with the same set of type-ids. */ 1099169699Skan if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl)) 1100169699Skan && ! DECL_IS_BUILTIN (old_decl) 1101169699Skan && flag_exceptions 1102169699Skan && !comp_except_specs (new_exceptions, old_exceptions, 1103169699Skan /*exact=*/true)) 1104169699Skan { 1105169699Skan error ("declaration of %qF throws different exceptions", new_decl); 1106169699Skan error ("from previous declaration %q+F", old_decl); 1107169699Skan } 1108169699Skan} 1109169699Skan 1110132747Skan/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations. 1111132747Skan If the redeclaration is invalid, a diagnostic is issued, and the 1112132747Skan error_mark_node is returned. Otherwise, OLDDECL is returned. 111318334Speter 1114132747Skan If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is 1115169699Skan returned. 111618334Speter 1117169699Skan NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */ 1118169699Skan 1119132747Skantree 1120169699Skanduplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend) 112118334Speter{ 112218334Speter unsigned olddecl_uid = DECL_UID (olddecl); 1123169699Skan int olddecl_friend = 0, types_match = 0, hidden_friend = 0; 112451412Sobrien int new_defines_function = 0; 1125169699Skan tree new_template; 112618334Speter 112751412Sobrien if (newdecl == olddecl) 1128132747Skan return olddecl; 112918334Speter 113018334Speter types_match = decls_match (newdecl, olddecl); 113118334Speter 113218334Speter /* If either the type of the new decl or the type of the old decl is an 113318334Speter error_mark_node, then that implies that we have already issued an 113418334Speter error (earlier) for some bogus type specification, and in that case, 113518334Speter it is rather pointless to harass the user with yet more error message 113651412Sobrien about the same declaration, so just pretend the types match here. */ 113751412Sobrien if (TREE_TYPE (newdecl) == error_mark_node 113851412Sobrien || TREE_TYPE (olddecl) == error_mark_node) 1139169699Skan return error_mark_node; 114090287Sobrien 114190287Sobrien if (DECL_P (olddecl) 114290287Sobrien && TREE_CODE (newdecl) == FUNCTION_DECL 114390287Sobrien && TREE_CODE (olddecl) == FUNCTION_DECL 114490287Sobrien && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl))) 114590287Sobrien { 114690287Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 114790287Sobrien && DECL_UNINLINABLE (newdecl) 114890287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 114990287Sobrien /* Already warned elsewhere. */; 115090287Sobrien else if (DECL_DECLARED_INLINE_P (olddecl) 115190287Sobrien && DECL_UNINLINABLE (olddecl) 115290287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 115390287Sobrien /* Already warned. */; 115490287Sobrien else if (DECL_DECLARED_INLINE_P (newdecl) 115590287Sobrien && DECL_UNINLINABLE (olddecl) 115690287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 115790287Sobrien { 1158169699Skan warning (OPT_Wattributes, "function %q+D redeclared as inline", 1159169699Skan newdecl); 1160169699Skan warning (OPT_Wattributes, "previous declaration of %q+D " 1161169699Skan "with attribute noinline", olddecl); 116290287Sobrien } 116390287Sobrien else if (DECL_DECLARED_INLINE_P (olddecl) 116490287Sobrien && DECL_UNINLINABLE (newdecl) 116590287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 116690287Sobrien { 1167169699Skan warning (OPT_Wattributes, "function %q+D redeclared with " 1168169699Skan "attribute noinline", newdecl); 1169169699Skan warning (OPT_Wattributes, "previous declaration of %q+D was inline", 1170169699Skan olddecl); 117190287Sobrien } 117290287Sobrien } 117390287Sobrien 1174117410Skan /* Check for redeclaration and other discrepancies. */ 117551412Sobrien if (TREE_CODE (olddecl) == FUNCTION_DECL 117690287Sobrien && DECL_ARTIFICIAL (olddecl)) 117718334Speter { 1178169699Skan gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl)); 117990287Sobrien if (TREE_CODE (newdecl) != FUNCTION_DECL) 118018334Speter { 1181169699Skan /* Avoid warnings redeclaring built-ins which have not been 1182169699Skan explicitly declared. */ 1183169699Skan if (DECL_ANTICIPATED (olddecl)) 1184169699Skan return NULL_TREE; 118596292Sobrien 118690287Sobrien /* If you declare a built-in or predefined function name as static, 118790287Sobrien the old definition is overridden, but optionally warn this was a 118890287Sobrien bad choice of name. */ 118990287Sobrien if (! TREE_PUBLIC (newdecl)) 119090287Sobrien { 1191169699Skan warning (OPT_Wshadow, "shadowing %s function %q#D", 1192169699Skan DECL_BUILT_IN (olddecl) ? "built-in" : "library", 1193169699Skan olddecl); 119490287Sobrien /* Discard the old built-in function. */ 1195132747Skan return NULL_TREE; 119690287Sobrien } 119790287Sobrien /* If the built-in is not ansi, then programs can override 119890287Sobrien it even globally without an error. */ 119990287Sobrien else if (! DECL_BUILT_IN (olddecl)) 1200169699Skan warning (0, "library function %q#D redeclared as non-function %q#D", 1201169699Skan olddecl, newdecl); 120290287Sobrien else 120390287Sobrien { 1204169699Skan error ("declaration of %q#D", newdecl); 1205169699Skan error ("conflicts with built-in declaration %q#D", 1206169699Skan olddecl); 120790287Sobrien } 1208132747Skan return NULL_TREE; 120918334Speter } 121090287Sobrien else if (!types_match) 121118334Speter { 1212169699Skan /* Avoid warnings redeclaring built-ins which have not been 1213169699Skan explicitly declared. */ 1214169699Skan if (DECL_ANTICIPATED (olddecl)) 1215169699Skan { 1216169699Skan /* Deal with fileptr_type_node. FILE type is not known 1217169699Skan at the time we create the builtins. */ 1218169699Skan tree t1, t2; 1219169699Skan 1220169699Skan for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1221169699Skan t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 1222169699Skan t1 || t2; 1223169699Skan t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) 1224169699Skan if (!t1 || !t2) 1225169699Skan break; 1226169699Skan else if (TREE_VALUE (t2) == fileptr_type_node) 1227169699Skan { 1228169699Skan tree t = TREE_VALUE (t1); 1229169699Skan 1230169699Skan if (TREE_CODE (t) == POINTER_TYPE 1231169699Skan && TYPE_NAME (TREE_TYPE (t)) 1232169699Skan && DECL_NAME (TYPE_NAME (TREE_TYPE (t))) 1233169699Skan == get_identifier ("FILE") 1234169699Skan && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2))) 1235169699Skan { 1236169699Skan tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 1237169699Skan 1238169699Skan TYPE_ARG_TYPES (TREE_TYPE (olddecl)) 1239169699Skan = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); 1240169699Skan types_match = decls_match (newdecl, olddecl); 1241169699Skan if (types_match) 1242169699Skan return duplicate_decls (newdecl, olddecl, 1243169699Skan newdecl_is_friend); 1244169699Skan TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs; 1245169699Skan } 1246169699Skan } 1247169699Skan else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) 1248169699Skan break; 1249169699Skan } 125096292Sobrien else if ((DECL_EXTERN_C_P (newdecl) 1251122192Skan && DECL_EXTERN_C_P (olddecl)) 1252122192Skan || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1253122192Skan TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 125418334Speter { 125590287Sobrien /* A near match; override the builtin. */ 125690287Sobrien 125790287Sobrien if (TREE_PUBLIC (newdecl)) 125818334Speter { 1259169699Skan warning (0, "new declaration %q#D", newdecl); 1260169699Skan warning (0, "ambiguates built-in declaration %q#D", 1261169699Skan olddecl); 126290287Sobrien } 1263169699Skan else 1264169699Skan warning (OPT_Wshadow, "shadowing %s function %q#D", 1265169699Skan DECL_BUILT_IN (olddecl) ? "built-in" : "library", 1266169699Skan olddecl); 126718334Speter } 126890287Sobrien else 126990287Sobrien /* Discard the old built-in function. */ 1270132747Skan return NULL_TREE; 127196292Sobrien 127296292Sobrien /* Replace the old RTL to avoid problems with inlining. */ 1273169699Skan COPY_DECL_RTL (newdecl, olddecl); 127490287Sobrien } 1275169699Skan /* Even if the types match, prefer the new declarations type for 1276169699Skan built-ins which have not been explicitly declared, for 1277169699Skan exception lists, etc... */ 1278117410Skan else if (DECL_ANTICIPATED (olddecl)) 1279132747Skan { 1280132747Skan tree type = TREE_TYPE (newdecl); 1281132747Skan tree attribs = (*targetm.merge_type_attributes) 1282132747Skan (TREE_TYPE (olddecl), type); 128318334Speter 1284132747Skan type = cp_build_type_attribute_variant (type, attribs); 1285132747Skan TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type; 1286132747Skan } 1287132747Skan 1288122192Skan /* Whether or not the builtin can throw exceptions has no 1289122192Skan bearing on this declarator. */ 1290122192Skan TREE_NOTHROW (olddecl) = 0; 1291122192Skan 129290287Sobrien if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) 129390287Sobrien { 129490287Sobrien /* If a builtin function is redeclared as `static', merge 129590287Sobrien the declarations, but make the original one static. */ 129690287Sobrien DECL_THIS_STATIC (olddecl) = 1; 129790287Sobrien TREE_PUBLIC (olddecl) = 0; 129890287Sobrien 129990287Sobrien /* Make the old declaration consistent with the new one so 130090287Sobrien that all remnants of the builtin-ness of this function 130190287Sobrien will be banished. */ 130290287Sobrien SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 1303169699Skan COPY_DECL_RTL (newdecl, olddecl); 130418334Speter } 130518334Speter } 130618334Speter else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 130718334Speter { 130851412Sobrien if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl) 130951412Sobrien && TREE_CODE (newdecl) != TYPE_DECL 131051412Sobrien && ! (TREE_CODE (newdecl) == TEMPLATE_DECL 131151412Sobrien && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)) 131251412Sobrien || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl) 131351412Sobrien && TREE_CODE (olddecl) != TYPE_DECL 131451412Sobrien && ! (TREE_CODE (olddecl) == TEMPLATE_DECL 131551412Sobrien && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) 131651412Sobrien == TYPE_DECL)))) 131751412Sobrien { 131851412Sobrien /* We do nothing special here, because C++ does such nasty 131951412Sobrien things with TYPE_DECLs. Instead, just let the TYPE_DECL 132051412Sobrien get shadowed, and know that if we need to find a TYPE_DECL 132151412Sobrien for a given name, we can look in the IDENTIFIER_TYPE_VALUE 132251412Sobrien slot of the identifier. */ 1323132747Skan return NULL_TREE; 132451412Sobrien } 132551412Sobrien 132618334Speter if ((TREE_CODE (newdecl) == FUNCTION_DECL 132751412Sobrien && DECL_FUNCTION_TEMPLATE_P (olddecl)) 132818334Speter || (TREE_CODE (olddecl) == FUNCTION_DECL 132951412Sobrien && DECL_FUNCTION_TEMPLATE_P (newdecl))) 1330132747Skan return NULL_TREE; 133151412Sobrien 1332169699Skan error ("%q#D redeclared as different kind of symbol", newdecl); 133318334Speter if (TREE_CODE (olddecl) == TREE_LIST) 133418334Speter olddecl = TREE_VALUE (olddecl); 1335169699Skan error ("previous declaration of %q+#D", olddecl); 133618334Speter 1337169699Skan return error_mark_node; 133818334Speter } 133918334Speter else if (!types_match) 134018334Speter { 134190287Sobrien if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) 134252290Sobrien /* These are certainly not duplicate declarations; they're 134352290Sobrien from different scopes. */ 1344132747Skan return NULL_TREE; 134552290Sobrien 134618334Speter if (TREE_CODE (newdecl) == TEMPLATE_DECL) 134718334Speter { 134818334Speter /* The name of a class template may not be declared to refer to 134918334Speter any other template, class, function, object, namespace, value, 135051412Sobrien or type in the same scope. */ 135151412Sobrien if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL 135251412Sobrien || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 135318334Speter { 1354169699Skan error ("declaration of template %q#D", newdecl); 1355169699Skan error ("conflicts with previous declaration %q+#D", olddecl); 135618334Speter } 135751412Sobrien else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL 135851412Sobrien && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL 135951412Sobrien && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))), 136052290Sobrien TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)))) 136151412Sobrien && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), 1362117410Skan DECL_TEMPLATE_PARMS (olddecl)) 1363117410Skan /* Template functions can be disambiguated by 1364117410Skan return type. */ 1365117410Skan && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)), 1366117410Skan TREE_TYPE (TREE_TYPE (olddecl)))) 136751412Sobrien { 1368169699Skan error ("new declaration %q#D", newdecl); 1369169699Skan error ("ambiguates old declaration %q+#D", olddecl); 137051412Sobrien } 1371132747Skan return NULL_TREE; 137218334Speter } 137318334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 137418334Speter { 137590287Sobrien if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) 137618334Speter { 1377169699Skan error ("declaration of C function %q#D conflicts with", 1378169699Skan newdecl); 1379169699Skan error ("previous declaration %q+#D here", olddecl); 138018334Speter } 138118334Speter else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 138252290Sobrien TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 138318334Speter { 1384169699Skan error ("new declaration %q#D", newdecl); 1385169699Skan error ("ambiguates old declaration %q+#D", olddecl); 1386169699Skan return error_mark_node; 138718334Speter } 138818334Speter else 1389132747Skan return NULL_TREE; 139018334Speter } 1391161660Skan else 139218334Speter { 1393169699Skan error ("conflicting declaration %q#D", newdecl); 1394169699Skan error ("%q+D has a previous declaration as %q#D", olddecl, olddecl); 1395169699Skan return error_mark_node; 139618334Speter } 139718334Speter } 139890287Sobrien else if (TREE_CODE (newdecl) == FUNCTION_DECL 139951412Sobrien && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) 140051412Sobrien && (!DECL_TEMPLATE_INFO (newdecl) 140190287Sobrien || (DECL_TI_TEMPLATE (newdecl) 140251412Sobrien != DECL_TI_TEMPLATE (olddecl)))) 140351412Sobrien || (DECL_TEMPLATE_SPECIALIZATION (newdecl) 140451412Sobrien && (!DECL_TEMPLATE_INFO (olddecl) 140590287Sobrien || (DECL_TI_TEMPLATE (olddecl) 140651412Sobrien != DECL_TI_TEMPLATE (newdecl)))))) 140751412Sobrien /* It's OK to have a template specialization and a non-template 140851412Sobrien with the same type, or to have specializations of two 140951412Sobrien different templates with the same type. Note that if one is a 141051412Sobrien specialization, and the other is an instantiation of the same 141151412Sobrien template, that we do not exit at this point. That situation 141251412Sobrien can occur if we instantiate a template class, and then 1413117410Skan specialize one of its methods. This situation is valid, but 141451412Sobrien the declarations must be merged in the usual way. */ 1415132747Skan return NULL_TREE; 141690287Sobrien else if (TREE_CODE (newdecl) == FUNCTION_DECL 141790287Sobrien && ((DECL_TEMPLATE_INSTANTIATION (olddecl) 141851412Sobrien && !DECL_USE_TEMPLATE (newdecl)) 141951412Sobrien || (DECL_TEMPLATE_INSTANTIATION (newdecl) 142051412Sobrien && !DECL_USE_TEMPLATE (olddecl)))) 142151412Sobrien /* One of the declarations is a template instantiation, and the 142251412Sobrien other is not a template at all. That's OK. */ 1423132747Skan return NULL_TREE; 1424146906Skan else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 1425146906Skan { 1426146906Skan /* In [namespace.alias] we have: 1427169699Skan 1428169699Skan In a declarative region, a namespace-alias-definition can be 1429146906Skan used to redefine a namespace-alias declared in that declarative 1430146906Skan region to refer only to the namespace to which it already 1431146906Skan refers. 1432169699Skan 1433146906Skan Therefore, if we encounter a second alias directive for the same 1434146906Skan alias, we can just ignore the second directive. */ 1435146906Skan if (DECL_NAMESPACE_ALIAS (newdecl) 1436169699Skan && (DECL_NAMESPACE_ALIAS (newdecl) 1437146906Skan == DECL_NAMESPACE_ALIAS (olddecl))) 1438146906Skan return olddecl; 1439146906Skan /* [namespace.alias] 1440132747Skan 1441169699Skan A namespace-name or namespace-alias shall not be declared as 1442146906Skan the name of any other entity in the same declarative region. 1443146906Skan A namespace-name defined at global scope shall not be 1444169699Skan declared as the name of any other entity in any global scope 1445146906Skan of the program. */ 1446169699Skan error ("declaration of namespace %qD conflicts with", newdecl); 1447169699Skan error ("previous declaration of namespace %q+D here", olddecl); 1448146906Skan return error_mark_node; 1449146906Skan } 145018334Speter else 145118334Speter { 145252290Sobrien const char *errmsg = redeclaration_error_message (newdecl, olddecl); 145318334Speter if (errmsg) 145418334Speter { 145590287Sobrien error (errmsg, newdecl); 145618334Speter if (DECL_NAME (olddecl) != NULL_TREE) 1457169699Skan error ((DECL_INITIAL (olddecl) && namespace_bindings_p ()) 1458169699Skan ? "%q+#D previously defined here" 1459169699Skan : "%q+#D previously declared here", olddecl); 1460132747Skan return error_mark_node; 146118334Speter } 146218334Speter else if (TREE_CODE (olddecl) == FUNCTION_DECL 146318334Speter && DECL_INITIAL (olddecl) != NULL_TREE 146418334Speter && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE 146518334Speter && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE) 146618334Speter { 146718334Speter /* Prototype decl follows defn w/o prototype. */ 1468169699Skan warning (0, "prototype for %q+#D", newdecl); 1469169699Skan warning (0, "%Jfollows non-prototype definition here", olddecl); 147018334Speter } 1471169699Skan else if ((TREE_CODE (olddecl) == FUNCTION_DECL 1472169699Skan || TREE_CODE (olddecl) == VAR_DECL) 147318334Speter && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) 147418334Speter { 1475169699Skan /* [dcl.link] 1476169699Skan If two declarations of the same function or object 1477169699Skan specify different linkage-specifications ..., the program 1478169699Skan is ill-formed.... Except for functions with C++ linkage, 1479169699Skan a function declaration without a linkage specification 1480169699Skan shall not precede the first linkage specification for 1481169699Skan that function. A function can be declared without a 1482169699Skan linkage specification after an explicit linkage 1483169699Skan specification has been seen; the linkage explicitly 1484169699Skan specified in the earlier declaration is not affected by 1485169699Skan such a function declaration. 1486169699Skan 1487169699Skan DR 563 raises the question why the restrictions on 1488169699Skan functions should not also apply to objects. Older 1489169699Skan versions of G++ silently ignore the linkage-specification 1490169699Skan for this example: 1491169699Skan 1492169699Skan namespace N { 1493169699Skan extern int i; 1494169699Skan extern "C" int i; 1495169699Skan } 1496169699Skan 1497169699Skan which is clearly wrong. Therefore, we now treat objects 1498169699Skan like functions. */ 149990287Sobrien if (current_lang_depth () == 0) 1500169699Skan { 1501169699Skan /* There is no explicit linkage-specification, so we use 1502169699Skan the linkage from the previous declaration. */ 1503169699Skan if (!DECL_LANG_SPECIFIC (newdecl)) 1504169699Skan retrofit_lang_decl (newdecl); 1505169699Skan SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 1506169699Skan } 150718334Speter else 150818334Speter { 1509169699Skan error ("previous declaration of %q+#D with %qL linkage", 1510169699Skan olddecl, DECL_LANGUAGE (olddecl)); 1511169699Skan error ("conflicts with new declaration with %qL linkage", 1512169699Skan DECL_LANGUAGE (newdecl)); 151318334Speter } 151418334Speter } 151518334Speter 151651412Sobrien if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) 151751412Sobrien ; 151851412Sobrien else if (TREE_CODE (olddecl) == FUNCTION_DECL) 151918334Speter { 152018334Speter tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 152118334Speter tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); 152218334Speter int i = 1; 152318334Speter 152418334Speter if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE) 152518334Speter t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2); 152690287Sobrien 152718334Speter for (; t1 && t1 != void_list_node; 152818334Speter t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) 152918334Speter if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) 153018334Speter { 153118334Speter if (1 == simple_cst_equal (TREE_PURPOSE (t1), 153218334Speter TREE_PURPOSE (t2))) 153318334Speter { 1534169699Skan pedwarn ("default argument given for parameter %d of %q#D", 153596292Sobrien i, newdecl); 1536169699Skan pedwarn ("after previous specification in %q+#D", olddecl); 153718334Speter } 153818334Speter else 153918334Speter { 1540169699Skan error ("default argument given for parameter %d of %q#D", 1541169699Skan i, newdecl); 1542169699Skan error ("after previous specification in %q+#D", 154318334Speter olddecl); 154418334Speter } 154518334Speter } 154618334Speter 1547169699Skan if (DECL_DECLARED_INLINE_P (newdecl) 154890287Sobrien && ! DECL_DECLARED_INLINE_P (olddecl) 154951412Sobrien && TREE_ADDRESSABLE (olddecl) && warn_inline) 155018334Speter { 1551169699Skan warning (0, "%q#D was used before it was declared inline", newdecl); 1552169699Skan warning (0, "%Jprevious non-inline declaration here", olddecl); 155318334Speter } 155418334Speter } 155518334Speter } 155618334Speter 155796292Sobrien /* Do not merge an implicit typedef with an explicit one. In: 155896292Sobrien 155996292Sobrien class A; 156096292Sobrien ... 156196292Sobrien typedef class A A __attribute__ ((foo)); 156296292Sobrien 156396292Sobrien the attribute should apply only to the typedef. */ 156496292Sobrien if (TREE_CODE (olddecl) == TYPE_DECL 156596292Sobrien && (DECL_IMPLICIT_TYPEDEF_P (olddecl) 156696292Sobrien || DECL_IMPLICIT_TYPEDEF_P (newdecl))) 1567132747Skan return NULL_TREE; 156896292Sobrien 156918334Speter /* If new decl is `static' and an `extern' was seen previously, 157018334Speter warn about it. */ 157118334Speter warn_extern_redeclared_static (newdecl, olddecl); 157218334Speter 157351412Sobrien /* We have committed to returning 1 at this point. */ 157418334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 157518334Speter { 157618334Speter /* Now that functions must hold information normally held 157718334Speter by field decls, there is extra work to do so that 157818334Speter declaration information does not get destroyed during 157918334Speter definition. */ 158018334Speter if (DECL_VINDEX (olddecl)) 158118334Speter DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); 158218334Speter if (DECL_CONTEXT (olddecl)) 158318334Speter DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 158418334Speter DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); 158518334Speter DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 158690287Sobrien DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); 158751412Sobrien DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); 1588171835Skan DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl); 158990287Sobrien DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); 159090287Sobrien if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK) 159190287Sobrien SET_OVERLOADED_OPERATOR_CODE 159290287Sobrien (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl)); 159351412Sobrien new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; 159490287Sobrien 159551412Sobrien /* Optionally warn about more than one declaration for the same 1596169699Skan name, but don't warn about a function declaration followed by a 1597169699Skan definition. */ 159851412Sobrien if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) 159951412Sobrien && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) 1600117410Skan /* Don't warn about extern decl followed by definition. */ 160151412Sobrien && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) 1602117410Skan /* Don't warn about friends, let add_friend take care of it. */ 1603169699Skan && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))) 160451412Sobrien { 1605169699Skan warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl); 1606169699Skan warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl); 160751412Sobrien } 160818334Speter } 160918334Speter 161018334Speter /* Deal with C++: must preserve virtual function table size. */ 161118334Speter if (TREE_CODE (olddecl) == TYPE_DECL) 161218334Speter { 1613132747Skan tree newtype = TREE_TYPE (newdecl); 1614132747Skan tree oldtype = TREE_TYPE (olddecl); 161518334Speter 161618334Speter if (newtype != error_mark_node && oldtype != error_mark_node 161718334Speter && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) 1618117410Skan CLASSTYPE_FRIEND_CLASSES (newtype) 1619117410Skan = CLASSTYPE_FRIEND_CLASSES (oldtype); 1620132747Skan 162190287Sobrien DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); 162218334Speter } 162318334Speter 162451412Sobrien /* Copy all the DECL_... slots specified in the new decl 162551412Sobrien except for any that we copy here from the old type. */ 162690287Sobrien DECL_ATTRIBUTES (newdecl) 162790287Sobrien = (*targetm.merge_decl_attributes) (olddecl, newdecl); 162818334Speter 162951412Sobrien if (TREE_CODE (newdecl) == TEMPLATE_DECL) 163018334Speter { 1631169699Skan tree old_result; 1632169699Skan tree new_result; 1633169699Skan old_result = DECL_TEMPLATE_RESULT (olddecl); 1634169699Skan new_result = DECL_TEMPLATE_RESULT (newdecl); 1635169699Skan TREE_TYPE (olddecl) = TREE_TYPE (old_result); 163690287Sobrien DECL_TEMPLATE_SPECIALIZATIONS (olddecl) 163752290Sobrien = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), 163852290Sobrien DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); 163990287Sobrien 1640169699Skan if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 1641169699Skan { 1642169699Skan DECL_INLINE (old_result) 1643169699Skan |= DECL_INLINE (new_result); 1644169699Skan DECL_DECLARED_INLINE_P (old_result) 1645169699Skan |= DECL_DECLARED_INLINE_P (new_result); 1646169699Skan check_redeclaration_exception_specification (newdecl, olddecl); 1647169699Skan } 1648169699Skan 164990287Sobrien /* If the new declaration is a definition, update the file and 1650260140Spfg line information on the declaration, and also make 1651260140Spfg the old declaration the same definition. */ 1652169699Skan if (DECL_INITIAL (old_result) == NULL_TREE 1653169699Skan && DECL_INITIAL (new_result) != NULL_TREE) 165490287Sobrien { 1655169699Skan DECL_SOURCE_LOCATION (olddecl) 1656169699Skan = DECL_SOURCE_LOCATION (old_result) 1657117410Skan = DECL_SOURCE_LOCATION (newdecl); 1658260140Spfg DECL_INITIAL (old_result) = DECL_INITIAL (new_result); 1659132747Skan if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 1660169699Skan DECL_ARGUMENTS (old_result) 1661169699Skan = DECL_ARGUMENTS (new_result); 166290287Sobrien } 166390287Sobrien 1664132747Skan return olddecl; 166518334Speter } 166690287Sobrien 166718334Speter if (types_match) 166818334Speter { 166918334Speter /* Automatically handles default parameters. */ 167018334Speter tree oldtype = TREE_TYPE (olddecl); 167118334Speter tree newtype; 167218334Speter 167318334Speter /* Merge the data types specified in the two decls. */ 167496292Sobrien newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); 167518334Speter 167696292Sobrien /* If merge_types produces a non-typedef type, just use the old type. */ 167790287Sobrien if (TREE_CODE (newdecl) == TYPE_DECL 167890287Sobrien && newtype == DECL_ORIGINAL_TYPE (newdecl)) 167990287Sobrien newtype = oldtype; 168090287Sobrien 168118334Speter if (TREE_CODE (newdecl) == VAR_DECL) 1682117410Skan { 1683117410Skan DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); 1684117410Skan DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); 1685169699Skan DECL_NONTRIVIALLY_INITIALIZED_P (newdecl) 1686169699Skan |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl); 1687132747Skan DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl) 1688132747Skan |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl); 1689169699Skan 1690169699Skan /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */ 1691169699Skan if (DECL_LANG_SPECIFIC (olddecl) 1692169699Skan && CP_DECL_THREADPRIVATE_P (olddecl)) 1693169699Skan { 1694169699Skan /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */ 1695169699Skan if (!DECL_LANG_SPECIFIC (newdecl)) 1696169699Skan retrofit_lang_decl (newdecl); 1697169699Skan 1698169699Skan DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl); 1699169699Skan CP_DECL_THREADPRIVATE_P (newdecl) = 1; 1700169699Skan } 1701117410Skan } 1702117410Skan 170396292Sobrien /* Do this after calling `merge_types' so that default 170418334Speter parameters don't confuse us. */ 1705169699Skan else if (TREE_CODE (newdecl) == FUNCTION_DECL) 1706169699Skan check_redeclaration_exception_specification (newdecl, olddecl); 170718334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; 170818334Speter 1709169699Skan if (TREE_CODE (newdecl) == FUNCTION_DECL) 1710169699Skan check_default_args (newdecl); 1711169699Skan 171218334Speter /* Lay the type out, unless already done. */ 171352290Sobrien if (! same_type_p (newtype, oldtype) 171451412Sobrien && TREE_TYPE (newdecl) != error_mark_node 171551412Sobrien && !(processing_template_decl && uses_template_parms (newdecl))) 171618334Speter layout_type (TREE_TYPE (newdecl)); 171718334Speter 171851412Sobrien if ((TREE_CODE (newdecl) == VAR_DECL 171951412Sobrien || TREE_CODE (newdecl) == PARM_DECL 172051412Sobrien || TREE_CODE (newdecl) == RESULT_DECL 172151412Sobrien || TREE_CODE (newdecl) == FIELD_DECL 172251412Sobrien || TREE_CODE (newdecl) == TYPE_DECL) 172351412Sobrien && !(processing_template_decl && uses_template_parms (newdecl))) 172418334Speter layout_decl (newdecl, 0); 172518334Speter 172618334Speter /* Merge the type qualifiers. */ 172718334Speter if (TREE_READONLY (newdecl)) 172818334Speter TREE_READONLY (olddecl) = 1; 172918334Speter if (TREE_THIS_VOLATILE (newdecl)) 173018334Speter TREE_THIS_VOLATILE (olddecl) = 1; 1731169699Skan if (TREE_NOTHROW (newdecl)) 1732169699Skan TREE_NOTHROW (olddecl) = 1; 173318334Speter 1734169699Skan /* Merge deprecatedness. */ 1735169699Skan if (TREE_DEPRECATED (newdecl)) 1736169699Skan TREE_DEPRECATED (olddecl) = 1; 1737169699Skan 173818334Speter /* Merge the initialization information. */ 173918334Speter if (DECL_INITIAL (newdecl) == NULL_TREE 174018334Speter && DECL_INITIAL (olddecl) != NULL_TREE) 174118334Speter { 174218334Speter DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1743117410Skan DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 174490287Sobrien if (CAN_HAVE_FULL_LANG_DECL_P (newdecl) 174590287Sobrien && DECL_LANG_SPECIFIC (newdecl) 174652290Sobrien && DECL_LANG_SPECIFIC (olddecl)) 1747132747Skan { 1748132747Skan DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 1749169699Skan DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 1750132747Skan } 175118334Speter } 175218334Speter 175318334Speter /* Merge the section attribute. 1754169699Skan We want to issue an error if the sections conflict but that must be 175518334Speter done later in decl_attributes since we are called before attributes 175618334Speter are assigned. */ 175718334Speter if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 175818334Speter DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 175918334Speter 176090287Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 176190287Sobrien { 176290287Sobrien DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 176390287Sobrien |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1764132747Skan DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1765132747Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1766132747Skan TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1767132747Skan TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl); 1768132747Skan DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1769132747Skan DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 177096292Sobrien /* Keep the old RTL. */ 177196292Sobrien COPY_DECL_RTL (olddecl, newdecl); 177290287Sobrien } 1773169699Skan else if (TREE_CODE (newdecl) == VAR_DECL 177496292Sobrien && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) 177596292Sobrien { 177696292Sobrien /* Keep the old RTL. We cannot keep the old RTL if the old 177796292Sobrien declaration was for an incomplete object and the new 177896292Sobrien declaration is not since many attributes of the RTL will 177996292Sobrien change. */ 178096292Sobrien COPY_DECL_RTL (olddecl, newdecl); 178196292Sobrien } 178218334Speter } 178318334Speter /* If cannot merge, then use the new type and qualifiers, 178418334Speter and don't preserve the old rtl. */ 178518334Speter else 178618334Speter { 178718334Speter /* Clean out any memory we had of the old declaration. */ 178818334Speter tree oldstatic = value_member (olddecl, static_aggregates); 178918334Speter if (oldstatic) 179018334Speter TREE_VALUE (oldstatic) = error_mark_node; 179118334Speter 179218334Speter TREE_TYPE (olddecl) = TREE_TYPE (newdecl); 179318334Speter TREE_READONLY (olddecl) = TREE_READONLY (newdecl); 179418334Speter TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); 179518334Speter TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); 179618334Speter } 179718334Speter 179818334Speter /* Merge the storage class information. */ 179996292Sobrien merge_weak (newdecl, olddecl); 180096292Sobrien 180151412Sobrien DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl); 180290287Sobrien DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); 180318334Speter TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 180418334Speter TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); 180518334Speter if (! DECL_EXTERNAL (olddecl)) 180618334Speter DECL_EXTERNAL (newdecl) = 0; 180790287Sobrien 1808169699Skan new_template = NULL_TREE; 180951412Sobrien if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) 181018334Speter { 181118334Speter DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); 181218334Speter DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); 181351412Sobrien DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); 181490287Sobrien DECL_TEMPLATE_INSTANTIATED (newdecl) 181552290Sobrien |= DECL_TEMPLATE_INSTANTIATED (olddecl); 1816169699Skan 1817169699Skan /* If the OLDDECL is an instantiation and/or specialization, 1818169699Skan then the NEWDECL must be too. But, it may not yet be marked 1819169699Skan as such if the caller has created NEWDECL, but has not yet 1820169699Skan figured out that it is a redeclaration. */ 1821169699Skan if (!DECL_USE_TEMPLATE (newdecl)) 1822169699Skan DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl); 1823169699Skan 182451412Sobrien /* Don't really know how much of the language-specific 182551412Sobrien values we should copy from old to new. */ 182651412Sobrien DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); 1827169699Skan DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 1828117410Skan DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2; 182951412Sobrien DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); 1830169699Skan DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl); 1831169699Skan if (DECL_TEMPLATE_INFO (newdecl)) 1832169699Skan new_template = DECL_TI_TEMPLATE (newdecl); 183352290Sobrien DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); 183490287Sobrien DECL_INITIALIZED_IN_CLASS_P (newdecl) 1835169699Skan |= DECL_INITIALIZED_IN_CLASS_P (olddecl); 183651412Sobrien olddecl_friend = DECL_FRIEND_P (olddecl); 1837169699Skan hidden_friend = (DECL_ANTICIPATED (olddecl) 1838169699Skan && DECL_HIDDEN_FRIEND_P (olddecl) 1839169699Skan && newdecl_is_friend); 184052290Sobrien 184152290Sobrien /* Only functions have DECL_BEFRIENDING_CLASSES. */ 184252290Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL 184352290Sobrien || DECL_FUNCTION_TEMPLATE_P (newdecl)) 1844117410Skan { 1845117410Skan DECL_BEFRIENDING_CLASSES (newdecl) 1846117410Skan = chainon (DECL_BEFRIENDING_CLASSES (newdecl), 1847117410Skan DECL_BEFRIENDING_CLASSES (olddecl)); 1848117410Skan /* DECL_THUNKS is only valid for virtual functions, 1849117410Skan otherwise it is a DECL_FRIEND_CONTEXT. */ 1850117410Skan if (DECL_VIRTUAL_P (newdecl)) 1851117410Skan DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl); 1852117410Skan } 185318334Speter } 185418334Speter 185518334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 185618334Speter { 185790287Sobrien if (DECL_TEMPLATE_INSTANTIATION (olddecl) 185890287Sobrien && !DECL_TEMPLATE_INSTANTIATION (newdecl)) 185951412Sobrien { 186051412Sobrien /* If newdecl is not a specialization, then it is not a 186151412Sobrien template-related function at all. And that means that we 1862132747Skan should have exited above, returning 0. */ 1863169699Skan gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl)); 186451412Sobrien 186590287Sobrien if (TREE_USED (olddecl)) 186651412Sobrien /* From [temp.expl.spec]: 186790287Sobrien 186851412Sobrien If a template, a member template or the member of a class 186951412Sobrien template is explicitly specialized then that 187051412Sobrien specialization shall be declared before the first use of 187151412Sobrien that specialization that would cause an implicit 187251412Sobrien instantiation to take place, in every translation unit in 187351412Sobrien which such a use occurs. */ 1874169699Skan error ("explicit specialization of %qD after first use", 187551412Sobrien olddecl); 187651412Sobrien 187751412Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); 1878117410Skan 1879169699Skan /* Don't propagate visibility from the template to the 1880169699Skan specialization here. We'll do that in determine_visibility if 1881169699Skan appropriate. */ 1882169699Skan DECL_VISIBILITY_SPECIFIED (olddecl) = 0; 1883169699Skan 1884117410Skan /* [temp.expl.spec/14] We don't inline explicit specialization 1885117410Skan just because the primary template says so. */ 188651412Sobrien } 1887117410Skan else 1888117410Skan { 1889117410Skan if (DECL_PENDING_INLINE_INFO (newdecl) == 0) 1890117410Skan DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl); 189118334Speter 1892117410Skan DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); 189318334Speter 1894169699Skan /* If either decl says `inline', this fn is inline, unless 1895117410Skan its definition was passed already. */ 1896117410Skan if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE) 1897117410Skan DECL_INLINE (olddecl) = 1; 1898117410Skan DECL_INLINE (newdecl) = DECL_INLINE (olddecl); 189990287Sobrien 1900117410Skan DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 1901117410Skan = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 1902117410Skan } 1903117410Skan 190490287Sobrien /* Preserve abstractness on cloned [cd]tors. */ 190590287Sobrien DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl); 190690287Sobrien 190718334Speter if (! types_match) 190818334Speter { 190990287Sobrien SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 191090287Sobrien COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); 1911169699Skan COPY_DECL_RTL (newdecl, olddecl); 191251412Sobrien } 191351412Sobrien if (! types_match || new_defines_function) 191451412Sobrien { 191590287Sobrien /* These need to be copied so that the names are available. 191690287Sobrien Note that if the types do match, we'll preserve inline 191790287Sobrien info and other bits, but if not, we won't. */ 191818334Speter DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); 191918334Speter DECL_RESULT (olddecl) = DECL_RESULT (newdecl); 192018334Speter } 192118334Speter if (new_defines_function) 192218334Speter /* If defining a function declared with other language 192318334Speter linkage, use the previously declared language linkage. */ 192490287Sobrien SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 192590287Sobrien else if (types_match) 192618334Speter { 192718334Speter /* If redeclaring a builtin function, and not a definition, 192818334Speter it stays built in. */ 192918334Speter if (DECL_BUILT_IN (olddecl)) 193018334Speter { 193190287Sobrien DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 193218334Speter DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 193318334Speter /* If we're keeping the built-in definition, keep the rtl, 193418334Speter regardless of declaration matches. */ 1935169699Skan COPY_DECL_RTL (olddecl, newdecl); 193618334Speter } 193718334Speter 193818334Speter DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 193918334Speter /* Don't clear out the arguments if we're redefining a function. */ 194018334Speter if (DECL_ARGUMENTS (olddecl)) 194118334Speter DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 194218334Speter } 194318334Speter } 194490287Sobrien else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 194590287Sobrien NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); 194618334Speter 194718334Speter /* Now preserve various other info from the definition. */ 194818334Speter TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); 194918334Speter TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); 195018334Speter DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 195190287Sobrien COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 195218334Speter 1953169699Skan /* Warn about conflicting visibility specifications. */ 1954169699Skan if (DECL_VISIBILITY_SPECIFIED (olddecl) 1955169699Skan && DECL_VISIBILITY_SPECIFIED (newdecl) 1956169699Skan && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1957132747Skan { 1958169699Skan warning (OPT_Wattributes, "%q+D: visibility attribute ignored " 1959169699Skan "because it", newdecl); 1960169699Skan warning (OPT_Wattributes, "%Jconflicts with previous " 1961169699Skan "declaration here", olddecl); 1962169699Skan } 1963169699Skan /* Choose the declaration which specified visibility. */ 1964169699Skan if (DECL_VISIBILITY_SPECIFIED (olddecl)) 1965169699Skan { 1966132747Skan DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1967169699Skan DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 1968132747Skan } 1969169699Skan /* Init priority used to be merged from newdecl to olddecl by the memcpy, 1970169699Skan so keep this behavior. */ 1971169699Skan if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl)) 1972169699Skan { 1973169699Skan SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl)); 1974169699Skan DECL_HAS_INIT_PRIORITY_P (olddecl) = 1; 1975169699Skan } 1976132747Skan 1977169699Skan /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced 1978169699Skan with that from NEWDECL below. */ 1979169699Skan if (DECL_LANG_SPECIFIC (olddecl)) 1980169699Skan { 1981169699Skan gcc_assert (DECL_LANG_SPECIFIC (olddecl) 1982169699Skan != DECL_LANG_SPECIFIC (newdecl)); 1983169699Skan ggc_free (DECL_LANG_SPECIFIC (olddecl)); 1984169699Skan } 1985169699Skan 198618334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 198718334Speter { 198818334Speter int function_size; 198918334Speter 1990169699Skan function_size = sizeof (struct tree_decl_common); 199118334Speter 199290287Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 199390287Sobrien (char *) newdecl + sizeof (struct tree_common), 199490287Sobrien function_size - sizeof (struct tree_common)); 199518334Speter 1996169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 1997169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 1998169699Skan sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common)); 1999169699Skan if (new_template) 2000132747Skan /* If newdecl is a template instantiation, it is possible that 2001132747Skan the following sequence of events has occurred: 200251412Sobrien 2003132747Skan o A friend function was declared in a class template. The 2004132747Skan class template was instantiated. 200551412Sobrien 2006132747Skan o The instantiation of the friend declaration was 2007132747Skan recorded on the instantiation list, and is newdecl. 200851412Sobrien 2009132747Skan o Later, however, instantiate_class_template called pushdecl 2010132747Skan on the newdecl to perform name injection. But, pushdecl in 2011132747Skan turn called duplicate_decls when it discovered that another 2012132747Skan declaration of a global function with the same name already 2013132747Skan existed. 201451412Sobrien 2015132747Skan o Here, in duplicate_decls, we decided to clobber newdecl. 201651412Sobrien 2017132747Skan If we're going to do that, we'd better make sure that 2018132747Skan olddecl, and not newdecl, is on the list of 2019132747Skan instantiations so that if we try to do the instantiation 2020132747Skan again we won't get the clobbered declaration. */ 2021169699Skan reregister_specialization (newdecl, 2022169699Skan new_template, 2023132747Skan olddecl); 202418334Speter } 202518334Speter else 202618334Speter { 2027169699Skan size_t size = tree_code_size (TREE_CODE (olddecl)); 202890287Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 202990287Sobrien (char *) newdecl + sizeof (struct tree_common), 2030169699Skan sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 2031169699Skan switch (TREE_CODE (olddecl)) 2032169699Skan { 2033169699Skan case LABEL_DECL: 2034169699Skan case VAR_DECL: 2035169699Skan case RESULT_DECL: 2036169699Skan case PARM_DECL: 2037169699Skan case FIELD_DECL: 2038169699Skan case TYPE_DECL: 2039169699Skan case CONST_DECL: 2040169699Skan { 2041169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 2042169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 2043169699Skan size - sizeof (struct tree_decl_common) 2044169699Skan + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 2045169699Skan } 2046169699Skan break; 2047169699Skan default: 2048169699Skan memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 2049169699Skan (char *) newdecl + sizeof (struct tree_decl_common), 2050169699Skan sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common) 2051169699Skan + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 2052169699Skan break; 2053169699Skan } 205418334Speter } 205518334Speter DECL_UID (olddecl) = olddecl_uid; 205618334Speter if (olddecl_friend) 205718334Speter DECL_FRIEND_P (olddecl) = 1; 2058169699Skan if (hidden_friend) 2059169699Skan { 2060169699Skan DECL_ANTICIPATED (olddecl) = 1; 2061169699Skan DECL_HIDDEN_FRIEND_P (olddecl) = 1; 2062169699Skan } 206318334Speter 206451412Sobrien /* NEWDECL contains the merged attribute lists. 206551412Sobrien Update OLDDECL to be the same. */ 206690287Sobrien DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); 206751412Sobrien 2068119270Skan /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 2069132747Skan so that encode_section_info has a chance to look at the new decl 2070132747Skan flags and attributes. */ 2071169699Skan if (DECL_RTL_SET_P (olddecl) 2072119270Skan && (TREE_CODE (olddecl) == FUNCTION_DECL 2073119270Skan || (TREE_CODE (olddecl) == VAR_DECL 2074119270Skan && TREE_STATIC (olddecl)))) 2075169699Skan make_decl_rtl (olddecl); 2076119270Skan 2077169699Skan /* The NEWDECL will no longer be needed. Because every out-of-class 2078169699Skan declaration of a member results in a call to duplicate_decls, 2079169699Skan freeing these nodes represents in a significant savings. */ 2080169699Skan ggc_free (newdecl); 2081169699Skan 2082132747Skan return olddecl; 208318334Speter} 208418334Speter 208518334Speter/* Return zero if the declaration NEWDECL is valid 208618334Speter when the declaration OLDDECL (assumed to be for the same name) 208718334Speter has already been seen. 208818334Speter Otherwise return an error message format string with a %s 208918334Speter where the identifier should go. */ 209018334Speter 209152290Sobrienstatic const char * 2092132747Skanredeclaration_error_message (tree newdecl, tree olddecl) 209318334Speter{ 209418334Speter if (TREE_CODE (newdecl) == TYPE_DECL) 209518334Speter { 209618334Speter /* Because C++ can put things into name space for free, 209718334Speter constructs like "typedef struct foo { ... } foo" 209818334Speter would look like an erroneous redeclaration. */ 209952290Sobrien if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) 2100169699Skan return NULL; 210118334Speter else 2102169699Skan return "redefinition of %q#D"; 210318334Speter } 210418334Speter else if (TREE_CODE (newdecl) == FUNCTION_DECL) 210518334Speter { 210618334Speter /* If this is a pure function, its olddecl will actually be 210718334Speter the original initialization to `0' (which we force to call 210818334Speter abort()). Don't complain about redefinition in this case. */ 2109169699Skan if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl) 2110169699Skan && DECL_INITIAL (olddecl) == NULL_TREE) 2111169699Skan return NULL; 211218334Speter 211351412Sobrien /* If both functions come from different namespaces, this is not 2114117410Skan a redeclaration - this is a conflict with a used function. */ 211551412Sobrien if (DECL_NAMESPACE_SCOPE_P (olddecl) 2116146906Skan && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl) 2117146906Skan && ! decls_match (olddecl, newdecl)) 2118169699Skan return "%qD conflicts with used function"; 211951412Sobrien 212018334Speter /* We'll complain about linkage mismatches in 2121169699Skan warn_extern_redeclared_static. */ 212218334Speter 212351412Sobrien /* Defining the same name twice is no good. */ 212418334Speter if (DECL_INITIAL (olddecl) != NULL_TREE 212518334Speter && DECL_INITIAL (newdecl) != NULL_TREE) 212618334Speter { 212718334Speter if (DECL_NAME (olddecl) == NULL_TREE) 2128169699Skan return "%q#D not declared in class"; 212918334Speter else 2130169699Skan return "redefinition of %q#D"; 213118334Speter } 2132169699Skan return NULL; 213318334Speter } 213418334Speter else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 213518334Speter { 2136132747Skan tree nt, ot; 2137132747Skan 2138132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 2139132747Skan { 2140132747Skan if (COMPLETE_TYPE_P (TREE_TYPE (newdecl)) 2141132747Skan && COMPLETE_TYPE_P (TREE_TYPE (olddecl))) 2142169699Skan return "redefinition of %q#D"; 2143132747Skan return NULL; 2144132747Skan } 2145132747Skan 2146132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL 2147169699Skan || (DECL_TEMPLATE_RESULT (newdecl) 2148132747Skan == DECL_TEMPLATE_RESULT (olddecl))) 2149132747Skan return NULL; 2150132747Skan 2151132747Skan nt = DECL_TEMPLATE_RESULT (newdecl); 2152132747Skan if (DECL_TEMPLATE_INFO (nt)) 2153132747Skan nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt)); 2154132747Skan ot = DECL_TEMPLATE_RESULT (olddecl); 2155132747Skan if (DECL_TEMPLATE_INFO (ot)) 2156132747Skan ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot)); 2157132747Skan if (DECL_INITIAL (nt) && DECL_INITIAL (ot)) 2158169699Skan return "redefinition of %q#D"; 2159132747Skan 2160132747Skan return NULL; 216118334Speter } 2162169699Skan else if (TREE_CODE (newdecl) == VAR_DECL 2163169699Skan && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl) 2164169699Skan && (! DECL_LANG_SPECIFIC (olddecl) 2165169699Skan || ! CP_DECL_THREADPRIVATE_P (olddecl) 2166169699Skan || DECL_THREAD_LOCAL_P (newdecl))) 2167169699Skan { 2168169699Skan /* Only variables can be thread-local, and all declarations must 2169169699Skan agree on this property. */ 2170169699Skan if (DECL_THREAD_LOCAL_P (newdecl)) 2171169699Skan return "thread-local declaration of %q#D follows " 2172169699Skan "non-thread-local declaration"; 2173169699Skan else 2174169699Skan return "non-thread-local declaration of %q#D follows " 2175169699Skan "thread-local declaration"; 2176169699Skan } 217790287Sobrien else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) 217818334Speter { 2179171835Skan /* The objects have been declared at namespace scope. If either 2180171835Skan is a member of an anonymous union, then this is an invalid 2181171835Skan redeclaration. For example: 2182171835Skan 2183171835Skan int i; 2184171835Skan union { int i; }; 2185171835Skan 2186171835Skan is invalid. */ 2187171835Skan if (DECL_ANON_UNION_VAR_P (newdecl) 2188171835Skan || DECL_ANON_UNION_VAR_P (olddecl)) 2189171835Skan return "redeclaration of %q#D"; 2190171835Skan /* If at least one declaration is a reference, there is no 2191171835Skan conflict. For example: 2192171835Skan 2193171835Skan int i = 3; 2194171835Skan extern int i; 2195171835Skan 2196171835Skan is valid. */ 219718334Speter if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) 2198169699Skan return NULL; 219918334Speter /* Reject two definitions. */ 2200169699Skan return "redefinition of %q#D"; 220118334Speter } 220218334Speter else 220318334Speter { 220418334Speter /* Objects declared with block scope: */ 220518334Speter /* Reject two definitions, and reject a definition 220618334Speter together with an external reference. */ 220718334Speter if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) 2208169699Skan return "redeclaration of %q#D"; 2209169699Skan return NULL; 221018334Speter } 221118334Speter} 221218334Speter 2213169699Skan/* Hash and equality functions for the named_label table. */ 2214169699Skan 2215169699Skanstatic hashval_t 2216169699Skannamed_label_entry_hash (const void *data) 2217169699Skan{ 2218169699Skan const struct named_label_entry *ent = (const struct named_label_entry *) data; 2219169699Skan return DECL_UID (ent->label_decl); 2220169699Skan} 2221169699Skan 2222169699Skanstatic int 2223169699Skannamed_label_entry_eq (const void *a, const void *b) 2224169699Skan{ 2225169699Skan const struct named_label_entry *ent_a = (const struct named_label_entry *) a; 2226169699Skan const struct named_label_entry *ent_b = (const struct named_label_entry *) b; 2227169699Skan return ent_a->label_decl == ent_b->label_decl; 2228169699Skan} 2229169699Skan 223090287Sobrien/* Create a new label, named ID. */ 223118334Speter 223290287Sobrienstatic tree 2233132747Skanmake_label_decl (tree id, int local_p) 223418334Speter{ 2235169699Skan struct named_label_entry *ent; 2236169699Skan void **slot; 223790287Sobrien tree decl; 223818334Speter 223990287Sobrien decl = build_decl (LABEL_DECL, id, void_type_node); 224018334Speter 224190287Sobrien DECL_CONTEXT (decl) = current_function_decl; 224290287Sobrien DECL_MODE (decl) = VOIDmode; 224390287Sobrien C_DECLARED_LABEL_FLAG (decl) = local_p; 224490287Sobrien 224590287Sobrien /* Say where one reference is to the label, for the sake of the 224690287Sobrien error if it is not defined. */ 2247132747Skan DECL_SOURCE_LOCATION (decl) = input_location; 224890287Sobrien 224990287Sobrien /* Record the fact that this identifier is bound to this label. */ 225090287Sobrien SET_IDENTIFIER_LABEL_VALUE (id, decl); 225190287Sobrien 2252169699Skan /* Create the label htab for the function on demand. */ 2253169699Skan if (!named_labels) 2254169699Skan named_labels = htab_create_ggc (13, named_label_entry_hash, 2255169699Skan named_label_entry_eq, NULL); 225690287Sobrien 2257169699Skan /* Record this label on the list of labels used in this function. 2258169699Skan We do this before calling make_label_decl so that we get the 2259169699Skan IDENTIFIER_LABEL_VALUE before the new label is declared. */ 2260169699Skan ent = GGC_CNEW (struct named_label_entry); 2261169699Skan ent->label_decl = decl; 226290287Sobrien 2263169699Skan slot = htab_find_slot (named_labels, ent, INSERT); 2264169699Skan gcc_assert (*slot == NULL); 2265169699Skan *slot = ent; 2266169699Skan 2267169699Skan return decl; 226890287Sobrien} 226918334Speter 227090287Sobrien/* Look for a label named ID in the current function. If one cannot 227190287Sobrien be found, create one. (We keep track of used, but undefined, 227290287Sobrien labels, and complain about them at the end of a function.) */ 227390287Sobrien 227490287Sobrientree 2275132747Skanlookup_label (tree id) 227690287Sobrien{ 227790287Sobrien tree decl; 227890287Sobrien 2279117410Skan timevar_push (TV_NAME_LOOKUP); 228090287Sobrien /* You can't use labels at global scope. */ 228190287Sobrien if (current_function_decl == NULL_TREE) 228218334Speter { 2283169699Skan error ("label %qE referenced outside of any function", id); 2284117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 228518334Speter } 228618334Speter 228790287Sobrien /* See if we've already got this label. */ 228890287Sobrien decl = IDENTIFIER_LABEL_VALUE (id); 228990287Sobrien if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl) 2290117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 229118334Speter 229290287Sobrien decl = make_label_decl (id, /*local_p=*/0); 2293117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 229490287Sobrien} 229518334Speter 229690287Sobrien/* Declare a local label named ID. */ 229718334Speter 229890287Sobrientree 2299132747Skandeclare_local_label (tree id) 230090287Sobrien{ 2301169699Skan tree decl, shadow; 230218334Speter 230390287Sobrien /* Add a new entry to the SHADOWED_LABELS list so that when we leave 2304169699Skan this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */ 2305169699Skan shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, 2306169699Skan current_binding_level->shadowed_labels); 2307169699Skan current_binding_level->shadowed_labels = shadow; 2308169699Skan 230990287Sobrien decl = make_label_decl (id, /*local_p=*/1); 2310169699Skan TREE_VALUE (shadow) = decl; 231190287Sobrien 231218334Speter return decl; 231318334Speter} 231418334Speter 231590287Sobrien/* Returns nonzero if it is ill-formed to jump past the declaration of 231690287Sobrien DECL. Returns 2 if it's also a real problem. */ 231718334Speter 231890287Sobrienstatic int 2319132747Skandecl_jump_unsafe (tree decl) 232090287Sobrien{ 2321169699Skan if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl) 2322169699Skan || TREE_TYPE (decl) == error_mark_node) 232390287Sobrien return 0; 232418334Speter 2325169699Skan if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) 2326169699Skan || DECL_NONTRIVIALLY_INITIALIZED_P (decl)) 2327169699Skan return 2; 2328169699Skan 2329169699Skan if (pod_type_p (TREE_TYPE (decl))) 233090287Sobrien return 0; 233190287Sobrien 2332169699Skan /* The POD stuff is just pedantry; why should it matter if the class 233390287Sobrien contains a field of pointer to member type? */ 233490287Sobrien return 1; 233590287Sobrien} 233690287Sobrien 2337169699Skan/* A subroutine of check_previous_goto_1 to identify a branch to the user. */ 2338169699Skan 2339169699Skanstatic void 2340169699Skanidentify_goto (tree decl, const location_t *locus) 2341169699Skan{ 2342169699Skan if (decl) 2343169699Skan pedwarn ("jump to label %qD", decl); 2344169699Skan else 2345169699Skan pedwarn ("jump to case label"); 2346169699Skan if (locus) 2347169699Skan pedwarn ("%H from here", locus); 2348169699Skan} 2349169699Skan 235090287Sobrien/* Check that a single previously seen jump to a newly defined label 235190287Sobrien is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for 235290287Sobrien the jump context; NAMES are the names in scope in LEVEL at the jump 2353169699Skan context; LOCUS is the source position of the jump or 0. Returns 2354169699Skan true if all is well. */ 235590287Sobrien 2356169699Skanstatic bool 2357169699Skancheck_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names, 2358169699Skan bool exited_omp, const location_t *locus) 235918334Speter{ 2360169699Skan struct cp_binding_level *b; 2361169699Skan bool identified = false, saw_eh = false, saw_omp = false; 2362169699Skan 2363169699Skan if (exited_omp) 236490287Sobrien { 2365169699Skan identify_goto (decl, locus); 2366169699Skan error (" exits OpenMP structured block"); 2367169699Skan identified = saw_omp = true; 2368169699Skan } 2369169699Skan 2370169699Skan for (b = current_binding_level; b ; b = b->level_chain) 2371169699Skan { 2372169699Skan tree new_decls, old_decls = (b == level ? names : NULL_TREE); 2373169699Skan 2374169699Skan for (new_decls = b->names; new_decls != old_decls; 237590287Sobrien new_decls = TREE_CHAIN (new_decls)) 237690287Sobrien { 237790287Sobrien int problem = decl_jump_unsafe (new_decls); 237890287Sobrien if (! problem) 237990287Sobrien continue; 238018334Speter 2381169699Skan if (!identified) 238290287Sobrien { 2383169699Skan identify_goto (decl, locus); 2384169699Skan identified = true; 238590287Sobrien } 238690287Sobrien if (problem > 1) 2387169699Skan error (" crosses initialization of %q+#D", new_decls); 238890287Sobrien else 2389169699Skan pedwarn (" enters scope of non-POD %q+#D", new_decls); 239090287Sobrien } 239190287Sobrien 239290287Sobrien if (b == level) 239390287Sobrien break; 2394169699Skan if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh) 239590287Sobrien { 2396169699Skan if (!identified) 239790287Sobrien { 2398169699Skan identify_goto (decl, locus); 2399169699Skan identified = true; 240090287Sobrien } 2401132747Skan if (b->kind == sk_try) 240290287Sobrien error (" enters try block"); 240390287Sobrien else 240490287Sobrien error (" enters catch block"); 2405169699Skan saw_eh = true; 240690287Sobrien } 2407169699Skan if (b->kind == sk_omp && !saw_omp) 2408169699Skan { 2409169699Skan if (!identified) 2410169699Skan { 2411169699Skan identify_goto (decl, locus); 2412169699Skan identified = true; 2413169699Skan } 2414169699Skan error (" enters OpenMP structured block"); 2415169699Skan saw_omp = true; 2416169699Skan } 241790287Sobrien } 2418169699Skan 2419169699Skan return !identified; 242090287Sobrien} 242190287Sobrien 242290287Sobrienstatic void 2423169699Skancheck_previous_goto (tree decl, struct named_label_use_entry *use) 242490287Sobrien{ 2425169699Skan check_previous_goto_1 (decl, use->binding_level, 2426169699Skan use->names_in_scope, use->in_omp_scope, 2427169699Skan &use->o_goto_locus); 242890287Sobrien} 242990287Sobrien 2430169699Skanstatic bool 2431132747Skancheck_switch_goto (struct cp_binding_level* level) 243290287Sobrien{ 2433169699Skan return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL); 243490287Sobrien} 243590287Sobrien 243690287Sobrien/* Check that a new jump to a label DECL is OK. Called by 243790287Sobrien finish_goto_stmt. */ 243890287Sobrien 243990287Sobrienvoid 2440132747Skancheck_goto (tree decl) 244190287Sobrien{ 2442169699Skan struct named_label_entry *ent, dummy; 2443169699Skan bool saw_catch = false, identified = false; 244490287Sobrien tree bad; 244590287Sobrien 2446169699Skan /* We can't know where a computed goto is jumping. 2447169699Skan So we assume that it's OK. */ 2448169699Skan if (TREE_CODE (decl) != LABEL_DECL) 244990287Sobrien return; 245090287Sobrien 2451169699Skan /* We didn't record any information about this label when we created it, 2452169699Skan and there's not much point since it's trivial to analyze as a return. */ 2453169699Skan if (decl == cdtor_label) 2454169699Skan return; 2455169699Skan 2456169699Skan dummy.label_decl = decl; 2457169699Skan ent = (struct named_label_entry *) htab_find (named_labels, &dummy); 2458169699Skan gcc_assert (ent != NULL); 2459169699Skan 246090287Sobrien /* If the label hasn't been defined yet, defer checking. */ 246190287Sobrien if (! DECL_INITIAL (decl)) 246290287Sobrien { 2463169699Skan struct named_label_use_entry *new_use; 2464169699Skan 2465169699Skan /* Don't bother creating another use if the last goto had the 2466169699Skan same data, and will therefore create the same set of errors. */ 2467169699Skan if (ent->uses 2468169699Skan && ent->uses->names_in_scope == current_binding_level->names) 2469169699Skan return; 2470169699Skan 2471169699Skan new_use = GGC_NEW (struct named_label_use_entry); 2472169699Skan new_use->binding_level = current_binding_level; 2473169699Skan new_use->names_in_scope = current_binding_level->names; 2474169699Skan new_use->o_goto_locus = input_location; 2475169699Skan new_use->in_omp_scope = false; 2476169699Skan 2477169699Skan new_use->next = ent->uses; 2478169699Skan ent->uses = new_use; 247990287Sobrien return; 248090287Sobrien } 248190287Sobrien 2482169699Skan if (ent->in_try_scope || ent->in_catch_scope 2483169699Skan || ent->in_omp_scope || ent->bad_decls) 248490287Sobrien { 2485169699Skan pedwarn ("jump to label %q+D", decl); 248690287Sobrien pedwarn (" from here"); 2487169699Skan identified = true; 248890287Sobrien } 248990287Sobrien 2490169699Skan for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad)) 249190287Sobrien { 249290287Sobrien tree b = TREE_VALUE (bad); 249390287Sobrien int u = decl_jump_unsafe (b); 249490287Sobrien 249590287Sobrien if (u > 1 && DECL_ARTIFICIAL (b)) 2496169699Skan { 2497169699Skan /* Can't skip init of __exception_info. */ 2498169699Skan error ("%J enters catch block", b); 2499169699Skan saw_catch = true; 2500169699Skan } 250190287Sobrien else if (u > 1) 2502169699Skan error (" skips initialization of %q+#D", b); 250390287Sobrien else 2504169699Skan pedwarn (" enters scope of non-POD %q+#D", b); 250590287Sobrien } 250690287Sobrien 2507169699Skan if (ent->in_try_scope) 250890287Sobrien error (" enters try block"); 2509169699Skan else if (ent->in_catch_scope && !saw_catch) 251090287Sobrien error (" enters catch block"); 2511169699Skan 2512169699Skan if (ent->in_omp_scope) 2513169699Skan error (" enters OpenMP structured block"); 2514169699Skan else if (flag_openmp) 2515169699Skan { 2516169699Skan struct cp_binding_level *b; 2517169699Skan for (b = current_binding_level; b ; b = b->level_chain) 2518169699Skan { 2519169699Skan if (b == ent->binding_level) 2520169699Skan break; 2521169699Skan if (b->kind == sk_omp) 2522169699Skan { 2523169699Skan if (!identified) 2524169699Skan { 2525169699Skan pedwarn ("jump to label %q+D", decl); 2526169699Skan pedwarn (" from here"); 2527169699Skan identified = true; 2528169699Skan } 2529169699Skan error (" exits OpenMP structured block"); 2530169699Skan break; 2531169699Skan } 2532169699Skan } 2533169699Skan } 253418334Speter} 253518334Speter 2536169699Skan/* Check that a return is ok wrt OpenMP structured blocks. 2537169699Skan Called by finish_return_stmt. Returns true if all is well. */ 2538169699Skan 2539169699Skanbool 2540169699Skancheck_omp_return (void) 2541169699Skan{ 2542169699Skan struct cp_binding_level *b; 2543169699Skan for (b = current_binding_level; b ; b = b->level_chain) 2544169699Skan if (b->kind == sk_omp) 2545169699Skan { 2546169699Skan error ("invalid exit from OpenMP structured block"); 2547169699Skan return false; 2548169699Skan } 2549169699Skan return true; 2550169699Skan} 2551169699Skan 255218334Speter/* Define a label, specifying the location in the source file. 2553122192Skan Return the LABEL_DECL node for the label. */ 255418334Speter 255518334Spetertree 2556132747Skandefine_label (location_t location, tree name) 255718334Speter{ 2558169699Skan struct named_label_entry *ent, dummy; 2559132747Skan struct cp_binding_level *p; 2560169699Skan tree decl; 256118334Speter 2562117410Skan timevar_push (TV_NAME_LOOKUP); 256351412Sobrien 2564169699Skan decl = lookup_label (name); 2565169699Skan 2566169699Skan dummy.label_decl = decl; 2567169699Skan ent = (struct named_label_entry *) htab_find (named_labels, &dummy); 2568169699Skan gcc_assert (ent != NULL); 2569169699Skan 257090287Sobrien /* After labels, make any new cleanups in the function go into their 257118334Speter own new (temporary) binding contour. */ 2572169699Skan for (p = current_binding_level; 2573169699Skan p->kind != sk_function_parms; 2574132747Skan p = p->level_chain) 257590287Sobrien p->more_cleanups_ok = 0; 257618334Speter 257718334Speter if (name == get_identifier ("wchar_t")) 257890287Sobrien pedwarn ("label named wchar_t"); 257918334Speter 258018334Speter if (DECL_INITIAL (decl) != NULL_TREE) 2581169699Skan { 2582169699Skan error ("duplicate label %qD", decl); 2583169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 2584169699Skan } 258518334Speter else 258618334Speter { 2587169699Skan struct named_label_use_entry *use; 2588169699Skan 258918334Speter /* Mark label as having been defined. */ 259018334Speter DECL_INITIAL (decl) = error_mark_node; 259118334Speter /* Say where in the source. */ 2592132747Skan DECL_SOURCE_LOCATION (decl) = location; 2593169699Skan 2594169699Skan ent->binding_level = current_binding_level; 2595169699Skan ent->names_in_scope = current_binding_level->names; 2596169699Skan 2597169699Skan for (use = ent->uses; use ; use = use->next) 2598169699Skan check_previous_goto (decl, use); 2599169699Skan ent->uses = NULL; 260018334Speter } 2601122192Skan 2602169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 260318334Speter} 260418334Speter 260518334Speterstruct cp_switch 260618334Speter{ 2607117410Skan struct cp_binding_level *level; 260818334Speter struct cp_switch *next; 260990287Sobrien /* The SWITCH_STMT being built. */ 261090287Sobrien tree switch_stmt; 261190287Sobrien /* A splay-tree mapping the low element of a case range to the high 261290287Sobrien element, or NULL_TREE if there is no high element. Used to 261390287Sobrien determine whether or not a new case label duplicates an old case 261490287Sobrien label. We need a tree, rather than simply a hash table, because 261590287Sobrien of the GNU case range extension. */ 261690287Sobrien splay_tree cases; 261718334Speter}; 261818334Speter 261990287Sobrien/* A stack of the currently active switch statements. The innermost 262090287Sobrien switch statement is on the top of the stack. There is no need to 262190287Sobrien mark the stack for garbage collection because it is only active 262290287Sobrien during the processing of the body of a function, and we never 262390287Sobrien collect at that point. */ 262490287Sobrien 262518334Speterstatic struct cp_switch *switch_stack; 262618334Speter 262790287Sobrien/* Called right after a switch-statement condition is parsed. 262890287Sobrien SWITCH_STMT is the switch statement being parsed. */ 262990287Sobrien 263018334Spetervoid 2631132747Skanpush_switch (tree switch_stmt) 263218334Speter{ 2633169699Skan struct cp_switch *p = XNEW (struct cp_switch); 263418334Speter p->level = current_binding_level; 263518334Speter p->next = switch_stack; 263690287Sobrien p->switch_stmt = switch_stmt; 263790287Sobrien p->cases = splay_tree_new (case_compare, NULL, NULL); 263818334Speter switch_stack = p; 263918334Speter} 264018334Speter 264118334Spetervoid 2642132747Skanpop_switch (void) 264318334Speter{ 2644169699Skan struct cp_switch *cs = switch_stack; 2645169699Skan location_t switch_location; 264690287Sobrien 2647169699Skan /* Emit warnings as needed. */ 2648169699Skan if (EXPR_HAS_LOCATION (cs->switch_stmt)) 2649169699Skan switch_location = EXPR_LOCATION (cs->switch_stmt); 2650169699Skan else 2651169699Skan switch_location = input_location; 2652169699Skan if (!processing_template_decl) 2653169699Skan c_do_switch_warnings (cs->cases, switch_location, 2654169699Skan SWITCH_STMT_TYPE (cs->switch_stmt), 2655169699Skan SWITCH_STMT_COND (cs->switch_stmt)); 2656169699Skan 265790287Sobrien splay_tree_delete (cs->cases); 265818334Speter switch_stack = switch_stack->next; 265990287Sobrien free (cs); 266018334Speter} 266118334Speter 266290287Sobrien/* Note that we've seen a definition of a case label, and complain if this 266390287Sobrien is a bad place for one. */ 266451412Sobrien 266590287Sobrientree 2666132747Skanfinish_case_label (tree low_value, tree high_value) 266718334Speter{ 266890287Sobrien tree cond, r; 2669132747Skan struct cp_binding_level *p; 267018334Speter 267190287Sobrien if (processing_template_decl) 267218334Speter { 267390287Sobrien tree label; 267490287Sobrien 267590287Sobrien /* For templates, just add the case label; we'll do semantic 267690287Sobrien analysis at instantiation-time. */ 267790287Sobrien label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); 267890287Sobrien return add_stmt (build_case_label (low_value, high_value, label)); 267918334Speter } 268018334Speter 268190287Sobrien /* Find the condition on which this switch statement depends. */ 2682169699Skan cond = SWITCH_STMT_COND (switch_stack->switch_stmt); 268390287Sobrien if (cond && TREE_CODE (cond) == TREE_LIST) 268490287Sobrien cond = TREE_VALUE (cond); 268590287Sobrien 2686169699Skan if (!check_switch_goto (switch_stack->level)) 2687169699Skan return error_mark_node; 268890287Sobrien 2689169699Skan r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond), 2690169699Skan low_value, high_value); 269190287Sobrien 269290287Sobrien /* After labels, make any new cleanups in the function go into their 269318334Speter own new (temporary) binding contour. */ 2694169699Skan for (p = current_binding_level; 2695169699Skan p->kind != sk_function_parms; 2696132747Skan p = p->level_chain) 269790287Sobrien p->more_cleanups_ok = 0; 269818334Speter 269990287Sobrien return r; 270018334Speter} 270118334Speter 270252290Sobrien/* Hash a TYPENAME_TYPE. K is really of type `tree'. */ 270352290Sobrien 2704117410Skanstatic hashval_t 2705132747Skantypename_hash (const void* k) 270652290Sobrien{ 2707117410Skan hashval_t hash; 2708117410Skan tree t = (tree) k; 270952290Sobrien 2710117410Skan hash = (htab_hash_pointer (TYPE_CONTEXT (t)) 2711117410Skan ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t)))); 271252290Sobrien 271352290Sobrien return hash; 271452290Sobrien} 271552290Sobrien 2716169699Skantypedef struct typename_info { 2717169699Skan tree scope; 2718169699Skan tree name; 2719169699Skan tree template_id; 2720169699Skan bool enum_p; 2721169699Skan bool class_p; 2722169699Skan} typename_info; 2723169699Skan 272452290Sobrien/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */ 272552290Sobrien 2726117410Skanstatic int 2727132747Skantypename_compare (const void * k1, const void * k2) 272852290Sobrien{ 272952290Sobrien tree t1; 2730169699Skan const typename_info *t2; 273152290Sobrien 273252290Sobrien t1 = (tree) k1; 2733169699Skan t2 = (const typename_info *) k2; 273490287Sobrien 2735169699Skan return (DECL_NAME (TYPE_NAME (t1)) == t2->name 2736169699Skan && TYPE_CONTEXT (t1) == t2->scope 2737169699Skan && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id 2738169699Skan && TYPENAME_IS_ENUM_P (t1) == t2->enum_p 2739169699Skan && TYPENAME_IS_CLASS_P (t1) == t2->class_p); 274052290Sobrien} 274152290Sobrien 274252290Sobrien/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is 2743169699Skan the type of `T', NAME is the IDENTIFIER_NODE for `t'. 274452290Sobrien 274552290Sobrien Returns the new TYPENAME_TYPE. */ 274652290Sobrien 2747117410Skanstatic GTY ((param_is (union tree_node))) htab_t typename_htab; 2748117410Skan 2749132747Skanstatic tree 2750169699Skanbuild_typename_type (tree context, tree name, tree fullname, 2751169699Skan enum tag_types tag_type) 275252290Sobrien{ 275352290Sobrien tree t; 275452290Sobrien tree d; 2755169699Skan typename_info ti; 2756132747Skan void **e; 2757169699Skan hashval_t hash; 275852290Sobrien 2759117410Skan if (typename_htab == NULL) 2760169699Skan typename_htab = htab_create_ggc (61, &typename_hash, 2761169699Skan &typename_compare, NULL); 276252290Sobrien 2763169699Skan ti.scope = FROB_CONTEXT (context); 2764169699Skan ti.name = name; 2765169699Skan ti.template_id = fullname; 2766169699Skan ti.enum_p = tag_type == enum_type; 2767169699Skan ti.class_p = (tag_type == class_type 2768169699Skan || tag_type == record_type 2769169699Skan || tag_type == union_type); 2770169699Skan hash = (htab_hash_pointer (ti.scope) 2771169699Skan ^ htab_hash_pointer (ti.name)); 277252290Sobrien 277352290Sobrien /* See if we already have this type. */ 2774169699Skan e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT); 2775117410Skan if (*e) 2776117410Skan t = (tree) *e; 277752290Sobrien else 2778169699Skan { 2779169699Skan /* Build the TYPENAME_TYPE. */ 2780169699Skan t = make_aggr_type (TYPENAME_TYPE); 2781169699Skan TYPE_CONTEXT (t) = ti.scope; 2782169699Skan TYPENAME_TYPE_FULLNAME (t) = ti.template_id; 2783169699Skan TYPENAME_IS_ENUM_P (t) = ti.enum_p; 2784169699Skan TYPENAME_IS_CLASS_P (t) = ti.class_p; 278552290Sobrien 2786169699Skan /* Build the corresponding TYPE_DECL. */ 2787169699Skan d = build_decl (TYPE_DECL, name, t); 2788169699Skan TYPE_NAME (TREE_TYPE (d)) = d; 2789169699Skan TYPE_STUB_DECL (TREE_TYPE (d)) = d; 2790169699Skan DECL_CONTEXT (d) = FROB_CONTEXT (context); 2791169699Skan DECL_ARTIFICIAL (d) = 1; 2792169699Skan 2793169699Skan /* Store it in the hash table. */ 2794169699Skan *e = t; 2795169699Skan } 2796169699Skan 279752290Sobrien return t; 279852290Sobrien} 279952290Sobrien 2800169699Skan/* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag 2801169699Skan provided to name the type. Returns an appropriate type, unless an 2802169699Skan error occurs, in which case error_mark_node is returned. If we 2803169699Skan locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we 2804169699Skan return that, rather than the _TYPE it corresponds to, in other 2805169699Skan cases we look through the type decl. If TF_ERROR is set, complain 2806169699Skan about errors, otherwise be quiet. */ 280790287Sobrien 280852290Sobrientree 2809169699Skanmake_typename_type (tree context, tree name, enum tag_types tag_type, 2810169699Skan tsubst_flags_t complain) 281151412Sobrien{ 281251412Sobrien tree fullname; 2813169699Skan tree t; 2814169699Skan bool want_template; 281551412Sobrien 2816132747Skan if (name == error_mark_node 2817132747Skan || context == NULL_TREE 2818132747Skan || context == error_mark_node) 2819132747Skan return error_mark_node; 2820132747Skan 282190287Sobrien if (TYPE_P (name)) 282252290Sobrien { 282390287Sobrien if (!(TYPE_LANG_SPECIFIC (name) 282490287Sobrien && (CLASSTYPE_IS_TEMPLATE (name) 282552290Sobrien || CLASSTYPE_USE_TEMPLATE (name)))) 282652290Sobrien name = TYPE_IDENTIFIER (name); 282752290Sobrien else 282852290Sobrien /* Create a TEMPLATE_ID_EXPR for the type. */ 282952290Sobrien name = build_nt (TEMPLATE_ID_EXPR, 283052290Sobrien CLASSTYPE_TI_TEMPLATE (name), 283152290Sobrien CLASSTYPE_TI_ARGS (name)); 283252290Sobrien } 283351412Sobrien else if (TREE_CODE (name) == TYPE_DECL) 283451412Sobrien name = DECL_NAME (name); 283551412Sobrien 283651412Sobrien fullname = name; 283751412Sobrien 283851412Sobrien if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 283951412Sobrien { 284051412Sobrien name = TREE_OPERAND (name, 0); 284151412Sobrien if (TREE_CODE (name) == TEMPLATE_DECL) 284251412Sobrien name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); 2843169699Skan else if (TREE_CODE (name) == OVERLOAD) 2844169699Skan { 2845169699Skan error ("%qD is not a type", name); 2846169699Skan return error_mark_node; 2847169699Skan } 284851412Sobrien } 284990287Sobrien if (TREE_CODE (name) == TEMPLATE_DECL) 285090287Sobrien { 2851169699Skan error ("%qD used without template parameters", name); 285290287Sobrien return error_mark_node; 285390287Sobrien } 2854169699Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 2855169699Skan gcc_assert (TYPE_P (context)); 285652290Sobrien 2857169699Skan /* When the CONTEXT is a dependent type, NAME could refer to a 2858169699Skan dependent base class of CONTEXT. So we cannot peek inside it, 2859169699Skan even if CONTEXT is a currently open scope. */ 2860169699Skan if (dependent_type_p (context)) 2861169699Skan return build_typename_type (context, name, fullname, tag_type); 2862169699Skan 2863169699Skan if (!IS_AGGR_TYPE (context)) 286451412Sobrien { 2865169699Skan if (complain & tf_error) 2866169699Skan error ("%q#T is not a class", context); 2867169699Skan return error_mark_node; 286851412Sobrien } 2869169699Skan 2870169699Skan want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR; 2871169699Skan 2872169699Skan /* We should only set WANT_TYPE when we're a nested typename type. 2873169699Skan Then we can give better diagnostics if we find a non-type. */ 2874169699Skan t = lookup_field (context, name, 0, /*want_type=*/true); 2875169699Skan if (!t) 287652290Sobrien { 287796292Sobrien if (complain & tf_error) 2878169699Skan error (want_template ? "no class template named %q#T in %q#T" 2879169699Skan : "no type named %q#T in %q#T", name, context); 288052290Sobrien return error_mark_node; 288152290Sobrien } 2882169699Skan 2883169699Skan if (want_template && !DECL_CLASS_TEMPLATE_P (t)) 2884169699Skan { 2885169699Skan if (complain & tf_error) 2886169699Skan error ("%<typename %T::%D%> names %q#T, which is not a class template", 2887169699Skan context, name, t); 2888169699Skan return error_mark_node; 2889169699Skan } 2890169699Skan if (!want_template && TREE_CODE (t) != TYPE_DECL) 2891169699Skan { 2892169699Skan if (complain & tf_error) 2893169699Skan error ("%<typename %T::%D%> names %q#T, which is not a type", 2894169699Skan context, name, t); 2895169699Skan return error_mark_node; 2896169699Skan } 2897169699Skan 2898169699Skan if (complain & tf_error) 2899169699Skan perform_or_defer_access_check (TYPE_BINFO (context), t, t); 290090287Sobrien 2901169699Skan if (want_template) 2902169699Skan return lookup_template_class (t, TREE_OPERAND (fullname, 1), 2903169699Skan NULL_TREE, context, 2904169699Skan /*entering_scope=*/0, 2905169699Skan tf_warning_or_error | tf_user); 2906169699Skan 2907169699Skan if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) 2908169699Skan t = TREE_TYPE (t); 2909169699Skan 2910169699Skan return t; 291151412Sobrien} 291251412Sobrien 2913169699Skan/* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name 2914169699Skan can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs, 2915169699Skan in which case error_mark_node is returned. 291690287Sobrien 2917169699Skan If PARM_LIST is non-NULL, also make sure that the template parameter 2918169699Skan list of TEMPLATE_DECL matches. 2919169699Skan 2920169699Skan If COMPLAIN zero, don't complain about any errors that occur. */ 2921169699Skan 292290287Sobrientree 2923169699Skanmake_unbound_class_template (tree context, tree name, tree parm_list, 2924169699Skan tsubst_flags_t complain) 292590287Sobrien{ 292690287Sobrien tree t; 292790287Sobrien tree d; 292890287Sobrien 292990287Sobrien if (TYPE_P (name)) 293090287Sobrien name = TYPE_IDENTIFIER (name); 293190287Sobrien else if (DECL_P (name)) 293290287Sobrien name = DECL_NAME (name); 2933169699Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 293490287Sobrien 2935132747Skan if (!dependent_type_p (context) 293690287Sobrien || currently_open_class (context)) 293790287Sobrien { 293890287Sobrien tree tmpl = NULL_TREE; 293990287Sobrien 294090287Sobrien if (IS_AGGR_TYPE (context)) 2941132747Skan tmpl = lookup_field (context, name, 0, false); 294290287Sobrien 294390287Sobrien if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) 294490287Sobrien { 2945117410Skan if (complain & tf_error) 2946169699Skan error ("no class template named %q#T in %q#T", name, context); 294790287Sobrien return error_mark_node; 294890287Sobrien } 2949169699Skan 2950169699Skan if (parm_list 2951169699Skan && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list)) 2952169699Skan { 2953169699Skan if (complain & tf_error) 2954169699Skan { 2955169699Skan error ("template parameters do not match template"); 2956169699Skan error ("%q+D declared here", tmpl); 2957169699Skan } 2958169699Skan return error_mark_node; 2959169699Skan } 2960169699Skan 2961117410Skan if (complain & tf_error) 2962169699Skan perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl); 296390287Sobrien 296490287Sobrien return tmpl; 296590287Sobrien } 296690287Sobrien 296790287Sobrien /* Build the UNBOUND_CLASS_TEMPLATE. */ 296890287Sobrien t = make_aggr_type (UNBOUND_CLASS_TEMPLATE); 296990287Sobrien TYPE_CONTEXT (t) = FROB_CONTEXT (context); 297090287Sobrien TREE_TYPE (t) = NULL_TREE; 297190287Sobrien 297290287Sobrien /* Build the corresponding TEMPLATE_DECL. */ 297390287Sobrien d = build_decl (TEMPLATE_DECL, name, t); 297490287Sobrien TYPE_NAME (TREE_TYPE (d)) = d; 297590287Sobrien TYPE_STUB_DECL (TREE_TYPE (d)) = d; 297690287Sobrien DECL_CONTEXT (d) = FROB_CONTEXT (context); 297790287Sobrien DECL_ARTIFICIAL (d) = 1; 2978169699Skan DECL_TEMPLATE_PARMS (d) = parm_list; 297990287Sobrien 298090287Sobrien return t; 298190287Sobrien} 298290287Sobrien 2983132747Skan 298451412Sobrien 298518334Speter/* Push the declarations of builtin types into the namespace. 2986132747Skan RID_INDEX is the index of the builtin type in the array 2987132747Skan RID_POINTERS. NAME is the name used when looking up the builtin 2988132747Skan type. TYPE is the _TYPE node for the builtin type. */ 298918334Speter 299090287Sobrienvoid 2991169699Skanrecord_builtin_type (enum rid rid_index, 2992169699Skan const char* name, 2993169699Skan tree type) 299418334Speter{ 299518334Speter tree rname = NULL_TREE, tname = NULL_TREE; 299651412Sobrien tree tdecl = NULL_TREE; 299718334Speter 299818334Speter if ((int) rid_index < (int) RID_MAX) 299918334Speter rname = ridpointers[(int) rid_index]; 300018334Speter if (name) 300118334Speter tname = get_identifier (name); 300218334Speter 3003132747Skan /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be 3004132747Skan eliminated. Built-in types should not be looked up name; their 3005132747Skan names are keywords that the parser can recognize. However, there 3006132747Skan is code in c-common.c that uses identifier_global_value to look 3007132747Skan up built-in types by name. */ 300818334Speter if (tname) 300918334Speter { 3010132747Skan tdecl = build_decl (TYPE_DECL, tname, type); 3011132747Skan DECL_ARTIFICIAL (tdecl) = 1; 3012132747Skan SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl); 301318334Speter } 3014132747Skan if (rname) 301518334Speter { 3016132747Skan if (!tdecl) 301718334Speter { 3018132747Skan tdecl = build_decl (TYPE_DECL, rname, type); 3019132747Skan DECL_ARTIFICIAL (tdecl) = 1; 302018334Speter } 3021132747Skan SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl); 302218334Speter } 3023132747Skan 3024132747Skan if (!TYPE_NAME (type)) 3025132747Skan TYPE_NAME (type) = tdecl; 3026132747Skan 3027132747Skan if (tdecl) 3028169699Skan debug_hooks->type_decl (tdecl, 0); 302951412Sobrien} 303018334Speter 303151412Sobrien/* Record one of the standard Java types. 303251412Sobrien * Declare it as having the given NAME. 303351412Sobrien * If SIZE > 0, it is the size of one of the integral types; 303451412Sobrien * otherwise it is the negative of the size of one of the other types. */ 303551412Sobrien 303651412Sobrienstatic tree 3037132747Skanrecord_builtin_java_type (const char* name, int size) 303851412Sobrien{ 303951412Sobrien tree type, decl; 304051412Sobrien if (size > 0) 304151412Sobrien type = make_signed_type (size); 304251412Sobrien else if (size > -32) 3043117410Skan { /* "__java_char" or ""__java_boolean". */ 304451412Sobrien type = make_unsigned_type (-size); 304551412Sobrien /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/ 304618334Speter } 304751412Sobrien else 3048117410Skan { /* "__java_float" or ""__java_double". */ 304951412Sobrien type = make_node (REAL_TYPE); 305051412Sobrien TYPE_PRECISION (type) = - size; 305151412Sobrien layout_type (type); 305251412Sobrien } 305351412Sobrien record_builtin_type (RID_MAX, name, type); 305451412Sobrien decl = TYPE_NAME (type); 305552290Sobrien 305652290Sobrien /* Suppress generate debug symbol entries for these types, 305752290Sobrien since for normal C++ they are just clutter. 3058117410Skan However, push_lang_context undoes this if extern "Java" is seen. */ 305951412Sobrien DECL_IGNORED_P (decl) = 1; 306052290Sobrien 306151412Sobrien TYPE_FOR_JAVA (type) = 1; 306251412Sobrien return type; 306318334Speter} 306418334Speter 3065117410Skan/* Push a type into the namespace so that the back-ends ignore it. */ 306651412Sobrien 306718334Speterstatic void 3068132747Skanrecord_unknown_type (tree type, const char* name) 306918334Speter{ 307051412Sobrien tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type)); 307151412Sobrien /* Make sure the "unknown type" typedecl gets ignored for debug info. */ 307251412Sobrien DECL_IGNORED_P (decl) = 1; 307351412Sobrien TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 307451412Sobrien TYPE_SIZE (type) = TYPE_SIZE (void_type_node); 307551412Sobrien TYPE_ALIGN (type) = 1; 307690287Sobrien TYPE_USER_ALIGN (type) = 0; 307751412Sobrien TYPE_MODE (type) = TYPE_MODE (void_type_node); 307890287Sobrien} 307918334Speter 3080169699Skan/* A string for which we should create an IDENTIFIER_NODE at 308190287Sobrien startup. */ 308251412Sobrien 308390287Sobrientypedef struct predefined_identifier 308490287Sobrien{ 308590287Sobrien /* The name of the identifier. */ 308690287Sobrien const char *const name; 308790287Sobrien /* The place where the IDENTIFIER_NODE should be stored. */ 308890287Sobrien tree *const node; 3089117410Skan /* Nonzero if this is the name of a constructor or destructor. */ 309090287Sobrien const int ctor_or_dtor_p; 309190287Sobrien} predefined_identifier; 309290287Sobrien 309390287Sobrien/* Create all the predefined identifiers. */ 309490287Sobrien 309518334Speterstatic void 3096132747Skaninitialize_predefined_identifiers (void) 309718334Speter{ 309890287Sobrien const predefined_identifier *pid; 309918334Speter 310090287Sobrien /* A table of identifiers to create at startup. */ 310190287Sobrien static const predefined_identifier predefined_identifiers[] = { 310290287Sobrien { "C++", &lang_name_cplusplus, 0 }, 310390287Sobrien { "C", &lang_name_c, 0 }, 310490287Sobrien { "Java", &lang_name_java, 0 }, 3105169699Skan /* Some of these names have a trailing space so that it is 3106169699Skan impossible for them to conflict with names written by users. */ 3107169699Skan { "__ct ", &ctor_identifier, 1 }, 3108169699Skan { "__base_ctor ", &base_ctor_identifier, 1 }, 3109169699Skan { "__comp_ctor ", &complete_ctor_identifier, 1 }, 3110169699Skan { "__dt ", &dtor_identifier, 1 }, 3111169699Skan { "__comp_dtor ", &complete_dtor_identifier, 1 }, 3112169699Skan { "__base_dtor ", &base_dtor_identifier, 1 }, 3113169699Skan { "__deleting_dtor ", &deleting_dtor_identifier, 1 }, 311490287Sobrien { IN_CHARGE_NAME, &in_charge_identifier, 0 }, 311590287Sobrien { "nelts", &nelts_identifier, 0 }, 311690287Sobrien { THIS_NAME, &this_identifier, 0 }, 311790287Sobrien { VTABLE_DELTA_NAME, &delta_identifier, 0 }, 311890287Sobrien { VTABLE_PFN_NAME, &pfn_identifier, 0 }, 311990287Sobrien { "_vptr", &vptr_identifier, 0 }, 312090287Sobrien { "__vtt_parm", &vtt_parm_identifier, 0 }, 3121132747Skan { "::", &global_scope_name, 0 }, 312290287Sobrien { "std", &std_identifier, 0 }, 312390287Sobrien { NULL, NULL, 0 } 312490287Sobrien }; 312590287Sobrien 312690287Sobrien for (pid = predefined_identifiers; pid->name; ++pid) 312790287Sobrien { 312890287Sobrien *pid->node = get_identifier (pid->name); 312990287Sobrien if (pid->ctor_or_dtor_p) 313090287Sobrien IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1; 313190287Sobrien } 313218334Speter} 313318334Speter 313418334Speter/* Create the predefined scalar types of C, 313518334Speter and some nodes representing standard constants (0, 1, (void *)0). 313618334Speter Initialize the global binding level. 313718334Speter Make definitions for built-in primitive functions. */ 313818334Speter 313918334Spetervoid 3140132747Skancxx_init_decl_processing (void) 314118334Speter{ 314290287Sobrien tree void_ftype; 314390287Sobrien tree void_ftype_ptr; 314418334Speter 3145169699Skan build_common_tree_nodes (flag_signed_char, false); 3146169699Skan 314790287Sobrien /* Create all the identifiers we need. */ 314890287Sobrien initialize_predefined_identifiers (); 314918334Speter 315090287Sobrien /* Create the global variables. */ 315190287Sobrien push_to_top_level (); 315290287Sobrien 3153132747Skan current_function_decl = NULL_TREE; 3154132747Skan current_binding_level = NULL; 3155117410Skan /* Enter the global namespace. */ 3156169699Skan gcc_assert (global_namespace == NULL_TREE); 3157132747Skan global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name, 3158169699Skan void_type_node); 3159169699Skan TREE_PUBLIC (global_namespace) = 1; 3160132747Skan begin_scope (sk_namespace, global_namespace); 3161132747Skan 316251412Sobrien current_lang_name = NULL_TREE; 316351412Sobrien 316490287Sobrien /* Adjust various flags based on command-line settings. */ 3165117410Skan if (!flag_permissive) 316652290Sobrien flag_pedantic_errors = 1; 316790287Sobrien if (!flag_no_inline) 316890287Sobrien { 316990287Sobrien flag_inline_trees = 1; 317090287Sobrien flag_no_inline = 1; 317190287Sobrien } 317290287Sobrien if (flag_inline_functions) 3173169699Skan flag_inline_trees = 2; 3174260075Spfg 3175260075Spfg if (flag_visibility_ms_compat) 3176260075Spfg default_visibility = VISIBILITY_HIDDEN; 317718334Speter 317818334Speter /* Initially, C. */ 317918334Speter current_lang_name = lang_name_c; 318018334Speter 318190287Sobrien /* Create the `std' namespace. */ 318290287Sobrien push_namespace (std_identifier); 318390287Sobrien std_node = current_namespace; 318490287Sobrien pop_namespace (); 318518334Speter 318690287Sobrien c_common_nodes_and_builtins (); 318718334Speter 318851412Sobrien java_byte_type_node = record_builtin_java_type ("__java_byte", 8); 318951412Sobrien java_short_type_node = record_builtin_java_type ("__java_short", 16); 319051412Sobrien java_int_type_node = record_builtin_java_type ("__java_int", 32); 319151412Sobrien java_long_type_node = record_builtin_java_type ("__java_long", 64); 319251412Sobrien java_float_type_node = record_builtin_java_type ("__java_float", -32); 319351412Sobrien java_double_type_node = record_builtin_java_type ("__java_double", -64); 319451412Sobrien java_char_type_node = record_builtin_java_type ("__java_char", -16); 319551412Sobrien java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1); 319651412Sobrien 3197169699Skan integer_two_node = build_int_cst (NULL_TREE, 2); 3198169699Skan integer_three_node = build_int_cst (NULL_TREE, 3); 319918334Speter 320018334Speter record_builtin_type (RID_BOOL, "bool", boolean_type_node); 3201132747Skan truthvalue_type_node = boolean_type_node; 3202132747Skan truthvalue_false_node = boolean_false_node; 3203132747Skan truthvalue_true_node = boolean_true_node; 320418334Speter 320590287Sobrien empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); 320618334Speter 320751412Sobrien#if 0 320890287Sobrien record_builtin_type (RID_MAX, NULL, string_type_node); 320951412Sobrien#endif 321018334Speter 321190287Sobrien delta_type_node = ptrdiff_type_node; 321290287Sobrien vtable_index_type = ptrdiff_type_node; 321318334Speter 321490287Sobrien vtt_parm_type = build_pointer_type (const_ptr_type_node); 321590287Sobrien void_ftype = build_function_type (void_type_node, void_list_node); 321690287Sobrien void_ftype_ptr = build_function_type (void_type_node, 321790287Sobrien tree_cons (NULL_TREE, 3218169699Skan ptr_type_node, 321990287Sobrien void_list_node)); 322051412Sobrien void_ftype_ptr 322190287Sobrien = build_exception_variant (void_ftype_ptr, empty_except_spec); 322251412Sobrien 322318334Speter /* C++ extensions */ 322418334Speter 322518334Speter unknown_type_node = make_node (UNKNOWN_TYPE); 322651412Sobrien record_unknown_type (unknown_type_node, "unknown type"); 322751412Sobrien 322818334Speter /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ 322918334Speter TREE_TYPE (unknown_type_node) = unknown_type_node; 323051412Sobrien 323151412Sobrien /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same 323251412Sobrien result. */ 323318334Speter TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; 323418334Speter TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; 323518334Speter 323690287Sobrien { 323790287Sobrien /* Make sure we get a unique function type, so we can give 323890287Sobrien its pointer type a name. (This wins for gdb.) */ 323990287Sobrien tree vfunc_type = make_node (FUNCTION_TYPE); 324090287Sobrien TREE_TYPE (vfunc_type) = integer_type_node; 324190287Sobrien TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; 324290287Sobrien layout_type (vfunc_type); 324318334Speter 324490287Sobrien vtable_entry_type = build_pointer_type (vfunc_type); 324590287Sobrien } 324618334Speter record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type); 324718334Speter 324818334Speter vtbl_type_node 324952290Sobrien = build_cplus_array_type (vtable_entry_type, NULL_TREE); 325018334Speter layout_type (vtbl_type_node); 325152290Sobrien vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); 325290287Sobrien record_builtin_type (RID_MAX, NULL, vtbl_type_node); 325352290Sobrien vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); 325452290Sobrien layout_type (vtbl_ptr_type_node); 325590287Sobrien record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); 325618334Speter 325790287Sobrien push_namespace (get_identifier ("__cxxabiv1")); 325890287Sobrien abi_node = current_namespace; 325990287Sobrien pop_namespace (); 326060970Sobrien 326151412Sobrien global_type_node = make_node (LANG_TYPE); 326251412Sobrien record_unknown_type (global_type_node, "global type"); 326318334Speter 326418334Speter /* Now, C++. */ 326518334Speter current_lang_name = lang_name_cplusplus; 326618334Speter 326751412Sobrien { 3268132747Skan tree bad_alloc_id; 3269132747Skan tree bad_alloc_type_node; 3270132747Skan tree bad_alloc_decl; 3271132747Skan tree newtype, deltype; 327290287Sobrien tree ptr_ftype_sizetype; 327390287Sobrien 327490287Sobrien push_namespace (std_identifier); 3275132747Skan bad_alloc_id = get_identifier ("bad_alloc"); 3276132747Skan bad_alloc_type_node = make_aggr_type (RECORD_TYPE); 3277132747Skan TYPE_CONTEXT (bad_alloc_type_node) = current_namespace; 3278169699Skan bad_alloc_decl 3279132747Skan = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node); 3280132747Skan DECL_CONTEXT (bad_alloc_decl) = current_namespace; 3281132747Skan TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl; 328290287Sobrien pop_namespace (); 3283169699Skan 3284169699Skan ptr_ftype_sizetype 328590287Sobrien = build_function_type (ptr_type_node, 328690287Sobrien tree_cons (NULL_TREE, 3287110631Skan size_type_node, 328890287Sobrien void_list_node)); 328951412Sobrien newtype = build_exception_variant 329090287Sobrien (ptr_ftype_sizetype, add_exception_specifier 329190287Sobrien (NULL_TREE, bad_alloc_type_node, -1)); 329290287Sobrien deltype = build_exception_variant (void_ftype_ptr, empty_except_spec); 329390287Sobrien push_cp_library_fn (NEW_EXPR, newtype); 329490287Sobrien push_cp_library_fn (VEC_NEW_EXPR, newtype); 329590287Sobrien global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype); 329690287Sobrien push_cp_library_fn (VEC_DELETE_EXPR, deltype); 329751412Sobrien } 329818334Speter 329918334Speter abort_fndecl 330090287Sobrien = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype); 330118334Speter 330218334Speter /* Perform other language dependent initializations. */ 330318334Speter init_class_processing (); 330490287Sobrien init_rtti_processing (); 330518334Speter 330651412Sobrien if (flag_exceptions) 330718334Speter init_exception_processing (); 330818334Speter 330951412Sobrien if (! supports_one_only ()) 331051412Sobrien flag_weak = 0; 331151412Sobrien 331290287Sobrien make_fname_decl = cp_make_fname_decl; 331390287Sobrien start_fname_decls (); 331418334Speter 331551412Sobrien /* Show we use EH for cleanups. */ 3316169699Skan if (flag_exceptions) 3317169699Skan using_eh_for_cleanups (); 331818334Speter} 331918334Speter 332090287Sobrien/* Generate an initializer for a function naming variable from 3321132747Skan NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is 3322132747Skan filled in with the type of the init. */ 332318334Speter 332490287Sobrientree 3325132747Skancp_fname_init (const char* name, tree *type_p) 332618334Speter{ 332790287Sobrien tree domain = NULL_TREE; 332890287Sobrien tree type; 332990287Sobrien tree init = NULL_TREE; 333090287Sobrien size_t length = 0; 333190287Sobrien 333290287Sobrien if (name) 333390287Sobrien { 333490287Sobrien length = strlen (name); 333590287Sobrien domain = build_index_type (size_int (length)); 333690287Sobrien init = build_string (length + 1, name); 333790287Sobrien } 3338169699Skan 333990287Sobrien type = build_qualified_type (char_type_node, TYPE_QUAL_CONST); 334090287Sobrien type = build_cplus_array_type (type, domain); 334190287Sobrien 3342132747Skan *type_p = type; 3343169699Skan 334490287Sobrien if (init) 334590287Sobrien TREE_TYPE (init) = type; 334690287Sobrien else 3347132747Skan init = error_mark_node; 3348169699Skan 334990287Sobrien return init; 335051412Sobrien} 335118334Speter 335290287Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 335390287Sobrien decl, NAME is the initialization string and TYPE_DEP indicates whether 335490287Sobrien NAME depended on the type of the function. We make use of that to detect 335590287Sobrien __PRETTY_FUNCTION__ inside a template fn. This is being done 3356169699Skan lazily at the point of first use, so we mustn't push the decl now. */ 335790287Sobrien 335890287Sobrienstatic tree 3359132747Skancp_make_fname_decl (tree id, int type_dep) 336090287Sobrien{ 336190287Sobrien const char *const name = (type_dep && processing_template_decl 3362132747Skan ? NULL : fname_as_string (type_dep)); 3363132747Skan tree type; 3364132747Skan tree init = cp_fname_init (name, &type); 3365132747Skan tree decl = build_decl (VAR_DECL, id, type); 336690287Sobrien 3367169699Skan if (name) 3368169699Skan free ((char *) name); 3369169699Skan 3370132747Skan /* As we're using pushdecl_with_scope, we must set the context. */ 337190287Sobrien DECL_CONTEXT (decl) = current_function_decl; 337290287Sobrien DECL_PRETTY_FUNCTION_P (decl) = type_dep; 3373169699Skan 337490287Sobrien TREE_STATIC (decl) = 1; 337590287Sobrien TREE_READONLY (decl) = 1; 337690287Sobrien DECL_ARTIFICIAL (decl) = 1; 3377169699Skan 337890287Sobrien TREE_USED (decl) = 1; 337990287Sobrien 3380132747Skan if (current_function_decl) 3381132747Skan { 3382132747Skan struct cp_binding_level *b = current_binding_level; 3383132747Skan while (b->level_chain->kind != sk_function_parms) 3384132747Skan b = b->level_chain; 3385169699Skan pushdecl_with_scope (decl, b, /*is_friend=*/false); 3386169699Skan cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE, 3387169699Skan LOOKUP_ONLYCONVERTING); 3388132747Skan } 3389132747Skan else 3390132747Skan pushdecl_top_level_and_finish (decl, init); 3391169699Skan 339290287Sobrien return decl; 339390287Sobrien} 339490287Sobrien 339596292Sobrien/* Make a definition for a builtin function named NAME in the current 339696292Sobrien namespace, whose data type is TYPE and whose context is CONTEXT. 339796292Sobrien TYPE should be a function type with argument types. 339890287Sobrien 339990287Sobrien CLASS and CODE tell later passes how to compile calls to this function. 340090287Sobrien See tree.h for possible values. 340190287Sobrien 340290287Sobrien If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, 3403117410Skan the name to be called if we can't opencode the function. 3404117410Skan If ATTRS is nonzero, use that for the function's attribute 3405117410Skan list. */ 340618334Speter 340796292Sobrienstatic tree 3408132747Skanbuiltin_function_1 (const char* name, 3409169699Skan tree type, 3410169699Skan tree context, 3411169699Skan enum built_in_function code, 3412169699Skan enum built_in_class class, 3413169699Skan const char* libname, 3414169699Skan tree attrs) 341518334Speter{ 341690287Sobrien tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type); 341790287Sobrien DECL_BUILT_IN_CLASS (decl) = class; 341890287Sobrien DECL_FUNCTION_CODE (decl) = code; 341996292Sobrien DECL_CONTEXT (decl) = context; 342018334Speter 342190287Sobrien pushdecl (decl); 342290287Sobrien 342318334Speter /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME, 342418334Speter we cannot change DECL_ASSEMBLER_NAME until we have installed this 342518334Speter function in the namespace. */ 342690287Sobrien if (libname) 342790287Sobrien SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname)); 342890287Sobrien 3429169699Skan /* A function in the user's namespace should have an explicit 3430169699Skan declaration before it is used. Mark the built-in function as 3431169699Skan anticipated but not actually declared. */ 343290287Sobrien if (name[0] != '_' || name[1] != '_') 343390287Sobrien DECL_ANTICIPATED (decl) = 1; 343490287Sobrien 343590287Sobrien /* Possibly apply some default attributes to this built-in function. */ 3436117410Skan if (attrs) 3437117410Skan decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 3438117410Skan else 3439117410Skan decl_attributes (&decl, NULL_TREE, 0); 344090287Sobrien 344118334Speter return decl; 344218334Speter} 344390287Sobrien 344496292Sobrien/* Entry point for the benefit of c_common_nodes_and_builtins. 344596292Sobrien 3446132747Skan Make a definition for a builtin function named NAME and whose data type 344796292Sobrien is TYPE. TYPE should be a function type with argument types. This 344896292Sobrien function places the anticipated declaration in the global namespace 344996292Sobrien and additionally in the std namespace if appropriate. 345096292Sobrien 345196292Sobrien CLASS and CODE tell later passes how to compile calls to this function. 345296292Sobrien See tree.h for possible values. 345396292Sobrien 345496292Sobrien If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, 3455117410Skan the name to be called if we can't opencode the function. 345696292Sobrien 3457117410Skan If ATTRS is nonzero, use that for the function's attribute 3458117410Skan list. */ 3459117410Skan 346096292Sobrientree 3461132747Skanbuiltin_function (const char* name, 3462169699Skan tree type, 3463169699Skan int code, 3464169699Skan enum built_in_class cl, 3465169699Skan const char* libname, 3466169699Skan tree attrs) 346796292Sobrien{ 346896292Sobrien /* All builtins that don't begin with an '_' should additionally 346996292Sobrien go in the 'std' namespace. */ 347096292Sobrien if (name[0] != '_') 347196292Sobrien { 347296292Sobrien push_namespace (std_identifier); 3473169699Skan builtin_function_1 (name, type, std_node, code, cl, libname, attrs); 347496292Sobrien pop_namespace (); 347596292Sobrien } 347696292Sobrien 3477117410Skan return builtin_function_1 (name, type, NULL_TREE, code, 3478169699Skan cl, libname, attrs); 347996292Sobrien} 348096292Sobrien 348190287Sobrien/* Generate a FUNCTION_DECL with the typical flags for a runtime library 348290287Sobrien function. Not called directly. */ 348390287Sobrien 348490287Sobrienstatic tree 3485132747Skanbuild_library_fn_1 (tree name, enum tree_code operator_code, tree type) 348690287Sobrien{ 348790287Sobrien tree fn = build_lang_decl (FUNCTION_DECL, name, type); 348890287Sobrien DECL_EXTERNAL (fn) = 1; 348990287Sobrien TREE_PUBLIC (fn) = 1; 349090287Sobrien DECL_ARTIFICIAL (fn) = 1; 349190287Sobrien SET_OVERLOADED_OPERATOR_CODE (fn, operator_code); 349290287Sobrien SET_DECL_LANGUAGE (fn, lang_c); 3493169699Skan /* Runtime library routines are, by definition, available in an 3494169699Skan external shared object. */ 3495169699Skan DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT; 3496169699Skan DECL_VISIBILITY_SPECIFIED (fn) = 1; 349790287Sobrien return fn; 349890287Sobrien} 349990287Sobrien 350090287Sobrien/* Returns the _DECL for a library function with C linkage. 350190287Sobrien We assume that such functions never throw; if this is incorrect, 350290287Sobrien callers should unset TREE_NOTHROW. */ 350390287Sobrien 350490287Sobrientree 3505132747Skanbuild_library_fn (tree name, tree type) 350690287Sobrien{ 3507169699Skan tree fn = build_library_fn_1 (name, ERROR_MARK, type); 3508169699Skan TREE_NOTHROW (fn) = 1; 3509169699Skan return fn; 351090287Sobrien} 351190287Sobrien 351290287Sobrien/* Returns the _DECL for a library function with C++ linkage. */ 351390287Sobrien 351490287Sobrienstatic tree 3515132747Skanbuild_cp_library_fn (tree name, enum tree_code operator_code, tree type) 351690287Sobrien{ 351790287Sobrien tree fn = build_library_fn_1 (name, operator_code, type); 351890287Sobrien TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type); 351990287Sobrien DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); 352090287Sobrien SET_DECL_LANGUAGE (fn, lang_cplusplus); 352190287Sobrien return fn; 352290287Sobrien} 352390287Sobrien 352490287Sobrien/* Like build_library_fn, but takes a C string instead of an 352590287Sobrien IDENTIFIER_NODE. */ 352690287Sobrien 352790287Sobrientree 3528132747Skanbuild_library_fn_ptr (const char* name, tree type) 352990287Sobrien{ 353090287Sobrien return build_library_fn (get_identifier (name), type); 353190287Sobrien} 353290287Sobrien 353390287Sobrien/* Like build_cp_library_fn, but takes a C string instead of an 353490287Sobrien IDENTIFIER_NODE. */ 353590287Sobrien 353690287Sobrientree 3537132747Skanbuild_cp_library_fn_ptr (const char* name, tree type) 353890287Sobrien{ 353990287Sobrien return build_cp_library_fn (get_identifier (name), ERROR_MARK, type); 354090287Sobrien} 354190287Sobrien 354290287Sobrien/* Like build_library_fn, but also pushes the function so that we will 354390287Sobrien be able to find it via IDENTIFIER_GLOBAL_VALUE. */ 354490287Sobrien 354590287Sobrientree 3546132747Skanpush_library_fn (tree name, tree type) 354790287Sobrien{ 354890287Sobrien tree fn = build_library_fn (name, type); 354990287Sobrien pushdecl_top_level (fn); 355090287Sobrien return fn; 355190287Sobrien} 355290287Sobrien 355390287Sobrien/* Like build_cp_library_fn, but also pushes the function so that it 355490287Sobrien will be found by normal lookup. */ 355590287Sobrien 355690287Sobrienstatic tree 3557132747Skanpush_cp_library_fn (enum tree_code operator_code, tree type) 355890287Sobrien{ 355990287Sobrien tree fn = build_cp_library_fn (ansi_opname (operator_code), 356090287Sobrien operator_code, 356190287Sobrien type); 356290287Sobrien pushdecl (fn); 356390287Sobrien return fn; 356490287Sobrien} 356590287Sobrien 356690287Sobrien/* Like push_library_fn, but takes a TREE_LIST of parm types rather than 356790287Sobrien a FUNCTION_TYPE. */ 356890287Sobrien 356990287Sobrientree 3570132747Skanpush_void_library_fn (tree name, tree parmtypes) 357190287Sobrien{ 357290287Sobrien tree type = build_function_type (void_type_node, parmtypes); 357390287Sobrien return push_library_fn (name, type); 357490287Sobrien} 357590287Sobrien 357690287Sobrien/* Like push_library_fn, but also note that this function throws 357790287Sobrien and does not return. Used for __throw_foo and the like. */ 357890287Sobrien 357990287Sobrientree 3580132747Skanpush_throw_library_fn (tree name, tree type) 358190287Sobrien{ 358290287Sobrien tree fn = push_library_fn (name, type); 358390287Sobrien TREE_THIS_VOLATILE (fn) = 1; 358490287Sobrien TREE_NOTHROW (fn) = 0; 358590287Sobrien return fn; 358690287Sobrien} 358718334Speter 358852290Sobrien/* When we call finish_struct for an anonymous union, we create 358952290Sobrien default copy constructors and such. But, an anonymous union 359052290Sobrien shouldn't have such things; this function undoes the damage to the 359152290Sobrien anonymous union type T. 359218334Speter 359352290Sobrien (The reason that we create the synthesized methods is that we don't 359452290Sobrien distinguish `union { int i; }' from `typedef union { int i; } U'. 359552290Sobrien The first is an anonymous union; the second is just an ordinary 359652290Sobrien union type.) */ 359718334Speter 359818334Spetervoid 3599132747Skanfixup_anonymous_aggr (tree t) 360052290Sobrien{ 360152290Sobrien tree *q; 360252290Sobrien 3603132747Skan /* Wipe out memory of synthesized methods. */ 360452290Sobrien TYPE_HAS_CONSTRUCTOR (t) = 0; 360552290Sobrien TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; 360652290Sobrien TYPE_HAS_INIT_REF (t) = 0; 360752290Sobrien TYPE_HAS_CONST_INIT_REF (t) = 0; 360852290Sobrien TYPE_HAS_ASSIGN_REF (t) = 0; 360952290Sobrien TYPE_HAS_CONST_ASSIGN_REF (t) = 0; 361052290Sobrien 361152290Sobrien /* Splice the implicitly generated functions out of the TYPE_METHODS 361252290Sobrien list. */ 361352290Sobrien q = &TYPE_METHODS (t); 361452290Sobrien while (*q) 361552290Sobrien { 361652290Sobrien if (DECL_ARTIFICIAL (*q)) 361752290Sobrien *q = TREE_CHAIN (*q); 361852290Sobrien else 361952290Sobrien q = &TREE_CHAIN (*q); 362052290Sobrien } 362152290Sobrien 362290287Sobrien /* ISO C++ 9.5.3. Anonymous unions may not have function members. */ 362352290Sobrien if (TYPE_METHODS (t)) 3624132747Skan error ("%Jan anonymous union cannot have function members", 3625132747Skan TYPE_MAIN_DECL (t)); 362690287Sobrien 362790287Sobrien /* Anonymous aggregates cannot have fields with ctors, dtors or complex 362890287Sobrien assignment operators (because they cannot have these methods themselves). 362990287Sobrien For anonymous unions this is already checked because they are not allowed 363090287Sobrien in any union, otherwise we have to check it. */ 363190287Sobrien if (TREE_CODE (t) != UNION_TYPE) 363290287Sobrien { 363390287Sobrien tree field, type; 363490287Sobrien 363590287Sobrien for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 363690287Sobrien if (TREE_CODE (field) == FIELD_DECL) 363790287Sobrien { 363890287Sobrien type = TREE_TYPE (field); 363990287Sobrien if (CLASS_TYPE_P (type)) 364090287Sobrien { 3641169699Skan if (TYPE_NEEDS_CONSTRUCTING (type)) 3642169699Skan error ("member %q+#D with constructor not allowed " 3643169699Skan "in anonymous aggregate", field); 364490287Sobrien if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 3645169699Skan error ("member %q+#D with destructor not allowed " 3646169699Skan "in anonymous aggregate", field); 364790287Sobrien if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) 3648169699Skan error ("member %q+#D with copy assignment operator " 3649169699Skan "not allowed in anonymous aggregate", field); 365090287Sobrien } 365190287Sobrien } 365290287Sobrien } 365352290Sobrien} 365452290Sobrien 365552290Sobrien/* Make sure that a declaration with no declarator is well-formed, i.e. 3656132747Skan just declares a tagged type or anonymous union. 365752290Sobrien 3658132747Skan Returns the type declared; or NULL_TREE if none. */ 365952290Sobrien 366052290Sobrientree 3661169699Skancheck_tag_decl (cp_decl_specifier_seq *declspecs) 366218334Speter{ 3663169699Skan int saw_friend = declspecs->specs[(int)ds_friend] != 0; 3664169699Skan int saw_typedef = declspecs->specs[(int)ds_typedef] != 0; 3665132747Skan /* If a class, struct, or enum type is declared by the DECLSPECS 3666132747Skan (i.e, if a class-specifier, enum-specifier, or non-typename 3667132747Skan elaborated-type-specifier appears in the DECLSPECS), 3668132747Skan DECLARED_TYPE is set to the corresponding type. */ 3669132747Skan tree declared_type = NULL_TREE; 3670132747Skan bool error_p = false; 367118334Speter 3672169699Skan if (declspecs->multiple_types_p) 3673169699Skan error ("multiple types in one declaration"); 3674169699Skan else if (declspecs->redefined_builtin_type) 367518334Speter { 3676169699Skan if (!in_system_header) 3677169699Skan pedwarn ("redeclaration of C++ built-in type %qT", 3678169699Skan declspecs->redefined_builtin_type); 3679169699Skan return NULL_TREE; 368018334Speter } 368118334Speter 3682169699Skan if (declspecs->type 3683169699Skan && TYPE_P (declspecs->type) 3684169699Skan && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE 3685169699Skan && IS_AGGR_TYPE (declspecs->type)) 3686169699Skan || TREE_CODE (declspecs->type) == ENUMERAL_TYPE)) 3687169699Skan declared_type = declspecs->type; 3688169699Skan else if (declspecs->type == error_mark_node) 3689169699Skan error_p = true; 3690132747Skan if (declared_type == NULL_TREE && ! saw_friend && !error_p) 369152290Sobrien pedwarn ("declaration does not declare anything"); 369290287Sobrien /* Check for an anonymous union. */ 3693132747Skan else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type)) 3694132747Skan && TYPE_ANONYMOUS_P (declared_type)) 369552290Sobrien { 369690287Sobrien /* 7/3 In a simple-declaration, the optional init-declarator-list 3697169699Skan can be omitted only when declaring a class (clause 9) or 3698169699Skan enumeration (7.2), that is, when the decl-specifier-seq contains 3699169699Skan either a class-specifier, an elaborated-type-specifier with 3700169699Skan a class-key (9.1), or an enum-specifier. In these cases and 3701169699Skan whenever a class-specifier or enum-specifier is present in the 3702169699Skan decl-specifier-seq, the identifiers in these specifiers are among 3703169699Skan the names being declared by the declaration (as class-name, 3704169699Skan enum-names, or enumerators, depending on the syntax). In such 3705169699Skan cases, and except for the declaration of an unnamed bit-field (9.6), 3706169699Skan the decl-specifier-seq shall introduce one or more names into the 3707169699Skan program, or shall redeclare a name introduced by a previous 3708169699Skan declaration. [Example: 3709169699Skan enum { }; // ill-formed 3710169699Skan typedef class { }; // ill-formed 3711169699Skan --end example] */ 371290287Sobrien if (saw_typedef) 3713169699Skan { 3714169699Skan error ("missing type-name in typedef-declaration"); 3715169699Skan return NULL_TREE; 3716169699Skan } 371752290Sobrien /* Anonymous unions are objects, so they can have specifiers. */; 3718132747Skan SET_ANON_AGGR_TYPE_P (declared_type); 371990287Sobrien 3720169699Skan if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 3721132747Skan && !in_system_header) 372290287Sobrien pedwarn ("ISO C++ prohibits anonymous structs"); 372352290Sobrien } 372452290Sobrien 3725169699Skan else 372652290Sobrien { 3727169699Skan if (declspecs->specs[(int)ds_inline] 3728169699Skan || declspecs->specs[(int)ds_virtual]) 3729169699Skan error ("%qs can only be specified for functions", 3730169699Skan declspecs->specs[(int)ds_inline] 3731169699Skan ? "inline" : "virtual"); 3732169699Skan else if (saw_friend 3733169699Skan && (!current_class_type 3734169699Skan || current_scope () != current_class_type)) 3735169699Skan error ("%<friend%> can only be specified inside a class"); 3736169699Skan else if (declspecs->specs[(int)ds_explicit]) 3737169699Skan error ("%<explicit%> can only be specified for constructors"); 3738169699Skan else if (declspecs->storage_class) 3739169699Skan error ("a storage class can only be specified for objects " 3740169699Skan "and functions"); 3741169699Skan else if (declspecs->specs[(int)ds_const] 3742169699Skan || declspecs->specs[(int)ds_volatile] 3743169699Skan || declspecs->specs[(int)ds_restrict] 3744169699Skan || declspecs->specs[(int)ds_thread]) 3745169699Skan error ("qualifiers can only be specified for objects " 3746169699Skan "and functions"); 374752290Sobrien } 374852290Sobrien 3749132747Skan return declared_type; 375052290Sobrien} 375152290Sobrien 375252290Sobrien/* Called when a declaration is seen that contains no names to declare. 375352290Sobrien If its type is a reference to a structure, union or enum inherited 375452290Sobrien from a containing scope, shadow that tag name for the current scope 375552290Sobrien with a forward reference. 375652290Sobrien If its type defines a new named structure or union 375752290Sobrien or defines an enum, it is valid but we need not do anything here. 375852290Sobrien Otherwise, it is an error. 375952290Sobrien 376052290Sobrien C++: may have to grok the declspecs to learn about static, 3761169699Skan complain for anonymous unions. 376252290Sobrien 3763132747Skan Returns the TYPE declared -- or NULL_TREE if none. */ 3764132747Skan 3765132747Skantree 3766169699Skanshadow_tag (cp_decl_specifier_seq *declspecs) 376752290Sobrien{ 376852290Sobrien tree t = check_tag_decl (declspecs); 376952290Sobrien 3770132747Skan if (!t) 3771132747Skan return NULL_TREE; 377252290Sobrien 3773169699Skan if (declspecs->attributes) 3774169699Skan { 3775169699Skan warning (0, "attribute ignored in declaration of %q+#T", t); 3776169699Skan warning (0, "attribute for %q+#T must follow the %qs keyword", 3777169699Skan t, class_key_or_enum_as_string (t)); 3778132747Skan 3779169699Skan } 3780169699Skan 3781169699Skan if (maybe_process_partial_specialization (t) == error_mark_node) 3782169699Skan return NULL_TREE; 3783169699Skan 378418334Speter /* This is where the variables in an anonymous union are 378518334Speter declared. An anonymous union declaration looks like: 378618334Speter union { ... } ; 378718334Speter because there is no declarator after the union, the parser 378818334Speter sends that declaration here. */ 3789132747Skan if (ANON_AGGR_TYPE_P (t)) 379018334Speter { 379190287Sobrien fixup_anonymous_aggr (t); 379251412Sobrien 379318334Speter if (TYPE_FIELDS (t)) 379418334Speter { 3795169699Skan tree decl = grokdeclarator (/*declarator=*/NULL, 3796169699Skan declspecs, NORMAL, 0, NULL); 379718334Speter finish_anon_union (decl); 379818334Speter } 379918334Speter } 3800132747Skan 3801132747Skan return t; 380218334Speter} 380318334Speter 380418334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 380518334Speter 380618334Spetertree 3807169699Skangroktypename (cp_decl_specifier_seq *type_specifiers, 3808169699Skan const cp_declarator *declarator) 380918334Speter{ 3810169699Skan tree attrs; 381190287Sobrien tree type; 3812169699Skan attrs = type_specifiers->attributes; 3813169699Skan type_specifiers->attributes = NULL_TREE; 3814169699Skan type = grokdeclarator (declarator, type_specifiers, TYPENAME, 0, &attrs); 381590287Sobrien if (attrs) 381690287Sobrien cplus_decl_attributes (&type, attrs, 0); 381790287Sobrien return type; 381818334Speter} 381918334Speter 382018334Speter/* Decode a declarator in an ordinary declaration or data definition. 382118334Speter This is called as soon as the type information and variable name 382218334Speter have been parsed, before parsing the initializer if any. 382318334Speter Here we create the ..._DECL node, fill in its type, 382418334Speter and put it on the list of decls for the current context. 382518334Speter The ..._DECL node is returned as the value. 382618334Speter 382718334Speter Exception: for arrays where the length is not specified, 382818334Speter the type is left null, to be filled in by `cp_finish_decl'. 382918334Speter 383018334Speter Function definitions do not come here; they go to start_function 383118334Speter instead. However, external and forward declarations of functions 383218334Speter do go through here. Structure field declarations are done by 383318334Speter grokfield and not through here. */ 383418334Speter 383518334Spetertree 3836169699Skanstart_decl (const cp_declarator *declarator, 3837169699Skan cp_decl_specifier_seq *declspecs, 3838169699Skan int initialized, 3839169699Skan tree attributes, 3840169699Skan tree prefix_attributes, 3841169699Skan tree *pushed_scope_p) 384218334Speter{ 384390287Sobrien tree decl; 3844132747Skan tree type, tem; 384518334Speter tree context; 3846260919Spfg /* APPLE LOCAL "unavailable" attribute (radar 2809697) */ 3847260919Spfg tree a; 3848169699Skan bool was_public; 384918334Speter 3850169699Skan *pushed_scope_p = NULL_TREE; 385118334Speter 3852260919Spfg /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */ 3853260919Spfg /* An object declared as __attribute__((unavailable)) suppresses 3854260919Spfg any reports of being declared with unavailable or deprecated 3855260919Spfg items. An object declared as __attribute__((deprecated)) 3856260919Spfg suppresses warnings of uses of other deprecated items. */ 3857260919Spfg#ifdef A_LESS_INEFFICENT_WAY /* which I really don't want to do! */ 385890287Sobrien if (lookup_attribute ("deprecated", attributes)) 385990287Sobrien deprecated_state = DEPRECATED_SUPPRESS; 3860260919Spfg else if (lookup_attribute ("unavailable", attributes)) 3861260919Spfg deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS; 3862260919Spfg#else /* a more efficient way doing what lookup_attribute would do */ 3863260919Spfg for (a = attributes; a; a = TREE_CHAIN (a)) 3864260919Spfg { 3865260919Spfg tree name = TREE_PURPOSE (a); 3866260919Spfg if (TREE_CODE (name) == IDENTIFIER_NODE) 3867260919Spfg if (is_attribute_p ("deprecated", name)) 3868260919Spfg { 3869260919Spfg deprecated_state = DEPRECATED_SUPPRESS; 3870260919Spfg break; 3871260919Spfg } 3872260919Spfg if (is_attribute_p ("unavailable", name)) 3873260919Spfg { 3874260919Spfg deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS; 3875260919Spfg break; 3876260919Spfg } 3877260919Spfg } 3878260919Spfg#endif 3879260919Spfg /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */ 388052290Sobrien 388190287Sobrien attributes = chainon (attributes, prefix_attributes); 388290287Sobrien 388351412Sobrien decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, 388490287Sobrien &attributes); 388590287Sobrien 388690287Sobrien deprecated_state = DEPRECATED_NORMAL; 388790287Sobrien 3888169699Skan if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE 3889169699Skan || decl == error_mark_node) 3890146906Skan return error_mark_node; 389118334Speter 389218334Speter type = TREE_TYPE (decl); 389318334Speter 389490287Sobrien context = DECL_CONTEXT (decl); 389518334Speter 3896169699Skan if (context) 389718334Speter { 3898169699Skan *pushed_scope_p = push_scope (context); 3899169699Skan 3900169699Skan /* We are only interested in class contexts, later. */ 3901169699Skan if (TREE_CODE (context) == NAMESPACE_DECL) 3902169699Skan context = NULL_TREE; 390318334Speter } 390418334Speter 390518334Speter if (initialized) 390618334Speter /* Is it valid for this decl to have an initializer at all? 390718334Speter If not, set INITIALIZED to zero, which will indirectly 390818334Speter tell `cp_finish_decl' to ignore the initializer once it is parsed. */ 390918334Speter switch (TREE_CODE (decl)) 391018334Speter { 391118334Speter case TYPE_DECL: 3912169699Skan error ("typedef %qD is initialized (use __typeof__ instead)", decl); 3913169699Skan return error_mark_node; 391418334Speter 391518334Speter case FUNCTION_DECL: 3916169699Skan error ("function %q#D is initialized like a variable", decl); 3917169699Skan return error_mark_node; 391818334Speter 391918334Speter default: 392090287Sobrien break; 392118334Speter } 392218334Speter 392318334Speter if (initialized) 392418334Speter { 392518334Speter if (! toplevel_bindings_p () 392618334Speter && DECL_EXTERNAL (decl)) 3927169699Skan warning (0, "declaration of %q#D has %<extern%> and is initialized", 3928169699Skan decl); 392918334Speter DECL_EXTERNAL (decl) = 0; 393051412Sobrien if (toplevel_bindings_p ()) 393118334Speter TREE_STATIC (decl) = 1; 393218334Speter } 393318334Speter 393452290Sobrien /* Set attributes here so if duplicate decl, will have proper attributes. */ 393590287Sobrien cplus_decl_attributes (&decl, attributes, 0); 393652290Sobrien 3937169699Skan /* Dllimported symbols cannot be defined. Static data members (which 3938169699Skan can be initialized in-class and dllimported) go through grokfield, 3939169699Skan not here, so we don't need to exclude those decls when checking for 3940169699Skan a definition. */ 3941169699Skan if (initialized && DECL_DLLIMPORT_P (decl)) 3942169699Skan { 3943169699Skan error ("definition of %q#D is marked %<dllimport%>", decl); 3944169699Skan DECL_DLLIMPORT_P (decl) = 0; 3945169699Skan } 3946169699Skan 394796292Sobrien /* If #pragma weak was used, mark the decl weak now. */ 3948169699Skan maybe_apply_pragma_weak (decl); 394996292Sobrien 395090287Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 395190287Sobrien && DECL_DECLARED_INLINE_P (decl) 395290287Sobrien && DECL_UNINLINABLE (decl) 395390287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 3954169699Skan warning (0, "inline function %q+D given attribute noinline", decl); 395590287Sobrien 395690287Sobrien if (context && COMPLETE_TYPE_P (complete_type (context))) 395718334Speter { 395818334Speter if (TREE_CODE (decl) == VAR_DECL) 395918334Speter { 3960132747Skan tree field = lookup_field (context, DECL_NAME (decl), 0, false); 396118334Speter if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL) 3962169699Skan error ("%q#D is not a static member of %q#T", decl, context); 396351412Sobrien else 396451412Sobrien { 396551412Sobrien if (DECL_CONTEXT (field) != context) 396651412Sobrien { 3967132747Skan if (!same_type_p (DECL_CONTEXT (field), context)) 3968169699Skan pedwarn ("ISO C++ does not permit %<%T::%D%> " 3969169699Skan "to be defined as %<%T::%D%>", 3970132747Skan DECL_CONTEXT (field), DECL_NAME (decl), 3971132747Skan context, DECL_NAME (decl)); 397251412Sobrien DECL_CONTEXT (decl) = DECL_CONTEXT (field); 397351412Sobrien } 3974169699Skan if (processing_specialization 3975169699Skan && template_class_depth (context) == 0 3976169699Skan && CLASSTYPE_TEMPLATE_SPECIALIZATION (context)) 3977169699Skan error ("template header not allowed in member definition " 3978169699Skan "of explicitly specialized class"); 397951412Sobrien /* Static data member are tricky; an in-class initialization 398051412Sobrien still doesn't provide a definition, so the in-class 398151412Sobrien declaration will have DECL_EXTERNAL set, but will have an 398251412Sobrien initialization. Thus, duplicate_decls won't warn 398351412Sobrien about this situation, and so we check here. */ 3984169699Skan if (initialized && DECL_INITIALIZED_IN_CLASS_P (field)) 3985169699Skan error ("duplicate initialization of %qD", decl); 3986169699Skan if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false)) 398751412Sobrien decl = field; 398851412Sobrien } 398918334Speter } 399018334Speter else 399118334Speter { 3992132747Skan tree field = check_classfn (context, decl, 3993169699Skan (processing_template_decl 3994169699Skan > template_class_depth (context)) 3995169699Skan ? current_template_parms 3996169699Skan : NULL_TREE); 3997169699Skan if (field && duplicate_decls (decl, field, 3998169699Skan /*newdecl_is_friend=*/false)) 399918334Speter decl = field; 400018334Speter } 400118334Speter 400218334Speter /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ 400351412Sobrien DECL_IN_AGGR_P (decl) = 0; 4004169699Skan /* Do not mark DECL as an explicit specialization if it was not 4005169699Skan already marked as an instantiation; a declaration should 4006169699Skan never be marked as a specialization unless we know what 4007169699Skan template is being specialized. */ 4008169699Skan if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 400952290Sobrien { 401052290Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (decl); 4011169699Skan 401252290Sobrien /* [temp.expl.spec] An explicit specialization of a static data 401352290Sobrien member of a template is a definition if the declaration 401452290Sobrien includes an initializer; otherwise, it is a declaration. 401518334Speter 401652290Sobrien We check for processing_specialization so this only applies 401752290Sobrien to the new specialization syntax. */ 4018169699Skan if (!initialized && processing_specialization) 401952290Sobrien DECL_EXTERNAL (decl) = 1; 402052290Sobrien } 402152290Sobrien 402251412Sobrien if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)) 4023169699Skan pedwarn ("declaration of %q#D outside of class is not definition", 4024169699Skan decl); 402518334Speter } 402618334Speter 4027169699Skan was_public = TREE_PUBLIC (decl); 4028169699Skan 402990287Sobrien /* Enter this declaration into the symbol table. */ 403090287Sobrien tem = maybe_push_decl (decl); 403118334Speter 403251412Sobrien if (processing_template_decl) 403390287Sobrien tem = push_template_decl (tem); 4034132747Skan if (tem == error_mark_node) 4035132747Skan return error_mark_node; 403651412Sobrien 403751412Sobrien /* Tell the back-end to use or not use .common as appropriate. If we say 403851412Sobrien -fconserve-space, we want this to save .data space, at the expense of 403951412Sobrien wrong semantics. If we say -fno-conserve-space, we want this to 404051412Sobrien produce errors about redefs; to do this we force variables into the 404151412Sobrien data segment. */ 4042169699Skan if (flag_conserve_space 4043169699Skan && TREE_CODE (tem) == VAR_DECL 4044169699Skan && TREE_PUBLIC (tem) 4045169699Skan && !DECL_THREAD_LOCAL_P (tem) 4046169699Skan && !have_global_bss_p ()) 4047169699Skan DECL_COMMON (tem) = 1; 404890287Sobrien 4049169699Skan if (TREE_CODE (tem) == VAR_DECL 4050169699Skan && DECL_NAMESPACE_SCOPE_P (tem) && !TREE_PUBLIC (tem) && !was_public 4051169699Skan && !DECL_THIS_STATIC (tem) && !DECL_ARTIFICIAL (tem)) 4052169699Skan { 4053169699Skan /* This is a const variable with implicit 'static'. Set 4054169699Skan DECL_THIS_STATIC so we can tell it from variables that are 4055169699Skan !TREE_PUBLIC because of the anonymous namespace. */ 4056169699Skan gcc_assert (cp_type_readonly (TREE_TYPE (tem))); 4057169699Skan DECL_THIS_STATIC (tem) = 1; 4058169699Skan } 405918334Speter 4060169699Skan if (!processing_template_decl && TREE_CODE (tem) == VAR_DECL) 4061169699Skan start_decl_1 (tem, initialized); 4062169699Skan 406318334Speter return tem; 406418334Speter} 406518334Speter 406651412Sobrienvoid 4067169699Skanstart_decl_1 (tree decl, bool initialized) 406818334Speter{ 4069169699Skan tree type; 407018334Speter 4071169699Skan gcc_assert (!processing_template_decl); 4072169699Skan 4073169699Skan if (error_operand_p (decl)) 407452290Sobrien return; 407552290Sobrien 4076169699Skan gcc_assert (TREE_CODE (decl) == VAR_DECL); 4077169699Skan type = TREE_TYPE (decl); 4078169699Skan 407951412Sobrien if (initialized) 408051412Sobrien /* Is it valid for this decl to have an initializer at all? 408151412Sobrien If not, set INITIALIZED to zero, which will indirectly 408251412Sobrien tell `cp_finish_decl' to ignore the initializer once it is parsed. */ 408318334Speter { 408451412Sobrien /* Don't allow initializations for incomplete types except for 408551412Sobrien arrays which might be completed by the initialization. */ 408690287Sobrien if (COMPLETE_TYPE_P (complete_type (type))) 408751412Sobrien ; /* A complete type is ok. */ 408851412Sobrien else if (TREE_CODE (type) != ARRAY_TYPE) 408951412Sobrien { 4090169699Skan error ("variable %q#D has initializer but incomplete type", decl); 409151412Sobrien initialized = 0; 409251412Sobrien type = TREE_TYPE (decl) = error_mark_node; 409351412Sobrien } 409490287Sobrien else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) 409551412Sobrien { 409651412Sobrien if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) 4097169699Skan error ("elements of array %q#D have incomplete type", decl); 409851412Sobrien /* else we already gave an error in start_decl. */ 409951412Sobrien initialized = 0; 410051412Sobrien } 410118334Speter } 4102169699Skan else if (IS_AGGR_TYPE (type) 4103169699Skan && ! DECL_EXTERNAL (decl)) 410418334Speter { 4105169699Skan if (!COMPLETE_TYPE_P (complete_type (type))) 410618334Speter { 4107169699Skan error ("aggregate %q#D has incomplete type and cannot be defined", 410851412Sobrien decl); 410951412Sobrien /* Change the type so that assemble_variable will give 411051412Sobrien DECL an rtl we can live with: (mem (const_int 0)). */ 411151412Sobrien type = TREE_TYPE (decl) = error_mark_node; 411218334Speter } 411318334Speter else 411418334Speter { 411551412Sobrien /* If any base type in the hierarchy of TYPE needs a constructor, 411651412Sobrien then we set initialized to 1. This way any nodes which are 411751412Sobrien created for the purposes of initializing this aggregate 411851412Sobrien will live as long as it does. This is necessary for global 411951412Sobrien aggregates which do not have their initializers processed until 412051412Sobrien the end of the file. */ 412151412Sobrien initialized = TYPE_NEEDS_CONSTRUCTING (type); 412218334Speter } 412318334Speter } 412451412Sobrien 4125132747Skan /* Create a new scope to hold this declaration if necessary. 4126132747Skan Whether or not a new scope is necessary cannot be determined 4127132747Skan until after the type has been completed; if the type is a 4128132747Skan specialization of a class template it is not until after 4129132747Skan instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR 4130132747Skan will be set correctly. */ 4131132747Skan maybe_push_cleanup_level (type); 413251412Sobrien} 413351412Sobrien 4134122192Skan/* Handle initialization of references. DECL, TYPE, and INIT have the 4135122192Skan same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry, 4136122192Skan but will be set to a new CLEANUP_STMT if a temporary is created 4137132747Skan that must be destroyed subsequently. 413851412Sobrien 4139122192Skan Returns an initializer expression to use to initialize DECL, or 4140122192Skan NULL if the initialization can be performed statically. 4141122192Skan 414251412Sobrien Quotes on semantics can be found in ARM 8.4.3. */ 4143132747Skan 414490287Sobrienstatic tree 4145122192Skangrok_reference_init (tree decl, tree type, tree init, tree *cleanup) 414618334Speter{ 414718334Speter tree tmp; 414818334Speter 414918334Speter if (init == NULL_TREE) 415018334Speter { 415118334Speter if ((DECL_LANG_SPECIFIC (decl) == 0 415218334Speter || DECL_IN_AGGR_P (decl) == 0) 415318334Speter && ! DECL_THIS_EXTERN (decl)) 4154169699Skan error ("%qD declared as reference but not initialized", decl); 415590287Sobrien return NULL_TREE; 415618334Speter } 415718334Speter 415890287Sobrien if (TREE_CODE (init) == CONSTRUCTOR) 415918334Speter { 4160169699Skan error ("ISO C++ forbids use of initializer list to " 4161169699Skan "initialize reference %qD", decl); 416290287Sobrien return NULL_TREE; 416318334Speter } 416418334Speter 416518334Speter if (TREE_CODE (init) == TREE_LIST) 4166132747Skan init = build_x_compound_expr_from_list (init, "initializer"); 416718334Speter 416818334Speter if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE 416918334Speter && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) 4170132747Skan /* Note: default conversion is only called in very special cases. */ 4171132747Skan init = decay_conversion (init); 417218334Speter 417390287Sobrien /* Convert INIT to the reference type TYPE. This may involve the 417490287Sobrien creation of a temporary, whose lifetime must be the same as that 4175169699Skan of the reference. If so, a DECL_EXPR for the temporary will be 4176169699Skan added just after the DECL_EXPR for DECL. That's why we don't set 417790287Sobrien DECL_INITIAL for local references (instead assigning to them 417890287Sobrien explicitly); we need to allow the temporary to be initialized 417990287Sobrien first. */ 4180122192Skan tmp = initialize_reference (type, init, decl, cleanup); 418118334Speter 418218334Speter if (tmp == error_mark_node) 418390287Sobrien return NULL_TREE; 418490287Sobrien else if (tmp == NULL_TREE) 418518334Speter { 4186169699Skan error ("cannot initialize %qT from %qT", type, TREE_TYPE (init)); 418790287Sobrien return NULL_TREE; 418818334Speter } 418918334Speter 419090287Sobrien if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp)) 419190287Sobrien return tmp; 419218334Speter 419390287Sobrien DECL_INITIAL (decl) = tmp; 419418334Speter 419590287Sobrien return NULL_TREE; 419618334Speter} 419718334Speter 4198169699Skan/* Designated initializers in arrays are not supported in GNU C++. 4199169699Skan The parser cannot detect this error since it does not know whether 4200169699Skan a given brace-enclosed initializer is for a class type or for an 4201169699Skan array. This function checks that CE does not use a designated 4202169699Skan initializer. If it does, an error is issued. Returns true if CE 4203169699Skan is valid, i.e., does not have a designated initializer. */ 4204169699Skan 4205169699Skanstatic bool 4206169699Skancheck_array_designated_initializer (const constructor_elt *ce) 4207169699Skan{ 4208169699Skan /* Designated initializers for array elements arenot supported. */ 4209169699Skan if (ce->index) 4210169699Skan { 4211169699Skan /* The parser only allows identifiers as designated 4212169699Skan intializers. */ 4213169699Skan gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE); 4214169699Skan error ("name %qD used in a GNU-style designated " 4215169699Skan "initializer for an array", ce->index); 4216169699Skan return false; 4217169699Skan } 4218169699Skan 4219169699Skan return true; 4220169699Skan} 4221169699Skan 422290287Sobrien/* When parsing `int a[] = {1, 2};' we don't know the size of the 422390287Sobrien array until we finish parsing the initializer. If that's the 422490287Sobrien situation we're in, update DECL accordingly. */ 422552290Sobrien 422652290Sobrienstatic void 4227132747Skanmaybe_deduce_size_from_array_init (tree decl, tree init) 422852290Sobrien{ 422952290Sobrien tree type = TREE_TYPE (decl); 423052290Sobrien 423190287Sobrien if (TREE_CODE (type) == ARRAY_TYPE 423290287Sobrien && TYPE_DOMAIN (type) == NULL_TREE 423390287Sobrien && TREE_CODE (decl) != TYPE_DECL) 423490287Sobrien { 423590287Sobrien /* do_default is really a C-ism to deal with tentative definitions. 423690287Sobrien But let's leave it here to ease the eventual merge. */ 423790287Sobrien int do_default = !DECL_EXTERNAL (decl); 423890287Sobrien tree initializer = init ? init : DECL_INITIAL (decl); 4239169699Skan int failure = 0; 424052290Sobrien 4241169699Skan /* Check that there are no designated initializers in INIT, as 4242169699Skan those are not supported in GNU C++, and as the middle-end 4243169699Skan will crash if presented with a non-numeric designated 4244169699Skan initializer. */ 4245169699Skan if (initializer && TREE_CODE (initializer) == CONSTRUCTOR) 4246169699Skan { 4247169699Skan VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initializer); 4248169699Skan constructor_elt *ce; 4249169699Skan HOST_WIDE_INT i; 4250169699Skan for (i = 0; 4251169699Skan VEC_iterate (constructor_elt, v, i, ce); 4252169699Skan ++i) 4253169699Skan if (!check_array_designated_initializer (ce)) 4254169699Skan failure = 1; 4255169699Skan } 425618334Speter 4257169699Skan if (!failure) 425890287Sobrien { 4259169699Skan failure = cp_complete_array_type (&TREE_TYPE (decl), initializer, 4260169699Skan do_default); 4261169699Skan if (failure == 1) 4262169699Skan { 4263169699Skan error ("initializer fails to determine size of %qD", decl); 4264169699Skan TREE_TYPE (decl) = error_mark_node; 4265169699Skan } 4266169699Skan else if (failure == 2) 4267169699Skan { 4268169699Skan if (do_default) 4269169699Skan { 4270169699Skan error ("array size missing in %qD", decl); 4271169699Skan TREE_TYPE (decl) = error_mark_node; 4272169699Skan } 4273169699Skan /* If a `static' var's size isn't known, make it extern as 4274169699Skan well as static, so it does not get allocated. If it's not 4275169699Skan `static', then don't mark it extern; finish_incomplete_decl 4276169699Skan will give it a default size and it will get allocated. */ 4277169699Skan else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 4278169699Skan DECL_EXTERNAL (decl) = 1; 4279169699Skan } 4280169699Skan else if (failure == 3) 4281169699Skan { 4282169699Skan error ("zero-size array %qD", decl); 4283169699Skan TREE_TYPE (decl) = error_mark_node; 4284169699Skan } 428590287Sobrien } 428618334Speter 4287169699Skan cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl); 428818334Speter 428990287Sobrien layout_decl (decl, 0); 429090287Sobrien } 429190287Sobrien} 429218334Speter 429390287Sobrien/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue 429490287Sobrien any appropriate error messages regarding the layout. */ 429518334Speter 429690287Sobrienstatic void 4297132747Skanlayout_var_decl (tree decl) 429818334Speter{ 4299169699Skan tree type; 430018334Speter 4301169699Skan type = TREE_TYPE (decl); 4302169699Skan if (type == error_mark_node) 4303169699Skan return; 4304169699Skan 430590287Sobrien /* If we haven't already layed out this declaration, do so now. 430690287Sobrien Note that we must not call complete type for an external object 430790287Sobrien because it's type might involve templates that we are not 4308132747Skan supposed to instantiate yet. (And it's perfectly valid to say 430990287Sobrien `extern X x' for some incomplete type `X'.) */ 431090287Sobrien if (!DECL_EXTERNAL (decl)) 431190287Sobrien complete_type (type); 4312169699Skan if (!DECL_SIZE (decl) 4313117410Skan && TREE_TYPE (decl) != error_mark_node 4314117410Skan && (COMPLETE_TYPE_P (type) 4315169699Skan || (TREE_CODE (type) == ARRAY_TYPE 4316117410Skan && !TYPE_DOMAIN (type) 4317117410Skan && COMPLETE_TYPE_P (TREE_TYPE (type))))) 431890287Sobrien layout_decl (decl, 0); 431990287Sobrien 432090287Sobrien if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) 432118334Speter { 432290287Sobrien /* An automatic variable with an incomplete type: that is an error. 432390287Sobrien Don't talk about array types here, since we took care of that 432490287Sobrien message in grokdeclarator. */ 4325169699Skan error ("storage size of %qD isn't known", decl); 432690287Sobrien TREE_TYPE (decl) = error_mark_node; 432718334Speter } 432890287Sobrien#if 0 432990287Sobrien /* Keep this code around in case we later want to control debug info 433090287Sobrien based on whether a type is "used". (jason 1999-11-11) */ 433118334Speter 433290287Sobrien else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype)) 433390287Sobrien /* Let debugger know it should output info for this type. */ 433490287Sobrien note_debug_info_needed (ttype); 433518334Speter 433690287Sobrien if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) 433790287Sobrien note_debug_info_needed (DECL_CONTEXT (decl)); 433890287Sobrien#endif 433951412Sobrien 434090287Sobrien if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 434190287Sobrien && DECL_SIZE (decl) != NULL_TREE 434290287Sobrien && ! TREE_CONSTANT (DECL_SIZE (decl))) 434351412Sobrien { 434490287Sobrien if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 434590287Sobrien constant_expression_warning (DECL_SIZE (decl)); 434690287Sobrien else 4347169699Skan error ("storage size of %qD isn't constant", decl); 434851412Sobrien } 434990287Sobrien} 435018334Speter 435190287Sobrien/* If a local static variable is declared in an inline function, or if 435290287Sobrien we have a weak definition, we must endeavor to create only one 435390287Sobrien instance of the variable at link-time. */ 435418334Speter 435590287Sobrienstatic void 4356132747Skanmaybe_commonize_var (tree decl) 435790287Sobrien{ 435890287Sobrien /* Static data in a function with comdat linkage also has comdat 435990287Sobrien linkage. */ 436090287Sobrien if (TREE_STATIC (decl) 436190287Sobrien /* Don't mess with __FUNCTION__. */ 436290287Sobrien && ! DECL_ARTIFICIAL (decl) 4363117410Skan && DECL_FUNCTION_SCOPE_P (decl) 4364117410Skan /* Unfortunately, import_export_decl has not always been called 4365117410Skan before the function is processed, so we cannot simply check 4366169699Skan DECL_COMDAT. */ 4367117410Skan && (DECL_COMDAT (DECL_CONTEXT (decl)) 4368117410Skan || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl)) 4369117410Skan || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl))) 4370117410Skan && TREE_PUBLIC (DECL_CONTEXT (decl))))) 437118334Speter { 4372117410Skan if (flag_weak) 437351412Sobrien { 4374117410Skan /* With weak symbols, we simply make the variable COMDAT; 4375117410Skan that will cause copies in multiple translations units to 4376117410Skan be merged. */ 4377117410Skan comdat_linkage (decl); 4378117410Skan } 4379117410Skan else 4380117410Skan { 4381117410Skan if (DECL_INITIAL (decl) == NULL_TREE 4382117410Skan || DECL_INITIAL (decl) == error_mark_node) 438351412Sobrien { 4384117410Skan /* Without weak symbols, we can use COMMON to merge 4385117410Skan uninitialized variables. */ 438690287Sobrien TREE_PUBLIC (decl) = 1; 438790287Sobrien DECL_COMMON (decl) = 1; 438890287Sobrien } 4389117410Skan else 439090287Sobrien { 4391117410Skan /* While for initialized variables, we must use internal 4392117410Skan linkage -- which means that multiple copies will not 4393117410Skan be merged. */ 4394117410Skan TREE_PUBLIC (decl) = 0; 4395117410Skan DECL_COMMON (decl) = 0; 4396169699Skan warning (0, "sorry: semantics of inline function static " 4397169699Skan "data %q+#D are wrong (you'll wind up " 4398169699Skan "with multiple copies)", decl); 4399169699Skan warning (0, "%J you can work around this by removing " 4400169699Skan "the initializer", 4401132747Skan decl); 440290287Sobrien } 440351412Sobrien } 440451412Sobrien } 440590287Sobrien else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl)) 440690287Sobrien /* Set it up again; we might have set DECL_INITIAL since the last 440790287Sobrien time. */ 440890287Sobrien comdat_linkage (decl); 440990287Sobrien} 441051412Sobrien 441190287Sobrien/* Issue an error message if DECL is an uninitialized const variable. */ 441251412Sobrien 441390287Sobrienstatic void 4414132747Skancheck_for_uninitialized_const_var (tree decl) 441590287Sobrien{ 441690287Sobrien tree type = TREE_TYPE (decl); 441718334Speter 441890287Sobrien /* ``Unless explicitly declared extern, a const object does not have 441990287Sobrien external linkage and must be initialized. ($8.4; $12.1)'' ARM 442090287Sobrien 7.1.6 */ 442190287Sobrien if (TREE_CODE (decl) == VAR_DECL 442290287Sobrien && TREE_CODE (type) != REFERENCE_TYPE 442390287Sobrien && CP_TYPE_CONST_P (type) 442490287Sobrien && !TYPE_NEEDS_CONSTRUCTING (type) 442590287Sobrien && !DECL_INITIAL (decl)) 4426169699Skan error ("uninitialized const %qD", decl); 442790287Sobrien} 442818334Speter 4429169699Skan 4430169699Skan/* Structure holding the current initializer being processed by reshape_init. 4431169699Skan CUR is a pointer to the current element being processed, END is a pointer 4432169699Skan after the last element present in the initializer. */ 4433169699Skantypedef struct reshape_iterator_t 4434169699Skan{ 4435169699Skan constructor_elt *cur; 4436169699Skan constructor_elt *end; 4437169699Skan} reshape_iter; 4438169699Skan 4439169699Skanstatic tree reshape_init_r (tree, reshape_iter *, bool); 4440169699Skan 4441117410Skan/* FIELD is a FIELD_DECL or NULL. In the former case, the value 4442117410Skan returned is the next FIELD_DECL (possibly FIELD itself) that can be 4443117410Skan initialized. If there are no more such fields, the return value 4444117410Skan will be NULL. */ 444518334Speter 444690287Sobrienstatic tree 4447117410Skannext_initializable_field (tree field) 444890287Sobrien{ 4449117410Skan while (field 4450117410Skan && (TREE_CODE (field) != FIELD_DECL 4451117410Skan || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field)) 4452117410Skan || DECL_ARTIFICIAL (field))) 4453117410Skan field = TREE_CHAIN (field); 445490287Sobrien 4455117410Skan return field; 4456117410Skan} 445790287Sobrien 4458169699Skan/* Subroutine of reshape_init_array and reshape_init_vector, which does 4459169699Skan the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an 4460169699Skan INTEGER_CST representing the size of the array minus one (the maximum index), 4461169699Skan or NULL_TREE if the array was declared without specifying the size. D is 4462169699Skan the iterator within the constructor. */ 4463146906Skan 4464169699Skanstatic tree 4465169699Skanreshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d) 4466146906Skan{ 4467169699Skan tree new_init; 4468146906Skan bool sized_array_p = (max_index != NULL_TREE); 4469146906Skan unsigned HOST_WIDE_INT max_index_cst = 0; 4470146906Skan unsigned HOST_WIDE_INT index; 4471146906Skan 4472169699Skan /* The initializer for an array is always a CONSTRUCTOR. */ 4473169699Skan new_init = build_constructor (NULL_TREE, NULL); 4474169699Skan 4475146906Skan if (sized_array_p) 4476146906Skan { 4477169699Skan /* Minus 1 is used for zero sized arrays. */ 4478169699Skan if (integer_all_onesp (max_index)) 4479169699Skan return new_init; 4480169699Skan 4481146906Skan if (host_integerp (max_index, 1)) 4482146906Skan max_index_cst = tree_low_cst (max_index, 1); 4483146906Skan /* sizetype is sign extended, not zero extended. */ 4484146906Skan else 4485169699Skan max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index), 4486169699Skan 1); 4487146906Skan } 4488146906Skan 4489146906Skan /* Loop until there are no more initializers. */ 4490146906Skan for (index = 0; 4491169699Skan d->cur != d->end && (!sized_array_p || index <= max_index_cst); 4492146906Skan ++index) 4493146906Skan { 4494169699Skan tree elt_init; 4495146906Skan 4496169699Skan check_array_designated_initializer (d->cur); 4497169699Skan elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false); 4498169699Skan if (elt_init == error_mark_node) 4499169699Skan return error_mark_node; 4500169699Skan CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), NULL_TREE, elt_init); 4501146906Skan } 4502146906Skan 4503169699Skan return new_init; 4504146906Skan} 4505146906Skan 4506169699Skan/* Subroutine of reshape_init_r, processes the initializers for arrays. 4507169699Skan Parameters are the same of reshape_init_r. */ 450890287Sobrien 4509169699Skanstatic tree 4510169699Skanreshape_init_array (tree type, reshape_iter *d) 4511169699Skan{ 4512169699Skan tree max_index = NULL_TREE; 451390287Sobrien 4514169699Skan gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 4515117410Skan 4516169699Skan if (TYPE_DOMAIN (type)) 4517169699Skan max_index = array_type_nelts (type); 4518169699Skan 4519169699Skan return reshape_init_array_1 (TREE_TYPE (type), max_index, d); 4520169699Skan} 4521169699Skan 4522169699Skan/* Subroutine of reshape_init_r, processes the initializers for vectors. 4523169699Skan Parameters are the same of reshape_init_r. */ 4524169699Skan 4525117410Skanstatic tree 4526169699Skanreshape_init_vector (tree type, reshape_iter *d) 4527117410Skan{ 4528169699Skan tree max_index = NULL_TREE; 4529169699Skan tree rtype; 4530169699Skan 4531169699Skan gcc_assert (TREE_CODE (type) == VECTOR_TYPE); 4532169699Skan 4533169699Skan if (COMPOUND_LITERAL_P (d->cur->value)) 4534169699Skan { 4535169699Skan tree value = d->cur->value; 4536169699Skan if (!same_type_p (TREE_TYPE (value), type)) 4537169699Skan { 4538169699Skan error ("invalid type %qT as initializer for a vector of type %qT", 4539169699Skan TREE_TYPE (d->cur->value), type); 4540169699Skan value = error_mark_node; 4541169699Skan } 4542169699Skan ++d->cur; 4543169699Skan return value; 4544169699Skan } 4545169699Skan 4546169699Skan /* For a vector, the representation type is a struct 4547169699Skan containing a single member which is an array of the 4548169699Skan appropriate size. */ 4549169699Skan rtype = TYPE_DEBUG_REPRESENTATION_TYPE (type); 4550169699Skan if (rtype && TYPE_DOMAIN (TREE_TYPE (TYPE_FIELDS (rtype)))) 4551169699Skan max_index = array_type_nelts (TREE_TYPE (TYPE_FIELDS (rtype))); 4552169699Skan 4553169699Skan return reshape_init_array_1 (TREE_TYPE (type), max_index, d); 4554169699Skan} 4555169699Skan 4556169699Skan/* Subroutine of reshape_init_r, processes the initializers for classes 4557169699Skan or union. Parameters are the same of reshape_init_r. */ 4558169699Skan 4559169699Skanstatic tree 4560169699Skanreshape_init_class (tree type, reshape_iter *d, bool first_initializer_p) 4561169699Skan{ 4562169699Skan tree field; 4563117410Skan tree new_init; 4564117410Skan 4565169699Skan gcc_assert (CLASS_TYPE_P (type)); 4566117410Skan 4567169699Skan /* The initializer for a class is always a CONSTRUCTOR. */ 4568169699Skan new_init = build_constructor (NULL_TREE, NULL); 4569169699Skan field = next_initializable_field (TYPE_FIELDS (type)); 457018334Speter 4571169699Skan if (!field) 4572117410Skan { 4573169699Skan /* [dcl.init.aggr] 4574169699Skan 4575169699Skan An initializer for an aggregate member that is an 4576169699Skan empty class shall have the form of an empty 4577169699Skan initializer-list {}. */ 4578169699Skan if (!first_initializer_p) 4579169699Skan { 4580169699Skan error ("initializer for %qT must be brace-enclosed", type); 4581169699Skan return error_mark_node; 4582169699Skan } 4583169699Skan return new_init; 4584117410Skan } 4585169699Skan 4586169699Skan /* Loop through the initializable fields, gathering initializers. */ 4587169699Skan while (d->cur != d->end) 4588117410Skan { 4589169699Skan tree field_init; 4590169699Skan 4591169699Skan /* Handle designated initializers, as an extension. */ 4592169699Skan if (d->cur->index) 4593169699Skan { 4594169699Skan field = lookup_field_1 (type, d->cur->index, /*want_type=*/false); 4595169699Skan 4596169699Skan if (!field || TREE_CODE (field) != FIELD_DECL) 4597169699Skan { 4598169699Skan error ("%qT has no non-static data member named %qD", type, 4599169699Skan d->cur->index); 4600169699Skan return error_mark_node; 4601169699Skan } 4602169699Skan } 4603169699Skan 4604169699Skan /* If we processed all the member of the class, we are done. */ 4605169699Skan if (!field) 4606169699Skan break; 4607169699Skan 4608169699Skan field_init = reshape_init_r (TREE_TYPE (field), d, 4609169699Skan /*first_initializer_p=*/false); 4610169699Skan CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init); 4611169699Skan 4612169699Skan /* [dcl.init.aggr] 4613169699Skan 4614169699Skan When a union is initialized with a brace-enclosed 4615169699Skan initializer, the braces shall only contain an 4616169699Skan initializer for the first member of the union. */ 4617169699Skan if (TREE_CODE (type) == UNION_TYPE) 4618169699Skan break; 4619169699Skan 4620169699Skan field = next_initializable_field (TREE_CHAIN (field)); 4621117410Skan } 4622117410Skan 4623169699Skan return new_init; 4624169699Skan} 4625169699Skan 4626169699Skan/* Subroutine of reshape_init, which processes a single initializer (part of 4627169699Skan a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the 4628169699Skan iterator within the CONSTRUCTOR which points to the initializer to process. 4629169699Skan FIRST_INITIALIZER_P is true if this is the first initializer of the 4630169699Skan CONSTRUCTOR node. */ 4631169699Skan 4632169699Skanstatic tree 4633169699Skanreshape_init_r (tree type, reshape_iter *d, bool first_initializer_p) 4634169699Skan{ 4635169699Skan tree init = d->cur->value; 4636169699Skan 4637117410Skan /* A non-aggregate type is always initialized with a single 4638117410Skan initializer. */ 4639117410Skan if (!CP_AGGREGATE_TYPE_P (type)) 4640169699Skan { 4641169699Skan /* It is invalid to initialize a non-aggregate type with a 4642169699Skan brace-enclosed initializer. 4643169699Skan We need to check for BRACE_ENCLOSED_INITIALIZER_P here because 4644169699Skan of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is 4645169699Skan a CONSTRUCTOR (with a record type). */ 4646169699Skan if (TREE_CODE (init) == CONSTRUCTOR 4647169699Skan && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */ 4648169699Skan { 4649169699Skan error ("braces around scalar initializer for type %qT", type); 4650169699Skan init = error_mark_node; 4651169699Skan } 4652117410Skan 4653169699Skan d->cur++; 4654169699Skan return init; 4655169699Skan } 4656169699Skan 4657117410Skan /* [dcl.init.aggr] 4658117410Skan 4659117410Skan All implicit type conversions (clause _conv_) are considered when 4660117410Skan initializing the aggregate member with an initializer from an 4661117410Skan initializer-list. If the initializer can initialize a member, 4662117410Skan the member is initialized. Otherwise, if the member is itself a 4663117410Skan non-empty subaggregate, brace elision is assumed and the 4664117410Skan initializer is considered for the initialization of the first 4665117410Skan member of the subaggregate. */ 4666169699Skan if (TREE_CODE (init) != CONSTRUCTOR 4667169699Skan && can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL)) 4668117410Skan { 4669169699Skan d->cur++; 4670169699Skan return init; 4671117410Skan } 4672117410Skan 4673169699Skan /* [dcl.init.string] 4674169699Skan 4675169699Skan A char array (whether plain char, signed char, or unsigned char) 4676169699Skan can be initialized by a string-literal (optionally enclosed in 4677169699Skan braces); a wchar_t array can be initialized by a wide 4678169699Skan string-literal (optionally enclosed in braces). */ 4679169699Skan if (TREE_CODE (type) == ARRAY_TYPE 4680117410Skan && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))) 4681117410Skan { 4682169699Skan tree str_init = init; 4683117410Skan 4684169699Skan /* Strip one level of braces if and only if they enclose a single 4685169699Skan element (as allowed by [dcl.init.string]). */ 4686169699Skan if (!first_initializer_p 4687169699Skan && TREE_CODE (str_init) == CONSTRUCTOR 4688169699Skan && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1) 4689169699Skan { 4690169699Skan str_init = VEC_index (constructor_elt, 4691169699Skan CONSTRUCTOR_ELTS (str_init), 0)->value; 4692169699Skan } 4693169699Skan 4694169699Skan /* If it's a string literal, then it's the initializer for the array 4695169699Skan as a whole. Otherwise, continue with normal initialization for 4696169699Skan array types (one value per array element). */ 4697169699Skan if (TREE_CODE (str_init) == STRING_CST) 4698169699Skan { 4699169699Skan d->cur++; 4700169699Skan return str_init; 4701169699Skan } 4702117410Skan } 4703169699Skan 4704169699Skan /* The following cases are about aggregates. If we are not within a full 4705169699Skan initializer already, and there is not a CONSTRUCTOR, it means that there 4706169699Skan is a missing set of braces (that is, we are processing the case for 4707169699Skan which reshape_init exists). */ 4708169699Skan if (!first_initializer_p) 4709117410Skan { 4710169699Skan if (TREE_CODE (init) == CONSTRUCTOR) 471118334Speter { 4712169699Skan if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init))) 4713169699Skan /* There is no need to reshape pointer-to-member function 4714169699Skan initializers, as they are always constructed correctly 4715169699Skan by the front end. */ 4716169699Skan ; 4717169699Skan else if (COMPOUND_LITERAL_P (init)) 4718169699Skan /* For a nested compound literal, there is no need to reshape since 4719169699Skan brace elision is not allowed. Even if we decided to allow it, 4720169699Skan we should add a call to reshape_init in finish_compound_literal, 4721169699Skan before calling digest_init, so changing this code would still 4722169699Skan not be necessary. */ 4723169699Skan gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init)); 4724169699Skan else 4725117410Skan { 4726169699Skan ++d->cur; 4727169699Skan gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); 4728169699Skan return reshape_init (type, init); 4729117410Skan } 4730169699Skan } 4731117410Skan 4732169699Skan warning (OPT_Wmissing_braces, "missing braces around initializer for %qT", 4733169699Skan type); 4734169699Skan } 4735117410Skan 4736169699Skan /* Dispatch to specialized routines. */ 4737169699Skan if (CLASS_TYPE_P (type)) 4738169699Skan return reshape_init_class (type, d, first_initializer_p); 4739169699Skan else if (TREE_CODE (type) == ARRAY_TYPE) 4740169699Skan return reshape_init_array (type, d); 4741169699Skan else if (TREE_CODE (type) == VECTOR_TYPE) 4742169699Skan return reshape_init_vector (type, d); 4743169699Skan else 4744169699Skan gcc_unreachable(); 4745169699Skan} 4746117410Skan 4747169699Skan/* Undo the brace-elision allowed by [dcl.init.aggr] in a 4748169699Skan brace-enclosed aggregate initializer. 4749117410Skan 4750169699Skan INIT is the CONSTRUCTOR containing the list of initializers describing 4751169699Skan a brace-enclosed initializer for an entity of the indicated aggregate TYPE. 4752169699Skan It may not presently match the shape of the TYPE; for example: 4753117410Skan 4754169699Skan struct S { int a; int b; }; 4755169699Skan struct S a[] = { 1, 2, 3, 4 }; 4756117410Skan 4757169699Skan Here INIT will hold a VEC of four elements, rather than a 4758169699Skan VEC of two elements, each itself a VEC of two elements. This 4759169699Skan routine transforms INIT from the former form into the latter. The 4760169699Skan revised CONSTRUCTOR node is returned. */ 476118334Speter 4762169699Skantree 4763169699Skanreshape_init (tree type, tree init) 4764169699Skan{ 4765169699Skan VEC(constructor_elt, gc) *v; 4766169699Skan reshape_iter d; 4767169699Skan tree new_init; 4768117410Skan 4769169699Skan gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); 4770169699Skan 4771169699Skan v = CONSTRUCTOR_ELTS (init); 4772169699Skan 4773169699Skan /* An empty constructor does not need reshaping, and it is always a valid 4774169699Skan initializer. */ 4775169699Skan if (VEC_empty (constructor_elt, v)) 4776169699Skan return init; 4777169699Skan 4778169699Skan /* Recurse on this CONSTRUCTOR. */ 4779169699Skan d.cur = VEC_index (constructor_elt, v, 0); 4780169699Skan d.end = d.cur + VEC_length (constructor_elt, v); 4781169699Skan 4782169699Skan new_init = reshape_init_r (type, &d, true); 4783169699Skan if (new_init == error_mark_node) 4784169699Skan return error_mark_node; 4785169699Skan 4786169699Skan /* Make sure all the element of the constructor were used. Otherwise, 4787169699Skan issue an error about exceeding initializers. */ 4788169699Skan if (d.cur != d.end) 4789169699Skan error ("too many initializers for %qT", type); 4790169699Skan 4791117410Skan return new_init; 4792117410Skan} 4793117410Skan 4794117410Skan/* Verify INIT (the initializer for DECL), and record the 4795122192Skan initialization in DECL_INITIAL, if appropriate. CLEANUP is as for 4796122192Skan grok_reference_init. 4797117410Skan 4798117410Skan If the return value is non-NULL, it is an expression that must be 4799117410Skan evaluated dynamically to initialize DECL. */ 4800117410Skan 4801117410Skanstatic tree 4802122192Skancheck_initializer (tree decl, tree init, int flags, tree *cleanup) 4803117410Skan{ 4804117410Skan tree type = TREE_TYPE (decl); 4805132747Skan tree init_code = NULL; 4806117410Skan 4807117410Skan /* Things that are going to be initialized need to have complete 4808117410Skan type. */ 4809117410Skan TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); 4810117410Skan 4811117410Skan if (type == error_mark_node) 4812117410Skan /* We will have already complained. */ 4813169699Skan return NULL_TREE; 4814169699Skan 4815169699Skan if (TREE_CODE (type) == ARRAY_TYPE) 4816117410Skan { 4817169699Skan tree element_type = TREE_TYPE (type); 4818169699Skan 4819169699Skan /* The array type itself need not be complete, because the 4820169699Skan initializer may tell us how many elements are in the array. 4821169699Skan But, the elements of the array must be complete. */ 4822169699Skan if (!COMPLETE_TYPE_P (complete_type (element_type))) 4823169699Skan { 4824169699Skan error ("elements of array %q#D have incomplete type", decl); 4825169699Skan return NULL_TREE; 4826169699Skan } 4827169699Skan /* It is not valid to initialize an a VLA. */ 4828169699Skan if (init 4829169699Skan && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) 4830169699Skan || !TREE_CONSTANT (TYPE_SIZE (element_type)))) 4831169699Skan { 4832169699Skan error ("variable-sized object %qD may not be initialized", decl); 4833169699Skan return NULL_TREE; 4834169699Skan } 4835117410Skan } 4836169699Skan else if (!COMPLETE_TYPE_P (type)) 4837117410Skan { 4838169699Skan error ("%qD has incomplete type", decl); 4839169699Skan TREE_TYPE (decl) = error_mark_node; 4840169699Skan return NULL_TREE; 4841117410Skan } 4842169699Skan else 4843169699Skan /* There is no way to make a variable-sized class type in GNU C++. */ 4844169699Skan gcc_assert (TREE_CONSTANT (TYPE_SIZE (type))); 4845169699Skan 4846169699Skan if (!CP_AGGREGATE_TYPE_P (type) 4847169699Skan && init && BRACE_ENCLOSED_INITIALIZER_P (init) 4848169699Skan && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1) 4849117410Skan { 4850169699Skan error ("scalar object %qD requires one element in initializer", decl); 4851117410Skan TREE_TYPE (decl) = error_mark_node; 4852169699Skan return NULL_TREE; 4853117410Skan } 4854117410Skan 485590287Sobrien if (TREE_CODE (decl) == CONST_DECL) 485618334Speter { 4857169699Skan gcc_assert (TREE_CODE (type) != REFERENCE_TYPE); 485818334Speter 485918334Speter DECL_INITIAL (decl) = init; 486018334Speter 4861169699Skan gcc_assert (init != NULL_TREE); 486218334Speter init = NULL_TREE; 486318334Speter } 486490287Sobrien else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE) 4865122192Skan init = grok_reference_init (decl, type, init, cleanup); 4866117410Skan else if (init) 486790287Sobrien { 4868169699Skan /* Do not reshape constructors of vectors (they don't need to be 4869169699Skan reshaped. */ 4870169699Skan if (TREE_CODE (init) == CONSTRUCTOR 4871169699Skan && !COMPOUND_LITERAL_P (init) 4872169699Skan && !TREE_TYPE (init)) /* ptrmemfunc */ 4873117410Skan { 4874169699Skan init = reshape_init (type, init); 4875132747Skan 4876132747Skan if ((*targetm.vector_opaque_p) (type)) 4877132747Skan { 4878132747Skan error ("opaque vector types cannot be initialized"); 4879132747Skan init = error_mark_node; 4880132747Skan } 4881117410Skan } 4882102804Skan 4883117410Skan /* If DECL has an array type without a specific bound, deduce the 4884117410Skan array size from the initializer. */ 4885117410Skan maybe_deduce_size_from_array_init (decl, init); 4886117410Skan type = TREE_TYPE (decl); 4887169699Skan if (type == error_mark_node) 4888169699Skan return NULL_TREE; 4889117410Skan 489018334Speter if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type)) 489118334Speter { 489218334Speter if (TREE_CODE (type) == ARRAY_TYPE) 4893117410Skan goto initialize_aggr; 4894169699Skan else if (TREE_CODE (init) == CONSTRUCTOR) 489518334Speter { 489618334Speter if (TYPE_NON_AGGREGATE_CLASS (type)) 489718334Speter { 4898169699Skan error ("%qD must be initialized by constructor, " 4899169699Skan "not by %<{...}%>", 4900117410Skan decl); 490118334Speter init = error_mark_node; 490218334Speter } 490318334Speter else 490418334Speter goto dont_use_constructor; 490518334Speter } 4906117410Skan else 4907117410Skan { 4908117410Skan int saved_stmts_are_full_exprs_p; 4909117410Skan 4910117410Skan initialize_aggr: 4911117410Skan saved_stmts_are_full_exprs_p = 0; 4912117410Skan if (building_stmt_tree ()) 4913117410Skan { 4914117410Skan saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); 4915117410Skan current_stmt_tree ()->stmts_are_full_exprs_p = 1; 4916117410Skan } 4917117410Skan init = build_aggr_init (decl, init, flags); 4918117410Skan if (building_stmt_tree ()) 4919117410Skan current_stmt_tree ()->stmts_are_full_exprs_p = 4920117410Skan saved_stmts_are_full_exprs_p; 4921117410Skan return init; 4922117410Skan } 492318334Speter } 492418334Speter else 492518334Speter { 492618334Speter dont_use_constructor: 492718334Speter if (TREE_CODE (init) != TREE_VEC) 4928132747Skan { 4929132747Skan init_code = store_init_value (decl, init); 4930169699Skan if (pedantic && TREE_CODE (type) == ARRAY_TYPE 4931169699Skan && DECL_INITIAL (decl) 4932169699Skan && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST 4933169699Skan && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl))) 4934169699Skan warning (0, "array %qD initialized by parenthesized string literal %qE", 4935169699Skan decl, DECL_INITIAL (decl)); 4936132747Skan init = NULL; 4937132747Skan } 493818334Speter } 493918334Speter } 494018334Speter else if (DECL_EXTERNAL (decl)) 494118334Speter ; 4942117410Skan else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type)) 4943117410Skan goto initialize_aggr; 4944117410Skan else if (IS_AGGR_TYPE (type)) 494518334Speter { 494690287Sobrien tree core_type = strip_array_types (type); 494790287Sobrien 4948117410Skan if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)) 4949169699Skan error ("structure %qD with uninitialized const members", decl); 4950117410Skan if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) 4951169699Skan error ("structure %qD with uninitialized reference members", decl); 495218334Speter 495352290Sobrien check_for_uninitialized_const_var (decl); 495418334Speter } 495552290Sobrien else 495652290Sobrien check_for_uninitialized_const_var (decl); 495718334Speter 4958117410Skan if (init && init != error_mark_node) 4959169699Skan init_code = build2 (INIT_EXPR, type, decl, init); 4960117410Skan 4961132747Skan return init_code; 496290287Sobrien} 496318334Speter 496490287Sobrien/* If DECL is not a local variable, give it RTL. */ 496518334Speter 496690287Sobrienstatic void 4967132747Skanmake_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec) 496890287Sobrien{ 496990287Sobrien int toplev = toplevel_bindings_p (); 497090287Sobrien int defer_p; 4971169699Skan const char *filename; 497290287Sobrien 4973169699Skan /* Set the DECL_ASSEMBLER_NAME for the object. */ 4974169699Skan if (asmspec) 4975169699Skan { 4976169699Skan /* The `register' keyword, when used together with an 4977169699Skan asm-specification, indicates that the variable should be 4978169699Skan placed in a particular register. */ 4979169699Skan if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)) 4980169699Skan { 4981169699Skan set_user_assembler_name (decl, asmspec); 4982169699Skan DECL_HARD_REGISTER (decl) = 1; 4983169699Skan } 4984169699Skan else 4985169699Skan { 4986169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 4987169699Skan && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) 4988169699Skan set_builtin_user_assembler_name (decl, asmspec); 4989169699Skan set_user_assembler_name (decl, asmspec); 4990169699Skan } 4991169699Skan } 4992169699Skan 499390287Sobrien /* Handle non-variables up front. */ 499490287Sobrien if (TREE_CODE (decl) != VAR_DECL) 499518334Speter { 4996169699Skan rest_of_decl_compilation (decl, toplev, at_eof); 499790287Sobrien return; 499890287Sobrien } 499918334Speter 500090287Sobrien /* If we see a class member here, it should be a static data 500190287Sobrien member. */ 500290287Sobrien if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) 500390287Sobrien { 5004169699Skan gcc_assert (TREE_STATIC (decl)); 500590287Sobrien /* An in-class declaration of a static data member should be 500690287Sobrien external; it is only a declaration, and not a definition. */ 500790287Sobrien if (init == NULL_TREE) 5008224523Smm gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl)); 500990287Sobrien } 501018334Speter 501190287Sobrien /* We don't create any RTL for local variables. */ 501290287Sobrien if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) 501390287Sobrien return; 501490287Sobrien 501590287Sobrien /* We defer emission of local statics until the corresponding 5016169699Skan DECL_EXPR is expanded. */ 501790287Sobrien defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl); 501890287Sobrien 501990287Sobrien /* We try to defer namespace-scope static constants so that they are 502090287Sobrien not emitted into the object file unnecessarily. */ 5021169699Skan filename = input_filename; 502290287Sobrien if (!DECL_VIRTUAL_P (decl) 502390287Sobrien && TREE_READONLY (decl) 502490287Sobrien && DECL_INITIAL (decl) != NULL_TREE 502590287Sobrien && DECL_INITIAL (decl) != error_mark_node 5026169699Skan && filename != NULL 502790287Sobrien && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)) 502890287Sobrien && toplev 502990287Sobrien && !TREE_PUBLIC (decl)) 503090287Sobrien { 503196292Sobrien /* Fool with the linkage of static consts according to #pragma 503296292Sobrien interface. */ 5033169699Skan struct c_fileinfo *finfo = get_fileinfo (filename); 5034169699Skan if (!finfo->interface_unknown && !TREE_PUBLIC (decl)) 503518334Speter { 503690287Sobrien TREE_PUBLIC (decl) = 1; 5037169699Skan DECL_EXTERNAL (decl) = finfo->interface_only; 503818334Speter } 503918334Speter 504090287Sobrien defer_p = 1; 504118334Speter } 504296292Sobrien /* Likewise for template instantiations. */ 5043169699Skan else if (DECL_LANG_SPECIFIC (decl) 5044169699Skan && DECL_IMPLICIT_INSTANTIATION (decl)) 504596292Sobrien defer_p = 1; 504618334Speter 504790287Sobrien /* If we're not deferring, go ahead and assemble the variable. */ 5048169699Skan if (!defer_p) 5049169699Skan rest_of_decl_compilation (decl, toplev, at_eof); 505090287Sobrien} 505190287Sobrien 505290287Sobrien/* Generate code to initialize DECL (a local variable). */ 505390287Sobrien 5054117410Skanstatic void 5055132747Skaninitialize_local_var (tree decl, tree init) 505690287Sobrien{ 505790287Sobrien tree type = TREE_TYPE (decl); 5058117410Skan tree cleanup; 505990287Sobrien 5060169699Skan gcc_assert (TREE_CODE (decl) == VAR_DECL 5061169699Skan || TREE_CODE (decl) == RESULT_DECL); 5062169699Skan gcc_assert (!TREE_STATIC (decl)); 506390287Sobrien 5064117410Skan if (DECL_SIZE (decl) == NULL_TREE) 506518334Speter { 506690287Sobrien /* If we used it already as memory, it must stay in memory. */ 506790287Sobrien DECL_INITIAL (decl) = NULL_TREE; 506890287Sobrien TREE_ADDRESSABLE (decl) = TREE_USED (decl); 506918334Speter } 507018334Speter 507190287Sobrien if (DECL_SIZE (decl) && type != error_mark_node) 507218334Speter { 507390287Sobrien int already_used; 507418334Speter 507590287Sobrien /* Compute and store the initial value. */ 507690287Sobrien already_used = TREE_USED (decl) || TREE_USED (type); 507718334Speter 5078117410Skan /* Perform the initialization. */ 5079117410Skan if (init) 508051412Sobrien { 508190287Sobrien int saved_stmts_are_full_exprs_p; 508218334Speter 5083169699Skan gcc_assert (building_stmt_tree ()); 508490287Sobrien saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); 508590287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 1; 5086117410Skan finish_expr_stmt (init); 508790287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 508890287Sobrien saved_stmts_are_full_exprs_p; 508951412Sobrien } 509051412Sobrien 509190287Sobrien /* Set this to 0 so we can tell whether an aggregate which was 509290287Sobrien initialized was ever used. Don't do this if it has a 509390287Sobrien destructor, so we don't complain about the 'resource 509490287Sobrien allocation is initialization' idiom. Now set 509590287Sobrien attribute((unused)) on types so decls of that type will be 509690287Sobrien marked used. (see TREE_USED, above.) */ 509790287Sobrien if (TYPE_NEEDS_CONSTRUCTING (type) 509890287Sobrien && ! already_used 509990287Sobrien && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) 510090287Sobrien && DECL_NAME (decl)) 510190287Sobrien TREE_USED (decl) = 0; 510290287Sobrien else if (already_used) 510390287Sobrien TREE_USED (decl) = 1; 510490287Sobrien } 510551412Sobrien 5106117410Skan /* Generate a cleanup, if necessary. */ 5107117410Skan cleanup = cxx_maybe_build_cleanup (decl); 510896292Sobrien if (DECL_SIZE (decl) && cleanup) 510990287Sobrien finish_decl_cleanup (decl, cleanup); 511090287Sobrien} 511190287Sobrien 5112169699Skan/* DECL is a VAR_DECL for a compiler-generated variable with static 5113169699Skan storage duration (like a virtual table) whose initializer is a 5114169699Skan compile-time constant. INIT must be either a TREE_LIST of values, 5115169699Skan or a CONSTRUCTOR. Initialize the variable and provide it to the 5116169699Skan back end. */ 5117169699Skan 5118169699Skanvoid 5119169699Skaninitialize_artificial_var (tree decl, tree init) 5120169699Skan{ 5121169699Skan gcc_assert (DECL_ARTIFICIAL (decl)); 5122169699Skan if (TREE_CODE (init) == TREE_LIST) 5123169699Skan init = build_constructor_from_list (NULL_TREE, init); 5124169699Skan gcc_assert (TREE_CODE (init) == CONSTRUCTOR); 5125169699Skan DECL_INITIAL (decl) = init; 5126169699Skan DECL_INITIALIZED_P (decl) = 1; 5127169699Skan determine_visibility (decl); 5128169699Skan layout_var_decl (decl); 5129169699Skan maybe_commonize_var (decl); 5130169699Skan make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL); 5131169699Skan} 5132169699Skan 5133171835Skan/* INIT is the initializer for a variable, as represented by the 5134171835Skan parser. Returns true iff INIT is value-dependent. */ 5135171835Skan 5136171835Skanstatic bool 5137171835Skanvalue_dependent_init_p (tree init) 5138171835Skan{ 5139171835Skan if (TREE_CODE (init) == TREE_LIST) 5140171835Skan /* A parenthesized initializer, e.g.: int i (3, 2); ? */ 5141171835Skan return any_value_dependent_elements_p (init); 5142171835Skan else if (TREE_CODE (init) == CONSTRUCTOR) 5143171835Skan /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */ 5144171835Skan { 5145171835Skan VEC(constructor_elt, gc) *elts; 5146171835Skan size_t nelts; 5147171835Skan size_t i; 5148171835Skan 5149171835Skan elts = CONSTRUCTOR_ELTS (init); 5150171835Skan nelts = VEC_length (constructor_elt, elts); 5151171835Skan for (i = 0; i < nelts; ++i) 5152171835Skan if (value_dependent_init_p (VEC_index (constructor_elt, 5153171835Skan elts, i)->value)) 5154171835Skan return true; 5155171835Skan } 5156171835Skan else 5157171835Skan /* It must be a simple expression, e.g., int i = 3; */ 5158171835Skan return value_dependent_expression_p (init); 5159171835Skan 5160171835Skan return false; 5161171835Skan} 5162171835Skan 516390287Sobrien/* Finish processing of a declaration; 516490287Sobrien install its line number and initial value. 516590287Sobrien If the length of an array type is not known before, 516690287Sobrien it must be determined now, from the initial value, or it is an error. 516790287Sobrien 5168169699Skan INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is 5169169699Skan true, then INIT is an integral constant expression. 517090287Sobrien 517190287Sobrien FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 517290287Sobrien if the (init) syntax was used. */ 517390287Sobrien 517490287Sobrienvoid 5175169699Skancp_finish_decl (tree decl, tree init, bool init_const_expr_p, 5176169699Skan tree asmspec_tree, int flags) 517790287Sobrien{ 5178122192Skan tree type; 5179122192Skan tree cleanup; 518090287Sobrien const char *asmspec = NULL; 518190287Sobrien int was_readonly = 0; 5182146906Skan bool var_definition_p = false; 5183169699Skan int saved_processing_template_decl; 518490287Sobrien 5185132747Skan if (decl == error_mark_node) 5186132747Skan return; 5187132747Skan else if (! decl) 518890287Sobrien { 518990287Sobrien if (init) 519090287Sobrien error ("assignment (not initialization) in declaration"); 519190287Sobrien return; 519290287Sobrien } 519390287Sobrien 5194169699Skan gcc_assert (TREE_CODE (decl) != RESULT_DECL); 5195169699Skan /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ 5196169699Skan gcc_assert (TREE_CODE (decl) != PARM_DECL); 5197132747Skan 5198169699Skan type = TREE_TYPE (decl); 5199169699Skan if (type == error_mark_node) 5200169699Skan return; 5201169699Skan 5202122192Skan /* Assume no cleanup is required. */ 5203122192Skan cleanup = NULL_TREE; 5204169699Skan saved_processing_template_decl = processing_template_decl; 5205122192Skan 520690287Sobrien /* If a name was specified, get the string. */ 5207117410Skan if (global_scope_p (current_binding_level)) 520896292Sobrien asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 5209169699Skan if (asmspec_tree && asmspec_tree != error_mark_node) 521096292Sobrien asmspec = TREE_STRING_POINTER (asmspec_tree); 521190287Sobrien 521290287Sobrien if (current_class_type 521390287Sobrien && CP_DECL_CONTEXT (decl) == current_class_type 521490287Sobrien && TYPE_BEING_DEFINED (current_class_type) 521590287Sobrien && (DECL_INITIAL (decl) || init)) 521690287Sobrien DECL_INITIALIZED_IN_CLASS_P (decl) = 1; 521790287Sobrien 5218169699Skan if (processing_template_decl) 521990287Sobrien { 5220169699Skan bool type_dependent_p; 522190287Sobrien 5222169699Skan /* Add this declaration to the statement-tree. */ 5223169699Skan if (at_function_scope_p ()) 5224169699Skan add_decl_expr (decl); 522590287Sobrien 5226169699Skan type_dependent_p = dependent_type_p (type); 522790287Sobrien 5228169699Skan if (init && init_const_expr_p) 5229169699Skan { 5230169699Skan DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 5231169699Skan if (DECL_INTEGRAL_CONSTANT_VAR_P (decl)) 5232169699Skan TREE_CONSTANT (decl) = 1; 5233169699Skan } 523490287Sobrien 5235171835Skan /* Generally, initializers in templates are expanded when the 5236171835Skan template is instantiated. But, if DECL is an integral 5237171835Skan constant static data member, then it can be used in future 5238171835Skan integral constant expressions, and its value must be 5239171835Skan available. */ 5240171835Skan if (!(init 5241171835Skan && DECL_CLASS_SCOPE_P (decl) 5242171835Skan && DECL_INTEGRAL_CONSTANT_VAR_P (decl) 5243171835Skan && !type_dependent_p 5244171835Skan && !value_dependent_init_p (init))) 5245169699Skan { 5246169699Skan if (init) 5247169699Skan DECL_INITIAL (decl) = init; 5248169699Skan if (TREE_CODE (decl) == VAR_DECL 5249169699Skan && !DECL_PRETTY_FUNCTION_P (decl) 5250169699Skan && !type_dependent_p) 5251169699Skan maybe_deduce_size_from_array_init (decl, init); 5252169699Skan goto finish_end; 5253169699Skan } 525490287Sobrien 5255169699Skan init = fold_non_dependent_expr (init); 5256169699Skan processing_template_decl = 0; 525790287Sobrien } 525890287Sobrien 525990287Sobrien /* Take care of TYPE_DECLs up front. */ 526090287Sobrien if (TREE_CODE (decl) == TYPE_DECL) 526190287Sobrien { 526290287Sobrien if (type != error_mark_node 526390287Sobrien && IS_AGGR_TYPE (type) && DECL_NAME (decl)) 526418334Speter { 526590287Sobrien if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) 5266169699Skan warning (0, "shadowing previous type declaration of %q#D", decl); 5267132747Skan set_identifier_type_value (DECL_NAME (decl), decl); 526818334Speter } 526918334Speter 527090287Sobrien /* If we have installed this as the canonical typedef for this 527190287Sobrien type, and that type has not been defined yet, delay emitting 527290287Sobrien the debug information for it, as we will emit it later. */ 527390287Sobrien if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl 527490287Sobrien && !COMPLETE_TYPE_P (TREE_TYPE (decl))) 527590287Sobrien TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 527618334Speter 5277169699Skan rest_of_decl_compilation (decl, DECL_CONTEXT (decl) == NULL_TREE, 5278169699Skan at_eof); 527990287Sobrien goto finish_end; 528090287Sobrien } 528151412Sobrien 5282169699Skan /* A reference will be modified here, as it is initialized. */ 5283169699Skan if (! DECL_EXTERNAL (decl) 5284169699Skan && TREE_READONLY (decl) 5285169699Skan && TREE_CODE (type) == REFERENCE_TYPE) 528690287Sobrien { 528790287Sobrien was_readonly = 1; 5288169699Skan TREE_READONLY (decl) = 0; 528990287Sobrien } 529051412Sobrien 5291132747Skan if (TREE_CODE (decl) == VAR_DECL) 529290287Sobrien { 5293117410Skan /* Only PODs can have thread-local storage. Other types may require 5294117410Skan various kinds of non-trivial initialization. */ 5295169699Skan if (DECL_THREAD_LOCAL_P (decl) && !pod_type_p (TREE_TYPE (decl))) 5296169699Skan error ("%qD cannot be thread-local because it has non-POD type %qT", 5297117410Skan decl, TREE_TYPE (decl)); 5298169699Skan /* If this is a local variable that will need a mangled name, 5299169699Skan register it now. We must do this before processing the 5300169699Skan initializer for the variable, since the initialization might 5301169699Skan require a guard variable, and since the mangled name of the 5302169699Skan guard variable will depend on the mangled name of this 5303169699Skan variable. */ 5304169699Skan if (!processing_template_decl 5305169699Skan && DECL_FUNCTION_SCOPE_P (decl) 5306169699Skan && TREE_STATIC (decl) 5307169699Skan && !DECL_ARTIFICIAL (decl)) 5308169699Skan push_local_name (decl); 5309117410Skan /* Convert the initializer to the type of DECL, if we have not 5310117410Skan already initialized DECL. */ 5311117410Skan if (!DECL_INITIALIZED_P (decl) 5312117410Skan /* If !DECL_EXTERNAL then DECL is being defined. In the 5313117410Skan case of a static data member initialized inside the 5314117410Skan class-specifier, there can be an initializer even if DECL 5315117410Skan is *not* defined. */ 5316117410Skan && (!DECL_EXTERNAL (decl) || init)) 5317117410Skan { 5318169699Skan if (init) 5319169699Skan { 5320169699Skan DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1; 5321169699Skan if (init_const_expr_p) 5322169699Skan { 5323169699Skan DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 5324169699Skan if (DECL_INTEGRAL_CONSTANT_VAR_P (decl)) 5325169699Skan TREE_CONSTANT (decl) = 1; 5326169699Skan } 5327169699Skan } 5328122192Skan init = check_initializer (decl, init, flags, &cleanup); 5329117410Skan /* Thread-local storage cannot be dynamically initialized. */ 5330169699Skan if (DECL_THREAD_LOCAL_P (decl) && init) 5331117410Skan { 5332169699Skan error ("%qD is thread-local and so cannot be dynamically " 5333117410Skan "initialized", decl); 5334117410Skan init = NULL_TREE; 5335117410Skan } 5336169699Skan 5337169699Skan /* Check that the initializer for a static data member was a 5338169699Skan constant. Although we check in the parser that the 5339169699Skan initializer is an integral constant expression, we do not 5340169699Skan simplify division-by-zero at the point at which it 5341169699Skan occurs. Therefore, in: 5342169699Skan 5343169699Skan struct S { static const int i = 7 / 0; }; 5344169699Skan 5345169699Skan we issue an error at this point. It would 5346169699Skan probably be better to forbid division by zero in 5347169699Skan integral constant expressions. */ 5348161660Skan if (DECL_EXTERNAL (decl) && init) 5349161660Skan { 5350169699Skan error ("%qD cannot be initialized by a non-constant expression" 5351161660Skan " when being declared", decl); 5352161660Skan DECL_INITIALIZED_IN_CLASS_P (decl) = 0; 5353161660Skan init = NULL_TREE; 5354161660Skan } 5355169699Skan 5356117410Skan /* Handle: 5357169699Skan 5358117410Skan [dcl.init] 5359169699Skan 5360117410Skan The memory occupied by any object of static storage 5361117410Skan duration is zero-initialized at program startup before 5362117410Skan any other initialization takes place. 5363169699Skan 5364117410Skan We cannot create an appropriate initializer until after 5365117410Skan the type of DECL is finalized. If DECL_INITIAL is set, 5366117410Skan then the DECL is statically initialized, and any 5367117410Skan necessary zero-initialization has already been performed. */ 5368117410Skan if (TREE_STATIC (decl) && !DECL_INITIAL (decl)) 5369117410Skan DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl), 5370117410Skan /*nelts=*/NULL_TREE, 5371117410Skan /*static_storage_p=*/true); 5372117410Skan /* Remember that the initialization for this variable has 5373117410Skan taken place. */ 5374117410Skan DECL_INITIALIZED_P (decl) = 1; 5375146906Skan /* This declaration is the definition of this variable, 5376146906Skan unless we are initializing a static data member within 5377146906Skan the class specifier. */ 5378146906Skan if (!DECL_EXTERNAL (decl)) 5379146906Skan var_definition_p = true; 5380117410Skan } 5381117410Skan /* If the variable has an array type, lay out the type, even if 5382117410Skan there is no initializer. It is valid to index through the 5383117410Skan array, and we must get TYPE_ALIGN set correctly on the array 5384117410Skan type. */ 5385117410Skan else if (TREE_CODE (type) == ARRAY_TYPE) 5386117410Skan layout_type (type); 5387117410Skan } 538851412Sobrien 538990287Sobrien /* Add this declaration to the statement-tree. This needs to happen 5390169699Skan after the call to check_initializer so that the DECL_EXPR for a 5391169699Skan reference temp is added before the DECL_EXPR for the reference itself. */ 5392132747Skan if (at_function_scope_p ()) 5393169699Skan add_decl_expr (decl); 539418334Speter 5395169699Skan /* Let the middle end know about variables and functions -- but not 5396169699Skan static data members in uninstantiated class templates. */ 5397169699Skan if (!saved_processing_template_decl 5398169699Skan && (TREE_CODE (decl) == VAR_DECL 5399169699Skan || TREE_CODE (decl) == FUNCTION_DECL)) 540090287Sobrien { 540190287Sobrien if (TREE_CODE (decl) == VAR_DECL) 5402169699Skan { 5403169699Skan layout_var_decl (decl); 5404169699Skan maybe_commonize_var (decl); 5405169699Skan } 540618334Speter 540790287Sobrien make_rtl_for_nonlocal_decl (decl, init, asmspec); 540851412Sobrien 5409169699Skan /* Check for abstractness of the type. Notice that there is no 5410169699Skan need to strip array types here since the check for those types 5411169699Skan is already done within create_array_type_for_decl. */ 541290287Sobrien if (TREE_CODE (type) == FUNCTION_TYPE 541390287Sobrien || TREE_CODE (type) == METHOD_TYPE) 5414169699Skan abstract_virtuals_error (decl, TREE_TYPE (type)); 541590287Sobrien else 5416132747Skan abstract_virtuals_error (decl, type); 541751412Sobrien 5418169699Skan /* This needs to happen after the linkage is set. */ 5419169699Skan determine_visibility (decl); 5420169699Skan 5421169699Skan if (TREE_CODE (decl) == FUNCTION_DECL 5422117410Skan || TREE_TYPE (decl) == error_mark_node) 5423117410Skan /* No initialization required. */ 542490287Sobrien ; 542590287Sobrien else if (DECL_EXTERNAL (decl) 542690287Sobrien && ! (DECL_LANG_SPECIFIC (decl) 542790287Sobrien && DECL_NOT_REALLY_EXTERN (decl))) 542890287Sobrien { 542990287Sobrien if (init) 543090287Sobrien DECL_INITIAL (decl) = init; 543190287Sobrien } 5432117410Skan else 543390287Sobrien { 5434117410Skan /* A variable definition. */ 5435117410Skan if (DECL_FUNCTION_SCOPE_P (decl)) 543651412Sobrien { 5437117410Skan /* Initialize the local variable. */ 5438117410Skan if (processing_template_decl) 5439169699Skan DECL_INITIAL (decl) = init; 5440117410Skan else if (!TREE_STATIC (decl)) 5441117410Skan initialize_local_var (decl, init); 544218334Speter } 5443117410Skan 5444146906Skan /* If a variable is defined, and then a subsequent 5445169699Skan definition with external linkage is encountered, we will 5446146906Skan get here twice for the same variable. We want to avoid 5447146906Skan calling expand_static_init more than once. For variables 5448146906Skan that are not static data members, we can call 5449146906Skan expand_static_init only when we actually process the 5450146906Skan initializer. It is not legal to redeclare a static data 5451146906Skan member, so this issue does not arise in that case. */ 5452146906Skan if (var_definition_p && TREE_STATIC (decl)) 5453171835Skan { 5454171835Skan /* If a TREE_READONLY variable needs initialization 5455171835Skan at runtime, it is no longer readonly and we need to 5456171835Skan avoid MEM_READONLY_P being set on RTL created for it. */ 5457171835Skan if (init) 5458171835Skan { 5459171835Skan if (TREE_READONLY (decl)) 5460171835Skan TREE_READONLY (decl) = 0; 5461171835Skan was_readonly = 0; 5462171835Skan } 5463171835Skan expand_static_init (decl, init); 5464171835Skan } 546590287Sobrien } 546618334Speter } 546718334Speter 5468122192Skan /* If a CLEANUP_STMT was created to destroy a temporary bound to a 5469122192Skan reference, insert it in the statement-tree now. */ 5470122192Skan if (cleanup) 5471169699Skan push_cleanup (decl, cleanup, false); 5472122192Skan 547318334Speter finish_end: 5474169699Skan processing_template_decl = saved_processing_template_decl; 547518334Speter 547690287Sobrien if (was_readonly) 547790287Sobrien TREE_READONLY (decl) = 1; 5478132747Skan 5479132747Skan /* If this was marked 'used', be sure it will be output. */ 5480132747Skan if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 5481169699Skan mark_decl_referenced (decl); 548290287Sobrien} 548318334Speter 5484132747Skan/* This is here for a midend callback from c-common.c. */ 548590287Sobrien 548690287Sobrienvoid 5487132747Skanfinish_decl (tree decl, tree init, tree asmspec_tree) 548890287Sobrien{ 5489169699Skan cp_finish_decl (decl, init, /*init_const_expr_p=*/false, asmspec_tree, 0); 549090287Sobrien} 549190287Sobrien 549290287Sobrien/* Returns a declaration for a VAR_DECL as if: 549390287Sobrien 549490287Sobrien extern "C" TYPE NAME; 549590287Sobrien 549690287Sobrien had been seen. Used to create compiler-generated global 549790287Sobrien variables. */ 549890287Sobrien 5499169699Skanstatic tree 5500132747Skandeclare_global_var (tree name, tree type) 550190287Sobrien{ 550290287Sobrien tree decl; 550390287Sobrien 550490287Sobrien push_to_top_level (); 550590287Sobrien decl = build_decl (VAR_DECL, name, type); 550690287Sobrien TREE_PUBLIC (decl) = 1; 550790287Sobrien DECL_EXTERNAL (decl) = 1; 550890287Sobrien DECL_ARTIFICIAL (decl) = 1; 5509169699Skan /* If the user has explicitly declared this variable (perhaps 5510169699Skan because the code we are compiling is part of a low-level runtime 5511169699Skan library), then it is possible that our declaration will be merged 5512169699Skan with theirs by pushdecl. */ 5513169699Skan decl = pushdecl (decl); 5514169699Skan finish_decl (decl, NULL_TREE, NULL_TREE); 551590287Sobrien pop_from_top_level (); 551690287Sobrien 551790287Sobrien return decl; 551890287Sobrien} 551990287Sobrien 552090287Sobrien/* Returns a pointer to the `atexit' function. Note that if 5521117410Skan FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new 552290287Sobrien `__cxa_atexit' function specified in the IA64 C++ ABI. */ 552390287Sobrien 552490287Sobrienstatic tree 5525132747Skanget_atexit_node (void) 552690287Sobrien{ 552790287Sobrien tree atexit_fndecl; 552890287Sobrien tree arg_types; 552990287Sobrien tree fn_type; 553090287Sobrien tree fn_ptr_type; 553190287Sobrien const char *name; 5532169699Skan bool use_aeabi_atexit; 553390287Sobrien 553490287Sobrien if (atexit_node) 553590287Sobrien return atexit_node; 553690287Sobrien 553790287Sobrien if (flag_use_cxa_atexit) 553818334Speter { 553990287Sobrien /* The declaration for `__cxa_atexit' is: 554018334Speter 554190287Sobrien int __cxa_atexit (void (*)(void *), void *, void *) 554218334Speter 554390287Sobrien We build up the argument types and then then function type 554490287Sobrien itself. */ 554590287Sobrien 5546169699Skan use_aeabi_atexit = targetm.cxx.use_aeabi_atexit (); 554790287Sobrien /* First, build the pointer-to-function type for the first 554890287Sobrien argument. */ 554990287Sobrien arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); 555090287Sobrien fn_type = build_function_type (void_type_node, arg_types); 555190287Sobrien fn_ptr_type = build_pointer_type (fn_type); 555290287Sobrien /* Then, build the rest of the argument types. */ 555390287Sobrien arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); 5554169699Skan if (use_aeabi_atexit) 5555169699Skan { 5556169699Skan arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); 5557169699Skan arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); 5558169699Skan } 5559169699Skan else 5560169699Skan { 5561169699Skan arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); 5562169699Skan arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); 5563169699Skan } 556490287Sobrien /* And the final __cxa_atexit type. */ 556590287Sobrien fn_type = build_function_type (integer_type_node, arg_types); 556690287Sobrien fn_ptr_type = build_pointer_type (fn_type); 5567169699Skan if (use_aeabi_atexit) 5568169699Skan name = "__aeabi_atexit"; 5569169699Skan else 5570169699Skan name = "__cxa_atexit"; 557118334Speter } 557290287Sobrien else 557390287Sobrien { 557490287Sobrien /* The declaration for `atexit' is: 557518334Speter 5576169699Skan int atexit (void (*)()); 557790287Sobrien 557890287Sobrien We build up the argument types and then then function type 557990287Sobrien itself. */ 558090287Sobrien fn_type = build_function_type (void_type_node, void_list_node); 558190287Sobrien fn_ptr_type = build_pointer_type (fn_type); 558290287Sobrien arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node); 558390287Sobrien /* Build the final atexit type. */ 558490287Sobrien fn_type = build_function_type (integer_type_node, arg_types); 558590287Sobrien name = "atexit"; 558690287Sobrien } 558790287Sobrien 558890287Sobrien /* Now, build the function declaration. */ 558990287Sobrien push_lang_context (lang_name_c); 559090287Sobrien atexit_fndecl = build_library_fn_ptr (name, fn_type); 559190287Sobrien mark_used (atexit_fndecl); 559290287Sobrien pop_lang_context (); 5593132747Skan atexit_node = decay_conversion (atexit_fndecl); 559490287Sobrien 559590287Sobrien return atexit_node; 559690287Sobrien} 559790287Sobrien 559890287Sobrien/* Returns the __dso_handle VAR_DECL. */ 559990287Sobrien 560090287Sobrienstatic tree 5601132747Skanget_dso_handle_node (void) 560290287Sobrien{ 560390287Sobrien if (dso_handle_node) 560490287Sobrien return dso_handle_node; 560590287Sobrien 560690287Sobrien /* Declare the variable. */ 560790287Sobrien dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), 560890287Sobrien ptr_type_node); 560990287Sobrien 561090287Sobrien return dso_handle_node; 561190287Sobrien} 561290287Sobrien 561390287Sobrien/* Begin a new function with internal linkage whose job will be simply 561490287Sobrien to destroy some particular variable. */ 561590287Sobrien 5616132747Skanstatic GTY(()) int start_cleanup_cnt; 5617132747Skan 561890287Sobrienstatic tree 5619132747Skanstart_cleanup_fn (void) 562090287Sobrien{ 562190287Sobrien char name[32]; 562290287Sobrien tree parmtypes; 562390287Sobrien tree fntype; 562490287Sobrien tree fndecl; 562590287Sobrien 562690287Sobrien push_to_top_level (); 562790287Sobrien 562890287Sobrien /* No need to mangle this. */ 562990287Sobrien push_lang_context (lang_name_c); 563090287Sobrien 563190287Sobrien /* Build the parameter-types. */ 563290287Sobrien parmtypes = void_list_node; 563390287Sobrien /* Functions passed to __cxa_atexit take an additional parameter. 563490287Sobrien We'll just ignore it. After we implement the new calling 563590287Sobrien convention for destructors, we can eliminate the use of 563690287Sobrien additional cleanup functions entirely in the -fnew-abi case. */ 563790287Sobrien if (flag_use_cxa_atexit) 563890287Sobrien parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes); 563990287Sobrien /* Build the function type itself. */ 564090287Sobrien fntype = build_function_type (void_type_node, parmtypes); 564190287Sobrien /* Build the name of the function. */ 5642132747Skan sprintf (name, "__tcf_%d", start_cleanup_cnt++); 564390287Sobrien /* Build the function declaration. */ 564490287Sobrien fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); 564590287Sobrien /* It's a function with internal linkage, generated by the 564690287Sobrien compiler. */ 564790287Sobrien TREE_PUBLIC (fndecl) = 0; 564890287Sobrien DECL_ARTIFICIAL (fndecl) = 1; 564990287Sobrien /* Make the function `inline' so that it is only emitted if it is 565090287Sobrien actually needed. It is unlikely that it will be inlined, since 565190287Sobrien it is only called via a function pointer, but we avoid unnecessary 565290287Sobrien emissions this way. */ 565390287Sobrien DECL_INLINE (fndecl) = 1; 5654132747Skan DECL_DECLARED_INLINE_P (fndecl) = 1; 5655132747Skan DECL_INTERFACE_KNOWN (fndecl) = 1; 565690287Sobrien /* Build the parameter. */ 565790287Sobrien if (flag_use_cxa_atexit) 565818334Speter { 565990287Sobrien tree parmdecl; 566090287Sobrien 5661117410Skan parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node); 566290287Sobrien DECL_CONTEXT (parmdecl) = fndecl; 566390287Sobrien TREE_USED (parmdecl) = 1; 566490287Sobrien DECL_ARGUMENTS (fndecl) = parmdecl; 566518334Speter } 566618334Speter 566790287Sobrien pushdecl (fndecl); 5668169699Skan start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED); 566990287Sobrien 567090287Sobrien pop_lang_context (); 567190287Sobrien 567290287Sobrien return current_function_decl; 567318334Speter} 567418334Speter 567590287Sobrien/* Finish the cleanup function begun by start_cleanup_fn. */ 567651412Sobrien 567790287Sobrienstatic void 5678132747Skanend_cleanup_fn (void) 567990287Sobrien{ 5680132747Skan expand_or_defer_fn (finish_function (0)); 568190287Sobrien 568290287Sobrien pop_from_top_level (); 568390287Sobrien} 568490287Sobrien 568590287Sobrien/* Generate code to handle the destruction of DECL, an object with 568690287Sobrien static storage duration. */ 568790287Sobrien 5688169699Skantree 5689132747Skanregister_dtor_fn (tree decl) 569018334Speter{ 569190287Sobrien tree cleanup; 569290287Sobrien tree compound_stmt; 569390287Sobrien tree args; 569490287Sobrien tree fcall; 569590287Sobrien 569690287Sobrien if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 5697169699Skan return void_zero_node; 569890287Sobrien 569990287Sobrien /* Call build_cleanup before we enter the anonymous function so that 570090287Sobrien any access checks will be done relative to the current scope, 570190287Sobrien rather than the scope of the anonymous function. */ 570290287Sobrien build_cleanup (decl); 570390287Sobrien 570490287Sobrien /* Now start the function. */ 570590287Sobrien cleanup = start_cleanup_fn (); 570690287Sobrien 570790287Sobrien /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer 570890287Sobrien to the original function, rather than the anonymous one. That 570990287Sobrien will make the back-end think that nested functions are in use, 571090287Sobrien which causes confusion. */ 5711169699Skan 5712132747Skan push_deferring_access_checks (dk_no_check); 571390287Sobrien fcall = build_cleanup (decl); 5714132747Skan pop_deferring_access_checks (); 571590287Sobrien 571690287Sobrien /* Create the body of the anonymous function. */ 5717169699Skan compound_stmt = begin_compound_stmt (BCS_FN_BODY); 571890287Sobrien finish_expr_stmt (fcall); 5719132747Skan finish_compound_stmt (compound_stmt); 572090287Sobrien end_cleanup_fn (); 572190287Sobrien 572290287Sobrien /* Call atexit with the cleanup function. */ 5723117410Skan cxx_mark_addressable (cleanup); 5724132747Skan mark_used (cleanup); 572590287Sobrien cleanup = build_unary_op (ADDR_EXPR, cleanup, 0); 572690287Sobrien if (flag_use_cxa_atexit) 572790287Sobrien { 5728169699Skan args = tree_cons (NULL_TREE, 572996292Sobrien build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0), 573096292Sobrien NULL_TREE); 5731169699Skan if (targetm.cxx.use_aeabi_atexit ()) 5732169699Skan { 5733169699Skan args = tree_cons (NULL_TREE, cleanup, args); 5734169699Skan args = tree_cons (NULL_TREE, null_pointer_node, args); 5735169699Skan } 5736169699Skan else 5737169699Skan { 5738169699Skan args = tree_cons (NULL_TREE, null_pointer_node, args); 5739169699Skan args = tree_cons (NULL_TREE, cleanup, args); 5740169699Skan } 574190287Sobrien } 574290287Sobrien else 574390287Sobrien args = tree_cons (NULL_TREE, cleanup, NULL_TREE); 5744169699Skan return build_function_call (get_atexit_node (), args); 574518334Speter} 574618334Speter 5747117410Skan/* DECL is a VAR_DECL with static storage duration. INIT, if present, 5748117410Skan is its initializer. Generate code to handle the construction 5749117410Skan and destruction of DECL. */ 5750117410Skan 5751117410Skanstatic void 5752132747Skanexpand_static_init (tree decl, tree init) 575318334Speter{ 5754169699Skan gcc_assert (TREE_CODE (decl) == VAR_DECL); 5755169699Skan gcc_assert (TREE_STATIC (decl)); 5756117410Skan 5757117410Skan /* Some variables require no initialization. */ 5758169699Skan if (!init 5759117410Skan && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) 5760117410Skan && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 5761117410Skan return; 5762117410Skan 5763169699Skan if (DECL_FUNCTION_SCOPE_P (decl)) 576418334Speter { 576518334Speter /* Emit code to perform this initialization but once. */ 5766169699Skan tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE; 5767169699Skan tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE; 5768169699Skan tree guard, guard_addr, guard_addr_list; 5769169699Skan tree acquire_fn, release_fn, abort_fn; 5770169699Skan tree flag, begin; 577118334Speter 577252290Sobrien /* Emit code to perform this initialization but once. This code 577352290Sobrien looks like: 577452290Sobrien 5775169699Skan static <type> guard; 5776169699Skan if (!guard.first_byte) { 5777169699Skan if (__cxa_guard_acquire (&guard)) { 5778169699Skan bool flag = false; 5779169699Skan try { 5780169699Skan // Do initialization. 5781169699Skan flag = true; __cxa_guard_release (&guard); 5782169699Skan // Register variable for destruction at end of program. 5783169699Skan } catch { 5784169699Skan if (!flag) __cxa_guard_abort (&guard); 5785169699Skan } 578652290Sobrien } 578752290Sobrien 5788169699Skan Note that the `flag' variable is only set to 1 *after* the 578952290Sobrien initialization is complete. This ensures that an exception, 579052290Sobrien thrown during the construction, will cause the variable to 579152290Sobrien reinitialized when we pass through this code again, as per: 579290287Sobrien 579352290Sobrien [stmt.dcl] 579452290Sobrien 579552290Sobrien If the initialization exits by throwing an exception, the 579652290Sobrien initialization is not complete, so it will be tried again 579752290Sobrien the next time control enters the declaration. 579852290Sobrien 5799169699Skan This process should be thread-safe, too; multiple threads 5800169699Skan should not be able to initialize the variable more than 5801169699Skan once. */ 580252290Sobrien 580390287Sobrien /* Create the guard variable. */ 580490287Sobrien guard = get_guard (decl); 580590287Sobrien 5806169699Skan /* This optimization isn't safe on targets with relaxed memory 5807169699Skan consistency. On such targets we force synchronization in 5808169699Skan __cxa_guard_acquire. */ 5809169699Skan if (!targetm.relaxed_ordering || !flag_threadsafe_statics) 5810169699Skan { 5811169699Skan /* Begin the conditional initialization. */ 5812169699Skan if_stmt = begin_if_stmt (); 5813169699Skan finish_if_stmt_cond (get_guard_cond (guard), if_stmt); 5814169699Skan then_clause = begin_compound_stmt (BCS_NO_SCOPE); 5815169699Skan } 581651412Sobrien 5817169699Skan if (flag_threadsafe_statics) 5818169699Skan { 5819169699Skan guard_addr = build_address (guard); 5820169699Skan guard_addr_list = build_tree_list (NULL_TREE, guard_addr); 582190287Sobrien 5822169699Skan acquire_fn = get_identifier ("__cxa_guard_acquire"); 5823169699Skan release_fn = get_identifier ("__cxa_guard_release"); 5824169699Skan abort_fn = get_identifier ("__cxa_guard_abort"); 5825169699Skan if (!get_global_value_if_present (acquire_fn, &acquire_fn)) 5826169699Skan { 5827169699Skan tree argtypes = tree_cons (NULL_TREE, TREE_TYPE (guard_addr), 5828169699Skan void_list_node); 5829169699Skan tree vfntype = build_function_type (void_type_node, argtypes); 5830169699Skan acquire_fn = push_library_fn 5831169699Skan (acquire_fn, build_function_type (integer_type_node, argtypes)); 5832169699Skan release_fn = push_library_fn (release_fn, vfntype); 5833169699Skan abort_fn = push_library_fn (abort_fn, vfntype); 5834169699Skan } 5835169699Skan else 5836169699Skan { 5837169699Skan release_fn = identifier_global_value (release_fn); 5838169699Skan abort_fn = identifier_global_value (abort_fn); 5839169699Skan } 5840169699Skan 5841169699Skan inner_if_stmt = begin_if_stmt (); 5842169699Skan finish_if_stmt_cond (build_call (acquire_fn, guard_addr_list), 5843169699Skan inner_if_stmt); 5844169699Skan 5845169699Skan inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE); 5846169699Skan begin = get_target_expr (boolean_false_node); 5847169699Skan flag = TARGET_EXPR_SLOT (begin); 5848169699Skan 5849169699Skan TARGET_EXPR_CLEANUP (begin) 5850169699Skan = build3 (COND_EXPR, void_type_node, flag, 5851169699Skan void_zero_node, 5852169699Skan build_call (abort_fn, guard_addr_list)); 5853169699Skan CLEANUP_EH_ONLY (begin) = 1; 5854169699Skan 5855169699Skan /* Do the initialization itself. */ 5856169699Skan init = add_stmt_to_compound (begin, init); 5857169699Skan init = add_stmt_to_compound 5858169699Skan (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node)); 5859169699Skan init = add_stmt_to_compound 5860169699Skan (init, build_call (release_fn, guard_addr_list)); 5861169699Skan } 586290287Sobrien else 5863169699Skan init = add_stmt_to_compound (init, set_guard (guard)); 586418334Speter 586552290Sobrien /* Use atexit to register a function for destroying this static 586652290Sobrien variable. */ 5867169699Skan init = add_stmt_to_compound (init, register_dtor_fn (decl)); 586852290Sobrien 5869169699Skan finish_expr_stmt (init); 587051412Sobrien 5871169699Skan if (flag_threadsafe_statics) 5872169699Skan { 5873169699Skan finish_compound_stmt (inner_then_clause); 5874169699Skan finish_then_clause (inner_if_stmt); 5875169699Skan finish_if_stmt (inner_if_stmt); 5876169699Skan } 587751412Sobrien 5878169699Skan if (!targetm.relaxed_ordering || !flag_threadsafe_statics) 5879169699Skan { 5880169699Skan finish_compound_stmt (then_clause); 5881169699Skan finish_then_clause (if_stmt); 5882169699Skan finish_if_stmt (if_stmt); 5883169699Skan } 588418334Speter } 588518334Speter else 5886169699Skan static_aggregates = tree_cons (init, decl, static_aggregates); 588718334Speter} 588890287Sobrien 588918334Speter 589018334Speter/* Make TYPE a complete type based on INITIAL_VALUE. 589118334Speter Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 5892169699Skan 2 if there was no information (in which case assume 0 if DO_DEFAULT), 5893169699Skan 3 if the initializer list is empty (in pedantic mode). */ 589418334Speter 589518334Speterint 5896169699Skancp_complete_array_type (tree *ptype, tree initial_value, bool do_default) 589718334Speter{ 5898169699Skan int failure; 5899169699Skan tree type, elt_type; 590090287Sobrien 590118334Speter if (initial_value) 590218334Speter { 590390287Sobrien /* An array of character type can be initialized from a 5904169699Skan brace-enclosed string constant. 5905169699Skan 5906169699Skan FIXME: this code is duplicated from reshape_init. Probably 5907169699Skan we should just call reshape_init here? */ 5908169699Skan if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype))) 590990287Sobrien && TREE_CODE (initial_value) == CONSTRUCTOR 5910169699Skan && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value))) 591118334Speter { 5912169699Skan VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value); 5913169699Skan tree value = VEC_index (constructor_elt, v, 0)->value; 591490287Sobrien 5915169699Skan if (TREE_CODE (value) == STRING_CST 5916169699Skan && VEC_length (constructor_elt, v) == 1) 5917169699Skan initial_value = value; 591818334Speter } 591918334Speter } 592018334Speter 5921169699Skan failure = complete_array_type (ptype, initial_value, do_default); 592218334Speter 5923169699Skan /* We can create the array before the element type is complete, which 5924169699Skan means that we didn't have these two bits set in the original type 5925169699Skan either. In completing the type, we are expected to propagate these 5926169699Skan bits. See also complete_type which does the same thing for arrays 5927169699Skan of fixed size. */ 5928169699Skan type = *ptype; 5929169699Skan if (TYPE_DOMAIN (type)) 593018334Speter { 5931169699Skan elt_type = TREE_TYPE (type); 5932169699Skan TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type); 5933169699Skan TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 5934169699Skan = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type); 593518334Speter } 593618334Speter 5937169699Skan return failure; 593818334Speter} 593918334Speter 594018334Speter/* Return zero if something is declared to be a member of type 594118334Speter CTYPE when in the context of CUR_TYPE. STRING is the error 594218334Speter message to print in that case. Otherwise, quietly return 1. */ 594351412Sobrien 594418334Speterstatic int 5945132747Skanmember_function_or_else (tree ctype, tree cur_type, enum overload_flags flags) 594618334Speter{ 594718334Speter if (ctype && ctype != cur_type) 594818334Speter { 594990287Sobrien if (flags == DTOR_FLAG) 5950169699Skan error ("destructor for alien class %qT cannot be a member", ctype); 595190287Sobrien else 5952169699Skan error ("constructor for alien class %qT cannot be a member", ctype); 595318334Speter return 0; 595418334Speter } 595518334Speter return 1; 595618334Speter} 595718334Speter 595818334Speter/* Subroutine of `grokdeclarator'. */ 595918334Speter 596018334Speter/* Generate errors possibly applicable for a given set of specifiers. 596118334Speter This is for ARM $7.1.2. */ 596251412Sobrien 596318334Speterstatic void 5964132747Skanbad_specifiers (tree object, 5965169699Skan const char* type, 5966169699Skan int virtualp, 5967169699Skan int quals, 5968169699Skan int inlinep, 5969169699Skan int friendp, 5970169699Skan int raises) 597118334Speter{ 597218334Speter if (virtualp) 5973169699Skan error ("%qD declared as a %<virtual%> %s", object, type); 597418334Speter if (inlinep) 5975169699Skan error ("%qD declared as an %<inline%> %s", object, type); 597618334Speter if (quals) 5977169699Skan error ("%<const%> and %<volatile%> function specifiers on " 5978169699Skan "%qD invalid in %s declaration", 5979169699Skan object, type); 598018334Speter if (friendp) 5981169699Skan error ("%q+D declared as a friend", object); 598290287Sobrien if (raises 598390287Sobrien && (TREE_CODE (object) == TYPE_DECL 598490287Sobrien || (!TYPE_PTRFN_P (TREE_TYPE (object)) 598590287Sobrien && !TYPE_REFFN_P (TREE_TYPE (object)) 598690287Sobrien && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) 5987169699Skan error ("%q+D declared with an exception specification", object); 598818334Speter} 598918334Speter 5990169699Skan/* DECL is a member function or static data member and is presently 5991169699Skan being defined. Check that the definition is taking place in a 5992169699Skan valid namespace. */ 5993169699Skan 5994169699Skanstatic void 5995169699Skancheck_class_member_definition_namespace (tree decl) 5996169699Skan{ 5997169699Skan /* These checks only apply to member functions and static data 5998169699Skan members. */ 5999169699Skan gcc_assert (TREE_CODE (decl) == FUNCTION_DECL 6000169699Skan || TREE_CODE (decl) == VAR_DECL); 6001169699Skan /* We check for problems with specializations in pt.c in 6002169699Skan check_specialization_namespace, where we can issue better 6003169699Skan diagnostics. */ 6004169699Skan if (processing_specialization) 6005169699Skan return; 6006169699Skan /* There are no restrictions on the placement of 6007169699Skan explicit instantiations. */ 6008169699Skan if (processing_explicit_instantiation) 6009169699Skan return; 6010169699Skan /* [class.mfct] 6011169699Skan 6012169699Skan A member function definition that appears outside of the 6013169699Skan class definition shall appear in a namespace scope enclosing 6014169699Skan the class definition. 6015169699Skan 6016169699Skan [class.static.data] 6017169699Skan 6018169699Skan The definition for a static data member shall appear in a 6019169699Skan namespace scope enclosing the member's class definition. */ 6020169699Skan if (!is_ancestor (current_namespace, DECL_CONTEXT (decl))) 6021169699Skan pedwarn ("definition of %qD is not in namespace enclosing %qT", 6022169699Skan decl, DECL_CONTEXT (decl)); 6023169699Skan} 6024169699Skan 6025169699Skan/* Build a PARM_DECL for the "this" parameter. TYPE is the 6026169699Skan METHOD_TYPE for a non-static member function; QUALS are the 6027169699Skan cv-qualifiers that apply to the function. */ 6028169699Skan 6029169699Skantree 6030169699Skanbuild_this_parm (tree type, cp_cv_quals quals) 6031169699Skan{ 6032169699Skan tree this_type; 6033169699Skan tree qual_type; 6034169699Skan tree parm; 6035169699Skan cp_cv_quals this_quals; 6036169699Skan 6037169699Skan this_type = TREE_VALUE (TYPE_ARG_TYPES (type)); 6038169699Skan /* The `this' parameter is implicitly `const'; it cannot be 6039169699Skan assigned to. */ 6040169699Skan this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST; 6041169699Skan qual_type = cp_build_qualified_type (this_type, this_quals); 6042169699Skan parm = build_artificial_parm (this_identifier, qual_type); 6043169699Skan cp_apply_type_quals_to_decl (this_quals, parm); 6044169699Skan return parm; 6045169699Skan} 6046169699Skan 604718334Speter/* CTYPE is class type, or null if non-class. 604818334Speter TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE 604918334Speter or METHOD_TYPE. 605018334Speter DECLARATOR is the function's name. 6051132747Skan PARMS is a chain of PARM_DECLs for the function. 605218334Speter VIRTUALP is truthvalue of whether the function is virtual or not. 605318334Speter FLAGS are to be passed through to `grokclassfn'. 605418334Speter QUALS are qualifiers indicating whether the function is `const' 605518334Speter or `volatile'. 605618334Speter RAISES is a list of exceptions that this function can raise. 605718334Speter CHECK is 1 if we must find this method in CTYPE, 0 if we should 605890287Sobrien not look, and -1 if we should not call `grokclassfn' at all. 605951412Sobrien 6060169699Skan SFK is the kind of special function (if any) for the new function. 6061169699Skan 606252290Sobrien Returns `NULL_TREE' if something goes wrong, after issuing 606352290Sobrien applicable error messages. */ 606452290Sobrien 606518334Speterstatic tree 6066169699Skangrokfndecl (tree ctype, 6067169699Skan tree type, 6068169699Skan tree declarator, 6069132747Skan tree parms, 6070169699Skan tree orig_declarator, 6071169699Skan int virtualp, 6072169699Skan enum overload_flags flags, 6073169699Skan cp_cv_quals quals, 6074169699Skan tree raises, 6075169699Skan int check, 6076169699Skan int friendp, 6077169699Skan int publicp, 6078169699Skan int inlinep, 6079169699Skan special_function_kind sfk, 6080169699Skan bool funcdef_flag, 6081169699Skan int template_count, 6082169699Skan tree in_namespace, 6083169699Skan tree* attrlist) 608418334Speter{ 608590287Sobrien tree decl; 608618334Speter int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; 608751412Sobrien tree t; 608818334Speter 608918334Speter if (raises) 609090287Sobrien type = build_exception_variant (type, raises); 609151412Sobrien 609218334Speter decl = build_lang_decl (FUNCTION_DECL, declarator, type); 6093169699Skan if (TREE_CODE (type) == METHOD_TYPE) 6094169699Skan { 6095169699Skan tree parm; 6096169699Skan parm = build_this_parm (type, quals); 6097169699Skan TREE_CHAIN (parm) = parms; 6098169699Skan parms = parm; 6099169699Skan } 6100132747Skan DECL_ARGUMENTS (decl) = parms; 6101117410Skan /* Propagate volatile out from type to decl. */ 610218334Speter if (TYPE_VOLATILE (type)) 610351412Sobrien TREE_THIS_VOLATILE (decl) = 1; 610418334Speter 6105259705Spfg /* If pointers to member functions use the least significant bit to 6106259705Spfg indicate whether a function is virtual, ensure a pointer 6107259705Spfg to this function will have that bit clear. */ 6108259705Spfg if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn 6109259705Spfg && TREE_CODE (type) == METHOD_TYPE 6110259705Spfg && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT) 6111259705Spfg DECL_ALIGN (decl) = 2 * BITS_PER_UNIT; 6112259705Spfg 6113169699Skan if (friendp 6114169699Skan && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) 6115169699Skan { 6116169699Skan if (funcdef_flag) 6117169699Skan error 6118169699Skan ("defining explicit specialization %qD in friend declaration", 6119169699Skan orig_declarator); 6120169699Skan else 6121169699Skan { 6122169699Skan tree fns = TREE_OPERAND (orig_declarator, 0); 6123169699Skan tree args = TREE_OPERAND (orig_declarator, 1); 6124169699Skan 6125169699Skan if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 6126169699Skan { 6127169699Skan /* Something like `template <class T> friend void f<T>()'. */ 6128169699Skan error ("invalid use of template-id %qD in declaration " 6129169699Skan "of primary template", 6130169699Skan orig_declarator); 6131169699Skan return NULL_TREE; 6132169699Skan } 6133169699Skan 6134169699Skan 6135169699Skan /* A friend declaration of the form friend void f<>(). Record 6136169699Skan the information in the TEMPLATE_ID_EXPR. */ 6137169699Skan SET_DECL_IMPLICIT_INSTANTIATION (decl); 6138169699Skan 6139169699Skan if (TREE_CODE (fns) == COMPONENT_REF) 6140169699Skan { 6141169699Skan /* Due to bison parser ickiness, we will have already looked 6142169699Skan up an operator_name or PFUNCNAME within the current class 6143169699Skan (see template_id in parse.y). If the current class contains 6144169699Skan such a name, we'll get a COMPONENT_REF here. Undo that. */ 6145169699Skan 6146169699Skan gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0)) 6147169699Skan == current_class_type); 6148169699Skan fns = TREE_OPERAND (fns, 1); 6149169699Skan } 6150169699Skan gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE 6151169699Skan || TREE_CODE (fns) == OVERLOAD); 6152169699Skan DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE); 6153169699Skan 6154169699Skan for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) 6155169699Skan if (TREE_PURPOSE (t) 6156169699Skan && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) 6157169699Skan { 6158169699Skan error ("default arguments are not allowed in declaration " 6159169699Skan "of friend template specialization %qD", 6160169699Skan decl); 6161169699Skan return NULL_TREE; 6162169699Skan } 6163169699Skan 6164169699Skan if (inlinep) 6165169699Skan { 6166169699Skan error ("%<inline%> is not allowed in declaration of friend " 6167169699Skan "template specialization %qD", 6168169699Skan decl); 6169169699Skan return NULL_TREE; 6170169699Skan } 6171169699Skan } 6172169699Skan } 6173169699Skan 617452290Sobrien /* If this decl has namespace scope, set that up. */ 617551412Sobrien if (in_namespace) 617652290Sobrien set_decl_namespace (decl, in_namespace, friendp); 617790287Sobrien else if (!ctype) 617852290Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 617951412Sobrien 618052290Sobrien /* `main' and builtins have implicit 'C' linkage. */ 618152290Sobrien if ((MAIN_NAME_P (declarator) 618252290Sobrien || (IDENTIFIER_LENGTH (declarator) > 10 618352290Sobrien && IDENTIFIER_POINTER (declarator)[0] == '_' 618452290Sobrien && IDENTIFIER_POINTER (declarator)[1] == '_' 618552290Sobrien && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)) 618652290Sobrien && current_lang_name == lang_name_cplusplus 618752290Sobrien && ctype == NULL_TREE 618852290Sobrien /* NULL_TREE means global namespace. */ 618952290Sobrien && DECL_CONTEXT (decl) == NULL_TREE) 619090287Sobrien SET_DECL_LANGUAGE (decl, lang_c); 619152290Sobrien 619218334Speter /* Should probably propagate const out from type to decl I bet (mrs). */ 619318334Speter if (staticp) 619418334Speter { 619518334Speter DECL_STATIC_FUNCTION_P (decl) = 1; 619618334Speter DECL_CONTEXT (decl) = ctype; 619718334Speter } 619818334Speter 619951412Sobrien if (ctype) 6200169699Skan { 6201169699Skan DECL_CONTEXT (decl) = ctype; 6202169699Skan if (funcdef_flag) 6203169699Skan check_class_member_definition_namespace (decl); 6204169699Skan } 620518334Speter 620652290Sobrien if (ctype == NULL_TREE && DECL_MAIN_P (decl)) 620718334Speter { 620852290Sobrien if (processing_template_decl) 6209169699Skan error ("cannot declare %<::main%> to be a template"); 621018334Speter if (inlinep) 6211169699Skan error ("cannot declare %<::main%> to be inline"); 621290287Sobrien if (!publicp) 6213169699Skan error ("cannot declare %<::main%> to be static"); 621418334Speter inlinep = 0; 621518334Speter publicp = 1; 621618334Speter } 621752290Sobrien 621852290Sobrien /* Members of anonymous types and local classes have no linkage; make 6219169699Skan them internal. If a typedef is made later, this will be changed. */ 622090287Sobrien if (ctype && (TYPE_ANONYMOUS_P (ctype) 622190287Sobrien || decl_function_context (TYPE_MAIN_DECL (ctype)))) 622252290Sobrien publicp = 0; 622352290Sobrien 622452290Sobrien if (publicp) 622552290Sobrien { 622652290Sobrien /* [basic.link]: A name with no linkage (notably, the name of a class 622752290Sobrien or enumeration declared in a local scope) shall not be used to 622852290Sobrien declare an entity with linkage. 622952290Sobrien 6230132747Skan Only check this for public decls for now. See core 319, 389. */ 6231169699Skan t = no_linkage_check (TREE_TYPE (decl), 6232169699Skan /*relaxed_p=*/false); 623352290Sobrien if (t) 623452290Sobrien { 623590287Sobrien if (TYPE_ANONYMOUS_P (t)) 623652290Sobrien { 623790287Sobrien if (DECL_EXTERN_C_P (decl)) 623852290Sobrien /* Allow this; it's pretty common in C. */; 623952290Sobrien else 624090287Sobrien { 6241169699Skan pedwarn ("non-local function %q#D uses anonymous type", 624290287Sobrien decl); 624390287Sobrien if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) 6244169699Skan pedwarn ("%q+#D does not refer to the unqualified " 6245169699Skan "type, so it is not used for linkage", 6246169699Skan TYPE_NAME (t)); 624790287Sobrien } 624852290Sobrien } 624952290Sobrien else 6250169699Skan pedwarn ("non-local function %q#D uses local type %qT", decl, t); 625152290Sobrien } 625252290Sobrien } 625352290Sobrien 625451412Sobrien TREE_PUBLIC (decl) = publicp; 625518334Speter if (! publicp) 625651412Sobrien { 625751412Sobrien DECL_INTERFACE_KNOWN (decl) = 1; 625851412Sobrien DECL_NOT_REALLY_EXTERN (decl) = 1; 625951412Sobrien } 626018334Speter 626190287Sobrien /* If the declaration was declared inline, mark it as such. */ 626218334Speter if (inlinep) 626390287Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 626490287Sobrien /* We inline functions that are explicitly declared inline, or, when 626590287Sobrien the user explicitly asks us to, all functions. */ 6266132747Skan if (DECL_DECLARED_INLINE_P (decl) 6267132747Skan || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag)) 626890287Sobrien DECL_INLINE (decl) = 1; 626918334Speter 627018334Speter DECL_EXTERNAL (decl) = 1; 6271169699Skan if (quals && TREE_CODE (type) == FUNCTION_TYPE) 627218334Speter { 6273169699Skan error ("%smember function %qD cannot have cv-qualifier", 6274169699Skan (ctype ? "static " : "non-"), decl); 6275169699Skan quals = TYPE_UNQUALIFIED; 627618334Speter } 627718334Speter 6278169699Skan if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)) 6279169699Skan && !grok_op_properties (decl, /*complain=*/true)) 6280169699Skan return NULL_TREE; 628118334Speter 628290287Sobrien if (ctype && decl_function_context (decl)) 628351412Sobrien DECL_NO_STATIC_CHAIN (decl) = 1; 628451412Sobrien 628552290Sobrien if (funcdef_flag) 628652290Sobrien /* Make the init_value nonzero so pushdecl knows this is not 628752290Sobrien tentative. error_mark_node is replaced later with the BLOCK. */ 628852290Sobrien DECL_INITIAL (decl) = error_mark_node; 628952290Sobrien 629090287Sobrien if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) 629190287Sobrien TREE_NOTHROW (decl) = 1; 629290287Sobrien 629318334Speter /* Caller will do the rest of this. */ 629418334Speter if (check < 0) 629518334Speter return decl; 629618334Speter 629790287Sobrien if (ctype != NULL_TREE) 6298169699Skan { 6299169699Skan if (sfk == sfk_constructor) 6300169699Skan DECL_CONSTRUCTOR_P (decl) = 1; 630118334Speter 6302169699Skan grokclassfn (ctype, decl, flags); 6303169699Skan } 6304169699Skan 630590287Sobrien decl = check_explicit_specialization (orig_declarator, decl, 630690287Sobrien template_count, 6307169699Skan 2 * funcdef_flag + 630890287Sobrien 4 * (friendp != 0)); 630990287Sobrien if (decl == error_mark_node) 631090287Sobrien return NULL_TREE; 631151412Sobrien 6312169699Skan if (attrlist) 6313169699Skan { 6314169699Skan cplus_decl_attributes (&decl, *attrlist, 0); 6315169699Skan *attrlist = NULL_TREE; 6316169699Skan } 6317169699Skan 6318169699Skan /* Check main's type after attributes have been applied. */ 6319169699Skan if (ctype == NULL_TREE && DECL_MAIN_P (decl) 6320169699Skan && !same_type_p (TREE_TYPE (TREE_TYPE (decl)), 6321169699Skan integer_type_node)) 6322169699Skan { 6323169699Skan tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl)); 6324169699Skan tree newtype; 6325169699Skan error ("%<::main%> must return %<int%>"); 6326169699Skan newtype = build_function_type (integer_type_node, oldtypeargs); 6327169699Skan TREE_TYPE (decl) = newtype; 6328169699Skan } 6329169699Skan 633090287Sobrien if (ctype != NULL_TREE 633190287Sobrien && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl)) 633290287Sobrien && check) 633390287Sobrien { 633490287Sobrien tree old_decl; 633570639Sobrien 6336132747Skan old_decl = check_classfn (ctype, decl, 6337169699Skan (processing_template_decl 6338169699Skan > template_class_depth (ctype)) 6339169699Skan ? current_template_parms 6340169699Skan : NULL_TREE); 634190287Sobrien if (old_decl) 634290287Sobrien { 6343132747Skan tree ok; 6344169699Skan tree pushed_scope; 6345132747Skan 6346169699Skan if (TREE_CODE (old_decl) == TEMPLATE_DECL) 6347169699Skan /* Because grokfndecl is always supposed to return a 6348169699Skan FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT 6349169699Skan here. We depend on our callers to figure out that its 6350169699Skan really a template that's being returned. */ 6351169699Skan old_decl = DECL_TEMPLATE_RESULT (old_decl); 6352169699Skan 6353169699Skan if (DECL_STATIC_FUNCTION_P (old_decl) 6354169699Skan && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 6355169699Skan /* Remove the `this' parm added by grokclassfn. 6356169699Skan XXX Isn't this done in start_function, too? */ 6357169699Skan revert_static_member_fn (decl); 6358169699Skan if (DECL_ARTIFICIAL (old_decl)) 6359169699Skan error ("definition of implicitly-declared %qD", old_decl); 6360169699Skan 636190287Sobrien /* Since we've smashed OLD_DECL to its 636290287Sobrien DECL_TEMPLATE_RESULT, we must do the same to DECL. */ 636390287Sobrien if (TREE_CODE (decl) == TEMPLATE_DECL) 636490287Sobrien decl = DECL_TEMPLATE_RESULT (decl); 636551412Sobrien 636690287Sobrien /* Attempt to merge the declarations. This can fail, in 6367117410Skan the case of some invalid specialization declarations. */ 6368169699Skan pushed_scope = push_scope (ctype); 6369169699Skan ok = duplicate_decls (decl, old_decl, friendp); 6370169699Skan if (pushed_scope) 6371169699Skan pop_scope (pushed_scope); 6372132747Skan if (!ok) 6373132747Skan { 6374169699Skan error ("no %q#D member function declared in class %qT", 6375132747Skan decl, ctype); 6376132747Skan return NULL_TREE; 6377132747Skan } 637890287Sobrien return old_decl; 637951412Sobrien } 638018334Speter } 638118334Speter 638290287Sobrien if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) 638390287Sobrien return NULL_TREE; 638418334Speter 638590287Sobrien if (ctype == NULL_TREE || check) 638690287Sobrien return decl; 638752290Sobrien 638890287Sobrien if (virtualp) 638990287Sobrien DECL_VIRTUAL_P (decl) = 1; 639018334Speter 639118334Speter return decl; 639218334Speter} 639318334Speter 6394169699Skan/* DECL is a VAR_DECL for a static data member. Set flags to reflect 6395169699Skan the linkage that DECL will receive in the object file. */ 6396117410Skan 6397169699Skanstatic void 6398169699Skanset_linkage_for_static_data_member (tree decl) 6399169699Skan{ 6400169699Skan /* A static data member always has static storage duration and 6401169699Skan external linkage. Note that static data members are forbidden in 6402169699Skan local classes -- the only situation in which a class has 6403169699Skan non-external linkage. */ 6404169699Skan TREE_PUBLIC (decl) = 1; 6405169699Skan TREE_STATIC (decl) = 1; 6406169699Skan /* For non-template classes, static data members are always put 6407169699Skan out in exactly those files where they are defined, just as 6408169699Skan with ordinary namespace-scope variables. */ 6409169699Skan if (!processing_template_decl) 6410169699Skan DECL_INTERFACE_KNOWN (decl) = 1; 6411169699Skan} 6412169699Skan 6413169699Skan/* Create a VAR_DECL named NAME with the indicated TYPE. 6414169699Skan 6415117410Skan If SCOPE is non-NULL, it is the class type or namespace containing 6416117410Skan the variable. If SCOPE is NULL, the variable should is created in 6417117410Skan the innermost enclosings scope. */ 6418117410Skan 641918334Speterstatic tree 6420132747Skangrokvardecl (tree type, 6421169699Skan tree name, 6422169699Skan const cp_decl_specifier_seq *declspecs, 6423169699Skan int initialized, 6424169699Skan int constp, 6425169699Skan tree scope) 642618334Speter{ 642718334Speter tree decl; 6428161660Skan tree explicit_scope; 642918334Speter 6430169699Skan gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE); 6431117410Skan 6432161660Skan /* Compute the scope in which to place the variable, but remember 6433169699Skan whether or not that scope was explicitly specified by the user. */ 6434161660Skan explicit_scope = scope; 6435117410Skan if (!scope) 643618334Speter { 6437117410Skan /* An explicit "extern" specifier indicates a namespace-scope 6438117410Skan variable. */ 6439169699Skan if (declspecs->storage_class == sc_extern) 6440117410Skan scope = current_namespace; 6441117410Skan else if (!at_function_scope_p ()) 6442169699Skan scope = current_scope (); 644318334Speter } 6444117410Skan 6445117410Skan if (scope 6446117410Skan && (/* If the variable is a namespace-scope variable declared in a 6447117410Skan template, we need DECL_LANG_SPECIFIC. */ 6448117410Skan (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl) 6449117410Skan /* Similarly for namespace-scope variables with language linkage 6450117410Skan other than C++. */ 6451169699Skan || (TREE_CODE (scope) == NAMESPACE_DECL 6452117410Skan && current_lang_name != lang_name_cplusplus) 6453117410Skan /* Similarly for static data members. */ 6454117410Skan || TYPE_P (scope))) 6455117410Skan decl = build_lang_decl (VAR_DECL, name, type); 645618334Speter else 6457117410Skan decl = build_decl (VAR_DECL, name, type); 645852290Sobrien 6459161660Skan if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL) 6460161660Skan set_decl_namespace (decl, explicit_scope, 0); 6461117410Skan else 6462169699Skan DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 646352290Sobrien 6464169699Skan if (declspecs->storage_class == sc_extern) 646518334Speter { 646618334Speter DECL_THIS_EXTERN (decl) = 1; 646718334Speter DECL_EXTERNAL (decl) = !initialized; 646818334Speter } 646918334Speter 647051412Sobrien if (DECL_CLASS_SCOPE_P (decl)) 647118334Speter { 6472169699Skan set_linkage_for_static_data_member (decl); 6473169699Skan /* This function is only called with out-of-class definitions. */ 647418334Speter DECL_EXTERNAL (decl) = 0; 6475169699Skan check_class_member_definition_namespace (decl); 647618334Speter } 647718334Speter /* At top level, either `static' or no s.c. makes a definition 647818334Speter (perhaps tentative), and absence of `static' makes it public. */ 647918334Speter else if (toplevel_bindings_p ()) 648018334Speter { 6481169699Skan TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static 648218334Speter && (DECL_THIS_EXTERN (decl) || ! constp)); 648318334Speter TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); 648418334Speter } 648518334Speter /* Not at top level, only `static' makes a static definition. */ 648618334Speter else 648718334Speter { 6488169699Skan TREE_STATIC (decl) = declspecs->storage_class == sc_static; 648918334Speter TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); 649018334Speter } 649152290Sobrien 6492169699Skan if (declspecs->specs[(int)ds_thread]) 6493117410Skan { 6494117410Skan if (targetm.have_tls) 6495169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 6496117410Skan else 6497117410Skan /* A mere warning is sure to result in improper semantics 6498117410Skan at runtime. Don't bother to allow this to compile. */ 6499117410Skan error ("thread-local storage not supported for this target"); 6500117410Skan } 6501117410Skan 650252290Sobrien if (TREE_PUBLIC (decl)) 650352290Sobrien { 650452290Sobrien /* [basic.link]: A name with no linkage (notably, the name of a class 650552290Sobrien or enumeration declared in a local scope) shall not be used to 650652290Sobrien declare an entity with linkage. 650752290Sobrien 650852290Sobrien Only check this for public decls for now. */ 6509169699Skan tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false); 651052290Sobrien if (t) 651152290Sobrien { 651290287Sobrien if (TYPE_ANONYMOUS_P (t)) 6513169699Skan { 6514169699Skan if (DECL_EXTERN_C_P (decl)) 6515169699Skan /* Allow this; it's pretty common in C. */ 6516169699Skan ; 6517169699Skan else 6518169699Skan { 6519169699Skan /* DRs 132, 319 and 389 seem to indicate types with 6520169699Skan no linkage can only be used to declare extern "C" 6521169699Skan entities. Since it's not always an error in the 6522169699Skan ISO C++ 90 Standard, we only issue a warning. */ 6523169699Skan warning (0, "non-local variable %q#D uses anonymous type", 6524169699Skan decl); 6525169699Skan if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) 6526169699Skan warning (0, "%q+#D does not refer to the unqualified " 6527169699Skan "type, so it is not used for linkage", 6528169699Skan TYPE_NAME (t)); 6529169699Skan } 6530169699Skan } 653152290Sobrien else 6532169699Skan warning (0, "non-local variable %q#D uses local type %qT", decl, t); 653352290Sobrien } 653452290Sobrien } 6535169699Skan else 6536169699Skan DECL_INTERFACE_KNOWN (decl) = 1; 653752290Sobrien 653818334Speter return decl; 653918334Speter} 654018334Speter 654152290Sobrien/* Create and return a canonical pointer to member function type, for 654252290Sobrien TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ 654318334Speter 654418334Spetertree 6545117410Skanbuild_ptrmemfunc_type (tree type) 654618334Speter{ 6547132747Skan tree field, fields; 654818334Speter tree t; 654990287Sobrien tree unqualified_variant = NULL_TREE; 655018334Speter 655190287Sobrien if (type == error_mark_node) 655290287Sobrien return type; 655390287Sobrien 655418334Speter /* If a canonical type already exists for this type, use it. We use 655518334Speter this method instead of type_hash_canon, because it only does a 655618334Speter simple equality check on the list of field members. */ 655718334Speter 655818334Speter if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type))) 655918334Speter return t; 656018334Speter 656190287Sobrien /* Make sure that we always have the unqualified pointer-to-member 656290287Sobrien type first. */ 656390287Sobrien if (cp_type_quals (type) != TYPE_UNQUALIFIED) 656490287Sobrien unqualified_variant 656590287Sobrien = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); 656618334Speter 656790287Sobrien t = make_aggr_type (RECORD_TYPE); 6568169699Skan xref_basetypes (t, NULL_TREE); 6569169699Skan 657051412Sobrien /* Let the front-end know this is a pointer to member function... */ 657118334Speter TYPE_PTRMEMFUNC_FLAG (t) = 1; 657251412Sobrien /* ... and not really an aggregate. */ 657352290Sobrien SET_IS_AGGR_TYPE (t, 0); 657418334Speter 6575132747Skan field = build_decl (FIELD_DECL, pfn_identifier, type); 6576132747Skan fields = field; 6577169699Skan 6578132747Skan field = build_decl (FIELD_DECL, delta_identifier, delta_type_node); 6579132747Skan TREE_CHAIN (field) = fields; 6580132747Skan fields = field; 6581169699Skan 6582132747Skan finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node); 658318334Speter 658418334Speter /* Zap out the name so that the back-end will give us the debugging 658518334Speter information for this anonymous RECORD_TYPE. */ 658618334Speter TYPE_NAME (t) = NULL_TREE; 658718334Speter 658890287Sobrien /* If this is not the unqualified form of this pointer-to-member 658990287Sobrien type, set the TYPE_MAIN_VARIANT for this type to be the 659090287Sobrien unqualified type. Since they are actually RECORD_TYPEs that are 659190287Sobrien not variants of each other, we must do this manually. */ 659290287Sobrien if (cp_type_quals (type) != TYPE_UNQUALIFIED) 659390287Sobrien { 659490287Sobrien t = build_qualified_type (t, cp_type_quals (type)); 659590287Sobrien TYPE_MAIN_VARIANT (t) = unqualified_variant; 659690287Sobrien TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant); 659790287Sobrien TYPE_NEXT_VARIANT (unqualified_variant) = t; 659890287Sobrien } 659990287Sobrien 660090287Sobrien /* Cache this pointer-to-member type so that we can find it again 660190287Sobrien later. */ 660218334Speter TYPE_SET_PTRMEMFUNC_TYPE (type, t); 660318334Speter 660418334Speter return t; 660518334Speter} 660618334Speter 6607117410Skan/* Create and return a pointer to data member type. */ 6608117410Skan 6609117410Skantree 6610117410Skanbuild_ptrmem_type (tree class_type, tree member_type) 6611117410Skan{ 6612132747Skan if (TREE_CODE (member_type) == METHOD_TYPE) 6613132747Skan { 6614132747Skan tree arg_types; 6615132747Skan 6616132747Skan arg_types = TYPE_ARG_TYPES (member_type); 6617169699Skan class_type = (cp_build_qualified_type 6618132747Skan (class_type, 6619132747Skan cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types))))); 6620169699Skan member_type 6621169699Skan = build_method_type_directly (class_type, 6622132747Skan TREE_TYPE (member_type), 6623132747Skan TREE_CHAIN (arg_types)); 6624132747Skan return build_ptrmemfunc_type (build_pointer_type (member_type)); 6625132747Skan } 6626132747Skan else 6627132747Skan { 6628169699Skan gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE); 6629132747Skan return build_offset_type (class_type, member_type); 6630132747Skan } 6631117410Skan} 6632117410Skan 663352290Sobrien/* DECL is a VAR_DECL defined in-class, whose TYPE is also given. 663452290Sobrien Check to see that the definition is valid. Issue appropriate error 663552290Sobrien messages. Return 1 if the definition is particularly bad, or 0 663652290Sobrien otherwise. */ 663752290Sobrien 663852290Sobrienint 6639132747Skancheck_static_variable_definition (tree decl, tree type) 664052290Sobrien{ 664152290Sobrien /* Motion 10 at San Diego: If a static const integral data member is 664252290Sobrien initialized with an integral constant expression, the initializer 664352290Sobrien may appear either in the declaration (within the class), or in 664452290Sobrien the definition, but not both. If it appears in the class, the 664552290Sobrien member is a member constant. The file-scope definition is always 664652290Sobrien required. */ 6647110631Skan if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE) 664852290Sobrien { 6649169699Skan error ("invalid in-class initialization of static data member " 6650169699Skan "of non-integral type %qT", 6651110631Skan type); 665252290Sobrien /* If we just return the declaration, crashes will sometimes 6653169699Skan occur. We therefore return void_type_node, as if this were a 665452290Sobrien friend declaration, to cause callers to completely ignore 665552290Sobrien this declaration. */ 665652290Sobrien return 1; 665752290Sobrien } 665852290Sobrien else if (!CP_TYPE_CONST_P (type)) 6659169699Skan error ("ISO C++ forbids in-class initialization of non-const " 6660169699Skan "static member %qD", 6661169699Skan decl); 666252290Sobrien else if (pedantic && !INTEGRAL_TYPE_P (type)) 6663169699Skan pedwarn ("ISO C++ forbids initialization of member constant " 6664169699Skan "%qD of non-integral type %qT", decl, type); 666552290Sobrien 666652290Sobrien return 0; 666752290Sobrien} 666852290Sobrien 666990287Sobrien/* Given the SIZE (i.e., number of elements) in an array, compute an 667090287Sobrien appropriate index type for the array. If non-NULL, NAME is the 667190287Sobrien name of the thing being declared. */ 667290287Sobrien 667390287Sobrientree 6674132747Skancompute_array_index_type (tree name, tree size) 667590287Sobrien{ 6676169699Skan tree type; 667790287Sobrien tree itype; 667890287Sobrien 6679169699Skan if (error_operand_p (size)) 6680169699Skan return error_mark_node; 6681169699Skan 6682169699Skan type = TREE_TYPE (size); 6683132747Skan /* The array bound must be an integer type. */ 6684132747Skan if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type)) 668590287Sobrien { 6686132747Skan if (name) 6687169699Skan error ("size of array %qD has non-integral type %qT", name, type); 6688132747Skan else 6689169699Skan error ("size of array has non-integral type %qT", type); 6690132747Skan size = integer_one_node; 6691132747Skan type = TREE_TYPE (size); 669290287Sobrien } 669390287Sobrien 6694132747Skan if (abi_version_at_least (2) 6695132747Skan /* We should only handle value dependent expressions specially. */ 6696132747Skan ? value_dependent_expression_p (size) 6697132747Skan /* But for abi-1, we handled all instances in templates. This 6698132747Skan effects the manglings produced. */ 6699132747Skan : processing_template_decl) 6700132747Skan return build_index_type (build_min (MINUS_EXPR, sizetype, 6701132747Skan size, integer_one_node)); 6702132747Skan 6703117410Skan /* The size might be the result of a cast. */ 670490287Sobrien STRIP_TYPE_NOPS (size); 670590287Sobrien 670690287Sobrien /* It might be a const variable or enumeration constant. */ 6707169699Skan size = integral_constant_value (size); 670890287Sobrien 670990287Sobrien /* Normally, the array-bound will be a constant. */ 671090287Sobrien if (TREE_CODE (size) == INTEGER_CST) 671190287Sobrien { 671290287Sobrien /* Check to see if the array bound overflowed. Make that an 671390287Sobrien error, no matter how generous we're being. */ 671490287Sobrien int old_flag_pedantic_errors = flag_pedantic_errors; 671590287Sobrien int old_pedantic = pedantic; 671690287Sobrien pedantic = flag_pedantic_errors = 1; 671790287Sobrien constant_expression_warning (size); 671890287Sobrien pedantic = old_pedantic; 671990287Sobrien flag_pedantic_errors = old_flag_pedantic_errors; 672090287Sobrien 672190287Sobrien /* An array must have a positive number of elements. */ 672290287Sobrien if (INT_CST_LT (size, integer_zero_node)) 672390287Sobrien { 672490287Sobrien if (name) 6725169699Skan error ("size of array %qD is negative", name); 672690287Sobrien else 672790287Sobrien error ("size of array is negative"); 672890287Sobrien size = integer_one_node; 672990287Sobrien } 6730132747Skan /* As an extension we allow zero-sized arrays. We always allow 6731132747Skan them in system headers because glibc uses them. */ 673290287Sobrien else if (integer_zerop (size) && pedantic && !in_system_header) 673390287Sobrien { 673490287Sobrien if (name) 6735169699Skan pedwarn ("ISO C++ forbids zero-size array %qD", name); 673690287Sobrien else 673790287Sobrien pedwarn ("ISO C++ forbids zero-size array"); 673890287Sobrien } 673990287Sobrien } 674090287Sobrien else if (TREE_CONSTANT (size)) 674190287Sobrien { 674290287Sobrien /* `(int) &fn' is not a valid array bound. */ 674390287Sobrien if (name) 6744169699Skan error ("size of array %qD is not an integral constant-expression", 6745169699Skan name); 674690287Sobrien else 674790287Sobrien error ("size of array is not an integral constant-expression"); 6748169699Skan size = integer_one_node; 674990287Sobrien } 6750259269Spfg else if (pedantic && warn_vla != 0) 6751132747Skan { 6752132747Skan if (name) 6753259269Spfg pedwarn ("ISO C++ forbids variable length array %qD", name); 6754132747Skan else 6755259269Spfg pedwarn ("ISO C++ forbids variable length array"); 6756132747Skan } 6757259269Spfg else if (warn_vla > 0) 6758259269Spfg { 6759259269Spfg if (name) 6760259269Spfg warning (OPT_Wvla, 6761259269Spfg "variable length array %qD is used", name); 6762259269Spfg else 6763259269Spfg warning (OPT_Wvla, 6764259269Spfg "variable length array is used"); 6765259269Spfg } 676690287Sobrien 6767132747Skan if (processing_template_decl && !TREE_CONSTANT (size)) 6768132747Skan /* A variable sized array. */ 6769132747Skan itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node); 6770132747Skan else 677190287Sobrien { 6772169699Skan HOST_WIDE_INT saved_processing_template_decl; 6773169699Skan 6774132747Skan /* Compute the index of the largest element in the array. It is 6775169699Skan one less than the number of elements in the array. We save 6776169699Skan and restore PROCESSING_TEMPLATE_DECL so that computations in 6777169699Skan cp_build_binary_op will be appropriately folded. */ 6778169699Skan saved_processing_template_decl = processing_template_decl; 6779169699Skan processing_template_decl = 0; 6780169699Skan itype = cp_build_binary_op (MINUS_EXPR, 6781169699Skan cp_convert (ssizetype, size), 6782169699Skan cp_convert (ssizetype, integer_one_node)); 6783169699Skan itype = fold (itype); 6784169699Skan processing_template_decl = saved_processing_template_decl; 6785169699Skan 6786132747Skan if (!TREE_CONSTANT (itype)) 6787132747Skan /* A variable sized array. */ 6788132747Skan itype = variable_size (itype); 6789132747Skan /* Make sure that there was no overflow when creating to a signed 6790169699Skan index type. (For example, on a 32-bit machine, an array with 6791169699Skan size 2^32 - 1 is too big.) */ 6792169699Skan else if (TREE_CODE (itype) == INTEGER_CST 6793169699Skan && TREE_OVERFLOW (itype)) 679490287Sobrien { 6795132747Skan error ("overflow in array dimension"); 6796132747Skan TREE_OVERFLOW (itype) = 0; 679790287Sobrien } 679890287Sobrien } 679990287Sobrien 680090287Sobrien /* Create and return the appropriate index type. */ 680190287Sobrien return build_index_type (itype); 680290287Sobrien} 680390287Sobrien 6804132747Skan/* Returns the scope (if any) in which the entity declared by 6805132747Skan DECLARATOR will be located. If the entity was declared with an 6806132747Skan unqualified name, NULL_TREE is returned. */ 6807132747Skan 6808132747Skantree 6809169699Skanget_scope_of_declarator (const cp_declarator *declarator) 6810132747Skan{ 6811169699Skan while (declarator && declarator->kind != cdk_id) 6812169699Skan declarator = declarator->declarator; 6813132747Skan 6814169699Skan /* If the declarator-id is a SCOPE_REF, the scope in which the 6815169699Skan declaration occurs is the first operand. */ 6816169699Skan if (declarator 6817169699Skan && declarator->u.id.qualifying_scope) 6818169699Skan return declarator->u.id.qualifying_scope; 6819132747Skan 6820169699Skan /* Otherwise, the declarator is not a qualified name; the entity will 6821169699Skan be declared in the current scope. */ 6822169699Skan return NULL_TREE; 6823132747Skan} 6824132747Skan 682590287Sobrien/* Returns an ARRAY_TYPE for an array with SIZE elements of the 682690287Sobrien indicated TYPE. If non-NULL, NAME is the NAME of the declaration 682790287Sobrien with this type. */ 682890287Sobrien 682990287Sobrienstatic tree 6830132747Skancreate_array_type_for_decl (tree name, tree type, tree size) 683190287Sobrien{ 683290287Sobrien tree itype = NULL_TREE; 683390287Sobrien const char* error_msg; 683490287Sobrien 683590287Sobrien /* If things have already gone awry, bail now. */ 683690287Sobrien if (type == error_mark_node || size == error_mark_node) 683790287Sobrien return error_mark_node; 683890287Sobrien 683990287Sobrien /* Assume that everything will go OK. */ 684090287Sobrien error_msg = NULL; 684190287Sobrien 684290287Sobrien /* There are some types which cannot be array elements. */ 684390287Sobrien switch (TREE_CODE (type)) 684490287Sobrien { 684590287Sobrien case VOID_TYPE: 684690287Sobrien error_msg = "array of void"; 684790287Sobrien break; 684890287Sobrien 684990287Sobrien case FUNCTION_TYPE: 685090287Sobrien error_msg = "array of functions"; 685190287Sobrien break; 685290287Sobrien 685390287Sobrien case REFERENCE_TYPE: 685490287Sobrien error_msg = "array of references"; 685590287Sobrien break; 685690287Sobrien 685790287Sobrien case METHOD_TYPE: 685890287Sobrien error_msg = "array of function members"; 685990287Sobrien break; 686090287Sobrien 686190287Sobrien default: 686290287Sobrien break; 686390287Sobrien } 686490287Sobrien 686590287Sobrien /* If something went wrong, issue an error-message and return. */ 686690287Sobrien if (error_msg) 686790287Sobrien { 686890287Sobrien if (name) 6869169699Skan error ("declaration of %qD as %s", name, error_msg); 687090287Sobrien else 687190287Sobrien error ("creating %s", error_msg); 687290287Sobrien 687390287Sobrien return error_mark_node; 687490287Sobrien } 687590287Sobrien 687690287Sobrien /* [dcl.array] 687790287Sobrien 687890287Sobrien The constant expressions that specify the bounds of the arrays 687990287Sobrien can be omitted only for the first member of the sequence. */ 688090287Sobrien if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) 688190287Sobrien { 688290287Sobrien if (name) 6883169699Skan error ("declaration of %qD as multidimensional array must " 6884169699Skan "have bounds for all dimensions except the first", 6885169699Skan name); 688690287Sobrien else 6887169699Skan error ("multidimensional array must have bounds for all " 6888169699Skan "dimensions except the first"); 688990287Sobrien 689090287Sobrien return error_mark_node; 689190287Sobrien } 689290287Sobrien 689390287Sobrien /* Figure out the index type for the array. */ 689490287Sobrien if (size) 689590287Sobrien itype = compute_array_index_type (name, size); 689690287Sobrien 6897169699Skan /* [dcl.array] 6898169699Skan T is called the array element type; this type shall not be [...] an 6899169699Skan abstract class type. */ 6900169699Skan abstract_virtuals_error (name, type); 6901169699Skan 690290287Sobrien return build_cplus_array_type (type, itype); 690390287Sobrien} 690490287Sobrien 690590287Sobrien/* Check that it's OK to declare a function with the indicated TYPE. 690690287Sobrien SFK indicates the kind of special function (if any) that this 690790287Sobrien function is. OPTYPE is the type given in a conversion operator 6908169699Skan declaration, or the class type for a constructor/destructor. 6909169699Skan Returns the actual return type of the function; that 691090287Sobrien may be different than TYPE if an error occurs, or for certain 691190287Sobrien special functions. */ 691290287Sobrien 691390287Sobrienstatic tree 6914132747Skancheck_special_function_return_type (special_function_kind sfk, 6915169699Skan tree type, 6916169699Skan tree optype) 691790287Sobrien{ 691890287Sobrien switch (sfk) 691990287Sobrien { 692090287Sobrien case sfk_constructor: 692190287Sobrien if (type) 692290287Sobrien error ("return type specification for constructor invalid"); 692390287Sobrien 6924169699Skan if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype)) 6925169699Skan type = build_pointer_type (optype); 6926169699Skan else 6927169699Skan type = void_type_node; 692890287Sobrien break; 692990287Sobrien 693090287Sobrien case sfk_destructor: 693190287Sobrien if (type) 693290287Sobrien error ("return type specification for destructor invalid"); 6933169699Skan /* We can't use the proper return type here because we run into 6934169699Skan problems with ambiguous bases and covariant returns. 6935169699Skan Java classes are left unchanged because (void *) isn't a valid 6936169699Skan Java type, and we don't want to change the Java ABI. */ 6937169699Skan if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype)) 6938169699Skan type = build_pointer_type (void_type_node); 6939169699Skan else 6940169699Skan type = void_type_node; 694190287Sobrien break; 694290287Sobrien 694390287Sobrien case sfk_conversion: 694490287Sobrien if (type && !same_type_p (type, optype)) 6945169699Skan error ("operator %qT declared to return %qT", optype, type); 694690287Sobrien else if (type) 6947169699Skan pedwarn ("return type specified for %<operator %T%>", optype); 694890287Sobrien type = optype; 694990287Sobrien break; 695090287Sobrien 695190287Sobrien default: 6952169699Skan gcc_unreachable (); 695390287Sobrien } 695490287Sobrien 695590287Sobrien return type; 695690287Sobrien} 695790287Sobrien 6958146906Skan/* A variable or data member (whose unqualified name is IDENTIFIER) 6959146906Skan has been declared with the indicated TYPE. If the TYPE is not 6960146906Skan acceptable, issue an error message and return a type to use for 6961169699Skan error-recovery purposes. */ 6962146906Skan 6963146906Skantree 6964146906Skancheck_var_type (tree identifier, tree type) 6965146906Skan{ 6966146906Skan if (VOID_TYPE_P (type)) 6967146906Skan { 6968146906Skan if (!identifier) 6969146906Skan error ("unnamed variable or field declared void"); 6970146906Skan else if (TREE_CODE (identifier) == IDENTIFIER_NODE) 6971146906Skan { 6972169699Skan gcc_assert (!IDENTIFIER_OPNAME_P (identifier)); 6973169699Skan error ("variable or field %qE declared void", identifier); 6974146906Skan } 6975146906Skan else 6976146906Skan error ("variable or field declared void"); 6977169699Skan type = error_mark_node; 6978146906Skan } 6979169699Skan 6980146906Skan return type; 6981146906Skan} 6982146906Skan 6983132747Skan/* Given declspecs and a declarator (abstract or otherwise), determine 6984132747Skan the name and type of the object declared and construct a DECL node 6985132747Skan for it. 698618334Speter 698718334Speter DECLSPECS is a chain of tree_list nodes whose value fields 698818334Speter are the storage classes and type specifiers. 698918334Speter 699018334Speter DECL_CONTEXT says which syntactic context this declaration is in: 699118334Speter NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 699218334Speter FUNCDEF for a function definition. Like NORMAL but a few different 699318334Speter error messages in each case. Return value may be zero meaning 699418334Speter this definition is too screwy to try to parse. 699518334Speter MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to 699618334Speter handle member functions (which have FIELD context). 699718334Speter Return value may be zero meaning this definition is too screwy to 699818334Speter try to parse. 699918334Speter PARM for a parameter declaration (either within a function prototype 700018334Speter or before a function body). Make a PARM_DECL, or return void_type_node. 700118334Speter CATCHPARM for a parameter declaration before a catch clause. 700218334Speter TYPENAME if for a typename (in a cast or sizeof). 700318334Speter Don't make a DECL node; just return the ..._TYPE node. 700418334Speter FIELD for a struct or union field; make a FIELD_DECL. 700518334Speter BITFIELD for a field with specified width. 700618334Speter INITIALIZED is 1 if the decl has an initializer. 700718334Speter 700890287Sobrien ATTRLIST is a pointer to the list of attributes, which may be NULL 700990287Sobrien if there are none; *ATTRLIST may be modified if attributes from inside 701090287Sobrien the declarator should be applied to the declaration. 701152290Sobrien 7012132747Skan When this function is called, scoping variables (such as 7013132747Skan CURRENT_CLASS_TYPE) should reflect the scope in which the 7014132747Skan declaration occurs, not the scope in which the new declaration will 7015132747Skan be placed. For example, on: 701618334Speter 7017132747Skan void S::f() { ... } 701818334Speter 7019132747Skan when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE 7020169699Skan should not be `S'. 702118334Speter 7022169699Skan Returns a DECL (if a declarator is present), a TYPE (if there is no 7023169699Skan declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an 7024169699Skan error occurs. */ 7025169699Skan 702618334Spetertree 7027169699Skangrokdeclarator (const cp_declarator *declarator, 7028169699Skan const cp_decl_specifier_seq *declspecs, 7029169699Skan enum decl_context decl_context, 7030169699Skan int initialized, 7031169699Skan tree* attrlist) 703218334Speter{ 703318334Speter tree type = NULL_TREE; 703418334Speter int longlong = 0; 703518334Speter int virtualp, explicitp, friendp, inlinep, staticp; 703618334Speter int explicit_int = 0; 703718334Speter int explicit_char = 0; 703851412Sobrien int defaulted_int = 0; 7039169699Skan tree dependent_name = NULL_TREE; 7040169699Skan 704118334Speter tree typedef_decl = NULL_TREE; 7042169699Skan const char *name = NULL; 704318334Speter tree typedef_type = NULL_TREE; 7044169699Skan /* True if this declarator is a function definition. */ 7045169699Skan bool funcdef_flag = false; 7046169699Skan cp_declarator_kind innermost_code = cdk_error; 704718334Speter int bitfield = 0; 704851412Sobrien#if 0 704951412Sobrien /* See the code below that used this. */ 705090287Sobrien tree decl_attr = NULL_TREE; 705151412Sobrien#endif 705218334Speter 705318334Speter /* Keep track of what sort of function is being processed 705418334Speter so that we can warn about default return values, or explicit 705518334Speter return values which do not match prescribed defaults. */ 705690287Sobrien special_function_kind sfk = sfk_none; 705718334Speter 705818334Speter tree dname = NULL_TREE; 705918334Speter tree ctor_return_type = NULL_TREE; 706018334Speter enum overload_flags flags = NO_SPECIAL; 7061169699Skan /* cv-qualifiers that apply to the declarator, for a declaration of 7062169699Skan a member function. */ 7063169699Skan cp_cv_quals memfn_quals = TYPE_UNQUALIFIED; 7064169699Skan /* cv-qualifiers that apply to the type specified by the DECLSPECS. */ 7065169699Skan int type_quals; 706651412Sobrien tree raises = NULL_TREE; 706751412Sobrien int template_count = 0; 706890287Sobrien tree returned_attrs = NULL_TREE; 7069132747Skan tree parms = NULL_TREE; 7070169699Skan const cp_declarator *id_declarator; 7071169699Skan /* The unqualified name of the declarator; either an 7072169699Skan IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */ 7073169699Skan tree unqualified_id; 7074169699Skan /* The class type, if any, in which this entity is located, 7075169699Skan or NULL_TREE if none. Note that this value may be different from 7076169699Skan the current class type; for example if an attempt is made to declare 7077169699Skan "A::f" inside "B", this value will be "A". */ 7078169699Skan tree ctype = current_class_type; 7079169699Skan /* The NAMESPACE_DECL for the namespace in which this entity is 7080169699Skan located. If an unqualified name is used to declare the entity, 7081169699Skan this value will be NULL_TREE, even if the entity is located at 7082169699Skan namespace scope. */ 7083169699Skan tree in_namespace = NULL_TREE; 7084169699Skan cp_storage_class storage_class; 7085169699Skan bool unsigned_p, signed_p, short_p, long_p, thread_p; 7086169699Skan bool type_was_error_mark_node = false; 708718334Speter 7088169699Skan signed_p = declspecs->specs[(int)ds_signed]; 7089169699Skan unsigned_p = declspecs->specs[(int)ds_unsigned]; 7090169699Skan short_p = declspecs->specs[(int)ds_short]; 7091169699Skan long_p = declspecs->specs[(int)ds_long]; 7092169699Skan longlong = declspecs->specs[(int)ds_long] >= 2; 7093169699Skan thread_p = declspecs->specs[(int)ds_thread]; 7094169699Skan 709518334Speter if (decl_context == FUNCDEF) 7096169699Skan funcdef_flag = true, decl_context = NORMAL; 709718334Speter else if (decl_context == MEMFUNCDEF) 7098169699Skan funcdef_flag = true, decl_context = FIELD; 709918334Speter else if (decl_context == BITFIELD) 710018334Speter bitfield = 1, decl_context = FIELD; 710118334Speter 710218334Speter /* Look inside a declarator for the name being declared 710318334Speter and get it as a string, for an error message. */ 7104169699Skan for (id_declarator = declarator; 7105169699Skan id_declarator; 7106169699Skan id_declarator = id_declarator->declarator) 7107169699Skan { 7108169699Skan if (id_declarator->kind != cdk_id) 7109169699Skan innermost_code = id_declarator->kind; 711018334Speter 7111169699Skan switch (id_declarator->kind) 7112169699Skan { 7113169699Skan case cdk_function: 7114169699Skan if (id_declarator->declarator 7115169699Skan && id_declarator->declarator->kind == cdk_id) 711618334Speter { 7117169699Skan sfk = id_declarator->declarator->u.id.sfk; 7118169699Skan if (sfk == sfk_destructor) 7119169699Skan flags = DTOR_FLAG; 712018334Speter } 7121169699Skan break; 712218334Speter 7123169699Skan case cdk_id: 7124169699Skan { 7125169699Skan tree qualifying_scope = id_declarator->u.id.qualifying_scope; 7126169699Skan tree decl = id_declarator->u.id.unqualified_name; 7127169699Skan if (!decl) 7128169699Skan break; 7129169699Skan if (qualifying_scope) 713018334Speter { 7131169699Skan if (at_function_scope_p ()) 7132169699Skan { 7133169699Skan /* [dcl.meaning] 713490287Sobrien 7135169699Skan A declarator-id shall not be qualified except 7136169699Skan for ... 713751412Sobrien 7138169699Skan None of the cases are permitted in block 7139169699Skan scope. */ 7140169699Skan if (qualifying_scope == global_namespace) 7141169699Skan error ("invalid use of qualified-name %<::%D%>", 7142169699Skan decl); 7143169699Skan else if (TYPE_P (qualifying_scope)) 7144169699Skan error ("invalid use of qualified-name %<%T::%D%>", 7145169699Skan qualifying_scope, decl); 7146169699Skan else 7147169699Skan error ("invalid use of qualified-name %<%D::%D%>", 7148169699Skan qualifying_scope, decl); 7149169699Skan return error_mark_node; 7150169699Skan } 7151169699Skan else if (TYPE_P (qualifying_scope)) 715252290Sobrien { 7153169699Skan ctype = qualifying_scope; 7154169699Skan if (innermost_code != cdk_function 7155169699Skan && current_class_type 7156169699Skan && !UNIQUELY_DERIVED_FROM_P (ctype, 7157169699Skan current_class_type)) 7158169699Skan { 7159169699Skan error ("type %qT is not derived from type %qT", 7160169699Skan ctype, current_class_type); 7161169699Skan return error_mark_node; 7162169699Skan } 716352290Sobrien } 7164169699Skan else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL) 7165169699Skan in_namespace = qualifying_scope; 716618334Speter } 7167169699Skan switch (TREE_CODE (decl)) 716818334Speter { 7169169699Skan case BIT_NOT_EXPR: 7170169699Skan { 7171169699Skan tree type; 717218334Speter 7173169699Skan if (innermost_code != cdk_function) 717451412Sobrien { 7175169699Skan error ("declaration of %qD as non-function", decl); 7176169699Skan return error_mark_node; 717751412Sobrien } 7178169699Skan else if (!qualifying_scope 7179169699Skan && !(current_class_type && at_class_scope_p ())) 7180169699Skan { 7181169699Skan error ("declaration of %qD as non-member", decl); 7182169699Skan return error_mark_node; 7183169699Skan } 7184169699Skan 7185169699Skan type = TREE_OPERAND (decl, 0); 7186169699Skan name = IDENTIFIER_POINTER (constructor_name (type)); 7187169699Skan dname = decl; 718851412Sobrien } 7189169699Skan break; 719018334Speter 7191169699Skan case TEMPLATE_ID_EXPR: 719251412Sobrien { 7193169699Skan tree fns = TREE_OPERAND (decl, 0); 7194132747Skan 7195169699Skan dname = fns; 7196169699Skan if (TREE_CODE (dname) != IDENTIFIER_NODE) 719751412Sobrien { 7198169699Skan gcc_assert (is_overloaded_fn (dname)); 7199169699Skan dname = DECL_NAME (get_first_fn (dname)); 720051412Sobrien } 720151412Sobrien } 7202169699Skan /* Fall through. */ 720351412Sobrien 7204169699Skan case IDENTIFIER_NODE: 7205169699Skan if (TREE_CODE (decl) == IDENTIFIER_NODE) 7206169699Skan dname = decl; 720751412Sobrien 7208169699Skan if (C_IS_RESERVED_WORD (dname)) 7209169699Skan { 7210169699Skan error ("declarator-id missing; using reserved word %qD", 7211169699Skan dname); 7212169699Skan name = IDENTIFIER_POINTER (dname); 7213169699Skan } 7214169699Skan else if (!IDENTIFIER_TYPENAME_P (dname)) 7215169699Skan name = IDENTIFIER_POINTER (dname); 7216169699Skan else 7217169699Skan { 7218169699Skan gcc_assert (flags == NO_SPECIAL); 7219169699Skan flags = TYPENAME_FLAG; 7220169699Skan ctor_return_type = TREE_TYPE (dname); 7221169699Skan sfk = sfk_conversion; 7222169699Skan if (is_typename_at_global_scope (dname)) 7223169699Skan name = IDENTIFIER_POINTER (dname); 7224169699Skan else 7225169699Skan name = "<invalid operator>"; 7226169699Skan } 7227169699Skan break; 722851412Sobrien 7229169699Skan default: 7230169699Skan gcc_unreachable (); 7231169699Skan } 723251412Sobrien break; 723351412Sobrien 7234169699Skan case cdk_array: 7235169699Skan case cdk_pointer: 7236169699Skan case cdk_reference: 7237169699Skan case cdk_ptrmem: 7238117410Skan break; 7239117410Skan 7240169699Skan case cdk_error: 7241107601Sobrien return error_mark_node; 7242169699Skan 724351412Sobrien default: 7244169699Skan gcc_unreachable (); 724551412Sobrien } 7246169699Skan } 7247169699Skan if (id_declarator->kind == cdk_id) 7248169699Skan break; 7249169699Skan } 725018334Speter 7251169699Skan /* [dcl.fct.edf] 725218334Speter 7253169699Skan The declarator in a function-definition shall have the form 7254169699Skan D1 ( parameter-declaration-clause) ... */ 7255169699Skan if (funcdef_flag && innermost_code != cdk_function) 7256169699Skan { 7257169699Skan error ("function definition does not declare parameters"); 7258169699Skan return error_mark_node; 7259169699Skan } 726018334Speter 726118334Speter if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG) 7262169699Skan && innermost_code != cdk_function 7263169699Skan && ! (ctype && !declspecs->any_specifiers_p)) 726418334Speter { 7265169699Skan error ("declaration of %qD as non-function", dname); 7266169699Skan return error_mark_node; 726718334Speter } 726818334Speter 726918334Speter /* Anything declared one level down from the top level 727018334Speter must be one of the parameters of a function 727118334Speter (because the body is at least two levels down). */ 727218334Speter 727318334Speter /* This heuristic cannot be applied to C++ nodes! Fixed, however, 727418334Speter by not allowing C++ class definitions to specify their parameters 727518334Speter with xdecls (must be spec.d in the parmlist). 727618334Speter 727718334Speter Since we now wait to push a class scope until we are sure that 727818334Speter we are in a legitimate method context, we must set oldcname 727918334Speter explicitly (since current_class_name is not yet alive). 728018334Speter 728118334Speter We also want to avoid calling this a PARM if it is in a namespace. */ 728218334Speter 728390287Sobrien if (decl_context == NORMAL && !toplevel_bindings_p ()) 728418334Speter { 7285117410Skan struct cp_binding_level *b = current_binding_level; 728618334Speter current_binding_level = b->level_chain; 728718334Speter if (current_binding_level != 0 && toplevel_bindings_p ()) 728818334Speter decl_context = PARM; 728918334Speter current_binding_level = b; 729018334Speter } 729118334Speter 729290287Sobrien if (name == NULL) 729390287Sobrien name = decl_context == PARM ? "parameter" : "type name"; 729490287Sobrien 7295169699Skan /* If there were multiple types specified in the decl-specifier-seq, 7296169699Skan issue an error message. */ 7297169699Skan if (declspecs->multiple_types_p) 7298169699Skan { 7299169699Skan error ("two or more data types in declaration of %qs", name); 7300169699Skan return error_mark_node; 7301169699Skan } 730218334Speter 7303169699Skan /* Extract the basic type from the decl-specifier-seq. */ 7304169699Skan type = declspecs->type; 7305169699Skan if (type == error_mark_node) 730618334Speter { 7307169699Skan type = NULL_TREE; 7308169699Skan type_was_error_mark_node = true; 730918334Speter } 7310260919Spfg 7311260919Spfg /* APPLE LOCAL begin unavailable attribute (radar 2809697) --bowdidge */ 7312260919Spfg /* If the entire declaration is itself tagged as unavailable then 7313260919Spfg suppress reports of unavailable/deprecated items. If the 7314260919Spfg entire declaration is tagged as only deprecated we still 7315260919Spfg report unavailable uses. */ 7316260919Spfg if (type && TREE_DEPRECATED (type) && TREE_UNAVAILABLE (type)) 7317260919Spfg { 7318260919Spfg if (deprecated_state != DEPRECATED_UNAVAILABLE_SUPPRESS) 7319260919Spfg warn_deprecated_use (type); 7320260919Spfg } 7321260919Spfg else 7322260919Spfg /* APPLE LOCAL end unavailable attribute (radar 2809697) --bowdidge */ 7323169699Skan /* If the entire declaration is itself tagged as deprecated then 7324169699Skan suppress reports of deprecated items. */ 7325169699Skan if (type && TREE_DEPRECATED (type) 7326169699Skan && deprecated_state != DEPRECATED_SUPPRESS) 7327169699Skan warn_deprecated_use (type); 7328169699Skan if (type && TREE_CODE (type) == TYPE_DECL) 7329169699Skan { 7330169699Skan typedef_decl = type; 7331169699Skan type = TREE_TYPE (typedef_decl); 7332169699Skan } 7333169699Skan /* No type at all: default to `int', and set DEFAULTED_INT 7334169699Skan because it was not a user-defined typedef. */ 7335169699Skan if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p)) 7336169699Skan { 7337169699Skan /* These imply 'int'. */ 7338169699Skan type = integer_type_node; 7339169699Skan defaulted_int = 1; 7340169699Skan } 7341169699Skan /* Gather flags. */ 7342169699Skan explicit_int = declspecs->explicit_int_p; 7343169699Skan explicit_char = declspecs->explicit_char_p; 734418334Speter 734596292Sobrien#if 0 734696292Sobrien /* See the code below that used this. */ 734796292Sobrien if (typedef_decl) 734896292Sobrien decl_attr = DECL_ATTRIBUTES (typedef_decl); 734996292Sobrien#endif 735018334Speter typedef_type = type; 735118334Speter 735218334Speter 7353169699Skan if (sfk != sfk_conversion) 7354169699Skan ctor_return_type = ctype; 735518334Speter 735690287Sobrien if (sfk != sfk_none) 735790287Sobrien type = check_special_function_return_type (sfk, type, 735890287Sobrien ctor_return_type); 735990287Sobrien else if (type == NULL_TREE) 736018334Speter { 736190287Sobrien int is_main; 736290287Sobrien 736318334Speter explicit_int = -1; 736452290Sobrien 736590287Sobrien /* We handle `main' specially here, because 'main () { }' is so 736690287Sobrien common. With no options, it is allowed. With -Wreturn-type, 736790287Sobrien it is a warning. It is only an error with -pedantic-errors. */ 736890287Sobrien is_main = (funcdef_flag 736990287Sobrien && dname && MAIN_NAME_P (dname) 737090287Sobrien && ctype == NULL_TREE 737190287Sobrien && in_namespace == NULL_TREE 737290287Sobrien && current_namespace == global_namespace); 737352290Sobrien 7374169699Skan if (type_was_error_mark_node) 7375169699Skan /* We've already issued an error, don't complain more. */; 7376169699Skan else if (in_system_header || flag_ms_extensions) 737790287Sobrien /* Allow it, sigh. */; 737890287Sobrien else if (pedantic || ! is_main) 7379169699Skan pedwarn ("ISO C++ forbids declaration of %qs with no type", name); 738090287Sobrien else if (warn_return_type) 7381169699Skan warning (0, "ISO C++ forbids declaration of %qs with no type", name); 738290287Sobrien 738390287Sobrien type = integer_type_node; 738418334Speter } 7385169699Skan 738618334Speter ctype = NULL_TREE; 738718334Speter 738818334Speter /* Now process the modifiers that were specified 738918334Speter and check for invalid combinations. */ 739018334Speter 739118334Speter /* Long double is a special combination. */ 7392169699Skan if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node) 739318334Speter { 7394169699Skan long_p = false; 739590287Sobrien type = build_qualified_type (long_double_type_node, 739690287Sobrien cp_type_quals (type)); 739718334Speter } 739818334Speter 739918334Speter /* Check all other uses of type modifiers. */ 740018334Speter 7401169699Skan if (unsigned_p || signed_p || long_p || short_p) 740218334Speter { 740318334Speter int ok = 0; 740418334Speter 7405169699Skan if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE) 7406169699Skan error ("%<signed%> or %<unsigned%> invalid for %qs", name); 7407169699Skan else if (signed_p && unsigned_p) 7408169699Skan error ("%<signed%> and %<unsigned%> specified together for %qs", name); 7409169699Skan else if (longlong && TREE_CODE (type) != INTEGER_TYPE) 7410169699Skan error ("%<long long%> invalid for %qs", name); 7411169699Skan else if (long_p && TREE_CODE (type) == REAL_TYPE) 7412169699Skan error ("%<long%> invalid for %qs", name); 7413169699Skan else if (short_p && TREE_CODE (type) == REAL_TYPE) 7414169699Skan error ("%<short%> invalid for %qs", name); 7415169699Skan else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE) 7416169699Skan error ("%<long%> or %<short%> invalid for %qs", name); 7417169699Skan else if ((long_p || short_p) && explicit_char) 7418169699Skan error ("%<long%> or %<short%> specified with char for %qs", name); 7419169699Skan else if (long_p && short_p) 7420169699Skan error ("%<long%> and %<short%> specified together for %qs", name); 742118334Speter else 742218334Speter { 742318334Speter ok = 1; 742451412Sobrien if (!explicit_int && !defaulted_int && !explicit_char && pedantic) 742518334Speter { 7426169699Skan pedwarn ("long, short, signed or unsigned used invalidly for %qs", 742718334Speter name); 742818334Speter if (flag_pedantic_errors) 742918334Speter ok = 0; 743018334Speter } 743118334Speter } 743218334Speter 743318334Speter /* Discard the type modifiers if they are invalid. */ 743418334Speter if (! ok) 743518334Speter { 7436169699Skan unsigned_p = false; 7437169699Skan signed_p = false; 7438169699Skan long_p = false; 7439169699Skan short_p = false; 744018334Speter longlong = 0; 744118334Speter } 744218334Speter } 744318334Speter 744418334Speter /* Decide whether an integer type is signed or not. 744518334Speter Optionally treat bitfields as signed by default. */ 7446169699Skan if (unsigned_p 744790287Sobrien /* [class.bit] 744890287Sobrien 744990287Sobrien It is implementation-defined whether a plain (neither 745090287Sobrien explicitly signed or unsigned) char, short, int, or long 745190287Sobrien bit-field is signed or unsigned. 745290287Sobrien 745390287Sobrien Naturally, we extend this to long long as well. Note that 745490287Sobrien this does not include wchar_t. */ 745590287Sobrien || (bitfield && !flag_signed_bitfields 7456169699Skan && !signed_p 745790287Sobrien /* A typedef for plain `int' without `signed' can be 745890287Sobrien controlled just like plain `int', but a typedef for 745990287Sobrien `signed int' cannot be so controlled. */ 746090287Sobrien && !(typedef_decl 746190287Sobrien && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) 7462169699Skan && TREE_CODE (type) == INTEGER_TYPE 746390287Sobrien && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) 746418334Speter { 746518334Speter if (longlong) 746618334Speter type = long_long_unsigned_type_node; 7467169699Skan else if (long_p) 746818334Speter type = long_unsigned_type_node; 7469169699Skan else if (short_p) 747018334Speter type = short_unsigned_type_node; 747118334Speter else if (type == char_type_node) 747218334Speter type = unsigned_char_type_node; 747318334Speter else if (typedef_decl) 7474117410Skan type = c_common_unsigned_type (type); 747518334Speter else 747618334Speter type = unsigned_type_node; 747718334Speter } 7478169699Skan else if (signed_p && type == char_type_node) 747918334Speter type = signed_char_type_node; 748018334Speter else if (longlong) 748118334Speter type = long_long_integer_type_node; 7482169699Skan else if (long_p) 748318334Speter type = long_integer_type_node; 7484169699Skan else if (short_p) 748518334Speter type = short_integer_type_node; 748618334Speter 7487169699Skan if (declspecs->specs[(int)ds_complex]) 748851412Sobrien { 7489169699Skan if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) 7490169699Skan error ("complex invalid for %qs", name); 749151412Sobrien /* If we just have "complex", it is equivalent to 749251412Sobrien "complex double", but if any modifiers at all are specified it is 749351412Sobrien the complex form of TYPE. E.g, "complex short" is 749451412Sobrien "complex short int". */ 749551412Sobrien 7496169699Skan else if (defaulted_int && ! longlong 7497169699Skan && ! (long_p || short_p || signed_p || unsigned_p)) 749851412Sobrien type = complex_double_type_node; 749951412Sobrien else if (type == integer_type_node) 750051412Sobrien type = complex_integer_type_node; 750151412Sobrien else if (type == float_type_node) 750251412Sobrien type = complex_float_type_node; 750351412Sobrien else if (type == double_type_node) 750451412Sobrien type = complex_double_type_node; 750551412Sobrien else if (type == long_double_type_node) 750651412Sobrien type = complex_long_double_type_node; 750751412Sobrien else 750851412Sobrien type = build_complex_type (type); 750951412Sobrien } 751051412Sobrien 751196292Sobrien type_quals = TYPE_UNQUALIFIED; 7512169699Skan if (declspecs->specs[(int)ds_const]) 751396292Sobrien type_quals |= TYPE_QUAL_CONST; 7514169699Skan if (declspecs->specs[(int)ds_volatile]) 751596292Sobrien type_quals |= TYPE_QUAL_VOLATILE; 7516169699Skan if (declspecs->specs[(int)ds_restrict]) 751796292Sobrien type_quals |= TYPE_QUAL_RESTRICT; 751896292Sobrien if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED) 7519169699Skan error ("qualifiers are not allowed on declaration of %<operator %T%>", 7520169699Skan ctor_return_type); 752151412Sobrien 7522169699Skan if (TREE_CODE (type) == FUNCTION_TYPE 7523169699Skan && type_quals != TYPE_UNQUALIFIED) 7524169699Skan { 7525169699Skan /* This was an error in C++98 (cv-qualifiers cannot be added to 7526169699Skan a function type), but DR 295 makes the code well-formed by 7527169699Skan dropping the extra qualifiers. */ 7528169699Skan if (pedantic) 7529169699Skan { 7530169699Skan tree bad_type = build_qualified_type (type, type_quals); 7531169699Skan pedwarn ("ignoring %qV qualifiers added to function type %qT", 7532169699Skan bad_type, type); 7533169699Skan } 7534169699Skan type_quals = TYPE_UNQUALIFIED; 7535169699Skan } 753696292Sobrien type_quals |= cp_type_quals (type); 753796292Sobrien type = cp_build_qualified_type_real 753896292Sobrien (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl) 7539169699Skan ? tf_ignore_bad_quals : 0) | tf_warning_or_error)); 754096292Sobrien /* We might have ignored or rejected some of the qualifiers. */ 754196292Sobrien type_quals = cp_type_quals (type); 7542169699Skan 754318334Speter staticp = 0; 7544169699Skan inlinep = !! declspecs->specs[(int)ds_inline]; 7545169699Skan virtualp = !! declspecs->specs[(int)ds_virtual]; 7546169699Skan explicitp = !! declspecs->specs[(int)ds_explicit]; 754718334Speter 7548169699Skan storage_class = declspecs->storage_class; 7549169699Skan if (storage_class == sc_static) 755018334Speter staticp = 1 + (decl_context == FIELD); 755118334Speter 755218334Speter if (virtualp && staticp == 2) 755318334Speter { 7554169699Skan error ("member %qD cannot be declared both virtual and static", dname); 7555169699Skan storage_class = sc_none; 755618334Speter staticp = 0; 755718334Speter } 7558169699Skan friendp = !! declspecs->specs[(int)ds_friend]; 755918334Speter 7560169699Skan if (dependent_name && !friendp) 756196292Sobrien { 7562169699Skan error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name); 7563169699Skan return error_mark_node; 756496292Sobrien } 756518334Speter 7566169699Skan /* Issue errors about use of storage classes for parameters. */ 7567169699Skan if (decl_context == PARM) 756818334Speter { 7569169699Skan if (declspecs->specs[(int)ds_typedef]) 757018334Speter { 7571169699Skan error ("typedef declaration invalid in parameter declaration"); 7572169699Skan return error_mark_node; 757318334Speter } 7574169699Skan else if (storage_class == sc_static 7575169699Skan || storage_class == sc_extern 7576169699Skan || thread_p) 7577169699Skan error ("storage class specifiers invalid in parameter declarations"); 757818334Speter } 757918334Speter 758018334Speter /* Give error if `virtual' is used outside of class declaration. */ 758118334Speter if (virtualp 758218334Speter && (current_class_name == NULL_TREE || decl_context != FIELD)) 758318334Speter { 758418334Speter error ("virtual outside class declaration"); 758518334Speter virtualp = 0; 758618334Speter } 758718334Speter 758818334Speter /* Static anonymous unions are dealt with here. */ 758918334Speter if (staticp && decl_context == TYPENAME 7590169699Skan && declspecs->type 7591169699Skan && ANON_AGGR_TYPE_P (declspecs->type)) 759218334Speter decl_context = FIELD; 759318334Speter 759418334Speter /* Warn about storage classes that are invalid for certain 759518334Speter kinds of declarations (parameters, typenames, etc.). */ 7596169699Skan if (thread_p 7597169699Skan && ((storage_class 7598169699Skan && storage_class != sc_extern 7599169699Skan && storage_class != sc_static) 7600169699Skan || declspecs->specs[(int)ds_typedef])) 760118334Speter { 7602169699Skan error ("multiple storage classes in declaration of %qs", name); 7603169699Skan thread_p = false; 7604169699Skan } 7605169699Skan if (declspecs->conflicting_specifiers_p) 7606169699Skan { 7607169699Skan error ("conflicting specifiers in declaration of %qs", name); 7608169699Skan storage_class = sc_none; 7609169699Skan } 7610169699Skan else if (decl_context != NORMAL 7611169699Skan && ((storage_class != sc_none 7612169699Skan && storage_class != sc_mutable) 7613169699Skan || thread_p)) 7614169699Skan { 761518334Speter if ((decl_context == PARM || decl_context == CATCHPARM) 7616169699Skan && (storage_class == sc_register 7617169699Skan || storage_class == sc_auto)) 761818334Speter ; 7619169699Skan else if (declspecs->specs[(int)ds_typedef]) 762051412Sobrien ; 762118334Speter else if (decl_context == FIELD 7622132747Skan /* C++ allows static class elements. */ 7623169699Skan && storage_class == sc_static) 762490287Sobrien /* C++ also allows inlines and signed and unsigned elements, 762590287Sobrien but in those cases we don't come in here. */ 762618334Speter ; 762718334Speter else 762818334Speter { 762918334Speter if (decl_context == FIELD) 7630169699Skan error ("storage class specified for %qs", name); 763118334Speter else 763290287Sobrien { 763390287Sobrien if (decl_context == PARM || decl_context == CATCHPARM) 7634169699Skan error ("storage class specified for parameter %qs", name); 763590287Sobrien else 763690287Sobrien error ("storage class specified for typename"); 763790287Sobrien } 7638169699Skan if (storage_class == sc_register 7639169699Skan || storage_class == sc_auto 7640169699Skan || storage_class == sc_extern 7641169699Skan || thread_p) 7642169699Skan storage_class = sc_none; 764318334Speter } 764418334Speter } 7645169699Skan else if (storage_class == sc_extern && initialized 7646169699Skan && !funcdef_flag) 764718334Speter { 764818334Speter if (toplevel_bindings_p ()) 764918334Speter { 765018334Speter /* It's common practice (and completely valid) to have a const 765118334Speter be initialized and declared extern. */ 765252290Sobrien if (!(type_quals & TYPE_QUAL_CONST)) 7653169699Skan warning (0, "%qs initialized and declared %<extern%>", name); 765418334Speter } 765518334Speter else 7656169699Skan error ("%qs has both %<extern%> and initializer", name); 765718334Speter } 7658169699Skan else if (storage_class == sc_extern && funcdef_flag 765918334Speter && ! toplevel_bindings_p ()) 7660169699Skan error ("nested function %qs declared %<extern%>", name); 766118334Speter else if (toplevel_bindings_p ()) 766218334Speter { 7663169699Skan if (storage_class == sc_auto) 7664169699Skan error ("top-level declaration of %qs specifies %<auto%>", name); 766518334Speter } 7666169699Skan else if (thread_p 7667169699Skan && storage_class != sc_extern 7668169699Skan && storage_class != sc_static) 7669117410Skan { 7670169699Skan error ("function-scope %qs implicitly auto and declared %<__thread%>", 7671117410Skan name); 7672169699Skan thread_p = false; 7673117410Skan } 767418334Speter 7675169699Skan if (storage_class && friendp) 767651412Sobrien error ("storage class specifiers invalid in friend function declarations"); 767751412Sobrien 7678169699Skan if (!id_declarator) 7679169699Skan unqualified_id = NULL_TREE; 7680169699Skan else 7681169699Skan { 7682169699Skan unqualified_id = id_declarator->u.id.unqualified_name; 7683169699Skan switch (TREE_CODE (unqualified_id)) 7684169699Skan { 7685169699Skan case BIT_NOT_EXPR: 7686169699Skan unqualified_id 7687169699Skan = constructor_name (TREE_OPERAND (unqualified_id, 0)); 7688169699Skan break; 7689132747Skan 7690169699Skan case IDENTIFIER_NODE: 7691169699Skan case TEMPLATE_ID_EXPR: 7692169699Skan break; 769318334Speter 7694169699Skan default: 7695169699Skan gcc_unreachable (); 7696169699Skan } 7697169699Skan } 7698169699Skan 7699169699Skan /* Determine the type of the entity declared by recurring on the 7700169699Skan declarator. */ 7701169699Skan for (; declarator; declarator = declarator->declarator) 770218334Speter { 7703169699Skan const cp_declarator *inner_declarator; 7704169699Skan tree attrs; 770518334Speter 770651412Sobrien if (type == error_mark_node) 7707169699Skan return error_mark_node; 770852290Sobrien 7709169699Skan attrs = declarator->attributes; 7710169699Skan if (attrs) 771118334Speter { 7712169699Skan int attr_flags; 771390287Sobrien 7714169699Skan attr_flags = 0; 7715169699Skan if (declarator == NULL || declarator->kind == cdk_id) 7716169699Skan attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 7717169699Skan if (declarator->kind == cdk_function) 7718169699Skan attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 7719169699Skan if (declarator->kind == cdk_array) 7720169699Skan attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 7721169699Skan returned_attrs = decl_attributes (&type, 7722169699Skan chainon (returned_attrs, attrs), 7723169699Skan attr_flags); 7724169699Skan } 772552290Sobrien 7726169699Skan if (declarator->kind == cdk_id) 7727169699Skan break; 772818334Speter 7729169699Skan inner_declarator = declarator->declarator; 773052290Sobrien 7731169699Skan switch (declarator->kind) 7732169699Skan { 7733169699Skan case cdk_array: 7734169699Skan type = create_array_type_for_decl (dname, type, 7735169699Skan declarator->u.array.bounds); 773618334Speter break; 773718334Speter 7738169699Skan case cdk_function: 773918334Speter { 774018334Speter tree arg_types; 774118334Speter int funcdecl_p; 774218334Speter 774318334Speter /* Declaring a function type. 774418334Speter Make sure we have a valid type for the function to return. */ 774518334Speter 774652290Sobrien /* We now know that the TYPE_QUALS don't apply to the 7747169699Skan decl, but to its return type. */ 774852290Sobrien type_quals = TYPE_UNQUALIFIED; 774918334Speter 775018334Speter /* Warn about some types functions can't return. */ 775118334Speter 775218334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 775318334Speter { 7754169699Skan error ("%qs declared as function returning a function", name); 775518334Speter type = integer_type_node; 775618334Speter } 775718334Speter if (TREE_CODE (type) == ARRAY_TYPE) 775818334Speter { 7759169699Skan error ("%qs declared as function returning an array", name); 776018334Speter type = integer_type_node; 776118334Speter } 776218334Speter 776318334Speter /* Pick up type qualifiers which should be applied to `this'. */ 7764169699Skan memfn_quals = declarator->u.function.qualifiers; 776518334Speter 776651412Sobrien /* Pick up the exception specifications. */ 7767169699Skan raises = declarator->u.function.exception_specification; 776851412Sobrien 776918334Speter /* Say it's a definition only for the CALL_EXPR 777018334Speter closest to the identifier. */ 7771169699Skan funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id; 777290287Sobrien 777318334Speter if (ctype == NULL_TREE 777418334Speter && decl_context == FIELD 777518334Speter && funcdecl_p 777618334Speter && (friendp == 0 || dname == current_class_name)) 777718334Speter ctype = current_class_type; 777818334Speter 7779169699Skan if (ctype && (sfk == sfk_constructor 7780169699Skan || sfk == sfk_destructor)) 778118334Speter { 778218334Speter /* We are within a class's scope. If our declarator name 778318334Speter is the same as the class name, and we are defining 778418334Speter a function, then it is a constructor/destructor, and 778518334Speter therefore returns a void type. */ 778618334Speter 7787169699Skan /* ISO C++ 12.4/2. A destructor may not be declared 7788169699Skan const or volatile. A destructor may not be 7789169699Skan static. 7790169699Skan 7791169699Skan ISO C++ 12.1. A constructor may not be declared 7792169699Skan const or volatile. A constructor may not be 7793169699Skan virtual. A constructor may not be static. */ 7794169699Skan if (staticp == 2) 7795169699Skan error ((flags == DTOR_FLAG) 7796169699Skan ? "destructor cannot be static member function" 7797169699Skan : "constructor cannot be static member function"); 7798169699Skan if (memfn_quals) 779918334Speter { 7800169699Skan error ((flags == DTOR_FLAG) 7801169699Skan ? "destructors may not be cv-qualified" 7802169699Skan : "constructors may not be cv-qualified"); 7803169699Skan memfn_quals = TYPE_UNQUALIFIED; 780418334Speter } 7805169699Skan 7806169699Skan if (decl_context == FIELD 7807169699Skan && !member_function_or_else (ctype, 7808169699Skan current_class_type, 7809169699Skan flags)) 7810169699Skan return error_mark_node; 7811169699Skan 7812169699Skan if (flags != DTOR_FLAG) 781318334Speter { 7814169699Skan /* It's a constructor. */ 781518334Speter if (explicitp == 1) 781618334Speter explicitp = 2; 781718334Speter if (virtualp) 781818334Speter { 781918334Speter pedwarn ("constructors cannot be declared virtual"); 782018334Speter virtualp = 0; 782118334Speter } 7822169699Skan if (decl_context == FIELD 7823169699Skan && sfk != sfk_constructor) 7824169699Skan return error_mark_node; 782518334Speter } 782618334Speter if (decl_context == FIELD) 782718334Speter staticp = 0; 782818334Speter } 782918334Speter else if (friendp) 783018334Speter { 783118334Speter if (initialized) 7832169699Skan error ("can't initialize friend function %qs", name); 783318334Speter if (virtualp) 783418334Speter { 783518334Speter /* Cannot be both friend and virtual. */ 783618334Speter error ("virtual functions cannot be friends"); 783718334Speter friendp = 0; 783818334Speter } 783918334Speter if (decl_context == NORMAL) 784018334Speter error ("friend declaration not in class definition"); 784118334Speter if (current_function_decl && funcdef_flag) 7842169699Skan error ("can't define friend function %qs in a local " 7843169699Skan "class definition", 7844169699Skan name); 784518334Speter } 784618334Speter 7847169699Skan arg_types = grokparms (declarator->u.function.parameters, 7848169699Skan &parms); 784918334Speter 7850169699Skan if (inner_declarator 7851169699Skan && inner_declarator->kind == cdk_id 7852169699Skan && inner_declarator->u.id.sfk == sfk_destructor 7853169699Skan && arg_types != void_list_node) 785418334Speter { 7855169699Skan error ("destructors may not have parameters"); 7856169699Skan arg_types = void_list_node; 7857169699Skan parms = NULL_TREE; 785818334Speter } 785918334Speter 786051412Sobrien type = build_function_type (type, arg_types); 786118334Speter } 786218334Speter break; 786318334Speter 7864169699Skan case cdk_pointer: 7865169699Skan case cdk_reference: 7866169699Skan case cdk_ptrmem: 786718334Speter /* Filter out pointers-to-references and references-to-references. 786818334Speter We can get these if a TYPE_DECL is used. */ 786918334Speter 787018334Speter if (TREE_CODE (type) == REFERENCE_TYPE) 787118334Speter { 7872169699Skan error (declarator->kind == cdk_reference 7873169699Skan ? "cannot declare reference to %q#T" 7874169699Skan : "cannot declare pointer to %q#T", type); 787518334Speter type = TREE_TYPE (type); 787618334Speter } 7877169699Skan else if (VOID_TYPE_P (type)) 7878169699Skan { 7879169699Skan if (declarator->kind == cdk_reference) 7880169699Skan error ("cannot declare reference to %q#T", type); 7881169699Skan else if (declarator->kind == cdk_ptrmem) 7882169699Skan error ("cannot declare pointer to %q#T member", type); 7883169699Skan } 788418334Speter 788552290Sobrien /* We now know that the TYPE_QUALS don't apply to the decl, 788652290Sobrien but to the target of the pointer. */ 788752290Sobrien type_quals = TYPE_UNQUALIFIED; 788818334Speter 7889169699Skan if (declarator->kind == cdk_ptrmem 7890169699Skan && (TREE_CODE (type) == FUNCTION_TYPE || memfn_quals)) 789118334Speter { 7892169699Skan memfn_quals |= cp_type_quals (type); 7893169699Skan type = build_memfn_type (type, 7894169699Skan declarator->u.pointer.class_type, 7895169699Skan memfn_quals); 7896169699Skan memfn_quals = TYPE_UNQUALIFIED; 7897169699Skan } 7898169699Skan 7899169699Skan if (declarator->kind == cdk_reference) 7900169699Skan { 7901117410Skan if (!VOID_TYPE_P (type)) 790252290Sobrien type = build_reference_type (type); 790318334Speter } 790418334Speter else if (TREE_CODE (type) == METHOD_TYPE) 790552290Sobrien type = build_ptrmemfunc_type (build_pointer_type (type)); 7906169699Skan else if (declarator->kind == cdk_ptrmem) 7907169699Skan { 7908169699Skan gcc_assert (TREE_CODE (declarator->u.pointer.class_type) 7909169699Skan != NAMESPACE_DECL); 7910169699Skan if (declarator->u.pointer.class_type == error_mark_node) 7911169699Skan /* We will already have complained. */ 7912169699Skan type = error_mark_node; 7913169699Skan else 7914169699Skan type = build_ptrmem_type (declarator->u.pointer.class_type, 7915169699Skan type); 7916169699Skan } 791718334Speter else 791818334Speter type = build_pointer_type (type); 791918334Speter 792018334Speter /* Process a list of type modifier keywords (such as 792118334Speter const or volatile) that were given inside the `*' or `&'. */ 792218334Speter 7923169699Skan if (declarator->u.pointer.qualifiers) 792418334Speter { 7925169699Skan type 7926169699Skan = cp_build_qualified_type (type, 7927169699Skan declarator->u.pointer.qualifiers); 792896292Sobrien type_quals = cp_type_quals (type); 792918334Speter } 793018334Speter ctype = NULL_TREE; 793118334Speter break; 793218334Speter 7933169699Skan case cdk_error: 7934169699Skan break; 793518334Speter 7936169699Skan default: 7937169699Skan gcc_unreachable (); 7938169699Skan } 7939169699Skan } 794051412Sobrien 7941169699Skan if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR 7942169699Skan && TREE_CODE (type) != FUNCTION_TYPE 7943169699Skan && TREE_CODE (type) != METHOD_TYPE) 7944169699Skan { 7945169699Skan error ("template-id %qD used as a declarator", 7946169699Skan unqualified_id); 7947169699Skan unqualified_id = dname; 7948169699Skan } 794918334Speter 7950169699Skan /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly 7951169699Skan qualified with a class-name, turn it into a METHOD_TYPE, unless 7952169699Skan we know that the function is static. We take advantage of this 7953169699Skan opportunity to do other processing that pertains to entities 7954169699Skan explicitly declared to be class members. Note that if DECLARATOR 7955169699Skan is non-NULL, we know it is a cdk_id declarator; otherwise, we 7956169699Skan would not have exited the loop above. */ 7957169699Skan if (declarator 7958169699Skan && declarator->u.id.qualifying_scope 7959169699Skan && TYPE_P (declarator->u.id.qualifying_scope)) 7960169699Skan { 7961169699Skan tree t; 796218334Speter 7963169699Skan ctype = declarator->u.id.qualifying_scope; 7964169699Skan ctype = TYPE_MAIN_VARIANT (ctype); 7965169699Skan t = ctype; 7966169699Skan while (t != NULL_TREE && CLASS_TYPE_P (t)) 7967169699Skan { 7968169699Skan /* You're supposed to have one `template <...>' for every 7969169699Skan template class, but you don't need one for a full 7970169699Skan specialization. For example: 797190287Sobrien 7972169699Skan template <class T> struct S{}; 7973169699Skan template <> struct S<int> { void f(); }; 7974169699Skan void S<int>::f () {} 797518334Speter 7976169699Skan is correct; there shouldn't be a `template <>' for the 7977169699Skan definition of `S<int>::f'. */ 7978169699Skan if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t) 7979169699Skan && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t))) 7980169699Skan /* T is an explicit (not partial) specialization. All 7981169699Skan containing classes must therefore also be explicitly 7982169699Skan specialized. */ 7983169699Skan break; 7984169699Skan if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t)) 7985169699Skan && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) 7986169699Skan template_count += 1; 798718334Speter 7988169699Skan t = TYPE_MAIN_DECL (t); 7989169699Skan t = DECL_CONTEXT (t); 7990169699Skan } 799118334Speter 7992169699Skan if (ctype == current_class_type) 7993169699Skan { 7994169699Skan if (friendp) 7995169699Skan pedwarn ("member functions are implicitly friends of their class"); 7996169699Skan else 7997169699Skan pedwarn ("extra qualification %<%T::%> on member %qs", 7998169699Skan ctype, name); 7999169699Skan } 8000169699Skan else if (/* If the qualifying type is already complete, then we 8001169699Skan can skip the following checks. */ 8002169699Skan !COMPLETE_TYPE_P (ctype) 8003169699Skan && (/* If the function is being defined, then 8004169699Skan qualifying type must certainly be complete. */ 8005169699Skan funcdef_flag 8006169699Skan /* A friend declaration of "T::f" is OK, even if 8007169699Skan "T" is a template parameter. But, if this 8008169699Skan function is not a friend, the qualifying type 8009169699Skan must be a class. */ 8010169699Skan || (!friendp && !CLASS_TYPE_P (ctype)) 8011169699Skan /* For a declaration, the type need not be 8012169699Skan complete, if either it is dependent (since there 8013169699Skan is no meaningful definition of complete in that 8014169699Skan case) or the qualifying class is currently being 8015169699Skan defined. */ 8016169699Skan || !(dependent_type_p (ctype) 8017169699Skan || currently_open_class (ctype))) 8018169699Skan /* Check that the qualifying type is complete. */ 8019169699Skan && !complete_type_or_else (ctype, NULL_TREE)) 8020169699Skan return error_mark_node; 8021169699Skan else if (TREE_CODE (type) == FUNCTION_TYPE) 8022169699Skan { 8023169699Skan tree sname = declarator->u.id.unqualified_name; 802418334Speter 8025169699Skan if (current_class_type 8026169699Skan && (!friendp || funcdef_flag)) 8027169699Skan { 8028169699Skan error (funcdef_flag 8029169699Skan ? "cannot define member function %<%T::%s%> within %<%T%>" 8030169699Skan : "cannot declare member function %<%T::%s%> within %<%T%>", 8031169699Skan ctype, name, current_class_type); 8032169699Skan return error_mark_node; 8033169699Skan } 803418334Speter 8035169699Skan if (TREE_CODE (sname) == IDENTIFIER_NODE 8036169699Skan && NEW_DELETE_OPNAME_P (sname)) 8037169699Skan /* Overloaded operator new and operator delete 8038169699Skan are always static functions. */ 8039169699Skan ; 8040169699Skan else 8041169699Skan type = build_memfn_type (type, ctype, memfn_quals); 804218334Speter } 8043169699Skan else if (declspecs->specs[(int)ds_typedef] 8044169699Skan && current_class_type) 8045169699Skan { 8046169699Skan error ("cannot declare member %<%T::%s%> within %qT", 8047169699Skan ctype, name, current_class_type); 8048169699Skan return error_mark_node; 8049169699Skan } 805018334Speter } 805118334Speter 8052169699Skan /* Now TYPE has the actual type. */ 8053169699Skan 805490287Sobrien if (returned_attrs) 805552290Sobrien { 805690287Sobrien if (attrlist) 805790287Sobrien *attrlist = chainon (returned_attrs, *attrlist); 805852290Sobrien else 805990287Sobrien attrlist = &returned_attrs; 806052290Sobrien } 806152290Sobrien 806290287Sobrien /* Did array size calculations overflow? */ 806390287Sobrien 806490287Sobrien if (TREE_CODE (type) == ARRAY_TYPE 806590287Sobrien && COMPLETE_TYPE_P (type) 8066169699Skan && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 8067169699Skan && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) 806818334Speter { 8069169699Skan error ("size of array %qs is too large", name); 807090287Sobrien /* If we proceed with the array type as it is, we'll eventually 807190287Sobrien crash in tree_low_cst(). */ 807290287Sobrien type = error_mark_node; 807390287Sobrien } 807490287Sobrien 8075117410Skan if ((decl_context == FIELD || decl_context == PARM) 8076169699Skan && !processing_template_decl 8077169699Skan && variably_modified_type_p (type, NULL_TREE)) 8078107601Sobrien { 8079117410Skan if (decl_context == FIELD) 8080169699Skan error ("data member may not have variably modified type %qT", type); 8081117410Skan else 8082169699Skan error ("parameter may not have variably modified type %qT", type); 8083107601Sobrien type = error_mark_node; 8084107601Sobrien } 8085107601Sobrien 808690287Sobrien if (explicitp == 1 || (explicitp && friendp)) 808790287Sobrien { 808890287Sobrien /* [dcl.fct.spec] The explicit specifier shall only be used in 8089169699Skan declarations of constructors within a class definition. */ 8090169699Skan error ("only declarations of constructors can be %<explicit%>"); 809118334Speter explicitp = 0; 809218334Speter } 809318334Speter 8094169699Skan if (storage_class == sc_mutable) 809518334Speter { 8096117410Skan if (decl_context != FIELD || friendp) 809718334Speter { 8098169699Skan error ("non-member %qs cannot be declared %<mutable%>", name); 8099169699Skan storage_class = sc_none; 810018334Speter } 8101169699Skan else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef]) 8102169699Skan { 8103169699Skan error ("non-object member %qs cannot be declared %<mutable%>", name); 8104169699Skan storage_class = sc_none; 8105169699Skan } 810690287Sobrien else if (TREE_CODE (type) == FUNCTION_TYPE 8107169699Skan || TREE_CODE (type) == METHOD_TYPE) 8108169699Skan { 8109169699Skan error ("function %qs cannot be declared %<mutable%>", name); 8110169699Skan storage_class = sc_none; 8111169699Skan } 811218334Speter else if (staticp) 811318334Speter { 8114169699Skan error ("static %qs cannot be declared %<mutable%>", name); 8115169699Skan storage_class = sc_none; 811618334Speter } 811790287Sobrien else if (type_quals & TYPE_QUAL_CONST) 811890287Sobrien { 8119169699Skan error ("const %qs cannot be declared %<mutable%>", name); 8120169699Skan storage_class = sc_none; 812190287Sobrien } 812218334Speter } 812318334Speter 812452290Sobrien /* If this is declaring a typedef name, return a TYPE_DECL. */ 8125169699Skan if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME) 812618334Speter { 812718334Speter tree decl; 812818334Speter 812918334Speter /* Note that the grammar rejects storage classes 813018334Speter in typenames, fields or parameters. */ 813151412Sobrien if (current_lang_name == lang_name_java) 813251412Sobrien TYPE_FOR_JAVA (type) = 1; 813318334Speter 8134169699Skan /* This declaration: 8135169699Skan 8136169699Skan typedef void f(int) const; 8137169699Skan 8138169699Skan declares a function type which is not a member of any 8139169699Skan particular class, but which is cv-qualified; for 8140169699Skan example "f S::*" declares a pointer to a const-qualified 8141169699Skan member function of S. We record the cv-qualification in the 8142169699Skan function type. */ 8143169699Skan if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE) 8144169699Skan type = cp_build_qualified_type (type, memfn_quals); 8145169699Skan 814651412Sobrien if (decl_context == FIELD) 8147169699Skan decl = build_lang_decl (TYPE_DECL, unqualified_id, type); 814851412Sobrien else 8149169699Skan decl = build_decl (TYPE_DECL, unqualified_id, type); 8150169699Skan if (id_declarator && declarator->u.id.qualifying_scope) 8151169699Skan error ("%Jtypedef name may not be a nested-name-specifier", decl); 8152169699Skan 8153169699Skan if (decl_context != FIELD) 815451412Sobrien { 815590287Sobrien if (!current_function_decl) 815690287Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 8157169699Skan else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl) 8158169699Skan || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P 8159169699Skan (current_function_decl))) 8160169699Skan /* The TYPE_DECL is "abstract" because there will be 8161169699Skan clones of this constructor/destructor, and there will 8162169699Skan be copies of this TYPE_DECL generated in those 8163169699Skan clones. */ 8164169699Skan DECL_ABSTRACT (decl) = 1; 816551412Sobrien } 8166169699Skan else if (constructor_name_p (unqualified_id, current_class_type)) 8167169699Skan pedwarn ("ISO C++ forbids nested type %qD with same name " 8168169699Skan "as enclosing class", 8169169699Skan unqualified_id); 8170169699Skan 817190287Sobrien /* If the user declares "typedef struct {...} foo" then the 817290287Sobrien struct will have an anonymous name. Fill that name in now. 817390287Sobrien Nothing can refer to it, so nothing needs know about the name 817490287Sobrien change. */ 817518334Speter if (type != error_mark_node 8176169699Skan && unqualified_id 817718334Speter && TYPE_NAME (type) 817818334Speter && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL 817990287Sobrien && TYPE_ANONYMOUS_P (type) 818096292Sobrien /* Don't do this if there are attributes. */ 818196292Sobrien && (!attrlist || !*attrlist) 818290287Sobrien && cp_type_quals (type) == TYPE_UNQUALIFIED) 818318334Speter { 818452290Sobrien tree oldname = TYPE_NAME (type); 818552290Sobrien tree t; 818652290Sobrien 818751412Sobrien /* Replace the anonymous name with the real name everywhere. */ 818852290Sobrien for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 818952290Sobrien if (TYPE_NAME (t) == oldname) 819052290Sobrien TYPE_NAME (t) = decl; 819118334Speter 819218334Speter if (TYPE_LANG_SPECIFIC (type)) 819318334Speter TYPE_WAS_ANONYMOUS (type) = 1; 819418334Speter 819552290Sobrien /* If this is a typedef within a template class, the nested 819652290Sobrien type is a (non-primary) template. The name for the 819752290Sobrien template needs updating as well. */ 819852290Sobrien if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) 819990287Sobrien DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) 820052290Sobrien = TYPE_IDENTIFIER (type); 820152290Sobrien 820252290Sobrien /* FIXME remangle member functions; member functions of a 820352290Sobrien type with external linkage have external linkage. */ 820418334Speter } 820518334Speter 8206169699Skan /* Any qualifiers on a function type typedef have already been 8207169699Skan dealt with. */ 8208169699Skan if (memfn_quals && !ctype && TREE_CODE (type) == FUNCTION_TYPE) 8209169699Skan memfn_quals = TYPE_UNQUALIFIED; 821018334Speter 8211169699Skan if (signed_p 821218334Speter || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 821318334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 821418334Speter 8215169699Skan bad_specifiers (decl, "type", virtualp, 8216169699Skan memfn_quals != TYPE_UNQUALIFIED, 821751412Sobrien inlinep, friendp, raises != NULL_TREE); 821851412Sobrien 821918334Speter return decl; 822018334Speter } 822118334Speter 822218334Speter /* Detect the case of an array type of unspecified size 822318334Speter which came, as such, direct from a typedef name. 822496292Sobrien We must copy the type, so that the array's domain can be 822596292Sobrien individually set by the object's initializer. */ 822618334Speter 822796292Sobrien if (type && typedef_type 822896292Sobrien && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) 822990287Sobrien && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) 823096292Sobrien type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); 823118334Speter 823290287Sobrien /* Detect where we're using a typedef of function type to declare a 8233132747Skan function. PARMS will not be set, so we must create it now. */ 8234169699Skan 823590287Sobrien if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) 823690287Sobrien { 823790287Sobrien tree decls = NULL_TREE; 823890287Sobrien tree args; 823990287Sobrien 824090287Sobrien for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) 824190287Sobrien { 8242117410Skan tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args)); 824390287Sobrien 824490287Sobrien TREE_CHAIN (decl) = decls; 824590287Sobrien decls = decl; 824690287Sobrien } 8247169699Skan 8248132747Skan parms = nreverse (decls); 8249169699Skan 8250169699Skan if (decl_context != TYPENAME) 8251169699Skan { 8252169699Skan /* A cv-qualifier-seq shall only be part of the function type 8253169699Skan for a non-static member function. [8.3.5/4 dcl.fct] */ 8254169699Skan if (cp_type_quals (type) != TYPE_UNQUALIFIED 8255169699Skan && (current_class_type == NULL_TREE || staticp) ) 8256169699Skan { 8257169699Skan error ("qualified function types cannot be used to declare %s functions", 8258169699Skan (staticp? "static member" : "free")); 8259169699Skan type = TYPE_MAIN_VARIANT (type); 8260169699Skan } 8261169699Skan 8262169699Skan /* The qualifiers on the function type become the qualifiers on 8263169699Skan the non-static member function. */ 8264169699Skan memfn_quals |= cp_type_quals (type); 8265169699Skan } 826690287Sobrien } 826790287Sobrien 826818334Speter /* If this is a type name (such as, in a cast or sizeof), 826918334Speter compute the type and return it now. */ 827018334Speter 827118334Speter if (decl_context == TYPENAME) 827218334Speter { 827318334Speter /* Note that the grammar rejects storage classes 827418334Speter in typenames, fields or parameters. */ 827552290Sobrien if (type_quals != TYPE_UNQUALIFIED) 827690287Sobrien type_quals = TYPE_UNQUALIFIED; 827718334Speter 827818334Speter /* Special case: "friend class foo" looks like a TYPENAME context. */ 827918334Speter if (friendp) 828018334Speter { 828152290Sobrien if (type_quals != TYPE_UNQUALIFIED) 828218334Speter { 828390287Sobrien error ("type qualifiers specified for friend class declaration"); 828452290Sobrien type_quals = TYPE_UNQUALIFIED; 828518334Speter } 828618334Speter if (inlinep) 828718334Speter { 8288169699Skan error ("%<inline%> specified for friend class declaration"); 828918334Speter inlinep = 0; 829018334Speter } 829118334Speter 8292117410Skan if (!current_aggr) 829390287Sobrien { 8294117410Skan /* Don't allow friend declaration without a class-key. */ 829590287Sobrien if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 8296117410Skan pedwarn ("template parameters cannot be friends"); 829790287Sobrien else if (TREE_CODE (type) == TYPENAME_TYPE) 8298169699Skan pedwarn ("friend declaration requires class-key, " 8299169699Skan "i.e. %<friend class %T::%D%>", 8300117410Skan TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type)); 830190287Sobrien else 8302169699Skan pedwarn ("friend declaration requires class-key, " 8303169699Skan "i.e. %<friend %#T%>", 8304117410Skan type); 830590287Sobrien } 830690287Sobrien 830718334Speter /* Only try to do this stuff if we didn't already give up. */ 830818334Speter if (type != integer_type_node) 830918334Speter { 831018334Speter /* A friendly class? */ 831118334Speter if (current_class_type) 8312132747Skan make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type), 8313132747Skan /*complain=*/true); 831418334Speter else 8315169699Skan error ("trying to make class %qT a friend of global scope", 8316169699Skan type); 831790287Sobrien 831818334Speter type = void_type_node; 831918334Speter } 832018334Speter } 8321169699Skan else if (memfn_quals) 832218334Speter { 832318334Speter if (ctype == NULL_TREE) 832418334Speter { 832590287Sobrien if (TREE_CODE (type) != METHOD_TYPE) 8326169699Skan error ("invalid qualifiers on non-member function type"); 832790287Sobrien else 8328169699Skan ctype = TYPE_METHOD_BASETYPE (type); 832918334Speter } 833090287Sobrien if (ctype) 8331169699Skan type = build_memfn_type (type, ctype, memfn_quals); 833218334Speter } 833318334Speter 833418334Speter return type; 833518334Speter } 8336169699Skan else if (unqualified_id == NULL_TREE && decl_context != PARM 833718334Speter && decl_context != CATCHPARM 833818334Speter && TREE_CODE (type) != UNION_TYPE 833918334Speter && ! bitfield) 834018334Speter { 8341169699Skan error ("abstract declarator %qT used as declaration", type); 8342146906Skan return error_mark_node; 834318334Speter } 834418334Speter 8345146906Skan /* Only functions may be declared using an operator-function-id. */ 8346169699Skan if (unqualified_id 8347169699Skan && IDENTIFIER_OPNAME_P (unqualified_id) 8348146906Skan && TREE_CODE (type) != FUNCTION_TYPE 8349146906Skan && TREE_CODE (type) != METHOD_TYPE) 835018334Speter { 8351169699Skan error ("declaration of %qD as non-function", unqualified_id); 8352146906Skan return error_mark_node; 835318334Speter } 835418334Speter 8355146906Skan /* We don't check parameter types here because we can emit a better 8356146906Skan error message later. */ 8357146906Skan if (decl_context != PARM) 8358169699Skan { 8359169699Skan type = check_var_type (unqualified_id, type); 8360169699Skan if (type == error_mark_node) 8361169699Skan return error_mark_node; 8362169699Skan } 8363146906Skan 836418334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 836518334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 836618334Speter 836752290Sobrien if (decl_context == PARM || decl_context == CATCHPARM) 836852290Sobrien { 836952290Sobrien if (ctype || in_namespace) 8370169699Skan error ("cannot use %<::%> in parameter declaration"); 837152290Sobrien 837252290Sobrien /* A parameter declared as an array of T is really a pointer to T. 837352290Sobrien One declared as a function is really a pointer to a function. 837452290Sobrien One declared as a member is really a pointer to member. */ 837552290Sobrien 837652290Sobrien if (TREE_CODE (type) == ARRAY_TYPE) 837752290Sobrien { 837852290Sobrien /* Transfer const-ness of array into that of type pointed to. */ 837952290Sobrien type = build_pointer_type (TREE_TYPE (type)); 838052290Sobrien type_quals = TYPE_UNQUALIFIED; 838152290Sobrien } 838252290Sobrien else if (TREE_CODE (type) == FUNCTION_TYPE) 838352290Sobrien type = build_pointer_type (type); 838452290Sobrien } 838590287Sobrien 838618334Speter { 8387132747Skan tree decl; 838818334Speter 838918334Speter if (decl_context == PARM) 839018334Speter { 8391169699Skan decl = cp_build_parm_decl (unqualified_id, type); 839218334Speter 8393169699Skan bad_specifiers (decl, "parameter", virtualp, 8394169699Skan memfn_quals != TYPE_UNQUALIFIED, 839518334Speter inlinep, friendp, raises != NULL_TREE); 839618334Speter } 839718334Speter else if (decl_context == FIELD) 839818334Speter { 8399132747Skan /* The C99 flexible array extension. */ 8400132747Skan if (!staticp && TREE_CODE (type) == ARRAY_TYPE 8401132747Skan && TYPE_DOMAIN (type) == NULL_TREE) 8402132747Skan { 8403132747Skan tree itype = compute_array_index_type (dname, integer_zero_node); 8404132747Skan type = build_cplus_array_type (TREE_TYPE (type), itype); 8405132747Skan } 8406132747Skan 840718334Speter if (type == error_mark_node) 840818334Speter { 840918334Speter /* Happens when declaring arrays of sizes which 841018334Speter are error_mark_node, for example. */ 841118334Speter decl = NULL_TREE; 841218334Speter } 841352290Sobrien else if (in_namespace && !friendp) 841452290Sobrien { 841552290Sobrien /* Something like struct S { int N::j; }; */ 8416169699Skan error ("invalid use of %<::%>"); 8417169699Skan return error_mark_node; 841852290Sobrien } 841918334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 842018334Speter { 842118334Speter int publicp = 0; 842251412Sobrien tree function_context; 842318334Speter 842418334Speter if (friendp == 0) 842518334Speter { 842618334Speter if (ctype == NULL_TREE) 842718334Speter ctype = current_class_type; 842818334Speter 842918334Speter if (ctype == NULL_TREE) 843018334Speter { 8431169699Skan error ("can't make %qD into a method -- not in a class", 8432169699Skan unqualified_id); 8433169699Skan return error_mark_node; 843418334Speter } 843518334Speter 843618334Speter /* ``A union may [ ... ] not [ have ] virtual functions.'' 843718334Speter ARM 9.5 */ 843818334Speter if (virtualp && TREE_CODE (ctype) == UNION_TYPE) 843918334Speter { 8440169699Skan error ("function %qD declared virtual inside a union", 8441169699Skan unqualified_id); 8442169699Skan return error_mark_node; 844318334Speter } 844418334Speter 8445169699Skan if (NEW_DELETE_OPNAME_P (unqualified_id)) 844618334Speter { 844718334Speter if (virtualp) 844818334Speter { 8449169699Skan error ("%qD cannot be declared virtual, since it " 8450169699Skan "is always static", 8451169699Skan unqualified_id); 845218334Speter virtualp = 0; 845318334Speter } 845418334Speter } 845518334Speter else if (staticp < 2) 8456169699Skan type = build_memfn_type (type, ctype, memfn_quals); 845718334Speter } 845818334Speter 8459169699Skan /* Check that the name used for a destructor makes sense. */ 8460169699Skan if (sfk == sfk_destructor) 8461169699Skan { 8462169699Skan if (!ctype) 8463169699Skan { 8464169699Skan gcc_assert (friendp); 8465169699Skan error ("expected qualified name in friend declaration " 8466169699Skan "for destructor %qD", 8467169699Skan id_declarator->u.id.unqualified_name); 8468169699Skan return error_mark_node; 8469169699Skan } 8470169699Skan 8471169699Skan if (!same_type_p (TREE_OPERAND 8472169699Skan (id_declarator->u.id.unqualified_name, 0), 8473169699Skan ctype)) 8474169699Skan { 8475169699Skan error ("declaration of %qD as member of %qT", 8476169699Skan id_declarator->u.id.unqualified_name, ctype); 8477169699Skan return error_mark_node; 8478169699Skan } 8479169699Skan } 8480169699Skan 848118334Speter /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ 848290287Sobrien function_context = (ctype != NULL_TREE) ? 848390287Sobrien decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; 848451412Sobrien publicp = (! friendp || ! staticp) 848551412Sobrien && function_context == NULL_TREE; 848690287Sobrien decl = grokfndecl (ctype, type, 8487169699Skan TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR 8488169699Skan ? unqualified_id : dname, 8489132747Skan parms, 8490169699Skan unqualified_id, 8491169699Skan virtualp, flags, memfn_quals, raises, 849251412Sobrien friendp ? -1 : 0, friendp, publicp, inlinep, 8493169699Skan sfk, 8494169699Skan funcdef_flag, template_count, in_namespace, attrlist); 849518334Speter if (decl == NULL_TREE) 8496169699Skan return error_mark_node; 849751412Sobrien#if 0 849851412Sobrien /* This clobbers the attrs stored in `decl' from `attrlist'. */ 849990287Sobrien /* The decl and setting of decl_attr is also turned off. */ 850090287Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 850151412Sobrien#endif 850218334Speter 850352290Sobrien /* [class.conv.ctor] 850452290Sobrien 850552290Sobrien A constructor declared without the function-specifier 850652290Sobrien explicit that can be called with a single parameter 850752290Sobrien specifies a conversion from the type of its first 850852290Sobrien parameter to the type of its class. Such a constructor 850952290Sobrien is called a converting constructor. */ 851018334Speter if (explicitp == 2) 851118334Speter DECL_NONCONVERTING_P (decl) = 1; 851252290Sobrien else if (DECL_CONSTRUCTOR_P (decl)) 851352290Sobrien { 851452290Sobrien /* The constructor can be called with exactly one 851552290Sobrien parameter if there is at least one parameter, and 851652290Sobrien any subsequent parameters have default arguments. 851790287Sobrien Ignore any compiler-added parms. */ 851890287Sobrien tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl); 851952290Sobrien 852052290Sobrien if (arg_types == void_list_node 852190287Sobrien || (arg_types 852290287Sobrien && TREE_CHAIN (arg_types) 852352290Sobrien && TREE_CHAIN (arg_types) != void_list_node 852452290Sobrien && !TREE_PURPOSE (TREE_CHAIN (arg_types)))) 852552290Sobrien DECL_NONCONVERTING_P (decl) = 1; 852652290Sobrien } 852718334Speter } 852818334Speter else if (TREE_CODE (type) == METHOD_TYPE) 852918334Speter { 853018334Speter /* We only get here for friend declarations of 853118334Speter members of other classes. */ 853218334Speter /* All method decls are public, so tell grokfndecl to set 853318334Speter TREE_PUBLIC, also. */ 8534122192Skan decl = grokfndecl (ctype, type, 8535169699Skan TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR 8536169699Skan ? unqualified_id : dname, 8537132747Skan parms, 8538169699Skan unqualified_id, 8539169699Skan virtualp, flags, memfn_quals, raises, 8540169699Skan friendp ? -1 : 0, friendp, 1, 0, sfk, 8541169699Skan funcdef_flag, template_count, in_namespace, 8542169699Skan attrlist); 854318334Speter if (decl == NULL_TREE) 8544169699Skan return error_mark_node; 854518334Speter } 8546132747Skan else if (!staticp && !dependent_type_p (type) 854790287Sobrien && !COMPLETE_TYPE_P (complete_type (type)) 854818334Speter && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0)) 854918334Speter { 8550169699Skan if (unqualified_id) 8551169699Skan error ("field %qD has incomplete type", unqualified_id); 855218334Speter else 8553169699Skan error ("name %qT has incomplete type", type); 855418334Speter 855518334Speter /* If we're instantiating a template, tell them which 855618334Speter instantiation made the field's type be incomplete. */ 855718334Speter if (current_class_type 855818334Speter && TYPE_NAME (current_class_type) 855951412Sobrien && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)) 8560169699Skan && declspecs->type 8561169699Skan && declspecs->type == type) 8562169699Skan error (" in instantiation of template %qT", 8563169699Skan current_class_type); 856418334Speter 8565169699Skan return error_mark_node; 856618334Speter } 856718334Speter else 856818334Speter { 856918334Speter if (friendp) 857018334Speter { 8571169699Skan error ("%qE is neither function nor member function; " 8572169699Skan "cannot be declared friend", unqualified_id); 857318334Speter friendp = 0; 857418334Speter } 857518334Speter decl = NULL_TREE; 857618334Speter } 857718334Speter 857818334Speter if (friendp) 857918334Speter { 858018334Speter /* Friends are treated specially. */ 858118334Speter if (ctype == current_class_type) 8582169699Skan ; /* We already issued a pedwarn. */ 8583169699Skan else if (decl && DECL_NAME (decl)) 8584132747Skan { 8585132747Skan if (template_class_depth (current_class_type) == 0) 8586132747Skan { 8587132747Skan decl = check_explicit_specialization 8588169699Skan (unqualified_id, decl, template_count, 8589169699Skan 2 * funcdef_flag + 4); 8590132747Skan if (decl == error_mark_node) 8591132747Skan return error_mark_node; 8592132747Skan } 8593169699Skan 8594169699Skan decl = do_friend (ctype, unqualified_id, decl, 8595169699Skan *attrlist, flags, 8596169699Skan funcdef_flag); 8597132747Skan return decl; 8598132747Skan } 8599132747Skan else 8600169699Skan return error_mark_node; 860118334Speter } 860218334Speter 8603132747Skan /* Structure field. It may not be a function, except for C++. */ 860418334Speter 860518334Speter if (decl == NULL_TREE) 860618334Speter { 860718334Speter if (initialized) 860818334Speter { 860951412Sobrien if (!staticp) 861051412Sobrien { 861151412Sobrien /* An attempt is being made to initialize a non-static 861251412Sobrien member. But, from [class.mem]: 861390287Sobrien 861451412Sobrien 4 A member-declarator can contain a 861551412Sobrien constant-initializer only if it declares a static 861651412Sobrien member (_class.static_) of integral or enumeration 861790287Sobrien type, see _class.static.data_. 861851412Sobrien 861951412Sobrien This used to be relatively common practice, but 862051412Sobrien the rest of the compiler does not correctly 862151412Sobrien handle the initialization unless the member is 862251412Sobrien static so we make it static below. */ 8623169699Skan pedwarn ("ISO C++ forbids initialization of member %qD", 8624169699Skan unqualified_id); 8625169699Skan pedwarn ("making %qD static", unqualified_id); 862651412Sobrien staticp = 1; 862751412Sobrien } 862851412Sobrien 862952290Sobrien if (uses_template_parms (type)) 863052290Sobrien /* We'll check at instantiation time. */ 863152290Sobrien ; 8632169699Skan else if (check_static_variable_definition (unqualified_id, 863352290Sobrien type)) 863452290Sobrien /* If we just return the declaration, crashes 863552290Sobrien will sometimes occur. We therefore return 863652290Sobrien void_type_node, as if this was a friend 863752290Sobrien declaration, to cause callers to completely 863852290Sobrien ignore this declaration. */ 8639169699Skan return error_mark_node; 864018334Speter } 864118334Speter 864251412Sobrien if (staticp) 864318334Speter { 864490287Sobrien /* C++ allows static class members. All other work 864590287Sobrien for this is done by grokfield. */ 8646169699Skan decl = build_lang_decl (VAR_DECL, unqualified_id, type); 8647169699Skan set_linkage_for_static_data_member (decl); 8648169699Skan /* Even if there is an in-class initialization, DECL 8649169699Skan is considered undefined until an out-of-class 8650169699Skan definition is provided. */ 8651169699Skan DECL_EXTERNAL (decl) = 1; 8652169699Skan 8653169699Skan if (thread_p) 8654169699Skan { 8655169699Skan if (targetm.have_tls) 8656169699Skan DECL_TLS_MODEL (decl) = decl_default_tls_model (decl); 8657169699Skan else 8658169699Skan /* A mere warning is sure to result in improper 8659169699Skan semantics at runtime. Don't bother to allow this to 8660169699Skan compile. */ 8661169699Skan error ("thread-local storage not supported for this target"); 8662169699Skan } 866318334Speter } 866418334Speter else 866518334Speter { 8666169699Skan decl = build_decl (FIELD_DECL, unqualified_id, type); 866790287Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 8668169699Skan if (storage_class == sc_mutable) 866918334Speter { 867018334Speter DECL_MUTABLE_P (decl) = 1; 8671169699Skan storage_class = sc_none; 867218334Speter } 867318334Speter } 867418334Speter 8675169699Skan bad_specifiers (decl, "field", virtualp, 8676169699Skan memfn_quals != TYPE_UNQUALIFIED, 867718334Speter inlinep, friendp, raises != NULL_TREE); 867818334Speter } 867918334Speter } 8680132747Skan else if (TREE_CODE (type) == FUNCTION_TYPE 8681132747Skan || TREE_CODE (type) == METHOD_TYPE) 868218334Speter { 868351412Sobrien tree original_name; 868418334Speter int publicp = 0; 868518334Speter 8686169699Skan if (!unqualified_id) 8687169699Skan return error_mark_node; 868818334Speter 8689169699Skan if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) 869051412Sobrien original_name = dname; 869151412Sobrien else 8692169699Skan original_name = unqualified_id; 869351412Sobrien 8694169699Skan if (storage_class == sc_auto) 8695169699Skan error ("storage class %<auto%> invalid for function %qs", name); 8696169699Skan else if (storage_class == sc_register) 8697169699Skan error ("storage class %<register%> invalid for function %qs", name); 8698169699Skan else if (thread_p) 8699169699Skan error ("storage class %<__thread%> invalid for function %qs", name); 870018334Speter 870118334Speter /* Function declaration not at top level. 870218334Speter Storage classes other than `extern' are not allowed 870318334Speter and `extern' makes no difference. */ 870418334Speter if (! toplevel_bindings_p () 8705169699Skan && (storage_class == sc_static 8706169699Skan || declspecs->specs[(int)ds_inline]) 870718334Speter && pedantic) 870818334Speter { 8709169699Skan if (storage_class == sc_static) 8710169699Skan pedwarn ("%<static%> specified invalid for function %qs " 8711169699Skan "declared out of global scope", name); 871218334Speter else 8713169699Skan pedwarn ("%<inline%> specifier invalid for function %qs " 8714169699Skan "declared out of global scope", name); 871518334Speter } 871690287Sobrien 871718334Speter if (ctype == NULL_TREE) 871818334Speter { 871918334Speter if (virtualp) 872018334Speter { 8721169699Skan error ("virtual non-class function %qs", name); 872218334Speter virtualp = 0; 872318334Speter } 872418334Speter } 8725132747Skan else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2 8726132747Skan && !NEW_DELETE_OPNAME_P (original_name)) 8727169699Skan type = build_method_type_directly (ctype, 8728132747Skan TREE_TYPE (type), 8729132747Skan TYPE_ARG_TYPES (type)); 873018334Speter 873151412Sobrien /* Record presence of `static'. */ 873218334Speter publicp = (ctype != NULL_TREE 8733169699Skan || storage_class == sc_extern 8734169699Skan || storage_class != sc_static); 873518334Speter 8736169699Skan decl = grokfndecl (ctype, type, original_name, parms, unqualified_id, 8737169699Skan virtualp, flags, memfn_quals, raises, 873851412Sobrien 1, friendp, 8739169699Skan publicp, inlinep, sfk, funcdef_flag, 8740169699Skan template_count, in_namespace, attrlist); 874118334Speter if (decl == NULL_TREE) 8742169699Skan return error_mark_node; 874318334Speter 874418334Speter if (staticp == 1) 874518334Speter { 8746117410Skan int invalid_static = 0; 874718334Speter 874818334Speter /* Don't allow a static member function in a class, and forbid 874918334Speter declaring main to be static. */ 875018334Speter if (TREE_CODE (type) == METHOD_TYPE) 875118334Speter { 8752169699Skan pedwarn ("cannot declare member function %qD to have " 8753169699Skan "static linkage", decl); 8754117410Skan invalid_static = 1; 875518334Speter } 875618334Speter else if (current_function_decl) 875718334Speter { 875818334Speter /* FIXME need arm citation */ 875918334Speter error ("cannot declare static function inside another function"); 8760117410Skan invalid_static = 1; 876118334Speter } 876218334Speter 8763117410Skan if (invalid_static) 876418334Speter { 876518334Speter staticp = 0; 8766169699Skan storage_class = sc_none; 876718334Speter } 876818334Speter } 876918334Speter } 877018334Speter else 877118334Speter { 877218334Speter /* It's a variable. */ 877318334Speter 877418334Speter /* An uninitialized decl with `extern' is a reference. */ 8775169699Skan decl = grokvardecl (type, unqualified_id, 8776169699Skan declspecs, 877790287Sobrien initialized, 877890287Sobrien (type_quals & TYPE_QUAL_CONST) != 0, 8779117410Skan ctype ? ctype : in_namespace); 8780169699Skan bad_specifiers (decl, "variable", virtualp, 8781169699Skan memfn_quals != TYPE_UNQUALIFIED, 878218334Speter inlinep, friendp, raises != NULL_TREE); 878318334Speter 878418334Speter if (ctype) 878518334Speter { 878618334Speter DECL_CONTEXT (decl) = ctype; 878718334Speter if (staticp == 1) 878818334Speter { 8789169699Skan pedwarn ("%<static%> may not be used when defining " 8790169699Skan "(as opposed to declaring) a static data member"); 8791169699Skan staticp = 0; 8792169699Skan storage_class = sc_none; 879318334Speter } 8794169699Skan if (storage_class == sc_register && TREE_STATIC (decl)) 879518334Speter { 8796169699Skan error ("static member %qD declared %<register%>", decl); 8797169699Skan storage_class = sc_none; 879818334Speter } 8799169699Skan if (storage_class == sc_extern && pedantic) 880018334Speter { 8801169699Skan pedwarn ("cannot explicitly declare member %q#D to have " 8802169699Skan "extern linkage", 8803169699Skan decl); 8804169699Skan storage_class = sc_none; 880518334Speter } 880618334Speter } 880718334Speter } 880818334Speter 880918334Speter /* Record `register' declaration for warnings on & 881018334Speter and in case doing stupid register allocation. */ 881118334Speter 8812169699Skan if (storage_class == sc_register) 881318334Speter DECL_REGISTER (decl) = 1; 8814169699Skan else if (storage_class == sc_extern) 881518334Speter DECL_THIS_EXTERN (decl) = 1; 8816169699Skan else if (storage_class == sc_static) 881718334Speter DECL_THIS_STATIC (decl) = 1; 881818334Speter 881990287Sobrien /* Record constancy and volatility. There's no need to do this 882090287Sobrien when processing a template; we'll do this for the instantiated 882190287Sobrien declaration based on the type of DECL. */ 882290287Sobrien if (!processing_template_decl) 8823169699Skan cp_apply_type_quals_to_decl (type_quals, decl); 882418334Speter 882518334Speter return decl; 882618334Speter } 882718334Speter} 882818334Speter 882952290Sobrien/* Subroutine of start_function. Ensure that each of the parameter 883052290Sobrien types (as listed in PARMS) is complete, as is required for a 883152290Sobrien function definition. */ 883218334Speter 883318334Speterstatic void 8834132747Skanrequire_complete_types_for_parms (tree parms) 883518334Speter{ 883690287Sobrien for (; parms; parms = TREE_CHAIN (parms)) 883718334Speter { 8838169699Skan if (dependent_type_p (TREE_TYPE (parms))) 8839169699Skan continue; 8840169699Skan if (!VOID_TYPE_P (TREE_TYPE (parms)) 8841169699Skan && complete_type_or_else (TREE_TYPE (parms), parms)) 8842117410Skan { 8843169699Skan relayout_decl (parms); 8844117410Skan DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms)); 8845117410Skan } 8846169699Skan else 8847169699Skan /* grokparms or complete_type_or_else will have already issued 8848169699Skan an error. */ 8849169699Skan TREE_TYPE (parms) = error_mark_node; 885018334Speter } 885118334Speter} 885218334Speter 8853117410Skan/* Returns nonzero if T is a local variable. */ 885452290Sobrien 885590287Sobrienint 8856132747Skanlocal_variable_p (tree t) 885752290Sobrien{ 885890287Sobrien if ((TREE_CODE (t) == VAR_DECL 885952290Sobrien /* A VAR_DECL with a context that is a _TYPE is a static data 886052290Sobrien member. */ 886152290Sobrien && !TYPE_P (CP_DECL_CONTEXT (t)) 886252290Sobrien /* Any other non-local variable must be at namespace scope. */ 886390287Sobrien && !DECL_NAMESPACE_SCOPE_P (t)) 886452290Sobrien || (TREE_CODE (t) == PARM_DECL)) 886590287Sobrien return 1; 886652290Sobrien 886790287Sobrien return 0; 886852290Sobrien} 886952290Sobrien 8870117410Skan/* Returns nonzero if T is an automatic local variable or a label. 887190287Sobrien (These are the declarations that need to be remapped when the code 887290287Sobrien containing them is duplicated.) */ 887390287Sobrien 887490287Sobrienint 8875132747Skannonstatic_local_decl_p (tree t) 887690287Sobrien{ 887790287Sobrien return ((local_variable_p (t) && !TREE_STATIC (t)) 887890287Sobrien || TREE_CODE (t) == LABEL_DECL 887990287Sobrien || TREE_CODE (t) == RESULT_DECL); 888090287Sobrien} 888190287Sobrien 888290287Sobrien/* Like local_variable_p, but suitable for use as a tree-walking 888390287Sobrien function. */ 888490287Sobrien 888590287Sobrienstatic tree 8886169699Skanlocal_variable_p_walkfn (tree *tp, int *walk_subtrees, 8887169699Skan void *data ATTRIBUTE_UNUSED) 888890287Sobrien{ 8889169699Skan if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) 8890169699Skan return *tp; 8891169699Skan else if (TYPE_P (*tp)) 8892169699Skan *walk_subtrees = 0; 8893169699Skan 8894169699Skan return NULL_TREE; 889590287Sobrien} 889690287Sobrien 8897169699Skan 889852290Sobrien/* Check that ARG, which is a default-argument expression for a 8899117410Skan parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if 890052290Sobrien something goes wrong. DECL may also be a _TYPE node, rather than a 890152290Sobrien DECL, if there is no DECL available. */ 890252290Sobrien 890352290Sobrientree 8904132747Skancheck_default_argument (tree decl, tree arg) 890552290Sobrien{ 890652290Sobrien tree var; 890752290Sobrien tree decl_type; 890852290Sobrien 890952290Sobrien if (TREE_CODE (arg) == DEFAULT_ARG) 891052290Sobrien /* We get a DEFAULT_ARG when looking at an in-class declaration 891152290Sobrien with a default argument. Ignore the argument for now; we'll 891252290Sobrien deal with it after the class is complete. */ 891352290Sobrien return arg; 891452290Sobrien 891552290Sobrien if (TYPE_P (decl)) 891652290Sobrien { 891752290Sobrien decl_type = decl; 891852290Sobrien decl = NULL_TREE; 891952290Sobrien } 892052290Sobrien else 892152290Sobrien decl_type = TREE_TYPE (decl); 892252290Sobrien 892390287Sobrien if (arg == error_mark_node 892452290Sobrien || decl == error_mark_node 892552290Sobrien || TREE_TYPE (arg) == error_mark_node 892652290Sobrien || decl_type == error_mark_node) 892752290Sobrien /* Something already went wrong. There's no need to check 892852290Sobrien further. */ 892952290Sobrien return error_mark_node; 893052290Sobrien 893152290Sobrien /* [dcl.fct.default] 893290287Sobrien 893352290Sobrien A default argument expression is implicitly converted to the 893452290Sobrien parameter type. */ 893552290Sobrien if (!TREE_TYPE (arg) 8936169699Skan || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL)) 893752290Sobrien { 893852290Sobrien if (decl) 8939169699Skan error ("default argument for %q#D has type %qT", 8940169699Skan decl, TREE_TYPE (arg)); 894152290Sobrien else 8942169699Skan error ("default argument for parameter of type %qT has type %qT", 8943169699Skan decl_type, TREE_TYPE (arg)); 894452290Sobrien 894552290Sobrien return error_mark_node; 894652290Sobrien } 894752290Sobrien 894852290Sobrien /* [dcl.fct.default] 894952290Sobrien 895052290Sobrien Local variables shall not be used in default argument 895190287Sobrien expressions. 895252290Sobrien 895352290Sobrien The keyword `this' shall not be used in a default argument of a 895452290Sobrien member function. */ 895590287Sobrien var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn, 895690287Sobrien NULL); 895752290Sobrien if (var) 895852290Sobrien { 8959169699Skan error ("default argument %qE uses local variable %qD", arg, var); 896052290Sobrien return error_mark_node; 896152290Sobrien } 896252290Sobrien 896352290Sobrien /* All is well. */ 896452290Sobrien return arg; 896552290Sobrien} 896652290Sobrien 896718334Speter/* Decode the list of parameter types for a function type. 896818334Speter Given the list of things declared inside the parens, 896918334Speter return a list of types. 897018334Speter 8971169699Skan If this parameter does not end with an ellipsis, we append 8972169699Skan void_list_node. 897318334Speter 8974132747Skan *PARMS is set to the chain of PARM_DECLs created. */ 897518334Speter 897618334Speterstatic tree 8977169699Skangrokparms (cp_parameter_declarator *first_parm, tree *parms) 897818334Speter{ 897918334Speter tree result = NULL_TREE; 898018334Speter tree decls = NULL_TREE; 8981169699Skan int ellipsis = !first_parm || first_parm->ellipsis_p; 8982169699Skan cp_parameter_declarator *parm; 898390287Sobrien int any_error = 0; 898418334Speter 8985169699Skan for (parm = first_parm; parm != NULL; parm = parm->next) 898618334Speter { 898790287Sobrien tree type = NULL_TREE; 8988169699Skan tree init = parm->default_argument; 8989169699Skan tree attrs; 8990169699Skan tree decl; 899118334Speter 8992169699Skan if (parm == no_parameters) 8993169699Skan break; 899418334Speter 8995169699Skan attrs = parm->decl_specifiers.attributes; 8996169699Skan parm->decl_specifiers.attributes = NULL_TREE; 8997169699Skan decl = grokdeclarator (parm->declarator, &parm->decl_specifiers, 899890287Sobrien PARM, init != NULL_TREE, &attrs); 899990287Sobrien if (! decl || TREE_TYPE (decl) == error_mark_node) 9000169699Skan continue; 900118334Speter 900290287Sobrien if (attrs) 900390287Sobrien cplus_decl_attributes (&decl, attrs, 0); 900452290Sobrien 900590287Sobrien type = TREE_TYPE (decl); 900690287Sobrien if (VOID_TYPE_P (type)) 9007169699Skan { 9008169699Skan if (same_type_p (type, void_type_node) 9009169699Skan && DECL_SELF_REFERENCE_P (type) 9010169699Skan && !DECL_NAME (decl) && !result && !parm->next && !ellipsis) 9011169699Skan /* this is a parmlist of `(void)', which is ok. */ 9012169699Skan break; 9013169699Skan cxx_incomplete_type_error (decl, type); 901490287Sobrien /* It's not a good idea to actually create parameters of 901590287Sobrien type `void'; other parts of the compiler assume that a 901690287Sobrien void type terminates the parameter list. */ 901790287Sobrien type = error_mark_node; 901890287Sobrien TREE_TYPE (decl) = error_mark_node; 9019169699Skan } 902052290Sobrien 902190287Sobrien if (type != error_mark_node) 902290287Sobrien { 902390287Sobrien /* Top-level qualifiers on the parameters are 902490287Sobrien ignored for function types. */ 9025132747Skan type = cp_build_qualified_type (type, 0); 902690287Sobrien if (TREE_CODE (type) == METHOD_TYPE) 902790287Sobrien { 9028169699Skan error ("parameter %qD invalidly declared method type", decl); 902990287Sobrien type = build_pointer_type (type); 903090287Sobrien TREE_TYPE (decl) = type; 903190287Sobrien } 903290287Sobrien else if (abstract_virtuals_error (decl, type)) 9033117410Skan any_error = 1; /* Seems like a good idea. */ 903490287Sobrien else if (POINTER_TYPE_P (type)) 903590287Sobrien { 903690287Sobrien /* [dcl.fct]/6, parameter types cannot contain pointers 903790287Sobrien (references) to arrays of unknown bound. */ 903890287Sobrien tree t = TREE_TYPE (type); 903990287Sobrien int ptr = TYPE_PTR_P (type); 904018334Speter 9041169699Skan while (1) 9042169699Skan { 9043169699Skan if (TYPE_PTR_P (t)) 9044169699Skan ptr = 1; 9045169699Skan else if (TREE_CODE (t) != ARRAY_TYPE) 9046169699Skan break; 9047169699Skan else if (!TYPE_DOMAIN (t)) 9048169699Skan break; 9049169699Skan t = TREE_TYPE (t); 9050169699Skan } 905190287Sobrien if (TREE_CODE (t) == ARRAY_TYPE) 9052169699Skan error ("parameter %qD includes %s to array of unknown " 9053169699Skan "bound %qT", 9054169699Skan decl, ptr ? "pointer" : "reference", t); 905590287Sobrien } 905618334Speter 9057169699Skan if (any_error) 9058169699Skan init = NULL_TREE; 9059169699Skan else if (init && !processing_template_decl) 906090287Sobrien init = check_default_argument (decl, init); 906190287Sobrien } 906218334Speter 906390287Sobrien TREE_CHAIN (decl) = decls; 906490287Sobrien decls = decl; 906590287Sobrien result = tree_cons (init, type, result); 906618334Speter } 906790287Sobrien decls = nreverse (decls); 906890287Sobrien result = nreverse (result); 906990287Sobrien if (!ellipsis) 907090287Sobrien result = chainon (result, void_list_node); 9071132747Skan *parms = decls; 907218334Speter 907318334Speter return result; 907418334Speter} 907551412Sobrien 907618334Speter 907790287Sobrien/* D is a constructor or overloaded `operator='. 907860970Sobrien 907990287Sobrien Let T be the class in which D is declared. Then, this function 908090287Sobrien returns: 908190287Sobrien 908290287Sobrien -1 if D's is an ill-formed constructor or copy assignment operator 908390287Sobrien whose first parameter is of type `T'. 908490287Sobrien 0 if D is not a copy constructor or copy assignment 908590287Sobrien operator. 908690287Sobrien 1 if D is a copy constructor or copy assignment operator whose 908790287Sobrien first parameter is a reference to const qualified T. 908890287Sobrien 2 if D is a copy constructor or copy assignment operator whose 908990287Sobrien first parameter is a reference to non-const qualified T. 909090287Sobrien 909190287Sobrien This function can be used as a predicate. Positive values indicate 9092117410Skan a copy constructor and nonzero values indicate a copy assignment 909390287Sobrien operator. */ 909490287Sobrien 909551412Sobrienint 9096132747Skancopy_fn_p (tree d) 909751412Sobrien{ 909890287Sobrien tree args; 909990287Sobrien tree arg_type; 910090287Sobrien int result = 1; 910151412Sobrien 9102169699Skan gcc_assert (DECL_FUNCTION_MEMBER_P (d)); 9103169699Skan 9104169699Skan if (TREE_CODE (d) == TEMPLATE_DECL 9105169699Skan || (DECL_TEMPLATE_INFO (d) 9106169699Skan && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d)))) 910790287Sobrien /* Instantiations of template member functions are never copy 910890287Sobrien functions. Note that member functions of templated classes are 910990287Sobrien represented as template functions internally, and we must 911090287Sobrien accept those as copy functions. */ 911190287Sobrien return 0; 9112169699Skan 911390287Sobrien args = FUNCTION_FIRST_USER_PARMTYPE (d); 911490287Sobrien if (!args) 911590287Sobrien return 0; 911651412Sobrien 911790287Sobrien arg_type = TREE_VALUE (args); 9118169699Skan if (arg_type == error_mark_node) 9119169699Skan return 0; 912018334Speter 912190287Sobrien if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) 912218334Speter { 912390287Sobrien /* Pass by value copy assignment operator. */ 912490287Sobrien result = -1; 912518334Speter } 912690287Sobrien else if (TREE_CODE (arg_type) == REFERENCE_TYPE 912790287Sobrien && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) 912860970Sobrien { 912990287Sobrien if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) 913090287Sobrien result = 2; 913160970Sobrien } 913290287Sobrien else 913390287Sobrien return 0; 9134169699Skan 913590287Sobrien args = TREE_CHAIN (args); 913660970Sobrien 913790287Sobrien if (args && args != void_list_node && !TREE_PURPOSE (args)) 913890287Sobrien /* There are more non-optional args. */ 913990287Sobrien return 0; 914070639Sobrien 914190287Sobrien return result; 914290287Sobrien} 914352290Sobrien 914490287Sobrien/* Remember any special properties of member function DECL. */ 914590287Sobrien 9146132747Skanvoid grok_special_member_properties (tree decl) 914790287Sobrien{ 9148169699Skan tree class_type; 9149169699Skan 9150169699Skan if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 9151169699Skan return; 9152169699Skan 9153169699Skan class_type = DECL_CONTEXT (decl); 9154169699Skan if (DECL_CONSTRUCTOR_P (decl)) 915518334Speter { 915690287Sobrien int ctor = copy_fn_p (decl); 9157169699Skan 9158169699Skan TYPE_HAS_CONSTRUCTOR (class_type) = 1; 9159169699Skan 916090287Sobrien if (ctor > 0) 916190287Sobrien { 916290287Sobrien /* [class.copy] 9163169699Skan 9164169699Skan A non-template constructor for class X is a copy 9165169699Skan constructor if its first parameter is of type X&, const 9166169699Skan X&, volatile X& or const volatile X&, and either there 9167169699Skan are no other parameters or else all other parameters have 9168169699Skan default arguments. */ 9169169699Skan TYPE_HAS_INIT_REF (class_type) = 1; 917090287Sobrien if (ctor > 1) 9171169699Skan TYPE_HAS_CONST_INIT_REF (class_type) = 1; 917290287Sobrien } 917390287Sobrien else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) 9174169699Skan TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1; 917518334Speter } 917690287Sobrien else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR) 917790287Sobrien { 917890287Sobrien /* [class.copy] 9179169699Skan 9180169699Skan A non-template assignment operator for class X is a copy 9181169699Skan assignment operator if its parameter is of type X, X&, const 9182169699Skan X&, volatile X& or const volatile X&. */ 9183169699Skan 918490287Sobrien int assop = copy_fn_p (decl); 9185169699Skan 918690287Sobrien if (assop) 918790287Sobrien { 9188169699Skan TYPE_HAS_ASSIGN_REF (class_type) = 1; 918990287Sobrien if (assop != 1) 9190169699Skan TYPE_HAS_CONST_ASSIGN_REF (class_type) = 1; 919190287Sobrien } 919290287Sobrien } 919390287Sobrien} 919452290Sobrien 919590287Sobrien/* Check a constructor DECL has the correct form. Complains 919690287Sobrien if the class has a constructor of the form X(X). */ 919752290Sobrien 919890287Sobrienint 9199132747Skangrok_ctor_properties (tree ctype, tree decl) 920090287Sobrien{ 920190287Sobrien int ctor_parm = copy_fn_p (decl); 920290287Sobrien 920390287Sobrien if (ctor_parm < 0) 920418334Speter { 920590287Sobrien /* [class.copy] 9206169699Skan 9207169699Skan A declaration of a constructor for a class X is ill-formed if 9208169699Skan its first parameter is of type (optionally cv-qualified) X 9209169699Skan and either there are no other parameters or else all other 9210169699Skan parameters have default arguments. 9211169699Skan 9212169699Skan We *don't* complain about member template instantiations that 9213169699Skan have this form, though; they can occur as we try to decide 9214169699Skan what constructor to use during overload resolution. Since 9215169699Skan overload resolution will never prefer such a constructor to 9216169699Skan the non-template copy constructor (which is either explicitly 9217169699Skan or implicitly defined), there's no need to worry about their 9218169699Skan existence. Theoretically, they should never even be 9219169699Skan instantiated, but that's hard to forestall. */ 9220169699Skan error ("invalid constructor; you probably meant %<%T (const %T&)%>", 922151412Sobrien ctype, ctype); 922251412Sobrien return 0; 922318334Speter } 9224169699Skan 922518334Speter return 1; 922618334Speter} 922718334Speter 922890287Sobrien/* An operator with this code is unary, but can also be binary. */ 922951412Sobrien 923018334Speterstatic int 9231132747Skanambi_op_p (enum tree_code code) 923218334Speter{ 923390287Sobrien return (code == INDIRECT_REF 923490287Sobrien || code == ADDR_EXPR 9235169699Skan || code == UNARY_PLUS_EXPR 923690287Sobrien || code == NEGATE_EXPR 923790287Sobrien || code == PREINCREMENT_EXPR 923890287Sobrien || code == PREDECREMENT_EXPR); 923918334Speter} 924018334Speter 924118334Speter/* An operator with this name can only be unary. */ 924251412Sobrien 924318334Speterstatic int 9244132747Skanunary_op_p (enum tree_code code) 924518334Speter{ 924690287Sobrien return (code == TRUTH_NOT_EXPR 924790287Sobrien || code == BIT_NOT_EXPR 924890287Sobrien || code == COMPONENT_REF 924990287Sobrien || code == TYPE_EXPR); 925018334Speter} 925118334Speter 9252169699Skan/* DECL is a declaration for an overloaded operator. If COMPLAIN is true, 9253132747Skan errors are issued for invalid declarations. */ 925451412Sobrien 9255132747Skanbool 9256161660Skangrok_op_properties (tree decl, bool complain) 925718334Speter{ 925818334Speter tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); 925990287Sobrien tree argtype; 926018334Speter int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); 926118334Speter tree name = DECL_NAME (decl); 926290287Sobrien enum tree_code operator_code; 926390287Sobrien int arity; 9264161660Skan bool ellipsis_p; 9265161660Skan tree class_type; 926618334Speter 9267169699Skan /* Count the number of arguments and check for ellipsis. */ 926890287Sobrien for (argtype = argtypes, arity = 0; 926990287Sobrien argtype && argtype != void_list_node; 927090287Sobrien argtype = TREE_CHAIN (argtype)) 927190287Sobrien ++arity; 9272161660Skan ellipsis_p = !argtype; 927390287Sobrien 9274161660Skan class_type = DECL_CONTEXT (decl); 9275161660Skan if (class_type && !CLASS_TYPE_P (class_type)) 9276161660Skan class_type = NULL_TREE; 9277169699Skan 927890287Sobrien if (DECL_CONV_FN_P (decl)) 927990287Sobrien operator_code = TYPE_EXPR; 928090287Sobrien else 928190287Sobrien do 928290287Sobrien { 928390287Sobrien#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ 928490287Sobrien if (ansi_opname (CODE) == name) \ 928590287Sobrien { \ 928690287Sobrien operator_code = (CODE); \ 928790287Sobrien break; \ 928890287Sobrien } \ 928990287Sobrien else if (ansi_assopname (CODE) == name) \ 929090287Sobrien { \ 929190287Sobrien operator_code = (CODE); \ 929290287Sobrien DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \ 929390287Sobrien break; \ 929490287Sobrien } 929590287Sobrien 929690287Sobrien#include "operators.def" 929790287Sobrien#undef DEF_OPERATOR 929890287Sobrien 9299169699Skan gcc_unreachable (); 930090287Sobrien } 930190287Sobrien while (0); 9302169699Skan gcc_assert (operator_code != LAST_CPLUS_TREE_CODE); 930390287Sobrien SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); 930490287Sobrien 9305161660Skan if (class_type) 9306161660Skan switch (operator_code) 9307161660Skan { 9308161660Skan case NEW_EXPR: 9309161660Skan TYPE_HAS_NEW_OPERATOR (class_type) = 1; 9310161660Skan break; 931190287Sobrien 9312161660Skan case DELETE_EXPR: 9313161660Skan TYPE_GETS_DELETE (class_type) |= 1; 9314161660Skan break; 931590287Sobrien 9316161660Skan case VEC_NEW_EXPR: 9317161660Skan TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1; 9318161660Skan break; 931990287Sobrien 9320161660Skan case VEC_DELETE_EXPR: 9321161660Skan TYPE_GETS_DELETE (class_type) |= 2; 9322161660Skan break; 932390287Sobrien 9324161660Skan default: 9325161660Skan break; 9326161660Skan } 932718334Speter 9328169699Skan /* [basic.std.dynamic.allocation]/1: 9329169699Skan 9330169699Skan A program is ill-formed if an allocation function is declared 9331169699Skan in a namespace scope other than global scope or declared static 9332169699Skan in global scope. 9333169699Skan 9334169699Skan The same also holds true for deallocation functions. */ 9335169699Skan if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR 9336169699Skan || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) 9337169699Skan { 9338169699Skan if (DECL_NAMESPACE_SCOPE_P (decl)) 9339169699Skan { 9340169699Skan if (CP_DECL_CONTEXT (decl) != global_namespace) 9341169699Skan { 9342169699Skan error ("%qD may not be declared within a namespace", decl); 9343169699Skan return false; 9344169699Skan } 9345169699Skan else if (!TREE_PUBLIC (decl)) 9346169699Skan { 9347169699Skan error ("%qD may not be declared as static", decl); 9348169699Skan return false; 9349169699Skan } 9350169699Skan } 9351169699Skan } 9352169699Skan 935390287Sobrien if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR) 9354132747Skan TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); 935590287Sobrien else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) 9356132747Skan TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); 935718334Speter else 935818334Speter { 935918334Speter /* An operator function must either be a non-static member function 936018334Speter or have at least one parameter of a class, a reference to a class, 936118334Speter an enumeration, or a reference to an enumeration. 13.4.0.6 */ 936218334Speter if (! methodp || DECL_STATIC_FUNCTION_P (decl)) 936318334Speter { 936490287Sobrien if (operator_code == TYPE_EXPR 936590287Sobrien || operator_code == CALL_EXPR 936690287Sobrien || operator_code == COMPONENT_REF 936790287Sobrien || operator_code == ARRAY_REF 936890287Sobrien || operator_code == NOP_EXPR) 9369169699Skan { 9370169699Skan error ("%qD must be a nonstatic member function", decl); 9371169699Skan return false; 9372169699Skan } 937318334Speter else 937418334Speter { 9375132747Skan tree p; 937618334Speter 937718334Speter if (DECL_STATIC_FUNCTION_P (decl)) 9378169699Skan { 9379169699Skan error ("%qD must be either a non-static member " 9380169699Skan "function or a non-member function", decl); 9381169699Skan return false; 9382169699Skan } 938318334Speter 9384132747Skan for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p)) 9385132747Skan { 9386132747Skan tree arg = non_reference (TREE_VALUE (p)); 9387169699Skan if (arg == error_mark_node) 9388169699Skan return false; 9389169699Skan 9390132747Skan /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used 9391132747Skan because these checks are performed even on 9392132747Skan template functions. */ 9393132747Skan if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE) 9394132747Skan break; 9395132747Skan } 939618334Speter 9397132747Skan if (!p || p == void_list_node) 9398132747Skan { 9399169699Skan if (complain) 9400169699Skan error ("%qD must have an argument of class or " 9401169699Skan "enumerated type", decl); 9402169699Skan return false; 9403132747Skan } 940418334Speter } 940518334Speter } 940690287Sobrien 9407132747Skan /* There are no restrictions on the arguments to an overloaded 9408132747Skan "operator ()". */ 940990287Sobrien if (operator_code == CALL_EXPR) 9410169699Skan return true; 941118334Speter 9412161660Skan /* Warn about conversion operators that will never be used. */ 9413169699Skan if (IDENTIFIER_TYPENAME_P (name) 9414161660Skan && ! DECL_TEMPLATE_INFO (decl) 9415161660Skan && warn_conversion 9416161660Skan /* Warn only declaring the function; there is no need to 9417161660Skan warn again about out-of-class definitions. */ 9418161660Skan && class_type == current_class_type) 941918334Speter { 942018334Speter tree t = TREE_TYPE (name); 9421161660Skan int ref = (TREE_CODE (t) == REFERENCE_TYPE); 9422161660Skan const char *what = 0; 9423169699Skan 9424161660Skan if (ref) 9425161660Skan t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); 9426169699Skan 9427161660Skan if (TREE_CODE (t) == VOID_TYPE) 9428161660Skan what = "void"; 9429161660Skan else if (class_type) 943018334Speter { 9431161660Skan if (t == class_type) 943218334Speter what = "the same type"; 943351412Sobrien /* Don't force t to be complete here. */ 943418334Speter else if (IS_AGGR_TYPE (t) 943590287Sobrien && COMPLETE_TYPE_P (t) 9436161660Skan && DERIVED_FROM_P (t, class_type)) 943718334Speter what = "a base class"; 9438161660Skan } 943918334Speter 9440161660Skan if (what) 9441169699Skan warning (OPT_Wconversion, "conversion to %s%s will never use a type " 9442169699Skan "conversion operator", 9443161660Skan ref ? "a reference to " : "", what); 944418334Speter } 9445161660Skan 944690287Sobrien if (operator_code == COND_EXPR) 944718334Speter { 944890287Sobrien /* 13.4.0.3 */ 944990287Sobrien error ("ISO C++ prohibits overloading operator ?:"); 9450169699Skan return false; 945118334Speter } 9452161660Skan else if (ellipsis_p) 9453169699Skan { 9454169699Skan error ("%qD must not have variable number of arguments", decl); 9455169699Skan return false; 9456169699Skan } 945790287Sobrien else if (ambi_op_p (operator_code)) 945818334Speter { 945990287Sobrien if (arity == 1) 946090287Sobrien /* We pick the one-argument operator codes by default, so 946190287Sobrien we don't have to change anything. */ 946290287Sobrien ; 946390287Sobrien else if (arity == 2) 946418334Speter { 946590287Sobrien /* If we thought this was a unary operator, we now know 946690287Sobrien it to be a binary operator. */ 946790287Sobrien switch (operator_code) 946890287Sobrien { 946990287Sobrien case INDIRECT_REF: 947090287Sobrien operator_code = MULT_EXPR; 947190287Sobrien break; 947290287Sobrien 947390287Sobrien case ADDR_EXPR: 947490287Sobrien operator_code = BIT_AND_EXPR; 947590287Sobrien break; 947690287Sobrien 9477169699Skan case UNARY_PLUS_EXPR: 947890287Sobrien operator_code = PLUS_EXPR; 947990287Sobrien break; 948090287Sobrien 948190287Sobrien case NEGATE_EXPR: 948290287Sobrien operator_code = MINUS_EXPR; 948390287Sobrien break; 948490287Sobrien 948590287Sobrien case PREINCREMENT_EXPR: 948690287Sobrien operator_code = POSTINCREMENT_EXPR; 948790287Sobrien break; 948890287Sobrien 948990287Sobrien case PREDECREMENT_EXPR: 949090287Sobrien operator_code = POSTDECREMENT_EXPR; 949190287Sobrien break; 949290287Sobrien 949390287Sobrien default: 9494169699Skan gcc_unreachable (); 949590287Sobrien } 949690287Sobrien 949790287Sobrien SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); 949890287Sobrien 949990287Sobrien if ((operator_code == POSTINCREMENT_EXPR 950090287Sobrien || operator_code == POSTDECREMENT_EXPR) 950151412Sobrien && ! processing_template_decl 950252290Sobrien && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node)) 950318334Speter { 950418334Speter if (methodp) 9505169699Skan error ("postfix %qD must take %<int%> as its argument", 9506169699Skan decl); 950718334Speter else 9508169699Skan error ("postfix %qD must take %<int%> as its second " 9509169699Skan "argument", decl); 9510169699Skan return false; 951118334Speter } 951218334Speter } 951318334Speter else 951418334Speter { 951518334Speter if (methodp) 9516169699Skan error ("%qD must take either zero or one argument", decl); 951718334Speter else 9518169699Skan error ("%qD must take either one or two arguments", decl); 9519169699Skan return false; 952018334Speter } 952151412Sobrien 952251412Sobrien /* More Effective C++ rule 6. */ 952351412Sobrien if (warn_ecpp 952490287Sobrien && (operator_code == POSTINCREMENT_EXPR 952590287Sobrien || operator_code == POSTDECREMENT_EXPR 952690287Sobrien || operator_code == PREINCREMENT_EXPR 952790287Sobrien || operator_code == PREDECREMENT_EXPR)) 952851412Sobrien { 952951412Sobrien tree arg = TREE_VALUE (argtypes); 953051412Sobrien tree ret = TREE_TYPE (TREE_TYPE (decl)); 953151412Sobrien if (methodp || TREE_CODE (arg) == REFERENCE_TYPE) 953251412Sobrien arg = TREE_TYPE (arg); 953351412Sobrien arg = TYPE_MAIN_VARIANT (arg); 953490287Sobrien if (operator_code == PREINCREMENT_EXPR 953590287Sobrien || operator_code == PREDECREMENT_EXPR) 953651412Sobrien { 953751412Sobrien if (TREE_CODE (ret) != REFERENCE_TYPE 953852290Sobrien || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), 953952290Sobrien arg)) 9540169699Skan warning (OPT_Weffc__, "prefix %qD should return %qT", decl, 9541169699Skan build_reference_type (arg)); 954251412Sobrien } 954351412Sobrien else 954451412Sobrien { 954552290Sobrien if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) 9546169699Skan warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg); 954751412Sobrien } 954851412Sobrien } 954918334Speter } 955090287Sobrien else if (unary_op_p (operator_code)) 955118334Speter { 955290287Sobrien if (arity != 1) 955318334Speter { 955418334Speter if (methodp) 9555169699Skan error ("%qD must take %<void%>", decl); 955618334Speter else 9557169699Skan error ("%qD must take exactly one argument", decl); 9558169699Skan return false; 955918334Speter } 956018334Speter } 956190287Sobrien else /* if (binary_op_p (operator_code)) */ 956218334Speter { 956390287Sobrien if (arity != 2) 956418334Speter { 956518334Speter if (methodp) 9566169699Skan error ("%qD must take exactly one argument", decl); 956718334Speter else 9568169699Skan error ("%qD must take exactly two arguments", decl); 9569169699Skan return false; 957018334Speter } 957151412Sobrien 957251412Sobrien /* More Effective C++ rule 7. */ 957351412Sobrien if (warn_ecpp 957490287Sobrien && (operator_code == TRUTH_ANDIF_EXPR 957590287Sobrien || operator_code == TRUTH_ORIF_EXPR 957690287Sobrien || operator_code == COMPOUND_EXPR)) 9577169699Skan warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments", 9578169699Skan decl); 957918334Speter } 958018334Speter 958151412Sobrien /* Effective C++ rule 23. */ 958251412Sobrien if (warn_ecpp 958390287Sobrien && arity == 2 958496292Sobrien && !DECL_ASSIGNMENT_OPERATOR_P (decl) 958590287Sobrien && (operator_code == PLUS_EXPR 958690287Sobrien || operator_code == MINUS_EXPR 958790287Sobrien || operator_code == TRUNC_DIV_EXPR 958896292Sobrien || operator_code == MULT_EXPR 958996292Sobrien || operator_code == TRUNC_MOD_EXPR) 959051412Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE) 9591169699Skan warning (OPT_Weffc__, "%qD should return by value", decl); 959251412Sobrien 959390287Sobrien /* [over.oper]/8 */ 959490287Sobrien for (; argtypes && argtypes != void_list_node; 9595169699Skan argtypes = TREE_CHAIN (argtypes)) 9596169699Skan if (TREE_PURPOSE (argtypes)) 9597169699Skan { 9598169699Skan TREE_PURPOSE (argtypes) = NULL_TREE; 9599169699Skan if (operator_code == POSTINCREMENT_EXPR 960090287Sobrien || operator_code == POSTDECREMENT_EXPR) 9601169699Skan { 9602169699Skan if (pedantic) 9603169699Skan pedwarn ("%qD cannot have default arguments", decl); 9604169699Skan } 9605169699Skan else 9606169699Skan { 9607169699Skan error ("%qD cannot have default arguments", decl); 9608169699Skan return false; 9609169699Skan } 9610169699Skan } 961118334Speter } 9612169699Skan return true; 961318334Speter} 961418334Speter 9615169699Skan/* Return a string giving the keyword associate with CODE. */ 9616169699Skan 961752290Sobrienstatic const char * 9618132747Skantag_name (enum tag_types code) 961952290Sobrien{ 962052290Sobrien switch (code) 962152290Sobrien { 962252290Sobrien case record_type: 962352290Sobrien return "struct"; 962452290Sobrien case class_type: 962552290Sobrien return "class"; 962652290Sobrien case union_type: 9627161660Skan return "union"; 962852290Sobrien case enum_type: 962952290Sobrien return "enum"; 9630161660Skan case typename_type: 9631161660Skan return "typename"; 963252290Sobrien default: 9633169699Skan gcc_unreachable (); 963452290Sobrien } 963552290Sobrien} 963652290Sobrien 9637117410Skan/* Name lookup in an elaborated-type-specifier (after the keyword 9638132747Skan indicated by TAG_CODE) has found the TYPE_DECL DECL. If the 9639117410Skan elaborated-type-specifier is invalid, issue a diagnostic and return 9640132747Skan error_mark_node; otherwise, return the *_TYPE to which it referred. 9641132747Skan If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */ 9642117410Skan 9643132747Skantree 9644117410Skancheck_elaborated_type_specifier (enum tag_types tag_code, 9645132747Skan tree decl, 9646132747Skan bool allow_template_p) 9647117410Skan{ 9648132747Skan tree type; 9649117410Skan 9650132747Skan /* In the case of: 9651117410Skan 9652132747Skan struct S { struct S *p; }; 9653132747Skan 9654132747Skan name lookup will find the TYPE_DECL for the implicit "S::S" 9655132747Skan typedef. Adjust for that here. */ 9656132747Skan if (DECL_SELF_REFERENCE_P (decl)) 9657132747Skan decl = TYPE_NAME (TREE_TYPE (decl)); 9658132747Skan 9659132747Skan type = TREE_TYPE (decl); 9660132747Skan 9661169699Skan /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P 9662169699Skan is false for this case as well. */ 9663169699Skan if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 9664169699Skan { 9665169699Skan error ("using template type parameter %qT after %qs", 9666169699Skan type, tag_name (tag_code)); 9667169699Skan return error_mark_node; 9668169699Skan } 9669169699Skan /* [dcl.type.elab] 9670132747Skan 9671132747Skan If the identifier resolves to a typedef-name or a template 9672132747Skan type-parameter, the elaborated-type-specifier is ill-formed. 9673132747Skan 9674132747Skan In other words, the only legitimate declaration to use in the 9675132747Skan elaborated type specifier is the implicit typedef created when 9676132747Skan the type is declared. */ 9677169699Skan else if (!DECL_IMPLICIT_TYPEDEF_P (decl) 9678169699Skan && tag_code != typename_type) 9679117410Skan { 9680169699Skan error ("using typedef-name %qD after %qs", decl, tag_name (tag_code)); 9681169699Skan error ("%q+D has a previous declaration here", decl); 9682132747Skan return error_mark_node; 9683117410Skan } 9684132747Skan else if (TREE_CODE (type) != RECORD_TYPE 9685132747Skan && TREE_CODE (type) != UNION_TYPE 9686161660Skan && tag_code != enum_type 9687161660Skan && tag_code != typename_type) 9688132747Skan { 9689169699Skan error ("%qT referred to as %qs", type, tag_name (tag_code)); 9690169699Skan error ("%q+T has a previous declaration here", type); 9691132747Skan return error_mark_node; 9692132747Skan } 9693132747Skan else if (TREE_CODE (type) != ENUMERAL_TYPE 9694132747Skan && tag_code == enum_type) 9695132747Skan { 9696169699Skan error ("%qT referred to as enum", type); 9697169699Skan error ("%q+T has a previous declaration here", type); 9698132747Skan return error_mark_node; 9699132747Skan } 9700132747Skan else if (!allow_template_p 9701132747Skan && TREE_CODE (type) == RECORD_TYPE 9702132747Skan && CLASSTYPE_IS_TEMPLATE (type)) 9703132747Skan { 9704132747Skan /* If a class template appears as elaborated type specifier 9705132747Skan without a template header such as: 9706117410Skan 9707132747Skan template <class T> class C {}; 9708132747Skan void f(class C); // No template header here 9709132747Skan 9710132747Skan then the required template argument is missing. */ 9711169699Skan error ("template argument required for %<%s %T%>", 9712132747Skan tag_name (tag_code), 9713132747Skan DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))); 9714132747Skan return error_mark_node; 9715132747Skan } 9716132747Skan 9717132747Skan return type; 9718117410Skan} 9719117410Skan 9720169699Skan/* Lookup NAME in elaborate type specifier in scope according to 9721169699Skan SCOPE and issue diagnostics if necessary. 9722169699Skan Return *_TYPE node upon success, NULL_TREE when the NAME is not 9723169699Skan found, and ERROR_MARK_NODE for type error. */ 9724169699Skan 9725169699Skanstatic tree 9726169699Skanlookup_and_check_tag (enum tag_types tag_code, tree name, 9727169699Skan tag_scope scope, bool template_header_p) 9728169699Skan{ 9729169699Skan tree t; 9730169699Skan tree decl; 9731169699Skan if (scope == ts_global) 9732169699Skan { 9733169699Skan /* First try ordinary name lookup, ignoring hidden class name 9734169699Skan injected via friend declaration. */ 9735169699Skan decl = lookup_name_prefer_type (name, 2); 9736169699Skan /* If that fails, the name will be placed in the smallest 9737169699Skan non-class, non-function-prototype scope according to 3.3.1/5. 9738169699Skan We may already have a hidden name declared as friend in this 9739169699Skan scope. So lookup again but not ignoring hidden names. 9740169699Skan If we find one, that name will be made visible rather than 9741169699Skan creating a new tag. */ 9742169699Skan if (!decl) 9743169699Skan decl = lookup_type_scope (name, ts_within_enclosing_non_class); 9744169699Skan } 9745169699Skan else 9746169699Skan decl = lookup_type_scope (name, scope); 9747169699Skan 9748169699Skan if (decl && DECL_CLASS_TEMPLATE_P (decl)) 9749169699Skan decl = DECL_TEMPLATE_RESULT (decl); 9750169699Skan 9751169699Skan if (decl && TREE_CODE (decl) == TYPE_DECL) 9752169699Skan { 9753169699Skan /* Look for invalid nested type: 9754169699Skan class C { 9755169699Skan class C {}; 9756169699Skan }; */ 9757169699Skan if (scope == ts_current && DECL_SELF_REFERENCE_P (decl)) 9758169699Skan { 9759169699Skan error ("%qD has the same name as the class in which it is " 9760169699Skan "declared", 9761169699Skan decl); 9762169699Skan return error_mark_node; 9763169699Skan } 9764169699Skan 9765169699Skan /* Two cases we need to consider when deciding if a class 9766169699Skan template is allowed as an elaborated type specifier: 9767169699Skan 1. It is a self reference to its own class. 9768169699Skan 2. It comes with a template header. 9769169699Skan 9770169699Skan For example: 9771169699Skan 9772169699Skan template <class T> class C { 9773169699Skan class C *c1; // DECL_SELF_REFERENCE_P is true 9774169699Skan class D; 9775169699Skan }; 9776169699Skan template <class U> class C; // template_header_p is true 9777169699Skan template <class T> class C<T>::D { 9778169699Skan class C *c2; // DECL_SELF_REFERENCE_P is true 9779169699Skan }; */ 9780169699Skan 9781169699Skan t = check_elaborated_type_specifier (tag_code, 9782169699Skan decl, 9783169699Skan template_header_p 9784169699Skan | DECL_SELF_REFERENCE_P (decl)); 9785169699Skan return t; 9786169699Skan } 9787260933Spfg else if (decl && TREE_CODE (decl) == TREE_LIST) 9788260933Spfg { 9789260933Spfg error ("reference to %qD is ambiguous", name); 9790260933Spfg print_candidates (decl); 9791260933Spfg return error_mark_node; 9792260933Spfg } 9793169699Skan else 9794169699Skan return NULL_TREE; 9795169699Skan} 9796169699Skan 9797132747Skan/* Get the struct, enum or union (TAG_CODE says which) with tag NAME. 979818334Speter Define the tag as a forward-reference if it is not defined. 979918334Speter 9800132747Skan If a declaration is given, process it here, and report an error if 9801132747Skan multiple declarations are not identical. 980218334Speter 9803169699Skan SCOPE is TS_CURRENT when this is also a definition. Only look in 980418334Speter the current frame for the name (since C++ allows new names in any 9805169699Skan scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend 9806169699Skan declaration. Only look beginning from the current scope outward up 9807169699Skan till the nearest non-class scope. Otherwise it is TS_GLOBAL. 980818334Speter 9809132747Skan TEMPLATE_HEADER_P is true when this declaration is preceded by 9810132747Skan a set of template parameters. */ 9811132747Skan 981218334Spetertree 9813132747Skanxref_tag (enum tag_types tag_code, tree name, 9814169699Skan tag_scope scope, bool template_header_p) 981518334Speter{ 981618334Speter enum tree_code code; 9817132747Skan tree t; 981852290Sobrien tree context = NULL_TREE; 981918334Speter 9820117410Skan timevar_push (TV_NAME_LOOKUP); 982151412Sobrien 9822169699Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 9823132747Skan 982418334Speter switch (tag_code) 982518334Speter { 982618334Speter case record_type: 982718334Speter case class_type: 982818334Speter code = RECORD_TYPE; 982918334Speter break; 983018334Speter case union_type: 983118334Speter code = UNION_TYPE; 983218334Speter break; 983318334Speter case enum_type: 983418334Speter code = ENUMERAL_TYPE; 983518334Speter break; 983618334Speter default: 9837169699Skan gcc_unreachable (); 983818334Speter } 983918334Speter 9840169699Skan /* In case of anonymous name, xref_tag is only called to 9841169699Skan make type node and push name. Name lookup is not required. */ 9842169699Skan if (ANON_AGGRNAME_P (name)) 9843169699Skan t = NULL_TREE; 984418334Speter else 9845169699Skan t = lookup_and_check_tag (tag_code, name, 9846169699Skan scope, template_header_p); 984790287Sobrien 9848169699Skan if (t == error_mark_node) 9849169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 9850132747Skan 9851169699Skan if (scope != ts_current && t && current_class_type 9852169699Skan && template_class_depth (current_class_type) 9853169699Skan && template_header_p) 9854169699Skan { 9855169699Skan /* Since SCOPE is not TS_CURRENT, we are not looking at a 9856169699Skan definition of this tag. Since, in addition, we are currently 9857169699Skan processing a (member) template declaration of a template 9858169699Skan class, we must be very careful; consider: 985951412Sobrien 9860169699Skan template <class X> 9861169699Skan struct S1 986251412Sobrien 9863169699Skan template <class U> 9864169699Skan struct S2 9865169699Skan { template <class V> 9866169699Skan friend struct S1; }; 9867132747Skan 9868169699Skan Here, the S2::S1 declaration should not be confused with the 9869169699Skan outer declaration. In particular, the inner version should 9870169699Skan have a template parameter of level 2, not level 1. This 9871169699Skan would be particularly important if the member declaration 9872169699Skan were instead: 987352290Sobrien 9874169699Skan template <class V = U> friend struct S1; 987552290Sobrien 9876169699Skan say, when we should tsubst into `U' when instantiating 9877169699Skan S2. On the other hand, when presented with: 987852290Sobrien 9879169699Skan template <class T> 9880169699Skan struct S1 { 9881169699Skan template <class U> 9882169699Skan struct S2 {}; 9883169699Skan template <class U> 9884169699Skan friend struct S2; 9885169699Skan }; 988652290Sobrien 9887169699Skan we must find the inner binding eventually. We 9888169699Skan accomplish this by making sure that the new type we 9889169699Skan create to represent this declaration has the right 9890169699Skan TYPE_CONTEXT. */ 9891169699Skan context = TYPE_CONTEXT (t); 9892169699Skan t = NULL_TREE; 989318334Speter } 989418334Speter 9895132747Skan if (! t) 989618334Speter { 989718334Speter /* If no such tag is yet defined, create a forward-reference node 989818334Speter and record it as the "definition". 989918334Speter When a real declaration of this type is found, 990018334Speter the forward-reference will be altered into a real type. */ 990118334Speter if (code == ENUMERAL_TYPE) 990218334Speter { 9903169699Skan error ("use of enum %q#D without previous declaration", name); 9904132747Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 990518334Speter } 990618334Speter else 990718334Speter { 9908132747Skan t = make_aggr_type (code); 9909132747Skan TYPE_CONTEXT (t) = context; 9910169699Skan t = pushtag (name, t, scope); 991118334Speter } 991218334Speter } 991318334Speter else 991418334Speter { 9915169699Skan if (template_header_p && IS_AGGR_TYPE (t)) 9916169699Skan { 9917169699Skan if (!redeclare_class_template (t, current_template_parms)) 9918169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 9919169699Skan } 9920169699Skan else if (!processing_template_decl 9921132747Skan && CLASS_TYPE_P (t) 9922132747Skan && CLASSTYPE_IS_TEMPLATE (t)) 9923132747Skan { 9924169699Skan error ("redeclaration of %qT as a non-template", t); 9925169699Skan error ("previous declaration %q+D", t); 9926169699Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 9927132747Skan } 9928169699Skan 9929169699Skan /* Make injected friend class visible. */ 9930169699Skan if (scope != ts_within_enclosing_non_class 9931169699Skan && hidden_name_p (TYPE_NAME (t))) 9932169699Skan { 9933169699Skan DECL_ANTICIPATED (TYPE_NAME (t)) = 0; 9934169699Skan DECL_FRIEND_P (TYPE_NAME (t)) = 0; 9935169699Skan 9936169699Skan if (TYPE_TEMPLATE_INFO (t)) 9937169699Skan { 9938169699Skan DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0; 9939169699Skan DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0; 9940169699Skan } 9941169699Skan } 994218334Speter } 994318334Speter 9944132747Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 994518334Speter} 994618334Speter 994751412Sobrientree 9948169699Skanxref_tag_from_type (tree old, tree id, tag_scope scope) 994951412Sobrien{ 9950117410Skan enum tag_types tag_kind; 995151412Sobrien 995251412Sobrien if (TREE_CODE (old) == RECORD_TYPE) 9953117410Skan tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type); 995451412Sobrien else 9955117410Skan tag_kind = union_type; 995651412Sobrien 995751412Sobrien if (id == NULL_TREE) 995851412Sobrien id = TYPE_IDENTIFIER (old); 995951412Sobrien 9960169699Skan return xref_tag (tag_kind, id, scope, false); 996151412Sobrien} 996251412Sobrien 9963169699Skan/* Create the binfo hierarchy for REF with (possibly NULL) base list 9964169699Skan BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an 9965169699Skan access_* node, and the TREE_VALUE is the type of the base-class. 9966169699Skan Non-NULL TREE_TYPE indicates virtual inheritance. 9967169699Skan 9968169699Skan Returns true if the binfo heirarchy was successfully created, 9969169699Skan false if an error was detected. */ 997052290Sobrien 9971169699Skanbool 9972132747Skanxref_basetypes (tree ref, tree base_list) 997318334Speter{ 9974132747Skan tree *basep; 9975169699Skan tree binfo, base_binfo; 9976169699Skan unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */ 9977169699Skan unsigned max_bases = 0; /* Maximum direct bases. */ 9978132747Skan int i; 9979169699Skan tree default_access; 9980169699Skan tree igo_prev; /* Track Inheritance Graph Order. */ 998118334Speter 9982132747Skan if (ref == error_mark_node) 9983169699Skan return false; 9984132747Skan 9985169699Skan /* The base of a derived class is private by default, all others are 9986169699Skan public. */ 9987169699Skan default_access = (TREE_CODE (ref) == RECORD_TYPE 9988169699Skan && CLASSTYPE_DECLARED_CLASS (ref) 9989169699Skan ? access_private_node : access_public_node); 999018334Speter 999152290Sobrien /* First, make sure that any templates in base-classes are 999252290Sobrien instantiated. This ensures that if we call ourselves recursively 999352290Sobrien we do not get confused about which classes are marked and which 999452290Sobrien are not. */ 9995169699Skan basep = &base_list; 9996169699Skan while (*basep) 9997132747Skan { 9998132747Skan tree basetype = TREE_VALUE (*basep); 9999169699Skan 10000132747Skan if (!(processing_template_decl && uses_template_parms (basetype)) 10001132747Skan && !complete_type_or_else (basetype, NULL)) 10002132747Skan /* An incomplete type. Remove it from the list. */ 10003132747Skan *basep = TREE_CHAIN (*basep); 10004132747Skan else 10005169699Skan { 10006169699Skan max_bases++; 10007169699Skan if (TREE_TYPE (*basep)) 10008169699Skan max_vbases++; 10009169699Skan if (CLASS_TYPE_P (basetype)) 10010169699Skan max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype)); 10011169699Skan basep = &TREE_CHAIN (*basep); 10012169699Skan } 10013132747Skan } 1001452290Sobrien 10015169699Skan TYPE_MARKED_P (ref) = 1; 10016169699Skan 10017169699Skan /* The binfo slot should be empty, unless this is an (ill-formed) 10018169699Skan redefinition. */ 10019169699Skan gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref)); 10020169699Skan gcc_assert (TYPE_MAIN_VARIANT (ref) == ref); 10021169699Skan 10022169699Skan binfo = make_tree_binfo (max_bases); 10023169699Skan 10024169699Skan TYPE_BINFO (ref) = binfo; 10025169699Skan BINFO_OFFSET (binfo) = size_zero_node; 10026169699Skan BINFO_TYPE (binfo) = ref; 10027169699Skan 10028169699Skan if (max_bases) 1002918334Speter { 10030169699Skan BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases); 10031169699Skan /* An aggregate cannot have baseclasses. */ 10032169699Skan CLASSTYPE_NON_AGGREGATE (ref) = 1; 10033169699Skan 10034169699Skan if (TREE_CODE (ref) == UNION_TYPE) 10035169699Skan { 10036169699Skan error ("derived union %qT invalid", ref); 10037169699Skan return false; 10038169699Skan } 10039169699Skan } 10040169699Skan 10041169699Skan if (max_bases > 1) 10042169699Skan { 10043169699Skan if (TYPE_FOR_JAVA (ref)) 10044169699Skan { 10045169699Skan error ("Java class %qT cannot have multiple bases", ref); 10046169699Skan return false; 10047169699Skan } 10048169699Skan } 10049169699Skan 10050169699Skan if (max_vbases) 10051169699Skan { 10052169699Skan CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases); 10053169699Skan 10054169699Skan if (TYPE_FOR_JAVA (ref)) 10055169699Skan { 10056169699Skan error ("Java class %qT cannot have virtual bases", ref); 10057169699Skan return false; 10058169699Skan } 10059169699Skan } 10060169699Skan 10061169699Skan for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list)) 10062169699Skan { 10063169699Skan tree access = TREE_PURPOSE (base_list); 10064169699Skan int via_virtual = TREE_TYPE (base_list) != NULL_TREE; 10065169699Skan tree basetype = TREE_VALUE (base_list); 10066169699Skan 10067169699Skan if (access == access_default_node) 10068169699Skan access = default_access; 10069169699Skan 10070169699Skan if (TREE_CODE (basetype) == TYPE_DECL) 10071169699Skan basetype = TREE_TYPE (basetype); 10072169699Skan if (TREE_CODE (basetype) != RECORD_TYPE 10073169699Skan && TREE_CODE (basetype) != TYPENAME_TYPE 10074169699Skan && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM 10075169699Skan && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM) 1007618334Speter { 10077169699Skan error ("base type %qT fails to be a struct or class type", 10078169699Skan basetype); 10079169699Skan return false; 1008018334Speter } 10081169699Skan 10082169699Skan if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0)) 10083169699Skan TYPE_FOR_JAVA (ref) = 1; 10084169699Skan 10085169699Skan base_binfo = NULL_TREE; 10086169699Skan if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype)) 10087132747Skan { 10088169699Skan base_binfo = TYPE_BINFO (basetype); 10089169699Skan /* The original basetype could have been a typedef'd type. */ 10090169699Skan basetype = BINFO_TYPE (base_binfo); 10091169699Skan 10092169699Skan /* Inherit flags from the base. */ 10093169699Skan TYPE_HAS_NEW_OPERATOR (ref) 10094169699Skan |= TYPE_HAS_NEW_OPERATOR (basetype); 10095169699Skan TYPE_HAS_ARRAY_NEW_OPERATOR (ref) 10096169699Skan |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); 10097169699Skan TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); 10098169699Skan TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype); 10099169699Skan CLASSTYPE_DIAMOND_SHAPED_P (ref) 10100169699Skan |= CLASSTYPE_DIAMOND_SHAPED_P (basetype); 10101169699Skan CLASSTYPE_REPEATED_BASE_P (ref) 10102169699Skan |= CLASSTYPE_REPEATED_BASE_P (basetype); 10103132747Skan } 1010418334Speter 10105169699Skan /* We must do this test after we've seen through a typedef 10106169699Skan type. */ 10107169699Skan if (TYPE_MARKED_P (basetype)) 10108169699Skan { 10109169699Skan if (basetype == ref) 10110169699Skan error ("recursive type %qT undefined", basetype); 10111169699Skan else 10112169699Skan error ("duplicate base type %qT invalid", basetype); 10113169699Skan return false; 10114169699Skan } 10115169699Skan TYPE_MARKED_P (basetype) = 1; 10116169699Skan 10117169699Skan base_binfo = copy_binfo (base_binfo, basetype, ref, 10118169699Skan &igo_prev, via_virtual); 10119169699Skan if (!BINFO_INHERITANCE_CHAIN (base_binfo)) 10120169699Skan BINFO_INHERITANCE_CHAIN (base_binfo) = binfo; 10121169699Skan 10122169699Skan BINFO_BASE_APPEND (binfo, base_binfo); 10123169699Skan BINFO_BASE_ACCESS_APPEND (binfo, access); 1012452290Sobrien } 1012552290Sobrien 10126169699Skan if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1)) 10127169699Skan /* If we have space in the vbase vector, we must have shared at 10128169699Skan least one of them, and are therefore diamond shaped. */ 10129169699Skan CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1; 10130169699Skan 1013118334Speter /* Unmark all the types. */ 10132169699Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 10133169699Skan TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; 10134169699Skan TYPE_MARKED_P (ref) = 0; 10135169699Skan 10136169699Skan /* Now see if we have a repeated base type. */ 10137169699Skan if (!CLASSTYPE_REPEATED_BASE_P (ref)) 10138132747Skan { 10139169699Skan for (base_binfo = binfo; base_binfo; 10140169699Skan base_binfo = TREE_CHAIN (base_binfo)) 10141132747Skan { 10142169699Skan if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) 10143169699Skan { 10144169699Skan CLASSTYPE_REPEATED_BASE_P (ref) = 1; 10145169699Skan break; 10146169699Skan } 10147169699Skan TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1; 10148132747Skan } 10149169699Skan for (base_binfo = binfo; base_binfo; 10150169699Skan base_binfo = TREE_CHAIN (base_binfo)) 10151169699Skan if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) 10152169699Skan TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; 10153169699Skan else 10154169699Skan break; 10155132747Skan } 10156169699Skan 10157169699Skan return true; 1015890287Sobrien} 1015952290Sobrien 1016018334Speter 1016118334Speter/* Begin compiling the definition of an enumeration type. 10162169699Skan NAME is its name. 1016318334Speter Returns the type object, as yet incomplete. 1016418334Speter Also records info about it so that build_enumerator 1016518334Speter may be used to declare the individual values as they are read. */ 1016618334Speter 1016718334Spetertree 10168132747Skanstart_enum (tree name) 1016918334Speter{ 10170169699Skan tree enumtype; 1017118334Speter 10172169699Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 10173169699Skan 1017418334Speter /* If this is the real definition for a previous forward reference, 1017518334Speter fill in the contents in the same object that used to be the 1017618334Speter forward reference. */ 1017718334Speter 10178169699Skan enumtype = lookup_and_check_tag (enum_type, name, 10179169699Skan /*tag_scope=*/ts_current, 10180169699Skan /*template_header_p=*/false); 1018118334Speter 1018218334Speter if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE) 1018390287Sobrien { 10184169699Skan error ("multiple definition of %q#T", enumtype); 10185132747Skan error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype)); 1018690287Sobrien /* Clear out TYPE_VALUES, and start again. */ 1018790287Sobrien TYPE_VALUES (enumtype) = NULL_TREE; 1018890287Sobrien } 1018918334Speter else 1019018334Speter { 10191169699Skan /* In case of error, make a dummy enum to allow parsing to 10192169699Skan continue. */ 10193169699Skan if (enumtype == error_mark_node) 10194169699Skan name = make_anon_name (); 10195169699Skan 1019618334Speter enumtype = make_node (ENUMERAL_TYPE); 10197169699Skan enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current); 1019818334Speter } 1019918334Speter 1020018334Speter return enumtype; 1020118334Speter} 1020218334Speter 1020318334Speter/* After processing and defining all the values of an enumeration type, 1020418334Speter install their decls in the enumeration type and finish it off. 1020590287Sobrien ENUMTYPE is the type object and VALUES a list of name-value pairs. */ 1020618334Speter 1020790287Sobrienvoid 10208132747Skanfinish_enum (tree enumtype) 1020918334Speter{ 10210132747Skan tree values; 10211132747Skan tree decl; 10212132747Skan tree value; 1021390287Sobrien tree minnode; 1021490287Sobrien tree maxnode; 1021590287Sobrien tree t; 1021690287Sobrien bool unsignedp; 10217169699Skan bool use_short_enum; 1021890287Sobrien int lowprec; 10219169699Skan int highprec; 1022090287Sobrien int precision; 10221132747Skan integer_type_kind itk; 10222132747Skan tree underlying_type = NULL_TREE; 1022318334Speter 1022490287Sobrien /* We built up the VALUES in reverse order. */ 1022590287Sobrien TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); 1022690287Sobrien 10227117410Skan /* For an enum defined in a template, just set the type of the values; 10228117410Skan all further processing is postponed until the template is 10229117410Skan instantiated. We need to set the type so that tsubst of a CONST_DECL 10230117410Skan works. */ 1023190287Sobrien if (processing_template_decl) 1023218334Speter { 10233169699Skan for (values = TYPE_VALUES (enumtype); 10234169699Skan values; 10235132747Skan values = TREE_CHAIN (values)) 10236132747Skan TREE_TYPE (TREE_VALUE (values)) = enumtype; 10237117410Skan if (at_function_scope_p ()) 1023890287Sobrien add_stmt (build_min (TAG_DEFN, enumtype)); 1023990287Sobrien return; 1024090287Sobrien } 1024152290Sobrien 10242132747Skan /* Determine the minimum and maximum values of the enumerators. */ 1024390287Sobrien if (TYPE_VALUES (enumtype)) 1024490287Sobrien { 1024590287Sobrien minnode = maxnode = NULL_TREE; 1024652290Sobrien 10247169699Skan for (values = TYPE_VALUES (enumtype); 10248169699Skan values; 10249132747Skan values = TREE_CHAIN (values)) 1025090287Sobrien { 10251132747Skan decl = TREE_VALUE (values); 1025252290Sobrien 10253117410Skan /* [dcl.enum]: Following the closing brace of an enum-specifier, 10254117410Skan each enumerator has the type of its enumeration. Prior to the 10255117410Skan closing brace, the type of each enumerator is the type of its 10256117410Skan initializing value. */ 10257117410Skan TREE_TYPE (decl) = enumtype; 1025852290Sobrien 10259132747Skan /* Update the minimum and maximum values, if appropriate. */ 10260132747Skan value = DECL_INITIAL (decl); 10261169699Skan if (value == error_mark_node) 10262169699Skan value = integer_zero_node; 10263117410Skan /* Figure out what the minimum and maximum values of the 10264117410Skan enumerators are. */ 1026590287Sobrien if (!minnode) 1026690287Sobrien minnode = maxnode = value; 1026790287Sobrien else if (tree_int_cst_lt (maxnode, value)) 1026890287Sobrien maxnode = value; 1026990287Sobrien else if (tree_int_cst_lt (value, minnode)) 1027090287Sobrien minnode = value; 1027118334Speter } 1027218334Speter } 1027318334Speter else 10274132747Skan /* [dcl.enum] 10275132747Skan 10276132747Skan If the enumerator-list is empty, the underlying type is as if 10277132747Skan the enumeration had a single enumerator with value 0. */ 1027890287Sobrien minnode = maxnode = integer_zero_node; 1027918334Speter 1028090287Sobrien /* Compute the number of bits require to represent all values of the 1028190287Sobrien enumeration. We must do this before the type of MINNODE and 1028290287Sobrien MAXNODE are transformed, since min_precision relies on the 1028390287Sobrien TREE_TYPE of the value it is passed. */ 1028490287Sobrien unsignedp = tree_int_cst_sgn (minnode) >= 0; 1028590287Sobrien lowprec = min_precision (minnode, unsignedp); 1028690287Sobrien highprec = min_precision (maxnode, unsignedp); 1028790287Sobrien precision = MAX (lowprec, highprec); 1028818334Speter 10289132747Skan /* Determine the underlying type of the enumeration. 10290132747Skan 10291132747Skan [dcl.enum] 10292132747Skan 10293132747Skan The underlying type of an enumeration is an integral type that 10294132747Skan can represent all the enumerator values defined in the 10295132747Skan enumeration. It is implementation-defined which integral type is 10296132747Skan used as the underlying type for an enumeration except that the 10297132747Skan underlying type shall not be larger than int unless the value of 10298169699Skan an enumerator cannot fit in an int or unsigned int. 10299132747Skan 10300132747Skan We use "int" or an "unsigned int" as the underlying type, even if 10301132747Skan a smaller integral type would work, unless the user has 10302169699Skan explicitly requested that we use the smallest possible type. The 10303169699Skan user can request that for all enumerations with a command line 10304169699Skan flag, or for just one enumeration with an attribute. */ 10305169699Skan 10306169699Skan use_short_enum = flag_short_enums 10307169699Skan || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype)); 10308169699Skan 10309169699Skan for (itk = (use_short_enum ? itk_char : itk_int); 10310169699Skan itk != itk_none; 10311132747Skan itk++) 1031251412Sobrien { 10313132747Skan underlying_type = integer_types[itk]; 10314132747Skan if (TYPE_PRECISION (underlying_type) >= precision 10315169699Skan && TYPE_UNSIGNED (underlying_type) == unsignedp) 10316132747Skan break; 10317132747Skan } 10318132747Skan if (itk == itk_none) 10319132747Skan { 10320132747Skan /* DR 377 10321132747Skan 10322132747Skan IF no integral type can represent all the enumerator values, the 10323132747Skan enumeration is ill-formed. */ 10324117410Skan error ("no integral type can represent all of the enumerator values " 10325169699Skan "for %qT", enumtype); 10326117410Skan precision = TYPE_PRECISION (long_long_integer_type_node); 10327132747Skan underlying_type = integer_types[itk_unsigned_long_long]; 1032851412Sobrien } 1032990287Sobrien 10330169699Skan /* Compute the minium and maximum values for the type. 10331132747Skan 10332132747Skan [dcl.enum] 10333132747Skan 10334132747Skan For an enumeration where emin is the smallest enumerator and emax 10335132747Skan is the largest, the values of the enumeration are the values of the 10336132747Skan underlying type in the range bmin to bmax, where bmin and bmax are, 10337132747Skan respectively, the smallest and largest values of the smallest bit- 10338132747Skan field that can store emin and emax. */ 10339146906Skan 10340146906Skan /* The middle-end currently assumes that types with TYPE_PRECISION 10341146906Skan narrower than their underlying type are suitably zero or sign 10342146906Skan extended to fill their mode. g++ doesn't make these guarantees. 10343146906Skan Until the middle-end can represent such paradoxical types, we 10344169699Skan set the TYPE_PRECISION to the width of the underlying type. */ 10345146906Skan TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type); 10346146906Skan 10347132747Skan set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp); 1034851412Sobrien 10349132747Skan /* [dcl.enum] 10350169699Skan 10351132747Skan The value of sizeof() applied to an enumeration type, an object 10352132747Skan of an enumeration type, or an enumerator, is the value of sizeof() 10353132747Skan applied to the underlying type. */ 10354132747Skan TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type); 10355132747Skan TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type); 10356132747Skan TYPE_MODE (enumtype) = TYPE_MODE (underlying_type); 10357132747Skan TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type); 10358132747Skan TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type); 10359169699Skan TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type); 1036018334Speter 10361132747Skan /* Convert each of the enumerators to the type of the underlying 10362132747Skan type of the enumeration. */ 10363132747Skan for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) 10364132747Skan { 10365169699Skan location_t saved_location; 10366169699Skan 10367132747Skan decl = TREE_VALUE (values); 10368169699Skan saved_location = input_location; 10369169699Skan input_location = DECL_SOURCE_LOCATION (decl); 10370132747Skan value = perform_implicit_conversion (underlying_type, 10371132747Skan DECL_INITIAL (decl)); 10372169699Skan input_location = saved_location; 10373169699Skan 10374169699Skan /* Do not clobber shared ints. */ 10375169699Skan value = copy_node (value); 10376169699Skan 10377132747Skan TREE_TYPE (value) = enumtype; 10378132747Skan DECL_INITIAL (decl) = value; 10379132747Skan TREE_VALUE (values) = value; 10380132747Skan } 10381119270Skan 1038290287Sobrien /* Fix up all variant types of this enum type. */ 1038390287Sobrien for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) 1038490287Sobrien { 1038590287Sobrien TYPE_VALUES (t) = TYPE_VALUES (enumtype); 1038690287Sobrien TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype); 1038790287Sobrien TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype); 1038890287Sobrien TYPE_SIZE (t) = TYPE_SIZE (enumtype); 1038990287Sobrien TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype); 1039090287Sobrien TYPE_MODE (t) = TYPE_MODE (enumtype); 1039190287Sobrien TYPE_PRECISION (t) = TYPE_PRECISION (enumtype); 1039290287Sobrien TYPE_ALIGN (t) = TYPE_ALIGN (enumtype); 1039390287Sobrien TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype); 10394169699Skan TYPE_UNSIGNED (t) = TYPE_UNSIGNED (enumtype); 1039552290Sobrien } 1039618334Speter 1039790287Sobrien /* Finish debugging output for this type. */ 1039890287Sobrien rest_of_type_compilation (enumtype, namespace_bindings_p ()); 1039918334Speter} 1040018334Speter 1040152290Sobrien/* Build and install a CONST_DECL for an enumeration constant of the 1040290287Sobrien enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. 1040318334Speter Assignment of sequential values by default is handled here. */ 1040418334Speter 1040590287Sobrienvoid 10406132747Skanbuild_enumerator (tree name, tree value, tree enumtype) 1040718334Speter{ 1040890287Sobrien tree decl; 1040952290Sobrien tree context; 1041090287Sobrien tree type; 1041118334Speter 10412169699Skan /* If the VALUE was erroneous, pretend it wasn't there; that will 10413169699Skan result in the enum being assigned the next value in sequence. */ 10414169699Skan if (value == error_mark_node) 10415169699Skan value = NULL_TREE; 10416169699Skan 1041718334Speter /* Remove no-op casts from the value. */ 1041818334Speter if (value) 1041918334Speter STRIP_TYPE_NOPS (value); 1042018334Speter 1042190287Sobrien if (! processing_template_decl) 1042290287Sobrien { 1042390287Sobrien /* Validate and default VALUE. */ 1042490287Sobrien if (value != NULL_TREE) 1042590287Sobrien { 10426169699Skan value = integral_constant_value (value); 1042718334Speter 1042890287Sobrien if (TREE_CODE (value) == INTEGER_CST) 1042990287Sobrien { 10430132747Skan value = perform_integral_promotions (value); 1043190287Sobrien constant_expression_warning (value); 1043290287Sobrien } 1043390287Sobrien else 1043490287Sobrien { 10435169699Skan error ("enumerator value for %qD not integer constant", name); 1043690287Sobrien value = NULL_TREE; 1043790287Sobrien } 1043890287Sobrien } 1043918334Speter 1044090287Sobrien /* Default based on previous value. */ 10441132747Skan if (value == NULL_TREE) 1044290287Sobrien { 1044390287Sobrien if (TYPE_VALUES (enumtype)) 1044490287Sobrien { 10445169699Skan HOST_WIDE_INT hi; 10446169699Skan unsigned HOST_WIDE_INT lo; 10447169699Skan tree prev_value; 10448169699Skan bool overflowed; 10449169699Skan 10450169699Skan /* The next value is the previous value plus one. We can 10451169699Skan safely assume that the previous value is an INTEGER_CST. 10452169699Skan add_double doesn't know the type of the target expression, 10453169699Skan so we must check with int_fits_type_p as well. */ 1045490287Sobrien prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); 10455169699Skan overflowed = add_double (TREE_INT_CST_LOW (prev_value), 10456169699Skan TREE_INT_CST_HIGH (prev_value), 10457169699Skan 1, 0, &lo, &hi); 10458169699Skan value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi); 10459169699Skan overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value)); 1046090287Sobrien 10461169699Skan if (overflowed) 10462169699Skan { 10463169699Skan error ("overflow in enumeration values at %qD", name); 10464169699Skan value = error_mark_node; 10465169699Skan } 1046690287Sobrien } 1046790287Sobrien else 1046890287Sobrien value = integer_zero_node; 1046990287Sobrien } 1047090287Sobrien 1047190287Sobrien /* Remove no-op casts from the value. */ 10472117410Skan STRIP_TYPE_NOPS (value); 1047390287Sobrien } 1047418334Speter 1047518334Speter /* C++ associates enums with global, function, or class declarations. */ 1047690287Sobrien context = current_scope (); 1047790287Sobrien 1047890287Sobrien /* Build the actual enumeration constant. Note that the enumeration 1047990287Sobrien constants have the type of their initializers until the 1048090287Sobrien enumeration is complete: 1048190287Sobrien 1048290287Sobrien [ dcl.enum ] 1048390287Sobrien 1048490287Sobrien Following the closing brace of an enum-specifier, each enumer- 1048590287Sobrien ator has the type of its enumeration. Prior to the closing 1048690287Sobrien brace, the type of each enumerator is the type of its 1048790287Sobrien initializing value. 1048890287Sobrien 1048990287Sobrien In finish_enum we will reset the type. Of course, if we're 10490117410Skan processing a template, there may be no value. */ 1049190287Sobrien type = value ? TREE_TYPE (value) : NULL_TREE; 1049290287Sobrien 1049390287Sobrien if (context && context == current_class_type) 1049490287Sobrien /* This enum declaration is local to the class. We need the full 1049590287Sobrien lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */ 1049690287Sobrien decl = build_lang_decl (CONST_DECL, name, type); 1049790287Sobrien else 1049890287Sobrien /* It's a global enum, or it's local to a function. (Note local to 1049952290Sobrien a function could mean local to a class method. */ 1050090287Sobrien decl = build_decl (CONST_DECL, name, type); 1050118334Speter 1050290287Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (context); 10503169699Skan TREE_CONSTANT (decl) = 1; 10504169699Skan TREE_INVARIANT (decl) = 1; 10505169699Skan TREE_READONLY (decl) = 1; 1050690287Sobrien DECL_INITIAL (decl) = value; 1050718334Speter 1050890287Sobrien if (context && context == current_class_type) 1050990287Sobrien /* In something like `struct S { enum E { i = 7 }; };' we put `i' 10510132747Skan on the TYPE_FIELDS list for `S'. (That's so that you can say 10511132747Skan things like `S::i' later.) */ 1051290287Sobrien finish_member_declaration (decl); 1051390287Sobrien else 1051496292Sobrien pushdecl (decl); 1051518334Speter 1051690287Sobrien /* Add this enumeration constant to the list for this type. */ 1051790287Sobrien TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); 1051818334Speter} 1051918334Speter 1052018334Speter 1052190287Sobrien/* We're defining DECL. Make sure that it's type is OK. */ 1052251412Sobrien 1052390287Sobrienstatic void 10524132747Skancheck_function_type (tree decl, tree current_function_parms) 1052590287Sobrien{ 1052690287Sobrien tree fntype = TREE_TYPE (decl); 1052790287Sobrien tree return_type = complete_type (TREE_TYPE (fntype)); 1052890287Sobrien 1052990287Sobrien /* In a function definition, arg types must be complete. */ 1053090287Sobrien require_complete_types_for_parms (current_function_parms); 1053190287Sobrien 10532169699Skan if (dependent_type_p (return_type)) 10533169699Skan return; 1053490287Sobrien if (!COMPLETE_OR_VOID_TYPE_P (return_type)) 1053590287Sobrien { 10536169699Skan tree args = TYPE_ARG_TYPES (fntype); 1053790287Sobrien 10538169699Skan error ("return type %q#T is incomplete", return_type); 10539169699Skan 10540169699Skan /* Make it return void instead. */ 1054190287Sobrien if (TREE_CODE (fntype) == METHOD_TYPE) 10542169699Skan fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)), 10543169699Skan void_type_node, 10544169699Skan TREE_CHAIN (args)); 1054590287Sobrien else 10546169699Skan fntype = build_function_type (void_type_node, args); 1054790287Sobrien TREE_TYPE (decl) 1054890287Sobrien = build_exception_variant (fntype, 10549169699Skan TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl))); 1055090287Sobrien } 1055190287Sobrien else 1055290287Sobrien abstract_virtuals_error (decl, TREE_TYPE (fntype)); 1055390287Sobrien} 1055490287Sobrien 1055518334Speter/* Create the FUNCTION_DECL for a function definition. 1055618334Speter DECLSPECS and DECLARATOR are the parts of the declaration; 1055718334Speter they describe the function's name and the type it returns, 1055818334Speter but twisted together in a fashion that parallels the syntax of C. 1055918334Speter 1056090287Sobrien FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the 1056190287Sobrien DECLARATOR is really the DECL for the function we are about to 1056290287Sobrien process and that DECLSPECS should be ignored), SF_INCLASS_INLINE 1056390287Sobrien indicating that the function is an inline defined in-class. 1056490287Sobrien 1056518334Speter This function creates a binding context for the function body 1056618334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 1056718334Speter 1056818334Speter For C++, we must first check whether that datum makes any sense. 1056918334Speter For example, "class A local_a(1,2);" means that variable local_a 1057018334Speter is an aggregate of type A, which should have a constructor 10571260140Spfg applied to it with the argument list [1, 2]. 1057218334Speter 10573260140Spfg On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node, 10574260140Spfg or may be a BLOCK if the function has been defined previously 10575260140Spfg in this translation unit. On exit, DECL_INITIAL (decl1) will be 10576260140Spfg error_mark_node if the function has never been defined, or 10577260140Spfg a BLOCK if the function has been defined somewhere. */ 10578260140Spfg 10579169699Skanvoid 10580169699Skanstart_preparsed_function (tree decl1, tree attrs, int flags) 1058118334Speter{ 1058218334Speter tree ctype = NULL_TREE; 1058318334Speter tree fntype; 1058418334Speter tree restype; 1058518334Speter int doing_friend = 0; 10586117410Skan struct cp_binding_level *bl; 1058790287Sobrien tree current_function_parms; 10588169699Skan struct c_fileinfo *finfo 10589169699Skan = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1))); 10590169699Skan bool honor_interface; 1059118334Speter 1059218334Speter /* Sanity check. */ 10593169699Skan gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE); 10594169699Skan gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE); 1059518334Speter 10596169699Skan fntype = TREE_TYPE (decl1); 10597169699Skan if (TREE_CODE (fntype) == METHOD_TYPE) 10598169699Skan ctype = TYPE_METHOD_BASETYPE (fntype); 1059918334Speter 10600169699Skan /* ISO C++ 11.4/5. A friend function defined in a class is in 10601169699Skan the (lexical) scope of the class in which it is defined. */ 10602169699Skan if (!ctype && DECL_FRIEND_P (decl1)) 1060318334Speter { 10604169699Skan ctype = DECL_FRIEND_CONTEXT (decl1); 1060518334Speter 10606169699Skan /* CTYPE could be null here if we're dealing with a template; 10607169699Skan for example, `inline friend float foo()' inside a template 10608169699Skan will have no CTYPE set. */ 10609169699Skan if (ctype && TREE_CODE (ctype) != RECORD_TYPE) 10610169699Skan ctype = NULL_TREE; 10611169699Skan else 10612169699Skan doing_friend = 1; 1061318334Speter } 1061418334Speter 1061590287Sobrien if (DECL_DECLARED_INLINE_P (decl1) 1061690287Sobrien && lookup_attribute ("noinline", attrs)) 10617169699Skan warning (0, "inline function %q+D given attribute noinline", decl1); 1061890287Sobrien 1061990287Sobrien if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) 1062090287Sobrien /* This is a constructor, we must ensure that any default args 1062190287Sobrien introduced by this definition are propagated to the clones 1062290287Sobrien now. The clones are used directly in overload resolution. */ 1062390287Sobrien adjust_clone_args (decl1); 1062490287Sobrien 1062590287Sobrien /* Sometimes we don't notice that a function is a static member, and 1062690287Sobrien build a METHOD_TYPE for it. Fix that up now. */ 1062790287Sobrien if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) 1062890287Sobrien && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE) 1062990287Sobrien { 1063090287Sobrien revert_static_member_fn (decl1); 1063190287Sobrien ctype = NULL_TREE; 1063290287Sobrien } 1063390287Sobrien 1063452290Sobrien /* Set up current_class_type, and enter the scope of the class, if 1063552290Sobrien appropriate. */ 1063652290Sobrien if (ctype) 10637132747Skan push_nested_class (ctype); 1063852290Sobrien else if (DECL_STATIC_FUNCTION_P (decl1)) 10639132747Skan push_nested_class (DECL_CONTEXT (decl1)); 1064052290Sobrien 1064152290Sobrien /* Now that we have entered the scope of the class, we must restore 1064252290Sobrien the bindings for any template parameters surrounding DECL1, if it 1064352290Sobrien is an inline member template. (Order is important; consider the 1064452290Sobrien case where a template parameter has the same name as a field of 1064552290Sobrien the class.) It is not until after this point that 1064652290Sobrien PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ 1064790287Sobrien if (flags & SF_INCLASS_INLINE) 1064852290Sobrien maybe_begin_member_template_processing (decl1); 1064952290Sobrien 1065090287Sobrien /* Effective C++ rule 15. */ 1065190287Sobrien if (warn_ecpp 1065290287Sobrien && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR 1065390287Sobrien && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE) 10654169699Skan warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>"); 1065590287Sobrien 1065690287Sobrien /* Make the init_value nonzero so pushdecl knows this is not tentative. 1065790287Sobrien error_mark_node is replaced below (in poplevel) with the BLOCK. */ 1065890287Sobrien if (!DECL_INITIAL (decl1)) 1065990287Sobrien DECL_INITIAL (decl1) = error_mark_node; 1066090287Sobrien 1066190287Sobrien /* This function exists in static storage. 1066290287Sobrien (This does not mean `static' in the C sense!) */ 1066390287Sobrien TREE_STATIC (decl1) = 1; 1066490287Sobrien 1066590287Sobrien /* We must call push_template_decl after current_class_type is set 1066690287Sobrien up. (If we are processing inline definitions after exiting a 1066790287Sobrien class scope, current_class_type will be NULL_TREE until set above 1066890287Sobrien by push_nested_class.) */ 1066990287Sobrien if (processing_template_decl) 10670161660Skan { 10671169699Skan /* FIXME: Handle error_mark_node more gracefully. */ 10672161660Skan tree newdecl1 = push_template_decl (decl1); 10673161660Skan if (newdecl1 != error_mark_node) 10674161660Skan decl1 = newdecl1; 10675161660Skan } 1067690287Sobrien 1067752290Sobrien /* We are now in the scope of the function being defined. */ 1067818334Speter current_function_decl = decl1; 1067952290Sobrien 1068051412Sobrien /* Save the parm names or decls from this function's declarator 1068151412Sobrien where store_parm_decls will find them. */ 10682132747Skan current_function_parms = DECL_ARGUMENTS (decl1); 1068318334Speter 1068490287Sobrien /* Make sure the parameter and return types are reasonable. When 1068590287Sobrien you declare a function, these types can be incomplete, but they 1068690287Sobrien must be complete when you define the function. */ 10687169699Skan check_function_type (decl1, current_function_parms); 1068890287Sobrien 1068990287Sobrien /* Build the return declaration for the function. */ 1069090287Sobrien restype = TREE_TYPE (fntype); 1069196292Sobrien /* Promote the value to int before returning it. */ 1069296292Sobrien if (c_promoting_integer_type_p (restype)) 1069396292Sobrien restype = type_promotes_to (restype); 1069496292Sobrien if (DECL_RESULT (decl1) == NULL_TREE) 1069518334Speter { 10696169699Skan tree resdecl; 10697169699Skan 10698169699Skan resdecl = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); 10699169699Skan DECL_ARTIFICIAL (resdecl) = 1; 10700169699Skan DECL_IGNORED_P (resdecl) = 1; 10701169699Skan DECL_RESULT (decl1) = resdecl; 10702169699Skan 10703169699Skan cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl); 1070418334Speter } 1070518334Speter 1070690287Sobrien /* Let the user know we're compiling this function. */ 1070790287Sobrien announce_function (decl1); 1070852290Sobrien 1070918334Speter /* Record the decl so that the function name is defined. 1071018334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 1071118334Speter use the old decl. */ 1071290287Sobrien if (!processing_template_decl && !(flags & SF_PRE_PARSED)) 1071318334Speter { 1071451412Sobrien /* A specialization is not used to guide overload resolution. */ 10715132747Skan if (!DECL_FUNCTION_MEMBER_P (decl1) 10716169699Skan && !(DECL_USE_TEMPLATE (decl1) && 10717132747Skan PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1)))) 10718132747Skan { 10719132747Skan tree olddecl = pushdecl (decl1); 10720132747Skan 10721132747Skan if (olddecl == error_mark_node) 10722132747Skan /* If something went wrong when registering the declaration, 10723132747Skan use DECL1; we have to have a FUNCTION_DECL to use when 10724132747Skan parsing the body of the function. */ 10725132747Skan ; 10726132747Skan else 10727132747Skan /* Otherwise, OLDDECL is either a previous declaration of 10728132747Skan the same function or DECL1 itself. */ 10729132747Skan decl1 = olddecl; 10730132747Skan } 1073151412Sobrien else 1073251412Sobrien { 10733117410Skan /* We need to set the DECL_CONTEXT. */ 1073451412Sobrien if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) 1073551412Sobrien DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); 1073651412Sobrien } 1073718334Speter fntype = TREE_TYPE (decl1); 10738169699Skan 10739169699Skan /* If #pragma weak applies, mark the decl appropriately now. 10740169699Skan The pragma only applies to global functions. Because 10741169699Skan determining whether or not the #pragma applies involves 10742169699Skan computing the mangled name for the declaration, we cannot 10743169699Skan apply the pragma until after we have merged this declaration 10744169699Skan with any previous declarations; if the original declaration 10745169699Skan has a linkage specification, that specification applies to 10746169699Skan the definition as well, and may affect the mangled name. */ 10747169699Skan if (!DECL_CONTEXT (decl1)) 10748169699Skan maybe_apply_pragma_weak (decl1); 1074918334Speter } 1075018334Speter 10751260140Spfg /* Reset this in case the call to pushdecl changed it. */ 1075251412Sobrien current_function_decl = decl1; 1075351412Sobrien 10754260140Spfg gcc_assert (DECL_INITIAL (decl1)); 10755260140Spfg 10756260140Spfg /* This function may already have been parsed, in which case just 10757260140Spfg return; our caller will skip over the body without parsing. */ 10758260140Spfg if (DECL_INITIAL (decl1) != error_mark_node) 10759260140Spfg return; 10760260140Spfg 10761260140Spfg /* Initialize RTL machinery. We cannot do this until 10762260140Spfg CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this 10763260140Spfg even when processing a template; this is how we get 10764260140Spfg CFUN set up, and our per-function variables initialized. 10765260140Spfg FIXME factor out the non-RTL stuff. */ 10766260140Spfg bl = current_binding_level; 10767260140Spfg allocate_struct_function (decl1); 10768260140Spfg current_binding_level = bl; 10769260140Spfg 10770260140Spfg /* Even though we're inside a function body, we still don't want to 10771260140Spfg call expand_expr to calculate the size of a variable-sized array. 10772260140Spfg We haven't necessarily assigned RTL to all variables yet, so it's 10773260140Spfg not safe to try to expand expressions involving them. */ 10774260140Spfg cfun->x_dont_save_pending_sizes_p = 1; 10775260140Spfg 10776260140Spfg /* Start the statement-tree, start the tree now. */ 10777260140Spfg DECL_SAVED_TREE (decl1) = push_stmt_list (); 10778260140Spfg 1077990287Sobrien /* If we are (erroneously) defining a function that we have already 1078090287Sobrien defined before, wipe out what we knew before. */ 10781117410Skan if (!DECL_PENDING_INLINE_P (decl1)) 10782117410Skan DECL_SAVED_FUNCTION_DATA (decl1) = NULL; 1078390287Sobrien 1078490287Sobrien if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) 1078590287Sobrien { 1078690287Sobrien /* We know that this was set up by `grokclassfn'. We do not 1078790287Sobrien wait until `store_parm_decls', since evil parse errors may 1078890287Sobrien never get us to that point. Here we keep the consistency 1078990287Sobrien between `current_class_type' and `current_class_ptr'. */ 1079090287Sobrien tree t = DECL_ARGUMENTS (decl1); 1079190287Sobrien 10792169699Skan gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL); 10793169699Skan gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE); 1079490287Sobrien 1079590287Sobrien cp_function_chain->x_current_class_ref 1079690287Sobrien = build_indirect_ref (t, NULL); 1079790287Sobrien cp_function_chain->x_current_class_ptr = t; 1079890287Sobrien 1079990287Sobrien /* Constructors and destructors need to know whether they're "in 1080090287Sobrien charge" of initializing virtual base classes. */ 1080190287Sobrien t = TREE_CHAIN (t); 1080290287Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) 1080390287Sobrien { 1080490287Sobrien current_in_charge_parm = t; 1080590287Sobrien t = TREE_CHAIN (t); 1080690287Sobrien } 1080790287Sobrien if (DECL_HAS_VTT_PARM_P (decl1)) 1080890287Sobrien { 10809169699Skan gcc_assert (DECL_NAME (t) == vtt_parm_identifier); 1081090287Sobrien current_vtt_parm = t; 1081190287Sobrien } 1081290287Sobrien } 1081390287Sobrien 10814169699Skan honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1) 10815169699Skan /* Implicitly-defined methods (like the 10816169699Skan destructor for a class in which no destructor 10817169699Skan is explicitly declared) must not be defined 10818169699Skan until their definition is needed. So, we 10819169699Skan ignore interface specifications for 10820169699Skan compiler-generated functions. */ 10821169699Skan && !DECL_ARTIFICIAL (decl1)); 10822169699Skan 1082318334Speter if (DECL_INTERFACE_KNOWN (decl1)) 1082418334Speter { 1082590287Sobrien tree ctx = decl_function_context (decl1); 1082651412Sobrien 1082718334Speter if (DECL_NOT_REALLY_EXTERN (decl1)) 1082818334Speter DECL_EXTERNAL (decl1) = 0; 1082951412Sobrien 1083090287Sobrien if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx) 1083151412Sobrien && TREE_PUBLIC (ctx)) 1083251412Sobrien /* This is a function in a local class in an extern inline 1083351412Sobrien function. */ 1083451412Sobrien comdat_linkage (decl1); 1083518334Speter } 1083618334Speter /* If this function belongs to an interface, it is public. 1083718334Speter If it belongs to someone else's interface, it is also external. 1083852290Sobrien This only affects inlines and template instantiations. */ 10839169699Skan else if (!finfo->interface_unknown && honor_interface) 1084018334Speter { 10841169699Skan if (DECL_DECLARED_INLINE_P (decl1) 1084290287Sobrien || DECL_TEMPLATE_INSTANTIATION (decl1) 1084351412Sobrien || processing_template_decl) 1084452290Sobrien { 1084552290Sobrien DECL_EXTERNAL (decl1) 10846169699Skan = (finfo->interface_only 10847169699Skan || (DECL_DECLARED_INLINE_P (decl1) 1084890287Sobrien && ! flag_implement_inlines 1084952290Sobrien && !DECL_VINDEX (decl1))); 1085052290Sobrien 1085152290Sobrien /* For WIN32 we also want to put these in linkonce sections. */ 1085252290Sobrien maybe_make_one_only (decl1); 1085352290Sobrien } 1085418334Speter else 1085518334Speter DECL_EXTERNAL (decl1) = 0; 1085618334Speter DECL_INTERFACE_KNOWN (decl1) = 1; 10857169699Skan /* If this function is in an interface implemented in this file, 10858169699Skan make sure that the backend knows to emit this function 10859169699Skan here. */ 10860169699Skan if (!DECL_EXTERNAL (decl1)) 10861169699Skan mark_needed (decl1); 1086218334Speter } 10863169699Skan else if (finfo->interface_unknown && finfo->interface_only 10864169699Skan && honor_interface) 1086552290Sobrien { 1086652290Sobrien /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma 10867169699Skan interface, we will have both finfo->interface_unknown and 10868169699Skan finfo->interface_only set. In that case, we don't want to 10869169699Skan use the normal heuristics because someone will supply a 10870169699Skan #pragma implementation elsewhere, and deducing it here would 10871169699Skan produce a conflict. */ 1087252290Sobrien comdat_linkage (decl1); 1087352290Sobrien DECL_EXTERNAL (decl1) = 0; 1087452290Sobrien DECL_INTERFACE_KNOWN (decl1) = 1; 1087552290Sobrien DECL_DEFER_OUTPUT (decl1) = 1; 1087652290Sobrien } 1087718334Speter else 1087818334Speter { 1087918334Speter /* This is a definition, not a reference. 1088018334Speter So clear DECL_EXTERNAL. */ 1088118334Speter DECL_EXTERNAL (decl1) = 0; 1088218334Speter 10883169699Skan if ((DECL_DECLARED_INLINE_P (decl1) 1088490287Sobrien || DECL_TEMPLATE_INSTANTIATION (decl1)) 1088551412Sobrien && ! DECL_INTERFACE_KNOWN (decl1) 1088651412Sobrien /* Don't try to defer nested functions for now. */ 1088790287Sobrien && ! decl_function_context (decl1)) 1088818334Speter DECL_DEFER_OUTPUT (decl1) = 1; 1088918334Speter else 1089051412Sobrien DECL_INTERFACE_KNOWN (decl1) = 1; 1089118334Speter } 1089218334Speter 10893169699Skan /* Determine the ELF visibility attribute for the function. We must not 10894169699Skan do this before calling "pushdecl", as we must allow "duplicate_decls" 10895169699Skan to merge any attributes appropriately. We also need to wait until 10896169699Skan linkage is set. */ 10897169699Skan if (!DECL_CLONED_FUNCTION_P (decl1)) 10898169699Skan determine_visibility (decl1); 10899169699Skan 10900132747Skan begin_scope (sk_function_parms, decl1); 1090118334Speter 1090251412Sobrien ++function_depth; 1090351412Sobrien 10904169699Skan if (DECL_DESTRUCTOR_P (decl1) 10905169699Skan || (DECL_CONSTRUCTOR_P (decl1) 10906169699Skan && targetm.cxx.cdtor_returns_this ())) 1090718334Speter { 10908169699Skan cdtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); 10909169699Skan DECL_CONTEXT (cdtor_label) = current_function_decl; 1091018334Speter } 1091118334Speter 1091290287Sobrien start_fname_decls (); 10913169699Skan 1091490287Sobrien store_parm_decls (current_function_parms); 10915169699Skan} 1091690287Sobrien 10917169699Skan 10918169699Skan/* Like start_preparsed_function, except that instead of a 10919169699Skan FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR. 10920169699Skan 10921169699Skan Returns 1 on success. If the DECLARATOR is not suitable for a function 10922169699Skan (it defines a datum instead), we return 0, which tells 10923169699Skan yyparse to report a parse error. */ 10924169699Skan 10925169699Skanint 10926169699Skanstart_function (cp_decl_specifier_seq *declspecs, 10927169699Skan const cp_declarator *declarator, 10928169699Skan tree attrs) 10929169699Skan{ 10930169699Skan tree decl1; 10931169699Skan 10932169699Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs); 10933169699Skan /* If the declarator is not suitable for a function definition, 10934169699Skan cause a syntax error. */ 10935169699Skan if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) 10936169699Skan return 0; 10937169699Skan 10938169699Skan if (DECL_MAIN_P (decl1)) 10939169699Skan /* main must return int. grokfndecl should have corrected it 10940169699Skan (and issued a diagnostic) if the user got it wrong. */ 10941169699Skan gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)), 10942169699Skan integer_type_node)); 10943169699Skan 10944169699Skan start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT); 10945169699Skan 1094618334Speter return 1; 1094718334Speter} 1094818334Speter 10949169699Skan/* Returns true iff an EH_SPEC_BLOCK should be created in the body of 10950169699Skan FN. */ 10951169699Skan 10952169699Skanstatic bool 10953169699Skanuse_eh_spec_block (tree fn) 10954169699Skan{ 10955169699Skan return (flag_exceptions && flag_enforce_eh_specs 10956169699Skan && !processing_template_decl 10957169699Skan && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)) 10958169699Skan /* We insert the EH_SPEC_BLOCK only in the original 10959169699Skan function; then, it is copied automatically to the 10960169699Skan clones. */ 10961169699Skan && !DECL_CLONED_FUNCTION_P (fn) 10962169699Skan /* Implicitly-generated constructors and destructors have 10963169699Skan exception specifications. However, those specifications 10964169699Skan are the union of the possible exceptions specified by the 10965169699Skan constructors/destructors for bases and members, so no 10966169699Skan unallowed exception will ever reach this function. By 10967169699Skan not creating the EH_SPEC_BLOCK we save a little memory, 10968169699Skan and we avoid spurious warnings about unreachable 10969169699Skan code. */ 10970169699Skan && !DECL_ARTIFICIAL (fn)); 10971169699Skan} 10972169699Skan 1097318334Speter/* Store the parameter declarations into the current function declaration. 1097418334Speter This is called after parsing the parameter declarations, before 1097518334Speter digesting the body of the function. 1097618334Speter 1097718334Speter Also install to binding contour return value identifier, if any. */ 1097818334Speter 1097990287Sobrienstatic void 10980132747Skanstore_parm_decls (tree current_function_parms) 1098118334Speter{ 10982132747Skan tree fndecl = current_function_decl; 10983132747Skan tree parm; 1098418334Speter 1098518334Speter /* This is a chain of any other decls that came in among the parm 1098618334Speter declarations. If a parm is declared with enum {foo, bar} x; 1098718334Speter then CONST_DECLs for foo and bar are put here. */ 1098818334Speter tree nonparms = NULL_TREE; 1098918334Speter 1099090287Sobrien if (current_function_parms) 1099118334Speter { 1099218334Speter /* This case is when the function was defined with an ANSI prototype. 1099318334Speter The parms already have decls, so we need not do anything here 1099418334Speter except record them as in effect 1099518334Speter and complain if any redundant old-style parm decls were written. */ 1099618334Speter 1099790287Sobrien tree specparms = current_function_parms; 1099890287Sobrien tree next; 1099918334Speter 1100018334Speter /* Must clear this because it might contain TYPE_DECLs declared 1100190287Sobrien at class level. */ 11002132747Skan current_binding_level->names = NULL; 1100351412Sobrien 1100490287Sobrien /* If we're doing semantic analysis, then we'll call pushdecl 1100590287Sobrien for each of these. We must do them in reverse order so that 1100690287Sobrien they end in the correct forward order. */ 1100790287Sobrien specparms = nreverse (specparms); 1100890287Sobrien 1100990287Sobrien for (parm = specparms; parm; parm = next) 1101018334Speter { 1101118334Speter next = TREE_CHAIN (parm); 1101218334Speter if (TREE_CODE (parm) == PARM_DECL) 1101318334Speter { 1101490287Sobrien if (DECL_NAME (parm) == NULL_TREE 1101590287Sobrien || TREE_CODE (parm) != VOID_TYPE) 1101690287Sobrien pushdecl (parm); 1101718334Speter else 11018169699Skan error ("parameter %qD declared void", parm); 1101918334Speter } 1102018334Speter else 1102118334Speter { 1102218334Speter /* If we find an enum constant or a type tag, 1102318334Speter put it aside for the moment. */ 1102418334Speter TREE_CHAIN (parm) = NULL_TREE; 1102518334Speter nonparms = chainon (nonparms, parm); 1102618334Speter } 1102718334Speter } 1102818334Speter 1102990287Sobrien /* Get the decls in their original chain order and record in the 1103090287Sobrien function. This is all and only the PARM_DECLs that were 1103190287Sobrien pushed into scope by the loop above. */ 1103218334Speter DECL_ARGUMENTS (fndecl) = getdecls (); 1103318334Speter } 1103418334Speter else 1103518334Speter DECL_ARGUMENTS (fndecl) = NULL_TREE; 1103618334Speter 1103718334Speter /* Now store the final chain of decls for the arguments 1103818334Speter as the decl-chain of the current lexical scope. 1103918334Speter Put the enumerators in as well, at the front so that 1104018334Speter DECL_ARGUMENTS is not modified. */ 11041132747Skan current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl)); 1104218334Speter 11043169699Skan if (use_eh_spec_block (current_function_decl)) 1104490287Sobrien current_eh_spec_block = begin_eh_spec_block (); 1104590287Sobrien} 1104652290Sobrien 1104790287Sobrien 1104890287Sobrien/* We have finished doing semantic analysis on DECL, but have not yet 1104990287Sobrien generated RTL for its body. Save away our current state, so that 1105090287Sobrien when we want to generate RTL later we know what to do. */ 1105118334Speter 1105290287Sobrienstatic void 11053132747Skansave_function_data (tree decl) 1105490287Sobrien{ 11055117410Skan struct language_function *f; 1105651412Sobrien 1105790287Sobrien /* Save the language-specific per-function data so that we can 1105890287Sobrien get it back when we really expand this function. */ 11059169699Skan gcc_assert (!DECL_PENDING_INLINE_P (decl)); 1106051412Sobrien 1106190287Sobrien /* Make a copy. */ 11062169699Skan f = GGC_NEW (struct language_function); 11063117410Skan memcpy (f, cp_function_chain, sizeof (struct language_function)); 1106490287Sobrien DECL_SAVED_FUNCTION_DATA (decl) = f; 1106551412Sobrien 1106690287Sobrien /* Clear out the bits we don't need. */ 11067169699Skan f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE; 1106890287Sobrien f->bindings = NULL; 1106990287Sobrien f->x_local_names = NULL; 1107018334Speter} 1107118334Speter 1107251412Sobrien 11073169699Skan/* Set the return value of the constructor (if present). */ 11074169699Skan 1107590287Sobrienstatic void 11076169699Skanfinish_constructor_body (void) 1107718334Speter{ 11078169699Skan tree val; 11079169699Skan tree exprstmt; 1108018334Speter 11081169699Skan if (targetm.cxx.cdtor_returns_this ()) 11082169699Skan { 11083169699Skan /* Any return from a constructor will end up here. */ 11084169699Skan add_stmt (build_stmt (LABEL_EXPR, cdtor_label)); 1108518334Speter 11086169699Skan val = DECL_ARGUMENTS (current_function_decl); 11087169699Skan val = build2 (MODIFY_EXPR, TREE_TYPE (val), 11088169699Skan DECL_RESULT (current_function_decl), val); 11089169699Skan /* Return the address of the object. */ 11090169699Skan exprstmt = build_stmt (RETURN_EXPR, val); 11091169699Skan add_stmt (exprstmt); 11092169699Skan } 1109318334Speter} 1109418334Speter 1109590287Sobrien/* Do all the processing for the beginning of a destructor; set up the 1109690287Sobrien vtable pointers and cleanups for bases and members. */ 1109760970Sobrien 1109860970Sobrienstatic void 11099132747Skanbegin_destructor_body (void) 1110060970Sobrien{ 1110190287Sobrien tree compound_stmt; 1110260970Sobrien 11103169699Skan /* If the CURRENT_CLASS_TYPE is incomplete, we will have already 11104169699Skan issued an error message. We still want to try to process the 11105169699Skan body of the function, but initialize_vtbl_ptrs will crash if 11106169699Skan TYPE_BINFO is NULL. */ 11107169699Skan if (COMPLETE_TYPE_P (current_class_type)) 11108169699Skan { 11109169699Skan compound_stmt = begin_compound_stmt (0); 11110169699Skan /* Make all virtual function table pointers in non-virtual base 11111169699Skan classes point to CURRENT_CLASS_TYPE's virtual function 11112169699Skan tables. */ 11113169699Skan initialize_vtbl_ptrs (current_class_ptr); 11114169699Skan finish_compound_stmt (compound_stmt); 1111560970Sobrien 11116169699Skan /* And insert cleanups for our bases and members so that they 11117169699Skan will be properly destroyed if we throw. */ 11118169699Skan push_base_cleanups (); 11119169699Skan } 1112060970Sobrien} 1112160970Sobrien 1112290287Sobrien/* At the end of every destructor we generate code to delete the object if 1112390287Sobrien necessary. Do that now. */ 1112460970Sobrien 1112560970Sobrienstatic void 11126132747Skanfinish_destructor_body (void) 1112760970Sobrien{ 1112890287Sobrien tree exprstmt; 1112960970Sobrien 11130107601Sobrien /* Any return from a destructor will end up here; that way all base 11131107601Sobrien and member cleanups will be run when the function returns. */ 11132169699Skan add_stmt (build_stmt (LABEL_EXPR, cdtor_label)); 11133107601Sobrien 1113490287Sobrien /* In a virtual destructor, we must call delete. */ 1113590287Sobrien if (DECL_VIRTUAL_P (current_function_decl)) 1113660970Sobrien { 1113790287Sobrien tree if_stmt; 11138117410Skan tree virtual_size = cxx_sizeof (current_class_type); 1113960970Sobrien 1114090287Sobrien /* [class.dtor] 1114160970Sobrien 1114290287Sobrien At the point of definition of a virtual destructor (including 1114390287Sobrien an implicit definition), non-placement operator delete shall 1114490287Sobrien be looked up in the scope of the destructor's class and if 1114590287Sobrien found shall be accessible and unambiguous. */ 11146169699Skan exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr, 11147169699Skan virtual_size, 11148169699Skan /*global_p=*/false, 11149169699Skan /*placement=*/NULL_TREE, 11150169699Skan /*alloc_fn=*/NULL_TREE); 1115160970Sobrien 1115290287Sobrien if_stmt = begin_if_stmt (); 11153169699Skan finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node, 11154169699Skan current_in_charge_parm, 11155169699Skan integer_one_node), 1115690287Sobrien if_stmt); 1115790287Sobrien finish_expr_stmt (exprstmt); 1115890287Sobrien finish_then_clause (if_stmt); 11159169699Skan finish_if_stmt (if_stmt); 1116060970Sobrien } 11161169699Skan 11162169699Skan if (targetm.cxx.cdtor_returns_this ()) 11163169699Skan { 11164169699Skan tree val; 11165169699Skan 11166169699Skan val = DECL_ARGUMENTS (current_function_decl); 11167169699Skan val = build2 (MODIFY_EXPR, TREE_TYPE (val), 11168169699Skan DECL_RESULT (current_function_decl), val); 11169169699Skan /* Return the address of the object. */ 11170169699Skan exprstmt = build_stmt (RETURN_EXPR, val); 11171169699Skan add_stmt (exprstmt); 11172169699Skan } 1117390287Sobrien} 1117460970Sobrien 1117590287Sobrien/* Do the necessary processing for the beginning of a function body, which 1117690287Sobrien in this case includes member-initializers, but not the catch clauses of 1117790287Sobrien a function-try-block. Currently, this means opening a binding level 11178169699Skan for the member-initializers (in a ctor) and member cleanups (in a dtor). */ 1117960970Sobrien 1118090287Sobrientree 11181132747Skanbegin_function_body (void) 1118290287Sobrien{ 1118390287Sobrien tree stmt; 1118460970Sobrien 11185169699Skan if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 11186169699Skan return NULL_TREE; 11187169699Skan 1118890287Sobrien if (processing_template_decl) 1118990287Sobrien /* Do nothing now. */; 1119090287Sobrien else 1119190287Sobrien /* Always keep the BLOCK node associated with the outermost pair of 1119290287Sobrien curly braces of a function. These are needed for correct 1119390287Sobrien operation of dwarfout.c. */ 11194132747Skan keep_next_level (true); 1119560970Sobrien 11196169699Skan stmt = begin_compound_stmt (BCS_FN_BODY); 1119760970Sobrien 1119890287Sobrien if (processing_template_decl) 1119990287Sobrien /* Do nothing now. */; 1120090287Sobrien else if (DECL_DESTRUCTOR_P (current_function_decl)) 1120190287Sobrien begin_destructor_body (); 1120260970Sobrien 1120390287Sobrien return stmt; 1120490287Sobrien} 1120560970Sobrien 1120690287Sobrien/* Do the processing for the end of a function body. Currently, this means 1120790287Sobrien closing out the cleanups for fully-constructed bases and members, and in 1120890287Sobrien the case of the destructor, deleting the object if desired. Again, this 1120990287Sobrien is only meaningful for [cd]tors, since they are the only functions where 1121090287Sobrien there is a significant distinction between the main body and any 1121190287Sobrien function catch clauses. Handling, say, main() return semantics here 1121290287Sobrien would be wrong, as flowing off the end of a function catch clause for 1121390287Sobrien main() would also need to return 0. */ 1121460970Sobrien 1121590287Sobrienvoid 11216132747Skanfinish_function_body (tree compstmt) 1121790287Sobrien{ 11218169699Skan if (compstmt == NULL_TREE) 11219169699Skan return; 11220169699Skan 11221107601Sobrien /* Close the block. */ 11222132747Skan finish_compound_stmt (compstmt); 1122360970Sobrien 1122490287Sobrien if (processing_template_decl) 1122590287Sobrien /* Do nothing now. */; 1122690287Sobrien else if (DECL_CONSTRUCTOR_P (current_function_decl)) 1122790287Sobrien finish_constructor_body (); 1122890287Sobrien else if (DECL_DESTRUCTOR_P (current_function_decl)) 1122990287Sobrien finish_destructor_body (); 11230169699Skan} 1123160970Sobrien 11232169699Skan/* Given a function, returns the BLOCK corresponding to the outermost level 11233169699Skan of curly braces, skipping the artificial block created for constructor 11234169699Skan initializers. */ 11235169699Skan 11236169699Skanstatic tree 11237169699Skanouter_curly_brace_block (tree fndecl) 11238169699Skan{ 11239169699Skan tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl)); 11240169699Skan if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 11241169699Skan /* Skip the artificial function body block. */ 11242169699Skan block = BLOCK_SUBBLOCKS (block); 11243169699Skan return block; 11244169699Skan} 11245169699Skan 1124618334Speter/* Finish up a function declaration and compile that function 1124718334Speter all the way to assembler language output. The free the storage 1124818334Speter for the function definition. 1124918334Speter 1125090287Sobrien FLAGS is a bitwise or of the following values: 1125152290Sobrien 2 - INCLASS_INLINE 1125252290Sobrien We just finished processing the body of an in-class inline 1125352290Sobrien function definition. (This processing will have taken place 1125490287Sobrien after the class definition is complete.) */ 1125518334Speter 1125690287Sobrientree 11257132747Skanfinish_function (int flags) 1125818334Speter{ 11259132747Skan tree fndecl = current_function_decl; 1126018334Speter tree fntype, ctype = NULL_TREE; 1126152290Sobrien int inclass_inline = (flags & 2) != 0; 1126290287Sobrien int nested; 1126318334Speter 1126418334Speter /* When we get some parse errors, we can end up without a 1126518334Speter current_function_decl, so cope. */ 1126618334Speter if (fndecl == NULL_TREE) 1126790287Sobrien return error_mark_node; 1126818334Speter 11269117410Skan if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) 11270117410Skan && DECL_VIRTUAL_P (fndecl) 11271117410Skan && !processing_template_decl) 11272117410Skan { 11273117410Skan tree fnclass = DECL_CONTEXT (fndecl); 11274117410Skan if (fndecl == CLASSTYPE_KEY_METHOD (fnclass)) 11275117410Skan keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes); 11276117410Skan } 11277117410Skan 1127890287Sobrien nested = function_depth > 1; 1127918334Speter fntype = TREE_TYPE (fndecl); 1128018334Speter 1128190287Sobrien /* TREE_READONLY (fndecl) = 1; 1128290287Sobrien This caused &foo to be of type ptr-to-const-function 1128390287Sobrien which then got a warning when stored in a ptr-to-function variable. */ 1128418334Speter 11285169699Skan gcc_assert (building_stmt_tree ()); 11286260140Spfg /* The current function is being defined, so its DECL_INITIAL should 11287260140Spfg be set, and unless there's a multiple definition, it should be 11288260140Spfg error_mark_node. */ 11289260140Spfg gcc_assert (DECL_INITIAL (fndecl) == error_mark_node); 11290169699Skan 1129190287Sobrien /* For a cloned function, we've already got all the code we need; 1129290287Sobrien there's no need to add any extra bits. */ 1129390287Sobrien if (!DECL_CLONED_FUNCTION_P (fndecl)) 1129418334Speter { 1129590287Sobrien if (DECL_MAIN_P (current_function_decl)) 1129651412Sobrien { 11297169699Skan tree stmt; 11298169699Skan 11299169699Skan /* Make it so that `main' always returns 0 by default (or 11300169699Skan 1 for VMS). */ 11301102804Skan#if VMS_TARGET 11302169699Skan stmt = finish_return_stmt (integer_one_node); 1130318334Speter#else 11304169699Skan stmt = finish_return_stmt (integer_zero_node); 1130518334Speter#endif 11306169699Skan /* Hack. We don't want the middle-end to warn that this 11307169699Skan return is unreachable, so put the statement on the 11308169699Skan special line 0. */ 11309169699Skan#ifdef USE_MAPPED_LOCATION 11310169699Skan SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION); 11311169699Skan#else 11312169699Skan annotate_with_file_line (stmt, input_filename, 0); 11313169699Skan#endif 1131451412Sobrien } 1131518334Speter 11316169699Skan if (use_eh_spec_block (current_function_decl)) 1131790287Sobrien finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS 1131890287Sobrien (TREE_TYPE (current_function_decl)), 1131990287Sobrien current_eh_spec_block); 1132090287Sobrien } 1132118334Speter 11322169699Skan /* If we're saving up tree structure, tie off the function now. */ 11323169699Skan DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 11324169699Skan 11325132747Skan finish_fname_decls (); 11326132747Skan 11327117410Skan /* If this function can't throw any exceptions, remember that. */ 11328117410Skan if (!processing_template_decl 11329117410Skan && !cp_function_chain->can_throw 11330169699Skan && !flag_non_call_exceptions 11331169699Skan && !DECL_REPLACEABLE_P (fndecl)) 11332117410Skan TREE_NOTHROW (fndecl) = 1; 11333117410Skan 1133490287Sobrien /* This must come after expand_function_end because cleanups might 1133590287Sobrien have declarations (from inline functions) that need to go into 1133690287Sobrien this function's blocks. */ 11337169699Skan 1133890287Sobrien /* If the current binding level isn't the outermost binding level 1133990287Sobrien for this function, either there is a bug, or we have experienced 1134090287Sobrien syntax errors and the statement tree is malformed. */ 11341132747Skan if (current_binding_level->kind != sk_function_parms) 1134290287Sobrien { 1134390287Sobrien /* Make sure we have already experienced errors. */ 11344169699Skan gcc_assert (errorcount); 1134518334Speter 1134690287Sobrien /* Throw away the broken statement tree and extra binding 11347169699Skan levels. */ 11348169699Skan DECL_SAVED_TREE (fndecl) = alloc_stmt_list (); 1134918334Speter 11350132747Skan while (current_binding_level->kind != sk_function_parms) 1135151412Sobrien { 11352132747Skan if (current_binding_level->kind == sk_class) 1135390287Sobrien pop_nested_class (); 1135490287Sobrien else 1135590287Sobrien poplevel (0, 0, 0); 1135651412Sobrien } 1135718334Speter } 1135818334Speter poplevel (1, 0, 1); 1135918334Speter 11360132747Skan /* Statements should always be full-expressions at the outermost set 11361132747Skan of curly braces for a function. */ 11362169699Skan gcc_assert (stmts_are_full_exprs_p ()); 11363132747Skan 11364169699Skan /* Set up the named return value optimization, if we can. Candidate 11365220150Smm variables are selected in check_return_expr. */ 1136690287Sobrien if (current_function_return_value) 1136718334Speter { 1136890287Sobrien tree r = current_function_return_value; 11369117410Skan tree outer; 11370117410Skan 1137190287Sobrien if (r != error_mark_node 11372117410Skan /* This is only worth doing for fns that return in memory--and 11373117410Skan simpler, since we don't have to worry about promoted modes. */ 11374132747Skan && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl) 11375117410Skan /* Only allow this for variables declared in the outer scope of 11376117410Skan the function so we know that their lifetime always ends with a 11377117410Skan return; see g++.dg/opt/nrv6.C. We could be more flexible if 11378117410Skan we were to do this optimization in tree-ssa. */ 11379169699Skan && (outer = outer_curly_brace_block (fndecl)) 11380169699Skan && chain_member (r, BLOCK_VARS (outer))) 11381169699Skan finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl)); 11382169699Skan 11383169699Skan current_function_return_value = NULL_TREE; 1138418334Speter } 1138518334Speter 1138690287Sobrien /* Remember that we were in class scope. */ 1138790287Sobrien if (current_class_name) 1138890287Sobrien ctype = current_class_type; 1138990287Sobrien 1139018334Speter /* Must mark the RESULT_DECL as being in this function. */ 1139151412Sobrien DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 1139218334Speter 1139318334Speter /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point 1139418334Speter to the FUNCTION_DECL node itself. */ 1139518334Speter BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 1139618334Speter 1139790287Sobrien /* Save away current state, if appropriate. */ 1139890287Sobrien if (!processing_template_decl) 1139990287Sobrien save_function_data (fndecl); 1140018334Speter 1140196292Sobrien /* Complain if there's just no return statement. */ 1140296292Sobrien if (warn_return_type 1140396292Sobrien && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE 11404132747Skan && !dependent_type_p (TREE_TYPE (fntype)) 1140596292Sobrien && !current_function_returns_value && !current_function_returns_null 1140696292Sobrien /* Don't complain if we abort or throw. */ 1140796292Sobrien && !current_function_returns_abnormally 1140896292Sobrien && !DECL_NAME (DECL_RESULT (fndecl)) 1140996292Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 1141096292Sobrien inline function, as we might never be compiled separately. */ 11411169699Skan && (DECL_INLINE (fndecl) || processing_template_decl) 11412169699Skan /* Structor return values (if any) are set by the compiler. */ 11413169699Skan && !DECL_CONSTRUCTOR_P (fndecl) 11414169699Skan && !DECL_DESTRUCTOR_P (fndecl)) 11415169699Skan warning (OPT_Wreturn_type, "no return statement in function returning non-void"); 11416132747Skan 11417169699Skan /* Store the end of the function, so that we get good line number 11418169699Skan info for the epilogue. */ 11419169699Skan cfun->function_end_locus = input_location; 11420169699Skan 11421169699Skan /* Genericize before inlining. */ 11422169699Skan if (!processing_template_decl) 11423169699Skan { 11424169699Skan struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl); 11425169699Skan cp_genericize (fndecl); 11426169699Skan /* Clear out the bits we don't need. */ 11427169699Skan f->x_current_class_ptr = NULL; 11428169699Skan f->x_current_class_ref = NULL; 11429169699Skan f->x_eh_spec_block = NULL; 11430169699Skan f->x_in_charge_parm = NULL; 11431169699Skan f->x_vtt_parm = NULL; 11432169699Skan f->x_return_value = NULL; 11433169699Skan f->bindings = NULL; 11434169699Skan f->extern_decl_map = NULL; 11435169699Skan 11436169699Skan /* Handle attribute((warn_unused_result)). Relies on gimple input. */ 11437169699Skan c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); 11438169699Skan } 11439169699Skan /* Clear out the bits we don't need. */ 11440169699Skan local_names = NULL; 11441169699Skan 11442132747Skan /* We're leaving the context of this function, so zap cfun. It's still in 11443169699Skan DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */ 1144490287Sobrien cfun = NULL; 11445132747Skan current_function_decl = NULL; 1144618334Speter 11447117410Skan /* If this is an in-class inline definition, we may have to pop the 1144890287Sobrien bindings for the template parameters that we added in 1144990287Sobrien maybe_begin_member_template_processing when start_function was 1145090287Sobrien called. */ 1145190287Sobrien if (inclass_inline) 1145290287Sobrien maybe_end_member_template_processing (); 1145318334Speter 1145490287Sobrien /* Leave the scope of the class. */ 1145590287Sobrien if (ctype) 1145690287Sobrien pop_nested_class (); 1145718334Speter 1145851412Sobrien --function_depth; 1145951412Sobrien 1146090287Sobrien /* Clean up. */ 1146118334Speter if (! nested) 1146290287Sobrien /* Let the error reporting routines know that we're outside a 1146390287Sobrien function. For a nested function, this value is used in 11464117410Skan cxx_pop_function_context and then reset via pop_function_context. */ 1146590287Sobrien current_function_decl = NULL_TREE; 1146618334Speter 1146790287Sobrien return fndecl; 1146818334Speter} 1146918334Speter 1147018334Speter/* Create the FUNCTION_DECL for a function definition. 1147118334Speter DECLSPECS and DECLARATOR are the parts of the declaration; 1147218334Speter they describe the return type and the name of the function, 1147318334Speter but twisted together in a fashion that parallels the syntax of C. 1147418334Speter 1147518334Speter This function creates a binding context for the function body 1147618334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 1147718334Speter 1147818334Speter Returns a FUNCTION_DECL on success. 1147918334Speter 1148018334Speter If the DECLARATOR is not suitable for a function (it defines a datum 1148118334Speter instead), we return 0, which tells yyparse to report a parse error. 1148218334Speter 1148318334Speter May return void_type_node indicating that this method is actually 1148418334Speter a friend. See grokfield for more details. 1148518334Speter 1148618334Speter Came here with a `.pushlevel' . 1148718334Speter 1148818334Speter DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING 1148918334Speter CHANGES TO CODE IN `grokfield'. */ 1149051412Sobrien 1149118334Spetertree 11492169699Skanstart_method (cp_decl_specifier_seq *declspecs, 11493169699Skan const cp_declarator *declarator, tree attrlist) 1149418334Speter{ 1149551412Sobrien tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, 1149690287Sobrien &attrlist); 1149718334Speter 11498132747Skan if (fndecl == error_mark_node) 11499132747Skan return error_mark_node; 1150018334Speter 11501132747Skan if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL) 11502132747Skan { 11503132747Skan error ("invalid member function declaration"); 11504132747Skan return error_mark_node; 11505132747Skan } 11506132747Skan 1150790287Sobrien if (attrlist) 1150890287Sobrien cplus_decl_attributes (&fndecl, attrlist, 0); 1150990287Sobrien 1151018334Speter /* Pass friends other than inline friend functions back. */ 1151151412Sobrien if (fndecl == void_type_node) 1151218334Speter return fndecl; 1151318334Speter 1151418334Speter if (DECL_IN_AGGR_P (fndecl)) 1151518334Speter { 11516169699Skan if (DECL_CONTEXT (fndecl) 11517169699Skan && TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL) 11518169699Skan error ("%qD is already defined in class %qT", fndecl, 11519169699Skan DECL_CONTEXT (fndecl)); 11520169699Skan return error_mark_node; 1152118334Speter } 1152218334Speter 1152352290Sobrien check_template_shadow (fndecl); 1152452290Sobrien 1152590287Sobrien DECL_DECLARED_INLINE_P (fndecl) = 1; 1152618334Speter if (flag_default_inline) 1152718334Speter DECL_INLINE (fndecl) = 1; 1152818334Speter 1152952290Sobrien /* We process method specializations in finish_struct_1. */ 1153052290Sobrien if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) 11531132747Skan { 11532132747Skan fndecl = push_template_decl (fndecl); 11533132747Skan if (fndecl == error_mark_node) 11534132747Skan return fndecl; 11535132747Skan } 1153618334Speter 1153718334Speter if (! DECL_FRIEND_P (fndecl)) 1153818334Speter { 1153918334Speter if (TREE_CHAIN (fndecl)) 1154018334Speter { 1154118334Speter fndecl = copy_node (fndecl); 1154218334Speter TREE_CHAIN (fndecl) = NULL_TREE; 1154318334Speter } 1154418334Speter } 1154518334Speter 11546169699Skan finish_decl (fndecl, NULL_TREE, NULL_TREE); 1154718334Speter 11548132747Skan /* Make a place for the parms. */ 11549132747Skan begin_scope (sk_function_parms, fndecl); 1155090287Sobrien 1155118334Speter DECL_IN_AGGR_P (fndecl) = 1; 1155218334Speter return fndecl; 1155318334Speter} 1155418334Speter 1155518334Speter/* Go through the motions of finishing a function definition. 1155618334Speter We don't compile this method until after the whole class has 1155718334Speter been processed. 1155818334Speter 1155918334Speter FINISH_METHOD must return something that looks as though it 1156018334Speter came from GROKFIELD (since we are defining a method, after all). 1156118334Speter 1156218334Speter This is called after parsing the body of the function definition. 1156318334Speter STMTS is the chain of statements that makes up the function body. 1156418334Speter 1156518334Speter DECL is the ..._DECL that `start_method' provided. */ 1156618334Speter 1156718334Spetertree 11568132747Skanfinish_method (tree decl) 1156918334Speter{ 11570132747Skan tree fndecl = decl; 1157118334Speter tree old_initial; 1157218334Speter 11573132747Skan tree link; 1157418334Speter 1157551412Sobrien if (decl == void_type_node) 1157618334Speter return decl; 1157718334Speter 1157818334Speter old_initial = DECL_INITIAL (fndecl); 1157918334Speter 1158018334Speter /* Undo the level for the parms (from start_method). 1158118334Speter This is like poplevel, but it causes nothing to be 1158218334Speter saved. Saving information here confuses symbol-table 1158318334Speter output routines. Besides, this information will 1158418334Speter be correctly output when this method is actually 1158518334Speter compiled. */ 1158618334Speter 1158718334Speter /* Clear out the meanings of the local variables of this level; 1158818334Speter also record in each decl which block it belongs to. */ 1158918334Speter 1159018334Speter for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) 1159118334Speter { 1159218334Speter if (DECL_NAME (link) != NULL_TREE) 1159352290Sobrien pop_binding (DECL_NAME (link), link); 11594169699Skan gcc_assert (TREE_CODE (link) != FUNCTION_DECL); 1159518334Speter DECL_CONTEXT (link) = NULL_TREE; 1159618334Speter } 1159718334Speter 1159818334Speter poplevel (0, 0, 0); 1159918334Speter 1160018334Speter DECL_INITIAL (fndecl) = old_initial; 1160118334Speter 1160218334Speter /* We used to check if the context of FNDECL was different from 1160318334Speter current_class_type as another way to get inside here. This didn't work 1160418334Speter for String.cc in libg++. */ 1160518334Speter if (DECL_FRIEND_P (fndecl)) 1160618334Speter { 11607169699Skan VEC_safe_push (tree, gc, CLASSTYPE_INLINE_FRIENDS (current_class_type), 11608169699Skan fndecl); 1160918334Speter decl = void_type_node; 1161018334Speter } 1161118334Speter 1161218334Speter return decl; 1161318334Speter} 1161418334Speter 1161518334Speter 1161696292Sobrien/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that 1161796292Sobrien we can lay it out later, when and if its type becomes complete. */ 1161896292Sobrien 1161918334Spetervoid 11620132747Skanmaybe_register_incomplete_var (tree var) 1162118334Speter{ 11622169699Skan gcc_assert (TREE_CODE (var) == VAR_DECL); 1162318334Speter 1162496292Sobrien /* Keep track of variables with incomplete types. */ 11625169699Skan if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 1162696292Sobrien && DECL_EXTERNAL (var)) 1162718334Speter { 1162896292Sobrien tree inner_type = TREE_TYPE (var); 11629169699Skan 1163096292Sobrien while (TREE_CODE (inner_type) == ARRAY_TYPE) 1163196292Sobrien inner_type = TREE_TYPE (inner_type); 1163296292Sobrien inner_type = TYPE_MAIN_VARIANT (inner_type); 11633169699Skan 1163496292Sobrien if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) 1163596292Sobrien /* RTTI TD entries are created while defining the type_info. */ 1163696292Sobrien || (TYPE_LANG_SPECIFIC (inner_type) 1163796292Sobrien && TYPE_BEING_DEFINED (inner_type))) 1163896292Sobrien incomplete_vars = tree_cons (inner_type, var, incomplete_vars); 1163990287Sobrien } 1164096292Sobrien} 1164190287Sobrien 1164296292Sobrien/* Called when a class type (given by TYPE) is defined. If there are 1164396292Sobrien any existing VAR_DECLs whose type hsa been completed by this 1164496292Sobrien declaration, update them now. */ 1164596292Sobrien 1164696292Sobrienvoid 11647132747Skancomplete_vars (tree type) 1164896292Sobrien{ 1164996292Sobrien tree *list = &incomplete_vars; 1165096292Sobrien 11651169699Skan gcc_assert (CLASS_TYPE_P (type)); 11652169699Skan while (*list) 1165390287Sobrien { 1165496292Sobrien if (same_type_p (type, TREE_PURPOSE (*list))) 1165518334Speter { 1165696292Sobrien tree var = TREE_VALUE (*list); 11657169699Skan tree type = TREE_TYPE (var); 1165896292Sobrien /* Complete the type of the variable. The VAR_DECL itself 1165996292Sobrien will be laid out in expand_expr. */ 11660169699Skan complete_type (type); 11661169699Skan cp_apply_type_quals_to_decl (cp_type_quals (type), var); 1166296292Sobrien /* Remove this entry from the list. */ 1166396292Sobrien *list = TREE_CHAIN (*list); 1166418334Speter } 1166518334Speter else 1166696292Sobrien list = &TREE_CHAIN (*list); 1166718334Speter } 11668169699Skan 11669169699Skan /* Check for pending declarations which may have abstract type. */ 11670169699Skan complete_type_check_abstract (type); 1167118334Speter} 1167218334Speter 1167390287Sobrien/* If DECL is of a type which needs a cleanup, build that cleanup 1167490287Sobrien here. */ 1167518334Speter 1167690287Sobrientree 11677132747Skancxx_maybe_build_cleanup (tree decl) 1167818334Speter{ 1167918334Speter tree type = TREE_TYPE (decl); 1168090287Sobrien 1168190287Sobrien if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 1168218334Speter { 1168390287Sobrien int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR; 1168418334Speter tree rval; 11685169699Skan bool has_vbases = (TREE_CODE (type) == RECORD_TYPE 11686169699Skan && CLASSTYPE_VBASECLASSES (type)); 1168718334Speter 1168818334Speter if (TREE_CODE (type) == ARRAY_TYPE) 1168918334Speter rval = decl; 1169018334Speter else 1169118334Speter { 11692117410Skan cxx_mark_addressable (decl); 1169318334Speter rval = build_unary_op (ADDR_EXPR, decl, 0); 1169418334Speter } 1169518334Speter 1169618334Speter /* Optimize for space over speed here. */ 11697169699Skan if (!has_vbases || flag_expensive_optimizations) 1169818334Speter flags |= LOOKUP_NONVIRTUAL; 1169918334Speter 1170090287Sobrien rval = build_delete (TREE_TYPE (rval), rval, 1170190287Sobrien sfk_complete_destructor, flags, 0); 1170218334Speter 1170318334Speter return rval; 1170418334Speter } 1170596292Sobrien return NULL_TREE; 1170618334Speter} 1170718334Speter 1170890287Sobrien/* When a stmt has been parsed, this function is called. */ 1170918334Speter 1171018334Spetervoid 11711132747Skanfinish_stmt (void) 1171218334Speter{ 1171318334Speter} 1171418334Speter 1171590287Sobrien/* DECL was originally constructed as a non-static member function, 1171690287Sobrien but turned out to be static. Update it accordingly. */ 1171718334Speter 1171818334Spetervoid 11719132747Skanrevert_static_member_fn (tree decl) 1172018334Speter{ 1172118334Speter tree tmp; 1172290287Sobrien tree function = TREE_TYPE (decl); 1172390287Sobrien tree args = TYPE_ARG_TYPES (function); 1172418334Speter 1172590287Sobrien if (cp_type_quals (TREE_TYPE (TREE_VALUE (args))) 1172652290Sobrien != TYPE_UNQUALIFIED) 11727169699Skan error ("static member function %q#D declared with type qualifiers", decl); 1172818334Speter 1172918334Speter args = TREE_CHAIN (args); 1173018334Speter tmp = build_function_type (TREE_TYPE (function), args); 1173190287Sobrien tmp = build_qualified_type (tmp, cp_type_quals (function)); 1173218334Speter tmp = build_exception_variant (tmp, 1173318334Speter TYPE_RAISES_EXCEPTIONS (function)); 1173490287Sobrien TREE_TYPE (decl) = tmp; 1173590287Sobrien if (DECL_ARGUMENTS (decl)) 1173690287Sobrien DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl)); 1173790287Sobrien DECL_STATIC_FUNCTION_P (decl) = 1; 1173818334Speter} 1173918334Speter 1174090287Sobrien/* Initialize the variables used during compilation of a C++ 1174190287Sobrien function. */ 1174290287Sobrien 11743117410Skanvoid 11744132747Skancxx_push_function_context (struct function * f) 1174518334Speter{ 11746169699Skan struct language_function *p = GGC_CNEW (struct language_function); 11747117410Skan f->language = p; 1174818334Speter 1174990287Sobrien /* Whenever we start a new function, we destroy temporaries in the 1175090287Sobrien usual way. */ 1175190287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 1; 11752132747Skan 11753132747Skan if (f->decl) 11754132747Skan { 11755132747Skan tree fn = f->decl; 11756132747Skan 11757132747Skan if (DECL_SAVED_FUNCTION_DATA (fn)) 11758132747Skan { 11759132747Skan /* If we already parsed this function, and we're just expanding it 11760132747Skan now, restore saved state. */ 11761132747Skan *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn); 11762132747Skan 11763132747Skan /* We don't need the saved data anymore. Unless this is an inline 11764132747Skan function; we need the named return value info for 11765169699Skan declare_return_variable. */ 11766132747Skan if (! DECL_INLINE (fn)) 11767132747Skan DECL_SAVED_FUNCTION_DATA (fn) = NULL; 11768132747Skan } 11769132747Skan } 1177090287Sobrien} 1177118334Speter 1177290287Sobrien/* Free the language-specific parts of F, now that we've finished 1177390287Sobrien compiling the function. */ 1177418334Speter 11775117410Skanvoid 11776132747Skancxx_pop_function_context (struct function * f) 1177718334Speter{ 1177890287Sobrien f->language = 0; 1177990287Sobrien} 1178018334Speter 11781117410Skan/* Return which tree structure is used by T, or TS_CP_GENERIC if T is 11782117410Skan one of the language-independent trees. */ 1178318334Speter 11784117410Skanenum cp_tree_node_structure_enum 11785132747Skancp_tree_node_structure (union lang_tree_node * t) 1178690287Sobrien{ 11787117410Skan switch (TREE_CODE (&t->generic)) 1178890287Sobrien { 11789132747Skan case DEFAULT_ARG: return TS_CP_DEFAULT_ARG; 11790117410Skan case IDENTIFIER_NODE: return TS_CP_IDENTIFIER; 11791117410Skan case OVERLOAD: return TS_CP_OVERLOAD; 11792117410Skan case TEMPLATE_PARM_INDEX: return TS_CP_TPI; 11793169699Skan case TINST_LEVEL: return TS_CP_TINST_LEVEL; 11794117410Skan case PTRMEM_CST: return TS_CP_PTRMEM; 11795169699Skan case BASELINK: return TS_CP_BASELINK; 11796117410Skan default: return TS_CP_GENERIC; 1179790287Sobrien } 1179890287Sobrien} 1179918334Speter 1180090287Sobrien/* Build the void_list_node (void_type_node having been created). */ 1180190287Sobrientree 11802132747Skanbuild_void_list_node (void) 1180318334Speter{ 1180490287Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 1180590287Sobrien return t; 1180618334Speter} 1180790287Sobrien 11808169699Skanbool 11809132747Skancp_missing_noreturn_ok_p (tree decl) 1181090287Sobrien{ 1181190287Sobrien /* A missing noreturn is ok for the `main' function. */ 1181290287Sobrien return DECL_MAIN_P (decl); 1181390287Sobrien} 11814117410Skan 11815169699Skan/* Return the COMDAT group into which DECL should be placed. */ 11816169699Skan 11817169699Skanconst char * 11818169699Skancxx_comdat_group (tree decl) 11819169699Skan{ 11820169699Skan tree name; 11821169699Skan 11822169699Skan /* Virtual tables, construction virtual tables, and virtual table 11823169699Skan tables all go in a single COMDAT group, named after the primary 11824169699Skan virtual table. */ 11825169699Skan if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl)) 11826169699Skan name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl))); 11827169699Skan /* For all other DECLs, the COMDAT group is the mangled name of the 11828169699Skan declaration itself. */ 11829169699Skan else 11830169699Skan { 11831169699Skan while (DECL_THUNK_P (decl)) 11832169699Skan { 11833169699Skan /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk 11834169699Skan into the same section as the target function. In that case 11835169699Skan we must return target's name. */ 11836169699Skan tree target = THUNK_TARGET (decl); 11837169699Skan if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target) 11838169699Skan && DECL_SECTION_NAME (target) != NULL 11839169699Skan && DECL_ONE_ONLY (target)) 11840169699Skan decl = target; 11841169699Skan else 11842169699Skan break; 11843169699Skan } 11844169699Skan name = DECL_ASSEMBLER_NAME (decl); 11845169699Skan } 11846169699Skan 11847169699Skan return IDENTIFIER_POINTER (name); 11848169699Skan} 11849169699Skan 11850117410Skan#include "gt-cp-decl.h" 11851