]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/cp-lang.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / cp / cp-lang.c
1 /* Language-dependent hooks for C++.
2 Copyright (C) 2001-2015 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 "tm.h"
25 #include "hash-set.h"
26 #include "vec.h"
27 #include "input.h"
28 #include "alias.h"
29 #include "symtab.h"
30 #include "inchash.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "cp-tree.h"
34 #include "c-family/c-common.h"
35 #include "langhooks.h"
36 #include "langhooks-def.h"
37 #include "debug.h"
38 #include "cp-objcp-common.h"
39 #include "hashtab.h"
40 #include "target.h"
41 #include "parser.h"
42
43 enum c_language_kind c_language = clk_cxx;
44 static void cp_init_ts (void);
45 static const char * cxx_dwarf_name (tree t, int verbosity);
46 static enum classify_record cp_classify_record (tree type);
47 static tree cp_eh_personality (void);
48 static tree get_template_innermost_arguments_folded (const_tree);
49 static tree get_template_argument_pack_elems_folded (const_tree);
50 static tree cxx_enum_underlying_base_type (const_tree);
51
52 /* Lang hooks common to C++ and ObjC++ are declared in cp/cp-objcp-common.h;
53 consequently, there should be very few hooks below. */
54
55 #undef LANG_HOOKS_NAME
56 #define LANG_HOOKS_NAME "GNU C++"
57 #undef LANG_HOOKS_INIT
58 #define LANG_HOOKS_INIT cxx_init
59 #undef LANG_HOOKS_CLASSIFY_RECORD
60 #define LANG_HOOKS_CLASSIFY_RECORD cp_classify_record
61 #undef LANG_HOOKS_GENERIC_TYPE_P
62 #define LANG_HOOKS_GENERIC_TYPE_P class_tmpl_impl_spec_p
63
64 #undef LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS
65 #define LANG_HOOKS_GET_INNERMOST_GENERIC_PARMS \
66 get_primary_template_innermost_parameters
67 #undef LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
68 #define LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS \
69 get_template_innermost_arguments_folded
70 #undef LANG_HOOKS_FUNCTION_PARAMETER_PACK_P
71 #define LANG_HOOKS_FUNCTION_PARAMETER_PACK_P \
72 function_parameter_pack_p
73 #undef LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS
74 #define LANG_HOOKS_GET_ARGUMENT_PACK_ELEMS \
75 get_template_argument_pack_elems_folded
76 #undef LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P
77 #define LANG_HOOKS_GENERIC_GENERIC_PARAMETER_DECL_P \
78 template_template_parameter_p
79 #undef LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P
80 #define LANG_HOOKS_FUNCTION_PARM_EXPANDED_FROM_PACK_P \
81 function_parameter_expanded_from_pack_p
82 #undef LANG_HOOKS_GET_GENERIC_FUNCTION_DECL
83 #define LANG_HOOKS_GET_GENERIC_FUNCTION_DECL get_function_template_decl
84 #undef LANG_HOOKS_DWARF_NAME
85 #define LANG_HOOKS_DWARF_NAME cxx_dwarf_name
86 #undef LANG_HOOKS_INIT_TS
87 #define LANG_HOOKS_INIT_TS cp_init_ts
88 #undef LANG_HOOKS_EH_PERSONALITY
89 #define LANG_HOOKS_EH_PERSONALITY cp_eh_personality
90 #undef LANG_HOOKS_EH_RUNTIME_TYPE
91 #define LANG_HOOKS_EH_RUNTIME_TYPE build_eh_type_type
92 #undef LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE
93 #define LANG_HOOKS_ENUM_UNDERLYING_BASE_TYPE cxx_enum_underlying_base_type
94
95 /* Each front end provides its own lang hook initializer. */
96 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
97
98 /* Lang hook routines common to C++ and ObjC++ appear in cp/cp-objcp-common.c;
99 there should be very few routines below. */
100
101 /* The following function does something real, but only in Objective-C++. */
102
103 tree
104 objcp_tsubst_copy_and_build (tree /*t*/,
105 tree /*args*/,
106 tsubst_flags_t /*complain*/,
107 tree /*in_decl*/,
108 bool /*function_p*/)
109 {
110 return NULL_TREE;
111 }
112
113 static void
114 cp_init_ts (void)
115 {
116 cp_common_init_ts ();
117
118 init_shadowed_var_for_decl ();
119 }
120
121 static const char *
122 cxx_dwarf_name (tree t, int verbosity)
123 {
124 gcc_assert (DECL_P (t));
125
126 if (DECL_NAME (t)
127 && (ANON_AGGRNAME_P (DECL_NAME (t)) || LAMBDA_TYPE_P (t)))
128 return NULL;
129 if (verbosity >= 2)
130 return decl_as_dwarf_string (t,
131 TFF_DECL_SPECIFIERS | TFF_UNQUALIFIED_NAME
132 | TFF_NO_OMIT_DEFAULT_TEMPLATE_ARGUMENTS);
133
134 return lang_decl_dwarf_name (t, verbosity, false);
135 }
136
137 static enum classify_record
138 cp_classify_record (tree type)
139 {
140 if (TYPE_LANG_SPECIFIC (type)
141 && CLASSTYPE_DECLARED_CLASS (type))
142 return RECORD_IS_CLASS;
143
144 return RECORD_IS_STRUCT;
145 }
146
147 static GTY(()) tree cp_eh_personality_decl;
148
149 static tree
150 cp_eh_personality (void)
151 {
152 if (!cp_eh_personality_decl)
153 {
154 const char *lang = (pragma_java_exceptions ? "gcj" : "gxx");
155 cp_eh_personality_decl = build_personality_function (lang);
156 }
157
158 return cp_eh_personality_decl;
159 }
160
161 /* This is a subroutine of fold_cplus_constants. It returns TRUE if T
162 is a C++ specific constant that needs to be folded further before
163 being passed to the debug info emitter. */
164
165 static bool
166 template_arg_needs_folding (const_tree t)
167 {
168 /* For now only PTRMEM_CST nodes are to be folded further. */
169 if (TREE_CODE (t) == PTRMEM_CST)
170 return true;
171 return false;
172 }
173
174 /* Fold the elements of the TREE_VEC C which are C++ specific nodes
175 that would need folding so that they can be processed by the debug
176 info emitter. This is a subroutine of
177 get_template_innermost_arguments_folded and
178 get_template_argument_pack_elems_folded. */
179
180 static tree
181 fold_cplus_constants (const_tree c)
182 {
183 tree folded_elems, elems = CONST_CAST_TREE (c);
184 int vec_len, i;
185
186 if (elems == NULL_TREE || elems == error_mark_node)
187 return elems;
188
189 vec_len = TREE_VEC_LENGTH (elems);
190
191 /* First check if there is at least one element that needs
192 folding. If there is none, we just return ELEMS. Otherwise create
193 and return a new tree vector that contains the folded versions of
194 ELEMS. This is to avoid allocating memory if we don't need
195 to. */
196 for (i = 0; i < vec_len; ++i)
197 {
198 if (template_arg_needs_folding (TREE_VEC_ELT (elems, i)))
199 break;
200 }
201 if (i == vec_len)
202 return elems;
203
204 folded_elems = make_tree_vec (vec_len);
205 for (i = 0; i < vec_len; ++i)
206 {
207 tree elem = TREE_VEC_ELT (elems, i);
208 TREE_VEC_ELT (folded_elems, i) =
209 (elem && !TYPE_P (elem)) ? cplus_expand_constant (elem) : elem;
210
211 }
212 return folded_elems;
213 }
214
215 /* The C++ implementation of the LANG_HOOKS_GET_INNERMOST_GENERIC_ARGS
216 hook. It returns the innermost template arguments of type T, and
217 makes sure those arguments are folded enough for the debug info
218 emitter. */
219
220 static tree
221 get_template_innermost_arguments_folded (const_tree t)
222 {
223 return fold_cplus_constants (get_template_innermost_arguments (t));
224 }
225
226 static tree
227 get_template_argument_pack_elems_folded (const_tree t)
228 {
229 return fold_cplus_constants (get_template_argument_pack_elems (t));
230 }
231
232 /* The C++ version of the enum_underlying_base_type langhook.
233 See also cp/semantics.c (finish_underlying_type). */
234
235 static
236 tree cxx_enum_underlying_base_type (const_tree type)
237 {
238 tree underlying_type = ENUM_UNDERLYING_TYPE (type);
239
240 if (! ENUM_FIXED_UNDERLYING_TYPE_P (type))
241 underlying_type
242 = c_common_type_for_mode (TYPE_MODE (underlying_type),
243 TYPE_UNSIGNED (underlying_type));
244
245 return underlying_type;
246 }
247
248 #include "gt-cp-cp-lang.h"
249 #include "gtype-cp.h"