1 1.1 mrg /* Process declarations and variables for C++ compiler. 2 1.1 mrg Copyright (C) 1988-2022 Free Software Foundation, Inc. 3 1.1 mrg Hacked by Michael Tiemann (tiemann (at) cygnus.com) 4 1.1 mrg 5 1.1 mrg This file is part of GCC. 6 1.1 mrg 7 1.1 mrg GCC is free software; you can redistribute it and/or modify 8 1.1 mrg it under the terms of the GNU General Public License as published by 9 1.1 mrg the Free Software Foundation; either version 3, or (at your option) 10 1.1 mrg any later version. 11 1.1 mrg 12 1.1 mrg GCC is distributed in the hope that it will be useful, 13 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 mrg GNU General Public License for more details. 16 1.1 mrg 17 1.1 mrg You should have received a copy of the GNU General Public License 18 1.1 mrg along with GCC; see the file COPYING3. If not see 19 1.1 mrg <http://www.gnu.org/licenses/>. */ 20 1.1 mrg 21 1.1 mrg 22 1.1 mrg /* Process declarations and symbol lookup for C++ front end. 23 1.1 mrg Also constructs types; the standard scalar types at initialization, 24 1.1 mrg and structure, union, array and enum types when they are declared. */ 25 1.1 mrg 26 1.1 mrg /* ??? not all decl nodes are given the most useful possible 27 1.1 mrg line numbers. For example, the CONST_DECLs for enum values. */ 28 1.1 mrg 29 1.1 mrg #include "config.h" 30 1.1 mrg #include "system.h" 31 1.1 mrg #include "coretypes.h" 32 1.1 mrg #include "memmodel.h" 33 1.1 mrg #include "target.h" 34 1.1 mrg #include "cp-tree.h" 35 1.1 mrg #include "c-family/c-common.h" 36 1.1 mrg #include "timevar.h" 37 1.1 mrg #include "stringpool.h" 38 1.1 mrg #include "cgraph.h" 39 1.1 mrg #include "varasm.h" 40 1.1 mrg #include "attribs.h" 41 1.1 mrg #include "stor-layout.h" 42 1.1 mrg #include "calls.h" 43 1.1 mrg #include "decl.h" 44 1.1 mrg #include "toplev.h" 45 1.1 mrg #include "c-family/c-objc.h" 46 1.1 mrg #include "c-family/c-pragma.h" 47 1.1 mrg #include "dumpfile.h" 48 1.1 mrg #include "intl.h" 49 1.1 mrg #include "c-family/c-ada-spec.h" 50 1.1 mrg #include "asan.h" 51 1.1 mrg #include "optabs-query.h" 52 1.1 mrg 53 1.1 mrg /* Id for dumping the raw trees. */ 54 1.1 mrg int raw_dump_id; 55 1.1 mrg 56 1.1 mrg extern cpp_reader *parse_in; 57 1.1 mrg 58 1.1 mrg /* This structure contains information about the initializations 59 1.1 mrg and/or destructions required for a particular priority level. */ 60 1.1 mrg typedef struct priority_info_s { 61 1.1 mrg /* Nonzero if there have been any initializations at this priority 62 1.1 mrg throughout the translation unit. */ 63 1.1 mrg int initializations_p; 64 1.1 mrg /* Nonzero if there have been any destructions at this priority 65 1.1 mrg throughout the translation unit. */ 66 1.1 mrg int destructions_p; 67 1.1 mrg } *priority_info; 68 1.1 mrg 69 1.1 mrg static tree start_objects (int, int); 70 1.1 mrg static void finish_objects (int, int, tree); 71 1.1 mrg static tree start_static_storage_duration_function (unsigned); 72 1.1 mrg static void finish_static_storage_duration_function (tree); 73 1.1 mrg static priority_info get_priority_info (int); 74 1.1 mrg static void do_static_initialization_or_destruction (tree, bool); 75 1.1 mrg static void one_static_initialization_or_destruction (tree, tree, bool); 76 1.1 mrg static void generate_ctor_or_dtor_function (bool, int, location_t *); 77 1.1 mrg static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node, 78 1.1 mrg void *); 79 1.1 mrg static tree prune_vars_needing_no_initialization (tree *); 80 1.1 mrg static void write_out_vars (tree); 81 1.1 mrg static void import_export_class (tree); 82 1.1 mrg static tree get_guard_bits (tree); 83 1.1 mrg static void determine_visibility_from_class (tree, tree); 84 1.1 mrg static bool determine_hidden_inline (tree); 85 1.1 mrg 86 1.1 mrg /* A list of static class variables. This is needed, because a 87 1.1 mrg static class variable can be declared inside the class without 88 1.1 mrg an initializer, and then initialized, statically, outside the class. */ 89 1.1 mrg static GTY(()) vec<tree, va_gc> *pending_statics; 90 1.1 mrg 91 1.1 mrg /* A list of functions which were declared inline, but which we 92 1.1 mrg may need to emit outline anyway. */ 93 1.1 mrg static GTY(()) vec<tree, va_gc> *deferred_fns; 94 1.1 mrg 95 1.1 mrg /* A list of decls that use types with no linkage, which we need to make 96 1.1 mrg sure are defined. */ 97 1.1 mrg static GTY(()) vec<tree, va_gc> *no_linkage_decls; 98 1.1 mrg 99 1.1 mrg /* A vector of alternating decls and identifiers, where the latter 100 1.1 mrg is to be an alias for the former if the former is defined. */ 101 1.1 mrg static GTY(()) vec<tree, va_gc> *mangling_aliases; 102 1.1 mrg 103 1.1 mrg /* hash traits for declarations. Hashes single decls via 104 1.1 mrg DECL_ASSEMBLER_NAME_RAW. */ 105 1.1 mrg 106 1.1 mrg struct mangled_decl_hash : ggc_remove <tree> 107 1.1 mrg { 108 1.1 mrg typedef tree value_type; /* A DECL. */ 109 1.1 mrg typedef tree compare_type; /* An identifier. */ 110 1.1 mrg 111 1.1 mrg static hashval_t hash (const value_type decl) 112 1.1 mrg { 113 1.1 mrg return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl)); 114 1.1 mrg } 115 1.1 mrg static bool equal (const value_type existing, compare_type candidate) 116 1.1 mrg { 117 1.1 mrg tree name = DECL_ASSEMBLER_NAME_RAW (existing); 118 1.1 mrg return candidate == name; 119 1.1 mrg } 120 1.1 mrg 121 1.1 mrg static const bool empty_zero_p = true; 122 1.1 mrg static inline void mark_empty (value_type &p) {p = NULL_TREE;} 123 1.1 mrg static inline bool is_empty (value_type p) {return !p;} 124 1.1 mrg 125 1.1 mrg static bool is_deleted (value_type e) 126 1.1 mrg { 127 1.1 mrg return e == reinterpret_cast <value_type> (1); 128 1.1 mrg } 129 1.1 mrg static void mark_deleted (value_type &e) 130 1.1 mrg { 131 1.1 mrg e = reinterpret_cast <value_type> (1); 132 1.1 mrg } 133 1.1 mrg }; 134 1.1 mrg 135 1.1 mrg /* A hash table of decls keyed by mangled name. Used to figure out if 136 1.1 mrg we need compatibility aliases. */ 137 1.1 mrg static GTY(()) hash_table<mangled_decl_hash> *mangled_decls; 138 1.1 mrg 139 1.1 mrg /* Nonzero if we're done parsing and into end-of-file activities. */ 140 1.1 mrg 141 1.1 mrg int at_eof; 142 1.1 mrg 143 1.1 mrg /* True if note_mangling_alias should enqueue mangling aliases for 144 1.1 mrg later generation, rather than emitting them right away. */ 145 1.1 mrg 146 1.1 mrg bool defer_mangling_aliases = true; 147 1.1 mrg 148 1.1 mrg 150 1.1 mrg /* Return a member function type (a METHOD_TYPE), given FNTYPE (a 151 1.1 mrg FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers 152 1.1 mrg that apply to the function). */ 153 1.1 mrg 154 1.1 mrg tree 155 1.1 mrg build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals, 156 1.1 mrg cp_ref_qualifier rqual) 157 1.1 mrg { 158 1.1 mrg if (fntype == error_mark_node || ctype == error_mark_node) 159 1.1 mrg return error_mark_node; 160 1.1 mrg 161 1.1 mrg gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype)); 162 1.1 mrg 163 1.1 mrg cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT; 164 1.1 mrg ctype = cp_build_qualified_type (ctype, type_quals); 165 1.1 mrg 166 1.1 mrg tree newtype 167 1.1 mrg = build_method_type_directly (ctype, TREE_TYPE (fntype), 168 1.1 mrg (TREE_CODE (fntype) == METHOD_TYPE 169 1.1 mrg ? TREE_CHAIN (TYPE_ARG_TYPES (fntype)) 170 1.1 mrg : TYPE_ARG_TYPES (fntype))); 171 1.1 mrg if (tree attrs = TYPE_ATTRIBUTES (fntype)) 172 1.1 mrg newtype = cp_build_type_attribute_variant (newtype, attrs); 173 1.1 mrg newtype = build_cp_fntype_variant (newtype, rqual, 174 1.1 mrg TYPE_RAISES_EXCEPTIONS (fntype), 175 1.1 mrg TYPE_HAS_LATE_RETURN_TYPE (fntype)); 176 1.1 mrg 177 1.1 mrg return newtype; 178 1.1 mrg } 179 1.1 mrg 180 1.1 mrg /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its 181 1.1 mrg return type changed to NEW_RET. */ 182 1.1 mrg 183 1.1 mrg tree 184 1.1 mrg change_return_type (tree new_ret, tree fntype) 185 1.1 mrg { 186 1.1 mrg if (new_ret == error_mark_node) 187 1.1 mrg return fntype; 188 1.1 mrg 189 1.1 mrg if (same_type_p (new_ret, TREE_TYPE (fntype))) 190 1.1 mrg return fntype; 191 1.1 mrg 192 1.1 mrg tree newtype; 193 1.1 mrg tree args = TYPE_ARG_TYPES (fntype); 194 1.1 mrg 195 1.1 mrg if (TREE_CODE (fntype) == FUNCTION_TYPE) 196 1.1 mrg { 197 1.1 mrg newtype = build_function_type (new_ret, args); 198 1.1 mrg newtype = apply_memfn_quals (newtype, 199 1.1 mrg type_memfn_quals (fntype)); 200 1.1 mrg } 201 1.1 mrg else 202 1.1 mrg newtype = build_method_type_directly 203 1.1 mrg (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args)); 204 1.1 mrg 205 1.1 mrg if (tree attrs = TYPE_ATTRIBUTES (fntype)) 206 1.1 mrg newtype = cp_build_type_attribute_variant (newtype, attrs); 207 1.1 mrg newtype = cxx_copy_lang_qualifiers (newtype, fntype); 208 1.1 mrg 209 1.1 mrg return newtype; 210 1.1 mrg } 211 1.1 mrg 212 1.1 mrg /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE 213 1.1 mrg appropriately. */ 214 1.1 mrg 215 1.1 mrg tree 216 1.1 mrg cp_build_parm_decl (tree fn, tree name, tree type) 217 1.1 mrg { 218 1.1 mrg tree parm = build_decl (input_location, 219 1.1 mrg PARM_DECL, name, type); 220 1.1 mrg DECL_CONTEXT (parm) = fn; 221 1.1 mrg 222 1.1 mrg /* DECL_ARG_TYPE is only used by the back end and the back end never 223 1.1 mrg sees templates. */ 224 1.1 mrg if (!processing_template_decl) 225 1.1 mrg DECL_ARG_TYPE (parm) = type_passed_as (type); 226 1.1 mrg 227 1.1 mrg return parm; 228 1.1 mrg } 229 1.1 mrg 230 1.1 mrg /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the 231 1.1 mrg indicated NAME. */ 232 1.1 mrg 233 1.1 mrg tree 234 1.1 mrg build_artificial_parm (tree fn, tree name, tree type) 235 1.1 mrg { 236 1.1 mrg tree parm = cp_build_parm_decl (fn, name, type); 237 1.1 mrg DECL_ARTIFICIAL (parm) = 1; 238 1.1 mrg /* All our artificial parms are implicitly `const'; they cannot be 239 1.1 mrg assigned to. */ 240 1.1 mrg TREE_READONLY (parm) = 1; 241 1.1 mrg return parm; 242 1.1 mrg } 243 1.1 mrg 244 1.1 mrg /* Constructors for types with virtual baseclasses need an "in-charge" flag 245 1.1 mrg saying whether this constructor is responsible for initialization of 246 1.1 mrg virtual baseclasses or not. All destructors also need this "in-charge" 247 1.1 mrg flag, which additionally determines whether or not the destructor should 248 1.1 mrg free the memory for the object. 249 1.1 mrg 250 1.1 mrg This function adds the "in-charge" flag to member function FN if 251 1.1 mrg appropriate. It is called from grokclassfn and tsubst. 252 1.1 mrg FN must be either a constructor or destructor. 253 1.1 mrg 254 1.1 mrg The in-charge flag follows the 'this' parameter, and is followed by the 255 1.1 mrg VTT parm (if any), then the user-written parms. */ 256 1.1 mrg 257 1.1 mrg void 258 1.1 mrg maybe_retrofit_in_chrg (tree fn) 259 1.1 mrg { 260 1.1 mrg tree basetype, arg_types, parms, parm, fntype; 261 1.1 mrg 262 1.1 mrg /* If we've already add the in-charge parameter don't do it again. */ 263 1.1 mrg if (DECL_HAS_IN_CHARGE_PARM_P (fn)) 264 1.1 mrg return; 265 1.1 mrg 266 1.1 mrg /* When processing templates we can't know, in general, whether or 267 1.1 mrg not we're going to have virtual baseclasses. */ 268 1.1 mrg if (processing_template_decl) 269 1.1 mrg return; 270 1.1 mrg 271 1.1 mrg /* We don't need an in-charge parameter for constructors that don't 272 1.1 mrg have virtual bases. */ 273 1.1 mrg if (DECL_CONSTRUCTOR_P (fn) 274 1.1 mrg && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn))) 275 1.1 mrg return; 276 1.1 mrg 277 1.1 mrg arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); 278 1.1 mrg basetype = TREE_TYPE (TREE_VALUE (arg_types)); 279 1.1 mrg arg_types = TREE_CHAIN (arg_types); 280 1.1 mrg 281 1.1 mrg parms = DECL_CHAIN (DECL_ARGUMENTS (fn)); 282 1.1 mrg 283 1.1 mrg /* If this is a subobject constructor or destructor, our caller will 284 1.1 mrg pass us a pointer to our VTT. */ 285 1.1 mrg if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn))) 286 1.1 mrg { 287 1.1 mrg parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type); 288 1.1 mrg 289 1.1 mrg /* First add it to DECL_ARGUMENTS between 'this' and the real args... */ 290 1.1 mrg DECL_CHAIN (parm) = parms; 291 1.1 mrg parms = parm; 292 1.1 mrg 293 1.1 mrg /* ...and then to TYPE_ARG_TYPES. */ 294 1.1 mrg arg_types = hash_tree_chain (vtt_parm_type, arg_types); 295 1.1 mrg 296 1.1 mrg DECL_HAS_VTT_PARM_P (fn) = 1; 297 1.1 mrg } 298 1.1 mrg 299 1.1 mrg /* Then add the in-charge parm (before the VTT parm). */ 300 1.1 mrg parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node); 301 1.1 mrg DECL_CHAIN (parm) = parms; 302 1.1 mrg parms = parm; 303 1.1 mrg arg_types = hash_tree_chain (integer_type_node, arg_types); 304 1.1 mrg 305 1.1 mrg /* Insert our new parameter(s) into the list. */ 306 1.1 mrg DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms; 307 1.1 mrg 308 1.1 mrg /* And rebuild the function type. */ 309 1.1 mrg fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)), 310 1.1 mrg arg_types); 311 1.1 mrg if (TYPE_ATTRIBUTES (TREE_TYPE (fn))) 312 1.1 mrg fntype = (cp_build_type_attribute_variant 313 1.1 mrg (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn)))); 314 1.1 mrg fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn)); 315 1.1 mrg TREE_TYPE (fn) = fntype; 316 1.1 mrg 317 1.1 mrg /* Now we've got the in-charge parameter. */ 318 1.1 mrg DECL_HAS_IN_CHARGE_PARM_P (fn) = 1; 319 1.1 mrg } 320 1.1 mrg 321 1.1 mrg /* Classes overload their constituent function names automatically. 322 1.1 mrg When a function name is declared in a record structure, 323 1.1 mrg its name is changed to it overloaded name. Since names for 324 1.1 mrg constructors and destructors can conflict, we place a leading 325 1.1 mrg '$' for destructors. 326 1.1 mrg 327 1.1 mrg CNAME is the name of the class we are grokking for. 328 1.1 mrg 329 1.1 mrg FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'. 330 1.1 mrg 331 1.1 mrg FLAGS contains bits saying what's special about today's 332 1.1 mrg arguments. DTOR_FLAG == DESTRUCTOR. 333 1.1 mrg 334 1.1 mrg If FUNCTION is a destructor, then we must add the `auto-delete' field 335 1.1 mrg as a second parameter. There is some hair associated with the fact 336 1.1 mrg that we must "declare" this variable in the manner consistent with the 337 1.1 mrg way the rest of the arguments were declared. 338 1.1 mrg 339 1.1 mrg QUALS are the qualifiers for the this pointer. */ 340 1.1 mrg 341 1.1 mrg void 342 1.1 mrg grokclassfn (tree ctype, tree function, enum overload_flags flags) 343 1.1 mrg { 344 1.1 mrg tree fn_name = DECL_NAME (function); 345 1.1 mrg 346 1.1 mrg /* Even within an `extern "C"' block, members get C++ linkage. See 347 1.1 mrg [dcl.link] for details. */ 348 1.1 mrg SET_DECL_LANGUAGE (function, lang_cplusplus); 349 1.1 mrg 350 1.1 mrg if (fn_name == NULL_TREE) 351 1.1 mrg { 352 1.1 mrg error ("name missing for member function"); 353 1.1 mrg fn_name = get_identifier ("<anonymous>"); 354 1.1 mrg DECL_NAME (function) = fn_name; 355 1.1 mrg } 356 1.1 mrg 357 1.1 mrg DECL_CONTEXT (function) = ctype; 358 1.1 mrg 359 1.1 mrg if (flags == DTOR_FLAG) 360 1.1 mrg DECL_CXX_DESTRUCTOR_P (function) = 1; 361 1.1 mrg 362 1.1 mrg if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)) 363 1.1 mrg maybe_retrofit_in_chrg (function); 364 1.1 mrg } 365 1.1 mrg 366 1.1 mrg /* Create an ARRAY_REF, checking for the user doing things backwards 367 1.1 mrg along the way. 368 1.1 mrg If INDEX_EXP is non-NULL, then that is the index expression, 369 1.1 mrg otherwise INDEX_EXP_LIST is the list of index expressions. */ 370 1.1 mrg 371 1.1 mrg tree 372 1.1 mrg grok_array_decl (location_t loc, tree array_expr, tree index_exp, 373 1.1 mrg vec<tree, va_gc> **index_exp_list, tsubst_flags_t complain) 374 1.1 mrg { 375 1.1 mrg tree type; 376 1.1 mrg tree expr; 377 1.1 mrg tree orig_array_expr = array_expr; 378 1.1 mrg tree orig_index_exp = index_exp; 379 1.1 mrg vec<tree, va_gc> *orig_index_exp_list 380 1.1 mrg = index_exp_list ? *index_exp_list : NULL; 381 1.1 mrg tree overload = NULL_TREE; 382 1.1 mrg 383 1.1 mrg if (error_operand_p (array_expr) || error_operand_p (index_exp)) 384 1.1 mrg return error_mark_node; 385 1.1 mrg 386 1.1 mrg if (processing_template_decl) 387 1.1 mrg { 388 1.1 mrg if (type_dependent_expression_p (array_expr) 389 1.1 mrg || (index_exp ? type_dependent_expression_p (index_exp) 390 1.1 mrg : any_type_dependent_arguments_p (*index_exp_list))) 391 1.1 mrg { 392 1.1 mrg if (index_exp == NULL) 393 1.1 mrg index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF), 394 1.1 mrg *index_exp_list); 395 1.1 mrg return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp, 396 1.1 mrg NULL_TREE, NULL_TREE); 397 1.1 mrg } 398 1.1 mrg array_expr = build_non_dependent_expr (array_expr); 399 1.1 mrg if (index_exp) 400 1.1 mrg index_exp = build_non_dependent_expr (index_exp); 401 1.1 mrg else 402 1.1 mrg { 403 1.1 mrg orig_index_exp_list = make_tree_vector_copy (*index_exp_list); 404 1.1 mrg make_args_non_dependent (*index_exp_list); 405 1.1 mrg } 406 1.1 mrg } 407 1.1 mrg 408 1.1 mrg type = TREE_TYPE (array_expr); 409 1.1 mrg gcc_assert (type); 410 1.1 mrg type = non_reference (type); 411 1.1 mrg 412 1.1 mrg /* If they have an `operator[]', use that. */ 413 1.1 mrg if (MAYBE_CLASS_TYPE_P (type) 414 1.1 mrg || (index_exp && MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp))) 415 1.1 mrg || (index_exp == NULL_TREE 416 1.1 mrg && !(*index_exp_list)->is_empty () 417 1.1 mrg && MAYBE_CLASS_TYPE_P (TREE_TYPE ((*index_exp_list)->last ())))) 418 1.1 mrg { 419 1.1 mrg if (index_exp) 420 1.1 mrg expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr, 421 1.1 mrg index_exp, NULL_TREE, NULL_TREE, 422 1.1 mrg &overload, complain); 423 1.1 mrg else if ((*index_exp_list)->is_empty ()) 424 1.1 mrg expr = build_op_subscript (loc, array_expr, index_exp_list, &overload, 425 1.1 mrg complain); 426 1.1 mrg else 427 1.1 mrg { 428 1.1 mrg expr = build_op_subscript (loc, array_expr, index_exp_list, 429 1.1 mrg &overload, complain & tf_decltype); 430 1.1 mrg if (expr == error_mark_node) 431 1.1 mrg { 432 1.1 mrg tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL, 433 1.1 mrg tf_none); 434 1.1 mrg if (idx != error_mark_node) 435 1.1 mrg expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr, 436 1.1 mrg idx, NULL_TREE, NULL_TREE, &overload, 437 1.1 mrg complain & tf_decltype); 438 1.1 mrg if (expr == error_mark_node) 439 1.1 mrg { 440 1.1 mrg overload = NULL_TREE; 441 1.1 mrg expr = build_op_subscript (loc, array_expr, index_exp_list, 442 1.1 mrg &overload, complain); 443 1.1 mrg } 444 1.1 mrg else 445 1.1 mrg { 446 1.1 mrg /* If it would be valid albeit deprecated expression in 447 1.1 mrg C++20, just pedwarn on it and treat it as if wrapped 448 1.1 mrg in (). */ 449 1.1 mrg pedwarn (loc, OPT_Wcomma_subscript, 450 1.1 mrg "top-level comma expression in array subscript " 451 1.1 mrg "changed meaning in C++23"); 452 1.1 mrg if (processing_template_decl) 453 1.1 mrg { 454 1.1 mrg orig_index_exp 455 1.1 mrg = build_x_compound_expr_from_vec (orig_index_exp_list, 456 1.1 mrg NULL, complain); 457 1.1 mrg if (orig_index_exp == error_mark_node) 458 1.1 mrg expr = error_mark_node; 459 1.1 mrg release_tree_vector (orig_index_exp_list); 460 1.1 mrg } 461 1.1 mrg } 462 1.1 mrg } 463 1.1 mrg } 464 1.1 mrg } 465 1.1 mrg else 466 1.1 mrg { 467 1.1 mrg tree p1, p2, i1, i2; 468 1.1 mrg bool swapped = false; 469 1.1 mrg 470 1.1 mrg /* Otherwise, create an ARRAY_REF for a pointer or array type. 471 1.1 mrg It is a little-known fact that, if `a' is an array and `i' is 472 1.1 mrg an int, you can write `i[a]', which means the same thing as 473 1.1 mrg `a[i]'. */ 474 1.1 mrg if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type)) 475 1.1 mrg p1 = array_expr; 476 1.1 mrg else 477 1.1 mrg p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false); 478 1.1 mrg 479 1.1 mrg if (index_exp == NULL_TREE) 480 1.1 mrg { 481 1.1 mrg if ((*index_exp_list)->is_empty ()) 482 1.1 mrg { 483 1.1 mrg error_at (loc, "built-in subscript operator without expression " 484 1.1 mrg "list"); 485 1.1 mrg return error_mark_node; 486 1.1 mrg } 487 1.1 mrg tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL, 488 1.1 mrg tf_none); 489 1.1 mrg if (idx != error_mark_node) 490 1.1 mrg /* If it would be valid albeit deprecated expression in C++20, 491 1.1 mrg just pedwarn on it and treat it as if wrapped in (). */ 492 1.1 mrg pedwarn (loc, OPT_Wcomma_subscript, 493 1.1 mrg "top-level comma expression in array subscript " 494 1.1 mrg "changed meaning in C++23"); 495 1.1 mrg else 496 1.1 mrg { 497 1.1 mrg error_at (loc, "built-in subscript operator with more than one " 498 1.1 mrg "expression in expression list"); 499 1.1 mrg return error_mark_node; 500 1.1 mrg } 501 1.1 mrg index_exp = idx; 502 1.1 mrg if (processing_template_decl) 503 1.1 mrg { 504 1.1 mrg orig_index_exp 505 1.1 mrg = build_x_compound_expr_from_vec (orig_index_exp_list, 506 1.1 mrg NULL, complain); 507 1.1 mrg release_tree_vector (orig_index_exp_list); 508 1.1 mrg if (orig_index_exp == error_mark_node) 509 1.1 mrg return error_mark_node; 510 1.1 mrg } 511 1.1 mrg } 512 1.1 mrg 513 1.1 mrg if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE) 514 1.1 mrg p2 = index_exp; 515 1.1 mrg else 516 1.1 mrg p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false); 517 1.1 mrg 518 1.1 mrg i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 519 1.1 mrg false); 520 1.1 mrg i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 521 1.1 mrg false); 522 1.1 mrg 523 1.1 mrg if ((p1 && i2) && (i1 && p2)) 524 1.1 mrg error ("ambiguous conversion for array subscript"); 525 1.1 mrg 526 1.1 mrg if (p1 && i2) 527 1.1 mrg array_expr = p1, index_exp = i2; 528 1.1 mrg else if (i1 && p2) 529 1.1 mrg swapped = true, array_expr = p2, index_exp = i1; 530 1.1 mrg else 531 1.1 mrg { 532 1.1 mrg error_at (loc, "invalid types %<%T[%T]%> for array subscript", 533 1.1 mrg type, TREE_TYPE (index_exp)); 534 1.1 mrg return error_mark_node; 535 1.1 mrg } 536 1.1 mrg 537 1.1 mrg if (array_expr == error_mark_node || index_exp == error_mark_node) 538 1.1 mrg error ("ambiguous conversion for array subscript"); 539 1.1 mrg 540 1.1 mrg if (TYPE_PTR_P (TREE_TYPE (array_expr))) 541 1.1 mrg array_expr = mark_rvalue_use (array_expr); 542 1.1 mrg else 543 1.1 mrg array_expr = mark_lvalue_use_nonread (array_expr); 544 1.1 mrg index_exp = mark_rvalue_use (index_exp); 545 1.1 mrg if (swapped 546 1.1 mrg && flag_strong_eval_order == 2 547 1.1 mrg && (TREE_SIDE_EFFECTS (array_expr) || TREE_SIDE_EFFECTS (index_exp))) 548 1.1 mrg expr = build_array_ref (input_location, index_exp, array_expr); 549 1.1 mrg else 550 1.1 mrg expr = build_array_ref (input_location, array_expr, index_exp); 551 1.1 mrg } 552 1.1 mrg if (processing_template_decl && expr != error_mark_node) 553 1.1 mrg { 554 1.1 mrg if (overload != NULL_TREE) 555 1.1 mrg { 556 1.1 mrg if (orig_index_exp == NULL_TREE) 557 1.1 mrg { 558 1.1 mrg expr = build_min_non_dep_op_overload (expr, overload, 559 1.1 mrg orig_array_expr, 560 1.1 mrg orig_index_exp_list); 561 1.1 mrg release_tree_vector (orig_index_exp_list); 562 1.1 mrg return expr; 563 1.1 mrg } 564 1.1 mrg return build_min_non_dep_op_overload (ARRAY_REF, expr, overload, 565 1.1 mrg orig_array_expr, 566 1.1 mrg orig_index_exp); 567 1.1 mrg } 568 1.1 mrg 569 1.1 mrg if (orig_index_exp == NULL_TREE) 570 1.1 mrg { 571 1.1 mrg orig_index_exp 572 1.1 mrg = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF), 573 1.1 mrg orig_index_exp_list); 574 1.1 mrg release_tree_vector (orig_index_exp_list); 575 1.1 mrg } 576 1.1 mrg 577 1.1 mrg return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, 578 1.1 mrg orig_index_exp, NULL_TREE, NULL_TREE); 579 1.1 mrg } 580 1.1 mrg return expr; 581 1.1 mrg } 582 1.1 mrg 583 1.1 mrg /* Given the cast expression EXP, checking out its validity. Either return 584 1.1 mrg an error_mark_node if there was an unavoidable error, return a cast to 585 1.1 mrg void for trying to delete a pointer w/ the value 0, or return the 586 1.1 mrg call to delete. If DOING_VEC is true, we handle things differently 587 1.1 mrg for doing an array delete. 588 1.1 mrg Implements ARM $5.3.4. This is called from the parser. */ 589 1.1 mrg 590 1.1 mrg tree 591 1.1 mrg delete_sanity (location_t loc, tree exp, tree size, bool doing_vec, 592 1.1 mrg int use_global_delete, tsubst_flags_t complain) 593 1.1 mrg { 594 1.1 mrg tree t, type; 595 1.1 mrg 596 1.1 mrg if (exp == error_mark_node) 597 1.1 mrg return exp; 598 1.1 mrg 599 1.1 mrg if (processing_template_decl) 600 1.1 mrg { 601 1.1 mrg t = build_min (DELETE_EXPR, void_type_node, exp, size); 602 1.1 mrg DELETE_EXPR_USE_GLOBAL (t) = use_global_delete; 603 1.1 mrg DELETE_EXPR_USE_VEC (t) = doing_vec; 604 1.1 mrg TREE_SIDE_EFFECTS (t) = 1; 605 1.1 mrg SET_EXPR_LOCATION (t, loc); 606 1.1 mrg return t; 607 1.1 mrg } 608 1.1 mrg 609 1.1 mrg location_t exp_loc = cp_expr_loc_or_loc (exp, loc); 610 1.1 mrg 611 1.1 mrg /* An array can't have been allocated by new, so complain. */ 612 1.1 mrg if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE 613 1.1 mrg && (complain & tf_warning)) 614 1.1 mrg warning_at (exp_loc, 0, "deleting array %q#E", exp); 615 1.1 mrg 616 1.1 mrg t = build_expr_type_conversion (WANT_POINTER, exp, true); 617 1.1 mrg 618 1.1 mrg if (t == NULL_TREE || t == error_mark_node) 619 1.1 mrg { 620 1.1 mrg if (complain & tf_error) 621 1.1 mrg error_at (exp_loc, 622 1.1 mrg "type %q#T argument given to %<delete%>, expected pointer", 623 1.1 mrg TREE_TYPE (exp)); 624 1.1 mrg return error_mark_node; 625 1.1 mrg } 626 1.1 mrg 627 1.1 mrg type = TREE_TYPE (t); 628 1.1 mrg 629 1.1 mrg /* As of Valley Forge, you can delete a pointer to const. */ 630 1.1 mrg 631 1.1 mrg /* You can't delete functions. */ 632 1.1 mrg if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) 633 1.1 mrg { 634 1.1 mrg if (complain & tf_error) 635 1.1 mrg error_at (exp_loc, 636 1.1 mrg "cannot delete a function. Only pointer-to-objects are " 637 1.1 mrg "valid arguments to %<delete%>"); 638 1.1 mrg return error_mark_node; 639 1.1 mrg } 640 1.1 mrg 641 1.1 mrg /* Deleting ptr to void is undefined behavior [expr.delete/3]. */ 642 1.1 mrg if (VOID_TYPE_P (TREE_TYPE (type))) 643 1.1 mrg { 644 1.1 mrg if (complain & tf_warning) 645 1.1 mrg warning_at (exp_loc, OPT_Wdelete_incomplete, 646 1.1 mrg "deleting %qT is undefined", type); 647 1.1 mrg doing_vec = 0; 648 1.1 mrg } 649 1.1 mrg 650 1.1 mrg /* Deleting a pointer with the value zero is valid and has no effect. */ 651 1.1 mrg if (integer_zerop (t)) 652 1.1 mrg return build1_loc (loc, NOP_EXPR, void_type_node, t); 653 1.1 mrg 654 1.1 mrg if (doing_vec) 655 1.1 mrg return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE, 656 1.1 mrg sfk_deleting_destructor, 657 1.1 mrg use_global_delete, complain); 658 1.1 mrg else 659 1.1 mrg return build_delete (loc, type, t, sfk_deleting_destructor, 660 1.1 mrg LOOKUP_NORMAL, use_global_delete, 661 1.1 mrg complain); 662 1.1 mrg } 663 1.1 mrg 664 1.1 mrg /* Report an error if the indicated template declaration is not the 665 1.1 mrg sort of thing that should be a member template. */ 666 1.1 mrg 667 1.1 mrg void 668 1.1 mrg check_member_template (tree tmpl) 669 1.1 mrg { 670 1.1 mrg tree decl; 671 1.1 mrg 672 1.1 mrg gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); 673 1.1 mrg decl = DECL_TEMPLATE_RESULT (tmpl); 674 1.1 mrg 675 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL 676 1.1 mrg || DECL_ALIAS_TEMPLATE_P (tmpl) 677 1.1 mrg || (TREE_CODE (decl) == TYPE_DECL 678 1.1 mrg && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))) 679 1.1 mrg { 680 1.1 mrg /* The parser rejects template declarations in local classes 681 1.1 mrg (with the exception of generic lambdas). */ 682 1.1 mrg gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl)); 683 1.1 mrg /* The parser rejects any use of virtual in a function template. */ 684 1.1 mrg gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL 685 1.1 mrg && DECL_VIRTUAL_P (decl))); 686 1.1 mrg 687 1.1 mrg /* The debug-information generating code doesn't know what to do 688 1.1 mrg with member templates. */ 689 1.1 mrg DECL_IGNORED_P (tmpl) = 1; 690 1.1 mrg } 691 1.1 mrg else if (variable_template_p (tmpl)) 692 1.1 mrg /* OK */; 693 1.1 mrg else 694 1.1 mrg error ("template declaration of %q#D", decl); 695 1.1 mrg } 696 1.1 mrg 697 1.1 mrg /* Sanity check: report error if this function FUNCTION is not 698 1.1 mrg really a member of the class (CTYPE) it is supposed to belong to. 699 1.1 mrg TEMPLATE_PARMS is used to specify the template parameters of a member 700 1.1 mrg template passed as FUNCTION_DECL. If the member template is passed as a 701 1.1 mrg TEMPLATE_DECL, it can be NULL since the parameters can be extracted 702 1.1 mrg from the declaration. If the function is not a function template, it 703 1.1 mrg must be NULL. 704 1.1 mrg It returns the original declaration for the function, NULL_TREE if 705 1.1 mrg no declaration was found, error_mark_node if an error was emitted. */ 706 1.1 mrg 707 1.1 mrg tree 708 1.1 mrg check_classfn (tree ctype, tree function, tree template_parms) 709 1.1 mrg { 710 1.1 mrg if (DECL_USE_TEMPLATE (function) 711 1.1 mrg && !(TREE_CODE (function) == TEMPLATE_DECL 712 1.1 mrg && DECL_TEMPLATE_SPECIALIZATION (function)) 713 1.1 mrg && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function))) 714 1.1 mrg /* Since this is a specialization of a member template, 715 1.1 mrg we're not going to find the declaration in the class. 716 1.1 mrg For example, in: 717 1.1 mrg 718 1.1 mrg struct S { template <typename T> void f(T); }; 719 1.1 mrg template <> void S::f(int); 720 1.1 mrg 721 1.1 mrg we're not going to find `S::f(int)', but there's no 722 1.1 mrg reason we should, either. We let our callers know we didn't 723 1.1 mrg find the method, but we don't complain. */ 724 1.1 mrg return NULL_TREE; 725 1.1 mrg 726 1.1 mrg /* Basic sanity check: for a template function, the template parameters 727 1.1 mrg either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */ 728 1.1 mrg if (TREE_CODE (function) == TEMPLATE_DECL) 729 1.1 mrg { 730 1.1 mrg if (template_parms 731 1.1 mrg && !comp_template_parms (template_parms, 732 1.1 mrg DECL_TEMPLATE_PARMS (function))) 733 1.1 mrg { 734 1.1 mrg error ("template parameter lists provided don%'t match the " 735 1.1 mrg "template parameters of %qD", function); 736 1.1 mrg return error_mark_node; 737 1.1 mrg } 738 1.1 mrg template_parms = DECL_TEMPLATE_PARMS (function); 739 1.1 mrg } 740 1.1 mrg 741 1.1 mrg /* OK, is this a definition of a member template? */ 742 1.1 mrg bool is_template = (template_parms != NULL_TREE); 743 1.1 mrg 744 1.1 mrg /* [temp.mem] 745 1.1 mrg 746 1.1 mrg A destructor shall not be a member template. */ 747 1.1 mrg if (DECL_DESTRUCTOR_P (function) && is_template) 748 1.1 mrg { 749 1.1 mrg error ("destructor %qD declared as member template", function); 750 1.1 mrg return error_mark_node; 751 1.1 mrg } 752 1.1 mrg 753 1.1 mrg /* We must enter the scope here, because conversion operators are 754 1.1 mrg named by target type, and type equivalence relies on typenames 755 1.1 mrg resolving within the scope of CTYPE. */ 756 1.1 mrg tree pushed_scope = push_scope (ctype); 757 1.1 mrg tree matched = NULL_TREE; 758 1.1 mrg tree fns = get_class_binding (ctype, DECL_NAME (function)); 759 1.1 mrg bool saw_template = false; 760 1.1 mrg 761 1.1 mrg for (ovl_iterator iter (fns); !matched && iter; ++iter) 762 1.1 mrg { 763 1.1 mrg tree fndecl = *iter; 764 1.1 mrg 765 1.1 mrg if (TREE_CODE (fndecl) == TEMPLATE_DECL) 766 1.1 mrg saw_template = true; 767 1.1 mrg 768 1.1 mrg /* A member template definition only matches a member template 769 1.1 mrg declaration. */ 770 1.1 mrg if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL)) 771 1.1 mrg continue; 772 1.1 mrg 773 1.1 mrg if (!DECL_DECLARES_FUNCTION_P (fndecl)) 774 1.1 mrg continue; 775 1.1 mrg 776 1.1 mrg tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function)); 777 1.1 mrg tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); 778 1.1 mrg 779 1.1 mrg /* We cannot simply call decls_match because this doesn't work 780 1.1 mrg for static member functions that are pretending to be 781 1.1 mrg methods, and because the name may have been changed by 782 1.1 mrg asm("new_name"). */ 783 1.1 mrg 784 1.1 mrg /* Get rid of the this parameter on functions that become 785 1.1 mrg static. */ 786 1.1 mrg if (DECL_STATIC_FUNCTION_P (fndecl) 787 1.1 mrg && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE) 788 1.1 mrg p1 = TREE_CHAIN (p1); 789 1.1 mrg 790 1.1 mrg /* ref-qualifier or absence of same must match. */ 791 1.1 mrg if (type_memfn_rqual (TREE_TYPE (function)) 792 1.1 mrg != type_memfn_rqual (TREE_TYPE (fndecl))) 793 1.1 mrg continue; 794 1.1 mrg 795 1.1 mrg // Include constraints in the match. 796 1.1 mrg tree c1 = get_constraints (function); 797 1.1 mrg tree c2 = get_constraints (fndecl); 798 1.1 mrg 799 1.1 mrg /* While finding a match, same types and params are not enough 800 1.1 mrg if the function is versioned. Also check version ("target") 801 1.1 mrg attributes. */ 802 1.1 mrg if (same_type_p (TREE_TYPE (TREE_TYPE (function)), 803 1.1 mrg TREE_TYPE (TREE_TYPE (fndecl))) 804 1.1 mrg && compparms (p1, p2) 805 1.1 mrg && !targetm.target_option.function_versions (function, fndecl) 806 1.1 mrg && (!is_template 807 1.1 mrg || comp_template_parms (template_parms, 808 1.1 mrg DECL_TEMPLATE_PARMS (fndecl))) 809 1.1 mrg && equivalent_constraints (c1, c2) 810 1.1 mrg && (DECL_TEMPLATE_SPECIALIZATION (function) 811 1.1 mrg == DECL_TEMPLATE_SPECIALIZATION (fndecl)) 812 1.1 mrg && (!DECL_TEMPLATE_SPECIALIZATION (function) 813 1.1 mrg || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl)))) 814 1.1 mrg matched = fndecl; 815 1.1 mrg } 816 1.1 mrg 817 1.1 mrg if (!matched && !is_template && saw_template 818 1.1 mrg && !processing_template_decl && DECL_UNIQUE_FRIEND_P (function)) 819 1.1 mrg { 820 1.1 mrg /* "[if no non-template match is found,] each remaining function template 821 1.1 mrg is replaced with the specialization chosen by deduction from the 822 1.1 mrg friend declaration or discarded if deduction fails." 823 1.1 mrg 824 1.1 mrg So ask check_explicit_specialization to find a matching template. */ 825 1.1 mrg SET_DECL_IMPLICIT_INSTANTIATION (function); 826 1.1 mrg tree spec = check_explicit_specialization (DECL_NAME (function), 827 1.1 mrg function, /* tcount */0, 828 1.1 mrg /* friend flag */4, 829 1.1 mrg /* attrlist */NULL_TREE); 830 1.1 mrg if (spec != error_mark_node) 831 1.1 mrg matched = spec; 832 1.1 mrg } 833 1.1 mrg 834 1.1 mrg if (!matched) 835 1.1 mrg { 836 1.1 mrg if (!COMPLETE_TYPE_P (ctype)) 837 1.1 mrg cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function), 838 1.1 mrg function, ctype); 839 1.1 mrg else 840 1.1 mrg { 841 1.1 mrg if (DECL_CONV_FN_P (function)) 842 1.1 mrg fns = get_class_binding (ctype, conv_op_identifier); 843 1.1 mrg 844 1.1 mrg error_at (DECL_SOURCE_LOCATION (function), 845 1.1 mrg "no declaration matches %q#D", function); 846 1.1 mrg if (fns) 847 1.1 mrg print_candidates (fns); 848 1.1 mrg else if (DECL_CONV_FN_P (function)) 849 1.1 mrg inform (DECL_SOURCE_LOCATION (function), 850 1.1 mrg "no conversion operators declared"); 851 1.1 mrg else 852 1.1 mrg inform (DECL_SOURCE_LOCATION (function), 853 1.1 mrg "no functions named %qD", function); 854 1.1 mrg inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)), 855 1.1 mrg "%#qT defined here", ctype); 856 1.1 mrg } 857 1.1 mrg matched = error_mark_node; 858 1.1 mrg } 859 1.1 mrg 860 1.1 mrg if (pushed_scope) 861 1.1 mrg pop_scope (pushed_scope); 862 1.1 mrg 863 1.1 mrg return matched; 864 1.1 mrg } 865 1.1 mrg 866 1.1 mrg /* DECL is a function with vague linkage. Remember it so that at the 867 1.1 mrg end of the translation unit we can decide whether or not to emit 868 1.1 mrg it. */ 869 1.1 mrg 870 1.1 mrg void 871 1.1 mrg note_vague_linkage_fn (tree decl) 872 1.1 mrg { 873 1.1 mrg if (processing_template_decl) 874 1.1 mrg return; 875 1.1 mrg 876 1.1 mrg DECL_DEFER_OUTPUT (decl) = 1; 877 1.1 mrg vec_safe_push (deferred_fns, decl); 878 1.1 mrg } 879 1.1 mrg 880 1.1 mrg /* As above, but for variable template instantiations. */ 881 1.1 mrg 882 1.1 mrg void 883 1.1 mrg note_variable_template_instantiation (tree decl) 884 1.1 mrg { 885 1.1 mrg vec_safe_push (pending_statics, decl); 886 1.1 mrg } 887 1.1 mrg 888 1.1 mrg /* We have just processed the DECL, which is a static data member. 889 1.1 mrg The other parameters are as for cp_finish_decl. */ 890 1.1 mrg 891 1.1 mrg void 892 1.1 mrg finish_static_data_member_decl (tree decl, 893 1.1 mrg tree init, bool init_const_expr_p, 894 1.1 mrg tree asmspec_tree, 895 1.1 mrg int flags) 896 1.1 mrg { 897 1.1 mrg if (DECL_TEMPLATE_INSTANTIATED (decl)) 898 1.1 mrg /* We already needed to instantiate this, so the processing in this 899 1.1 mrg function is unnecessary/wrong. */ 900 1.1 mrg return; 901 1.1 mrg 902 1.1 mrg DECL_CONTEXT (decl) = current_class_type; 903 1.1 mrg 904 1.1 mrg /* We cannot call pushdecl here, because that would fill in the 905 1.1 mrg TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do 906 1.1 mrg the right thing, namely, to put this decl out straight away. */ 907 1.1 mrg 908 1.1 mrg if (! processing_template_decl) 909 1.1 mrg vec_safe_push (pending_statics, decl); 910 1.1 mrg 911 1.1 mrg if (LOCAL_CLASS_P (current_class_type) 912 1.1 mrg /* We already complained about the template definition. */ 913 1.1 mrg && !DECL_TEMPLATE_INSTANTIATION (decl)) 914 1.1 mrg permerror (DECL_SOURCE_LOCATION (decl), 915 1.1 mrg "local class %q#T shall not have static data member %q#D", 916 1.1 mrg current_class_type, decl); 917 1.1 mrg else 918 1.1 mrg for (tree t = current_class_type; TYPE_P (t); 919 1.1 mrg t = CP_TYPE_CONTEXT (t)) 920 1.1 mrg if (TYPE_UNNAMED_P (t)) 921 1.1 mrg { 922 1.1 mrg auto_diagnostic_group d; 923 1.1 mrg if (permerror (DECL_SOURCE_LOCATION (decl), 924 1.1 mrg "static data member %qD in unnamed class", decl)) 925 1.1 mrg inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), 926 1.1 mrg "unnamed class defined here"); 927 1.1 mrg break; 928 1.1 mrg } 929 1.1 mrg 930 1.1 mrg if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl)) 931 1.1 mrg /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P. 932 1.1 mrg Except that if decl is a template instantiation, it isn't defined until 933 1.1 mrg instantiate_decl. */; 934 1.1 mrg else 935 1.1 mrg DECL_IN_AGGR_P (decl) = 1; 936 1.1 mrg 937 1.1 mrg if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE 938 1.1 mrg && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE) 939 1.1 mrg SET_VAR_HAD_UNKNOWN_BOUND (decl); 940 1.1 mrg 941 1.1 mrg if (init) 942 1.1 mrg { 943 1.1 mrg /* Similarly to start_decl_1, we want to complete the type in order 944 1.1 mrg to do the right thing in cp_apply_type_quals_to_decl, possibly 945 1.1 mrg clear TYPE_QUAL_CONST (c++/65579). */ 946 1.1 mrg tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl)); 947 1.1 mrg cp_apply_type_quals_to_decl (cp_type_quals (type), decl); 948 1.1 mrg } 949 1.1 mrg 950 1.1 mrg cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags); 951 1.1 mrg } 952 1.1 mrg 953 1.1 mrg /* DECLARATOR and DECLSPECS correspond to a class member. The other 954 1.1 mrg parameters are as for cp_finish_decl. Return the DECL for the 955 1.1 mrg class member declared. */ 956 1.1 mrg 957 1.1 mrg tree 958 1.1 mrg grokfield (const cp_declarator *declarator, 959 1.1 mrg cp_decl_specifier_seq *declspecs, 960 1.1 mrg tree init, bool init_const_expr_p, 961 1.1 mrg tree asmspec_tree, 962 1.1 mrg tree attrlist) 963 1.1 mrg { 964 1.1 mrg tree value; 965 1.1 mrg const char *asmspec = 0; 966 1.1 mrg int flags; 967 1.1 mrg 968 1.1 mrg if (init 969 1.1 mrg && TREE_CODE (init) == TREE_LIST 970 1.1 mrg && TREE_VALUE (init) == error_mark_node 971 1.1 mrg && TREE_CHAIN (init) == NULL_TREE) 972 1.1 mrg init = NULL_TREE; 973 1.1 mrg 974 1.1 mrg int initialized; 975 1.1 mrg if (init == ridpointers[(int)RID_DELETE]) 976 1.1 mrg initialized = SD_DELETED; 977 1.1 mrg else if (init == ridpointers[(int)RID_DEFAULT]) 978 1.1 mrg initialized = SD_DEFAULTED; 979 1.1 mrg else if (init) 980 1.1 mrg initialized = SD_INITIALIZED; 981 1.1 mrg else 982 1.1 mrg initialized = SD_UNINITIALIZED; 983 1.1 mrg 984 1.1 mrg value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist); 985 1.1 mrg if (! value || value == error_mark_node) 986 1.1 mrg /* friend or constructor went bad. */ 987 1.1 mrg return error_mark_node; 988 1.1 mrg if (TREE_TYPE (value) == error_mark_node) 989 1.1 mrg return value; 990 1.1 mrg 991 1.1 mrg if (TREE_CODE (value) == TYPE_DECL && init) 992 1.1 mrg { 993 1.1 mrg error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)), 994 1.1 mrg "typedef %qD is initialized (use %qs instead)", 995 1.1 mrg value, "decltype"); 996 1.1 mrg init = NULL_TREE; 997 1.1 mrg } 998 1.1 mrg 999 1.1 mrg /* Pass friendly classes back. */ 1000 1.1 mrg if (value == void_type_node) 1001 1.1 mrg return value; 1002 1.1 mrg 1003 1.1 mrg if (DECL_NAME (value) 1004 1.1 mrg && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR) 1005 1.1 mrg { 1006 1.1 mrg error_at (declarator->id_loc, 1007 1.1 mrg "explicit template argument list not allowed"); 1008 1.1 mrg return error_mark_node; 1009 1.1 mrg } 1010 1.1 mrg 1011 1.1 mrg /* Stash away type declarations. */ 1012 1.1 mrg if (TREE_CODE (value) == TYPE_DECL) 1013 1.1 mrg { 1014 1.1 mrg DECL_NONLOCAL (value) = 1; 1015 1.1 mrg DECL_CONTEXT (value) = current_class_type; 1016 1.1 mrg 1017 1.1 mrg if (attrlist) 1018 1.1 mrg { 1019 1.1 mrg int attrflags = 0; 1020 1.1 mrg 1021 1.1 mrg /* If this is a typedef that names the class for linkage purposes 1022 1.1 mrg (7.1.3p8), apply any attributes directly to the type. */ 1023 1.1 mrg if (OVERLOAD_TYPE_P (TREE_TYPE (value)) 1024 1.1 mrg && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value)))) 1025 1.1 mrg attrflags = ATTR_FLAG_TYPE_IN_PLACE; 1026 1.1 mrg 1027 1.1 mrg cplus_decl_attributes (&value, attrlist, attrflags); 1028 1.1 mrg } 1029 1.1 mrg 1030 1.1 mrg if (decl_spec_seq_has_spec_p (declspecs, ds_typedef) 1031 1.1 mrg && TREE_TYPE (value) != error_mark_node 1032 1.1 mrg && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value) 1033 1.1 mrg set_underlying_type (value); 1034 1.1 mrg 1035 1.1 mrg /* It's important that push_template_decl below follows 1036 1.1 mrg set_underlying_type above so that the created template 1037 1.1 mrg carries the properly set type of VALUE. */ 1038 1.1 mrg if (processing_template_decl) 1039 1.1 mrg value = push_template_decl (value); 1040 1.1 mrg 1041 1.1 mrg record_locally_defined_typedef (value); 1042 1.1 mrg return value; 1043 1.1 mrg } 1044 1.1 mrg 1045 1.1 mrg int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); 1046 1.1 mrg 1047 1.1 mrg if (!friendp && DECL_IN_AGGR_P (value)) 1048 1.1 mrg { 1049 1.1 mrg error ("%qD is already defined in %qT", value, DECL_CONTEXT (value)); 1050 1.1 mrg return void_type_node; 1051 1.1 mrg } 1052 1.1 mrg 1053 1.1 mrg if (asmspec_tree && asmspec_tree != error_mark_node) 1054 1.1 mrg asmspec = TREE_STRING_POINTER (asmspec_tree); 1055 1.1 mrg 1056 1.1 mrg if (init) 1057 1.1 mrg { 1058 1.1 mrg if (TREE_CODE (value) == FUNCTION_DECL) 1059 1.1 mrg { 1060 1.1 mrg if (init == ridpointers[(int)RID_DELETE]) 1061 1.1 mrg { 1062 1.1 mrg DECL_DELETED_FN (value) = 1; 1063 1.1 mrg DECL_DECLARED_INLINE_P (value) = 1; 1064 1.1 mrg } 1065 1.1 mrg else if (init == ridpointers[(int)RID_DEFAULT]) 1066 1.1 mrg { 1067 1.1 mrg if (defaultable_fn_check (value)) 1068 1.1 mrg { 1069 1.1 mrg DECL_DEFAULTED_FN (value) = 1; 1070 1.1 mrg DECL_INITIALIZED_IN_CLASS_P (value) = 1; 1071 1.1 mrg DECL_DECLARED_INLINE_P (value) = 1; 1072 1.1 mrg /* grokfndecl set this to error_mark_node, but we want to 1073 1.1 mrg leave it unset until synthesize_method. */ 1074 1.1 mrg DECL_INITIAL (value) = NULL_TREE; 1075 1.1 mrg } 1076 1.1 mrg } 1077 1.1 mrg else if (TREE_CODE (init) == DEFERRED_PARSE) 1078 1.1 mrg error ("invalid initializer for member function %qD", value); 1079 1.1 mrg else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE) 1080 1.1 mrg { 1081 1.1 mrg if (integer_zerop (init)) 1082 1.1 mrg DECL_PURE_VIRTUAL_P (value) = 1; 1083 1.1 mrg else if (error_operand_p (init)) 1084 1.1 mrg ; /* An error has already been reported. */ 1085 1.1 mrg else 1086 1.1 mrg error ("invalid initializer for member function %qD", 1087 1.1 mrg value); 1088 1.1 mrg } 1089 1.1 mrg else 1090 1.1 mrg { 1091 1.1 mrg gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE); 1092 1.1 mrg location_t iloc 1093 1.1 mrg = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)); 1094 1.1 mrg if (friendp) 1095 1.1 mrg error_at (iloc, "initializer specified for friend " 1096 1.1 mrg "function %qD", value); 1097 1.1 mrg else 1098 1.1 mrg error_at (iloc, "initializer specified for static " 1099 1.1 mrg "member function %qD", value); 1100 1.1 mrg } 1101 1.1 mrg } 1102 1.1 mrg else if (TREE_CODE (value) == FIELD_DECL) 1103 1.1 mrg /* C++11 NSDMI, keep going. */; 1104 1.1 mrg else if (!VAR_P (value)) 1105 1.1 mrg gcc_unreachable (); 1106 1.1 mrg } 1107 1.1 mrg 1108 1.1 mrg /* Pass friend decls back. */ 1109 1.1 mrg if ((TREE_CODE (value) == FUNCTION_DECL 1110 1.1 mrg || TREE_CODE (value) == TEMPLATE_DECL) 1111 1.1 mrg && DECL_CONTEXT (value) != current_class_type) 1112 1.1 mrg { 1113 1.1 mrg if (attrlist) 1114 1.1 mrg cplus_decl_attributes (&value, attrlist, 0); 1115 1.1 mrg return value; 1116 1.1 mrg } 1117 1.1 mrg 1118 1.1 mrg /* Need to set this before push_template_decl. */ 1119 1.1 mrg if (VAR_P (value)) 1120 1.1 mrg DECL_CONTEXT (value) = current_class_type; 1121 1.1 mrg 1122 1.1 mrg if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value)) 1123 1.1 mrg { 1124 1.1 mrg value = push_template_decl (value); 1125 1.1 mrg if (error_operand_p (value)) 1126 1.1 mrg return error_mark_node; 1127 1.1 mrg } 1128 1.1 mrg 1129 1.1 mrg if (attrlist) 1130 1.1 mrg cplus_decl_attributes (&value, attrlist, 0); 1131 1.1 mrg 1132 1.1 mrg if (init && DIRECT_LIST_INIT_P (init)) 1133 1.1 mrg flags = LOOKUP_NORMAL; 1134 1.1 mrg else 1135 1.1 mrg flags = LOOKUP_IMPLICIT; 1136 1.1 mrg 1137 1.1 mrg switch (TREE_CODE (value)) 1138 1.1 mrg { 1139 1.1 mrg case VAR_DECL: 1140 1.1 mrg finish_static_data_member_decl (value, init, init_const_expr_p, 1141 1.1 mrg asmspec_tree, flags); 1142 1.1 mrg return value; 1143 1.1 mrg 1144 1.1 mrg case FIELD_DECL: 1145 1.1 mrg if (asmspec) 1146 1.1 mrg error ("%<asm%> specifiers are not permitted on non-static data members"); 1147 1.1 mrg if (DECL_INITIAL (value) == error_mark_node) 1148 1.1 mrg init = error_mark_node; 1149 1.1 mrg cp_finish_decl (value, init, /*init_const_expr_p=*/false, 1150 1.1 mrg NULL_TREE, flags); 1151 1.1 mrg DECL_IN_AGGR_P (value) = 1; 1152 1.1 mrg return value; 1153 1.1 mrg 1154 1.1 mrg case FUNCTION_DECL: 1155 1.1 mrg if (asmspec) 1156 1.1 mrg set_user_assembler_name (value, asmspec); 1157 1.1 mrg 1158 1.1 mrg cp_finish_decl (value, 1159 1.1 mrg /*init=*/NULL_TREE, 1160 1.1 mrg /*init_const_expr_p=*/false, 1161 1.1 mrg asmspec_tree, flags); 1162 1.1 mrg 1163 1.1 mrg /* Pass friends back this way. */ 1164 1.1 mrg if (DECL_UNIQUE_FRIEND_P (value)) 1165 1.1 mrg return void_type_node; 1166 1.1 mrg 1167 1.1 mrg DECL_IN_AGGR_P (value) = 1; 1168 1.1 mrg return value; 1169 1.1 mrg 1170 1.1 mrg default: 1171 1.1 mrg gcc_unreachable (); 1172 1.1 mrg } 1173 1.1 mrg return NULL_TREE; 1174 1.1 mrg } 1175 1.1 mrg 1176 1.1 mrg /* Like `grokfield', but for bitfields. 1177 1.1 mrg WIDTH is the width of the bitfield, a constant expression. 1178 1.1 mrg The other parameters are as for grokfield. */ 1179 1.1 mrg 1180 1.1 mrg tree 1181 1.1 mrg grokbitfield (const cp_declarator *declarator, 1182 1.1 mrg cp_decl_specifier_seq *declspecs, tree width, tree init, 1183 1.1 mrg tree attrlist) 1184 1.1 mrg { 1185 1.1 mrg tree value = grokdeclarator (declarator, declspecs, BITFIELD, 1186 1.1 mrg init != NULL_TREE, &attrlist); 1187 1.1 mrg 1188 1.1 mrg if (value == error_mark_node) 1189 1.1 mrg return NULL_TREE; /* friends went bad. */ 1190 1.1 mrg 1191 1.1 mrg tree type = TREE_TYPE (value); 1192 1.1 mrg if (type == error_mark_node) 1193 1.1 mrg return value; 1194 1.1 mrg 1195 1.1 mrg /* Pass friendly classes back. */ 1196 1.1 mrg if (VOID_TYPE_P (value)) 1197 1.1 mrg return void_type_node; 1198 1.1 mrg 1199 1.1 mrg if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type) 1200 1.1 mrg && (INDIRECT_TYPE_P (type) || !dependent_type_p (type))) 1201 1.1 mrg { 1202 1.1 mrg error_at (DECL_SOURCE_LOCATION (value), 1203 1.1 mrg "bit-field %qD with non-integral type %qT", 1204 1.1 mrg value, type); 1205 1.1 mrg return error_mark_node; 1206 1.1 mrg } 1207 1.1 mrg 1208 1.1 mrg if (TREE_CODE (value) == TYPE_DECL) 1209 1.1 mrg { 1210 1.1 mrg error_at (DECL_SOURCE_LOCATION (value), 1211 1.1 mrg "cannot declare %qD to be a bit-field type", value); 1212 1.1 mrg return NULL_TREE; 1213 1.1 mrg } 1214 1.1 mrg 1215 1.1 mrg /* Usually, finish_struct_1 catches bitfields with invalid types. 1216 1.1 mrg But, in the case of bitfields with function type, we confuse 1217 1.1 mrg ourselves into thinking they are member functions, so we must 1218 1.1 mrg check here. */ 1219 1.1 mrg if (TREE_CODE (value) == FUNCTION_DECL) 1220 1.1 mrg { 1221 1.1 mrg error_at (DECL_SOURCE_LOCATION (value), 1222 1.1 mrg "cannot declare bit-field %qD with function type", value); 1223 1.1 mrg return NULL_TREE; 1224 1.1 mrg } 1225 1.1 mrg 1226 1.1 mrg if (TYPE_WARN_IF_NOT_ALIGN (type)) 1227 1.1 mrg { 1228 1.1 mrg error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field " 1229 1.1 mrg "%qD with %<warn_if_not_aligned%> type", value); 1230 1.1 mrg return NULL_TREE; 1231 1.1 mrg } 1232 1.1 mrg 1233 1.1 mrg if (DECL_IN_AGGR_P (value)) 1234 1.1 mrg { 1235 1.1 mrg error ("%qD is already defined in the class %qT", value, 1236 1.1 mrg DECL_CONTEXT (value)); 1237 1.1 mrg return void_type_node; 1238 1.1 mrg } 1239 1.1 mrg 1240 1.1 mrg if (TREE_STATIC (value)) 1241 1.1 mrg { 1242 1.1 mrg error_at (DECL_SOURCE_LOCATION (value), 1243 1.1 mrg "static member %qD cannot be a bit-field", value); 1244 1.1 mrg return NULL_TREE; 1245 1.1 mrg } 1246 1.1 mrg 1247 1.1 mrg int flags = LOOKUP_IMPLICIT; 1248 1.1 mrg if (init && DIRECT_LIST_INIT_P (init)) 1249 1.1 mrg flags = LOOKUP_NORMAL; 1250 1.1 mrg cp_finish_decl (value, init, false, NULL_TREE, flags); 1251 1.1 mrg 1252 1.1 mrg if (width != error_mark_node) 1253 1.1 mrg { 1254 1.1 mrg /* The width must be an integer type. */ 1255 1.1 mrg if (!type_dependent_expression_p (width) 1256 1.1 mrg && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width))) 1257 1.1 mrg error ("width of bit-field %qD has non-integral type %qT", value, 1258 1.1 mrg TREE_TYPE (width)); 1259 1.1 mrg else if (!check_for_bare_parameter_packs (width)) 1260 1.1 mrg { 1261 1.1 mrg /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE. 1262 1.1 mrg check_bitfield_decl picks it from there later and sets DECL_SIZE 1263 1.1 mrg accordingly. */ 1264 1.1 mrg DECL_BIT_FIELD_REPRESENTATIVE (value) = width; 1265 1.1 mrg SET_DECL_C_BIT_FIELD (value); 1266 1.1 mrg } 1267 1.1 mrg } 1268 1.1 mrg 1269 1.1 mrg DECL_IN_AGGR_P (value) = 1; 1270 1.1 mrg 1271 1.1 mrg if (attrlist) 1272 1.1 mrg cplus_decl_attributes (&value, attrlist, /*flags=*/0); 1273 1.1 mrg 1274 1.1 mrg return value; 1275 1.1 mrg } 1276 1.1 mrg 1277 1.1 mrg 1278 1.1 mrg /* Returns true iff ATTR is an attribute which needs to be applied at 1280 1.1 mrg instantiation time rather than template definition time. */ 1281 1.1 mrg 1282 1.1 mrg static bool 1283 1.1 mrg is_late_template_attribute (tree attr, tree decl) 1284 1.1 mrg { 1285 1.1 mrg tree name = get_attribute_name (attr); 1286 1.1 mrg tree args = TREE_VALUE (attr); 1287 1.1 mrg const struct attribute_spec *spec = lookup_attribute_spec (name); 1288 1.1 mrg tree arg; 1289 1.1 mrg 1290 1.1 mrg if (!spec) 1291 1.1 mrg /* Unknown attribute. */ 1292 1.1 mrg return false; 1293 1.1 mrg 1294 1.1 mrg /* Attribute weak handling wants to write out assembly right away. */ 1295 1.1 mrg if (is_attribute_p ("weak", name)) 1296 1.1 mrg return true; 1297 1.1 mrg 1298 1.1 mrg /* Attributes used and unused are applied directly to typedefs for the 1299 1.1 mrg benefit of maybe_warn_unused_local_typedefs. */ 1300 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL 1301 1.1 mrg && (is_attribute_p ("unused", name) 1302 1.1 mrg || is_attribute_p ("used", name))) 1303 1.1 mrg return false; 1304 1.1 mrg 1305 1.1 mrg /* Attribute tls_model wants to modify the symtab. */ 1306 1.1 mrg if (is_attribute_p ("tls_model", name)) 1307 1.1 mrg return true; 1308 1.1 mrg 1309 1.1 mrg /* #pragma omp declare simd attribute needs to be always deferred. */ 1310 1.1 mrg if (flag_openmp 1311 1.1 mrg && is_attribute_p ("omp declare simd", name)) 1312 1.1 mrg return true; 1313 1.1 mrg 1314 1.1 mrg if (args == error_mark_node) 1315 1.1 mrg return false; 1316 1.1 mrg 1317 1.1 mrg /* An attribute pack is clearly dependent. */ 1318 1.1 mrg if (args && PACK_EXPANSION_P (args)) 1319 1.1 mrg return true; 1320 1.1 mrg 1321 1.1 mrg /* If any of the arguments are dependent expressions, we can't evaluate 1322 1.1 mrg the attribute until instantiation time. */ 1323 1.1 mrg for (arg = args; arg; arg = TREE_CHAIN (arg)) 1324 1.1 mrg { 1325 1.1 mrg tree t = TREE_VALUE (arg); 1326 1.1 mrg 1327 1.1 mrg /* If the first attribute argument is an identifier, only consider 1328 1.1 mrg second and following arguments. Attributes like mode, format, 1329 1.1 mrg cleanup and several target specific attributes aren't late 1330 1.1 mrg just because they have an IDENTIFIER_NODE as first argument. */ 1331 1.1 mrg if (arg == args && attribute_takes_identifier_p (name) 1332 1.1 mrg && identifier_p (t)) 1333 1.1 mrg continue; 1334 1.1 mrg 1335 1.1 mrg if (value_dependent_expression_p (t)) 1336 1.1 mrg return true; 1337 1.1 mrg } 1338 1.1 mrg 1339 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL 1340 1.1 mrg || TYPE_P (decl) 1341 1.1 mrg || spec->type_required) 1342 1.1 mrg { 1343 1.1 mrg tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl); 1344 1.1 mrg 1345 1.1 mrg if (!type) 1346 1.1 mrg return true; 1347 1.1 mrg 1348 1.1 mrg /* We can't apply any attributes to a completely unknown type until 1349 1.1 mrg instantiation time. */ 1350 1.1 mrg enum tree_code code = TREE_CODE (type); 1351 1.1 mrg if (code == TEMPLATE_TYPE_PARM 1352 1.1 mrg || code == BOUND_TEMPLATE_TEMPLATE_PARM 1353 1.1 mrg || code == TYPENAME_TYPE) 1354 1.1 mrg return true; 1355 1.1 mrg /* Also defer most attributes on dependent types. This is not 1356 1.1 mrg necessary in all cases, but is the better default. */ 1357 1.1 mrg else if (dependent_type_p (type) 1358 1.1 mrg /* But some attributes specifically apply to templates. */ 1359 1.1 mrg && !is_attribute_p ("abi_tag", name) 1360 1.1 mrg && !is_attribute_p ("deprecated", name) 1361 1.1 mrg && !is_attribute_p ("unavailable", name) 1362 1.1 mrg && !is_attribute_p ("visibility", name)) 1363 1.1 mrg return true; 1364 1.1 mrg else 1365 1.1 mrg return false; 1366 1.1 mrg } 1367 1.1 mrg else 1368 1.1 mrg return false; 1369 1.1 mrg } 1370 1.1 mrg 1371 1.1 mrg /* ATTR_P is a list of attributes. Remove any attributes which need to be 1372 1.1 mrg applied at instantiation time and return them. If IS_DEPENDENT is true, 1373 1.1 mrg the declaration itself is dependent, so all attributes should be applied 1374 1.1 mrg at instantiation time. */ 1375 1.1 mrg 1376 1.1 mrg tree 1377 1.1 mrg splice_template_attributes (tree *attr_p, tree decl) 1378 1.1 mrg { 1379 1.1 mrg tree *p = attr_p; 1380 1.1 mrg tree late_attrs = NULL_TREE; 1381 1.1 mrg tree *q = &late_attrs; 1382 1.1 mrg 1383 1.1 mrg if (!p || *p == error_mark_node) 1384 1.1 mrg return NULL_TREE; 1385 1.1 mrg 1386 1.1 mrg for (; *p; ) 1387 1.1 mrg { 1388 1.1 mrg if (is_late_template_attribute (*p, decl)) 1389 1.1 mrg { 1390 1.1 mrg ATTR_IS_DEPENDENT (*p) = 1; 1391 1.1 mrg *q = *p; 1392 1.1 mrg *p = TREE_CHAIN (*p); 1393 1.1 mrg q = &TREE_CHAIN (*q); 1394 1.1 mrg *q = NULL_TREE; 1395 1.1 mrg } 1396 1.1 mrg else 1397 1.1 mrg p = &TREE_CHAIN (*p); 1398 1.1 mrg } 1399 1.1 mrg 1400 1.1 mrg return late_attrs; 1401 1.1 mrg } 1402 1.1 mrg 1403 1.1 mrg /* Attach any LATE_ATTRS to DECL_P, after the non-dependent attributes have 1404 1.1 mrg been applied by a previous call to decl_attributes. */ 1405 1.1 mrg 1406 1.1 mrg static void 1407 1.1 mrg save_template_attributes (tree late_attrs, tree *decl_p, int flags) 1408 1.1 mrg { 1409 1.1 mrg tree *q; 1410 1.1 mrg 1411 1.1 mrg if (!late_attrs) 1412 1.1 mrg return; 1413 1.1 mrg 1414 1.1 mrg if (DECL_P (*decl_p)) 1415 1.1 mrg q = &DECL_ATTRIBUTES (*decl_p); 1416 1.1 mrg else 1417 1.1 mrg q = &TYPE_ATTRIBUTES (*decl_p); 1418 1.1 mrg 1419 1.1 mrg tree old_attrs = *q; 1420 1.1 mrg 1421 1.1 mrg /* Place the late attributes at the beginning of the attribute 1422 1.1 mrg list. */ 1423 1.1 mrg late_attrs = chainon (late_attrs, *q); 1424 1.1 mrg if (*q != late_attrs 1425 1.1 mrg && !DECL_P (*decl_p) 1426 1.1 mrg && !(flags & ATTR_FLAG_TYPE_IN_PLACE)) 1427 1.1 mrg { 1428 1.1 mrg if (!dependent_type_p (*decl_p)) 1429 1.1 mrg *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs); 1430 1.1 mrg else 1431 1.1 mrg { 1432 1.1 mrg *decl_p = build_variant_type_copy (*decl_p); 1433 1.1 mrg TYPE_ATTRIBUTES (*decl_p) = late_attrs; 1434 1.1 mrg } 1435 1.1 mrg } 1436 1.1 mrg else 1437 1.1 mrg *q = late_attrs; 1438 1.1 mrg 1439 1.1 mrg if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p)) 1440 1.1 mrg { 1441 1.1 mrg /* We've added new attributes directly to the main variant, so 1442 1.1 mrg now we need to update all of the other variants to include 1443 1.1 mrg these new attributes. */ 1444 1.1 mrg tree variant; 1445 1.1 mrg for (variant = TYPE_NEXT_VARIANT (*decl_p); variant; 1446 1.1 mrg variant = TYPE_NEXT_VARIANT (variant)) 1447 1.1 mrg { 1448 1.1 mrg gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs); 1449 1.1 mrg TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p); 1450 1.1 mrg } 1451 1.1 mrg } 1452 1.1 mrg } 1453 1.1 mrg 1454 1.1 mrg /* True if ATTRS contains any dependent attributes that affect type 1455 1.1 mrg identity. */ 1456 1.1 mrg 1457 1.1 mrg bool 1458 1.1 mrg any_dependent_type_attributes_p (tree attrs) 1459 1.1 mrg { 1460 1.1 mrg for (tree a = attrs; a; a = TREE_CHAIN (a)) 1461 1.1 mrg if (ATTR_IS_DEPENDENT (a)) 1462 1.1 mrg { 1463 1.1 mrg const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a)); 1464 1.1 mrg if (as && as->affects_type_identity) 1465 1.1 mrg return true; 1466 1.1 mrg } 1467 1.1 mrg return false; 1468 1.1 mrg } 1469 1.1 mrg 1470 1.1 mrg /* Return true iff ATTRS are acceptable attributes to be applied in-place 1471 1.1 mrg to a typedef which gives a previously unnamed class or enum a name for 1472 1.1 mrg linkage purposes. */ 1473 1.1 mrg 1474 1.1 mrg bool 1475 1.1 mrg attributes_naming_typedef_ok (tree attrs) 1476 1.1 mrg { 1477 1.1 mrg for (; attrs; attrs = TREE_CHAIN (attrs)) 1478 1.1 mrg { 1479 1.1 mrg tree name = get_attribute_name (attrs); 1480 1.1 mrg if (is_attribute_p ("vector_size", name)) 1481 1.1 mrg return false; 1482 1.1 mrg } 1483 1.1 mrg return true; 1484 1.1 mrg } 1485 1.1 mrg 1486 1.1 mrg /* Like reconstruct_complex_type, but handle also template trees. */ 1487 1.1 mrg 1488 1.1 mrg tree 1489 1.1 mrg cp_reconstruct_complex_type (tree type, tree bottom) 1490 1.1 mrg { 1491 1.1 mrg tree inner, outer; 1492 1.1 mrg 1493 1.1 mrg if (TYPE_PTR_P (type)) 1494 1.1 mrg { 1495 1.1 mrg inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1496 1.1 mrg outer = build_pointer_type_for_mode (inner, TYPE_MODE (type), 1497 1.1 mrg TYPE_REF_CAN_ALIAS_ALL (type)); 1498 1.1 mrg } 1499 1.1 mrg else if (TYPE_REF_P (type)) 1500 1.1 mrg { 1501 1.1 mrg inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1502 1.1 mrg outer = build_reference_type_for_mode (inner, TYPE_MODE (type), 1503 1.1 mrg TYPE_REF_CAN_ALIAS_ALL (type)); 1504 1.1 mrg } 1505 1.1 mrg else if (TREE_CODE (type) == ARRAY_TYPE) 1506 1.1 mrg { 1507 1.1 mrg inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1508 1.1 mrg outer = build_cplus_array_type (inner, TYPE_DOMAIN (type)); 1509 1.1 mrg /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the 1510 1.1 mrg element type qualification will be handled by the recursive 1511 1.1 mrg cp_reconstruct_complex_type call and cp_build_qualified_type 1512 1.1 mrg for ARRAY_TYPEs changes the element type. */ 1513 1.1 mrg return outer; 1514 1.1 mrg } 1515 1.1 mrg else if (TREE_CODE (type) == FUNCTION_TYPE) 1516 1.1 mrg { 1517 1.1 mrg inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1518 1.1 mrg outer = build_function_type (inner, TYPE_ARG_TYPES (type)); 1519 1.1 mrg outer = apply_memfn_quals (outer, type_memfn_quals (type)); 1520 1.1 mrg } 1521 1.1 mrg else if (TREE_CODE (type) == METHOD_TYPE) 1522 1.1 mrg { 1523 1.1 mrg inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1524 1.1 mrg /* The build_method_type_directly() routine prepends 'this' to argument list, 1525 1.1 mrg so we must compensate by getting rid of it. */ 1526 1.1 mrg outer 1527 1.1 mrg = build_method_type_directly 1528 1.1 mrg (class_of_this_parm (type), inner, 1529 1.1 mrg TREE_CHAIN (TYPE_ARG_TYPES (type))); 1530 1.1 mrg } 1531 1.1 mrg else if (TREE_CODE (type) == OFFSET_TYPE) 1532 1.1 mrg { 1533 1.1 mrg inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1534 1.1 mrg outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner); 1535 1.1 mrg } 1536 1.1 mrg else 1537 1.1 mrg return bottom; 1538 1.1 mrg 1539 1.1 mrg if (TYPE_ATTRIBUTES (type)) 1540 1.1 mrg outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type)); 1541 1.1 mrg outer = cp_build_qualified_type (outer, cp_type_quals (type)); 1542 1.1 mrg outer = cxx_copy_lang_qualifiers (outer, type); 1543 1.1 mrg 1544 1.1 mrg return outer; 1545 1.1 mrg } 1546 1.1 mrg 1547 1.1 mrg /* Replaces any constexpr expression that may be into the attributes 1548 1.1 mrg arguments with their reduced value. */ 1549 1.1 mrg 1550 1.1 mrg void 1551 1.1 mrg cp_check_const_attributes (tree attributes) 1552 1.1 mrg { 1553 1.1 mrg if (attributes == error_mark_node) 1554 1.1 mrg return; 1555 1.1 mrg 1556 1.1 mrg tree attr; 1557 1.1 mrg for (attr = attributes; attr; attr = TREE_CHAIN (attr)) 1558 1.1 mrg { 1559 1.1 mrg tree arg; 1560 1.1 mrg /* As we implement alignas using gnu::aligned attribute and 1561 1.1 mrg alignas argument is a constant expression, force manifestly 1562 1.1 mrg constant evaluation of aligned attribute argument. */ 1563 1.1 mrg bool manifestly_const_eval 1564 1.1 mrg = is_attribute_p ("aligned", get_attribute_name (attr)); 1565 1.1 mrg for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST; 1566 1.1 mrg arg = TREE_CHAIN (arg)) 1567 1.1 mrg { 1568 1.1 mrg tree expr = TREE_VALUE (arg); 1569 1.1 mrg if (EXPR_P (expr)) 1570 1.1 mrg TREE_VALUE (arg) 1571 1.1 mrg = fold_non_dependent_expr (expr, tf_warning_or_error, 1572 1.1 mrg manifestly_const_eval); 1573 1.1 mrg } 1574 1.1 mrg } 1575 1.1 mrg } 1576 1.1 mrg 1577 1.1 mrg /* Return true if TYPE is an OpenMP mappable type. 1578 1.1 mrg If NOTES is non-zero, emit a note message for each problem. */ 1579 1.1 mrg static bool 1580 1.1 mrg cp_omp_mappable_type_1 (tree type, bool notes) 1581 1.1 mrg { 1582 1.1 mrg bool result = true; 1583 1.1 mrg 1584 1.1 mrg /* Mappable type has to be complete. */ 1585 1.1 mrg if (type == error_mark_node || !COMPLETE_TYPE_P (type)) 1586 1.1 mrg { 1587 1.1 mrg if (notes && type != error_mark_node) 1588 1.1 mrg { 1589 1.1 mrg tree decl = TYPE_MAIN_DECL (type); 1590 1.1 mrg inform ((decl ? DECL_SOURCE_LOCATION (decl) : input_location), 1591 1.1 mrg "incomplete type %qT is not mappable", type); 1592 1.1 mrg } 1593 1.1 mrg result = false; 1594 1.1 mrg } 1595 1.1 mrg /* Arrays have mappable type if the elements have mappable type. */ 1596 1.1 mrg while (TREE_CODE (type) == ARRAY_TYPE) 1597 1.1 mrg type = TREE_TYPE (type); 1598 1.1 mrg /* All data members must be non-static. */ 1599 1.1 mrg if (CLASS_TYPE_P (type)) 1600 1.1 mrg { 1601 1.1 mrg tree field; 1602 1.1 mrg for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 1603 1.1 mrg if (VAR_P (field)) 1604 1.1 mrg { 1605 1.1 mrg if (notes) 1606 1.1 mrg inform (DECL_SOURCE_LOCATION (field), 1607 1.1 mrg "static field %qD is not mappable", field); 1608 1.1 mrg result = false; 1609 1.1 mrg } 1610 1.1 mrg /* All fields must have mappable types. */ 1611 1.1 mrg else if (TREE_CODE (field) == FIELD_DECL 1612 1.1 mrg && !cp_omp_mappable_type_1 (TREE_TYPE (field), notes)) 1613 1.1 mrg result = false; 1614 1.1 mrg } 1615 1.1 mrg return result; 1616 1.1 mrg } 1617 1.1 mrg 1618 1.1 mrg /* Return true if TYPE is an OpenMP mappable type. */ 1619 1.1 mrg bool 1620 1.1 mrg cp_omp_mappable_type (tree type) 1621 1.1 mrg { 1622 1.1 mrg return cp_omp_mappable_type_1 (type, false); 1623 1.1 mrg } 1624 1.1 mrg 1625 1.1 mrg /* Return true if TYPE is an OpenMP mappable type. 1626 1.1 mrg Emit an error messages if not. */ 1627 1.1 mrg bool 1628 1.1 mrg cp_omp_emit_unmappable_type_notes (tree type) 1629 1.1 mrg { 1630 1.1 mrg return cp_omp_mappable_type_1 (type, true); 1631 1.1 mrg } 1632 1.1 mrg 1633 1.1 mrg /* Return the last pushed declaration for the symbol DECL or NULL 1634 1.1 mrg when no such declaration exists. */ 1635 1.1 mrg 1636 1.1 mrg static tree 1637 1.1 mrg find_last_decl (tree decl) 1638 1.1 mrg { 1639 1.1 mrg tree last_decl = NULL_TREE; 1640 1.1 mrg 1641 1.1 mrg if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE) 1642 1.1 mrg { 1643 1.1 mrg /* Look up the declaration in its scope. */ 1644 1.1 mrg tree pushed_scope = NULL_TREE; 1645 1.1 mrg if (tree ctype = DECL_CONTEXT (decl)) 1646 1.1 mrg pushed_scope = push_scope (ctype); 1647 1.1 mrg 1648 1.1 mrg last_decl = lookup_name (name); 1649 1.1 mrg 1650 1.1 mrg if (pushed_scope) 1651 1.1 mrg pop_scope (pushed_scope); 1652 1.1 mrg 1653 1.1 mrg /* The declaration may be a member conversion operator 1654 1.1 mrg or a bunch of overfloads (handle the latter below). */ 1655 1.1 mrg if (last_decl && BASELINK_P (last_decl)) 1656 1.1 mrg last_decl = BASELINK_FUNCTIONS (last_decl); 1657 1.1 mrg } 1658 1.1 mrg 1659 1.1 mrg if (!last_decl) 1660 1.1 mrg return NULL_TREE; 1661 1.1 mrg 1662 1.1 mrg if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD) 1663 1.1 mrg { 1664 1.1 mrg /* A set of overloads of the same function. */ 1665 1.1 mrg for (lkp_iterator iter (last_decl); iter; ++iter) 1666 1.1 mrg { 1667 1.1 mrg if (TREE_CODE (*iter) == OVERLOAD) 1668 1.1 mrg continue; 1669 1.1 mrg 1670 1.1 mrg tree d = *iter; 1671 1.1 mrg 1672 1.1 mrg /* We can't compare versions in the middle of processing the 1673 1.1 mrg attribute that has the version. */ 1674 1.1 mrg if (TREE_CODE (d) == FUNCTION_DECL 1675 1.1 mrg && DECL_FUNCTION_VERSIONED (d)) 1676 1.1 mrg return NULL_TREE; 1677 1.1 mrg 1678 1.1 mrg if (decls_match (decl, d, /*record_decls=*/false)) 1679 1.1 mrg return d; 1680 1.1 mrg } 1681 1.1 mrg return NULL_TREE; 1682 1.1 mrg } 1683 1.1 mrg 1684 1.1 mrg return NULL_TREE; 1685 1.1 mrg } 1686 1.1 mrg 1687 1.1 mrg /* Like decl_attributes, but handle C++ complexity. */ 1688 1.1 mrg 1689 1.1 mrg void 1690 1.1 mrg cplus_decl_attributes (tree *decl, tree attributes, int flags) 1691 1.1 mrg { 1692 1.1 mrg if (*decl == NULL_TREE || *decl == void_type_node 1693 1.1 mrg || *decl == error_mark_node || attributes == error_mark_node) 1694 1.1 mrg return; 1695 1.1 mrg 1696 1.1 mrg /* Add implicit "omp declare target" attribute if requested. */ 1697 1.1 mrg if (vec_safe_length (scope_chain->omp_declare_target_attribute) 1698 1.1 mrg && ((VAR_P (*decl) 1699 1.1 mrg && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl))) 1700 1.1 mrg || TREE_CODE (*decl) == FUNCTION_DECL)) 1701 1.1 mrg { 1702 1.1 mrg if (VAR_P (*decl) 1703 1.1 mrg && DECL_CLASS_SCOPE_P (*decl)) 1704 1.1 mrg error ("%q+D static data member inside of declare target directive", 1705 1.1 mrg *decl); 1706 1.1 mrg else if (VAR_P (*decl) 1707 1.1 mrg && (processing_template_decl 1708 1.1 mrg || !cp_omp_mappable_type (TREE_TYPE (*decl)))) 1709 1.1 mrg attributes = tree_cons (get_identifier ("omp declare target implicit"), 1710 1.1 mrg NULL_TREE, attributes); 1711 1.1 mrg else 1712 1.1 mrg { 1713 1.1 mrg attributes = tree_cons (get_identifier ("omp declare target"), 1714 1.1 mrg NULL_TREE, attributes); 1715 1.1 mrg attributes = tree_cons (get_identifier ("omp declare target block"), 1716 1.1 mrg NULL_TREE, attributes); 1717 1.1 mrg } 1718 1.1 mrg } 1719 1.1 mrg 1720 1.1 mrg tree late_attrs = NULL_TREE; 1721 1.1 mrg if (processing_template_decl) 1722 1.1 mrg { 1723 1.1 mrg if (check_for_bare_parameter_packs (attributes)) 1724 1.1 mrg return; 1725 1.1 mrg late_attrs = splice_template_attributes (&attributes, *decl); 1726 1.1 mrg } 1727 1.1 mrg 1728 1.1 mrg cp_check_const_attributes (attributes); 1729 1.1 mrg 1730 1.1 mrg if (flag_openmp || flag_openmp_simd) 1731 1.1 mrg { 1732 1.1 mrg bool diagnosed = false; 1733 1.1 mrg for (tree *pa = &attributes; *pa; ) 1734 1.1 mrg { 1735 1.1 mrg if (get_attribute_namespace (*pa) == omp_identifier) 1736 1.1 mrg { 1737 1.1 mrg tree name = get_attribute_name (*pa); 1738 1.1 mrg if (is_attribute_p ("directive", name) 1739 1.1 mrg || is_attribute_p ("sequence", name)) 1740 1.1 mrg { 1741 1.1 mrg if (!diagnosed) 1742 1.1 mrg { 1743 1.1 mrg error ("%<omp::%E%> not allowed to be specified in this " 1744 1.1 mrg "context", name); 1745 1.1 mrg diagnosed = true; 1746 1.1 mrg } 1747 1.1 mrg *pa = TREE_CHAIN (*pa); 1748 1.1 mrg continue; 1749 1.1 mrg } 1750 1.1 mrg } 1751 1.1 mrg pa = &TREE_CHAIN (*pa); 1752 1.1 mrg } 1753 1.1 mrg } 1754 1.1 mrg 1755 1.1 mrg if (TREE_CODE (*decl) == TEMPLATE_DECL) 1756 1.1 mrg decl = &DECL_TEMPLATE_RESULT (*decl); 1757 1.1 mrg 1758 1.1 mrg if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl))) 1759 1.1 mrg { 1760 1.1 mrg attributes 1761 1.1 mrg = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT); 1762 1.1 mrg decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)), 1763 1.1 mrg attributes, flags); 1764 1.1 mrg } 1765 1.1 mrg else 1766 1.1 mrg { 1767 1.1 mrg tree last_decl = find_last_decl (*decl); 1768 1.1 mrg decl_attributes (decl, attributes, flags, last_decl); 1769 1.1 mrg } 1770 1.1 mrg 1771 1.1 mrg if (late_attrs) 1772 1.1 mrg save_template_attributes (late_attrs, decl, flags); 1773 1.1 mrg 1774 1.1 mrg /* Propagate deprecation out to the template. */ 1775 1.1 mrg if (TREE_DEPRECATED (*decl)) 1776 1.1 mrg if (tree ti = get_template_info (*decl)) 1777 1.1 mrg { 1778 1.1 mrg tree tmpl = TI_TEMPLATE (ti); 1779 1.1 mrg tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl) 1780 1.1 mrg : DECL_TEMPLATE_RESULT (tmpl)); 1781 1.1 mrg if (*decl == pattern) 1782 1.1 mrg TREE_DEPRECATED (tmpl) = true; 1783 1.1 mrg } 1784 1.1 mrg 1785 1.1 mrg /* Likewise, propagate unavailability out to the template. */ 1786 1.1 mrg if (TREE_UNAVAILABLE (*decl)) 1787 1.1 mrg if (tree ti = get_template_info (*decl)) 1788 1.1 mrg { 1789 1.1 mrg tree tmpl = TI_TEMPLATE (ti); 1790 1.1 mrg tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl) 1791 1.1 mrg : DECL_TEMPLATE_RESULT (tmpl)); 1792 1.1 mrg if (*decl == pattern) 1793 1.1 mrg TREE_UNAVAILABLE (tmpl) = true; 1794 1.1 mrg } 1795 1.1 mrg } 1796 1.1 mrg 1797 1.1 mrg /* Walks through the namespace- or function-scope anonymous union 1799 1.1 mrg OBJECT, with the indicated TYPE, building appropriate VAR_DECLs. 1800 1.1 mrg Returns one of the fields for use in the mangled name. */ 1801 1.1 mrg 1802 1.1 mrg static tree 1803 1.1 mrg build_anon_union_vars (tree type, tree object) 1804 1.1 mrg { 1805 1.1 mrg tree main_decl = NULL_TREE; 1806 1.1 mrg tree field; 1807 1.1 mrg 1808 1.1 mrg /* Rather than write the code to handle the non-union case, 1809 1.1 mrg just give an error. */ 1810 1.1 mrg if (TREE_CODE (type) != UNION_TYPE) 1811 1.1 mrg { 1812 1.1 mrg error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), 1813 1.1 mrg "anonymous struct not inside named type"); 1814 1.1 mrg return error_mark_node; 1815 1.1 mrg } 1816 1.1 mrg 1817 1.1 mrg for (field = TYPE_FIELDS (type); 1818 1.1 mrg field != NULL_TREE; 1819 1.1 mrg field = DECL_CHAIN (field)) 1820 1.1 mrg { 1821 1.1 mrg tree decl; 1822 1.1 mrg tree ref; 1823 1.1 mrg 1824 1.1 mrg if (DECL_ARTIFICIAL (field)) 1825 1.1 mrg continue; 1826 1.1 mrg if (TREE_CODE (field) != FIELD_DECL) 1827 1.1 mrg { 1828 1.1 mrg permerror (DECL_SOURCE_LOCATION (field), 1829 1.1 mrg "%q#D invalid; an anonymous union can only " 1830 1.1 mrg "have non-static data members", field); 1831 1.1 mrg continue; 1832 1.1 mrg } 1833 1.1 mrg 1834 1.1 mrg if (TREE_PRIVATE (field)) 1835 1.1 mrg permerror (DECL_SOURCE_LOCATION (field), 1836 1.1 mrg "private member %q#D in anonymous union", field); 1837 1.1 mrg else if (TREE_PROTECTED (field)) 1838 1.1 mrg permerror (DECL_SOURCE_LOCATION (field), 1839 1.1 mrg "protected member %q#D in anonymous union", field); 1840 1.1 mrg 1841 1.1 mrg if (processing_template_decl) 1842 1.1 mrg ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object, 1843 1.1 mrg DECL_NAME (field), NULL_TREE); 1844 1.1 mrg else 1845 1.1 mrg ref = build_class_member_access_expr (object, field, NULL_TREE, 1846 1.1 mrg false, tf_warning_or_error); 1847 1.1 mrg 1848 1.1 mrg if (DECL_NAME (field)) 1849 1.1 mrg { 1850 1.1 mrg tree base; 1851 1.1 mrg 1852 1.1 mrg decl = build_decl (input_location, 1853 1.1 mrg VAR_DECL, DECL_NAME (field), TREE_TYPE (field)); 1854 1.1 mrg DECL_ANON_UNION_VAR_P (decl) = 1; 1855 1.1 mrg DECL_ARTIFICIAL (decl) = 1; 1856 1.1 mrg 1857 1.1 mrg base = get_base_address (object); 1858 1.1 mrg TREE_PUBLIC (decl) = TREE_PUBLIC (base); 1859 1.1 mrg TREE_STATIC (decl) = TREE_STATIC (base); 1860 1.1 mrg DECL_EXTERNAL (decl) = DECL_EXTERNAL (base); 1861 1.1 mrg 1862 1.1 mrg SET_DECL_VALUE_EXPR (decl, ref); 1863 1.1 mrg DECL_HAS_VALUE_EXPR_P (decl) = 1; 1864 1.1 mrg 1865 1.1 mrg decl = pushdecl (decl); 1866 1.1 mrg } 1867 1.1 mrg else if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 1868 1.1 mrg decl = build_anon_union_vars (TREE_TYPE (field), ref); 1869 1.1 mrg else 1870 1.1 mrg decl = 0; 1871 1.1 mrg 1872 1.1 mrg if (main_decl == NULL_TREE) 1873 1.1 mrg main_decl = decl; 1874 1.1 mrg } 1875 1.1 mrg 1876 1.1 mrg return main_decl; 1877 1.1 mrg } 1878 1.1 mrg 1879 1.1 mrg /* Finish off the processing of a UNION_TYPE structure. If the union is an 1880 1.1 mrg anonymous union, then all members must be laid out together. PUBLIC_P 1881 1.1 mrg is nonzero if this union is not declared static. */ 1882 1.1 mrg 1883 1.1 mrg void 1884 1.1 mrg finish_anon_union (tree anon_union_decl) 1885 1.1 mrg { 1886 1.1 mrg tree type; 1887 1.1 mrg tree main_decl; 1888 1.1 mrg bool public_p; 1889 1.1 mrg 1890 1.1 mrg if (anon_union_decl == error_mark_node) 1891 1.1 mrg return; 1892 1.1 mrg 1893 1.1 mrg type = TREE_TYPE (anon_union_decl); 1894 1.1 mrg public_p = TREE_PUBLIC (anon_union_decl); 1895 1.1 mrg 1896 1.1 mrg /* The VAR_DECL's context is the same as the TYPE's context. */ 1897 1.1 mrg DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type)); 1898 1.1 mrg 1899 1.1 mrg if (TYPE_FIELDS (type) == NULL_TREE) 1900 1.1 mrg return; 1901 1.1 mrg 1902 1.1 mrg if (public_p) 1903 1.1 mrg { 1904 1.1 mrg error ("namespace-scope anonymous aggregates must be static"); 1905 1.1 mrg return; 1906 1.1 mrg } 1907 1.1 mrg 1908 1.1 mrg main_decl = build_anon_union_vars (type, anon_union_decl); 1909 1.1 mrg if (main_decl == error_mark_node) 1910 1.1 mrg return; 1911 1.1 mrg if (main_decl == NULL_TREE) 1912 1.1 mrg { 1913 1.1 mrg pedwarn (input_location, 0, "anonymous union with no members"); 1914 1.1 mrg return; 1915 1.1 mrg } 1916 1.1 mrg 1917 1.1 mrg if (!processing_template_decl) 1918 1.1 mrg { 1919 1.1 mrg /* Use main_decl to set the mangled name. */ 1920 1.1 mrg DECL_NAME (anon_union_decl) = DECL_NAME (main_decl); 1921 1.1 mrg maybe_commonize_var (anon_union_decl); 1922 1.1 mrg if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl)) 1923 1.1 mrg { 1924 1.1 mrg if (DECL_DISCRIMINATOR_P (anon_union_decl)) 1925 1.1 mrg determine_local_discriminator (anon_union_decl); 1926 1.1 mrg mangle_decl (anon_union_decl); 1927 1.1 mrg } 1928 1.1 mrg DECL_NAME (anon_union_decl) = NULL_TREE; 1929 1.1 mrg } 1930 1.1 mrg 1931 1.1 mrg pushdecl (anon_union_decl); 1932 1.1 mrg cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0); 1933 1.1 mrg } 1934 1.1 mrg 1935 1.1 mrg /* Auxiliary functions to make type signatures for 1937 1.1 mrg `operator new' and `operator delete' correspond to 1938 1.1 mrg what compiler will be expecting. */ 1939 1.1 mrg 1940 1.1 mrg tree 1941 1.1 mrg coerce_new_type (tree type, location_t loc) 1942 1.1 mrg { 1943 1.1 mrg int e = 0; 1944 1.1 mrg tree args = TYPE_ARG_TYPES (type); 1945 1.1 mrg 1946 1.1 mrg gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); 1947 1.1 mrg 1948 1.1 mrg if (!same_type_p (TREE_TYPE (type), ptr_type_node)) 1949 1.1 mrg { 1950 1.1 mrg e = 1; 1951 1.1 mrg error_at (loc, "%<operator new%> must return type %qT", 1952 1.1 mrg ptr_type_node); 1953 1.1 mrg } 1954 1.1 mrg 1955 1.1 mrg if (args && args != void_list_node) 1956 1.1 mrg { 1957 1.1 mrg if (TREE_PURPOSE (args)) 1958 1.1 mrg { 1959 1.1 mrg /* [basic.stc.dynamic.allocation] 1960 1.1 mrg 1961 1.1 mrg The first parameter shall not have an associated default 1962 1.1 mrg argument. */ 1963 1.1 mrg error_at (loc, "the first parameter of %<operator new%> cannot " 1964 1.1 mrg "have a default argument"); 1965 1.1 mrg /* Throw away the default argument. */ 1966 1.1 mrg TREE_PURPOSE (args) = NULL_TREE; 1967 1.1 mrg } 1968 1.1 mrg 1969 1.1 mrg if (!same_type_p (TREE_VALUE (args), size_type_node)) 1970 1.1 mrg { 1971 1.1 mrg e = 2; 1972 1.1 mrg args = TREE_CHAIN (args); 1973 1.1 mrg } 1974 1.1 mrg } 1975 1.1 mrg else 1976 1.1 mrg e = 2; 1977 1.1 mrg 1978 1.1 mrg if (e == 2) 1979 1.1 mrg permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) " 1980 1.1 mrg "as first parameter", size_type_node); 1981 1.1 mrg 1982 1.1 mrg switch (e) 1983 1.1 mrg { 1984 1.1 mrg case 2: 1985 1.1 mrg args = tree_cons (NULL_TREE, size_type_node, args); 1986 1.1 mrg /* Fall through. */ 1987 1.1 mrg case 1: 1988 1.1 mrg type = (cxx_copy_lang_qualifiers 1989 1.1 mrg (build_function_type (ptr_type_node, args), 1990 1.1 mrg type)); 1991 1.1 mrg /* Fall through. */ 1992 1.1 mrg default:; 1993 1.1 mrg } 1994 1.1 mrg return type; 1995 1.1 mrg } 1996 1.1 mrg 1997 1.1 mrg void 1998 1.1 mrg coerce_delete_type (tree decl, location_t loc) 1999 1.1 mrg { 2000 1.1 mrg int e = 0; 2001 1.1 mrg tree type = TREE_TYPE (decl); 2002 1.1 mrg tree args = TYPE_ARG_TYPES (type); 2003 1.1 mrg 2004 1.1 mrg gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); 2005 1.1 mrg 2006 1.1 mrg if (!same_type_p (TREE_TYPE (type), void_type_node)) 2007 1.1 mrg { 2008 1.1 mrg e = 1; 2009 1.1 mrg error_at (loc, "%<operator delete%> must return type %qT", 2010 1.1 mrg void_type_node); 2011 1.1 mrg } 2012 1.1 mrg 2013 1.1 mrg tree ptrtype = ptr_type_node; 2014 1.1 mrg if (destroying_delete_p (decl)) 2015 1.1 mrg { 2016 1.1 mrg if (DECL_CLASS_SCOPE_P (decl)) 2017 1.1 mrg /* If the function is a destroying operator delete declared in class 2018 1.1 mrg type C, the type of its first parameter shall be C*. */ 2019 1.1 mrg ptrtype = build_pointer_type (DECL_CONTEXT (decl)); 2020 1.1 mrg else 2021 1.1 mrg /* A destroying operator delete shall be a class member function named 2022 1.1 mrg operator delete. */ 2023 1.1 mrg error_at (loc, 2024 1.1 mrg "destroying %<operator delete%> must be a member function"); 2025 1.1 mrg const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl)); 2026 1.1 mrg if (op->flags & OVL_OP_FLAG_VEC) 2027 1.1 mrg error_at (loc, "%<operator delete[]%> cannot be a destroying delete"); 2028 1.1 mrg if (!usual_deallocation_fn_p (decl)) 2029 1.1 mrg error_at (loc, "destroying %<operator delete%> must be a usual " 2030 1.1 mrg "deallocation function"); 2031 1.1 mrg } 2032 1.1 mrg 2033 1.1 mrg if (!args || args == void_list_node 2034 1.1 mrg || !same_type_p (TREE_VALUE (args), ptrtype)) 2035 1.1 mrg { 2036 1.1 mrg e = 2; 2037 1.1 mrg if (args && args != void_list_node) 2038 1.1 mrg args = TREE_CHAIN (args); 2039 1.1 mrg error_at (loc, "%<operator delete%> takes type %qT as first parameter", 2040 1.1 mrg ptrtype); 2041 1.1 mrg } 2042 1.1 mrg switch (e) 2043 1.1 mrg { 2044 1.1 mrg case 2: 2045 1.1 mrg args = tree_cons (NULL_TREE, ptrtype, args); 2046 1.1 mrg /* Fall through. */ 2047 1.1 mrg case 1: 2048 1.1 mrg type = (cxx_copy_lang_qualifiers 2049 1.1 mrg (build_function_type (void_type_node, args), 2050 1.1 mrg type)); 2051 1.1 mrg /* Fall through. */ 2052 1.1 mrg default:; 2053 1.1 mrg } 2054 1.1 mrg 2055 1.1 mrg TREE_TYPE (decl) = type; 2056 1.1 mrg } 2057 1.1 mrg 2058 1.1 mrg /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable 2060 1.1 mrg and mark them as needed. */ 2061 1.1 mrg 2062 1.1 mrg static void 2063 1.1 mrg mark_vtable_entries (tree decl, vec<tree> &consteval_vtables) 2064 1.1 mrg { 2065 1.1 mrg /* It's OK for the vtable to refer to deprecated virtual functions. */ 2066 1.1 mrg auto du = make_temp_override (deprecated_state, 2067 1.1 mrg UNAVAILABLE_DEPRECATED_SUPPRESS); 2068 1.1 mrg 2069 1.1 mrg bool consteval_seen = false; 2070 1.1 mrg 2071 1.1 mrg for (auto &e: CONSTRUCTOR_ELTS (DECL_INITIAL (decl))) 2072 1.1 mrg { 2073 1.1 mrg tree fnaddr = e.value; 2074 1.1 mrg 2075 1.1 mrg STRIP_NOPS (fnaddr); 2076 1.1 mrg 2077 1.1 mrg if (TREE_CODE (fnaddr) != ADDR_EXPR 2078 1.1 mrg && TREE_CODE (fnaddr) != FDESC_EXPR) 2079 1.1 mrg /* This entry is an offset: a virtual base class offset, a 2080 1.1 mrg virtual call offset, an RTTI offset, etc. */ 2081 1.1 mrg continue; 2082 1.1 mrg 2083 1.1 mrg tree fn = TREE_OPERAND (fnaddr, 0); 2084 1.1 mrg if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn)) 2085 1.1 mrg { 2086 1.1 mrg if (!consteval_seen) 2087 1.1 mrg { 2088 1.1 mrg consteval_seen = true; 2089 1.1 mrg consteval_vtables.safe_push (decl); 2090 1.1 mrg } 2091 1.1 mrg continue; 2092 1.1 mrg } 2093 1.1 mrg TREE_ADDRESSABLE (fn) = 1; 2094 1.1 mrg /* When we don't have vcall offsets, we output thunks whenever 2095 1.1 mrg we output the vtables that contain them. With vcall offsets, 2096 1.1 mrg we know all the thunks we'll need when we emit a virtual 2097 1.1 mrg function, so we emit the thunks there instead. */ 2098 1.1 mrg if (DECL_THUNK_P (fn)) 2099 1.1 mrg use_thunk (fn, /*emit_p=*/0); 2100 1.1 mrg /* Set the location, as marking the function could cause 2101 1.1 mrg instantiation. We do not need to preserve the incoming 2102 1.1 mrg location, as we're called from c_parse_final_cleanups, which 2103 1.1 mrg takes care of that. */ 2104 1.1 mrg input_location = DECL_SOURCE_LOCATION (fn); 2105 1.1 mrg mark_used (fn); 2106 1.1 mrg } 2107 1.1 mrg } 2108 1.1 mrg 2109 1.1 mrg /* Replace any consteval functions in vtables with null pointers. */ 2110 1.1 mrg 2111 1.1 mrg static void 2112 1.1 mrg clear_consteval_vfns (vec<tree> &consteval_vtables) 2113 1.1 mrg { 2114 1.1 mrg for (tree vtable : consteval_vtables) 2115 1.1 mrg for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable))) 2116 1.1 mrg { 2117 1.1 mrg tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false); 2118 1.1 mrg if (fn && DECL_IMMEDIATE_FUNCTION_P (fn)) 2119 1.1 mrg elt.value = build_zero_cst (vtable_entry_type); 2120 1.1 mrg } 2121 1.1 mrg } 2122 1.1 mrg 2123 1.1 mrg /* Adjust the TLS model on variable DECL if need be, typically after 2124 1.1 mrg the linkage of DECL has been modified. */ 2125 1.1 mrg 2126 1.1 mrg static void 2127 1.1 mrg adjust_var_decl_tls_model (tree decl) 2128 1.1 mrg { 2129 1.1 mrg if (CP_DECL_THREAD_LOCAL_P (decl) 2130 1.1 mrg && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl))) 2131 1.1 mrg set_decl_tls_model (decl, decl_default_tls_model (decl)); 2132 1.1 mrg } 2133 1.1 mrg 2134 1.1 mrg /* Set DECL up to have the closest approximation of "initialized common" 2135 1.1 mrg linkage available. */ 2136 1.1 mrg 2137 1.1 mrg void 2138 1.1 mrg comdat_linkage (tree decl) 2139 1.1 mrg { 2140 1.1 mrg if (flag_weak) 2141 1.1 mrg make_decl_one_only (decl, cxx_comdat_group (decl)); 2142 1.1 mrg else if (TREE_CODE (decl) == FUNCTION_DECL 2143 1.1 mrg || (VAR_P (decl) && DECL_ARTIFICIAL (decl))) 2144 1.1 mrg /* We can just emit function and compiler-generated variables 2145 1.1 mrg statically; having multiple copies is (for the most part) only 2146 1.1 mrg a waste of space. 2147 1.1 mrg 2148 1.1 mrg There are two correctness issues, however: the address of a 2149 1.1 mrg template instantiation with external linkage should be the 2150 1.1 mrg same, independent of what translation unit asks for the 2151 1.1 mrg address, and this will not hold when we emit multiple copies of 2152 1.1 mrg the function. However, there's little else we can do. 2153 1.1 mrg 2154 1.1 mrg Also, by default, the typeinfo implementation assumes that 2155 1.1 mrg there will be only one copy of the string used as the name for 2156 1.1 mrg each type. Therefore, if weak symbols are unavailable, the 2157 1.1 mrg run-time library should perform a more conservative check; it 2158 1.1 mrg should perform a string comparison, rather than an address 2159 1.1 mrg comparison. */ 2160 1.1 mrg TREE_PUBLIC (decl) = 0; 2161 1.1 mrg else 2162 1.1 mrg { 2163 1.1 mrg /* Static data member template instantiations, however, cannot 2164 1.1 mrg have multiple copies. */ 2165 1.1 mrg if (DECL_INITIAL (decl) == 0 2166 1.1 mrg || DECL_INITIAL (decl) == error_mark_node) 2167 1.1 mrg DECL_COMMON (decl) = 1; 2168 1.1 mrg else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))) 2169 1.1 mrg { 2170 1.1 mrg DECL_COMMON (decl) = 1; 2171 1.1 mrg DECL_INITIAL (decl) = error_mark_node; 2172 1.1 mrg } 2173 1.1 mrg else if (!DECL_EXPLICIT_INSTANTIATION (decl)) 2174 1.1 mrg { 2175 1.1 mrg /* We can't do anything useful; leave vars for explicit 2176 1.1 mrg instantiation. */ 2177 1.1 mrg DECL_EXTERNAL (decl) = 1; 2178 1.1 mrg DECL_NOT_REALLY_EXTERN (decl) = 0; 2179 1.1 mrg } 2180 1.1 mrg } 2181 1.1 mrg 2182 1.1 mrg if (TREE_PUBLIC (decl)) 2183 1.1 mrg DECL_COMDAT (decl) = 1; 2184 1.1 mrg 2185 1.1 mrg if (VAR_P (decl)) 2186 1.1 mrg adjust_var_decl_tls_model (decl); 2187 1.1 mrg } 2188 1.1 mrg 2189 1.1 mrg /* For win32 we also want to put explicit instantiations in 2190 1.1 mrg linkonce sections, so that they will be merged with implicit 2191 1.1 mrg instantiations; otherwise we get duplicate symbol errors. 2192 1.1 mrg For Darwin we do not want explicit instantiations to be 2193 1.1 mrg linkonce. */ 2194 1.1 mrg 2195 1.1 mrg void 2196 1.1 mrg maybe_make_one_only (tree decl) 2197 1.1 mrg { 2198 1.1 mrg /* We used to say that this was not necessary on targets that support weak 2199 1.1 mrg symbols, because the implicit instantiations will defer to the explicit 2200 1.1 mrg one. However, that's not actually the case in SVR4; a strong definition 2201 1.1 mrg after a weak one is an error. Also, not making explicit 2202 1.1 mrg instantiations one_only means that we can end up with two copies of 2203 1.1 mrg some template instantiations. */ 2204 1.1 mrg if (! flag_weak) 2205 1.1 mrg return; 2206 1.1 mrg 2207 1.1 mrg /* We can't set DECL_COMDAT on functions, or cp_finish_file will think 2208 1.1 mrg we can get away with not emitting them if they aren't used. We need 2209 1.1 mrg to for variables so that cp_finish_decl will update their linkage, 2210 1.1 mrg because their DECL_INITIAL may not have been set properly yet. */ 2211 1.1 mrg 2212 1.1 mrg if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC 2213 1.1 mrg || (! DECL_EXPLICIT_INSTANTIATION (decl) 2214 1.1 mrg && ! DECL_TEMPLATE_SPECIALIZATION (decl))) 2215 1.1 mrg { 2216 1.1 mrg make_decl_one_only (decl, cxx_comdat_group (decl)); 2217 1.1 mrg 2218 1.1 mrg if (VAR_P (decl)) 2219 1.1 mrg { 2220 1.1 mrg varpool_node *node = varpool_node::get_create (decl); 2221 1.1 mrg DECL_COMDAT (decl) = 1; 2222 1.1 mrg /* Mark it needed so we don't forget to emit it. */ 2223 1.1 mrg node->forced_by_abi = true; 2224 1.1 mrg TREE_USED (decl) = 1; 2225 1.1 mrg 2226 1.1 mrg adjust_var_decl_tls_model (decl); 2227 1.1 mrg } 2228 1.1 mrg } 2229 1.1 mrg } 2230 1.1 mrg 2231 1.1 mrg /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage. 2232 1.1 mrg This predicate will give the right answer during parsing of the 2233 1.1 mrg function, which other tests may not. */ 2234 1.1 mrg 2235 1.1 mrg bool 2236 1.1 mrg vague_linkage_p (tree decl) 2237 1.1 mrg { 2238 1.1 mrg if (!TREE_PUBLIC (decl)) 2239 1.1 mrg { 2240 1.1 mrg /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the 2241 1.1 mrg maybe-in-charge 'tor variants; in that case we need to check one of 2242 1.1 mrg the "clones" for the real linkage. But only in that case; before 2243 1.1 mrg maybe_clone_body we haven't yet copied the linkage to the clones. */ 2244 1.1 mrg if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl) 2245 1.1 mrg && !DECL_ABSTRACT_P (decl) 2246 1.1 mrg && DECL_CHAIN (decl) 2247 1.1 mrg && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl))) 2248 1.1 mrg return vague_linkage_p (DECL_CHAIN (decl)); 2249 1.1 mrg 2250 1.1 mrg gcc_checking_assert (!DECL_COMDAT (decl)); 2251 1.1 mrg return false; 2252 1.1 mrg } 2253 1.1 mrg /* Unfortunately, import_export_decl has not always been called 2254 1.1 mrg before the function is processed, so we cannot simply check 2255 1.1 mrg DECL_COMDAT. */ 2256 1.1 mrg if (DECL_COMDAT (decl) 2257 1.1 mrg || (TREE_CODE (decl) == FUNCTION_DECL 2258 1.1 mrg && DECL_DECLARED_INLINE_P (decl)) 2259 1.1 mrg || (DECL_LANG_SPECIFIC (decl) 2260 1.1 mrg && DECL_TEMPLATE_INSTANTIATION (decl)) 2261 1.1 mrg || (VAR_P (decl) && DECL_INLINE_VAR_P (decl))) 2262 1.1 mrg return true; 2263 1.1 mrg else if (DECL_FUNCTION_SCOPE_P (decl)) 2264 1.1 mrg /* A local static in an inline effectively has vague linkage. */ 2265 1.1 mrg return (TREE_STATIC (decl) 2266 1.1 mrg && vague_linkage_p (DECL_CONTEXT (decl))); 2267 1.1 mrg else 2268 1.1 mrg return false; 2269 1.1 mrg } 2270 1.1 mrg 2271 1.1 mrg /* Determine whether or not we want to specifically import or export CTYPE, 2272 1.1 mrg using various heuristics. */ 2273 1.1 mrg 2274 1.1 mrg static void 2275 1.1 mrg import_export_class (tree ctype) 2276 1.1 mrg { 2277 1.1 mrg /* -1 for imported, 1 for exported. */ 2278 1.1 mrg int import_export = 0; 2279 1.1 mrg 2280 1.1 mrg /* It only makes sense to call this function at EOF. The reason is 2281 1.1 mrg that this function looks at whether or not the first non-inline 2282 1.1 mrg non-abstract virtual member function has been defined in this 2283 1.1 mrg translation unit. But, we can't possibly know that until we've 2284 1.1 mrg seen the entire translation unit. */ 2285 1.1 mrg gcc_assert (at_eof); 2286 1.1 mrg 2287 1.1 mrg if (CLASSTYPE_INTERFACE_KNOWN (ctype)) 2288 1.1 mrg return; 2289 1.1 mrg 2290 1.1 mrg /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface, 2291 1.1 mrg we will have CLASSTYPE_INTERFACE_ONLY set but not 2292 1.1 mrg CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this 2293 1.1 mrg heuristic because someone will supply a #pragma implementation 2294 1.1 mrg elsewhere, and deducing it here would produce a conflict. */ 2295 1.1 mrg if (CLASSTYPE_INTERFACE_ONLY (ctype)) 2296 1.1 mrg return; 2297 1.1 mrg 2298 1.1 mrg if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype))) 2299 1.1 mrg import_export = -1; 2300 1.1 mrg else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype))) 2301 1.1 mrg import_export = 1; 2302 1.1 mrg else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype) 2303 1.1 mrg && !flag_implicit_templates) 2304 1.1 mrg /* For a template class, without -fimplicit-templates, check the 2305 1.1 mrg repository. If the virtual table is assigned to this 2306 1.1 mrg translation unit, then export the class; otherwise, import 2307 1.1 mrg it. */ 2308 1.1 mrg import_export = -1; 2309 1.1 mrg else if (TYPE_POLYMORPHIC_P (ctype)) 2310 1.1 mrg { 2311 1.1 mrg /* The ABI specifies that the virtual table and associated 2312 1.1 mrg information are emitted with the key method, if any. */ 2313 1.1 mrg tree method = CLASSTYPE_KEY_METHOD (ctype); 2314 1.1 mrg /* If weak symbol support is not available, then we must be 2315 1.1 mrg careful not to emit the vtable when the key function is 2316 1.1 mrg inline. An inline function can be defined in multiple 2317 1.1 mrg translation units. If we were to emit the vtable in each 2318 1.1 mrg translation unit containing a definition, we would get 2319 1.1 mrg multiple definition errors at link-time. */ 2320 1.1 mrg if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method))) 2321 1.1 mrg import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1); 2322 1.1 mrg } 2323 1.1 mrg 2324 1.1 mrg /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing 2325 1.1 mrg a definition anywhere else. */ 2326 1.1 mrg if (MULTIPLE_SYMBOL_SPACES && import_export == -1) 2327 1.1 mrg import_export = 0; 2328 1.1 mrg 2329 1.1 mrg /* Allow back ends the chance to overrule the decision. */ 2330 1.1 mrg if (targetm.cxx.import_export_class) 2331 1.1 mrg import_export = targetm.cxx.import_export_class (ctype, import_export); 2332 1.1 mrg 2333 1.1 mrg if (import_export) 2334 1.1 mrg { 2335 1.1 mrg SET_CLASSTYPE_INTERFACE_KNOWN (ctype); 2336 1.1 mrg CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0); 2337 1.1 mrg } 2338 1.1 mrg } 2339 1.1 mrg 2340 1.1 mrg /* Return true if VAR has already been provided to the back end; in that 2341 1.1 mrg case VAR should not be modified further by the front end. */ 2342 1.1 mrg static bool 2343 1.1 mrg var_finalized_p (tree var) 2344 1.1 mrg { 2345 1.1 mrg return varpool_node::get_create (var)->definition; 2346 1.1 mrg } 2347 1.1 mrg 2348 1.1 mrg /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason, 2349 1.1 mrg must be emitted in this translation unit. Mark it as such. */ 2350 1.1 mrg 2351 1.1 mrg void 2352 1.1 mrg mark_needed (tree decl) 2353 1.1 mrg { 2354 1.1 mrg TREE_USED (decl) = 1; 2355 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL) 2356 1.1 mrg { 2357 1.1 mrg /* Extern inline functions don't become needed when referenced. 2358 1.1 mrg If we know a method will be emitted in other TU and no new 2359 1.1 mrg functions can be marked reachable, just use the external 2360 1.1 mrg definition. */ 2361 1.1 mrg struct cgraph_node *node = cgraph_node::get_create (decl); 2362 1.1 mrg node->forced_by_abi = true; 2363 1.1 mrg 2364 1.1 mrg /* #pragma interface can call mark_needed for 2365 1.1 mrg maybe-in-charge 'tors; mark the clones as well. */ 2366 1.1 mrg tree clone; 2367 1.1 mrg FOR_EACH_CLONE (clone, decl) 2368 1.1 mrg mark_needed (clone); 2369 1.1 mrg } 2370 1.1 mrg else if (VAR_P (decl)) 2371 1.1 mrg { 2372 1.1 mrg varpool_node *node = varpool_node::get_create (decl); 2373 1.1 mrg /* C++ frontend use mark_decl_references to force COMDAT variables 2374 1.1 mrg to be output that might appear dead otherwise. */ 2375 1.1 mrg node->forced_by_abi = true; 2376 1.1 mrg } 2377 1.1 mrg } 2378 1.1 mrg 2379 1.1 mrg /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function 2380 1.1 mrg returns true if a definition of this entity should be provided in 2381 1.1 mrg this object file. Callers use this function to determine whether 2382 1.1 mrg or not to let the back end know that a definition of DECL is 2383 1.1 mrg available in this translation unit. */ 2384 1.1 mrg 2385 1.1 mrg bool 2386 1.1 mrg decl_needed_p (tree decl) 2387 1.1 mrg { 2388 1.1 mrg gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); 2389 1.1 mrg /* This function should only be called at the end of the translation 2390 1.1 mrg unit. We cannot be sure of whether or not something will be 2391 1.1 mrg COMDAT until that point. */ 2392 1.1 mrg gcc_assert (at_eof); 2393 1.1 mrg 2394 1.1 mrg /* All entities with external linkage that are not COMDAT/EXTERN should be 2395 1.1 mrg emitted; they may be referred to from other object files. */ 2396 1.1 mrg if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl)) 2397 1.1 mrg return true; 2398 1.1 mrg 2399 1.1 mrg /* Functions marked "dllexport" must be emitted so that they are 2400 1.1 mrg visible to other DLLs. */ 2401 1.1 mrg if (flag_keep_inline_dllexport 2402 1.1 mrg && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))) 2403 1.1 mrg return true; 2404 1.1 mrg 2405 1.1 mrg /* When not optimizing, do not bother to produce definitions for extern 2406 1.1 mrg symbols. */ 2407 1.1 mrg if (DECL_REALLY_EXTERN (decl) 2408 1.1 mrg && ((TREE_CODE (decl) != FUNCTION_DECL 2409 1.1 mrg && !optimize) 2410 1.1 mrg || (TREE_CODE (decl) == FUNCTION_DECL 2411 1.1 mrg && !opt_for_fn (decl, optimize))) 2412 1.1 mrg && !lookup_attribute ("always_inline", decl)) 2413 1.1 mrg return false; 2414 1.1 mrg 2415 1.1 mrg /* If this entity was used, let the back end see it; it will decide 2416 1.1 mrg whether or not to emit it into the object file. */ 2417 1.1 mrg if (TREE_USED (decl)) 2418 1.1 mrg return true; 2419 1.1 mrg 2420 1.1 mrg /* Virtual functions might be needed for devirtualization. */ 2421 1.1 mrg if (flag_devirtualize 2422 1.1 mrg && TREE_CODE (decl) == FUNCTION_DECL 2423 1.1 mrg && DECL_VIRTUAL_P (decl)) 2424 1.1 mrg return true; 2425 1.1 mrg 2426 1.1 mrg /* Otherwise, DECL does not need to be emitted -- yet. A subsequent 2427 1.1 mrg reference to DECL might cause it to be emitted later. */ 2428 1.1 mrg return false; 2429 1.1 mrg } 2430 1.1 mrg 2431 1.1 mrg /* If necessary, write out the vtables for the dynamic class CTYPE. 2432 1.1 mrg Returns true if any vtables were emitted. */ 2433 1.1 mrg 2434 1.1 mrg static bool 2435 1.1 mrg maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables) 2436 1.1 mrg { 2437 1.1 mrg tree vtbl; 2438 1.1 mrg tree primary_vtbl; 2439 1.1 mrg int needed = 0; 2440 1.1 mrg varpool_node *current = NULL, *last = NULL; 2441 1.1 mrg 2442 1.1 mrg /* If the vtables for this class have already been emitted there is 2443 1.1 mrg nothing more to do. */ 2444 1.1 mrg primary_vtbl = CLASSTYPE_VTABLES (ctype); 2445 1.1 mrg if (var_finalized_p (primary_vtbl)) 2446 1.1 mrg return false; 2447 1.1 mrg /* Ignore dummy vtables made by get_vtable_decl. */ 2448 1.1 mrg if (TREE_TYPE (primary_vtbl) == void_type_node) 2449 1.1 mrg return false; 2450 1.1 mrg 2451 1.1 mrg /* On some targets, we cannot determine the key method until the end 2452 1.1 mrg of the translation unit -- which is when this function is 2453 1.1 mrg called. */ 2454 1.1 mrg if (!targetm.cxx.key_method_may_be_inline ()) 2455 1.1 mrg determine_key_method (ctype); 2456 1.1 mrg 2457 1.1 mrg /* See if any of the vtables are needed. */ 2458 1.1 mrg for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl)) 2459 1.1 mrg { 2460 1.1 mrg import_export_decl (vtbl); 2461 1.1 mrg if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl)) 2462 1.1 mrg needed = 1; 2463 1.1 mrg } 2464 1.1 mrg if (!needed) 2465 1.1 mrg { 2466 1.1 mrg /* If the references to this class' vtables are optimized away, 2467 1.1 mrg still emit the appropriate debugging information. See 2468 1.1 mrg dfs_debug_mark. */ 2469 1.1 mrg if (DECL_COMDAT (primary_vtbl) 2470 1.1 mrg && CLASSTYPE_DEBUG_REQUESTED (ctype)) 2471 1.1 mrg note_debug_info_needed (ctype); 2472 1.1 mrg return false; 2473 1.1 mrg } 2474 1.1 mrg 2475 1.1 mrg /* The ABI requires that we emit all of the vtables if we emit any 2476 1.1 mrg of them. */ 2477 1.1 mrg for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl)) 2478 1.1 mrg { 2479 1.1 mrg /* Mark entities references from the virtual table as used. */ 2480 1.1 mrg mark_vtable_entries (vtbl, consteval_vtables); 2481 1.1 mrg 2482 1.1 mrg if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0) 2483 1.1 mrg { 2484 1.1 mrg vec<tree, va_gc> *cleanups = NULL; 2485 1.1 mrg tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups, 2486 1.1 mrg LOOKUP_NORMAL); 2487 1.1 mrg 2488 1.1 mrg /* It had better be all done at compile-time. */ 2489 1.1 mrg gcc_assert (!expr && !cleanups); 2490 1.1 mrg } 2491 1.1 mrg 2492 1.1 mrg /* Write it out. */ 2493 1.1 mrg DECL_EXTERNAL (vtbl) = 0; 2494 1.1 mrg rest_of_decl_compilation (vtbl, 1, 1); 2495 1.1 mrg 2496 1.1 mrg /* Because we're only doing syntax-checking, we'll never end up 2497 1.1 mrg actually marking the variable as written. */ 2498 1.1 mrg if (flag_syntax_only) 2499 1.1 mrg TREE_ASM_WRITTEN (vtbl) = 1; 2500 1.1 mrg else if (DECL_ONE_ONLY (vtbl)) 2501 1.1 mrg { 2502 1.1 mrg current = varpool_node::get_create (vtbl); 2503 1.1 mrg if (last) 2504 1.1 mrg current->add_to_same_comdat_group (last); 2505 1.1 mrg last = current; 2506 1.1 mrg } 2507 1.1 mrg } 2508 1.1 mrg 2509 1.1 mrg /* For abstract classes, the destructor has been removed from the 2510 1.1 mrg vtable (in class.cc's build_vtbl_initializer). For a compiler- 2511 1.1 mrg generated destructor, it hence might not have been generated in 2512 1.1 mrg this translation unit - and with '#pragma interface' it might 2513 1.1 mrg never get generated. */ 2514 1.1 mrg if (CLASSTYPE_PURE_VIRTUALS (ctype) 2515 1.1 mrg && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) 2516 1.1 mrg && !CLASSTYPE_LAZY_DESTRUCTOR (ctype) 2517 1.1 mrg && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype))) 2518 1.1 mrg note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype)); 2519 1.1 mrg 2520 1.1 mrg /* Since we're writing out the vtable here, also write the debug 2521 1.1 mrg info. */ 2522 1.1 mrg note_debug_info_needed (ctype); 2523 1.1 mrg 2524 1.1 mrg return true; 2525 1.1 mrg } 2526 1.1 mrg 2527 1.1 mrg /* A special return value from type_visibility meaning internal 2528 1.1 mrg linkage. */ 2529 1.1 mrg 2530 1.1 mrg enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 }; 2531 1.1 mrg 2532 1.1 mrg static int expr_visibility (tree); 2533 1.1 mrg static int type_visibility (tree); 2534 1.1 mrg 2535 1.1 mrg /* walk_tree helper function for type_visibility. */ 2536 1.1 mrg 2537 1.1 mrg static tree 2538 1.1 mrg min_vis_r (tree *tp, int *walk_subtrees, void *data) 2539 1.1 mrg { 2540 1.1 mrg int *vis_p = (int *)data; 2541 1.1 mrg int this_vis = VISIBILITY_DEFAULT; 2542 1.1 mrg if (! TYPE_P (*tp)) 2543 1.1 mrg *walk_subtrees = 0; 2544 1.1 mrg else if (OVERLOAD_TYPE_P (*tp) 2545 1.1 mrg && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp))) 2546 1.1 mrg { 2547 1.1 mrg this_vis = VISIBILITY_ANON; 2548 1.1 mrg *walk_subtrees = 0; 2549 1.1 mrg } 2550 1.1 mrg else if (CLASS_TYPE_P (*tp)) 2551 1.1 mrg { 2552 1.1 mrg this_vis = CLASSTYPE_VISIBILITY (*tp); 2553 1.1 mrg *walk_subtrees = 0; 2554 1.1 mrg } 2555 1.1 mrg else if (TREE_CODE (*tp) == ARRAY_TYPE 2556 1.1 mrg && uses_template_parms (TYPE_DOMAIN (*tp))) 2557 1.1 mrg this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp))); 2558 1.1 mrg 2559 1.1 mrg if (this_vis > *vis_p) 2560 1.1 mrg *vis_p = this_vis; 2561 1.1 mrg 2562 1.1 mrg /* Tell cp_walk_subtrees to look through typedefs. */ 2563 1.1 mrg if (*walk_subtrees == 1) 2564 1.1 mrg *walk_subtrees = 2; 2565 1.1 mrg 2566 1.1 mrg return NULL; 2567 1.1 mrg } 2568 1.1 mrg 2569 1.1 mrg /* walk_tree helper function for expr_visibility. */ 2570 1.1 mrg 2571 1.1 mrg static tree 2572 1.1 mrg min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data) 2573 1.1 mrg { 2574 1.1 mrg int *vis_p = (int *)data; 2575 1.1 mrg int tpvis = VISIBILITY_DEFAULT; 2576 1.1 mrg 2577 1.1 mrg switch (TREE_CODE (*tp)) 2578 1.1 mrg { 2579 1.1 mrg case CAST_EXPR: 2580 1.1 mrg case IMPLICIT_CONV_EXPR: 2581 1.1 mrg case STATIC_CAST_EXPR: 2582 1.1 mrg case REINTERPRET_CAST_EXPR: 2583 1.1 mrg case CONST_CAST_EXPR: 2584 1.1 mrg case DYNAMIC_CAST_EXPR: 2585 1.1 mrg case NEW_EXPR: 2586 1.1 mrg case CONSTRUCTOR: 2587 1.1 mrg case LAMBDA_EXPR: 2588 1.1 mrg tpvis = type_visibility (TREE_TYPE (*tp)); 2589 1.1 mrg break; 2590 1.1 mrg 2591 1.1 mrg case VAR_DECL: 2592 1.1 mrg case FUNCTION_DECL: 2593 1.1 mrg if (! TREE_PUBLIC (*tp)) 2594 1.1 mrg tpvis = VISIBILITY_ANON; 2595 1.1 mrg else 2596 1.1 mrg tpvis = DECL_VISIBILITY (*tp); 2597 1.1 mrg break; 2598 1.1 mrg 2599 1.1 mrg default: 2600 1.1 mrg break; 2601 1.1 mrg } 2602 1.1 mrg 2603 1.1 mrg if (tpvis > *vis_p) 2604 1.1 mrg *vis_p = tpvis; 2605 1.1 mrg 2606 1.1 mrg return NULL_TREE; 2607 1.1 mrg } 2608 1.1 mrg 2609 1.1 mrg /* Returns the visibility of TYPE, which is the minimum visibility of its 2610 1.1 mrg component types. */ 2611 1.1 mrg 2612 1.1 mrg static int 2613 1.1 mrg type_visibility (tree type) 2614 1.1 mrg { 2615 1.1 mrg int vis = VISIBILITY_DEFAULT; 2616 1.1 mrg cp_walk_tree_without_duplicates (&type, min_vis_r, &vis); 2617 1.1 mrg return vis; 2618 1.1 mrg } 2619 1.1 mrg 2620 1.1 mrg /* Returns the visibility of an expression EXPR that appears in the signature 2621 1.1 mrg of a function template, which is the minimum visibility of names that appear 2622 1.1 mrg in its mangling. */ 2623 1.1 mrg 2624 1.1 mrg static int 2625 1.1 mrg expr_visibility (tree expr) 2626 1.1 mrg { 2627 1.1 mrg int vis = VISIBILITY_DEFAULT; 2628 1.1 mrg cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis); 2629 1.1 mrg return vis; 2630 1.1 mrg } 2631 1.1 mrg 2632 1.1 mrg /* Limit the visibility of DECL to VISIBILITY, if not explicitly 2633 1.1 mrg specified (or if VISIBILITY is static). If TMPL is true, this 2634 1.1 mrg constraint is for a template argument, and takes precedence 2635 1.1 mrg over explicitly-specified visibility on the template. */ 2636 1.1 mrg 2637 1.1 mrg static void 2638 1.1 mrg constrain_visibility (tree decl, int visibility, bool tmpl) 2639 1.1 mrg { 2640 1.1 mrg if (visibility == VISIBILITY_ANON) 2641 1.1 mrg { 2642 1.1 mrg /* extern "C" declarations aren't affected by the anonymous 2643 1.1 mrg namespace. */ 2644 1.1 mrg if (!DECL_EXTERN_C_P (decl)) 2645 1.1 mrg { 2646 1.1 mrg TREE_PUBLIC (decl) = 0; 2647 1.1 mrg DECL_WEAK (decl) = 0; 2648 1.1 mrg DECL_COMMON (decl) = 0; 2649 1.1 mrg DECL_COMDAT (decl) = false; 2650 1.1 mrg if (VAR_OR_FUNCTION_DECL_P (decl)) 2651 1.1 mrg { 2652 1.1 mrg struct symtab_node *snode = symtab_node::get (decl); 2653 1.1 mrg 2654 1.1 mrg if (snode) 2655 1.1 mrg snode->set_comdat_group (NULL); 2656 1.1 mrg } 2657 1.1 mrg DECL_INTERFACE_KNOWN (decl) = 1; 2658 1.1 mrg if (DECL_LANG_SPECIFIC (decl)) 2659 1.1 mrg DECL_NOT_REALLY_EXTERN (decl) = 1; 2660 1.1 mrg } 2661 1.1 mrg } 2662 1.1 mrg else if (visibility > DECL_VISIBILITY (decl) 2663 1.1 mrg && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl))) 2664 1.1 mrg { 2665 1.1 mrg DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility; 2666 1.1 mrg /* This visibility was not specified. */ 2667 1.1 mrg DECL_VISIBILITY_SPECIFIED (decl) = false; 2668 1.1 mrg } 2669 1.1 mrg } 2670 1.1 mrg 2671 1.1 mrg /* Constrain the visibility of DECL based on the visibility of its template 2672 1.1 mrg arguments. */ 2673 1.1 mrg 2674 1.1 mrg static void 2675 1.1 mrg constrain_visibility_for_template (tree decl, tree targs) 2676 1.1 mrg { 2677 1.1 mrg /* If this is a template instantiation, check the innermost 2678 1.1 mrg template args for visibility constraints. The outer template 2679 1.1 mrg args are covered by the class check. */ 2680 1.1 mrg tree args = INNERMOST_TEMPLATE_ARGS (targs); 2681 1.1 mrg int i; 2682 1.1 mrg for (i = TREE_VEC_LENGTH (args); i > 0; --i) 2683 1.1 mrg { 2684 1.1 mrg int vis = 0; 2685 1.1 mrg 2686 1.1 mrg tree arg = TREE_VEC_ELT (args, i-1); 2687 1.1 mrg if (TYPE_P (arg)) 2688 1.1 mrg vis = type_visibility (arg); 2689 1.1 mrg else 2690 1.1 mrg vis = expr_visibility (arg); 2691 1.1 mrg if (vis) 2692 1.1 mrg constrain_visibility (decl, vis, true); 2693 1.1 mrg } 2694 1.1 mrg } 2695 1.1 mrg 2696 1.1 mrg /* Like c_determine_visibility, but with additional C++-specific 2697 1.1 mrg behavior. 2698 1.1 mrg 2699 1.1 mrg Function-scope entities can rely on the function's visibility because 2700 1.1 mrg it is set in start_preparsed_function. 2701 1.1 mrg 2702 1.1 mrg Class-scope entities cannot rely on the class's visibility until the end 2703 1.1 mrg of the enclosing class definition. 2704 1.1 mrg 2705 1.1 mrg Note that because namespaces have multiple independent definitions, 2706 1.1 mrg namespace visibility is handled elsewhere using the #pragma visibility 2707 1.1 mrg machinery rather than by decorating the namespace declaration. 2708 1.1 mrg 2709 1.1 mrg The goal is for constraints from the type to give a diagnostic, and 2710 1.1 mrg other constraints to be applied silently. */ 2711 1.1 mrg 2712 1.1 mrg void 2713 1.1 mrg determine_visibility (tree decl) 2714 1.1 mrg { 2715 1.1 mrg /* Remember that all decls get VISIBILITY_DEFAULT when built. */ 2716 1.1 mrg 2717 1.1 mrg /* Only relevant for names with external linkage. */ 2718 1.1 mrg if (!TREE_PUBLIC (decl)) 2719 1.1 mrg return; 2720 1.1 mrg 2721 1.1 mrg /* Cloned constructors and destructors get the same visibility as 2722 1.1 mrg the underlying function. That should be set up in 2723 1.1 mrg maybe_clone_body. */ 2724 1.1 mrg gcc_assert (!DECL_CLONED_FUNCTION_P (decl)); 2725 1.1 mrg 2726 1.1 mrg bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl); 2727 1.1 mrg enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl); 2728 1.1 mrg 2729 1.1 mrg /* The decl may be a template instantiation, which could influence 2730 1.1 mrg visibilty. */ 2731 1.1 mrg tree template_decl = NULL_TREE; 2732 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL) 2733 1.1 mrg { 2734 1.1 mrg if (CLASS_TYPE_P (TREE_TYPE (decl))) 2735 1.1 mrg { 2736 1.1 mrg if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl))) 2737 1.1 mrg template_decl = decl; 2738 1.1 mrg } 2739 1.1 mrg else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl))) 2740 1.1 mrg template_decl = decl; 2741 1.1 mrg } 2742 1.1 mrg else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 2743 1.1 mrg template_decl = decl; 2744 1.1 mrg 2745 1.1 mrg if (TREE_CODE (decl) == TYPE_DECL 2746 1.1 mrg && LAMBDA_TYPE_P (TREE_TYPE (decl)) 2747 1.1 mrg && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node) 2748 1.1 mrg if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))) 2749 1.1 mrg { 2750 1.1 mrg /* The lambda's visibility is limited by that of its extra 2751 1.1 mrg scope. */ 2752 1.1 mrg int vis = 0; 2753 1.1 mrg if (TYPE_P (extra)) 2754 1.1 mrg vis = type_visibility (extra); 2755 1.1 mrg else 2756 1.1 mrg vis = expr_visibility (extra); 2757 1.1 mrg constrain_visibility (decl, vis, false); 2758 1.1 mrg } 2759 1.1 mrg 2760 1.1 mrg /* If DECL is a member of a class, visibility specifiers on the 2761 1.1 mrg class can influence the visibility of the DECL. */ 2762 1.1 mrg tree class_type = NULL_TREE; 2763 1.1 mrg if (DECL_CLASS_SCOPE_P (decl)) 2764 1.1 mrg class_type = DECL_CONTEXT (decl); 2765 1.1 mrg else 2766 1.1 mrg { 2767 1.1 mrg /* Not a class member. */ 2768 1.1 mrg 2769 1.1 mrg /* Virtual tables have DECL_CONTEXT set to their associated class, 2770 1.1 mrg so they are automatically handled above. */ 2771 1.1 mrg gcc_assert (!VAR_P (decl) 2772 1.1 mrg || !DECL_VTABLE_OR_VTT_P (decl)); 2773 1.1 mrg 2774 1.1 mrg if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl)) 2775 1.1 mrg { 2776 1.1 mrg /* Local statics and classes get the visibility of their 2777 1.1 mrg containing function by default, except that 2778 1.1 mrg -fvisibility-inlines-hidden doesn't affect them. */ 2779 1.1 mrg tree fn = DECL_CONTEXT (decl); 2780 1.1 mrg if (DECL_VISIBILITY_SPECIFIED (fn)) 2781 1.1 mrg { 2782 1.1 mrg DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn); 2783 1.1 mrg DECL_VISIBILITY_SPECIFIED (decl) = 2784 1.1 mrg DECL_VISIBILITY_SPECIFIED (fn); 2785 1.1 mrg } 2786 1.1 mrg else 2787 1.1 mrg { 2788 1.1 mrg if (DECL_CLASS_SCOPE_P (fn)) 2789 1.1 mrg determine_visibility_from_class (decl, DECL_CONTEXT (fn)); 2790 1.1 mrg else if (determine_hidden_inline (fn)) 2791 1.1 mrg { 2792 1.1 mrg DECL_VISIBILITY (decl) = default_visibility; 2793 1.1 mrg DECL_VISIBILITY_SPECIFIED (decl) = 2794 1.1 mrg visibility_options.inpragma; 2795 1.1 mrg } 2796 1.1 mrg else 2797 1.1 mrg { 2798 1.1 mrg DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn); 2799 1.1 mrg DECL_VISIBILITY_SPECIFIED (decl) = 2800 1.1 mrg DECL_VISIBILITY_SPECIFIED (fn); 2801 1.1 mrg } 2802 1.1 mrg } 2803 1.1 mrg 2804 1.1 mrg /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set, 2805 1.1 mrg but have no TEMPLATE_INFO, so don't try to check it. */ 2806 1.1 mrg template_decl = NULL_TREE; 2807 1.1 mrg } 2808 1.1 mrg else if (VAR_P (decl) && DECL_TINFO_P (decl) 2809 1.1 mrg && flag_visibility_ms_compat) 2810 1.1 mrg { 2811 1.1 mrg /* Under -fvisibility-ms-compat, types are visible by default, 2812 1.1 mrg even though their contents aren't. */ 2813 1.1 mrg tree underlying_type = TREE_TYPE (DECL_NAME (decl)); 2814 1.1 mrg int underlying_vis = type_visibility (underlying_type); 2815 1.1 mrg if (underlying_vis == VISIBILITY_ANON 2816 1.1 mrg || (CLASS_TYPE_P (underlying_type) 2817 1.1 mrg && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))) 2818 1.1 mrg constrain_visibility (decl, underlying_vis, false); 2819 1.1 mrg else 2820 1.1 mrg DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; 2821 1.1 mrg } 2822 1.1 mrg else if (VAR_P (decl) && DECL_TINFO_P (decl)) 2823 1.1 mrg { 2824 1.1 mrg /* tinfo visibility is based on the type it's for. */ 2825 1.1 mrg constrain_visibility 2826 1.1 mrg (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false); 2827 1.1 mrg 2828 1.1 mrg /* Give the target a chance to override the visibility associated 2829 1.1 mrg with DECL. */ 2830 1.1 mrg if (TREE_PUBLIC (decl) 2831 1.1 mrg && !DECL_REALLY_EXTERN (decl) 2832 1.1 mrg && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl))) 2833 1.1 mrg && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl)))) 2834 1.1 mrg targetm.cxx.determine_class_data_visibility (decl); 2835 1.1 mrg } 2836 1.1 mrg else if (template_decl) 2837 1.1 mrg /* Template instantiations and specializations get visibility based 2838 1.1 mrg on their template unless they override it with an attribute. */; 2839 1.1 mrg else if (! DECL_VISIBILITY_SPECIFIED (decl)) 2840 1.1 mrg { 2841 1.1 mrg if (determine_hidden_inline (decl)) 2842 1.1 mrg DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; 2843 1.1 mrg else 2844 1.1 mrg { 2845 1.1 mrg /* Set default visibility to whatever the user supplied with 2846 1.1 mrg #pragma GCC visibility or a namespace visibility attribute. */ 2847 1.1 mrg DECL_VISIBILITY (decl) = default_visibility; 2848 1.1 mrg DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; 2849 1.1 mrg } 2850 1.1 mrg } 2851 1.1 mrg } 2852 1.1 mrg 2853 1.1 mrg if (template_decl) 2854 1.1 mrg { 2855 1.1 mrg /* If the specialization doesn't specify visibility, use the 2856 1.1 mrg visibility from the template. */ 2857 1.1 mrg tree tinfo = get_template_info (template_decl); 2858 1.1 mrg tree args = TI_ARGS (tinfo); 2859 1.1 mrg tree attribs = (TREE_CODE (decl) == TYPE_DECL 2860 1.1 mrg ? TYPE_ATTRIBUTES (TREE_TYPE (decl)) 2861 1.1 mrg : DECL_ATTRIBUTES (decl)); 2862 1.1 mrg tree attr = lookup_attribute ("visibility", attribs); 2863 1.1 mrg 2864 1.1 mrg if (args != error_mark_node) 2865 1.1 mrg { 2866 1.1 mrg tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo)); 2867 1.1 mrg 2868 1.1 mrg if (!DECL_VISIBILITY_SPECIFIED (decl)) 2869 1.1 mrg { 2870 1.1 mrg if (!attr 2871 1.1 mrg && determine_hidden_inline (decl)) 2872 1.1 mrg DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; 2873 1.1 mrg else 2874 1.1 mrg { 2875 1.1 mrg DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern); 2876 1.1 mrg DECL_VISIBILITY_SPECIFIED (decl) 2877 1.1 mrg = DECL_VISIBILITY_SPECIFIED (pattern); 2878 1.1 mrg } 2879 1.1 mrg } 2880 1.1 mrg 2881 1.1 mrg if (args 2882 1.1 mrg /* Template argument visibility outweighs #pragma or namespace 2883 1.1 mrg visibility, but not an explicit attribute. */ 2884 1.1 mrg && !attr) 2885 1.1 mrg { 2886 1.1 mrg int depth = TMPL_ARGS_DEPTH (args); 2887 1.1 mrg if (DECL_VISIBILITY_SPECIFIED (decl)) 2888 1.1 mrg { 2889 1.1 mrg /* A class template member with explicit visibility 2890 1.1 mrg overrides the class visibility, so we need to apply 2891 1.1 mrg all the levels of template args directly. */ 2892 1.1 mrg int i; 2893 1.1 mrg for (i = 1; i <= depth; ++i) 2894 1.1 mrg { 2895 1.1 mrg tree lev = TMPL_ARGS_LEVEL (args, i); 2896 1.1 mrg constrain_visibility_for_template (decl, lev); 2897 1.1 mrg } 2898 1.1 mrg } 2899 1.1 mrg else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))) 2900 1.1 mrg /* Limit visibility based on its template arguments. */ 2901 1.1 mrg constrain_visibility_for_template (decl, args); 2902 1.1 mrg } 2903 1.1 mrg } 2904 1.1 mrg } 2905 1.1 mrg 2906 1.1 mrg if (class_type) 2907 1.1 mrg determine_visibility_from_class (decl, class_type); 2908 1.1 mrg 2909 1.1 mrg if (decl_anon_ns_mem_p (decl)) 2910 1.1 mrg /* Names in an anonymous namespace get internal linkage. */ 2911 1.1 mrg constrain_visibility (decl, VISIBILITY_ANON, false); 2912 1.1 mrg else if (TREE_CODE (decl) != TYPE_DECL) 2913 1.1 mrg { 2914 1.1 mrg /* Propagate anonymity from type to decl. */ 2915 1.1 mrg int tvis = type_visibility (TREE_TYPE (decl)); 2916 1.1 mrg if (tvis == VISIBILITY_ANON 2917 1.1 mrg || ! DECL_VISIBILITY_SPECIFIED (decl)) 2918 1.1 mrg constrain_visibility (decl, tvis, false); 2919 1.1 mrg } 2920 1.1 mrg else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true)) 2921 1.1 mrg /* DR 757: A type without linkage shall not be used as the type of a 2922 1.1 mrg variable or function with linkage, unless 2923 1.1 mrg o the variable or function has extern "C" linkage (7.5 [dcl.link]), or 2924 1.1 mrg o the variable or function is not used (3.2 [basic.def.odr]) or is 2925 1.1 mrg defined in the same translation unit. 2926 1.1 mrg 2927 1.1 mrg Since non-extern "C" decls need to be defined in the same 2928 1.1 mrg translation unit, we can make the type internal. */ 2929 1.1 mrg constrain_visibility (decl, VISIBILITY_ANON, false); 2930 1.1 mrg 2931 1.1 mrg /* If visibility changed and DECL already has DECL_RTL, ensure 2932 1.1 mrg symbol flags are updated. */ 2933 1.1 mrg if ((DECL_VISIBILITY (decl) != orig_visibility 2934 1.1 mrg || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified) 2935 1.1 mrg && ((VAR_P (decl) && TREE_STATIC (decl)) 2936 1.1 mrg || TREE_CODE (decl) == FUNCTION_DECL) 2937 1.1 mrg && DECL_RTL_SET_P (decl)) 2938 1.1 mrg make_decl_rtl (decl); 2939 1.1 mrg } 2940 1.1 mrg 2941 1.1 mrg /* By default, static data members and function members receive 2942 1.1 mrg the visibility of their containing class. */ 2943 1.1 mrg 2944 1.1 mrg static void 2945 1.1 mrg determine_visibility_from_class (tree decl, tree class_type) 2946 1.1 mrg { 2947 1.1 mrg if (DECL_VISIBILITY_SPECIFIED (decl)) 2948 1.1 mrg return; 2949 1.1 mrg 2950 1.1 mrg if (determine_hidden_inline (decl)) 2951 1.1 mrg DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; 2952 1.1 mrg else 2953 1.1 mrg { 2954 1.1 mrg /* Default to the class visibility. */ 2955 1.1 mrg DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type); 2956 1.1 mrg DECL_VISIBILITY_SPECIFIED (decl) 2957 1.1 mrg = CLASSTYPE_VISIBILITY_SPECIFIED (class_type); 2958 1.1 mrg } 2959 1.1 mrg 2960 1.1 mrg /* Give the target a chance to override the visibility associated 2961 1.1 mrg with DECL. */ 2962 1.1 mrg if (VAR_P (decl) 2963 1.1 mrg && TREE_PUBLIC (decl) 2964 1.1 mrg && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl)) 2965 1.1 mrg && !DECL_REALLY_EXTERN (decl) 2966 1.1 mrg && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type)) 2967 1.1 mrg targetm.cxx.determine_class_data_visibility (decl); 2968 1.1 mrg } 2969 1.1 mrg 2970 1.1 mrg /* Returns true iff DECL is an inline that should get hidden visibility 2971 1.1 mrg because of -fvisibility-inlines-hidden. */ 2972 1.1 mrg 2973 1.1 mrg static bool 2974 1.1 mrg determine_hidden_inline (tree decl) 2975 1.1 mrg { 2976 1.1 mrg return (visibility_options.inlines_hidden 2977 1.1 mrg /* Don't do this for inline templates; specializations might not be 2978 1.1 mrg inline, and we don't want them to inherit the hidden 2979 1.1 mrg visibility. We'll set it here for all inline instantiations. */ 2980 1.1 mrg && !processing_template_decl 2981 1.1 mrg && TREE_CODE (decl) == FUNCTION_DECL 2982 1.1 mrg && DECL_DECLARED_INLINE_P (decl) 2983 1.1 mrg && (! DECL_LANG_SPECIFIC (decl) 2984 1.1 mrg || ! DECL_EXPLICIT_INSTANTIATION (decl))); 2985 1.1 mrg } 2986 1.1 mrg 2987 1.1 mrg /* Constrain the visibility of a class TYPE based on the visibility of its 2988 1.1 mrg field types. Warn if any fields require lesser visibility. */ 2989 1.1 mrg 2990 1.1 mrg void 2991 1.1 mrg constrain_class_visibility (tree type) 2992 1.1 mrg { 2993 1.1 mrg tree binfo; 2994 1.1 mrg tree t; 2995 1.1 mrg int i; 2996 1.1 mrg 2997 1.1 mrg int vis = type_visibility (type); 2998 1.1 mrg 2999 1.1 mrg if (vis == VISIBILITY_ANON 3000 1.1 mrg || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type))) 3001 1.1 mrg return; 3002 1.1 mrg 3003 1.1 mrg /* Don't warn about visibility if the class has explicit visibility. */ 3004 1.1 mrg if (CLASSTYPE_VISIBILITY_SPECIFIED (type)) 3005 1.1 mrg vis = VISIBILITY_INTERNAL; 3006 1.1 mrg 3007 1.1 mrg for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t)) 3008 1.1 mrg if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node 3009 1.1 mrg && !DECL_ARTIFICIAL (t)) 3010 1.1 mrg { 3011 1.1 mrg tree ftype = strip_pointer_or_array_types (TREE_TYPE (t)); 3012 1.1 mrg int subvis = type_visibility (ftype); 3013 1.1 mrg 3014 1.1 mrg if (subvis == VISIBILITY_ANON) 3015 1.1 mrg { 3016 1.1 mrg if (!in_main_input_context()) 3017 1.1 mrg { 3018 1.1 mrg tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false); 3019 1.1 mrg if (nlt) 3020 1.1 mrg { 3021 1.1 mrg if (same_type_p (TREE_TYPE (t), nlt)) 3022 1.1 mrg warning (OPT_Wsubobject_linkage, "\ 3023 1.1 mrg %qT has a field %qD whose type has no linkage", 3024 1.1 mrg type, t); 3025 1.1 mrg else 3026 1.1 mrg warning (OPT_Wsubobject_linkage, "\ 3027 1.1 mrg %qT has a field %qD whose type depends on the type %qT which has no linkage", 3028 1.1 mrg type, t, nlt); 3029 1.1 mrg } 3030 1.1 mrg else 3031 1.1 mrg warning (OPT_Wsubobject_linkage, "\ 3032 1.1 mrg %qT has a field %qD whose type uses the anonymous namespace", 3033 1.1 mrg type, t); 3034 1.1 mrg } 3035 1.1 mrg } 3036 1.1 mrg else if (MAYBE_CLASS_TYPE_P (ftype) 3037 1.1 mrg && vis < VISIBILITY_HIDDEN 3038 1.1 mrg && subvis >= VISIBILITY_HIDDEN) 3039 1.1 mrg warning (OPT_Wattributes, "\ 3040 1.1 mrg %qT declared with greater visibility than the type of its field %qD", 3041 1.1 mrg type, t); 3042 1.1 mrg } 3043 1.1 mrg 3044 1.1 mrg binfo = TYPE_BINFO (type); 3045 1.1 mrg for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i) 3046 1.1 mrg { 3047 1.1 mrg int subvis = type_visibility (TREE_TYPE (t)); 3048 1.1 mrg 3049 1.1 mrg if (subvis == VISIBILITY_ANON) 3050 1.1 mrg { 3051 1.1 mrg if (!in_main_input_context()) 3052 1.1 mrg { 3053 1.1 mrg tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false); 3054 1.1 mrg if (nlt) 3055 1.1 mrg { 3056 1.1 mrg if (same_type_p (TREE_TYPE (t), nlt)) 3057 1.1 mrg warning (OPT_Wsubobject_linkage, "\ 3058 1.1 mrg %qT has a base %qT whose type has no linkage", 3059 1.1 mrg type, TREE_TYPE (t)); 3060 1.1 mrg else 3061 1.1 mrg warning (OPT_Wsubobject_linkage, "\ 3062 1.1 mrg %qT has a base %qT whose type depends on the type %qT which has no linkage", 3063 1.1 mrg type, TREE_TYPE (t), nlt); 3064 1.1 mrg } 3065 1.1 mrg else 3066 1.1 mrg warning (OPT_Wsubobject_linkage, "\ 3067 1.1 mrg %qT has a base %qT whose type uses the anonymous namespace", 3068 1.1 mrg type, TREE_TYPE (t)); 3069 1.1 mrg } 3070 1.1 mrg } 3071 1.1 mrg else if (vis < VISIBILITY_HIDDEN 3072 1.1 mrg && subvis >= VISIBILITY_HIDDEN) 3073 1.1 mrg warning (OPT_Wattributes, "\ 3074 1.1 mrg %qT declared with greater visibility than its base %qT", 3075 1.1 mrg type, TREE_TYPE (t)); 3076 1.1 mrg } 3077 1.1 mrg } 3078 1.1 mrg 3079 1.1 mrg /* Functions for adjusting the visibility of a tagged type and its nested 3080 1.1 mrg types and declarations when it gets a name for linkage purposes from a 3081 1.1 mrg typedef. */ 3082 1.1 mrg // FIXME: It is now a DR for such a class type to contain anything 3083 1.1 mrg // other than C. So at minium most of this can probably be deleted. 3084 1.1 mrg 3085 1.1 mrg /* First reset the visibility of all the types. */ 3086 1.1 mrg 3087 1.1 mrg static void 3088 1.1 mrg reset_type_linkage_1 (tree type) 3089 1.1 mrg { 3090 1.1 mrg set_linkage_according_to_type (type, TYPE_MAIN_DECL (type)); 3091 1.1 mrg if (CLASS_TYPE_P (type)) 3092 1.1 mrg for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member)) 3093 1.1 mrg if (DECL_IMPLICIT_TYPEDEF_P (member)) 3094 1.1 mrg reset_type_linkage_1 (TREE_TYPE (member)); 3095 1.1 mrg } 3096 1.1 mrg 3097 1.1 mrg /* Then reset the visibility of any static data members or member 3098 1.1 mrg functions that use those types. */ 3099 1.1 mrg 3100 1.1 mrg static void 3101 1.1 mrg reset_decl_linkage (tree decl) 3102 1.1 mrg { 3103 1.1 mrg if (TREE_PUBLIC (decl)) 3104 1.1 mrg return; 3105 1.1 mrg if (DECL_CLONED_FUNCTION_P (decl)) 3106 1.1 mrg return; 3107 1.1 mrg TREE_PUBLIC (decl) = true; 3108 1.1 mrg DECL_INTERFACE_KNOWN (decl) = false; 3109 1.1 mrg determine_visibility (decl); 3110 1.1 mrg tentative_decl_linkage (decl); 3111 1.1 mrg } 3112 1.1 mrg 3113 1.1 mrg void 3114 1.1 mrg reset_type_linkage (tree type) 3115 1.1 mrg { 3116 1.1 mrg reset_type_linkage_1 (type); 3117 1.1 mrg if (CLASS_TYPE_P (type)) 3118 1.1 mrg { 3119 1.1 mrg if (tree vt = CLASSTYPE_VTABLES (type)) 3120 1.1 mrg { 3121 1.1 mrg tree name = mangle_vtbl_for_type (type); 3122 1.1 mrg DECL_NAME (vt) = name; 3123 1.1 mrg SET_DECL_ASSEMBLER_NAME (vt, name); 3124 1.1 mrg reset_decl_linkage (vt); 3125 1.1 mrg } 3126 1.1 mrg if (!ANON_AGGR_TYPE_P (type)) 3127 1.1 mrg if (tree ti = CLASSTYPE_TYPEINFO_VAR (type)) 3128 1.1 mrg { 3129 1.1 mrg tree name = mangle_typeinfo_for_type (type); 3130 1.1 mrg DECL_NAME (ti) = name; 3131 1.1 mrg SET_DECL_ASSEMBLER_NAME (ti, name); 3132 1.1 mrg TREE_TYPE (name) = type; 3133 1.1 mrg reset_decl_linkage (ti); 3134 1.1 mrg } 3135 1.1 mrg for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m)) 3136 1.1 mrg { 3137 1.1 mrg tree mem = STRIP_TEMPLATE (m); 3138 1.1 mrg if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL) 3139 1.1 mrg reset_decl_linkage (mem); 3140 1.1 mrg else if (DECL_IMPLICIT_TYPEDEF_P (mem)) 3141 1.1 mrg reset_type_linkage (TREE_TYPE (mem)); 3142 1.1 mrg } 3143 1.1 mrg } 3144 1.1 mrg } 3145 1.1 mrg 3146 1.1 mrg /* Set up our initial idea of what the linkage of DECL should be. */ 3147 1.1 mrg 3148 1.1 mrg void 3149 1.1 mrg tentative_decl_linkage (tree decl) 3150 1.1 mrg { 3151 1.1 mrg if (DECL_INTERFACE_KNOWN (decl)) 3152 1.1 mrg /* We've already made a decision as to how this function will 3153 1.1 mrg be handled. */; 3154 1.1 mrg else if (vague_linkage_p (decl)) 3155 1.1 mrg { 3156 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL 3157 1.1 mrg && decl_defined_p (decl)) 3158 1.1 mrg { 3159 1.1 mrg DECL_EXTERNAL (decl) = 1; 3160 1.1 mrg DECL_NOT_REALLY_EXTERN (decl) = 1; 3161 1.1 mrg note_vague_linkage_fn (decl); 3162 1.1 mrg /* A non-template inline function with external linkage will 3163 1.1 mrg always be COMDAT. As we must eventually determine the 3164 1.1 mrg linkage of all functions, and as that causes writes to 3165 1.1 mrg the data mapped in from the PCH file, it's advantageous 3166 1.1 mrg to mark the functions at this point. */ 3167 1.1 mrg if (DECL_DECLARED_INLINE_P (decl) 3168 1.1 mrg && (!DECL_IMPLICIT_INSTANTIATION (decl) 3169 1.1 mrg || DECL_DEFAULTED_FN (decl))) 3170 1.1 mrg { 3171 1.1 mrg /* This function must have external linkage, as 3172 1.1 mrg otherwise DECL_INTERFACE_KNOWN would have been 3173 1.1 mrg set. */ 3174 1.1 mrg gcc_assert (TREE_PUBLIC (decl)); 3175 1.1 mrg comdat_linkage (decl); 3176 1.1 mrg DECL_INTERFACE_KNOWN (decl) = 1; 3177 1.1 mrg } 3178 1.1 mrg } 3179 1.1 mrg else if (VAR_P (decl)) 3180 1.1 mrg maybe_commonize_var (decl); 3181 1.1 mrg } 3182 1.1 mrg } 3183 1.1 mrg 3184 1.1 mrg /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage 3185 1.1 mrg for DECL has not already been determined, do so now by setting 3186 1.1 mrg DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this 3187 1.1 mrg function is called entities with vague linkage whose definitions 3188 1.1 mrg are available must have TREE_PUBLIC set. 3189 1.1 mrg 3190 1.1 mrg If this function decides to place DECL in COMDAT, it will set 3191 1.1 mrg appropriate flags -- but will not clear DECL_EXTERNAL. It is up to 3192 1.1 mrg the caller to decide whether or not to clear DECL_EXTERNAL. Some 3193 1.1 mrg callers defer that decision until it is clear that DECL is actually 3194 1.1 mrg required. */ 3195 1.1 mrg 3196 1.1 mrg void 3197 1.1 mrg import_export_decl (tree decl) 3198 1.1 mrg { 3199 1.1 mrg bool comdat_p; 3200 1.1 mrg bool import_p; 3201 1.1 mrg tree class_type = NULL_TREE; 3202 1.1 mrg 3203 1.1 mrg if (DECL_INTERFACE_KNOWN (decl)) 3204 1.1 mrg return; 3205 1.1 mrg 3206 1.1 mrg /* We cannot determine what linkage to give to an entity with vague 3207 1.1 mrg linkage until the end of the file. For example, a virtual table 3208 1.1 mrg for a class will be defined if and only if the key method is 3209 1.1 mrg defined in this translation unit. */ 3210 1.1 mrg gcc_assert (at_eof); 3211 1.1 mrg /* Object file linkage for explicit instantiations is handled in 3212 1.1 mrg mark_decl_instantiated. For static variables in functions with 3213 1.1 mrg vague linkage, maybe_commonize_var is used. 3214 1.1 mrg 3215 1.1 mrg Therefore, the only declarations that should be provided to this 3216 1.1 mrg function are those with external linkage that are: 3217 1.1 mrg 3218 1.1 mrg * implicit instantiations of function templates 3219 1.1 mrg 3220 1.1 mrg * inline function 3221 1.1 mrg 3222 1.1 mrg * implicit instantiations of static data members of class 3223 1.1 mrg templates 3224 1.1 mrg 3225 1.1 mrg * virtual tables 3226 1.1 mrg 3227 1.1 mrg * typeinfo objects 3228 1.1 mrg 3229 1.1 mrg Furthermore, all entities that reach this point must have a 3230 1.1 mrg definition available in this translation unit. 3231 1.1 mrg 3232 1.1 mrg The following assertions check these conditions. */ 3233 1.1 mrg gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); 3234 1.1 mrg /* Any code that creates entities with TREE_PUBLIC cleared should 3235 1.1 mrg also set DECL_INTERFACE_KNOWN. */ 3236 1.1 mrg gcc_assert (TREE_PUBLIC (decl)); 3237 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL) 3238 1.1 mrg gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl) 3239 1.1 mrg || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) 3240 1.1 mrg || DECL_DECLARED_INLINE_P (decl)); 3241 1.1 mrg else 3242 1.1 mrg gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl) 3243 1.1 mrg || DECL_VTABLE_OR_VTT_P (decl) 3244 1.1 mrg || DECL_TINFO_P (decl)); 3245 1.1 mrg /* Check that a definition of DECL is available in this translation 3246 1.1 mrg unit. */ 3247 1.1 mrg gcc_assert (!DECL_REALLY_EXTERN (decl)); 3248 1.1 mrg 3249 1.1 mrg /* Assume that DECL will not have COMDAT linkage. */ 3250 1.1 mrg comdat_p = false; 3251 1.1 mrg /* Assume that DECL will not be imported into this translation 3252 1.1 mrg unit. */ 3253 1.1 mrg import_p = false; 3254 1.1 mrg 3255 1.1 mrg if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl)) 3256 1.1 mrg { 3257 1.1 mrg class_type = DECL_CONTEXT (decl); 3258 1.1 mrg import_export_class (class_type); 3259 1.1 mrg if (CLASSTYPE_INTERFACE_KNOWN (class_type) 3260 1.1 mrg && CLASSTYPE_INTERFACE_ONLY (class_type)) 3261 1.1 mrg import_p = true; 3262 1.1 mrg else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC) 3263 1.1 mrg && !CLASSTYPE_USE_TEMPLATE (class_type) 3264 1.1 mrg && CLASSTYPE_KEY_METHOD (class_type) 3265 1.1 mrg && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))) 3266 1.1 mrg /* The ABI requires that all virtual tables be emitted with 3267 1.1 mrg COMDAT linkage. However, on systems where COMDAT symbols 3268 1.1 mrg don't show up in the table of contents for a static 3269 1.1 mrg archive, or on systems without weak symbols (where we 3270 1.1 mrg approximate COMDAT linkage by using internal linkage), the 3271 1.1 mrg linker will report errors about undefined symbols because 3272 1.1 mrg it will not see the virtual table definition. Therefore, 3273 1.1 mrg in the case that we know that the virtual table will be 3274 1.1 mrg emitted in only one translation unit, we make the virtual 3275 1.1 mrg table an ordinary definition with external linkage. */ 3276 1.1 mrg DECL_EXTERNAL (decl) = 0; 3277 1.1 mrg else if (CLASSTYPE_INTERFACE_KNOWN (class_type)) 3278 1.1 mrg { 3279 1.1 mrg /* CLASS_TYPE is being exported from this translation unit, 3280 1.1 mrg so DECL should be defined here. */ 3281 1.1 mrg if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type)) 3282 1.1 mrg /* If a class is declared in a header with the "extern 3283 1.1 mrg template" extension, then it will not be instantiated, 3284 1.1 mrg even in translation units that would normally require 3285 1.1 mrg it. Often such classes are explicitly instantiated in 3286 1.1 mrg one translation unit. Therefore, the explicit 3287 1.1 mrg instantiation must be made visible to other translation 3288 1.1 mrg units. */ 3289 1.1 mrg DECL_EXTERNAL (decl) = 0; 3290 1.1 mrg else 3291 1.1 mrg { 3292 1.1 mrg /* The generic C++ ABI says that class data is always 3293 1.1 mrg COMDAT, even if there is a key function. Some 3294 1.1 mrg variants (e.g., the ARM EABI) says that class data 3295 1.1 mrg only has COMDAT linkage if the class data might be 3296 1.1 mrg emitted in more than one translation unit. When the 3297 1.1 mrg key method can be inline and is inline, we still have 3298 1.1 mrg to arrange for comdat even though 3299 1.1 mrg class_data_always_comdat is false. */ 3300 1.1 mrg if (!CLASSTYPE_KEY_METHOD (class_type) 3301 1.1 mrg || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)) 3302 1.1 mrg || targetm.cxx.class_data_always_comdat ()) 3303 1.1 mrg { 3304 1.1 mrg /* The ABI requires COMDAT linkage. Normally, we 3305 1.1 mrg only emit COMDAT things when they are needed; 3306 1.1 mrg make sure that we realize that this entity is 3307 1.1 mrg indeed needed. */ 3308 1.1 mrg comdat_p = true; 3309 1.1 mrg mark_needed (decl); 3310 1.1 mrg } 3311 1.1 mrg } 3312 1.1 mrg } 3313 1.1 mrg else if (!flag_implicit_templates 3314 1.1 mrg && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type)) 3315 1.1 mrg import_p = true; 3316 1.1 mrg else 3317 1.1 mrg comdat_p = true; 3318 1.1 mrg } 3319 1.1 mrg else if (VAR_P (decl) && DECL_TINFO_P (decl)) 3320 1.1 mrg { 3321 1.1 mrg tree type = TREE_TYPE (DECL_NAME (decl)); 3322 1.1 mrg if (CLASS_TYPE_P (type)) 3323 1.1 mrg { 3324 1.1 mrg class_type = type; 3325 1.1 mrg import_export_class (type); 3326 1.1 mrg if (CLASSTYPE_INTERFACE_KNOWN (type) 3327 1.1 mrg && TYPE_POLYMORPHIC_P (type) 3328 1.1 mrg && CLASSTYPE_INTERFACE_ONLY (type) 3329 1.1 mrg /* If -fno-rtti was specified, then we cannot be sure 3330 1.1 mrg that RTTI information will be emitted with the 3331 1.1 mrg virtual table of the class, so we must emit it 3332 1.1 mrg wherever it is used. */ 3333 1.1 mrg && flag_rtti) 3334 1.1 mrg import_p = true; 3335 1.1 mrg else 3336 1.1 mrg { 3337 1.1 mrg if (CLASSTYPE_INTERFACE_KNOWN (type) 3338 1.1 mrg && !CLASSTYPE_INTERFACE_ONLY (type)) 3339 1.1 mrg { 3340 1.1 mrg comdat_p = (targetm.cxx.class_data_always_comdat () 3341 1.1 mrg || (CLASSTYPE_KEY_METHOD (type) 3342 1.1 mrg && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))); 3343 1.1 mrg mark_needed (decl); 3344 1.1 mrg if (!flag_weak) 3345 1.1 mrg { 3346 1.1 mrg comdat_p = false; 3347 1.1 mrg DECL_EXTERNAL (decl) = 0; 3348 1.1 mrg } 3349 1.1 mrg } 3350 1.1 mrg else 3351 1.1 mrg comdat_p = true; 3352 1.1 mrg } 3353 1.1 mrg } 3354 1.1 mrg else 3355 1.1 mrg comdat_p = true; 3356 1.1 mrg } 3357 1.1 mrg else if (DECL_TEMPLOID_INSTANTIATION (decl)) 3358 1.1 mrg { 3359 1.1 mrg /* DECL is an implicit instantiation of a function or static 3360 1.1 mrg data member. */ 3361 1.1 mrg if (flag_implicit_templates 3362 1.1 mrg || (flag_implicit_inline_templates 3363 1.1 mrg && TREE_CODE (decl) == FUNCTION_DECL 3364 1.1 mrg && DECL_DECLARED_INLINE_P (decl))) 3365 1.1 mrg comdat_p = true; 3366 1.1 mrg else 3367 1.1 mrg /* If we are not implicitly generating templates, then mark 3368 1.1 mrg this entity as undefined in this translation unit. */ 3369 1.1 mrg import_p = true; 3370 1.1 mrg } 3371 1.1 mrg else if (DECL_FUNCTION_MEMBER_P (decl)) 3372 1.1 mrg { 3373 1.1 mrg if (!DECL_DECLARED_INLINE_P (decl)) 3374 1.1 mrg { 3375 1.1 mrg tree ctype = DECL_CONTEXT (decl); 3376 1.1 mrg import_export_class (ctype); 3377 1.1 mrg if (CLASSTYPE_INTERFACE_KNOWN (ctype)) 3378 1.1 mrg { 3379 1.1 mrg DECL_NOT_REALLY_EXTERN (decl) 3380 1.1 mrg = ! (CLASSTYPE_INTERFACE_ONLY (ctype) 3381 1.1 mrg || (DECL_DECLARED_INLINE_P (decl) 3382 1.1 mrg && ! flag_implement_inlines 3383 1.1 mrg && !DECL_VINDEX (decl))); 3384 1.1 mrg 3385 1.1 mrg if (!DECL_NOT_REALLY_EXTERN (decl)) 3386 1.1 mrg DECL_EXTERNAL (decl) = 1; 3387 1.1 mrg 3388 1.1 mrg /* Always make artificials weak. */ 3389 1.1 mrg if (DECL_ARTIFICIAL (decl) && flag_weak) 3390 1.1 mrg comdat_p = true; 3391 1.1 mrg else 3392 1.1 mrg maybe_make_one_only (decl); 3393 1.1 mrg } 3394 1.1 mrg } 3395 1.1 mrg else 3396 1.1 mrg comdat_p = true; 3397 1.1 mrg } 3398 1.1 mrg else 3399 1.1 mrg comdat_p = true; 3400 1.1 mrg 3401 1.1 mrg if (import_p) 3402 1.1 mrg { 3403 1.1 mrg /* If we are importing DECL into this translation unit, mark is 3404 1.1 mrg an undefined here. */ 3405 1.1 mrg DECL_EXTERNAL (decl) = 1; 3406 1.1 mrg DECL_NOT_REALLY_EXTERN (decl) = 0; 3407 1.1 mrg } 3408 1.1 mrg else if (comdat_p) 3409 1.1 mrg { 3410 1.1 mrg /* If we decided to put DECL in COMDAT, mark it accordingly at 3411 1.1 mrg this point. */ 3412 1.1 mrg comdat_linkage (decl); 3413 1.1 mrg } 3414 1.1 mrg 3415 1.1 mrg DECL_INTERFACE_KNOWN (decl) = 1; 3416 1.1 mrg } 3417 1.1 mrg 3418 1.1 mrg /* Return an expression that performs the destruction of DECL, which 3419 1.1 mrg must be a VAR_DECL whose type has a non-trivial destructor, or is 3420 1.1 mrg an array whose (innermost) elements have a non-trivial destructor. */ 3421 1.1 mrg 3422 1.1 mrg tree 3423 1.1 mrg build_cleanup (tree decl) 3424 1.1 mrg { 3425 1.1 mrg tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error); 3426 1.1 mrg gcc_assert (clean != NULL_TREE); 3427 1.1 mrg return clean; 3428 1.1 mrg } 3429 1.1 mrg 3430 1.1 mrg /* GUARD is a helper variable for DECL; make them have the same linkage and 3431 1.1 mrg visibility. */ 3432 1.1 mrg 3433 1.1 mrg void 3434 1.1 mrg copy_linkage (tree guard, tree decl) 3435 1.1 mrg { 3436 1.1 mrg TREE_PUBLIC (guard) = TREE_PUBLIC (decl); 3437 1.1 mrg TREE_STATIC (guard) = TREE_STATIC (decl); 3438 1.1 mrg DECL_COMMON (guard) = DECL_COMMON (decl); 3439 1.1 mrg DECL_COMDAT (guard) = DECL_COMDAT (decl); 3440 1.1 mrg if (TREE_STATIC (guard)) 3441 1.1 mrg { 3442 1.1 mrg CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl); 3443 1.1 mrg set_decl_tls_model (guard, DECL_TLS_MODEL (decl)); 3444 1.1 mrg if (DECL_ONE_ONLY (decl)) 3445 1.1 mrg make_decl_one_only (guard, cxx_comdat_group (guard)); 3446 1.1 mrg if (TREE_PUBLIC (decl)) 3447 1.1 mrg DECL_WEAK (guard) = DECL_WEAK (decl); 3448 1.1 mrg /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not 3449 1.1 mrg be set until import_export_decl at EOF. */ 3450 1.1 mrg if (vague_linkage_p (decl)) 3451 1.1 mrg comdat_linkage (guard); 3452 1.1 mrg DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl); 3453 1.1 mrg DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl); 3454 1.1 mrg } 3455 1.1 mrg } 3456 1.1 mrg 3457 1.1 mrg /* Returns the initialization guard variable for the variable DECL, 3458 1.1 mrg which has static storage duration. */ 3459 1.1 mrg 3460 1.1 mrg tree 3461 1.1 mrg get_guard (tree decl) 3462 1.1 mrg { 3463 1.1 mrg tree sname = mangle_guard_variable (decl); 3464 1.1 mrg tree guard = get_global_binding (sname); 3465 1.1 mrg if (! guard) 3466 1.1 mrg { 3467 1.1 mrg tree guard_type; 3468 1.1 mrg 3469 1.1 mrg /* We use a type that is big enough to contain a mutex as well 3470 1.1 mrg as an integer counter. */ 3471 1.1 mrg guard_type = targetm.cxx.guard_type (); 3472 1.1 mrg guard = build_decl (DECL_SOURCE_LOCATION (decl), 3473 1.1 mrg VAR_DECL, sname, guard_type); 3474 1.1 mrg 3475 1.1 mrg /* The guard should have the same linkage as what it guards. */ 3476 1.1 mrg copy_linkage (guard, decl); 3477 1.1 mrg 3478 1.1 mrg DECL_ARTIFICIAL (guard) = 1; 3479 1.1 mrg DECL_IGNORED_P (guard) = 1; 3480 1.1 mrg TREE_USED (guard) = 1; 3481 1.1 mrg pushdecl_top_level_and_finish (guard, NULL_TREE); 3482 1.1 mrg } 3483 1.1 mrg return guard; 3484 1.1 mrg } 3485 1.1 mrg 3486 1.1 mrg /* Returns true if accessing the GUARD atomic is expensive, 3487 1.1 mrg i.e. involves a call to __sync_synchronize or similar. 3488 1.1 mrg In this case let __cxa_guard_acquire handle the atomics. */ 3489 1.1 mrg 3490 1.1 mrg static bool 3491 1.1 mrg is_atomic_expensive_p (machine_mode mode) 3492 1.1 mrg { 3493 1.1 mrg if (!flag_inline_atomics) 3494 1.1 mrg return true; 3495 1.1 mrg 3496 1.1 mrg if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode)) 3497 1.1 mrg return true; 3498 1.1 mrg 3499 1.1 mrg return false; 3500 1.1 mrg } 3501 1.1 mrg 3502 1.1 mrg /* Return an atomic load of src with the appropriate memory model. */ 3503 1.1 mrg 3504 1.1 mrg static tree 3505 1.1 mrg build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type) 3506 1.1 mrg { 3507 1.1 mrg tree ptr_type = build_pointer_type (type); 3508 1.1 mrg tree mem_model = build_int_cst (integer_type_node, model); 3509 1.1 mrg tree t, addr, val; 3510 1.1 mrg unsigned int size; 3511 1.1 mrg int fncode; 3512 1.1 mrg 3513 1.1 mrg size = tree_to_uhwi (TYPE_SIZE_UNIT (type)); 3514 1.1 mrg 3515 1.1 mrg fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1; 3516 1.1 mrg t = builtin_decl_implicit ((enum built_in_function) fncode); 3517 1.1 mrg 3518 1.1 mrg addr = build1 (ADDR_EXPR, ptr_type, src); 3519 1.1 mrg val = build_call_expr (t, 2, addr, mem_model); 3520 1.1 mrg return val; 3521 1.1 mrg } 3522 1.1 mrg 3523 1.1 mrg /* Return those bits of the GUARD variable that should be set when the 3524 1.1 mrg guarded entity is actually initialized. */ 3525 1.1 mrg 3526 1.1 mrg static tree 3527 1.1 mrg get_guard_bits (tree guard) 3528 1.1 mrg { 3529 1.1 mrg if (!targetm.cxx.guard_mask_bit ()) 3530 1.1 mrg { 3531 1.1 mrg /* We only set the first byte of the guard, in order to leave room 3532 1.1 mrg for a mutex in the high-order bits. */ 3533 1.1 mrg guard = build1 (ADDR_EXPR, 3534 1.1 mrg build_pointer_type (TREE_TYPE (guard)), 3535 1.1 mrg guard); 3536 1.1 mrg guard = build1 (NOP_EXPR, 3537 1.1 mrg build_pointer_type (char_type_node), 3538 1.1 mrg guard); 3539 1.1 mrg guard = build1 (INDIRECT_REF, char_type_node, guard); 3540 1.1 mrg } 3541 1.1 mrg 3542 1.1 mrg return guard; 3543 1.1 mrg } 3544 1.1 mrg 3545 1.1 mrg /* Return an expression which determines whether or not the GUARD 3546 1.1 mrg variable has already been initialized. */ 3547 1.1 mrg 3548 1.1 mrg tree 3549 1.1 mrg get_guard_cond (tree guard, bool thread_safe) 3550 1.1 mrg { 3551 1.1 mrg tree guard_value; 3552 1.1 mrg 3553 1.1 mrg if (!thread_safe) 3554 1.1 mrg guard = get_guard_bits (guard); 3555 1.1 mrg else 3556 1.1 mrg { 3557 1.1 mrg tree type = targetm.cxx.guard_mask_bit () 3558 1.1 mrg ? TREE_TYPE (guard) : char_type_node; 3559 1.1 mrg 3560 1.1 mrg if (is_atomic_expensive_p (TYPE_MODE (type))) 3561 1.1 mrg guard = integer_zero_node; 3562 1.1 mrg else 3563 1.1 mrg guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type); 3564 1.1 mrg } 3565 1.1 mrg 3566 1.1 mrg /* Mask off all but the low bit. */ 3567 1.1 mrg if (targetm.cxx.guard_mask_bit ()) 3568 1.1 mrg { 3569 1.1 mrg guard_value = integer_one_node; 3570 1.1 mrg if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard))) 3571 1.1 mrg guard_value = fold_convert (TREE_TYPE (guard), guard_value); 3572 1.1 mrg guard = cp_build_binary_op (input_location, 3573 1.1 mrg BIT_AND_EXPR, guard, guard_value, 3574 1.1 mrg tf_warning_or_error); 3575 1.1 mrg } 3576 1.1 mrg 3577 1.1 mrg guard_value = integer_zero_node; 3578 1.1 mrg if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard))) 3579 1.1 mrg guard_value = fold_convert (TREE_TYPE (guard), guard_value); 3580 1.1 mrg return cp_build_binary_op (input_location, 3581 1.1 mrg EQ_EXPR, guard, guard_value, 3582 1.1 mrg tf_warning_or_error); 3583 1.1 mrg } 3584 1.1 mrg 3585 1.1 mrg /* Return an expression which sets the GUARD variable, indicating that 3586 1.1 mrg the variable being guarded has been initialized. */ 3587 1.1 mrg 3588 1.1 mrg tree 3589 1.1 mrg set_guard (tree guard) 3590 1.1 mrg { 3591 1.1 mrg tree guard_init; 3592 1.1 mrg 3593 1.1 mrg /* Set the GUARD to one. */ 3594 1.1 mrg guard = get_guard_bits (guard); 3595 1.1 mrg guard_init = integer_one_node; 3596 1.1 mrg if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard))) 3597 1.1 mrg guard_init = fold_convert (TREE_TYPE (guard), guard_init); 3598 1.1 mrg return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init, 3599 1.1 mrg tf_warning_or_error); 3600 1.1 mrg } 3601 1.1 mrg 3602 1.1 mrg /* Returns true iff we can tell that VAR does not have a dynamic 3603 1.1 mrg initializer. */ 3604 1.1 mrg 3605 1.1 mrg static bool 3606 1.1 mrg var_defined_without_dynamic_init (tree var) 3607 1.1 mrg { 3608 1.1 mrg /* constinit vars are guaranteed to not have dynamic initializer, 3609 1.1 mrg but still registering the destructor counts as dynamic initialization. */ 3610 1.1 mrg if (DECL_DECLARED_CONSTINIT_P (var) 3611 1.1 mrg && COMPLETE_TYPE_P (TREE_TYPE (var)) 3612 1.1 mrg && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var))) 3613 1.1 mrg return true; 3614 1.1 mrg /* If it's defined in another TU, we can't tell. */ 3615 1.1 mrg if (DECL_EXTERNAL (var)) 3616 1.1 mrg return false; 3617 1.1 mrg /* If it has a non-trivial destructor, registering the destructor 3618 1.1 mrg counts as dynamic initialization. */ 3619 1.1 mrg if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var))) 3620 1.1 mrg return false; 3621 1.1 mrg /* If it's in this TU, its initializer has been processed, unless 3622 1.1 mrg it's a case of self-initialization, then DECL_INITIALIZED_P is 3623 1.1 mrg false while the initializer is handled by finish_id_expression. */ 3624 1.1 mrg if (!DECL_INITIALIZED_P (var)) 3625 1.1 mrg return false; 3626 1.1 mrg /* If it has no initializer or a constant one, it's not dynamic. */ 3627 1.1 mrg return (!DECL_NONTRIVIALLY_INITIALIZED_P (var) 3628 1.1 mrg || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var)); 3629 1.1 mrg } 3630 1.1 mrg 3631 1.1 mrg /* Returns true iff VAR is a variable that needs uses to be 3632 1.1 mrg wrapped for possible dynamic initialization. */ 3633 1.1 mrg 3634 1.1 mrg bool 3635 1.1 mrg var_needs_tls_wrapper (tree var) 3636 1.1 mrg { 3637 1.1 mrg return (!error_operand_p (var) 3638 1.1 mrg && CP_DECL_THREAD_LOCAL_P (var) 3639 1.1 mrg && !DECL_GNU_TLS_P (var) 3640 1.1 mrg && !DECL_FUNCTION_SCOPE_P (var) 3641 1.1 mrg && !var_defined_without_dynamic_init (var)); 3642 1.1 mrg } 3643 1.1 mrg 3644 1.1 mrg /* Get the FUNCTION_DECL for the shared TLS init function for this 3645 1.1 mrg translation unit. */ 3646 1.1 mrg 3647 1.1 mrg static tree 3648 1.1 mrg get_local_tls_init_fn (location_t loc) 3649 1.1 mrg { 3650 1.1 mrg tree sname = get_identifier ("__tls_init"); 3651 1.1 mrg tree fn = get_global_binding (sname); 3652 1.1 mrg if (!fn) 3653 1.1 mrg { 3654 1.1 mrg fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname, 3655 1.1 mrg build_function_type (void_type_node, 3656 1.1 mrg void_list_node)); 3657 1.1 mrg SET_DECL_LANGUAGE (fn, lang_c); 3658 1.1 mrg TREE_PUBLIC (fn) = false; 3659 1.1 mrg DECL_ARTIFICIAL (fn) = true; 3660 1.1 mrg mark_used (fn); 3661 1.1 mrg set_global_binding (fn); 3662 1.1 mrg } 3663 1.1 mrg return fn; 3664 1.1 mrg } 3665 1.1 mrg 3666 1.1 mrg /* Get a FUNCTION_DECL for the init function for the thread_local 3667 1.1 mrg variable VAR. The init function will be an alias to the function 3668 1.1 mrg that initializes all the non-local TLS variables in the translation 3669 1.1 mrg unit. The init function is only used by the wrapper function. */ 3670 1.1 mrg 3671 1.1 mrg static tree 3672 1.1 mrg get_tls_init_fn (tree var) 3673 1.1 mrg { 3674 1.1 mrg /* Only C++11 TLS vars need this init fn. */ 3675 1.1 mrg if (!var_needs_tls_wrapper (var)) 3676 1.1 mrg return NULL_TREE; 3677 1.1 mrg 3678 1.1 mrg /* If -fno-extern-tls-init, assume that we don't need to call 3679 1.1 mrg a tls init function for a variable defined in another TU. */ 3680 1.1 mrg if (!flag_extern_tls_init && DECL_EXTERNAL (var)) 3681 1.1 mrg return NULL_TREE; 3682 1.1 mrg 3683 1.1 mrg /* If the variable is internal, or if we can't generate aliases, 3684 1.1 mrg call the local init function directly. */ 3685 1.1 mrg if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES) 3686 1.1 mrg return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var)); 3687 1.1 mrg 3688 1.1 mrg tree sname = mangle_tls_init_fn (var); 3689 1.1 mrg tree fn = get_global_binding (sname); 3690 1.1 mrg if (!fn) 3691 1.1 mrg { 3692 1.1 mrg fn = build_lang_decl (FUNCTION_DECL, sname, 3693 1.1 mrg build_function_type (void_type_node, 3694 1.1 mrg void_list_node)); 3695 1.1 mrg SET_DECL_LANGUAGE (fn, lang_c); 3696 1.1 mrg TREE_PUBLIC (fn) = TREE_PUBLIC (var); 3697 1.1 mrg DECL_ARTIFICIAL (fn) = true; 3698 1.1 mrg DECL_COMDAT (fn) = DECL_COMDAT (var); 3699 1.1 mrg DECL_EXTERNAL (fn) = DECL_EXTERNAL (var); 3700 1.1 mrg if (DECL_ONE_ONLY (var)) 3701 1.1 mrg make_decl_one_only (fn, cxx_comdat_group (fn)); 3702 1.1 mrg if (TREE_PUBLIC (var)) 3703 1.1 mrg { 3704 1.1 mrg tree obtype = strip_array_types (non_reference (TREE_TYPE (var))); 3705 1.1 mrg /* If the variable is defined somewhere else and might have static 3706 1.1 mrg initialization, make the init function a weak reference. */ 3707 1.1 mrg if ((!TYPE_NEEDS_CONSTRUCTING (obtype) 3708 1.1 mrg || TYPE_HAS_CONSTEXPR_CTOR (obtype) 3709 1.1 mrg || TYPE_HAS_TRIVIAL_DFLT (obtype)) 3710 1.1 mrg && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype) 3711 1.1 mrg && DECL_EXTERNAL (var)) 3712 1.1 mrg declare_weak (fn); 3713 1.1 mrg else 3714 1.1 mrg DECL_WEAK (fn) = DECL_WEAK (var); 3715 1.1 mrg } 3716 1.1 mrg DECL_VISIBILITY (fn) = DECL_VISIBILITY (var); 3717 1.1 mrg DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var); 3718 1.1 mrg DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var); 3719 1.1 mrg DECL_IGNORED_P (fn) = 1; 3720 1.1 mrg mark_used (fn); 3721 1.1 mrg 3722 1.1 mrg DECL_BEFRIENDING_CLASSES (fn) = var; 3723 1.1 mrg 3724 1.1 mrg set_global_binding (fn); 3725 1.1 mrg } 3726 1.1 mrg return fn; 3727 1.1 mrg } 3728 1.1 mrg 3729 1.1 mrg /* Get a FUNCTION_DECL for the init wrapper function for the thread_local 3730 1.1 mrg variable VAR. The wrapper function calls the init function (if any) for 3731 1.1 mrg VAR and then returns a reference to VAR. The wrapper function is used 3732 1.1 mrg in place of VAR everywhere VAR is mentioned. */ 3733 1.1 mrg 3734 1.1 mrg static tree 3735 1.1 mrg get_tls_wrapper_fn (tree var) 3736 1.1 mrg { 3737 1.1 mrg /* Only C++11 TLS vars need this wrapper fn. */ 3738 1.1 mrg if (!var_needs_tls_wrapper (var)) 3739 1.1 mrg return NULL_TREE; 3740 1.1 mrg 3741 1.1 mrg tree sname = mangle_tls_wrapper_fn (var); 3742 1.1 mrg tree fn = get_global_binding (sname); 3743 1.1 mrg if (!fn) 3744 1.1 mrg { 3745 1.1 mrg /* A named rvalue reference is an lvalue, so the wrapper should 3746 1.1 mrg always return an lvalue reference. */ 3747 1.1 mrg tree type = non_reference (TREE_TYPE (var)); 3748 1.1 mrg type = build_reference_type (type); 3749 1.1 mrg tree fntype = build_function_type (type, void_list_node); 3750 1.1 mrg 3751 1.1 mrg fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var), 3752 1.1 mrg FUNCTION_DECL, sname, fntype); 3753 1.1 mrg SET_DECL_LANGUAGE (fn, lang_c); 3754 1.1 mrg TREE_PUBLIC (fn) = TREE_PUBLIC (var); 3755 1.1 mrg DECL_ARTIFICIAL (fn) = true; 3756 1.1 mrg DECL_IGNORED_P (fn) = 1; 3757 1.1 mrg /* The wrapper is inline and emitted everywhere var is used. */ 3758 1.1 mrg DECL_DECLARED_INLINE_P (fn) = true; 3759 1.1 mrg if (TREE_PUBLIC (var)) 3760 1.1 mrg { 3761 1.1 mrg comdat_linkage (fn); 3762 1.1 mrg #ifdef HAVE_GAS_HIDDEN 3763 1.1 mrg /* Make the wrapper bind locally; there's no reason to share 3764 1.1 mrg the wrapper between multiple shared objects. */ 3765 1.1 mrg DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL; 3766 1.1 mrg DECL_VISIBILITY_SPECIFIED (fn) = true; 3767 1.1 mrg #endif 3768 1.1 mrg } 3769 1.1 mrg if (!TREE_PUBLIC (fn)) 3770 1.1 mrg DECL_INTERFACE_KNOWN (fn) = true; 3771 1.1 mrg mark_used (fn); 3772 1.1 mrg note_vague_linkage_fn (fn); 3773 1.1 mrg 3774 1.1 mrg #if 0 3775 1.1 mrg /* We want CSE to commonize calls to the wrapper, but marking it as 3776 1.1 mrg pure is unsafe since it has side-effects. I guess we need a new 3777 1.1 mrg ECF flag even weaker than ECF_PURE. FIXME! */ 3778 1.1 mrg DECL_PURE_P (fn) = true; 3779 1.1 mrg #endif 3780 1.1 mrg 3781 1.1 mrg DECL_BEFRIENDING_CLASSES (fn) = var; 3782 1.1 mrg 3783 1.1 mrg set_global_binding (fn); 3784 1.1 mrg } 3785 1.1 mrg return fn; 3786 1.1 mrg } 3787 1.1 mrg 3788 1.1 mrg /* If EXPR is a thread_local variable that should be wrapped by init 3789 1.1 mrg wrapper function, return a call to that function, otherwise return 3790 1.1 mrg NULL. */ 3791 1.1 mrg 3792 1.1 mrg tree 3793 1.1 mrg maybe_get_tls_wrapper_call (tree expr) 3794 1.1 mrg { 3795 1.1 mrg if (VAR_P (expr) 3796 1.1 mrg && !processing_template_decl 3797 1.1 mrg && !cp_unevaluated_operand 3798 1.1 mrg && CP_DECL_THREAD_LOCAL_P (expr)) 3799 1.1 mrg if (tree wrap = get_tls_wrapper_fn (expr)) 3800 1.1 mrg return build_cxx_call (wrap, 0, NULL, tf_warning_or_error); 3801 1.1 mrg return NULL; 3802 1.1 mrg } 3803 1.1 mrg 3804 1.1 mrg /* At EOF, generate the definition for the TLS wrapper function FN: 3805 1.1 mrg 3806 1.1 mrg T& var_wrapper() { 3807 1.1 mrg if (init_fn) init_fn(); 3808 1.1 mrg return var; 3809 1.1 mrg } */ 3810 1.1 mrg 3811 1.1 mrg static void 3812 1.1 mrg generate_tls_wrapper (tree fn) 3813 1.1 mrg { 3814 1.1 mrg tree var = DECL_BEFRIENDING_CLASSES (fn); 3815 1.1 mrg 3816 1.1 mrg start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED); 3817 1.1 mrg tree body = begin_function_body (); 3818 1.1 mrg /* Only call the init fn if there might be one. */ 3819 1.1 mrg if (tree init_fn = get_tls_init_fn (var)) 3820 1.1 mrg { 3821 1.1 mrg tree if_stmt = NULL_TREE; 3822 1.1 mrg /* If init_fn is a weakref, make sure it exists before calling. */ 3823 1.1 mrg if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn))) 3824 1.1 mrg { 3825 1.1 mrg if_stmt = begin_if_stmt (); 3826 1.1 mrg tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error); 3827 1.1 mrg tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var), 3828 1.1 mrg NE_EXPR, addr, nullptr_node, 3829 1.1 mrg tf_warning_or_error); 3830 1.1 mrg finish_if_stmt_cond (cond, if_stmt); 3831 1.1 mrg } 3832 1.1 mrg finish_expr_stmt (build_cxx_call 3833 1.1 mrg (init_fn, 0, NULL, tf_warning_or_error)); 3834 1.1 mrg if (if_stmt) 3835 1.1 mrg { 3836 1.1 mrg finish_then_clause (if_stmt); 3837 1.1 mrg finish_if_stmt (if_stmt); 3838 1.1 mrg } 3839 1.1 mrg } 3840 1.1 mrg else 3841 1.1 mrg /* If there's no initialization, the wrapper is a constant function. */ 3842 1.1 mrg TREE_READONLY (fn) = true; 3843 1.1 mrg finish_return_stmt (convert_from_reference (var)); 3844 1.1 mrg finish_function_body (body); 3845 1.1 mrg expand_or_defer_fn (finish_function (/*inline_p=*/false)); 3846 1.1 mrg } 3847 1.1 mrg 3848 1.1 mrg /* Start the process of running a particular set of global constructors 3849 1.1 mrg or destructors. Subroutine of do_[cd]tors. Also called from 3850 1.1 mrg vtv_start_verification_constructor_init_function. */ 3851 1.1 mrg 3852 1.1 mrg static tree 3853 1.1 mrg start_objects (int method_type, int initp) 3854 1.1 mrg { 3855 1.1 mrg /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */ 3856 1.1 mrg int module_init = 0; 3857 1.1 mrg 3858 1.1 mrg if (initp == DEFAULT_INIT_PRIORITY && method_type == 'I') 3859 1.1 mrg module_init = module_initializer_kind (); 3860 1.1 mrg 3861 1.1 mrg tree name = NULL_TREE; 3862 1.1 mrg if (module_init > 0) 3863 1.1 mrg name = mangle_module_global_init (0); 3864 1.1 mrg else 3865 1.1 mrg { 3866 1.1 mrg char type[14]; 3867 1.1 mrg 3868 1.1 mrg unsigned len = sprintf (type, "sub_%c", method_type); 3869 1.1 mrg if (initp != DEFAULT_INIT_PRIORITY) 3870 1.1 mrg { 3871 1.1 mrg char joiner = '_'; 3872 1.1 mrg #ifdef JOINER 3873 1.1 mrg joiner = JOINER; 3874 1.1 mrg #endif 3875 1.1 mrg type[len++] = joiner; 3876 1.1 mrg sprintf (type + len, "%.5u", initp); 3877 1.1 mrg } 3878 1.1 mrg name = get_file_function_name (type); 3879 1.1 mrg } 3880 1.1 mrg 3881 1.1 mrg tree fntype = build_function_type (void_type_node, void_list_node); 3882 1.1 mrg tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype); 3883 1.1 mrg DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace); 3884 1.1 mrg if (module_init > 0) 3885 1.1 mrg { 3886 1.1 mrg SET_DECL_ASSEMBLER_NAME (fndecl, name); 3887 1.1 mrg TREE_PUBLIC (fndecl) = true; 3888 1.1 mrg determine_visibility (fndecl); 3889 1.1 mrg } 3890 1.1 mrg else 3891 1.1 mrg TREE_PUBLIC (fndecl) = 0; 3892 1.1 mrg start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED); 3893 1.1 mrg 3894 1.1 mrg /* Mark as artificial because it's not explicitly in the user's 3895 1.1 mrg source code. */ 3896 1.1 mrg DECL_ARTIFICIAL (current_function_decl) = 1; 3897 1.1 mrg 3898 1.1 mrg /* Mark this declaration as used to avoid spurious warnings. */ 3899 1.1 mrg TREE_USED (current_function_decl) = 1; 3900 1.1 mrg 3901 1.1 mrg /* Mark this function as a global constructor or destructor. */ 3902 1.1 mrg if (method_type == 'I') 3903 1.1 mrg DECL_GLOBAL_CTOR_P (current_function_decl) = 1; 3904 1.1 mrg else 3905 1.1 mrg DECL_GLOBAL_DTOR_P (current_function_decl) = 1; 3906 1.1 mrg 3907 1.1 mrg tree body = begin_compound_stmt (BCS_FN_BODY); 3908 1.1 mrg 3909 1.1 mrg if (module_init > 0) 3910 1.1 mrg { 3911 1.1 mrg // 'static bool __in_chrg = false; 3912 1.1 mrg // if (__inchrg) return; 3913 1.1 mrg // __inchrg = true 3914 1.1 mrg tree var = build_lang_decl (VAR_DECL, in_charge_identifier, 3915 1.1 mrg boolean_type_node); 3916 1.1 mrg DECL_CONTEXT (var) = fndecl; 3917 1.1 mrg DECL_ARTIFICIAL (var) = true; 3918 1.1 mrg TREE_STATIC (var) = true; 3919 1.1 mrg pushdecl (var); 3920 1.1 mrg cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0); 3921 1.1 mrg 3922 1.1 mrg tree if_stmt = begin_if_stmt (); 3923 1.1 mrg finish_if_stmt_cond (var, if_stmt); 3924 1.1 mrg finish_return_stmt (NULL_TREE); 3925 1.1 mrg finish_then_clause (if_stmt); 3926 1.1 mrg finish_if_stmt (if_stmt); 3927 1.1 mrg 3928 1.1 mrg tree assign = build2 (MODIFY_EXPR, boolean_type_node, 3929 1.1 mrg var, boolean_true_node); 3930 1.1 mrg TREE_SIDE_EFFECTS (assign) = true; 3931 1.1 mrg finish_expr_stmt (assign); 3932 1.1 mrg } 3933 1.1 mrg 3934 1.1 mrg if (module_init) 3935 1.1 mrg module_add_import_initializers (); 3936 1.1 mrg 3937 1.1 mrg return body; 3938 1.1 mrg } 3939 1.1 mrg 3940 1.1 mrg /* Finish the process of running a particular set of global constructors 3941 1.1 mrg or destructors. Subroutine of do_[cd]tors. */ 3942 1.1 mrg 3943 1.1 mrg static void 3944 1.1 mrg finish_objects (int method_type, int initp, tree body) 3945 1.1 mrg { 3946 1.1 mrg /* Finish up. */ 3947 1.1 mrg finish_compound_stmt (body); 3948 1.1 mrg tree fn = finish_function (/*inline_p=*/false); 3949 1.1 mrg 3950 1.1 mrg if (method_type == 'I') 3951 1.1 mrg { 3952 1.1 mrg DECL_STATIC_CONSTRUCTOR (fn) = 1; 3953 1.1 mrg decl_init_priority_insert (fn, initp); 3954 1.1 mrg } 3955 1.1 mrg else 3956 1.1 mrg { 3957 1.1 mrg DECL_STATIC_DESTRUCTOR (fn) = 1; 3958 1.1 mrg decl_fini_priority_insert (fn, initp); 3959 1.1 mrg } 3960 1.1 mrg 3961 1.1 mrg expand_or_defer_fn (fn); 3962 1.1 mrg } 3963 1.1 mrg 3964 1.1 mrg /* The names of the parameters to the function created to handle 3965 1.1 mrg initializations and destructions for objects with static storage 3966 1.1 mrg duration. */ 3967 1.1 mrg #define INITIALIZE_P_IDENTIFIER "__initialize_p" 3968 1.1 mrg #define PRIORITY_IDENTIFIER "__priority" 3969 1.1 mrg 3970 1.1 mrg /* The name of the function we create to handle initializations and 3971 1.1 mrg destructions for objects with static storage duration. */ 3972 1.1 mrg #define SSDF_IDENTIFIER "__static_initialization_and_destruction" 3973 1.1 mrg 3974 1.1 mrg /* The declaration for the __INITIALIZE_P argument. */ 3975 1.1 mrg static GTY(()) tree initialize_p_decl; 3976 1.1 mrg 3977 1.1 mrg /* The declaration for the __PRIORITY argument. */ 3978 1.1 mrg static GTY(()) tree priority_decl; 3979 1.1 mrg 3980 1.1 mrg /* The declaration for the static storage duration function. */ 3981 1.1 mrg static GTY(()) tree ssdf_decl; 3982 1.1 mrg 3983 1.1 mrg /* All the static storage duration functions created in this 3984 1.1 mrg translation unit. */ 3985 1.1 mrg static GTY(()) vec<tree, va_gc> *ssdf_decls; 3986 1.1 mrg 3987 1.1 mrg /* A map from priority levels to information about that priority 3988 1.1 mrg level. There may be many such levels, so efficient lookup is 3989 1.1 mrg important. */ 3990 1.1 mrg static splay_tree priority_info_map; 3991 1.1 mrg 3992 1.1 mrg /* Begins the generation of the function that will handle all 3993 1.1 mrg initialization and destruction of objects with static storage 3994 1.1 mrg duration. The function generated takes two parameters of type 3995 1.1 mrg `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is 3996 1.1 mrg nonzero, it performs initializations. Otherwise, it performs 3997 1.1 mrg destructions. It only performs those initializations or 3998 1.1 mrg destructions with the indicated __PRIORITY. The generated function 3999 1.1 mrg returns no value. 4000 1.1 mrg 4001 1.1 mrg It is assumed that this function will only be called once per 4002 1.1 mrg translation unit. */ 4003 1.1 mrg 4004 1.1 mrg static tree 4005 1.1 mrg start_static_storage_duration_function (unsigned count) 4006 1.1 mrg { 4007 1.1 mrg tree type; 4008 1.1 mrg tree body; 4009 1.1 mrg char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32]; 4010 1.1 mrg 4011 1.1 mrg /* Create the identifier for this function. It will be of the form 4012 1.1 mrg SSDF_IDENTIFIER_<number>. */ 4013 1.1 mrg sprintf (id, "%s_%u", SSDF_IDENTIFIER, count); 4014 1.1 mrg 4015 1.1 mrg type = build_function_type_list (void_type_node, 4016 1.1 mrg integer_type_node, integer_type_node, 4017 1.1 mrg NULL_TREE); 4018 1.1 mrg 4019 1.1 mrg /* Create the FUNCTION_DECL itself. */ 4020 1.1 mrg ssdf_decl = build_lang_decl (FUNCTION_DECL, 4021 1.1 mrg get_identifier (id), 4022 1.1 mrg type); 4023 1.1 mrg TREE_PUBLIC (ssdf_decl) = 0; 4024 1.1 mrg DECL_ARTIFICIAL (ssdf_decl) = 1; 4025 1.1 mrg 4026 1.1 mrg /* Put this function in the list of functions to be called from the 4027 1.1 mrg static constructors and destructors. */ 4028 1.1 mrg if (!ssdf_decls) 4029 1.1 mrg { 4030 1.1 mrg vec_alloc (ssdf_decls, 32); 4031 1.1 mrg 4032 1.1 mrg /* Take this opportunity to initialize the map from priority 4033 1.1 mrg numbers to information about that priority level. */ 4034 1.1 mrg priority_info_map = splay_tree_new (splay_tree_compare_ints, 4035 1.1 mrg /*delete_key_fn=*/0, 4036 1.1 mrg /*delete_value_fn=*/ 4037 1.1 mrg splay_tree_delete_pointers); 4038 1.1 mrg 4039 1.1 mrg /* We always need to generate functions for the 4040 1.1 mrg DEFAULT_INIT_PRIORITY so enter it now. That way when we walk 4041 1.1 mrg priorities later, we'll be sure to find the 4042 1.1 mrg DEFAULT_INIT_PRIORITY. */ 4043 1.1 mrg get_priority_info (DEFAULT_INIT_PRIORITY); 4044 1.1 mrg } 4045 1.1 mrg 4046 1.1 mrg vec_safe_push (ssdf_decls, ssdf_decl); 4047 1.1 mrg 4048 1.1 mrg /* Create the argument list. */ 4049 1.1 mrg initialize_p_decl = cp_build_parm_decl 4050 1.1 mrg (ssdf_decl, get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node); 4051 1.1 mrg TREE_USED (initialize_p_decl) = 1; 4052 1.1 mrg priority_decl = cp_build_parm_decl 4053 1.1 mrg (ssdf_decl, get_identifier (PRIORITY_IDENTIFIER), integer_type_node); 4054 1.1 mrg TREE_USED (priority_decl) = 1; 4055 1.1 mrg 4056 1.1 mrg DECL_CHAIN (initialize_p_decl) = priority_decl; 4057 1.1 mrg DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl; 4058 1.1 mrg 4059 1.1 mrg /* Put the function in the global scope. */ 4060 1.1 mrg pushdecl (ssdf_decl); 4061 1.1 mrg 4062 1.1 mrg /* Start the function itself. This is equivalent to declaring the 4063 1.1 mrg function as: 4064 1.1 mrg 4065 1.1 mrg static void __ssdf (int __initialize_p, init __priority_p); 4066 1.1 mrg 4067 1.1 mrg It is static because we only need to call this function from the 4068 1.1 mrg various constructor and destructor functions for this module. */ 4069 1.1 mrg start_preparsed_function (ssdf_decl, 4070 1.1 mrg /*attrs=*/NULL_TREE, 4071 1.1 mrg SF_PRE_PARSED); 4072 1.1 mrg 4073 1.1 mrg /* Set up the scope of the outermost block in the function. */ 4074 1.1 mrg body = begin_compound_stmt (BCS_FN_BODY); 4075 1.1 mrg 4076 1.1 mrg return body; 4077 1.1 mrg } 4078 1.1 mrg 4079 1.1 mrg /* Finish the generation of the function which performs initialization 4080 1.1 mrg and destruction of objects with static storage duration. After 4081 1.1 mrg this point, no more such objects can be created. */ 4082 1.1 mrg 4083 1.1 mrg static void 4084 1.1 mrg finish_static_storage_duration_function (tree body) 4085 1.1 mrg { 4086 1.1 mrg /* Close out the function. */ 4087 1.1 mrg finish_compound_stmt (body); 4088 1.1 mrg expand_or_defer_fn (finish_function (/*inline_p=*/false)); 4089 1.1 mrg } 4090 1.1 mrg 4091 1.1 mrg /* Return the information about the indicated PRIORITY level. If no 4092 1.1 mrg code to handle this level has yet been generated, generate the 4093 1.1 mrg appropriate prologue. */ 4094 1.1 mrg 4095 1.1 mrg static priority_info 4096 1.1 mrg get_priority_info (int priority) 4097 1.1 mrg { 4098 1.1 mrg priority_info pi; 4099 1.1 mrg splay_tree_node n; 4100 1.1 mrg 4101 1.1 mrg n = splay_tree_lookup (priority_info_map, 4102 1.1 mrg (splay_tree_key) priority); 4103 1.1 mrg if (!n) 4104 1.1 mrg { 4105 1.1 mrg /* Create a new priority information structure, and insert it 4106 1.1 mrg into the map. */ 4107 1.1 mrg pi = XNEW (struct priority_info_s); 4108 1.1 mrg pi->initializations_p = 0; 4109 1.1 mrg pi->destructions_p = 0; 4110 1.1 mrg splay_tree_insert (priority_info_map, 4111 1.1 mrg (splay_tree_key) priority, 4112 1.1 mrg (splay_tree_value) pi); 4113 1.1 mrg } 4114 1.1 mrg else 4115 1.1 mrg pi = (priority_info) n->value; 4116 1.1 mrg 4117 1.1 mrg return pi; 4118 1.1 mrg } 4119 1.1 mrg 4120 1.1 mrg /* The effective initialization priority of a DECL. */ 4121 1.1 mrg 4122 1.1 mrg #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \ 4123 1.1 mrg ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \ 4124 1.1 mrg ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl)) 4125 1.1 mrg 4126 1.1 mrg /* Whether a DECL needs a guard to protect it against multiple 4127 1.1 mrg initialization. */ 4128 1.1 mrg 4129 1.1 mrg #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \ 4130 1.1 mrg || DECL_ONE_ONLY (decl) \ 4131 1.1 mrg || DECL_WEAK (decl))) 4132 1.1 mrg 4133 1.1 mrg /* Called from one_static_initialization_or_destruction(), 4134 1.1 mrg via walk_tree. 4135 1.1 mrg Walks the initializer list of a global variable and looks for 4136 1.1 mrg temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0) 4137 1.1 mrg and that have their DECL_CONTEXT() == NULL. 4138 1.1 mrg For each such temporary variable, set their DECL_CONTEXT() to 4139 1.1 mrg the current function. This is necessary because otherwise 4140 1.1 mrg some optimizers (enabled by -O2 -fprofile-arcs) might crash 4141 1.1 mrg when trying to refer to a temporary variable that does not have 4142 1.1 mrg it's DECL_CONTECT() properly set. */ 4143 1.1 mrg static tree 4144 1.1 mrg fix_temporary_vars_context_r (tree *node, 4145 1.1 mrg int * /*unused*/, 4146 1.1 mrg void * /*unused1*/) 4147 1.1 mrg { 4148 1.1 mrg gcc_assert (current_function_decl); 4149 1.1 mrg 4150 1.1 mrg if (TREE_CODE (*node) == BIND_EXPR) 4151 1.1 mrg { 4152 1.1 mrg tree var; 4153 1.1 mrg 4154 1.1 mrg for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var)) 4155 1.1 mrg if (VAR_P (var) 4156 1.1 mrg && !DECL_NAME (var) 4157 1.1 mrg && DECL_ARTIFICIAL (var) 4158 1.1 mrg && !DECL_CONTEXT (var)) 4159 1.1 mrg DECL_CONTEXT (var) = current_function_decl; 4160 1.1 mrg } 4161 1.1 mrg 4162 1.1 mrg return NULL_TREE; 4163 1.1 mrg } 4164 1.1 mrg 4165 1.1 mrg /* Set up to handle the initialization or destruction of DECL. If 4166 1.1 mrg INITP is nonzero, we are initializing the variable. Otherwise, we 4167 1.1 mrg are destroying it. */ 4168 1.1 mrg 4169 1.1 mrg static void 4170 1.1 mrg one_static_initialization_or_destruction (tree decl, tree init, bool initp) 4171 1.1 mrg { 4172 1.1 mrg tree guard_if_stmt = NULL_TREE; 4173 1.1 mrg tree guard; 4174 1.1 mrg 4175 1.1 mrg /* If we are supposed to destruct and there's a trivial destructor, 4176 1.1 mrg nothing has to be done. */ 4177 1.1 mrg if (!initp 4178 1.1 mrg && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 4179 1.1 mrg return; 4180 1.1 mrg 4181 1.1 mrg /* Trick the compiler into thinking we are at the file and line 4182 1.1 mrg where DECL was declared so that error-messages make sense, and so 4183 1.1 mrg that the debugger will show somewhat sensible file and line 4184 1.1 mrg information. */ 4185 1.1 mrg input_location = DECL_SOURCE_LOCATION (decl); 4186 1.1 mrg 4187 1.1 mrg /* Make sure temporary variables in the initialiser all have 4188 1.1 mrg their DECL_CONTEXT() set to a value different from NULL_TREE. 4189 1.1 mrg This can happen when global variables initializers are built. 4190 1.1 mrg In that case, the DECL_CONTEXT() of the global variables _AND_ of all 4191 1.1 mrg the temporary variables that might have been generated in the 4192 1.1 mrg accompanying initializers is NULL_TREE, meaning the variables have been 4193 1.1 mrg declared in the global namespace. 4194 1.1 mrg What we want to do here is to fix that and make sure the DECL_CONTEXT() 4195 1.1 mrg of the temporaries are set to the current function decl. */ 4196 1.1 mrg cp_walk_tree_without_duplicates (&init, 4197 1.1 mrg fix_temporary_vars_context_r, 4198 1.1 mrg NULL); 4199 1.1 mrg 4200 1.1 mrg /* Because of: 4201 1.1 mrg 4202 1.1 mrg [class.access.spec] 4203 1.1 mrg 4204 1.1 mrg Access control for implicit calls to the constructors, 4205 1.1 mrg the conversion functions, or the destructor called to 4206 1.1 mrg create and destroy a static data member is performed as 4207 1.1 mrg if these calls appeared in the scope of the member's 4208 1.1 mrg class. 4209 1.1 mrg 4210 1.1 mrg we pretend we are in a static member function of the class of 4211 1.1 mrg which the DECL is a member. */ 4212 1.1 mrg if (member_p (decl)) 4213 1.1 mrg { 4214 1.1 mrg DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl); 4215 1.1 mrg DECL_STATIC_FUNCTION_P (current_function_decl) = 1; 4216 1.1 mrg } 4217 1.1 mrg 4218 1.1 mrg /* Assume we don't need a guard. */ 4219 1.1 mrg guard = NULL_TREE; 4220 1.1 mrg /* We need a guard if this is an object with external linkage that 4221 1.1 mrg might be initialized in more than one place. (For example, a 4222 1.1 mrg static data member of a template, when the data member requires 4223 1.1 mrg construction.) */ 4224 1.1 mrg if (NEEDS_GUARD_P (decl)) 4225 1.1 mrg { 4226 1.1 mrg tree guard_cond; 4227 1.1 mrg 4228 1.1 mrg guard = get_guard (decl); 4229 1.1 mrg 4230 1.1 mrg /* When using __cxa_atexit, we just check the GUARD as we would 4231 1.1 mrg for a local static. */ 4232 1.1 mrg if (flag_use_cxa_atexit) 4233 1.1 mrg { 4234 1.1 mrg /* When using __cxa_atexit, we never try to destroy 4235 1.1 mrg anything from a static destructor. */ 4236 1.1 mrg gcc_assert (initp); 4237 1.1 mrg guard_cond = get_guard_cond (guard, false); 4238 1.1 mrg } 4239 1.1 mrg /* If we don't have __cxa_atexit, then we will be running 4240 1.1 mrg destructors from .fini sections, or their equivalents. So, 4241 1.1 mrg we need to know how many times we've tried to initialize this 4242 1.1 mrg object. We do initializations only if the GUARD is zero, 4243 1.1 mrg i.e., if we are the first to initialize the variable. We do 4244 1.1 mrg destructions only if the GUARD is one, i.e., if we are the 4245 1.1 mrg last to destroy the variable. */ 4246 1.1 mrg else if (initp) 4247 1.1 mrg guard_cond 4248 1.1 mrg = cp_build_binary_op (input_location, 4249 1.1 mrg EQ_EXPR, 4250 1.1 mrg cp_build_unary_op (PREINCREMENT_EXPR, 4251 1.1 mrg guard, 4252 1.1 mrg /*noconvert=*/true, 4253 1.1 mrg tf_warning_or_error), 4254 1.1 mrg integer_one_node, 4255 1.1 mrg tf_warning_or_error); 4256 1.1 mrg else 4257 1.1 mrg guard_cond 4258 1.1 mrg = cp_build_binary_op (input_location, 4259 1.1 mrg EQ_EXPR, 4260 1.1 mrg cp_build_unary_op (PREDECREMENT_EXPR, 4261 1.1 mrg guard, 4262 1.1 mrg /*noconvert=*/true, 4263 1.1 mrg tf_warning_or_error), 4264 1.1 mrg integer_zero_node, 4265 1.1 mrg tf_warning_or_error); 4266 1.1 mrg 4267 1.1 mrg guard_if_stmt = begin_if_stmt (); 4268 1.1 mrg finish_if_stmt_cond (guard_cond, guard_if_stmt); 4269 1.1 mrg } 4270 1.1 mrg 4271 1.1 mrg 4272 1.1 mrg /* If we're using __cxa_atexit, we have not already set the GUARD, 4273 1.1 mrg so we must do so now. */ 4274 1.1 mrg if (guard && initp && flag_use_cxa_atexit) 4275 1.1 mrg finish_expr_stmt (set_guard (guard)); 4276 1.1 mrg 4277 1.1 mrg /* Perform the initialization or destruction. */ 4278 1.1 mrg if (initp) 4279 1.1 mrg { 4280 1.1 mrg if (init) 4281 1.1 mrg { 4282 1.1 mrg finish_expr_stmt (init); 4283 1.1 mrg if (sanitize_flags_p (SANITIZE_ADDRESS, decl)) 4284 1.1 mrg { 4285 1.1 mrg varpool_node *vnode = varpool_node::get (decl); 4286 1.1 mrg if (vnode) 4287 1.1 mrg vnode->dynamically_initialized = 1; 4288 1.1 mrg } 4289 1.1 mrg } 4290 1.1 mrg 4291 1.1 mrg /* If we're using __cxa_atexit, register a function that calls the 4292 1.1 mrg destructor for the object. */ 4293 1.1 mrg if (flag_use_cxa_atexit) 4294 1.1 mrg finish_expr_stmt (register_dtor_fn (decl)); 4295 1.1 mrg } 4296 1.1 mrg else 4297 1.1 mrg finish_expr_stmt (build_cleanup (decl)); 4298 1.1 mrg 4299 1.1 mrg /* Finish the guard if-stmt, if necessary. */ 4300 1.1 mrg if (guard) 4301 1.1 mrg { 4302 1.1 mrg finish_then_clause (guard_if_stmt); 4303 1.1 mrg finish_if_stmt (guard_if_stmt); 4304 1.1 mrg } 4305 1.1 mrg 4306 1.1 mrg /* Now that we're done with DECL we don't need to pretend to be a 4307 1.1 mrg member of its class any longer. */ 4308 1.1 mrg DECL_CONTEXT (current_function_decl) = NULL_TREE; 4309 1.1 mrg DECL_STATIC_FUNCTION_P (current_function_decl) = 0; 4310 1.1 mrg } 4311 1.1 mrg 4312 1.1 mrg /* Generate code to do the initialization or destruction of the decls in VARS, 4313 1.1 mrg a TREE_LIST of VAR_DECL with static storage duration. 4314 1.1 mrg Whether initialization or destruction is performed is specified by INITP. */ 4315 1.1 mrg 4316 1.1 mrg static void 4317 1.1 mrg do_static_initialization_or_destruction (tree vars, bool initp) 4318 1.1 mrg { 4319 1.1 mrg tree node, init_if_stmt, cond; 4320 1.1 mrg 4321 1.1 mrg /* Build the outer if-stmt to check for initialization or destruction. */ 4322 1.1 mrg init_if_stmt = begin_if_stmt (); 4323 1.1 mrg cond = initp ? integer_one_node : integer_zero_node; 4324 1.1 mrg cond = cp_build_binary_op (input_location, 4325 1.1 mrg EQ_EXPR, 4326 1.1 mrg initialize_p_decl, 4327 1.1 mrg cond, 4328 1.1 mrg tf_warning_or_error); 4329 1.1 mrg finish_if_stmt_cond (cond, init_if_stmt); 4330 1.1 mrg 4331 1.1 mrg /* To make sure dynamic construction doesn't access globals from other 4332 1.1 mrg compilation units where they might not be yet constructed, for 4333 1.1 mrg -fsanitize=address insert __asan_before_dynamic_init call that 4334 1.1 mrg prevents access to either all global variables that need construction 4335 1.1 mrg in other compilation units, or at least those that haven't been 4336 1.1 mrg initialized yet. Variables that need dynamic construction in 4337 1.1 mrg the current compilation unit are kept accessible. */ 4338 1.1 mrg if (initp && (flag_sanitize & SANITIZE_ADDRESS)) 4339 1.1 mrg finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false)); 4340 1.1 mrg 4341 1.1 mrg node = vars; 4342 1.1 mrg do { 4343 1.1 mrg tree decl = TREE_VALUE (node); 4344 1.1 mrg tree priority_if_stmt; 4345 1.1 mrg int priority; 4346 1.1 mrg priority_info pi; 4347 1.1 mrg 4348 1.1 mrg /* If we don't need a destructor, there's nothing to do. Avoid 4349 1.1 mrg creating a possibly empty if-stmt. */ 4350 1.1 mrg if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 4351 1.1 mrg { 4352 1.1 mrg node = TREE_CHAIN (node); 4353 1.1 mrg continue; 4354 1.1 mrg } 4355 1.1 mrg 4356 1.1 mrg /* Remember that we had an initialization or finalization at this 4357 1.1 mrg priority. */ 4358 1.1 mrg priority = DECL_EFFECTIVE_INIT_PRIORITY (decl); 4359 1.1 mrg pi = get_priority_info (priority); 4360 1.1 mrg if (initp) 4361 1.1 mrg pi->initializations_p = 1; 4362 1.1 mrg else 4363 1.1 mrg pi->destructions_p = 1; 4364 1.1 mrg 4365 1.1 mrg /* Conditionalize this initialization on being in the right priority 4366 1.1 mrg and being initializing/finalizing appropriately. */ 4367 1.1 mrg priority_if_stmt = begin_if_stmt (); 4368 1.1 mrg cond = cp_build_binary_op (input_location, 4369 1.1 mrg EQ_EXPR, 4370 1.1 mrg priority_decl, 4371 1.1 mrg build_int_cst (NULL_TREE, priority), 4372 1.1 mrg tf_warning_or_error); 4373 1.1 mrg finish_if_stmt_cond (cond, priority_if_stmt); 4374 1.1 mrg 4375 1.1 mrg /* Process initializers with same priority. */ 4376 1.1 mrg for (; node 4377 1.1 mrg && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority; 4378 1.1 mrg node = TREE_CHAIN (node)) 4379 1.1 mrg /* Do one initialization or destruction. */ 4380 1.1 mrg one_static_initialization_or_destruction (TREE_VALUE (node), 4381 1.1 mrg TREE_PURPOSE (node), initp); 4382 1.1 mrg 4383 1.1 mrg /* Finish up the priority if-stmt body. */ 4384 1.1 mrg finish_then_clause (priority_if_stmt); 4385 1.1 mrg finish_if_stmt (priority_if_stmt); 4386 1.1 mrg 4387 1.1 mrg } while (node); 4388 1.1 mrg 4389 1.1 mrg /* Revert what __asan_before_dynamic_init did by calling 4390 1.1 mrg __asan_after_dynamic_init. */ 4391 1.1 mrg if (initp && (flag_sanitize & SANITIZE_ADDRESS)) 4392 1.1 mrg finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true)); 4393 1.1 mrg 4394 1.1 mrg /* Finish up the init/destruct if-stmt body. */ 4395 1.1 mrg finish_then_clause (init_if_stmt); 4396 1.1 mrg finish_if_stmt (init_if_stmt); 4397 1.1 mrg } 4398 1.1 mrg 4399 1.1 mrg /* VARS is a list of variables with static storage duration which may 4400 1.1 mrg need initialization and/or finalization. Remove those variables 4401 1.1 mrg that don't really need to be initialized or finalized, and return 4402 1.1 mrg the resulting list. The order in which the variables appear in 4403 1.1 mrg VARS is in reverse order of the order in which they should actually 4404 1.1 mrg be initialized. The list we return is in the unreversed order; 4405 1.1 mrg i.e., the first variable should be initialized first. */ 4406 1.1 mrg 4407 1.1 mrg static tree 4408 1.1 mrg prune_vars_needing_no_initialization (tree *vars) 4409 1.1 mrg { 4410 1.1 mrg tree *var = vars; 4411 1.1 mrg tree result = NULL_TREE; 4412 1.1 mrg 4413 1.1 mrg while (*var) 4414 1.1 mrg { 4415 1.1 mrg tree t = *var; 4416 1.1 mrg tree decl = TREE_VALUE (t); 4417 1.1 mrg tree init = TREE_PURPOSE (t); 4418 1.1 mrg 4419 1.1 mrg /* Deal gracefully with error. */ 4420 1.1 mrg if (error_operand_p (decl)) 4421 1.1 mrg { 4422 1.1 mrg var = &TREE_CHAIN (t); 4423 1.1 mrg continue; 4424 1.1 mrg } 4425 1.1 mrg 4426 1.1 mrg /* The only things that can be initialized are variables. */ 4427 1.1 mrg gcc_assert (VAR_P (decl)); 4428 1.1 mrg 4429 1.1 mrg /* If this object is not defined, we don't need to do anything 4430 1.1 mrg here. */ 4431 1.1 mrg if (DECL_EXTERNAL (decl)) 4432 1.1 mrg { 4433 1.1 mrg var = &TREE_CHAIN (t); 4434 1.1 mrg continue; 4435 1.1 mrg } 4436 1.1 mrg 4437 1.1 mrg /* Also, if the initializer already contains errors, we can bail 4438 1.1 mrg out now. */ 4439 1.1 mrg if (init && TREE_CODE (init) == TREE_LIST 4440 1.1 mrg && value_member (error_mark_node, init)) 4441 1.1 mrg { 4442 1.1 mrg var = &TREE_CHAIN (t); 4443 1.1 mrg continue; 4444 1.1 mrg } 4445 1.1 mrg 4446 1.1 mrg /* This variable is going to need initialization and/or 4447 1.1 mrg finalization, so we add it to the list. */ 4448 1.1 mrg *var = TREE_CHAIN (t); 4449 1.1 mrg TREE_CHAIN (t) = result; 4450 1.1 mrg result = t; 4451 1.1 mrg } 4452 1.1 mrg 4453 1.1 mrg return result; 4454 1.1 mrg } 4455 1.1 mrg 4456 1.1 mrg /* Make sure we have told the back end about all the variables in 4457 1.1 mrg VARS. */ 4458 1.1 mrg 4459 1.1 mrg static void 4460 1.1 mrg write_out_vars (tree vars) 4461 1.1 mrg { 4462 1.1 mrg tree v; 4463 1.1 mrg 4464 1.1 mrg for (v = vars; v; v = TREE_CHAIN (v)) 4465 1.1 mrg { 4466 1.1 mrg tree var = TREE_VALUE (v); 4467 1.1 mrg if (!var_finalized_p (var)) 4468 1.1 mrg { 4469 1.1 mrg import_export_decl (var); 4470 1.1 mrg rest_of_decl_compilation (var, 1, 1); 4471 1.1 mrg } 4472 1.1 mrg } 4473 1.1 mrg } 4474 1.1 mrg 4475 1.1 mrg /* Generate a static constructor (if CONSTRUCTOR_P) or destructor 4476 1.1 mrg (otherwise) that will initialize all global objects with static 4477 1.1 mrg storage duration having the indicated PRIORITY. */ 4478 1.1 mrg 4479 1.1 mrg static void 4480 1.1 mrg generate_ctor_or_dtor_function (bool constructor_p, int priority, 4481 1.1 mrg location_t *locus) 4482 1.1 mrg { 4483 1.1 mrg input_location = *locus; 4484 1.1 mrg 4485 1.1 mrg /* We use `I' to indicate initialization and `D' to indicate 4486 1.1 mrg destruction. */ 4487 1.1 mrg char function_key = constructor_p ? 'I' : 'D'; 4488 1.1 mrg 4489 1.1 mrg /* We emit the function lazily, to avoid generating empty 4490 1.1 mrg global constructors and destructors. */ 4491 1.1 mrg tree body = NULL_TREE; 4492 1.1 mrg 4493 1.1 mrg if (constructor_p && priority == DEFAULT_INIT_PRIORITY) 4494 1.1 mrg { 4495 1.1 mrg bool objc = c_dialect_objc () && objc_static_init_needed_p (); 4496 1.1 mrg 4497 1.1 mrg /* We may have module initialization to emit and/or insert 4498 1.1 mrg before other intializations. */ 4499 1.1 mrg if (module_initializer_kind () || objc) 4500 1.1 mrg body = start_objects (function_key, priority); 4501 1.1 mrg 4502 1.1 mrg /* For Objective-C++, we may need to initialize metadata found 4503 1.1 mrg in this module. This must be done _before_ any other static 4504 1.1 mrg initializations. */ 4505 1.1 mrg if (objc) 4506 1.1 mrg objc_generate_static_init_call (NULL_TREE); 4507 1.1 mrg } 4508 1.1 mrg 4509 1.1 mrg /* Call the static storage duration function with appropriate 4510 1.1 mrg arguments. */ 4511 1.1 mrg tree fndecl; 4512 1.1 mrg size_t i; 4513 1.1 mrg FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl) 4514 1.1 mrg { 4515 1.1 mrg /* Calls to pure or const functions will expand to nothing. */ 4516 1.1 mrg if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE))) 4517 1.1 mrg { 4518 1.1 mrg if (! body) 4519 1.1 mrg body = start_objects (function_key, priority); 4520 1.1 mrg 4521 1.1 mrg tree call = cp_build_function_call_nary (fndecl, tf_warning_or_error, 4522 1.1 mrg build_int_cst (NULL_TREE, 4523 1.1 mrg constructor_p), 4524 1.1 mrg build_int_cst (NULL_TREE, 4525 1.1 mrg priority), 4526 1.1 mrg NULL_TREE); 4527 1.1 mrg finish_expr_stmt (call); 4528 1.1 mrg } 4529 1.1 mrg } 4530 1.1 mrg 4531 1.1 mrg /* Close out the function. */ 4532 1.1 mrg if (body) 4533 1.1 mrg finish_objects (function_key, priority, body); 4534 1.1 mrg } 4535 1.1 mrg 4536 1.1 mrg /* Generate constructor and destructor functions for the priority 4537 1.1 mrg indicated by N. */ 4538 1.1 mrg 4539 1.1 mrg static int 4540 1.1 mrg generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data) 4541 1.1 mrg { 4542 1.1 mrg location_t *locus = (location_t *) data; 4543 1.1 mrg int priority = (int) n->key; 4544 1.1 mrg priority_info pi = (priority_info) n->value; 4545 1.1 mrg 4546 1.1 mrg /* Generate the functions themselves, but only if they are really 4547 1.1 mrg needed. */ 4548 1.1 mrg if (pi->initializations_p) 4549 1.1 mrg generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus); 4550 1.1 mrg if (pi->destructions_p) 4551 1.1 mrg generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus); 4552 1.1 mrg 4553 1.1 mrg /* Keep iterating. */ 4554 1.1 mrg return 0; 4555 1.1 mrg } 4556 1.1 mrg 4557 1.1 mrg /* Return C++ property of T, based on given operation OP. */ 4558 1.1 mrg 4559 1.1 mrg static int 4560 1.1 mrg cpp_check (tree t, cpp_operation op) 4561 1.1 mrg { 4562 1.1 mrg switch (op) 4563 1.1 mrg { 4564 1.1 mrg case HAS_DEPENDENT_TEMPLATE_ARGS: 4565 1.1 mrg { 4566 1.1 mrg tree ti = CLASSTYPE_TEMPLATE_INFO (t); 4567 1.1 mrg if (!ti) 4568 1.1 mrg return 0; 4569 1.1 mrg ++processing_template_decl; 4570 1.1 mrg const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti)); 4571 1.1 mrg --processing_template_decl; 4572 1.1 mrg return dep; 4573 1.1 mrg } 4574 1.1 mrg case IS_ABSTRACT: 4575 1.1 mrg return DECL_PURE_VIRTUAL_P (t); 4576 1.1 mrg case IS_ASSIGNMENT_OPERATOR: 4577 1.1 mrg return DECL_ASSIGNMENT_OPERATOR_P (t); 4578 1.1 mrg case IS_CONSTRUCTOR: 4579 1.1 mrg return DECL_CONSTRUCTOR_P (t); 4580 1.1 mrg case IS_DESTRUCTOR: 4581 1.1 mrg return DECL_DESTRUCTOR_P (t); 4582 1.1 mrg case IS_COPY_CONSTRUCTOR: 4583 1.1 mrg return DECL_COPY_CONSTRUCTOR_P (t); 4584 1.1 mrg case IS_MOVE_CONSTRUCTOR: 4585 1.1 mrg return DECL_MOVE_CONSTRUCTOR_P (t); 4586 1.1 mrg case IS_TEMPLATE: 4587 1.1 mrg return TREE_CODE (t) == TEMPLATE_DECL; 4588 1.1 mrg case IS_TRIVIAL: 4589 1.1 mrg return trivial_type_p (t); 4590 1.1 mrg default: 4591 1.1 mrg return 0; 4592 1.1 mrg } 4593 1.1 mrg } 4594 1.1 mrg 4595 1.1 mrg /* Collect source file references recursively, starting from NAMESPC. */ 4596 1.1 mrg 4597 1.1 mrg static void 4598 1.1 mrg collect_source_refs (tree namespc) 4599 1.1 mrg { 4600 1.1 mrg /* Iterate over names in this name space. */ 4601 1.1 mrg for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t)) 4602 1.1 mrg if (DECL_IS_UNDECLARED_BUILTIN (t)) 4603 1.1 mrg ; 4604 1.1 mrg else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t)) 4605 1.1 mrg collect_source_refs (t); 4606 1.1 mrg else 4607 1.1 mrg collect_source_ref (DECL_SOURCE_FILE (t)); 4608 1.1 mrg } 4609 1.1 mrg 4610 1.1 mrg /* Collect decls relevant to SOURCE_FILE from all namespaces recursively, 4611 1.1 mrg starting from NAMESPC. */ 4612 1.1 mrg 4613 1.1 mrg static void 4614 1.1 mrg collect_ada_namespace (tree namespc, const char *source_file) 4615 1.1 mrg { 4616 1.1 mrg tree decl = NAMESPACE_LEVEL (namespc)->names; 4617 1.1 mrg 4618 1.1 mrg /* Collect decls from this namespace. This will skip 4619 1.1 mrg NAMESPACE_DECLs (both aliases and regular, it cannot tell). */ 4620 1.1 mrg collect_ada_nodes (decl, source_file); 4621 1.1 mrg 4622 1.1 mrg /* Now scan for namespace children, and dump them. */ 4623 1.1 mrg for (; decl; decl = TREE_CHAIN (decl)) 4624 1.1 mrg if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)) 4625 1.1 mrg collect_ada_namespace (decl, source_file); 4626 1.1 mrg } 4627 1.1 mrg 4628 1.1 mrg /* Returns true iff there is a definition available for variable or 4629 1.1 mrg function DECL. */ 4630 1.1 mrg 4631 1.1 mrg bool 4632 1.1 mrg decl_defined_p (tree decl) 4633 1.1 mrg { 4634 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL) 4635 1.1 mrg return (DECL_INITIAL (decl) != NULL_TREE 4636 1.1 mrg /* A pending instantiation of a friend temploid is defined. */ 4637 1.1 mrg || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) 4638 1.1 mrg && DECL_INITIAL (DECL_TEMPLATE_RESULT 4639 1.1 mrg (DECL_TI_TEMPLATE (decl))))); 4640 1.1 mrg else 4641 1.1 mrg { 4642 1.1 mrg gcc_assert (VAR_P (decl)); 4643 1.1 mrg return !DECL_EXTERNAL (decl); 4644 1.1 mrg } 4645 1.1 mrg } 4646 1.1 mrg 4647 1.1 mrg /* Nonzero for a VAR_DECL whose value can be used in a constant expression. 4648 1.1 mrg 4649 1.1 mrg [expr.const] 4650 1.1 mrg 4651 1.1 mrg An integral constant-expression can only involve ... const 4652 1.1 mrg variables of integral or enumeration types initialized with 4653 1.1 mrg constant expressions ... 4654 1.1 mrg 4655 1.1 mrg C++0x also allows constexpr variables and temporaries initialized 4656 1.1 mrg with constant expressions. We handle the former here, but the latter 4657 1.1 mrg are just folded away in cxx_eval_constant_expression. 4658 1.1 mrg 4659 1.1 mrg The standard does not require that the expression be non-volatile. 4660 1.1 mrg G++ implements the proposed correction in DR 457. */ 4661 1.1 mrg 4662 1.1 mrg bool 4663 1.1 mrg decl_constant_var_p (tree decl) 4664 1.1 mrg { 4665 1.1 mrg if (!decl_maybe_constant_var_p (decl)) 4666 1.1 mrg return false; 4667 1.1 mrg 4668 1.1 mrg /* We don't know if a template static data member is initialized with 4669 1.1 mrg a constant expression until we instantiate its initializer. Even 4670 1.1 mrg in the case of a constexpr variable, we can't treat it as a 4671 1.1 mrg constant until its initializer is complete in case it's used in 4672 1.1 mrg its own initializer. */ 4673 1.1 mrg maybe_instantiate_decl (decl); 4674 1.1 mrg return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl); 4675 1.1 mrg } 4676 1.1 mrg 4677 1.1 mrg /* Returns true if DECL could be a symbolic constant variable, depending on 4678 1.1 mrg its initializer. */ 4679 1.1 mrg 4680 1.1 mrg bool 4681 1.1 mrg decl_maybe_constant_var_p (tree decl) 4682 1.1 mrg { 4683 1.1 mrg tree type = TREE_TYPE (decl); 4684 1.1 mrg if (!VAR_P (decl)) 4685 1.1 mrg return false; 4686 1.1 mrg if (DECL_DECLARED_CONSTEXPR_P (decl) 4687 1.1 mrg && (!TREE_THIS_VOLATILE (decl) || NULLPTR_TYPE_P (type))) 4688 1.1 mrg return true; 4689 1.1 mrg if (DECL_HAS_VALUE_EXPR_P (decl)) 4690 1.1 mrg /* A proxy isn't constant. */ 4691 1.1 mrg return false; 4692 1.1 mrg if (TYPE_REF_P (type)) 4693 1.1 mrg /* References can be constant. */; 4694 1.1 mrg else if (CP_TYPE_CONST_NON_VOLATILE_P (type) 4695 1.1 mrg && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 4696 1.1 mrg /* And const integers. */; 4697 1.1 mrg else 4698 1.1 mrg return false; 4699 1.1 mrg 4700 1.1 mrg if (DECL_INITIAL (decl) 4701 1.1 mrg && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) 4702 1.1 mrg /* We know the initializer, and it isn't constant. */ 4703 1.1 mrg return false; 4704 1.1 mrg else 4705 1.1 mrg return true; 4706 1.1 mrg } 4707 1.1 mrg 4708 1.1 mrg /* Complain that DECL uses a type with no linkage. In C++98 mode this is 4709 1.1 mrg called from grokfndecl and grokvardecl; in all modes it is called from 4710 1.1 mrg cp_write_global_declarations. */ 4711 1.1 mrg 4712 1.1 mrg void 4713 1.1 mrg no_linkage_error (tree decl) 4714 1.1 mrg { 4715 1.1 mrg if (cxx_dialect >= cxx11 4716 1.1 mrg && (decl_defined_p (decl) 4717 1.1 mrg /* Treat templates which limit_bad_template_recursion decided 4718 1.1 mrg not to instantiate as if they were defined. */ 4719 1.1 mrg || (errorcount + sorrycount > 0 4720 1.1 mrg && DECL_LANG_SPECIFIC (decl) 4721 1.1 mrg && DECL_TEMPLATE_INFO (decl) 4722 1.1 mrg && warning_suppressed_p (decl /* What warning? */)))) 4723 1.1 mrg /* In C++11 it's ok if the decl is defined. */ 4724 1.1 mrg return; 4725 1.1 mrg 4726 1.1 mrg if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl)) 4727 1.1 mrg /* An imported decl is ok. */ 4728 1.1 mrg return; 4729 1.1 mrg 4730 1.1 mrg tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false); 4731 1.1 mrg if (t == NULL_TREE) 4732 1.1 mrg /* The type that got us on no_linkage_decls must have gotten a name for 4733 1.1 mrg linkage purposes. */; 4734 1.1 mrg else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t)) 4735 1.1 mrg // FIXME: This is now invalid, as a DR to c++98 4736 1.1 mrg /* The type might end up having a typedef name for linkage purposes. */ 4737 1.1 mrg vec_safe_push (no_linkage_decls, decl); 4738 1.1 mrg else if (TYPE_UNNAMED_P (t)) 4739 1.1 mrg { 4740 1.1 mrg bool d = false; 4741 1.1 mrg auto_diagnostic_group grp; 4742 1.1 mrg if (cxx_dialect >= cxx11) 4743 1.1 mrg d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using " 4744 1.1 mrg "unnamed type, is used but never defined", decl); 4745 1.1 mrg else if (DECL_EXTERN_C_P (decl)) 4746 1.1 mrg /* Allow this; it's pretty common in C. */; 4747 1.1 mrg else if (VAR_P (decl)) 4748 1.1 mrg /* DRs 132, 319 and 389 seem to indicate types with 4749 1.1 mrg no linkage can only be used to declare extern "C" 4750 1.1 mrg entities. Since it's not always an error in the 4751 1.1 mrg ISO C++ 90 Standard, we only issue a warning. */ 4752 1.1 mrg d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type " 4753 1.1 mrg "with no linkage used to declare variable %q#D with " 4754 1.1 mrg "linkage", decl); 4755 1.1 mrg else 4756 1.1 mrg d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no " 4757 1.1 mrg "linkage used to declare function %q#D with linkage", 4758 1.1 mrg decl); 4759 1.1 mrg if (d && is_typedef_decl (TYPE_NAME (t))) 4760 1.1 mrg inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer " 4761 1.1 mrg "to the unqualified type, so it is not used for linkage", 4762 1.1 mrg TYPE_NAME (t)); 4763 1.1 mrg } 4764 1.1 mrg else if (cxx_dialect >= cxx11) 4765 1.1 mrg { 4766 1.1 mrg if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl)) 4767 1.1 mrg permerror (DECL_SOURCE_LOCATION (decl), 4768 1.1 mrg "%q#D, declared using local type " 4769 1.1 mrg "%qT, is used but never defined", decl, t); 4770 1.1 mrg } 4771 1.1 mrg else if (VAR_P (decl)) 4772 1.1 mrg warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage " 4773 1.1 mrg "used to declare variable %q#D with linkage", t, decl); 4774 1.1 mrg else 4775 1.1 mrg permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used " 4776 1.1 mrg "to declare function %q#D with linkage", t, decl); 4777 1.1 mrg } 4778 1.1 mrg 4779 1.1 mrg /* Collect declarations from all namespaces relevant to SOURCE_FILE. */ 4780 1.1 mrg 4781 1.1 mrg static void 4782 1.1 mrg collect_all_refs (const char *source_file) 4783 1.1 mrg { 4784 1.1 mrg collect_ada_namespace (global_namespace, source_file); 4785 1.1 mrg } 4786 1.1 mrg 4787 1.1 mrg /* Clear DECL_EXTERNAL for NODE. */ 4788 1.1 mrg 4789 1.1 mrg static bool 4790 1.1 mrg clear_decl_external (struct cgraph_node *node, void * /*data*/) 4791 1.1 mrg { 4792 1.1 mrg DECL_EXTERNAL (node->decl) = 0; 4793 1.1 mrg return false; 4794 1.1 mrg } 4795 1.1 mrg 4796 1.1 mrg /* Build up the function to run dynamic initializers for thread_local 4797 1.1 mrg variables in this translation unit and alias the init functions for the 4798 1.1 mrg individual variables to it. */ 4799 1.1 mrg 4800 1.1 mrg static void 4801 1.1 mrg handle_tls_init (void) 4802 1.1 mrg { 4803 1.1 mrg tree vars = prune_vars_needing_no_initialization (&tls_aggregates); 4804 1.1 mrg if (vars == NULL_TREE) 4805 1.1 mrg return; 4806 1.1 mrg 4807 1.1 mrg location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars)); 4808 1.1 mrg 4809 1.1 mrg write_out_vars (vars); 4810 1.1 mrg 4811 1.1 mrg tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"), 4812 1.1 mrg boolean_type_node); 4813 1.1 mrg TREE_PUBLIC (guard) = false; 4814 1.1 mrg TREE_STATIC (guard) = true; 4815 1.1 mrg DECL_ARTIFICIAL (guard) = true; 4816 1.1 mrg DECL_IGNORED_P (guard) = true; 4817 1.1 mrg TREE_USED (guard) = true; 4818 1.1 mrg CP_DECL_THREAD_LOCAL_P (guard) = true; 4819 1.1 mrg set_decl_tls_model (guard, decl_default_tls_model (guard)); 4820 1.1 mrg pushdecl_top_level_and_finish (guard, NULL_TREE); 4821 1.1 mrg 4822 1.1 mrg tree fn = get_local_tls_init_fn (loc); 4823 1.1 mrg start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED); 4824 1.1 mrg tree body = begin_function_body (); 4825 1.1 mrg tree if_stmt = begin_if_stmt (); 4826 1.1 mrg tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false, 4827 1.1 mrg tf_warning_or_error); 4828 1.1 mrg finish_if_stmt_cond (cond, if_stmt); 4829 1.1 mrg finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR, 4830 1.1 mrg boolean_true_node, 4831 1.1 mrg tf_warning_or_error)); 4832 1.1 mrg for (; vars; vars = TREE_CHAIN (vars)) 4833 1.1 mrg { 4834 1.1 mrg tree var = TREE_VALUE (vars); 4835 1.1 mrg tree init = TREE_PURPOSE (vars); 4836 1.1 mrg one_static_initialization_or_destruction (var, init, true); 4837 1.1 mrg 4838 1.1 mrg /* Output init aliases even with -fno-extern-tls-init. */ 4839 1.1 mrg if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var)) 4840 1.1 mrg { 4841 1.1 mrg tree single_init_fn = get_tls_init_fn (var); 4842 1.1 mrg if (single_init_fn == NULL_TREE) 4843 1.1 mrg continue; 4844 1.1 mrg cgraph_node *alias 4845 1.1 mrg = cgraph_node::get_create (fn)->create_same_body_alias 4846 1.1 mrg (single_init_fn, fn); 4847 1.1 mrg gcc_assert (alias != NULL); 4848 1.1 mrg } 4849 1.1 mrg } 4850 1.1 mrg 4851 1.1 mrg finish_then_clause (if_stmt); 4852 1.1 mrg finish_if_stmt (if_stmt); 4853 1.1 mrg finish_function_body (body); 4854 1.1 mrg expand_or_defer_fn (finish_function (/*inline_p=*/false)); 4855 1.1 mrg } 4856 1.1 mrg 4857 1.1 mrg /* We're at the end of compilation, so generate any mangling aliases that 4858 1.1 mrg we've been saving up, if DECL is going to be output and ID2 isn't 4859 1.1 mrg already taken by another declaration. */ 4860 1.1 mrg 4861 1.1 mrg static void 4862 1.1 mrg generate_mangling_alias (tree decl, tree id2) 4863 1.1 mrg { 4864 1.1 mrg struct cgraph_node *n = NULL; 4865 1.1 mrg 4866 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL) 4867 1.1 mrg { 4868 1.1 mrg n = cgraph_node::get (decl); 4869 1.1 mrg if (!n) 4870 1.1 mrg /* Don't create an alias to an unreferenced function. */ 4871 1.1 mrg return; 4872 1.1 mrg } 4873 1.1 mrg 4874 1.1 mrg tree *slot 4875 1.1 mrg = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2), 4876 1.1 mrg INSERT); 4877 1.1 mrg 4878 1.1 mrg /* If there's a declaration already using this mangled name, 4879 1.1 mrg don't create a compatibility alias that conflicts. */ 4880 1.1 mrg if (*slot) 4881 1.1 mrg return; 4882 1.1 mrg 4883 1.1 mrg tree alias = make_alias_for (decl, id2); 4884 1.1 mrg *slot = alias; 4885 1.1 mrg 4886 1.1 mrg DECL_IGNORED_P (alias) = 1; 4887 1.1 mrg TREE_PUBLIC (alias) = TREE_PUBLIC (decl); 4888 1.1 mrg DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl); 4889 1.1 mrg if (vague_linkage_p (decl)) 4890 1.1 mrg DECL_WEAK (alias) = 1; 4891 1.1 mrg 4892 1.1 mrg if (n) 4893 1.1 mrg n->create_same_body_alias (alias, decl); 4894 1.1 mrg else 4895 1.1 mrg varpool_node::create_extra_name_alias (alias, decl); 4896 1.1 mrg } 4897 1.1 mrg 4898 1.1 mrg /* Note that we might want to emit an alias with the symbol ID2 for DECL at 4899 1.1 mrg the end of translation, for compatibility across bugs in the mangling 4900 1.1 mrg implementation. */ 4901 1.1 mrg 4902 1.1 mrg void 4903 1.1 mrg note_mangling_alias (tree decl, tree id2) 4904 1.1 mrg { 4905 1.1 mrg if (TARGET_SUPPORTS_ALIASES) 4906 1.1 mrg { 4907 1.1 mrg if (!defer_mangling_aliases) 4908 1.1 mrg generate_mangling_alias (decl, id2); 4909 1.1 mrg else 4910 1.1 mrg { 4911 1.1 mrg vec_safe_push (mangling_aliases, decl); 4912 1.1 mrg vec_safe_push (mangling_aliases, id2); 4913 1.1 mrg } 4914 1.1 mrg } 4915 1.1 mrg } 4916 1.1 mrg 4917 1.1 mrg /* Emit all mangling aliases that were deferred up to this point. */ 4918 1.1 mrg 4919 1.1 mrg void 4920 1.1 mrg generate_mangling_aliases () 4921 1.1 mrg { 4922 1.1 mrg while (!vec_safe_is_empty (mangling_aliases)) 4923 1.1 mrg { 4924 1.1 mrg tree id2 = mangling_aliases->pop(); 4925 1.1 mrg tree decl = mangling_aliases->pop(); 4926 1.1 mrg generate_mangling_alias (decl, id2); 4927 1.1 mrg } 4928 1.1 mrg defer_mangling_aliases = false; 4929 1.1 mrg } 4930 1.1 mrg 4931 1.1 mrg /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been 4932 1.1 mrg set. NEED_WARNING is true if we must warn about collisions. We do 4933 1.1 mrg this to spot changes in mangling that may require compatibility 4934 1.1 mrg aliases. */ 4935 1.1 mrg 4936 1.1 mrg void 4937 1.1 mrg record_mangling (tree decl, bool need_warning) 4938 1.1 mrg { 4939 1.1 mrg if (!mangled_decls) 4940 1.1 mrg mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499); 4941 1.1 mrg 4942 1.1 mrg gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl)); 4943 1.1 mrg tree id = DECL_ASSEMBLER_NAME_RAW (decl); 4944 1.1 mrg tree *slot 4945 1.1 mrg = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id), 4946 1.1 mrg INSERT); 4947 1.1 mrg 4948 1.1 mrg /* If this is already an alias, remove the alias, because the real 4949 1.1 mrg decl takes precedence. */ 4950 1.1 mrg if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot)) 4951 1.1 mrg if (symtab_node *n = symtab_node::get (*slot)) 4952 1.1 mrg if (n->cpp_implicit_alias) 4953 1.1 mrg { 4954 1.1 mrg n->remove (); 4955 1.1 mrg *slot = NULL_TREE; 4956 1.1 mrg } 4957 1.1 mrg 4958 1.1 mrg if (!*slot) 4959 1.1 mrg *slot = decl; 4960 1.1 mrg else if (need_warning) 4961 1.1 mrg { 4962 1.1 mrg error_at (DECL_SOURCE_LOCATION (decl), 4963 1.1 mrg "mangling of %q#D as %qE conflicts with a previous mangle", 4964 1.1 mrg decl, id); 4965 1.1 mrg inform (DECL_SOURCE_LOCATION (*slot), 4966 1.1 mrg "previous mangling %q#D", *slot); 4967 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), 4968 1.1 mrg "a later %<-fabi-version=%> (or =0)" 4969 1.1 mrg " avoids this error with a change in mangling"); 4970 1.1 mrg *slot = decl; 4971 1.1 mrg } 4972 1.1 mrg } 4973 1.1 mrg 4974 1.1 mrg /* The mangled name of DECL is being forcibly changed to NAME. Remove 4975 1.1 mrg any existing knowledge of DECL's mangled name meaning DECL. */ 4976 1.1 mrg 4977 1.1 mrg void 4978 1.1 mrg overwrite_mangling (tree decl, tree name) 4979 1.1 mrg { 4980 1.1 mrg if (tree id = DECL_ASSEMBLER_NAME_RAW (decl)) 4981 1.1 mrg if ((TREE_CODE (decl) == VAR_DECL 4982 1.1 mrg || TREE_CODE (decl) == FUNCTION_DECL) 4983 1.1 mrg && mangled_decls) 4984 1.1 mrg if (tree *slot 4985 1.1 mrg = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id), 4986 1.1 mrg NO_INSERT)) 4987 1.1 mrg if (*slot == decl) 4988 1.1 mrg { 4989 1.1 mrg mangled_decls->clear_slot (slot); 4990 1.1 mrg 4991 1.1 mrg /* If this is an alias, remove it from the symbol table. */ 4992 1.1 mrg if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl)) 4993 1.1 mrg if (symtab_node *n = symtab_node::get (decl)) 4994 1.1 mrg if (n->cpp_implicit_alias) 4995 1.1 mrg n->remove (); 4996 1.1 mrg } 4997 1.1 mrg 4998 1.1 mrg DECL_ASSEMBLER_NAME_RAW (decl) = name; 4999 1.1 mrg } 5000 1.1 mrg 5001 1.1 mrg /* The entire file is now complete. If requested, dump everything 5002 1.1 mrg to a file. */ 5003 1.1 mrg 5004 1.1 mrg static void 5005 1.1 mrg dump_tu (void) 5006 1.1 mrg { 5007 1.1 mrg dump_flags_t flags; 5008 1.1 mrg if (FILE *stream = dump_begin (raw_dump_id, &flags)) 5009 1.1 mrg { 5010 1.1 mrg dump_node (global_namespace, flags & ~TDF_SLIM, stream); 5011 1.1 mrg dump_end (raw_dump_id, stream); 5012 1.1 mrg } 5013 1.1 mrg } 5014 1.1 mrg 5015 1.1 mrg static location_t locus_at_end_of_parsing; 5016 1.1 mrg 5017 1.1 mrg /* Check the deallocation functions for CODE to see if we want to warn that 5018 1.1 mrg only one was defined. */ 5019 1.1 mrg 5020 1.1 mrg static void 5021 1.1 mrg maybe_warn_sized_delete (enum tree_code code) 5022 1.1 mrg { 5023 1.1 mrg tree sized = NULL_TREE; 5024 1.1 mrg tree unsized = NULL_TREE; 5025 1.1 mrg 5026 1.1 mrg for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code))); 5027 1.1 mrg iter; ++iter) 5028 1.1 mrg { 5029 1.1 mrg tree fn = *iter; 5030 1.1 mrg /* We're only interested in usual deallocation functions. */ 5031 1.1 mrg if (!usual_deallocation_fn_p (fn)) 5032 1.1 mrg continue; 5033 1.1 mrg if (FUNCTION_ARG_CHAIN (fn) == void_list_node) 5034 1.1 mrg unsized = fn; 5035 1.1 mrg else 5036 1.1 mrg sized = fn; 5037 1.1 mrg } 5038 1.1 mrg if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized)) 5039 1.1 mrg warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation, 5040 1.1 mrg "the program should also define %qD", sized); 5041 1.1 mrg else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized)) 5042 1.1 mrg warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation, 5043 1.1 mrg "the program should also define %qD", unsized); 5044 1.1 mrg } 5045 1.1 mrg 5046 1.1 mrg /* Check the global deallocation functions to see if we want to warn about 5047 1.1 mrg defining unsized without sized (or vice versa). */ 5048 1.1 mrg 5049 1.1 mrg static void 5050 1.1 mrg maybe_warn_sized_delete () 5051 1.1 mrg { 5052 1.1 mrg if (!flag_sized_deallocation || !warn_sized_deallocation) 5053 1.1 mrg return; 5054 1.1 mrg maybe_warn_sized_delete (DELETE_EXPR); 5055 1.1 mrg maybe_warn_sized_delete (VEC_DELETE_EXPR); 5056 1.1 mrg } 5057 1.1 mrg 5058 1.1 mrg /* Earlier we left PTRMEM_CST in variable initializers alone so that we could 5059 1.1 mrg look them up when evaluating non-type template parameters. Now we need to 5060 1.1 mrg lower them to something the back end can understand. */ 5061 1.1 mrg 5062 1.1 mrg static void 5063 1.1 mrg lower_var_init () 5064 1.1 mrg { 5065 1.1 mrg varpool_node *node; 5066 1.1 mrg FOR_EACH_VARIABLE (node) 5067 1.1 mrg { 5068 1.1 mrg tree d = node->decl; 5069 1.1 mrg if (tree init = DECL_INITIAL (d)) 5070 1.1 mrg DECL_INITIAL (d) = cplus_expand_constant (init); 5071 1.1 mrg } 5072 1.1 mrg } 5073 1.1 mrg 5074 1.1 mrg /* This routine is called at the end of compilation. 5075 1.1 mrg Its job is to create all the code needed to initialize and 5076 1.1 mrg destroy the global aggregates. We do the destruction 5077 1.1 mrg first, since that way we only need to reverse the decls once. */ 5078 1.1 mrg 5079 1.1 mrg void 5080 1.1 mrg c_parse_final_cleanups (void) 5081 1.1 mrg { 5082 1.1 mrg size_t i; 5083 1.1 mrg tree decl; 5084 1.1 mrg 5085 1.1 mrg locus_at_end_of_parsing = input_location; 5086 1.1 mrg at_eof = 1; 5087 1.1 mrg 5088 1.1 mrg /* Bad parse errors. Just forget about it. */ 5089 1.1 mrg if (! global_bindings_p () || current_class_type 5090 1.1 mrg || !vec_safe_is_empty (decl_namespace_list)) 5091 1.1 mrg return; 5092 1.1 mrg 5093 1.1 mrg /* This is the point to write out a PCH if we're doing that. 5094 1.1 mrg In that case we do not want to do anything else. */ 5095 1.1 mrg if (pch_file) 5096 1.1 mrg { 5097 1.1 mrg /* Mangle all symbols at PCH creation time. */ 5098 1.1 mrg symtab_node *node; 5099 1.1 mrg FOR_EACH_SYMBOL (node) 5100 1.1 mrg if (! is_a <varpool_node *> (node) 5101 1.1 mrg || ! DECL_HARD_REGISTER (node->decl)) 5102 1.1 mrg DECL_ASSEMBLER_NAME (node->decl); 5103 1.1 mrg c_common_write_pch (); 5104 1.1 mrg dump_tu (); 5105 1.1 mrg /* Ensure even the callers don't try to finalize the CU. */ 5106 1.1 mrg flag_syntax_only = 1; 5107 1.1 mrg return; 5108 1.1 mrg } 5109 1.1 mrg 5110 1.1 mrg timevar_stop (TV_PHASE_PARSING); 5111 1.1 mrg timevar_start (TV_PHASE_DEFERRED); 5112 1.1 mrg 5113 1.1 mrg symtab->process_same_body_aliases (); 5114 1.1 mrg 5115 1.1 mrg /* Handle -fdump-ada-spec[-slim] */ 5116 1.1 mrg if (flag_dump_ada_spec || flag_dump_ada_spec_slim) 5117 1.1 mrg { 5118 1.1 mrg collect_source_ref (main_input_filename); 5119 1.1 mrg if (!flag_dump_ada_spec_slim) 5120 1.1 mrg collect_source_refs (global_namespace); 5121 1.1 mrg 5122 1.1 mrg dump_ada_specs (collect_all_refs, cpp_check); 5123 1.1 mrg } 5124 1.1 mrg 5125 1.1 mrg /* FIXME - huh? was input_line -= 1;*/ 5126 1.1 mrg 5127 1.1 mrg /* We now have to write out all the stuff we put off writing out. 5128 1.1 mrg These include: 5129 1.1 mrg 5130 1.1 mrg o Template specializations that we have not yet instantiated, 5131 1.1 mrg but which are needed. 5132 1.1 mrg o Initialization and destruction for non-local objects with 5133 1.1 mrg static storage duration. (Local objects with static storage 5134 1.1 mrg duration are initialized when their scope is first entered, 5135 1.1 mrg and are cleaned up via atexit.) 5136 1.1 mrg o Virtual function tables. 5137 1.1 mrg 5138 1.1 mrg All of these may cause others to be needed. For example, 5139 1.1 mrg instantiating one function may cause another to be needed, and 5140 1.1 mrg generating the initializer for an object may cause templates to be 5141 1.1 mrg instantiated, etc., etc. */ 5142 1.1 mrg 5143 1.1 mrg emit_support_tinfos (); 5144 1.1 mrg 5145 1.1 mrg /* Track vtables we want to emit that refer to consteval functions. */ 5146 1.1 mrg auto_vec<tree> consteval_vtables; 5147 1.1 mrg 5148 1.1 mrg int retries = 0; 5149 1.1 mrg unsigned ssdf_count = 0; 5150 1.1 mrg for (bool reconsider = true; reconsider; retries++) 5151 1.1 mrg { 5152 1.1 mrg reconsider = false; 5153 1.1 mrg 5154 1.1 mrg /* If there are templates that we've put off instantiating, do 5155 1.1 mrg them now. */ 5156 1.1 mrg instantiate_pending_templates (retries); 5157 1.1 mrg ggc_collect (); 5158 1.1 mrg 5159 1.1 mrg if (header_module_p ()) 5160 1.1 mrg /* A header modules initializations are handled in its 5161 1.1 mrg importer. */ 5162 1.1 mrg continue; 5163 1.1 mrg 5164 1.1 mrg /* Write out virtual tables as required. Writing out the 5165 1.1 mrg virtual table for a template class may cause the 5166 1.1 mrg instantiation of members of that class. If we write out 5167 1.1 mrg vtables then we remove the class from our list so we don't 5168 1.1 mrg have to look at it again. */ 5169 1.1 mrg tree t; 5170 1.1 mrg for (i = keyed_classes->length (); 5171 1.1 mrg keyed_classes->iterate (--i, &t);) 5172 1.1 mrg if (maybe_emit_vtables (t, consteval_vtables)) 5173 1.1 mrg { 5174 1.1 mrg reconsider = true; 5175 1.1 mrg keyed_classes->unordered_remove (i); 5176 1.1 mrg } 5177 1.1 mrg /* The input_location may have been changed during marking of 5178 1.1 mrg vtable entries. */ 5179 1.1 mrg input_location = locus_at_end_of_parsing; 5180 1.1 mrg 5181 1.1 mrg /* Write out needed type info variables. We have to be careful 5182 1.1 mrg looping through unemitted decls, because emit_tinfo_decl may 5183 1.1 mrg cause other variables to be needed. New elements will be 5184 1.1 mrg appended, and we remove from the vector those that actually 5185 1.1 mrg get emitted. */ 5186 1.1 mrg for (i = unemitted_tinfo_decls->length (); 5187 1.1 mrg unemitted_tinfo_decls->iterate (--i, &t);) 5188 1.1 mrg if (emit_tinfo_decl (t)) 5189 1.1 mrg { 5190 1.1 mrg reconsider = true; 5191 1.1 mrg unemitted_tinfo_decls->unordered_remove (i); 5192 1.1 mrg } 5193 1.1 mrg 5194 1.1 mrg /* The list of objects with static storage duration is built up 5195 1.1 mrg in reverse order. We clear STATIC_AGGREGATES so that any new 5196 1.1 mrg aggregates added during the initialization of these will be 5197 1.1 mrg initialized in the correct order when we next come around the 5198 1.1 mrg loop. */ 5199 1.1 mrg if (tree vars = prune_vars_needing_no_initialization (&static_aggregates)) 5200 1.1 mrg { 5201 1.1 mrg if (flag_openmp) 5202 1.1 mrg /* Add initializer information from VARS into 5203 1.1 mrg DYNAMIC_INITIALIZERS. */ 5204 1.1 mrg for (t = vars; t; t = TREE_CHAIN (t)) 5205 1.1 mrg hash_map_safe_put<hm_ggc> (dynamic_initializers, 5206 1.1 mrg TREE_VALUE (t), TREE_PURPOSE (t)); 5207 1.1 mrg 5208 1.1 mrg /* We need to start a new initialization function each time 5209 1.1 mrg through the loop. That's because we need to know which 5210 1.1 mrg vtables have been referenced, and TREE_SYMBOL_REFERENCED 5211 1.1 mrg isn't computed until a function is finished, and written 5212 1.1 mrg out. That's a deficiency in the back end. When this is 5213 1.1 mrg fixed, these initialization functions could all become 5214 1.1 mrg inline, with resulting performance improvements. */ 5215 1.1 mrg tree ssdf_body; 5216 1.1 mrg 5217 1.1 mrg /* Make sure the back end knows about all the variables. */ 5218 1.1 mrg write_out_vars (vars); 5219 1.1 mrg 5220 1.1 mrg /* Set the line and file, so that it is obviously not from 5221 1.1 mrg the source file. */ 5222 1.1 mrg input_location = locus_at_end_of_parsing; 5223 1.1 mrg ssdf_body = start_static_storage_duration_function (ssdf_count); 5224 1.1 mrg 5225 1.1 mrg /* First generate code to do all the initializations. */ 5226 1.1 mrg if (vars) 5227 1.1 mrg do_static_initialization_or_destruction (vars, /*initp=*/true); 5228 1.1 mrg 5229 1.1 mrg /* Then, generate code to do all the destructions. Do these 5230 1.1 mrg in reverse order so that the most recently constructed 5231 1.1 mrg variable is the first destroyed. If we're using 5232 1.1 mrg __cxa_atexit, then we don't need to do this; functions 5233 1.1 mrg were registered at initialization time to destroy the 5234 1.1 mrg local statics. */ 5235 1.1 mrg if (!flag_use_cxa_atexit && vars) 5236 1.1 mrg { 5237 1.1 mrg vars = nreverse (vars); 5238 1.1 mrg do_static_initialization_or_destruction (vars, /*initp=*/false); 5239 1.1 mrg } 5240 1.1 mrg else 5241 1.1 mrg vars = NULL_TREE; 5242 1.1 mrg 5243 1.1 mrg /* Finish up the static storage duration function for this 5244 1.1 mrg round. */ 5245 1.1 mrg input_location = locus_at_end_of_parsing; 5246 1.1 mrg finish_static_storage_duration_function (ssdf_body); 5247 1.1 mrg 5248 1.1 mrg /* All those initializations and finalizations might cause 5249 1.1 mrg us to need more inline functions, more template 5250 1.1 mrg instantiations, etc. */ 5251 1.1 mrg reconsider = true; 5252 1.1 mrg ssdf_count++; 5253 1.1 mrg } 5254 1.1 mrg 5255 1.1 mrg /* Now do the same for thread_local variables. */ 5256 1.1 mrg handle_tls_init (); 5257 1.1 mrg 5258 1.1 mrg /* Go through the set of inline functions whose bodies have not 5259 1.1 mrg been emitted yet. If out-of-line copies of these functions 5260 1.1 mrg are required, emit them. */ 5261 1.1 mrg FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl) 5262 1.1 mrg { 5263 1.1 mrg /* Does it need synthesizing? */ 5264 1.1 mrg if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl) 5265 1.1 mrg && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl))) 5266 1.1 mrg { 5267 1.1 mrg /* Even though we're already at the top-level, we push 5268 1.1 mrg there again. That way, when we pop back a few lines 5269 1.1 mrg hence, all of our state is restored. Otherwise, 5270 1.1 mrg finish_function doesn't clean things up, and we end 5271 1.1 mrg up with CURRENT_FUNCTION_DECL set. */ 5272 1.1 mrg push_to_top_level (); 5273 1.1 mrg /* The decl's location will mark where it was first 5274 1.1 mrg needed. Save that so synthesize method can indicate 5275 1.1 mrg where it was needed from, in case of error */ 5276 1.1 mrg input_location = DECL_SOURCE_LOCATION (decl); 5277 1.1 mrg synthesize_method (decl); 5278 1.1 mrg pop_from_top_level (); 5279 1.1 mrg reconsider = true; 5280 1.1 mrg } 5281 1.1 mrg 5282 1.1 mrg if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl)) 5283 1.1 mrg generate_tls_wrapper (decl); 5284 1.1 mrg 5285 1.1 mrg if (!DECL_SAVED_TREE (decl)) 5286 1.1 mrg continue; 5287 1.1 mrg 5288 1.1 mrg cgraph_node *node = cgraph_node::get_create (decl); 5289 1.1 mrg 5290 1.1 mrg /* We lie to the back end, pretending that some functions 5291 1.1 mrg are not defined when they really are. This keeps these 5292 1.1 mrg functions from being put out unnecessarily. But, we must 5293 1.1 mrg stop lying when the functions are referenced, or if they 5294 1.1 mrg are not comdat since they need to be put out now. If 5295 1.1 mrg DECL_INTERFACE_KNOWN, then we have already set 5296 1.1 mrg DECL_EXTERNAL appropriately, so there's no need to check 5297 1.1 mrg again, and we do not want to clear DECL_EXTERNAL if a 5298 1.1 mrg previous call to import_export_decl set it. 5299 1.1 mrg 5300 1.1 mrg This is done in a separate for cycle, because if some 5301 1.1 mrg deferred function is contained in another deferred 5302 1.1 mrg function later in deferred_fns varray, 5303 1.1 mrg rest_of_compilation would skip this function and we 5304 1.1 mrg really cannot expand the same function twice. */ 5305 1.1 mrg import_export_decl (decl); 5306 1.1 mrg if (DECL_NOT_REALLY_EXTERN (decl) 5307 1.1 mrg && DECL_INITIAL (decl) 5308 1.1 mrg && decl_needed_p (decl)) 5309 1.1 mrg { 5310 1.1 mrg if (node->cpp_implicit_alias) 5311 1.1 mrg node = node->get_alias_target (); 5312 1.1 mrg 5313 1.1 mrg node->call_for_symbol_thunks_and_aliases (clear_decl_external, 5314 1.1 mrg NULL, true); 5315 1.1 mrg /* If we mark !DECL_EXTERNAL one of the symbols in some comdat 5316 1.1 mrg group, we need to mark all symbols in the same comdat group 5317 1.1 mrg that way. */ 5318 1.1 mrg if (node->same_comdat_group) 5319 1.1 mrg for (cgraph_node *next 5320 1.1 mrg = dyn_cast<cgraph_node *> (node->same_comdat_group); 5321 1.1 mrg next != node; 5322 1.1 mrg next = dyn_cast<cgraph_node *> (next->same_comdat_group)) 5323 1.1 mrg next->call_for_symbol_thunks_and_aliases (clear_decl_external, 5324 1.1 mrg NULL, true); 5325 1.1 mrg } 5326 1.1 mrg 5327 1.1 mrg /* If we're going to need to write this function out, and 5328 1.1 mrg there's already a body for it, create RTL for it now. 5329 1.1 mrg (There might be no body if this is a method we haven't 5330 1.1 mrg gotten around to synthesizing yet.) */ 5331 1.1 mrg if (!DECL_EXTERNAL (decl) 5332 1.1 mrg && decl_needed_p (decl) 5333 1.1 mrg && !TREE_ASM_WRITTEN (decl) 5334 1.1 mrg && !DECL_IMMEDIATE_FUNCTION_P (decl) 5335 1.1 mrg && !node->definition) 5336 1.1 mrg { 5337 1.1 mrg /* We will output the function; no longer consider it in this 5338 1.1 mrg loop. */ 5339 1.1 mrg DECL_DEFER_OUTPUT (decl) = 0; 5340 1.1 mrg /* Generate RTL for this function now that we know we 5341 1.1 mrg need it. */ 5342 1.1 mrg expand_or_defer_fn (decl); 5343 1.1 mrg reconsider = true; 5344 1.1 mrg } 5345 1.1 mrg } 5346 1.1 mrg 5347 1.1 mrg if (wrapup_namespace_globals ()) 5348 1.1 mrg reconsider = true; 5349 1.1 mrg 5350 1.1 mrg /* Static data members are just like namespace-scope globals. */ 5351 1.1 mrg FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl) 5352 1.1 mrg { 5353 1.1 mrg if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl) 5354 1.1 mrg /* Don't write it out if we haven't seen a definition. */ 5355 1.1 mrg || DECL_IN_AGGR_P (decl)) 5356 1.1 mrg continue; 5357 1.1 mrg import_export_decl (decl); 5358 1.1 mrg /* If this static data member is needed, provide it to the 5359 1.1 mrg back end. */ 5360 1.1 mrg if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl)) 5361 1.1 mrg DECL_EXTERNAL (decl) = 0; 5362 1.1 mrg } 5363 1.1 mrg 5364 1.1 mrg if (vec_safe_length (pending_statics) != 0 5365 1.1 mrg && wrapup_global_declarations (pending_statics->address (), 5366 1.1 mrg pending_statics->length ())) 5367 1.1 mrg reconsider = true; 5368 1.1 mrg } 5369 1.1 mrg 5370 1.1 mrg finish_module_processing (parse_in); 5371 1.1 mrg 5372 1.1 mrg lower_var_init (); 5373 1.1 mrg 5374 1.1 mrg generate_mangling_aliases (); 5375 1.1 mrg 5376 1.1 mrg /* All used inline functions must have a definition at this point. */ 5377 1.1 mrg FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl) 5378 1.1 mrg { 5379 1.1 mrg if (/* Check online inline functions that were actually used. */ 5380 1.1 mrg DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl) 5381 1.1 mrg /* If the definition actually was available here, then the 5382 1.1 mrg fact that the function was not defined merely represents 5383 1.1 mrg that for some reason (use of a template repository, 5384 1.1 mrg #pragma interface, etc.) we decided not to emit the 5385 1.1 mrg definition here. */ 5386 1.1 mrg && !DECL_INITIAL (decl) 5387 1.1 mrg /* A defaulted fn in a header module can be synthesized on 5388 1.1 mrg demand later. (In non-header modules we should have 5389 1.1 mrg synthesized it above.) */ 5390 1.1 mrg && !(DECL_DEFAULTED_FN (decl) && header_module_p ()) 5391 1.1 mrg /* Don't complain if the template was defined. */ 5392 1.1 mrg && !(DECL_TEMPLATE_INSTANTIATION (decl) 5393 1.1 mrg && DECL_INITIAL (DECL_TEMPLATE_RESULT 5394 1.1 mrg (template_for_substitution (decl)))) 5395 1.1 mrg && warning_at (DECL_SOURCE_LOCATION (decl), 0, 5396 1.1 mrg "inline function %qD used but never defined", decl)) 5397 1.1 mrg /* Avoid a duplicate warning from check_global_declaration. */ 5398 1.1 mrg suppress_warning (decl, OPT_Wunused); 5399 1.1 mrg } 5400 1.1 mrg 5401 1.1 mrg /* So must decls that use a type with no linkage. */ 5402 1.1 mrg FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl) 5403 1.1 mrg no_linkage_error (decl); 5404 1.1 mrg 5405 1.1 mrg maybe_warn_sized_delete (); 5406 1.1 mrg 5407 1.1 mrg /* Then, do the Objective-C stuff. This is where all the 5408 1.1 mrg Objective-C module stuff gets generated (symtab, 5409 1.1 mrg class/protocol/selector lists etc). This must be done after C++ 5410 1.1 mrg templates, destructors etc. so that selectors used in C++ 5411 1.1 mrg templates are properly allocated. */ 5412 1.1 mrg if (c_dialect_objc ()) 5413 1.1 mrg objc_write_global_declarations (); 5414 1.1 mrg 5415 1.1 mrg /* We give C linkage to static constructors and destructors. */ 5416 1.1 mrg push_lang_context (lang_name_c); 5417 1.1 mrg 5418 1.1 mrg /* Generate initialization and destruction functions for all 5419 1.1 mrg priorities for which they are required. */ 5420 1.1 mrg if (priority_info_map) 5421 1.1 mrg splay_tree_foreach (priority_info_map, 5422 1.1 mrg generate_ctor_and_dtor_functions_for_priority, 5423 1.1 mrg /*data=*/&locus_at_end_of_parsing); 5424 1.1 mrg else if ((c_dialect_objc () && objc_static_init_needed_p ()) 5425 1.1 mrg || module_initializer_kind ()) 5426 1.1 mrg generate_ctor_or_dtor_function (/*constructor_p=*/true, 5427 1.1 mrg DEFAULT_INIT_PRIORITY, 5428 1.1 mrg &locus_at_end_of_parsing); 5429 1.1 mrg 5430 1.1 mrg /* We're done with the splay-tree now. */ 5431 1.1 mrg if (priority_info_map) 5432 1.1 mrg splay_tree_delete (priority_info_map); 5433 1.1 mrg 5434 1.1 mrg fini_modules (); 5435 1.1 mrg 5436 1.1 mrg /* Generate any missing aliases. */ 5437 1.1 mrg maybe_apply_pending_pragma_weaks (); 5438 1.1 mrg 5439 1.1 mrg /* We're done with static constructors, so we can go back to "C++" 5440 1.1 mrg linkage now. */ 5441 1.1 mrg pop_lang_context (); 5442 1.1 mrg 5443 1.1 mrg if (flag_vtable_verify) 5444 1.1 mrg { 5445 1.1 mrg vtv_recover_class_info (); 5446 1.1 mrg vtv_compute_class_hierarchy_transitive_closure (); 5447 1.1 mrg vtv_build_vtable_verify_fndecl (); 5448 1.1 mrg } 5449 1.1 mrg 5450 1.1 mrg perform_deferred_noexcept_checks (); 5451 1.1 mrg 5452 1.1 mrg fini_constexpr (); 5453 1.1 mrg cp_tree_c_finish_parsing (); 5454 1.1 mrg clear_consteval_vfns (consteval_vtables); 5455 1.1 mrg 5456 1.1 mrg /* The entire file is now complete. If requested, dump everything 5457 1.1 mrg to a file. */ 5458 1.1 mrg dump_tu (); 5459 1.1 mrg 5460 1.1 mrg if (flag_detailed_statistics) 5461 1.1 mrg { 5462 1.1 mrg dump_tree_statistics (); 5463 1.1 mrg dump_time_statistics (); 5464 1.1 mrg } 5465 1.1 mrg 5466 1.1 mrg timevar_stop (TV_PHASE_DEFERRED); 5467 1.1 mrg timevar_start (TV_PHASE_PARSING); 5468 1.1 mrg 5469 1.1 mrg /* Indicate that we're done with front end processing. */ 5470 1.1 mrg at_eof = 2; 5471 1.1 mrg } 5472 1.1 mrg 5473 1.1 mrg /* Perform any post compilation-proper cleanups for the C++ front-end. 5474 1.1 mrg This should really go away. No front-end should need to do 5475 1.1 mrg anything past the compilation process. */ 5476 1.1 mrg 5477 1.1 mrg void 5478 1.1 mrg cxx_post_compilation_parsing_cleanups (void) 5479 1.1 mrg { 5480 1.1 mrg timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS); 5481 1.1 mrg 5482 1.1 mrg if (flag_vtable_verify) 5483 1.1 mrg { 5484 1.1 mrg /* Generate the special constructor initialization function that 5485 1.1 mrg calls __VLTRegisterPairs, and give it a very high 5486 1.1 mrg initialization priority. This must be done after 5487 1.1 mrg finalize_compilation_unit so that we have accurate 5488 1.1 mrg information about which vtable will actually be emitted. */ 5489 1.1 mrg vtv_generate_init_routine (); 5490 1.1 mrg } 5491 1.1 mrg 5492 1.1 mrg input_location = locus_at_end_of_parsing; 5493 1.1 mrg 5494 1.1 mrg if (flag_checking) 5495 1.1 mrg validate_conversion_obstack (); 5496 1.1 mrg 5497 1.1 mrg timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS); 5498 1.1 mrg } 5499 1.1 mrg 5500 1.1 mrg /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the 5501 1.1 mrg function to call in parse-tree form; it has not yet been 5502 1.1 mrg semantically analyzed. ARGS are the arguments to the function. 5503 1.1 mrg They have already been semantically analyzed. This may change 5504 1.1 mrg ARGS. */ 5505 1.1 mrg 5506 1.1 mrg tree 5507 1.1 mrg build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args, 5508 1.1 mrg tsubst_flags_t complain) 5509 1.1 mrg { 5510 1.1 mrg tree orig_fn; 5511 1.1 mrg vec<tree, va_gc> *orig_args = NULL; 5512 1.1 mrg tree expr; 5513 1.1 mrg tree object; 5514 1.1 mrg 5515 1.1 mrg orig_fn = fn; 5516 1.1 mrg object = TREE_OPERAND (fn, 0); 5517 1.1 mrg 5518 1.1 mrg if (processing_template_decl) 5519 1.1 mrg { 5520 1.1 mrg gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR 5521 1.1 mrg || TREE_CODE (fn) == MEMBER_REF); 5522 1.1 mrg if (type_dependent_expression_p (fn) 5523 1.1 mrg || any_type_dependent_arguments_p (*args)) 5524 1.1 mrg return build_min_nt_call_vec (fn, *args); 5525 1.1 mrg 5526 1.1 mrg orig_args = make_tree_vector_copy (*args); 5527 1.1 mrg 5528 1.1 mrg /* Transform the arguments and add the implicit "this" 5529 1.1 mrg parameter. That must be done before the FN is transformed 5530 1.1 mrg because we depend on the form of FN. */ 5531 1.1 mrg make_args_non_dependent (*args); 5532 1.1 mrg object = build_non_dependent_expr (object); 5533 1.1 mrg if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 5534 1.1 mrg { 5535 1.1 mrg if (TREE_CODE (fn) == DOTSTAR_EXPR) 5536 1.1 mrg object = cp_build_addr_expr (object, complain); 5537 1.1 mrg vec_safe_insert (*args, 0, object); 5538 1.1 mrg } 5539 1.1 mrg /* Now that the arguments are done, transform FN. */ 5540 1.1 mrg fn = build_non_dependent_expr (fn); 5541 1.1 mrg } 5542 1.1 mrg 5543 1.1 mrg /* A qualified name corresponding to a bound pointer-to-member is 5544 1.1 mrg represented as an OFFSET_REF: 5545 1.1 mrg 5546 1.1 mrg struct B { void g(); }; 5547 1.1 mrg void (B::*p)(); 5548 1.1 mrg void B::g() { (this->*p)(); } */ 5549 1.1 mrg if (TREE_CODE (fn) == OFFSET_REF) 5550 1.1 mrg { 5551 1.1 mrg tree object_addr = cp_build_addr_expr (object, complain); 5552 1.1 mrg fn = TREE_OPERAND (fn, 1); 5553 1.1 mrg fn = get_member_function_from_ptrfunc (&object_addr, fn, 5554 1.1 mrg complain); 5555 1.1 mrg vec_safe_insert (*args, 0, object_addr); 5556 1.1 mrg } 5557 1.1 mrg 5558 1.1 mrg if (CLASS_TYPE_P (TREE_TYPE (fn))) 5559 1.1 mrg expr = build_op_call (fn, args, complain); 5560 1.1 mrg else 5561 1.1 mrg expr = cp_build_function_call_vec (fn, args, complain); 5562 1.1 mrg if (processing_template_decl && expr != error_mark_node) 5563 1.1 mrg expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args); 5564 1.1 mrg 5565 1.1 mrg if (orig_args != NULL) 5566 1.1 mrg release_tree_vector (orig_args); 5567 1.1 mrg 5568 1.1 mrg return expr; 5569 1.1 mrg } 5570 1.1 mrg 5571 1.1 mrg 5572 1.1 mrg void 5573 1.1 mrg check_default_args (tree x) 5574 1.1 mrg { 5575 1.1 mrg tree arg = TYPE_ARG_TYPES (TREE_TYPE (x)); 5576 1.1 mrg bool saw_def = false; 5577 1.1 mrg bool noted_first_def = false; 5578 1.1 mrg int idx_of_first_default_arg = 0; 5579 1.1 mrg location_t loc_of_first_default_arg = UNKNOWN_LOCATION; 5580 1.1 mrg int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE); 5581 1.1 mrg tree fndecl = STRIP_TEMPLATE (x); 5582 1.1 mrg auto_diagnostic_group d; 5583 1.1 mrg for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i) 5584 1.1 mrg { 5585 1.1 mrg if (TREE_PURPOSE (arg)) 5586 1.1 mrg { 5587 1.1 mrg if (!saw_def) 5588 1.1 mrg { 5589 1.1 mrg saw_def = true; 5590 1.1 mrg idx_of_first_default_arg = i; 5591 1.1 mrg location_t loc = get_fndecl_argument_location (fndecl, i); 5592 1.1 mrg if (loc != DECL_SOURCE_LOCATION (x)) 5593 1.1 mrg loc_of_first_default_arg = loc; 5594 1.1 mrg } 5595 1.1 mrg } 5596 1.1 mrg else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg))) 5597 1.1 mrg { 5598 1.1 mrg error_at (get_fndecl_argument_location (fndecl, i), 5599 1.1 mrg "default argument missing for parameter %P of %q#D", i, x); 5600 1.1 mrg if (loc_of_first_default_arg != UNKNOWN_LOCATION 5601 1.1 mrg && !noted_first_def) 5602 1.1 mrg { 5603 1.1 mrg inform (loc_of_first_default_arg, 5604 1.1 mrg "...following parameter %P which has a default argument", 5605 1.1 mrg idx_of_first_default_arg); 5606 1.1 mrg noted_first_def = true; 5607 1.1 mrg } 5608 1.1 mrg TREE_PURPOSE (arg) = error_mark_node; 5609 1.1 mrg } 5610 1.1 mrg } 5611 1.1 mrg } 5612 1.1 mrg 5613 1.1 mrg /* Return true if function DECL can be inlined. This is used to force 5614 1.1 mrg instantiation of methods that might be interesting for inlining. */ 5615 1.1 mrg bool 5616 1.1 mrg possibly_inlined_p (tree decl) 5617 1.1 mrg { 5618 1.1 mrg gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); 5619 1.1 mrg if (DECL_UNINLINABLE (decl)) 5620 1.1 mrg return false; 5621 1.1 mrg if (!optimize) 5622 1.1 mrg return DECL_DECLARED_INLINE_P (decl); 5623 1.1 mrg /* When optimizing, we might inline everything when flatten 5624 1.1 mrg attribute or heuristics inlining for size or autoinlining 5625 1.1 mrg is used. */ 5626 1.1 mrg return true; 5627 1.1 mrg } 5628 1.1 mrg 5629 1.1 mrg /* Normally, we can wait until instantiation-time to synthesize DECL. 5630 1.1 mrg However, if DECL is a static data member initialized with a constant 5631 1.1 mrg or a constexpr function, we need it right now because a reference to 5632 1.1 mrg such a data member or a call to such function is not value-dependent. 5633 1.1 mrg For a function that uses auto in the return type, we need to instantiate 5634 1.1 mrg it to find out its type. For OpenMP user defined reductions, we need 5635 1.1 mrg them instantiated for reduction clauses which inline them by hand 5636 1.1 mrg directly. */ 5637 1.1 mrg 5638 1.1 mrg void 5639 1.1 mrg maybe_instantiate_decl (tree decl) 5640 1.1 mrg { 5641 1.1 mrg if (DECL_LANG_SPECIFIC (decl) 5642 1.1 mrg && DECL_TEMPLATE_INFO (decl) 5643 1.1 mrg && (decl_maybe_constant_var_p (decl) 5644 1.1 mrg || (TREE_CODE (decl) == FUNCTION_DECL 5645 1.1 mrg && DECL_OMP_DECLARE_REDUCTION_P (decl)) 5646 1.1 mrg || undeduced_auto_decl (decl)) 5647 1.1 mrg && !DECL_DECLARED_CONCEPT_P (decl) 5648 1.1 mrg && !uses_template_parms (DECL_TI_ARGS (decl))) 5649 1.1 mrg { 5650 1.1 mrg /* Instantiating a function will result in garbage collection. We 5651 1.1 mrg must treat this situation as if we were within the body of a 5652 1.1 mrg function so as to avoid collecting live data only referenced from 5653 1.1 mrg the stack (such as overload resolution candidates). */ 5654 1.1 mrg ++function_depth; 5655 1.1 mrg instantiate_decl (decl, /*defer_ok=*/false, 5656 1.1 mrg /*expl_inst_class_mem_p=*/false); 5657 1.1 mrg --function_depth; 5658 1.1 mrg } 5659 1.1 mrg } 5660 1.1 mrg 5661 1.1 mrg /* Error if the DECL is unavailable (unless this is currently suppressed). 5662 1.1 mrg Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if 5663 1.1 mrg an error or warning was emitted. */ 5664 1.1 mrg 5665 1.1 mrg bool 5666 1.1 mrg cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain) 5667 1.1 mrg { 5668 1.1 mrg if (!decl) 5669 1.1 mrg return false; 5670 1.1 mrg 5671 1.1 mrg if ((complain & tf_error) 5672 1.1 mrg && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) 5673 1.1 mrg { 5674 1.1 mrg if (TREE_UNAVAILABLE (decl)) 5675 1.1 mrg { 5676 1.1 mrg error_unavailable_use (decl, NULL_TREE); 5677 1.1 mrg return true; 5678 1.1 mrg } 5679 1.1 mrg else 5680 1.1 mrg { 5681 1.1 mrg /* Perhaps this is an unavailable typedef. */ 5682 1.1 mrg if (TYPE_P (decl) 5683 1.1 mrg && TYPE_NAME (decl) 5684 1.1 mrg && TREE_UNAVAILABLE (TYPE_NAME (decl))) 5685 1.1 mrg { 5686 1.1 mrg decl = TYPE_NAME (decl); 5687 1.1 mrg /* Don't error within members of a unavailable type. */ 5688 1.1 mrg if (TYPE_P (decl) 5689 1.1 mrg && currently_open_class (decl)) 5690 1.1 mrg return false; 5691 1.1 mrg 5692 1.1 mrg error_unavailable_use (decl, NULL_TREE); 5693 1.1 mrg return true; 5694 1.1 mrg } 5695 1.1 mrg } 5696 1.1 mrg /* Carry on to consider deprecatedness. */ 5697 1.1 mrg } 5698 1.1 mrg 5699 1.1 mrg if (!(complain & tf_warning) 5700 1.1 mrg || deprecated_state == DEPRECATED_SUPPRESS 5701 1.1 mrg || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS) 5702 1.1 mrg return false; 5703 1.1 mrg 5704 1.1 mrg if (!TREE_DEPRECATED (decl)) 5705 1.1 mrg { 5706 1.1 mrg /* Perhaps this is a deprecated typedef. */ 5707 1.1 mrg if (TYPE_P (decl) && TYPE_NAME (decl)) 5708 1.1 mrg decl = TYPE_NAME (decl); 5709 1.1 mrg 5710 1.1 mrg if (!TREE_DEPRECATED (decl)) 5711 1.1 mrg return false; 5712 1.1 mrg } 5713 1.1 mrg 5714 1.1 mrg /* Don't warn within members of a deprecated type. */ 5715 1.1 mrg if (TYPE_P (decl) 5716 1.1 mrg && currently_open_class (decl)) 5717 1.1 mrg return false; 5718 1.1 mrg 5719 1.1 mrg bool warned = false; 5720 1.1 mrg if (cxx_dialect >= cxx11 5721 1.1 mrg && DECL_P (decl) 5722 1.1 mrg && DECL_ARTIFICIAL (decl) 5723 1.1 mrg && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) 5724 1.1 mrg && copy_fn_p (decl)) 5725 1.1 mrg { 5726 1.1 mrg /* Don't warn if the flag was disabled around the class definition 5727 1.1 mrg (c++/94492). */ 5728 1.1 mrg if (warning_enabled_at (DECL_SOURCE_LOCATION (decl), 5729 1.1 mrg OPT_Wdeprecated_copy)) 5730 1.1 mrg { 5731 1.1 mrg auto_diagnostic_group d; 5732 1.1 mrg tree ctx = DECL_CONTEXT (decl); 5733 1.1 mrg tree other = classtype_has_depr_implicit_copy (ctx); 5734 1.1 mrg int opt = (DECL_DESTRUCTOR_P (other) 5735 1.1 mrg ? OPT_Wdeprecated_copy_dtor 5736 1.1 mrg : OPT_Wdeprecated_copy); 5737 1.1 mrg warned = warning (opt, "implicitly-declared %qD is deprecated", 5738 1.1 mrg decl); 5739 1.1 mrg if (warned) 5740 1.1 mrg inform (DECL_SOURCE_LOCATION (other), 5741 1.1 mrg "because %qT has user-provided %qD", 5742 1.1 mrg ctx, other); 5743 1.1 mrg } 5744 1.1 mrg } 5745 1.1 mrg else 5746 1.1 mrg warned = warn_deprecated_use (decl, NULL_TREE); 5747 1.1 mrg 5748 1.1 mrg return warned; 5749 1.1 mrg } 5750 1.1 mrg 5751 1.1 mrg /* Like above, but takes into account outer scopes. */ 5752 1.1 mrg 5753 1.1 mrg void 5754 1.1 mrg cp_warn_deprecated_use_scopes (tree scope) 5755 1.1 mrg { 5756 1.1 mrg while (scope 5757 1.1 mrg && scope != error_mark_node 5758 1.1 mrg && scope != global_namespace) 5759 1.1 mrg { 5760 1.1 mrg if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope)) 5761 1.1 mrg && cp_handle_deprecated_or_unavailable (scope)) 5762 1.1 mrg return; 5763 1.1 mrg if (TYPE_P (scope)) 5764 1.1 mrg scope = CP_TYPE_CONTEXT (scope); 5765 1.1 mrg else 5766 1.1 mrg scope = CP_DECL_CONTEXT (scope); 5767 1.1 mrg } 5768 1.1 mrg } 5769 1.1 mrg 5770 1.1 mrg /* True if DECL or its enclosing scope have unbound template parameters. */ 5771 1.1 mrg 5772 1.1 mrg bool 5773 1.1 mrg decl_dependent_p (tree decl) 5774 1.1 mrg { 5775 1.1 mrg if (DECL_FUNCTION_SCOPE_P (decl) 5776 1.1 mrg || TREE_CODE (decl) == CONST_DECL 5777 1.1 mrg || TREE_CODE (decl) == USING_DECL 5778 1.1 mrg || TREE_CODE (decl) == FIELD_DECL) 5779 1.1 mrg decl = CP_DECL_CONTEXT (decl); 5780 1.1 mrg if (tree tinfo = get_template_info (decl)) 5781 1.1 mrg if (any_dependent_template_arguments_p (TI_ARGS (tinfo))) 5782 1.1 mrg return true; 5783 1.1 mrg if (LAMBDA_FUNCTION_P (decl) 5784 1.1 mrg && dependent_type_p (DECL_CONTEXT (decl))) 5785 1.1 mrg return true; 5786 1.1 mrg return false; 5787 1.1 mrg } 5788 1.1 mrg 5789 1.1 mrg /* [basic.def.odr] A function is named [and therefore odr-used] by an 5790 1.1 mrg expression or conversion if it is the selected member of an overload set in 5791 1.1 mrg an overload resolution performed as part of forming that expression or 5792 1.1 mrg conversion, unless it is a pure virtual function and either the expression 5793 1.1 mrg is not an id-expression naming the function with an explicitly qualified 5794 1.1 mrg name or the expression forms a pointer to member. 5795 1.1 mrg 5796 1.1 mrg Mostly, we call mark_used in places that actually do something with a 5797 1.1 mrg function, like build_over_call. But in a few places we end up with a 5798 1.1 mrg non-overloaded FUNCTION_DECL that we aren't going to do any more with, like 5799 1.1 mrg convert_to_void. resolve_nondeduced_context is called in those places, 5800 1.1 mrg but it's also called in too many other places. */ 5801 1.1 mrg 5802 1.1 mrg bool 5803 1.1 mrg mark_single_function (tree expr, tsubst_flags_t complain) 5804 1.1 mrg { 5805 1.1 mrg expr = maybe_undo_parenthesized_ref (expr); 5806 1.1 mrg expr = tree_strip_any_location_wrapper (expr); 5807 1.1 mrg 5808 1.1 mrg if (is_overloaded_fn (expr) == 1 5809 1.1 mrg && !mark_used (expr, complain) 5810 1.1 mrg && !(complain & tf_error)) 5811 1.1 mrg return false; 5812 1.1 mrg return true; 5813 1.1 mrg } 5814 1.1 mrg 5815 1.1 mrg /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program. 5816 1.1 mrg If DECL is a specialization or implicitly declared class member, 5817 1.1 mrg generate the actual definition. Return false if something goes 5818 1.1 mrg wrong, true otherwise. */ 5819 1.1 mrg 5820 1.1 mrg bool 5821 1.1 mrg mark_used (tree decl, tsubst_flags_t complain) 5822 1.1 mrg { 5823 1.1 mrg /* If we're just testing conversions or resolving overloads, we 5824 1.1 mrg don't want any permanent effects like forcing functions to be 5825 1.1 mrg output or instantiating templates. */ 5826 1.1 mrg if ((complain & tf_conv)) 5827 1.1 mrg return true; 5828 1.1 mrg 5829 1.1 mrg /* If DECL is a BASELINK for a single function, then treat it just 5830 1.1 mrg like the DECL for the function. Otherwise, if the BASELINK is 5831 1.1 mrg for an overloaded function, we don't know which function was 5832 1.1 mrg actually used until after overload resolution. */ 5833 1.1 mrg if (BASELINK_P (decl)) 5834 1.1 mrg { 5835 1.1 mrg tree fns = BASELINK_FUNCTIONS (decl); 5836 1.1 mrg if (really_overloaded_fn (fns)) 5837 1.1 mrg return true; 5838 1.1 mrg fns = OVL_FIRST (fns); 5839 1.1 mrg if (!mark_used (fns, complain)) 5840 1.1 mrg return false; 5841 1.1 mrg /* We might have deduced its return type. */ 5842 1.1 mrg TREE_TYPE (decl) = TREE_TYPE (fns); 5843 1.1 mrg return true; 5844 1.1 mrg } 5845 1.1 mrg 5846 1.1 mrg if (!DECL_P (decl)) 5847 1.1 mrg return true; 5848 1.1 mrg 5849 1.1 mrg /* Set TREE_USED for the benefit of -Wunused. */ 5850 1.1 mrg TREE_USED (decl) = true; 5851 1.1 mrg 5852 1.1 mrg /* And for structured bindings also the underlying decl. */ 5853 1.1 mrg if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl)) 5854 1.1 mrg TREE_USED (DECL_DECOMP_BASE (decl)) = true; 5855 1.1 mrg 5856 1.1 mrg if (TREE_CODE (decl) == TEMPLATE_DECL) 5857 1.1 mrg return true; 5858 1.1 mrg 5859 1.1 mrg if (DECL_CLONED_FUNCTION_P (decl)) 5860 1.1 mrg TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1; 5861 1.1 mrg 5862 1.1 mrg /* Mark enumeration types as used. */ 5863 1.1 mrg if (TREE_CODE (decl) == CONST_DECL) 5864 1.1 mrg used_types_insert (DECL_CONTEXT (decl)); 5865 1.1 mrg 5866 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL) 5867 1.1 mrg { 5868 1.1 mrg if (DECL_MAYBE_DELETED (decl)) 5869 1.1 mrg { 5870 1.1 mrg ++function_depth; 5871 1.1 mrg maybe_synthesize_method (decl); 5872 1.1 mrg --function_depth; 5873 1.1 mrg } 5874 1.1 mrg 5875 1.1 mrg if (DECL_DELETED_FN (decl)) 5876 1.1 mrg { 5877 1.1 mrg if (DECL_ARTIFICIAL (decl) 5878 1.1 mrg && DECL_CONV_FN_P (decl) 5879 1.1 mrg && LAMBDA_TYPE_P (DECL_CONTEXT (decl))) 5880 1.1 mrg /* We mark a lambda conversion op as deleted if we can't 5881 1.1 mrg generate it properly; see maybe_add_lambda_conv_op. */ 5882 1.1 mrg sorry ("converting lambda that uses %<...%> to function pointer"); 5883 1.1 mrg else if (complain & tf_error) 5884 1.1 mrg { 5885 1.1 mrg error ("use of deleted function %qD", decl); 5886 1.1 mrg if (!maybe_explain_implicit_delete (decl)) 5887 1.1 mrg inform (DECL_SOURCE_LOCATION (decl), "declared here"); 5888 1.1 mrg } 5889 1.1 mrg return false; 5890 1.1 mrg } 5891 1.1 mrg 5892 1.1 mrg if (!maybe_instantiate_noexcept (decl, complain)) 5893 1.1 mrg return false; 5894 1.1 mrg } 5895 1.1 mrg 5896 1.1 mrg if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)) 5897 1.1 mrg { 5898 1.1 mrg if (!DECL_LANG_SPECIFIC (decl)) 5899 1.1 mrg /* An unresolved dependent local extern. */ 5900 1.1 mrg return true; 5901 1.1 mrg 5902 1.1 mrg DECL_ODR_USED (decl) = 1; 5903 1.1 mrg auto alias = DECL_LOCAL_DECL_ALIAS (decl); 5904 1.1 mrg if (!alias || alias == error_mark_node) 5905 1.1 mrg return true; 5906 1.1 mrg 5907 1.1 mrg /* Process the underlying decl. */ 5908 1.1 mrg decl = alias; 5909 1.1 mrg TREE_USED (decl) = true; 5910 1.1 mrg } 5911 1.1 mrg 5912 1.1 mrg cp_handle_deprecated_or_unavailable (decl, complain); 5913 1.1 mrg 5914 1.1 mrg /* We can only check DECL_ODR_USED on variables or functions with 5915 1.1 mrg DECL_LANG_SPECIFIC set, and these are also the only decls that we 5916 1.1 mrg might need special handling for. */ 5917 1.1 mrg if (!VAR_OR_FUNCTION_DECL_P (decl) 5918 1.1 mrg || DECL_LANG_SPECIFIC (decl) == NULL 5919 1.1 mrg || DECL_THUNK_P (decl)) 5920 1.1 mrg { 5921 1.1 mrg if (!decl_dependent_p (decl) 5922 1.1 mrg && !require_deduced_type (decl, complain)) 5923 1.1 mrg return false; 5924 1.1 mrg return true; 5925 1.1 mrg } 5926 1.1 mrg 5927 1.1 mrg /* We only want to do this processing once. We don't need to keep trying 5928 1.1 mrg to instantiate inline templates, because unit-at-a-time will make sure 5929 1.1 mrg we get them compiled before functions that want to inline them. */ 5930 1.1 mrg if (DECL_ODR_USED (decl)) 5931 1.1 mrg return true; 5932 1.1 mrg 5933 1.1 mrg if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL 5934 1.1 mrg && !constraints_satisfied_p (decl)) 5935 1.1 mrg { 5936 1.1 mrg if (complain & tf_error) 5937 1.1 mrg { 5938 1.1 mrg auto_diagnostic_group d; 5939 1.1 mrg error ("use of function %qD with unsatisfied constraints", 5940 1.1 mrg decl); 5941 1.1 mrg location_t loc = DECL_SOURCE_LOCATION (decl); 5942 1.1 mrg inform (loc, "declared here"); 5943 1.1 mrg diagnose_constraints (loc, decl, NULL_TREE); 5944 1.1 mrg } 5945 1.1 mrg return false; 5946 1.1 mrg } 5947 1.1 mrg 5948 1.1 mrg /* Normally, we can wait until instantiation-time to synthesize DECL. 5949 1.1 mrg However, if DECL is a static data member initialized with a constant 5950 1.1 mrg or a constexpr function, we need it right now because a reference to 5951 1.1 mrg such a data member or a call to such function is not value-dependent. 5952 1.1 mrg For a function that uses auto in the return type, we need to instantiate 5953 1.1 mrg it to find out its type. For OpenMP user defined reductions, we need 5954 1.1 mrg them instantiated for reduction clauses which inline them by hand 5955 1.1 mrg directly. */ 5956 1.1 mrg maybe_instantiate_decl (decl); 5957 1.1 mrg 5958 1.1 mrg if (processing_template_decl || in_template_function ()) 5959 1.1 mrg return true; 5960 1.1 mrg 5961 1.1 mrg /* Check this too in case we're within instantiate_non_dependent_expr. */ 5962 1.1 mrg if (DECL_TEMPLATE_INFO (decl) 5963 1.1 mrg && uses_template_parms (DECL_TI_ARGS (decl))) 5964 1.1 mrg return true; 5965 1.1 mrg 5966 1.1 mrg if (!require_deduced_type (decl, complain)) 5967 1.1 mrg return false; 5968 1.1 mrg 5969 1.1 mrg if (builtin_pack_fn_p (decl)) 5970 1.1 mrg { 5971 1.1 mrg error ("use of built-in parameter pack %qD outside of a template", 5972 1.1 mrg DECL_NAME (decl)); 5973 1.1 mrg return false; 5974 1.1 mrg } 5975 1.1 mrg 5976 1.1 mrg /* If we don't need a value, then we don't need to synthesize DECL. */ 5977 1.1 mrg if (cp_unevaluated_operand || in_discarded_stmt) 5978 1.1 mrg return true; 5979 1.1 mrg 5980 1.1 mrg DECL_ODR_USED (decl) = 1; 5981 1.1 mrg if (DECL_CLONED_FUNCTION_P (decl)) 5982 1.1 mrg DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1; 5983 1.1 mrg 5984 1.1 mrg /* DR 757: A type without linkage shall not be used as the type of a 5985 1.1 mrg variable or function with linkage, unless 5986 1.1 mrg o the variable or function has extern "C" linkage (7.5 [dcl.link]), or 5987 1.1 mrg o the variable or function is not used (3.2 [basic.def.odr]) or is 5988 1.1 mrg defined in the same translation unit. */ 5989 1.1 mrg if (cxx_dialect > cxx98 5990 1.1 mrg && decl_linkage (decl) != lk_none 5991 1.1 mrg && !DECL_EXTERN_C_P (decl) 5992 1.1 mrg && !DECL_ARTIFICIAL (decl) 5993 1.1 mrg && !decl_defined_p (decl) 5994 1.1 mrg && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false)) 5995 1.1 mrg vec_safe_push (no_linkage_decls, decl); 5996 1.1 mrg 5997 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL 5998 1.1 mrg && DECL_DECLARED_INLINE_P (decl) 5999 1.1 mrg && !DECL_INITIAL (decl) 6000 1.1 mrg && !DECL_ARTIFICIAL (decl) 6001 1.1 mrg && !DECL_PURE_VIRTUAL_P (decl)) 6002 1.1 mrg /* Remember it, so we can check it was defined. */ 6003 1.1 mrg note_vague_linkage_fn (decl); 6004 1.1 mrg 6005 1.1 mrg /* Is it a synthesized method that needs to be synthesized? */ 6006 1.1 mrg if (TREE_CODE (decl) == FUNCTION_DECL 6007 1.1 mrg && DECL_DEFAULTED_FN (decl) 6008 1.1 mrg /* A function defaulted outside the class is synthesized either by 6009 1.1 mrg cp_finish_decl or instantiate_decl. */ 6010 1.1 mrg && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl) 6011 1.1 mrg && ! DECL_INITIAL (decl)) 6012 1.1 mrg { 6013 1.1 mrg /* Defer virtual destructors so that thunks get the right 6014 1.1 mrg linkage. */ 6015 1.1 mrg if (DECL_VIRTUAL_P (decl) && !at_eof) 6016 1.1 mrg { 6017 1.1 mrg note_vague_linkage_fn (decl); 6018 1.1 mrg return true; 6019 1.1 mrg } 6020 1.1 mrg 6021 1.1 mrg /* Remember the current location for a function we will end up 6022 1.1 mrg synthesizing. Then we can inform the user where it was 6023 1.1 mrg required in the case of error. */ 6024 1.1 mrg if (decl_remember_implicit_trigger_p (decl)) 6025 1.1 mrg DECL_SOURCE_LOCATION (decl) = input_location; 6026 1.1 mrg 6027 1.1 mrg /* Synthesizing an implicitly defined member function will result in 6028 1.1 mrg garbage collection. We must treat this situation as if we were 6029 1.1 mrg within the body of a function so as to avoid collecting live data 6030 1.1 mrg on the stack (such as overload resolution candidates). 6031 1.1 mrg 6032 1.1 mrg We could just let c_parse_final_cleanups handle synthesizing 6033 1.1 mrg this function by adding it to deferred_fns, but doing 6034 1.1 mrg it at the use site produces better error messages. */ 6035 1.1 mrg ++function_depth; 6036 1.1 mrg synthesize_method (decl); 6037 1.1 mrg --function_depth; 6038 1.1 mrg /* If this is a synthesized method we don't need to 6039 1.1 mrg do the instantiation test below. */ 6040 1.1 mrg } 6041 1.1 mrg else if (VAR_OR_FUNCTION_DECL_P (decl) 6042 1.1 mrg && DECL_TEMPLATE_INFO (decl) 6043 1.1 mrg && !DECL_DECLARED_CONCEPT_P (decl) 6044 1.1 mrg && (!DECL_EXPLICIT_INSTANTIATION (decl) 6045 1.1 mrg || always_instantiate_p (decl))) 6046 1.1 mrg /* If this is a function or variable that is an instance of some 6047 1.1 mrg template, we now know that we will need to actually do the 6048 1.1 mrg instantiation. We check that DECL is not an explicit 6049 1.1 mrg instantiation because that is not checked in instantiate_decl. 6050 1.1 mrg 6051 1.1 mrg We put off instantiating functions in order to improve compile 6052 1.1 mrg times. Maintaining a stack of active functions is expensive, 6053 1.1 mrg and the inliner knows to instantiate any functions it might 6054 1.1 mrg need. Therefore, we always try to defer instantiation. */ 6055 1.1 mrg { 6056 1.1 mrg ++function_depth; 6057 1.1 mrg instantiate_decl (decl, /*defer_ok=*/true, 6058 1.1 mrg /*expl_inst_class_mem_p=*/false); 6059 1.1 mrg --function_depth; 6060 1.1 mrg } 6061 1.1 mrg 6062 1.1 mrg return true; 6063 1.1 mrg } 6064 1.1 mrg 6065 1.1 mrg bool 6066 1.1 mrg mark_used (tree decl) 6067 1.1 mrg { 6068 1.1 mrg return mark_used (decl, tf_warning_or_error); 6069 1.1 mrg } 6070 1.1 mrg 6071 1.1 mrg tree 6072 1.1 mrg vtv_start_verification_constructor_init_function (void) 6073 1.1 mrg { 6074 1.1 mrg return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1); 6075 1.1 mrg } 6076 1.1 mrg 6077 1.1 mrg tree 6078 1.1 mrg vtv_finish_verification_constructor_init_function (tree function_body) 6079 1.1 mrg { 6080 1.1 mrg tree fn; 6081 1.1 mrg 6082 1.1 mrg finish_compound_stmt (function_body); 6083 1.1 mrg fn = finish_function (/*inline_p=*/false); 6084 1.1 mrg DECL_STATIC_CONSTRUCTOR (fn) = 1; 6085 1.1 mrg decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1); 6086 6087 return fn; 6088 } 6089 6090 #include "gt-cp-decl2.h" 6091