testsuite: g++.dg: debug: fix test filenames
[official-gcc.git] / gcc / cp / cp-lang.cc
blobfaab86d2d13bb79a94873f0f508ef0ad015279ec
1 /* Language-dependent hooks for C++.
2 Copyright (C) 2001-2024 Free Software Foundation, Inc.
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 #define INCLUDE_MEMORY
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "stor-layout.h"
27 #include "langhooks.h"
28 #include "langhooks-def.h"
29 #include "cp-objcp-common.h"
31 enum c_language_kind c_language = clk_cxx;
32 static const char * cxx_dwarf_name (tree t, int verbosity);
33 static enum classify_record cp_classify_record (tree type);
34 static tree cp_eh_personality (void);
35 static tree get_template_innermost_arguments_folded (const_tree);
36 static tree get_template_argument_pack_elems_folded (const_tree);
37 static tree cxx_enum_underlying_base_type (const_tree);
38 static tree *cxx_omp_get_decl_init (tree);
39 static void cxx_omp_finish_decl_inits (void);
40 static const char *cp_get_sarif_source_language (const char *);
42 /* Lang hooks common to C++ and ObjC++ are declared in cp/cp-objcp-common.h;
43 consequently, there should be very few hooks below. */
45 #undef LANG_HOOKS_NAME
46 #define LANG_HOOKS_NAME "GNU C++"
47 #undef LANG_HOOKS_INIT
48 #define LANG_HOOKS_INIT cxx_init
49 #undef LANG_HOOKS_CLASSIFY_RECORD
50 #define LANG_HOOKS_CLASSIFY_RECORD cp_classify_record
51 #undef LANG_HOOKS_GENERIC_TYPE_P
52 #define LANG_HOOKS_GENERIC_TYPE_P class_tmpl_impl_spec_p
54 #undef LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS
55 #define LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS \
56 get_primary_template_innermost_parameters
57 #undef LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
58 #define LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS \
59 get_template_innermost_arguments_folded
60 #undef LANG_HOOKS_FUNCTION_PARAMETER_PACK_P
61 #define LANG_HOOKS_FUNCTION_PARAMETER_PACK_P \
62 function_parameter_pack_p
63 #undef LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS
64 #define LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS \
65 get_template_argument_pack_elems_folded
66 #undef LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P
67 #define LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P \
68 template_template_parameter_p
69 #undef LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P
70 #define LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P \
71 function_parameter_expanded_from_pack_p
72 #undef LANG_HOOKS_GET_GENERIC_FUNCTION_DECL
73 #define LANG_HOOKS_GET_GENERIC_FUNCTION_DECL get_function_template_decl
74 #undef LANG_HOOKS_DWARF_NAME
75 #define LANG_HOOKS_DWARF_NAME cxx_dwarf_name
76 #undef LANG_HOOKS_INIT_TS
77 #define LANG_HOOKS_INIT_TS cp_common_init_ts
78 #undef LANG_HOOKS_EH_PERSONALITY
79 #define LANG_HOOKS_EH_PERSONALITY cp_eh_personality
80 #undef LANG_HOOKS_EH_RUNTIME_TYPE
81 #define LANG_HOOKS_EH_RUNTIME_TYPE build_eh_type_type
82 #undef LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE
83 #define LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE cxx_enum_underlying_base_type
84 #undef LANG_HOOKS_PREPROCESS_MAIN_FILE
85 #define LANG_HOOKS_PREPROCESS_MAIN_FILE module_begin_main_file
86 #undef LANG_HOOKS_PREPROCESS_OPTIONS
87 #define LANG_HOOKS_PREPROCESS_OPTIONS module_preprocess_options
88 #undef LANG_HOOKS_PREPROCESS_TOKEN
89 #define LANG_HOOKS_PREPROCESS_TOKEN module_token_pre
91 #if CHECKING_P
92 #undef LANG_HOOKS_RUN_LANG_SELFTESTS
93 #define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_cp_tests
94 #endif /* #if CHECKING_P */
96 #undef LANG_HOOKS_GET_SUBSTRING_LOCATION
97 #define LANG_HOOKS_GET_SUBSTRING_LOCATION c_get_substring_location
99 #undef LANG_HOOKS_OMP_GET_DECL_INIT
100 #define LANG_HOOKS_OMP_GET_DECL_INIT cxx_omp_get_decl_init
102 #undef LANG_HOOKS_OMP_FINISH_DECL_INITS
103 #define LANG_HOOKS_OMP_FINISH_DECL_INITS cxx_omp_finish_decl_inits
105 #undef LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE
106 #define LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE cp_get_sarif_source_language
108 /* Each front end provides its own lang hook initializer. */
109 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
111 /* Lang hook routines common to C++ and ObjC++ appear in cp/cp-objcp-common.cc;
112 there should be very few routines below. */
114 /* The following function does something real, but only in Objective-C++. */
116 tree
117 objcp_tsubst_expr (tree /*t*/, tree /*args*/, tsubst_flags_t /*complain*/,
118 tree /*in_decl*/)
120 return NULL_TREE;
123 /* Implement c-family hook to add language-specific features
124 for __has_{feature,extension}. */
126 void
127 c_family_register_lang_features ()
129 cp_register_features ();
132 static const char *
133 cxx_dwarf_name (tree t, int verbosity)
135 gcc_assert (DECL_P (t));
137 if (DECL_NAME (t) && IDENTIFIER_ANON_P (DECL_NAME (t)))
138 return NULL;
139 if (verbosity >= 2)
140 return decl_as_dwarf_string (t,
141 TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME
142 | TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
144 return lang_decl_dwarf_name (t, verbosity, false);
147 static enum classify_record
148 cp_classify_record (tree type)
150 if (TYPE_LANG_SPECIFIC (type)
151 && CLASSTYPE_DECLARED_CLASS (type))
152 return RECORD_IS_CLASS;
154 return RECORD_IS_STRUCT;
157 static GTY(()) tree cp_eh_personality_decl;
159 static tree
160 cp_eh_personality (void)
162 if (!cp_eh_personality_decl)
163 cp_eh_personality_decl = build_personality_function ("gxx");
165 return cp_eh_personality_decl;
168 /* This is a subroutine of fold_cplus_constants. It returns TRUE if T
169 is a C++ specific constant that needs to be folded further before
170 being passed to the debug info emitter. */
172 static bool
173 template_arg_needs_folding (const_tree t)
175 /* For now only PTRMEM_CST nodes are to be folded further. */
176 if (TREE_CODE (t) == PTRMEM_CST)
177 return true;
178 return false;
181 /* Fold the elements of the TREE_VEC C which are C++ specific nodes
182 that would need folding so that they can be processed by the debug
183 info emitter. This is a subroutine of
184 get_template_innermost_arguments_folded and
185 get_template_argument_pack_elems_folded. */
187 static tree
188 fold_cplus_constants (const_tree c)
190 tree folded_elems, elems = CONST_CAST_TREE (c);
191 int vec_len, i;
193 if (elems == NULL_TREE || elems == error_mark_node)
194 return elems;
196 vec_len = TREE_VEC_LENGTH (elems);
198 /* First check if there is at least one element that needs
199 folding. If there is none, we just return ELEMS. Otherwise create
200 and return a new tree vector that contains the folded versions of
201 ELEMS. This is to avoid allocating memory if we don't need
202 to. */
203 for (i = 0; i < vec_len; ++i)
205 if (template_arg_needs_folding (TREE_VEC_ELT (elems, i)))
206 break;
208 if (i == vec_len)
209 return elems;
211 folded_elems = make_tree_vec (vec_len);
212 for (i = 0; i < vec_len; ++i)
214 tree elem = TREE_VEC_ELT (elems, i);
215 TREE_VEC_ELT (folded_elems, i) =
216 (elem && !TYPE_P (elem)) ? cplus_expand_constant (elem) : elem;
219 return folded_elems;
222 /* The C++ implementation of the LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
223 hook. It returns the innermost template arguments of type T, and
224 makes sure those arguments are folded enough for the debug info
225 emitter. */
227 static tree
228 get_template_innermost_arguments_folded (const_tree t)
230 return fold_cplus_constants (get_template_innermost_arguments (t));
233 static tree
234 get_template_argument_pack_elems_folded (const_tree t)
236 return fold_cplus_constants (get_template_argument_pack_elems (t));
239 /* The C++ version of the enum_underlying_base_type langhook.
240 See also cp/semantics.cc (finish_underlying_type). */
242 static
243 tree cxx_enum_underlying_base_type (const_tree type)
245 tree underlying_type = ENUM_UNDERLYING_TYPE (type);
247 if (! ENUM_FIXED_UNDERLYING_TYPE_P (type))
248 underlying_type
249 = c_common_type_for_mode (TYPE_MODE (underlying_type),
250 TYPE_UNSIGNED (underlying_type));
252 return underlying_type;
255 /* The C++ version of the omp_get_decl_init langhook returns the static
256 initializer for a variable declaration if present, otherwise it
257 tries to find and return the dynamic initializer. If not present,
258 it returns NULL. */
260 static tree *
261 cxx_omp_get_decl_init (tree decl)
263 if (DECL_INITIAL (decl))
264 return &DECL_INITIAL (decl);
266 return hash_map_safe_get (dynamic_initializers, decl);
269 /* The C++ version of the omp_finish_decl_inits langhook allows GC to
270 reclaim the memory used by the hash-map used to hold dynamic initializer
271 information. */
273 static void
274 cxx_omp_finish_decl_inits (void)
276 dynamic_initializers = NULL;
279 /* Get a value for the SARIF v2.1.0 "artifact.sourceLanguage" property,
280 based on the list in SARIF v2.1.0 Appendix J. */
282 static const char *
283 cp_get_sarif_source_language (const char *)
285 return "cplusplus";
288 #if CHECKING_P
290 namespace selftest {
292 /* Implementation of LANG_HOOKS_RUN_LANG_SELFTESTS for the C++ frontend. */
294 void
295 run_cp_tests (void)
297 /* Run selftests shared within the C family. */
298 c_family_tests ();
300 /* Additional C++-specific tests. */
301 cp_pt_cc_tests ();
302 cp_tree_cc_tests ();
305 } // namespace selftest
307 #endif /* #if CHECKING_P */
310 #include "gt-cp-cp-lang.h"
311 #include "gtype-cp.h"