OSDN Git Service

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