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