c-lang.c (LANG_HOOKS_NAME): New.
[gcc.git] / gcc / c-lang.c
1 /* Language-specific hook definitions for C front end.
2 Copyright (C) 1991, 1995, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "tree.h"
26 #include "tree-inline.h"
27 #include "function.h"
28 #include "input.h"
29 #include "toplev.h"
30 #include "diagnostic.h"
31 #include "output.h"
32 #include "flags.h"
33 #include "ggc.h"
34 #include "rtl.h"
35 #include "expr.h"
36 #include "c-tree.h"
37 #include "c-lex.h"
38 #include "cpplib.h"
39 #include "insn-config.h"
40 #include "integrate.h"
41 #include "langhooks.h"
42 #include "langhooks-def.h"
43
44 static int c_tree_printer PARAMS ((output_buffer *));
45 static int c_missing_noreturn_ok_p PARAMS ((tree));
46 static void c_init PARAMS ((void));
47 static void c_init_options PARAMS ((void));
48 static void c_post_options PARAMS ((void));
49 static int c_disregard_inline_limits PARAMS ((tree));
50 static int c_cannot_inline_tree_fn PARAMS ((tree *));
51
52 #undef LANG_HOOKS_NAME
53 #define LANG_HOOKS_NAME "GNU C"
54 #undef LANG_HOOKS_INIT
55 #define LANG_HOOKS_INIT c_init
56 #undef LANG_HOOKS_INIT_OPTIONS
57 #define LANG_HOOKS_INIT_OPTIONS c_init_options
58 #undef LANG_HOOKS_DECODE_OPTION
59 #define LANG_HOOKS_DECODE_OPTION c_decode_option
60 #undef LANG_HOOKS_POST_OPTIONS
61 #define LANG_HOOKS_POST_OPTIONS c_post_options
62 #undef LANG_HOOKS_GET_ALIAS_SET
63 #define LANG_HOOKS_GET_ALIAS_SET c_common_get_alias_set
64
65 #undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
66 #define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
67 c_cannot_inline_tree_fn
68 #undef LANG_HOOKS_TREE_INLINING_DISREGARD_INLINE_LIMITS
69 #define LANG_HOOKS_TREE_INLINING_DISREGARD_INLINE_LIMITS \
70 c_disregard_inline_limits
71 #undef LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P
72 #define LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P \
73 anon_aggr_type_p
74
75 /* Each front end provides its own. */
76 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
77
78 /* Post-switch processing. */
79 static void
80 c_post_options ()
81 {
82 cpp_post_options (parse_in);
83
84 /* Use tree inlining if possible. Function instrumentation is only
85 done in the RTL level, so we disable tree inlining. */
86 if (! flag_instrument_function_entry_exit)
87 {
88 if (!flag_no_inline)
89 {
90 flag_inline_trees = 1;
91 flag_no_inline = 1;
92 }
93 if (flag_inline_functions)
94 {
95 flag_inline_trees = 2;
96 flag_inline_functions = 0;
97 }
98 }
99 }
100
101 static void
102 c_init_options ()
103 {
104 parse_in = cpp_create_reader (ident_hash, CLK_GNUC89);
105
106 /* Mark as "unspecified". */
107 flag_bounds_check = -1;
108 }
109
110 static void
111 c_init ()
112 {
113 c_common_lang_init ();
114
115 /* If still unspecified, make it match -std=c99
116 (allowing for -pedantic-errors). */
117 if (mesg_implicit_function_declaration < 0)
118 {
119 if (flag_isoc99)
120 mesg_implicit_function_declaration = flag_pedantic_errors ? 2 : 1;
121 else
122 mesg_implicit_function_declaration = 0;
123 }
124
125 save_lang_status = &push_c_function_context;
126 restore_lang_status = &pop_c_function_context;
127 mark_lang_status = &mark_c_function_context;
128 lang_expand_expr = &c_expand_expr;
129 lang_safe_from_p = &c_safe_from_p;
130 diagnostic_format_decoder (global_dc) = &c_tree_printer;
131 lang_expand_decl_stmt = &c_expand_decl_stmt;
132 lang_missing_noreturn_ok_p = &c_missing_noreturn_ok_p;
133
134 c_parse_init ();
135 }
136
137 void
138 print_lang_statistics ()
139 {
140 }
141
142 /* used by print-tree.c */
143
144 void
145 lang_print_xnode (file, node, indent)
146 FILE *file ATTRIBUTE_UNUSED;
147 tree node ATTRIBUTE_UNUSED;
148 int indent ATTRIBUTE_UNUSED;
149 {
150 }
151
152 /* Used by c-lex.c, but only for objc. */
153
154 tree
155 lookup_interface (arg)
156 tree arg ATTRIBUTE_UNUSED;
157 {
158 return 0;
159 }
160
161 tree
162 is_class_name (arg)
163 tree arg ATTRIBUTE_UNUSED;
164 {
165 return 0;
166 }
167
168 void
169 maybe_objc_check_decl (decl)
170 tree decl ATTRIBUTE_UNUSED;
171 {
172 }
173
174 int
175 maybe_objc_comptypes (lhs, rhs, reflexive)
176 tree lhs ATTRIBUTE_UNUSED;
177 tree rhs ATTRIBUTE_UNUSED;
178 int reflexive ATTRIBUTE_UNUSED;
179 {
180 return -1;
181 }
182
183 tree
184 maybe_building_objc_message_expr ()
185 {
186 return 0;
187 }
188
189 int
190 recognize_objc_keyword ()
191 {
192 return 0;
193 }
194
195 /* Used by c-typeck.c (build_external_ref), but only for objc. */
196
197 tree
198 lookup_objc_ivar (id)
199 tree id ATTRIBUTE_UNUSED;
200 {
201 return 0;
202 }
203
204 #if !defined(ASM_OUTPUT_CONSTRUCTOR) || !defined(ASM_OUTPUT_DESTRUCTOR)
205 extern tree static_ctors;
206 extern tree static_dtors;
207
208 static tree start_cdtor PARAMS ((int));
209 static void finish_cdtor PARAMS ((tree));
210
211 static tree
212 start_cdtor (method_type)
213 int method_type;
214 {
215 tree fnname = get_file_function_name (method_type);
216 tree void_list_node_1 = build_tree_list (NULL_TREE, void_type_node);
217 tree body;
218
219 start_function (void_list_node_1,
220 build_nt (CALL_EXPR, fnname,
221 tree_cons (NULL_TREE, NULL_TREE, void_list_node_1),
222 NULL_TREE),
223 NULL_TREE);
224 store_parm_decls ();
225
226 current_function_cannot_inline
227 = "static constructors and destructors cannot be inlined";
228
229 body = c_begin_compound_stmt ();
230
231 pushlevel (0);
232 clear_last_expr ();
233 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
234
235 return body;
236 }
237
238 static void
239 finish_cdtor (body)
240 tree body;
241 {
242 tree scope;
243 tree block;
244
245 scope = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
246 block = poplevel (0, 0, 0);
247 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope)) = block;
248 SCOPE_STMT_BLOCK (TREE_VALUE (scope)) = block;
249
250 RECHAIN_STMTS (body, COMPOUND_BODY (body));
251
252 finish_function (0);
253 }
254 #endif
255
256 /* Called at end of parsing, but before end-of-file processing. */
257
258 void
259 finish_file ()
260 {
261 #ifndef ASM_OUTPUT_CONSTRUCTOR
262 if (static_ctors)
263 {
264 tree body = start_cdtor ('I');
265
266 for (; static_ctors; static_ctors = TREE_CHAIN (static_ctors))
267 c_expand_expr_stmt (build_function_call (TREE_VALUE (static_ctors),
268 NULL_TREE));
269
270 finish_cdtor (body);
271 }
272 #endif
273 #ifndef ASM_OUTPUT_DESTRUCTOR
274 if (static_dtors)
275 {
276 tree body = start_cdtor ('D');
277
278 for (; static_dtors; static_dtors = TREE_CHAIN (static_dtors))
279 c_expand_expr_stmt (build_function_call (TREE_VALUE (static_dtors),
280 NULL_TREE));
281
282 finish_cdtor (body);
283 }
284 #endif
285
286 if (back_end_hook)
287 (*back_end_hook) (getdecls ());
288
289 {
290 int flags;
291 FILE *stream = dump_begin (TDI_all, &flags);
292
293 if (stream)
294 {
295 dump_node (getdecls (), flags & ~TDF_SLIM, stream);
296 dump_end (TDI_all, stream);
297 }
298 }
299 }
300
301 /* Called during diagnostic message formatting process to print a
302 source-level entity onto BUFFER. The meaning of the format specifiers
303 is as follows:
304 %D: a general decl,
305 %F: a function declaration,
306 %T: a type.
307
308 These format specifiers form a subset of the format specifiers set used
309 by the C++ front-end.
310 Please notice when called, the `%' part was already skipped by the
311 diagnostic machinery. */
312 static int
313 c_tree_printer (buffer)
314 output_buffer *buffer;
315 {
316 tree t = va_arg (output_buffer_format_args (buffer), tree);
317
318 switch (*output_buffer_text_cursor (buffer))
319 {
320 case 'D':
321 case 'F':
322 case 'T':
323 {
324 const char *n = DECL_NAME (t)
325 ? (*decl_printable_name) (t, 2)
326 : "({anonymous})";
327 output_add_string (buffer, n);
328 }
329 return 1;
330
331 default:
332 return 0;
333 }
334 }
335
336 static int
337 c_missing_noreturn_ok_p (decl)
338 tree decl;
339 {
340 /* A missing noreturn is not ok for freestanding implementations and
341 ok for the `main' function in hosted implementations. */
342 return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl));
343 }
344
345 /* We want to inline `extern inline' functions even if this would
346 violate inlining limits. Some glibc and linux constructs depend on
347 such functions always being inlined when optimizing. */
348
349 static int
350 c_disregard_inline_limits (fn)
351 tree fn;
352 {
353 return DECL_DECLARED_INLINE_P (fn) && DECL_EXTERNAL (fn);
354 }
355
356 static tree inline_forbidden_p PARAMS ((tree *, int *, void *));
357
358 static tree
359 inline_forbidden_p (nodep, walk_subtrees, fn)
360 tree *nodep;
361 int *walk_subtrees ATTRIBUTE_UNUSED;
362 void *fn;
363 {
364 tree node = *nodep;
365 tree t;
366
367 switch (TREE_CODE (node))
368 {
369 case CALL_EXPR:
370 t = get_callee_fndecl (node);
371
372 if (! t)
373 break;
374
375 /* We cannot inline functions that call setjmp. */
376 if (setjmp_call_p (t))
377 return node;
378
379 switch (DECL_FUNCTION_CODE (t))
380 {
381 /* We cannot inline functions that take a variable number of
382 arguments. */
383 case BUILT_IN_VARARGS_START:
384 case BUILT_IN_STDARG_START:
385 #if 0
386 /* Functions that need information about the address of the
387 caller can't (shouldn't?) be inlined. */
388 case BUILT_IN_RETURN_ADDRESS:
389 #endif
390 return node;
391
392 default:
393 break;
394 }
395
396 break;
397
398 case DECL_STMT:
399 /* We cannot inline functions that contain other functions. */
400 if (TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
401 && DECL_INITIAL (TREE_OPERAND (node, 0)))
402 return node;
403 break;
404
405 case GOTO_STMT:
406 case GOTO_EXPR:
407 t = TREE_OPERAND (node, 0);
408
409 /* We will not inline a function which uses computed goto. The
410 addresses of its local labels, which may be tucked into
411 global storage, are of course not constant across
412 instantiations, which causes unexpected behaviour. */
413 if (TREE_CODE (t) != LABEL_DECL)
414 return node;
415
416 /* We cannot inline a nested function that jumps to a nonlocal
417 label. */
418 if (TREE_CODE (t) == LABEL_DECL
419 && DECL_CONTEXT (t) && DECL_CONTEXT (t) != fn)
420 return node;
421
422 break;
423
424 default:
425 break;
426 }
427
428 return NULL_TREE;
429 }
430
431 static int
432 c_cannot_inline_tree_fn (fnp)
433 tree *fnp;
434 {
435 tree fn = *fnp;
436 tree t;
437
438 if (! function_attribute_inlinable_p (fn))
439 {
440 DECL_UNINLINABLE (fn) = 1;
441 return 1;
442 }
443
444 /* If a function has pending sizes, we must not defer its
445 compilation, and we can't inline it as a tree. */
446 if (fn == current_function_decl)
447 {
448 t = get_pending_sizes ();
449 put_pending_sizes (t);
450
451 if (t)
452 {
453 DECL_UNINLINABLE (fn) = 1;
454 return 1;
455 }
456 }
457
458 if (DECL_CONTEXT (fn))
459 {
460 /* If a nested function has pending sizes, we may have already
461 saved them. */
462 if (DECL_LANG_SPECIFIC (fn)->pending_sizes)
463 {
464 DECL_UNINLINABLE (fn) = 1;
465 return 1;
466 }
467 }
468 else
469 {
470 /* We rely on the fact that this function is called upfront,
471 just before we start expanding a function. If FN is active
472 (i.e., it's the current_function_decl or a parent thereof),
473 we have to walk FN's saved tree. Otherwise, we can safely
474 assume we have done it before and, if we didn't mark it as
475 uninlinable (in which case we wouldn't have been called), it
476 is inlinable. Unfortunately, this strategy doesn't work for
477 nested functions, because they're only expanded as part of
478 their enclosing functions, so the inlinability test comes in
479 late. */
480 t = current_function_decl;
481
482 while (t && t != fn)
483 t = DECL_CONTEXT (t);
484 if (! t)
485 return 0;
486 }
487
488 if (walk_tree (&DECL_SAVED_TREE (fn), inline_forbidden_p, fn, NULL))
489 {
490 DECL_UNINLINABLE (fn) = 1;
491 return 1;
492 }
493
494 return 0;
495 }