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