PR c++/9373
[official-gcc.git] / gcc / cp / cp-lang.c
blobb35c31c0763452e6a455704eaafcab7205dfd547
1 /* Language-dependent hooks for C++.
2 Copyright 2001, 2002 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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"
33 static HOST_WIDE_INT cxx_get_alias_set (tree);
34 static bool ok_to_generate_alias_set_for_type (tree);
35 static bool cxx_warn_unused_global_decl (tree);
36 static tree cp_expr_size (tree);
37 static bool cp_var_mod_type_p (tree);
39 #undef LANG_HOOKS_NAME
40 #define LANG_HOOKS_NAME "GNU C++"
41 #undef LANG_HOOKS_INIT
42 #define LANG_HOOKS_INIT cxx_init
43 #undef LANG_HOOKS_FINISH
44 #define LANG_HOOKS_FINISH cxx_finish
45 #undef LANG_HOOKS_CLEAR_BINDING_STACK
46 #define LANG_HOOKS_CLEAR_BINDING_STACK pop_everything
47 #undef LANG_HOOKS_INIT_OPTIONS
48 #define LANG_HOOKS_INIT_OPTIONS cxx_init_options
49 #undef LANG_HOOKS_DECODE_OPTION
50 #define LANG_HOOKS_DECODE_OPTION c_common_decode_option
51 #undef LANG_HOOKS_POST_OPTIONS
52 #define LANG_HOOKS_POST_OPTIONS c_common_post_options
53 #undef LANG_HOOKS_GET_ALIAS_SET
54 #define LANG_HOOKS_GET_ALIAS_SET cxx_get_alias_set
55 #undef LANG_HOOKS_EXPAND_CONSTANT
56 #define LANG_HOOKS_EXPAND_CONSTANT cplus_expand_constant
57 #undef LANG_HOOKS_EXPAND_EXPR
58 #define LANG_HOOKS_EXPAND_EXPR cxx_expand_expr
59 #undef LANG_HOOKS_SAFE_FROM_P
60 #define LANG_HOOKS_SAFE_FROM_P c_safe_from_p
61 #undef LANG_HOOKS_PARSE_FILE
62 #define LANG_HOOKS_PARSE_FILE c_common_parse_file
63 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
64 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL cxx_dup_lang_specific_decl
65 #undef LANG_HOOKS_UNSAVE_EXPR_NOW
66 #define LANG_HOOKS_UNSAVE_EXPR_NOW cxx_unsave_expr_now
67 #undef LANG_HOOKS_MAYBE_BUILD_CLEANUP
68 #define LANG_HOOKS_MAYBE_BUILD_CLEANUP cxx_maybe_build_cleanup
69 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
70 #define LANG_HOOKS_TRUTHVALUE_CONVERSION c_common_truthvalue_conversion
71 #undef LANG_HOOKS_INSERT_DEFAULT_ATTRIBUTES
72 #define LANG_HOOKS_INSERT_DEFAULT_ATTRIBUTES cxx_insert_default_attributes
73 #undef LANG_HOOKS_UNSAFE_FOR_REEVAL
74 #define LANG_HOOKS_UNSAFE_FOR_REEVAL c_common_unsafe_for_reeval
75 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
76 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME mangle_decl
77 #undef LANG_HOOKS_MARK_ADDRESSABLE
78 #define LANG_HOOKS_MARK_ADDRESSABLE cxx_mark_addressable
79 #undef LANG_HOOKS_PRINT_STATISTICS
80 #define LANG_HOOKS_PRINT_STATISTICS cxx_print_statistics
81 #undef LANG_HOOKS_PRINT_XNODE
82 #define LANG_HOOKS_PRINT_XNODE cxx_print_xnode
83 #undef LANG_HOOKS_PRINT_DECL
84 #define LANG_HOOKS_PRINT_DECL cxx_print_decl
85 #undef LANG_HOOKS_PRINT_TYPE
86 #define LANG_HOOKS_PRINT_TYPE cxx_print_type
87 #undef LANG_HOOKS_PRINT_IDENTIFIER
88 #define LANG_HOOKS_PRINT_IDENTIFIER cxx_print_identifier
89 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
90 #define LANG_HOOKS_DECL_PRINTABLE_NAME cxx_printable_name
91 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
92 #define LANG_HOOKS_PRINT_ERROR_FUNCTION cxx_print_error_function
93 #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
94 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL cxx_warn_unused_global_decl
95 #undef LANG_HOOKS_WRITE_GLOBALS
96 #define LANG_HOOKS_WRITE_GLOBALS lhd_do_nothing
99 #undef LANG_HOOKS_FUNCTION_INIT
100 #define LANG_HOOKS_FUNCTION_INIT cxx_push_function_context
101 #undef LANG_HOOKS_FUNCTION_FINAL
102 #define LANG_HOOKS_FUNCTION_FINAL cxx_pop_function_context
104 /* Attribute hooks. */
105 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
106 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE c_common_attribute_table
107 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
108 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE c_common_format_attribute_table
109 #undef LANG_HOOKS_ATTRIBUTE_TABLE
110 #define LANG_HOOKS_ATTRIBUTE_TABLE cxx_attribute_table
112 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
113 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES \
114 cp_walk_subtrees
115 #undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
116 #define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
117 cp_cannot_inline_tree_fn
118 #undef LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS
119 #define LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS \
120 cp_add_pending_fn_decls
121 #undef LANG_HOOKS_TREE_INLINING_TREE_CHAIN_MATTERS_P
122 #define LANG_HOOKS_TREE_INLINING_TREE_CHAIN_MATTERS_P \
123 cp_is_overload_p
124 #undef LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P
125 #define LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P \
126 cp_auto_var_in_fn_p
127 #undef LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING
128 #define LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING \
129 cp_copy_res_decl_for_inlining
130 #undef LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P
131 #define LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P anon_aggr_type_p
132 #undef LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P
133 #define LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P cp_var_mod_type_p
134 #undef LANG_HOOKS_TREE_INLINING_START_INLINING
135 #define LANG_HOOKS_TREE_INLINING_START_INLINING cp_start_inlining
136 #undef LANG_HOOKS_TREE_INLINING_END_INLINING
137 #define LANG_HOOKS_TREE_INLINING_END_INLINING cp_end_inlining
138 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
139 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN cp_dump_tree
140 #undef LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN
141 #define LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN cp_type_quals
142 #undef LANG_HOOKS_EXPR_SIZE
143 #define LANG_HOOKS_EXPR_SIZE cp_expr_size
145 #undef LANG_HOOKS_MAKE_TYPE
146 #define LANG_HOOKS_MAKE_TYPE cxx_make_type
147 #undef LANG_HOOKS_TYPE_FOR_MODE
148 #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
149 #undef LANG_HOOKS_TYPE_FOR_SIZE
150 #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
151 #undef LANG_HOOKS_SIGNED_TYPE
152 #define LANG_HOOKS_SIGNED_TYPE c_common_signed_type
153 #undef LANG_HOOKS_UNSIGNED_TYPE
154 #define LANG_HOOKS_UNSIGNED_TYPE c_common_unsigned_type
155 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
156 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE c_common_signed_or_unsigned_type
157 #undef LANG_HOOKS_INCOMPLETE_TYPE_ERROR
158 #define LANG_HOOKS_INCOMPLETE_TYPE_ERROR cxx_incomplete_type_error
159 #undef LANG_HOOKS_TYPE_PROMOTES_TO
160 #define LANG_HOOKS_TYPE_PROMOTES_TO cxx_type_promotes_to
162 /* Each front end provides its own hooks, for toplev.c. */
163 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
165 /* Tree code classes. */
167 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
169 const char tree_code_type[] = {
170 #include "tree.def"
171 'x',
172 #include "c-common.def"
173 'x',
174 #include "cp-tree.def"
176 #undef DEFTREECODE
178 /* Table indexed by tree code giving number of expression
179 operands beyond the fixed part of the node structure.
180 Not used for types or decls. */
182 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
184 const unsigned char tree_code_length[] = {
185 #include "tree.def"
187 #include "c-common.def"
189 #include "cp-tree.def"
191 #undef DEFTREECODE
193 /* Names of tree components.
194 Used for printing out the tree and error messages. */
195 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
197 const char *const tree_code_name[] = {
198 #include "tree.def"
199 "@@dummy",
200 #include "c-common.def"
201 "@@dummy",
202 #include "cp-tree.def"
204 #undef DEFTREECODE
206 /* Check if a C++ type is safe for aliasing.
207 Return TRUE if T safe for aliasing FALSE otherwise. */
209 static bool
210 ok_to_generate_alias_set_for_type (tree t)
212 if (TYPE_PTRMEMFUNC_P (t))
213 return true;
214 if (AGGREGATE_TYPE_P (t))
216 if ((TREE_CODE (t) == RECORD_TYPE) || (TREE_CODE (t) == UNION_TYPE))
218 tree fields;
219 /* Backend-created structs are safe. */
220 if (! CLASS_TYPE_P (t))
221 return true;
222 /* PODs are safe. */
223 if (! CLASSTYPE_NON_POD_P(t))
224 return true;
225 /* Classes with virtual baseclasses are not. */
226 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
227 return false;
228 /* Recursively check the base classes. */
229 if (TYPE_BINFO (t) != NULL && TYPE_BINFO_BASETYPES (t) != NULL)
231 int i;
232 for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t)); i++)
234 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
235 if (!ok_to_generate_alias_set_for_type (BINFO_TYPE (binfo)))
236 return false;
239 /* Check all the fields. */
240 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
242 if (TREE_CODE (fields) != FIELD_DECL)
243 continue;
244 if (! ok_to_generate_alias_set_for_type (TREE_TYPE (fields)))
245 return false;
247 return true;
249 else if (TREE_CODE (t) == ARRAY_TYPE)
250 return ok_to_generate_alias_set_for_type (TREE_TYPE (t));
251 else
252 /* This should never happen, we dealt with all the aggregate
253 types that can appear in C++ above. */
254 abort ();
256 else
257 return true;
260 /* Special routine to get the alias set for C++. */
262 static HOST_WIDE_INT
263 cxx_get_alias_set (tree t)
266 if (/* It's not yet safe to use alias sets for some classes in C++. */
267 !ok_to_generate_alias_set_for_type (t)
268 /* Nor is it safe to use alias sets for pointers-to-member
269 functions, due to the fact that there may be more than one
270 RECORD_TYPE type corresponding to the same pointer-to-member
271 type. */
272 || TYPE_PTRMEMFUNC_P (t))
273 return 0;
275 return c_common_get_alias_set (t);
278 /* Called from check_global_declarations. */
280 static bool
281 cxx_warn_unused_global_decl (tree decl)
283 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
284 return false;
285 if (DECL_IN_SYSTEM_HEADER (decl))
286 return false;
288 /* Const variables take the place of #defines in C++. */
289 if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
290 return false;
292 return true;
295 /* Langhook for expr_size: Tell the backend that the value of an expression
296 of non-POD class type does not include any tail padding; a derived class
297 might have allocated something there. */
299 static tree
300 cp_expr_size (tree exp)
302 if (CLASS_TYPE_P (TREE_TYPE (exp)))
304 /* The backend should not be interested in the size of an expression
305 of a type with both of these set; all copies of such types must go
306 through a constructor or assignment op. */
307 if (TYPE_HAS_COMPLEX_INIT_REF (TREE_TYPE (exp))
308 && TYPE_HAS_COMPLEX_ASSIGN_REF (TREE_TYPE (exp))
309 /* But storing a CONSTRUCTOR isn't a copy. */
310 && TREE_CODE (exp) != CONSTRUCTOR)
311 abort ();
312 /* This would be wrong for a type with virtual bases, but they are
313 caught by the abort above. */
314 return CLASSTYPE_SIZE_UNIT (TREE_TYPE (exp));
316 else
317 /* Use the default code. */
318 return lhd_expr_size (exp);
321 /* Returns true if T is a variably modified type, in the sense of C99.
322 This routine needs only check cases that cannot be handled by the
323 language-independent logic in tree-inline.c. */
325 static bool
326 cp_var_mod_type_p (tree type)
328 /* If TYPE is a pointer-to-member, it is variably modified if either
329 the class or the member are variably modified. */
330 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
331 return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
332 || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type)));
334 /* All other types are not variably modified. */
335 return false;