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)
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. */
24 #include "coretypes.h"
30 #include "langhooks.h"
31 #include "langhooks-def.h"
33 enum c_language_kind c_language
= clk_cxx
;
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
);
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
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
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
119 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
120 #define LANG_HOOKS_TREE_INLINING_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 \
131 #undef LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P
132 #define LANG_HOOKS_TREE_INLINING_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
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
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
176 /* Each front end provides its own hooks, for toplev.c. */
177 const struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
179 /* Tree code classes. */
181 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
183 const char tree_code_type
[] = {
186 #include "c-common.def"
188 #include "cp-tree.def"
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. */
196 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
198 const unsigned char tree_code_length
[] = {
201 #include "c-common.def"
203 #include "cp-tree.def"
207 /* Names of tree components.
208 Used for printing out the tree and error messages. */
209 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
211 const char *const tree_code_name
[] = {
214 #include "c-common.def"
216 #include "cp-tree.def"
220 /* Check if a C++ type is safe for aliasing.
221 Return TRUE if T safe for aliasing FALSE otherwise. */
224 ok_to_generate_alias_set_for_type (tree t
)
226 if (TYPE_PTRMEMFUNC_P (t
))
228 if (AGGREGATE_TYPE_P (t
))
230 if ((TREE_CODE (t
) == RECORD_TYPE
) || (TREE_CODE (t
) == UNION_TYPE
))
233 /* Backend-created structs are safe. */
234 if (! CLASS_TYPE_P (t
))
237 if (! CLASSTYPE_NON_POD_P(t
))
239 /* Classes with virtual baseclasses are not. */
240 if (TYPE_USES_VIRTUAL_BASECLASSES (t
))
242 /* Recursively check the base classes. */
243 if (TYPE_BINFO (t
) != NULL
&& TYPE_BINFO_BASETYPES (t
) != NULL
)
246 for (i
= 0; i
< TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t
)); i
++)
248 tree binfo
= TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), i
);
249 if (!ok_to_generate_alias_set_for_type (BINFO_TYPE (binfo
)))
253 /* Check all the fields. */
254 for (fields
= TYPE_FIELDS (t
); fields
; fields
= TREE_CHAIN (fields
))
256 if (TREE_CODE (fields
) != FIELD_DECL
)
258 if (! ok_to_generate_alias_set_for_type (TREE_TYPE (fields
)))
263 else if (TREE_CODE (t
) == ARRAY_TYPE
)
264 return ok_to_generate_alias_set_for_type (TREE_TYPE (t
));
266 /* This should never happen, we dealt with all the aggregate
267 types that can appear in C++ above. */
274 /* Special routine to get the alias set for C++. */
277 cxx_get_alias_set (tree t
)
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
286 || TYPE_PTRMEMFUNC_P (t
))
289 return c_common_get_alias_set (t
);
292 /* Called from check_global_declarations. */
295 cxx_warn_unused_global_decl (tree decl
)
297 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_DECLARED_INLINE_P (decl
))
299 if (DECL_IN_SYSTEM_HEADER (decl
))
302 /* Const variables take the place of #defines in C++. */
303 if (TREE_CODE (decl
) == VAR_DECL
&& TREE_READONLY (decl
))
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. */
314 cp_expr_size (tree exp
)
316 if (CLASS_TYPE_P (TREE_TYPE (exp
)))
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
)
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
));
331 /* Use the default code. */
332 return lhd_expr_size (exp
);
335 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */
337 cp_tree_size (enum tree_code code
)
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
);
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. */
358 cp_var_mod_type_p (tree type
)
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
)));
366 /* All other types are not variably modified. */
370 /* Stub routine to tell people that this doesn't work yet. */
374 sorry ("inter-module optimisations not implemented yet");