OSDN Git Service

* tree.def (VTABLE_REF): Remove.
[pf3gnuchains/gcc-fork.git] / gcc / cp / cp-lang.c
1 /* Language-dependent hooks for C++.
2    Copyright 2001, 2002, 2004 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 #include "diagnostic.h"
33 #include "cxx-pretty-print.h"
34
35 enum c_language_kind c_language = clk_cxx;
36
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);
42 static int cxx_types_compatible_p (tree, tree);
43 static void cxx_initialize_diagnostics (diagnostic_context *);
44
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_SAFE_FROM_P
76 #define LANG_HOOKS_SAFE_FROM_P c_safe_from_p
77 #undef LANG_HOOKS_PARSE_FILE
78 #define LANG_HOOKS_PARSE_FILE c_common_parse_file
79 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
80 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL cxx_dup_lang_specific_decl
81 #undef LANG_HOOKS_MAYBE_BUILD_CLEANUP
82 #define LANG_HOOKS_MAYBE_BUILD_CLEANUP cxx_maybe_build_cleanup
83 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
84 #define LANG_HOOKS_TRUTHVALUE_CONVERSION c_common_truthvalue_conversion
85 #undef LANG_HOOKS_UNSAFE_FOR_REEVAL
86 #define LANG_HOOKS_UNSAFE_FOR_REEVAL c_common_unsafe_for_reeval
87 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
88 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME mangle_decl
89 #undef LANG_HOOKS_MARK_ADDRESSABLE
90 #define LANG_HOOKS_MARK_ADDRESSABLE cxx_mark_addressable
91 #undef LANG_HOOKS_PRINT_STATISTICS
92 #define LANG_HOOKS_PRINT_STATISTICS cxx_print_statistics
93 #undef LANG_HOOKS_PRINT_XNODE
94 #define LANG_HOOKS_PRINT_XNODE cxx_print_xnode
95 #undef LANG_HOOKS_PRINT_DECL
96 #define LANG_HOOKS_PRINT_DECL cxx_print_decl
97 #undef LANG_HOOKS_PRINT_TYPE
98 #define LANG_HOOKS_PRINT_TYPE cxx_print_type
99 #undef LANG_HOOKS_PRINT_IDENTIFIER
100 #define LANG_HOOKS_PRINT_IDENTIFIER cxx_print_identifier
101 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
102 #define LANG_HOOKS_DECL_PRINTABLE_NAME  cxx_printable_name
103 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
104 #define LANG_HOOKS_PRINT_ERROR_FUNCTION cxx_print_error_function
105 #undef LANG_HOOKS_PUSHLEVEL
106 #define LANG_HOOKS_PUSHLEVEL lhd_do_nothing_i
107 #undef LANG_HOOKS_POPLEVEL
108 #define LANG_HOOKS_POPLEVEL lhd_do_nothing_iii_return_null_tree
109 #undef LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL
110 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL cxx_warn_unused_global_decl
111 #undef LANG_HOOKS_WRITE_GLOBALS
112 #define LANG_HOOKS_WRITE_GLOBALS lhd_do_nothing
113 #undef LANG_HOOKS_UPDATE_DECL_AFTER_SAVING
114 #define LANG_HOOKS_UPDATE_DECL_AFTER_SAVING cp_update_decl_after_saving
115
116
117 #undef LANG_HOOKS_FUNCTION_INIT
118 #define LANG_HOOKS_FUNCTION_INIT cxx_push_function_context
119 #undef LANG_HOOKS_FUNCTION_FINAL
120 #define LANG_HOOKS_FUNCTION_FINAL cxx_pop_function_context
121 #undef LANG_HOOKS_FUNCTION_MISSING_NORETURN_OK_P
122 #define LANG_HOOKS_FUNCTION_MISSING_NORETURN_OK_P cp_missing_noreturn_ok_p
123
124 /* Attribute hooks.  */
125 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
126 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE c_common_attribute_table
127 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
128 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE c_common_format_attribute_table
129 #undef LANG_HOOKS_ATTRIBUTE_TABLE
130 #define LANG_HOOKS_ATTRIBUTE_TABLE cxx_attribute_table
131
132 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
133 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES \
134   cp_walk_subtrees
135 #undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
136 #define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
137   cp_cannot_inline_tree_fn
138 #undef LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS
139 #define LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS \
140   cp_add_pending_fn_decls
141 #undef LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P
142 #define LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P \
143   cp_auto_var_in_fn_p
144 #undef LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING
145 #define LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING \
146   cp_copy_res_decl_for_inlining
147 #undef LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P
148 #define LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P anon_aggr_type_p
149 #undef LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P
150 #define LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P cp_var_mod_type_p
151 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
152 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN cp_dump_tree
153 #undef LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN
154 #define LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN cp_type_quals
155 #undef LANG_HOOKS_EXPR_SIZE
156 #define LANG_HOOKS_EXPR_SIZE cp_expr_size
157
158 #undef LANG_HOOKS_CALLGRAPH_ANALYZE_EXPR
159 #define LANG_HOOKS_CALLGRAPH_ANALYZE_EXPR cxx_callgraph_analyze_expr
160 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
161 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION expand_body
162
163 #undef LANG_HOOKS_MAKE_TYPE
164 #define LANG_HOOKS_MAKE_TYPE cxx_make_type
165 #undef LANG_HOOKS_TYPE_FOR_MODE
166 #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
167 #undef LANG_HOOKS_TYPE_FOR_SIZE
168 #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
169 #undef LANG_HOOKS_SIGNED_TYPE
170 #define LANG_HOOKS_SIGNED_TYPE c_common_signed_type
171 #undef LANG_HOOKS_UNSIGNED_TYPE
172 #define LANG_HOOKS_UNSIGNED_TYPE c_common_unsigned_type
173 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
174 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE c_common_signed_or_unsigned_type
175 #undef LANG_HOOKS_INCOMPLETE_TYPE_ERROR
176 #define LANG_HOOKS_INCOMPLETE_TYPE_ERROR cxx_incomplete_type_error
177 #undef LANG_HOOKS_TYPE_PROMOTES_TO
178 #define LANG_HOOKS_TYPE_PROMOTES_TO cxx_type_promotes_to
179 #undef LANG_HOOKS_TYPES_COMPATIBLE_P
180 #define LANG_HOOKS_TYPES_COMPATIBLE_P cxx_types_compatible_p
181 #undef LANG_HOOKS_REGISTER_BUILTIN_TYPE
182 #define LANG_HOOKS_REGISTER_BUILTIN_TYPE c_register_builtin_type
183 #undef LANG_HOOKS_GIMPLIFY_EXPR
184 #define LANG_HOOKS_GIMPLIFY_EXPR cp_gimplify_expr
185 #undef LANG_HOOKS_FOLD_OBJ_TYPE_REF
186 #define LANG_HOOKS_FOLD_OBJ_TYPE_REF cp_fold_obj_type_ref
187
188 /* Each front end provides its own hooks, for toplev.c.  */
189 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
190
191 /* Tree code classes.  */
192
193 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
194
195 const char tree_code_type[] = {
196 #include "tree.def"
197   'x',
198 #include "c-common.def"
199   'x',
200 #include "cp-tree.def"
201 };
202 #undef DEFTREECODE
203
204 /* Table indexed by tree code giving number of expression
205    operands beyond the fixed part of the node structure.
206    Not used for types or decls.  */
207
208 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
209
210 const unsigned char tree_code_length[] = {
211 #include "tree.def"
212   0,
213 #include "c-common.def"
214   0,
215 #include "cp-tree.def"
216 };
217 #undef DEFTREECODE
218
219 /* Names of tree components.
220    Used for printing out the tree and error messages.  */
221 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
222
223 const char *const tree_code_name[] = {
224 #include "tree.def"
225   "@@dummy",
226 #include "c-common.def"
227   "@@dummy",
228 #include "cp-tree.def"
229 };
230 #undef DEFTREECODE
231
232 /* Special routine to get the alias set for C++.  */
233
234 static HOST_WIDE_INT
235 cxx_get_alias_set (tree t)
236 {
237   if (IS_FAKE_BASE_TYPE (t))
238     /* The base variant of a type must be in the same alias set as the
239        complete type.  */
240     return get_alias_set (TYPE_CONTEXT (t));
241   
242   /* Punt on PMFs until we canonicalize functions properly.  */
243   if (TYPE_PTRMEMFUNC_P (t))
244     return 0;
245
246   return c_common_get_alias_set (t);
247 }
248
249 /* Called from check_global_declarations.  */
250
251 static bool
252 cxx_warn_unused_global_decl (tree decl)
253 {
254   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
255     return false;
256   if (DECL_IN_SYSTEM_HEADER (decl))
257     return false;
258
259   /* Const variables take the place of #defines in C++.  */
260   if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
261     return false;
262
263   return true;
264 }
265
266 /* Langhook for expr_size: Tell the backend that the value of an expression
267    of non-POD class type does not include any tail padding; a derived class
268    might have allocated something there.  */
269
270 static tree
271 cp_expr_size (tree exp)
272 {
273   if (CLASS_TYPE_P (TREE_TYPE (exp)))
274     {
275       /* The backend should not be interested in the size of an expression
276          of a type with both of these set; all copies of such types must go
277          through a constructor or assignment op.  */
278       if (TYPE_HAS_COMPLEX_INIT_REF (TREE_TYPE (exp))
279           && TYPE_HAS_COMPLEX_ASSIGN_REF (TREE_TYPE (exp))
280           /* But storing a CONSTRUCTOR isn't a copy.  */
281           && TREE_CODE (exp) != CONSTRUCTOR)
282         abort ();
283       /* This would be wrong for a type with virtual bases, but they are
284          caught by the abort above.  */
285       return (is_empty_class (TREE_TYPE (exp))
286               ? size_zero_node 
287               : CLASSTYPE_SIZE_UNIT (TREE_TYPE (exp)));
288     }
289   else
290     /* Use the default code.  */
291     return lhd_expr_size (exp);
292 }
293
294 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes.  */
295 static size_t
296 cp_tree_size (enum tree_code code)
297 {
298   switch (code)
299     {
300     case PTRMEM_CST:            return sizeof (struct ptrmem_cst);
301     case BASELINK:              return sizeof (struct tree_baselink);
302     case TEMPLATE_PARM_INDEX:   return sizeof (template_parm_index);
303     case DEFAULT_ARG:           return sizeof (struct tree_default_arg);
304     case OVERLOAD:              return sizeof (struct tree_overload);
305     default:
306       abort ();
307     }
308   /* NOTREACHED */
309 }
310
311 /* Returns true if T is a variably modified type, in the sense of C99.
312    This routine needs only check cases that cannot be handled by the
313    language-independent logic in tree-inline.c.  */
314
315 static bool
316 cp_var_mod_type_p (tree type)
317 {
318   /* If TYPE is a pointer-to-member, it is variably modified if either
319      the class or the member are variably modified.  */
320   if (TYPE_PTR_TO_MEMBER_P (type))
321     return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
322             || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type)));
323
324   /* All other types are not variably modified.  */
325   return false;
326 }
327
328 static int cxx_types_compatible_p (tree x, tree y)
329 {
330   return same_type_ignoring_top_level_qualifiers_p (x, y);
331 }
332
333 /* Construct a C++-aware pretty-printer for CONTEXT.  It is assumed
334    that CONTEXT->printer is an already constructed basic pretty_printer.  */
335 static void
336 cxx_initialize_diagnostics (diagnostic_context *context)
337 {
338   pretty_printer *base = context->printer;
339   cxx_pretty_printer *pp = xmalloc (sizeof (cxx_pretty_printer));
340   memcpy (pp_base (pp), base, sizeof (pretty_printer));
341   pp_cxx_pretty_printer_init (pp);
342   context->printer = (pretty_printer *) pp;
343
344   /* It is safe to free this object because it was previously malloc()'d.  */
345   free (base);
346 }
347
348 /* Stubs to keep c-opts.c happy.  */
349 void
350 push_file_scope (void)
351 {
352 }
353
354 void
355 pop_file_scope (void)
356 {
357 }
358
359 /* c-pragma.c needs to query whether a decl has extern "C" linkage.  */
360 bool
361 has_c_linkage (tree decl)
362 {
363   return DECL_EXTERN_C_P (decl);
364 }