OSDN Git Service

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