OSDN Git Service

* c-common.c: Include c-lex.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 #include "varray.h"
42 #include "langhooks.h"
43 #include "langhooks-def.h"
44
45 static int c_tree_printer PARAMS ((output_buffer *));
46 static int c_missing_noreturn_ok_p PARAMS ((tree));
47 static const char *c_init PARAMS ((const char *));
48 static void c_init_options PARAMS ((void));
49 static void c_post_options PARAMS ((void));
50 static int c_disregard_inline_limits PARAMS ((tree));
51 static int c_cannot_inline_tree_fn PARAMS ((tree *));
52
53 #undef LANG_HOOKS_NAME
54 #define LANG_HOOKS_NAME "GNU C"
55 #undef LANG_HOOKS_INIT
56 #define LANG_HOOKS_INIT c_init
57 #undef LANG_HOOKS_INIT_OPTIONS
58 #define LANG_HOOKS_INIT_OPTIONS c_init_options
59 #undef LANG_HOOKS_DECODE_OPTION
60 #define LANG_HOOKS_DECODE_OPTION c_decode_option
61 #undef LANG_HOOKS_POST_OPTIONS
62 #define LANG_HOOKS_POST_OPTIONS c_post_options
63 #undef LANG_HOOKS_GET_ALIAS_SET
64 #define LANG_HOOKS_GET_ALIAS_SET c_common_get_alias_set
65 #undef LANG_HOOKS_PRINT_IDENTIFIER
66 #define LANG_HOOKS_PRINT_IDENTIFIER c_print_identifier
67 #undef LANG_HOOKS_SET_YYDEBUG
68 #define LANG_HOOKS_SET_YYDEBUG c_set_yydebug
69
70 #undef LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN
71 #define LANG_HOOKS_TREE_INLINING_CANNOT_INLINE_TREE_FN \
72   c_cannot_inline_tree_fn
73 #undef LANG_HOOKS_TREE_INLINING_DISREGARD_INLINE_LIMITS
74 #define LANG_HOOKS_TREE_INLINING_DISREGARD_INLINE_LIMITS \
75   c_disregard_inline_limits
76 #undef LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P
77 #define LANG_HOOKS_TREE_INLINING_ANON_AGGR_TYPE_P \
78   anon_aggr_type_p
79
80 /* Each front end provides its own.  */
81 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
82
83 static varray_type deferred_fns;
84
85 /* Post-switch processing.  */
86 static void
87 c_post_options ()
88 {
89   cpp_post_options (parse_in);
90
91   /* Use tree inlining if possible.  Function instrumentation is only
92      done in the RTL level, so we disable tree inlining.  */
93   if (! flag_instrument_function_entry_exit)
94     {
95       if (!flag_no_inline)
96         {
97           flag_inline_trees = 1;
98           flag_no_inline = 1;
99         }
100       if (flag_inline_functions)
101         {
102           flag_inline_trees = 2;
103           flag_inline_functions = 0;
104         }
105     }
106 }
107
108 static void
109 c_init_options ()
110 {
111   parse_in = cpp_create_reader (CLK_GNUC89);
112
113   /* Mark as "unspecified".  */
114   flag_bounds_check = -1;
115 }
116
117 static const char *
118 c_init (filename)
119      const char *filename;
120 {
121   c_init_decl_processing ();
122
123   filename = c_common_lang_init (filename);
124
125   add_c_tree_codes ();
126
127   /* If still unspecified, make it match -std=c99
128      (allowing for -pedantic-errors).  */
129   if (mesg_implicit_function_declaration < 0)
130     {
131       if (flag_isoc99)
132         mesg_implicit_function_declaration = flag_pedantic_errors ? 2 : 1;
133       else
134         mesg_implicit_function_declaration = 0;
135     }
136
137   save_lang_status = &push_c_function_context;
138   restore_lang_status = &pop_c_function_context;
139   mark_lang_status = &mark_c_function_context;
140   lang_expand_expr = &c_expand_expr;
141   lang_safe_from_p = &c_safe_from_p;
142   diagnostic_format_decoder (global_dc) = &c_tree_printer;
143   lang_expand_decl_stmt = &c_expand_decl_stmt;
144   lang_missing_noreturn_ok_p = &c_missing_noreturn_ok_p;
145
146   VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
147   ggc_add_tree_varray_root (&deferred_fns, 1);
148
149   return filename;
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 /* Register a function tree, so that its optimization and conversion
257    to RTL is only done at the end of the compilation.  */
258
259 int
260 defer_fn (fn)
261      tree fn;
262 {
263   VARRAY_PUSH_TREE (deferred_fns, fn);
264
265   return 1;
266 }
267
268 /* Called at end of parsing, but before end-of-file processing.  */
269
270 void
271 finish_file ()
272 {
273   unsigned int i;
274
275   for (i = 0; i < VARRAY_ACTIVE_SIZE (deferred_fns); i++)
276     /* Don't output the same function twice.  We may run into such
277        situations when an extern inline function is later given a
278        non-extern-inline definition.  */
279     if (! TREE_ASM_WRITTEN (VARRAY_TREE (deferred_fns, i)))
280       c_expand_deferred_function (VARRAY_TREE (deferred_fns, i));
281   VARRAY_FREE (deferred_fns);
282
283 #ifndef ASM_OUTPUT_CONSTRUCTOR
284   if (static_ctors)
285     {
286       tree body = start_cdtor ('I');
287
288       for (; static_ctors; static_ctors = TREE_CHAIN (static_ctors))
289         c_expand_expr_stmt (build_function_call (TREE_VALUE (static_ctors),
290                                                  NULL_TREE));
291
292       finish_cdtor (body);
293     }
294 #endif
295 #ifndef ASM_OUTPUT_DESTRUCTOR
296   if (static_dtors)
297     {
298       tree body = start_cdtor ('D');
299
300       for (; static_dtors; static_dtors = TREE_CHAIN (static_dtors))
301         c_expand_expr_stmt (build_function_call (TREE_VALUE (static_dtors),
302                                                  NULL_TREE));
303
304       finish_cdtor (body);
305     }
306 #endif
307
308   if (back_end_hook)
309     (*back_end_hook) (getdecls ());
310   
311   {
312     int flags;
313     FILE *stream = dump_begin (TDI_all, &flags);
314
315     if (stream)
316       {
317         dump_node (getdecls (), flags & ~TDF_SLIM, stream);
318         dump_end (TDI_all, stream);
319       }
320   }
321 }
322
323 /* Called during diagnostic message formatting process to print a
324    source-level entity onto BUFFER.  The meaning of the format specifiers
325    is as follows:
326    %D: a general decl,
327    %F: a function declaration,
328    %T: a type.
329
330    These format specifiers form a subset of the format specifiers set used
331    by the C++ front-end.
332    Please notice when called, the `%' part was already skipped by the
333    diagnostic machinery.  */
334 static int
335 c_tree_printer (buffer)
336      output_buffer *buffer;
337 {
338   tree t = va_arg (output_buffer_format_args (buffer), tree);
339
340   switch (*output_buffer_text_cursor (buffer))
341     {
342     case 'D':
343     case 'F':
344     case 'T':
345       {
346         const char *n = DECL_NAME (t)
347           ? (*decl_printable_name) (t, 2)
348           : "({anonymous})";
349         output_add_string (buffer, n);
350       }
351       return 1;
352
353     default:
354       return 0;
355     }
356 }
357
358 static int
359 c_missing_noreturn_ok_p (decl)
360      tree decl;
361 {
362   /* A missing noreturn is not ok for freestanding implementations and
363      ok for the `main' function in hosted implementations.  */
364   return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl));
365 }
366
367 /* We want to inline `extern inline' functions even if this would
368    violate inlining limits.  Some glibc and linux constructs depend on
369    such functions always being inlined when optimizing.  */
370
371 static int
372 c_disregard_inline_limits (fn)
373      tree fn;
374 {
375   return DECL_DECLARED_INLINE_P (fn) && DECL_EXTERNAL (fn);
376 }
377
378 static tree inline_forbidden_p PARAMS ((tree *, int *, void *));
379
380 static tree
381 inline_forbidden_p (nodep, walk_subtrees, fn)
382      tree *nodep;
383      int *walk_subtrees ATTRIBUTE_UNUSED;
384      void *fn;
385 {
386   tree node = *nodep;
387   tree t;
388
389   switch (TREE_CODE (node))
390     {
391     case CALL_EXPR:
392       t = get_callee_fndecl (node);
393
394       if (! t)
395         break;
396
397       /* We cannot inline functions that call setjmp.  */
398       if (setjmp_call_p (t))
399         return node;
400
401       switch (DECL_FUNCTION_CODE (t))
402         {
403           /* We cannot inline functions that take a variable number of
404              arguments.  */
405         case BUILT_IN_VARARGS_START:
406         case BUILT_IN_STDARG_START:
407 #if 0
408           /* Functions that need information about the address of the
409              caller can't (shouldn't?) be inlined.  */
410         case BUILT_IN_RETURN_ADDRESS:
411 #endif
412           return node;
413
414         default:
415           break;
416         }
417
418       break;
419
420     case DECL_STMT:
421       /* We cannot inline functions that contain other functions.  */
422       if (TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
423           && DECL_INITIAL (TREE_OPERAND (node, 0)))
424         return node;
425       break;
426
427     case GOTO_STMT:
428     case GOTO_EXPR:
429       t = TREE_OPERAND (node, 0);
430
431       /* We will not inline a function which uses computed goto.  The
432          addresses of its local labels, which may be tucked into
433          global storage, are of course not constant across
434          instantiations, which causes unexpected behaviour.  */
435       if (TREE_CODE (t) != LABEL_DECL)
436         return node;
437
438       /* We cannot inline a nested function that jumps to a nonlocal
439          label.  */
440       if (TREE_CODE (t) == LABEL_DECL
441           && DECL_CONTEXT (t) && DECL_CONTEXT (t) != fn)
442         return node;
443
444       break;
445
446     default:
447       break;
448     }
449
450   return NULL_TREE;
451 }
452
453 static int
454 c_cannot_inline_tree_fn (fnp)
455      tree *fnp;
456 {
457   tree fn = *fnp;
458   tree t;
459
460   if (! function_attribute_inlinable_p (fn))
461     {
462       DECL_UNINLINABLE (fn) = 1;
463       return 1;
464     }
465
466   /* If a function has pending sizes, we must not defer its
467      compilation, and we can't inline it as a tree.  */
468   if (fn == current_function_decl)
469     {
470       t = get_pending_sizes ();
471       put_pending_sizes (t);
472
473       if (t)
474         {
475           DECL_UNINLINABLE (fn) = 1;
476           return 1;
477         }
478     }
479
480   if (DECL_CONTEXT (fn))
481     {
482       /* If a nested function has pending sizes, we may have already
483          saved them.  */
484       if (DECL_LANG_SPECIFIC (fn)->pending_sizes)
485         {
486           DECL_UNINLINABLE (fn) = 1;
487           return 1;
488         }
489     }
490   else
491     {
492       /* We rely on the fact that this function is called upfront,
493          just before we start expanding a function.  If FN is active
494          (i.e., it's the current_function_decl or a parent thereof),
495          we have to walk FN's saved tree.  Otherwise, we can safely
496          assume we have done it before and, if we didn't mark it as
497          uninlinable (in which case we wouldn't have been called), it
498          is inlinable.  Unfortunately, this strategy doesn't work for
499          nested functions, because they're only expanded as part of
500          their enclosing functions, so the inlinability test comes in
501          late.  */
502       t = current_function_decl;
503
504       while (t && t != fn)
505         t = DECL_CONTEXT (t);
506       if (! t)
507         return 0;
508     }
509     
510   if (walk_tree (&DECL_SAVED_TREE (fn), inline_forbidden_p, fn, NULL))
511     {
512       DECL_UNINLINABLE (fn) = 1;
513       return 1;
514     }
515
516   return 0;
517 }