PR rtl-optimization/82913
[official-gcc.git] / gcc / cp / cp-objcp-common.c
blobe051d66b67b837b41b83af772006d1e5a25ae83e
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>
5 This file is part of GCC.
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.
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.
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/>. */
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"
28 /* Special routine to get the alias set for C++. */
30 alias_set_type
31 cxx_get_alias_set (tree t)
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));
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;
44 return c_common_get_alias_set (t);
47 /* Called from check_global_declaration. */
49 bool
50 cxx_warn_unused_global_decl (const_tree decl)
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;
57 return true;
60 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */
61 size_t
62 cp_tree_size (enum tree_code code)
64 gcc_checking_assert (code >= NUM_TREE_CODES);
65 switch (code)
67 case PTRMEM_CST: return sizeof (ptrmem_cst);
68 case BASELINK: return sizeof (tree_baselink);
69 case TEMPLATE_PARM_INDEX: return sizeof (template_parm_index);
70 case DEFAULT_ARG: return sizeof (tree_default_arg);
71 case DEFERRED_NOEXCEPT: return sizeof (tree_deferred_noexcept);
72 case OVERLOAD: return sizeof (tree_overload);
73 case STATIC_ASSERT: return sizeof (tree_static_assert);
74 case TYPE_ARGUMENT_PACK:
75 case TYPE_PACK_EXPANSION: return sizeof (tree_type_non_common);
76 case NONTYPE_ARGUMENT_PACK:
77 case EXPR_PACK_EXPANSION: return sizeof (tree_exp);
78 case ARGUMENT_PACK_SELECT: return sizeof (tree_argument_pack_select);
79 case TRAIT_EXPR: return sizeof (tree_trait_expr);
80 case LAMBDA_EXPR: return sizeof (tree_lambda_expr);
81 case TEMPLATE_INFO: return sizeof (tree_template_info);
82 case CONSTRAINT_INFO: return sizeof (tree_constraint_info);
83 case USERDEF_LITERAL: return sizeof (tree_userdef_literal);
84 case TEMPLATE_DECL: return sizeof (tree_template_decl);
85 default:
86 switch (TREE_CODE_CLASS (code))
88 case tcc_declaration: return sizeof (tree_decl_non_common);
89 case tcc_type: return sizeof (tree_type_non_common);
90 default: gcc_unreachable ();
93 /* NOTREACHED */
96 /* Returns true if T is a variably modified type, in the sense of C99.
97 FN is as passed to variably_modified_p.
98 This routine needs only check cases that cannot be handled by the
99 language-independent logic in tree.c. */
101 bool
102 cp_var_mod_type_p (tree type, tree fn)
104 /* If TYPE is a pointer-to-member, it is variably modified if either
105 the class or the member are variably modified. */
106 if (TYPE_PTRMEM_P (type))
107 return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type), fn)
108 || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type),
109 fn));
111 /* All other types are not variably modified. */
112 return false;
115 /* This compares two types for equivalence ("compatible" in C-based languages).
116 This routine should only return 1 if it is sure. It should not be used
117 in contexts where erroneously returning 0 causes problems. */
120 cxx_types_compatible_p (tree x, tree y)
122 return same_type_ignoring_top_level_qualifiers_p (x, y);
125 struct debug_type_hasher : ggc_cache_ptr_hash<tree_map>
127 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
128 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
130 static int
131 keep_cache_entry (tree_map *&e)
133 return ggc_marked_p (e->base.from);
137 static GTY((cache)) hash_table<debug_type_hasher> *debug_type_hash;
139 /* Return a type to use in the debug info instead of TYPE, or NULL_TREE to
140 keep TYPE. */
142 tree
143 cp_get_debug_type (const_tree type)
145 if (TYPE_PTRMEMFUNC_P (type) && !typedef_variant_p (type))
147 if (debug_type_hash == NULL)
148 debug_type_hash = hash_table<debug_type_hasher>::create_ggc (512);
150 /* We cannot simply use build_offset_type here because the function uses
151 the type canonicalization hashtable, which is GC-ed, so its behavior
152 depends on the actual collection points. Since we are building these
153 types on the fly for the debug info only, they would not be attached
154 to any GC root and always be swept, so we would make the contents of
155 the debug info depend on the collection points. */
156 struct tree_map in, *h, **slot;
158 in.base.from = CONST_CAST_TREE (type);
159 in.hash = htab_hash_pointer (type);
160 slot = debug_type_hash->find_slot_with_hash (&in, in.hash, INSERT);
161 if (*slot)
162 return (*slot)->to;
164 tree t = build_offset_type (TYPE_PTRMEMFUNC_OBJECT_TYPE (type),
165 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)));
167 h = ggc_alloc<tree_map> ();
168 h->base.from = CONST_CAST_TREE (type);
169 h->hash = htab_hash_pointer (type);
170 h->to = t;
171 *slot = h;
173 return t;
176 return NULL_TREE;
179 /* Return -1 if dwarf ATTR shouldn't be added for DECL, or the attribute
180 value otherwise. */
182 cp_decl_dwarf_attribute (const_tree decl, int attr)
184 if (decl == NULL_TREE)
185 return -1;
187 switch (attr)
189 case DW_AT_explicit:
190 if (TREE_CODE (decl) == FUNCTION_DECL
191 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl))
192 && DECL_NONCONVERTING_P (decl))
193 return 1;
194 break;
196 case DW_AT_deleted:
197 if (TREE_CODE (decl) == FUNCTION_DECL
198 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl))
199 && DECL_DELETED_FN (decl))
200 return 1;
201 break;
203 case DW_AT_defaulted:
204 if (TREE_CODE (decl) == FUNCTION_DECL
205 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl))
206 && DECL_DEFAULTED_FN (decl))
208 if (DECL_DEFAULTED_IN_CLASS_P (decl))
209 return DW_DEFAULTED_in_class;
211 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
212 return DW_DEFAULTED_out_of_class;
214 break;
216 case DW_AT_const_expr:
217 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
218 return 1;
219 break;
221 case DW_AT_reference:
222 if (TREE_CODE (decl) == FUNCTION_DECL
223 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
224 && FUNCTION_REF_QUALIFIED (TREE_TYPE (decl))
225 && !FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
226 return 1;
227 break;
229 case DW_AT_rvalue_reference:
230 if (TREE_CODE (decl) == FUNCTION_DECL
231 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
232 && FUNCTION_REF_QUALIFIED (TREE_TYPE (decl))
233 && FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
234 return 1;
235 break;
237 case DW_AT_inline:
238 if (VAR_P (decl) && DECL_INLINE_VAR_P (decl))
240 if (DECL_VAR_DECLARED_INLINE_P (decl))
241 return DW_INL_declared_inlined;
242 else
243 return DW_INL_inlined;
245 break;
247 case DW_AT_export_symbols:
248 if (TREE_CODE (decl) == NAMESPACE_DECL
249 && (DECL_NAMESPACE_INLINE_P (decl)
250 || (DECL_NAME (decl) == NULL_TREE && dwarf_version >= 5)))
251 return 1;
252 break;
254 default:
255 break;
258 return -1;
261 /* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
262 value otherwise. */
264 cp_type_dwarf_attribute (const_tree type, int attr)
266 if (type == NULL_TREE)
267 return -1;
269 switch (attr)
271 case DW_AT_reference:
272 if ((TREE_CODE (type) == FUNCTION_TYPE
273 || TREE_CODE (type) == METHOD_TYPE)
274 && FUNCTION_REF_QUALIFIED (type)
275 && !FUNCTION_RVALUE_QUALIFIED (type))
276 return 1;
277 break;
279 case DW_AT_rvalue_reference:
280 if ((TREE_CODE (type) == FUNCTION_TYPE
281 || TREE_CODE (type) == METHOD_TYPE)
282 && FUNCTION_REF_QUALIFIED (type)
283 && FUNCTION_RVALUE_QUALIFIED (type))
284 return 1;
285 break;
287 default:
288 break;
291 return -1;
294 /* Return the unit size of TYPE without reusable tail padding. */
296 tree
297 cp_unit_size_without_reusable_padding (tree type)
299 if (CLASS_TYPE_P (type))
300 return CLASSTYPE_SIZE_UNIT (type);
301 return TYPE_SIZE_UNIT (type);
304 /* Stubs to keep c-opts.c happy. */
305 void
306 push_file_scope (void)
310 void
311 pop_file_scope (void)
315 /* c-pragma.c needs to query whether a decl has extern "C" linkage. */
316 bool
317 has_c_linkage (const_tree decl)
319 return DECL_EXTERN_C_P (decl);
322 static GTY ((cache))
323 hash_table<tree_decl_map_cache_hasher> *shadowed_var_for_decl;
325 /* Lookup a shadowed var for FROM, and return it if we find one. */
327 tree
328 decl_shadowed_for_var_lookup (tree from)
330 struct tree_decl_map *h, in;
331 in.base.from = from;
333 h = shadowed_var_for_decl->find_with_hash (&in, DECL_UID (from));
334 if (h)
335 return h->to;
336 return NULL_TREE;
339 /* Insert a mapping FROM->TO in the shadowed var hashtable. */
341 void
342 decl_shadowed_for_var_insert (tree from, tree to)
344 struct tree_decl_map *h;
346 h = ggc_alloc<tree_decl_map> ();
347 h->base.from = from;
348 h->to = to;
349 *shadowed_var_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
352 void
353 init_shadowed_var_for_decl (void)
355 shadowed_var_for_decl
356 = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
359 /* Return true if stmt can fall through. Used by block_may_fallthru
360 default case. */
362 bool
363 cxx_block_may_fallthru (const_tree stmt)
365 switch (TREE_CODE (stmt))
367 case EXPR_STMT:
368 return block_may_fallthru (EXPR_STMT_EXPR (stmt));
370 case THROW_EXPR:
371 return false;
373 default:
374 return true;
378 /* Return the list of decls in the global namespace. */
380 tree
381 cp_get_global_decls ()
383 return NAMESPACE_LEVEL (global_namespace)->names;
386 /* Push DECL into the current scope. */
388 tree
389 cp_pushdecl (tree decl)
391 return pushdecl (decl);
394 /* Get the global value binding of NAME. Called directly from
395 c-common.c, not via a hook. */
397 tree
398 identifier_global_value (tree name)
400 return get_global_binding (name);
403 /* Register c++-specific dumps. */
405 void
406 cp_register_dumps (gcc::dump_manager *dumps)
408 class_dump_id = dumps->dump_register
409 (".class", "lang-class", "lang-class", DK_lang, OPTGROUP_NONE, false);
411 raw_dump_id = dumps->dump_register
412 (".raw", "lang-raw", "lang-raw", DK_lang, OPTGROUP_NONE, false);
415 void
416 cp_common_init_ts (void)
418 MARK_TS_DECL_NON_COMMON (USING_DECL);
419 MARK_TS_DECL_COMMON (TEMPLATE_DECL);
420 MARK_TS_DECL_COMMON (WILDCARD_DECL);
422 MARK_TS_COMMON (TEMPLATE_TEMPLATE_PARM);
423 MARK_TS_COMMON (TEMPLATE_TYPE_PARM);
424 MARK_TS_COMMON (TEMPLATE_PARM_INDEX);
425 MARK_TS_COMMON (OVERLOAD);
426 MARK_TS_COMMON (TEMPLATE_INFO);
427 MARK_TS_COMMON (TYPENAME_TYPE);
428 MARK_TS_COMMON (TYPEOF_TYPE);
429 MARK_TS_COMMON (UNDERLYING_TYPE);
430 MARK_TS_COMMON (BASELINK);
431 MARK_TS_COMMON (TYPE_PACK_EXPANSION);
432 MARK_TS_COMMON (TYPE_ARGUMENT_PACK);
433 MARK_TS_COMMON (DECLTYPE_TYPE);
434 MARK_TS_COMMON (BOUND_TEMPLATE_TEMPLATE_PARM);
435 MARK_TS_COMMON (UNBOUND_CLASS_TEMPLATE);
437 MARK_TS_TYPED (EXPR_PACK_EXPANSION);
438 MARK_TS_TYPED (SWITCH_STMT);
439 MARK_TS_TYPED (IF_STMT);
440 MARK_TS_TYPED (FOR_STMT);
441 MARK_TS_TYPED (RANGE_FOR_STMT);
442 MARK_TS_TYPED (AGGR_INIT_EXPR);
443 MARK_TS_TYPED (EXPR_STMT);
444 MARK_TS_TYPED (EH_SPEC_BLOCK);
445 MARK_TS_TYPED (CLEANUP_STMT);
446 MARK_TS_TYPED (SCOPE_REF);
447 MARK_TS_TYPED (CAST_EXPR);
448 MARK_TS_TYPED (NON_DEPENDENT_EXPR);
449 MARK_TS_TYPED (MODOP_EXPR);
450 MARK_TS_TYPED (TRY_BLOCK);
451 MARK_TS_TYPED (THROW_EXPR);
452 MARK_TS_TYPED (HANDLER);
453 MARK_TS_TYPED (REINTERPRET_CAST_EXPR);
454 MARK_TS_TYPED (CONST_CAST_EXPR);
455 MARK_TS_TYPED (STATIC_CAST_EXPR);
456 MARK_TS_TYPED (DYNAMIC_CAST_EXPR);
457 MARK_TS_TYPED (IMPLICIT_CONV_EXPR);
458 MARK_TS_TYPED (TEMPLATE_ID_EXPR);
459 MARK_TS_TYPED (ARROW_EXPR);
460 MARK_TS_TYPED (SIZEOF_EXPR);
461 MARK_TS_TYPED (ALIGNOF_EXPR);
462 MARK_TS_TYPED (AT_ENCODE_EXPR);
463 MARK_TS_TYPED (UNARY_PLUS_EXPR);
464 MARK_TS_TYPED (TRAIT_EXPR);
465 MARK_TS_TYPED (TYPE_ARGUMENT_PACK);
466 MARK_TS_TYPED (NOEXCEPT_EXPR);
467 MARK_TS_TYPED (NONTYPE_ARGUMENT_PACK);
468 MARK_TS_TYPED (WHILE_STMT);
469 MARK_TS_TYPED (NEW_EXPR);
470 MARK_TS_TYPED (VEC_NEW_EXPR);
471 MARK_TS_TYPED (BREAK_STMT);
472 MARK_TS_TYPED (MEMBER_REF);
473 MARK_TS_TYPED (DOTSTAR_EXPR);
474 MARK_TS_TYPED (DO_STMT);
475 MARK_TS_TYPED (DELETE_EXPR);
476 MARK_TS_TYPED (VEC_DELETE_EXPR);
477 MARK_TS_TYPED (CONTINUE_STMT);
478 MARK_TS_TYPED (TAG_DEFN);
479 MARK_TS_TYPED (PSEUDO_DTOR_EXPR);
480 MARK_TS_TYPED (TYPEID_EXPR);
481 MARK_TS_TYPED (MUST_NOT_THROW_EXPR);
482 MARK_TS_TYPED (STMT_EXPR);
483 MARK_TS_TYPED (OFFSET_REF);
484 MARK_TS_TYPED (OFFSETOF_EXPR);
485 MARK_TS_TYPED (ADDRESSOF_EXPR);
486 MARK_TS_TYPED (PTRMEM_CST);
487 MARK_TS_TYPED (EMPTY_CLASS_EXPR);
488 MARK_TS_TYPED (VEC_INIT_EXPR);
489 MARK_TS_TYPED (USING_STMT);
490 MARK_TS_TYPED (LAMBDA_EXPR);
491 MARK_TS_TYPED (CTOR_INITIALIZER);
492 MARK_TS_TYPED (ARRAY_NOTATION_REF);
493 MARK_TS_TYPED (REQUIRES_EXPR);
494 MARK_TS_TYPED (UNARY_LEFT_FOLD_EXPR);
495 MARK_TS_TYPED (UNARY_RIGHT_FOLD_EXPR);
496 MARK_TS_TYPED (BINARY_LEFT_FOLD_EXPR);
497 MARK_TS_TYPED (BINARY_RIGHT_FOLD_EXPR);
500 #include "gt-cp-cp-objcp-common.h"