aarch64: Use dup and zip1 for interleaving elements in vector initializer.
[official-gcc.git] / gcc / cp / cp-lang.cc
bloba3f29eda0d6996832b8ad0b0680cf6b608d44d1c
1 /* Language-dependent hooks for C++.
2 Copyright (C) 2001-2022 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 #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"
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 *);
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. */
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
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_ENUM_UNDERLYING_BASE_TYPE
82 #define LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE cxx_enum_underlying_base_type
83 #undef LANG_HOOKS_PREPROCESS_MAIN_FILE
84 #define LANG_HOOKS_PREPROCESS_MAIN_FILE module_begin_main_file
85 #undef LANG_HOOKS_PREPROCESS_OPTIONS
86 #define LANG_HOOKS_PREPROCESS_OPTIONS module_preprocess_options
87 #undef LANG_HOOKS_PREPROCESS_TOKEN
88 #define LANG_HOOKS_PREPROCESS_TOKEN module_token_pre
90 #if CHECKING_P
91 #undef LANG_HOOKS_RUN_LANG_SELFTESTS
92 #define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_cp_tests
93 #endif /* #if CHECKING_P */
95 #undef LANG_HOOKS_GET_SUBSTRING_LOCATION
96 #define LANG_HOOKS_GET_SUBSTRING_LOCATION c_get_substring_location
98 #undef LANG_HOOKS_OMP_GET_DECL_INIT
99 #define LANG_HOOKS_OMP_GET_DECL_INIT cxx_omp_get_decl_init
101 #undef LANG_HOOKS_OMP_FINISH_DECL_INITS
102 #define LANG_HOOKS_OMP_FINISH_DECL_INITS cxx_omp_finish_decl_inits
104 #undef LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE
105 #define LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE cp_get_sarif_source_language
107 /* Each front end provides its own lang hook initializer. */
108 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
110 /* Lang hook routines common to C++ and ObjC++ appear in cp/cp-objcp-common.cc;
111 there should be very few routines below. */
113 /* The following function does something real, but only in Objective-C++. */
115 tree
116 objcp_tsubst_copy_and_build (tree /*t*/,
117 tree /*args*/,
118 tsubst_flags_t /*complain*/,
119 tree /*in_decl*/)
121 return NULL_TREE;
124 static const char *
125 cxx_dwarf_name (tree t, int verbosity)
127 gcc_assert (DECL_P (t));
129 if (DECL_NAME (t) && IDENTIFIER_ANON_P (DECL_NAME (t)))
130 return NULL;
131 if (verbosity >= 2)
132 return decl_as_dwarf_string (t,
133 TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME
134 | TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
136 return lang_decl_dwarf_name (t, verbosity, false);
139 static enum classify_record
140 cp_classify_record (tree type)
142 if (TYPE_LANG_SPECIFIC (type)
143 && CLASSTYPE_DECLARED_CLASS (type))
144 return RECORD_IS_CLASS;
146 return RECORD_IS_STRUCT;
149 static GTY(()) tree cp_eh_personality_decl;
151 static tree
152 cp_eh_personality (void)
154 if (!cp_eh_personality_decl)
155 cp_eh_personality_decl = build_personality_function ("gxx");
157 return cp_eh_personality_decl;
160 /* This is a subroutine of fold_cplus_constants. It returns TRUE if T
161 is a C++ specific constant that needs to be folded further before
162 being passed to the debug info emitter. */
164 static bool
165 template_arg_needs_folding (const_tree t)
167 /* For now only PTRMEM_CST nodes are to be folded further. */
168 if (TREE_CODE (t) == PTRMEM_CST)
169 return true;
170 return false;
173 /* Fold the elements of the TREE_VEC C which are C++ specific nodes
174 that would need folding so that they can be processed by the debug
175 info emitter. This is a subroutine of
176 get_template_innermost_arguments_folded and
177 get_template_argument_pack_elems_folded. */
179 static tree
180 fold_cplus_constants (const_tree c)
182 tree folded_elems, elems = CONST_CAST_TREE (c);
183 int vec_len, i;
185 if (elems == NULL_TREE || elems == error_mark_node)
186 return elems;
188 vec_len = TREE_VEC_LENGTH (elems);
190 /* First check if there is at least one element that needs
191 folding. If there is none, we just return ELEMS. Otherwise create
192 and return a new tree vector that contains the folded versions of
193 ELEMS. This is to avoid allocating memory if we don't need
194 to. */
195 for (i = 0; i < vec_len; ++i)
197 if (template_arg_needs_folding (TREE_VEC_ELT (elems, i)))
198 break;
200 if (i == vec_len)
201 return elems;
203 folded_elems = make_tree_vec (vec_len);
204 for (i = 0; i < vec_len; ++i)
206 tree elem = TREE_VEC_ELT (elems, i);
207 TREE_VEC_ELT (folded_elems, i) =
208 (elem && !TYPE_P (elem)) ? cplus_expand_constant (elem) : elem;
211 return folded_elems;
214 /* The C++ implementation of the LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
215 hook. It returns the innermost template arguments of type T, and
216 makes sure those arguments are folded enough for the debug info
217 emitter. */
219 static tree
220 get_template_innermost_arguments_folded (const_tree t)
222 return fold_cplus_constants (get_template_innermost_arguments (t));
225 static tree
226 get_template_argument_pack_elems_folded (const_tree t)
228 return fold_cplus_constants (get_template_argument_pack_elems (t));
231 /* The C++ version of the enum_underlying_base_type langhook.
232 See also cp/semantics.cc (finish_underlying_type). */
234 static
235 tree cxx_enum_underlying_base_type (const_tree type)
237 tree underlying_type = ENUM_UNDERLYING_TYPE (type);
239 if (! ENUM_FIXED_UNDERLYING_TYPE_P (type))
240 underlying_type
241 = c_common_type_for_mode (TYPE_MODE (underlying_type),
242 TYPE_UNSIGNED (underlying_type));
244 return underlying_type;
247 /* The C++ version of the omp_get_decl_init langhook returns the static
248 initializer for a variable declaration if present, otherwise it
249 tries to find and return the dynamic initializer. If not present,
250 it returns NULL. */
252 static tree *
253 cxx_omp_get_decl_init (tree decl)
255 if (DECL_INITIAL (decl))
256 return &DECL_INITIAL (decl);
258 return hash_map_safe_get (dynamic_initializers, decl);
261 /* The C++ version of the omp_finish_decl_inits langhook allows GC to
262 reclaim the memory used by the hash-map used to hold dynamic initializer
263 information. */
265 static void
266 cxx_omp_finish_decl_inits (void)
268 dynamic_initializers = NULL;
271 /* Get a value for the SARIF v2.1.0 "artifact.sourceLanguage" property,
272 based on the list in SARIF v2.1.0 Appendix J. */
274 static const char *
275 cp_get_sarif_source_language (const char *)
277 return "cplusplus";
280 #if CHECKING_P
282 namespace selftest {
284 /* Implementation of LANG_HOOKS_RUN_LANG_SELFTESTS for the C++ frontend. */
286 void
287 run_cp_tests (void)
289 /* Run selftests shared within the C family. */
290 c_family_tests ();
292 /* Additional C++-specific tests. */
293 cp_pt_cc_tests ();
294 cp_tree_cc_tests ();
297 } // namespace selftest
299 #endif /* #if CHECKING_P */
302 #include "gt-cp-cp-lang.h"
303 #include "gtype-cp.h"