* cp-lang.c (cxx_types_compatible_p): To the middle-end,
[official-gcc.git] / gcc / cp / cp-lang.c
blobb6e933fede6f8b9ce75fd877d42fed3aafdb31b4
1 /* Language-dependent hooks for C++.
2 Copyright 2001, 2002, 2004 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"
32 #include "diagnostic.h"
33 #include "cxx-pretty-print.h"
35 enum c_language_kind c_language = clk_cxx;
37 static HOST_WIDE_INT cxx_get_alias_set (tree);
38 static bool cxx_warn_unused_global_decl (tree);
39 static tree cp_expr_size (tree);
40 static size_t cp_tree_size (enum tree_code);
41 static bool cp_var_mod_type_p (tree, tree);
42 static int cxx_types_compatible_p (tree, tree);
43 static void cxx_initialize_diagnostics (diagnostic_context *);
45 #undef LANG_HOOKS_NAME
46 #define LANG_HOOKS_NAME "GNU C++"
47 #undef LANG_HOOKS_TREE_SIZE
48 #define LANG_HOOKS_TREE_SIZE cp_tree_size
49 #undef LANG_HOOKS_INIT
50 #define LANG_HOOKS_INIT cxx_init
51 #undef LANG_HOOKS_FINISH
52 #define LANG_HOOKS_FINISH cxx_finish
53 #undef LANG_HOOKS_CLEAR_BINDING_STACK
54 #define LANG_HOOKS_CLEAR_BINDING_STACK pop_everything
55 #undef LANG_HOOKS_INIT_OPTIONS
56 #define LANG_HOOKS_INIT_OPTIONS c_common_init_options
57 #undef LANG_HOOKS_INITIALIZE_DIAGNOSTICS
58 #define LANG_HOOKS_INITIALIZE_DIAGNOSTICS cxx_initialize_diagnostics
59 #undef LANG_HOOKS_HANDLE_OPTION
60 #define LANG_HOOKS_HANDLE_OPTION c_common_handle_option
61 #undef LANG_HOOKS_HANDLE_FILENAME
62 #define LANG_HOOKS_HANDLE_FILENAME c_common_handle_filename
63 #undef LANG_HOOKS_MISSING_ARGUMENT
64 #define LANG_HOOKS_MISSING_ARGUMENT c_common_missing_argument
65 #undef LANG_HOOKS_POST_OPTIONS
66 #define LANG_HOOKS_POST_OPTIONS c_common_post_options
67 #undef LANG_HOOKS_GET_ALIAS_SET
68 #define LANG_HOOKS_GET_ALIAS_SET cxx_get_alias_set
69 #undef LANG_HOOKS_EXPAND_CONSTANT
70 #define LANG_HOOKS_EXPAND_CONSTANT cplus_expand_constant
71 #undef LANG_HOOKS_EXPAND_EXPR
72 #define LANG_HOOKS_EXPAND_EXPR cxx_expand_expr
73 #undef LANG_HOOKS_EXPAND_DECL
74 #define LANG_HOOKS_EXPAND_DECL c_expand_decl
75 #undef LANG_HOOKS_PARSE_FILE
76 #define LANG_HOOKS_PARSE_FILE c_common_parse_file
77 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
78 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL cxx_dup_lang_specific_decl
79 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
80 #define LANG_HOOKS_TRUTHVALUE_CONVERSION c_common_truthvalue_conversion
81 #undef LANG_HOOKS_UNSAFE_FOR_REEVAL
82 #define LANG_HOOKS_UNSAFE_FOR_REEVAL c_common_unsafe_for_reeval
83 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
84 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME mangle_decl
85 #undef LANG_HOOKS_MARK_ADDRESSABLE
86 #define LANG_HOOKS_MARK_ADDRESSABLE cxx_mark_addressable
87 #undef LANG_HOOKS_PRINT_STATISTICS
88 #define LANG_HOOKS_PRINT_STATISTICS cxx_print_statistics
89 #undef LANG_HOOKS_PRINT_XNODE
90 #define LANG_HOOKS_PRINT_XNODE cxx_print_xnode
91 #undef LANG_HOOKS_PRINT_DECL
92 #define LANG_HOOKS_PRINT_DECL cxx_print_decl
93 #undef LANG_HOOKS_PRINT_TYPE
94 #define LANG_HOOKS_PRINT_TYPE cxx_print_type
95 #undef LANG_HOOKS_PRINT_IDENTIFIER
96 #define LANG_HOOKS_PRINT_IDENTIFIER cxx_print_identifier
97 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
98 #define LANG_HOOKS_DECL_PRINTABLE_NAME cxx_printable_name
99 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
100 #define LANG_HOOKS_PRINT_ERROR_FUNCTION cxx_print_error_function
101 #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
102 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL cxx_warn_unused_global_decl
103 #undef LANG_HOOKS_WRITE_GLOBALS
104 #define LANG_HOOKS_WRITE_GLOBALS lhd_do_nothing
107 #undef LANG_HOOKS_FUNCTION_INIT
108 #define LANG_HOOKS_FUNCTION_INIT cxx_push_function_context
109 #undef LANG_HOOKS_FUNCTION_FINAL
110 #define LANG_HOOKS_FUNCTION_FINAL cxx_pop_function_context
111 #undef LANG_HOOKS_FUNCTION_MISSING_NORETURN_OK_P
112 #define LANG_HOOKS_FUNCTION_MISSING_NORETURN_OK_P cp_missing_noreturn_ok_p
114 /* Attribute hooks. */
115 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
116 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE c_common_attribute_table
117 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
118 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE c_common_format_attribute_table
119 #undef LANG_HOOKS_ATTRIBUTE_TABLE
120 #define LANG_HOOKS_ATTRIBUTE_TABLE cxx_attribute_table
122 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
123 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES \
124 cp_walk_subtrees
125 #undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
126 #define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
127 cp_cannot_inline_tree_fn
128 #undef LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS
129 #define LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS \
130 cp_add_pending_fn_decls
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_DUMP_DUMP_TREE_FN
142 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN cp_dump_tree
143 #undef LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN
144 #define LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN cp_type_quals
145 #undef LANG_HOOKS_EXPR_SIZE
146 #define LANG_HOOKS_EXPR_SIZE cp_expr_size
148 #undef LANG_HOOKS_CALLGRAPH_ANALYZE_EXPR
149 #define LANG_HOOKS_CALLGRAPH_ANALYZE_EXPR cxx_callgraph_analyze_expr
150 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
151 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION expand_body
153 #undef LANG_HOOKS_MAKE_TYPE
154 #define LANG_HOOKS_MAKE_TYPE cxx_make_type
155 #undef LANG_HOOKS_TYPE_FOR_MODE
156 #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
157 #undef LANG_HOOKS_TYPE_FOR_SIZE
158 #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
159 #undef LANG_HOOKS_SIGNED_TYPE
160 #define LANG_HOOKS_SIGNED_TYPE c_common_signed_type
161 #undef LANG_HOOKS_UNSIGNED_TYPE
162 #define LANG_HOOKS_UNSIGNED_TYPE c_common_unsigned_type
163 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
164 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE c_common_signed_or_unsigned_type
165 #undef LANG_HOOKS_INCOMPLETE_TYPE_ERROR
166 #define LANG_HOOKS_INCOMPLETE_TYPE_ERROR cxx_incomplete_type_error
167 #undef LANG_HOOKS_TYPE_PROMOTES_TO
168 #define LANG_HOOKS_TYPE_PROMOTES_TO cxx_type_promotes_to
169 #undef LANG_HOOKS_TYPES_COMPATIBLE_P
170 #define LANG_HOOKS_TYPES_COMPATIBLE_P cxx_types_compatible_p
171 #undef LANG_HOOKS_REGISTER_BUILTIN_TYPE
172 #define LANG_HOOKS_REGISTER_BUILTIN_TYPE c_register_builtin_type
173 #undef LANG_HOOKS_GIMPLIFY_EXPR
174 #define LANG_HOOKS_GIMPLIFY_EXPR cp_gimplify_expr
175 #undef LANG_HOOKS_FOLD_OBJ_TYPE_REF
176 #define LANG_HOOKS_FOLD_OBJ_TYPE_REF cp_fold_obj_type_ref
178 /* Each front end provides its own hooks, for toplev.c. */
179 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
181 /* Tree code classes. */
183 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
185 const char tree_code_type[] = {
186 #include "tree.def"
187 'x',
188 #include "c-common.def"
189 'x',
190 #include "cp-tree.def"
192 #undef DEFTREECODE
194 /* Table indexed by tree code giving number of expression
195 operands beyond the fixed part of the node structure.
196 Not used for types or decls. */
198 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
200 const unsigned char tree_code_length[] = {
201 #include "tree.def"
203 #include "c-common.def"
205 #include "cp-tree.def"
207 #undef DEFTREECODE
209 /* Names of tree components.
210 Used for printing out the tree and error messages. */
211 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
213 const char *const tree_code_name[] = {
214 #include "tree.def"
215 "@@dummy",
216 #include "c-common.def"
217 "@@dummy",
218 #include "cp-tree.def"
220 #undef DEFTREECODE
222 /* Special routine to get the alias set for C++. */
224 static HOST_WIDE_INT
225 cxx_get_alias_set (tree t)
227 if (IS_FAKE_BASE_TYPE (t))
228 /* The base variant of a type must be in the same alias set as the
229 complete type. */
230 return get_alias_set (TYPE_CONTEXT (t));
232 /* Punt on PMFs until we canonicalize functions properly. */
233 if (TYPE_PTRMEMFUNC_P (t))
234 return 0;
236 return c_common_get_alias_set (t);
239 /* Called from check_global_declarations. */
241 static bool
242 cxx_warn_unused_global_decl (tree decl)
244 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
245 return false;
246 if (DECL_IN_SYSTEM_HEADER (decl))
247 return false;
249 /* Const variables take the place of #defines in C++. */
250 if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
251 return false;
253 return true;
256 /* Langhook for expr_size: Tell the backend that the value of an expression
257 of non-POD class type does not include any tail padding; a derived class
258 might have allocated something there. */
260 static tree
261 cp_expr_size (tree exp)
263 if (CLASS_TYPE_P (TREE_TYPE (exp)))
265 /* The backend should not be interested in the size of an expression
266 of a type with both of these set; all copies of such types must go
267 through a constructor or assignment op. */
268 if (TYPE_HAS_COMPLEX_INIT_REF (TREE_TYPE (exp))
269 && TYPE_HAS_COMPLEX_ASSIGN_REF (TREE_TYPE (exp))
270 /* But storing a CONSTRUCTOR isn't a copy. */
271 && TREE_CODE (exp) != CONSTRUCTOR)
272 abort ();
273 /* This would be wrong for a type with virtual bases, but they are
274 caught by the abort above. */
275 return (is_empty_class (TREE_TYPE (exp))
276 ? size_zero_node
277 : CLASSTYPE_SIZE_UNIT (TREE_TYPE (exp)));
279 else
280 /* Use the default code. */
281 return lhd_expr_size (exp);
284 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */
285 static size_t
286 cp_tree_size (enum tree_code code)
288 switch (code)
290 case PTRMEM_CST: return sizeof (struct ptrmem_cst);
291 case BASELINK: return sizeof (struct tree_baselink);
292 case TEMPLATE_PARM_INDEX: return sizeof (template_parm_index);
293 case DEFAULT_ARG: return sizeof (struct tree_default_arg);
294 case OVERLOAD: return sizeof (struct tree_overload);
295 default:
296 abort ();
298 /* NOTREACHED */
301 /* Returns true if T is a variably modified type, in the sense of C99.
302 FN is as passed to variably_modified_p.
303 This routine needs only check cases that cannot be handled by the
304 language-independent logic in tree.c. */
306 static bool
307 cp_var_mod_type_p (tree type, tree fn)
309 /* If TYPE is a pointer-to-member, it is variably modified if either
310 the class or the member are variably modified. */
311 if (TYPE_PTR_TO_MEMBER_P (type))
312 return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type), fn)
313 || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type),
314 fn));
316 /* All other types are not variably modified. */
317 return false;
320 /* This compares two types for equivalence ("compatible" in C-based languages).
321 This routine should only return 1 if it is sure. It should not be used
322 in contexts where erroneously returning 0 causes problems. */
324 static int cxx_types_compatible_p (tree x, tree y)
326 if (same_type_ignoring_top_level_qualifiers_p (x, y))
327 return 1;
329 /* Once we get to the middle-end, references and pointers are
330 interchangeable. FIXME should we try to replace all references with
331 pointers? */
332 if (POINTER_TYPE_P (x) && POINTER_TYPE_P (y)
333 && same_type_p (TREE_TYPE (x), TREE_TYPE (y)))
334 return 1;
336 return 0;
339 /* Construct a C++-aware pretty-printer for CONTEXT. It is assumed
340 that CONTEXT->printer is an already constructed basic pretty_printer. */
341 static void
342 cxx_initialize_diagnostics (diagnostic_context *context)
344 pretty_printer *base = context->printer;
345 cxx_pretty_printer *pp = xmalloc (sizeof (cxx_pretty_printer));
346 memcpy (pp_base (pp), base, sizeof (pretty_printer));
347 pp_cxx_pretty_printer_init (pp);
348 context->printer = (pretty_printer *) pp;
350 /* It is safe to free this object because it was previously malloc()'d. */
351 free (base);
354 /* Stubs to keep c-opts.c happy. */
355 void
356 push_file_scope (void)
360 void
361 pop_file_scope (void)
365 /* c-pragma.c needs to query whether a decl has extern "C" linkage. */
366 bool
367 has_c_linkage (tree decl)
369 return DECL_EXTERN_C_P (decl);