1 /* Some code common to C++ and ObjC++ front ends. 2 Copyright (C) 2004-2017 Free Software Foundation, Inc. 3 Contributed by Ziemowit Laski <zlaski@apple.com> 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify it under 8 the terms of the GNU General Public License as published by the Free 9 Software Foundation; either version 3, or (at your option) any later 10 version. 11 12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 13 WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 #include "config.h" 22 #include "system.h" 23 #include "coretypes.h" 24 #include "cp-tree.h" 25 #include "cp-objcp-common.h" 26 #include "dwarf2.h" 27 28 /* Special routine to get the alias set for C++. */ 29 30 alias_set_type 31 cxx_get_alias_set (tree t) 32 { 33 if (IS_FAKE_BASE_TYPE (t)) 34 /* The base variant of a type must be in the same alias set as the 35 complete type. */ 36 return get_alias_set (TYPE_CONTEXT (t)); 37 38 /* Punt on PMFs until we canonicalize functions properly. */ 39 if (TYPE_PTRMEMFUNC_P (t) 40 || (POINTER_TYPE_P (t) 41 && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))) 42 return 0; 43 44 return c_common_get_alias_set (t); 45 } 46 47 /* Called from check_global_declaration. */ 48 49 bool 50 cxx_warn_unused_global_decl (const_tree decl) 51 { 52 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl)) 53 return false; 54 if (DECL_IN_SYSTEM_HEADER (decl)) 55 return false; 56 57 return true; 58 } 59 60 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */ 61 size_t 62 cp_tree_size (enum tree_code code) 63 { 64 switch (code) 65 { 66 case PTRMEM_CST: return sizeof (struct ptrmem_cst); 67 case BASELINK: return sizeof (struct tree_baselink); 68 case TEMPLATE_PARM_INDEX: return sizeof (template_parm_index); 69 case DEFAULT_ARG: return sizeof (struct tree_default_arg); 70 case DEFERRED_NOEXCEPT: return sizeof (struct tree_deferred_noexcept); 71 case OVERLOAD: return sizeof (struct tree_overload); 72 case STATIC_ASSERT: return sizeof (struct tree_static_assert); 73 case TYPE_ARGUMENT_PACK: 74 case TYPE_PACK_EXPANSION: 75 return sizeof (struct tree_common); 76 77 case NONTYPE_ARGUMENT_PACK: 78 case EXPR_PACK_EXPANSION: 79 return sizeof (struct tree_exp); 80 81 case ARGUMENT_PACK_SELECT: 82 return sizeof (struct tree_argument_pack_select); 83 84 case TRAIT_EXPR: 85 return sizeof (struct tree_trait_expr); 86 87 case LAMBDA_EXPR: return sizeof (struct tree_lambda_expr); 88 89 case TEMPLATE_INFO: return sizeof (struct tree_template_info); 90 91 case CONSTRAINT_INFO: return sizeof (struct tree_constraint_info); 92 93 case USERDEF_LITERAL: return sizeof (struct tree_userdef_literal); 94 95 case TEMPLATE_DECL: return sizeof (struct tree_template_decl); 96 97 default: 98 if (TREE_CODE_CLASS (code) == tcc_declaration) 99 return sizeof (struct tree_decl_non_common); 100 gcc_unreachable (); 101 } 102 /* NOTREACHED */ 103 } 104 105 /* Returns true if T is a variably modified type, in the sense of C99. 106 FN is as passed to variably_modified_p. 107 This routine needs only check cases that cannot be handled by the 108 language-independent logic in tree.c. */ 109 110 bool 111 cp_var_mod_type_p (tree type, tree fn) 112 { 113 /* If TYPE is a pointer-to-member, it is variably modified if either 114 the class or the member are variably modified. */ 115 if (TYPE_PTRMEM_P (type)) 116 return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type), fn) 117 || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type), 118 fn)); 119 120 /* All other types are not variably modified. */ 121 return false; 122 } 123 124 /* This compares two types for equivalence ("compatible" in C-based languages). 125 This routine should only return 1 if it is sure. It should not be used 126 in contexts where erroneously returning 0 causes problems. */ 127 128 int 129 cxx_types_compatible_p (tree x, tree y) 130 { 131 return same_type_ignoring_top_level_qualifiers_p (x, y); 132 } 133 134 struct debug_type_hasher : ggc_cache_ptr_hash<tree_map> 135 { 136 static hashval_t hash (tree_map *m) { return tree_map_hash (m); } 137 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); } 138 139 static int 140 keep_cache_entry (tree_map *&e) 141 { 142 return ggc_marked_p (e->base.from); 143 } 144 }; 145 146 static GTY((cache)) hash_table<debug_type_hasher> *debug_type_hash; 147 148 /* Return a type to use in the debug info instead of TYPE, or NULL_TREE to 149 keep TYPE. */ 150 151 tree 152 cp_get_debug_type (const_tree type) 153 { 154 if (TYPE_PTRMEMFUNC_P (type) && !typedef_variant_p (type)) 155 { 156 if (debug_type_hash == NULL) 157 debug_type_hash = hash_table<debug_type_hasher>::create_ggc (512); 158 159 /* We cannot simply use build_offset_type here because the function uses 160 the type canonicalization hashtable, which is GC-ed, so its behavior 161 depends on the actual collection points. Since we are building these 162 types on the fly for the debug info only, they would not be attached 163 to any GC root and always be swept, so we would make the contents of 164 the debug info depend on the collection points. */ 165 struct tree_map in, *h, **slot; 166 167 in.base.from = CONST_CAST_TREE (type); 168 in.hash = htab_hash_pointer (type); 169 slot = debug_type_hash->find_slot_with_hash (&in, in.hash, INSERT); 170 if (*slot) 171 return (*slot)->to; 172 173 tree t = build_offset_type (TYPE_PTRMEMFUNC_OBJECT_TYPE (type), 174 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))); 175 176 h = ggc_alloc<tree_map> (); 177 h->base.from = CONST_CAST_TREE (type); 178 h->hash = htab_hash_pointer (type); 179 h->to = t; 180 *slot = h; 181 182 return t; 183 } 184 185 return NULL_TREE; 186 } 187 188 /* Return -1 if dwarf ATTR shouldn't be added for DECL, or the attribute 189 value otherwise. */ 190 int 191 cp_decl_dwarf_attribute (const_tree decl, int attr) 192 { 193 if (decl == NULL_TREE) 194 return -1; 195 196 switch (attr) 197 { 198 case DW_AT_explicit: 199 if (TREE_CODE (decl) == FUNCTION_DECL 200 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl)) 201 && DECL_NONCONVERTING_P (decl)) 202 return 1; 203 break; 204 205 case DW_AT_deleted: 206 if (TREE_CODE (decl) == FUNCTION_DECL 207 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl)) 208 && DECL_DELETED_FN (decl)) 209 return 1; 210 break; 211 212 case DW_AT_defaulted: 213 if (TREE_CODE (decl) == FUNCTION_DECL 214 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl)) 215 && DECL_DEFAULTED_FN (decl)) 216 { 217 if (DECL_DEFAULTED_IN_CLASS_P (decl)) 218 return DW_DEFAULTED_in_class; 219 220 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (decl)) 221 return DW_DEFAULTED_out_of_class; 222 } 223 break; 224 225 case DW_AT_const_expr: 226 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl)) 227 return 1; 228 break; 229 230 case DW_AT_reference: 231 if (TREE_CODE (decl) == FUNCTION_DECL 232 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) 233 && FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)) 234 && !FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl))) 235 return 1; 236 break; 237 238 case DW_AT_rvalue_reference: 239 if (TREE_CODE (decl) == FUNCTION_DECL 240 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) 241 && FUNCTION_REF_QUALIFIED (TREE_TYPE (decl)) 242 && FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl))) 243 return 1; 244 break; 245 246 case DW_AT_inline: 247 if (VAR_P (decl) && DECL_INLINE_VAR_P (decl)) 248 { 249 if (DECL_VAR_DECLARED_INLINE_P (decl)) 250 return DW_INL_declared_inlined; 251 else 252 return DW_INL_inlined; 253 } 254 break; 255 256 default: 257 break; 258 } 259 260 return -1; 261 } 262 263 /* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute 264 value otherwise. */ 265 int 266 cp_type_dwarf_attribute (const_tree type, int attr) 267 { 268 if (type == NULL_TREE) 269 return -1; 270 271 switch (attr) 272 { 273 case DW_AT_reference: 274 if ((TREE_CODE (type) == FUNCTION_TYPE 275 || TREE_CODE (type) == METHOD_TYPE) 276 && FUNCTION_REF_QUALIFIED (type) 277 && !FUNCTION_RVALUE_QUALIFIED (type)) 278 return 1; 279 break; 280 281 case DW_AT_rvalue_reference: 282 if ((TREE_CODE (type) == FUNCTION_TYPE 283 || TREE_CODE (type) == METHOD_TYPE) 284 && FUNCTION_REF_QUALIFIED (type) 285 && FUNCTION_RVALUE_QUALIFIED (type)) 286 return 1; 287 break; 288 289 default: 290 break; 291 } 292 293 return -1; 294 } 295 296 /* Return the unit size of TYPE without reusable tail padding. */ 297 298 tree 299 cp_unit_size_without_reusable_padding (tree type) 300 { 301 if (CLASS_TYPE_P (type)) 302 return CLASSTYPE_SIZE_UNIT (type); 303 return TYPE_SIZE_UNIT (type); 304 } 305 306 /* Stubs to keep c-opts.c happy. */ 307 void 308 push_file_scope (void) 309 { 310 } 311 312 void 313 pop_file_scope (void) 314 { 315 } 316 317 /* c-pragma.c needs to query whether a decl has extern "C" linkage. */ 318 bool 319 has_c_linkage (const_tree decl) 320 { 321 return DECL_EXTERN_C_P (decl); 322 } 323 324 static GTY ((cache)) 325 hash_table<tree_decl_map_cache_hasher> *shadowed_var_for_decl; 326 327 /* Lookup a shadowed var for FROM, and return it if we find one. */ 328 329 tree 330 decl_shadowed_for_var_lookup (tree from) 331 { 332 struct tree_decl_map *h, in; 333 in.base.from = from; 334 335 h = shadowed_var_for_decl->find_with_hash (&in, DECL_UID (from)); 336 if (h) 337 return h->to; 338 return NULL_TREE; 339 } 340 341 /* Insert a mapping FROM->TO in the shadowed var hashtable. */ 342 343 void 344 decl_shadowed_for_var_insert (tree from, tree to) 345 { 346 struct tree_decl_map *h; 347 348 h = ggc_alloc<tree_decl_map> (); 349 h->base.from = from; 350 h->to = to; 351 *shadowed_var_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h; 352 } 353 354 void 355 init_shadowed_var_for_decl (void) 356 { 357 shadowed_var_for_decl 358 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512); 359 } 360 361 /* Return true if stmt can fall through. Used by block_may_fallthru 362 default case. */ 363 364 bool 365 cxx_block_may_fallthru (const_tree stmt) 366 { 367 switch (TREE_CODE (stmt)) 368 { 369 case EXPR_STMT: 370 return block_may_fallthru (EXPR_STMT_EXPR (stmt)); 371 372 case THROW_EXPR: 373 return false; 374 375 default: 376 return true; 377 } 378 } 379 380 void 381 cp_common_init_ts (void) 382 { 383 MARK_TS_DECL_NON_COMMON (USING_DECL); 384 MARK_TS_DECL_COMMON (TEMPLATE_DECL); 385 MARK_TS_DECL_COMMON (WILDCARD_DECL); 386 387 MARK_TS_COMMON (TEMPLATE_TEMPLATE_PARM); 388 MARK_TS_COMMON (TEMPLATE_TYPE_PARM); 389 MARK_TS_COMMON (TEMPLATE_PARM_INDEX); 390 MARK_TS_COMMON (OVERLOAD); 391 MARK_TS_COMMON (TEMPLATE_INFO); 392 MARK_TS_COMMON (TYPENAME_TYPE); 393 MARK_TS_COMMON (TYPEOF_TYPE); 394 MARK_TS_COMMON (UNDERLYING_TYPE); 395 MARK_TS_COMMON (BASELINK); 396 MARK_TS_COMMON (TYPE_PACK_EXPANSION); 397 MARK_TS_COMMON (TYPE_ARGUMENT_PACK); 398 MARK_TS_COMMON (DECLTYPE_TYPE); 399 MARK_TS_COMMON (BOUND_TEMPLATE_TEMPLATE_PARM); 400 MARK_TS_COMMON (UNBOUND_CLASS_TEMPLATE); 401 402 MARK_TS_TYPED (EXPR_PACK_EXPANSION); 403 MARK_TS_TYPED (SWITCH_STMT); 404 MARK_TS_TYPED (IF_STMT); 405 MARK_TS_TYPED (FOR_STMT); 406 MARK_TS_TYPED (RANGE_FOR_STMT); 407 MARK_TS_TYPED (AGGR_INIT_EXPR); 408 MARK_TS_TYPED (EXPR_STMT); 409 MARK_TS_TYPED (EH_SPEC_BLOCK); 410 MARK_TS_TYPED (CLEANUP_STMT); 411 MARK_TS_TYPED (SCOPE_REF); 412 MARK_TS_TYPED (CAST_EXPR); 413 MARK_TS_TYPED (NON_DEPENDENT_EXPR); 414 MARK_TS_TYPED (MODOP_EXPR); 415 MARK_TS_TYPED (TRY_BLOCK); 416 MARK_TS_TYPED (THROW_EXPR); 417 MARK_TS_TYPED (HANDLER); 418 MARK_TS_TYPED (REINTERPRET_CAST_EXPR); 419 MARK_TS_TYPED (CONST_CAST_EXPR); 420 MARK_TS_TYPED (STATIC_CAST_EXPR); 421 MARK_TS_TYPED (DYNAMIC_CAST_EXPR); 422 MARK_TS_TYPED (IMPLICIT_CONV_EXPR); 423 MARK_TS_TYPED (TEMPLATE_ID_EXPR); 424 MARK_TS_TYPED (ARROW_EXPR); 425 MARK_TS_TYPED (SIZEOF_EXPR); 426 MARK_TS_TYPED (ALIGNOF_EXPR); 427 MARK_TS_TYPED (AT_ENCODE_EXPR); 428 MARK_TS_TYPED (UNARY_PLUS_EXPR); 429 MARK_TS_TYPED (TRAIT_EXPR); 430 MARK_TS_TYPED (TYPE_ARGUMENT_PACK); 431 MARK_TS_TYPED (NOEXCEPT_EXPR); 432 MARK_TS_TYPED (NONTYPE_ARGUMENT_PACK); 433 MARK_TS_TYPED (WHILE_STMT); 434 MARK_TS_TYPED (NEW_EXPR); 435 MARK_TS_TYPED (VEC_NEW_EXPR); 436 MARK_TS_TYPED (BREAK_STMT); 437 MARK_TS_TYPED (MEMBER_REF); 438 MARK_TS_TYPED (DOTSTAR_EXPR); 439 MARK_TS_TYPED (DO_STMT); 440 MARK_TS_TYPED (DELETE_EXPR); 441 MARK_TS_TYPED (VEC_DELETE_EXPR); 442 MARK_TS_TYPED (CONTINUE_STMT); 443 MARK_TS_TYPED (TAG_DEFN); 444 MARK_TS_TYPED (PSEUDO_DTOR_EXPR); 445 MARK_TS_TYPED (TYPEID_EXPR); 446 MARK_TS_TYPED (MUST_NOT_THROW_EXPR); 447 MARK_TS_TYPED (STMT_EXPR); 448 MARK_TS_TYPED (OFFSET_REF); 449 MARK_TS_TYPED (OFFSETOF_EXPR); 450 MARK_TS_TYPED (ADDRESSOF_EXPR); 451 MARK_TS_TYPED (PTRMEM_CST); 452 MARK_TS_TYPED (EMPTY_CLASS_EXPR); 453 MARK_TS_TYPED (VEC_INIT_EXPR); 454 MARK_TS_TYPED (USING_STMT); 455 MARK_TS_TYPED (LAMBDA_EXPR); 456 MARK_TS_TYPED (CTOR_INITIALIZER); 457 MARK_TS_TYPED (ARRAY_NOTATION_REF); 458 MARK_TS_TYPED (REQUIRES_EXPR); 459 MARK_TS_TYPED (UNARY_LEFT_FOLD_EXPR); 460 MARK_TS_TYPED (UNARY_RIGHT_FOLD_EXPR); 461 MARK_TS_TYPED (BINARY_LEFT_FOLD_EXPR); 462 MARK_TS_TYPED (BINARY_RIGHT_FOLD_EXPR); 463 } 464 465 #include "gt-cp-cp-objcp-common.h" 466