decl.c revision 132747
1132747Skan/* Process declarations and variables for C++ compiler. 290287Sobrien Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 3132747Skan 2001, 2002, 2003, 2004 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 2018334Speterthe Free Software Foundation, 59 Temple Place - Suite 330, 2118334SpeterBoston, MA 02111-1307, 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" 4218334Speter#include "lex.h" 4351412Sobrien#include "output.h" 4451412Sobrien#include "except.h" 4551412Sobrien#include "toplev.h" 46117410Skan#include "hashtab.h" 4790287Sobrien#include "tm_p.h" 4890287Sobrien#include "target.h" 4990287Sobrien#include "c-common.h" 5096292Sobrien#include "c-pragma.h" 5190287Sobrien#include "diagnostic.h" 52117410Skan#include "debug.h" 53117410Skan#include "timevar.h" 5418334Speter 55132747Skanstatic tree grokparms (tree, 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 *); 64132747Skanstatic tree grokfndecl (tree, tree, tree, tree, tree, int, 65132747Skan enum overload_flags, tree, 66132747Skan tree, int, int, int, int, int, int, tree); 67132747Skanstatic tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree); 68132747Skanstatic void record_unknown_type (tree, const char *); 69132747Skanstatic tree builtin_function_1 (const char *, tree, tree, int, 70132747Skan enum built_in_class, const char *, 71132747Skan tree); 72132747Skanstatic tree build_library_fn_1 (tree, enum tree_code, tree); 73132747Skanstatic int member_function_or_else (tree, tree, enum overload_flags); 74132747Skanstatic void bad_specifiers (tree, const char *, int, int, int, int, 75132747Skan int); 76132747Skanstatic void check_for_uninitialized_const_var (tree); 77132747Skanstatic hashval_t typename_hash (const void *); 78132747Skanstatic int typename_compare (const void *, const void *); 79132747Skanstatic tree local_variable_p_walkfn (tree *, int *, void *); 80132747Skanstatic tree record_builtin_java_type (const char *, int); 81132747Skanstatic const char *tag_name (enum tag_types code); 82132747Skanstatic int walk_namespaces_r (tree, walk_namespaces_fn, void *); 83132747Skanstatic int walk_globals_r (tree, void*); 84132747Skanstatic int walk_vtables_r (tree, void*); 85132747Skanstatic tree make_label_decl (tree, int); 86132747Skanstatic void use_label (tree); 87132747Skanstatic void check_previous_goto_1 (tree, struct cp_binding_level *, tree, 88132747Skan const location_t *); 89132747Skanstatic void check_previous_goto (struct named_label_use_list *); 90132747Skanstatic void check_switch_goto (struct cp_binding_level *); 91132747Skanstatic void check_previous_gotos (tree); 92132747Skanstatic void pop_label (tree, tree); 93132747Skanstatic void pop_labels (tree); 94132747Skanstatic void maybe_deduce_size_from_array_init (tree, tree); 95132747Skanstatic void layout_var_decl (tree); 96132747Skanstatic void maybe_commonize_var (tree); 97122192Skanstatic tree check_initializer (tree, tree, int, tree *); 98132747Skanstatic void make_rtl_for_nonlocal_decl (tree, tree, const char *); 99132747Skanstatic void save_function_data (tree); 100132747Skanstatic void check_function_type (tree, tree); 101132747Skanstatic void begin_constructor_body (void); 102132747Skanstatic void finish_constructor_body (void); 103132747Skanstatic void begin_destructor_body (void); 104132747Skanstatic void finish_destructor_body (void); 105132747Skanstatic tree create_array_type_for_decl (tree, tree, tree); 106132747Skanstatic tree get_atexit_node (void); 107132747Skanstatic tree get_dso_handle_node (void); 108132747Skanstatic tree start_cleanup_fn (void); 109132747Skanstatic void end_cleanup_fn (void); 110132747Skanstatic tree cp_make_fname_decl (tree, int); 111132747Skanstatic void initialize_predefined_identifiers (void); 112132747Skanstatic tree check_special_function_return_type 113132747Skan (special_function_kind, tree, tree); 114132747Skanstatic tree push_cp_library_fn (enum tree_code, tree); 115132747Skanstatic tree build_cp_library_fn (tree, enum tree_code, tree); 116132747Skanstatic void store_parm_decls (tree); 117132747Skanstatic int cp_missing_noreturn_ok_p (tree); 118117410Skanstatic void initialize_local_var (tree, tree); 119117410Skanstatic void expand_static_init (tree, tree); 120117410Skanstatic tree next_initializable_field (tree); 121117410Skanstatic tree reshape_init (tree, tree *); 122132747Skanstatic tree build_typename_type (tree, tree, tree); 12318334Speter 12418334Speter/* Erroneous argument lists can use this *IFF* they do not modify it. */ 12518334Spetertree error_mark_list; 12618334Speter 12790287Sobrien/* The following symbols are subsumed in the cp_global_trees array, and 12890287Sobrien listed here individually for documentation purposes. 12918334Speter 13090287Sobrien C++ extensions 13190287Sobrien tree wchar_decl_node; 13218334Speter 13390287Sobrien tree vtable_entry_type; 13490287Sobrien tree delta_type_node; 13590287Sobrien tree __t_desc_type_node; 13690287Sobrien tree ti_desc_type_node; 13790287Sobrien tree bltn_desc_type_node, ptr_desc_type_node; 13890287Sobrien tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node; 13990287Sobrien tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node; 14090287Sobrien tree ptm_desc_type_node; 14190287Sobrien tree base_desc_type_node; 14218334Speter 143132747Skan tree class_type_node; 14490287Sobrien tree unknown_type_node; 14518334Speter 14690287Sobrien Array type `vtable_entry_type[]' 14718334Speter 14890287Sobrien tree vtbl_type_node; 14990287Sobrien tree vtbl_ptr_type_node; 15018334Speter 15190287Sobrien Namespaces, 15218334Speter 15390287Sobrien tree std_node; 15490287Sobrien tree abi_node; 15551412Sobrien 15690287Sobrien A FUNCTION_DECL which can call `abort'. Not necessarily the 15790287Sobrien one that the user will declare, but sufficient to be called 15890287Sobrien by routines that want to abort the program. 15918334Speter 16090287Sobrien tree abort_fndecl; 16118334Speter 16290287Sobrien The FUNCTION_DECL for the default `::operator delete'. 16318334Speter 16490287Sobrien tree global_delete_fndecl; 16551412Sobrien 16690287Sobrien Used by RTTI 16790287Sobrien tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; 16890287Sobrien tree tinfo_var_id; 16918334Speter 17090287Sobrien*/ 17118334Speter 17290287Sobrientree cp_global_trees[CPTI_MAX]; 17318334Speter 17451412Sobrien/* Indicates that there is a type value in some namespace, although 17590287Sobrien that is not necessarily in scope at the moment. */ 17651412Sobrien 177132747Skantree global_type_node; 17851412Sobrien 179132747Skan/* The node that holds the "name" of the global scope. */ 180132747Skantree global_scope_name; 18151412Sobrien 18290287Sobrien/* Used only for jumps to as-yet undefined labels, since jumps to 18390287Sobrien defined labels can have their validity checked immediately. */ 18418334Speter 185117410Skanstruct named_label_use_list GTY(()) 18651412Sobrien{ 187117410Skan struct cp_binding_level *binding_level; 18851412Sobrien tree names_in_scope; 18951412Sobrien tree label_decl; 190132747Skan location_t o_goto_locus; 19190287Sobrien struct named_label_use_list *next; 19251412Sobrien}; 19351412Sobrien 19490287Sobrien#define named_label_uses cp_function_chain->x_named_label_uses 19518334Speter 19690287Sobrien#define local_names cp_function_chain->x_local_names 19751412Sobrien 19818334Speter/* A list of objects which have constructors or destructors 19918334Speter which reside in the global scope. The decl is stored in 20018334Speter the TREE_VALUE slot and the initializer is stored 20118334Speter in the TREE_PURPOSE slot. */ 20218334Spetertree static_aggregates; 20318334Speter 20418334Speter/* -- end of C++ */ 20518334Speter 20690287Sobrien/* A node for the integer constants 2, and 3. */ 20718334Speter 20890287Sobrientree integer_two_node, integer_three_node; 20918334Speter 21090287Sobrien/* A list of all LABEL_DECLs in the function that have names. Here so 21190287Sobrien we can clear out their names' definitions at the end of the 21290287Sobrien function, and so we can check the validity of jumps to these labels. */ 21318334Speter 214117410Skanstruct named_label_list GTY(()) 21590287Sobrien{ 216117410Skan struct cp_binding_level *binding_level; 21790287Sobrien tree names_in_scope; 21890287Sobrien tree old_value; 21990287Sobrien tree label_decl; 22090287Sobrien tree bad_decls; 22190287Sobrien struct named_label_list *next; 22290287Sobrien unsigned int in_try_scope : 1; 22390287Sobrien unsigned int in_catch_scope : 1; 22490287Sobrien}; 22518334Speter 22690287Sobrien#define named_labels cp_function_chain->x_named_labels 22718334Speter 22890287Sobrien/* The number of function bodies which we are currently processing. 22990287Sobrien (Zero if we are at namespace scope, one inside the body of a 23090287Sobrien function, two inside the body of a function in a local class, etc.) */ 23190287Sobrienint function_depth; 23251412Sobrien 23390287Sobrien/* States indicating how grokdeclarator() should handle declspecs marked 23490287Sobrien with __attribute__((deprecated)). An object declared as 23590287Sobrien __attribute__((deprecated)) suppresses warnings of uses of other 23690287Sobrien deprecated items. */ 23790287Sobrien 23890287Sobrienenum deprecated_states { 23990287Sobrien DEPRECATED_NORMAL, 24090287Sobrien DEPRECATED_SUPPRESS 24190287Sobrien}; 24218334Speter 24390287Sobrienstatic enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 24490287Sobrien 24590287Sobrien/* Set by add_implicitly_declared_members() to keep those members from 24690287Sobrien being flagged as deprecated or reported as using deprecated 24790287Sobrien types. */ 24890287Sobrienint adding_implicit_members = 0; 249117410Skan 250117410Skan/* True if a declaration with an `extern' linkage specifier is being 251117410Skan processed. */ 252117410Skanbool have_extern_spec; 253117410Skan 25418334Speter 25596292Sobrien/* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or 25696292Sobrien UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the 25796292Sobrien time the VAR_DECL was declared, the type was incomplete. */ 25896292Sobrien 259117410Skanstatic GTY(()) tree incomplete_vars; 26018334Speter 26190287Sobrien/* Returns the kind of template specialization we are currently 26290287Sobrien processing, given that it's declaration contained N_CLASS_SCOPES 26390287Sobrien explicit scope qualifications. */ 26490287Sobrien 26590287Sobrientmpl_spec_kind 266132747Skancurrent_tmpl_spec_kind (int n_class_scopes) 26790287Sobrien{ 26890287Sobrien int n_template_parm_scopes = 0; 26990287Sobrien int seen_specialization_p = 0; 27090287Sobrien int innermost_specialization_p = 0; 271117410Skan struct cp_binding_level *b; 27290287Sobrien 27390287Sobrien /* Scan through the template parameter scopes. */ 274132747Skan for (b = current_binding_level; 275132747Skan b->kind == sk_template_parms; 276132747Skan b = b->level_chain) 27790287Sobrien { 27890287Sobrien /* If we see a specialization scope inside a parameter scope, 27990287Sobrien then something is wrong. That corresponds to a declaration 28090287Sobrien like: 28190287Sobrien 28290287Sobrien template <class T> template <> ... 28390287Sobrien 284117410Skan which is always invalid since [temp.expl.spec] forbids the 28590287Sobrien specialization of a class member template if the enclosing 28690287Sobrien class templates are not explicitly specialized as well. */ 287132747Skan if (b->explicit_spec_p) 28890287Sobrien { 28990287Sobrien if (n_template_parm_scopes == 0) 29090287Sobrien innermost_specialization_p = 1; 29190287Sobrien else 29290287Sobrien seen_specialization_p = 1; 29390287Sobrien } 29490287Sobrien else if (seen_specialization_p == 1) 29590287Sobrien return tsk_invalid_member_spec; 29690287Sobrien 29790287Sobrien ++n_template_parm_scopes; 29890287Sobrien } 29990287Sobrien 30090287Sobrien /* Handle explicit instantiations. */ 30190287Sobrien if (processing_explicit_instantiation) 30290287Sobrien { 30390287Sobrien if (n_template_parm_scopes != 0) 30490287Sobrien /* We've seen a template parameter list during an explicit 30590287Sobrien instantiation. For example: 30690287Sobrien 30790287Sobrien template <class T> template void f(int); 30890287Sobrien 30990287Sobrien This is erroneous. */ 31090287Sobrien return tsk_invalid_expl_inst; 31190287Sobrien else 31290287Sobrien return tsk_expl_inst; 31390287Sobrien } 31490287Sobrien 31590287Sobrien if (n_template_parm_scopes < n_class_scopes) 31690287Sobrien /* We've not seen enough template headers to match all the 31790287Sobrien specialized classes present. For example: 31890287Sobrien 31990287Sobrien template <class T> void R<T>::S<T>::f(int); 32090287Sobrien 321117410Skan This is invalid; there needs to be one set of template 32290287Sobrien parameters for each class. */ 32390287Sobrien return tsk_insufficient_parms; 32490287Sobrien else if (n_template_parm_scopes == n_class_scopes) 32590287Sobrien /* We're processing a non-template declaration (even though it may 32690287Sobrien be a member of a template class.) For example: 32790287Sobrien 32890287Sobrien template <class T> void S<T>::f(int); 32990287Sobrien 33090287Sobrien The `class T' maches the `S<T>', leaving no template headers 33190287Sobrien corresponding to the `f'. */ 33290287Sobrien return tsk_none; 33390287Sobrien else if (n_template_parm_scopes > n_class_scopes + 1) 33490287Sobrien /* We've got too many template headers. For example: 33590287Sobrien 33690287Sobrien template <> template <class T> void f (T); 33790287Sobrien 33890287Sobrien There need to be more enclosing classes. */ 33990287Sobrien return tsk_excessive_parms; 34090287Sobrien else 34190287Sobrien /* This must be a template. It's of the form: 34290287Sobrien 34390287Sobrien template <class T> template <class U> void S<T>::f(U); 34490287Sobrien 34590287Sobrien This is a specialization if the innermost level was a 34690287Sobrien specialization; otherwise it's just a definition of the 34790287Sobrien template. */ 34890287Sobrien return innermost_specialization_p ? tsk_expl_spec : tsk_template; 34918334Speter} 35018334Speter 35190287Sobrien/* Exit the current scope. */ 35290287Sobrien 35390287Sobrienvoid 354132747Skanfinish_scope (void) 35590287Sobrien{ 35690287Sobrien poplevel (0, 0, 0); 35790287Sobrien} 35890287Sobrien 35990287Sobrien/* When a label goes out of scope, check to see if that label was used 36090287Sobrien in a valid manner, and issue any appropriate warnings or errors. */ 36190287Sobrien 36290287Sobrienstatic void 363132747Skanpop_label (tree label, tree old_value) 36490287Sobrien{ 365132747Skan if (!processing_template_decl) 36690287Sobrien { 36790287Sobrien if (DECL_INITIAL (label) == NULL_TREE) 36890287Sobrien { 369132747Skan location_t location; 370132747Skan 37190287Sobrien cp_error_at ("label `%D' used but not defined", label); 372132747Skan location.file = input_filename; 373132747Skan location.line = 0; 37490287Sobrien /* Avoid crashing later. */ 375132747Skan define_label (location, DECL_NAME (label)); 37690287Sobrien } 37790287Sobrien else if (warn_unused_label && !TREE_USED (label)) 37890287Sobrien cp_warning_at ("label `%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 38890287Sobrienstatic void 389132747Skanpop_labels (tree block) 39090287Sobrien{ 39190287Sobrien struct named_label_list *link; 39290287Sobrien 39390287Sobrien /* Clear out the definitions of all label names, since their scopes 39490287Sobrien end here. */ 39590287Sobrien for (link = named_labels; link; link = link->next) 39690287Sobrien { 39790287Sobrien pop_label (link->label_decl, link->old_value); 39890287Sobrien /* Put the labels into the "variables" of the top-level block, 39990287Sobrien so debugger can see them. */ 40090287Sobrien TREE_CHAIN (link->label_decl) = BLOCK_VARS (block); 40190287Sobrien BLOCK_VARS (block) = link->label_decl; 40290287Sobrien } 40390287Sobrien 40490287Sobrien named_labels = NULL; 40590287Sobrien} 40690287Sobrien 40718334Speter/* Exit a binding level. 40818334Speter Pop the level off, and restore the state of the identifier-decl mappings 40918334Speter that were in effect when this level was entered. 41018334Speter 41118334Speter If KEEP == 1, this level had explicit declarations, so 41218334Speter and create a "block" (a BLOCK node) for the level 41318334Speter to record its declarations and subblocks for symbol table output. 41418334Speter 41518334Speter If FUNCTIONBODY is nonzero, this level is the body of a function, 41618334Speter so create a block as if KEEP were set and also clear out all 41718334Speter label names. 41818334Speter 41918334Speter If REVERSE is nonzero, reverse the order of decls before putting 42018334Speter them into the BLOCK. */ 42118334Speter 42218334Spetertree 423132747Skanpoplevel (int keep, int reverse, int functionbody) 42418334Speter{ 425132747Skan tree link; 42618334Speter /* The chain of decls was accumulated in reverse order. 42718334Speter Put it into forward order, just for cleanliness. */ 42818334Speter tree decls; 42918334Speter int tmp = functionbody; 43090287Sobrien int real_functionbody; 43190287Sobrien tree subblocks; 43218334Speter tree block = NULL_TREE; 43318334Speter tree decl; 43452290Sobrien int leaving_for_scope; 435132747Skan scope_kind kind; 43618334Speter 437117410Skan timevar_push (TV_NAME_LOOKUP); 438117410Skan 439132747Skan my_friendly_assert (current_binding_level->kind != sk_class, 19990916); 44052290Sobrien 441132747Skan real_functionbody = (current_binding_level->kind == sk_cleanup 44290287Sobrien ? ((functionbody = 0), tmp) : functionbody); 44390287Sobrien subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; 44490287Sobrien 44552290Sobrien my_friendly_assert (!current_binding_level->class_shadowed, 44652290Sobrien 19990414); 44752290Sobrien 44852290Sobrien /* We used to use KEEP == 2 to indicate that the new block should go 44952290Sobrien at the beginning of the list of blocks at this binding level, 45052290Sobrien rather than the end. This hack is no longer used. */ 45152290Sobrien my_friendly_assert (keep == 0 || keep == 1, 0); 45252290Sobrien 453132747Skan if (current_binding_level->keep) 45418334Speter keep = 1; 45518334Speter 45690287Sobrien /* Any uses of undefined labels, and any defined labels, now operate 45790287Sobrien under constraints of next binding contour. */ 45890287Sobrien if (cfun && !functionbody) 45990287Sobrien { 460117410Skan struct cp_binding_level *level_chain; 46190287Sobrien level_chain = current_binding_level->level_chain; 46290287Sobrien if (level_chain) 46390287Sobrien { 46490287Sobrien struct named_label_use_list *uses; 46590287Sobrien struct named_label_list *labels; 46690287Sobrien for (labels = named_labels; labels; labels = labels->next) 46790287Sobrien if (labels->binding_level == current_binding_level) 46890287Sobrien { 46990287Sobrien tree decl; 470132747Skan if (current_binding_level->kind == sk_try) 47190287Sobrien labels->in_try_scope = 1; 472132747Skan if (current_binding_level->kind == sk_catch) 47390287Sobrien labels->in_catch_scope = 1; 47490287Sobrien for (decl = labels->names_in_scope; decl; 47590287Sobrien decl = TREE_CHAIN (decl)) 47690287Sobrien if (decl_jump_unsafe (decl)) 47790287Sobrien labels->bad_decls = tree_cons (NULL_TREE, decl, 47890287Sobrien labels->bad_decls); 47990287Sobrien labels->binding_level = level_chain; 48090287Sobrien labels->names_in_scope = level_chain->names; 48190287Sobrien } 48290287Sobrien 48390287Sobrien for (uses = named_label_uses; uses; uses = uses->next) 48490287Sobrien if (uses->binding_level == current_binding_level) 48590287Sobrien { 48690287Sobrien uses->binding_level = level_chain; 48790287Sobrien uses->names_in_scope = level_chain->names; 48890287Sobrien } 48990287Sobrien } 49090287Sobrien } 49190287Sobrien 49218334Speter /* Get the decls in the order they were written. 49318334Speter Usually current_binding_level->names is in reverse order. 49418334Speter But parameter decls were previously put in forward order. */ 49518334Speter 49618334Speter if (reverse) 49718334Speter current_binding_level->names 49818334Speter = decls = nreverse (current_binding_level->names); 49918334Speter else 50018334Speter decls = current_binding_level->names; 50118334Speter 50218334Speter /* Output any nested inline functions within this block 50318334Speter if they weren't already output. */ 50418334Speter for (decl = decls; decl; decl = TREE_CHAIN (decl)) 50518334Speter if (TREE_CODE (decl) == FUNCTION_DECL 50618334Speter && ! TREE_ASM_WRITTEN (decl) 50718334Speter && DECL_INITIAL (decl) != NULL_TREE 50818334Speter && TREE_ADDRESSABLE (decl) 50918334Speter && decl_function_context (decl) == current_function_decl) 51018334Speter { 51118334Speter /* If this decl was copied from a file-scope decl 51218334Speter on account of a block-scope extern decl, 51318334Speter propagate TREE_ADDRESSABLE to the file-scope decl. */ 51418334Speter if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE) 51518334Speter TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1; 51618334Speter else 51718334Speter { 51818334Speter push_function_context (); 51918334Speter output_inline_function (decl); 52018334Speter pop_function_context (); 52118334Speter } 52218334Speter } 52318334Speter 52490287Sobrien /* When not in function-at-a-time mode, expand_end_bindings will 52590287Sobrien warn about unused variables. But, in function-at-a-time mode 52690287Sobrien expand_end_bindings is not passed the list of variables in the 52790287Sobrien current scope, and therefore no warning is emitted. So, we 52890287Sobrien explicitly warn here. */ 52990287Sobrien if (!processing_template_decl) 53090287Sobrien warn_about_unused_variables (getdecls ()); 53190287Sobrien 53218334Speter /* If there were any declarations or structure tags in that level, 53318334Speter or if this level is a function body, 53418334Speter create a BLOCK to record them for the life of this function. */ 53518334Speter block = NULL_TREE; 53690287Sobrien if (keep == 1 || functionbody) 53718334Speter block = make_node (BLOCK); 53818334Speter if (block != NULL_TREE) 53918334Speter { 54090287Sobrien BLOCK_VARS (block) = decls; 54190287Sobrien BLOCK_SUBBLOCKS (block) = subblocks; 54218334Speter } 54318334Speter 54418334Speter /* In each subblock, record that this is its superior. */ 54518334Speter if (keep >= 0) 54618334Speter for (link = subblocks; link; link = TREE_CHAIN (link)) 54718334Speter BLOCK_SUPERCONTEXT (link) = block; 54818334Speter 54952290Sobrien /* We still support the old for-scope rules, whereby the variables 55052290Sobrien in a for-init statement were in scope after the for-statement 551132747Skan ended. We only use the new rules if flag_new_for_scope is 55252290Sobrien nonzero. */ 55390287Sobrien leaving_for_scope 554132747Skan = current_binding_level->kind == sk_for && flag_new_for_scope == 1; 55518334Speter 55652290Sobrien /* Remove declarations for all the DECLs in this level. */ 55752290Sobrien for (link = decls; link; link = TREE_CHAIN (link)) 55818334Speter { 55990287Sobrien if (leaving_for_scope && TREE_CODE (link) == VAR_DECL 56090287Sobrien && DECL_NAME (link)) 56118334Speter { 562117410Skan cxx_binding *outer_binding 563117410Skan = IDENTIFIER_BINDING (DECL_NAME (link))->previous; 56452290Sobrien tree ns_binding; 56552290Sobrien 56652290Sobrien if (!outer_binding) 56752290Sobrien ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link)); 56851412Sobrien else 56952290Sobrien ns_binding = NULL_TREE; 57051412Sobrien 57190287Sobrien if (outer_binding 572132747Skan && outer_binding->scope == current_binding_level->level_chain) 57352290Sobrien /* We have something like: 57490287Sobrien 57552290Sobrien int i; 57652290Sobrien for (int i; ;); 57790287Sobrien 57852290Sobrien and we are leaving the `for' scope. There's no reason to 57952290Sobrien keep the binding of the inner `i' in this case. */ 58052290Sobrien pop_binding (DECL_NAME (link), link); 58190287Sobrien else if ((outer_binding 582132747Skan && (TREE_CODE (outer_binding->value) == TYPE_DECL)) 583132747Skan || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL)) 58452290Sobrien /* Here, we have something like: 58551412Sobrien 58652290Sobrien typedef int I; 58751412Sobrien 58852290Sobrien void f () { 58952290Sobrien for (int I; ;); 59052290Sobrien } 59152290Sobrien 59252290Sobrien We must pop the for-scope binding so we know what's a 59352290Sobrien type and what isn't. */ 59452290Sobrien pop_binding (DECL_NAME (link), link); 59552290Sobrien else 59651412Sobrien { 59752290Sobrien /* Mark this VAR_DECL as dead so that we can tell we left it 59852290Sobrien there only for backward compatibility. */ 59952290Sobrien DECL_DEAD_FOR_LOCAL (link) = 1; 60090287Sobrien 601132747Skan /* Keep track of what should have happened when we 60252290Sobrien popped the binding. */ 603132747Skan if (outer_binding && outer_binding->value) 604132747Skan DECL_SHADOWED_FOR_VAR (link) = outer_binding->value; 60552290Sobrien 60652290Sobrien /* Add it to the list of dead variables in the next 60752290Sobrien outermost binding to that we can remove these when we 60852290Sobrien leave that binding. */ 60952290Sobrien current_binding_level->level_chain->dead_vars_from_for 61052290Sobrien = tree_cons (NULL_TREE, link, 61152290Sobrien current_binding_level->level_chain-> 61252290Sobrien dead_vars_from_for); 61352290Sobrien 614117410Skan /* Although we don't pop the cxx_binding, we do clear 615132747Skan its SCOPE since the scope is going away now. */ 616132747Skan IDENTIFIER_BINDING (DECL_NAME (link))->scope = NULL; 61751412Sobrien } 61851412Sobrien } 61990287Sobrien else 62018334Speter { 62152290Sobrien /* Remove the binding. */ 62252290Sobrien decl = link; 62352290Sobrien if (TREE_CODE (decl) == TREE_LIST) 62452290Sobrien decl = TREE_VALUE (decl); 62590287Sobrien if (DECL_P (decl)) 62652290Sobrien pop_binding (DECL_NAME (decl), decl); 62752290Sobrien else if (TREE_CODE (decl) == OVERLOAD) 62852290Sobrien pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl); 62990287Sobrien else 63090287Sobrien abort (); 63118334Speter } 63252290Sobrien } 63318334Speter 63452290Sobrien /* Remove declarations for any `for' variables from inner scopes 63552290Sobrien that we kept around. */ 63652290Sobrien for (link = current_binding_level->dead_vars_from_for; 63752290Sobrien link; link = TREE_CHAIN (link)) 63852290Sobrien pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link)); 63918334Speter 64052290Sobrien /* Restore the IDENTIFIER_TYPE_VALUEs. */ 64152290Sobrien for (link = current_binding_level->type_shadowed; 64252290Sobrien link; link = TREE_CHAIN (link)) 64352290Sobrien SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); 64490287Sobrien 64590287Sobrien /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */ 64690287Sobrien for (link = current_binding_level->shadowed_labels; 64790287Sobrien link; 64890287Sobrien link = TREE_CHAIN (link)) 64990287Sobrien pop_label (TREE_VALUE (link), TREE_PURPOSE (link)); 65090287Sobrien 65152290Sobrien /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs 65252290Sobrien list if a `using' declaration put them there. The debugging 65352290Sobrien back-ends won't understand OVERLOAD, so we remove them here. 65452290Sobrien Because the BLOCK_VARS are (temporarily) shared with 65552290Sobrien CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have 65652290Sobrien popped all the bindings. */ 65752290Sobrien if (block) 65852290Sobrien { 65952290Sobrien tree* d; 66051412Sobrien 66152290Sobrien for (d = &BLOCK_VARS (block); *d; ) 66218334Speter { 66352290Sobrien if (TREE_CODE (*d) == TREE_LIST) 66452290Sobrien *d = TREE_CHAIN (*d); 66552290Sobrien else 66652290Sobrien d = &TREE_CHAIN (*d); 66718334Speter } 66818334Speter } 66918334Speter 67018334Speter /* If the level being exited is the top level of a function, 67118334Speter check over all the labels. */ 67218334Speter if (functionbody) 67318334Speter { 67490287Sobrien /* Since this is the top level block of a function, the vars are 67590287Sobrien the function's parameters. Don't leave them in the BLOCK 67690287Sobrien because they are found in the FUNCTION_DECL instead. */ 67718334Speter BLOCK_VARS (block) = 0; 67890287Sobrien pop_labels (block); 67918334Speter } 68018334Speter 681132747Skan kind = current_binding_level->kind; 68218334Speter 683132747Skan leave_scope (); 68418334Speter if (functionbody) 68518334Speter DECL_INITIAL (current_function_decl) = block; 68618334Speter else if (block) 68790287Sobrien current_binding_level->blocks 68890287Sobrien = chainon (current_binding_level->blocks, block); 68990287Sobrien 69018334Speter /* If we did not make a block for the level just exited, 69118334Speter any blocks made for inner levels 69218334Speter (since they cannot be recorded as subblocks in that level) 69318334Speter must be carried forward so they will later become subblocks 69418334Speter of something else. */ 69518334Speter else if (subblocks) 69652290Sobrien current_binding_level->blocks 69752290Sobrien = chainon (current_binding_level->blocks, subblocks); 69818334Speter 69990287Sobrien /* Each and every BLOCK node created here in `poplevel' is important 70090287Sobrien (e.g. for proper debugging information) so if we created one 70190287Sobrien earlier, mark it as "used". */ 70290287Sobrien if (block) 70390287Sobrien TREE_USED (block) = 1; 70490287Sobrien 70518334Speter /* Take care of compiler's internal binding structures. */ 706132747Skan if (kind == sk_cleanup) 70718334Speter { 70890287Sobrien tree scope_stmts; 70990287Sobrien 71090287Sobrien scope_stmts 71190287Sobrien = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1); 71218334Speter if (block) 71390287Sobrien { 71490287Sobrien SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block; 71590287Sobrien SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block; 71690287Sobrien } 71790287Sobrien 71890287Sobrien block = poplevel (keep, reverse, functionbody); 71918334Speter } 72018334Speter 721117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block); 72218334Speter} 72318334Speter 72418334Speter/* Delete the node BLOCK from the current binding level. 72518334Speter This is used for the block inside a stmt expr ({...}) 72618334Speter so that the block can be reinserted where appropriate. */ 72718334Speter 72818334Spetervoid 729132747Skandelete_block (tree block) 73018334Speter{ 73118334Speter tree t; 73218334Speter if (current_binding_level->blocks == block) 73318334Speter current_binding_level->blocks = TREE_CHAIN (block); 73418334Speter for (t = current_binding_level->blocks; t;) 73518334Speter { 73618334Speter if (TREE_CHAIN (t) == block) 73718334Speter TREE_CHAIN (t) = TREE_CHAIN (block); 73818334Speter else 73918334Speter t = TREE_CHAIN (t); 74018334Speter } 74118334Speter TREE_CHAIN (block) = NULL_TREE; 74218334Speter /* Clear TREE_USED which is always set by poplevel. 74318334Speter The flag is set again if insert_block is called. */ 74418334Speter TREE_USED (block) = 0; 74518334Speter} 74618334Speter 74718334Speter/* Insert BLOCK at the end of the list of subblocks of the 74818334Speter current binding level. This is used when a BIND_EXPR is expanded, 74918334Speter to handle the BLOCK node inside the BIND_EXPR. */ 75018334Speter 75118334Spetervoid 752132747Skaninsert_block (tree block) 75318334Speter{ 75418334Speter TREE_USED (block) = 1; 75518334Speter current_binding_level->blocks 75618334Speter = chainon (current_binding_level->blocks, block); 75718334Speter} 75818334Speter 75918334Speter/* Set the BLOCK node for the innermost scope 76018334Speter (the one we are currently in). */ 76118334Speter 76218334Spetervoid 763132747Skanset_block (tree block ATTRIBUTE_UNUSED ) 76418334Speter{ 76590287Sobrien /* The RTL expansion machinery requires us to provide this callback, 76690287Sobrien but it is not applicable in function-at-a-time mode. */ 76718334Speter} 76818334Speter 769117410Skan/* Returns nonzero if T is a virtual function table. */ 77052290Sobrien 77152290Sobrienint 772132747Skanvtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED ) 77352290Sobrien{ 77452290Sobrien return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t)); 77552290Sobrien} 77652290Sobrien 777117410Skan/* Returns nonzero if T is a TYPE_DECL for a type with virtual 77852290Sobrien functions. */ 77952290Sobrien 78052290Sobrienint 781132747Skanvtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED ) 78252290Sobrien{ 78352290Sobrien return (TREE_CODE (t) == TYPE_DECL 78490287Sobrien && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE 78590287Sobrien && TYPE_POLYMORPHIC_P (TREE_TYPE (t))); 78652290Sobrien} 78752290Sobrien 788117410Skanstruct walk_globals_data { 789117410Skan walk_globals_pred p; 790117410Skan walk_globals_fn f; 791117410Skan void *data; 792117410Skan}; 793117410Skan 794117410Skan/* Walk the vtable declarations in NAMESPACE. Whenever one is found 795117410Skan for which P returns nonzero, call F with its address. If any call 796117410Skan to F returns a nonzero value, return a nonzero value. */ 797117410Skan 798117410Skanstatic int 799132747Skanwalk_vtables_r (tree namespace, void* data) 800117410Skan{ 801117410Skan struct walk_globals_data* wgd = (struct walk_globals_data *) data; 802117410Skan walk_globals_fn f = wgd->f; 803117410Skan void *d = wgd->data; 804117410Skan tree decl = NAMESPACE_LEVEL (namespace)->vtables; 805117410Skan int result = 0; 806117410Skan 807117410Skan for (; decl ; decl = TREE_CHAIN (decl)) 808117410Skan result |= (*f) (&decl, d); 809117410Skan 810117410Skan return result; 811117410Skan} 812117410Skan 813117410Skan/* Walk the vtable declarations. Whenever one is found for which P 814117410Skan returns nonzero, call F with its address. If any call to F 815117410Skan returns a nonzero value, return a nonzero value. */ 816132747Skanbool 817132747Skanwalk_vtables (walk_globals_pred p, walk_globals_fn f, void *data) 818117410Skan{ 819117410Skan struct walk_globals_data wgd; 820117410Skan wgd.p = p; 821117410Skan wgd.f = f; 822117410Skan wgd.data = data; 823117410Skan 824117410Skan return walk_namespaces (walk_vtables_r, &wgd); 825117410Skan} 826117410Skan 82752290Sobrien/* Walk all the namespaces contained NAMESPACE, including NAMESPACE 82852290Sobrien itself, calling F for each. The DATA is passed to F as well. */ 82952290Sobrien 83052290Sobrienstatic int 831132747Skanwalk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data) 83252290Sobrien{ 83352290Sobrien int result = 0; 834117410Skan tree current = NAMESPACE_LEVEL (namespace)->namespaces; 83552290Sobrien 83652290Sobrien result |= (*f) (namespace, data); 83752290Sobrien 838117410Skan for (; current; current = TREE_CHAIN (current)) 839117410Skan result |= walk_namespaces_r (current, f, data); 84052290Sobrien 84152290Sobrien return result; 84252290Sobrien} 84352290Sobrien 84452290Sobrien/* Walk all the namespaces, calling F for each. The DATA is passed to 84552290Sobrien F as well. */ 84652290Sobrien 84752290Sobrienint 848132747Skanwalk_namespaces (walk_namespaces_fn f, void* data) 84952290Sobrien{ 85052290Sobrien return walk_namespaces_r (global_namespace, f, data); 85152290Sobrien} 85252290Sobrien 85352290Sobrien/* Walk the global declarations in NAMESPACE. Whenever one is found 854117410Skan for which P returns nonzero, call F with its address. If any call 855117410Skan to F returns a nonzero value, return a nonzero value. */ 85652290Sobrien 85790287Sobrienstatic int 858132747Skanwalk_globals_r (tree namespace, void* data) 85952290Sobrien{ 86052290Sobrien struct walk_globals_data* wgd = (struct walk_globals_data *) data; 86152290Sobrien walk_globals_pred p = wgd->p; 86252290Sobrien walk_globals_fn f = wgd->f; 86352290Sobrien void *d = wgd->data; 86452290Sobrien tree *t; 86552290Sobrien int result = 0; 86652290Sobrien 86752290Sobrien t = &NAMESPACE_LEVEL (namespace)->names; 86852290Sobrien 86952290Sobrien while (*t) 87052290Sobrien { 87152290Sobrien tree glbl = *t; 87252290Sobrien 87352290Sobrien if ((*p) (glbl, d)) 87452290Sobrien result |= (*f) (t, d); 87552290Sobrien 87652290Sobrien /* If F changed *T, then *T still points at the next item to 87752290Sobrien examine. */ 87852290Sobrien if (*t == glbl) 87952290Sobrien t = &TREE_CHAIN (*t); 88052290Sobrien } 88152290Sobrien 88252290Sobrien return result; 88352290Sobrien} 88452290Sobrien 88552290Sobrien/* Walk the global declarations. Whenever one is found for which P 886132747Skan returns true, call F with its address. If any call to F 887132747Skan returns true, return true. */ 88852290Sobrien 889132747Skanbool 890132747Skanwalk_globals (walk_globals_pred p, walk_globals_fn f, void *data) 89152290Sobrien{ 89252290Sobrien struct walk_globals_data wgd; 89352290Sobrien wgd.p = p; 89452290Sobrien wgd.f = f; 89552290Sobrien wgd.data = data; 89652290Sobrien 89752290Sobrien return walk_namespaces (walk_globals_r, &wgd); 89852290Sobrien} 89952290Sobrien 90052290Sobrien/* Call wrapup_globals_declarations for the globals in NAMESPACE. If 90152290Sobrien DATA is non-NULL, this is the last time we will call 90252290Sobrien wrapup_global_declarations for this NAMESPACE. */ 90352290Sobrien 90452290Sobrienint 905132747Skanwrapup_globals_for_namespace (tree namespace, void* data) 90652290Sobrien{ 907117410Skan struct cp_binding_level *level = NAMESPACE_LEVEL (namespace); 908117410Skan varray_type statics = level->static_decls; 909117410Skan tree *vec = &VARRAY_TREE (statics, 0); 910117410Skan int len = VARRAY_ACTIVE_SIZE (statics); 91152290Sobrien int last_time = (data != 0); 91252290Sobrien 91352290Sobrien if (last_time) 91452290Sobrien { 91552290Sobrien check_global_declarations (vec, len); 91652290Sobrien return 0; 91752290Sobrien } 91852290Sobrien 91952290Sobrien /* Write out any globals that need to be output. */ 920117410Skan return wrapup_global_declarations (vec, len); 92152290Sobrien} 92252290Sobrien 92318334Speter 92490287Sobrien/* In C++, you don't have to write `struct S' to refer to `S'; you 92590287Sobrien can just use `S'. We accomplish this by creating a TYPE_DECL as 92690287Sobrien if the user had written `typedef struct S S'. Create and return 92790287Sobrien the TYPE_DECL for TYPE. */ 92890287Sobrien 92990287Sobrientree 930132747Skancreate_implicit_typedef (tree name, tree type) 93190287Sobrien{ 93290287Sobrien tree decl; 93390287Sobrien 93490287Sobrien decl = build_decl (TYPE_DECL, name, type); 93590287Sobrien DECL_ARTIFICIAL (decl) = 1; 93690287Sobrien /* There are other implicit type declarations, like the one *within* 93790287Sobrien a class that allows you to write `S::S'. We must distinguish 93890287Sobrien amongst these. */ 93990287Sobrien SET_DECL_IMPLICIT_TYPEDEF_P (decl); 94090287Sobrien TYPE_NAME (type) = decl; 94190287Sobrien 94290287Sobrien return decl; 94390287Sobrien} 94490287Sobrien 94590287Sobrien/* Remember a local name for name-mangling purposes. */ 94690287Sobrien 94790287Sobrienstatic void 948132747Skanpush_local_name (tree decl) 94990287Sobrien{ 95090287Sobrien size_t i, nelts; 95190287Sobrien tree t, name; 95290287Sobrien 953117410Skan timevar_push (TV_NAME_LOOKUP); 95490287Sobrien if (!local_names) 95590287Sobrien VARRAY_TREE_INIT (local_names, 8, "local_names"); 95690287Sobrien 95790287Sobrien name = DECL_NAME (decl); 95890287Sobrien 95990287Sobrien nelts = VARRAY_ACTIVE_SIZE (local_names); 96090287Sobrien for (i = 0; i < nelts; i++) 96190287Sobrien { 96290287Sobrien t = VARRAY_TREE (local_names, i); 96390287Sobrien if (DECL_NAME (t) == name) 96490287Sobrien { 96590287Sobrien if (!DECL_LANG_SPECIFIC (decl)) 96690287Sobrien retrofit_lang_decl (decl); 967117410Skan DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1; 96890287Sobrien if (DECL_LANG_SPECIFIC (t)) 96990287Sobrien DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1; 97090287Sobrien else 97190287Sobrien DECL_DISCRIMINATOR (decl) = 1; 97290287Sobrien 97390287Sobrien VARRAY_TREE (local_names, i) = decl; 974132747Skan timevar_pop (TV_NAME_LOOKUP); 975132747Skan return; 97690287Sobrien } 97790287Sobrien } 97890287Sobrien 97990287Sobrien VARRAY_PUSH_TREE (local_names, decl); 980117410Skan timevar_pop (TV_NAME_LOOKUP); 98190287Sobrien} 98218334Speter 98318334Speter/* Subroutine of duplicate_decls: return truthvalue of whether 98418334Speter or not types of these decls match. 98518334Speter 98618334Speter For C++, we must compare the parameter list so that `int' can match 98718334Speter `int&' in a parameter position, but `int&' is not confused with 98818334Speter `const int&'. */ 98951412Sobrien 99018334Speterint 991132747Skandecls_match (tree newdecl, tree olddecl) 99218334Speter{ 99318334Speter int types_match; 99418334Speter 99552290Sobrien if (newdecl == olddecl) 99652290Sobrien return 1; 99752290Sobrien 99852290Sobrien if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) 99952290Sobrien /* If the two DECLs are not even the same kind of thing, we're not 100052290Sobrien interested in their types. */ 100152290Sobrien return 0; 100252290Sobrien 100352290Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 100418334Speter { 100518334Speter tree f1 = TREE_TYPE (newdecl); 100618334Speter tree f2 = TREE_TYPE (olddecl); 100718334Speter tree p1 = TYPE_ARG_TYPES (f1); 100818334Speter tree p2 = TYPE_ARG_TYPES (f2); 100918334Speter 101090287Sobrien if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 101190287Sobrien && ! (DECL_EXTERN_C_P (newdecl) 101290287Sobrien && DECL_EXTERN_C_P (olddecl))) 101351412Sobrien return 0; 101451412Sobrien 101518334Speter if (TREE_CODE (f1) != TREE_CODE (f2)) 101690287Sobrien return 0; 101718334Speter 101852290Sobrien if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) 101918334Speter { 102090287Sobrien if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl) 102190287Sobrien && (DECL_BUILT_IN (olddecl) 102290287Sobrien#ifndef NO_IMPLICIT_EXTERN_C 102390287Sobrien || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl)) 102490287Sobrien || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl)) 102590287Sobrien#endif 102690287Sobrien )) 102718334Speter { 102818334Speter types_match = self_promoting_args_p (p1); 102918334Speter if (p1 == void_list_node) 103018334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 103118334Speter } 103290287Sobrien#ifndef NO_IMPLICIT_EXTERN_C 103390287Sobrien else if (p1 == NULL_TREE 103490287Sobrien && (DECL_EXTERN_C_P (olddecl) 103590287Sobrien && DECL_IN_SYSTEM_HEADER (olddecl) 103690287Sobrien && !DECL_CLASS_SCOPE_P (olddecl)) 103790287Sobrien && (DECL_EXTERN_C_P (newdecl) 103890287Sobrien && DECL_IN_SYSTEM_HEADER (newdecl) 103990287Sobrien && !DECL_CLASS_SCOPE_P (newdecl))) 104018334Speter { 104118334Speter types_match = self_promoting_args_p (p2); 104218334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 104318334Speter } 104490287Sobrien#endif 104518334Speter else 104652290Sobrien types_match = compparms (p1, p2); 104718334Speter } 104818334Speter else 104918334Speter types_match = 0; 105018334Speter } 105152290Sobrien else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 105218334Speter { 1053132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) 1054132747Skan != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))) 1055132747Skan return 0; 1056132747Skan 105751412Sobrien if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), 105851412Sobrien DECL_TEMPLATE_PARMS (olddecl))) 105951412Sobrien return 0; 106090287Sobrien 106151412Sobrien if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 1062132747Skan types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)), 1063132747Skan TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))); 106451412Sobrien else 106551412Sobrien types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl), 106651412Sobrien DECL_TEMPLATE_RESULT (newdecl)); 106718334Speter } 106818334Speter else 106918334Speter { 107018334Speter if (TREE_TYPE (newdecl) == error_mark_node) 107118334Speter types_match = TREE_TYPE (olddecl) == error_mark_node; 107218334Speter else if (TREE_TYPE (olddecl) == NULL_TREE) 107318334Speter types_match = TREE_TYPE (newdecl) == NULL_TREE; 107418334Speter else if (TREE_TYPE (newdecl) == NULL_TREE) 107518334Speter types_match = 0; 107618334Speter else 107752290Sobrien types_match = comptypes (TREE_TYPE (newdecl), 107852290Sobrien TREE_TYPE (olddecl), 107952290Sobrien COMPARE_REDECLARATION); 108018334Speter } 108118334Speter 108218334Speter return types_match; 108318334Speter} 108418334Speter 108518334Speter/* If NEWDECL is `static' and an `extern' was seen previously, 108690287Sobrien warn about it. OLDDECL is the previous declaration. 108718334Speter 108818334Speter Note that this does not apply to the C++ case of declaring 108918334Speter a variable `extern const' and then later `const'. 109018334Speter 109118334Speter Don't complain about built-in functions, since they are beyond 109218334Speter the user's control. */ 109318334Speter 1094132747Skanvoid 1095132747Skanwarn_extern_redeclared_static (tree newdecl, tree olddecl) 109618334Speter{ 109790287Sobrien static const char *const explicit_extern_static_warning 109818334Speter = "`%D' was declared `extern' and later `static'"; 109990287Sobrien static const char *const implicit_extern_static_warning 110018334Speter = "`%D' was declared implicitly `extern' and later `static'"; 110118334Speter 110290287Sobrien tree name; 110390287Sobrien 110490287Sobrien if (TREE_CODE (newdecl) == TYPE_DECL 110590287Sobrien || TREE_CODE (newdecl) == TEMPLATE_DECL 1106132747Skan || TREE_CODE (newdecl) == CONST_DECL 1107132747Skan || TREE_CODE (newdecl) == NAMESPACE_DECL) 110818334Speter return; 110918334Speter 111090287Sobrien /* Don't get confused by static member functions; that's a different 111190287Sobrien use of `static'. */ 111290287Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL 111390287Sobrien && DECL_STATIC_FUNCTION_P (newdecl)) 111490287Sobrien return; 111590287Sobrien 111690287Sobrien /* If the old declaration was `static', or the new one isn't, then 111790287Sobrien then everything is OK. */ 111890287Sobrien if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) 111990287Sobrien return; 112090287Sobrien 112190287Sobrien /* It's OK to declare a builtin function as `static'. */ 112290287Sobrien if (TREE_CODE (olddecl) == FUNCTION_DECL 112390287Sobrien && DECL_ARTIFICIAL (olddecl)) 112490287Sobrien return; 112590287Sobrien 112618334Speter name = DECL_ASSEMBLER_NAME (newdecl); 112790287Sobrien pedwarn (IDENTIFIER_IMPLICIT_DECL (name) 112890287Sobrien ? implicit_extern_static_warning 112990287Sobrien : explicit_extern_static_warning, newdecl); 113090287Sobrien cp_pedwarn_at ("previous declaration of `%D'", olddecl); 113118334Speter} 113218334Speter 1133132747Skan/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations. 1134132747Skan If the redeclaration is invalid, a diagnostic is issued, and the 1135132747Skan error_mark_node is returned. Otherwise, OLDDECL is returned. 113618334Speter 1137132747Skan If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is 1138132747Skan returned. */ 113918334Speter 1140132747Skantree 1141132747Skanduplicate_decls (tree newdecl, tree olddecl) 114218334Speter{ 114318334Speter unsigned olddecl_uid = DECL_UID (olddecl); 114418334Speter int olddecl_friend = 0, types_match = 0; 114551412Sobrien int new_defines_function = 0; 114618334Speter 114751412Sobrien if (newdecl == olddecl) 1148132747Skan return olddecl; 114918334Speter 115018334Speter types_match = decls_match (newdecl, olddecl); 115118334Speter 115218334Speter /* If either the type of the new decl or the type of the old decl is an 115318334Speter error_mark_node, then that implies that we have already issued an 115418334Speter error (earlier) for some bogus type specification, and in that case, 115518334Speter it is rather pointless to harass the user with yet more error message 115651412Sobrien about the same declaration, so just pretend the types match here. */ 115751412Sobrien if (TREE_TYPE (newdecl) == error_mark_node 115851412Sobrien || TREE_TYPE (olddecl) == error_mark_node) 115918334Speter types_match = 1; 116090287Sobrien 116190287Sobrien if (DECL_P (olddecl) 116290287Sobrien && TREE_CODE (newdecl) == FUNCTION_DECL 116390287Sobrien && TREE_CODE (olddecl) == FUNCTION_DECL 116490287Sobrien && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl))) 116590287Sobrien { 116690287Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 116790287Sobrien && DECL_UNINLINABLE (newdecl) 116890287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 116990287Sobrien /* Already warned elsewhere. */; 117090287Sobrien else if (DECL_DECLARED_INLINE_P (olddecl) 117190287Sobrien && DECL_UNINLINABLE (olddecl) 117290287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 117390287Sobrien /* Already warned. */; 117490287Sobrien else if (DECL_DECLARED_INLINE_P (newdecl) 117590287Sobrien && DECL_UNINLINABLE (olddecl) 117690287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) 117790287Sobrien { 1178132747Skan warning ("%Jfunction '%D' redeclared as inline", newdecl, newdecl); 1179132747Skan warning ("%Jprevious declaration of '%D' with attribute noinline", 1180132747Skan olddecl, olddecl); 118190287Sobrien } 118290287Sobrien else if (DECL_DECLARED_INLINE_P (olddecl) 118390287Sobrien && DECL_UNINLINABLE (newdecl) 118490287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) 118590287Sobrien { 1186132747Skan warning ("%Jfunction '%D' redeclared with attribute noinline", 1187132747Skan newdecl, newdecl); 1188132747Skan warning ("%Jprevious declaration of '%D' was inline", 1189132747Skan olddecl, olddecl); 119090287Sobrien } 119190287Sobrien } 119290287Sobrien 1193117410Skan /* Check for redeclaration and other discrepancies. */ 119451412Sobrien if (TREE_CODE (olddecl) == FUNCTION_DECL 119590287Sobrien && DECL_ARTIFICIAL (olddecl)) 119618334Speter { 119790287Sobrien if (TREE_CODE (newdecl) != FUNCTION_DECL) 119818334Speter { 119996292Sobrien /* Avoid warnings redeclaring anticipated built-ins. */ 120096292Sobrien if (DECL_ANTICIPATED (olddecl)) 1201132747Skan return NULL_TREE; 120296292Sobrien 120390287Sobrien /* If you declare a built-in or predefined function name as static, 120490287Sobrien the old definition is overridden, but optionally warn this was a 120590287Sobrien bad choice of name. */ 120690287Sobrien if (! TREE_PUBLIC (newdecl)) 120790287Sobrien { 120890287Sobrien if (warn_shadow) 120990287Sobrien warning ("shadowing %s function `%#D'", 121090287Sobrien DECL_BUILT_IN (olddecl) ? "built-in" : "library", 121190287Sobrien olddecl); 121290287Sobrien /* Discard the old built-in function. */ 1213132747Skan return NULL_TREE; 121490287Sobrien } 121590287Sobrien /* If the built-in is not ansi, then programs can override 121690287Sobrien it even globally without an error. */ 121790287Sobrien else if (! DECL_BUILT_IN (olddecl)) 121890287Sobrien warning ("library function `%#D' redeclared as non-function `%#D'", 121990287Sobrien olddecl, newdecl); 122090287Sobrien else 122190287Sobrien { 122290287Sobrien error ("declaration of `%#D'", newdecl); 122390287Sobrien error ("conflicts with built-in declaration `%#D'", 122418334Speter olddecl); 122590287Sobrien } 1226132747Skan return NULL_TREE; 122718334Speter } 122890287Sobrien else if (!types_match) 122918334Speter { 123096292Sobrien /* Avoid warnings redeclaring anticipated built-ins. */ 123196292Sobrien if (DECL_ANTICIPATED (olddecl)) 123296292Sobrien ; /* Do nothing yet. */ 123396292Sobrien else if ((DECL_EXTERN_C_P (newdecl) 1234122192Skan && DECL_EXTERN_C_P (olddecl)) 1235122192Skan || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1236122192Skan TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 123718334Speter { 123890287Sobrien /* A near match; override the builtin. */ 123990287Sobrien 124090287Sobrien if (TREE_PUBLIC (newdecl)) 124118334Speter { 124290287Sobrien warning ("new declaration `%#D'", newdecl); 124390287Sobrien warning ("ambiguates built-in declaration `%#D'", 124490287Sobrien olddecl); 124590287Sobrien } 124690287Sobrien else if (warn_shadow) 124790287Sobrien warning ("shadowing %s function `%#D'", 124890287Sobrien DECL_BUILT_IN (olddecl) ? "built-in" : "library", 124918334Speter olddecl); 125018334Speter } 125190287Sobrien else 125290287Sobrien /* Discard the old built-in function. */ 1253132747Skan return NULL_TREE; 125496292Sobrien 125596292Sobrien /* Replace the old RTL to avoid problems with inlining. */ 125696292Sobrien SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); 125790287Sobrien } 1258117410Skan /* Even if the types match, prefer the new declarations type 1259132747Skan for anticipated built-ins, for exception lists, etc... */ 1260117410Skan else if (DECL_ANTICIPATED (olddecl)) 1261132747Skan { 1262132747Skan tree type = TREE_TYPE (newdecl); 1263132747Skan tree attribs = (*targetm.merge_type_attributes) 1264132747Skan (TREE_TYPE (olddecl), type); 126518334Speter 1266132747Skan type = cp_build_type_attribute_variant (type, attribs); 1267132747Skan TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type; 1268132747Skan } 1269132747Skan 1270122192Skan /* Whether or not the builtin can throw exceptions has no 1271122192Skan bearing on this declarator. */ 1272122192Skan TREE_NOTHROW (olddecl) = 0; 1273122192Skan 127490287Sobrien if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) 127590287Sobrien { 127690287Sobrien /* If a builtin function is redeclared as `static', merge 127790287Sobrien the declarations, but make the original one static. */ 127890287Sobrien DECL_THIS_STATIC (olddecl) = 1; 127990287Sobrien TREE_PUBLIC (olddecl) = 0; 128090287Sobrien 128190287Sobrien /* Make the old declaration consistent with the new one so 128290287Sobrien that all remnants of the builtin-ness of this function 128390287Sobrien will be banished. */ 128490287Sobrien SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 128590287Sobrien SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); 128618334Speter } 128718334Speter } 128818334Speter else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 128918334Speter { 129051412Sobrien if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl) 129151412Sobrien && TREE_CODE (newdecl) != TYPE_DECL 129251412Sobrien && ! (TREE_CODE (newdecl) == TEMPLATE_DECL 129351412Sobrien && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)) 129451412Sobrien || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl) 129551412Sobrien && TREE_CODE (olddecl) != TYPE_DECL 129651412Sobrien && ! (TREE_CODE (olddecl) == TEMPLATE_DECL 129751412Sobrien && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) 129851412Sobrien == TYPE_DECL)))) 129951412Sobrien { 130051412Sobrien /* We do nothing special here, because C++ does such nasty 130151412Sobrien things with TYPE_DECLs. Instead, just let the TYPE_DECL 130251412Sobrien get shadowed, and know that if we need to find a TYPE_DECL 130351412Sobrien for a given name, we can look in the IDENTIFIER_TYPE_VALUE 130451412Sobrien slot of the identifier. */ 1305132747Skan return NULL_TREE; 130651412Sobrien } 130751412Sobrien 130818334Speter if ((TREE_CODE (newdecl) == FUNCTION_DECL 130951412Sobrien && DECL_FUNCTION_TEMPLATE_P (olddecl)) 131018334Speter || (TREE_CODE (olddecl) == FUNCTION_DECL 131151412Sobrien && DECL_FUNCTION_TEMPLATE_P (newdecl))) 1312132747Skan return NULL_TREE; 131351412Sobrien 131490287Sobrien error ("`%#D' redeclared as different kind of symbol", newdecl); 131518334Speter if (TREE_CODE (olddecl) == TREE_LIST) 131618334Speter olddecl = TREE_VALUE (olddecl); 131718334Speter cp_error_at ("previous declaration of `%#D'", olddecl); 131818334Speter 131918334Speter /* New decl is completely inconsistent with the old one => 132018334Speter tell caller to replace the old one. */ 132118334Speter 1322132747Skan return NULL_TREE; 132318334Speter } 132418334Speter else if (!types_match) 132518334Speter { 132690287Sobrien if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) 132752290Sobrien /* These are certainly not duplicate declarations; they're 132852290Sobrien from different scopes. */ 1329132747Skan return NULL_TREE; 133052290Sobrien 133118334Speter if (TREE_CODE (newdecl) == TEMPLATE_DECL) 133218334Speter { 133318334Speter /* The name of a class template may not be declared to refer to 133418334Speter any other template, class, function, object, namespace, value, 133551412Sobrien or type in the same scope. */ 133651412Sobrien if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL 133751412Sobrien || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 133818334Speter { 133990287Sobrien error ("declaration of template `%#D'", newdecl); 134018334Speter cp_error_at ("conflicts with previous declaration `%#D'", 134118334Speter olddecl); 134218334Speter } 134351412Sobrien else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL 134451412Sobrien && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL 134551412Sobrien && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))), 134652290Sobrien TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)))) 134751412Sobrien && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), 1348117410Skan DECL_TEMPLATE_PARMS (olddecl)) 1349117410Skan /* Template functions can be disambiguated by 1350117410Skan return type. */ 1351117410Skan && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)), 1352117410Skan TREE_TYPE (TREE_TYPE (olddecl)))) 135351412Sobrien { 135490287Sobrien error ("new declaration `%#D'", newdecl); 135551412Sobrien cp_error_at ("ambiguates old declaration `%#D'", olddecl); 135651412Sobrien } 1357132747Skan return NULL_TREE; 135818334Speter } 135918334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 136018334Speter { 136190287Sobrien if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) 136218334Speter { 136390287Sobrien error ("declaration of C function `%#D' conflicts with", 136418334Speter newdecl); 136518334Speter cp_error_at ("previous declaration `%#D' here", olddecl); 136618334Speter } 136718334Speter else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 136852290Sobrien TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 136918334Speter { 137090287Sobrien error ("new declaration `%#D'", newdecl); 137118334Speter cp_error_at ("ambiguates old declaration `%#D'", olddecl); 137218334Speter } 137318334Speter else 1374132747Skan return NULL_TREE; 137518334Speter } 137618334Speter 137718334Speter /* Already complained about this, so don't do so again. */ 137818334Speter else if (current_class_type == NULL_TREE 1379132747Skan || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type) 138018334Speter { 1381132747Skan error ("conflicting declaration '%#D'", newdecl); 1382132747Skan cp_error_at ("'%D' has a previous declaration as `%#D'", 1383132747Skan olddecl, olddecl); 1384132747Skan return NULL_TREE; 138518334Speter } 138618334Speter } 138790287Sobrien else if (TREE_CODE (newdecl) == FUNCTION_DECL 138851412Sobrien && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) 138951412Sobrien && (!DECL_TEMPLATE_INFO (newdecl) 139090287Sobrien || (DECL_TI_TEMPLATE (newdecl) 139151412Sobrien != DECL_TI_TEMPLATE (olddecl)))) 139251412Sobrien || (DECL_TEMPLATE_SPECIALIZATION (newdecl) 139351412Sobrien && (!DECL_TEMPLATE_INFO (olddecl) 139490287Sobrien || (DECL_TI_TEMPLATE (olddecl) 139551412Sobrien != DECL_TI_TEMPLATE (newdecl)))))) 139651412Sobrien /* It's OK to have a template specialization and a non-template 139751412Sobrien with the same type, or to have specializations of two 139851412Sobrien different templates with the same type. Note that if one is a 139951412Sobrien specialization, and the other is an instantiation of the same 140051412Sobrien template, that we do not exit at this point. That situation 140151412Sobrien can occur if we instantiate a template class, and then 1402117410Skan specialize one of its methods. This situation is valid, but 140351412Sobrien the declarations must be merged in the usual way. */ 1404132747Skan return NULL_TREE; 140590287Sobrien else if (TREE_CODE (newdecl) == FUNCTION_DECL 140690287Sobrien && ((DECL_TEMPLATE_INSTANTIATION (olddecl) 140751412Sobrien && !DECL_USE_TEMPLATE (newdecl)) 140851412Sobrien || (DECL_TEMPLATE_INSTANTIATION (newdecl) 140951412Sobrien && !DECL_USE_TEMPLATE (olddecl)))) 141051412Sobrien /* One of the declarations is a template instantiation, and the 141151412Sobrien other is not a template at all. That's OK. */ 1412132747Skan return NULL_TREE; 141351412Sobrien else if (TREE_CODE (newdecl) == NAMESPACE_DECL 141451412Sobrien && DECL_NAMESPACE_ALIAS (newdecl) 141551412Sobrien && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl)) 1416132747Skan /* In [namespace.alias] we have: 1417132747Skan 1418132747Skan In a declarative region, a namespace-alias-definition can be 1419132747Skan used to redefine a namespace-alias declared in that declarative 1420132747Skan region to refer only to the namespace to which it already 1421132747Skan refers. 1422132747Skan 1423132747Skan Therefore, if we encounter a second alias directive for the same 1424132747Skan alias, we can just ignore the second directive. */ 1425132747Skan return olddecl; 142618334Speter else 142718334Speter { 142852290Sobrien const char *errmsg = redeclaration_error_message (newdecl, olddecl); 142918334Speter if (errmsg) 143018334Speter { 143190287Sobrien error (errmsg, newdecl); 143218334Speter if (DECL_NAME (olddecl) != NULL_TREE) 143318334Speter cp_error_at ((DECL_INITIAL (olddecl) 143451412Sobrien && namespace_bindings_p ()) 143518334Speter ? "`%#D' previously defined here" 143618334Speter : "`%#D' previously declared here", olddecl); 1437132747Skan return error_mark_node; 143818334Speter } 143918334Speter else if (TREE_CODE (olddecl) == FUNCTION_DECL 144018334Speter && DECL_INITIAL (olddecl) != NULL_TREE 144118334Speter && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE 144218334Speter && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE) 144318334Speter { 144418334Speter /* Prototype decl follows defn w/o prototype. */ 144518334Speter cp_warning_at ("prototype for `%#D'", newdecl); 1446132747Skan warning ("%Jfollows non-prototype definition here", olddecl); 144718334Speter } 144818334Speter else if (TREE_CODE (olddecl) == FUNCTION_DECL 144918334Speter && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) 145018334Speter { 145118334Speter /* extern "C" int foo (); 145218334Speter int foo () { bar (); } 145318334Speter is OK. */ 145490287Sobrien if (current_lang_depth () == 0) 145590287Sobrien SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 145618334Speter else 145718334Speter { 145818334Speter cp_error_at ("previous declaration of `%#D' with %L linkage", 145918334Speter olddecl, DECL_LANGUAGE (olddecl)); 146090287Sobrien error ("conflicts with new declaration with %L linkage", 146118334Speter DECL_LANGUAGE (newdecl)); 146218334Speter } 146318334Speter } 146418334Speter 146551412Sobrien if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) 146651412Sobrien ; 146751412Sobrien else if (TREE_CODE (olddecl) == FUNCTION_DECL) 146818334Speter { 146918334Speter tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 147018334Speter tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); 147118334Speter int i = 1; 147218334Speter 147318334Speter if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE) 147418334Speter t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2); 147590287Sobrien 147618334Speter for (; t1 && t1 != void_list_node; 147718334Speter t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) 147818334Speter if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) 147918334Speter { 148018334Speter if (1 == simple_cst_equal (TREE_PURPOSE (t1), 148118334Speter TREE_PURPOSE (t2))) 148218334Speter { 148396292Sobrien pedwarn ("default argument given for parameter %d of `%#D'", 148496292Sobrien i, newdecl); 148596292Sobrien cp_pedwarn_at ("after previous specification in `%#D'", 148696292Sobrien olddecl); 148718334Speter } 148818334Speter else 148918334Speter { 149090287Sobrien error ("default argument given for parameter %d of `%#D'", 149118334Speter i, newdecl); 149251412Sobrien cp_error_at ("after previous specification in `%#D'", 149318334Speter olddecl); 149418334Speter } 149518334Speter } 149618334Speter 149790287Sobrien if (DECL_DECLARED_INLINE_P (newdecl) 149890287Sobrien && ! DECL_DECLARED_INLINE_P (olddecl) 149951412Sobrien && TREE_ADDRESSABLE (olddecl) && warn_inline) 150018334Speter { 1501132747Skan warning ("`%#D' was used before it was declared inline", newdecl); 1502132747Skan warning ("%Jprevious non-inline declaration here", olddecl); 150318334Speter } 150418334Speter } 150518334Speter } 150618334Speter 150796292Sobrien /* Do not merge an implicit typedef with an explicit one. In: 150896292Sobrien 150996292Sobrien class A; 151096292Sobrien ... 151196292Sobrien typedef class A A __attribute__ ((foo)); 151296292Sobrien 151396292Sobrien the attribute should apply only to the typedef. */ 151496292Sobrien if (TREE_CODE (olddecl) == TYPE_DECL 151596292Sobrien && (DECL_IMPLICIT_TYPEDEF_P (olddecl) 151696292Sobrien || DECL_IMPLICIT_TYPEDEF_P (newdecl))) 1517132747Skan return NULL_TREE; 151896292Sobrien 151918334Speter /* If new decl is `static' and an `extern' was seen previously, 152018334Speter warn about it. */ 152118334Speter warn_extern_redeclared_static (newdecl, olddecl); 152218334Speter 152351412Sobrien /* We have committed to returning 1 at this point. */ 152418334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 152518334Speter { 152618334Speter /* Now that functions must hold information normally held 152718334Speter by field decls, there is extra work to do so that 152818334Speter declaration information does not get destroyed during 152918334Speter definition. */ 153018334Speter if (DECL_VINDEX (olddecl)) 153118334Speter DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); 153218334Speter if (DECL_CONTEXT (olddecl)) 153318334Speter DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 153418334Speter DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); 153518334Speter DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 153690287Sobrien DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); 153751412Sobrien DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); 153851412Sobrien DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl); 153990287Sobrien DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); 154090287Sobrien if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK) 154190287Sobrien SET_OVERLOADED_OPERATOR_CODE 154290287Sobrien (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl)); 154351412Sobrien new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; 154490287Sobrien 154551412Sobrien /* Optionally warn about more than one declaration for the same 154651412Sobrien name, but don't warn about a function declaration followed by a 154751412Sobrien definition. */ 154851412Sobrien if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) 154951412Sobrien && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) 1550117410Skan /* Don't warn about extern decl followed by definition. */ 155151412Sobrien && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) 1552117410Skan /* Don't warn about friends, let add_friend take care of it. */ 155390287Sobrien && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl))) 155451412Sobrien { 155590287Sobrien warning ("redundant redeclaration of `%D' in same scope", newdecl); 155651412Sobrien cp_warning_at ("previous declaration of `%D'", olddecl); 155751412Sobrien } 155818334Speter } 155918334Speter 156018334Speter /* Deal with C++: must preserve virtual function table size. */ 156118334Speter if (TREE_CODE (olddecl) == TYPE_DECL) 156218334Speter { 1563132747Skan tree newtype = TREE_TYPE (newdecl); 1564132747Skan tree oldtype = TREE_TYPE (olddecl); 156518334Speter 156618334Speter if (newtype != error_mark_node && oldtype != error_mark_node 156718334Speter && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) 1568117410Skan CLASSTYPE_FRIEND_CLASSES (newtype) 1569117410Skan = CLASSTYPE_FRIEND_CLASSES (oldtype); 1570132747Skan 157190287Sobrien DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); 157218334Speter } 157318334Speter 157451412Sobrien /* Copy all the DECL_... slots specified in the new decl 157551412Sobrien except for any that we copy here from the old type. */ 157690287Sobrien DECL_ATTRIBUTES (newdecl) 157790287Sobrien = (*targetm.merge_decl_attributes) (olddecl, newdecl); 157818334Speter 157951412Sobrien if (TREE_CODE (newdecl) == TEMPLATE_DECL) 158018334Speter { 158152290Sobrien TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)); 158290287Sobrien DECL_TEMPLATE_SPECIALIZATIONS (olddecl) 158352290Sobrien = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), 158452290Sobrien DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); 158590287Sobrien 158690287Sobrien /* If the new declaration is a definition, update the file and 158790287Sobrien line information on the declaration. */ 158890287Sobrien if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE 158990287Sobrien && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE) 159090287Sobrien { 1591117410Skan DECL_SOURCE_LOCATION (olddecl) 1592117410Skan = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl)) 1593117410Skan = DECL_SOURCE_LOCATION (newdecl); 1594132747Skan if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 1595132747Skan DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (olddecl)) 1596132747Skan = DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (newdecl)); 159790287Sobrien } 159890287Sobrien 1599132747Skan if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 1600132747Skan { 1601132747Skan DECL_INLINE (DECL_TEMPLATE_RESULT (olddecl)) 1602132747Skan |= DECL_INLINE (DECL_TEMPLATE_RESULT (newdecl)); 1603132747Skan DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (olddecl)) 1604132747Skan |= DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (newdecl)); 1605132747Skan } 1606132747Skan 1607132747Skan return olddecl; 160818334Speter } 160990287Sobrien 161018334Speter if (types_match) 161118334Speter { 161218334Speter /* Automatically handles default parameters. */ 161318334Speter tree oldtype = TREE_TYPE (olddecl); 161418334Speter tree newtype; 161518334Speter 161618334Speter /* Merge the data types specified in the two decls. */ 161796292Sobrien newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); 161818334Speter 161996292Sobrien /* If merge_types produces a non-typedef type, just use the old type. */ 162090287Sobrien if (TREE_CODE (newdecl) == TYPE_DECL 162190287Sobrien && newtype == DECL_ORIGINAL_TYPE (newdecl)) 162290287Sobrien newtype = oldtype; 162390287Sobrien 162418334Speter if (TREE_CODE (newdecl) == VAR_DECL) 1625117410Skan { 1626117410Skan DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); 1627117410Skan DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); 1628132747Skan DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl) 1629132747Skan |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl); 1630117410Skan } 1631117410Skan 163296292Sobrien /* Do this after calling `merge_types' so that default 163318334Speter parameters don't confuse us. */ 163418334Speter else if (TREE_CODE (newdecl) == FUNCTION_DECL 163518334Speter && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)) 163618334Speter != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)))) 163718334Speter { 163818334Speter TREE_TYPE (newdecl) = build_exception_variant (newtype, 163918334Speter TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))); 164018334Speter TREE_TYPE (olddecl) = build_exception_variant (newtype, 164118334Speter TYPE_RAISES_EXCEPTIONS (oldtype)); 164218334Speter 164390287Sobrien if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl)) 164451412Sobrien && DECL_SOURCE_LINE (olddecl) != 0 164551412Sobrien && flag_exceptions 164690287Sobrien && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)), 164790287Sobrien TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1)) 164818334Speter { 164990287Sobrien error ("declaration of `%F' throws different exceptions", 165018334Speter newdecl); 165190287Sobrien cp_error_at ("than previous declaration `%F'", olddecl); 165218334Speter } 165318334Speter } 165418334Speter TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; 165518334Speter 165618334Speter /* Lay the type out, unless already done. */ 165752290Sobrien if (! same_type_p (newtype, oldtype) 165851412Sobrien && TREE_TYPE (newdecl) != error_mark_node 165951412Sobrien && !(processing_template_decl && uses_template_parms (newdecl))) 166018334Speter layout_type (TREE_TYPE (newdecl)); 166118334Speter 166251412Sobrien if ((TREE_CODE (newdecl) == VAR_DECL 166351412Sobrien || TREE_CODE (newdecl) == PARM_DECL 166451412Sobrien || TREE_CODE (newdecl) == RESULT_DECL 166551412Sobrien || TREE_CODE (newdecl) == FIELD_DECL 166651412Sobrien || TREE_CODE (newdecl) == TYPE_DECL) 166751412Sobrien && !(processing_template_decl && uses_template_parms (newdecl))) 166818334Speter layout_decl (newdecl, 0); 166918334Speter 167018334Speter /* Merge the type qualifiers. */ 167118334Speter if (TREE_READONLY (newdecl)) 167218334Speter TREE_READONLY (olddecl) = 1; 167318334Speter if (TREE_THIS_VOLATILE (newdecl)) 167418334Speter TREE_THIS_VOLATILE (olddecl) = 1; 167518334Speter 167618334Speter /* Merge the initialization information. */ 167718334Speter if (DECL_INITIAL (newdecl) == NULL_TREE 167818334Speter && DECL_INITIAL (olddecl) != NULL_TREE) 167918334Speter { 168018334Speter DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 1681117410Skan DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 168290287Sobrien if (CAN_HAVE_FULL_LANG_DECL_P (newdecl) 168390287Sobrien && DECL_LANG_SPECIFIC (newdecl) 168452290Sobrien && DECL_LANG_SPECIFIC (olddecl)) 1685132747Skan { 1686132747Skan DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 1687132747Skan DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl); 1688132747Skan } 168918334Speter } 169018334Speter 169118334Speter /* Merge the section attribute. 169218334Speter We want to issue an error if the sections conflict but that must be 169318334Speter done later in decl_attributes since we are called before attributes 169418334Speter are assigned. */ 169518334Speter if (DECL_SECTION_NAME (newdecl) == NULL_TREE) 169618334Speter DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); 169718334Speter 169890287Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL) 169990287Sobrien { 170090287Sobrien DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 170190287Sobrien |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 1702132747Skan DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 1703132747Skan TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1704132747Skan TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1705132747Skan TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl); 1706132747Skan DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1707132747Skan DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); 170896292Sobrien /* Keep the old RTL. */ 170996292Sobrien COPY_DECL_RTL (olddecl, newdecl); 171090287Sobrien } 171196292Sobrien else if (TREE_CODE (newdecl) == VAR_DECL 171296292Sobrien && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) 171396292Sobrien { 171496292Sobrien /* Keep the old RTL. We cannot keep the old RTL if the old 171596292Sobrien declaration was for an incomplete object and the new 171696292Sobrien declaration is not since many attributes of the RTL will 171796292Sobrien change. */ 171896292Sobrien COPY_DECL_RTL (olddecl, newdecl); 171996292Sobrien } 172018334Speter } 172118334Speter /* If cannot merge, then use the new type and qualifiers, 172218334Speter and don't preserve the old rtl. */ 172318334Speter else 172418334Speter { 172518334Speter /* Clean out any memory we had of the old declaration. */ 172618334Speter tree oldstatic = value_member (olddecl, static_aggregates); 172718334Speter if (oldstatic) 172818334Speter TREE_VALUE (oldstatic) = error_mark_node; 172918334Speter 173018334Speter TREE_TYPE (olddecl) = TREE_TYPE (newdecl); 173118334Speter TREE_READONLY (olddecl) = TREE_READONLY (newdecl); 173218334Speter TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); 173318334Speter TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); 173418334Speter } 173518334Speter 173618334Speter /* Merge the storage class information. */ 173796292Sobrien merge_weak (newdecl, olddecl); 173896292Sobrien 173951412Sobrien DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl); 174090287Sobrien DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); 174118334Speter TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 174218334Speter TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); 174318334Speter if (! DECL_EXTERNAL (olddecl)) 174418334Speter DECL_EXTERNAL (newdecl) = 0; 174590287Sobrien 174651412Sobrien if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) 174718334Speter { 174818334Speter DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); 174918334Speter DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); 175051412Sobrien DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); 175190287Sobrien DECL_TEMPLATE_INSTANTIATED (newdecl) 175252290Sobrien |= DECL_TEMPLATE_INSTANTIATED (olddecl); 175351412Sobrien /* Don't really know how much of the language-specific 175451412Sobrien values we should copy from old to new. */ 175551412Sobrien DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); 1756117410Skan DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 1757117410Skan DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2; 175851412Sobrien DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); 175952290Sobrien DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); 176090287Sobrien DECL_INITIALIZED_IN_CLASS_P (newdecl) 176190287Sobrien |= DECL_INITIALIZED_IN_CLASS_P (olddecl); 176251412Sobrien olddecl_friend = DECL_FRIEND_P (olddecl); 176352290Sobrien 176452290Sobrien /* Only functions have DECL_BEFRIENDING_CLASSES. */ 176552290Sobrien if (TREE_CODE (newdecl) == FUNCTION_DECL 176652290Sobrien || DECL_FUNCTION_TEMPLATE_P (newdecl)) 1767117410Skan { 1768117410Skan DECL_BEFRIENDING_CLASSES (newdecl) 1769117410Skan = chainon (DECL_BEFRIENDING_CLASSES (newdecl), 1770117410Skan DECL_BEFRIENDING_CLASSES (olddecl)); 1771117410Skan /* DECL_THUNKS is only valid for virtual functions, 1772117410Skan otherwise it is a DECL_FRIEND_CONTEXT. */ 1773117410Skan if (DECL_VIRTUAL_P (newdecl)) 1774117410Skan DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl); 1775117410Skan } 177618334Speter } 177718334Speter 177818334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 177918334Speter { 178090287Sobrien if (DECL_TEMPLATE_INSTANTIATION (olddecl) 178190287Sobrien && !DECL_TEMPLATE_INSTANTIATION (newdecl)) 178251412Sobrien { 178351412Sobrien /* If newdecl is not a specialization, then it is not a 178451412Sobrien template-related function at all. And that means that we 1785132747Skan should have exited above, returning 0. */ 178651412Sobrien my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl), 178751412Sobrien 0); 178851412Sobrien 178990287Sobrien if (TREE_USED (olddecl)) 179051412Sobrien /* From [temp.expl.spec]: 179190287Sobrien 179251412Sobrien If a template, a member template or the member of a class 179351412Sobrien template is explicitly specialized then that 179451412Sobrien specialization shall be declared before the first use of 179551412Sobrien that specialization that would cause an implicit 179651412Sobrien instantiation to take place, in every translation unit in 179751412Sobrien which such a use occurs. */ 179890287Sobrien error ("explicit specialization of %D after first use", 179951412Sobrien olddecl); 180051412Sobrien 180151412Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); 1802117410Skan 1803117410Skan /* [temp.expl.spec/14] We don't inline explicit specialization 1804117410Skan just because the primary template says so. */ 180551412Sobrien } 1806117410Skan else 1807117410Skan { 1808117410Skan if (DECL_PENDING_INLINE_INFO (newdecl) == 0) 1809117410Skan DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl); 181018334Speter 1811117410Skan DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); 181218334Speter 1813117410Skan /* If either decl says `inline', this fn is inline, unless 1814117410Skan its definition was passed already. */ 1815117410Skan if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE) 1816117410Skan DECL_INLINE (olddecl) = 1; 1817117410Skan DECL_INLINE (newdecl) = DECL_INLINE (olddecl); 181890287Sobrien 1819117410Skan DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 1820117410Skan = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 1821117410Skan } 1822117410Skan 182390287Sobrien /* Preserve abstractness on cloned [cd]tors. */ 182490287Sobrien DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl); 182590287Sobrien 182618334Speter if (! types_match) 182718334Speter { 182890287Sobrien SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 182990287Sobrien COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); 183090287Sobrien SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); 183151412Sobrien } 183251412Sobrien if (! types_match || new_defines_function) 183351412Sobrien { 183490287Sobrien /* These need to be copied so that the names are available. 183590287Sobrien Note that if the types do match, we'll preserve inline 183690287Sobrien info and other bits, but if not, we won't. */ 183718334Speter DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); 183818334Speter DECL_RESULT (olddecl) = DECL_RESULT (newdecl); 183918334Speter } 184018334Speter if (new_defines_function) 184118334Speter /* If defining a function declared with other language 184218334Speter linkage, use the previously declared language linkage. */ 184390287Sobrien SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 184490287Sobrien else if (types_match) 184518334Speter { 184618334Speter /* If redeclaring a builtin function, and not a definition, 184718334Speter it stays built in. */ 184818334Speter if (DECL_BUILT_IN (olddecl)) 184918334Speter { 185090287Sobrien DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); 185118334Speter DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); 185218334Speter /* If we're keeping the built-in definition, keep the rtl, 185318334Speter regardless of declaration matches. */ 185490287Sobrien SET_DECL_RTL (newdecl, DECL_RTL (olddecl)); 185518334Speter } 185618334Speter 185718334Speter DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 185818334Speter /* Don't clear out the arguments if we're redefining a function. */ 185918334Speter if (DECL_ARGUMENTS (olddecl)) 186018334Speter DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 186118334Speter } 186218334Speter } 186390287Sobrien else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 186490287Sobrien NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); 186518334Speter 186618334Speter /* Now preserve various other info from the definition. */ 186718334Speter TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); 186818334Speter TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); 186918334Speter DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 187090287Sobrien COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 187118334Speter 1872132747Skan /* If either declaration has a nondefault visibility, use it. */ 1873132747Skan if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT) 1874132747Skan { 1875132747Skan if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT 1876132747Skan && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 1877132747Skan { 1878132747Skan warning ("%J'%D': visibility attribute ignored because it", 1879132747Skan newdecl, newdecl); 1880132747Skan warning ("%Jconflicts with previous declaration here", olddecl); 1881132747Skan } 1882132747Skan DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 1883132747Skan } 1884132747Skan 188518334Speter if (TREE_CODE (newdecl) == FUNCTION_DECL) 188618334Speter { 188718334Speter int function_size; 188818334Speter 188918334Speter function_size = sizeof (struct tree_decl); 189018334Speter 189190287Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 189290287Sobrien (char *) newdecl + sizeof (struct tree_common), 189390287Sobrien function_size - sizeof (struct tree_common)); 189418334Speter 189551412Sobrien if (DECL_TEMPLATE_INSTANTIATION (newdecl)) 1896132747Skan /* If newdecl is a template instantiation, it is possible that 1897132747Skan the following sequence of events has occurred: 189851412Sobrien 1899132747Skan o A friend function was declared in a class template. The 1900132747Skan class template was instantiated. 190151412Sobrien 1902132747Skan o The instantiation of the friend declaration was 1903132747Skan recorded on the instantiation list, and is newdecl. 190451412Sobrien 1905132747Skan o Later, however, instantiate_class_template called pushdecl 1906132747Skan on the newdecl to perform name injection. But, pushdecl in 1907132747Skan turn called duplicate_decls when it discovered that another 1908132747Skan declaration of a global function with the same name already 1909132747Skan existed. 191051412Sobrien 1911132747Skan o Here, in duplicate_decls, we decided to clobber newdecl. 191251412Sobrien 1913132747Skan If we're going to do that, we'd better make sure that 1914132747Skan olddecl, and not newdecl, is on the list of 1915132747Skan instantiations so that if we try to do the instantiation 1916132747Skan again we won't get the clobbered declaration. */ 1917132747Skan reregister_specialization (newdecl, 1918132747Skan DECL_TI_TEMPLATE (newdecl), 1919132747Skan olddecl); 192018334Speter } 192118334Speter else 192218334Speter { 192390287Sobrien memcpy ((char *) olddecl + sizeof (struct tree_common), 192490287Sobrien (char *) newdecl + sizeof (struct tree_common), 192590287Sobrien sizeof (struct tree_decl) - sizeof (struct tree_common) 1926117410Skan + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 192718334Speter } 192818334Speter 192918334Speter DECL_UID (olddecl) = olddecl_uid; 193018334Speter if (olddecl_friend) 193118334Speter DECL_FRIEND_P (olddecl) = 1; 193218334Speter 193351412Sobrien /* NEWDECL contains the merged attribute lists. 193451412Sobrien Update OLDDECL to be the same. */ 193590287Sobrien DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); 193651412Sobrien 1937119270Skan /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 1938132747Skan so that encode_section_info has a chance to look at the new decl 1939132747Skan flags and attributes. */ 1940132747Skan if (DECL_RTL_SET_P (olddecl) 1941119270Skan && (TREE_CODE (olddecl) == FUNCTION_DECL 1942119270Skan || (TREE_CODE (olddecl) == VAR_DECL 1943119270Skan && TREE_STATIC (olddecl)))) 1944119270Skan make_decl_rtl (olddecl, NULL); 1945119270Skan 1946132747Skan return olddecl; 194718334Speter} 194818334Speter 194918334Speter/* Generate an implicit declaration for identifier FUNCTIONID 195018334Speter as a function of type int (). Print a warning if appropriate. */ 195118334Speter 195218334Spetertree 1953132747Skanimplicitly_declare (tree functionid) 195418334Speter{ 1955132747Skan tree decl; 195618334Speter 195718334Speter /* We used to reuse an old implicit decl here, 195818334Speter but this loses with inline functions because it can clobber 195918334Speter the saved decl chains. */ 196018334Speter decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type); 196118334Speter 196218334Speter DECL_EXTERNAL (decl) = 1; 196318334Speter TREE_PUBLIC (decl) = 1; 196418334Speter 196590287Sobrien /* ISO standard says implicit declarations are in the innermost block. 196651412Sobrien So we record the decl in the standard fashion. */ 196718334Speter pushdecl (decl); 196890287Sobrien rest_of_decl_compilation (decl, NULL, 0, 0); 196918334Speter 197018334Speter if (warn_implicit 197118334Speter /* Only one warning per identifier. */ 197218334Speter && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE) 197318334Speter { 197490287Sobrien pedwarn ("implicit declaration of function `%#D'", decl); 197518334Speter } 197618334Speter 197718334Speter SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl); 197818334Speter 197918334Speter return decl; 198018334Speter} 198118334Speter 198218334Speter/* Return zero if the declaration NEWDECL is valid 198318334Speter when the declaration OLDDECL (assumed to be for the same name) 198418334Speter has already been seen. 198518334Speter Otherwise return an error message format string with a %s 198618334Speter where the identifier should go. */ 198718334Speter 198852290Sobrienstatic const char * 1989132747Skanredeclaration_error_message (tree newdecl, tree olddecl) 199018334Speter{ 199118334Speter if (TREE_CODE (newdecl) == TYPE_DECL) 199218334Speter { 199318334Speter /* Because C++ can put things into name space for free, 199418334Speter constructs like "typedef struct foo { ... } foo" 199518334Speter would look like an erroneous redeclaration. */ 199652290Sobrien if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) 199718334Speter return 0; 199818334Speter else 199918334Speter return "redefinition of `%#D'"; 200018334Speter } 200118334Speter else if (TREE_CODE (newdecl) == FUNCTION_DECL) 200218334Speter { 200318334Speter /* If this is a pure function, its olddecl will actually be 200418334Speter the original initialization to `0' (which we force to call 200518334Speter abort()). Don't complain about redefinition in this case. */ 200690287Sobrien if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)) 200718334Speter return 0; 200818334Speter 200951412Sobrien /* If both functions come from different namespaces, this is not 2010117410Skan a redeclaration - this is a conflict with a used function. */ 201151412Sobrien if (DECL_NAMESPACE_SCOPE_P (olddecl) 201251412Sobrien && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)) 201351412Sobrien return "`%D' conflicts with used function"; 201451412Sobrien 201518334Speter /* We'll complain about linkage mismatches in 201618334Speter warn_extern_redeclared_static. */ 201718334Speter 201851412Sobrien /* Defining the same name twice is no good. */ 201918334Speter if (DECL_INITIAL (olddecl) != NULL_TREE 202018334Speter && DECL_INITIAL (newdecl) != NULL_TREE) 202118334Speter { 202218334Speter if (DECL_NAME (olddecl) == NULL_TREE) 202318334Speter return "`%#D' not declared in class"; 202418334Speter else 202518334Speter return "redefinition of `%#D'"; 202618334Speter } 202718334Speter return 0; 202818334Speter } 202918334Speter else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 203018334Speter { 2031132747Skan tree nt, ot; 2032132747Skan 2033132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) 2034132747Skan { 2035132747Skan if (COMPLETE_TYPE_P (TREE_TYPE (newdecl)) 2036132747Skan && COMPLETE_TYPE_P (TREE_TYPE (olddecl))) 2037132747Skan return "redefinition of `%#D'"; 2038132747Skan return NULL; 2039132747Skan } 2040132747Skan 2041132747Skan if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL 2042132747Skan || (DECL_TEMPLATE_RESULT (newdecl) 2043132747Skan == DECL_TEMPLATE_RESULT (olddecl))) 2044132747Skan return NULL; 2045132747Skan 2046132747Skan nt = DECL_TEMPLATE_RESULT (newdecl); 2047132747Skan if (DECL_TEMPLATE_INFO (nt)) 2048132747Skan nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt)); 2049132747Skan ot = DECL_TEMPLATE_RESULT (olddecl); 2050132747Skan if (DECL_TEMPLATE_INFO (ot)) 2051132747Skan ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot)); 2052132747Skan if (DECL_INITIAL (nt) && DECL_INITIAL (ot)) 205318334Speter return "redefinition of `%#D'"; 2054132747Skan 2055132747Skan return NULL; 205618334Speter } 205790287Sobrien else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) 205818334Speter { 205918334Speter /* Objects declared at top level: */ 206018334Speter /* If at least one is a reference, it's ok. */ 206118334Speter if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) 206218334Speter return 0; 206318334Speter /* Reject two definitions. */ 206418334Speter return "redefinition of `%#D'"; 206518334Speter } 206618334Speter else 206718334Speter { 206818334Speter /* Objects declared with block scope: */ 206918334Speter /* Reject two definitions, and reject a definition 207018334Speter together with an external reference. */ 207118334Speter if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) 207218334Speter return "redeclaration of `%#D'"; 207318334Speter return 0; 207418334Speter } 207518334Speter} 207618334Speter 207790287Sobrien/* Create a new label, named ID. */ 207818334Speter 207990287Sobrienstatic tree 2080132747Skanmake_label_decl (tree id, int local_p) 208118334Speter{ 208290287Sobrien tree decl; 208318334Speter 208490287Sobrien decl = build_decl (LABEL_DECL, id, void_type_node); 208518334Speter 208690287Sobrien DECL_CONTEXT (decl) = current_function_decl; 208790287Sobrien DECL_MODE (decl) = VOIDmode; 208890287Sobrien C_DECLARED_LABEL_FLAG (decl) = local_p; 208990287Sobrien 209090287Sobrien /* Say where one reference is to the label, for the sake of the 209190287Sobrien error if it is not defined. */ 2092132747Skan DECL_SOURCE_LOCATION (decl) = input_location; 209390287Sobrien 209490287Sobrien /* Record the fact that this identifier is bound to this label. */ 209590287Sobrien SET_IDENTIFIER_LABEL_VALUE (id, decl); 209690287Sobrien 209790287Sobrien return decl; 209890287Sobrien} 209990287Sobrien 210090287Sobrien/* Record this label on the list of used labels so that we can check 210190287Sobrien at the end of the function to see whether or not the label was 210290287Sobrien actually defined, and so we can check when the label is defined whether 210390287Sobrien this use is valid. */ 210490287Sobrien 210590287Sobrienstatic void 2106132747Skanuse_label (tree decl) 210790287Sobrien{ 210890287Sobrien if (named_label_uses == NULL 210990287Sobrien || named_label_uses->names_in_scope != current_binding_level->names 211090287Sobrien || named_label_uses->label_decl != decl) 211118334Speter { 211290287Sobrien struct named_label_use_list *new_ent; 2113132747Skan new_ent = ggc_alloc (sizeof (struct named_label_use_list)); 211451412Sobrien new_ent->label_decl = decl; 211551412Sobrien new_ent->names_in_scope = current_binding_level->names; 211651412Sobrien new_ent->binding_level = current_binding_level; 2117132747Skan new_ent->o_goto_locus = input_location; 211851412Sobrien new_ent->next = named_label_uses; 211951412Sobrien named_label_uses = new_ent; 212018334Speter } 212190287Sobrien} 212218334Speter 212390287Sobrien/* Look for a label named ID in the current function. If one cannot 212490287Sobrien be found, create one. (We keep track of used, but undefined, 212590287Sobrien labels, and complain about them at the end of a function.) */ 212690287Sobrien 212790287Sobrientree 2128132747Skanlookup_label (tree id) 212990287Sobrien{ 213090287Sobrien tree decl; 213190287Sobrien struct named_label_list *ent; 213290287Sobrien 2133117410Skan timevar_push (TV_NAME_LOOKUP); 213490287Sobrien /* You can't use labels at global scope. */ 213590287Sobrien if (current_function_decl == NULL_TREE) 213618334Speter { 213790287Sobrien error ("label `%s' referenced outside of any function", 213890287Sobrien IDENTIFIER_POINTER (id)); 2139117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 214018334Speter } 214118334Speter 214290287Sobrien /* See if we've already got this label. */ 214390287Sobrien decl = IDENTIFIER_LABEL_VALUE (id); 214490287Sobrien if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl) 2145117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 214618334Speter 214790287Sobrien /* Record this label on the list of labels used in this function. 214890287Sobrien We do this before calling make_label_decl so that we get the 214990287Sobrien IDENTIFIER_LABEL_VALUE before the new label is declared. */ 2150132747Skan ent = ggc_alloc_cleared (sizeof (struct named_label_list)); 215190287Sobrien ent->old_value = IDENTIFIER_LABEL_VALUE (id); 215290287Sobrien ent->next = named_labels; 215390287Sobrien named_labels = ent; 215451412Sobrien 215590287Sobrien /* We need a new label. */ 215690287Sobrien decl = make_label_decl (id, /*local_p=*/0); 215718334Speter 215890287Sobrien /* Now fill in the information we didn't have before. */ 215990287Sobrien ent->label_decl = decl; 216018334Speter 2161117410Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 216290287Sobrien} 216318334Speter 216490287Sobrien/* Declare a local label named ID. */ 216518334Speter 216690287Sobrientree 2167132747Skandeclare_local_label (tree id) 216890287Sobrien{ 216990287Sobrien tree decl; 217018334Speter 217190287Sobrien /* Add a new entry to the SHADOWED_LABELS list so that when we leave 217290287Sobrien this scope we can restore the old value of 217390287Sobrien IDENTIFIER_TYPE_VALUE. */ 217490287Sobrien current_binding_level->shadowed_labels 217590287Sobrien = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, 217690287Sobrien current_binding_level->shadowed_labels); 217790287Sobrien /* Look for the label. */ 217890287Sobrien decl = make_label_decl (id, /*local_p=*/1); 217990287Sobrien /* Now fill in the information we didn't have before. */ 218090287Sobrien TREE_VALUE (current_binding_level->shadowed_labels) = decl; 218190287Sobrien 218218334Speter return decl; 218318334Speter} 218418334Speter 218590287Sobrien/* Returns nonzero if it is ill-formed to jump past the declaration of 218690287Sobrien DECL. Returns 2 if it's also a real problem. */ 218718334Speter 218890287Sobrienstatic int 2189132747Skandecl_jump_unsafe (tree decl) 219090287Sobrien{ 219190287Sobrien if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) 219290287Sobrien return 0; 219318334Speter 219490287Sobrien if (DECL_INITIAL (decl) == NULL_TREE 219590287Sobrien && pod_type_p (TREE_TYPE (decl))) 219690287Sobrien return 0; 219790287Sobrien 219890287Sobrien /* This is really only important if we're crossing an initialization. 219990287Sobrien The POD stuff is just pedantry; why should it matter if the class 220090287Sobrien contains a field of pointer to member type? */ 220190287Sobrien if (DECL_INITIAL (decl) 220290287Sobrien || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))) 220390287Sobrien return 2; 220490287Sobrien return 1; 220590287Sobrien} 220690287Sobrien 220790287Sobrien/* Check that a single previously seen jump to a newly defined label 220890287Sobrien is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for 220990287Sobrien the jump context; NAMES are the names in scope in LEVEL at the jump 221090287Sobrien context; FILE and LINE are the source position of the jump or 0. */ 221190287Sobrien 221290287Sobrienstatic void 2213132747Skancheck_previous_goto_1 (tree decl, 2214132747Skan struct cp_binding_level* level, 2215132747Skan tree names, const location_t *locus) 221618334Speter{ 221790287Sobrien int identified = 0; 221890287Sobrien int saw_eh = 0; 2219117410Skan struct cp_binding_level *b = current_binding_level; 222090287Sobrien for (; b; b = b->level_chain) 222190287Sobrien { 222290287Sobrien tree new_decls = b->names; 222390287Sobrien tree old_decls = (b == level ? names : NULL_TREE); 222490287Sobrien for (; new_decls != old_decls; 222590287Sobrien new_decls = TREE_CHAIN (new_decls)) 222690287Sobrien { 222790287Sobrien int problem = decl_jump_unsafe (new_decls); 222890287Sobrien if (! problem) 222990287Sobrien continue; 223018334Speter 223190287Sobrien if (! identified) 223290287Sobrien { 223390287Sobrien if (decl) 223490287Sobrien pedwarn ("jump to label `%D'", decl); 223590287Sobrien else 223690287Sobrien pedwarn ("jump to case label"); 223790287Sobrien 2238132747Skan if (locus) 2239132747Skan pedwarn ("%H from here", locus); 224090287Sobrien identified = 1; 224190287Sobrien } 224290287Sobrien 224390287Sobrien if (problem > 1) 224490287Sobrien cp_error_at (" crosses initialization of `%#D'", 224590287Sobrien new_decls); 224690287Sobrien else 224790287Sobrien cp_pedwarn_at (" enters scope of non-POD `%#D'", 224890287Sobrien new_decls); 224990287Sobrien } 225090287Sobrien 225190287Sobrien if (b == level) 225290287Sobrien break; 2253132747Skan if ((b->kind == sk_try || b->kind == sk_catch) && ! saw_eh) 225490287Sobrien { 225590287Sobrien if (! identified) 225690287Sobrien { 225790287Sobrien if (decl) 225890287Sobrien pedwarn ("jump to label `%D'", decl); 225990287Sobrien else 226090287Sobrien pedwarn ("jump to case label"); 226190287Sobrien 2262132747Skan if (locus) 2263132747Skan pedwarn ("%H from here", locus); 226490287Sobrien identified = 1; 226590287Sobrien } 2266132747Skan if (b->kind == sk_try) 226790287Sobrien error (" enters try block"); 226890287Sobrien else 226990287Sobrien error (" enters catch block"); 227090287Sobrien saw_eh = 1; 227190287Sobrien } 227290287Sobrien } 227390287Sobrien} 227490287Sobrien 227590287Sobrienstatic void 2276132747Skancheck_previous_goto (struct named_label_use_list* use) 227790287Sobrien{ 227890287Sobrien check_previous_goto_1 (use->label_decl, use->binding_level, 2279132747Skan use->names_in_scope, &use->o_goto_locus); 228090287Sobrien} 228190287Sobrien 228290287Sobrienstatic void 2283132747Skancheck_switch_goto (struct cp_binding_level* level) 228490287Sobrien{ 2285132747Skan check_previous_goto_1 (NULL_TREE, level, level->names, NULL); 228690287Sobrien} 228790287Sobrien 228890287Sobrien/* Check that any previously seen jumps to a newly defined label DECL 228990287Sobrien are OK. Called by define_label. */ 229090287Sobrien 229190287Sobrienstatic void 2292132747Skancheck_previous_gotos (tree decl) 229390287Sobrien{ 229490287Sobrien struct named_label_use_list **usep; 229590287Sobrien 229690287Sobrien if (! TREE_USED (decl)) 229790287Sobrien return; 229890287Sobrien 229990287Sobrien for (usep = &named_label_uses; *usep; ) 230018334Speter { 230190287Sobrien struct named_label_use_list *use = *usep; 230290287Sobrien if (use->label_decl == decl) 230390287Sobrien { 230490287Sobrien check_previous_goto (use); 230590287Sobrien *usep = use->next; 230690287Sobrien } 230790287Sobrien else 230890287Sobrien usep = &(use->next); 230918334Speter } 231090287Sobrien} 231118334Speter 231290287Sobrien/* Check that a new jump to a label DECL is OK. Called by 231390287Sobrien finish_goto_stmt. */ 231490287Sobrien 231590287Sobrienvoid 2316132747Skancheck_goto (tree decl) 231790287Sobrien{ 231890287Sobrien int identified = 0; 231990287Sobrien tree bad; 232090287Sobrien struct named_label_list *lab; 232190287Sobrien 232290287Sobrien /* We can't know where a computed goto is jumping. So we assume 232390287Sobrien that it's OK. */ 232490287Sobrien if (! DECL_P (decl)) 232590287Sobrien return; 232690287Sobrien 232790287Sobrien /* If the label hasn't been defined yet, defer checking. */ 232890287Sobrien if (! DECL_INITIAL (decl)) 232990287Sobrien { 233090287Sobrien use_label (decl); 233190287Sobrien return; 233290287Sobrien } 233390287Sobrien 233490287Sobrien for (lab = named_labels; lab; lab = lab->next) 233590287Sobrien if (decl == lab->label_decl) 233690287Sobrien break; 233790287Sobrien 233890287Sobrien /* If the label is not on named_labels it's a gcc local label, so 233990287Sobrien it must be in an outer scope, so jumping to it is always OK. */ 234090287Sobrien if (lab == 0) 234190287Sobrien return; 234290287Sobrien 234390287Sobrien if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls) 234490287Sobrien && !identified) 234590287Sobrien { 234690287Sobrien cp_pedwarn_at ("jump to label `%D'", decl); 234790287Sobrien pedwarn (" from here"); 234890287Sobrien identified = 1; 234990287Sobrien } 235090287Sobrien 235190287Sobrien for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad)) 235290287Sobrien { 235390287Sobrien tree b = TREE_VALUE (bad); 235490287Sobrien int u = decl_jump_unsafe (b); 235590287Sobrien 235690287Sobrien if (u > 1 && DECL_ARTIFICIAL (b)) 235790287Sobrien /* Can't skip init of __exception_info. */ 2358132747Skan error ("%J enters catch block", b); 235990287Sobrien else if (u > 1) 236090287Sobrien cp_error_at (" skips initialization of `%#D'", b); 236190287Sobrien else 236290287Sobrien cp_pedwarn_at (" enters scope of non-POD `%#D'", b); 236390287Sobrien } 236490287Sobrien 236590287Sobrien if (lab->in_try_scope) 236690287Sobrien error (" enters try block"); 236790287Sobrien else if (lab->in_catch_scope) 236890287Sobrien error (" enters catch block"); 236918334Speter} 237018334Speter 237118334Speter/* Define a label, specifying the location in the source file. 2372122192Skan Return the LABEL_DECL node for the label. */ 237318334Speter 237418334Spetertree 2375132747Skandefine_label (location_t location, tree name) 237618334Speter{ 237790287Sobrien tree decl = lookup_label (name); 237890287Sobrien struct named_label_list *ent; 2379132747Skan struct cp_binding_level *p; 238018334Speter 2381117410Skan timevar_push (TV_NAME_LOOKUP); 238290287Sobrien for (ent = named_labels; ent; ent = ent->next) 238390287Sobrien if (ent->label_decl == decl) 238490287Sobrien break; 238551412Sobrien 238690287Sobrien /* After labels, make any new cleanups in the function go into their 238718334Speter own new (temporary) binding contour. */ 2388132747Skan for (p = current_binding_level; 2389132747Skan p->kind != sk_function_parms; 2390132747Skan p = p->level_chain) 239190287Sobrien p->more_cleanups_ok = 0; 239218334Speter 239318334Speter if (name == get_identifier ("wchar_t")) 239490287Sobrien pedwarn ("label named wchar_t"); 239518334Speter 239618334Speter if (DECL_INITIAL (decl) != NULL_TREE) 2397122192Skan error ("duplicate label `%D'", decl); 239818334Speter else 239918334Speter { 240018334Speter /* Mark label as having been defined. */ 240118334Speter DECL_INITIAL (decl) = error_mark_node; 240218334Speter /* Say where in the source. */ 2403132747Skan DECL_SOURCE_LOCATION (decl) = location; 240490287Sobrien if (ent) 240590287Sobrien { 240690287Sobrien ent->names_in_scope = current_binding_level->names; 240790287Sobrien ent->binding_level = current_binding_level; 240890287Sobrien } 240990287Sobrien check_previous_gotos (decl); 241018334Speter } 2411122192Skan 2412117410Skan timevar_pop (TV_NAME_LOOKUP); 2413122192Skan return decl; 241418334Speter} 241518334Speter 241618334Speterstruct cp_switch 241718334Speter{ 2418117410Skan struct cp_binding_level *level; 241918334Speter struct cp_switch *next; 242090287Sobrien /* The SWITCH_STMT being built. */ 242190287Sobrien tree switch_stmt; 242290287Sobrien /* A splay-tree mapping the low element of a case range to the high 242390287Sobrien element, or NULL_TREE if there is no high element. Used to 242490287Sobrien determine whether or not a new case label duplicates an old case 242590287Sobrien label. We need a tree, rather than simply a hash table, because 242690287Sobrien of the GNU case range extension. */ 242790287Sobrien splay_tree cases; 242818334Speter}; 242918334Speter 243090287Sobrien/* A stack of the currently active switch statements. The innermost 243190287Sobrien switch statement is on the top of the stack. There is no need to 243290287Sobrien mark the stack for garbage collection because it is only active 243390287Sobrien during the processing of the body of a function, and we never 243490287Sobrien collect at that point. */ 243590287Sobrien 243618334Speterstatic struct cp_switch *switch_stack; 243718334Speter 243890287Sobrien/* Called right after a switch-statement condition is parsed. 243990287Sobrien SWITCH_STMT is the switch statement being parsed. */ 244090287Sobrien 244118334Spetervoid 2442132747Skanpush_switch (tree switch_stmt) 244318334Speter{ 2444132747Skan struct cp_switch *p = xmalloc (sizeof (struct cp_switch)); 244518334Speter p->level = current_binding_level; 244618334Speter p->next = switch_stack; 244790287Sobrien p->switch_stmt = switch_stmt; 244890287Sobrien p->cases = splay_tree_new (case_compare, NULL, NULL); 244918334Speter switch_stack = p; 245018334Speter} 245118334Speter 245218334Spetervoid 2453132747Skanpop_switch (void) 245418334Speter{ 245590287Sobrien struct cp_switch *cs; 245690287Sobrien 245790287Sobrien cs = switch_stack; 245890287Sobrien splay_tree_delete (cs->cases); 245918334Speter switch_stack = switch_stack->next; 246090287Sobrien free (cs); 246118334Speter} 246218334Speter 246390287Sobrien/* Note that we've seen a definition of a case label, and complain if this 246490287Sobrien is a bad place for one. */ 246551412Sobrien 246690287Sobrientree 2467132747Skanfinish_case_label (tree low_value, tree high_value) 246818334Speter{ 246990287Sobrien tree cond, r; 2470132747Skan struct cp_binding_level *p; 247118334Speter 247290287Sobrien if (processing_template_decl) 247318334Speter { 247490287Sobrien tree label; 247590287Sobrien 247690287Sobrien /* For templates, just add the case label; we'll do semantic 247790287Sobrien analysis at instantiation-time. */ 247890287Sobrien label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); 247990287Sobrien return add_stmt (build_case_label (low_value, high_value, label)); 248018334Speter } 248118334Speter 248290287Sobrien /* Find the condition on which this switch statement depends. */ 248390287Sobrien cond = SWITCH_COND (switch_stack->switch_stmt); 248490287Sobrien if (cond && TREE_CODE (cond) == TREE_LIST) 248590287Sobrien cond = TREE_VALUE (cond); 248690287Sobrien 248790287Sobrien r = c_add_case_label (switch_stack->cases, cond, low_value, high_value); 248890287Sobrien 248990287Sobrien check_switch_goto (switch_stack->level); 249090287Sobrien 249190287Sobrien /* After labels, make any new cleanups in the function go into their 249218334Speter own new (temporary) binding contour. */ 2493132747Skan for (p = current_binding_level; 2494132747Skan p->kind != sk_function_parms; 2495132747Skan p = p->level_chain) 249690287Sobrien p->more_cleanups_ok = 0; 249718334Speter 249890287Sobrien return r; 249918334Speter} 250018334Speter 250152290Sobrien/* Hash a TYPENAME_TYPE. K is really of type `tree'. */ 250252290Sobrien 2503117410Skanstatic hashval_t 2504132747Skantypename_hash (const void* k) 250552290Sobrien{ 2506117410Skan hashval_t hash; 2507117410Skan tree t = (tree) k; 250852290Sobrien 2509117410Skan hash = (htab_hash_pointer (TYPE_CONTEXT (t)) 2510117410Skan ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t)))); 251152290Sobrien 251252290Sobrien return hash; 251352290Sobrien} 251452290Sobrien 251552290Sobrien/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */ 251652290Sobrien 2517117410Skanstatic int 2518132747Skantypename_compare (const void * k1, const void * k2) 251952290Sobrien{ 252052290Sobrien tree t1; 252152290Sobrien tree t2; 252252290Sobrien tree d1; 252352290Sobrien tree d2; 252452290Sobrien 252552290Sobrien t1 = (tree) k1; 252652290Sobrien t2 = (tree) k2; 252752290Sobrien d1 = TYPE_NAME (t1); 252852290Sobrien d2 = TYPE_NAME (t2); 252990287Sobrien 253052290Sobrien return (DECL_NAME (d1) == DECL_NAME (d2) 2531117410Skan && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2) 253290287Sobrien && ((TREE_TYPE (t1) != NULL_TREE) 253352290Sobrien == (TREE_TYPE (t2) != NULL_TREE)) 253452290Sobrien && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) 253552290Sobrien && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2)); 253652290Sobrien} 253752290Sobrien 253852290Sobrien/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is 253952290Sobrien the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE 254052290Sobrien is non-NULL, this type is being created by the implicit typename 254152290Sobrien extension, and BASE_TYPE is a type named `t' in some base class of 254290287Sobrien `T' which depends on template parameters. 254352290Sobrien 254452290Sobrien Returns the new TYPENAME_TYPE. */ 254552290Sobrien 2546117410Skanstatic GTY ((param_is (union tree_node))) htab_t typename_htab; 2547117410Skan 2548132747Skanstatic tree 2549132747Skanbuild_typename_type (tree context, tree name, tree fullname) 255052290Sobrien{ 255152290Sobrien tree t; 255252290Sobrien tree d; 2553132747Skan void **e; 255452290Sobrien 2555117410Skan if (typename_htab == NULL) 255690287Sobrien { 2557117410Skan typename_htab = htab_create_ggc (61, &typename_hash, 2558117410Skan &typename_compare, NULL); 255990287Sobrien } 256052290Sobrien 256152290Sobrien /* Build the TYPENAME_TYPE. */ 256290287Sobrien t = make_aggr_type (TYPENAME_TYPE); 256352290Sobrien TYPE_CONTEXT (t) = FROB_CONTEXT (context); 256452290Sobrien TYPENAME_TYPE_FULLNAME (t) = fullname; 256552290Sobrien 256652290Sobrien /* Build the corresponding TYPE_DECL. */ 256752290Sobrien d = build_decl (TYPE_DECL, name, t); 256852290Sobrien TYPE_NAME (TREE_TYPE (d)) = d; 256952290Sobrien TYPE_STUB_DECL (TREE_TYPE (d)) = d; 257052290Sobrien DECL_CONTEXT (d) = FROB_CONTEXT (context); 257152290Sobrien DECL_ARTIFICIAL (d) = 1; 257252290Sobrien 257352290Sobrien /* See if we already have this type. */ 2574117410Skan e = htab_find_slot (typename_htab, t, INSERT); 2575117410Skan if (*e) 2576117410Skan t = (tree) *e; 257752290Sobrien else 2578117410Skan *e = t; 257952290Sobrien 258052290Sobrien return t; 258152290Sobrien} 258252290Sobrien 258390287Sobrien/* Resolve `typename CONTEXT::NAME'. Returns an appropriate type, 258490287Sobrien unless an error occurs, in which case error_mark_node is returned. 258596292Sobrien If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is 258696292Sobrien set, we return that, rather than the _TYPE it corresponds to, in 258796292Sobrien other cases we look through the type decl. If TF_ERROR is set, 258896292Sobrien complain about errors, otherwise be quiet. */ 258990287Sobrien 259052290Sobrientree 2591132747Skanmake_typename_type (tree context, tree name, tsubst_flags_t complain) 259251412Sobrien{ 259351412Sobrien tree fullname; 259451412Sobrien 2595132747Skan if (name == error_mark_node 2596132747Skan || context == NULL_TREE 2597132747Skan || context == error_mark_node) 2598132747Skan return error_mark_node; 2599132747Skan 260090287Sobrien if (TYPE_P (name)) 260152290Sobrien { 260290287Sobrien if (!(TYPE_LANG_SPECIFIC (name) 260390287Sobrien && (CLASSTYPE_IS_TEMPLATE (name) 260452290Sobrien || CLASSTYPE_USE_TEMPLATE (name)))) 260552290Sobrien name = TYPE_IDENTIFIER (name); 260652290Sobrien else 260752290Sobrien /* Create a TEMPLATE_ID_EXPR for the type. */ 260852290Sobrien name = build_nt (TEMPLATE_ID_EXPR, 260952290Sobrien CLASSTYPE_TI_TEMPLATE (name), 261052290Sobrien CLASSTYPE_TI_ARGS (name)); 261152290Sobrien } 261251412Sobrien else if (TREE_CODE (name) == TYPE_DECL) 261351412Sobrien name = DECL_NAME (name); 261451412Sobrien 261551412Sobrien fullname = name; 261651412Sobrien 261751412Sobrien if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 261851412Sobrien { 261951412Sobrien name = TREE_OPERAND (name, 0); 262051412Sobrien if (TREE_CODE (name) == TEMPLATE_DECL) 262151412Sobrien name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); 262251412Sobrien } 262390287Sobrien if (TREE_CODE (name) == TEMPLATE_DECL) 262490287Sobrien { 262590287Sobrien error ("`%D' used without template parameters", name); 262690287Sobrien return error_mark_node; 262790287Sobrien } 2628132747Skan my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030802); 2629132747Skan 263052290Sobrien if (TREE_CODE (context) == NAMESPACE_DECL) 263152290Sobrien { 263252290Sobrien /* We can get here from typename_sub0 in the explicit_template_type 263352290Sobrien expansion. Just fail. */ 263496292Sobrien if (complain & tf_error) 263590287Sobrien error ("no class template named `%#T' in `%#T'", 263690287Sobrien name, context); 263752290Sobrien return error_mark_node; 263852290Sobrien } 263952290Sobrien 2640132747Skan if (!dependent_type_p (context) 264151412Sobrien || currently_open_class (context)) 264251412Sobrien { 264351412Sobrien if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR) 264451412Sobrien { 264552290Sobrien tree tmpl = NULL_TREE; 264651412Sobrien if (IS_AGGR_TYPE (context)) 2647132747Skan tmpl = lookup_field (context, name, 0, false); 264852290Sobrien if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) 264951412Sobrien { 265096292Sobrien if (complain & tf_error) 265190287Sobrien error ("no class template named `%#T' in `%#T'", 265290287Sobrien name, context); 265351412Sobrien return error_mark_node; 265451412Sobrien } 265551412Sobrien 2656117410Skan if (complain & tf_error) 2657132747Skan perform_or_defer_access_check (TYPE_BINFO (context), tmpl); 2658117410Skan 265990287Sobrien return lookup_template_class (tmpl, 266052290Sobrien TREE_OPERAND (fullname, 1), 266190287Sobrien NULL_TREE, context, 266290287Sobrien /*entering_scope=*/0, 2663132747Skan tf_error | tf_warning | tf_user); 266451412Sobrien } 266551412Sobrien else 266651412Sobrien { 266790287Sobrien tree t; 266890287Sobrien 266990287Sobrien if (!IS_AGGR_TYPE (context)) 267051412Sobrien { 267196292Sobrien if (complain & tf_error) 267290287Sobrien error ("no type named `%#T' in `%#T'", name, context); 267351412Sobrien return error_mark_node; 267451412Sobrien } 267551412Sobrien 2676132747Skan t = lookup_field (context, name, 0, true); 267752290Sobrien if (t) 267896292Sobrien { 2679107601Sobrien if (TREE_CODE (t) != TYPE_DECL) 2680107601Sobrien { 2681107601Sobrien if (complain & tf_error) 2682107601Sobrien error ("no type named `%#T' in `%#T'", name, context); 2683107601Sobrien return error_mark_node; 2684107601Sobrien } 2685117410Skan 2686117410Skan if (complain & tf_error) 2687132747Skan perform_or_defer_access_check (TYPE_BINFO (context), t); 2688117410Skan 268996292Sobrien if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) 269096292Sobrien t = TREE_TYPE (t); 269196292Sobrien 269296292Sobrien return t; 269396292Sobrien } 269451412Sobrien } 269551412Sobrien } 269651412Sobrien 269752290Sobrien /* If the CONTEXT is not a template type, then either the field is 269852290Sobrien there now or its never going to be. */ 2699132747Skan if (!dependent_type_p (context)) 270052290Sobrien { 270196292Sobrien if (complain & tf_error) 270290287Sobrien error ("no type named `%#T' in `%#T'", name, context); 270352290Sobrien return error_mark_node; 270452290Sobrien } 270590287Sobrien 2706132747Skan return build_typename_type (context, name, fullname); 270751412Sobrien} 270851412Sobrien 270990287Sobrien/* Resolve `CONTEXT::template NAME'. Returns an appropriate type, 271090287Sobrien unless an error occurs, in which case error_mark_node is returned. 271196292Sobrien If we locate a TYPE_DECL, we return that, rather than the _TYPE it 271296292Sobrien corresponds to. If COMPLAIN zero, don't complain about any errors 271396292Sobrien that occur. */ 271490287Sobrien 271590287Sobrientree 2716132747Skanmake_unbound_class_template (tree context, tree name, tsubst_flags_t complain) 271790287Sobrien{ 271890287Sobrien tree t; 271990287Sobrien tree d; 272090287Sobrien 272190287Sobrien if (TYPE_P (name)) 272290287Sobrien name = TYPE_IDENTIFIER (name); 272390287Sobrien else if (DECL_P (name)) 272490287Sobrien name = DECL_NAME (name); 272590287Sobrien if (TREE_CODE (name) != IDENTIFIER_NODE) 272690287Sobrien abort (); 272790287Sobrien 2728132747Skan if (!dependent_type_p (context) 272990287Sobrien || currently_open_class (context)) 273090287Sobrien { 273190287Sobrien tree tmpl = NULL_TREE; 273290287Sobrien 273390287Sobrien if (IS_AGGR_TYPE (context)) 2734132747Skan tmpl = lookup_field (context, name, 0, false); 273590287Sobrien 273690287Sobrien if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) 273790287Sobrien { 2738117410Skan if (complain & tf_error) 273990287Sobrien error ("no class template named `%#T' in `%#T'", name, context); 274090287Sobrien return error_mark_node; 274190287Sobrien } 274290287Sobrien 2743117410Skan if (complain & tf_error) 2744132747Skan perform_or_defer_access_check (TYPE_BINFO (context), tmpl); 274590287Sobrien 274690287Sobrien return tmpl; 274790287Sobrien } 274890287Sobrien 274990287Sobrien /* Build the UNBOUND_CLASS_TEMPLATE. */ 275090287Sobrien t = make_aggr_type (UNBOUND_CLASS_TEMPLATE); 275190287Sobrien TYPE_CONTEXT (t) = FROB_CONTEXT (context); 275290287Sobrien TREE_TYPE (t) = NULL_TREE; 275390287Sobrien 275490287Sobrien /* Build the corresponding TEMPLATE_DECL. */ 275590287Sobrien d = build_decl (TEMPLATE_DECL, name, t); 275690287Sobrien TYPE_NAME (TREE_TYPE (d)) = d; 275790287Sobrien TYPE_STUB_DECL (TREE_TYPE (d)) = d; 275890287Sobrien DECL_CONTEXT (d) = FROB_CONTEXT (context); 275990287Sobrien DECL_ARTIFICIAL (d) = 1; 276090287Sobrien 276190287Sobrien return t; 276290287Sobrien} 276390287Sobrien 2764132747Skan 276551412Sobrien 2766132747Skan/* A chain of TYPE_DECLs for the builtin types. */ 2767117410Skan 2768132747Skanstatic GTY(()) tree builtin_type_decls; 276990287Sobrien 2770132747Skan/* Return a chain of TYPE_DECLs for the builtin types. */ 277190287Sobrien 277290287Sobrientree 2773132747Skancxx_builtin_type_decls (void) 277451412Sobrien{ 2775132747Skan return builtin_type_decls; 277651412Sobrien} 277751412Sobrien 277818334Speter/* Push the declarations of builtin types into the namespace. 2779132747Skan RID_INDEX is the index of the builtin type in the array 2780132747Skan RID_POINTERS. NAME is the name used when looking up the builtin 2781132747Skan type. TYPE is the _TYPE node for the builtin type. */ 278218334Speter 278390287Sobrienvoid 2784132747Skanrecord_builtin_type (enum rid rid_index, 2785132747Skan const char* name, 2786132747Skan tree type) 278718334Speter{ 278818334Speter tree rname = NULL_TREE, tname = NULL_TREE; 278951412Sobrien tree tdecl = NULL_TREE; 279018334Speter 279118334Speter if ((int) rid_index < (int) RID_MAX) 279218334Speter rname = ridpointers[(int) rid_index]; 279318334Speter if (name) 279418334Speter tname = get_identifier (name); 279518334Speter 2796132747Skan /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be 2797132747Skan eliminated. Built-in types should not be looked up name; their 2798132747Skan names are keywords that the parser can recognize. However, there 2799132747Skan is code in c-common.c that uses identifier_global_value to look 2800132747Skan up built-in types by name. */ 280118334Speter if (tname) 280218334Speter { 2803132747Skan tdecl = build_decl (TYPE_DECL, tname, type); 2804132747Skan DECL_ARTIFICIAL (tdecl) = 1; 2805132747Skan SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl); 280618334Speter } 2807132747Skan if (rname) 280818334Speter { 2809132747Skan if (!tdecl) 281018334Speter { 2811132747Skan tdecl = build_decl (TYPE_DECL, rname, type); 2812132747Skan DECL_ARTIFICIAL (tdecl) = 1; 281318334Speter } 2814132747Skan SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl); 281518334Speter } 2816132747Skan 2817132747Skan if (!TYPE_NAME (type)) 2818132747Skan TYPE_NAME (type) = tdecl; 2819132747Skan 2820132747Skan if (tdecl) 2821132747Skan { 2822132747Skan TREE_CHAIN (tdecl) = builtin_type_decls; 2823132747Skan builtin_type_decls = tdecl; 2824132747Skan } 282551412Sobrien} 282618334Speter 282751412Sobrien/* Record one of the standard Java types. 282851412Sobrien * Declare it as having the given NAME. 282951412Sobrien * If SIZE > 0, it is the size of one of the integral types; 283051412Sobrien * otherwise it is the negative of the size of one of the other types. */ 283151412Sobrien 283251412Sobrienstatic tree 2833132747Skanrecord_builtin_java_type (const char* name, int size) 283451412Sobrien{ 283551412Sobrien tree type, decl; 283651412Sobrien if (size > 0) 283751412Sobrien type = make_signed_type (size); 283851412Sobrien else if (size > -32) 2839117410Skan { /* "__java_char" or ""__java_boolean". */ 284051412Sobrien type = make_unsigned_type (-size); 284151412Sobrien /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/ 284218334Speter } 284351412Sobrien else 2844117410Skan { /* "__java_float" or ""__java_double". */ 284551412Sobrien type = make_node (REAL_TYPE); 284651412Sobrien TYPE_PRECISION (type) = - size; 284751412Sobrien layout_type (type); 284851412Sobrien } 284951412Sobrien record_builtin_type (RID_MAX, name, type); 285051412Sobrien decl = TYPE_NAME (type); 285152290Sobrien 285252290Sobrien /* Suppress generate debug symbol entries for these types, 285352290Sobrien since for normal C++ they are just clutter. 2854117410Skan However, push_lang_context undoes this if extern "Java" is seen. */ 285551412Sobrien DECL_IGNORED_P (decl) = 1; 285652290Sobrien 285751412Sobrien TYPE_FOR_JAVA (type) = 1; 285851412Sobrien return type; 285918334Speter} 286018334Speter 2861117410Skan/* Push a type into the namespace so that the back-ends ignore it. */ 286251412Sobrien 286318334Speterstatic void 2864132747Skanrecord_unknown_type (tree type, const char* name) 286518334Speter{ 286651412Sobrien tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type)); 286751412Sobrien /* Make sure the "unknown type" typedecl gets ignored for debug info. */ 286851412Sobrien DECL_IGNORED_P (decl) = 1; 286951412Sobrien TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 287051412Sobrien TYPE_SIZE (type) = TYPE_SIZE (void_type_node); 287151412Sobrien TYPE_ALIGN (type) = 1; 287290287Sobrien TYPE_USER_ALIGN (type) = 0; 287351412Sobrien TYPE_MODE (type) = TYPE_MODE (void_type_node); 287490287Sobrien} 287518334Speter 287690287Sobrien/* An string for which we should create an IDENTIFIER_NODE at 287790287Sobrien startup. */ 287851412Sobrien 287990287Sobrientypedef struct predefined_identifier 288090287Sobrien{ 288190287Sobrien /* The name of the identifier. */ 288290287Sobrien const char *const name; 288390287Sobrien /* The place where the IDENTIFIER_NODE should be stored. */ 288490287Sobrien tree *const node; 2885117410Skan /* Nonzero if this is the name of a constructor or destructor. */ 288690287Sobrien const int ctor_or_dtor_p; 288790287Sobrien} predefined_identifier; 288890287Sobrien 288990287Sobrien/* Create all the predefined identifiers. */ 289090287Sobrien 289118334Speterstatic void 2892132747Skaninitialize_predefined_identifiers (void) 289318334Speter{ 289490287Sobrien const predefined_identifier *pid; 289518334Speter 289690287Sobrien /* A table of identifiers to create at startup. */ 289790287Sobrien static const predefined_identifier predefined_identifiers[] = { 289890287Sobrien { "C++", &lang_name_cplusplus, 0 }, 289990287Sobrien { "C", &lang_name_c, 0 }, 290090287Sobrien { "Java", &lang_name_java, 0 }, 290190287Sobrien { CTOR_NAME, &ctor_identifier, 1 }, 290290287Sobrien { "__base_ctor", &base_ctor_identifier, 1 }, 290390287Sobrien { "__comp_ctor", &complete_ctor_identifier, 1 }, 290490287Sobrien { DTOR_NAME, &dtor_identifier, 1 }, 290590287Sobrien { "__comp_dtor", &complete_dtor_identifier, 1 }, 290690287Sobrien { "__base_dtor", &base_dtor_identifier, 1 }, 290790287Sobrien { "__deleting_dtor", &deleting_dtor_identifier, 1 }, 290890287Sobrien { IN_CHARGE_NAME, &in_charge_identifier, 0 }, 290990287Sobrien { "nelts", &nelts_identifier, 0 }, 291090287Sobrien { THIS_NAME, &this_identifier, 0 }, 291190287Sobrien { VTABLE_DELTA_NAME, &delta_identifier, 0 }, 291290287Sobrien { VTABLE_PFN_NAME, &pfn_identifier, 0 }, 291390287Sobrien { "_vptr", &vptr_identifier, 0 }, 291490287Sobrien { "__vtt_parm", &vtt_parm_identifier, 0 }, 2915132747Skan { "::", &global_scope_name, 0 }, 291690287Sobrien { "std", &std_identifier, 0 }, 291790287Sobrien { NULL, NULL, 0 } 291890287Sobrien }; 291990287Sobrien 292090287Sobrien for (pid = predefined_identifiers; pid->name; ++pid) 292190287Sobrien { 292290287Sobrien *pid->node = get_identifier (pid->name); 292390287Sobrien if (pid->ctor_or_dtor_p) 292490287Sobrien IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1; 292590287Sobrien } 292618334Speter} 292718334Speter 292818334Speter/* Create the predefined scalar types of C, 292918334Speter and some nodes representing standard constants (0, 1, (void *)0). 293018334Speter Initialize the global binding level. 293118334Speter Make definitions for built-in primitive functions. */ 293218334Speter 293318334Spetervoid 2934132747Skancxx_init_decl_processing (void) 293518334Speter{ 293690287Sobrien tree void_ftype; 293790287Sobrien tree void_ftype_ptr; 293818334Speter 293990287Sobrien /* Create all the identifiers we need. */ 294090287Sobrien initialize_predefined_identifiers (); 294118334Speter 294290287Sobrien /* Fill in back-end hooks. */ 294390287Sobrien lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p; 294490287Sobrien 294590287Sobrien /* Create the global variables. */ 294690287Sobrien push_to_top_level (); 294790287Sobrien 2948132747Skan current_function_decl = NULL_TREE; 2949132747Skan current_binding_level = NULL; 2950117410Skan /* Enter the global namespace. */ 295151412Sobrien my_friendly_assert (global_namespace == NULL_TREE, 375); 2952132747Skan global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name, 2953132747Skan void_type_node); 2954132747Skan begin_scope (sk_namespace, global_namespace); 2955132747Skan 295651412Sobrien current_lang_name = NULL_TREE; 295751412Sobrien 295890287Sobrien /* Adjust various flags based on command-line settings. */ 2959117410Skan if (!flag_permissive) 296052290Sobrien flag_pedantic_errors = 1; 296190287Sobrien if (!flag_no_inline) 296290287Sobrien { 296390287Sobrien flag_inline_trees = 1; 296490287Sobrien flag_no_inline = 1; 296590287Sobrien } 296690287Sobrien if (flag_inline_functions) 296790287Sobrien { 296890287Sobrien flag_inline_trees = 2; 296990287Sobrien flag_inline_functions = 0; 297090287Sobrien } 297118334Speter 2972104769Skan /* Force minimum function alignment if using the least significant 2973104769Skan bit of function pointers to store the virtual bit. */ 2974104769Skan if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn 2975104769Skan && force_align_functions_log < 1) 2976104769Skan force_align_functions_log = 1; 2977104769Skan 297818334Speter /* Initially, C. */ 297918334Speter current_lang_name = lang_name_c; 298018334Speter 298190287Sobrien build_common_tree_nodes (flag_signed_char); 298218334Speter 298318334Speter error_mark_list = build_tree_list (error_mark_node, error_mark_node); 298418334Speter TREE_TYPE (error_mark_list) = error_mark_node; 298518334Speter 298690287Sobrien /* Create the `std' namespace. */ 298790287Sobrien push_namespace (std_identifier); 298890287Sobrien std_node = current_namespace; 298990287Sobrien pop_namespace (); 299018334Speter 299190287Sobrien c_common_nodes_and_builtins (); 299218334Speter 299351412Sobrien java_byte_type_node = record_builtin_java_type ("__java_byte", 8); 299451412Sobrien java_short_type_node = record_builtin_java_type ("__java_short", 16); 299551412Sobrien java_int_type_node = record_builtin_java_type ("__java_int", 32); 299651412Sobrien java_long_type_node = record_builtin_java_type ("__java_long", 64); 299751412Sobrien java_float_type_node = record_builtin_java_type ("__java_float", -32); 299851412Sobrien java_double_type_node = record_builtin_java_type ("__java_double", -64); 299951412Sobrien java_char_type_node = record_builtin_java_type ("__java_char", -16); 300051412Sobrien java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1); 300151412Sobrien 300218334Speter integer_two_node = build_int_2 (2, 0); 300318334Speter TREE_TYPE (integer_two_node) = integer_type_node; 300418334Speter integer_three_node = build_int_2 (3, 0); 300518334Speter TREE_TYPE (integer_three_node) = integer_type_node; 300618334Speter 300718334Speter record_builtin_type (RID_BOOL, "bool", boolean_type_node); 3008132747Skan truthvalue_type_node = boolean_type_node; 3009132747Skan truthvalue_false_node = boolean_false_node; 3010132747Skan truthvalue_true_node = boolean_true_node; 301118334Speter 301290287Sobrien empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); 301318334Speter 301451412Sobrien#if 0 301590287Sobrien record_builtin_type (RID_MAX, NULL, string_type_node); 301651412Sobrien#endif 301718334Speter 301890287Sobrien delta_type_node = ptrdiff_type_node; 301990287Sobrien vtable_index_type = ptrdiff_type_node; 302018334Speter 302190287Sobrien vtt_parm_type = build_pointer_type (const_ptr_type_node); 302290287Sobrien void_ftype = build_function_type (void_type_node, void_list_node); 302390287Sobrien void_ftype_ptr = build_function_type (void_type_node, 302490287Sobrien tree_cons (NULL_TREE, 302590287Sobrien ptr_type_node, 302690287Sobrien void_list_node)); 302751412Sobrien void_ftype_ptr 302890287Sobrien = build_exception_variant (void_ftype_ptr, empty_except_spec); 302951412Sobrien 303018334Speter /* C++ extensions */ 303118334Speter 303218334Speter unknown_type_node = make_node (UNKNOWN_TYPE); 303351412Sobrien record_unknown_type (unknown_type_node, "unknown type"); 303451412Sobrien 303518334Speter /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ 303618334Speter TREE_TYPE (unknown_type_node) = unknown_type_node; 303751412Sobrien 303851412Sobrien /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same 303951412Sobrien result. */ 304018334Speter TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; 304118334Speter TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; 304218334Speter 304390287Sobrien { 304490287Sobrien /* Make sure we get a unique function type, so we can give 304590287Sobrien its pointer type a name. (This wins for gdb.) */ 304690287Sobrien tree vfunc_type = make_node (FUNCTION_TYPE); 304790287Sobrien TREE_TYPE (vfunc_type) = integer_type_node; 304890287Sobrien TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; 304990287Sobrien layout_type (vfunc_type); 305018334Speter 305190287Sobrien vtable_entry_type = build_pointer_type (vfunc_type); 305290287Sobrien } 305318334Speter record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type); 305418334Speter 305518334Speter vtbl_type_node 305652290Sobrien = build_cplus_array_type (vtable_entry_type, NULL_TREE); 305718334Speter layout_type (vtbl_type_node); 305852290Sobrien vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); 305990287Sobrien record_builtin_type (RID_MAX, NULL, vtbl_type_node); 306052290Sobrien vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); 306152290Sobrien layout_type (vtbl_ptr_type_node); 306290287Sobrien record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); 306318334Speter 306490287Sobrien push_namespace (get_identifier ("__cxxabiv1")); 306590287Sobrien abi_node = current_namespace; 306690287Sobrien pop_namespace (); 306760970Sobrien 306851412Sobrien global_type_node = make_node (LANG_TYPE); 306951412Sobrien record_unknown_type (global_type_node, "global type"); 307018334Speter 307118334Speter /* Now, C++. */ 307218334Speter current_lang_name = lang_name_cplusplus; 307318334Speter 307451412Sobrien { 3075132747Skan tree bad_alloc_id; 3076132747Skan tree bad_alloc_type_node; 3077132747Skan tree bad_alloc_decl; 3078132747Skan tree newtype, deltype; 307990287Sobrien tree ptr_ftype_sizetype; 308090287Sobrien 308190287Sobrien push_namespace (std_identifier); 3082132747Skan bad_alloc_id = get_identifier ("bad_alloc"); 3083132747Skan bad_alloc_type_node = make_aggr_type (RECORD_TYPE); 3084132747Skan TYPE_CONTEXT (bad_alloc_type_node) = current_namespace; 3085132747Skan bad_alloc_decl 3086132747Skan = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node); 3087132747Skan DECL_CONTEXT (bad_alloc_decl) = current_namespace; 3088132747Skan TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl; 308990287Sobrien pop_namespace (); 3090132747Skan 309190287Sobrien ptr_ftype_sizetype 309290287Sobrien = build_function_type (ptr_type_node, 309390287Sobrien tree_cons (NULL_TREE, 3094110631Skan size_type_node, 309590287Sobrien void_list_node)); 309651412Sobrien newtype = build_exception_variant 309790287Sobrien (ptr_ftype_sizetype, add_exception_specifier 309890287Sobrien (NULL_TREE, bad_alloc_type_node, -1)); 309990287Sobrien deltype = build_exception_variant (void_ftype_ptr, empty_except_spec); 310090287Sobrien push_cp_library_fn (NEW_EXPR, newtype); 310190287Sobrien push_cp_library_fn (VEC_NEW_EXPR, newtype); 310290287Sobrien global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype); 310390287Sobrien push_cp_library_fn (VEC_DELETE_EXPR, deltype); 310451412Sobrien } 310518334Speter 310618334Speter abort_fndecl 310790287Sobrien = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype); 310818334Speter 310918334Speter /* Perform other language dependent initializations. */ 311018334Speter init_class_processing (); 311118334Speter init_search_processing (); 311290287Sobrien init_rtti_processing (); 311318334Speter 311451412Sobrien if (flag_exceptions) 311518334Speter init_exception_processing (); 311618334Speter 311751412Sobrien if (! supports_one_only ()) 311851412Sobrien flag_weak = 0; 311951412Sobrien 312090287Sobrien make_fname_decl = cp_make_fname_decl; 312190287Sobrien start_fname_decls (); 312218334Speter 312351412Sobrien /* Show we use EH for cleanups. */ 312451412Sobrien using_eh_for_cleanups (); 312551412Sobrien 312652290Sobrien /* Maintain consistency. Perhaps we should just complain if they 312752290Sobrien say -fwritable-strings? */ 312852290Sobrien if (flag_writable_strings) 312952290Sobrien flag_const_strings = 0; 313018334Speter} 313118334Speter 313290287Sobrien/* Generate an initializer for a function naming variable from 3133132747Skan NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is 3134132747Skan filled in with the type of the init. */ 313518334Speter 313690287Sobrientree 3137132747Skancp_fname_init (const char* name, tree *type_p) 313818334Speter{ 313990287Sobrien tree domain = NULL_TREE; 314090287Sobrien tree type; 314190287Sobrien tree init = NULL_TREE; 314290287Sobrien size_t length = 0; 314390287Sobrien 314490287Sobrien if (name) 314590287Sobrien { 314690287Sobrien length = strlen (name); 314790287Sobrien domain = build_index_type (size_int (length)); 314890287Sobrien init = build_string (length + 1, name); 314990287Sobrien } 315090287Sobrien 315190287Sobrien type = build_qualified_type (char_type_node, TYPE_QUAL_CONST); 315290287Sobrien type = build_cplus_array_type (type, domain); 315390287Sobrien 3154132747Skan *type_p = type; 3155132747Skan 315690287Sobrien if (init) 315790287Sobrien TREE_TYPE (init) = type; 315890287Sobrien else 3159132747Skan init = error_mark_node; 316090287Sobrien 316190287Sobrien return init; 316251412Sobrien} 316318334Speter 316490287Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the 316590287Sobrien decl, NAME is the initialization string and TYPE_DEP indicates whether 316690287Sobrien NAME depended on the type of the function. We make use of that to detect 316790287Sobrien __PRETTY_FUNCTION__ inside a template fn. This is being done 316890287Sobrien lazily at the point of first use, so we musn't push the decl now. */ 316990287Sobrien 317090287Sobrienstatic tree 3171132747Skancp_make_fname_decl (tree id, int type_dep) 317290287Sobrien{ 317390287Sobrien const char *const name = (type_dep && processing_template_decl 3174132747Skan ? NULL : fname_as_string (type_dep)); 3175132747Skan tree type; 3176132747Skan tree init = cp_fname_init (name, &type); 3177132747Skan tree decl = build_decl (VAR_DECL, id, type); 317890287Sobrien 3179132747Skan /* As we're using pushdecl_with_scope, we must set the context. */ 318090287Sobrien DECL_CONTEXT (decl) = current_function_decl; 318190287Sobrien DECL_PRETTY_FUNCTION_P (decl) = type_dep; 318290287Sobrien 318390287Sobrien TREE_STATIC (decl) = 1; 318490287Sobrien TREE_READONLY (decl) = 1; 318590287Sobrien DECL_ARTIFICIAL (decl) = 1; 318690287Sobrien DECL_INITIAL (decl) = init; 318790287Sobrien 318890287Sobrien TREE_USED (decl) = 1; 318990287Sobrien 3190132747Skan if (current_function_decl) 3191132747Skan { 3192132747Skan struct cp_binding_level *b = current_binding_level; 3193132747Skan while (b->level_chain->kind != sk_function_parms) 3194132747Skan b = b->level_chain; 3195132747Skan pushdecl_with_scope (decl, b); 3196132747Skan cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING); 3197132747Skan } 3198132747Skan else 3199132747Skan pushdecl_top_level_and_finish (decl, init); 320090287Sobrien 320190287Sobrien return decl; 320290287Sobrien} 320390287Sobrien 320496292Sobrien/* Make a definition for a builtin function named NAME in the current 320596292Sobrien namespace, whose data type is TYPE and whose context is CONTEXT. 320696292Sobrien TYPE should be a function type with argument types. 320790287Sobrien 320890287Sobrien CLASS and CODE tell later passes how to compile calls to this function. 320990287Sobrien See tree.h for possible values. 321090287Sobrien 321190287Sobrien If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, 3212117410Skan the name to be called if we can't opencode the function. 3213117410Skan If ATTRS is nonzero, use that for the function's attribute 3214117410Skan list. */ 321518334Speter 321696292Sobrienstatic tree 3217132747Skanbuiltin_function_1 (const char* name, 3218132747Skan tree type, 3219132747Skan tree context, 3220132747Skan int code, 3221132747Skan enum built_in_class class, 3222132747Skan const char* libname, 3223132747Skan tree attrs) 322418334Speter{ 322590287Sobrien tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type); 322690287Sobrien DECL_BUILT_IN_CLASS (decl) = class; 322790287Sobrien DECL_FUNCTION_CODE (decl) = code; 322896292Sobrien DECL_CONTEXT (decl) = context; 322918334Speter 323090287Sobrien pushdecl (decl); 323190287Sobrien 323218334Speter /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME, 323318334Speter we cannot change DECL_ASSEMBLER_NAME until we have installed this 323418334Speter function in the namespace. */ 323590287Sobrien if (libname) 323690287Sobrien SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname)); 323790287Sobrien make_decl_rtl (decl, NULL); 323890287Sobrien 323990287Sobrien /* Warn if a function in the namespace for users 324090287Sobrien is used without an occasion to consider it declared. */ 324190287Sobrien if (name[0] != '_' || name[1] != '_') 324290287Sobrien DECL_ANTICIPATED (decl) = 1; 324390287Sobrien 324490287Sobrien /* Possibly apply some default attributes to this built-in function. */ 3245117410Skan if (attrs) 3246117410Skan decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); 3247117410Skan else 3248117410Skan decl_attributes (&decl, NULL_TREE, 0); 324990287Sobrien 325018334Speter return decl; 325118334Speter} 325290287Sobrien 325396292Sobrien/* Entry point for the benefit of c_common_nodes_and_builtins. 325496292Sobrien 3255132747Skan Make a definition for a builtin function named NAME and whose data type 325696292Sobrien is TYPE. TYPE should be a function type with argument types. This 325796292Sobrien function places the anticipated declaration in the global namespace 325896292Sobrien and additionally in the std namespace if appropriate. 325996292Sobrien 326096292Sobrien CLASS and CODE tell later passes how to compile calls to this function. 326196292Sobrien See tree.h for possible values. 326296292Sobrien 326396292Sobrien If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, 3264117410Skan the name to be called if we can't opencode the function. 326596292Sobrien 3266117410Skan If ATTRS is nonzero, use that for the function's attribute 3267117410Skan list. */ 3268117410Skan 326996292Sobrientree 3270132747Skanbuiltin_function (const char* name, 3271132747Skan tree type, 3272132747Skan int code, 3273132747Skan enum built_in_class class, 3274132747Skan const char* libname, 3275132747Skan tree attrs) 327696292Sobrien{ 327796292Sobrien /* All builtins that don't begin with an '_' should additionally 327896292Sobrien go in the 'std' namespace. */ 327996292Sobrien if (name[0] != '_') 328096292Sobrien { 328196292Sobrien push_namespace (std_identifier); 3282117410Skan builtin_function_1 (name, type, std_node, code, class, libname, attrs); 328396292Sobrien pop_namespace (); 328496292Sobrien } 328596292Sobrien 3286117410Skan return builtin_function_1 (name, type, NULL_TREE, code, 3287117410Skan class, libname, attrs); 328896292Sobrien} 328996292Sobrien 329090287Sobrien/* Generate a FUNCTION_DECL with the typical flags for a runtime library 329190287Sobrien function. Not called directly. */ 329290287Sobrien 329390287Sobrienstatic tree 3294132747Skanbuild_library_fn_1 (tree name, enum tree_code operator_code, tree type) 329590287Sobrien{ 329690287Sobrien tree fn = build_lang_decl (FUNCTION_DECL, name, type); 329790287Sobrien DECL_EXTERNAL (fn) = 1; 329890287Sobrien TREE_PUBLIC (fn) = 1; 329990287Sobrien DECL_ARTIFICIAL (fn) = 1; 330090287Sobrien TREE_NOTHROW (fn) = 1; 330190287Sobrien SET_OVERLOADED_OPERATOR_CODE (fn, operator_code); 330290287Sobrien SET_DECL_LANGUAGE (fn, lang_c); 330390287Sobrien return fn; 330490287Sobrien} 330590287Sobrien 330690287Sobrien/* Returns the _DECL for a library function with C linkage. 330790287Sobrien We assume that such functions never throw; if this is incorrect, 330890287Sobrien callers should unset TREE_NOTHROW. */ 330990287Sobrien 331090287Sobrientree 3311132747Skanbuild_library_fn (tree name, tree type) 331290287Sobrien{ 331390287Sobrien return build_library_fn_1 (name, ERROR_MARK, type); 331490287Sobrien} 331590287Sobrien 331690287Sobrien/* Returns the _DECL for a library function with C++ linkage. */ 331790287Sobrien 331890287Sobrienstatic tree 3319132747Skanbuild_cp_library_fn (tree name, enum tree_code operator_code, tree type) 332090287Sobrien{ 332190287Sobrien tree fn = build_library_fn_1 (name, operator_code, type); 332290287Sobrien TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type); 332390287Sobrien DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); 332490287Sobrien SET_DECL_LANGUAGE (fn, lang_cplusplus); 332590287Sobrien set_mangled_name_for_decl (fn); 332690287Sobrien return fn; 332790287Sobrien} 332890287Sobrien 332990287Sobrien/* Like build_library_fn, but takes a C string instead of an 333090287Sobrien IDENTIFIER_NODE. */ 333190287Sobrien 333290287Sobrientree 3333132747Skanbuild_library_fn_ptr (const char* name, tree type) 333490287Sobrien{ 333590287Sobrien return build_library_fn (get_identifier (name), type); 333690287Sobrien} 333790287Sobrien 333890287Sobrien/* Like build_cp_library_fn, but takes a C string instead of an 333990287Sobrien IDENTIFIER_NODE. */ 334090287Sobrien 334190287Sobrientree 3342132747Skanbuild_cp_library_fn_ptr (const char* name, tree type) 334390287Sobrien{ 334490287Sobrien return build_cp_library_fn (get_identifier (name), ERROR_MARK, type); 334590287Sobrien} 334690287Sobrien 334790287Sobrien/* Like build_library_fn, but also pushes the function so that we will 334890287Sobrien be able to find it via IDENTIFIER_GLOBAL_VALUE. */ 334990287Sobrien 335090287Sobrientree 3351132747Skanpush_library_fn (tree name, tree type) 335290287Sobrien{ 335390287Sobrien tree fn = build_library_fn (name, type); 335490287Sobrien pushdecl_top_level (fn); 335590287Sobrien return fn; 335690287Sobrien} 335790287Sobrien 335890287Sobrien/* Like build_cp_library_fn, but also pushes the function so that it 335990287Sobrien will be found by normal lookup. */ 336090287Sobrien 336190287Sobrienstatic tree 3362132747Skanpush_cp_library_fn (enum tree_code operator_code, tree type) 336390287Sobrien{ 336490287Sobrien tree fn = build_cp_library_fn (ansi_opname (operator_code), 336590287Sobrien operator_code, 336690287Sobrien type); 336790287Sobrien pushdecl (fn); 336890287Sobrien return fn; 336990287Sobrien} 337090287Sobrien 337190287Sobrien/* Like push_library_fn, but takes a TREE_LIST of parm types rather than 337290287Sobrien a FUNCTION_TYPE. */ 337390287Sobrien 337490287Sobrientree 3375132747Skanpush_void_library_fn (tree name, tree parmtypes) 337690287Sobrien{ 337790287Sobrien tree type = build_function_type (void_type_node, parmtypes); 337890287Sobrien return push_library_fn (name, type); 337990287Sobrien} 338090287Sobrien 338190287Sobrien/* Like push_library_fn, but also note that this function throws 338290287Sobrien and does not return. Used for __throw_foo and the like. */ 338390287Sobrien 338490287Sobrientree 3385132747Skanpush_throw_library_fn (tree name, tree type) 338690287Sobrien{ 338790287Sobrien tree fn = push_library_fn (name, type); 338890287Sobrien TREE_THIS_VOLATILE (fn) = 1; 338990287Sobrien TREE_NOTHROW (fn) = 0; 339090287Sobrien return fn; 339190287Sobrien} 339218334Speter 339352290Sobrien/* When we call finish_struct for an anonymous union, we create 339452290Sobrien default copy constructors and such. But, an anonymous union 339552290Sobrien shouldn't have such things; this function undoes the damage to the 339652290Sobrien anonymous union type T. 339718334Speter 339852290Sobrien (The reason that we create the synthesized methods is that we don't 339952290Sobrien distinguish `union { int i; }' from `typedef union { int i; } U'. 340052290Sobrien The first is an anonymous union; the second is just an ordinary 340152290Sobrien union type.) */ 340218334Speter 340318334Spetervoid 3404132747Skanfixup_anonymous_aggr (tree t) 340552290Sobrien{ 340652290Sobrien tree *q; 340752290Sobrien 3408132747Skan /* Wipe out memory of synthesized methods. */ 340952290Sobrien TYPE_HAS_CONSTRUCTOR (t) = 0; 341052290Sobrien TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; 341152290Sobrien TYPE_HAS_INIT_REF (t) = 0; 341252290Sobrien TYPE_HAS_CONST_INIT_REF (t) = 0; 341352290Sobrien TYPE_HAS_ASSIGN_REF (t) = 0; 341452290Sobrien TYPE_HAS_CONST_ASSIGN_REF (t) = 0; 341552290Sobrien 341652290Sobrien /* Splice the implicitly generated functions out of the TYPE_METHODS 341752290Sobrien list. */ 341852290Sobrien q = &TYPE_METHODS (t); 341952290Sobrien while (*q) 342052290Sobrien { 342152290Sobrien if (DECL_ARTIFICIAL (*q)) 342252290Sobrien *q = TREE_CHAIN (*q); 342352290Sobrien else 342452290Sobrien q = &TREE_CHAIN (*q); 342552290Sobrien } 342652290Sobrien 342790287Sobrien /* ISO C++ 9.5.3. Anonymous unions may not have function members. */ 342852290Sobrien if (TYPE_METHODS (t)) 3429132747Skan error ("%Jan anonymous union cannot have function members", 3430132747Skan TYPE_MAIN_DECL (t)); 343190287Sobrien 343290287Sobrien /* Anonymous aggregates cannot have fields with ctors, dtors or complex 343390287Sobrien assignment operators (because they cannot have these methods themselves). 343490287Sobrien For anonymous unions this is already checked because they are not allowed 343590287Sobrien in any union, otherwise we have to check it. */ 343690287Sobrien if (TREE_CODE (t) != UNION_TYPE) 343790287Sobrien { 343890287Sobrien tree field, type; 343990287Sobrien 344090287Sobrien for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 344190287Sobrien if (TREE_CODE (field) == FIELD_DECL) 344290287Sobrien { 344390287Sobrien type = TREE_TYPE (field); 344490287Sobrien if (CLASS_TYPE_P (type)) 344590287Sobrien { 344690287Sobrien if (TYPE_NEEDS_CONSTRUCTING (type)) 344790287Sobrien cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate", 344890287Sobrien field); 344990287Sobrien if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 345090287Sobrien cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate", 345190287Sobrien field); 345290287Sobrien if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) 345390287Sobrien cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate", 345490287Sobrien field); 345590287Sobrien } 345690287Sobrien } 345790287Sobrien } 345852290Sobrien} 345952290Sobrien 346052290Sobrien/* Make sure that a declaration with no declarator is well-formed, i.e. 3461132747Skan just declares a tagged type or anonymous union. 346252290Sobrien 3463132747Skan Returns the type declared; or NULL_TREE if none. */ 346452290Sobrien 346552290Sobrientree 3466132747Skancheck_tag_decl (tree declspecs) 346718334Speter{ 346852290Sobrien int found_type = 0; 346990287Sobrien int saw_friend = 0; 347090287Sobrien int saw_typedef = 0; 347118334Speter tree ob_modifier = NULL_TREE; 3472132747Skan tree link; 3473132747Skan /* If a class, struct, or enum type is declared by the DECLSPECS 3474132747Skan (i.e, if a class-specifier, enum-specifier, or non-typename 3475132747Skan elaborated-type-specifier appears in the DECLSPECS), 3476132747Skan DECLARED_TYPE is set to the corresponding type. */ 3477132747Skan tree declared_type = NULL_TREE; 3478132747Skan bool error_p = false; 347918334Speter 348018334Speter for (link = declspecs; link; link = TREE_CHAIN (link)) 348118334Speter { 3482132747Skan tree value = TREE_VALUE (link); 348318334Speter 3484132747Skan if (TYPE_P (value) || TREE_CODE (value) == TYPE_DECL 348590287Sobrien || (TREE_CODE (value) == IDENTIFIER_NODE 3486132747Skan && is_typename_at_global_scope (value))) 348718334Speter { 348852290Sobrien ++found_type; 348918334Speter 349090287Sobrien if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE) 349151412Sobrien { 349290287Sobrien if (! in_system_header) 349390287Sobrien pedwarn ("redeclaration of C++ built-in type `%T'", value); 349490287Sobrien return NULL_TREE; 349590287Sobrien } 349690287Sobrien 349790287Sobrien if (TYPE_P (value) 349890287Sobrien && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value)) 349990287Sobrien || TREE_CODE (value) == ENUMERAL_TYPE)) 350090287Sobrien { 350152290Sobrien my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261); 3502132747Skan declared_type = value; 350351412Sobrien } 350418334Speter } 350590287Sobrien else if (value == ridpointers[(int) RID_TYPEDEF]) 350690287Sobrien saw_typedef = 1; 350752290Sobrien else if (value == ridpointers[(int) RID_FRIEND]) 350852290Sobrien { 350952290Sobrien if (current_class_type == NULL_TREE 351052290Sobrien || current_scope () != current_class_type) 351152290Sobrien ob_modifier = value; 351290287Sobrien else 351390287Sobrien saw_friend = 1; 351452290Sobrien } 351518334Speter else if (value == ridpointers[(int) RID_STATIC] 351618334Speter || value == ridpointers[(int) RID_EXTERN] 351718334Speter || value == ridpointers[(int) RID_AUTO] 351818334Speter || value == ridpointers[(int) RID_REGISTER] 351918334Speter || value == ridpointers[(int) RID_INLINE] 352018334Speter || value == ridpointers[(int) RID_VIRTUAL] 352152290Sobrien || value == ridpointers[(int) RID_CONST] 352252290Sobrien || value == ridpointers[(int) RID_VOLATILE] 3523117410Skan || value == ridpointers[(int) RID_EXPLICIT] 3524117410Skan || value == ridpointers[(int) RID_THREAD]) 352518334Speter ob_modifier = value; 3526132747Skan else if (value == error_mark_node) 3527132747Skan error_p = true; 352818334Speter } 352918334Speter 353052290Sobrien if (found_type > 1) 353152290Sobrien error ("multiple types in one declaration"); 353252290Sobrien 3533132747Skan if (declared_type == NULL_TREE && ! saw_friend && !error_p) 353452290Sobrien pedwarn ("declaration does not declare anything"); 353590287Sobrien /* Check for an anonymous union. */ 3536132747Skan else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type)) 3537132747Skan && TYPE_ANONYMOUS_P (declared_type)) 353852290Sobrien { 353990287Sobrien /* 7/3 In a simple-declaration, the optional init-declarator-list 354090287Sobrien can be omitted only when declaring a class (clause 9) or 354190287Sobrien enumeration (7.2), that is, when the decl-specifier-seq contains 354290287Sobrien either a class-specifier, an elaborated-type-specifier with 354390287Sobrien a class-key (9.1), or an enum-specifier. In these cases and 354490287Sobrien whenever a class-specifier or enum-specifier is present in the 354590287Sobrien decl-specifier-seq, the identifiers in these specifiers are among 354690287Sobrien the names being declared by the declaration (as class-name, 354790287Sobrien enum-names, or enumerators, depending on the syntax). In such 354890287Sobrien cases, and except for the declaration of an unnamed bit-field (9.6), 354990287Sobrien the decl-specifier-seq shall introduce one or more names into the 355090287Sobrien program, or shall redeclare a name introduced by a previous 355190287Sobrien declaration. [Example: 355290287Sobrien enum { }; // ill-formed 355390287Sobrien typedef class { }; // ill-formed 355490287Sobrien --end example] */ 355590287Sobrien if (saw_typedef) 355690287Sobrien { 355790287Sobrien error ("missing type-name in typedef-declaration"); 355890287Sobrien return NULL_TREE; 355990287Sobrien } 356052290Sobrien /* Anonymous unions are objects, so they can have specifiers. */; 3561132747Skan SET_ANON_AGGR_TYPE_P (declared_type); 356290287Sobrien 3563132747Skan if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 3564132747Skan && !in_system_header) 356590287Sobrien pedwarn ("ISO C++ prohibits anonymous structs"); 356652290Sobrien } 356752290Sobrien 356852290Sobrien else if (ob_modifier) 356952290Sobrien { 357052290Sobrien if (ob_modifier == ridpointers[(int) RID_INLINE] 357152290Sobrien || ob_modifier == ridpointers[(int) RID_VIRTUAL]) 357290287Sobrien error ("`%D' can only be specified for functions", ob_modifier); 357352290Sobrien else if (ob_modifier == ridpointers[(int) RID_FRIEND]) 357490287Sobrien error ("`%D' can only be specified inside a class", ob_modifier); 357552290Sobrien else if (ob_modifier == ridpointers[(int) RID_EXPLICIT]) 357690287Sobrien error ("`%D' can only be specified for constructors", 357752290Sobrien ob_modifier); 357852290Sobrien else 357990287Sobrien error ("`%D' can only be specified for objects and functions", 358052290Sobrien ob_modifier); 358152290Sobrien } 358252290Sobrien 3583132747Skan return declared_type; 358452290Sobrien} 358552290Sobrien 358652290Sobrien/* Called when a declaration is seen that contains no names to declare. 358752290Sobrien If its type is a reference to a structure, union or enum inherited 358852290Sobrien from a containing scope, shadow that tag name for the current scope 358952290Sobrien with a forward reference. 359052290Sobrien If its type defines a new named structure or union 359152290Sobrien or defines an enum, it is valid but we need not do anything here. 359252290Sobrien Otherwise, it is an error. 359352290Sobrien 359452290Sobrien C++: may have to grok the declspecs to learn about static, 3595132747Skan complain for anonymous unions. 359652290Sobrien 3597132747Skan Returns the TYPE declared -- or NULL_TREE if none. */ 3598132747Skan 3599132747Skantree 3600132747Skanshadow_tag (tree declspecs) 360152290Sobrien{ 360252290Sobrien tree t = check_tag_decl (declspecs); 360352290Sobrien 3604132747Skan if (!t) 3605132747Skan return NULL_TREE; 360652290Sobrien 3607132747Skan maybe_process_partial_specialization (t); 3608132747Skan 360918334Speter /* This is where the variables in an anonymous union are 361018334Speter declared. An anonymous union declaration looks like: 361118334Speter union { ... } ; 361218334Speter because there is no declarator after the union, the parser 361318334Speter sends that declaration here. */ 3614132747Skan if (ANON_AGGR_TYPE_P (t)) 361518334Speter { 361690287Sobrien fixup_anonymous_aggr (t); 361751412Sobrien 361818334Speter if (TYPE_FIELDS (t)) 361918334Speter { 362018334Speter tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0, 362190287Sobrien NULL); 362218334Speter finish_anon_union (decl); 362318334Speter } 362418334Speter } 3625132747Skan 3626132747Skan return t; 362718334Speter} 362818334Speter 362918334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 363018334Speter 363118334Spetertree 3632132747Skangroktypename (tree typename) 363318334Speter{ 363490287Sobrien tree specs, attrs; 363590287Sobrien tree type; 363618334Speter if (TREE_CODE (typename) != TREE_LIST) 363718334Speter return typename; 363890287Sobrien split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs); 363990287Sobrien type = grokdeclarator (TREE_VALUE (typename), specs, 364090287Sobrien TYPENAME, 0, &attrs); 364190287Sobrien if (attrs) 364290287Sobrien cplus_decl_attributes (&type, attrs, 0); 364390287Sobrien return type; 364418334Speter} 364518334Speter 364618334Speter/* Decode a declarator in an ordinary declaration or data definition. 364718334Speter This is called as soon as the type information and variable name 364818334Speter have been parsed, before parsing the initializer if any. 364918334Speter Here we create the ..._DECL node, fill in its type, 365018334Speter and put it on the list of decls for the current context. 365118334Speter The ..._DECL node is returned as the value. 365218334Speter 365318334Speter Exception: for arrays where the length is not specified, 365418334Speter the type is left null, to be filled in by `cp_finish_decl'. 365518334Speter 365618334Speter Function definitions do not come here; they go to start_function 365718334Speter instead. However, external and forward declarations of functions 365818334Speter do go through here. Structure field declarations are done by 365918334Speter grokfield and not through here. */ 366018334Speter 366118334Spetertree 3662132747Skanstart_decl (tree declarator, 3663132747Skan tree declspecs, 3664132747Skan int initialized, 3665132747Skan tree attributes, 3666132747Skan tree prefix_attributes) 366718334Speter{ 366890287Sobrien tree decl; 3669132747Skan tree type, tem; 367018334Speter tree context; 367118334Speter 367251412Sobrien /* This should only be done once on the top most decl. */ 3673117410Skan if (have_extern_spec) 367418334Speter { 367590287Sobrien declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), 367690287Sobrien declspecs); 3677117410Skan have_extern_spec = false; 367818334Speter } 367918334Speter 368090287Sobrien /* An object declared as __attribute__((deprecated)) suppresses 368190287Sobrien warnings of uses of other deprecated items. */ 368290287Sobrien if (lookup_attribute ("deprecated", attributes)) 368390287Sobrien deprecated_state = DEPRECATED_SUPPRESS; 368452290Sobrien 368590287Sobrien attributes = chainon (attributes, prefix_attributes); 368690287Sobrien 368751412Sobrien decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, 368890287Sobrien &attributes); 368990287Sobrien 369090287Sobrien deprecated_state = DEPRECATED_NORMAL; 369190287Sobrien 369251412Sobrien if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE) 369318334Speter return NULL_TREE; 369418334Speter 369518334Speter type = TREE_TYPE (decl); 369618334Speter 369752290Sobrien if (type == error_mark_node) 369852290Sobrien return NULL_TREE; 369952290Sobrien 370090287Sobrien context = DECL_CONTEXT (decl); 370118334Speter 370251412Sobrien if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL 370351412Sobrien && context != current_namespace && TREE_CODE (decl) == VAR_DECL) 370418334Speter { 370551412Sobrien /* When parsing the initializer, lookup should use the object's 3706117410Skan namespace. */ 370751412Sobrien push_decl_namespace (context); 370818334Speter } 370918334Speter 3710117410Skan /* We are only interested in class contexts, later. */ 371151412Sobrien if (context && TREE_CODE (context) == NAMESPACE_DECL) 371251412Sobrien context = NULL_TREE; 371318334Speter 371418334Speter if (initialized) 371518334Speter /* Is it valid for this decl to have an initializer at all? 371618334Speter If not, set INITIALIZED to zero, which will indirectly 371718334Speter tell `cp_finish_decl' to ignore the initializer once it is parsed. */ 371818334Speter switch (TREE_CODE (decl)) 371918334Speter { 372018334Speter case TYPE_DECL: 3721107601Sobrien error ("typedef `%D' is initialized (use __typeof__ instead)", decl); 3722104769Skan initialized = 0; 372318334Speter break; 372418334Speter 372518334Speter case FUNCTION_DECL: 372690287Sobrien error ("function `%#D' is initialized like a variable", decl); 372718334Speter initialized = 0; 372818334Speter break; 372918334Speter 373018334Speter default: 373190287Sobrien break; 373218334Speter } 373318334Speter 373418334Speter if (initialized) 373518334Speter { 373618334Speter if (! toplevel_bindings_p () 373718334Speter && DECL_EXTERNAL (decl)) 373890287Sobrien warning ("declaration of `%#D' has `extern' and is initialized", 373918334Speter decl); 374018334Speter DECL_EXTERNAL (decl) = 0; 374151412Sobrien if (toplevel_bindings_p ()) 374218334Speter TREE_STATIC (decl) = 1; 374318334Speter 374418334Speter /* Tell `pushdecl' this is an initialized decl 374518334Speter even though we don't yet have the initializer expression. 374618334Speter Also tell `cp_finish_decl' it may store the real initializer. */ 374718334Speter DECL_INITIAL (decl) = error_mark_node; 374818334Speter } 374918334Speter 375052290Sobrien /* Set attributes here so if duplicate decl, will have proper attributes. */ 375190287Sobrien cplus_decl_attributes (&decl, attributes, 0); 375252290Sobrien 375396292Sobrien /* If #pragma weak was used, mark the decl weak now. */ 3754117410Skan if (global_scope_p (current_binding_level)) 375596292Sobrien maybe_apply_pragma_weak (decl); 375696292Sobrien 375790287Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 375890287Sobrien && DECL_DECLARED_INLINE_P (decl) 375990287Sobrien && DECL_UNINLINABLE (decl) 376090287Sobrien && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 3761132747Skan warning ("%Jinline function '%D' given attribute noinline", decl, decl); 376290287Sobrien 376390287Sobrien if (context && COMPLETE_TYPE_P (complete_type (context))) 376418334Speter { 3765132747Skan push_nested_class (context); 376652290Sobrien 376718334Speter if (TREE_CODE (decl) == VAR_DECL) 376818334Speter { 3769132747Skan tree field = lookup_field (context, DECL_NAME (decl), 0, false); 377018334Speter if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL) 377190287Sobrien error ("`%#D' is not a static member of `%#T'", decl, context); 377251412Sobrien else 377351412Sobrien { 377451412Sobrien if (DECL_CONTEXT (field) != context) 377551412Sobrien { 3776132747Skan if (!same_type_p (DECL_CONTEXT (field), context)) 3777132747Skan pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'", 3778132747Skan DECL_CONTEXT (field), DECL_NAME (decl), 3779132747Skan context, DECL_NAME (decl)); 378051412Sobrien DECL_CONTEXT (decl) = DECL_CONTEXT (field); 378151412Sobrien } 378251412Sobrien /* Static data member are tricky; an in-class initialization 378351412Sobrien still doesn't provide a definition, so the in-class 378451412Sobrien declaration will have DECL_EXTERNAL set, but will have an 378551412Sobrien initialization. Thus, duplicate_decls won't warn 378651412Sobrien about this situation, and so we check here. */ 378751412Sobrien if (DECL_INITIAL (decl) && DECL_INITIAL (field)) 378890287Sobrien error ("duplicate initialization of %D", decl); 378951412Sobrien if (duplicate_decls (decl, field)) 379051412Sobrien decl = field; 379151412Sobrien } 379218334Speter } 379318334Speter else 379418334Speter { 3795132747Skan tree field = check_classfn (context, decl, 3796132747Skan processing_template_decl 3797132747Skan > template_class_depth (context)); 379818334Speter if (field && duplicate_decls (decl, field)) 379918334Speter decl = field; 380018334Speter } 380118334Speter 380218334Speter /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ 380351412Sobrien DECL_IN_AGGR_P (decl) = 0; 380490287Sobrien if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 380590287Sobrien || CLASSTYPE_TEMPLATE_INSTANTIATION (context)) 380652290Sobrien { 380752290Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (decl); 380852290Sobrien /* [temp.expl.spec] An explicit specialization of a static data 380952290Sobrien member of a template is a definition if the declaration 381052290Sobrien includes an initializer; otherwise, it is a declaration. 381118334Speter 381252290Sobrien We check for processing_specialization so this only applies 381352290Sobrien to the new specialization syntax. */ 381452290Sobrien if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization) 381552290Sobrien DECL_EXTERNAL (decl) = 1; 381652290Sobrien } 381752290Sobrien 381851412Sobrien if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)) 381990287Sobrien pedwarn ("declaration of `%#D' outside of class is not definition", 382018334Speter decl); 382118334Speter } 382218334Speter 382390287Sobrien /* Enter this declaration into the symbol table. */ 382490287Sobrien tem = maybe_push_decl (decl); 382518334Speter 382651412Sobrien if (processing_template_decl) 382790287Sobrien tem = push_template_decl (tem); 3828132747Skan if (tem == error_mark_node) 3829132747Skan return error_mark_node; 383051412Sobrien 383151412Sobrien#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) 383251412Sobrien /* Tell the back-end to use or not use .common as appropriate. If we say 383351412Sobrien -fconserve-space, we want this to save .data space, at the expense of 383451412Sobrien wrong semantics. If we say -fno-conserve-space, we want this to 383551412Sobrien produce errors about redefs; to do this we force variables into the 383651412Sobrien data segment. */ 3837117410Skan DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL 3838117410Skan || !DECL_THREAD_LOCAL (tem)) 3839117410Skan && (flag_conserve_space || ! TREE_PUBLIC (tem))); 384018334Speter#endif 384190287Sobrien 384251412Sobrien if (! processing_template_decl) 384351412Sobrien start_decl_1 (tem); 384418334Speter 384518334Speter return tem; 384618334Speter} 384718334Speter 384851412Sobrienvoid 3849132747Skanstart_decl_1 (tree decl) 385018334Speter{ 385118334Speter tree type = TREE_TYPE (decl); 385251412Sobrien int initialized = (DECL_INITIAL (decl) != NULL_TREE); 385318334Speter 385452290Sobrien if (type == error_mark_node) 385552290Sobrien return; 385652290Sobrien 385751412Sobrien if (initialized) 385851412Sobrien /* Is it valid for this decl to have an initializer at all? 385951412Sobrien If not, set INITIALIZED to zero, which will indirectly 386051412Sobrien tell `cp_finish_decl' to ignore the initializer once it is parsed. */ 386118334Speter { 386251412Sobrien /* Don't allow initializations for incomplete types except for 386351412Sobrien arrays which might be completed by the initialization. */ 386490287Sobrien if (COMPLETE_TYPE_P (complete_type (type))) 386551412Sobrien ; /* A complete type is ok. */ 386651412Sobrien else if (TREE_CODE (type) != ARRAY_TYPE) 386751412Sobrien { 386890287Sobrien error ("variable `%#D' has initializer but incomplete type", 386951412Sobrien decl); 387051412Sobrien initialized = 0; 387151412Sobrien type = TREE_TYPE (decl) = error_mark_node; 387251412Sobrien } 387390287Sobrien else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) 387451412Sobrien { 387551412Sobrien if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) 387690287Sobrien error ("elements of array `%#D' have incomplete type", decl); 387751412Sobrien /* else we already gave an error in start_decl. */ 387851412Sobrien initialized = 0; 387951412Sobrien } 388018334Speter } 388118334Speter 388251412Sobrien if (!initialized 388351412Sobrien && TREE_CODE (decl) != TYPE_DECL 388451412Sobrien && TREE_CODE (decl) != TEMPLATE_DECL 388590287Sobrien && type != error_mark_node 388690287Sobrien && IS_AGGR_TYPE (type) 388790287Sobrien && ! DECL_EXTERNAL (decl)) 388818334Speter { 388951412Sobrien if ((! processing_template_decl || ! uses_template_parms (type)) 389090287Sobrien && !COMPLETE_TYPE_P (complete_type (type))) 389118334Speter { 389296292Sobrien error ("aggregate `%#D' has incomplete type and cannot be defined", 389351412Sobrien decl); 389451412Sobrien /* Change the type so that assemble_variable will give 389551412Sobrien DECL an rtl we can live with: (mem (const_int 0)). */ 389651412Sobrien type = TREE_TYPE (decl) = error_mark_node; 389718334Speter } 389818334Speter else 389918334Speter { 390051412Sobrien /* If any base type in the hierarchy of TYPE needs a constructor, 390151412Sobrien then we set initialized to 1. This way any nodes which are 390251412Sobrien created for the purposes of initializing this aggregate 390351412Sobrien will live as long as it does. This is necessary for global 390451412Sobrien aggregates which do not have their initializers processed until 390551412Sobrien the end of the file. */ 390651412Sobrien initialized = TYPE_NEEDS_CONSTRUCTING (type); 390718334Speter } 390818334Speter } 390951412Sobrien 391051412Sobrien if (! initialized) 391151412Sobrien DECL_INITIAL (decl) = NULL_TREE; 3912132747Skan 3913132747Skan /* Create a new scope to hold this declaration if necessary. 3914132747Skan Whether or not a new scope is necessary cannot be determined 3915132747Skan until after the type has been completed; if the type is a 3916132747Skan specialization of a class template it is not until after 3917132747Skan instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR 3918132747Skan will be set correctly. */ 3919132747Skan maybe_push_cleanup_level (type); 392051412Sobrien} 392151412Sobrien 3922122192Skan/* Handle initialization of references. DECL, TYPE, and INIT have the 3923122192Skan same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry, 3924122192Skan but will be set to a new CLEANUP_STMT if a temporary is created 3925132747Skan that must be destroyed subsequently. 392651412Sobrien 3927122192Skan Returns an initializer expression to use to initialize DECL, or 3928122192Skan NULL if the initialization can be performed statically. 3929122192Skan 393051412Sobrien Quotes on semantics can be found in ARM 8.4.3. */ 3931132747Skan 393290287Sobrienstatic tree 3933122192Skangrok_reference_init (tree decl, tree type, tree init, tree *cleanup) 393418334Speter{ 393518334Speter tree tmp; 393618334Speter 393718334Speter if (init == NULL_TREE) 393818334Speter { 393918334Speter if ((DECL_LANG_SPECIFIC (decl) == 0 394018334Speter || DECL_IN_AGGR_P (decl) == 0) 394118334Speter && ! DECL_THIS_EXTERN (decl)) 394290287Sobrien error ("`%D' declared as reference but not initialized", decl); 394390287Sobrien return NULL_TREE; 394418334Speter } 394518334Speter 394690287Sobrien if (TREE_CODE (init) == CONSTRUCTOR) 394718334Speter { 394890287Sobrien error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl); 394990287Sobrien return NULL_TREE; 395018334Speter } 395118334Speter 395218334Speter if (TREE_CODE (init) == TREE_LIST) 3953132747Skan init = build_x_compound_expr_from_list (init, "initializer"); 395418334Speter 395518334Speter if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE) 395618334Speter init = convert_from_reference (init); 395718334Speter 395818334Speter if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE 395918334Speter && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) 3960132747Skan /* Note: default conversion is only called in very special cases. */ 3961132747Skan init = decay_conversion (init); 396218334Speter 396390287Sobrien /* Convert INIT to the reference type TYPE. This may involve the 396490287Sobrien creation of a temporary, whose lifetime must be the same as that 396590287Sobrien of the reference. If so, a DECL_STMT for the temporary will be 396690287Sobrien added just after the DECL_STMT for DECL. That's why we don't set 396790287Sobrien DECL_INITIAL for local references (instead assigning to them 396890287Sobrien explicitly); we need to allow the temporary to be initialized 396990287Sobrien first. */ 3970122192Skan tmp = initialize_reference (type, init, decl, cleanup); 397118334Speter 397218334Speter if (tmp == error_mark_node) 397390287Sobrien return NULL_TREE; 397490287Sobrien else if (tmp == NULL_TREE) 397518334Speter { 397690287Sobrien error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init)); 397790287Sobrien return NULL_TREE; 397818334Speter } 397918334Speter 398090287Sobrien if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp)) 398190287Sobrien return tmp; 398218334Speter 398390287Sobrien DECL_INITIAL (decl) = tmp; 398418334Speter 398590287Sobrien return NULL_TREE; 398618334Speter} 398718334Speter 398890287Sobrien/* When parsing `int a[] = {1, 2};' we don't know the size of the 398990287Sobrien array until we finish parsing the initializer. If that's the 399090287Sobrien situation we're in, update DECL accordingly. */ 399152290Sobrien 399252290Sobrienstatic void 3993132747Skanmaybe_deduce_size_from_array_init (tree decl, tree init) 399452290Sobrien{ 399552290Sobrien tree type = TREE_TYPE (decl); 399652290Sobrien 399790287Sobrien if (TREE_CODE (type) == ARRAY_TYPE 399890287Sobrien && TYPE_DOMAIN (type) == NULL_TREE 399990287Sobrien && TREE_CODE (decl) != TYPE_DECL) 400090287Sobrien { 400190287Sobrien /* do_default is really a C-ism to deal with tentative definitions. 400290287Sobrien But let's leave it here to ease the eventual merge. */ 400390287Sobrien int do_default = !DECL_EXTERNAL (decl); 400490287Sobrien tree initializer = init ? init : DECL_INITIAL (decl); 400590287Sobrien int failure = complete_array_type (type, initializer, do_default); 400652290Sobrien 400790287Sobrien if (failure == 1) 400890287Sobrien error ("initializer fails to determine size of `%D'", decl); 400918334Speter 401090287Sobrien if (failure == 2) 401190287Sobrien { 401290287Sobrien if (do_default) 401390287Sobrien error ("array size missing in `%D'", decl); 401490287Sobrien /* If a `static' var's size isn't known, make it extern as 401590287Sobrien well as static, so it does not get allocated. If it's not 401690287Sobrien `static', then don't mark it extern; finish_incomplete_decl 401790287Sobrien will give it a default size and it will get allocated. */ 401890287Sobrien else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 401990287Sobrien DECL_EXTERNAL (decl) = 1; 402090287Sobrien } 402118334Speter 402290287Sobrien if (pedantic && TYPE_DOMAIN (type) != NULL_TREE 402390287Sobrien && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), 402490287Sobrien integer_zero_node)) 402590287Sobrien error ("zero-size array `%D'", decl); 402618334Speter 402790287Sobrien layout_decl (decl, 0); 402890287Sobrien } 402990287Sobrien} 403018334Speter 403190287Sobrien/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue 403290287Sobrien any appropriate error messages regarding the layout. */ 403318334Speter 403490287Sobrienstatic void 4035132747Skanlayout_var_decl (tree decl) 403618334Speter{ 403790287Sobrien tree type = TREE_TYPE (decl); 403890287Sobrien#if 0 403990287Sobrien tree ttype = target_type (type); 404090287Sobrien#endif 404118334Speter 404290287Sobrien /* If we haven't already layed out this declaration, do so now. 404390287Sobrien Note that we must not call complete type for an external object 404490287Sobrien because it's type might involve templates that we are not 4045132747Skan supposed to instantiate yet. (And it's perfectly valid to say 404690287Sobrien `extern X x' for some incomplete type `X'.) */ 404790287Sobrien if (!DECL_EXTERNAL (decl)) 404890287Sobrien complete_type (type); 4049117410Skan if (!DECL_SIZE (decl) 4050117410Skan && TREE_TYPE (decl) != error_mark_node 4051117410Skan && (COMPLETE_TYPE_P (type) 4052117410Skan || (TREE_CODE (type) == ARRAY_TYPE 4053117410Skan && !TYPE_DOMAIN (type) 4054117410Skan && COMPLETE_TYPE_P (TREE_TYPE (type))))) 405590287Sobrien layout_decl (decl, 0); 405690287Sobrien 405790287Sobrien if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) 405818334Speter { 405990287Sobrien /* An automatic variable with an incomplete type: that is an error. 406090287Sobrien Don't talk about array types here, since we took care of that 406190287Sobrien message in grokdeclarator. */ 406290287Sobrien error ("storage size of `%D' isn't known", decl); 406390287Sobrien TREE_TYPE (decl) = error_mark_node; 406418334Speter } 406590287Sobrien#if 0 406690287Sobrien /* Keep this code around in case we later want to control debug info 406790287Sobrien based on whether a type is "used". (jason 1999-11-11) */ 406818334Speter 406990287Sobrien else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype)) 407090287Sobrien /* Let debugger know it should output info for this type. */ 407190287Sobrien note_debug_info_needed (ttype); 407218334Speter 407390287Sobrien if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) 407490287Sobrien note_debug_info_needed (DECL_CONTEXT (decl)); 407590287Sobrien#endif 407651412Sobrien 407790287Sobrien if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 407890287Sobrien && DECL_SIZE (decl) != NULL_TREE 407990287Sobrien && ! TREE_CONSTANT (DECL_SIZE (decl))) 408051412Sobrien { 408190287Sobrien if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) 408290287Sobrien constant_expression_warning (DECL_SIZE (decl)); 408390287Sobrien else 408490287Sobrien error ("storage size of `%D' isn't constant", decl); 408551412Sobrien } 408651412Sobrien 408790287Sobrien if (TREE_STATIC (decl) 408890287Sobrien && !DECL_ARTIFICIAL (decl) 408990287Sobrien && current_function_decl 409090287Sobrien && DECL_CONTEXT (decl) == current_function_decl) 409190287Sobrien push_local_name (decl); 409290287Sobrien} 409318334Speter 409490287Sobrien/* If a local static variable is declared in an inline function, or if 409590287Sobrien we have a weak definition, we must endeavor to create only one 409690287Sobrien instance of the variable at link-time. */ 409718334Speter 409890287Sobrienstatic void 4099132747Skanmaybe_commonize_var (tree decl) 410090287Sobrien{ 410190287Sobrien /* Static data in a function with comdat linkage also has comdat 410290287Sobrien linkage. */ 410390287Sobrien if (TREE_STATIC (decl) 410490287Sobrien /* Don't mess with __FUNCTION__. */ 410590287Sobrien && ! DECL_ARTIFICIAL (decl) 4106117410Skan && DECL_FUNCTION_SCOPE_P (decl) 4107117410Skan /* Unfortunately, import_export_decl has not always been called 4108117410Skan before the function is processed, so we cannot simply check 4109117410Skan DECL_COMDAT. */ 4110117410Skan && (DECL_COMDAT (DECL_CONTEXT (decl)) 4111117410Skan || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl)) 4112117410Skan || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl))) 4113117410Skan && TREE_PUBLIC (DECL_CONTEXT (decl))))) 411418334Speter { 4115117410Skan if (flag_weak) 411651412Sobrien { 4117117410Skan /* With weak symbols, we simply make the variable COMDAT; 4118117410Skan that will cause copies in multiple translations units to 4119117410Skan be merged. */ 4120117410Skan comdat_linkage (decl); 4121117410Skan } 4122117410Skan else 4123117410Skan { 4124117410Skan if (DECL_INITIAL (decl) == NULL_TREE 4125117410Skan || DECL_INITIAL (decl) == error_mark_node) 412651412Sobrien { 4127117410Skan /* Without weak symbols, we can use COMMON to merge 4128117410Skan uninitialized variables. */ 412990287Sobrien TREE_PUBLIC (decl) = 1; 413090287Sobrien DECL_COMMON (decl) = 1; 413190287Sobrien } 4132117410Skan else 413390287Sobrien { 4134117410Skan /* While for initialized variables, we must use internal 4135117410Skan linkage -- which means that multiple copies will not 4136117410Skan be merged. */ 4137117410Skan TREE_PUBLIC (decl) = 0; 4138117410Skan DECL_COMMON (decl) = 0; 413990287Sobrien cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl); 4140132747Skan warning ("%J you can work around this by removing the initializer", 4141132747Skan decl); 414290287Sobrien } 414351412Sobrien } 414451412Sobrien } 414590287Sobrien else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl)) 414690287Sobrien /* Set it up again; we might have set DECL_INITIAL since the last 414790287Sobrien time. */ 414890287Sobrien comdat_linkage (decl); 414990287Sobrien} 415051412Sobrien 415190287Sobrien/* Issue an error message if DECL is an uninitialized const variable. */ 415251412Sobrien 415390287Sobrienstatic void 4154132747Skancheck_for_uninitialized_const_var (tree decl) 415590287Sobrien{ 415690287Sobrien tree type = TREE_TYPE (decl); 415718334Speter 415890287Sobrien /* ``Unless explicitly declared extern, a const object does not have 415990287Sobrien external linkage and must be initialized. ($8.4; $12.1)'' ARM 416090287Sobrien 7.1.6 */ 416190287Sobrien if (TREE_CODE (decl) == VAR_DECL 416290287Sobrien && TREE_CODE (type) != REFERENCE_TYPE 416390287Sobrien && CP_TYPE_CONST_P (type) 416490287Sobrien && !TYPE_NEEDS_CONSTRUCTING (type) 416590287Sobrien && !DECL_INITIAL (decl)) 416690287Sobrien error ("uninitialized const `%D'", decl); 416790287Sobrien} 416818334Speter 4169117410Skan/* FIELD is a FIELD_DECL or NULL. In the former case, the value 4170117410Skan returned is the next FIELD_DECL (possibly FIELD itself) that can be 4171117410Skan initialized. If there are no more such fields, the return value 4172117410Skan will be NULL. */ 417318334Speter 417490287Sobrienstatic tree 4175117410Skannext_initializable_field (tree field) 417690287Sobrien{ 4177117410Skan while (field 4178117410Skan && (TREE_CODE (field) != FIELD_DECL 4179117410Skan || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field)) 4180117410Skan || DECL_ARTIFICIAL (field))) 4181117410Skan field = TREE_CHAIN (field); 418290287Sobrien 4183117410Skan return field; 4184117410Skan} 418590287Sobrien 4186117410Skan/* Undo the brace-elision allowed by [dcl.init.aggr] in a 4187117410Skan brace-enclosed aggregate initializer. 418890287Sobrien 4189117410Skan *INITP is one of a list of initializers describing a brace-enclosed 4190117410Skan initializer for an entity of the indicated aggregate TYPE. It may 4191117410Skan not presently match the shape of the TYPE; for example: 4192117410Skan 4193117410Skan struct S { int a; int b; }; 4194117410Skan struct S a[] = { 1, 2, 3, 4 }; 419590287Sobrien 4196117410Skan Here *INITP will point to TREE_LIST of four elements, rather than a 4197117410Skan list of two elements, each itself a list of two elements. This 4198117410Skan routine transforms INIT from the former form into the latter. The 4199117410Skan revised initializer is returned. */ 4200117410Skan 4201117410Skanstatic tree 4202117410Skanreshape_init (tree type, tree *initp) 4203117410Skan{ 4204117410Skan tree inits; 4205117410Skan tree old_init; 4206117410Skan tree old_init_value; 4207117410Skan tree new_init; 4208117410Skan bool brace_enclosed_p; 4209117410Skan 4210117410Skan old_init = *initp; 4211117410Skan old_init_value = (TREE_CODE (*initp) == TREE_LIST 4212117410Skan ? TREE_VALUE (*initp) : old_init); 4213117410Skan 4214132747Skan my_friendly_assert (old_init_value, 20030723); 421518334Speter 4216117410Skan /* If the initializer is brace-enclosed, pull initializers from the 4217117410Skan enclosed elements. Advance past the brace-enclosed initializer 4218117410Skan now. */ 4219132747Skan if (TREE_CODE (old_init_value) == CONSTRUCTOR 4220132747Skan && TREE_TYPE (old_init_value) == NULL_TREE 4221117410Skan && TREE_HAS_CONSTRUCTOR (old_init_value)) 4222117410Skan { 4223117410Skan *initp = TREE_CHAIN (old_init); 4224117410Skan TREE_CHAIN (old_init) = NULL_TREE; 4225117410Skan inits = CONSTRUCTOR_ELTS (old_init_value); 4226117410Skan initp = &inits; 4227117410Skan brace_enclosed_p = true; 4228117410Skan } 4229117410Skan else 4230117410Skan { 4231117410Skan inits = NULL_TREE; 4232117410Skan brace_enclosed_p = false; 4233117410Skan } 4234117410Skan 4235117410Skan /* A non-aggregate type is always initialized with a single 4236117410Skan initializer. */ 4237117410Skan if (!CP_AGGREGATE_TYPE_P (type)) 4238117410Skan { 4239117410Skan *initp = TREE_CHAIN (old_init); 4240117410Skan TREE_CHAIN (old_init) = NULL_TREE; 4241117410Skan /* It is invalid to initialize a non-aggregate type with a 4242117410Skan brace-enclosed initializer. */ 4243117410Skan if (brace_enclosed_p) 4244117410Skan { 4245117410Skan error ("brace-enclosed initializer used to initialize `%T'", 4246117410Skan type); 4247117410Skan if (TREE_CODE (old_init) == TREE_LIST) 4248117410Skan TREE_VALUE (old_init) = error_mark_node; 4249117410Skan else 4250117410Skan old_init = error_mark_node; 4251117410Skan } 4252117410Skan 4253117410Skan return old_init; 4254117410Skan } 4255117410Skan 4256117410Skan /* [dcl.init.aggr] 4257117410Skan 4258117410Skan All implicit type conversions (clause _conv_) are considered when 4259117410Skan initializing the aggregate member with an initializer from an 4260117410Skan initializer-list. If the initializer can initialize a member, 4261117410Skan the member is initialized. Otherwise, if the member is itself a 4262117410Skan non-empty subaggregate, brace elision is assumed and the 4263117410Skan initializer is considered for the initialization of the first 4264117410Skan member of the subaggregate. */ 4265132747Skan if (!brace_enclosed_p 4266117410Skan && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value)) 4267117410Skan { 4268117410Skan *initp = TREE_CHAIN (old_init); 4269117410Skan TREE_CHAIN (old_init) = NULL_TREE; 4270117410Skan return old_init; 4271117410Skan } 4272117410Skan 4273117410Skan if (TREE_CODE (old_init_value) == STRING_CST 4274117410Skan && TREE_CODE (type) == ARRAY_TYPE 4275117410Skan && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))) 4276117410Skan { 4277117410Skan /* [dcl.init.string] 4278117410Skan 4279117410Skan A char array (whether plain char, signed char, or unsigned char) 4280117410Skan can be initialized by a string-literal (optionally enclosed in 4281117410Skan braces); a wchar_t array can be initialized by a wide 4282117410Skan string-literal (optionally enclosed in braces). */ 4283117410Skan new_init = old_init; 4284117410Skan /* Move past the initializer. */ 4285117410Skan *initp = TREE_CHAIN (old_init); 4286117410Skan TREE_CHAIN (old_init) = NULL_TREE; 4287117410Skan } 4288117410Skan else 4289117410Skan { 4290117410Skan /* Build a CONSTRUCTOR to hold the contents of the aggregate. */ 4291132747Skan new_init = build_constructor (type, NULL_TREE); 4292117410Skan TREE_HAS_CONSTRUCTOR (new_init) = 1; 4293117410Skan 4294117410Skan if (CLASS_TYPE_P (type)) 429518334Speter { 4296117410Skan tree field; 4297117410Skan 4298117410Skan field = next_initializable_field (TYPE_FIELDS (type)); 4299117410Skan 4300117410Skan if (!field) 4301117410Skan { 4302117410Skan /* [dcl.init.aggr] 4303117410Skan 4304117410Skan An initializer for an aggregate member that is an 4305117410Skan empty class shall have the form of an empty 4306117410Skan initializer-list {}. */ 4307117410Skan if (!brace_enclosed_p) 4308132747Skan { 4309132747Skan error ("initializer for `%T' must be brace-enclosed", 4310132747Skan type); 4311132747Skan return error_mark_node; 4312132747Skan } 4313117410Skan } 4314117410Skan else 4315117410Skan { 4316117410Skan /* Loop through the initializable fields, gathering 4317117410Skan initializers. */ 4318117410Skan while (*initp) 4319117410Skan { 4320117410Skan tree field_init; 4321117410Skan 4322117410Skan /* Handle designated initializers, as an extension. */ 4323117410Skan if (TREE_PURPOSE (*initp)) 4324117410Skan { 4325117410Skan if (pedantic) 4326117410Skan pedwarn ("ISO C++ does not allow designated initializers"); 4327117410Skan field = lookup_field_1 (type, TREE_PURPOSE (*initp), 4328117410Skan /*want_type=*/false); 4329117410Skan if (!field || TREE_CODE (field) != FIELD_DECL) 4330117410Skan error ("`%T' has no non-static data member named `%D'", 4331117410Skan type, TREE_PURPOSE (*initp)); 4332117410Skan } 4333117410Skan if (!field) 4334117410Skan break; 4335117410Skan 4336117410Skan field_init = reshape_init (TREE_TYPE (field), initp); 4337132747Skan if (field_init == error_mark_node) 4338132747Skan return error_mark_node; 4339117410Skan TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init); 4340117410Skan CONSTRUCTOR_ELTS (new_init) = field_init; 4341117410Skan /* [dcl.init.aggr] 4342117410Skan 4343117410Skan When a union is initialized with a brace-enclosed 4344117410Skan initializer, the braces shall only contain an 4345117410Skan initializer for the first member of the union. */ 4346117410Skan if (TREE_CODE (type) == UNION_TYPE) 4347117410Skan break; 4348117410Skan field = next_initializable_field (TREE_CHAIN (field)); 4349117410Skan } 4350117410Skan } 435118334Speter } 4352132747Skan else if ((TREE_CODE (type) == ARRAY_TYPE)|| (TREE_CODE (type) == VECTOR_TYPE)) 435390287Sobrien { 4354117410Skan tree index; 4355117410Skan tree max_index; 4356117410Skan 4357117410Skan /* If the bound of the array is known, take no more initializers 4358117410Skan than are allowed. */ 4359132747Skan max_index = ((TYPE_DOMAIN (type) && (TREE_CODE (type) == ARRAY_TYPE)) 4360117410Skan ? array_type_nelts (type) : NULL_TREE); 4361117410Skan /* Loop through the array elements, gathering initializers. */ 4362117410Skan for (index = size_zero_node; 4363117410Skan *initp && (!max_index || !tree_int_cst_lt (max_index, index)); 4364117410Skan index = size_binop (PLUS_EXPR, index, size_one_node)) 4365117410Skan { 4366117410Skan tree element_init; 4367117410Skan 4368117410Skan element_init = reshape_init (TREE_TYPE (type), initp); 4369132747Skan if (element_init == error_mark_node) 4370132747Skan return error_mark_node; 4371117410Skan TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init); 4372117410Skan CONSTRUCTOR_ELTS (new_init) = element_init; 4373117410Skan if (TREE_PURPOSE (element_init)) 4374132747Skan { 4375132747Skan tree next_index = TREE_PURPOSE (element_init); 4376132747Skan if (TREE_CODE (next_index) == IDENTIFIER_NODE) 4377132747Skan { 4378132747Skan error ("name `%D' used in a GNU-style designated " 4379132747Skan "initializer for an array", next_index); 4380132747Skan TREE_PURPOSE (element_init) = NULL_TREE; 4381132747Skan } 4382132747Skan else 4383132747Skan index = next_index; 4384132747Skan } 4385117410Skan } 438690287Sobrien } 4387117410Skan else 4388117410Skan abort (); 4389117410Skan 4390117410Skan /* The initializers were placed in reverse order in the 4391117410Skan CONSTRUCTOR. */ 4392117410Skan CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init)); 4393117410Skan 4394117410Skan if (TREE_CODE (old_init) == TREE_LIST) 4395117410Skan new_init = build_tree_list (TREE_PURPOSE (old_init), new_init); 439618334Speter } 439718334Speter 4398117410Skan /* If this was a brace-enclosed initializer and all of the 4399117410Skan initializers were not used up, there is a problem. */ 4400117410Skan if (brace_enclosed_p && *initp) 4401117410Skan error ("too many initializers for `%T'", type); 4402117410Skan 4403117410Skan return new_init; 4404117410Skan} 4405117410Skan 4406117410Skan/* Verify INIT (the initializer for DECL), and record the 4407122192Skan initialization in DECL_INITIAL, if appropriate. CLEANUP is as for 4408122192Skan grok_reference_init. 4409117410Skan 4410117410Skan If the return value is non-NULL, it is an expression that must be 4411117410Skan evaluated dynamically to initialize DECL. */ 4412117410Skan 4413117410Skanstatic tree 4414122192Skancheck_initializer (tree decl, tree init, int flags, tree *cleanup) 4415117410Skan{ 4416117410Skan tree type = TREE_TYPE (decl); 4417132747Skan tree init_code = NULL; 4418117410Skan 4419117410Skan /* If `start_decl' didn't like having an initialization, ignore it now. */ 4420117410Skan if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE) 4421117410Skan init = NULL_TREE; 4422117410Skan 4423117410Skan /* If an initializer is present, DECL_INITIAL has been 4424117410Skan error_mark_node, to indicate that an as-of-yet unevaluated 4425117410Skan initialization will occur. From now on, DECL_INITIAL reflects 4426117410Skan the static initialization -- if any -- of DECL. */ 4427117410Skan DECL_INITIAL (decl) = NULL_TREE; 4428117410Skan 4429117410Skan /* Things that are going to be initialized need to have complete 4430117410Skan type. */ 4431117410Skan TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); 4432117410Skan 4433117410Skan if (type == error_mark_node) 4434117410Skan /* We will have already complained. */ 4435117410Skan init = NULL_TREE; 4436117410Skan else if (init && COMPLETE_TYPE_P (type) 4437117410Skan && !TREE_CONSTANT (TYPE_SIZE (type))) 4438117410Skan { 4439117410Skan error ("variable-sized object `%D' may not be initialized", decl); 4440117410Skan init = NULL_TREE; 4441117410Skan } 4442117410Skan else if (TREE_CODE (type) == ARRAY_TYPE 4443117410Skan && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) 4444117410Skan { 4445117410Skan error ("elements of array `%#D' have incomplete type", decl); 4446117410Skan init = NULL_TREE; 4447117410Skan } 4448117410Skan else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type)) 4449117410Skan { 4450117410Skan error ("`%D' has incomplete type", decl); 4451117410Skan TREE_TYPE (decl) = error_mark_node; 4452117410Skan init = NULL_TREE; 4453117410Skan } 4454117410Skan 445590287Sobrien if (TREE_CODE (decl) == CONST_DECL) 445618334Speter { 445718334Speter my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148); 445818334Speter 445918334Speter DECL_INITIAL (decl) = init; 446018334Speter 446118334Speter my_friendly_assert (init != NULL_TREE, 149); 446218334Speter init = NULL_TREE; 446318334Speter } 446490287Sobrien else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE) 4465122192Skan init = grok_reference_init (decl, type, init, cleanup); 4466117410Skan else if (init) 446790287Sobrien { 4468117410Skan if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)) 4469117410Skan { 4470117410Skan /* [dcl.init] paragraph 13, 4471117410Skan If T is a scalar type, then a declaration of the form 4472117410Skan T x = { a }; 4473117410Skan is equivalent to 4474117410Skan T x = a; 4475117410Skan 4476117410Skan reshape_init will complain about the extra braces, 4477117410Skan and doesn't do anything useful in the case where TYPE is 4478117410Skan scalar, so just don't call it. */ 4479117410Skan if (CP_AGGREGATE_TYPE_P (type)) 4480117410Skan init = reshape_init (type, &init); 4481132747Skan 4482132747Skan if ((*targetm.vector_opaque_p) (type)) 4483132747Skan { 4484132747Skan error ("opaque vector types cannot be initialized"); 4485132747Skan init = error_mark_node; 4486132747Skan } 4487117410Skan } 4488102804Skan 4489117410Skan /* If DECL has an array type without a specific bound, deduce the 4490117410Skan array size from the initializer. */ 4491117410Skan maybe_deduce_size_from_array_init (decl, init); 4492117410Skan type = TREE_TYPE (decl); 4493117410Skan if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)) 4494117410Skan TREE_TYPE (init) = type; 4495117410Skan 449618334Speter if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type)) 449718334Speter { 449818334Speter if (TREE_CODE (type) == ARRAY_TYPE) 4499117410Skan goto initialize_aggr; 450052290Sobrien else if (TREE_CODE (init) == CONSTRUCTOR 450152290Sobrien && TREE_HAS_CONSTRUCTOR (init)) 450218334Speter { 450318334Speter if (TYPE_NON_AGGREGATE_CLASS (type)) 450418334Speter { 450590287Sobrien error ("`%D' must be initialized by constructor, not by `{...}'", 4506117410Skan decl); 450718334Speter init = error_mark_node; 450818334Speter } 450918334Speter else 451018334Speter goto dont_use_constructor; 451118334Speter } 4512117410Skan else 4513117410Skan { 4514117410Skan int saved_stmts_are_full_exprs_p; 4515117410Skan 4516117410Skan initialize_aggr: 4517117410Skan saved_stmts_are_full_exprs_p = 0; 4518117410Skan if (building_stmt_tree ()) 4519117410Skan { 4520117410Skan saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); 4521117410Skan current_stmt_tree ()->stmts_are_full_exprs_p = 1; 4522117410Skan } 4523117410Skan init = build_aggr_init (decl, init, flags); 4524117410Skan if (building_stmt_tree ()) 4525117410Skan current_stmt_tree ()->stmts_are_full_exprs_p = 4526117410Skan saved_stmts_are_full_exprs_p; 4527117410Skan return init; 4528117410Skan } 452918334Speter } 453018334Speter else 453118334Speter { 453218334Speter dont_use_constructor: 453318334Speter if (TREE_CODE (init) != TREE_VEC) 4534132747Skan { 4535132747Skan init_code = store_init_value (decl, init); 4536132747Skan init = NULL; 4537132747Skan } 453818334Speter } 453918334Speter } 454018334Speter else if (DECL_EXTERNAL (decl)) 454118334Speter ; 4542117410Skan else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type)) 4543117410Skan goto initialize_aggr; 4544117410Skan else if (IS_AGGR_TYPE (type)) 454518334Speter { 454690287Sobrien tree core_type = strip_array_types (type); 454790287Sobrien 4548117410Skan if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)) 4549117410Skan error ("structure `%D' with uninitialized const members", decl); 4550117410Skan if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) 4551117410Skan error ("structure `%D' with uninitialized reference members", 4552117410Skan decl); 455318334Speter 455452290Sobrien check_for_uninitialized_const_var (decl); 455518334Speter } 455652290Sobrien else 455752290Sobrien check_for_uninitialized_const_var (decl); 455818334Speter 4559117410Skan if (init && init != error_mark_node) 4560132747Skan init_code = build (INIT_EXPR, type, decl, init); 4561117410Skan 4562132747Skan return init_code; 456390287Sobrien} 456418334Speter 456590287Sobrien/* If DECL is not a local variable, give it RTL. */ 456618334Speter 456790287Sobrienstatic void 4568132747Skanmake_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec) 456990287Sobrien{ 457090287Sobrien int toplev = toplevel_bindings_p (); 457190287Sobrien int defer_p; 457290287Sobrien 457390287Sobrien /* Handle non-variables up front. */ 457490287Sobrien if (TREE_CODE (decl) != VAR_DECL) 457518334Speter { 457690287Sobrien rest_of_decl_compilation (decl, asmspec, toplev, at_eof); 457790287Sobrien return; 457890287Sobrien } 457918334Speter 458090287Sobrien /* If we see a class member here, it should be a static data 458190287Sobrien member. */ 458290287Sobrien if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) 458390287Sobrien { 458490287Sobrien my_friendly_assert (TREE_STATIC (decl), 19990828); 458590287Sobrien /* An in-class declaration of a static data member should be 458690287Sobrien external; it is only a declaration, and not a definition. */ 458790287Sobrien if (init == NULL_TREE) 458890287Sobrien my_friendly_assert (DECL_EXTERNAL (decl), 20000723); 458990287Sobrien } 459018334Speter 459190287Sobrien /* Set the DECL_ASSEMBLER_NAME for the variable. */ 459290287Sobrien if (asmspec) 459390287Sobrien { 4594132747Skan change_decl_assembler_name (decl, get_identifier (asmspec)); 459590287Sobrien /* The `register' keyword, when used together with an 459690287Sobrien asm-specification, indicates that the variable should be 459790287Sobrien placed in a particular register. */ 459890287Sobrien if (DECL_REGISTER (decl)) 459990287Sobrien DECL_C_HARD_REGISTER (decl) = 1; 460090287Sobrien } 460190287Sobrien 460290287Sobrien /* We don't create any RTL for local variables. */ 460390287Sobrien if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) 460490287Sobrien return; 460590287Sobrien 460690287Sobrien /* We defer emission of local statics until the corresponding 460790287Sobrien DECL_STMT is expanded. */ 460890287Sobrien defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl); 460990287Sobrien 461090287Sobrien /* We try to defer namespace-scope static constants so that they are 461190287Sobrien not emitted into the object file unnecessarily. */ 461290287Sobrien if (!DECL_VIRTUAL_P (decl) 461390287Sobrien && TREE_READONLY (decl) 461490287Sobrien && DECL_INITIAL (decl) != NULL_TREE 461590287Sobrien && DECL_INITIAL (decl) != error_mark_node 461690287Sobrien && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)) 461790287Sobrien && toplev 461890287Sobrien && !TREE_PUBLIC (decl)) 461990287Sobrien { 462096292Sobrien /* Fool with the linkage of static consts according to #pragma 462196292Sobrien interface. */ 462296292Sobrien if (!interface_unknown && !TREE_PUBLIC (decl)) 462318334Speter { 462490287Sobrien TREE_PUBLIC (decl) = 1; 462590287Sobrien DECL_EXTERNAL (decl) = interface_only; 462618334Speter } 462718334Speter 462890287Sobrien defer_p = 1; 462918334Speter } 463096292Sobrien /* Likewise for template instantiations. */ 463196292Sobrien else if (DECL_COMDAT (decl)) 463296292Sobrien defer_p = 1; 463318334Speter 463490287Sobrien /* If we're deferring the variable, we only need to make RTL if 463590287Sobrien there's an ASMSPEC. Otherwise, we'll lazily create it later when 463690287Sobrien we need it. (There's no way to lazily create RTL for things that 463790287Sobrien have assembly specs because the information about the specifier 463890287Sobrien isn't stored in the tree, yet) */ 463990287Sobrien if (defer_p && asmspec) 464090287Sobrien make_decl_rtl (decl, asmspec); 464190287Sobrien /* If we're not deferring, go ahead and assemble the variable. */ 464290287Sobrien else if (!defer_p) 464390287Sobrien rest_of_decl_compilation (decl, asmspec, toplev, at_eof); 464490287Sobrien} 464590287Sobrien 464690287Sobrien/* Generate code to initialize DECL (a local variable). */ 464790287Sobrien 4648117410Skanstatic void 4649132747Skaninitialize_local_var (tree decl, tree init) 465090287Sobrien{ 465190287Sobrien tree type = TREE_TYPE (decl); 4652117410Skan tree cleanup; 465390287Sobrien 4654117410Skan my_friendly_assert (TREE_CODE (decl) == VAR_DECL 4655117410Skan || TREE_CODE (decl) == RESULT_DECL, 4656117410Skan 20021010); 4657117410Skan my_friendly_assert (!TREE_STATIC (decl), 20021010); 465890287Sobrien 4659117410Skan if (DECL_SIZE (decl) == NULL_TREE) 466018334Speter { 466190287Sobrien /* If we used it already as memory, it must stay in memory. */ 466290287Sobrien DECL_INITIAL (decl) = NULL_TREE; 466390287Sobrien TREE_ADDRESSABLE (decl) = TREE_USED (decl); 466418334Speter } 466518334Speter 466690287Sobrien if (DECL_SIZE (decl) && type != error_mark_node) 466718334Speter { 466890287Sobrien int already_used; 466918334Speter 467090287Sobrien /* Compute and store the initial value. */ 467190287Sobrien already_used = TREE_USED (decl) || TREE_USED (type); 467218334Speter 4673117410Skan /* Perform the initialization. */ 4674117410Skan if (init) 467551412Sobrien { 467690287Sobrien int saved_stmts_are_full_exprs_p; 467718334Speter 467890287Sobrien my_friendly_assert (building_stmt_tree (), 20000906); 467990287Sobrien saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); 468090287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 1; 4681117410Skan finish_expr_stmt (init); 468290287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 468390287Sobrien saved_stmts_are_full_exprs_p; 468451412Sobrien } 468551412Sobrien 468690287Sobrien /* Set this to 0 so we can tell whether an aggregate which was 468790287Sobrien initialized was ever used. Don't do this if it has a 468890287Sobrien destructor, so we don't complain about the 'resource 468990287Sobrien allocation is initialization' idiom. Now set 469090287Sobrien attribute((unused)) on types so decls of that type will be 469190287Sobrien marked used. (see TREE_USED, above.) */ 469290287Sobrien if (TYPE_NEEDS_CONSTRUCTING (type) 469390287Sobrien && ! already_used 469490287Sobrien && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) 469590287Sobrien && DECL_NAME (decl)) 469690287Sobrien TREE_USED (decl) = 0; 469790287Sobrien else if (already_used) 469890287Sobrien TREE_USED (decl) = 1; 469990287Sobrien } 470051412Sobrien 4701117410Skan /* Generate a cleanup, if necessary. */ 4702117410Skan cleanup = cxx_maybe_build_cleanup (decl); 470396292Sobrien if (DECL_SIZE (decl) && cleanup) 470490287Sobrien finish_decl_cleanup (decl, cleanup); 470590287Sobrien} 470690287Sobrien 470790287Sobrien/* Finish processing of a declaration; 470890287Sobrien install its line number and initial value. 470990287Sobrien If the length of an array type is not known before, 471090287Sobrien it must be determined now, from the initial value, or it is an error. 471190287Sobrien 471290287Sobrien INIT holds the value of an initializer that should be allowed to escape 471390287Sobrien the normal rules. 471490287Sobrien 471590287Sobrien FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 471690287Sobrien if the (init) syntax was used. */ 471790287Sobrien 471890287Sobrienvoid 4719132747Skancp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags) 472090287Sobrien{ 4721122192Skan tree type; 472290287Sobrien tree ttype = NULL_TREE; 4723122192Skan tree cleanup; 472490287Sobrien const char *asmspec = NULL; 472590287Sobrien int was_readonly = 0; 472690287Sobrien 4727132747Skan if (decl == error_mark_node) 4728132747Skan return; 4729132747Skan else if (! decl) 473090287Sobrien { 473190287Sobrien if (init) 473290287Sobrien error ("assignment (not initialization) in declaration"); 473390287Sobrien return; 473490287Sobrien } 473590287Sobrien 4736132747Skan my_friendly_assert (TREE_CODE (decl) != RESULT_DECL, 20030619); 4737132747Skan 4738122192Skan /* Assume no cleanup is required. */ 4739122192Skan cleanup = NULL_TREE; 4740122192Skan 474190287Sobrien /* If a name was specified, get the string. */ 4742117410Skan if (global_scope_p (current_binding_level)) 474396292Sobrien asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 4744132747Skan if (asmspec_tree) 474596292Sobrien asmspec = TREE_STRING_POINTER (asmspec_tree); 474690287Sobrien 474790287Sobrien if (init && TREE_CODE (init) == NAMESPACE_DECL) 474890287Sobrien { 474990287Sobrien error ("cannot initialize `%D' to namespace `%D'", 475090287Sobrien decl, init); 475190287Sobrien init = NULL_TREE; 475290287Sobrien } 475390287Sobrien 475490287Sobrien if (current_class_type 475590287Sobrien && CP_DECL_CONTEXT (decl) == current_class_type 475690287Sobrien && TYPE_BEING_DEFINED (current_class_type) 475790287Sobrien && (DECL_INITIAL (decl) || init)) 475890287Sobrien DECL_INITIALIZED_IN_CLASS_P (decl) = 1; 475990287Sobrien 476090287Sobrien if (TREE_CODE (decl) == VAR_DECL 476190287Sobrien && DECL_CONTEXT (decl) 476290287Sobrien && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL 476390287Sobrien && DECL_CONTEXT (decl) != current_namespace 476490287Sobrien && init) 476590287Sobrien { 4766117410Skan /* Leave the namespace of the object. */ 476790287Sobrien pop_decl_namespace (); 476890287Sobrien } 476990287Sobrien 477090287Sobrien type = TREE_TYPE (decl); 477190287Sobrien 477290287Sobrien if (type == error_mark_node) 4773132747Skan goto finish_end0; 477490287Sobrien 477590287Sobrien if (TYPE_HAS_MUTABLE_P (type)) 477690287Sobrien TREE_READONLY (decl) = 0; 477790287Sobrien 477890287Sobrien if (processing_template_decl) 477990287Sobrien { 478090287Sobrien /* Add this declaration to the statement-tree. */ 4781132747Skan if (at_function_scope_p ()) 478290287Sobrien add_decl_stmt (decl); 478390287Sobrien 478490287Sobrien if (init && DECL_INITIAL (decl)) 478590287Sobrien DECL_INITIAL (decl) = init; 4786132747Skan if (TREE_CODE (decl) == VAR_DECL 4787132747Skan && !DECL_PRETTY_FUNCTION_P (decl) 4788132747Skan && !dependent_type_p (TREE_TYPE (decl))) 4789132747Skan maybe_deduce_size_from_array_init (decl, init); 479090287Sobrien goto finish_end0; 479190287Sobrien } 479290287Sobrien 479390287Sobrien /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ 479490287Sobrien my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828); 479590287Sobrien 479690287Sobrien /* Take care of TYPE_DECLs up front. */ 479790287Sobrien if (TREE_CODE (decl) == TYPE_DECL) 479890287Sobrien { 479990287Sobrien if (type != error_mark_node 480090287Sobrien && IS_AGGR_TYPE (type) && DECL_NAME (decl)) 480118334Speter { 480290287Sobrien if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) 480390287Sobrien warning ("shadowing previous type declaration of `%#D'", decl); 4804132747Skan set_identifier_type_value (DECL_NAME (decl), decl); 480518334Speter } 480618334Speter 480790287Sobrien /* If we have installed this as the canonical typedef for this 480890287Sobrien type, and that type has not been defined yet, delay emitting 480990287Sobrien the debug information for it, as we will emit it later. */ 481090287Sobrien if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl 481190287Sobrien && !COMPLETE_TYPE_P (TREE_TYPE (decl))) 481290287Sobrien TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 481318334Speter 481490287Sobrien rest_of_decl_compilation (decl, NULL, 481590287Sobrien DECL_CONTEXT (decl) == NULL_TREE, at_eof); 481690287Sobrien goto finish_end; 481790287Sobrien } 481851412Sobrien 481990287Sobrien if (TREE_CODE (decl) != FUNCTION_DECL) 482090287Sobrien ttype = target_type (type); 482151412Sobrien 4822132747Skan 4823132747Skan /* Currently, GNU C++ puts constants in text space, making them 4824132747Skan impossible to initialize. In the future, one would hope for 4825132747Skan an operating system which understood the difference between 4826132747Skan initialization and the running of a program. */ 4827132747Skan if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)) 482890287Sobrien { 482990287Sobrien was_readonly = 1; 4830132747Skan if (TYPE_NEEDS_CONSTRUCTING (type) 4831132747Skan || TREE_CODE (type) == REFERENCE_TYPE) 4832132747Skan TREE_READONLY (decl) = 0; 483390287Sobrien } 483451412Sobrien 4835132747Skan if (TREE_CODE (decl) == VAR_DECL) 483690287Sobrien { 4837117410Skan /* Only PODs can have thread-local storage. Other types may require 4838117410Skan various kinds of non-trivial initialization. */ 4839117410Skan if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl))) 4840117410Skan error ("`%D' cannot be thread-local because it has non-POD type `%T'", 4841117410Skan decl, TREE_TYPE (decl)); 4842117410Skan /* Convert the initializer to the type of DECL, if we have not 4843117410Skan already initialized DECL. */ 4844117410Skan if (!DECL_INITIALIZED_P (decl) 4845117410Skan /* If !DECL_EXTERNAL then DECL is being defined. In the 4846117410Skan case of a static data member initialized inside the 4847117410Skan class-specifier, there can be an initializer even if DECL 4848117410Skan is *not* defined. */ 4849117410Skan && (!DECL_EXTERNAL (decl) || init)) 4850117410Skan { 4851122192Skan init = check_initializer (decl, init, flags, &cleanup); 4852117410Skan /* Thread-local storage cannot be dynamically initialized. */ 4853117410Skan if (DECL_THREAD_LOCAL (decl) && init) 4854117410Skan { 4855117410Skan error ("`%D' is thread-local and so cannot be dynamically " 4856117410Skan "initialized", decl); 4857117410Skan init = NULL_TREE; 4858117410Skan } 4859117410Skan /* Handle: 4860117410Skan 4861117410Skan [dcl.init] 4862117410Skan 4863117410Skan The memory occupied by any object of static storage 4864117410Skan duration is zero-initialized at program startup before 4865117410Skan any other initialization takes place. 4866117410Skan 4867117410Skan We cannot create an appropriate initializer until after 4868117410Skan the type of DECL is finalized. If DECL_INITIAL is set, 4869117410Skan then the DECL is statically initialized, and any 4870117410Skan necessary zero-initialization has already been performed. */ 4871117410Skan if (TREE_STATIC (decl) && !DECL_INITIAL (decl)) 4872117410Skan DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl), 4873117410Skan /*nelts=*/NULL_TREE, 4874117410Skan /*static_storage_p=*/true); 4875117410Skan /* Remember that the initialization for this variable has 4876117410Skan taken place. */ 4877117410Skan DECL_INITIALIZED_P (decl) = 1; 4878117410Skan } 4879117410Skan /* If the variable has an array type, lay out the type, even if 4880117410Skan there is no initializer. It is valid to index through the 4881117410Skan array, and we must get TYPE_ALIGN set correctly on the array 4882117410Skan type. */ 4883117410Skan else if (TREE_CODE (type) == ARRAY_TYPE) 4884117410Skan layout_type (type); 4885117410Skan } 488651412Sobrien 488790287Sobrien /* Add this declaration to the statement-tree. This needs to happen 488890287Sobrien after the call to check_initializer so that the DECL_STMT for a 488990287Sobrien reference temp is added before the DECL_STMT for the reference itself. */ 4890132747Skan if (at_function_scope_p ()) 489190287Sobrien add_decl_stmt (decl); 489218334Speter 489390287Sobrien if (TREE_CODE (decl) == VAR_DECL) 489490287Sobrien layout_var_decl (decl); 489551412Sobrien 489690287Sobrien /* Output the assembler code and/or RTL code for variables and functions, 489790287Sobrien unless the type is an undefined structure or union. 489890287Sobrien If not, it will get done when the type is completed. */ 4899132747Skan if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) 490090287Sobrien { 490190287Sobrien if (TREE_CODE (decl) == VAR_DECL) 490290287Sobrien maybe_commonize_var (decl); 490318334Speter 490490287Sobrien make_rtl_for_nonlocal_decl (decl, init, asmspec); 490551412Sobrien 490690287Sobrien if (TREE_CODE (type) == FUNCTION_TYPE 490790287Sobrien || TREE_CODE (type) == METHOD_TYPE) 490890287Sobrien abstract_virtuals_error (decl, 490990287Sobrien strip_array_types (TREE_TYPE (type))); 4910132747Skan else if (POINTER_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE) 4911132747Skan { 4912132747Skan /* If it's either a pointer or an array type, strip through all 4913132747Skan of them but the last one. If the last is an array type, issue 4914132747Skan an error if the element type is abstract. */ 4915132747Skan while (POINTER_TYPE_P (TREE_TYPE (type)) 4916132747Skan || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE) 4917132747Skan type = TREE_TYPE (type); 4918132747Skan if (TREE_CODE (type) == ARRAY_TYPE) 4919132747Skan abstract_virtuals_error (decl, TREE_TYPE (type)); 4920132747Skan } 492190287Sobrien else 4922132747Skan abstract_virtuals_error (decl, type); 492351412Sobrien 4924117410Skan if (TREE_CODE (decl) == FUNCTION_DECL 4925117410Skan || TREE_TYPE (decl) == error_mark_node) 4926117410Skan /* No initialization required. */ 492790287Sobrien ; 492890287Sobrien else if (DECL_EXTERNAL (decl) 492990287Sobrien && ! (DECL_LANG_SPECIFIC (decl) 493090287Sobrien && DECL_NOT_REALLY_EXTERN (decl))) 493190287Sobrien { 493290287Sobrien if (init) 493390287Sobrien DECL_INITIAL (decl) = init; 493490287Sobrien } 4935117410Skan else 493690287Sobrien { 4937117410Skan /* A variable definition. */ 4938117410Skan if (DECL_FUNCTION_SCOPE_P (decl)) 493951412Sobrien { 4940117410Skan /* This is a local declaration. */ 4941132747Skan maybe_inject_for_scope_var (decl); 4942117410Skan /* Initialize the local variable. */ 4943117410Skan if (processing_template_decl) 4944117410Skan { 4945117410Skan if (init || DECL_INITIAL (decl) == error_mark_node) 4946117410Skan DECL_INITIAL (decl) = init; 4947117410Skan } 4948117410Skan else if (!TREE_STATIC (decl)) 4949117410Skan initialize_local_var (decl, init); 495018334Speter } 4951117410Skan 4952117410Skan if (TREE_STATIC (decl)) 495390287Sobrien expand_static_init (decl, init); 495490287Sobrien } 495518334Speter finish_end0: 495618334Speter 495718334Speter /* Undo call to `pushclass' that was done in `start_decl' 495818334Speter due to initialization of qualified member variable. 495918334Speter I.e., Foo::x = 10; */ 496018334Speter { 496190287Sobrien tree context = CP_DECL_CONTEXT (decl); 496218334Speter if (context 496390287Sobrien && TYPE_P (context) 496418334Speter && (TREE_CODE (decl) == VAR_DECL 496518334Speter /* We also have a pushclass done that we need to undo here 496618334Speter if we're at top level and declare a method. */ 496751412Sobrien || TREE_CODE (decl) == FUNCTION_DECL) 496851412Sobrien /* If size hasn't been set, we're still defining it, 496951412Sobrien and therefore inside the class body; don't pop 497051412Sobrien the binding level.. */ 497190287Sobrien && COMPLETE_TYPE_P (context) 497251412Sobrien && context == current_class_type) 497352290Sobrien pop_nested_class (); 497418334Speter } 497518334Speter } 497618334Speter 4977122192Skan /* If a CLEANUP_STMT was created to destroy a temporary bound to a 4978122192Skan reference, insert it in the statement-tree now. */ 4979122192Skan if (cleanup) 4980122192Skan add_stmt (cleanup); 4981122192Skan 498218334Speter finish_end: 498318334Speter 498490287Sobrien if (was_readonly) 498590287Sobrien TREE_READONLY (decl) = 1; 4986132747Skan 4987132747Skan /* If this was marked 'used', be sure it will be output. */ 4988132747Skan if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) 4989132747Skan mark_referenced (DECL_ASSEMBLER_NAME (decl)); 499090287Sobrien} 499118334Speter 4992132747Skan/* This is here for a midend callback from c-common.c. */ 499390287Sobrien 499490287Sobrienvoid 4995132747Skanfinish_decl (tree decl, tree init, tree asmspec_tree) 499690287Sobrien{ 499790287Sobrien cp_finish_decl (decl, init, asmspec_tree, 0); 499890287Sobrien} 499990287Sobrien 500090287Sobrien/* Returns a declaration for a VAR_DECL as if: 500190287Sobrien 500290287Sobrien extern "C" TYPE NAME; 500390287Sobrien 500490287Sobrien had been seen. Used to create compiler-generated global 500590287Sobrien variables. */ 500690287Sobrien 500790287Sobrientree 5008132747Skandeclare_global_var (tree name, tree type) 500990287Sobrien{ 501090287Sobrien tree decl; 501190287Sobrien 501290287Sobrien push_to_top_level (); 501390287Sobrien decl = build_decl (VAR_DECL, name, type); 501490287Sobrien TREE_PUBLIC (decl) = 1; 501590287Sobrien DECL_EXTERNAL (decl) = 1; 501690287Sobrien DECL_ARTIFICIAL (decl) = 1; 501790287Sobrien pushdecl (decl); 501890287Sobrien cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0); 501990287Sobrien pop_from_top_level (); 502090287Sobrien 502190287Sobrien return decl; 502290287Sobrien} 502390287Sobrien 502490287Sobrien/* Returns a pointer to the `atexit' function. Note that if 5025117410Skan FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new 502690287Sobrien `__cxa_atexit' function specified in the IA64 C++ ABI. */ 502790287Sobrien 502890287Sobrienstatic tree 5029132747Skanget_atexit_node (void) 503090287Sobrien{ 503190287Sobrien tree atexit_fndecl; 503290287Sobrien tree arg_types; 503390287Sobrien tree fn_type; 503490287Sobrien tree fn_ptr_type; 503590287Sobrien const char *name; 503690287Sobrien 503790287Sobrien if (atexit_node) 503890287Sobrien return atexit_node; 503990287Sobrien 504090287Sobrien if (flag_use_cxa_atexit) 504118334Speter { 504290287Sobrien /* The declaration for `__cxa_atexit' is: 504318334Speter 504490287Sobrien int __cxa_atexit (void (*)(void *), void *, void *) 504518334Speter 504690287Sobrien We build up the argument types and then then function type 504790287Sobrien itself. */ 504890287Sobrien 504990287Sobrien /* First, build the pointer-to-function type for the first 505090287Sobrien argument. */ 505190287Sobrien arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); 505290287Sobrien fn_type = build_function_type (void_type_node, arg_types); 505390287Sobrien fn_ptr_type = build_pointer_type (fn_type); 505490287Sobrien /* Then, build the rest of the argument types. */ 505590287Sobrien arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); 505690287Sobrien arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); 505790287Sobrien arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); 505890287Sobrien /* And the final __cxa_atexit type. */ 505990287Sobrien fn_type = build_function_type (integer_type_node, arg_types); 506090287Sobrien fn_ptr_type = build_pointer_type (fn_type); 506190287Sobrien name = "__cxa_atexit"; 506218334Speter } 506390287Sobrien else 506490287Sobrien { 506590287Sobrien /* The declaration for `atexit' is: 506618334Speter 506790287Sobrien int atexit (void (*)()); 506890287Sobrien 506990287Sobrien We build up the argument types and then then function type 507090287Sobrien itself. */ 507190287Sobrien fn_type = build_function_type (void_type_node, void_list_node); 507290287Sobrien fn_ptr_type = build_pointer_type (fn_type); 507390287Sobrien arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node); 507490287Sobrien /* Build the final atexit type. */ 507590287Sobrien fn_type = build_function_type (integer_type_node, arg_types); 507690287Sobrien name = "atexit"; 507790287Sobrien } 507890287Sobrien 507990287Sobrien /* Now, build the function declaration. */ 508090287Sobrien push_lang_context (lang_name_c); 508190287Sobrien atexit_fndecl = build_library_fn_ptr (name, fn_type); 508290287Sobrien mark_used (atexit_fndecl); 508390287Sobrien pop_lang_context (); 5084132747Skan atexit_node = decay_conversion (atexit_fndecl); 508590287Sobrien 508690287Sobrien return atexit_node; 508790287Sobrien} 508890287Sobrien 508990287Sobrien/* Returns the __dso_handle VAR_DECL. */ 509090287Sobrien 509190287Sobrienstatic tree 5092132747Skanget_dso_handle_node (void) 509390287Sobrien{ 509490287Sobrien if (dso_handle_node) 509590287Sobrien return dso_handle_node; 509690287Sobrien 509790287Sobrien /* Declare the variable. */ 509890287Sobrien dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), 509990287Sobrien ptr_type_node); 510090287Sobrien 510190287Sobrien return dso_handle_node; 510290287Sobrien} 510390287Sobrien 510490287Sobrien/* Begin a new function with internal linkage whose job will be simply 510590287Sobrien to destroy some particular variable. */ 510690287Sobrien 5107132747Skanstatic GTY(()) int start_cleanup_cnt; 5108132747Skan 510990287Sobrienstatic tree 5110132747Skanstart_cleanup_fn (void) 511190287Sobrien{ 5112103455Skan int old_interface_only = interface_only; 511390287Sobrien int old_interface_unknown = interface_unknown; 511490287Sobrien char name[32]; 511590287Sobrien tree parmtypes; 511690287Sobrien tree fntype; 511790287Sobrien tree fndecl; 511890287Sobrien 511990287Sobrien push_to_top_level (); 512090287Sobrien 512190287Sobrien /* No need to mangle this. */ 512290287Sobrien push_lang_context (lang_name_c); 512390287Sobrien 5124103455Skan interface_only = 0; 512590287Sobrien interface_unknown = 1; 512690287Sobrien 512790287Sobrien /* Build the parameter-types. */ 512890287Sobrien parmtypes = void_list_node; 512990287Sobrien /* Functions passed to __cxa_atexit take an additional parameter. 513090287Sobrien We'll just ignore it. After we implement the new calling 513190287Sobrien convention for destructors, we can eliminate the use of 513290287Sobrien additional cleanup functions entirely in the -fnew-abi case. */ 513390287Sobrien if (flag_use_cxa_atexit) 513490287Sobrien parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes); 513590287Sobrien /* Build the function type itself. */ 513690287Sobrien fntype = build_function_type (void_type_node, parmtypes); 513790287Sobrien /* Build the name of the function. */ 5138132747Skan sprintf (name, "__tcf_%d", start_cleanup_cnt++); 513990287Sobrien /* Build the function declaration. */ 514090287Sobrien fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); 514190287Sobrien /* It's a function with internal linkage, generated by the 514290287Sobrien compiler. */ 514390287Sobrien TREE_PUBLIC (fndecl) = 0; 514490287Sobrien DECL_ARTIFICIAL (fndecl) = 1; 514590287Sobrien /* Make the function `inline' so that it is only emitted if it is 514690287Sobrien actually needed. It is unlikely that it will be inlined, since 514790287Sobrien it is only called via a function pointer, but we avoid unnecessary 514890287Sobrien emissions this way. */ 514990287Sobrien DECL_INLINE (fndecl) = 1; 5150132747Skan DECL_DECLARED_INLINE_P (fndecl) = 1; 5151132747Skan DECL_INTERFACE_KNOWN (fndecl) = 1; 515290287Sobrien /* Build the parameter. */ 515390287Sobrien if (flag_use_cxa_atexit) 515418334Speter { 515590287Sobrien tree parmdecl; 515690287Sobrien 5157117410Skan parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node); 515890287Sobrien DECL_CONTEXT (parmdecl) = fndecl; 515990287Sobrien TREE_USED (parmdecl) = 1; 516090287Sobrien DECL_ARGUMENTS (fndecl) = parmdecl; 516118334Speter } 516218334Speter 516390287Sobrien pushdecl (fndecl); 516490287Sobrien start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED); 516590287Sobrien 516690287Sobrien interface_unknown = old_interface_unknown; 5167103455Skan interface_only = old_interface_only; 516890287Sobrien 516990287Sobrien pop_lang_context (); 517090287Sobrien 517190287Sobrien return current_function_decl; 517218334Speter} 517318334Speter 517490287Sobrien/* Finish the cleanup function begun by start_cleanup_fn. */ 517551412Sobrien 517690287Sobrienstatic void 5177132747Skanend_cleanup_fn (void) 517890287Sobrien{ 5179132747Skan expand_or_defer_fn (finish_function (0)); 518090287Sobrien 518190287Sobrien pop_from_top_level (); 518290287Sobrien} 518390287Sobrien 518490287Sobrien/* Generate code to handle the destruction of DECL, an object with 518590287Sobrien static storage duration. */ 518690287Sobrien 518718334Spetervoid 5188132747Skanregister_dtor_fn (tree decl) 518918334Speter{ 519090287Sobrien tree cleanup; 519190287Sobrien tree compound_stmt; 519290287Sobrien tree args; 519390287Sobrien tree fcall; 519490287Sobrien 519590287Sobrien if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 519690287Sobrien return; 519790287Sobrien 519890287Sobrien /* Call build_cleanup before we enter the anonymous function so that 519990287Sobrien any access checks will be done relative to the current scope, 520090287Sobrien rather than the scope of the anonymous function. */ 520190287Sobrien build_cleanup (decl); 520290287Sobrien 520390287Sobrien /* Now start the function. */ 520490287Sobrien cleanup = start_cleanup_fn (); 520590287Sobrien 520690287Sobrien /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer 520790287Sobrien to the original function, rather than the anonymous one. That 520890287Sobrien will make the back-end think that nested functions are in use, 520990287Sobrien which causes confusion. */ 5210132747Skan 5211132747Skan push_deferring_access_checks (dk_no_check); 521290287Sobrien fcall = build_cleanup (decl); 5213132747Skan pop_deferring_access_checks (); 521490287Sobrien 521590287Sobrien /* Create the body of the anonymous function. */ 5216132747Skan compound_stmt = begin_compound_stmt (/*has_no_scope=*/false); 521790287Sobrien finish_expr_stmt (fcall); 5218132747Skan finish_compound_stmt (compound_stmt); 521990287Sobrien end_cleanup_fn (); 522090287Sobrien 522190287Sobrien /* Call atexit with the cleanup function. */ 5222117410Skan cxx_mark_addressable (cleanup); 5223132747Skan mark_used (cleanup); 522490287Sobrien cleanup = build_unary_op (ADDR_EXPR, cleanup, 0); 522590287Sobrien if (flag_use_cxa_atexit) 522690287Sobrien { 522796292Sobrien args = tree_cons (NULL_TREE, 522896292Sobrien build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0), 522996292Sobrien NULL_TREE); 523090287Sobrien args = tree_cons (NULL_TREE, null_pointer_node, args); 523190287Sobrien args = tree_cons (NULL_TREE, cleanup, args); 523290287Sobrien } 523390287Sobrien else 523490287Sobrien args = tree_cons (NULL_TREE, cleanup, NULL_TREE); 523590287Sobrien finish_expr_stmt (build_function_call (get_atexit_node (), args)); 523618334Speter} 523718334Speter 5238117410Skan/* DECL is a VAR_DECL with static storage duration. INIT, if present, 5239117410Skan is its initializer. Generate code to handle the construction 5240117410Skan and destruction of DECL. */ 5241117410Skan 5242117410Skanstatic void 5243132747Skanexpand_static_init (tree decl, tree init) 524418334Speter{ 5245117410Skan my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010); 5246117410Skan my_friendly_assert (TREE_STATIC (decl), 20021010); 5247117410Skan 5248117410Skan /* Some variables require no initialization. */ 5249117410Skan if (!init 5250117410Skan && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) 5251117410Skan && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 5252117410Skan return; 5253117410Skan 5254132747Skan if (! toplevel_bindings_p ()) 525518334Speter { 525618334Speter /* Emit code to perform this initialization but once. */ 525790287Sobrien tree if_stmt; 525890287Sobrien tree then_clause; 525990287Sobrien tree assignment; 526090287Sobrien tree guard; 526190287Sobrien tree guard_init; 526218334Speter 526352290Sobrien /* Emit code to perform this initialization but once. This code 526452290Sobrien looks like: 526552290Sobrien 526690287Sobrien static int guard = 0; 526790287Sobrien if (!guard) { 526852290Sobrien // Do initialization. 526990287Sobrien guard = 1; 527052290Sobrien // Register variable for destruction at end of program. 527152290Sobrien } 527252290Sobrien 527352290Sobrien Note that the `temp' variable is only set to 1 *after* the 527452290Sobrien initialization is complete. This ensures that an exception, 527552290Sobrien thrown during the construction, will cause the variable to 527652290Sobrien reinitialized when we pass through this code again, as per: 527790287Sobrien 527852290Sobrien [stmt.dcl] 527952290Sobrien 528052290Sobrien If the initialization exits by throwing an exception, the 528152290Sobrien initialization is not complete, so it will be tried again 528252290Sobrien the next time control enters the declaration. 528352290Sobrien 528452290Sobrien In theory, this process should be thread-safe, too; multiple 528552290Sobrien threads should not be able to initialize the variable more 528652290Sobrien than once. We don't yet attempt to ensure thread-safety. */ 528752290Sobrien 528890287Sobrien /* Create the guard variable. */ 528990287Sobrien guard = get_guard (decl); 529090287Sobrien 529152290Sobrien /* Begin the conditional initialization. */ 529290287Sobrien if_stmt = begin_if_stmt (); 529390287Sobrien finish_if_stmt_cond (get_guard_cond (guard), if_stmt); 5294132747Skan then_clause = begin_compound_stmt (/*has_no_scope=*/false); 529551412Sobrien 529652290Sobrien /* Do the initialization itself. */ 5297117410Skan assignment = init ? init : NULL_TREE; 529890287Sobrien 529990287Sobrien /* Once the assignment is complete, set TEMP to 1. Since the 530090287Sobrien construction of the static object is complete at this point, 530190287Sobrien we want to make sure TEMP is set to 1 even if a temporary 530290287Sobrien constructed during the initialization throws an exception 530390287Sobrien when it is destroyed. So, we combine the initialization and 530490287Sobrien the assignment to TEMP into a single expression, ensuring 530590287Sobrien that when we call finish_expr_stmt the cleanups will not be 530690287Sobrien run until after TEMP is set to 1. */ 530790287Sobrien guard_init = set_guard (guard); 530890287Sobrien if (assignment) 5309132747Skan assignment = build_compound_expr (assignment, guard_init); 531090287Sobrien else 531190287Sobrien assignment = guard_init; 531290287Sobrien finish_expr_stmt (assignment); 531318334Speter 531452290Sobrien /* Use atexit to register a function for destroying this static 531552290Sobrien variable. */ 531690287Sobrien register_dtor_fn (decl); 531752290Sobrien 5318132747Skan finish_compound_stmt (then_clause); 531990287Sobrien finish_then_clause (if_stmt); 532090287Sobrien finish_if_stmt (); 532190287Sobrien } 532290287Sobrien else 532390287Sobrien static_aggregates = tree_cons (init, decl, static_aggregates); 532490287Sobrien} 532551412Sobrien 532690287Sobrien/* Finish the declaration of a catch-parameter. */ 532751412Sobrien 532890287Sobrientree 5329132747Skanstart_handler_parms (tree declspecs, tree declarator) 533090287Sobrien{ 533190287Sobrien tree decl; 533290287Sobrien if (declspecs) 533390287Sobrien { 533490287Sobrien decl = grokdeclarator (declarator, declspecs, CATCHPARM, 533590287Sobrien 1, NULL); 533690287Sobrien if (decl == NULL_TREE) 533790287Sobrien error ("invalid catch parameter"); 533818334Speter } 533918334Speter else 534090287Sobrien decl = NULL_TREE; 534190287Sobrien 534290287Sobrien return decl; 534318334Speter} 534490287Sobrien 534518334Speter 534618334Speter/* Make TYPE a complete type based on INITIAL_VALUE. 534718334Speter Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 534852290Sobrien 2 if there was no information (in which case assume 0 if DO_DEFAULT). */ 534918334Speter 535018334Speterint 5351132747Skancomplete_array_type (tree type, tree initial_value, int do_default) 535218334Speter{ 5353132747Skan tree maxindex = NULL_TREE; 535418334Speter int value = 0; 535590287Sobrien 535618334Speter if (initial_value) 535718334Speter { 535890287Sobrien /* An array of character type can be initialized from a 535990287Sobrien brace-enclosed string constant. */ 536090287Sobrien if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) 536190287Sobrien && TREE_CODE (initial_value) == CONSTRUCTOR 536290287Sobrien && CONSTRUCTOR_ELTS (initial_value) 536390287Sobrien && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value))) 536490287Sobrien == STRING_CST) 536590287Sobrien && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE) 536690287Sobrien initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)); 536790287Sobrien 536890287Sobrien /* Note MAXINDEX is really the maximum index, one less than the 536990287Sobrien size. */ 537018334Speter if (TREE_CODE (initial_value) == STRING_CST) 537118334Speter { 537218334Speter int eltsize 537318334Speter = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); 537418334Speter maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) 537518334Speter / eltsize) - 1, 0); 537618334Speter } 537718334Speter else if (TREE_CODE (initial_value) == CONSTRUCTOR) 537818334Speter { 537918334Speter tree elts = CONSTRUCTOR_ELTS (initial_value); 538090287Sobrien 538190287Sobrien maxindex = ssize_int (-1); 538218334Speter for (; elts; elts = TREE_CHAIN (elts)) 538318334Speter { 538418334Speter if (TREE_PURPOSE (elts)) 538518334Speter maxindex = TREE_PURPOSE (elts); 538618334Speter else 538790287Sobrien maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1)); 538818334Speter } 538918334Speter maxindex = copy_node (maxindex); 539018334Speter } 539118334Speter else 539218334Speter { 539318334Speter /* Make an error message unless that happened already. */ 539418334Speter if (initial_value != error_mark_node) 539518334Speter value = 1; 539652290Sobrien else 539752290Sobrien initial_value = NULL_TREE; 539818334Speter 539918334Speter /* Prevent further error messages. */ 540018334Speter maxindex = build_int_2 (0, 0); 540118334Speter } 540218334Speter } 540318334Speter 540418334Speter if (!maxindex) 540518334Speter { 540618334Speter if (do_default) 540718334Speter maxindex = build_int_2 (0, 0); 540818334Speter value = 2; 540918334Speter } 541018334Speter 541118334Speter if (maxindex) 541218334Speter { 541318334Speter tree itype; 541452290Sobrien tree domain; 541518334Speter 541652290Sobrien domain = build_index_type (maxindex); 541752290Sobrien TYPE_DOMAIN (type) = domain; 541852290Sobrien 541918334Speter if (! TREE_TYPE (maxindex)) 542052290Sobrien TREE_TYPE (maxindex) = domain; 542118334Speter if (initial_value) 542218334Speter itype = TREE_TYPE (initial_value); 542318334Speter else 542418334Speter itype = NULL; 542518334Speter if (itype && !TYPE_DOMAIN (itype)) 542652290Sobrien TYPE_DOMAIN (itype) = domain; 542718334Speter /* The type of the main variant should never be used for arrays 542818334Speter of different sizes. It should only ever be completed with the 542918334Speter size of the array. */ 543018334Speter if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type))) 543152290Sobrien TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain; 543218334Speter } 543318334Speter 543418334Speter /* Lay out the type now that we can get the real answer. */ 543518334Speter 543618334Speter layout_type (type); 543718334Speter 543818334Speter return value; 543918334Speter} 544018334Speter 544118334Speter/* Return zero if something is declared to be a member of type 544218334Speter CTYPE when in the context of CUR_TYPE. STRING is the error 544318334Speter message to print in that case. Otherwise, quietly return 1. */ 544451412Sobrien 544518334Speterstatic int 5446132747Skanmember_function_or_else (tree ctype, tree cur_type, enum overload_flags flags) 544718334Speter{ 544818334Speter if (ctype && ctype != cur_type) 544918334Speter { 545090287Sobrien if (flags == DTOR_FLAG) 545190287Sobrien error ("destructor for alien class `%T' cannot be a member", 545290287Sobrien ctype); 545390287Sobrien else 545490287Sobrien error ("constructor for alien class `%T' cannot be a member", 545590287Sobrien ctype); 545618334Speter return 0; 545718334Speter } 545818334Speter return 1; 545918334Speter} 546018334Speter 546118334Speter/* Subroutine of `grokdeclarator'. */ 546218334Speter 546318334Speter/* Generate errors possibly applicable for a given set of specifiers. 546418334Speter This is for ARM $7.1.2. */ 546551412Sobrien 546618334Speterstatic void 5467132747Skanbad_specifiers (tree object, 5468132747Skan const char* type, 5469132747Skan int virtualp, 5470132747Skan int quals, 5471132747Skan int inlinep, 5472132747Skan int friendp, 5473132747Skan int raises) 547418334Speter{ 547518334Speter if (virtualp) 547690287Sobrien error ("`%D' declared as a `virtual' %s", object, type); 547718334Speter if (inlinep) 547890287Sobrien error ("`%D' declared as an `inline' %s", object, type); 547918334Speter if (quals) 548090287Sobrien error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration", 548118334Speter object, type); 548218334Speter if (friendp) 548390287Sobrien cp_error_at ("`%D' declared as a friend", object); 548490287Sobrien if (raises 548590287Sobrien && (TREE_CODE (object) == TYPE_DECL 548690287Sobrien || (!TYPE_PTRFN_P (TREE_TYPE (object)) 548790287Sobrien && !TYPE_REFFN_P (TREE_TYPE (object)) 548890287Sobrien && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) 548990287Sobrien cp_error_at ("`%D' declared with an exception specification", object); 549018334Speter} 549118334Speter 549218334Speter/* CTYPE is class type, or null if non-class. 549318334Speter TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE 549418334Speter or METHOD_TYPE. 549518334Speter DECLARATOR is the function's name. 5496132747Skan PARMS is a chain of PARM_DECLs for the function. 549718334Speter VIRTUALP is truthvalue of whether the function is virtual or not. 549818334Speter FLAGS are to be passed through to `grokclassfn'. 549918334Speter QUALS are qualifiers indicating whether the function is `const' 550018334Speter or `volatile'. 550118334Speter RAISES is a list of exceptions that this function can raise. 550218334Speter CHECK is 1 if we must find this method in CTYPE, 0 if we should 550390287Sobrien not look, and -1 if we should not call `grokclassfn' at all. 550451412Sobrien 550552290Sobrien Returns `NULL_TREE' if something goes wrong, after issuing 550652290Sobrien applicable error messages. */ 550752290Sobrien 550818334Speterstatic tree 5509132747Skangrokfndecl (tree ctype, 5510132747Skan tree type, 5511132747Skan tree declarator, 5512132747Skan tree parms, 5513132747Skan tree orig_declarator, 5514132747Skan int virtualp, 5515132747Skan enum overload_flags flags, 5516132747Skan tree quals, 5517132747Skan tree raises, 5518132747Skan int check, 5519132747Skan int friendp, 5520132747Skan int publicp, 5521132747Skan int inlinep, 5522132747Skan int funcdef_flag, 5523132747Skan int template_count, 5524132747Skan tree in_namespace) 552518334Speter{ 552690287Sobrien tree decl; 552718334Speter int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; 552852290Sobrien int has_default_arg = 0; 552951412Sobrien tree t; 553018334Speter 553118334Speter if (raises) 553290287Sobrien type = build_exception_variant (type, raises); 553351412Sobrien 553418334Speter decl = build_lang_decl (FUNCTION_DECL, declarator, type); 5535132747Skan DECL_ARGUMENTS (decl) = parms; 5536117410Skan /* Propagate volatile out from type to decl. */ 553718334Speter if (TYPE_VOLATILE (type)) 553851412Sobrien TREE_THIS_VOLATILE (decl) = 1; 553918334Speter 554052290Sobrien /* If this decl has namespace scope, set that up. */ 554151412Sobrien if (in_namespace) 554252290Sobrien set_decl_namespace (decl, in_namespace, friendp); 554390287Sobrien else if (!ctype) 554452290Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 554551412Sobrien 554652290Sobrien /* `main' and builtins have implicit 'C' linkage. */ 554752290Sobrien if ((MAIN_NAME_P (declarator) 554852290Sobrien || (IDENTIFIER_LENGTH (declarator) > 10 554952290Sobrien && IDENTIFIER_POINTER (declarator)[0] == '_' 555052290Sobrien && IDENTIFIER_POINTER (declarator)[1] == '_' 555152290Sobrien && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)) 555252290Sobrien && current_lang_name == lang_name_cplusplus 555352290Sobrien && ctype == NULL_TREE 555452290Sobrien /* NULL_TREE means global namespace. */ 555552290Sobrien && DECL_CONTEXT (decl) == NULL_TREE) 555690287Sobrien SET_DECL_LANGUAGE (decl, lang_c); 555752290Sobrien 555818334Speter /* Should probably propagate const out from type to decl I bet (mrs). */ 555918334Speter if (staticp) 556018334Speter { 556118334Speter DECL_STATIC_FUNCTION_P (decl) = 1; 556218334Speter DECL_CONTEXT (decl) = ctype; 556318334Speter } 556418334Speter 556551412Sobrien if (ctype) 556690287Sobrien DECL_CONTEXT (decl) = ctype; 556718334Speter 556852290Sobrien if (ctype == NULL_TREE && DECL_MAIN_P (decl)) 556918334Speter { 557052290Sobrien if (processing_template_decl) 557190287Sobrien error ("cannot declare `::main' to be a template"); 557218334Speter if (inlinep) 557390287Sobrien error ("cannot declare `::main' to be inline"); 557490287Sobrien if (!publicp) 557590287Sobrien error ("cannot declare `::main' to be static"); 557690287Sobrien if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), 557790287Sobrien integer_type_node)) 557890287Sobrien error ("`main' must return `int'"); 557918334Speter inlinep = 0; 558018334Speter publicp = 1; 558118334Speter } 558252290Sobrien 558352290Sobrien /* Members of anonymous types and local classes have no linkage; make 558452290Sobrien them internal. */ 558590287Sobrien /* FIXME what if it gets a name from typedef? */ 558690287Sobrien if (ctype && (TYPE_ANONYMOUS_P (ctype) 558790287Sobrien || decl_function_context (TYPE_MAIN_DECL (ctype)))) 558852290Sobrien publicp = 0; 558952290Sobrien 559052290Sobrien if (publicp) 559152290Sobrien { 559252290Sobrien /* [basic.link]: A name with no linkage (notably, the name of a class 559352290Sobrien or enumeration declared in a local scope) shall not be used to 559452290Sobrien declare an entity with linkage. 559552290Sobrien 5596132747Skan Only check this for public decls for now. See core 319, 389. */ 559752290Sobrien t = no_linkage_check (TREE_TYPE (decl)); 559852290Sobrien if (t) 559952290Sobrien { 560090287Sobrien if (TYPE_ANONYMOUS_P (t)) 560152290Sobrien { 560290287Sobrien if (DECL_EXTERN_C_P (decl)) 560352290Sobrien /* Allow this; it's pretty common in C. */; 560452290Sobrien else 560590287Sobrien { 560690287Sobrien pedwarn ("non-local function `%#D' uses anonymous type", 560790287Sobrien decl); 560890287Sobrien if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) 560990287Sobrien cp_pedwarn_at ("\ 561090287Sobrien`%#D' does not refer to the unqualified type, so it is not used for linkage", 561190287Sobrien TYPE_NAME (t)); 561290287Sobrien } 561352290Sobrien } 561452290Sobrien else 561590287Sobrien pedwarn ("non-local function `%#D' uses local type `%T'", 561652290Sobrien decl, t); 561752290Sobrien } 561852290Sobrien } 561952290Sobrien 562051412Sobrien TREE_PUBLIC (decl) = publicp; 562118334Speter if (! publicp) 562251412Sobrien { 562351412Sobrien DECL_INTERFACE_KNOWN (decl) = 1; 562451412Sobrien DECL_NOT_REALLY_EXTERN (decl) = 1; 562551412Sobrien } 562618334Speter 562790287Sobrien /* If the declaration was declared inline, mark it as such. */ 562818334Speter if (inlinep) 562990287Sobrien DECL_DECLARED_INLINE_P (decl) = 1; 563090287Sobrien /* We inline functions that are explicitly declared inline, or, when 563190287Sobrien the user explicitly asks us to, all functions. */ 5632132747Skan if (DECL_DECLARED_INLINE_P (decl) 5633132747Skan || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag)) 563490287Sobrien DECL_INLINE (decl) = 1; 563518334Speter 563618334Speter DECL_EXTERNAL (decl) = 1; 563718334Speter if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE) 563818334Speter { 563990287Sobrien error ("%smember function `%D' cannot have `%T' method qualifier", 564018334Speter (ctype ? "static " : "non-"), decl, TREE_VALUE (quals)); 564118334Speter quals = NULL_TREE; 564218334Speter } 564318334Speter 564418334Speter if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))) 5645132747Skan grok_op_properties (decl, friendp, /*complain=*/true); 564618334Speter 564790287Sobrien if (ctype && decl_function_context (decl)) 564851412Sobrien DECL_NO_STATIC_CHAIN (decl) = 1; 564951412Sobrien 565051412Sobrien for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) 565151412Sobrien if (TREE_PURPOSE (t) 565251412Sobrien && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) 565351412Sobrien { 565452290Sobrien has_default_arg = 1; 565551412Sobrien break; 565651412Sobrien } 565751412Sobrien 565851412Sobrien if (friendp 565951412Sobrien && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) 566051412Sobrien { 566151412Sobrien if (funcdef_flag) 566290287Sobrien error 566351412Sobrien ("defining explicit specialization `%D' in friend declaration", 566451412Sobrien orig_declarator); 566551412Sobrien else 566651412Sobrien { 566790287Sobrien tree fns = TREE_OPERAND (orig_declarator, 0); 566890287Sobrien tree args = TREE_OPERAND (orig_declarator, 1); 566990287Sobrien 567052290Sobrien if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 567152290Sobrien { 567252290Sobrien /* Something like `template <class T> friend void f<T>()'. */ 567390287Sobrien error ("invalid use of template-id `%D' in declaration of primary template", 567452290Sobrien orig_declarator); 567552290Sobrien return NULL_TREE; 567652290Sobrien } 567752290Sobrien 567852290Sobrien 567951412Sobrien /* A friend declaration of the form friend void f<>(). Record 568051412Sobrien the information in the TEMPLATE_ID_EXPR. */ 568151412Sobrien SET_DECL_IMPLICIT_INSTANTIATION (decl); 568252290Sobrien 568390287Sobrien if (TREE_CODE (fns) == COMPONENT_REF) 568490287Sobrien { 568590287Sobrien /* Due to bison parser ickiness, we will have already looked 568690287Sobrien up an operator_name or PFUNCNAME within the current class 568790287Sobrien (see template_id in parse.y). If the current class contains 5688117410Skan such a name, we'll get a COMPONENT_REF here. Undo that. */ 568990287Sobrien 569090287Sobrien my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0)) 569190287Sobrien == current_class_type, 20001120); 569290287Sobrien fns = TREE_OPERAND (fns, 1); 569390287Sobrien } 569490287Sobrien my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE 569590287Sobrien || TREE_CODE (fns) == OVERLOAD, 20001120); 569690287Sobrien DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE); 569790287Sobrien 569852290Sobrien if (has_default_arg) 569952290Sobrien { 570090287Sobrien error ("default arguments are not allowed in declaration of friend template specialization `%D'", 570152290Sobrien decl); 570252290Sobrien return NULL_TREE; 570352290Sobrien } 570452290Sobrien 570552290Sobrien if (inlinep) 570652290Sobrien { 570790287Sobrien error ("`inline' is not allowed in declaration of friend template specialization `%D'", 570852290Sobrien decl); 570952290Sobrien return NULL_TREE; 571052290Sobrien } 571151412Sobrien } 571251412Sobrien } 571351412Sobrien 571452290Sobrien if (funcdef_flag) 571552290Sobrien /* Make the init_value nonzero so pushdecl knows this is not 571652290Sobrien tentative. error_mark_node is replaced later with the BLOCK. */ 571752290Sobrien DECL_INITIAL (decl) = error_mark_node; 571852290Sobrien 571990287Sobrien if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) 572090287Sobrien TREE_NOTHROW (decl) = 1; 572190287Sobrien 572218334Speter /* Caller will do the rest of this. */ 572318334Speter if (check < 0) 572418334Speter return decl; 572518334Speter 5726132747Skan if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype)) 572790287Sobrien DECL_CONSTRUCTOR_P (decl) = 1; 572851412Sobrien 572990287Sobrien /* Function gets the ugly name, field gets the nice one. This call 573090287Sobrien may change the type of the function (because of default 573190287Sobrien parameters)! */ 573290287Sobrien if (ctype != NULL_TREE) 573390287Sobrien grokclassfn (ctype, decl, flags, quals); 573418334Speter 573590287Sobrien decl = check_explicit_specialization (orig_declarator, decl, 573690287Sobrien template_count, 573790287Sobrien 2 * (funcdef_flag != 0) + 573890287Sobrien 4 * (friendp != 0)); 573990287Sobrien if (decl == error_mark_node) 574090287Sobrien return NULL_TREE; 574151412Sobrien 574290287Sobrien if (ctype != NULL_TREE 574390287Sobrien && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl)) 574490287Sobrien && check) 574590287Sobrien { 574690287Sobrien tree old_decl; 574770639Sobrien 5748132747Skan old_decl = check_classfn (ctype, decl, 5749132747Skan processing_template_decl 5750132747Skan > template_class_depth (ctype)); 575151412Sobrien 575290287Sobrien if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL) 575390287Sobrien /* Because grokfndecl is always supposed to return a 575490287Sobrien FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT 575590287Sobrien here. We depend on our callers to figure out that its 575690287Sobrien really a template that's being returned. */ 575790287Sobrien old_decl = DECL_TEMPLATE_RESULT (old_decl); 575870639Sobrien 575990287Sobrien if (old_decl && DECL_STATIC_FUNCTION_P (old_decl) 576090287Sobrien && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 5761132747Skan /* Remove the `this' parm added by grokclassfn. 5762132747Skan XXX Isn't this done in start_function, too? */ 5763132747Skan revert_static_member_fn (decl); 576490287Sobrien if (old_decl && DECL_ARTIFICIAL (old_decl)) 576590287Sobrien error ("definition of implicitly-declared `%D'", old_decl); 576651412Sobrien 576790287Sobrien if (old_decl) 576890287Sobrien { 5769132747Skan tree ok; 5770132747Skan bool pop_p; 5771132747Skan 577290287Sobrien /* Since we've smashed OLD_DECL to its 577390287Sobrien DECL_TEMPLATE_RESULT, we must do the same to DECL. */ 577490287Sobrien if (TREE_CODE (decl) == TEMPLATE_DECL) 577590287Sobrien decl = DECL_TEMPLATE_RESULT (decl); 577651412Sobrien 577790287Sobrien /* Attempt to merge the declarations. This can fail, in 5778117410Skan the case of some invalid specialization declarations. */ 5779132747Skan pop_p = push_scope (ctype); 5780132747Skan ok = duplicate_decls (decl, old_decl); 5781132747Skan if (pop_p) 5782132747Skan pop_scope (ctype); 5783132747Skan if (!ok) 5784132747Skan { 5785132747Skan error ("no `%#D' member function declared in class `%T'", 5786132747Skan decl, ctype); 5787132747Skan return NULL_TREE; 5788132747Skan } 578990287Sobrien return old_decl; 579051412Sobrien } 579118334Speter } 579218334Speter 579390287Sobrien if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) 579490287Sobrien return NULL_TREE; 579518334Speter 579690287Sobrien if (ctype == NULL_TREE || check) 579790287Sobrien return decl; 579852290Sobrien 579990287Sobrien if (virtualp) 580090287Sobrien DECL_VIRTUAL_P (decl) = 1; 580118334Speter 580218334Speter return decl; 580318334Speter} 580418334Speter 5805117410Skan/* Create a VAR_DECL named NAME with the indicated TYPE. 5806117410Skan 5807117410Skan If SCOPE is non-NULL, it is the class type or namespace containing 5808117410Skan the variable. If SCOPE is NULL, the variable should is created in 5809117410Skan the innermost enclosings scope. */ 5810117410Skan 581118334Speterstatic tree 5812132747Skangrokvardecl (tree type, 5813132747Skan tree name, 5814132747Skan RID_BIT_TYPE * specbits_in, 5815132747Skan int initialized, 5816132747Skan int constp, 5817132747Skan tree scope) 581818334Speter{ 581918334Speter tree decl; 582051412Sobrien RID_BIT_TYPE specbits; 582118334Speter 5822117410Skan my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, 5823117410Skan 20020808); 5824117410Skan 582551412Sobrien specbits = *specbits_in; 582651412Sobrien 5827117410Skan /* Compute the scope in which to place the variable. */ 5828117410Skan if (!scope) 582918334Speter { 5830117410Skan /* An explicit "extern" specifier indicates a namespace-scope 5831117410Skan variable. */ 5832117410Skan if (RIDBIT_SETP (RID_EXTERN, specbits)) 5833117410Skan scope = current_namespace; 5834117410Skan else if (!at_function_scope_p ()) 5835117410Skan { 5836117410Skan scope = current_scope (); 5837117410Skan if (!scope) 5838117410Skan scope = current_namespace; 5839117410Skan } 584018334Speter } 5841117410Skan 5842117410Skan if (scope 5843117410Skan && (/* If the variable is a namespace-scope variable declared in a 5844117410Skan template, we need DECL_LANG_SPECIFIC. */ 5845117410Skan (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl) 5846117410Skan /* Similarly for namespace-scope variables with language linkage 5847117410Skan other than C++. */ 5848117410Skan || (TREE_CODE (scope) == NAMESPACE_DECL 5849117410Skan && current_lang_name != lang_name_cplusplus) 5850117410Skan /* Similarly for static data members. */ 5851117410Skan || TYPE_P (scope))) 5852117410Skan decl = build_lang_decl (VAR_DECL, name, type); 585318334Speter else 5854117410Skan decl = build_decl (VAR_DECL, name, type); 585552290Sobrien 5856117410Skan if (scope && TREE_CODE (scope) == NAMESPACE_DECL) 5857117410Skan set_decl_namespace (decl, scope, 0); 5858117410Skan else 5859117410Skan DECL_CONTEXT (decl) = scope; 586052290Sobrien 5861117410Skan if (name && scope && current_lang_name != lang_name_c) 5862117410Skan /* We can't mangle lazily here because we don't have any 5863117410Skan way to recover whether or not a variable was `extern 5864117410Skan "C"' later. */ 5865117410Skan mangle_decl (decl); 586652290Sobrien 586718334Speter if (RIDBIT_SETP (RID_EXTERN, specbits)) 586818334Speter { 586918334Speter DECL_THIS_EXTERN (decl) = 1; 587018334Speter DECL_EXTERNAL (decl) = !initialized; 587118334Speter } 587218334Speter 587318334Speter /* In class context, static means one per class, 587418334Speter public access, and static storage. */ 587551412Sobrien if (DECL_CLASS_SCOPE_P (decl)) 587618334Speter { 587718334Speter TREE_PUBLIC (decl) = 1; 587818334Speter TREE_STATIC (decl) = 1; 587918334Speter DECL_EXTERNAL (decl) = 0; 588018334Speter } 588118334Speter /* At top level, either `static' or no s.c. makes a definition 588218334Speter (perhaps tentative), and absence of `static' makes it public. */ 588318334Speter else if (toplevel_bindings_p ()) 588418334Speter { 588518334Speter TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits) 588618334Speter && (DECL_THIS_EXTERN (decl) || ! constp)); 588718334Speter TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); 588818334Speter } 588918334Speter /* Not at top level, only `static' makes a static definition. */ 589018334Speter else 589118334Speter { 589218334Speter TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits); 589318334Speter TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); 589418334Speter } 589552290Sobrien 5896117410Skan if (RIDBIT_SETP (RID_THREAD, specbits)) 5897117410Skan { 5898117410Skan if (targetm.have_tls) 5899117410Skan DECL_THREAD_LOCAL (decl) = 1; 5900117410Skan else 5901117410Skan /* A mere warning is sure to result in improper semantics 5902117410Skan at runtime. Don't bother to allow this to compile. */ 5903117410Skan error ("thread-local storage not supported for this target"); 5904117410Skan } 5905117410Skan 590652290Sobrien if (TREE_PUBLIC (decl)) 590752290Sobrien { 590852290Sobrien /* [basic.link]: A name with no linkage (notably, the name of a class 590952290Sobrien or enumeration declared in a local scope) shall not be used to 591052290Sobrien declare an entity with linkage. 591152290Sobrien 591252290Sobrien Only check this for public decls for now. */ 591352290Sobrien tree t = no_linkage_check (TREE_TYPE (decl)); 591452290Sobrien if (t) 591552290Sobrien { 591690287Sobrien if (TYPE_ANONYMOUS_P (t)) 591752290Sobrien /* Ignore for now; `enum { foo } e' is pretty common. */; 591852290Sobrien else 591990287Sobrien pedwarn ("non-local variable `%#D' uses local type `%T'", 592052290Sobrien decl, t); 592152290Sobrien } 592252290Sobrien } 592352290Sobrien 592418334Speter return decl; 592518334Speter} 592618334Speter 592752290Sobrien/* Create and return a canonical pointer to member function type, for 592852290Sobrien TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ 592918334Speter 593018334Spetertree 5931117410Skanbuild_ptrmemfunc_type (tree type) 593218334Speter{ 5933132747Skan tree field, fields; 593418334Speter tree t; 593590287Sobrien tree unqualified_variant = NULL_TREE; 593618334Speter 593790287Sobrien if (type == error_mark_node) 593890287Sobrien return type; 593990287Sobrien 594018334Speter /* If a canonical type already exists for this type, use it. We use 594118334Speter this method instead of type_hash_canon, because it only does a 594218334Speter simple equality check on the list of field members. */ 594318334Speter 594418334Speter if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type))) 594518334Speter return t; 594618334Speter 594790287Sobrien /* Make sure that we always have the unqualified pointer-to-member 594890287Sobrien type first. */ 594990287Sobrien if (cp_type_quals (type) != TYPE_UNQUALIFIED) 595090287Sobrien unqualified_variant 595190287Sobrien = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); 595218334Speter 595390287Sobrien t = make_aggr_type (RECORD_TYPE); 595451412Sobrien /* Let the front-end know this is a pointer to member function... */ 595518334Speter TYPE_PTRMEMFUNC_FLAG (t) = 1; 595651412Sobrien /* ... and not really an aggregate. */ 595752290Sobrien SET_IS_AGGR_TYPE (t, 0); 595818334Speter 5959132747Skan field = build_decl (FIELD_DECL, pfn_identifier, type); 5960132747Skan fields = field; 5961132747Skan 5962132747Skan field = build_decl (FIELD_DECL, delta_identifier, delta_type_node); 5963132747Skan TREE_CHAIN (field) = fields; 5964132747Skan fields = field; 5965132747Skan 5966132747Skan finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node); 596718334Speter 596818334Speter /* Zap out the name so that the back-end will give us the debugging 596918334Speter information for this anonymous RECORD_TYPE. */ 597018334Speter TYPE_NAME (t) = NULL_TREE; 597118334Speter 597290287Sobrien /* If this is not the unqualified form of this pointer-to-member 597390287Sobrien type, set the TYPE_MAIN_VARIANT for this type to be the 597490287Sobrien unqualified type. Since they are actually RECORD_TYPEs that are 597590287Sobrien not variants of each other, we must do this manually. */ 597690287Sobrien if (cp_type_quals (type) != TYPE_UNQUALIFIED) 597790287Sobrien { 597890287Sobrien t = build_qualified_type (t, cp_type_quals (type)); 597990287Sobrien TYPE_MAIN_VARIANT (t) = unqualified_variant; 598090287Sobrien TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant); 598190287Sobrien TYPE_NEXT_VARIANT (unqualified_variant) = t; 598290287Sobrien } 598390287Sobrien 598490287Sobrien /* Cache this pointer-to-member type so that we can find it again 598590287Sobrien later. */ 598618334Speter TYPE_SET_PTRMEMFUNC_TYPE (type, t); 598718334Speter 598818334Speter return t; 598918334Speter} 599018334Speter 5991117410Skan/* Create and return a pointer to data member type. */ 5992117410Skan 5993117410Skantree 5994117410Skanbuild_ptrmem_type (tree class_type, tree member_type) 5995117410Skan{ 5996132747Skan if (TREE_CODE (member_type) == METHOD_TYPE) 5997132747Skan { 5998132747Skan tree arg_types; 5999132747Skan 6000132747Skan arg_types = TYPE_ARG_TYPES (member_type); 6001132747Skan class_type = (cp_build_qualified_type 6002132747Skan (class_type, 6003132747Skan cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types))))); 6004132747Skan member_type 6005132747Skan = build_method_type_directly (class_type, 6006132747Skan TREE_TYPE (member_type), 6007132747Skan TREE_CHAIN (arg_types)); 6008132747Skan return build_ptrmemfunc_type (build_pointer_type (member_type)); 6009132747Skan } 6010132747Skan else 6011132747Skan { 6012132747Skan my_friendly_assert (TREE_CODE (member_type) != FUNCTION_TYPE, 6013132747Skan 20030716); 6014132747Skan return build_offset_type (class_type, member_type); 6015132747Skan } 6016117410Skan} 6017117410Skan 601852290Sobrien/* DECL is a VAR_DECL defined in-class, whose TYPE is also given. 601952290Sobrien Check to see that the definition is valid. Issue appropriate error 602052290Sobrien messages. Return 1 if the definition is particularly bad, or 0 602152290Sobrien otherwise. */ 602252290Sobrien 602352290Sobrienint 6024132747Skancheck_static_variable_definition (tree decl, tree type) 602552290Sobrien{ 602652290Sobrien /* Motion 10 at San Diego: If a static const integral data member is 602752290Sobrien initialized with an integral constant expression, the initializer 602852290Sobrien may appear either in the declaration (within the class), or in 602952290Sobrien the definition, but not both. If it appears in the class, the 603052290Sobrien member is a member constant. The file-scope definition is always 603152290Sobrien required. */ 6032110631Skan if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE) 603352290Sobrien { 603490287Sobrien error ("invalid in-class initialization of static data member of non-integral type `%T'", 6035110631Skan type); 603652290Sobrien /* If we just return the declaration, crashes will sometimes 603752290Sobrien occur. We therefore return void_type_node, as if this was a 603852290Sobrien friend declaration, to cause callers to completely ignore 603952290Sobrien this declaration. */ 604052290Sobrien return 1; 604152290Sobrien } 604252290Sobrien else if (!CP_TYPE_CONST_P (type)) 604390287Sobrien error ("ISO C++ forbids in-class initialization of non-const static member `%D'", 604452290Sobrien decl); 604552290Sobrien else if (pedantic && !INTEGRAL_TYPE_P (type)) 604690287Sobrien pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type); 604752290Sobrien 604852290Sobrien return 0; 604952290Sobrien} 605052290Sobrien 605190287Sobrien/* Given the SIZE (i.e., number of elements) in an array, compute an 605290287Sobrien appropriate index type for the array. If non-NULL, NAME is the 605390287Sobrien name of the thing being declared. */ 605490287Sobrien 605590287Sobrientree 6056132747Skancompute_array_index_type (tree name, tree size) 605790287Sobrien{ 6058132747Skan tree type = TREE_TYPE (size); 605990287Sobrien tree itype; 606090287Sobrien 6061132747Skan /* The array bound must be an integer type. */ 6062132747Skan if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type)) 606390287Sobrien { 6064132747Skan if (name) 6065132747Skan error ("size of array `%D' has non-integral type `%T'", name, type); 6066132747Skan else 6067132747Skan error ("size of array has non-integral type `%T'", type); 6068132747Skan size = integer_one_node; 6069132747Skan type = TREE_TYPE (size); 607090287Sobrien } 607190287Sobrien 6072132747Skan if (abi_version_at_least (2) 6073132747Skan /* We should only handle value dependent expressions specially. */ 6074132747Skan ? value_dependent_expression_p (size) 6075132747Skan /* But for abi-1, we handled all instances in templates. This 6076132747Skan effects the manglings produced. */ 6077132747Skan : processing_template_decl) 6078132747Skan return build_index_type (build_min (MINUS_EXPR, sizetype, 6079132747Skan size, integer_one_node)); 6080132747Skan 6081117410Skan /* The size might be the result of a cast. */ 608290287Sobrien STRIP_TYPE_NOPS (size); 608390287Sobrien 608490287Sobrien /* It might be a const variable or enumeration constant. */ 608590287Sobrien size = decl_constant_value (size); 608690287Sobrien 608790287Sobrien /* Normally, the array-bound will be a constant. */ 608890287Sobrien if (TREE_CODE (size) == INTEGER_CST) 608990287Sobrien { 609090287Sobrien /* Check to see if the array bound overflowed. Make that an 609190287Sobrien error, no matter how generous we're being. */ 609290287Sobrien int old_flag_pedantic_errors = flag_pedantic_errors; 609390287Sobrien int old_pedantic = pedantic; 609490287Sobrien pedantic = flag_pedantic_errors = 1; 609590287Sobrien constant_expression_warning (size); 609690287Sobrien pedantic = old_pedantic; 609790287Sobrien flag_pedantic_errors = old_flag_pedantic_errors; 609890287Sobrien 609990287Sobrien /* An array must have a positive number of elements. */ 610090287Sobrien if (INT_CST_LT (size, integer_zero_node)) 610190287Sobrien { 610290287Sobrien if (name) 610390287Sobrien error ("size of array `%D' is negative", name); 610490287Sobrien else 610590287Sobrien error ("size of array is negative"); 610690287Sobrien size = integer_one_node; 610790287Sobrien } 6108132747Skan /* As an extension we allow zero-sized arrays. We always allow 6109132747Skan them in system headers because glibc uses them. */ 611090287Sobrien else if (integer_zerop (size) && pedantic && !in_system_header) 611190287Sobrien { 611290287Sobrien if (name) 611390287Sobrien pedwarn ("ISO C++ forbids zero-size array `%D'", name); 611490287Sobrien else 611590287Sobrien pedwarn ("ISO C++ forbids zero-size array"); 611690287Sobrien } 611790287Sobrien } 611890287Sobrien else if (TREE_CONSTANT (size)) 611990287Sobrien { 612090287Sobrien /* `(int) &fn' is not a valid array bound. */ 612190287Sobrien if (name) 612290287Sobrien error ("size of array `%D' is not an integral constant-expression", 612390287Sobrien name); 612490287Sobrien else 612590287Sobrien error ("size of array is not an integral constant-expression"); 612690287Sobrien } 6127132747Skan else if (pedantic) 6128132747Skan { 6129132747Skan if (name) 6130132747Skan pedwarn ("ISO C++ forbids variable-size array `%D'", name); 6131132747Skan else 6132132747Skan pedwarn ("ISO C++ forbids variable-size array"); 6133132747Skan } 613490287Sobrien 6135132747Skan if (processing_template_decl && !TREE_CONSTANT (size)) 6136132747Skan /* A variable sized array. */ 6137132747Skan itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node); 6138132747Skan else 613990287Sobrien { 6140132747Skan /* Compute the index of the largest element in the array. It is 6141132747Skan one less than the number of elements in the array. */ 6142132747Skan itype 6143132747Skan = fold (cp_build_binary_op (MINUS_EXPR, 6144132747Skan cp_convert (ssizetype, size), 6145132747Skan cp_convert (ssizetype, integer_one_node))); 6146132747Skan if (!TREE_CONSTANT (itype)) 6147132747Skan /* A variable sized array. */ 6148132747Skan itype = variable_size (itype); 6149132747Skan /* Make sure that there was no overflow when creating to a signed 6150132747Skan index type. (For example, on a 32-bit machine, an array with 6151132747Skan size 2^32 - 1 is too big.) */ 6152132747Skan else if (TREE_OVERFLOW (itype)) 615390287Sobrien { 6154132747Skan error ("overflow in array dimension"); 6155132747Skan TREE_OVERFLOW (itype) = 0; 615690287Sobrien } 615790287Sobrien } 615890287Sobrien 615990287Sobrien /* Create and return the appropriate index type. */ 616090287Sobrien return build_index_type (itype); 616190287Sobrien} 616290287Sobrien 6163132747Skan/* Returns the scope (if any) in which the entity declared by 6164132747Skan DECLARATOR will be located. If the entity was declared with an 6165132747Skan unqualified name, NULL_TREE is returned. */ 6166132747Skan 6167132747Skantree 6168132747Skanget_scope_of_declarator (tree declarator) 6169132747Skan{ 6170132747Skan if (!declarator) 6171132747Skan return NULL_TREE; 6172132747Skan 6173132747Skan switch (TREE_CODE (declarator)) 6174132747Skan { 6175132747Skan case CALL_EXPR: 6176132747Skan case ARRAY_REF: 6177132747Skan case INDIRECT_REF: 6178132747Skan case ADDR_EXPR: 6179132747Skan /* For any of these, the main declarator is the first operand. */ 6180132747Skan return get_scope_of_declarator (TREE_OPERAND 6181132747Skan (declarator, 0)); 6182132747Skan 6183132747Skan case SCOPE_REF: 6184132747Skan /* For a pointer-to-member, continue descending. */ 6185132747Skan if (TREE_CODE (TREE_OPERAND (declarator, 1)) 6186132747Skan == INDIRECT_REF) 6187132747Skan return get_scope_of_declarator (TREE_OPERAND 6188132747Skan (declarator, 1)); 6189132747Skan /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in 6190132747Skan which the declaration occurs is the first operand. */ 6191132747Skan return TREE_OPERAND (declarator, 0); 6192132747Skan 6193132747Skan case TREE_LIST: 6194132747Skan /* Attributes to be applied. The declarator is TREE_VALUE. */ 6195132747Skan return get_scope_of_declarator (TREE_VALUE (declarator)); 6196132747Skan 6197132747Skan default: 6198132747Skan /* Otherwise, we have a declarator-id which is not a qualified 6199132747Skan name; the entity will be declared in the current scope. */ 6200132747Skan return NULL_TREE; 6201132747Skan } 6202132747Skan} 6203132747Skan 620490287Sobrien/* Returns an ARRAY_TYPE for an array with SIZE elements of the 620590287Sobrien indicated TYPE. If non-NULL, NAME is the NAME of the declaration 620690287Sobrien with this type. */ 620790287Sobrien 620890287Sobrienstatic tree 6209132747Skancreate_array_type_for_decl (tree name, tree type, tree size) 621090287Sobrien{ 621190287Sobrien tree itype = NULL_TREE; 621290287Sobrien const char* error_msg; 621390287Sobrien 621490287Sobrien /* If things have already gone awry, bail now. */ 621590287Sobrien if (type == error_mark_node || size == error_mark_node) 621690287Sobrien return error_mark_node; 621790287Sobrien 621890287Sobrien /* Assume that everything will go OK. */ 621990287Sobrien error_msg = NULL; 622090287Sobrien 622190287Sobrien /* There are some types which cannot be array elements. */ 622290287Sobrien switch (TREE_CODE (type)) 622390287Sobrien { 622490287Sobrien case VOID_TYPE: 622590287Sobrien error_msg = "array of void"; 622690287Sobrien break; 622790287Sobrien 622890287Sobrien case FUNCTION_TYPE: 622990287Sobrien error_msg = "array of functions"; 623090287Sobrien break; 623190287Sobrien 623290287Sobrien case REFERENCE_TYPE: 623390287Sobrien error_msg = "array of references"; 623490287Sobrien break; 623590287Sobrien 623690287Sobrien case METHOD_TYPE: 623790287Sobrien error_msg = "array of function members"; 623890287Sobrien break; 623990287Sobrien 624090287Sobrien default: 624190287Sobrien break; 624290287Sobrien } 624390287Sobrien 624490287Sobrien /* If something went wrong, issue an error-message and return. */ 624590287Sobrien if (error_msg) 624690287Sobrien { 624790287Sobrien if (name) 624890287Sobrien error ("declaration of `%D' as %s", name, error_msg); 624990287Sobrien else 625090287Sobrien error ("creating %s", error_msg); 625190287Sobrien 625290287Sobrien return error_mark_node; 625390287Sobrien } 625490287Sobrien 625590287Sobrien /* [dcl.array] 625690287Sobrien 625790287Sobrien The constant expressions that specify the bounds of the arrays 625890287Sobrien can be omitted only for the first member of the sequence. */ 625990287Sobrien if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) 626090287Sobrien { 626190287Sobrien if (name) 626290287Sobrien error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first", 626390287Sobrien name); 626490287Sobrien else 626590287Sobrien error ("multidimensional array must have bounds for all dimensions except the first"); 626690287Sobrien 626790287Sobrien return error_mark_node; 626890287Sobrien } 626990287Sobrien 627090287Sobrien /* Figure out the index type for the array. */ 627190287Sobrien if (size) 627290287Sobrien itype = compute_array_index_type (name, size); 627390287Sobrien 627490287Sobrien return build_cplus_array_type (type, itype); 627590287Sobrien} 627690287Sobrien 627790287Sobrien/* Check that it's OK to declare a function with the indicated TYPE. 627890287Sobrien SFK indicates the kind of special function (if any) that this 627990287Sobrien function is. OPTYPE is the type given in a conversion operator 628090287Sobrien declaration. Returns the actual return type of the function; that 628190287Sobrien may be different than TYPE if an error occurs, or for certain 628290287Sobrien special functions. */ 628390287Sobrien 628490287Sobrienstatic tree 6285132747Skancheck_special_function_return_type (special_function_kind sfk, 6286132747Skan tree type, 6287132747Skan tree optype) 628890287Sobrien{ 628990287Sobrien switch (sfk) 629090287Sobrien { 629190287Sobrien case sfk_constructor: 629290287Sobrien if (type) 629390287Sobrien error ("return type specification for constructor invalid"); 629490287Sobrien 629590287Sobrien type = void_type_node; 629690287Sobrien break; 629790287Sobrien 629890287Sobrien case sfk_destructor: 629990287Sobrien if (type) 630090287Sobrien error ("return type specification for destructor invalid"); 630190287Sobrien type = void_type_node; 630290287Sobrien break; 630390287Sobrien 630490287Sobrien case sfk_conversion: 630590287Sobrien if (type && !same_type_p (type, optype)) 630690287Sobrien error ("operator `%T' declared to return `%T'", optype, type); 630790287Sobrien else if (type) 630890287Sobrien pedwarn ("return type specified for `operator %T'", optype); 630990287Sobrien type = optype; 631090287Sobrien break; 631190287Sobrien 631290287Sobrien default: 631390287Sobrien abort (); 631490287Sobrien break; 631590287Sobrien } 631690287Sobrien 631790287Sobrien return type; 631890287Sobrien} 631990287Sobrien 6320132747Skan/* Given declspecs and a declarator (abstract or otherwise), determine 6321132747Skan the name and type of the object declared and construct a DECL node 6322132747Skan for it. 632318334Speter 632418334Speter DECLSPECS is a chain of tree_list nodes whose value fields 632518334Speter are the storage classes and type specifiers. 632618334Speter 632718334Speter DECL_CONTEXT says which syntactic context this declaration is in: 632818334Speter NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 632918334Speter FUNCDEF for a function definition. Like NORMAL but a few different 633018334Speter error messages in each case. Return value may be zero meaning 633118334Speter this definition is too screwy to try to parse. 633218334Speter MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to 633318334Speter handle member functions (which have FIELD context). 633418334Speter Return value may be zero meaning this definition is too screwy to 633518334Speter try to parse. 633618334Speter PARM for a parameter declaration (either within a function prototype 633718334Speter or before a function body). Make a PARM_DECL, or return void_type_node. 633818334Speter CATCHPARM for a parameter declaration before a catch clause. 633918334Speter TYPENAME if for a typename (in a cast or sizeof). 634018334Speter Don't make a DECL node; just return the ..._TYPE node. 634118334Speter FIELD for a struct or union field; make a FIELD_DECL. 634218334Speter BITFIELD for a field with specified width. 634318334Speter INITIALIZED is 1 if the decl has an initializer. 634418334Speter 634590287Sobrien ATTRLIST is a pointer to the list of attributes, which may be NULL 634690287Sobrien if there are none; *ATTRLIST may be modified if attributes from inside 634790287Sobrien the declarator should be applied to the declaration. 634852290Sobrien 6349132747Skan When this function is called, scoping variables (such as 6350132747Skan CURRENT_CLASS_TYPE) should reflect the scope in which the 6351132747Skan declaration occurs, not the scope in which the new declaration will 6352132747Skan be placed. For example, on: 635318334Speter 6354132747Skan void S::f() { ... } 635518334Speter 6356132747Skan when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE 6357132747Skan should not be `S'. */ 635818334Speter 635918334Spetertree 6360132747Skangrokdeclarator (tree declarator, 6361132747Skan tree declspecs, 6362132747Skan enum decl_context decl_context, 6363132747Skan int initialized, 6364132747Skan tree* attrlist) 636518334Speter{ 636618334Speter RID_BIT_TYPE specbits; 636718334Speter int nclasses = 0; 636818334Speter tree spec; 636918334Speter tree type = NULL_TREE; 637018334Speter int longlong = 0; 637152290Sobrien int type_quals; 637218334Speter int virtualp, explicitp, friendp, inlinep, staticp; 637318334Speter int explicit_int = 0; 637418334Speter int explicit_char = 0; 637551412Sobrien int defaulted_int = 0; 637690287Sobrien int extern_langp = 0; 637796292Sobrien tree dependant_name = NULL_TREE; 637890287Sobrien 637918334Speter tree typedef_decl = NULL_TREE; 638090287Sobrien const char *name; 638118334Speter tree typedef_type = NULL_TREE; 638218334Speter int funcdef_flag = 0; 638318334Speter enum tree_code innermost_code = ERROR_MARK; 638418334Speter int bitfield = 0; 638551412Sobrien#if 0 638651412Sobrien /* See the code below that used this. */ 638790287Sobrien tree decl_attr = NULL_TREE; 638851412Sobrien#endif 638918334Speter 639018334Speter /* Keep track of what sort of function is being processed 639118334Speter so that we can warn about default return values, or explicit 639218334Speter return values which do not match prescribed defaults. */ 639390287Sobrien special_function_kind sfk = sfk_none; 639418334Speter 639518334Speter tree dname = NULL_TREE; 639618334Speter tree ctype = current_class_type; 639718334Speter tree ctor_return_type = NULL_TREE; 639818334Speter enum overload_flags flags = NO_SPECIAL; 639918334Speter tree quals = NULL_TREE; 640051412Sobrien tree raises = NULL_TREE; 640151412Sobrien int template_count = 0; 640251412Sobrien tree in_namespace = NULL_TREE; 640390287Sobrien tree returned_attrs = NULL_TREE; 6404132747Skan tree scope = NULL_TREE; 6405132747Skan tree parms = NULL_TREE; 640618334Speter 640718334Speter RIDBIT_RESET_ALL (specbits); 640818334Speter if (decl_context == FUNCDEF) 640918334Speter funcdef_flag = 1, decl_context = NORMAL; 641018334Speter else if (decl_context == MEMFUNCDEF) 641118334Speter funcdef_flag = -1, decl_context = FIELD; 641218334Speter else if (decl_context == BITFIELD) 641318334Speter bitfield = 1, decl_context = FIELD; 641418334Speter 641518334Speter /* Look inside a declarator for the name being declared 641618334Speter and get it as a string, for an error message. */ 641718334Speter { 641851412Sobrien tree *next = &declarator; 6419132747Skan tree decl; 642018334Speter name = NULL; 642118334Speter 642251412Sobrien while (next && *next) 642351412Sobrien { 642451412Sobrien decl = *next; 642551412Sobrien switch (TREE_CODE (decl)) 642618334Speter { 642752290Sobrien case TREE_LIST: 642852290Sobrien /* For attributes. */ 642952290Sobrien next = &TREE_VALUE (decl); 643052290Sobrien break; 643152290Sobrien 643251412Sobrien case COND_EXPR: 643351412Sobrien ctype = NULL_TREE; 643451412Sobrien next = &TREE_OPERAND (decl, 0); 643551412Sobrien break; 643618334Speter 643751412Sobrien case BIT_NOT_EXPR: /* For C++ destructors! */ 643818334Speter { 643951412Sobrien tree name = TREE_OPERAND (decl, 0); 644051412Sobrien tree rename = NULL_TREE; 644118334Speter 644251412Sobrien my_friendly_assert (flags == NO_SPECIAL, 152); 644351412Sobrien flags = DTOR_FLAG; 644490287Sobrien sfk = sfk_destructor; 6445132747Skan if (TYPE_P (name)) 644651412Sobrien TREE_OPERAND (decl, 0) = name = constructor_name (name); 644751412Sobrien my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153); 644851412Sobrien if (ctype == NULL_TREE) 644951412Sobrien { 645051412Sobrien if (current_class_type == NULL_TREE) 645151412Sobrien { 645251412Sobrien error ("destructors must be member functions"); 645351412Sobrien flags = NO_SPECIAL; 645451412Sobrien } 645551412Sobrien else 645651412Sobrien { 6457132747Skan tree t = constructor_name (current_class_type); 645851412Sobrien if (t != name) 645951412Sobrien rename = t; 646051412Sobrien } 646151412Sobrien } 646218334Speter else 646351412Sobrien { 646451412Sobrien tree t = constructor_name (ctype); 646551412Sobrien if (t != name) 646651412Sobrien rename = t; 646751412Sobrien } 646818334Speter 646951412Sobrien if (rename) 647018334Speter { 647190287Sobrien error ("destructor `%T' must match class name `%T'", 647251412Sobrien name, rename); 647351412Sobrien TREE_OPERAND (decl, 0) = rename; 647418334Speter } 647551412Sobrien next = &name; 647618334Speter } 647751412Sobrien break; 647818334Speter 647951412Sobrien case ADDR_EXPR: /* C++ reference declaration */ 6480117410Skan /* Fall through. */ 648151412Sobrien case ARRAY_REF: 648251412Sobrien case INDIRECT_REF: 648351412Sobrien ctype = NULL_TREE; 648451412Sobrien innermost_code = TREE_CODE (decl); 648551412Sobrien next = &TREE_OPERAND (decl, 0); 648651412Sobrien break; 648718334Speter 648851412Sobrien case CALL_EXPR: 648951412Sobrien innermost_code = TREE_CODE (decl); 649051412Sobrien if (decl_context == FIELD && ctype == NULL_TREE) 649151412Sobrien ctype = current_class_type; 649251412Sobrien if (ctype 649351412Sobrien && TREE_OPERAND (decl, 0) 649451412Sobrien && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL 6495117410Skan && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)), 6496117410Skan ctype))) 649751412Sobrien TREE_OPERAND (decl, 0) = constructor_name (ctype); 649851412Sobrien next = &TREE_OPERAND (decl, 0); 649951412Sobrien decl = *next; 650051412Sobrien if (ctype != NULL_TREE 650151412Sobrien && decl != NULL_TREE && flags != DTOR_FLAG 6502132747Skan && constructor_name_p (decl, ctype)) 650318334Speter { 650490287Sobrien sfk = sfk_constructor; 650551412Sobrien ctor_return_type = ctype; 650618334Speter } 650751412Sobrien ctype = NULL_TREE; 650851412Sobrien break; 650990287Sobrien 651051412Sobrien case TEMPLATE_ID_EXPR: 651118334Speter { 651251412Sobrien tree fns = TREE_OPERAND (decl, 0); 651351412Sobrien 651452290Sobrien dname = fns; 651552290Sobrien if (TREE_CODE (dname) == COMPONENT_REF) 651652290Sobrien dname = TREE_OPERAND (dname, 1); 651752290Sobrien if (TREE_CODE (dname) != IDENTIFIER_NODE) 651852290Sobrien { 651952290Sobrien my_friendly_assert (is_overloaded_fn (dname), 652052290Sobrien 19990331); 652152290Sobrien dname = DECL_NAME (get_first_fn (dname)); 652252290Sobrien } 652318334Speter } 6524117410Skan /* Fall through. */ 652518334Speter 652651412Sobrien case IDENTIFIER_NODE: 652751412Sobrien if (TREE_CODE (decl) == IDENTIFIER_NODE) 652851412Sobrien dname = decl; 652951412Sobrien 653051412Sobrien next = 0; 653151412Sobrien 653290287Sobrien if (C_IS_RESERVED_WORD (dname)) 653318334Speter { 653490287Sobrien error ("declarator-id missing; using reserved word `%D'", 653551412Sobrien dname); 653651412Sobrien name = IDENTIFIER_POINTER (dname); 653751412Sobrien } 653890287Sobrien else if (!IDENTIFIER_TYPENAME_P (dname)) 653951412Sobrien name = IDENTIFIER_POINTER (dname); 654051412Sobrien else 654151412Sobrien { 654290287Sobrien my_friendly_assert (flags == NO_SPECIAL, 154); 654390287Sobrien flags = TYPENAME_FLAG; 654490287Sobrien ctor_return_type = TREE_TYPE (dname); 654590287Sobrien sfk = sfk_conversion; 6546132747Skan if (is_typename_at_global_scope (dname)) 654790287Sobrien name = IDENTIFIER_POINTER (dname); 654890287Sobrien else 654990287Sobrien name = "<invalid operator>"; 655018334Speter } 655151412Sobrien break; 655218334Speter 655351412Sobrien /* C++ extension */ 655451412Sobrien case SCOPE_REF: 655551412Sobrien { 655651412Sobrien /* Perform error checking, and decide on a ctype. */ 655751412Sobrien tree cname = TREE_OPERAND (decl, 0); 655851412Sobrien if (cname == NULL_TREE) 655951412Sobrien ctype = NULL_TREE; 656051412Sobrien else if (TREE_CODE (cname) == NAMESPACE_DECL) 656151412Sobrien { 656251412Sobrien ctype = NULL_TREE; 656351412Sobrien in_namespace = TREE_OPERAND (decl, 0); 656451412Sobrien } 656551412Sobrien else if (! is_aggr_type (cname, 1)) 6566132747Skan ctype = NULL_TREE; 656751412Sobrien /* Must test TREE_OPERAND (decl, 1), in case user gives 656851412Sobrien us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */ 656951412Sobrien else if (TREE_OPERAND (decl, 1) 657051412Sobrien && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF) 657151412Sobrien ctype = cname; 657251412Sobrien else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM 657390287Sobrien || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM) 657451412Sobrien { 657596292Sobrien /* This might be declaring a member of a template 657696292Sobrien parm to be a friend. */ 657796292Sobrien ctype = cname; 657896292Sobrien dependant_name = TREE_OPERAND (decl, 1); 657951412Sobrien } 658051412Sobrien else if (ctype == NULL_TREE) 658151412Sobrien ctype = cname; 658251412Sobrien else if (TREE_COMPLEXITY (decl) == current_class_depth) 6583132747Skan ; 658451412Sobrien else 658551412Sobrien { 658651412Sobrien if (! UNIQUELY_DERIVED_FROM_P (cname, ctype)) 658751412Sobrien { 658890287Sobrien error ("type `%T' is not derived from type `%T'", 658951412Sobrien cname, ctype); 6590132747Skan ctype = NULL_TREE; 659151412Sobrien } 659251412Sobrien else 659351412Sobrien ctype = cname; 659451412Sobrien } 659518334Speter 6596132747Skan /* It is valid to write: 6597117410Skan 6598132747Skan class C { void f(); }; 6599132747Skan typedef C D; 6600132747Skan void D::f(); 6601132747Skan 6602132747Skan The standard is not clear about whether `typedef const C D' is 6603132747Skan legal; as of 2002-09-15 the committee is considering 6604132747Skan that question. EDG 3.0 allows that syntax. 6605132747Skan Therefore, we do as well. */ 6606132747Skan if (ctype) 6607132747Skan ctype = TYPE_MAIN_VARIANT (ctype); 6608132747Skan /* Update the declarator so that when we process it 6609132747Skan again the correct type is present. */ 6610132747Skan TREE_OPERAND (decl, 0) = ctype; 6611132747Skan 661251412Sobrien if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL 6613117410Skan && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)), 6614117410Skan ctype)) 661551412Sobrien TREE_OPERAND (decl, 1) = constructor_name (ctype); 661651412Sobrien next = &TREE_OPERAND (decl, 1); 661751412Sobrien decl = *next; 661851412Sobrien if (ctype) 661951412Sobrien { 6620132747Skan tree name = decl; 6621132747Skan 6622132747Skan if (TREE_CODE (name) == BIT_NOT_EXPR) 6623132747Skan name = TREE_OPERAND (name, 0); 6624132747Skan 6625132747Skan if (!constructor_name_p (decl, ctype)) 6626132747Skan ; 6627132747Skan else if (decl == name) 662851412Sobrien { 662990287Sobrien sfk = sfk_constructor; 663051412Sobrien ctor_return_type = ctype; 663151412Sobrien } 6632132747Skan else 663351412Sobrien { 663490287Sobrien sfk = sfk_destructor; 663551412Sobrien ctor_return_type = ctype; 663651412Sobrien flags = DTOR_FLAG; 663751412Sobrien TREE_OPERAND (decl, 0) = constructor_name (ctype); 663851412Sobrien next = &TREE_OPERAND (decl, 0); 663951412Sobrien } 664051412Sobrien } 664151412Sobrien } 664251412Sobrien break; 664351412Sobrien 664451412Sobrien case ERROR_MARK: 664551412Sobrien next = 0; 664651412Sobrien break; 664751412Sobrien 664851412Sobrien case TYPE_DECL: 664951412Sobrien /* Parse error puts this typespec where 665051412Sobrien a declarator should go. */ 665190287Sobrien error ("`%T' specified as declarator-id", DECL_NAME (decl)); 665251412Sobrien if (TREE_TYPE (decl) == current_class_type) 665390287Sobrien error (" perhaps you want `%T' for a constructor", 665451412Sobrien current_class_name); 665551412Sobrien dname = DECL_NAME (decl); 665651412Sobrien name = IDENTIFIER_POINTER (dname); 665751412Sobrien 665851412Sobrien /* Avoid giving two errors for this. */ 665951412Sobrien IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE; 666051412Sobrien 666190287Sobrien declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs); 666251412Sobrien *next = dname; 666351412Sobrien next = 0; 666451412Sobrien break; 666551412Sobrien 6666117410Skan case BASELINK: 6667117410Skan next = &BASELINK_FUNCTIONS (decl); 6668117410Skan break; 6669117410Skan 6670107601Sobrien case TEMPLATE_DECL: 6671107601Sobrien /* Sometimes, we see a template-name used as part of a 6672107601Sobrien decl-specifier like in 6673117410Skan std::allocator alloc; 6674117410Skan Handle that gracefully. */ 6675117410Skan error ("invalid use of template-name '%E' in a declarator", decl); 6676107601Sobrien return error_mark_node; 6677107601Sobrien break; 6678117410Skan 667951412Sobrien default: 6680117410Skan my_friendly_assert (0, 20020917); 668151412Sobrien } 668251412Sobrien } 668318334Speter } 668418334Speter 668518334Speter /* A function definition's declarator must have the form of 668618334Speter a function declarator. */ 668718334Speter 668818334Speter if (funcdef_flag && innermost_code != CALL_EXPR) 668918334Speter return 0; 669018334Speter 669118334Speter if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG) 669218334Speter && innermost_code != CALL_EXPR 669318334Speter && ! (ctype && declspecs == NULL_TREE)) 669418334Speter { 669590287Sobrien error ("declaration of `%D' as non-function", dname); 669618334Speter return void_type_node; 669718334Speter } 669818334Speter 669918334Speter /* Anything declared one level down from the top level 670018334Speter must be one of the parameters of a function 670118334Speter (because the body is at least two levels down). */ 670218334Speter 670318334Speter /* This heuristic cannot be applied to C++ nodes! Fixed, however, 670418334Speter by not allowing C++ class definitions to specify their parameters 670518334Speter with xdecls (must be spec.d in the parmlist). 670618334Speter 670718334Speter Since we now wait to push a class scope until we are sure that 670818334Speter we are in a legitimate method context, we must set oldcname 670918334Speter explicitly (since current_class_name is not yet alive). 671018334Speter 671118334Speter We also want to avoid calling this a PARM if it is in a namespace. */ 671218334Speter 671390287Sobrien if (decl_context == NORMAL && !toplevel_bindings_p ()) 671418334Speter { 6715117410Skan struct cp_binding_level *b = current_binding_level; 671618334Speter current_binding_level = b->level_chain; 671718334Speter if (current_binding_level != 0 && toplevel_bindings_p ()) 671818334Speter decl_context = PARM; 671918334Speter current_binding_level = b; 672018334Speter } 672118334Speter 672290287Sobrien if (name == NULL) 672390287Sobrien name = decl_context == PARM ? "parameter" : "type name"; 672490287Sobrien 672518334Speter /* Look through the decl specs and record which ones appear. 672618334Speter Some typespecs are defined as built-in typenames. 672718334Speter Others, the ones that are modifiers of other types, 672818334Speter are represented by bits in SPECBITS: set the bits for 672918334Speter the modifiers that appear. Storage class keywords are also in SPECBITS. 673018334Speter 673118334Speter If there is a typedef name or a type, store the type in TYPE. 673218334Speter This includes builtin typedefs such as `int'. 673318334Speter 673418334Speter Set EXPLICIT_INT if the type is `int' or `char' and did not 673518334Speter come from a user typedef. 673618334Speter 673718334Speter Set LONGLONG if `long' is mentioned twice. 673818334Speter 673918334Speter For C++, constructors and destructors have their own fast treatment. */ 674018334Speter 674118334Speter for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) 674218334Speter { 6743132747Skan int i; 6744132747Skan tree id; 674518334Speter 674618334Speter /* Certain parse errors slip through. For example, 674718334Speter `int class;' is not caught by the parser. Try 674818334Speter weakly to recover here. */ 674918334Speter if (TREE_CODE (spec) != TREE_LIST) 675018334Speter return 0; 675118334Speter 675218334Speter id = TREE_VALUE (spec); 675318334Speter 675490287Sobrien /* If the entire declaration is itself tagged as deprecated then 675590287Sobrien suppress reports of deprecated items. */ 675690287Sobrien if (!adding_implicit_members && id && TREE_DEPRECATED (id)) 675790287Sobrien { 675890287Sobrien if (deprecated_state != DEPRECATED_SUPPRESS) 675990287Sobrien warn_deprecated_use (id); 676090287Sobrien } 676190287Sobrien 676218334Speter if (TREE_CODE (id) == IDENTIFIER_NODE) 676318334Speter { 676418334Speter if (id == ridpointers[(int) RID_INT] 676518334Speter || id == ridpointers[(int) RID_CHAR] 676618334Speter || id == ridpointers[(int) RID_BOOL] 676718334Speter || id == ridpointers[(int) RID_WCHAR]) 676818334Speter { 676918334Speter if (type) 677018334Speter { 677118334Speter if (id == ridpointers[(int) RID_BOOL]) 677218334Speter error ("`bool' is now a keyword"); 677318334Speter else 677490287Sobrien error ("extraneous `%T' ignored", id); 677518334Speter } 677618334Speter else 677718334Speter { 677818334Speter if (id == ridpointers[(int) RID_INT]) 677918334Speter explicit_int = 1; 678018334Speter else if (id == ridpointers[(int) RID_CHAR]) 678118334Speter explicit_char = 1; 678218334Speter type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id)); 678318334Speter } 678418334Speter goto found; 678518334Speter } 678651412Sobrien /* C++ aggregate types. */ 678718334Speter if (IDENTIFIER_HAS_TYPE_VALUE (id)) 678818334Speter { 678918334Speter if (type) 679090287Sobrien error ("multiple declarations `%T' and `%T'", type, id); 679118334Speter else 679218334Speter type = IDENTIFIER_TYPE_VALUE (id); 679318334Speter goto found; 679418334Speter } 679518334Speter 679618334Speter for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++) 679718334Speter { 679818334Speter if (ridpointers[i] == id) 679918334Speter { 680018334Speter if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits)) 680118334Speter { 680251412Sobrien if (pedantic && ! in_system_header && warn_long_long) 680390287Sobrien pedwarn ("ISO C++ does not support `long long'"); 680451412Sobrien if (longlong) 680518334Speter error ("`long long long' is too long for GCC"); 680618334Speter else 680718334Speter longlong = 1; 680818334Speter } 680918334Speter else if (RIDBIT_SETP (i, specbits)) 681018334Speter pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); 6811117410Skan 6812132747Skan /* Diagnose "__thread extern" or "__thread static". */ 6813132747Skan if (RIDBIT_SETP (RID_THREAD, specbits)) 6814117410Skan { 6815132747Skan if (i == (int)RID_EXTERN) 6816117410Skan error ("`__thread' before `extern'"); 6817132747Skan else if (i == (int)RID_STATIC) 6818117410Skan error ("`__thread' before `static'"); 6819117410Skan } 6820117410Skan 682190287Sobrien if (i == (int)RID_EXTERN 682290287Sobrien && TREE_PURPOSE (spec) == error_mark_node) 682390287Sobrien /* This extern was part of a language linkage. */ 682490287Sobrien extern_langp = 1; 6825117410Skan 682618334Speter RIDBIT_SET (i, specbits); 682718334Speter goto found; 682818334Speter } 682918334Speter } 683018334Speter } 683190287Sobrien else if (TREE_CODE (id) == TYPE_DECL) 683251412Sobrien { 683351412Sobrien if (type) 683490287Sobrien error ("multiple declarations `%T' and `%T'", type, 683551412Sobrien TREE_TYPE (id)); 683651412Sobrien else 683751412Sobrien { 683851412Sobrien type = TREE_TYPE (id); 683951412Sobrien TREE_VALUE (spec) = type; 684096292Sobrien typedef_decl = id; 684151412Sobrien } 684251412Sobrien goto found; 684351412Sobrien } 684418334Speter if (type) 684518334Speter error ("two or more data types in declaration of `%s'", name); 684618334Speter else if (TREE_CODE (id) == IDENTIFIER_NODE) 684718334Speter { 6848132747Skan tree t = lookup_name (id, 1); 684918334Speter if (!t || TREE_CODE (t) != TYPE_DECL) 685018334Speter error ("`%s' fails to be a typedef or built in type", 685118334Speter IDENTIFIER_POINTER (id)); 685218334Speter else 685318334Speter { 685418334Speter type = TREE_TYPE (t); 685518334Speter typedef_decl = t; 685618334Speter } 685718334Speter } 685851412Sobrien else if (id != error_mark_node) 685918334Speter /* Can't change CLASS nodes into RECORD nodes here! */ 686018334Speter type = id; 686118334Speter 686218334Speter found: ; 686318334Speter } 686418334Speter 686596292Sobrien#if 0 686696292Sobrien /* See the code below that used this. */ 686796292Sobrien if (typedef_decl) 686896292Sobrien decl_attr = DECL_ATTRIBUTES (typedef_decl); 686996292Sobrien#endif 687018334Speter typedef_type = type; 687118334Speter 687251412Sobrien /* No type at all: default to `int', and set DEFAULTED_INT 687390287Sobrien because it was not a user-defined typedef. */ 687418334Speter 687518334Speter if (type == NULL_TREE 687618334Speter && (RIDBIT_SETP (RID_SIGNED, specbits) 687718334Speter || RIDBIT_SETP (RID_UNSIGNED, specbits) 687818334Speter || RIDBIT_SETP (RID_LONG, specbits) 687918334Speter || RIDBIT_SETP (RID_SHORT, specbits))) 688018334Speter { 688118334Speter /* These imply 'int'. */ 688218334Speter type = integer_type_node; 688351412Sobrien defaulted_int = 1; 688418334Speter } 688518334Speter 688690287Sobrien if (sfk != sfk_none) 688790287Sobrien type = check_special_function_return_type (sfk, type, 688890287Sobrien ctor_return_type); 688990287Sobrien else if (type == NULL_TREE) 689018334Speter { 689190287Sobrien int is_main; 689290287Sobrien 689318334Speter explicit_int = -1; 689452290Sobrien 689590287Sobrien /* We handle `main' specially here, because 'main () { }' is so 689690287Sobrien common. With no options, it is allowed. With -Wreturn-type, 689790287Sobrien it is a warning. It is only an error with -pedantic-errors. */ 689890287Sobrien is_main = (funcdef_flag 689990287Sobrien && dname && MAIN_NAME_P (dname) 690090287Sobrien && ctype == NULL_TREE 690190287Sobrien && in_namespace == NULL_TREE 690290287Sobrien && current_namespace == global_namespace); 690352290Sobrien 690490287Sobrien if (in_system_header || flag_ms_extensions) 690590287Sobrien /* Allow it, sigh. */; 690690287Sobrien else if (pedantic || ! is_main) 690790287Sobrien pedwarn ("ISO C++ forbids declaration of `%s' with no type", 690890287Sobrien name); 690990287Sobrien else if (warn_return_type) 691090287Sobrien warning ("ISO C++ forbids declaration of `%s' with no type", 691190287Sobrien name); 691290287Sobrien 691390287Sobrien type = integer_type_node; 691418334Speter } 691590287Sobrien 691618334Speter ctype = NULL_TREE; 691718334Speter 691818334Speter /* Now process the modifiers that were specified 691918334Speter and check for invalid combinations. */ 692018334Speter 692118334Speter /* Long double is a special combination. */ 692218334Speter 692318334Speter if (RIDBIT_SETP (RID_LONG, specbits) 692418334Speter && TYPE_MAIN_VARIANT (type) == double_type_node) 692518334Speter { 692618334Speter RIDBIT_RESET (RID_LONG, specbits); 692790287Sobrien type = build_qualified_type (long_double_type_node, 692890287Sobrien cp_type_quals (type)); 692918334Speter } 693018334Speter 693118334Speter /* Check all other uses of type modifiers. */ 693218334Speter 693318334Speter if (RIDBIT_SETP (RID_UNSIGNED, specbits) 693418334Speter || RIDBIT_SETP (RID_SIGNED, specbits) 693518334Speter || RIDBIT_SETP (RID_LONG, specbits) 693618334Speter || RIDBIT_SETP (RID_SHORT, specbits)) 693718334Speter { 693818334Speter int ok = 0; 693918334Speter 694018334Speter if (TREE_CODE (type) == REAL_TYPE) 694118334Speter error ("short, signed or unsigned invalid for `%s'", name); 694218334Speter else if (TREE_CODE (type) != INTEGER_TYPE) 694318334Speter error ("long, short, signed or unsigned invalid for `%s'", name); 694418334Speter else if (RIDBIT_SETP (RID_LONG, specbits) 694518334Speter && RIDBIT_SETP (RID_SHORT, specbits)) 694618334Speter error ("long and short specified together for `%s'", name); 694718334Speter else if ((RIDBIT_SETP (RID_LONG, specbits) 694818334Speter || RIDBIT_SETP (RID_SHORT, specbits)) 694918334Speter && explicit_char) 695018334Speter error ("long or short specified with char for `%s'", name); 695118334Speter else if ((RIDBIT_SETP (RID_LONG, specbits) 695218334Speter || RIDBIT_SETP (RID_SHORT, specbits)) 695318334Speter && TREE_CODE (type) == REAL_TYPE) 695418334Speter error ("long or short specified with floating type for `%s'", name); 695518334Speter else if (RIDBIT_SETP (RID_SIGNED, specbits) 695618334Speter && RIDBIT_SETP (RID_UNSIGNED, specbits)) 695718334Speter error ("signed and unsigned given together for `%s'", name); 695818334Speter else 695918334Speter { 696018334Speter ok = 1; 696151412Sobrien if (!explicit_int && !defaulted_int && !explicit_char && pedantic) 696218334Speter { 696318334Speter pedwarn ("long, short, signed or unsigned used invalidly for `%s'", 696418334Speter name); 696518334Speter if (flag_pedantic_errors) 696618334Speter ok = 0; 696718334Speter } 696818334Speter } 696918334Speter 697018334Speter /* Discard the type modifiers if they are invalid. */ 697118334Speter if (! ok) 697218334Speter { 697318334Speter RIDBIT_RESET (RID_UNSIGNED, specbits); 697418334Speter RIDBIT_RESET (RID_SIGNED, specbits); 697518334Speter RIDBIT_RESET (RID_LONG, specbits); 697618334Speter RIDBIT_RESET (RID_SHORT, specbits); 697718334Speter longlong = 0; 697818334Speter } 697918334Speter } 698018334Speter 698151412Sobrien if (RIDBIT_SETP (RID_COMPLEX, specbits) 698251412Sobrien && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) 698351412Sobrien { 698451412Sobrien error ("complex invalid for `%s'", name); 698551412Sobrien RIDBIT_RESET (RID_COMPLEX, specbits); 698651412Sobrien } 698751412Sobrien 698818334Speter /* Decide whether an integer type is signed or not. 698918334Speter Optionally treat bitfields as signed by default. */ 699018334Speter if (RIDBIT_SETP (RID_UNSIGNED, specbits) 699190287Sobrien /* [class.bit] 699290287Sobrien 699390287Sobrien It is implementation-defined whether a plain (neither 699490287Sobrien explicitly signed or unsigned) char, short, int, or long 699590287Sobrien bit-field is signed or unsigned. 699690287Sobrien 699790287Sobrien Naturally, we extend this to long long as well. Note that 699890287Sobrien this does not include wchar_t. */ 699990287Sobrien || (bitfield && !flag_signed_bitfields 700090287Sobrien && RIDBIT_NOTSETP (RID_SIGNED, specbits) 700190287Sobrien /* A typedef for plain `int' without `signed' can be 700290287Sobrien controlled just like plain `int', but a typedef for 700390287Sobrien `signed int' cannot be so controlled. */ 700490287Sobrien && !(typedef_decl 700590287Sobrien && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) 700690287Sobrien && (TREE_CODE (type) == INTEGER_TYPE 700790287Sobrien || TREE_CODE (type) == CHAR_TYPE) 700890287Sobrien && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) 700918334Speter { 701018334Speter if (longlong) 701118334Speter type = long_long_unsigned_type_node; 701218334Speter else if (RIDBIT_SETP (RID_LONG, specbits)) 701318334Speter type = long_unsigned_type_node; 701418334Speter else if (RIDBIT_SETP (RID_SHORT, specbits)) 701518334Speter type = short_unsigned_type_node; 701618334Speter else if (type == char_type_node) 701718334Speter type = unsigned_char_type_node; 701818334Speter else if (typedef_decl) 7019117410Skan type = c_common_unsigned_type (type); 702018334Speter else 702118334Speter type = unsigned_type_node; 702218334Speter } 702318334Speter else if (RIDBIT_SETP (RID_SIGNED, specbits) 702418334Speter && type == char_type_node) 702518334Speter type = signed_char_type_node; 702618334Speter else if (longlong) 702718334Speter type = long_long_integer_type_node; 702818334Speter else if (RIDBIT_SETP (RID_LONG, specbits)) 702918334Speter type = long_integer_type_node; 703018334Speter else if (RIDBIT_SETP (RID_SHORT, specbits)) 703118334Speter type = short_integer_type_node; 703218334Speter 703351412Sobrien if (RIDBIT_SETP (RID_COMPLEX, specbits)) 703451412Sobrien { 703551412Sobrien /* If we just have "complex", it is equivalent to 703651412Sobrien "complex double", but if any modifiers at all are specified it is 703751412Sobrien the complex form of TYPE. E.g, "complex short" is 703851412Sobrien "complex short int". */ 703951412Sobrien 704051412Sobrien if (defaulted_int && ! longlong 704151412Sobrien && ! (RIDBIT_SETP (RID_LONG, specbits) 704251412Sobrien || RIDBIT_SETP (RID_SHORT, specbits) 704351412Sobrien || RIDBIT_SETP (RID_SIGNED, specbits) 704451412Sobrien || RIDBIT_SETP (RID_UNSIGNED, specbits))) 704551412Sobrien type = complex_double_type_node; 704651412Sobrien else if (type == integer_type_node) 704751412Sobrien type = complex_integer_type_node; 704851412Sobrien else if (type == float_type_node) 704951412Sobrien type = complex_float_type_node; 705051412Sobrien else if (type == double_type_node) 705151412Sobrien type = complex_double_type_node; 705251412Sobrien else if (type == long_double_type_node) 705351412Sobrien type = complex_long_double_type_node; 705451412Sobrien else 705551412Sobrien type = build_complex_type (type); 705651412Sobrien } 705751412Sobrien 705896292Sobrien type_quals = TYPE_UNQUALIFIED; 705996292Sobrien if (RIDBIT_SETP (RID_CONST, specbits)) 706096292Sobrien type_quals |= TYPE_QUAL_CONST; 706196292Sobrien if (RIDBIT_SETP (RID_VOLATILE, specbits)) 706296292Sobrien type_quals |= TYPE_QUAL_VOLATILE; 706396292Sobrien if (RIDBIT_SETP (RID_RESTRICT, specbits)) 706496292Sobrien type_quals |= TYPE_QUAL_RESTRICT; 706596292Sobrien if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED) 706690287Sobrien error ("qualifiers are not allowed on declaration of `operator %T'", 706751412Sobrien ctor_return_type); 706851412Sobrien 706996292Sobrien type_quals |= cp_type_quals (type); 707096292Sobrien type = cp_build_qualified_type_real 707196292Sobrien (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl) 707296292Sobrien ? tf_ignore_bad_quals : 0) | tf_error | tf_warning)); 707396292Sobrien /* We might have ignored or rejected some of the qualifiers. */ 707496292Sobrien type_quals = cp_type_quals (type); 707596292Sobrien 707618334Speter staticp = 0; 707718334Speter inlinep = !! RIDBIT_SETP (RID_INLINE, specbits); 707818334Speter virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits); 707918334Speter RIDBIT_RESET (RID_VIRTUAL, specbits); 708018334Speter explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0; 708118334Speter RIDBIT_RESET (RID_EXPLICIT, specbits); 708218334Speter 708318334Speter if (RIDBIT_SETP (RID_STATIC, specbits)) 708418334Speter staticp = 1 + (decl_context == FIELD); 708518334Speter 708618334Speter if (virtualp && staticp == 2) 708718334Speter { 708890287Sobrien error ("member `%D' cannot be declared both virtual and static", 708918334Speter dname); 709018334Speter staticp = 0; 709118334Speter } 709218334Speter friendp = RIDBIT_SETP (RID_FRIEND, specbits); 709318334Speter RIDBIT_RESET (RID_FRIEND, specbits); 709418334Speter 709596292Sobrien if (dependant_name && !friendp) 709696292Sobrien { 709796292Sobrien error ("`%T::%D' is not a valid declarator", ctype, dependant_name); 709896292Sobrien return void_type_node; 709996292Sobrien } 710096292Sobrien 710118334Speter /* Warn if two storage classes are given. Default to `auto'. */ 710218334Speter 710318334Speter if (RIDBIT_ANY_SET (specbits)) 710418334Speter { 710518334Speter if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++; 710690287Sobrien if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++; 7107117410Skan if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++; 710818334Speter if (decl_context == PARM && nclasses > 0) 710918334Speter error ("storage class specifiers invalid in parameter declarations"); 711018334Speter if (RIDBIT_SETP (RID_TYPEDEF, specbits)) 711118334Speter { 711218334Speter if (decl_context == PARM) 711318334Speter error ("typedef declaration invalid in parameter declaration"); 711418334Speter nclasses++; 711518334Speter } 711618334Speter if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++; 711718334Speter if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++; 711890287Sobrien if (!nclasses && !friendp && extern_langp) 711990287Sobrien nclasses++; 712018334Speter } 712118334Speter 712218334Speter /* Give error if `virtual' is used outside of class declaration. */ 712318334Speter if (virtualp 712418334Speter && (current_class_name == NULL_TREE || decl_context != FIELD)) 712518334Speter { 712618334Speter error ("virtual outside class declaration"); 712718334Speter virtualp = 0; 712818334Speter } 712918334Speter 713018334Speter /* Static anonymous unions are dealt with here. */ 713118334Speter if (staticp && decl_context == TYPENAME 713218334Speter && TREE_CODE (declspecs) == TREE_LIST 713390287Sobrien && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs))) 713418334Speter decl_context = FIELD; 713518334Speter 713618334Speter /* Warn about storage classes that are invalid for certain 713718334Speter kinds of declarations (parameters, typenames, etc.). */ 713818334Speter 7139117410Skan /* "static __thread" and "extern __thread" are allowed. */ 7140117410Skan if (nclasses == 2 7141117410Skan && RIDBIT_SETP (RID_THREAD, specbits) 7142117410Skan && (RIDBIT_SETP (RID_EXTERN, specbits) 7143117410Skan || RIDBIT_SETP (RID_STATIC, specbits))) 7144117410Skan nclasses = 1; 7145117410Skan 714618334Speter if (nclasses > 1) 714718334Speter error ("multiple storage classes in declaration of `%s'", name); 714818334Speter else if (decl_context != NORMAL && nclasses > 0) 714918334Speter { 715018334Speter if ((decl_context == PARM || decl_context == CATCHPARM) 715118334Speter && (RIDBIT_SETP (RID_REGISTER, specbits) 715218334Speter || RIDBIT_SETP (RID_AUTO, specbits))) 715318334Speter ; 715451412Sobrien else if (RIDBIT_SETP (RID_TYPEDEF, specbits)) 715551412Sobrien ; 715618334Speter else if (decl_context == FIELD 7157132747Skan /* C++ allows static class elements. */ 715890287Sobrien && RIDBIT_SETP (RID_STATIC, specbits)) 715990287Sobrien /* C++ also allows inlines and signed and unsigned elements, 716090287Sobrien but in those cases we don't come in here. */ 716118334Speter ; 716218334Speter else 716318334Speter { 716418334Speter if (decl_context == FIELD) 716518334Speter { 716618334Speter tree tmp = NULL_TREE; 7167132747Skan int op = 0; 716818334Speter 716918334Speter if (declarator) 717018334Speter { 717190287Sobrien /* Avoid trying to get an operand off an identifier node. */ 717251412Sobrien if (TREE_CODE (declarator) == IDENTIFIER_NODE) 717351412Sobrien tmp = declarator; 717451412Sobrien else 717551412Sobrien tmp = TREE_OPERAND (declarator, 0); 717618334Speter op = IDENTIFIER_OPNAME_P (tmp); 717790287Sobrien if (IDENTIFIER_TYPENAME_P (tmp)) 717890287Sobrien { 7179132747Skan if (is_typename_at_global_scope (tmp)) 718090287Sobrien name = IDENTIFIER_POINTER (tmp); 718190287Sobrien else 718290287Sobrien name = "<invalid operator>"; 718390287Sobrien } 718418334Speter } 718518334Speter error ("storage class specified for %s `%s'", 718690287Sobrien op ? "member operator" : "field", 718790287Sobrien name); 718818334Speter } 718918334Speter else 719090287Sobrien { 719190287Sobrien if (decl_context == PARM || decl_context == CATCHPARM) 719290287Sobrien error ("storage class specified for parameter `%s'", name); 719390287Sobrien else 719490287Sobrien error ("storage class specified for typename"); 719590287Sobrien } 719618334Speter RIDBIT_RESET (RID_REGISTER, specbits); 719718334Speter RIDBIT_RESET (RID_AUTO, specbits); 719818334Speter RIDBIT_RESET (RID_EXTERN, specbits); 7199117410Skan RIDBIT_RESET (RID_THREAD, specbits); 720018334Speter } 720118334Speter } 720218334Speter else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag) 720318334Speter { 720418334Speter if (toplevel_bindings_p ()) 720518334Speter { 720618334Speter /* It's common practice (and completely valid) to have a const 720718334Speter be initialized and declared extern. */ 720852290Sobrien if (!(type_quals & TYPE_QUAL_CONST)) 720918334Speter warning ("`%s' initialized and declared `extern'", name); 721018334Speter } 721118334Speter else 721218334Speter error ("`%s' has both `extern' and initializer", name); 721318334Speter } 721418334Speter else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag 721518334Speter && ! toplevel_bindings_p ()) 721618334Speter error ("nested function `%s' declared `extern'", name); 721718334Speter else if (toplevel_bindings_p ()) 721818334Speter { 721918334Speter if (RIDBIT_SETP (RID_AUTO, specbits)) 722018334Speter error ("top-level declaration of `%s' specifies `auto'", name); 722118334Speter } 7222117410Skan else if (RIDBIT_SETP (RID_THREAD, specbits) 7223117410Skan && !RIDBIT_SETP (RID_EXTERN, specbits) 7224117410Skan && !RIDBIT_SETP (RID_STATIC, specbits)) 7225117410Skan { 7226117410Skan error ("function-scope `%s' implicitly auto and declared `__thread'", 7227117410Skan name); 7228117410Skan RIDBIT_RESET (RID_THREAD, specbits); 7229117410Skan } 723018334Speter 723151412Sobrien if (nclasses > 0 && friendp) 723251412Sobrien error ("storage class specifiers invalid in friend function declarations"); 723351412Sobrien 7234132747Skan scope = get_scope_of_declarator (declarator); 7235132747Skan 723618334Speter /* Now figure out the structure of the declarator proper. 723718334Speter Descend through it, creating more complex types, until we reach 7238117410Skan the declared identifier (or NULL_TREE, in an abstract declarator). */ 723918334Speter 724051412Sobrien while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE 724151412Sobrien && TREE_CODE (declarator) != TEMPLATE_ID_EXPR) 724218334Speter { 724318334Speter /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), 724418334Speter an INDIRECT_REF (for *...), 724518334Speter a CALL_EXPR (for ...(...)), 724618334Speter an identifier (for the name being declared) 724718334Speter or a null pointer (for the place in an absolute declarator 724818334Speter where the name was omitted). 724918334Speter For the last two cases, we have just exited the loop. 725018334Speter 725118334Speter For C++ it could also be 725218334Speter a SCOPE_REF (for class :: ...). In this case, we have converted 725318334Speter sensible names to types, and those are the values we use to 725418334Speter qualify the member name. 725518334Speter an ADDR_EXPR (for &...), 725618334Speter a BIT_NOT_EXPR (for destructors) 725718334Speter 725818334Speter At this point, TYPE is the type of elements of an array, 725918334Speter or for a function to return, or for a pointer to point to. 726018334Speter After this sequence of ifs, TYPE is the type of the 726118334Speter array or function or pointer, and DECLARATOR has had its 726218334Speter outermost layer removed. */ 726318334Speter 726451412Sobrien if (type == error_mark_node) 726518334Speter { 7266132747Skan if (declarator == error_mark_node) 7267132747Skan return error_mark_node; 7268132747Skan else if (TREE_CODE (declarator) == SCOPE_REF) 726918334Speter declarator = TREE_OPERAND (declarator, 1); 727018334Speter else 727118334Speter declarator = TREE_OPERAND (declarator, 0); 727218334Speter continue; 727318334Speter } 727418334Speter if (quals != NULL_TREE 727518334Speter && (declarator == NULL_TREE 727618334Speter || TREE_CODE (declarator) != SCOPE_REF)) 727718334Speter { 727818334Speter if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE) 727918334Speter ctype = TYPE_METHOD_BASETYPE (type); 728018334Speter if (ctype != NULL_TREE) 728118334Speter { 728218334Speter tree dummy = build_decl (TYPE_DECL, NULL_TREE, type); 728390287Sobrien grok_method_quals (ctype, dummy, quals); 728418334Speter type = TREE_TYPE (dummy); 728518334Speter quals = NULL_TREE; 728618334Speter } 728718334Speter } 728852290Sobrien 728918334Speter switch (TREE_CODE (declarator)) 729018334Speter { 729152290Sobrien case TREE_LIST: 729252290Sobrien { 729352290Sobrien /* We encode a declarator with embedded attributes using 729490287Sobrien a TREE_LIST. */ 729590287Sobrien tree attrs = TREE_PURPOSE (declarator); 729690287Sobrien tree inner_decl; 729790287Sobrien int attr_flags; 729890287Sobrien 729952290Sobrien declarator = TREE_VALUE (declarator); 730090287Sobrien inner_decl = declarator; 730190287Sobrien while (inner_decl != NULL_TREE 730290287Sobrien && TREE_CODE (inner_decl) == TREE_LIST) 730390287Sobrien inner_decl = TREE_VALUE (inner_decl); 730490287Sobrien attr_flags = 0; 730590287Sobrien if (inner_decl == NULL_TREE 730690287Sobrien || TREE_CODE (inner_decl) == IDENTIFIER_NODE) 730790287Sobrien attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 730890287Sobrien if (TREE_CODE (inner_decl) == CALL_EXPR) 730990287Sobrien attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 731090287Sobrien if (TREE_CODE (inner_decl) == ARRAY_REF) 731190287Sobrien attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 731290287Sobrien returned_attrs = decl_attributes (&type, 731390287Sobrien chainon (returned_attrs, attrs), 731490287Sobrien attr_flags); 731552290Sobrien } 731652290Sobrien break; 731752290Sobrien 731818334Speter case ARRAY_REF: 731918334Speter { 7320132747Skan tree size = TREE_OPERAND (declarator, 1); 732190287Sobrien declarator = TREE_OPERAND (declarator, 0); 732218334Speter 732390287Sobrien type = create_array_type_for_decl (dname, type, size); 732452290Sobrien 732518334Speter ctype = NULL_TREE; 732618334Speter } 732718334Speter break; 732818334Speter 732918334Speter case CALL_EXPR: 733018334Speter { 733118334Speter tree arg_types; 733218334Speter int funcdecl_p; 733390287Sobrien tree inner_parms = CALL_DECLARATOR_PARMS (declarator); 733418334Speter tree inner_decl = TREE_OPERAND (declarator, 0); 733518334Speter 733618334Speter /* Declaring a function type. 733718334Speter Make sure we have a valid type for the function to return. */ 733818334Speter 733952290Sobrien /* We now know that the TYPE_QUALS don't apply to the 734052290Sobrien decl, but to its return type. */ 734152290Sobrien type_quals = TYPE_UNQUALIFIED; 734218334Speter 734318334Speter /* Warn about some types functions can't return. */ 734418334Speter 734518334Speter if (TREE_CODE (type) == FUNCTION_TYPE) 734618334Speter { 734718334Speter error ("`%s' declared as function returning a function", name); 734818334Speter type = integer_type_node; 734918334Speter } 735018334Speter if (TREE_CODE (type) == ARRAY_TYPE) 735118334Speter { 735218334Speter error ("`%s' declared as function returning an array", name); 735318334Speter type = integer_type_node; 735418334Speter } 735518334Speter 735618334Speter if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF) 735718334Speter inner_decl = TREE_OPERAND (inner_decl, 1); 735818334Speter 735990287Sobrien if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR) 736051412Sobrien inner_decl = dname; 736151412Sobrien 736218334Speter /* Pick up type qualifiers which should be applied to `this'. */ 736390287Sobrien quals = CALL_DECLARATOR_QUALS (declarator); 736418334Speter 736551412Sobrien /* Pick up the exception specifications. */ 736690287Sobrien raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator); 736751412Sobrien 736818334Speter /* Say it's a definition only for the CALL_EXPR 736918334Speter closest to the identifier. */ 737051412Sobrien funcdecl_p 737190287Sobrien = inner_decl 737251412Sobrien && (TREE_CODE (inner_decl) == IDENTIFIER_NODE 737390287Sobrien || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR 737451412Sobrien || TREE_CODE (inner_decl) == BIT_NOT_EXPR); 737590287Sobrien 737618334Speter if (ctype == NULL_TREE 737718334Speter && decl_context == FIELD 737818334Speter && funcdecl_p 737918334Speter && (friendp == 0 || dname == current_class_name)) 738018334Speter ctype = current_class_type; 738118334Speter 738290287Sobrien if (ctype && sfk == sfk_conversion) 738318334Speter TYPE_HAS_CONVERSION (ctype) = 1; 7384132747Skan if (ctype && constructor_name_p (dname, ctype)) 738518334Speter { 738618334Speter /* We are within a class's scope. If our declarator name 738718334Speter is the same as the class name, and we are defining 738818334Speter a function, then it is a constructor/destructor, and 738918334Speter therefore returns a void type. */ 739018334Speter 739118334Speter if (flags == DTOR_FLAG) 739218334Speter { 739390287Sobrien /* ISO C++ 12.4/2. A destructor may not be 739490287Sobrien declared const or volatile. A destructor may 739590287Sobrien not be static. */ 739618334Speter if (staticp == 2) 739718334Speter error ("destructor cannot be static member function"); 739818334Speter if (quals) 739918334Speter { 740090287Sobrien error ("destructors may not be `%s'", 740151412Sobrien IDENTIFIER_POINTER (TREE_VALUE (quals))); 740251412Sobrien quals = NULL_TREE; 740318334Speter } 740418334Speter if (decl_context == FIELD) 740518334Speter { 740690287Sobrien if (! member_function_or_else (ctype, 740790287Sobrien current_class_type, 740890287Sobrien flags)) 740918334Speter return void_type_node; 741018334Speter } 741118334Speter } 741251412Sobrien else /* It's a constructor. */ 741318334Speter { 741418334Speter if (explicitp == 1) 741518334Speter explicitp = 2; 741690287Sobrien /* ISO C++ 12.1. A constructor may not be 741790287Sobrien declared const or volatile. A constructor may 741890287Sobrien not be virtual. A constructor may not be 741990287Sobrien static. */ 742018334Speter if (staticp == 2) 742118334Speter error ("constructor cannot be static member function"); 742218334Speter if (virtualp) 742318334Speter { 742418334Speter pedwarn ("constructors cannot be declared virtual"); 742518334Speter virtualp = 0; 742618334Speter } 742718334Speter if (quals) 742818334Speter { 742990287Sobrien error ("constructors may not be `%s'", 743051412Sobrien IDENTIFIER_POINTER (TREE_VALUE (quals))); 743151412Sobrien quals = NULL_TREE; 743290287Sobrien } 743318334Speter { 743418334Speter RID_BIT_TYPE tmp_bits; 743590287Sobrien memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE)); 743618334Speter RIDBIT_RESET (RID_INLINE, tmp_bits); 743718334Speter RIDBIT_RESET (RID_STATIC, tmp_bits); 743818334Speter if (RIDBIT_ANY_SET (tmp_bits)) 743918334Speter error ("return value type specifier for constructor ignored"); 744018334Speter } 744190287Sobrien if (decl_context == FIELD) 744218334Speter { 744390287Sobrien if (! member_function_or_else (ctype, 744490287Sobrien current_class_type, 744590287Sobrien flags)) 744618334Speter return void_type_node; 744718334Speter TYPE_HAS_CONSTRUCTOR (ctype) = 1; 744890287Sobrien if (sfk != sfk_constructor) 744918334Speter return NULL_TREE; 745018334Speter } 745118334Speter } 745218334Speter if (decl_context == FIELD) 745318334Speter staticp = 0; 745418334Speter } 745518334Speter else if (friendp) 745618334Speter { 745718334Speter if (initialized) 745818334Speter error ("can't initialize friend function `%s'", name); 745918334Speter if (virtualp) 746018334Speter { 746118334Speter /* Cannot be both friend and virtual. */ 746218334Speter error ("virtual functions cannot be friends"); 746318334Speter RIDBIT_RESET (RID_FRIEND, specbits); 746418334Speter friendp = 0; 746518334Speter } 746618334Speter if (decl_context == NORMAL) 746718334Speter error ("friend declaration not in class definition"); 746818334Speter if (current_function_decl && funcdef_flag) 746990287Sobrien error ("can't define friend function `%s' in a local class definition", 747018334Speter name); 747118334Speter } 747218334Speter 747318334Speter /* Construct the function type and go to the next 747418334Speter inner layer of declarator. */ 747518334Speter 747618334Speter declarator = TREE_OPERAND (declarator, 0); 747718334Speter 7478132747Skan arg_types = grokparms (inner_parms, &parms); 747918334Speter 748051412Sobrien if (declarator && flags == DTOR_FLAG) 748118334Speter { 748251412Sobrien /* A destructor declared in the body of a class will 748351412Sobrien be represented as a BIT_NOT_EXPR. But, we just 748451412Sobrien want the underlying IDENTIFIER. */ 748518334Speter if (TREE_CODE (declarator) == BIT_NOT_EXPR) 748651412Sobrien declarator = TREE_OPERAND (declarator, 0); 748790287Sobrien 748890287Sobrien if (arg_types != void_list_node) 748918334Speter { 749090287Sobrien error ("destructors may not have parameters"); 749151412Sobrien arg_types = void_list_node; 7492132747Skan parms = NULL_TREE; 749318334Speter } 749418334Speter } 749518334Speter 749651412Sobrien /* ANSI says that `const int foo ();' 749718334Speter does not make the function foo const. */ 749851412Sobrien type = build_function_type (type, arg_types); 749918334Speter } 750018334Speter break; 750118334Speter 750218334Speter case ADDR_EXPR: 750318334Speter case INDIRECT_REF: 750418334Speter /* Filter out pointers-to-references and references-to-references. 750518334Speter We can get these if a TYPE_DECL is used. */ 750618334Speter 750718334Speter if (TREE_CODE (type) == REFERENCE_TYPE) 750818334Speter { 7509117410Skan error (TREE_CODE (declarator) == ADDR_EXPR 7510117410Skan ? "cannot declare reference to `%#T'" 7511117410Skan : "cannot declare pointer to `%#T'", type); 751218334Speter type = TREE_TYPE (type); 751318334Speter } 7514117410Skan else if (VOID_TYPE_P (type) 7515117410Skan && (ctype || TREE_CODE (declarator) == ADDR_EXPR)) 7516117410Skan error (ctype ? "cannot declare pointer to `%#T' member" 7517117410Skan : "cannot declare reference to `%#T'", type); 751818334Speter 751918334Speter /* Merge any constancy or volatility into the target type 752018334Speter for the pointer. */ 752118334Speter 752252290Sobrien /* We now know that the TYPE_QUALS don't apply to the decl, 752352290Sobrien but to the target of the pointer. */ 752452290Sobrien type_quals = TYPE_UNQUALIFIED; 752518334Speter 752690287Sobrien if (TREE_CODE (declarator) == ADDR_EXPR) 752718334Speter { 7528117410Skan if (!VOID_TYPE_P (type)) 752952290Sobrien type = build_reference_type (type); 753018334Speter } 753118334Speter else if (TREE_CODE (type) == METHOD_TYPE) 753252290Sobrien type = build_ptrmemfunc_type (build_pointer_type (type)); 7533117410Skan else if (ctype) 7534117410Skan type = build_ptrmem_type (ctype, type); 753518334Speter else 753618334Speter type = build_pointer_type (type); 753718334Speter 753818334Speter /* Process a list of type modifier keywords (such as 753918334Speter const or volatile) that were given inside the `*' or `&'. */ 754018334Speter 754118334Speter if (TREE_TYPE (declarator)) 754218334Speter { 7543132747Skan tree typemodlist; 754418334Speter int erred = 0; 754596292Sobrien int constp = 0; 754696292Sobrien int volatilep = 0; 754796292Sobrien int restrictp = 0; 754896292Sobrien 754918334Speter for (typemodlist = TREE_TYPE (declarator); typemodlist; 755018334Speter typemodlist = TREE_CHAIN (typemodlist)) 755118334Speter { 755252290Sobrien tree qualifier = TREE_VALUE (typemodlist); 755352290Sobrien 755452290Sobrien if (qualifier == ridpointers[(int) RID_CONST]) 755596292Sobrien { 755696292Sobrien constp++; 755796292Sobrien type_quals |= TYPE_QUAL_CONST; 755896292Sobrien } 755952290Sobrien else if (qualifier == ridpointers[(int) RID_VOLATILE]) 756096292Sobrien { 756196292Sobrien volatilep++; 756296292Sobrien type_quals |= TYPE_QUAL_VOLATILE; 756396292Sobrien } 756452290Sobrien else if (qualifier == ridpointers[(int) RID_RESTRICT]) 756596292Sobrien { 756696292Sobrien restrictp++; 756796292Sobrien type_quals |= TYPE_QUAL_RESTRICT; 756896292Sobrien } 756918334Speter else if (!erred) 757018334Speter { 757118334Speter erred = 1; 757252290Sobrien error ("invalid type modifier within pointer declarator"); 757318334Speter } 757418334Speter } 757518334Speter if (constp > 1) 757618334Speter pedwarn ("duplicate `const'"); 757718334Speter if (volatilep > 1) 757818334Speter pedwarn ("duplicate `volatile'"); 757952290Sobrien if (restrictp > 1) 758052290Sobrien pedwarn ("duplicate `restrict'"); 758152290Sobrien type = cp_build_qualified_type (type, type_quals); 758296292Sobrien type_quals = cp_type_quals (type); 758318334Speter } 758418334Speter declarator = TREE_OPERAND (declarator, 0); 758518334Speter ctype = NULL_TREE; 758618334Speter break; 758718334Speter 758818334Speter case SCOPE_REF: 758918334Speter { 759018334Speter /* We have converted type names to NULL_TREE if the 759118334Speter name was bogus, or to a _TYPE node, if not. 759218334Speter 759318334Speter The variable CTYPE holds the type we will ultimately 759418334Speter resolve to. The code here just needs to build 759518334Speter up appropriate member types. */ 759618334Speter tree sname = TREE_OPERAND (declarator, 1); 759751412Sobrien tree t; 759851412Sobrien 759918334Speter /* Destructors can have their visibilities changed as well. */ 760018334Speter if (TREE_CODE (sname) == BIT_NOT_EXPR) 760118334Speter sname = TREE_OPERAND (sname, 0); 760218334Speter 760318334Speter if (TREE_OPERAND (declarator, 0) == NULL_TREE) 760418334Speter { 760518334Speter /* We had a reference to a global decl, or 760618334Speter perhaps we were given a non-aggregate typedef, 760718334Speter in which case we cleared this out, and should just 760818334Speter keep going as though it wasn't there. */ 760918334Speter declarator = sname; 761018334Speter continue; 761118334Speter } 761218334Speter ctype = TREE_OPERAND (declarator, 0); 761318334Speter 761451412Sobrien t = ctype; 7615132747Skan if (TREE_CODE (TREE_OPERAND (declarator, 1)) != INDIRECT_REF) 7616132747Skan while (t != NULL_TREE && CLASS_TYPE_P (t)) 7617132747Skan { 7618132747Skan /* You're supposed to have one `template <...>' 7619132747Skan for every template class, but you don't need one 7620132747Skan for a full specialization. For example: 7621132747Skan 762290287Sobrien template <class T> struct S{}; 762390287Sobrien template <> struct S<int> { void f(); }; 762490287Sobrien void S<int>::f () {} 7625132747Skan 7626132747Skan is correct; there shouldn't be a `template <>' for 7627132747Skan the definition of `S<int>::f'. */ 7628132747Skan if (CLASSTYPE_TEMPLATE_INFO (t) 7629132747Skan && (CLASSTYPE_TEMPLATE_INSTANTIATION (t) 7630132747Skan || uses_template_parms (CLASSTYPE_TI_ARGS (t))) 7631132747Skan && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) 7632132747Skan template_count += 1; 7633132747Skan 7634132747Skan t = TYPE_MAIN_DECL (t); 7635132747Skan t = DECL_CONTEXT (t); 7636132747Skan } 763790287Sobrien 763818334Speter if (sname == NULL_TREE) 763918334Speter goto done_scoping; 764018334Speter 764118334Speter if (TREE_CODE (sname) == IDENTIFIER_NODE) 764218334Speter { 764318334Speter /* This is the `standard' use of the scoping operator: 764418334Speter basetype :: member . */ 764518334Speter 764618334Speter if (ctype == current_class_type) 764718334Speter { 764818334Speter /* class A { 764918334Speter void A::f (); 765018334Speter }; 765118334Speter 765218334Speter Is this ill-formed? */ 765318334Speter 765418334Speter if (pedantic) 765590287Sobrien pedwarn ("extra qualification `%T::' on member `%s' ignored", 765618334Speter ctype, name); 765718334Speter } 765818334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 765918334Speter { 7660132747Skan if (NEW_DELETE_OPNAME_P (sname)) 7661132747Skan /* Overloaded operator new and operator delete 7662132747Skan are always static functions. */ 7663132747Skan ; 7664132747Skan else if (current_class_type == NULL_TREE || friendp) 7665132747Skan type 7666132747Skan = build_method_type_directly (ctype, 7667132747Skan TREE_TYPE (type), 766852290Sobrien TYPE_ARG_TYPES (type)); 766918334Speter else 767018334Speter { 767190287Sobrien error ("cannot declare member function `%T::%s' within `%T'", 767218334Speter ctype, name, current_class_type); 7673132747Skan return error_mark_node; 767418334Speter } 767518334Speter } 767651412Sobrien else if (RIDBIT_SETP (RID_TYPEDEF, specbits) 767790287Sobrien || COMPLETE_TYPE_P (complete_type (ctype))) 767818334Speter { 767951412Sobrien /* Have to move this code elsewhere in this function. 768018334Speter this code is used for i.e., typedef int A::M; M *pm; 768118334Speter 768218334Speter It is? How? jason 10/2/94 */ 768318334Speter 768418334Speter if (current_class_type) 768518334Speter { 768690287Sobrien error ("cannot declare member `%T::%s' within `%T'", 768718334Speter ctype, name, current_class_type); 768818334Speter return void_type_node; 768918334Speter } 769018334Speter } 769118334Speter else 769290287Sobrien { 7693117410Skan cxx_incomplete_type_error (NULL_TREE, ctype); 769490287Sobrien return error_mark_node; 769518334Speter } 769618334Speter 769718334Speter declarator = sname; 769818334Speter } 769918334Speter else if (TREE_CODE (sname) == SCOPE_REF) 770090287Sobrien abort (); 770118334Speter else 770218334Speter { 770318334Speter done_scoping: 770418334Speter declarator = TREE_OPERAND (declarator, 1); 770518334Speter if (declarator && TREE_CODE (declarator) == CALL_EXPR) 770618334Speter /* In this case, we will deal with it later. */ 770718334Speter ; 7708117410Skan else if (TREE_CODE (type) == FUNCTION_TYPE) 7709132747Skan type = build_method_type_directly (ctype, 7710132747Skan TREE_TYPE (type), 7711132747Skan TYPE_ARG_TYPES (type)); 771218334Speter } 771318334Speter } 771418334Speter break; 771518334Speter 771618334Speter case BIT_NOT_EXPR: 771718334Speter declarator = TREE_OPERAND (declarator, 0); 771818334Speter break; 771918334Speter 7720117410Skan case BASELINK: 7721117410Skan declarator = BASELINK_FUNCTIONS (declarator); 7722117410Skan break; 7723117410Skan 772418334Speter case RECORD_TYPE: 772518334Speter case UNION_TYPE: 772618334Speter case ENUMERAL_TYPE: 772718334Speter declarator = NULL_TREE; 772818334Speter break; 772918334Speter 773018334Speter case ERROR_MARK: 773118334Speter declarator = NULL_TREE; 773218334Speter break; 773318334Speter 773418334Speter default: 773590287Sobrien abort (); 773618334Speter } 773718334Speter } 773818334Speter 773990287Sobrien if (returned_attrs) 774052290Sobrien { 774190287Sobrien if (attrlist) 774290287Sobrien *attrlist = chainon (returned_attrs, *attrlist); 774352290Sobrien else 774490287Sobrien attrlist = &returned_attrs; 774552290Sobrien } 774652290Sobrien 774752290Sobrien /* Now TYPE has the actual type. */ 774852290Sobrien 774990287Sobrien /* Did array size calculations overflow? */ 775090287Sobrien 775190287Sobrien if (TREE_CODE (type) == ARRAY_TYPE 775290287Sobrien && COMPLETE_TYPE_P (type) 775390287Sobrien && TREE_OVERFLOW (TYPE_SIZE (type))) 775418334Speter { 775590287Sobrien error ("size of array `%s' is too large", name); 775690287Sobrien /* If we proceed with the array type as it is, we'll eventually 775790287Sobrien crash in tree_low_cst(). */ 775890287Sobrien type = error_mark_node; 775990287Sobrien } 776090287Sobrien 7761117410Skan if ((decl_context == FIELD || decl_context == PARM) 7762107601Sobrien && !processing_template_decl 7763107601Sobrien && variably_modified_type_p (type)) 7764107601Sobrien { 7765117410Skan if (decl_context == FIELD) 7766117410Skan error ("data member may not have variably modified type `%T'", type); 7767117410Skan else 7768117410Skan error ("parameter may not have variably modified type `%T'", type); 7769107601Sobrien type = error_mark_node; 7770107601Sobrien } 7771107601Sobrien 777290287Sobrien if (explicitp == 1 || (explicitp && friendp)) 777390287Sobrien { 777490287Sobrien /* [dcl.fct.spec] The explicit specifier shall only be used in 777590287Sobrien declarations of constructors within a class definition. */ 777690287Sobrien error ("only declarations of constructors can be `explicit'"); 777718334Speter explicitp = 0; 777818334Speter } 777918334Speter 778018334Speter if (RIDBIT_SETP (RID_MUTABLE, specbits)) 778118334Speter { 7782117410Skan if (decl_context != FIELD || friendp) 778390287Sobrien { 778490287Sobrien error ("non-member `%s' cannot be declared `mutable'", name); 778590287Sobrien RIDBIT_RESET (RID_MUTABLE, specbits); 778690287Sobrien } 778790287Sobrien else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits)) 778818334Speter { 778990287Sobrien error ("non-object member `%s' cannot be declared `mutable'", name); 779018334Speter RIDBIT_RESET (RID_MUTABLE, specbits); 779118334Speter } 779290287Sobrien else if (TREE_CODE (type) == FUNCTION_TYPE 779390287Sobrien || TREE_CODE (type) == METHOD_TYPE) 779490287Sobrien { 779590287Sobrien error ("function `%s' cannot be declared `mutable'", name); 779690287Sobrien RIDBIT_RESET (RID_MUTABLE, specbits); 779790287Sobrien } 779818334Speter else if (staticp) 779918334Speter { 780018334Speter error ("static `%s' cannot be declared `mutable'", name); 780118334Speter RIDBIT_RESET (RID_MUTABLE, specbits); 780218334Speter } 780390287Sobrien else if (type_quals & TYPE_QUAL_CONST) 780490287Sobrien { 780590287Sobrien error ("const `%s' cannot be declared `mutable'", name); 780696292Sobrien RIDBIT_RESET (RID_MUTABLE, specbits); 780790287Sobrien } 780818334Speter } 780918334Speter 781052290Sobrien if (declarator == NULL_TREE 781152290Sobrien || TREE_CODE (declarator) == IDENTIFIER_NODE 781252290Sobrien || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR 781352290Sobrien && (TREE_CODE (type) == FUNCTION_TYPE 781452290Sobrien || TREE_CODE (type) == METHOD_TYPE))) 781552290Sobrien /* OK */; 781652290Sobrien else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) 781752290Sobrien { 781890287Sobrien error ("template-id `%D' used as a declarator", declarator); 781952290Sobrien declarator = dname; 782052290Sobrien } 782152290Sobrien else 782252290Sobrien /* Unexpected declarator format. */ 782390287Sobrien abort (); 782452290Sobrien 782552290Sobrien /* If this is declaring a typedef name, return a TYPE_DECL. */ 782652290Sobrien 782751412Sobrien if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME) 782818334Speter { 782918334Speter tree decl; 783018334Speter 783118334Speter /* Note that the grammar rejects storage classes 783218334Speter in typenames, fields or parameters. */ 783351412Sobrien if (current_lang_name == lang_name_java) 783451412Sobrien TYPE_FOR_JAVA (type) = 1; 783518334Speter 783651412Sobrien if (decl_context == FIELD) 783751412Sobrien { 7838132747Skan if (constructor_name_p (declarator, current_class_type)) 783990287Sobrien pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class", 784051412Sobrien declarator); 784151412Sobrien decl = build_lang_decl (TYPE_DECL, declarator, type); 784251412Sobrien } 784351412Sobrien else 784451412Sobrien { 784551412Sobrien decl = build_decl (TYPE_DECL, declarator, type); 7846132747Skan if (in_namespace || ctype) 7847132747Skan error ("%Jtypedef name may not be a nested-name-specifier", decl); 784890287Sobrien if (!current_function_decl) 784990287Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 785051412Sobrien } 785190287Sobrien 785290287Sobrien /* If the user declares "typedef struct {...} foo" then the 785390287Sobrien struct will have an anonymous name. Fill that name in now. 785490287Sobrien Nothing can refer to it, so nothing needs know about the name 785590287Sobrien change. */ 785618334Speter if (type != error_mark_node 785790287Sobrien && declarator 785818334Speter && TYPE_NAME (type) 785918334Speter && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL 786090287Sobrien && TYPE_ANONYMOUS_P (type) 786196292Sobrien /* Don't do this if there are attributes. */ 786296292Sobrien && (!attrlist || !*attrlist) 786390287Sobrien && cp_type_quals (type) == TYPE_UNQUALIFIED) 786418334Speter { 786552290Sobrien tree oldname = TYPE_NAME (type); 786652290Sobrien tree t; 786752290Sobrien 786851412Sobrien /* Replace the anonymous name with the real name everywhere. */ 786918334Speter lookup_tag_reverse (type, declarator); 787052290Sobrien for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 787152290Sobrien if (TYPE_NAME (t) == oldname) 787252290Sobrien TYPE_NAME (t) = decl; 787318334Speter 787418334Speter if (TYPE_LANG_SPECIFIC (type)) 787518334Speter TYPE_WAS_ANONYMOUS (type) = 1; 787618334Speter 787752290Sobrien /* If this is a typedef within a template class, the nested 787852290Sobrien type is a (non-primary) template. The name for the 787952290Sobrien template needs updating as well. */ 788052290Sobrien if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) 788190287Sobrien DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) 788252290Sobrien = TYPE_IDENTIFIER (type); 788352290Sobrien 788452290Sobrien /* FIXME remangle member functions; member functions of a 788552290Sobrien type with external linkage have external linkage. */ 788618334Speter } 788718334Speter 7888132747Skan if (quals) 788918334Speter { 789018334Speter if (ctype == NULL_TREE) 789118334Speter { 789218334Speter if (TREE_CODE (type) != METHOD_TYPE) 7893132747Skan error ("%Jinvalid type qualifier for non-member function type", 7894132747Skan decl); 789518334Speter else 789618334Speter ctype = TYPE_METHOD_BASETYPE (type); 789718334Speter } 789818334Speter if (ctype != NULL_TREE) 789918334Speter grok_method_quals (ctype, decl, quals); 790018334Speter } 790118334Speter 790218334Speter if (RIDBIT_SETP (RID_SIGNED, specbits) 790318334Speter || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 790418334Speter C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 790518334Speter 790651412Sobrien bad_specifiers (decl, "type", virtualp, quals != NULL_TREE, 790751412Sobrien inlinep, friendp, raises != NULL_TREE); 790851412Sobrien 790918334Speter return decl; 791018334Speter } 791118334Speter 791218334Speter /* Detect the case of an array type of unspecified size 791318334Speter which came, as such, direct from a typedef name. 791496292Sobrien We must copy the type, so that the array's domain can be 791596292Sobrien individually set by the object's initializer. */ 791618334Speter 791796292Sobrien if (type && typedef_type 791896292Sobrien && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) 791990287Sobrien && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) 792096292Sobrien type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); 792118334Speter 792290287Sobrien /* Detect where we're using a typedef of function type to declare a 7923132747Skan function. PARMS will not be set, so we must create it now. */ 792490287Sobrien 792590287Sobrien if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) 792690287Sobrien { 792790287Sobrien tree decls = NULL_TREE; 792890287Sobrien tree args; 792990287Sobrien 793090287Sobrien for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) 793190287Sobrien { 7932117410Skan tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args)); 793390287Sobrien 793490287Sobrien TREE_CHAIN (decl) = decls; 793590287Sobrien decls = decl; 793690287Sobrien } 793790287Sobrien 7938132747Skan parms = nreverse (decls); 793990287Sobrien } 794090287Sobrien 794118334Speter /* If this is a type name (such as, in a cast or sizeof), 794218334Speter compute the type and return it now. */ 794318334Speter 794418334Speter if (decl_context == TYPENAME) 794518334Speter { 794618334Speter /* Note that the grammar rejects storage classes 794718334Speter in typenames, fields or parameters. */ 794852290Sobrien if (type_quals != TYPE_UNQUALIFIED) 794990287Sobrien type_quals = TYPE_UNQUALIFIED; 795018334Speter 795118334Speter /* Special case: "friend class foo" looks like a TYPENAME context. */ 795218334Speter if (friendp) 795318334Speter { 795452290Sobrien if (type_quals != TYPE_UNQUALIFIED) 795518334Speter { 795690287Sobrien error ("type qualifiers specified for friend class declaration"); 795752290Sobrien type_quals = TYPE_UNQUALIFIED; 795818334Speter } 795918334Speter if (inlinep) 796018334Speter { 796190287Sobrien error ("`inline' specified for friend class declaration"); 796218334Speter inlinep = 0; 796318334Speter } 796418334Speter 7965117410Skan if (!current_aggr) 796690287Sobrien { 7967117410Skan /* Don't allow friend declaration without a class-key. */ 796890287Sobrien if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 7969117410Skan pedwarn ("template parameters cannot be friends"); 797090287Sobrien else if (TREE_CODE (type) == TYPENAME_TYPE) 7971117410Skan pedwarn ("friend declaration requires class-key, " 7972117410Skan "i.e. `friend class %T::%D'", 7973117410Skan TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type)); 797490287Sobrien else 7975117410Skan pedwarn ("friend declaration requires class-key, " 7976117410Skan "i.e. `friend %#T'", 7977117410Skan type); 797890287Sobrien } 797990287Sobrien 798018334Speter /* Only try to do this stuff if we didn't already give up. */ 798118334Speter if (type != integer_type_node) 798218334Speter { 798318334Speter /* A friendly class? */ 798418334Speter if (current_class_type) 7985132747Skan make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type), 7986132747Skan /*complain=*/true); 798718334Speter else 798890287Sobrien error ("trying to make class `%T' a friend of global scope", 798990287Sobrien type); 799090287Sobrien 799118334Speter type = void_type_node; 799218334Speter } 799318334Speter } 799418334Speter else if (quals) 799518334Speter { 799618334Speter if (ctype == NULL_TREE) 799718334Speter { 799890287Sobrien if (TREE_CODE (type) != METHOD_TYPE) 799990287Sobrien error ("invalid qualifiers on non-member function type"); 800090287Sobrien else 800190287Sobrien ctype = TYPE_METHOD_BASETYPE (type); 800218334Speter } 800390287Sobrien if (ctype) 800490287Sobrien { 800590287Sobrien tree dummy = build_decl (TYPE_DECL, declarator, type); 800690287Sobrien grok_method_quals (ctype, dummy, quals); 800790287Sobrien type = TREE_TYPE (dummy); 800890287Sobrien } 800918334Speter } 801018334Speter 801118334Speter return type; 801218334Speter } 801318334Speter else if (declarator == NULL_TREE && decl_context != PARM 801418334Speter && decl_context != CATCHPARM 801518334Speter && TREE_CODE (type) != UNION_TYPE 801618334Speter && ! bitfield) 801718334Speter { 801890287Sobrien error ("abstract declarator `%T' used as declaration", type); 801918334Speter declarator = make_anon_name (); 802018334Speter } 802118334Speter 802218334Speter /* `void' at top level (not within pointer) 802318334Speter is allowed only in typedefs or type names. 802418334Speter We don't complain about parms either, but that is because 802518334Speter a better error message can be made later. */ 802618334Speter 802751412Sobrien if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM) 802818334Speter { 802918334Speter if (! declarator) 803018334Speter error ("unnamed variable or field declared void"); 803118334Speter else if (TREE_CODE (declarator) == IDENTIFIER_NODE) 803218334Speter { 803318334Speter if (IDENTIFIER_OPNAME_P (declarator)) 803490287Sobrien abort (); 803518334Speter else 803618334Speter error ("variable or field `%s' declared void", name); 803718334Speter } 803818334Speter else 803918334Speter error ("variable or field declared void"); 804018334Speter type = integer_type_node; 804118334Speter } 804218334Speter 804318334Speter /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 804418334Speter or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 804518334Speter 804652290Sobrien if (decl_context == PARM || decl_context == CATCHPARM) 804752290Sobrien { 804852290Sobrien if (ctype || in_namespace) 804952290Sobrien error ("cannot use `::' in parameter declaration"); 805052290Sobrien 805152290Sobrien /* A parameter declared as an array of T is really a pointer to T. 805252290Sobrien One declared as a function is really a pointer to a function. 805352290Sobrien One declared as a member is really a pointer to member. */ 805452290Sobrien 805552290Sobrien if (TREE_CODE (type) == ARRAY_TYPE) 805652290Sobrien { 805752290Sobrien /* Transfer const-ness of array into that of type pointed to. */ 805852290Sobrien type = build_pointer_type (TREE_TYPE (type)); 805952290Sobrien type_quals = TYPE_UNQUALIFIED; 806052290Sobrien } 806152290Sobrien else if (TREE_CODE (type) == FUNCTION_TYPE) 806252290Sobrien type = build_pointer_type (type); 806352290Sobrien } 806490287Sobrien 806518334Speter { 8066132747Skan tree decl; 806718334Speter 806818334Speter if (decl_context == PARM) 806918334Speter { 8070117410Skan decl = cp_build_parm_decl (declarator, type); 807118334Speter 807218334Speter bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE, 807318334Speter inlinep, friendp, raises != NULL_TREE); 807418334Speter } 807518334Speter else if (decl_context == FIELD) 807618334Speter { 8077132747Skan /* The C99 flexible array extension. */ 8078132747Skan if (!staticp && TREE_CODE (type) == ARRAY_TYPE 8079132747Skan && TYPE_DOMAIN (type) == NULL_TREE) 8080132747Skan { 8081132747Skan tree itype = compute_array_index_type (dname, integer_zero_node); 8082132747Skan type = build_cplus_array_type (TREE_TYPE (type), itype); 8083132747Skan } 8084132747Skan 808518334Speter if (type == error_mark_node) 808618334Speter { 808718334Speter /* Happens when declaring arrays of sizes which 808818334Speter are error_mark_node, for example. */ 808918334Speter decl = NULL_TREE; 809018334Speter } 809152290Sobrien else if (in_namespace && !friendp) 809252290Sobrien { 809352290Sobrien /* Something like struct S { int N::j; }; */ 809490287Sobrien error ("invalid use of `::'"); 809552290Sobrien decl = NULL_TREE; 809652290Sobrien } 809718334Speter else if (TREE_CODE (type) == FUNCTION_TYPE) 809818334Speter { 809918334Speter int publicp = 0; 810051412Sobrien tree function_context; 810118334Speter 810251412Sobrien /* We catch the others as conflicts with the builtin 810351412Sobrien typedefs. */ 810451412Sobrien if (friendp && declarator == ridpointers[(int) RID_SIGNED]) 810551412Sobrien { 810690287Sobrien error ("function `%D' cannot be declared friend", 810751412Sobrien declarator); 810851412Sobrien friendp = 0; 810951412Sobrien } 811051412Sobrien 811118334Speter if (friendp == 0) 811218334Speter { 811318334Speter if (ctype == NULL_TREE) 811418334Speter ctype = current_class_type; 811518334Speter 811618334Speter if (ctype == NULL_TREE) 811718334Speter { 811890287Sobrien error ("can't make `%D' into a method -- not in a class", 811918334Speter declarator); 812018334Speter return void_type_node; 812118334Speter } 812218334Speter 812318334Speter /* ``A union may [ ... ] not [ have ] virtual functions.'' 812418334Speter ARM 9.5 */ 812518334Speter if (virtualp && TREE_CODE (ctype) == UNION_TYPE) 812618334Speter { 812790287Sobrien error ("function `%D' declared virtual inside a union", 812818334Speter declarator); 812918334Speter return void_type_node; 813018334Speter } 813118334Speter 8132132747Skan if (NEW_DELETE_OPNAME_P (declarator)) 813318334Speter { 813418334Speter if (virtualp) 813518334Speter { 813690287Sobrien error ("`%D' cannot be declared virtual, since it is always static", 813718334Speter declarator); 813818334Speter virtualp = 0; 813918334Speter } 814018334Speter } 814118334Speter else if (staticp < 2) 8142132747Skan type = build_method_type_directly (ctype, 8143132747Skan TREE_TYPE (type), 8144132747Skan TYPE_ARG_TYPES (type)); 814518334Speter } 814618334Speter 814718334Speter /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ 814890287Sobrien function_context = (ctype != NULL_TREE) ? 814990287Sobrien decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; 815051412Sobrien publicp = (! friendp || ! staticp) 815151412Sobrien && function_context == NULL_TREE; 815290287Sobrien decl = grokfndecl (ctype, type, 815351412Sobrien TREE_CODE (declarator) != TEMPLATE_ID_EXPR 815451412Sobrien ? declarator : dname, 8155132747Skan parms, 815651412Sobrien declarator, 815752290Sobrien virtualp, flags, quals, raises, 815851412Sobrien friendp ? -1 : 0, friendp, publicp, inlinep, 815951412Sobrien funcdef_flag, template_count, in_namespace); 816018334Speter if (decl == NULL_TREE) 816152290Sobrien return decl; 816251412Sobrien#if 0 816351412Sobrien /* This clobbers the attrs stored in `decl' from `attrlist'. */ 816490287Sobrien /* The decl and setting of decl_attr is also turned off. */ 816590287Sobrien decl = build_decl_attribute_variant (decl, decl_attr); 816651412Sobrien#endif 816718334Speter 816852290Sobrien /* [class.conv.ctor] 816952290Sobrien 817052290Sobrien A constructor declared without the function-specifier 817152290Sobrien explicit that can be called with a single parameter 817252290Sobrien specifies a conversion from the type of its first 817352290Sobrien parameter to the type of its class. Such a constructor 817452290Sobrien is called a converting constructor. */ 817518334Speter if (explicitp == 2) 817618334Speter DECL_NONCONVERTING_P (decl) = 1; 817752290Sobrien else if (DECL_CONSTRUCTOR_P (decl)) 817852290Sobrien { 817952290Sobrien /* The constructor can be called with exactly one 818052290Sobrien parameter if there is at least one parameter, and 818152290Sobrien any subsequent parameters have default arguments. 818290287Sobrien Ignore any compiler-added parms. */ 818390287Sobrien tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl); 818452290Sobrien 818552290Sobrien if (arg_types == void_list_node 818690287Sobrien || (arg_types 818790287Sobrien && TREE_CHAIN (arg_types) 818852290Sobrien && TREE_CHAIN (arg_types) != void_list_node 818952290Sobrien && !TREE_PURPOSE (TREE_CHAIN (arg_types)))) 819052290Sobrien DECL_NONCONVERTING_P (decl) = 1; 819152290Sobrien } 819218334Speter } 819318334Speter else if (TREE_CODE (type) == METHOD_TYPE) 819418334Speter { 819518334Speter /* We only get here for friend declarations of 819618334Speter members of other classes. */ 819718334Speter /* All method decls are public, so tell grokfndecl to set 819818334Speter TREE_PUBLIC, also. */ 8199122192Skan decl = grokfndecl (ctype, type, 8200122192Skan TREE_CODE (declarator) != TEMPLATE_ID_EXPR 8201122192Skan ? declarator : dname, 8202132747Skan parms, 8203122192Skan declarator, 820452290Sobrien virtualp, flags, quals, raises, 820551412Sobrien friendp ? -1 : 0, friendp, 1, 0, funcdef_flag, 820651412Sobrien template_count, in_namespace); 820718334Speter if (decl == NULL_TREE) 820818334Speter return NULL_TREE; 820918334Speter } 8210132747Skan else if (!staticp && !dependent_type_p (type) 821190287Sobrien && !COMPLETE_TYPE_P (complete_type (type)) 821218334Speter && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0)) 821318334Speter { 821418334Speter if (declarator) 821590287Sobrien error ("field `%D' has incomplete type", declarator); 821618334Speter else 821790287Sobrien error ("name `%T' has incomplete type", type); 821818334Speter 821918334Speter /* If we're instantiating a template, tell them which 822018334Speter instantiation made the field's type be incomplete. */ 822118334Speter if (current_class_type 822218334Speter && TYPE_NAME (current_class_type) 822351412Sobrien && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)) 822418334Speter && declspecs && TREE_VALUE (declspecs) 822518334Speter && TREE_TYPE (TREE_VALUE (declspecs)) == type) 822690287Sobrien error (" in instantiation of template `%T'", 822718334Speter current_class_type); 822818334Speter 822918334Speter type = error_mark_node; 823018334Speter decl = NULL_TREE; 823118334Speter } 823218334Speter else 823318334Speter { 823418334Speter if (friendp) 823518334Speter { 823690287Sobrien error ("`%s' is neither function nor member function; cannot be declared friend", 823718334Speter IDENTIFIER_POINTER (declarator)); 823818334Speter friendp = 0; 823918334Speter } 824018334Speter decl = NULL_TREE; 824118334Speter } 824218334Speter 824318334Speter if (friendp) 824418334Speter { 824518334Speter /* Friends are treated specially. */ 824618334Speter if (ctype == current_class_type) 824718334Speter warning ("member functions are implicitly friends of their class"); 8248132747Skan else if (decl && DECL_NAME (decl)) 8249132747Skan { 8250132747Skan if (template_class_depth (current_class_type) == 0) 8251132747Skan { 8252132747Skan decl = check_explicit_specialization 8253132747Skan (declarator, decl, template_count, 8254132747Skan 2 * (funcdef_flag != 0) + 4); 8255132747Skan if (decl == error_mark_node) 8256132747Skan return error_mark_node; 8257132747Skan } 8258132747Skan 8259132747Skan decl = do_friend (ctype, declarator, decl, 8260132747Skan *attrlist, flags, quals, funcdef_flag); 8261132747Skan return decl; 8262132747Skan } 8263132747Skan else 8264132747Skan return void_type_node; 826518334Speter } 826618334Speter 8267132747Skan /* Structure field. It may not be a function, except for C++. */ 826818334Speter 826918334Speter if (decl == NULL_TREE) 827018334Speter { 827118334Speter if (initialized) 827218334Speter { 827351412Sobrien if (!staticp) 827451412Sobrien { 827551412Sobrien /* An attempt is being made to initialize a non-static 827651412Sobrien member. But, from [class.mem]: 827790287Sobrien 827851412Sobrien 4 A member-declarator can contain a 827951412Sobrien constant-initializer only if it declares a static 828051412Sobrien member (_class.static_) of integral or enumeration 828190287Sobrien type, see _class.static.data_. 828251412Sobrien 828351412Sobrien This used to be relatively common practice, but 828451412Sobrien the rest of the compiler does not correctly 828551412Sobrien handle the initialization unless the member is 828651412Sobrien static so we make it static below. */ 828790287Sobrien pedwarn ("ISO C++ forbids initialization of member `%D'", 828851412Sobrien declarator); 828990287Sobrien pedwarn ("making `%D' static", declarator); 829051412Sobrien staticp = 1; 829151412Sobrien } 829251412Sobrien 829352290Sobrien if (uses_template_parms (type)) 829452290Sobrien /* We'll check at instantiation time. */ 829552290Sobrien ; 829652290Sobrien else if (check_static_variable_definition (declarator, 829752290Sobrien type)) 829852290Sobrien /* If we just return the declaration, crashes 829952290Sobrien will sometimes occur. We therefore return 830052290Sobrien void_type_node, as if this was a friend 830152290Sobrien declaration, to cause callers to completely 830252290Sobrien ignore this declaration. */ 830352290Sobrien return void_type_node; 830418334Speter } 830518334Speter 830651412Sobrien if (staticp) 830718334Speter { 830890287Sobrien /* C++ allows static class members. All other work 830990287Sobrien for this is done by grokfield. */ 831090287Sobrien decl = build_lang_decl (VAR_DECL, declarator, type); 831118334Speter TREE_STATIC (decl) = 1; 831218334Speter /* In class context, 'static' means public access. */ 831351412Sobrien TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1; 831418334Speter } 831518334Speter else 831618334Speter { 831790287Sobrien decl = build_decl (FIELD_DECL, declarator, type); 831890287Sobrien DECL_NONADDRESSABLE_P (decl) = bitfield; 831918334Speter if (RIDBIT_SETP (RID_MUTABLE, specbits)) 832018334Speter { 832118334Speter DECL_MUTABLE_P (decl) = 1; 832218334Speter RIDBIT_RESET (RID_MUTABLE, specbits); 832318334Speter } 832418334Speter } 832518334Speter 832618334Speter bad_specifiers (decl, "field", virtualp, quals != NULL_TREE, 832718334Speter inlinep, friendp, raises != NULL_TREE); 832818334Speter } 832918334Speter } 8330132747Skan else if (TREE_CODE (type) == FUNCTION_TYPE 8331132747Skan || TREE_CODE (type) == METHOD_TYPE) 833218334Speter { 833351412Sobrien tree original_name; 833418334Speter int publicp = 0; 833518334Speter 833618334Speter if (! declarator) 833718334Speter return NULL_TREE; 833818334Speter 833951412Sobrien if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) 834051412Sobrien original_name = dname; 834151412Sobrien else 834251412Sobrien original_name = declarator; 834351412Sobrien 834418334Speter if (RIDBIT_SETP (RID_AUTO, specbits)) 834518334Speter error ("storage class `auto' invalid for function `%s'", name); 834618334Speter else if (RIDBIT_SETP (RID_REGISTER, specbits)) 834718334Speter error ("storage class `register' invalid for function `%s'", name); 8348117410Skan else if (RIDBIT_SETP (RID_THREAD, specbits)) 8349117410Skan error ("storage class `__thread' invalid for function `%s'", name); 835018334Speter 835118334Speter /* Function declaration not at top level. 835218334Speter Storage classes other than `extern' are not allowed 835318334Speter and `extern' makes no difference. */ 835418334Speter if (! toplevel_bindings_p () 835518334Speter && (RIDBIT_SETP (RID_STATIC, specbits) 835618334Speter || RIDBIT_SETP (RID_INLINE, specbits)) 835718334Speter && pedantic) 835818334Speter { 835918334Speter if (RIDBIT_SETP (RID_STATIC, specbits)) 836018334Speter pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name); 836118334Speter else 836218334Speter pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name); 836318334Speter } 836490287Sobrien 836518334Speter if (ctype == NULL_TREE) 836618334Speter { 836718334Speter if (virtualp) 836818334Speter { 836918334Speter error ("virtual non-class function `%s'", name); 837018334Speter virtualp = 0; 837118334Speter } 837218334Speter } 8373132747Skan else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2 8374132747Skan && !NEW_DELETE_OPNAME_P (original_name)) 8375132747Skan type = build_method_type_directly (ctype, 8376132747Skan TREE_TYPE (type), 8377132747Skan TYPE_ARG_TYPES (type)); 837818334Speter 837951412Sobrien /* Record presence of `static'. */ 838018334Speter publicp = (ctype != NULL_TREE 838118334Speter || RIDBIT_SETP (RID_EXTERN, specbits) 838251412Sobrien || !RIDBIT_SETP (RID_STATIC, specbits)); 838318334Speter 8384132747Skan decl = grokfndecl (ctype, type, original_name, parms, declarator, 838552290Sobrien virtualp, flags, quals, raises, 838651412Sobrien 1, friendp, 838790287Sobrien publicp, inlinep, funcdef_flag, 838851412Sobrien template_count, in_namespace); 838918334Speter if (decl == NULL_TREE) 839018334Speter return NULL_TREE; 839118334Speter 839218334Speter if (staticp == 1) 839318334Speter { 8394117410Skan int invalid_static = 0; 839518334Speter 839618334Speter /* Don't allow a static member function in a class, and forbid 839718334Speter declaring main to be static. */ 839818334Speter if (TREE_CODE (type) == METHOD_TYPE) 839918334Speter { 840090287Sobrien pedwarn ("cannot declare member function `%D' to have static linkage", decl); 8401117410Skan invalid_static = 1; 840218334Speter } 840318334Speter else if (current_function_decl) 840418334Speter { 840518334Speter /* FIXME need arm citation */ 840618334Speter error ("cannot declare static function inside another function"); 8407117410Skan invalid_static = 1; 840818334Speter } 840918334Speter 8410117410Skan if (invalid_static) 841118334Speter { 841218334Speter staticp = 0; 841318334Speter RIDBIT_RESET (RID_STATIC, specbits); 841418334Speter } 841518334Speter } 841618334Speter } 841718334Speter else 841818334Speter { 841918334Speter /* It's a variable. */ 842018334Speter 842118334Speter /* An uninitialized decl with `extern' is a reference. */ 842290287Sobrien decl = grokvardecl (type, declarator, &specbits, 842390287Sobrien initialized, 842490287Sobrien (type_quals & TYPE_QUAL_CONST) != 0, 8425117410Skan ctype ? ctype : in_namespace); 842618334Speter bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE, 842718334Speter inlinep, friendp, raises != NULL_TREE); 842818334Speter 842918334Speter if (ctype) 843018334Speter { 843118334Speter DECL_CONTEXT (decl) = ctype; 843218334Speter if (staticp == 1) 843318334Speter { 843490287Sobrien pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member"); 843518334Speter staticp = 0; 843618334Speter RIDBIT_RESET (RID_STATIC, specbits); 843718334Speter } 843818334Speter if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl)) 843918334Speter { 844090287Sobrien error ("static member `%D' declared `register'", decl); 844118334Speter RIDBIT_RESET (RID_REGISTER, specbits); 844218334Speter } 844318334Speter if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic) 844418334Speter { 844590287Sobrien pedwarn ("cannot explicitly declare member `%#D' to have extern linkage", 844618334Speter decl); 844718334Speter RIDBIT_RESET (RID_EXTERN, specbits); 844818334Speter } 844918334Speter } 845018334Speter } 845118334Speter 845290287Sobrien my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927); 845318334Speter 845418334Speter /* Record `register' declaration for warnings on & 845518334Speter and in case doing stupid register allocation. */ 845618334Speter 845718334Speter if (RIDBIT_SETP (RID_REGISTER, specbits)) 845818334Speter DECL_REGISTER (decl) = 1; 845918334Speter 846018334Speter if (RIDBIT_SETP (RID_EXTERN, specbits)) 846118334Speter DECL_THIS_EXTERN (decl) = 1; 846218334Speter 846318334Speter if (RIDBIT_SETP (RID_STATIC, specbits)) 846418334Speter DECL_THIS_STATIC (decl) = 1; 846518334Speter 846690287Sobrien /* Record constancy and volatility. There's no need to do this 846790287Sobrien when processing a template; we'll do this for the instantiated 846890287Sobrien declaration based on the type of DECL. */ 846990287Sobrien if (!processing_template_decl) 847090287Sobrien c_apply_type_quals_to_decl (type_quals, decl); 847118334Speter 847218334Speter return decl; 847318334Speter } 847418334Speter} 847518334Speter 847652290Sobrien/* Subroutine of start_function. Ensure that each of the parameter 847752290Sobrien types (as listed in PARMS) is complete, as is required for a 847852290Sobrien function definition. */ 847918334Speter 848018334Speterstatic void 8481132747Skanrequire_complete_types_for_parms (tree parms) 848218334Speter{ 848390287Sobrien for (; parms; parms = TREE_CHAIN (parms)) 848418334Speter { 848590287Sobrien if (VOID_TYPE_P (TREE_TYPE (parms))) 8486132747Skan /* grokparms will have already issued an error. */ 848790287Sobrien TREE_TYPE (parms) = error_mark_node; 848890287Sobrien else if (complete_type_or_else (TREE_TYPE (parms), parms)) 8489117410Skan { 8490117410Skan layout_decl (parms, 0); 8491117410Skan DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms)); 8492117410Skan } 849318334Speter } 849418334Speter} 849518334Speter 8496117410Skan/* Returns nonzero if T is a local variable. */ 849752290Sobrien 849890287Sobrienint 8499132747Skanlocal_variable_p (tree t) 850052290Sobrien{ 850190287Sobrien if ((TREE_CODE (t) == VAR_DECL 850252290Sobrien /* A VAR_DECL with a context that is a _TYPE is a static data 850352290Sobrien member. */ 850452290Sobrien && !TYPE_P (CP_DECL_CONTEXT (t)) 850552290Sobrien /* Any other non-local variable must be at namespace scope. */ 850690287Sobrien && !DECL_NAMESPACE_SCOPE_P (t)) 850752290Sobrien || (TREE_CODE (t) == PARM_DECL)) 850890287Sobrien return 1; 850952290Sobrien 851090287Sobrien return 0; 851152290Sobrien} 851252290Sobrien 8513117410Skan/* Returns nonzero if T is an automatic local variable or a label. 851490287Sobrien (These are the declarations that need to be remapped when the code 851590287Sobrien containing them is duplicated.) */ 851690287Sobrien 851790287Sobrienint 8518132747Skannonstatic_local_decl_p (tree t) 851990287Sobrien{ 852090287Sobrien return ((local_variable_p (t) && !TREE_STATIC (t)) 852190287Sobrien || TREE_CODE (t) == LABEL_DECL 852290287Sobrien || TREE_CODE (t) == RESULT_DECL); 852390287Sobrien} 852490287Sobrien 852590287Sobrien/* Like local_variable_p, but suitable for use as a tree-walking 852690287Sobrien function. */ 852790287Sobrien 852890287Sobrienstatic tree 8529132747Skanlocal_variable_p_walkfn (tree* tp, 8530132747Skan int* walk_subtrees ATTRIBUTE_UNUSED , 8531132747Skan void* data ATTRIBUTE_UNUSED ) 853290287Sobrien{ 853390287Sobrien return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) 853490287Sobrien ? *tp : NULL_TREE); 853590287Sobrien} 853690287Sobrien 853752290Sobrien/* Check that ARG, which is a default-argument expression for a 8538117410Skan parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if 853952290Sobrien something goes wrong. DECL may also be a _TYPE node, rather than a 854052290Sobrien DECL, if there is no DECL available. */ 854152290Sobrien 854252290Sobrientree 8543132747Skancheck_default_argument (tree decl, tree arg) 854452290Sobrien{ 854552290Sobrien tree var; 854652290Sobrien tree decl_type; 854752290Sobrien 854852290Sobrien if (TREE_CODE (arg) == DEFAULT_ARG) 854952290Sobrien /* We get a DEFAULT_ARG when looking at an in-class declaration 855052290Sobrien with a default argument. Ignore the argument for now; we'll 855152290Sobrien deal with it after the class is complete. */ 855252290Sobrien return arg; 855352290Sobrien 855452290Sobrien if (processing_template_decl || uses_template_parms (arg)) 855552290Sobrien /* We don't do anything checking until instantiation-time. Note 855652290Sobrien that there may be uninstantiated arguments even for an 855752290Sobrien instantiated function, since default arguments are not 855852290Sobrien instantiated until they are needed. */ 855952290Sobrien return arg; 856052290Sobrien 856152290Sobrien if (TYPE_P (decl)) 856252290Sobrien { 856352290Sobrien decl_type = decl; 856452290Sobrien decl = NULL_TREE; 856552290Sobrien } 856652290Sobrien else 856752290Sobrien decl_type = TREE_TYPE (decl); 856852290Sobrien 856990287Sobrien if (arg == error_mark_node 857052290Sobrien || decl == error_mark_node 857152290Sobrien || TREE_TYPE (arg) == error_mark_node 857252290Sobrien || decl_type == error_mark_node) 857352290Sobrien /* Something already went wrong. There's no need to check 857452290Sobrien further. */ 857552290Sobrien return error_mark_node; 857652290Sobrien 857752290Sobrien /* [dcl.fct.default] 857890287Sobrien 857952290Sobrien A default argument expression is implicitly converted to the 858052290Sobrien parameter type. */ 858152290Sobrien if (!TREE_TYPE (arg) 858252290Sobrien || !can_convert_arg (decl_type, TREE_TYPE (arg), arg)) 858352290Sobrien { 858452290Sobrien if (decl) 858590287Sobrien error ("default argument for `%#D' has type `%T'", 858652290Sobrien decl, TREE_TYPE (arg)); 858752290Sobrien else 858890287Sobrien error ("default argument for parameter of type `%T' has type `%T'", 858952290Sobrien decl_type, TREE_TYPE (arg)); 859052290Sobrien 859152290Sobrien return error_mark_node; 859252290Sobrien } 859352290Sobrien 859452290Sobrien /* [dcl.fct.default] 859552290Sobrien 859652290Sobrien Local variables shall not be used in default argument 859790287Sobrien expressions. 859852290Sobrien 859952290Sobrien The keyword `this' shall not be used in a default argument of a 860052290Sobrien member function. */ 860190287Sobrien var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn, 860290287Sobrien NULL); 860352290Sobrien if (var) 860452290Sobrien { 860590287Sobrien error ("default argument `%E' uses local variable `%D'", 860652290Sobrien arg, var); 860752290Sobrien return error_mark_node; 860852290Sobrien } 860952290Sobrien 861052290Sobrien /* All is well. */ 861152290Sobrien return arg; 861252290Sobrien} 861352290Sobrien 861418334Speter/* Decode the list of parameter types for a function type. 861518334Speter Given the list of things declared inside the parens, 861618334Speter return a list of types. 861718334Speter 861890287Sobrien We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P 861990287Sobrien flag. If unset, we append void_list_node. A parmlist declared 862090287Sobrien as `(void)' is accepted as the empty parmlist. 862118334Speter 8622132747Skan *PARMS is set to the chain of PARM_DECLs created. */ 862318334Speter 862418334Speterstatic tree 8625132747Skangrokparms (tree first_parm, tree *parms) 862618334Speter{ 862718334Speter tree result = NULL_TREE; 862818334Speter tree decls = NULL_TREE; 862990287Sobrien int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm); 863090287Sobrien tree parm, chain; 863190287Sobrien int any_error = 0; 863218334Speter 863390287Sobrien my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115); 863490287Sobrien 863590287Sobrien for (parm = first_parm; parm != NULL_TREE; parm = chain) 863618334Speter { 863790287Sobrien tree type = NULL_TREE; 863890287Sobrien tree decl = TREE_VALUE (parm); 863990287Sobrien tree init = TREE_PURPOSE (parm); 864090287Sobrien tree specs, attrs; 864118334Speter 864290287Sobrien chain = TREE_CHAIN (parm); 864390287Sobrien /* @@ weak defense against parse errors. */ 864490287Sobrien if (TREE_CODE (decl) != VOID_TYPE 864590287Sobrien && TREE_CODE (decl) != TREE_LIST) 864618334Speter { 864790287Sobrien /* Give various messages as the need arises. */ 864890287Sobrien if (TREE_CODE (decl) == STRING_CST) 864990287Sobrien error ("invalid string constant `%E'", decl); 865090287Sobrien else if (TREE_CODE (decl) == INTEGER_CST) 865190287Sobrien error ("invalid integer constant in parameter list, did you forget to give parameter name?"); 865290287Sobrien continue; 865390287Sobrien } 865418334Speter 865590287Sobrien if (parm == void_list_node) 865690287Sobrien break; 865718334Speter 865890287Sobrien split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs); 865990287Sobrien decl = grokdeclarator (TREE_VALUE (decl), specs, 866090287Sobrien PARM, init != NULL_TREE, &attrs); 866190287Sobrien if (! decl || TREE_TYPE (decl) == error_mark_node) 866290287Sobrien continue; 866318334Speter 866490287Sobrien if (attrs) 866590287Sobrien cplus_decl_attributes (&decl, attrs, 0); 866652290Sobrien 866790287Sobrien type = TREE_TYPE (decl); 866890287Sobrien if (VOID_TYPE_P (type)) 866990287Sobrien { 867090287Sobrien if (same_type_p (type, void_type_node) 867190287Sobrien && !DECL_NAME (decl) && !result && !chain && !ellipsis) 867290287Sobrien /* this is a parmlist of `(void)', which is ok. */ 867390287Sobrien break; 8674117410Skan cxx_incomplete_type_error (decl, type); 867590287Sobrien /* It's not a good idea to actually create parameters of 867690287Sobrien type `void'; other parts of the compiler assume that a 867790287Sobrien void type terminates the parameter list. */ 867890287Sobrien type = error_mark_node; 867990287Sobrien TREE_TYPE (decl) = error_mark_node; 868090287Sobrien } 868152290Sobrien 868290287Sobrien if (type != error_mark_node) 868390287Sobrien { 868490287Sobrien /* Top-level qualifiers on the parameters are 868590287Sobrien ignored for function types. */ 8686132747Skan type = cp_build_qualified_type (type, 0); 868790287Sobrien if (TREE_CODE (type) == METHOD_TYPE) 868890287Sobrien { 868990287Sobrien error ("parameter `%D' invalidly declared method type", decl); 869090287Sobrien type = build_pointer_type (type); 869190287Sobrien TREE_TYPE (decl) = type; 869290287Sobrien } 869390287Sobrien else if (abstract_virtuals_error (decl, type)) 8694117410Skan any_error = 1; /* Seems like a good idea. */ 869590287Sobrien else if (POINTER_TYPE_P (type)) 869690287Sobrien { 869790287Sobrien /* [dcl.fct]/6, parameter types cannot contain pointers 869890287Sobrien (references) to arrays of unknown bound. */ 869990287Sobrien tree t = TREE_TYPE (type); 870090287Sobrien int ptr = TYPE_PTR_P (type); 870118334Speter 870290287Sobrien while (1) 870390287Sobrien { 870490287Sobrien if (TYPE_PTR_P (t)) 870590287Sobrien ptr = 1; 870690287Sobrien else if (TREE_CODE (t) != ARRAY_TYPE) 870790287Sobrien break; 870890287Sobrien else if (!TYPE_DOMAIN (t)) 870990287Sobrien break; 871090287Sobrien t = TREE_TYPE (t); 871190287Sobrien } 871290287Sobrien if (TREE_CODE (t) == ARRAY_TYPE) 871390287Sobrien error ("parameter `%D' includes %s to array of unknown bound `%T'", 871490287Sobrien decl, ptr ? "pointer" : "reference", t); 871590287Sobrien } 871618334Speter 871790287Sobrien if (!any_error && init) 871890287Sobrien init = check_default_argument (decl, init); 871990287Sobrien else 872090287Sobrien init = NULL_TREE; 872190287Sobrien } 872218334Speter 872390287Sobrien TREE_CHAIN (decl) = decls; 872490287Sobrien decls = decl; 872590287Sobrien result = tree_cons (init, type, result); 872618334Speter } 872790287Sobrien decls = nreverse (decls); 872890287Sobrien result = nreverse (result); 872990287Sobrien if (!ellipsis) 873090287Sobrien result = chainon (result, void_list_node); 8731132747Skan *parms = decls; 873218334Speter 873318334Speter return result; 873418334Speter} 873551412Sobrien 873618334Speter 873790287Sobrien/* D is a constructor or overloaded `operator='. 873860970Sobrien 873990287Sobrien Let T be the class in which D is declared. Then, this function 874090287Sobrien returns: 874190287Sobrien 874290287Sobrien -1 if D's is an ill-formed constructor or copy assignment operator 874390287Sobrien whose first parameter is of type `T'. 874490287Sobrien 0 if D is not a copy constructor or copy assignment 874590287Sobrien operator. 874690287Sobrien 1 if D is a copy constructor or copy assignment operator whose 874790287Sobrien first parameter is a reference to const qualified T. 874890287Sobrien 2 if D is a copy constructor or copy assignment operator whose 874990287Sobrien first parameter is a reference to non-const qualified T. 875090287Sobrien 875190287Sobrien This function can be used as a predicate. Positive values indicate 8752117410Skan a copy constructor and nonzero values indicate a copy assignment 875390287Sobrien operator. */ 875490287Sobrien 875551412Sobrienint 8756132747Skancopy_fn_p (tree d) 875751412Sobrien{ 875890287Sobrien tree args; 875990287Sobrien tree arg_type; 876090287Sobrien int result = 1; 876160970Sobrien 876290287Sobrien my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208); 876351412Sobrien 876490287Sobrien if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d))) 876590287Sobrien /* Instantiations of template member functions are never copy 876690287Sobrien functions. Note that member functions of templated classes are 876790287Sobrien represented as template functions internally, and we must 876890287Sobrien accept those as copy functions. */ 876990287Sobrien return 0; 877090287Sobrien 877190287Sobrien args = FUNCTION_FIRST_USER_PARMTYPE (d); 877290287Sobrien if (!args) 877390287Sobrien return 0; 877451412Sobrien 877590287Sobrien arg_type = TREE_VALUE (args); 877618334Speter 877790287Sobrien if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) 877818334Speter { 877990287Sobrien /* Pass by value copy assignment operator. */ 878090287Sobrien result = -1; 878118334Speter } 878290287Sobrien else if (TREE_CODE (arg_type) == REFERENCE_TYPE 878390287Sobrien && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) 878460970Sobrien { 878590287Sobrien if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) 878690287Sobrien result = 2; 878760970Sobrien } 878890287Sobrien else 878990287Sobrien return 0; 879090287Sobrien 879190287Sobrien args = TREE_CHAIN (args); 879260970Sobrien 879390287Sobrien if (args && args != void_list_node && !TREE_PURPOSE (args)) 879490287Sobrien /* There are more non-optional args. */ 879590287Sobrien return 0; 879670639Sobrien 879790287Sobrien return result; 879890287Sobrien} 879952290Sobrien 880090287Sobrien/* Remember any special properties of member function DECL. */ 880190287Sobrien 8802132747Skanvoid grok_special_member_properties (tree decl) 880390287Sobrien{ 880490287Sobrien if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl)) 880590287Sobrien ; /* Not special. */ 880690287Sobrien else if (DECL_CONSTRUCTOR_P (decl)) 880718334Speter { 880890287Sobrien int ctor = copy_fn_p (decl); 880990287Sobrien 881090287Sobrien if (ctor > 0) 881190287Sobrien { 881290287Sobrien /* [class.copy] 881390287Sobrien 881490287Sobrien A non-template constructor for class X is a copy 881590287Sobrien constructor if its first parameter is of type X&, const 881690287Sobrien X&, volatile X& or const volatile X&, and either there 881790287Sobrien are no other parameters or else all other parameters have 881890287Sobrien default arguments. */ 881990287Sobrien TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1; 882090287Sobrien if (ctor > 1) 882190287Sobrien TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1; 882290287Sobrien } 882390287Sobrien else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) 882490287Sobrien TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1; 882518334Speter } 882690287Sobrien else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR) 882790287Sobrien { 882890287Sobrien /* [class.copy] 882990287Sobrien 883090287Sobrien A non-template assignment operator for class X is a copy 883190287Sobrien assignment operator if its parameter is of type X, X&, const 883290287Sobrien X&, volatile X& or const volatile X&. */ 883390287Sobrien 883490287Sobrien int assop = copy_fn_p (decl); 883590287Sobrien 883690287Sobrien if (assop) 883790287Sobrien { 883890287Sobrien TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; 883990287Sobrien if (assop != 1) 884090287Sobrien TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; 884190287Sobrien if (DECL_PURE_VIRTUAL_P (decl)) 884290287Sobrien TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; 884390287Sobrien } 884490287Sobrien } 884590287Sobrien} 884652290Sobrien 884790287Sobrien/* Check a constructor DECL has the correct form. Complains 884890287Sobrien if the class has a constructor of the form X(X). */ 884952290Sobrien 885090287Sobrienint 8851132747Skangrok_ctor_properties (tree ctype, tree decl) 885290287Sobrien{ 885390287Sobrien int ctor_parm = copy_fn_p (decl); 885490287Sobrien 885590287Sobrien if (ctor_parm < 0) 885618334Speter { 885790287Sobrien /* [class.copy] 885890287Sobrien 885990287Sobrien A declaration of a constructor for a class X is ill-formed if 886090287Sobrien its first parameter is of type (optionally cv-qualified) X 886190287Sobrien and either there are no other parameters or else all other 886290287Sobrien parameters have default arguments. 886390287Sobrien 886490287Sobrien We *don't* complain about member template instantiations that 886590287Sobrien have this form, though; they can occur as we try to decide 886690287Sobrien what constructor to use during overload resolution. Since 886790287Sobrien overload resolution will never prefer such a constructor to 886890287Sobrien the non-template copy constructor (which is either explicitly 886990287Sobrien or implicitly defined), there's no need to worry about their 887090287Sobrien existence. Theoretically, they should never even be 887190287Sobrien instantiated, but that's hard to forestall. */ 887290287Sobrien error ("invalid constructor; you probably meant `%T (const %T&)'", 887351412Sobrien ctype, ctype); 887451412Sobrien SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype); 887551412Sobrien return 0; 887618334Speter } 887790287Sobrien 887818334Speter return 1; 887918334Speter} 888018334Speter 888190287Sobrien/* An operator with this code is unary, but can also be binary. */ 888251412Sobrien 888318334Speterstatic int 8884132747Skanambi_op_p (enum tree_code code) 888518334Speter{ 888690287Sobrien return (code == INDIRECT_REF 888790287Sobrien || code == ADDR_EXPR 888890287Sobrien || code == CONVERT_EXPR 888990287Sobrien || code == NEGATE_EXPR 889090287Sobrien || code == PREINCREMENT_EXPR 889190287Sobrien || code == PREDECREMENT_EXPR); 889218334Speter} 889318334Speter 889418334Speter/* An operator with this name can only be unary. */ 889551412Sobrien 889618334Speterstatic int 8897132747Skanunary_op_p (enum tree_code code) 889818334Speter{ 889990287Sobrien return (code == TRUTH_NOT_EXPR 890090287Sobrien || code == BIT_NOT_EXPR 890190287Sobrien || code == COMPONENT_REF 890290287Sobrien || code == TYPE_EXPR); 890318334Speter} 890418334Speter 8905132747Skan/* DECL is a declaration for an overloaded operator. Returns true if 8906132747Skan the declaration is valid; false otherwise. If COMPLAIN is true, 8907132747Skan errors are issued for invalid declarations. */ 890851412Sobrien 8909132747Skanbool 8910132747Skangrok_op_properties (tree decl, int friendp, bool complain) 891118334Speter{ 891218334Speter tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); 891390287Sobrien tree argtype; 891418334Speter int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); 891518334Speter tree name = DECL_NAME (decl); 891690287Sobrien enum tree_code operator_code; 891790287Sobrien int arity; 8918132747Skan bool ok; 891918334Speter 8920132747Skan /* Assume that the declaration is valid. */ 8921132747Skan ok = true; 8922132747Skan 892390287Sobrien /* Count the number of arguments. */ 892490287Sobrien for (argtype = argtypes, arity = 0; 892590287Sobrien argtype && argtype != void_list_node; 892690287Sobrien argtype = TREE_CHAIN (argtype)) 892790287Sobrien ++arity; 892890287Sobrien 892918334Speter if (current_class_type == NULL_TREE) 893018334Speter friendp = 1; 893118334Speter 893290287Sobrien if (DECL_CONV_FN_P (decl)) 893390287Sobrien operator_code = TYPE_EXPR; 893490287Sobrien else 893590287Sobrien do 893690287Sobrien { 893790287Sobrien#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ 893890287Sobrien if (ansi_opname (CODE) == name) \ 893990287Sobrien { \ 894090287Sobrien operator_code = (CODE); \ 894190287Sobrien break; \ 894290287Sobrien } \ 894390287Sobrien else if (ansi_assopname (CODE) == name) \ 894490287Sobrien { \ 894590287Sobrien operator_code = (CODE); \ 894690287Sobrien DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \ 894790287Sobrien break; \ 894890287Sobrien } 894990287Sobrien 895090287Sobrien#include "operators.def" 895190287Sobrien#undef DEF_OPERATOR 895290287Sobrien 895390287Sobrien abort (); 895490287Sobrien } 895590287Sobrien while (0); 895690287Sobrien my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526); 895790287Sobrien SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); 895890287Sobrien 895918334Speter if (! friendp) 896018334Speter { 896190287Sobrien switch (operator_code) 896290287Sobrien { 896390287Sobrien case NEW_EXPR: 896490287Sobrien TYPE_HAS_NEW_OPERATOR (current_class_type) = 1; 896590287Sobrien break; 896690287Sobrien 896790287Sobrien case DELETE_EXPR: 896890287Sobrien TYPE_GETS_DELETE (current_class_type) |= 1; 896990287Sobrien break; 897090287Sobrien 897190287Sobrien case VEC_NEW_EXPR: 897290287Sobrien TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1; 897390287Sobrien break; 897490287Sobrien 897590287Sobrien case VEC_DELETE_EXPR: 897690287Sobrien TYPE_GETS_DELETE (current_class_type) |= 2; 897790287Sobrien break; 897890287Sobrien 897990287Sobrien default: 898090287Sobrien break; 898190287Sobrien } 898218334Speter } 898318334Speter 898490287Sobrien if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR) 8985132747Skan TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); 898690287Sobrien else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) 8987132747Skan TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); 898818334Speter else 898918334Speter { 899018334Speter /* An operator function must either be a non-static member function 899118334Speter or have at least one parameter of a class, a reference to a class, 899218334Speter an enumeration, or a reference to an enumeration. 13.4.0.6 */ 899318334Speter if (! methodp || DECL_STATIC_FUNCTION_P (decl)) 899418334Speter { 899590287Sobrien if (operator_code == TYPE_EXPR 899690287Sobrien || operator_code == CALL_EXPR 899790287Sobrien || operator_code == COMPONENT_REF 899890287Sobrien || operator_code == ARRAY_REF 899990287Sobrien || operator_code == NOP_EXPR) 900090287Sobrien error ("`%D' must be a nonstatic member function", decl); 900118334Speter else 900218334Speter { 9003132747Skan tree p; 900418334Speter 900518334Speter if (DECL_STATIC_FUNCTION_P (decl)) 900690287Sobrien error ("`%D' must be either a non-static member function or a non-member function", decl); 900718334Speter 9008132747Skan for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p)) 9009132747Skan { 9010132747Skan tree arg = non_reference (TREE_VALUE (p)); 9011132747Skan /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used 9012132747Skan because these checks are performed even on 9013132747Skan template functions. */ 9014132747Skan if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE) 9015132747Skan break; 9016132747Skan } 901718334Speter 9018132747Skan if (!p || p == void_list_node) 9019132747Skan { 9020132747Skan if (!complain) 9021132747Skan return false; 9022132747Skan 9023132747Skan error ("`%D' must have an argument of class or " 9024132747Skan "enumerated type", 9025132747Skan decl); 9026132747Skan ok = false; 9027132747Skan } 902818334Speter } 902918334Speter } 903090287Sobrien 9031132747Skan /* There are no restrictions on the arguments to an overloaded 9032132747Skan "operator ()". */ 903390287Sobrien if (operator_code == CALL_EXPR) 9034132747Skan return ok; 903518334Speter 903651412Sobrien if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl)) 903718334Speter { 903818334Speter tree t = TREE_TYPE (name); 903990287Sobrien if (! friendp) 904018334Speter { 904118334Speter int ref = (TREE_CODE (t) == REFERENCE_TYPE); 904252290Sobrien const char *what = 0; 904390287Sobrien 904418334Speter if (ref) 904518334Speter t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); 904618334Speter 904790287Sobrien if (TREE_CODE (t) == VOID_TYPE) 904890287Sobrien what = "void"; 904990287Sobrien else if (t == current_class_type) 905018334Speter what = "the same type"; 905151412Sobrien /* Don't force t to be complete here. */ 905218334Speter else if (IS_AGGR_TYPE (t) 905390287Sobrien && COMPLETE_TYPE_P (t) 905418334Speter && DERIVED_FROM_P (t, current_class_type)) 905518334Speter what = "a base class"; 905618334Speter 9057117410Skan if (what && warn_conversion) 905818334Speter warning ("conversion to %s%s will never use a type conversion operator", 905918334Speter ref ? "a reference to " : "", what); 906018334Speter } 906118334Speter } 906290287Sobrien if (operator_code == COND_EXPR) 906318334Speter { 906490287Sobrien /* 13.4.0.3 */ 906590287Sobrien error ("ISO C++ prohibits overloading operator ?:"); 906618334Speter } 906790287Sobrien else if (ambi_op_p (operator_code)) 906818334Speter { 906990287Sobrien if (arity == 1) 907090287Sobrien /* We pick the one-argument operator codes by default, so 907190287Sobrien we don't have to change anything. */ 907290287Sobrien ; 907390287Sobrien else if (arity == 2) 907418334Speter { 907590287Sobrien /* If we thought this was a unary operator, we now know 907690287Sobrien it to be a binary operator. */ 907790287Sobrien switch (operator_code) 907890287Sobrien { 907990287Sobrien case INDIRECT_REF: 908090287Sobrien operator_code = MULT_EXPR; 908190287Sobrien break; 908290287Sobrien 908390287Sobrien case ADDR_EXPR: 908490287Sobrien operator_code = BIT_AND_EXPR; 908590287Sobrien break; 908690287Sobrien 908790287Sobrien case CONVERT_EXPR: 908890287Sobrien operator_code = PLUS_EXPR; 908990287Sobrien break; 909090287Sobrien 909190287Sobrien case NEGATE_EXPR: 909290287Sobrien operator_code = MINUS_EXPR; 909390287Sobrien break; 909490287Sobrien 909590287Sobrien case PREINCREMENT_EXPR: 909690287Sobrien operator_code = POSTINCREMENT_EXPR; 909790287Sobrien break; 909890287Sobrien 909990287Sobrien case PREDECREMENT_EXPR: 910090287Sobrien operator_code = POSTDECREMENT_EXPR; 910190287Sobrien break; 910290287Sobrien 910390287Sobrien default: 910490287Sobrien abort (); 910590287Sobrien } 910690287Sobrien 910790287Sobrien SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); 910890287Sobrien 910990287Sobrien if ((operator_code == POSTINCREMENT_EXPR 911090287Sobrien || operator_code == POSTDECREMENT_EXPR) 911151412Sobrien && ! processing_template_decl 911252290Sobrien && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node)) 911318334Speter { 911418334Speter if (methodp) 911590287Sobrien error ("postfix `%D' must take `int' as its argument", 911618334Speter decl); 911718334Speter else 911890287Sobrien error 911918334Speter ("postfix `%D' must take `int' as its second argument", 912018334Speter decl); 912118334Speter } 912218334Speter } 912318334Speter else 912418334Speter { 912518334Speter if (methodp) 912690287Sobrien error ("`%D' must take either zero or one argument", decl); 912718334Speter else 912890287Sobrien error ("`%D' must take either one or two arguments", decl); 912918334Speter } 913051412Sobrien 913151412Sobrien /* More Effective C++ rule 6. */ 913251412Sobrien if (warn_ecpp 913390287Sobrien && (operator_code == POSTINCREMENT_EXPR 913490287Sobrien || operator_code == POSTDECREMENT_EXPR 913590287Sobrien || operator_code == PREINCREMENT_EXPR 913690287Sobrien || operator_code == PREDECREMENT_EXPR)) 913751412Sobrien { 913851412Sobrien tree arg = TREE_VALUE (argtypes); 913951412Sobrien tree ret = TREE_TYPE (TREE_TYPE (decl)); 914051412Sobrien if (methodp || TREE_CODE (arg) == REFERENCE_TYPE) 914151412Sobrien arg = TREE_TYPE (arg); 914251412Sobrien arg = TYPE_MAIN_VARIANT (arg); 914390287Sobrien if (operator_code == PREINCREMENT_EXPR 914490287Sobrien || operator_code == PREDECREMENT_EXPR) 914551412Sobrien { 914651412Sobrien if (TREE_CODE (ret) != REFERENCE_TYPE 914752290Sobrien || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), 914852290Sobrien arg)) 914990287Sobrien warning ("prefix `%D' should return `%T'", decl, 915051412Sobrien build_reference_type (arg)); 915151412Sobrien } 915251412Sobrien else 915351412Sobrien { 915452290Sobrien if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) 915590287Sobrien warning ("postfix `%D' should return `%T'", decl, arg); 915651412Sobrien } 915751412Sobrien } 915818334Speter } 915990287Sobrien else if (unary_op_p (operator_code)) 916018334Speter { 916190287Sobrien if (arity != 1) 916218334Speter { 916318334Speter if (methodp) 916490287Sobrien error ("`%D' must take `void'", decl); 916518334Speter else 916690287Sobrien error ("`%D' must take exactly one argument", decl); 916718334Speter } 916818334Speter } 916990287Sobrien else /* if (binary_op_p (operator_code)) */ 917018334Speter { 917190287Sobrien if (arity != 2) 917218334Speter { 917318334Speter if (methodp) 917490287Sobrien error ("`%D' must take exactly one argument", decl); 917518334Speter else 917690287Sobrien error ("`%D' must take exactly two arguments", decl); 917718334Speter } 917851412Sobrien 917951412Sobrien /* More Effective C++ rule 7. */ 918051412Sobrien if (warn_ecpp 918190287Sobrien && (operator_code == TRUTH_ANDIF_EXPR 918290287Sobrien || operator_code == TRUTH_ORIF_EXPR 918390287Sobrien || operator_code == COMPOUND_EXPR)) 918490287Sobrien warning ("user-defined `%D' always evaluates both arguments", 918551412Sobrien decl); 918618334Speter } 918718334Speter 918851412Sobrien /* Effective C++ rule 23. */ 918951412Sobrien if (warn_ecpp 919090287Sobrien && arity == 2 919196292Sobrien && !DECL_ASSIGNMENT_OPERATOR_P (decl) 919290287Sobrien && (operator_code == PLUS_EXPR 919390287Sobrien || operator_code == MINUS_EXPR 919490287Sobrien || operator_code == TRUNC_DIV_EXPR 919596292Sobrien || operator_code == MULT_EXPR 919696292Sobrien || operator_code == TRUNC_MOD_EXPR) 919751412Sobrien && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE) 919890287Sobrien warning ("`%D' should return by value", decl); 919951412Sobrien 920090287Sobrien /* [over.oper]/8 */ 920190287Sobrien for (; argtypes && argtypes != void_list_node; 920290287Sobrien argtypes = TREE_CHAIN (argtypes)) 920390287Sobrien if (TREE_PURPOSE (argtypes)) 920490287Sobrien { 920590287Sobrien TREE_PURPOSE (argtypes) = NULL_TREE; 920690287Sobrien if (operator_code == POSTINCREMENT_EXPR 920790287Sobrien || operator_code == POSTDECREMENT_EXPR) 920890287Sobrien { 920990287Sobrien if (pedantic) 921090287Sobrien pedwarn ("`%D' cannot have default arguments", decl); 921190287Sobrien } 921290287Sobrien else 921390287Sobrien error ("`%D' cannot have default arguments", decl); 921490287Sobrien } 921590287Sobrien 921618334Speter } 9217132747Skan 9218132747Skan return ok; 921918334Speter} 922018334Speter 922152290Sobrienstatic const char * 9222132747Skantag_name (enum tag_types code) 922352290Sobrien{ 922452290Sobrien switch (code) 922552290Sobrien { 922652290Sobrien case record_type: 922752290Sobrien return "struct"; 922852290Sobrien case class_type: 922952290Sobrien return "class"; 923052290Sobrien case union_type: 923152290Sobrien return "union "; 923252290Sobrien case enum_type: 923352290Sobrien return "enum"; 923452290Sobrien default: 923590287Sobrien abort (); 923652290Sobrien } 923752290Sobrien} 923852290Sobrien 9239117410Skan/* Name lookup in an elaborated-type-specifier (after the keyword 9240132747Skan indicated by TAG_CODE) has found the TYPE_DECL DECL. If the 9241117410Skan elaborated-type-specifier is invalid, issue a diagnostic and return 9242132747Skan error_mark_node; otherwise, return the *_TYPE to which it referred. 9243132747Skan If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */ 9244117410Skan 9245132747Skantree 9246117410Skancheck_elaborated_type_specifier (enum tag_types tag_code, 9247132747Skan tree decl, 9248132747Skan bool allow_template_p) 9249117410Skan{ 9250132747Skan tree type; 9251117410Skan 9252132747Skan /* In the case of: 9253117410Skan 9254132747Skan struct S { struct S *p; }; 9255132747Skan 9256132747Skan name lookup will find the TYPE_DECL for the implicit "S::S" 9257132747Skan typedef. Adjust for that here. */ 9258132747Skan if (DECL_SELF_REFERENCE_P (decl)) 9259132747Skan decl = TYPE_NAME (TREE_TYPE (decl)); 9260132747Skan 9261132747Skan type = TREE_TYPE (decl); 9262132747Skan 9263132747Skan /* [dcl.type.elab] 9264132747Skan 9265132747Skan If the identifier resolves to a typedef-name or a template 9266132747Skan type-parameter, the elaborated-type-specifier is ill-formed. 9267132747Skan 9268132747Skan In other words, the only legitimate declaration to use in the 9269132747Skan elaborated type specifier is the implicit typedef created when 9270132747Skan the type is declared. */ 9271132747Skan if (!DECL_IMPLICIT_TYPEDEF_P (decl)) 9272117410Skan { 9273132747Skan error ("using typedef-name `%D' after `%s'", decl, tag_name (tag_code)); 9274132747Skan return IS_AGGR_TYPE (type) ? type : error_mark_node; 9275117410Skan } 9276132747Skan 9277132747Skan if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 9278117410Skan { 9279117410Skan error ("using template type parameter `%T' after `%s'", 9280117410Skan type, tag_name (tag_code)); 9281132747Skan return error_mark_node; 9282117410Skan } 9283132747Skan else if (TREE_CODE (type) != RECORD_TYPE 9284132747Skan && TREE_CODE (type) != UNION_TYPE 9285132747Skan && tag_code != enum_type) 9286132747Skan { 9287132747Skan error ("`%T' referred to as `%s'", type, tag_name (tag_code)); 9288132747Skan return error_mark_node; 9289132747Skan } 9290132747Skan else if (TREE_CODE (type) != ENUMERAL_TYPE 9291132747Skan && tag_code == enum_type) 9292132747Skan { 9293132747Skan error ("`%T' referred to as enum", type); 9294132747Skan return error_mark_node; 9295132747Skan } 9296132747Skan else if (!allow_template_p 9297132747Skan && TREE_CODE (type) == RECORD_TYPE 9298132747Skan && CLASSTYPE_IS_TEMPLATE (type)) 9299132747Skan { 9300132747Skan /* If a class template appears as elaborated type specifier 9301132747Skan without a template header such as: 9302117410Skan 9303132747Skan template <class T> class C {}; 9304132747Skan void f(class C); // No template header here 9305132747Skan 9306132747Skan then the required template argument is missing. */ 9307132747Skan 9308132747Skan error ("template argument required for `%s %T'", 9309132747Skan tag_name (tag_code), 9310132747Skan DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))); 9311132747Skan return error_mark_node; 9312132747Skan } 9313132747Skan 9314132747Skan return type; 9315117410Skan} 9316117410Skan 9317132747Skan/* Get the struct, enum or union (TAG_CODE says which) with tag NAME. 931818334Speter Define the tag as a forward-reference if it is not defined. 931918334Speter 9320132747Skan If a declaration is given, process it here, and report an error if 9321132747Skan multiple declarations are not identical. 932218334Speter 9323132747Skan GLOBALIZE is false when this is also a definition. Only look in 932418334Speter the current frame for the name (since C++ allows new names in any 9325132747Skan scope.) 932618334Speter 9327132747Skan TEMPLATE_HEADER_P is true when this declaration is preceded by 9328132747Skan a set of template parameters. */ 9329132747Skan 933018334Spetertree 9331132747Skanxref_tag (enum tag_types tag_code, tree name, 9332132747Skan bool globalize, bool template_header_p) 933318334Speter{ 933418334Speter enum tree_code code; 9335132747Skan tree t; 9336117410Skan struct cp_binding_level *b = current_binding_level; 933752290Sobrien tree context = NULL_TREE; 933818334Speter 9339117410Skan timevar_push (TV_NAME_LOOKUP); 934051412Sobrien 9341132747Skan my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0); 9342132747Skan 934318334Speter switch (tag_code) 934418334Speter { 934518334Speter case record_type: 934618334Speter case class_type: 934718334Speter code = RECORD_TYPE; 934818334Speter break; 934918334Speter case union_type: 935018334Speter code = UNION_TYPE; 935118334Speter break; 935218334Speter case enum_type: 935318334Speter code = ENUMERAL_TYPE; 935418334Speter break; 935518334Speter default: 935690287Sobrien abort (); 935718334Speter } 935818334Speter 935918334Speter if (! globalize) 936018334Speter { 936152290Sobrien /* If we know we are defining this tag, only look it up in 936252290Sobrien this scope and don't try to find it as a type. */ 9363132747Skan t = lookup_tag (code, name, b, 1); 936418334Speter } 936518334Speter else 936618334Speter { 9367132747Skan tree decl = lookup_name (name, 2); 936890287Sobrien 9369132747Skan if (decl && DECL_CLASS_TEMPLATE_P (decl)) 9370132747Skan decl = DECL_TEMPLATE_RESULT (decl); 9371132747Skan 9372132747Skan if (decl && TREE_CODE (decl) == TYPE_DECL) 937318334Speter { 9374132747Skan /* Two cases we need to consider when deciding if a class 9375132747Skan template is allowed as an elaborated type specifier: 9376132747Skan 1. It is a self reference to its own class. 9377132747Skan 2. It comes with a template header. 937851412Sobrien 9379132747Skan For example: 938051412Sobrien 9381132747Skan template <class T> class C { 9382132747Skan class C *c1; // DECL_SELF_REFERENCE_P is true 9383132747Skan class D; 9384132747Skan }; 9385132747Skan template <class U> class C; // template_header_p is true 9386132747Skan template <class T> class C<T>::D { 9387132747Skan class C *c2; // DECL_SELF_REFERENCE_P is true 9388132747Skan }; */ 9389132747Skan 9390132747Skan t = check_elaborated_type_specifier (tag_code, 9391132747Skan decl, 9392132747Skan template_header_p 9393132747Skan | DECL_SELF_REFERENCE_P (decl)); 9394132747Skan if (t == error_mark_node) 9395132747Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 939618334Speter } 9397132747Skan else 9398132747Skan t = NULL_TREE; 939952290Sobrien 9400132747Skan if (t && current_class_type 940190287Sobrien && template_class_depth (current_class_type) 9402132747Skan && template_header_p) 940352290Sobrien { 9404117410Skan /* Since GLOBALIZE is nonzero, we are not looking at a 940552290Sobrien definition of this tag. Since, in addition, we are currently 940652290Sobrien processing a (member) template declaration of a template 940752290Sobrien class, we must be very careful; consider: 940852290Sobrien 940952290Sobrien template <class X> 941052290Sobrien struct S1 941152290Sobrien 941252290Sobrien template <class U> 941352290Sobrien struct S2 941452290Sobrien { template <class V> 941552290Sobrien friend struct S1; }; 941652290Sobrien 941752290Sobrien Here, the S2::S1 declaration should not be confused with the 941852290Sobrien outer declaration. In particular, the inner version should 941952290Sobrien have a template parameter of level 2, not level 1. This 942052290Sobrien would be particularly important if the member declaration 942152290Sobrien were instead: 942252290Sobrien 942352290Sobrien template <class V = U> friend struct S1; 942452290Sobrien 942552290Sobrien say, when we should tsubst into `U' when instantiating 942652290Sobrien S2. On the other hand, when presented with: 942752290Sobrien 942852290Sobrien template <class T> 942952290Sobrien struct S1 { 943052290Sobrien template <class U> 943152290Sobrien struct S2 {}; 943252290Sobrien template <class U> 943352290Sobrien friend struct S2; 943452290Sobrien }; 943552290Sobrien 943652290Sobrien we must find the inner binding eventually. We 943752290Sobrien accomplish this by making sure that the new type we 943852290Sobrien create to represent this declaration has the right 943952290Sobrien TYPE_CONTEXT. */ 9440132747Skan context = TYPE_CONTEXT (t); 9441132747Skan t = NULL_TREE; 944252290Sobrien } 944318334Speter } 944418334Speter 9445132747Skan if (! t) 944618334Speter { 944718334Speter /* If no such tag is yet defined, create a forward-reference node 944818334Speter and record it as the "definition". 944918334Speter When a real declaration of this type is found, 945018334Speter the forward-reference will be altered into a real type. */ 945118334Speter if (code == ENUMERAL_TYPE) 945218334Speter { 945390287Sobrien error ("use of enum `%#D' without previous declaration", name); 9454132747Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 945518334Speter } 945618334Speter else 945718334Speter { 9458132747Skan t = make_aggr_type (code); 9459132747Skan TYPE_CONTEXT (t) = context; 9460132747Skan pushtag (name, t, globalize); 946118334Speter } 946218334Speter } 946318334Speter else 946418334Speter { 9465132747Skan if (!globalize && processing_template_decl && IS_AGGR_TYPE (t)) 9466132747Skan redeclare_class_template (t, current_template_parms); 9467132747Skan else if (!processing_template_decl 9468132747Skan && CLASS_TYPE_P (t) 9469132747Skan && CLASSTYPE_IS_TEMPLATE (t)) 9470132747Skan { 9471132747Skan error ("redeclaration of `%T' as a non-template", t); 9472132747Skan t = error_mark_node; 9473132747Skan } 947418334Speter } 947518334Speter 9476132747Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 947718334Speter} 947818334Speter 947951412Sobrientree 9480132747Skanxref_tag_from_type (tree old, tree id, int globalize) 948151412Sobrien{ 9482117410Skan enum tag_types tag_kind; 948351412Sobrien 948451412Sobrien if (TREE_CODE (old) == RECORD_TYPE) 9485117410Skan tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type); 948651412Sobrien else 9487117410Skan tag_kind = union_type; 948851412Sobrien 948951412Sobrien if (id == NULL_TREE) 949051412Sobrien id = TYPE_IDENTIFIER (old); 949151412Sobrien 9492132747Skan return xref_tag (tag_kind, id, globalize, false); 949351412Sobrien} 949451412Sobrien 949552290Sobrien/* REF is a type (named NAME), for which we have just seen some 9496132747Skan baseclasses. BASE_LIST is a list of those baseclasses; the 949752290Sobrien TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of 9498132747Skan the base-class. TREE_VIA_VIRTUAL indicates virtual 9499132747Skan inheritance. CODE_TYPE_NODE indicates whether REF is a class, 950052290Sobrien struct, or union. */ 950152290Sobrien 950218334Spetervoid 9503132747Skanxref_basetypes (tree ref, tree base_list) 950418334Speter{ 950518334Speter /* In the declaration `A : X, Y, ... Z' we mark all the types 950618334Speter (A, X, Y, ..., Z) so we can check for duplicates. */ 9507132747Skan tree *basep; 950852290Sobrien 9509132747Skan int i; 951090287Sobrien enum tag_types tag_code; 951118334Speter 9512132747Skan if (ref == error_mark_node) 9513132747Skan return; 9514132747Skan 9515117410Skan if (TREE_CODE (ref) == UNION_TYPE) 951618334Speter { 951790287Sobrien error ("derived union `%T' invalid", ref); 951818334Speter return; 951918334Speter } 952018334Speter 9521117410Skan tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type); 9522117410Skan 952352290Sobrien /* First, make sure that any templates in base-classes are 952452290Sobrien instantiated. This ensures that if we call ourselves recursively 952552290Sobrien we do not get confused about which classes are marked and which 952652290Sobrien are not. */ 9527132747Skan basep = &base_list; 9528132747Skan while (*basep) 9529132747Skan { 9530132747Skan tree basetype = TREE_VALUE (*basep); 9531132747Skan if (!(processing_template_decl && uses_template_parms (basetype)) 9532132747Skan && !complete_type_or_else (basetype, NULL)) 9533132747Skan /* An incomplete type. Remove it from the list. */ 9534132747Skan *basep = TREE_CHAIN (*basep); 9535132747Skan else 9536132747Skan basep = &TREE_CHAIN (*basep); 9537132747Skan } 953852290Sobrien 953918334Speter SET_CLASSTYPE_MARKED (ref); 9540132747Skan i = list_length (base_list); 9541132747Skan if (i) 954218334Speter { 9543132747Skan tree binfo = TYPE_BINFO (ref); 9544132747Skan tree binfos = make_tree_vec (i); 9545132747Skan tree accesses = make_tree_vec (i); 9546132747Skan 9547132747Skan BINFO_BASETYPES (binfo) = binfos; 9548132747Skan BINFO_BASEACCESSES (binfo) = accesses; 9549132747Skan 9550132747Skan for (i = 0; base_list; base_list = TREE_CHAIN (base_list)) 955118334Speter { 9552132747Skan tree access = TREE_PURPOSE (base_list); 9553132747Skan int via_virtual = TREE_VIA_VIRTUAL (base_list); 9554132747Skan tree basetype = TREE_VALUE (base_list); 9555132747Skan tree base_binfo; 9556132747Skan 9557132747Skan if (access == access_default_node) 9558132747Skan /* The base of a derived struct is public by default. */ 9559132747Skan access = (tag_code == class_type 9560132747Skan ? access_private_node : access_public_node); 9561132747Skan 9562132747Skan if (basetype && TREE_CODE (basetype) == TYPE_DECL) 9563132747Skan basetype = TREE_TYPE (basetype); 9564132747Skan if (!basetype 9565132747Skan || (TREE_CODE (basetype) != RECORD_TYPE 9566132747Skan && TREE_CODE (basetype) != TYPENAME_TYPE 9567132747Skan && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM 9568132747Skan && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)) 9569132747Skan { 9570132747Skan error ("base type `%T' fails to be a struct or class type", 9571132747Skan basetype); 9572132747Skan continue; 9573132747Skan } 9574132747Skan 957518334Speter if (CLASSTYPE_MARKED (basetype)) 957618334Speter { 957718334Speter if (basetype == ref) 957890287Sobrien error ("recursive type `%T' undefined", basetype); 957918334Speter else 958090287Sobrien error ("duplicate base type `%T' invalid", basetype); 958118334Speter continue; 958218334Speter } 9583132747Skan 958451412Sobrien if (TYPE_FOR_JAVA (basetype) 958590287Sobrien && (current_lang_depth () == 0)) 958651412Sobrien TYPE_FOR_JAVA (ref) = 1; 9587132747Skan 9588132747Skan if (CLASS_TYPE_P (basetype)) 9589132747Skan { 9590132747Skan base_binfo = TYPE_BINFO (basetype); 9591132747Skan /* This flag will be in the binfo of the base type, we must 9592132747Skan clear it after copying the base binfos. */ 9593132747Skan BINFO_DEPENDENT_BASE_P (base_binfo) 9594132747Skan = dependent_type_p (basetype); 9595132747Skan } 9596132747Skan else 9597132747Skan base_binfo = make_binfo (size_zero_node, basetype, 9598132747Skan NULL_TREE, NULL_TREE); 9599132747Skan 960018334Speter TREE_VEC_ELT (binfos, i) = base_binfo; 9601132747Skan TREE_VEC_ELT (accesses, i) = access; 9602132747Skan /* This flag will be in the binfo of the base type, we must 9603132747Skan clear it after copying the base binfos. */ 960418334Speter TREE_VIA_VIRTUAL (base_binfo) = via_virtual; 9605132747Skan 960618334Speter SET_CLASSTYPE_MARKED (basetype); 9607132747Skan 960818334Speter /* We are free to modify these bits because they are meaningless 960918334Speter at top level, and BASETYPE is a top-level type. */ 961018334Speter if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype)) 961118334Speter { 961218334Speter TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1; 961390287Sobrien /* Converting to a virtual base class requires looking 9614132747Skan up the offset of the virtual base. */ 961590287Sobrien TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; 961618334Speter } 9617132747Skan 961852290Sobrien if (CLASS_TYPE_P (basetype)) 961952290Sobrien { 962090287Sobrien TYPE_HAS_NEW_OPERATOR (ref) 962190287Sobrien |= TYPE_HAS_NEW_OPERATOR (basetype); 962290287Sobrien TYPE_HAS_ARRAY_NEW_OPERATOR (ref) 962390287Sobrien |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); 962452290Sobrien TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); 962590287Sobrien /* If the base-class uses multiple inheritance, so do we. */ 962690287Sobrien TYPE_USES_MULTIPLE_INHERITANCE (ref) 962790287Sobrien |= TYPE_USES_MULTIPLE_INHERITANCE (basetype); 962890287Sobrien /* Likewise, if converting to a base of the base may require 9629132747Skan code, then we may need to generate code to convert to a 9630132747Skan base as well. */ 963190287Sobrien TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) 963290287Sobrien |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype); 963352290Sobrien } 9634132747Skan i++; 963518334Speter } 9636132747Skan if (i) 9637132747Skan TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i; 9638132747Skan else 9639132747Skan BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE; 9640132747Skan 9641132747Skan if (i > 1) 9642132747Skan { 9643132747Skan TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1; 9644132747Skan /* If there is more than one non-empty they cannot be at the same 9645132747Skan address. */ 9646132747Skan TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; 9647132747Skan } 964818334Speter } 9649132747Skan 9650132747Skan /* Copy the base binfos, collect the virtual bases and set the 9651132747Skan inheritance order chain. */ 9652132747Skan copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE); 9653132747Skan CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref)); 965418334Speter 9655132747Skan if (TYPE_FOR_JAVA (ref)) 965652290Sobrien { 9657132747Skan if (TYPE_USES_MULTIPLE_INHERITANCE (ref)) 9658132747Skan error ("Java class '%T' cannot have multiple bases", ref); 9659132747Skan if (CLASSTYPE_VBASECLASSES (ref)) 9660132747Skan error ("Java class '%T' cannot have virtual bases", ref); 966152290Sobrien } 966252290Sobrien 966318334Speter /* Unmark all the types. */ 9664132747Skan while (i--) 9665132747Skan { 9666132747Skan tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i)); 9667132747Skan 9668132747Skan CLEAR_CLASSTYPE_MARKED (basetype); 9669132747Skan if (CLASS_TYPE_P (basetype)) 9670132747Skan { 9671132747Skan TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0; 9672132747Skan BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0; 9673132747Skan } 9674132747Skan } 967518334Speter CLEAR_CLASSTYPE_MARKED (ref); 967690287Sobrien} 967752290Sobrien 967818334Speter 967918334Speter/* Begin compiling the definition of an enumeration type. 968018334Speter NAME is its name (or null if anonymous). 968118334Speter Returns the type object, as yet incomplete. 968218334Speter Also records info about it so that build_enumerator 968318334Speter may be used to declare the individual values as they are read. */ 968418334Speter 968518334Spetertree 9686132747Skanstart_enum (tree name) 968718334Speter{ 9688132747Skan tree enumtype = NULL_TREE; 9689117410Skan struct cp_binding_level *b = current_binding_level; 969018334Speter 969118334Speter /* If this is the real definition for a previous forward reference, 969218334Speter fill in the contents in the same object that used to be the 969318334Speter forward reference. */ 969418334Speter 969518334Speter if (name != NULL_TREE) 969618334Speter enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1); 969718334Speter 969818334Speter if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE) 969990287Sobrien { 970090287Sobrien error ("multiple definition of `%#T'", enumtype); 9701132747Skan error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype)); 970290287Sobrien /* Clear out TYPE_VALUES, and start again. */ 970390287Sobrien TYPE_VALUES (enumtype) = NULL_TREE; 970490287Sobrien } 970518334Speter else 970618334Speter { 970718334Speter enumtype = make_node (ENUMERAL_TYPE); 970818334Speter pushtag (name, enumtype, 0); 970918334Speter } 971018334Speter 971118334Speter return enumtype; 971218334Speter} 971318334Speter 971418334Speter/* After processing and defining all the values of an enumeration type, 971518334Speter install their decls in the enumeration type and finish it off. 971690287Sobrien ENUMTYPE is the type object and VALUES a list of name-value pairs. */ 971718334Speter 971890287Sobrienvoid 9719132747Skanfinish_enum (tree enumtype) 972018334Speter{ 9721132747Skan tree values; 9722132747Skan tree decl; 9723132747Skan tree value; 972490287Sobrien tree minnode; 972590287Sobrien tree maxnode; 972690287Sobrien tree t; 972790287Sobrien bool unsignedp; 972890287Sobrien int lowprec; 972990287Sobrien int highprec; 973090287Sobrien int precision; 9731132747Skan integer_type_kind itk; 9732132747Skan tree underlying_type = NULL_TREE; 973318334Speter 973490287Sobrien /* We built up the VALUES in reverse order. */ 973590287Sobrien TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); 973690287Sobrien 9737117410Skan /* For an enum defined in a template, just set the type of the values; 9738117410Skan all further processing is postponed until the template is 9739117410Skan instantiated. We need to set the type so that tsubst of a CONST_DECL 9740117410Skan works. */ 974190287Sobrien if (processing_template_decl) 974218334Speter { 9743132747Skan for (values = TYPE_VALUES (enumtype); 9744132747Skan values; 9745132747Skan values = TREE_CHAIN (values)) 9746132747Skan TREE_TYPE (TREE_VALUE (values)) = enumtype; 9747117410Skan if (at_function_scope_p ()) 974890287Sobrien add_stmt (build_min (TAG_DEFN, enumtype)); 974990287Sobrien return; 975090287Sobrien } 975152290Sobrien 9752132747Skan /* Determine the minimum and maximum values of the enumerators. */ 975390287Sobrien if (TYPE_VALUES (enumtype)) 975490287Sobrien { 975590287Sobrien minnode = maxnode = NULL_TREE; 975652290Sobrien 9757132747Skan for (values = TYPE_VALUES (enumtype); 9758132747Skan values; 9759132747Skan values = TREE_CHAIN (values)) 976090287Sobrien { 9761132747Skan decl = TREE_VALUE (values); 976252290Sobrien 9763117410Skan /* [dcl.enum]: Following the closing brace of an enum-specifier, 9764117410Skan each enumerator has the type of its enumeration. Prior to the 9765117410Skan closing brace, the type of each enumerator is the type of its 9766117410Skan initializing value. */ 9767117410Skan TREE_TYPE (decl) = enumtype; 976852290Sobrien 9769132747Skan /* Update the minimum and maximum values, if appropriate. */ 9770132747Skan value = DECL_INITIAL (decl); 9771117410Skan /* Figure out what the minimum and maximum values of the 9772117410Skan enumerators are. */ 977390287Sobrien if (!minnode) 977490287Sobrien minnode = maxnode = value; 977590287Sobrien else if (tree_int_cst_lt (maxnode, value)) 977690287Sobrien maxnode = value; 977790287Sobrien else if (tree_int_cst_lt (value, minnode)) 977890287Sobrien minnode = value; 9779117410Skan 9780117410Skan /* Set the TREE_TYPE for the values as well. That's so that when 9781117410Skan we call decl_constant_value we get an entity of the right type 9782117410Skan (but with the constant value). But first make a copy so we 9783117410Skan don't clobber shared INTEGER_CSTs. */ 9784117410Skan if (TREE_TYPE (value) != enumtype) 9785117410Skan { 9786117410Skan value = DECL_INITIAL (decl) = copy_node (value); 9787117410Skan TREE_TYPE (value) = enumtype; 9788117410Skan } 978918334Speter } 979018334Speter } 979118334Speter else 9792132747Skan /* [dcl.enum] 9793132747Skan 9794132747Skan If the enumerator-list is empty, the underlying type is as if 9795132747Skan the enumeration had a single enumerator with value 0. */ 979690287Sobrien minnode = maxnode = integer_zero_node; 979718334Speter 979890287Sobrien /* Compute the number of bits require to represent all values of the 979990287Sobrien enumeration. We must do this before the type of MINNODE and 980090287Sobrien MAXNODE are transformed, since min_precision relies on the 980190287Sobrien TREE_TYPE of the value it is passed. */ 980290287Sobrien unsignedp = tree_int_cst_sgn (minnode) >= 0; 980390287Sobrien lowprec = min_precision (minnode, unsignedp); 980490287Sobrien highprec = min_precision (maxnode, unsignedp); 980590287Sobrien precision = MAX (lowprec, highprec); 980618334Speter 9807132747Skan /* Determine the underlying type of the enumeration. 9808132747Skan 9809132747Skan [dcl.enum] 9810132747Skan 9811132747Skan The underlying type of an enumeration is an integral type that 9812132747Skan can represent all the enumerator values defined in the 9813132747Skan enumeration. It is implementation-defined which integral type is 9814132747Skan used as the underlying type for an enumeration except that the 9815132747Skan underlying type shall not be larger than int unless the value of 9816132747Skan an enumerator cannot fit in an int or unsigned int. 9817132747Skan 9818132747Skan We use "int" or an "unsigned int" as the underlying type, even if 9819132747Skan a smaller integral type would work, unless the user has 9820132747Skan explicitly requested that we use the smallest possible type. */ 9821132747Skan for (itk = (flag_short_enums ? itk_char : itk_int); 9822132747Skan itk != itk_none; 9823132747Skan itk++) 982451412Sobrien { 9825132747Skan underlying_type = integer_types[itk]; 9826132747Skan if (TYPE_PRECISION (underlying_type) >= precision 9827132747Skan && TREE_UNSIGNED (underlying_type) == unsignedp) 9828132747Skan break; 9829132747Skan } 9830132747Skan if (itk == itk_none) 9831132747Skan { 9832132747Skan /* DR 377 9833132747Skan 9834132747Skan IF no integral type can represent all the enumerator values, the 9835132747Skan enumeration is ill-formed. */ 9836117410Skan error ("no integral type can represent all of the enumerator values " 9837117410Skan "for `%T'", enumtype); 9838117410Skan precision = TYPE_PRECISION (long_long_integer_type_node); 9839132747Skan underlying_type = integer_types[itk_unsigned_long_long]; 984051412Sobrien } 984190287Sobrien 9842132747Skan /* Compute the minium and maximum values for the type. 9843132747Skan 9844132747Skan [dcl.enum] 9845132747Skan 9846132747Skan For an enumeration where emin is the smallest enumerator and emax 9847132747Skan is the largest, the values of the enumeration are the values of the 9848132747Skan underlying type in the range bmin to bmax, where bmin and bmax are, 9849132747Skan respectively, the smallest and largest values of the smallest bit- 9850132747Skan field that can store emin and emax. */ 9851117410Skan TYPE_PRECISION (enumtype) = precision; 9852132747Skan set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp); 985351412Sobrien 9854132747Skan /* [dcl.enum] 9855132747Skan 9856132747Skan The value of sizeof() applied to an enumeration type, an object 9857132747Skan of an enumeration type, or an enumerator, is the value of sizeof() 9858132747Skan applied to the underlying type. */ 9859132747Skan TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type); 9860132747Skan TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type); 9861132747Skan TYPE_MODE (enumtype) = TYPE_MODE (underlying_type); 9862132747Skan TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type); 9863132747Skan TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type); 9864132747Skan TREE_UNSIGNED (enumtype) = TREE_UNSIGNED (underlying_type); 986518334Speter 9866132747Skan /* Convert each of the enumerators to the type of the underlying 9867132747Skan type of the enumeration. */ 9868132747Skan for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) 9869132747Skan { 9870132747Skan decl = TREE_VALUE (values); 9871132747Skan value = perform_implicit_conversion (underlying_type, 9872132747Skan DECL_INITIAL (decl)); 9873132747Skan TREE_TYPE (value) = enumtype; 9874132747Skan DECL_INITIAL (decl) = value; 9875132747Skan TREE_VALUE (values) = value; 9876132747Skan } 9877119270Skan 987890287Sobrien /* Fix up all variant types of this enum type. */ 987990287Sobrien for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) 988090287Sobrien { 988190287Sobrien TYPE_VALUES (t) = TYPE_VALUES (enumtype); 988290287Sobrien TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype); 988390287Sobrien TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype); 988490287Sobrien TYPE_SIZE (t) = TYPE_SIZE (enumtype); 988590287Sobrien TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype); 988690287Sobrien TYPE_MODE (t) = TYPE_MODE (enumtype); 988790287Sobrien TYPE_PRECISION (t) = TYPE_PRECISION (enumtype); 988890287Sobrien TYPE_ALIGN (t) = TYPE_ALIGN (enumtype); 988990287Sobrien TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype); 989090287Sobrien TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype); 989152290Sobrien } 989218334Speter 989390287Sobrien /* Finish debugging output for this type. */ 989490287Sobrien rest_of_type_compilation (enumtype, namespace_bindings_p ()); 989518334Speter} 989618334Speter 989752290Sobrien/* Build and install a CONST_DECL for an enumeration constant of the 989890287Sobrien enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. 989918334Speter Assignment of sequential values by default is handled here. */ 990018334Speter 990190287Sobrienvoid 9902132747Skanbuild_enumerator (tree name, tree value, tree enumtype) 990318334Speter{ 990490287Sobrien tree decl; 990552290Sobrien tree context; 990690287Sobrien tree type; 990718334Speter 990818334Speter /* Remove no-op casts from the value. */ 990918334Speter if (value) 991018334Speter STRIP_TYPE_NOPS (value); 991118334Speter 991290287Sobrien if (! processing_template_decl) 991390287Sobrien { 991490287Sobrien /* Validate and default VALUE. */ 991590287Sobrien if (value != NULL_TREE) 991690287Sobrien { 991790287Sobrien value = decl_constant_value (value); 991818334Speter 991990287Sobrien if (TREE_CODE (value) == INTEGER_CST) 992090287Sobrien { 9921132747Skan value = perform_integral_promotions (value); 992290287Sobrien constant_expression_warning (value); 992390287Sobrien } 992490287Sobrien else 992590287Sobrien { 992690287Sobrien error ("enumerator value for `%D' not integer constant", name); 992790287Sobrien value = NULL_TREE; 992890287Sobrien } 992990287Sobrien } 993018334Speter 993190287Sobrien /* Default based on previous value. */ 9932132747Skan if (value == NULL_TREE) 993390287Sobrien { 993490287Sobrien tree prev_value; 993518334Speter 993690287Sobrien if (TYPE_VALUES (enumtype)) 993790287Sobrien { 9938117410Skan /* The next value is the previous value ... */ 993990287Sobrien prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); 994090287Sobrien /* ... plus one. */ 994190287Sobrien value = cp_build_binary_op (PLUS_EXPR, 994290287Sobrien prev_value, 994390287Sobrien integer_one_node); 994490287Sobrien 994590287Sobrien if (tree_int_cst_lt (value, prev_value)) 994690287Sobrien error ("overflow in enumeration values at `%D'", name); 994790287Sobrien } 994890287Sobrien else 994990287Sobrien value = integer_zero_node; 995090287Sobrien } 995190287Sobrien 995290287Sobrien /* Remove no-op casts from the value. */ 9953117410Skan STRIP_TYPE_NOPS (value); 995490287Sobrien } 995518334Speter 995618334Speter /* C++ associates enums with global, function, or class declarations. */ 995790287Sobrien context = current_scope (); 9958132747Skan if (!context) 9959132747Skan context = current_namespace; 996090287Sobrien 996190287Sobrien /* Build the actual enumeration constant. Note that the enumeration 996290287Sobrien constants have the type of their initializers until the 996390287Sobrien enumeration is complete: 996490287Sobrien 996590287Sobrien [ dcl.enum ] 996690287Sobrien 996790287Sobrien Following the closing brace of an enum-specifier, each enumer- 996890287Sobrien ator has the type of its enumeration. Prior to the closing 996990287Sobrien brace, the type of each enumerator is the type of its 997090287Sobrien initializing value. 997190287Sobrien 997290287Sobrien In finish_enum we will reset the type. Of course, if we're 9973117410Skan processing a template, there may be no value. */ 997490287Sobrien type = value ? TREE_TYPE (value) : NULL_TREE; 997590287Sobrien 997690287Sobrien if (context && context == current_class_type) 997790287Sobrien /* This enum declaration is local to the class. We need the full 997890287Sobrien lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */ 997990287Sobrien decl = build_lang_decl (CONST_DECL, name, type); 998090287Sobrien else 998190287Sobrien /* It's a global enum, or it's local to a function. (Note local to 998252290Sobrien a function could mean local to a class method. */ 998390287Sobrien decl = build_decl (CONST_DECL, name, type); 998418334Speter 998590287Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (context); 9986132747Skan TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1; 998790287Sobrien DECL_INITIAL (decl) = value; 998818334Speter 998990287Sobrien if (context && context == current_class_type) 999090287Sobrien /* In something like `struct S { enum E { i = 7 }; };' we put `i' 9991132747Skan on the TYPE_FIELDS list for `S'. (That's so that you can say 9992132747Skan things like `S::i' later.) */ 999390287Sobrien finish_member_declaration (decl); 999490287Sobrien else 999596292Sobrien pushdecl (decl); 999618334Speter 999790287Sobrien /* Add this enumeration constant to the list for this type. */ 999890287Sobrien TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); 999918334Speter} 1000018334Speter 1000118334Speter 1000290287Sobrien/* We're defining DECL. Make sure that it's type is OK. */ 1000351412Sobrien 1000490287Sobrienstatic void 10005132747Skancheck_function_type (tree decl, tree current_function_parms) 1000690287Sobrien{ 1000790287Sobrien tree fntype = TREE_TYPE (decl); 1000890287Sobrien tree return_type = complete_type (TREE_TYPE (fntype)); 1000990287Sobrien 1001090287Sobrien /* In a function definition, arg types must be complete. */ 1001190287Sobrien require_complete_types_for_parms (current_function_parms); 1001290287Sobrien 1001390287Sobrien if (!COMPLETE_OR_VOID_TYPE_P (return_type)) 1001490287Sobrien { 1001590287Sobrien error ("return type `%#T' is incomplete", TREE_TYPE (fntype)); 1001690287Sobrien 1001790287Sobrien /* Make it return void instead, but don't change the 1001890287Sobrien type of the DECL_RESULT, in case we have a named return value. */ 1001990287Sobrien if (TREE_CODE (fntype) == METHOD_TYPE) 1002090287Sobrien { 1002190287Sobrien tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))); 1002290287Sobrien TREE_TYPE (decl) 10023132747Skan = build_method_type_directly (ctype, 10024132747Skan void_type_node, 10025132747Skan FUNCTION_ARG_CHAIN (decl)); 1002690287Sobrien } 1002790287Sobrien else 1002890287Sobrien TREE_TYPE (decl) 1002990287Sobrien = build_function_type (void_type_node, 1003090287Sobrien TYPE_ARG_TYPES (TREE_TYPE (decl))); 1003190287Sobrien TREE_TYPE (decl) 1003290287Sobrien = build_exception_variant (fntype, 1003390287Sobrien TYPE_RAISES_EXCEPTIONS (fntype)); 1003490287Sobrien } 1003590287Sobrien else 1003690287Sobrien abstract_virtuals_error (decl, TREE_TYPE (fntype)); 1003790287Sobrien} 1003890287Sobrien 1003918334Speter/* Create the FUNCTION_DECL for a function definition. 1004018334Speter DECLSPECS and DECLARATOR are the parts of the declaration; 1004118334Speter they describe the function's name and the type it returns, 1004218334Speter but twisted together in a fashion that parallels the syntax of C. 1004318334Speter 1004490287Sobrien FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the 1004590287Sobrien DECLARATOR is really the DECL for the function we are about to 1004690287Sobrien process and that DECLSPECS should be ignored), SF_INCLASS_INLINE 1004790287Sobrien indicating that the function is an inline defined in-class. 1004890287Sobrien 1004918334Speter This function creates a binding context for the function body 1005018334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 1005118334Speter 1005218334Speter Returns 1 on success. If the DECLARATOR is not suitable for a function 1005318334Speter (it defines a datum instead), we return 0, which tells 1005418334Speter yyparse to report a parse error. 1005518334Speter 1005618334Speter For C++, we must first check whether that datum makes any sense. 1005718334Speter For example, "class A local_a(1,2);" means that variable local_a 1005818334Speter is an aggregate of type A, which should have a constructor 1005990287Sobrien applied to it with the argument list [1, 2]. */ 1006018334Speter 1006118334Speterint 10062132747Skanstart_function (tree declspecs, tree declarator, tree attrs, int flags) 1006318334Speter{ 1006451412Sobrien tree decl1; 1006518334Speter tree ctype = NULL_TREE; 1006618334Speter tree fntype; 1006718334Speter tree restype; 1006818334Speter int doing_friend = 0; 10069117410Skan struct cp_binding_level *bl; 1007090287Sobrien tree current_function_parms; 1007118334Speter 1007218334Speter /* Sanity check. */ 1007351412Sobrien my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160); 1007418334Speter my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161); 1007518334Speter 1007651412Sobrien /* This should only be done once on the top most decl. */ 10077117410Skan if (have_extern_spec) 1007818334Speter { 1007990287Sobrien declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs); 10080117410Skan have_extern_spec = false; 1008118334Speter } 1008218334Speter 1008390287Sobrien if (flags & SF_PRE_PARSED) 1008418334Speter { 1008518334Speter decl1 = declarator; 1008618334Speter 1008718334Speter fntype = TREE_TYPE (decl1); 1008818334Speter if (TREE_CODE (fntype) == METHOD_TYPE) 1008918334Speter ctype = TYPE_METHOD_BASETYPE (fntype); 1009018334Speter 1009190287Sobrien /* ISO C++ 11.4/5. A friend function defined in a class is in 1009290287Sobrien the (lexical) scope of the class in which it is defined. */ 1009318334Speter if (!ctype && DECL_FRIEND_P (decl1)) 1009418334Speter { 1009590287Sobrien ctype = DECL_FRIEND_CONTEXT (decl1); 1009618334Speter 1009718334Speter /* CTYPE could be null here if we're dealing with a template; 1009818334Speter for example, `inline friend float foo()' inside a template 1009918334Speter will have no CTYPE set. */ 1010018334Speter if (ctype && TREE_CODE (ctype) != RECORD_TYPE) 1010118334Speter ctype = NULL_TREE; 1010218334Speter else 1010318334Speter doing_friend = 1; 1010418334Speter } 1010518334Speter } 1010618334Speter else 1010718334Speter { 10108122192Skan decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs); 1010918334Speter /* If the declarator is not suitable for a function definition, 1011018334Speter cause a syntax error. */ 1011196292Sobrien if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) 1011296292Sobrien return 0; 1011318334Speter 1011496292Sobrien cplus_decl_attributes (&decl1, attrs, 0); 1011596292Sobrien 1011696292Sobrien /* If #pragma weak was used, mark the decl weak now. */ 10117117410Skan if (global_scope_p (current_binding_level)) 1011896292Sobrien maybe_apply_pragma_weak (decl1); 1011996292Sobrien 1012018334Speter fntype = TREE_TYPE (decl1); 1012118334Speter 1012218334Speter restype = TREE_TYPE (fntype); 1012318334Speter 1012418334Speter if (TREE_CODE (fntype) == METHOD_TYPE) 1012518334Speter ctype = TYPE_METHOD_BASETYPE (fntype); 1012651412Sobrien else if (DECL_MAIN_P (decl1)) 1012718334Speter { 10128132747Skan /* If this doesn't return integer_type, or a typedef to 10129132747Skan integer_type, complain. */ 10130132747Skan if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node)) 1013118334Speter { 1013218334Speter if (pedantic || warn_return_type) 1013351412Sobrien pedwarn ("return type for `main' changed to `int'"); 1013418334Speter TREE_TYPE (decl1) = fntype = default_function_type; 1013518334Speter } 1013618334Speter } 1013718334Speter } 1013818334Speter 1013990287Sobrien if (DECL_DECLARED_INLINE_P (decl1) 1014090287Sobrien && lookup_attribute ("noinline", attrs)) 10141132747Skan warning ("%Jinline function '%D' given attribute noinline", decl1, decl1); 1014290287Sobrien 1014390287Sobrien if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) 1014490287Sobrien /* This is a constructor, we must ensure that any default args 1014590287Sobrien introduced by this definition are propagated to the clones 1014690287Sobrien now. The clones are used directly in overload resolution. */ 1014790287Sobrien adjust_clone_args (decl1); 1014890287Sobrien 1014990287Sobrien /* Sometimes we don't notice that a function is a static member, and 1015090287Sobrien build a METHOD_TYPE for it. Fix that up now. */ 1015190287Sobrien if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) 1015290287Sobrien && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE) 1015390287Sobrien { 1015490287Sobrien revert_static_member_fn (decl1); 1015590287Sobrien ctype = NULL_TREE; 1015690287Sobrien } 1015790287Sobrien 1015818334Speter /* Warn if function was previously implicitly declared 1015918334Speter (but not if we warned then). */ 1016018334Speter if (! warn_implicit 1016118334Speter && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE) 1016218334Speter cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1))); 1016318334Speter 1016452290Sobrien /* Set up current_class_type, and enter the scope of the class, if 1016552290Sobrien appropriate. */ 1016652290Sobrien if (ctype) 10167132747Skan push_nested_class (ctype); 1016852290Sobrien else if (DECL_STATIC_FUNCTION_P (decl1)) 10169132747Skan push_nested_class (DECL_CONTEXT (decl1)); 1017052290Sobrien 1017152290Sobrien /* Now that we have entered the scope of the class, we must restore 1017252290Sobrien the bindings for any template parameters surrounding DECL1, if it 1017352290Sobrien is an inline member template. (Order is important; consider the 1017452290Sobrien case where a template parameter has the same name as a field of 1017552290Sobrien the class.) It is not until after this point that 1017652290Sobrien PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ 1017790287Sobrien if (flags & SF_INCLASS_INLINE) 1017852290Sobrien maybe_begin_member_template_processing (decl1); 1017952290Sobrien 1018090287Sobrien /* Effective C++ rule 15. */ 1018190287Sobrien if (warn_ecpp 1018290287Sobrien && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR 1018390287Sobrien && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE) 1018490287Sobrien warning ("`operator=' should return a reference to `*this'"); 1018590287Sobrien 1018690287Sobrien /* Make the init_value nonzero so pushdecl knows this is not tentative. 1018790287Sobrien error_mark_node is replaced below (in poplevel) with the BLOCK. */ 1018890287Sobrien if (!DECL_INITIAL (decl1)) 1018990287Sobrien DECL_INITIAL (decl1) = error_mark_node; 1019090287Sobrien 1019190287Sobrien /* This function exists in static storage. 1019290287Sobrien (This does not mean `static' in the C sense!) */ 1019390287Sobrien TREE_STATIC (decl1) = 1; 1019490287Sobrien 1019590287Sobrien /* We must call push_template_decl after current_class_type is set 1019690287Sobrien up. (If we are processing inline definitions after exiting a 1019790287Sobrien class scope, current_class_type will be NULL_TREE until set above 1019890287Sobrien by push_nested_class.) */ 1019990287Sobrien if (processing_template_decl) 1020090287Sobrien decl1 = push_template_decl (decl1); 1020190287Sobrien 1020252290Sobrien /* We are now in the scope of the function being defined. */ 1020318334Speter current_function_decl = decl1; 1020452290Sobrien 1020551412Sobrien /* Save the parm names or decls from this function's declarator 1020651412Sobrien where store_parm_decls will find them. */ 10207132747Skan current_function_parms = DECL_ARGUMENTS (decl1); 1020818334Speter 1020990287Sobrien /* Make sure the parameter and return types are reasonable. When 1021090287Sobrien you declare a function, these types can be incomplete, but they 1021190287Sobrien must be complete when you define the function. */ 1021251412Sobrien if (! processing_template_decl) 1021390287Sobrien check_function_type (decl1, current_function_parms); 1021490287Sobrien 1021590287Sobrien /* Build the return declaration for the function. */ 1021690287Sobrien restype = TREE_TYPE (fntype); 1021796292Sobrien /* Promote the value to int before returning it. */ 1021896292Sobrien if (c_promoting_integer_type_p (restype)) 1021996292Sobrien restype = type_promotes_to (restype); 1022096292Sobrien if (DECL_RESULT (decl1) == NULL_TREE) 1022118334Speter { 1022296292Sobrien DECL_RESULT (decl1) 1022396292Sobrien = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); 1022496292Sobrien c_apply_type_quals_to_decl (cp_type_quals (restype), 1022596292Sobrien DECL_RESULT (decl1)); 1022618334Speter } 1022718334Speter 1022890287Sobrien /* Initialize RTL machinery. We cannot do this until 1022990287Sobrien CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this 1023090287Sobrien even when processing a template; this is how we get 1023190287Sobrien CFUN set up, and our per-function variables initialized. 1023290287Sobrien FIXME factor out the non-RTL stuff. */ 1023390287Sobrien bl = current_binding_level; 10234132747Skan allocate_struct_function (decl1); 1023590287Sobrien current_binding_level = bl; 1023651412Sobrien 1023790287Sobrien /* Even though we're inside a function body, we still don't want to 1023890287Sobrien call expand_expr to calculate the size of a variable-sized array. 1023990287Sobrien We haven't necessarily assigned RTL to all variables yet, so it's 1024090287Sobrien not safe to try to expand expressions involving them. */ 1024190287Sobrien immediate_size_expand = 0; 1024290287Sobrien cfun->x_dont_save_pending_sizes_p = 1; 1024318334Speter 1024490287Sobrien /* Start the statement-tree, start the tree now. */ 1024590287Sobrien begin_stmt_tree (&DECL_SAVED_TREE (decl1)); 1024618334Speter 1024790287Sobrien /* Let the user know we're compiling this function. */ 1024890287Sobrien announce_function (decl1); 1024952290Sobrien 1025018334Speter /* Record the decl so that the function name is defined. 1025118334Speter If we already have a decl for this name, and it is a FUNCTION_DECL, 1025218334Speter use the old decl. */ 1025390287Sobrien if (!processing_template_decl && !(flags & SF_PRE_PARSED)) 1025418334Speter { 1025551412Sobrien /* A specialization is not used to guide overload resolution. */ 10256132747Skan if (!DECL_FUNCTION_MEMBER_P (decl1) 10257132747Skan && !(DECL_USE_TEMPLATE (decl1) && 10258132747Skan PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1)))) 10259132747Skan { 10260132747Skan tree olddecl = pushdecl (decl1); 10261132747Skan 10262132747Skan if (olddecl == error_mark_node) 10263132747Skan /* If something went wrong when registering the declaration, 10264132747Skan use DECL1; we have to have a FUNCTION_DECL to use when 10265132747Skan parsing the body of the function. */ 10266132747Skan ; 10267132747Skan else 10268132747Skan /* Otherwise, OLDDECL is either a previous declaration of 10269132747Skan the same function or DECL1 itself. */ 10270132747Skan decl1 = olddecl; 10271132747Skan } 1027251412Sobrien else 1027351412Sobrien { 10274117410Skan /* We need to set the DECL_CONTEXT. */ 1027551412Sobrien if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) 1027651412Sobrien DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); 1027751412Sobrien /* And make sure we have enough default args. */ 1027851412Sobrien check_default_args (decl1); 1027951412Sobrien } 1028018334Speter fntype = TREE_TYPE (decl1); 1028118334Speter } 1028218334Speter 1028390287Sobrien /* Reset these in case the call to pushdecl changed them. */ 1028451412Sobrien current_function_decl = decl1; 1028590287Sobrien cfun->decl = decl1; 1028651412Sobrien 1028790287Sobrien /* If we are (erroneously) defining a function that we have already 1028890287Sobrien defined before, wipe out what we knew before. */ 10289117410Skan if (!DECL_PENDING_INLINE_P (decl1)) 10290117410Skan DECL_SAVED_FUNCTION_DATA (decl1) = NULL; 1029190287Sobrien 1029290287Sobrien if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) 1029390287Sobrien { 1029490287Sobrien /* We know that this was set up by `grokclassfn'. We do not 1029590287Sobrien wait until `store_parm_decls', since evil parse errors may 1029690287Sobrien never get us to that point. Here we keep the consistency 1029790287Sobrien between `current_class_type' and `current_class_ptr'. */ 1029890287Sobrien tree t = DECL_ARGUMENTS (decl1); 1029990287Sobrien 1030090287Sobrien my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL, 1030190287Sobrien 162); 1030290287Sobrien my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE, 1030390287Sobrien 19990811); 1030490287Sobrien 1030590287Sobrien cp_function_chain->x_current_class_ref 1030690287Sobrien = build_indirect_ref (t, NULL); 1030790287Sobrien cp_function_chain->x_current_class_ptr = t; 1030890287Sobrien 1030990287Sobrien /* Constructors and destructors need to know whether they're "in 1031090287Sobrien charge" of initializing virtual base classes. */ 1031190287Sobrien t = TREE_CHAIN (t); 1031290287Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) 1031390287Sobrien { 1031490287Sobrien current_in_charge_parm = t; 1031590287Sobrien t = TREE_CHAIN (t); 1031690287Sobrien } 1031790287Sobrien if (DECL_HAS_VTT_PARM_P (decl1)) 1031890287Sobrien { 1031990287Sobrien if (DECL_NAME (t) != vtt_parm_identifier) 1032090287Sobrien abort (); 1032190287Sobrien current_vtt_parm = t; 1032290287Sobrien } 1032390287Sobrien } 1032490287Sobrien 1032518334Speter if (DECL_INTERFACE_KNOWN (decl1)) 1032618334Speter { 1032790287Sobrien tree ctx = decl_function_context (decl1); 1032851412Sobrien 1032918334Speter if (DECL_NOT_REALLY_EXTERN (decl1)) 1033018334Speter DECL_EXTERNAL (decl1) = 0; 1033151412Sobrien 1033290287Sobrien if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx) 1033351412Sobrien && TREE_PUBLIC (ctx)) 1033451412Sobrien /* This is a function in a local class in an extern inline 1033551412Sobrien function. */ 1033651412Sobrien comdat_linkage (decl1); 1033718334Speter } 1033818334Speter /* If this function belongs to an interface, it is public. 1033918334Speter If it belongs to someone else's interface, it is also external. 1034052290Sobrien This only affects inlines and template instantiations. */ 1034151412Sobrien else if (interface_unknown == 0 10342132747Skan && ! DECL_TEMPLATE_INSTANTIATION (decl1)) 1034318334Speter { 1034490287Sobrien if (DECL_DECLARED_INLINE_P (decl1) 1034590287Sobrien || DECL_TEMPLATE_INSTANTIATION (decl1) 1034651412Sobrien || processing_template_decl) 1034752290Sobrien { 1034852290Sobrien DECL_EXTERNAL (decl1) 1034952290Sobrien = (interface_only 1035090287Sobrien || (DECL_DECLARED_INLINE_P (decl1) 1035190287Sobrien && ! flag_implement_inlines 1035252290Sobrien && !DECL_VINDEX (decl1))); 1035352290Sobrien 1035452290Sobrien /* For WIN32 we also want to put these in linkonce sections. */ 1035552290Sobrien maybe_make_one_only (decl1); 1035652290Sobrien } 1035718334Speter else 1035818334Speter DECL_EXTERNAL (decl1) = 0; 1035918334Speter DECL_NOT_REALLY_EXTERN (decl1) = 0; 1036018334Speter DECL_INTERFACE_KNOWN (decl1) = 1; 1036118334Speter } 1036252290Sobrien else if (interface_unknown && interface_only 10363132747Skan && ! DECL_TEMPLATE_INSTANTIATION (decl1)) 1036452290Sobrien { 1036552290Sobrien /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma 1036652290Sobrien interface, we will have interface_only set but not 1036752290Sobrien interface_known. In that case, we don't want to use the normal 1036852290Sobrien heuristics because someone will supply a #pragma implementation 1036952290Sobrien elsewhere, and deducing it here would produce a conflict. */ 1037052290Sobrien comdat_linkage (decl1); 1037152290Sobrien DECL_EXTERNAL (decl1) = 0; 1037252290Sobrien DECL_INTERFACE_KNOWN (decl1) = 1; 1037352290Sobrien DECL_DEFER_OUTPUT (decl1) = 1; 1037452290Sobrien } 1037518334Speter else 1037618334Speter { 1037718334Speter /* This is a definition, not a reference. 1037818334Speter So clear DECL_EXTERNAL. */ 1037918334Speter DECL_EXTERNAL (decl1) = 0; 1038018334Speter 1038190287Sobrien if ((DECL_DECLARED_INLINE_P (decl1) 1038290287Sobrien || DECL_TEMPLATE_INSTANTIATION (decl1)) 1038351412Sobrien && ! DECL_INTERFACE_KNOWN (decl1) 1038451412Sobrien /* Don't try to defer nested functions for now. */ 1038590287Sobrien && ! decl_function_context (decl1)) 1038618334Speter DECL_DEFER_OUTPUT (decl1) = 1; 1038718334Speter else 1038851412Sobrien DECL_INTERFACE_KNOWN (decl1) = 1; 1038918334Speter } 1039018334Speter 10391132747Skan begin_scope (sk_function_parms, decl1); 1039218334Speter 1039351412Sobrien ++function_depth; 1039451412Sobrien 1039590287Sobrien if (DECL_DESTRUCTOR_P (decl1)) 1039618334Speter { 1039718334Speter dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); 1039890287Sobrien DECL_CONTEXT (dtor_label) = current_function_decl; 1039918334Speter } 1040018334Speter 1040190287Sobrien start_fname_decls (); 1040290287Sobrien 1040390287Sobrien store_parm_decls (current_function_parms); 1040490287Sobrien 1040518334Speter return 1; 1040618334Speter} 1040718334Speter 1040818334Speter/* Store the parameter declarations into the current function declaration. 1040918334Speter This is called after parsing the parameter declarations, before 1041018334Speter digesting the body of the function. 1041118334Speter 1041218334Speter Also install to binding contour return value identifier, if any. */ 1041318334Speter 1041490287Sobrienstatic void 10415132747Skanstore_parm_decls (tree current_function_parms) 1041618334Speter{ 10417132747Skan tree fndecl = current_function_decl; 10418132747Skan tree parm; 1041918334Speter 1042018334Speter /* This is a chain of any other decls that came in among the parm 1042118334Speter declarations. If a parm is declared with enum {foo, bar} x; 1042218334Speter then CONST_DECLs for foo and bar are put here. */ 1042318334Speter tree nonparms = NULL_TREE; 1042418334Speter 1042590287Sobrien if (current_function_parms) 1042618334Speter { 1042718334Speter /* This case is when the function was defined with an ANSI prototype. 1042818334Speter The parms already have decls, so we need not do anything here 1042918334Speter except record them as in effect 1043018334Speter and complain if any redundant old-style parm decls were written. */ 1043118334Speter 1043290287Sobrien tree specparms = current_function_parms; 1043390287Sobrien tree next; 1043418334Speter 1043518334Speter /* Must clear this because it might contain TYPE_DECLs declared 1043690287Sobrien at class level. */ 10437132747Skan current_binding_level->names = NULL; 1043851412Sobrien 1043990287Sobrien /* If we're doing semantic analysis, then we'll call pushdecl 1044090287Sobrien for each of these. We must do them in reverse order so that 1044190287Sobrien they end in the correct forward order. */ 1044290287Sobrien specparms = nreverse (specparms); 1044390287Sobrien 1044490287Sobrien for (parm = specparms; parm; parm = next) 1044518334Speter { 1044618334Speter next = TREE_CHAIN (parm); 1044718334Speter if (TREE_CODE (parm) == PARM_DECL) 1044818334Speter { 1044990287Sobrien if (DECL_NAME (parm) == NULL_TREE 1045090287Sobrien || TREE_CODE (parm) != VOID_TYPE) 1045190287Sobrien pushdecl (parm); 1045218334Speter else 1045390287Sobrien error ("parameter `%D' declared void", parm); 1045418334Speter } 1045518334Speter else 1045618334Speter { 1045718334Speter /* If we find an enum constant or a type tag, 1045818334Speter put it aside for the moment. */ 1045918334Speter TREE_CHAIN (parm) = NULL_TREE; 1046018334Speter nonparms = chainon (nonparms, parm); 1046118334Speter } 1046218334Speter } 1046318334Speter 1046490287Sobrien /* Get the decls in their original chain order and record in the 1046590287Sobrien function. This is all and only the PARM_DECLs that were 1046690287Sobrien pushed into scope by the loop above. */ 1046718334Speter DECL_ARGUMENTS (fndecl) = getdecls (); 1046818334Speter } 1046918334Speter else 1047018334Speter DECL_ARGUMENTS (fndecl) = NULL_TREE; 1047118334Speter 1047218334Speter /* Now store the final chain of decls for the arguments 1047318334Speter as the decl-chain of the current lexical scope. 1047418334Speter Put the enumerators in as well, at the front so that 1047518334Speter DECL_ARGUMENTS is not modified. */ 10476132747Skan current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl)); 1047718334Speter 1047890287Sobrien /* Do the starting of the exception specifications, if we have any. */ 1047990287Sobrien if (flag_exceptions && !processing_template_decl 1048090287Sobrien && flag_enforce_eh_specs 1048190287Sobrien && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) 1048290287Sobrien current_eh_spec_block = begin_eh_spec_block (); 1048390287Sobrien} 1048452290Sobrien 1048590287Sobrien 1048690287Sobrien/* We have finished doing semantic analysis on DECL, but have not yet 1048790287Sobrien generated RTL for its body. Save away our current state, so that 1048890287Sobrien when we want to generate RTL later we know what to do. */ 1048918334Speter 1049090287Sobrienstatic void 10491132747Skansave_function_data (tree decl) 1049290287Sobrien{ 10493117410Skan struct language_function *f; 1049451412Sobrien 1049590287Sobrien /* Save the language-specific per-function data so that we can 1049690287Sobrien get it back when we really expand this function. */ 1049790287Sobrien my_friendly_assert (!DECL_PENDING_INLINE_P (decl), 1049890287Sobrien 19990908); 1049951412Sobrien 1050090287Sobrien /* Make a copy. */ 10501132747Skan f = ggc_alloc (sizeof (struct language_function)); 10502117410Skan memcpy (f, cp_function_chain, sizeof (struct language_function)); 1050390287Sobrien DECL_SAVED_FUNCTION_DATA (decl) = f; 1050451412Sobrien 1050590287Sobrien /* Clear out the bits we don't need. */ 1050690287Sobrien f->base.x_stmt_tree.x_last_stmt = NULL_TREE; 1050790287Sobrien f->base.x_stmt_tree.x_last_expr_type = NULL_TREE; 1050890287Sobrien f->x_named_label_uses = NULL; 1050990287Sobrien f->bindings = NULL; 1051090287Sobrien f->x_local_names = NULL; 1051118334Speter 1051290287Sobrien /* If we've already decided that we cannot inline this function, we 1051390287Sobrien must remember that fact when we actually go to expand the 1051490287Sobrien function. */ 1051590287Sobrien if (current_function_cannot_inline) 1051618334Speter { 1051790287Sobrien f->cannot_inline = current_function_cannot_inline; 1051890287Sobrien DECL_INLINE (decl) = 0; 1051951412Sobrien } 1052018334Speter} 1052118334Speter 1052290287Sobrien/* Add a note to mark the beginning of the main body of the constructor. 1052390287Sobrien This is used to set up the data structures for the cleanup regions for 1052490287Sobrien fully-constructed bases and members. */ 1052551412Sobrien 1052690287Sobrienstatic void 10527132747Skanbegin_constructor_body (void) 1052818334Speter{ 1052990287Sobrien} 1053018334Speter 1053190287Sobrien/* Add a note to mark the end of the main body of the constructor. This is 1053290287Sobrien used to end the cleanup regions for fully-constructed bases and 1053390287Sobrien members. */ 1053418334Speter 1053590287Sobrienstatic void 10536132747Skanfinish_constructor_body (void) 1053790287Sobrien{ 1053818334Speter} 1053918334Speter 1054090287Sobrien/* Do all the processing for the beginning of a destructor; set up the 1054190287Sobrien vtable pointers and cleanups for bases and members. */ 1054260970Sobrien 1054360970Sobrienstatic void 10544132747Skanbegin_destructor_body (void) 1054560970Sobrien{ 1054690287Sobrien tree if_stmt; 1054790287Sobrien tree compound_stmt; 1054860970Sobrien 1054990287Sobrien /* If the dtor is empty, and we know there is not any possible 1055090287Sobrien way we could use any vtable entries, before they are possibly 1055190287Sobrien set by a base class dtor, we don't have to setup the vtables, 1055290287Sobrien as we know that any base class dtor will set up any vtables 1055390287Sobrien it needs. We avoid MI, because one base class dtor can do a 1055460970Sobrien virtual dispatch to an overridden function that would need to 1055560970Sobrien have a non-related vtable set up, we cannot avoid setting up 1055690287Sobrien vtables in that case. We could change this to see if there 1055790287Sobrien is just one vtable. 1055860970Sobrien 1055990287Sobrien ??? In the destructor for a class, the vtables are set 1056090287Sobrien appropriately for that class. There will be no non-related 1056190287Sobrien vtables. jason 2001-12-11. */ 1056290287Sobrien if_stmt = begin_if_stmt (); 1056360970Sobrien 1056490287Sobrien /* If it is not safe to avoid setting up the vtables, then 1056590287Sobrien someone will change the condition to be boolean_true_node. 1056690287Sobrien (Actually, for now, we do not have code to set the condition 1056790287Sobrien appropriately, so we just assume that we always need to 1056890287Sobrien initialize the vtables.) */ 1056990287Sobrien finish_if_stmt_cond (boolean_true_node, if_stmt); 1057060970Sobrien 10571132747Skan compound_stmt = begin_compound_stmt (/*has_no_scope=*/false); 1057260970Sobrien 1057390287Sobrien /* Make all virtual function table pointers in non-virtual base 1057490287Sobrien classes point to CURRENT_CLASS_TYPE's virtual function 1057590287Sobrien tables. */ 1057690287Sobrien initialize_vtbl_ptrs (current_class_ptr); 1057760970Sobrien 10578132747Skan finish_compound_stmt (compound_stmt); 1057990287Sobrien finish_then_clause (if_stmt); 1058090287Sobrien finish_if_stmt (); 10581117410Skan 10582117410Skan /* And insert cleanups for our bases and members so that they 10583117410Skan will be properly destroyed if we throw. */ 10584117410Skan push_base_cleanups (); 1058560970Sobrien} 1058660970Sobrien 1058790287Sobrien/* At the end of every destructor we generate code to delete the object if 1058890287Sobrien necessary. Do that now. */ 1058960970Sobrien 1059060970Sobrienstatic void 10591132747Skanfinish_destructor_body (void) 1059260970Sobrien{ 1059390287Sobrien tree exprstmt; 1059460970Sobrien 10595107601Sobrien /* Any return from a destructor will end up here; that way all base 10596107601Sobrien and member cleanups will be run when the function returns. */ 10597107601Sobrien add_stmt (build_stmt (LABEL_STMT, dtor_label)); 10598107601Sobrien 1059990287Sobrien /* In a virtual destructor, we must call delete. */ 1060090287Sobrien if (DECL_VIRTUAL_P (current_function_decl)) 1060160970Sobrien { 1060290287Sobrien tree if_stmt; 10603117410Skan tree virtual_size = cxx_sizeof (current_class_type); 1060460970Sobrien 1060590287Sobrien /* [class.dtor] 1060660970Sobrien 1060790287Sobrien At the point of definition of a virtual destructor (including 1060890287Sobrien an implicit definition), non-placement operator delete shall 1060990287Sobrien be looked up in the scope of the destructor's class and if 1061090287Sobrien found shall be accessible and unambiguous. */ 1061190287Sobrien exprstmt = build_op_delete_call 1061290287Sobrien (DELETE_EXPR, current_class_ptr, virtual_size, 1061390287Sobrien LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE); 1061460970Sobrien 1061590287Sobrien if_stmt = begin_if_stmt (); 1061690287Sobrien finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node, 1061790287Sobrien current_in_charge_parm, 1061890287Sobrien integer_one_node), 1061990287Sobrien if_stmt); 1062090287Sobrien finish_expr_stmt (exprstmt); 1062190287Sobrien finish_then_clause (if_stmt); 1062290287Sobrien finish_if_stmt (); 1062360970Sobrien } 1062490287Sobrien} 1062560970Sobrien 1062690287Sobrien/* Do the necessary processing for the beginning of a function body, which 1062790287Sobrien in this case includes member-initializers, but not the catch clauses of 1062890287Sobrien a function-try-block. Currently, this means opening a binding level 1062990287Sobrien for the member-initializers (in a ctor) and member cleanups (in a dtor). 1063090287Sobrien In other functions, this isn't necessary, but it doesn't hurt. */ 1063160970Sobrien 1063290287Sobrientree 10633132747Skanbegin_function_body (void) 1063490287Sobrien{ 1063590287Sobrien tree stmt; 1063660970Sobrien 1063790287Sobrien if (processing_template_decl) 1063890287Sobrien /* Do nothing now. */; 1063990287Sobrien else 1064090287Sobrien /* Always keep the BLOCK node associated with the outermost pair of 1064190287Sobrien curly braces of a function. These are needed for correct 1064290287Sobrien operation of dwarfout.c. */ 10643132747Skan keep_next_level (true); 1064460970Sobrien 10645132747Skan stmt = begin_compound_stmt (/*has_no_scope=*/false); 1064690287Sobrien COMPOUND_STMT_BODY_BLOCK (stmt) = 1; 1064760970Sobrien 1064890287Sobrien if (processing_template_decl) 1064990287Sobrien /* Do nothing now. */; 1065090287Sobrien else if (DECL_CONSTRUCTOR_P (current_function_decl)) 1065190287Sobrien begin_constructor_body (); 1065290287Sobrien else if (DECL_DESTRUCTOR_P (current_function_decl)) 1065390287Sobrien begin_destructor_body (); 1065460970Sobrien 1065590287Sobrien return stmt; 1065690287Sobrien} 1065760970Sobrien 1065890287Sobrien/* Do the processing for the end of a function body. Currently, this means 1065990287Sobrien closing out the cleanups for fully-constructed bases and members, and in 1066090287Sobrien the case of the destructor, deleting the object if desired. Again, this 1066190287Sobrien is only meaningful for [cd]tors, since they are the only functions where 1066290287Sobrien there is a significant distinction between the main body and any 1066390287Sobrien function catch clauses. Handling, say, main() return semantics here 1066490287Sobrien would be wrong, as flowing off the end of a function catch clause for 1066590287Sobrien main() would also need to return 0. */ 1066660970Sobrien 1066790287Sobrienvoid 10668132747Skanfinish_function_body (tree compstmt) 1066990287Sobrien{ 10670107601Sobrien /* Close the block. */ 10671132747Skan finish_compound_stmt (compstmt); 1067260970Sobrien 1067390287Sobrien if (processing_template_decl) 1067490287Sobrien /* Do nothing now. */; 1067590287Sobrien else if (DECL_CONSTRUCTOR_P (current_function_decl)) 1067690287Sobrien finish_constructor_body (); 1067790287Sobrien else if (DECL_DESTRUCTOR_P (current_function_decl)) 1067890287Sobrien finish_destructor_body (); 1067990287Sobrien} 1068060970Sobrien 1068118334Speter/* Finish up a function declaration and compile that function 1068218334Speter all the way to assembler language output. The free the storage 1068318334Speter for the function definition. 1068418334Speter 1068590287Sobrien FLAGS is a bitwise or of the following values: 1068652290Sobrien 2 - INCLASS_INLINE 1068752290Sobrien We just finished processing the body of an in-class inline 1068852290Sobrien function definition. (This processing will have taken place 1068990287Sobrien after the class definition is complete.) */ 1069018334Speter 1069190287Sobrientree 10692132747Skanfinish_function (int flags) 1069318334Speter{ 10694132747Skan tree fndecl = current_function_decl; 1069518334Speter tree fntype, ctype = NULL_TREE; 1069652290Sobrien int inclass_inline = (flags & 2) != 0; 1069790287Sobrien int nested; 1069818334Speter 1069918334Speter /* When we get some parse errors, we can end up without a 1070018334Speter current_function_decl, so cope. */ 1070118334Speter if (fndecl == NULL_TREE) 1070290287Sobrien return error_mark_node; 1070318334Speter 10704117410Skan if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) 10705117410Skan && DECL_VIRTUAL_P (fndecl) 10706117410Skan && !processing_template_decl) 10707117410Skan { 10708117410Skan tree fnclass = DECL_CONTEXT (fndecl); 10709117410Skan if (fndecl == CLASSTYPE_KEY_METHOD (fnclass)) 10710117410Skan keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes); 10711117410Skan } 10712117410Skan 1071390287Sobrien nested = function_depth > 1; 1071418334Speter fntype = TREE_TYPE (fndecl); 1071518334Speter 1071690287Sobrien /* TREE_READONLY (fndecl) = 1; 1071790287Sobrien This caused &foo to be of type ptr-to-const-function 1071890287Sobrien which then got a warning when stored in a ptr-to-function variable. */ 1071918334Speter 1072090287Sobrien my_friendly_assert (building_stmt_tree (), 20000911); 1072190287Sobrien 1072290287Sobrien /* For a cloned function, we've already got all the code we need; 1072390287Sobrien there's no need to add any extra bits. */ 1072490287Sobrien if (!DECL_CLONED_FUNCTION_P (fndecl)) 1072518334Speter { 1072690287Sobrien if (DECL_MAIN_P (current_function_decl)) 1072751412Sobrien { 1072851412Sobrien /* Make it so that `main' always returns 0 by default. */ 10729102804Skan#if VMS_TARGET 1073090287Sobrien finish_return_stmt (integer_one_node); 1073118334Speter#else 1073290287Sobrien finish_return_stmt (integer_zero_node); 1073318334Speter#endif 1073451412Sobrien } 1073518334Speter 1073690287Sobrien /* Finish dealing with exception specifiers. */ 1073790287Sobrien if (flag_exceptions && !processing_template_decl 1073890287Sobrien && flag_enforce_eh_specs 1073990287Sobrien && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) 1074090287Sobrien finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS 1074190287Sobrien (TREE_TYPE (current_function_decl)), 1074290287Sobrien current_eh_spec_block); 1074390287Sobrien } 1074418334Speter 10745132747Skan finish_fname_decls (); 10746132747Skan 1074790287Sobrien /* If we're saving up tree structure, tie off the function now. */ 1074890287Sobrien finish_stmt_tree (&DECL_SAVED_TREE (fndecl)); 1074918334Speter 10750117410Skan /* If this function can't throw any exceptions, remember that. */ 10751117410Skan if (!processing_template_decl 10752117410Skan && !cp_function_chain->can_throw 10753117410Skan && !flag_non_call_exceptions) 10754117410Skan TREE_NOTHROW (fndecl) = 1; 10755117410Skan 1075690287Sobrien /* This must come after expand_function_end because cleanups might 1075790287Sobrien have declarations (from inline functions) that need to go into 1075890287Sobrien this function's blocks. */ 1075990287Sobrien 1076090287Sobrien /* If the current binding level isn't the outermost binding level 1076190287Sobrien for this function, either there is a bug, or we have experienced 1076290287Sobrien syntax errors and the statement tree is malformed. */ 10763132747Skan if (current_binding_level->kind != sk_function_parms) 1076490287Sobrien { 1076590287Sobrien /* Make sure we have already experienced errors. */ 1076690287Sobrien if (errorcount == 0) 1076790287Sobrien abort (); 1076818334Speter 1076990287Sobrien /* Throw away the broken statement tree and extra binding 1077090287Sobrien levels. */ 1077190287Sobrien DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE); 1077218334Speter 10773132747Skan while (current_binding_level->kind != sk_function_parms) 1077451412Sobrien { 10775132747Skan if (current_binding_level->kind == sk_class) 1077690287Sobrien pop_nested_class (); 1077790287Sobrien else 1077890287Sobrien poplevel (0, 0, 0); 1077951412Sobrien } 1078018334Speter } 1078118334Speter poplevel (1, 0, 1); 1078218334Speter 10783132747Skan /* Statements should always be full-expressions at the outermost set 10784132747Skan of curly braces for a function. */ 10785132747Skan my_friendly_assert (stmts_are_full_exprs_p (), 19990831); 10786132747Skan 1078790287Sobrien /* Set up the named return value optimization, if we can. Here, we 1078890287Sobrien eliminate the copy from the nrv into the RESULT_DECL and any cleanup 1078990287Sobrien for the nrv. genrtl_start_function and declare_return_variable 1079090287Sobrien handle making the nrv and RESULT_DECL share space. */ 1079190287Sobrien if (current_function_return_value) 1079218334Speter { 1079390287Sobrien tree r = current_function_return_value; 10794117410Skan tree outer; 10795117410Skan 1079690287Sobrien if (r != error_mark_node 10797117410Skan /* This is only worth doing for fns that return in memory--and 10798117410Skan simpler, since we don't have to worry about promoted modes. */ 10799132747Skan && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl) 10800117410Skan /* Only allow this for variables declared in the outer scope of 10801117410Skan the function so we know that their lifetime always ends with a 10802117410Skan return; see g++.dg/opt/nrv6.C. We could be more flexible if 10803117410Skan we were to do this optimization in tree-ssa. */ 10804132747Skan /* Skip the artificial function body block. */ 10805132747Skan && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))), 10806117410Skan chain_member (r, BLOCK_VARS (outer)))) 1080790287Sobrien { 10808117410Skan 1080990287Sobrien DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl)); 1081090287Sobrien walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), 1081190287Sobrien nullify_returns_r, r); 1081290287Sobrien } 1081390287Sobrien else 1081490287Sobrien /* Clear it so genrtl_start_function and declare_return_variable 1081590287Sobrien know we're not optimizing. */ 1081690287Sobrien current_function_return_value = NULL_TREE; 1081718334Speter } 1081818334Speter 1081990287Sobrien /* Remember that we were in class scope. */ 1082090287Sobrien if (current_class_name) 1082190287Sobrien ctype = current_class_type; 1082290287Sobrien 1082318334Speter /* Must mark the RESULT_DECL as being in this function. */ 1082451412Sobrien DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 1082518334Speter 1082618334Speter /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point 1082718334Speter to the FUNCTION_DECL node itself. */ 1082818334Speter BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 1082918334Speter 1083090287Sobrien /* Save away current state, if appropriate. */ 1083190287Sobrien if (!processing_template_decl) 1083290287Sobrien save_function_data (fndecl); 1083318334Speter 1083490287Sobrien /* If this function calls `setjmp' it cannot be inlined. When 1083590287Sobrien `longjmp' is called it is not guaranteed to restore the value of 1083690287Sobrien local variables that have been modified since the call to 1083790287Sobrien `setjmp'. So, if were to inline this function into some caller 1083890287Sobrien `c', then when we `longjmp', we might not restore all variables 1083990287Sobrien in `c'. (It might seem, at first blush, that there's no way for 1084090287Sobrien this function to modify local variables in `c', but their 1084190287Sobrien addresses may have been stored somewhere accessible to this 1084290287Sobrien function.) */ 1084390287Sobrien if (!processing_template_decl && calls_setjmp_p (fndecl)) 1084490287Sobrien DECL_UNINLINABLE (fndecl) = 1; 1084518334Speter 1084696292Sobrien /* Complain if there's just no return statement. */ 1084796292Sobrien if (warn_return_type 1084896292Sobrien && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE 10849132747Skan && !dependent_type_p (TREE_TYPE (fntype)) 1085096292Sobrien && !current_function_returns_value && !current_function_returns_null 1085196292Sobrien /* Don't complain if we abort or throw. */ 1085296292Sobrien && !current_function_returns_abnormally 1085396292Sobrien && !DECL_NAME (DECL_RESULT (fndecl)) 1085496292Sobrien /* Normally, with -Wreturn-type, flow will complain. Unless we're an 1085596292Sobrien inline function, as we might never be compiled separately. */ 10856132747Skan && (DECL_INLINE (fndecl) || processing_template_decl)) 1085796292Sobrien warning ("no return statement in function returning non-void"); 10858132747Skan 10859132747Skan /* We're leaving the context of this function, so zap cfun. It's still in 10860132747Skan DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */ 1086190287Sobrien cfun = NULL; 10862132747Skan current_function_decl = NULL; 1086318334Speter 10864117410Skan /* If this is an in-class inline definition, we may have to pop the 1086590287Sobrien bindings for the template parameters that we added in 1086690287Sobrien maybe_begin_member_template_processing when start_function was 1086790287Sobrien called. */ 1086890287Sobrien if (inclass_inline) 1086990287Sobrien maybe_end_member_template_processing (); 1087018334Speter 1087190287Sobrien /* Leave the scope of the class. */ 1087290287Sobrien if (ctype) 1087390287Sobrien pop_nested_class (); 1087418334Speter 1087551412Sobrien --function_depth; 1087651412Sobrien 1087790287Sobrien /* Clean up. */ 1087818334Speter if (! nested) 1087990287Sobrien /* Let the error reporting routines know that we're outside a 1088090287Sobrien function. For a nested function, this value is used in 10881117410Skan cxx_pop_function_context and then reset via pop_function_context. */ 1088290287Sobrien current_function_decl = NULL_TREE; 1088318334Speter 1088490287Sobrien return fndecl; 1088518334Speter} 1088618334Speter 1088718334Speter/* Create the FUNCTION_DECL for a function definition. 1088818334Speter DECLSPECS and DECLARATOR are the parts of the declaration; 1088918334Speter they describe the return type and the name of the function, 1089018334Speter but twisted together in a fashion that parallels the syntax of C. 1089118334Speter 1089218334Speter This function creates a binding context for the function body 1089318334Speter as well as setting up the FUNCTION_DECL in current_function_decl. 1089418334Speter 1089518334Speter Returns a FUNCTION_DECL on success. 1089618334Speter 1089718334Speter If the DECLARATOR is not suitable for a function (it defines a datum 1089818334Speter instead), we return 0, which tells yyparse to report a parse error. 1089918334Speter 1090018334Speter May return void_type_node indicating that this method is actually 1090118334Speter a friend. See grokfield for more details. 1090218334Speter 1090318334Speter Came here with a `.pushlevel' . 1090418334Speter 1090518334Speter DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING 1090618334Speter CHANGES TO CODE IN `grokfield'. */ 1090751412Sobrien 1090818334Spetertree 10909132747Skanstart_method (tree declspecs, tree declarator, tree attrlist) 1091018334Speter{ 1091151412Sobrien tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, 1091290287Sobrien &attrlist); 1091318334Speter 10914132747Skan if (fndecl == error_mark_node) 10915132747Skan return error_mark_node; 1091618334Speter 10917132747Skan if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL) 10918132747Skan { 10919132747Skan error ("invalid member function declaration"); 10920132747Skan return error_mark_node; 10921132747Skan } 10922132747Skan 1092390287Sobrien if (attrlist) 1092490287Sobrien cplus_decl_attributes (&fndecl, attrlist, 0); 1092590287Sobrien 1092618334Speter /* Pass friends other than inline friend functions back. */ 1092751412Sobrien if (fndecl == void_type_node) 1092818334Speter return fndecl; 1092918334Speter 1093018334Speter if (DECL_IN_AGGR_P (fndecl)) 1093118334Speter { 1093218334Speter if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type) 1093318334Speter { 1093490287Sobrien if (DECL_CONTEXT (fndecl) 1093551412Sobrien && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL) 1093690287Sobrien error ("`%D' is already defined in class `%T'", fndecl, 1093790287Sobrien DECL_CONTEXT (fndecl)); 1093818334Speter } 1093918334Speter return void_type_node; 1094018334Speter } 1094118334Speter 1094252290Sobrien check_template_shadow (fndecl); 1094352290Sobrien 1094490287Sobrien DECL_DECLARED_INLINE_P (fndecl) = 1; 1094518334Speter if (flag_default_inline) 1094618334Speter DECL_INLINE (fndecl) = 1; 1094718334Speter 1094852290Sobrien /* We process method specializations in finish_struct_1. */ 1094952290Sobrien if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) 10950132747Skan { 10951132747Skan fndecl = push_template_decl (fndecl); 10952132747Skan if (fndecl == error_mark_node) 10953132747Skan return fndecl; 10954132747Skan } 1095518334Speter 1095618334Speter if (! DECL_FRIEND_P (fndecl)) 1095718334Speter { 1095818334Speter if (TREE_CHAIN (fndecl)) 1095918334Speter { 1096018334Speter fndecl = copy_node (fndecl); 1096118334Speter TREE_CHAIN (fndecl) = NULL_TREE; 1096218334Speter } 1096390287Sobrien grok_special_member_properties (fndecl); 1096418334Speter } 1096518334Speter 1096690287Sobrien cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0); 1096718334Speter 10968132747Skan /* Make a place for the parms. */ 10969132747Skan begin_scope (sk_function_parms, fndecl); 1097090287Sobrien 1097118334Speter DECL_IN_AGGR_P (fndecl) = 1; 1097218334Speter return fndecl; 1097318334Speter} 1097418334Speter 1097518334Speter/* Go through the motions of finishing a function definition. 1097618334Speter We don't compile this method until after the whole class has 1097718334Speter been processed. 1097818334Speter 1097918334Speter FINISH_METHOD must return something that looks as though it 1098018334Speter came from GROKFIELD (since we are defining a method, after all). 1098118334Speter 1098218334Speter This is called after parsing the body of the function definition. 1098318334Speter STMTS is the chain of statements that makes up the function body. 1098418334Speter 1098518334Speter DECL is the ..._DECL that `start_method' provided. */ 1098618334Speter 1098718334Spetertree 10988132747Skanfinish_method (tree decl) 1098918334Speter{ 10990132747Skan tree fndecl = decl; 1099118334Speter tree old_initial; 1099218334Speter 10993132747Skan tree link; 1099418334Speter 1099551412Sobrien if (decl == void_type_node) 1099618334Speter return decl; 1099718334Speter 1099818334Speter old_initial = DECL_INITIAL (fndecl); 1099918334Speter 1100018334Speter /* Undo the level for the parms (from start_method). 1100118334Speter This is like poplevel, but it causes nothing to be 1100218334Speter saved. Saving information here confuses symbol-table 1100318334Speter output routines. Besides, this information will 1100418334Speter be correctly output when this method is actually 1100518334Speter compiled. */ 1100618334Speter 1100718334Speter /* Clear out the meanings of the local variables of this level; 1100818334Speter also record in each decl which block it belongs to. */ 1100918334Speter 1101018334Speter for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) 1101118334Speter { 1101218334Speter if (DECL_NAME (link) != NULL_TREE) 1101352290Sobrien pop_binding (DECL_NAME (link), link); 1101418334Speter my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163); 1101518334Speter DECL_CONTEXT (link) = NULL_TREE; 1101618334Speter } 1101718334Speter 1101818334Speter poplevel (0, 0, 0); 1101918334Speter 1102018334Speter DECL_INITIAL (fndecl) = old_initial; 1102118334Speter 1102218334Speter /* We used to check if the context of FNDECL was different from 1102318334Speter current_class_type as another way to get inside here. This didn't work 1102418334Speter for String.cc in libg++. */ 1102518334Speter if (DECL_FRIEND_P (fndecl)) 1102618334Speter { 1102718334Speter CLASSTYPE_INLINE_FRIENDS (current_class_type) 1102818334Speter = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type)); 1102918334Speter decl = void_type_node; 1103018334Speter } 1103118334Speter 1103218334Speter return decl; 1103318334Speter} 1103418334Speter 1103518334Speter 1103696292Sobrien/* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that 1103796292Sobrien we can lay it out later, when and if its type becomes complete. */ 1103896292Sobrien 1103918334Spetervoid 11040132747Skanmaybe_register_incomplete_var (tree var) 1104118334Speter{ 1104296292Sobrien my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406); 1104318334Speter 1104496292Sobrien /* Keep track of variables with incomplete types. */ 1104596292Sobrien if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 1104696292Sobrien && DECL_EXTERNAL (var)) 1104718334Speter { 1104896292Sobrien tree inner_type = TREE_TYPE (var); 1104996292Sobrien 1105096292Sobrien while (TREE_CODE (inner_type) == ARRAY_TYPE) 1105196292Sobrien inner_type = TREE_TYPE (inner_type); 1105296292Sobrien inner_type = TYPE_MAIN_VARIANT (inner_type); 1105396292Sobrien 1105496292Sobrien if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) 1105596292Sobrien /* RTTI TD entries are created while defining the type_info. */ 1105696292Sobrien || (TYPE_LANG_SPECIFIC (inner_type) 1105796292Sobrien && TYPE_BEING_DEFINED (inner_type))) 1105896292Sobrien incomplete_vars = tree_cons (inner_type, var, incomplete_vars); 1105990287Sobrien } 1106096292Sobrien} 1106190287Sobrien 1106296292Sobrien/* Called when a class type (given by TYPE) is defined. If there are 1106396292Sobrien any existing VAR_DECLs whose type hsa been completed by this 1106496292Sobrien declaration, update them now. */ 1106596292Sobrien 1106696292Sobrienvoid 11067132747Skancomplete_vars (tree type) 1106896292Sobrien{ 1106996292Sobrien tree *list = &incomplete_vars; 1107096292Sobrien 1107196292Sobrien my_friendly_assert (CLASS_TYPE_P (type), 20020406); 1107296292Sobrien while (*list) 1107390287Sobrien { 1107496292Sobrien if (same_type_p (type, TREE_PURPOSE (*list))) 1107518334Speter { 1107696292Sobrien tree var = TREE_VALUE (*list); 1107796292Sobrien /* Complete the type of the variable. The VAR_DECL itself 1107896292Sobrien will be laid out in expand_expr. */ 1107996292Sobrien complete_type (TREE_TYPE (var)); 1108096292Sobrien /* Remove this entry from the list. */ 1108196292Sobrien *list = TREE_CHAIN (*list); 1108218334Speter } 1108318334Speter else 1108496292Sobrien list = &TREE_CHAIN (*list); 1108518334Speter } 1108618334Speter} 1108718334Speter 1108890287Sobrien/* If DECL is of a type which needs a cleanup, build that cleanup 1108990287Sobrien here. */ 1109018334Speter 1109190287Sobrientree 11092132747Skancxx_maybe_build_cleanup (tree decl) 1109318334Speter{ 1109418334Speter tree type = TREE_TYPE (decl); 1109590287Sobrien 1109690287Sobrien if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 1109718334Speter { 1109890287Sobrien int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR; 1109918334Speter tree rval; 1110018334Speter 1110118334Speter if (TREE_CODE (type) == ARRAY_TYPE) 1110218334Speter rval = decl; 1110318334Speter else 1110418334Speter { 11105117410Skan cxx_mark_addressable (decl); 1110618334Speter rval = build_unary_op (ADDR_EXPR, decl, 0); 1110718334Speter } 1110818334Speter 1110918334Speter /* Optimize for space over speed here. */ 1111018334Speter if (! TYPE_USES_VIRTUAL_BASECLASSES (type) 1111118334Speter || flag_expensive_optimizations) 1111218334Speter flags |= LOOKUP_NONVIRTUAL; 1111318334Speter 1111490287Sobrien rval = build_delete (TREE_TYPE (rval), rval, 1111590287Sobrien sfk_complete_destructor, flags, 0); 1111618334Speter 1111718334Speter if (TYPE_USES_VIRTUAL_BASECLASSES (type) 1111818334Speter && ! TYPE_HAS_DESTRUCTOR (type)) 11119132747Skan rval = build_compound_expr (rval, build_vbase_delete (type, decl)); 1112018334Speter 1112118334Speter return rval; 1112218334Speter } 1112396292Sobrien return NULL_TREE; 1112418334Speter} 1112518334Speter 1112690287Sobrien/* When a stmt has been parsed, this function is called. */ 1112718334Speter 1112818334Spetervoid 11129132747Skanfinish_stmt (void) 1113018334Speter{ 1113190287Sobrien /* Always assume this statement was not an expression statement. If 1113290287Sobrien it actually was an expression statement, its our callers 1113390287Sobrien responsibility to fix this up. */ 1113490287Sobrien last_expr_type = NULL_TREE; 1113518334Speter} 1113618334Speter 1113790287Sobrien/* DECL was originally constructed as a non-static member function, 1113890287Sobrien but turned out to be static. Update it accordingly. */ 1113918334Speter 1114018334Spetervoid 11141132747Skanrevert_static_member_fn (tree decl) 1114218334Speter{ 1114318334Speter tree tmp; 1114490287Sobrien tree function = TREE_TYPE (decl); 1114590287Sobrien tree args = TYPE_ARG_TYPES (function); 1114618334Speter 1114790287Sobrien if (cp_type_quals (TREE_TYPE (TREE_VALUE (args))) 1114852290Sobrien != TYPE_UNQUALIFIED) 1114990287Sobrien error ("static member function `%#D' declared with type qualifiers", 1115090287Sobrien decl); 1115118334Speter 1115218334Speter args = TREE_CHAIN (args); 1115318334Speter tmp = build_function_type (TREE_TYPE (function), args); 1115490287Sobrien tmp = build_qualified_type (tmp, cp_type_quals (function)); 1115518334Speter tmp = build_exception_variant (tmp, 1115618334Speter TYPE_RAISES_EXCEPTIONS (function)); 1115790287Sobrien TREE_TYPE (decl) = tmp; 1115890287Sobrien if (DECL_ARGUMENTS (decl)) 1115990287Sobrien DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl)); 1116090287Sobrien DECL_STATIC_FUNCTION_P (decl) = 1; 1116118334Speter} 1116218334Speter 1116390287Sobrien/* Initialize the variables used during compilation of a C++ 1116490287Sobrien function. */ 1116590287Sobrien 11166117410Skanvoid 11167132747Skancxx_push_function_context (struct function * f) 1116818334Speter{ 11169117410Skan struct language_function *p 11170132747Skan = ggc_alloc_cleared (sizeof (struct language_function)); 11171117410Skan f->language = p; 1117218334Speter 1117390287Sobrien /* Whenever we start a new function, we destroy temporaries in the 1117490287Sobrien usual way. */ 1117590287Sobrien current_stmt_tree ()->stmts_are_full_exprs_p = 1; 11176132747Skan 11177132747Skan if (f->decl) 11178132747Skan { 11179132747Skan tree fn = f->decl; 11180132747Skan 11181132747Skan if (DECL_SAVED_FUNCTION_DATA (fn)) 11182132747Skan { 11183132747Skan /* If we already parsed this function, and we're just expanding it 11184132747Skan now, restore saved state. */ 11185132747Skan *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn); 11186132747Skan 11187132747Skan /* If we decided that we didn't want to inline this function, 11188132747Skan make sure the back-end knows that. */ 11189132747Skan if (!current_function_cannot_inline) 11190132747Skan current_function_cannot_inline = cp_function_chain->cannot_inline; 11191132747Skan 11192132747Skan /* We don't need the saved data anymore. Unless this is an inline 11193132747Skan function; we need the named return value info for 11194132747Skan cp_copy_res_decl_for_inlining. */ 11195132747Skan if (! DECL_INLINE (fn)) 11196132747Skan DECL_SAVED_FUNCTION_DATA (fn) = NULL; 11197132747Skan } 11198132747Skan } 1119990287Sobrien} 1120018334Speter 1120190287Sobrien/* Free the language-specific parts of F, now that we've finished 1120290287Sobrien compiling the function. */ 1120318334Speter 11204117410Skanvoid 11205132747Skancxx_pop_function_context (struct function * f) 1120618334Speter{ 1120790287Sobrien f->language = 0; 1120890287Sobrien} 1120918334Speter 11210117410Skan/* Return which tree structure is used by T, or TS_CP_GENERIC if T is 11211117410Skan one of the language-independent trees. */ 1121218334Speter 11213117410Skanenum cp_tree_node_structure_enum 11214132747Skancp_tree_node_structure (union lang_tree_node * t) 1121590287Sobrien{ 11216117410Skan switch (TREE_CODE (&t->generic)) 1121790287Sobrien { 11218132747Skan case DEFAULT_ARG: return TS_CP_DEFAULT_ARG; 11219117410Skan case IDENTIFIER_NODE: return TS_CP_IDENTIFIER; 11220117410Skan case OVERLOAD: return TS_CP_OVERLOAD; 11221117410Skan case TEMPLATE_PARM_INDEX: return TS_CP_TPI; 11222117410Skan case PTRMEM_CST: return TS_CP_PTRMEM; 11223132747Skan case BASELINK: return TS_CP_BASELINK; 11224117410Skan case WRAPPER: return TS_CP_WRAPPER; 11225117410Skan default: return TS_CP_GENERIC; 1122690287Sobrien } 1122790287Sobrien} 1122818334Speter 1122990287Sobrien/* Build the void_list_node (void_type_node having been created). */ 1123090287Sobrientree 11231132747Skanbuild_void_list_node (void) 1123218334Speter{ 1123390287Sobrien tree t = build_tree_list (NULL_TREE, void_type_node); 1123490287Sobrien TREE_PARMLIST (t) = 1; 1123590287Sobrien return t; 1123618334Speter} 1123790287Sobrien 1123890287Sobrienstatic int 11239132747Skancp_missing_noreturn_ok_p (tree decl) 1124090287Sobrien{ 1124190287Sobrien /* A missing noreturn is ok for the `main' function. */ 1124290287Sobrien return DECL_MAIN_P (decl); 1124390287Sobrien} 11244117410Skan 11245117410Skan#include "gt-cp-decl.h" 11246117410Skan#include "gtype-cp.h" 11247