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_INSERT_DEFAULT_ATTRIBUTES
81 #define LANG_HOOKS_INSERT_DEFAULT_ATTRIBUTES cxx_insert_default_attributes
82 #undef LANG_HOOKS_UNSAFE_FOR_REEVAL
83 #define LANG_HOOKS_UNSAFE_FOR_REEVAL c_common_unsafe_for_reeval
84 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
85 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME mangle_decl
86 #undef LANG_HOOKS_MARK_ADDRESSABLE
87 #define LANG_HOOKS_MARK_ADDRESSABLE cxx_mark_addressable
88 #undef LANG_HOOKS_PRINT_STATISTICS
89 #define LANG_HOOKS_PRINT_STATISTICS cxx_print_statistics
90 #undef LANG_HOOKS_PRINT_XNODE
91 #define LANG_HOOKS_PRINT_XNODE cxx_print_xnode
92 #undef LANG_HOOKS_PRINT_DECL
93 #define LANG_HOOKS_PRINT_DECL cxx_print_decl
94 #undef LANG_HOOKS_PRINT_TYPE
95 #define LANG_HOOKS_PRINT_TYPE cxx_print_type
96 #undef LANG_HOOKS_PRINT_IDENTIFIER
97 #define LANG_HOOKS_PRINT_IDENTIFIER cxx_print_identifier
98 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
99 #define LANG_HOOKS_DECL_PRINTABLE_NAME cxx_printable_name
100 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
101 #define LANG_HOOKS_PRINT_ERROR_FUNCTION cxx_print_error_function
102 #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
103 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL cxx_warn_unused_global_decl
104 #undef LANG_HOOKS_WRITE_GLOBALS
105 #define LANG_HOOKS_WRITE_GLOBALS lhd_do_nothing
108 #undef LANG_HOOKS_FUNCTION_INIT
109 #define LANG_HOOKS_FUNCTION_INIT cxx_push_function_context
110 #undef LANG_HOOKS_FUNCTION_FINAL
111 #define LANG_HOOKS_FUNCTION_FINAL cxx_pop_function_context
113 /* Attribute hooks. */
114 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
115 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE c_common_attribute_table
116 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
117 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE c_common_format_attribute_table
118 #undef LANG_HOOKS_ATTRIBUTE_TABLE
119 #define LANG_HOOKS_ATTRIBUTE_TABLE cxx_attribute_table
121 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
122 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES \
124 #undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
125 #define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
126 cp_cannot_inline_tree_fn
127 #undef LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS
128 #define LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS \
129 cp_add_pending_fn_decls
130 #undef LANG_HOOKS_TREE_INLINING_TREE_CHAIN_MATTERS_P
131 #define LANG_HOOKS_TREE_INLINING_TREE_CHAIN_MATTERS_P \
133 #undef LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P
134 #define LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P \
136 #undef LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING
137 #define LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING \
138 cp_copy_res_decl_for_inlining
139 #undef LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P
140 #define LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P anon_aggr_type_p
141 #undef LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P
142 #define LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P cp_var_mod_type_p
143 #undef LANG_HOOKS_TREE_INLINING_START_INLINING
144 #define LANG_HOOKS_TREE_INLINING_START_INLINING cp_start_inlining
145 #undef LANG_HOOKS_TREE_INLINING_END_INLINING
146 #define LANG_HOOKS_TREE_INLINING_END_INLINING cp_end_inlining
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_PREPARE_ASSEMBLE_VARIABLE
155 #define LANG_HOOKS_PREPARE_ASSEMBLE_VARIABLE prepare_assemble_variable
157 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
158 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION expand_body
159 #undef LANG_HOOKS_CALLGRAPH_LOWER_FUNCTION
160 #define LANG_HOOKS_CALLGRAPH_LOWER_FUNCTION lower_function
162 #undef LANG_HOOKS_MAKE_TYPE
163 #define LANG_HOOKS_MAKE_TYPE cxx_make_type
164 #undef LANG_HOOKS_TYPE_FOR_MODE
165 #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
166 #undef LANG_HOOKS_TYPE_FOR_SIZE
167 #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
168 #undef LANG_HOOKS_SIGNED_TYPE
169 #define LANG_HOOKS_SIGNED_TYPE c_common_signed_type
170 #undef LANG_HOOKS_UNSIGNED_TYPE
171 #define LANG_HOOKS_UNSIGNED_TYPE c_common_unsigned_type
172 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
173 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE c_common_signed_or_unsigned_type
174 #undef LANG_HOOKS_INCOMPLETE_TYPE_ERROR
175 #define LANG_HOOKS_INCOMPLETE_TYPE_ERROR cxx_incomplete_type_error
176 #undef LANG_HOOKS_TYPE_PROMOTES_TO
177 #define LANG_HOOKS_TYPE_PROMOTES_TO cxx_type_promotes_to
179 /* Each front end provides its own hooks, for toplev.c. */
180 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
182 /* Tree code classes. */
184 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
186 const char tree_code_type[] = {
189 #include "c-common.def"
191 #include "cp-tree.def"
195 /* Table indexed by tree code giving number of expression
196 operands beyond the fixed part of the node structure.
197 Not used for types or decls. */
199 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
201 const unsigned char tree_code_length[] = {
204 #include "c-common.def"
206 #include "cp-tree.def"
210 /* Names of tree components.
211 Used for printing out the tree and error messages. */
212 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
214 const char *const tree_code_name[] = {
217 #include "c-common.def"
219 #include "cp-tree.def"
223 /* Check if a C++ type is safe for aliasing.
224 Return TRUE if T safe for aliasing FALSE otherwise. */
227 ok_to_generate_alias_set_for_type (tree t)
229 if (TYPE_PTRMEMFUNC_P (t))
231 if (AGGREGATE_TYPE_P (t))
233 if ((TREE_CODE (t) == RECORD_TYPE) || (TREE_CODE (t) == UNION_TYPE))
236 /* Backend-created structs are safe. */
237 if (! CLASS_TYPE_P (t))
240 if (! CLASSTYPE_NON_POD_P(t))
242 /* Classes with virtual baseclasses are not. */
243 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
245 /* Recursively check the base classes. */
246 if (TYPE_BINFO (t) != NULL && TYPE_BINFO_BASETYPES (t) != NULL)
249 for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t)); i++)
251 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
252 if (!ok_to_generate_alias_set_for_type (BINFO_TYPE (binfo)))
256 /* Check all the fields. */
257 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
259 if (TREE_CODE (fields) != FIELD_DECL)
261 if (! ok_to_generate_alias_set_for_type (TREE_TYPE (fields)))
266 else if (TREE_CODE (t) == ARRAY_TYPE)
267 return ok_to_generate_alias_set_for_type (TREE_TYPE (t));
269 /* This should never happen, we dealt with all the aggregate
270 types that can appear in C++ above. */
277 /* Special routine to get the alias set for C++. */
280 cxx_get_alias_set (tree t)
283 if (/* It's not yet safe to use alias sets for some classes in C++. */
284 !ok_to_generate_alias_set_for_type (t)
285 /* Nor is it safe to use alias sets for pointers-to-member
286 functions, due to the fact that there may be more than one
287 RECORD_TYPE type corresponding to the same pointer-to-member
289 || TYPE_PTRMEMFUNC_P (t))
292 return c_common_get_alias_set (t);
295 /* Called from check_global_declarations. */
298 cxx_warn_unused_global_decl (tree decl)
300 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
302 if (DECL_IN_SYSTEM_HEADER (decl))
305 /* Const variables take the place of #defines in C++. */
306 if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
312 /* Langhook for expr_size: Tell the backend that the value of an expression
313 of non-POD class type does not include any tail padding; a derived class
314 might have allocated something there. */
317 cp_expr_size (tree exp)
319 if (CLASS_TYPE_P (TREE_TYPE (exp)))
321 /* The backend should not be interested in the size of an expression
322 of a type with both of these set; all copies of such types must go
323 through a constructor or assignment op. */
324 if (TYPE_HAS_COMPLEX_INIT_REF (TREE_TYPE (exp))
325 && TYPE_HAS_COMPLEX_ASSIGN_REF (TREE_TYPE (exp))
326 /* But storing a CONSTRUCTOR isn't a copy. */
327 && TREE_CODE (exp) != CONSTRUCTOR)
329 /* This would be wrong for a type with virtual bases, but they are
330 caught by the abort above. */
331 return CLASSTYPE_SIZE_UNIT (TREE_TYPE (exp));
334 /* Use the default code. */
335 return lhd_expr_size (exp);
338 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */
340 cp_tree_size (enum tree_code code)
344 case PTRMEM_CST: return sizeof (struct ptrmem_cst);
345 case BASELINK: return sizeof (struct tree_baselink);
346 case TEMPLATE_PARM_INDEX: return sizeof (template_parm_index);
347 case DEFAULT_ARG: return sizeof (struct tree_default_arg);
348 case OVERLOAD: return sizeof (struct tree_overload);
349 case WRAPPER: return sizeof (struct tree_wrapper);
356 /* Returns true if T is a variably modified type, in the sense of C99.
357 This routine needs only check cases that cannot be handled by the
358 language-independent logic in tree-inline.c. */
361 cp_var_mod_type_p (tree type)
363 /* If TYPE is a pointer-to-member, it is variably modified if either
364 the class or the member are variably modified. */
365 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
366 return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
367 || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type)));
369 /* All other types are not variably modified. */