OSDN Git Service

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