OSDN Git Service

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