c-common.c (flag_noniso_default_format_attributes): Delete.
[gcc.git] / gcc / cp / cp-lang.c
1 /* Language-dependent hooks for C++.
2 Copyright 2001, 2002 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "c-common.h"
29 #include "toplev.h"
30 #include "langhooks.h"
31 #include "langhooks-def.h"
32
33 enum c_language_kind c_language = clk_cxx;
34
35 static HOST_WIDE_INT cxx_get_alias_set (tree);
36 static bool ok_to_generate_alias_set_for_type (tree);
37 static bool cxx_warn_unused_global_decl (tree);
38 static tree cp_expr_size (tree);
39 static size_t cp_tree_size (enum tree_code);
40 static bool cp_var_mod_type_p (tree);
41
42 #undef LANG_HOOKS_NAME
43 #define LANG_HOOKS_NAME "GNU C++"
44 #undef LANG_HOOKS_TREE_SIZE
45 #define LANG_HOOKS_TREE_SIZE cp_tree_size
46 #undef LANG_HOOKS_INIT
47 #define LANG_HOOKS_INIT cxx_init
48 #undef LANG_HOOKS_FINISH
49 #define LANG_HOOKS_FINISH cxx_finish
50 #undef LANG_HOOKS_CLEAR_BINDING_STACK
51 #define LANG_HOOKS_CLEAR_BINDING_STACK pop_everything
52 #undef LANG_HOOKS_INIT_OPTIONS
53 #define LANG_HOOKS_INIT_OPTIONS c_common_init_options
54 #undef LANG_HOOKS_HANDLE_OPTION
55 #define LANG_HOOKS_HANDLE_OPTION c_common_handle_option
56 #undef LANG_HOOKS_HANDLE_FILENAME
57 #define LANG_HOOKS_HANDLE_FILENAME c_common_handle_filename
58 #undef LANG_HOOKS_MISSING_ARGUMENT
59 #define LANG_HOOKS_MISSING_ARGUMENT c_common_missing_argument
60 #undef LANG_HOOKS_POST_OPTIONS
61 #define LANG_HOOKS_POST_OPTIONS c_common_post_options
62 #undef LANG_HOOKS_GET_ALIAS_SET
63 #define LANG_HOOKS_GET_ALIAS_SET cxx_get_alias_set
64 #undef LANG_HOOKS_EXPAND_CONSTANT
65 #define LANG_HOOKS_EXPAND_CONSTANT cplus_expand_constant
66 #undef LANG_HOOKS_EXPAND_EXPR
67 #define LANG_HOOKS_EXPAND_EXPR cxx_expand_expr
68 #undef LANG_HOOKS_SAFE_FROM_P
69 #define LANG_HOOKS_SAFE_FROM_P c_safe_from_p
70 #undef LANG_HOOKS_PARSE_FILE
71 #define LANG_HOOKS_PARSE_FILE c_common_parse_file
72 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
73 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL cxx_dup_lang_specific_decl
74 #undef LANG_HOOKS_UNSAVE_EXPR_NOW
75 #define LANG_HOOKS_UNSAVE_EXPR_NOW cxx_unsave_expr_now
76 #undef LANG_HOOKS_MAYBE_BUILD_CLEANUP
77 #define LANG_HOOKS_MAYBE_BUILD_CLEANUP cxx_maybe_build_cleanup
78 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
79 #define LANG_HOOKS_TRUTHVALUE_CONVERSION c_common_truthvalue_conversion
80 #undef LANG_HOOKS_UNSAFE_FOR_REEVAL
81 #define LANG_HOOKS_UNSAFE_FOR_REEVAL c_common_unsafe_for_reeval
82 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
83 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME mangle_decl
84 #undef LANG_HOOKS_MARK_ADDRESSABLE
85 #define LANG_HOOKS_MARK_ADDRESSABLE cxx_mark_addressable
86 #undef LANG_HOOKS_PRINT_STATISTICS
87 #define LANG_HOOKS_PRINT_STATISTICS cxx_print_statistics
88 #undef LANG_HOOKS_PRINT_XNODE
89 #define LANG_HOOKS_PRINT_XNODE cxx_print_xnode
90 #undef LANG_HOOKS_PRINT_DECL
91 #define LANG_HOOKS_PRINT_DECL cxx_print_decl
92 #undef LANG_HOOKS_PRINT_TYPE
93 #define LANG_HOOKS_PRINT_TYPE cxx_print_type
94 #undef LANG_HOOKS_PRINT_IDENTIFIER
95 #define LANG_HOOKS_PRINT_IDENTIFIER cxx_print_identifier
96 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
97 #define LANG_HOOKS_DECL_PRINTABLE_NAME cxx_printable_name
98 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
99 #define LANG_HOOKS_PRINT_ERROR_FUNCTION cxx_print_error_function
100 #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
101 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL cxx_warn_unused_global_decl
102 #undef LANG_HOOKS_WRITE_GLOBALS
103 #define LANG_HOOKS_WRITE_GLOBALS lhd_do_nothing
104
105
106 #undef LANG_HOOKS_FUNCTION_INIT
107 #define LANG_HOOKS_FUNCTION_INIT cxx_push_function_context
108 #undef LANG_HOOKS_FUNCTION_FINAL
109 #define LANG_HOOKS_FUNCTION_FINAL cxx_pop_function_context
110
111 /* Attribute hooks. */
112 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
113 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE c_common_attribute_table
114 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
115 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE c_common_format_attribute_table
116 #undef LANG_HOOKS_ATTRIBUTE_TABLE
117 #define LANG_HOOKS_ATTRIBUTE_TABLE cxx_attribute_table
118
119 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
120 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES \
121 cp_walk_subtrees
122 #undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
123 #define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
124 cp_cannot_inline_tree_fn
125 #undef LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS
126 #define LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS \
127 cp_add_pending_fn_decls
128 #undef LANG_HOOKS_TREE_INLINING_TREE_CHAIN_MATTERS_P
129 #define LANG_HOOKS_TREE_INLINING_TREE_CHAIN_MATTERS_P \
130 cp_is_overload_p
131 #undef LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P
132 #define LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P \
133 cp_auto_var_in_fn_p
134 #undef LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING
135 #define LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING \
136 cp_copy_res_decl_for_inlining
137 #undef LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P
138 #define LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P anon_aggr_type_p
139 #undef LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P
140 #define LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P cp_var_mod_type_p
141 #undef LANG_HOOKS_TREE_INLINING_START_INLINING
142 #define LANG_HOOKS_TREE_INLINING_START_INLINING cp_start_inlining
143 #undef LANG_HOOKS_TREE_INLINING_END_INLINING
144 #define LANG_HOOKS_TREE_INLINING_END_INLINING cp_end_inlining
145 #undef LANG_HOOKS_TREE_INLINING_ESTIMATE_NUM_INSNS
146 #define LANG_HOOKS_TREE_INLINING_ESTIMATE_NUM_INSNS c_estimate_num_insns
147 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
148 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN cp_dump_tree
149 #undef LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN
150 #define LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN cp_type_quals
151 #undef LANG_HOOKS_EXPR_SIZE
152 #define LANG_HOOKS_EXPR_SIZE cp_expr_size
153
154 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
155 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION expand_body
156 #undef LANG_HOOKS_CALLGRAPH_LOWER_FUNCTION
157 #define LANG_HOOKS_CALLGRAPH_LOWER_FUNCTION lower_function
158
159 #undef LANG_HOOKS_MAKE_TYPE
160 #define LANG_HOOKS_MAKE_TYPE cxx_make_type
161 #undef LANG_HOOKS_TYPE_FOR_MODE
162 #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
163 #undef LANG_HOOKS_TYPE_FOR_SIZE
164 #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
165 #undef LANG_HOOKS_SIGNED_TYPE
166 #define LANG_HOOKS_SIGNED_TYPE c_common_signed_type
167 #undef LANG_HOOKS_UNSIGNED_TYPE
168 #define LANG_HOOKS_UNSIGNED_TYPE c_common_unsigned_type
169 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
170 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE c_common_signed_or_unsigned_type
171 #undef LANG_HOOKS_INCOMPLETE_TYPE_ERROR
172 #define LANG_HOOKS_INCOMPLETE_TYPE_ERROR cxx_incomplete_type_error
173 #undef LANG_HOOKS_TYPE_PROMOTES_TO
174 #define LANG_HOOKS_TYPE_PROMOTES_TO cxx_type_promotes_to
175
176 /* Each front end provides its own hooks, for toplev.c. */
177 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
178
179 /* Tree code classes. */
180
181 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
182
183 const char tree_code_type[] = {
184 #include "tree.def"
185 'x',
186 #include "c-common.def"
187 'x',
188 #include "cp-tree.def"
189 };
190 #undef DEFTREECODE
191
192 /* Table indexed by tree code giving number of expression
193 operands beyond the fixed part of the node structure.
194 Not used for types or decls. */
195
196 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
197
198 const unsigned char tree_code_length[] = {
199 #include "tree.def"
200 0,
201 #include "c-common.def"
202 0,
203 #include "cp-tree.def"
204 };
205 #undef DEFTREECODE
206
207 /* Names of tree components.
208 Used for printing out the tree and error messages. */
209 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
210
211 const char *const tree_code_name[] = {
212 #include "tree.def"
213 "@@dummy",
214 #include "c-common.def"
215 "@@dummy",
216 #include "cp-tree.def"
217 };
218 #undef DEFTREECODE
219
220 /* Check if a C++ type is safe for aliasing.
221 Return TRUE if T safe for aliasing FALSE otherwise. */
222
223 static bool
224 ok_to_generate_alias_set_for_type (tree t)
225 {
226 if (TYPE_PTRMEMFUNC_P (t))
227 return true;
228 if (AGGREGATE_TYPE_P (t))
229 {
230 if ((TREE_CODE (t) == RECORD_TYPE) || (TREE_CODE (t) == UNION_TYPE))
231 {
232 tree fields;
233 /* Backend-created structs are safe. */
234 if (! CLASS_TYPE_P (t))
235 return true;
236 /* PODs are safe. */
237 if (! CLASSTYPE_NON_POD_P(t))
238 return true;
239 /* Classes with virtual baseclasses are not. */
240 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
241 return false;
242 /* Recursively check the base classes. */
243 if (TYPE_BINFO (t) != NULL && TYPE_BINFO_BASETYPES (t) != NULL)
244 {
245 int i;
246 for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t)); i++)
247 {
248 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
249 if (!ok_to_generate_alias_set_for_type (BINFO_TYPE (binfo)))
250 return false;
251 }
252 }
253 /* Check all the fields. */
254 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
255 {
256 if (TREE_CODE (fields) != FIELD_DECL)
257 continue;
258 if (! ok_to_generate_alias_set_for_type (TREE_TYPE (fields)))
259 return false;
260 }
261 return true;
262 }
263 else if (TREE_CODE (t) == ARRAY_TYPE)
264 return ok_to_generate_alias_set_for_type (TREE_TYPE (t));
265 else
266 /* This should never happen, we dealt with all the aggregate
267 types that can appear in C++ above. */
268 abort ();
269 }
270 else
271 return true;
272 }
273
274 /* Special routine to get the alias set for C++. */
275
276 static HOST_WIDE_INT
277 cxx_get_alias_set (tree t)
278 {
279
280 if (/* It's not yet safe to use alias sets for some classes in C++. */
281 !ok_to_generate_alias_set_for_type (t)
282 /* Nor is it safe to use alias sets for pointers-to-member
283 functions, due to the fact that there may be more than one
284 RECORD_TYPE type corresponding to the same pointer-to-member
285 type. */
286 || TYPE_PTRMEMFUNC_P (t))
287 return 0;
288
289 return c_common_get_alias_set (t);
290 }
291
292 /* Called from check_global_declarations. */
293
294 static bool
295 cxx_warn_unused_global_decl (tree decl)
296 {
297 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
298 return false;
299 if (DECL_IN_SYSTEM_HEADER (decl))
300 return false;
301
302 /* Const variables take the place of #defines in C++. */
303 if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
304 return false;
305
306 return true;
307 }
308
309 /* Langhook for expr_size: Tell the backend that the value of an expression
310 of non-POD class type does not include any tail padding; a derived class
311 might have allocated something there. */
312
313 static tree
314 cp_expr_size (tree exp)
315 {
316 if (CLASS_TYPE_P (TREE_TYPE (exp)))
317 {
318 /* The backend should not be interested in the size of an expression
319 of a type with both of these set; all copies of such types must go
320 through a constructor or assignment op. */
321 if (TYPE_HAS_COMPLEX_INIT_REF (TREE_TYPE (exp))
322 && TYPE_HAS_COMPLEX_ASSIGN_REF (TREE_TYPE (exp))
323 /* But storing a CONSTRUCTOR isn't a copy. */
324 && TREE_CODE (exp) != CONSTRUCTOR)
325 abort ();
326 /* This would be wrong for a type with virtual bases, but they are
327 caught by the abort above. */
328 return CLASSTYPE_SIZE_UNIT (TREE_TYPE (exp));
329 }
330 else
331 /* Use the default code. */
332 return lhd_expr_size (exp);
333 }
334
335 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */
336 static size_t
337 cp_tree_size (enum tree_code code)
338 {
339 switch (code)
340 {
341 case PTRMEM_CST: return sizeof (struct ptrmem_cst);
342 case BASELINK: return sizeof (struct tree_baselink);
343 case TEMPLATE_PARM_INDEX: return sizeof (template_parm_index);
344 case DEFAULT_ARG: return sizeof (struct tree_default_arg);
345 case OVERLOAD: return sizeof (struct tree_overload);
346 case WRAPPER: return sizeof (struct tree_wrapper);
347 default:
348 abort ();
349 }
350 /* NOTREACHED */
351 }
352
353 /* Returns true if T is a variably modified type, in the sense of C99.
354 This routine needs only check cases that cannot be handled by the
355 language-independent logic in tree-inline.c. */
356
357 static bool
358 cp_var_mod_type_p (tree type)
359 {
360 /* If TYPE is a pointer-to-member, it is variably modified if either
361 the class or the member are variably modified. */
362 if (TYPE_PTR_TO_MEMBER_P (type))
363 return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
364 || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type)));
365
366 /* All other types are not variably modified. */
367 return false;
368 }
369
370 /* Stub routine to tell people that this doesn't work yet. */
371 void
372 c_reset_state (void)
373 {
374 sorry ("inter-module optimisations not implemented yet");
375 }