OSDN Git Service

PR c++/10538, PR c/5582
[pf3gnuchains/gcc-fork.git] / gcc / cp / cp-lang.c
1 /* Language-dependent hooks for C++.
2    Copyright 2001, 2002 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 ok_to_generate_alias_set_for_type (tree);
39 static bool cxx_warn_unused_global_decl (tree);
40 static tree cp_expr_size (tree);
41 static size_t cp_tree_size (enum tree_code);
42 static bool cp_var_mod_type_p (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_SAFE_FROM_P
74 #define LANG_HOOKS_SAFE_FROM_P c_safe_from_p
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_UNSAVE_EXPR_NOW
80 #define LANG_HOOKS_UNSAVE_EXPR_NOW cxx_unsave_expr_now
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_WARN_UNUSED_GLOBAL_DECL
106 #define LANG_HOOKS_WARN_UNUSED_GLOBAL_DECL cxx_warn_unused_global_decl
107 #undef LANG_HOOKS_WRITE_GLOBALS
108 #define LANG_HOOKS_WRITE_GLOBALS lhd_do_nothing
109 #undef LANG_HOOKS_DECL_UNINIT
110 #define LANG_HOOKS_DECL_UNINIT c_decl_uninit
111
112
113 #undef LANG_HOOKS_FUNCTION_INIT
114 #define LANG_HOOKS_FUNCTION_INIT cxx_push_function_context
115 #undef LANG_HOOKS_FUNCTION_FINAL
116 #define LANG_HOOKS_FUNCTION_FINAL cxx_pop_function_context
117
118 /* Attribute hooks.  */
119 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
120 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE c_common_attribute_table
121 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
122 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE c_common_format_attribute_table
123 #undef LANG_HOOKS_ATTRIBUTE_TABLE
124 #define LANG_HOOKS_ATTRIBUTE_TABLE cxx_attribute_table
125
126 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
127 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES \
128   cp_walk_subtrees
129 #undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
130 #define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
131   cp_cannot_inline_tree_fn
132 #undef LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS
133 #define LANG_HOOKS_TREE_INLINING_ADD_PENDING_FN_DECLS \
134   cp_add_pending_fn_decls
135 #undef LANG_HOOKS_TREE_INLINING_TREE_CHAIN_MATTERS_P
136 #define LANG_HOOKS_TREE_INLINING_TREE_CHAIN_MATTERS_P \
137   cp_is_overload_p
138 #undef LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P
139 #define LANG_HOOKS_TREE_INLINING_AUTO_VAR_IN_FN_P \
140   cp_auto_var_in_fn_p
141 #undef LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING
142 #define LANG_HOOKS_TREE_INLINING_COPY_RES_DECL_FOR_INLINING \
143   cp_copy_res_decl_for_inlining
144 #undef LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P
145 #define LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P anon_aggr_type_p
146 #undef LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P
147 #define LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P cp_var_mod_type_p
148 #undef LANG_HOOKS_TREE_INLINING_START_INLINING
149 #define LANG_HOOKS_TREE_INLINING_START_INLINING cp_start_inlining
150 #undef LANG_HOOKS_TREE_INLINING_END_INLINING
151 #define LANG_HOOKS_TREE_INLINING_END_INLINING cp_end_inlining
152 #undef LANG_HOOKS_TREE_INLINING_ESTIMATE_NUM_INSNS
153 #define LANG_HOOKS_TREE_INLINING_ESTIMATE_NUM_INSNS c_estimate_num_insns
154 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
155 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN cp_dump_tree
156 #undef LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN
157 #define LANG_HOOKS_TREE_DUMP_TYPE_QUALS_FN cp_type_quals
158 #undef LANG_HOOKS_EXPR_SIZE
159 #define LANG_HOOKS_EXPR_SIZE cp_expr_size
160
161 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
162 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION expand_body
163 #undef LANG_HOOKS_CALLGRAPH_LOWER_FUNCTION
164 #define LANG_HOOKS_CALLGRAPH_LOWER_FUNCTION lower_function
165
166 #undef LANG_HOOKS_MAKE_TYPE
167 #define LANG_HOOKS_MAKE_TYPE cxx_make_type
168 #undef LANG_HOOKS_TYPE_FOR_MODE
169 #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
170 #undef LANG_HOOKS_TYPE_FOR_SIZE
171 #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
172 #undef LANG_HOOKS_SIGNED_TYPE
173 #define LANG_HOOKS_SIGNED_TYPE c_common_signed_type
174 #undef LANG_HOOKS_UNSIGNED_TYPE
175 #define LANG_HOOKS_UNSIGNED_TYPE c_common_unsigned_type
176 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
177 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE c_common_signed_or_unsigned_type
178 #undef LANG_HOOKS_INCOMPLETE_TYPE_ERROR
179 #define LANG_HOOKS_INCOMPLETE_TYPE_ERROR cxx_incomplete_type_error
180 #undef LANG_HOOKS_TYPE_PROMOTES_TO
181 #define LANG_HOOKS_TYPE_PROMOTES_TO cxx_type_promotes_to
182
183 /* Each front end provides its own hooks, for toplev.c.  */
184 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
185
186 /* Tree code classes.  */
187
188 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
189
190 const char tree_code_type[] = {
191 #include "tree.def"
192   'x',
193 #include "c-common.def"
194   'x',
195 #include "cp-tree.def"
196 };
197 #undef DEFTREECODE
198
199 /* Table indexed by tree code giving number of expression
200    operands beyond the fixed part of the node structure.
201    Not used for types or decls.  */
202
203 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
204
205 const unsigned char tree_code_length[] = {
206 #include "tree.def"
207   0,
208 #include "c-common.def"
209   0,
210 #include "cp-tree.def"
211 };
212 #undef DEFTREECODE
213
214 /* Names of tree components.
215    Used for printing out the tree and error messages.  */
216 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
217
218 const char *const tree_code_name[] = {
219 #include "tree.def"
220   "@@dummy",
221 #include "c-common.def"
222   "@@dummy",
223 #include "cp-tree.def"
224 };
225 #undef DEFTREECODE
226
227 /* Check if a C++ type is safe for aliasing.
228    Return TRUE if T safe for aliasing FALSE otherwise.  */
229
230 static bool
231 ok_to_generate_alias_set_for_type (tree t)
232 {
233   if (TYPE_PTRMEMFUNC_P (t))
234     return true;
235   if (AGGREGATE_TYPE_P (t))
236     {
237       if ((TREE_CODE (t) == RECORD_TYPE) || (TREE_CODE (t) == UNION_TYPE))
238         {
239           tree fields;
240           /* Backend-created structs are safe.  */
241           if (! CLASS_TYPE_P (t))
242             return true;
243           /* PODs are safe.  */
244           if (! CLASSTYPE_NON_POD_P(t))
245             return true;
246           /* Classes with virtual baseclasses are not.  */
247           if (TYPE_USES_VIRTUAL_BASECLASSES (t))
248             return false;
249           /* Recursively check the base classes.  */
250           if (TYPE_BINFO (t) != NULL && TYPE_BINFO_BASETYPES (t) != NULL)
251             {
252               int i;
253               for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t)); i++)
254                 {
255                   tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
256                   if (!ok_to_generate_alias_set_for_type (BINFO_TYPE (binfo)))
257                     return false;
258                 }
259             }
260           /* Check all the fields.  */
261           for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
262             {
263               if (TREE_CODE (fields) != FIELD_DECL)
264                 continue;
265               if (! ok_to_generate_alias_set_for_type (TREE_TYPE (fields)))
266                 return false;
267             }
268           return true;
269         }
270       else if (TREE_CODE (t) == ARRAY_TYPE)
271         return ok_to_generate_alias_set_for_type (TREE_TYPE (t));
272       else
273         /* This should never happen, we dealt with all the aggregate
274            types that can appear in C++ above.  */
275         abort ();
276     }
277   else
278     return true;
279 }
280
281 /* Special routine to get the alias set for C++.  */
282
283 static HOST_WIDE_INT
284 cxx_get_alias_set (tree t)
285 {
286   
287   if (/* It's not yet safe to use alias sets for some classes in C++.  */
288       !ok_to_generate_alias_set_for_type (t)
289       /* Nor is it safe to use alias sets for pointers-to-member
290          functions, due to the fact that there may be more than one
291          RECORD_TYPE type corresponding to the same pointer-to-member
292          type.  */
293       || TYPE_PTRMEMFUNC_P (t))
294     return 0;
295
296   return c_common_get_alias_set (t);
297 }
298
299 /* Called from check_global_declarations.  */
300
301 static bool
302 cxx_warn_unused_global_decl (tree decl)
303 {
304   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
305     return false;
306   if (DECL_IN_SYSTEM_HEADER (decl))
307     return false;
308
309   /* Const variables take the place of #defines in C++.  */
310   if (TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl))
311     return false;
312
313   return true;
314 }
315
316 /* Langhook for expr_size: Tell the backend that the value of an expression
317    of non-POD class type does not include any tail padding; a derived class
318    might have allocated something there.  */
319
320 static tree
321 cp_expr_size (tree exp)
322 {
323   if (CLASS_TYPE_P (TREE_TYPE (exp)))
324     {
325       /* The backend should not be interested in the size of an expression
326          of a type with both of these set; all copies of such types must go
327          through a constructor or assignment op.  */
328       if (TYPE_HAS_COMPLEX_INIT_REF (TREE_TYPE (exp))
329           && TYPE_HAS_COMPLEX_ASSIGN_REF (TREE_TYPE (exp))
330           /* But storing a CONSTRUCTOR isn't a copy.  */
331           && TREE_CODE (exp) != CONSTRUCTOR)
332         abort ();
333       /* This would be wrong for a type with virtual bases, but they are
334          caught by the abort above.  */
335       return CLASSTYPE_SIZE_UNIT (TREE_TYPE (exp));
336     }
337   else
338     /* Use the default code.  */
339     return lhd_expr_size (exp);
340 }
341
342 /* Langhook for tree_size: determine size of our 'x' and 'c' nodes.  */
343 static size_t
344 cp_tree_size (enum tree_code code)
345 {
346   switch (code)
347     {
348     case PTRMEM_CST:            return sizeof (struct ptrmem_cst);
349     case BASELINK:              return sizeof (struct tree_baselink);
350     case TEMPLATE_PARM_INDEX:   return sizeof (template_parm_index);
351     case DEFAULT_ARG:           return sizeof (struct tree_default_arg);
352     case OVERLOAD:              return sizeof (struct tree_overload);
353     case WRAPPER:               return sizeof (struct tree_wrapper);
354     default:
355       abort ();
356     }
357   /* NOTREACHED */
358 }
359
360 /* Returns true if T is a variably modified type, in the sense of C99.
361    This routine needs only check cases that cannot be handled by the
362    language-independent logic in tree-inline.c.  */
363
364 static bool
365 cp_var_mod_type_p (tree type)
366 {
367   /* If TYPE is a pointer-to-member, it is variably modified if either
368      the class or the member are variably modified.  */
369   if (TYPE_PTR_TO_MEMBER_P (type))
370     return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
371             || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type)));
372
373   /* All other types are not variably modified.  */
374   return false;
375 }
376
377 /* Stub routine to tell people that this doesn't work yet.  */
378 void
379 c_reset_state (void)
380 {
381   sorry ("inter-module optimisations not implemented yet");
382 }
383
384 /* Construct a C++-aware pretty-printer for CONTEXT.  It is assumed
385    that CONTEXT->printer is an already constructed basic pretty_printer.  */
386 static void
387 cxx_initialize_diagnostics (diagnostic_context *context)
388 {
389   pretty_printer *base = context->printer;
390   cxx_pretty_printer *pp = xmalloc (sizeof (cxx_pretty_printer));
391   memcpy (pp_base (pp), base, sizeof (pretty_printer));
392   pp_cxx_pretty_printer_init (pp);
393   context->printer = (pretty_printer *) pp;
394
395   /* It is safe to free this object because it was previously malloc()'d.  */
396   free (base);
397 }