1 /* Some code common to C++ and ObjC++ front ends.
2 Copyright (C) 2004-2020 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
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
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/>. */
23 #include "coretypes.h"
25 #include "cp-objcp-common.h"
27 #include "stringpool.h"
29 /* Special routine to get the alias set for C++. */
32 cxx_get_alias_set (tree t
)
34 if (IS_FAKE_BASE_TYPE (t
))
35 /* The base variant of a type must be in the same alias set as the
37 return get_alias_set (TYPE_CONTEXT (t
));
39 /* Punt on PMFs until we canonicalize functions properly. */
40 if (TYPE_PTRMEMFUNC_P (t
)
41 || (INDIRECT_TYPE_P (t
)
42 && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))))
45 return c_common_get_alias_set (t
);
48 /* Called from check_global_declaration. */
51 cxx_warn_unused_global_decl (const_tree decl
)
53 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
))
55 if (DECL_IN_SYSTEM_HEADER (decl
))
61 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */
63 cp_tree_size (enum tree_code code
)
65 gcc_checking_assert (code
>= NUM_TREE_CODES
);
68 case PTRMEM_CST
: return sizeof (ptrmem_cst
);
69 case BASELINK
: return sizeof (tree_baselink
);
70 case TEMPLATE_PARM_INDEX
: return sizeof (template_parm_index
);
71 case DEFERRED_PARSE
: return sizeof (tree_deferred_parse
);
72 case DEFERRED_NOEXCEPT
: return sizeof (tree_deferred_noexcept
);
73 case OVERLOAD
: return sizeof (tree_overload
);
74 case STATIC_ASSERT
: return sizeof (tree_static_assert
);
75 case TYPE_ARGUMENT_PACK
:
76 case TYPE_PACK_EXPANSION
: return sizeof (tree_type_non_common
);
77 case NONTYPE_ARGUMENT_PACK
:
78 case EXPR_PACK_EXPANSION
: return sizeof (tree_exp
);
79 case ARGUMENT_PACK_SELECT
: return sizeof (tree_argument_pack_select
);
80 case TRAIT_EXPR
: return sizeof (tree_trait_expr
);
81 case LAMBDA_EXPR
: return sizeof (tree_lambda_expr
);
82 case TEMPLATE_INFO
: return sizeof (tree_template_info
);
83 case CONSTRAINT_INFO
: return sizeof (tree_constraint_info
);
84 case USERDEF_LITERAL
: return sizeof (tree_userdef_literal
);
85 case TEMPLATE_DECL
: return sizeof (tree_template_decl
);
87 switch (TREE_CODE_CLASS (code
))
89 case tcc_declaration
: return sizeof (tree_decl_non_common
);
90 case tcc_type
: return sizeof (tree_type_non_common
);
91 default: gcc_unreachable ();
97 /* Returns true if T is a variably modified type, in the sense of C99.
98 FN is as passed to variably_modified_p.
99 This routine needs only check cases that cannot be handled by the
100 language-independent logic in tree.c. */
103 cp_var_mod_type_p (tree type
, tree fn
)
105 /* If TYPE is a pointer-to-member, it is variably modified if either
106 the class or the member are variably modified. */
107 if (TYPE_PTRMEM_P (type
))
108 return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type
), fn
)
109 || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type
),
112 /* All other types are not variably modified. */
116 /* This compares two types for equivalence ("compatible" in C-based languages).
117 This routine should only return 1 if it is sure. It should not be used
118 in contexts where erroneously returning 0 causes problems. */
121 cxx_types_compatible_p (tree x
, tree y
)
123 return same_type_ignoring_top_level_qualifiers_p (x
, y
);
126 static GTY((cache
)) type_tree_cache_map
*debug_type_map
;
128 /* Return a type to use in the debug info instead of TYPE, or NULL_TREE to
132 cp_get_debug_type (const_tree type
)
134 tree dtype
= NULL_TREE
;
136 if (TYPE_PTRMEMFUNC_P (type
) && !typedef_variant_p (type
))
137 dtype
= build_offset_type (TYPE_PTRMEMFUNC_OBJECT_TYPE (type
),
138 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type
)));
140 /* We cannot simply return the debug type here because the function uses
141 the type canonicalization hashtable, which is GC-ed, so its behavior
142 depends on the actual collection points. Since we are building these
143 types on the fly for the debug info only, they would not be attached
144 to any GC root and always be swept, so we would make the contents of
145 the debug info depend on the collection points. */
148 tree ktype
= CONST_CAST_TREE (type
);
149 if (tree
*slot
= hash_map_safe_get (debug_type_map
, ktype
))
151 hash_map_safe_put
<hm_ggc
> (debug_type_map
, ktype
, dtype
);
157 /* Return -1 if dwarf ATTR shouldn't be added for DECL, or the attribute
160 cp_decl_dwarf_attribute (const_tree decl
, int attr
)
162 if (decl
== NULL_TREE
)
168 if (TREE_CODE (decl
) == FUNCTION_DECL
169 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl
))
170 && DECL_NONCONVERTING_P (decl
))
175 if (TREE_CODE (decl
) == FUNCTION_DECL
176 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl
))
177 && DECL_DELETED_FN (decl
))
181 case DW_AT_defaulted
:
182 if (TREE_CODE (decl
) == FUNCTION_DECL
183 && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl
))
184 && DECL_DEFAULTED_FN (decl
))
186 if (DECL_DEFAULTED_IN_CLASS_P (decl
))
187 return DW_DEFAULTED_in_class
;
189 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (decl
))
190 return DW_DEFAULTED_out_of_class
;
194 case DW_AT_const_expr
:
195 if (VAR_OR_FUNCTION_DECL_P (decl
) && DECL_DECLARED_CONSTEXPR_P (decl
))
199 case DW_AT_reference
:
200 if (TREE_CODE (decl
) == FUNCTION_DECL
201 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
202 && FUNCTION_REF_QUALIFIED (TREE_TYPE (decl
))
203 && !FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl
)))
207 case DW_AT_rvalue_reference
:
208 if (TREE_CODE (decl
) == FUNCTION_DECL
209 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
210 && FUNCTION_REF_QUALIFIED (TREE_TYPE (decl
))
211 && FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (decl
)))
216 if (VAR_P (decl
) && DECL_INLINE_VAR_P (decl
))
218 if (DECL_VAR_DECLARED_INLINE_P (decl
))
219 return DW_INL_declared_inlined
;
221 return DW_INL_inlined
;
225 case DW_AT_export_symbols
:
226 if (TREE_CODE (decl
) == NAMESPACE_DECL
227 && (DECL_NAMESPACE_INLINE_P (decl
)
228 || (DECL_NAME (decl
) == NULL_TREE
&& dwarf_version
>= 5)))
239 /* Return -1 if dwarf ATTR shouldn't be added for TYPE, or the attribute
242 cp_type_dwarf_attribute (const_tree type
, int attr
)
244 if (type
== NULL_TREE
)
249 case DW_AT_reference
:
250 if (FUNC_OR_METHOD_TYPE_P (type
)
251 && FUNCTION_REF_QUALIFIED (type
)
252 && !FUNCTION_RVALUE_QUALIFIED (type
))
256 case DW_AT_rvalue_reference
:
257 if (FUNC_OR_METHOD_TYPE_P (type
)
258 && FUNCTION_REF_QUALIFIED (type
)
259 && FUNCTION_RVALUE_QUALIFIED (type
))
270 /* Return the unit size of TYPE without reusable tail padding. */
273 cp_unit_size_without_reusable_padding (tree type
)
275 if (CLASS_TYPE_P (type
))
276 return CLASSTYPE_SIZE_UNIT (type
);
277 return TYPE_SIZE_UNIT (type
);
280 /* Stubs to keep c-opts.c happy. */
282 push_file_scope (void)
287 pop_file_scope (void)
291 /* c-pragma.c needs to query whether a decl has extern "C" linkage. */
293 has_c_linkage (const_tree decl
)
295 return DECL_EXTERN_C_P (decl
);
298 /* Return true if stmt can fall through. Used by block_may_fallthru
302 cxx_block_may_fallthru (const_tree stmt
)
304 switch (TREE_CODE (stmt
))
307 return block_may_fallthru (EXPR_STMT_EXPR (stmt
));
313 if (block_may_fallthru (THEN_CLAUSE (stmt
)))
315 return block_may_fallthru (ELSE_CLAUSE (stmt
));
318 return c_block_may_fallthru (stmt
);
322 /* Return the list of decls in the global namespace. */
325 cp_get_global_decls ()
327 return NAMESPACE_LEVEL (global_namespace
)->names
;
330 /* Push DECL into the current (namespace) scope. */
333 cp_pushdecl (tree decl
)
335 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
336 return pushdecl (decl
);
339 /* Get the global value binding of NAME. Called directly from
340 c-common.c, not via a hook. */
343 identifier_global_value (tree name
)
345 return get_global_binding (name
);
348 /* Similarly, but return struct/class/union NAME instead. */
351 identifier_global_tag (tree name
)
353 tree ret
= lookup_qualified_name (global_namespace
, name
, LOOK_want::TYPE
,
355 if (ret
== error_mark_node
)
360 /* Returns true if NAME refers to a built-in function or function-like
364 names_builtin_p (const char *name
)
366 tree id
= get_identifier (name
);
367 if (tree binding
= get_global_binding (id
))
369 if (TREE_CODE (binding
) == FUNCTION_DECL
&& DECL_IS_BUILTIN (binding
))
372 /* Handle the case when an overload for a built-in name exists. */
373 if (TREE_CODE (binding
) != OVERLOAD
)
376 for (ovl_iterator
it (binding
); it
; ++it
)
379 if (DECL_IS_BUILTIN (decl
))
384 /* Also detect common reserved C++ words that aren't strictly built-in
386 switch (C_RID_CODE (id
))
389 case RID_BUILTIN_CONVERTVECTOR
:
390 case RID_BUILTIN_HAS_ATTRIBUTE
:
391 case RID_BUILTIN_SHUFFLE
:
392 case RID_BUILTIN_LAUNDER
:
394 case RID_HAS_NOTHROW_ASSIGN
:
395 case RID_HAS_NOTHROW_CONSTRUCTOR
:
396 case RID_HAS_NOTHROW_COPY
:
397 case RID_HAS_TRIVIAL_ASSIGN
:
398 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
399 case RID_HAS_TRIVIAL_COPY
:
400 case RID_HAS_TRIVIAL_DESTRUCTOR
:
401 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
402 case RID_HAS_VIRTUAL_DESTRUCTOR
:
403 case RID_IS_ABSTRACT
:
404 case RID_IS_AGGREGATE
:
410 case RID_IS_LITERAL_TYPE
:
412 case RID_IS_POLYMORPHIC
:
414 case RID_IS_STD_LAYOUT
:
416 case RID_IS_TRIVIALLY_ASSIGNABLE
:
417 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
418 case RID_IS_TRIVIALLY_COPYABLE
:
420 case RID_IS_ASSIGNABLE
:
421 case RID_IS_CONSTRUCTIBLE
:
422 case RID_UNDERLYING_TYPE
:
431 /* Register c++-specific dumps. */
434 cp_register_dumps (gcc::dump_manager
*dumps
)
436 class_dump_id
= dumps
->dump_register
437 (".class", "lang-class", "lang-class", DK_lang
, OPTGROUP_NONE
, false);
439 raw_dump_id
= dumps
->dump_register
440 (".raw", "lang-raw", "lang-raw", DK_lang
, OPTGROUP_NONE
, false);
444 cp_common_init_ts (void)
447 MARK_TS_TYPED (PTRMEM_CST
);
448 MARK_TS_TYPED (LAMBDA_EXPR
);
449 MARK_TS_TYPED (TYPE_ARGUMENT_PACK
);
451 /* Random new trees. */
452 MARK_TS_COMMON (BASELINK
);
453 MARK_TS_COMMON (DECLTYPE_TYPE
);
454 MARK_TS_COMMON (OVERLOAD
);
455 MARK_TS_COMMON (TEMPLATE_PARM_INDEX
);
456 MARK_TS_COMMON (TYPENAME_TYPE
);
457 MARK_TS_COMMON (TYPEOF_TYPE
);
458 MARK_TS_COMMON (UNBOUND_CLASS_TEMPLATE
);
459 MARK_TS_COMMON (UNDERLYING_TYPE
);
462 MARK_TS_DECL_COMMON (TEMPLATE_DECL
);
463 MARK_TS_DECL_COMMON (WILDCARD_DECL
);
464 MARK_TS_DECL_COMMON (CONCEPT_DECL
);
466 MARK_TS_DECL_NON_COMMON (USING_DECL
);
469 MARK_TS_TYPE_NON_COMMON (BOUND_TEMPLATE_TEMPLATE_PARM
);
470 MARK_TS_TYPE_NON_COMMON (TEMPLATE_TEMPLATE_PARM
);
471 MARK_TS_TYPE_NON_COMMON (TEMPLATE_TYPE_PARM
);
472 MARK_TS_TYPE_NON_COMMON (TYPE_ARGUMENT_PACK
);
473 MARK_TS_TYPE_NON_COMMON (TYPE_PACK_EXPANSION
);
476 MARK_TS_EXP (CLEANUP_STMT
);
477 MARK_TS_EXP (EH_SPEC_BLOCK
);
478 MARK_TS_EXP (HANDLER
);
479 MARK_TS_EXP (IF_STMT
);
480 MARK_TS_EXP (OMP_DEPOBJ
);
481 MARK_TS_EXP (RANGE_FOR_STMT
);
482 MARK_TS_EXP (TRY_BLOCK
);
483 MARK_TS_EXP (USING_STMT
);
485 /* Random expressions. */
486 MARK_TS_EXP (ADDRESSOF_EXPR
);
487 MARK_TS_EXP (AGGR_INIT_EXPR
);
488 MARK_TS_EXP (ALIGNOF_EXPR
);
489 MARK_TS_EXP (ARROW_EXPR
);
490 MARK_TS_EXP (AT_ENCODE_EXPR
);
491 MARK_TS_EXP (CAST_EXPR
);
492 MARK_TS_EXP (CONST_CAST_EXPR
);
493 MARK_TS_EXP (CTOR_INITIALIZER
);
494 MARK_TS_EXP (DELETE_EXPR
);
495 MARK_TS_EXP (DOTSTAR_EXPR
);
496 MARK_TS_EXP (DYNAMIC_CAST_EXPR
);
497 MARK_TS_EXP (EMPTY_CLASS_EXPR
);
498 MARK_TS_EXP (EXPR_STMT
);
499 MARK_TS_EXP (IMPLICIT_CONV_EXPR
);
500 MARK_TS_EXP (MEMBER_REF
);
501 MARK_TS_EXP (MODOP_EXPR
);
502 MARK_TS_EXP (MUST_NOT_THROW_EXPR
);
503 MARK_TS_EXP (NEW_EXPR
);
504 MARK_TS_EXP (NOEXCEPT_EXPR
);
505 MARK_TS_EXP (NON_DEPENDENT_EXPR
);
506 MARK_TS_EXP (OFFSETOF_EXPR
);
507 MARK_TS_EXP (OFFSET_REF
);
508 MARK_TS_EXP (PSEUDO_DTOR_EXPR
);
509 MARK_TS_EXP (REINTERPRET_CAST_EXPR
);
510 MARK_TS_EXP (SCOPE_REF
);
511 MARK_TS_EXP (STATIC_CAST_EXPR
);
512 MARK_TS_EXP (STMT_EXPR
);
513 MARK_TS_EXP (TAG_DEFN
);
514 MARK_TS_EXP (TEMPLATE_ID_EXPR
);
515 MARK_TS_EXP (THROW_EXPR
);
516 MARK_TS_EXP (TRAIT_EXPR
);
517 MARK_TS_EXP (TYPEID_EXPR
);
518 MARK_TS_EXP (TYPE_EXPR
);
519 MARK_TS_EXP (UNARY_PLUS_EXPR
);
520 MARK_TS_EXP (VEC_DELETE_EXPR
);
521 MARK_TS_EXP (VEC_INIT_EXPR
);
522 MARK_TS_EXP (VEC_NEW_EXPR
);
523 MARK_TS_EXP (SPACESHIP_EXPR
);
525 /* Fold expressions. */
526 MARK_TS_EXP (BINARY_LEFT_FOLD_EXPR
);
527 MARK_TS_EXP (BINARY_RIGHT_FOLD_EXPR
);
528 MARK_TS_EXP (EXPR_PACK_EXPANSION
);
529 MARK_TS_EXP (NONTYPE_ARGUMENT_PACK
);
530 MARK_TS_EXP (UNARY_LEFT_FOLD_EXPR
);
531 MARK_TS_EXP (UNARY_RIGHT_FOLD_EXPR
);
534 MARK_TS_EXP (CHECK_CONSTR
);
535 MARK_TS_EXP (COMPOUND_REQ
);
536 MARK_TS_EXP (CONJ_CONSTR
);
537 MARK_TS_EXP (DISJ_CONSTR
);
538 MARK_TS_EXP (ATOMIC_CONSTR
);
539 MARK_TS_EXP (NESTED_REQ
);
540 MARK_TS_EXP (REQUIRES_EXPR
);
541 MARK_TS_EXP (SIMPLE_REQ
);
542 MARK_TS_EXP (TYPE_REQ
);
544 MARK_TS_EXP (CO_AWAIT_EXPR
);
545 MARK_TS_EXP (CO_YIELD_EXPR
);
546 MARK_TS_EXP (CO_RETURN_EXPR
);
551 #include "gt-cp-cp-objcp-common.h"