Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / cp / cp-objcp-common.cc
blob93b027b80ce858ab0620a106f181e0ad2aeb1baa
1 /* Some code common to C++ and ObjC++ front ends.
2 Copyright (C) 2004-2023 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"
27 #include "stringpool.h"
28 #include "contracts.h"
30 /* Special routine to get the alias set for C++. */
32 alias_set_type
33 cxx_get_alias_set (tree t)
35 if (IS_FAKE_BASE_TYPE (t))
36 /* The base variant of a type must be in the same alias set as the
37 complete type. */
38 return get_alias_set (TYPE_CONTEXT (t));
40 /* Punt on PMFs until we canonicalize functions properly. */
41 if (TYPE_PTRMEMFUNC_P (t)
42 || (INDIRECT_TYPE_P (t)
43 && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))))
44 return 0;
46 return c_common_get_alias_set (t);
49 /* Called from check_global_declaration. */
51 bool
52 cxx_warn_unused_global_decl (const_tree decl)
54 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
55 return false;
56 if (DECL_IN_SYSTEM_HEADER (decl))
57 return false;
59 return true;
62 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */
63 size_t
64 cp_tree_size (enum tree_code code)
66 gcc_checking_assert (code >= NUM_TREE_CODES);
67 switch (code)
69 case PTRMEM_CST: return sizeof (ptrmem_cst);
70 case BASELINK: return sizeof (tree_baselink);
71 case TEMPLATE_PARM_INDEX: return sizeof (template_parm_index);
72 case DEFERRED_PARSE: return sizeof (tree_deferred_parse);
73 case DEFERRED_NOEXCEPT: return sizeof (tree_deferred_noexcept);
74 case OVERLOAD: return sizeof (tree_overload);
75 case STATIC_ASSERT: return sizeof (tree_static_assert);
76 #if 0
77 /* This would match cp_common_init_ts, but breaks GC because
78 tree_node_structure_for_code returns TS_TYPE_NON_COMMON for all
79 types. */
80 case UNBOUND_CLASS_TEMPLATE:
81 case TYPE_ARGUMENT_PACK: return sizeof (tree_type_common);
82 #endif
83 case ARGUMENT_PACK_SELECT: return sizeof (tree_argument_pack_select);
84 case TRAIT_EXPR: return sizeof (tree_trait_expr);
85 case LAMBDA_EXPR: return sizeof (tree_lambda_expr);
86 case TEMPLATE_INFO: return sizeof (tree_template_info);
87 case CONSTRAINT_INFO: return sizeof (tree_constraint_info);
88 case USERDEF_LITERAL: return sizeof (tree_userdef_literal);
89 case TEMPLATE_DECL: return sizeof (tree_template_decl);
90 case ASSERTION_STMT: return sizeof (tree_exp);
91 case PRECONDITION_STMT: return sizeof (tree_exp);
92 case POSTCONDITION_STMT: return sizeof (tree_exp);
93 default:
94 switch (TREE_CODE_CLASS (code))
96 case tcc_declaration: return sizeof (tree_decl_non_common);
97 case tcc_type: return sizeof (tree_type_non_common);
98 default: gcc_unreachable ();
101 /* NOTREACHED */
104 /* Returns true if T is a variably modified type, in the sense of C99.
105 FN is as passed to variably_modified_p.
106 This routine needs only check cases that cannot be handled by the
107 language-independent logic in tree.cc. */
109 bool
110 cp_var_mod_type_p (tree type, tree fn)
112 /* If TYPE is a pointer-to-member, it is variably modified if either
113 the class or the member are variably modified. */
114 if (TYPE_PTRMEM_P (type))
115 return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type), fn)
116 || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type),
117 fn));
119 /* All other types are not variably modified. */
120 return false;
123 /* This compares two types for equivalence ("compatible" in C-based languages).
124 This routine should only return 1 if it is sure. It should not be used
125 in contexts where erroneously returning 0 causes problems. */
128 cxx_types_compatible_p (tree x, tree y)
130 return same_type_ignoring_top_level_qualifiers_p (x, y);
133 static GTY((cache)) type_tree_cache_map *debug_type_map;
135 /* Return a type to use in the debug info instead of TYPE, or NULL_TREE to
136 keep TYPE. */
138 tree
139 cp_get_debug_type (const_tree type)
141 tree dtype = NULL_TREE;
143 if (TYPE_PTRMEMFUNC_P (type) && !typedef_variant_p (type))
144 dtype = build_offset_type (TYPE_PTRMEMFUNC_OBJECT_TYPE (type),
145 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)));
147 /* We cannot simply return the debug type here because the function uses
148 the type canonicalization hashtable, which is GC-ed, so its behavior
149 depends on the actual collection points. Since we are building these
150 types on the fly for the debug info only, they would not be attached
151 to any GC root and always be swept, so we would make the contents of
152 the debug info depend on the collection points. */
153 if (dtype)
155 tree ktype = CONST_CAST_TREE (type);
156 if (tree *slot = hash_map_safe_get (debug_type_map, ktype))
157 return *slot;
158 hash_map_safe_put<hm_ggc> (debug_type_map, ktype, dtype);
161 return dtype;
164 /* Return -1 if dwarf ATTR shouldn't be added for DECL, or the attribute
165 value otherwise. */
167 cp_decl_dwarf_attribute (const_tree decl, int attr)
169 if (decl == NULL_TREE)
170 return -1;
172 switch (attr)
174 case DW_AT_explicit:
175 if (TREE_CODE (decl) == FUNCTION_DECL
176 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl))
177 && DECL_NONCONVERTING_P (decl))
178 return 1;
179 break;
181 case DW_AT_deleted:
182 if (TREE_CODE (decl) == FUNCTION_DECL
183 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl))
184 && DECL_DELETED_FN (decl))
185 return 1;
186 break;
188 case DW_AT_defaulted:
189 if (TREE_CODE (decl) == FUNCTION_DECL
190 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl))
191 && DECL_DEFAULTED_FN (decl))
193 if (DECL_DEFAULTED_IN_CLASS_P (decl))
194 return DW_DEFAULTED_in_class;
196 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
197 return DW_DEFAULTED_out_of_class;
199 break;
201 case DW_AT_const_expr:
202 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
203 return 1;
204 break;
206 case DW_AT_reference:
207 if (TREE_CODE (decl) == FUNCTION_DECL
208 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
209 && FUNCTION_REF_QUALIFIED (TREE_TYPE (decl))
210 && !FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
211 return 1;
212 break;
214 case DW_AT_rvalue_reference:
215 if (TREE_CODE (decl) == FUNCTION_DECL
216 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
217 && FUNCTION_REF_QUALIFIED (TREE_TYPE (decl))
218 && FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl)))
219 return 1;
220 break;
222 case DW_AT_inline:
223 if (VAR_P (decl) && DECL_INLINE_VAR_P (decl))
225 if (DECL_VAR_DECLARED_INLINE_P (decl))
226 return DW_INL_declared_inlined;
227 else
228 return DW_INL_inlined;
230 break;
232 case DW_AT_export_symbols:
233 if (TREE_CODE (decl) == NAMESPACE_DECL
234 && (DECL_NAMESPACE_INLINE_P (decl)
235 || (DECL_NAME (decl) == NULL_TREE && dwarf_version >= 5)))
236 return 1;
237 break;
239 default:
240 break;
243 return -1;
246 /* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
247 value otherwise. */
249 cp_type_dwarf_attribute (const_tree type, int attr)
251 if (type == NULL_TREE)
252 return -1;
254 switch (attr)
256 case DW_AT_reference:
257 if (FUNC_OR_METHOD_TYPE_P (type)
258 && FUNCTION_REF_QUALIFIED (type)
259 && !FUNCTION_RVALUE_QUALIFIED (type))
260 return 1;
261 break;
263 case DW_AT_rvalue_reference:
264 if (FUNC_OR_METHOD_TYPE_P (type)
265 && FUNCTION_REF_QUALIFIED (type)
266 && FUNCTION_RVALUE_QUALIFIED (type))
267 return 1;
268 break;
270 default:
271 break;
274 return -1;
277 /* Return the unit size of TYPE without reusable tail padding. */
279 tree
280 cp_unit_size_without_reusable_padding (tree type)
282 if (CLASS_TYPE_P (type))
283 return CLASSTYPE_SIZE_UNIT (type);
284 return TYPE_SIZE_UNIT (type);
287 /* Returns type corresponding to FIELD's type when FIELD is a C++ base class
288 i.e., type without virtual base classes or tail padding. Returns
289 NULL_TREE otherwise. */
291 tree
292 cp_classtype_as_base (const_tree field)
294 if (DECL_FIELD_IS_BASE (field))
296 tree type = TREE_TYPE (field);
297 if (TYPE_LANG_SPECIFIC (type))
298 return CLASSTYPE_AS_BASE (type);
300 return NULL_TREE;
303 /* Stubs to keep c-opts.cc happy. */
304 void
305 push_file_scope (void)
309 void
310 pop_file_scope (void)
314 /* c-pragma.cc needs to query whether a decl has extern "C" linkage. */
315 bool
316 has_c_linkage (const_tree decl)
318 return DECL_EXTERN_C_P (decl);
321 /* Return true if stmt can fall through. Used by block_may_fallthru
322 default case. */
324 bool
325 cxx_block_may_fallthru (const_tree stmt)
327 switch (TREE_CODE (stmt))
329 case EXPR_STMT:
330 return block_may_fallthru (EXPR_STMT_EXPR (stmt));
332 case THROW_EXPR:
333 return false;
335 case IF_STMT:
336 if (IF_STMT_CONSTEXPR_P (stmt))
338 if (integer_nonzerop (IF_COND (stmt)))
339 return block_may_fallthru (THEN_CLAUSE (stmt));
340 if (integer_zerop (IF_COND (stmt)))
341 return block_may_fallthru (ELSE_CLAUSE (stmt));
343 if (block_may_fallthru (THEN_CLAUSE (stmt)))
344 return true;
345 return block_may_fallthru (ELSE_CLAUSE (stmt));
347 case CLEANUP_STMT:
348 /* Just handle the try/finally cases. */
349 if (!CLEANUP_EH_ONLY (stmt))
351 return (block_may_fallthru (CLEANUP_BODY (stmt))
352 && block_may_fallthru (CLEANUP_EXPR (stmt)));
354 return true;
356 default:
357 return c_block_may_fallthru (stmt);
361 /* Return the list of decls in the global namespace. */
363 tree
364 cp_get_global_decls ()
366 return NAMESPACE_LEVEL (global_namespace)->names;
369 /* Push DECL into the current (namespace) scope. */
371 tree
372 cp_pushdecl (tree decl)
374 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
375 return pushdecl (decl);
378 /* Get the global value binding of NAME. Called directly from
379 c-common.cc, not via a hook. */
381 tree
382 identifier_global_value (tree name)
384 return get_global_binding (name);
387 /* Similarly, but return struct/class/union NAME instead. */
389 tree
390 identifier_global_tag (tree name)
392 tree ret = lookup_qualified_name (global_namespace, name, LOOK_want::TYPE,
393 /*complain*/false);
394 if (ret == error_mark_node)
395 return NULL_TREE;
396 return ret;
399 /* Returns true if NAME refers to a built-in function or function-like
400 operator. */
402 bool
403 names_builtin_p (const char *name)
405 tree id = get_identifier (name);
406 if (tree binding = get_global_binding (id))
408 if (TREE_CODE (binding) == FUNCTION_DECL
409 && DECL_IS_UNDECLARED_BUILTIN (binding))
410 return true;
412 /* Handle the case when an overload for a built-in name exists. */
413 if (TREE_CODE (binding) != OVERLOAD)
414 return false;
416 for (ovl_iterator it (binding); it; ++it)
418 tree decl = *it;
419 if (DECL_IS_UNDECLARED_BUILTIN (decl))
420 return true;
424 /* Also detect common reserved C++ words that aren't strictly built-in
425 functions. */
426 switch (C_RID_CODE (id))
428 case RID_ADDRESSOF:
429 case RID_BUILTIN_CONVERTVECTOR:
430 case RID_BUILTIN_HAS_ATTRIBUTE:
431 case RID_BUILTIN_SHUFFLE:
432 case RID_BUILTIN_SHUFFLEVECTOR:
433 case RID_BUILTIN_LAUNDER:
434 case RID_BUILTIN_ASSOC_BARRIER:
435 case RID_BUILTIN_BIT_CAST:
436 case RID_OFFSETOF:
437 #define DEFTRAIT(TCC, CODE, NAME, ARITY) \
438 case RID_##CODE:
439 #include "cp-trait.def"
440 #undef DEFTRAIT
441 return true;
442 default:
443 break;
446 return false;
449 /* Register c++-specific dumps. */
451 void
452 cp_register_dumps (gcc::dump_manager *dumps)
454 class_dump_id = dumps->dump_register
455 (".class", "lang-class", "lang-class", DK_lang, OPTGROUP_NONE, false);
457 module_dump_id = dumps->dump_register
458 (".module", "lang-module", "lang-module", DK_lang, OPTGROUP_NONE, false);
460 raw_dump_id = dumps->dump_register
461 (".raw", "lang-raw", "lang-raw", DK_lang, OPTGROUP_NONE, false);
464 void
465 cp_common_init_ts (void)
467 /* With type. */
468 MARK_TS_TYPED (PTRMEM_CST);
469 MARK_TS_TYPED (LAMBDA_EXPR);
470 MARK_TS_TYPED (TYPE_ARGUMENT_PACK);
472 /* Random new trees. */
473 MARK_TS_COMMON (BASELINK);
474 MARK_TS_COMMON (OVERLOAD);
475 MARK_TS_COMMON (TEMPLATE_PARM_INDEX);
477 /* New decls. */
478 MARK_TS_DECL_COMMON (TEMPLATE_DECL);
479 MARK_TS_DECL_COMMON (WILDCARD_DECL);
481 MARK_TS_DECL_NON_COMMON (USING_DECL);
483 /* New Types. */
484 MARK_TS_TYPE_COMMON (UNBOUND_CLASS_TEMPLATE);
485 MARK_TS_TYPE_COMMON (TYPE_ARGUMENT_PACK);
486 MARK_TS_TYPE_COMMON (DEPENDENT_OPERATOR_TYPE);
488 MARK_TS_TYPE_NON_COMMON (DECLTYPE_TYPE);
489 MARK_TS_TYPE_NON_COMMON (TYPENAME_TYPE);
490 MARK_TS_TYPE_NON_COMMON (TYPEOF_TYPE);
491 MARK_TS_TYPE_NON_COMMON (TRAIT_TYPE);
492 MARK_TS_TYPE_NON_COMMON (BOUND_TEMPLATE_TEMPLATE_PARM);
493 MARK_TS_TYPE_NON_COMMON (TEMPLATE_TEMPLATE_PARM);
494 MARK_TS_TYPE_NON_COMMON (TEMPLATE_TYPE_PARM);
495 MARK_TS_TYPE_NON_COMMON (TYPE_PACK_EXPANSION);
497 /* Statements. */
498 MARK_TS_EXP (CLEANUP_STMT);
499 MARK_TS_EXP (EH_SPEC_BLOCK);
500 MARK_TS_EXP (HANDLER);
501 MARK_TS_EXP (IF_STMT);
502 MARK_TS_EXP (OMP_DEPOBJ);
503 MARK_TS_EXP (RANGE_FOR_STMT);
504 MARK_TS_EXP (TRY_BLOCK);
505 MARK_TS_EXP (USING_STMT);
507 /* Random expressions. */
508 MARK_TS_EXP (ADDRESSOF_EXPR);
509 MARK_TS_EXP (AGGR_INIT_EXPR);
510 MARK_TS_EXP (ALIGNOF_EXPR);
511 MARK_TS_EXP (ARROW_EXPR);
512 MARK_TS_EXP (AT_ENCODE_EXPR);
513 MARK_TS_EXP (BIT_CAST_EXPR);
514 MARK_TS_EXP (CAST_EXPR);
515 MARK_TS_EXP (CONST_CAST_EXPR);
516 MARK_TS_EXP (CTOR_INITIALIZER);
517 MARK_TS_EXP (DELETE_EXPR);
518 MARK_TS_EXP (DOTSTAR_EXPR);
519 MARK_TS_EXP (DYNAMIC_CAST_EXPR);
520 MARK_TS_EXP (EMPTY_CLASS_EXPR);
521 MARK_TS_EXP (EXPR_STMT);
522 MARK_TS_EXP (IMPLICIT_CONV_EXPR);
523 MARK_TS_EXP (MEMBER_REF);
524 MARK_TS_EXP (MODOP_EXPR);
525 MARK_TS_EXP (MUST_NOT_THROW_EXPR);
526 MARK_TS_EXP (NEW_EXPR);
527 MARK_TS_EXP (NOEXCEPT_EXPR);
528 MARK_TS_EXP (NON_DEPENDENT_EXPR);
529 MARK_TS_EXP (OFFSETOF_EXPR);
530 MARK_TS_EXP (OFFSET_REF);
531 MARK_TS_EXP (PSEUDO_DTOR_EXPR);
532 MARK_TS_EXP (REINTERPRET_CAST_EXPR);
533 MARK_TS_EXP (SCOPE_REF);
534 MARK_TS_EXP (STATIC_CAST_EXPR);
535 MARK_TS_EXP (STMT_EXPR);
536 MARK_TS_EXP (TAG_DEFN);
537 MARK_TS_EXP (TEMPLATE_ID_EXPR);
538 MARK_TS_EXP (THROW_EXPR);
539 MARK_TS_EXP (TRAIT_EXPR);
540 MARK_TS_EXP (TYPEID_EXPR);
541 MARK_TS_EXP (TYPE_EXPR);
542 MARK_TS_EXP (UNARY_PLUS_EXPR);
543 MARK_TS_EXP (VEC_DELETE_EXPR);
544 MARK_TS_EXP (VEC_INIT_EXPR);
545 MARK_TS_EXP (VEC_NEW_EXPR);
546 MARK_TS_EXP (SPACESHIP_EXPR);
548 /* Fold expressions. */
549 MARK_TS_EXP (BINARY_LEFT_FOLD_EXPR);
550 MARK_TS_EXP (BINARY_RIGHT_FOLD_EXPR);
551 MARK_TS_EXP (EXPR_PACK_EXPANSION);
552 MARK_TS_EXP (NONTYPE_ARGUMENT_PACK);
553 MARK_TS_EXP (UNARY_LEFT_FOLD_EXPR);
554 MARK_TS_EXP (UNARY_RIGHT_FOLD_EXPR);
556 /* Constraints. */
557 MARK_TS_EXP (CHECK_CONSTR);
558 MARK_TS_EXP (COMPOUND_REQ);
559 MARK_TS_EXP (CONJ_CONSTR);
560 MARK_TS_EXP (DISJ_CONSTR);
561 MARK_TS_EXP (ATOMIC_CONSTR);
562 MARK_TS_EXP (NESTED_REQ);
563 MARK_TS_EXP (REQUIRES_EXPR);
564 MARK_TS_EXP (SIMPLE_REQ);
565 MARK_TS_EXP (TYPE_REQ);
567 MARK_TS_EXP (CO_AWAIT_EXPR);
568 MARK_TS_EXP (CO_YIELD_EXPR);
569 MARK_TS_EXP (CO_RETURN_EXPR);
571 MARK_TS_EXP (ASSERTION_STMT);
572 MARK_TS_EXP (PRECONDITION_STMT);
573 MARK_TS_EXP (POSTCONDITION_STMT);
575 c_common_init_ts ();
578 /* Handle C++-specficic options here. Punt to c_common otherwise. */
580 bool
581 cp_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
582 int kind, location_t loc,
583 const struct cl_option_handlers *handlers)
585 if (handle_module_option (unsigned (scode), arg, value))
586 return true;
588 enum opt_code code = (enum opt_code) scode;
589 bool handled_p = true;
591 switch (code)
593 case OPT_fcontract_build_level_:
594 handle_OPT_fcontract_build_level_ (arg);
595 break;
597 case OPT_fcontract_assumption_mode_:
598 handle_OPT_fcontract_assumption_mode_ (arg);
599 break;
601 case OPT_fcontract_continuation_mode_:
602 handle_OPT_fcontract_continuation_mode_ (arg);
603 break;
605 case OPT_fcontract_role_:
606 handle_OPT_fcontract_role_ (arg);
607 break;
609 case OPT_fcontract_semantic_:
610 handle_OPT_fcontract_semantic_ (arg);
611 break;
613 default:
614 handled_p = false;
615 break;
617 if (handled_p)
618 return handled_p;
620 return c_common_handle_option (scode, arg, value, kind, loc, handlers);
623 #include "gt-cp-cp-objcp-common.h"