]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/cp-lang.c
c++: Module lang hook overriding
[thirdparty/gcc.git] / gcc / cp / cp-lang.c
CommitLineData
19551f29 1/* Language-dependent hooks for C++.
8d9254fc 2 Copyright (C) 2001-2020 Free Software Foundation, Inc.
19551f29
AO
3 Contributed by Alexandre Oliva <aoliva@redhat.com>
4
f5adbb8d 5This file is part of GCC.
19551f29 6
f5adbb8d 7GCC is free software; you can redistribute it and/or modify
19551f29 8it under the terms of the GNU General Public License as published by
e77f031d 9the Free Software Foundation; either version 3, or (at your option)
19551f29
AO
10any later version.
11
f5adbb8d 12GCC is distributed in the hope that it will be useful,
19551f29
AO
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e77f031d
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
19551f29
AO
20
21#include "config.h"
22#include "system.h"
4977bab6 23#include "coretypes.h"
19551f29 24#include "cp-tree.h"
2adfab87 25#include "stor-layout.h"
19551f29 26#include "langhooks.h"
d23c55c2 27#include "langhooks-def.h"
11bb4b27 28#include "cp-objcp-common.h"
19551f29 29
37fa72e9 30enum c_language_kind c_language = clk_cxx;
a0ad3539 31static const char * cxx_dwarf_name (tree t, int verbosity);
394d9fe7 32static enum classify_record cp_classify_record (tree type);
f9417da1 33static tree cp_eh_personality (void);
f8fb7295
DS
34static tree get_template_innermost_arguments_folded (const_tree);
35static tree get_template_argument_pack_elems_folded (const_tree);
7315daa6 36static tree cxx_enum_underlying_base_type (const_tree);
37fa72e9 37
11bb4b27
ZL
38/* Lang hooks common to C++ and ObjC++ are declared in cp/cp-objcp-common.h;
39 consequently, there should be very few hooks below. */
40
3ac88239
NB
41#undef LANG_HOOKS_NAME
42#define LANG_HOOKS_NAME "GNU C++"
19551f29
AO
43#undef LANG_HOOKS_INIT
44#define LANG_HOOKS_INIT cxx_init
394d9fe7
AO
45#undef LANG_HOOKS_CLASSIFY_RECORD
46#define LANG_HOOKS_CLASSIFY_RECORD cp_classify_record
39ef6592
LC
47#undef LANG_HOOKS_GENERIC_TYPE_P
48#define LANG_HOOKS_GENERIC_TYPE_P class_tmpl_impl_spec_p
f9329c35
DS
49
50#undef LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS
51#define LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS \
52 get_primary_template_innermost_parameters
53#undef LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
54#define LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS \
f8fb7295 55 get_template_innermost_arguments_folded
d40a19da
DS
56#undef LANG_HOOKS_FUNCTION_PARAMETER_PACK_P
57#define LANG_HOOKS_FUNCTION_PARAMETER_PACK_P \
58 function_parameter_pack_p
f9329c35
DS
59#undef LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS
60#define LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS \
f8fb7295 61 get_template_argument_pack_elems_folded
f9329c35
DS
62#undef LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P
63#define LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P \
64 template_template_parameter_p
d40a19da
DS
65#undef LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P
66#define LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P \
67 function_parameter_expanded_from_pack_p
68#undef LANG_HOOKS_GET_GENERIC_FUNCTION_DECL
69#define LANG_HOOKS_GET_GENERIC_FUNCTION_DECL get_function_template_decl
a0ad3539
MM
70#undef LANG_HOOKS_DWARF_NAME
71#define LANG_HOOKS_DWARF_NAME cxx_dwarf_name
820cc88f 72#undef LANG_HOOKS_INIT_TS
ee336e84 73#define LANG_HOOKS_INIT_TS cp_common_init_ts
f9417da1
RG
74#undef LANG_HOOKS_EH_PERSONALITY
75#define LANG_HOOKS_EH_PERSONALITY cp_eh_personality
76#undef LANG_HOOKS_EH_RUNTIME_TYPE
77#define LANG_HOOKS_EH_RUNTIME_TYPE build_eh_type_type
7315daa6
MW
78#undef LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE
79#define LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE cxx_enum_underlying_base_type
97b56dec
NS
80#undef LANG_HOOKS_PREPROCESS_MAIN_FILE
81#define LANG_HOOKS_PREPROCESS_MAIN_FILE module_begin_main_file
82#undef LANG_HOOKS_PREPROCESS_OPTIONS
83#define LANG_HOOKS_PREPROCESS_OPTIONS module_preprocess_options
84#undef LANG_HOOKS_PREPROCESS_TOKEN
85#define LANG_HOOKS_PREPROCESS_TOKEN module_token_pre
f1e639b1 86
b6f43128
DM
87#if CHECKING_P
88#undef LANG_HOOKS_RUN_LANG_SELFTESTS
89#define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_cp_tests
90#endif /* #if CHECKING_P */
91
0d48e877
DM
92#undef LANG_HOOKS_GET_SUBSTRING_LOCATION
93#define LANG_HOOKS_GET_SUBSTRING_LOCATION c_get_substring_location
94
11bb4b27 95/* Each front end provides its own lang hook initializer. */
4537ec0c 96struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
8ac61af7 97
11bb4b27
ZL
98/* Lang hook routines common to C++ and ObjC++ appear in cp/cp-objcp-common.c;
99 there should be very few routines below. */
8bcefb43 100
11bb4b27 101/* The following function does something real, but only in Objective-C++. */
e1a4dd13 102
11bb4b27 103tree
12308bc6
PC
104objcp_tsubst_copy_and_build (tree /*t*/,
105 tree /*args*/,
106 tsubst_flags_t /*complain*/,
107 tree /*in_decl*/,
108 bool /*function_p*/)
f75fbaf7 109{
11bb4b27 110 return NULL_TREE;
f75fbaf7
ZW
111}
112
a0ad3539
MM
113static const char *
114cxx_dwarf_name (tree t, int verbosity)
115{
116 gcc_assert (DECL_P (t));
117
ca3edeae 118 if (DECL_NAME (t) && IDENTIFIER_ANON_P (DECL_NAME (t)))
bde2d108 119 return NULL;
a0ad3539 120 if (verbosity >= 2)
7496cd5b
SA
121 return decl_as_dwarf_string (t,
122 TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME
123 | TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
a0ad3539 124
7496cd5b 125 return lang_decl_dwarf_name (t, verbosity, false);
a0ad3539
MM
126}
127
394d9fe7
AO
128static enum classify_record
129cp_classify_record (tree type)
130{
cc059bdd
JM
131 if (TYPE_LANG_SPECIFIC (type)
132 && CLASSTYPE_DECLARED_CLASS (type))
394d9fe7
AO
133 return RECORD_IS_CLASS;
134
135 return RECORD_IS_STRUCT;
136}
137
f9417da1
RG
138static GTY(()) tree cp_eh_personality_decl;
139
140static tree
141cp_eh_personality (void)
142{
143 if (!cp_eh_personality_decl)
bfecd57c 144 cp_eh_personality_decl = build_personality_function ("gxx");
f9417da1
RG
145
146 return cp_eh_personality_decl;
147}
148
f8fb7295
DS
149/* This is a subroutine of fold_cplus_constants. It returns TRUE if T
150 is a C++ specific constant that needs to be folded further before
151 being passed to the debug info emitter. */
152
153static bool
154template_arg_needs_folding (const_tree t)
155{
156 /* For now only PTRMEM_CST nodes are to be folded further. */
157 if (TREE_CODE (t) == PTRMEM_CST)
158 return true;
159 return false;
160}
161
162/* Fold the elements of the TREE_VEC C which are C++ specific nodes
163 that would need folding so that they can be processed by the debug
164 info emitter. This is a subroutine of
165 get_template_innermost_arguments_folded and
166 get_template_argument_pack_elems_folded. */
167
168static tree
169fold_cplus_constants (const_tree c)
170{
171 tree folded_elems, elems = CONST_CAST_TREE (c);
172 int vec_len, i;
173
174 if (elems == NULL_TREE || elems == error_mark_node)
175 return elems;
176
177 vec_len = TREE_VEC_LENGTH (elems);
178
179 /* First check if there is at least one element that needs
180 folding. If there is none, we just return ELEMS. Otherwise create
181 and return a new tree vector that contains the folded versions of
182 ELEMS. This is to avoid allocating memory if we don't need
183 to. */
184 for (i = 0; i < vec_len; ++i)
185 {
186 if (template_arg_needs_folding (TREE_VEC_ELT (elems, i)))
187 break;
188 }
189 if (i == vec_len)
190 return elems;
191
192 folded_elems = make_tree_vec (vec_len);
193 for (i = 0; i < vec_len; ++i)
194 {
195 tree elem = TREE_VEC_ELT (elems, i);
196 TREE_VEC_ELT (folded_elems, i) =
197 (elem && !TYPE_P (elem)) ? cplus_expand_constant (elem) : elem;
198
199 }
200 return folded_elems;
201}
202
203/* The C++ implementation of the LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
204 hook. It returns the innermost template arguments of type T, and
205 makes sure those arguments are folded enough for the debug info
206 emitter. */
207
208static tree
209get_template_innermost_arguments_folded (const_tree t)
210{
211 return fold_cplus_constants (get_template_innermost_arguments (t));
212}
213
214static tree
215get_template_argument_pack_elems_folded (const_tree t)
216{
217 return fold_cplus_constants (get_template_argument_pack_elems (t));
218}
219
7315daa6
MW
220/* The C++ version of the enum_underlying_base_type langhook.
221 See also cp/semantics.c (finish_underlying_type). */
222
223static
224tree cxx_enum_underlying_base_type (const_tree type)
225{
226 tree underlying_type = ENUM_UNDERLYING_TYPE (type);
227
228 if (! ENUM_FIXED_UNDERLYING_TYPE_P (type))
229 underlying_type
230 = c_common_type_for_mode (TYPE_MODE (underlying_type),
231 TYPE_UNSIGNED (underlying_type));
232
233 return underlying_type;
234}
235
b6f43128
DM
236#if CHECKING_P
237
238namespace selftest {
239
240/* Implementation of LANG_HOOKS_RUN_LANG_SELFTESTS for the C++ frontend. */
241
242void
243run_cp_tests (void)
244{
245 /* Run selftests shared within the C family. */
246 c_family_tests ();
247
248 /* Additional C++-specific tests. */
9a004410
DM
249 cp_pt_c_tests ();
250 cp_tree_c_tests ();
b6f43128
DM
251}
252
253} // namespace selftest
254
255#endif /* #if CHECKING_P */
256
641da50a 257
6561cdf6 258#include "gt-cp-cp-lang.h"
11bb4b27 259#include "gtype-cp.h"