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