Line data Source code
1 : /* Language-dependent hooks for C++.
2 : Copyright (C) 2001-2026 Free Software Foundation, Inc.
3 : Contributed by Alexandre Oliva <aoliva@redhat.com>
4 :
5 : This file is part of GCC.
6 :
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.
11 :
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.
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 "stor-layout.h"
26 : #include "langhooks.h"
27 : #include "langhooks-def.h"
28 : #include "cp-objcp-common.h"
29 :
30 : enum c_language_kind c_language = clk_cxx;
31 : static const char * cxx_dwarf_name (tree t, int verbosity);
32 : static enum classify_record cp_classify_record (tree type);
33 : static tree cp_eh_personality (void);
34 : static tree get_template_innermost_arguments_folded (const_tree);
35 : static tree get_template_argument_pack_elems_folded (const_tree);
36 : static tree cxx_enum_underlying_base_type (const_tree);
37 : static tree *cxx_omp_get_decl_init (tree);
38 : static void cxx_omp_finish_decl_inits (void);
39 : static const char *cp_get_sarif_source_language (const char *);
40 :
41 : /* Lang hooks common to C++ and ObjC++ are declared in cp/cp-objcp-common.h;
42 : consequently, there should be very few hooks below. */
43 :
44 : #undef LANG_HOOKS_NAME
45 : #define LANG_HOOKS_NAME "GNU C++"
46 : #undef LANG_HOOKS_INIT
47 : #define LANG_HOOKS_INIT cxx_init
48 : #undef LANG_HOOKS_CLASSIFY_RECORD
49 : #define LANG_HOOKS_CLASSIFY_RECORD cp_classify_record
50 : #undef LANG_HOOKS_GENERIC_TYPE_P
51 : #define LANG_HOOKS_GENERIC_TYPE_P class_tmpl_impl_spec_p
52 :
53 : #undef LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS
54 : #define LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS \
55 : get_primary_template_innermost_parameters
56 : #undef LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
57 : #define LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS \
58 : get_template_innermost_arguments_folded
59 : #undef LANG_HOOKS_FUNCTION_PARAMETER_PACK_P
60 : #define LANG_HOOKS_FUNCTION_PARAMETER_PACK_P \
61 : function_parameter_pack_p
62 : #undef LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS
63 : #define LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS \
64 : get_template_argument_pack_elems_folded
65 : #undef LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P
66 : #define LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P \
67 : template_template_parameter_p
68 : #undef LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P
69 : #define LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P \
70 : function_parameter_expanded_from_pack_p
71 : #undef LANG_HOOKS_GET_GENERIC_FUNCTION_DECL
72 : #define LANG_HOOKS_GET_GENERIC_FUNCTION_DECL get_function_template_decl
73 : #undef LANG_HOOKS_DWARF_NAME
74 : #define LANG_HOOKS_DWARF_NAME cxx_dwarf_name
75 : #undef LANG_HOOKS_INIT_TS
76 : #define LANG_HOOKS_INIT_TS cp_common_init_ts
77 : #undef LANG_HOOKS_EH_PERSONALITY
78 : #define LANG_HOOKS_EH_PERSONALITY cp_eh_personality
79 : #undef LANG_HOOKS_EH_RUNTIME_TYPE
80 : #define LANG_HOOKS_EH_RUNTIME_TYPE build_eh_type_type
81 : #undef LANG_HOOKS_EXCEPTION_MATCHES_TYPE_P
82 : #define LANG_HOOKS_EXCEPTION_MATCHES_TYPE_P handler_match_for_exception_type
83 : #undef LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE
84 : #define LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE cxx_enum_underlying_base_type
85 : #undef LANG_HOOKS_PREPROCESS_MAIN_FILE
86 : #define LANG_HOOKS_PREPROCESS_MAIN_FILE module_begin_main_file
87 : #undef LANG_HOOKS_PREPROCESS_OPTIONS
88 : #define LANG_HOOKS_PREPROCESS_OPTIONS module_preprocess_options
89 : #undef LANG_HOOKS_PREPROCESS_TOKEN
90 : #define LANG_HOOKS_PREPROCESS_TOKEN module_token_pre
91 :
92 : #if CHECKING_P
93 : #undef LANG_HOOKS_RUN_LANG_SELFTESTS
94 : #define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_cp_tests
95 : #endif /* #if CHECKING_P */
96 :
97 : #undef LANG_HOOKS_GET_SUBSTRING_LOCATION
98 : #define LANG_HOOKS_GET_SUBSTRING_LOCATION c_get_substring_location
99 :
100 : #undef LANG_HOOKS_OMP_GET_DECL_INIT
101 : #define LANG_HOOKS_OMP_GET_DECL_INIT cxx_omp_get_decl_init
102 :
103 : #undef LANG_HOOKS_OMP_FINISH_DECL_INITS
104 : #define LANG_HOOKS_OMP_FINISH_DECL_INITS cxx_omp_finish_decl_inits
105 :
106 : #undef LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE
107 : #define LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE cp_get_sarif_source_language
108 :
109 : /* Each front end provides its own lang hook initializer. */
110 : struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
111 :
112 : /* Lang hook routines common to C++ and ObjC++ appear in cp/cp-objcp-common.cc;
113 : there should be very few routines below. */
114 :
115 : /* The following function does something real, but only in Objective-C++. */
116 :
117 : tree
118 0 : objcp_tsubst_expr (tree /*t*/, tree /*args*/, tsubst_flags_t /*complain*/,
119 : tree /*in_decl*/)
120 : {
121 0 : return NULL_TREE;
122 : }
123 :
124 : /* Implement c-family hook to add language-specific features
125 : for __has_{feature,extension}. */
126 :
127 : void
128 22964 : c_family_register_lang_features ()
129 : {
130 22964 : cp_register_features ();
131 22964 : }
132 :
133 : static const char *
134 248100416 : cxx_dwarf_name (tree t, int verbosity)
135 : {
136 248100416 : gcc_assert (DECL_P (t));
137 :
138 248100416 : if (DECL_NAME (t) && IDENTIFIER_ANON_P (DECL_NAME (t)))
139 : return NULL;
140 246151156 : if (verbosity >= 2)
141 49999020 : return decl_as_dwarf_string (t,
142 : TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME
143 49999020 : | TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
144 :
145 196152136 : return lang_decl_dwarf_name (t, verbosity, false);
146 : }
147 :
148 : static enum classify_record
149 49776906 : cp_classify_record (tree type)
150 : {
151 49776906 : if (TYPE_LANG_SPECIFIC (type)
152 49776906 : && CLASSTYPE_DECLARED_CLASS (type))
153 6003553 : return RECORD_IS_CLASS;
154 :
155 : return RECORD_IS_STRUCT;
156 : }
157 :
158 : static GTY(()) tree cp_eh_personality_decl;
159 :
160 : static tree
161 894301 : cp_eh_personality (void)
162 : {
163 894301 : if (!cp_eh_personality_decl)
164 29210 : cp_eh_personality_decl = build_personality_function ("gxx");
165 :
166 894301 : return cp_eh_personality_decl;
167 : }
168 :
169 : /* This is a subroutine of fold_cplus_constants. It returns TRUE if T
170 : is a C++ specific constant that needs to be folded further before
171 : being passed to the debug info emitter. */
172 :
173 : static bool
174 72839940 : template_arg_needs_folding (const_tree t)
175 : {
176 : /* For now only PTRMEM_CST nodes are to be folded further. */
177 0 : if (TREE_CODE (t) == PTRMEM_CST)
178 0 : return true;
179 : return false;
180 : }
181 :
182 : /* Fold the elements of the TREE_VEC C which are C++ specific nodes
183 : that would need folding so that they can be processed by the debug
184 : info emitter. This is a subroutine of
185 : get_template_innermost_arguments_folded and
186 : get_template_argument_pack_elems_folded. */
187 :
188 : static tree
189 102447729 : fold_cplus_constants (const_tree c)
190 : {
191 102447729 : tree folded_elems, elems = const_cast<tree> (c);
192 102447729 : int vec_len, i;
193 :
194 102447729 : if (elems == NULL_TREE || elems == error_mark_node)
195 : return elems;
196 :
197 45342932 : vec_len = TREE_VEC_LENGTH (elems);
198 :
199 : /* First check if there is at least one element that needs
200 : folding. If there is none, we just return ELEMS. Otherwise create
201 : and return a new tree vector that contains the folded versions of
202 : ELEMS. This is to avoid allocating memory if we don't need
203 : to. */
204 118182524 : for (i = 0; i < vec_len; ++i)
205 : {
206 72839940 : if (template_arg_needs_folding (TREE_VEC_ELT (elems, i)))
207 : break;
208 : }
209 45342932 : if (i == vec_len)
210 : return elems;
211 :
212 348 : folded_elems = make_tree_vec (vec_len);
213 1642 : for (i = 0; i < vec_len; ++i)
214 : {
215 946 : tree elem = TREE_VEC_ELT (elems, i);
216 946 : TREE_VEC_ELT (folded_elems, i) =
217 946 : (elem && !TYPE_P (elem)) ? cplus_expand_constant (elem) : elem;
218 :
219 : }
220 : return folded_elems;
221 : }
222 :
223 : /* The C++ implementation of the LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
224 : hook. It returns the innermost template arguments of type T, and
225 : makes sure those arguments are folded enough for the debug info
226 : emitter. */
227 :
228 : static tree
229 40115571 : get_template_innermost_arguments_folded (const_tree t)
230 : {
231 40115571 : return fold_cplus_constants (get_template_innermost_arguments (t));
232 : }
233 :
234 : static tree
235 62332158 : get_template_argument_pack_elems_folded (const_tree t)
236 : {
237 62332158 : return fold_cplus_constants (get_template_argument_pack_elems (t));
238 : }
239 :
240 : /* The C++ version of the enum_underlying_base_type langhook.
241 : See also cp/semantics.cc (finish_underlying_type). */
242 :
243 : static
244 1780936 : tree cxx_enum_underlying_base_type (const_tree type)
245 : {
246 1780936 : tree underlying_type = ENUM_UNDERLYING_TYPE (type);
247 :
248 1780936 : if (! ENUM_FIXED_UNDERLYING_TYPE_P (type))
249 1331921 : underlying_type
250 1331921 : = c_common_type_for_mode (TYPE_MODE (underlying_type),
251 1331921 : TYPE_UNSIGNED (underlying_type));
252 :
253 1780936 : return underlying_type;
254 : }
255 :
256 : /* The C++ version of the omp_get_decl_init langhook returns the static
257 : initializer for a variable declaration if present, otherwise it
258 : tries to find and return the dynamic initializer. If not present,
259 : it returns NULL. */
260 :
261 : static tree *
262 114583 : cxx_omp_get_decl_init (tree decl)
263 : {
264 114583 : if (DECL_INITIAL (decl))
265 106046 : return &DECL_INITIAL (decl);
266 :
267 8537 : return hash_map_safe_get (dynamic_initializers, decl);
268 : }
269 :
270 : /* The C++ version of the omp_finish_decl_inits langhook allows GC to
271 : reclaim the memory used by the hash-map used to hold dynamic initializer
272 : information. */
273 :
274 : static void
275 4137 : cxx_omp_finish_decl_inits (void)
276 : {
277 4137 : dynamic_initializers = NULL;
278 4137 : }
279 :
280 : /* Get a value for the SARIF v2.1.0 "artifact.sourceLanguage" property,
281 : based on the list in SARIF v2.1.0 Appendix J. */
282 :
283 : static const char *
284 75 : cp_get_sarif_source_language (const char *)
285 : {
286 75 : return "cplusplus";
287 : }
288 :
289 : #if CHECKING_P
290 :
291 : namespace selftest {
292 :
293 : /* Implementation of LANG_HOOKS_RUN_LANG_SELFTESTS for the C++ frontend. */
294 :
295 : void
296 1 : run_cp_tests (void)
297 : {
298 : /* Run selftests shared within the C family. */
299 1 : c_family_tests ();
300 :
301 : /* Additional C++-specific tests. */
302 1 : cp_pt_cc_tests ();
303 1 : cp_tree_cc_tests ();
304 1 : }
305 :
306 : } // namespace selftest
307 :
308 : #endif /* #if CHECKING_P */
309 :
310 :
311 : #include "gt-cp-cp-lang.h"
312 : #include "gtype-cp.h"
|