96d6855f303e884062989231fd9f20a2b7f5184a
[gcc.git] / gcc / c-objc-common.c
1 /* Some code common to C and ObjC front ends.
2 Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "insn-config.h"
28 #include "integrate.h"
29 #include "expr.h"
30 #include "c-tree.h"
31 #include "function.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "diagnostic.h"
35 #include "tree-inline.h"
36 #include "varray.h"
37 #include "ggc.h"
38 #include "langhooks.h"
39 #include "target.h"
40 #include "cgraph.h"
41
42 static bool c_tree_printer (pretty_printer *, text_info *);
43 static tree inline_forbidden_p (tree *, int *, void *);
44 static tree start_cdtor (int);
45 static void finish_cdtor (tree);
46
47 int
48 c_missing_noreturn_ok_p (tree decl)
49 {
50 /* A missing noreturn is not ok for freestanding implementations and
51 ok for the `main' function in hosted implementations. */
52 return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl));
53 }
54
55 /* We want to inline `extern inline' functions even if this would
56 violate inlining limits. Some glibc and linux constructs depend on
57 such functions always being inlined when optimizing. */
58
59 int
60 c_disregard_inline_limits (tree fn)
61 {
62 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) != NULL)
63 return 1;
64
65 return DECL_DECLARED_INLINE_P (fn) && DECL_EXTERNAL (fn);
66 }
67
68 static tree
69 inline_forbidden_p (tree *nodep, int *walk_subtrees ATTRIBUTE_UNUSED,
70 void *fn)
71 {
72 tree node = *nodep;
73 tree t;
74
75 switch (TREE_CODE (node))
76 {
77 case CALL_EXPR:
78 t = get_callee_fndecl (node);
79
80 if (! t)
81 break;
82
83 /* We cannot inline functions that call setjmp. */
84 if (setjmp_call_p (t))
85 return node;
86
87 switch (DECL_FUNCTION_CODE (t))
88 {
89 /* We cannot inline functions that take a variable number of
90 arguments. */
91 case BUILT_IN_VA_START:
92 case BUILT_IN_STDARG_START:
93 #if 0
94 /* Functions that need information about the address of the
95 caller can't (shouldn't?) be inlined. */
96 case BUILT_IN_RETURN_ADDRESS:
97 #endif
98 return node;
99
100 default:
101 break;
102 }
103
104 break;
105
106 case DECL_STMT:
107 /* We cannot inline functions that contain other functions. */
108 if (TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
109 && DECL_INITIAL (TREE_OPERAND (node, 0)))
110 return node;
111 break;
112
113 case GOTO_STMT:
114 case GOTO_EXPR:
115 t = TREE_OPERAND (node, 0);
116
117 /* We will not inline a function which uses computed goto. The
118 addresses of its local labels, which may be tucked into
119 global storage, are of course not constant across
120 instantiations, which causes unexpected behavior. */
121 if (TREE_CODE (t) != LABEL_DECL)
122 return node;
123
124 /* We cannot inline a nested function that jumps to a nonlocal
125 label. */
126 if (TREE_CODE (t) == LABEL_DECL
127 && !DECL_FILE_SCOPE_P (t) && DECL_CONTEXT (t) != fn)
128 return node;
129
130 break;
131
132 case RECORD_TYPE:
133 case UNION_TYPE:
134 /* We cannot inline a function of the form
135
136 void F (int i) { struct S { int ar[i]; } s; }
137
138 Attempting to do so produces a catch-22 in tree-inline.c.
139 If walk_tree examines the TYPE_FIELDS chain of RECORD_TYPE/
140 UNION_TYPE nodes, then it goes into infinite recursion on a
141 structure containing a pointer to its own type. If it doesn't,
142 then the type node for S doesn't get adjusted properly when
143 F is inlined, and we abort in find_function_data. */
144 for (t = TYPE_FIELDS (node); t; t = TREE_CHAIN (t))
145 if (variably_modified_type_p (TREE_TYPE (t)))
146 return node;
147
148 default:
149 break;
150 }
151
152 return NULL_TREE;
153 }
154
155 int
156 c_cannot_inline_tree_fn (tree *fnp)
157 {
158 tree fn = *fnp;
159 tree t;
160
161 if (flag_really_no_inline
162 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) == NULL)
163 return 1;
164
165 /* Don't auto-inline anything that might not be bound within
166 this unit of translation. */
167 if (!DECL_DECLARED_INLINE_P (fn) && !(*targetm.binds_local_p) (fn))
168 goto cannot_inline;
169
170 if (! function_attribute_inlinable_p (fn))
171 goto cannot_inline;
172
173 /* If a function has pending sizes, we must not defer its
174 compilation, and we can't inline it as a tree. */
175 if (fn == current_function_decl)
176 {
177 t = get_pending_sizes ();
178 put_pending_sizes (t);
179
180 if (t)
181 goto cannot_inline;
182 }
183
184 if (! DECL_FILE_SCOPE_P (fn))
185 {
186 /* If a nested function has pending sizes, we may have already
187 saved them. */
188 if (DECL_LANG_SPECIFIC (fn)->pending_sizes)
189 goto cannot_inline;
190 }
191 else
192 {
193 /* We rely on the fact that this function is called upfront,
194 just before we start expanding a function. If FN is active
195 (i.e., it's the current_function_decl or a parent thereof),
196 we have to walk FN's saved tree. Otherwise, we can safely
197 assume we have done it before and, if we didn't mark it as
198 uninlinable (in which case we wouldn't have been called), it
199 is inlinable. Unfortunately, this strategy doesn't work for
200 nested functions, because they're only expanded as part of
201 their enclosing functions, so the inlinability test comes in
202 late. */
203 t = current_function_decl;
204
205 while (t && t != fn)
206 t = DECL_CONTEXT (t);
207 if (! t)
208 return 0;
209 }
210
211 if (walk_tree (&DECL_SAVED_TREE (fn), inline_forbidden_p, fn, NULL))
212 goto cannot_inline;
213
214 return 0;
215
216 cannot_inline:
217 DECL_UNINLINABLE (fn) = 1;
218 return 1;
219 }
220
221 /* Called from check_global_declarations. */
222
223 bool
224 c_warn_unused_global_decl (tree decl)
225 {
226 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
227 return false;
228 if (DECL_IN_SYSTEM_HEADER (decl))
229 return false;
230
231 return true;
232 }
233
234 /* Initialization common to C and Objective-C front ends. */
235 bool
236 c_objc_common_init (void)
237 {
238 static const enum tree_code stmt_codes[] = {
239 c_common_stmt_codes
240 };
241
242 INIT_STATEMENT_CODES (stmt_codes);
243
244 c_init_decl_processing ();
245
246 if (c_common_init () == false)
247 return false;
248
249 lang_expand_decl_stmt = c_expand_decl_stmt;
250
251 /* These were not defined in the Objective-C front end, but I'm
252 putting them here anyway. The diagnostic format decoder might
253 want an enhanced ObjC implementation. */
254 diagnostic_format_decoder (global_dc) = &c_tree_printer;
255 lang_missing_noreturn_ok_p = &c_missing_noreturn_ok_p;
256
257 /* If still unspecified, make it match -std=c99
258 (allowing for -pedantic-errors). */
259 if (mesg_implicit_function_declaration < 0)
260 {
261 if (flag_isoc99)
262 mesg_implicit_function_declaration = flag_pedantic_errors ? 2 : 1;
263 else
264 mesg_implicit_function_declaration = 0;
265 }
266
267 return true;
268 }
269
270 static tree
271 start_cdtor (int method_type)
272 {
273 tree fnname = get_file_function_name (method_type);
274 tree void_list_node_1 = build_tree_list (NULL_TREE, void_type_node);
275 tree body;
276
277 start_function (void_list_node_1,
278 build_nt (CALL_EXPR, fnname,
279 tree_cons (NULL_TREE, NULL_TREE, void_list_node_1),
280 NULL_TREE),
281 NULL_TREE);
282 store_parm_decls ();
283
284 current_function_cannot_inline
285 = "static constructors and destructors cannot be inlined";
286
287 body = c_begin_compound_stmt ();
288
289 pushlevel (0);
290 clear_last_expr ();
291 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
292
293 return body;
294 }
295
296 static void
297 finish_cdtor (tree body)
298 {
299 tree scope;
300 tree block;
301
302 scope = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
303 block = poplevel (0, 0, 0);
304 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope)) = block;
305 SCOPE_STMT_BLOCK (TREE_VALUE (scope)) = block;
306
307 RECHAIN_STMTS (body, COMPOUND_BODY (body));
308
309 finish_function ();
310 }
311
312 /* Called at end of parsing, but before end-of-file processing. */
313
314 void
315 c_objc_common_finish_file (void)
316 {
317 if (pch_file)
318 c_common_write_pch ();
319
320 /* If multiple translation units were built, copy information between
321 them based on linkage rules. */
322 merge_translation_unit_decls ();
323
324 cgraph_finalize_compilation_unit ();
325 cgraph_optimize ();
326
327 if (static_ctors)
328 {
329 tree body = start_cdtor ('I');
330
331 for (; static_ctors; static_ctors = TREE_CHAIN (static_ctors))
332 c_expand_expr_stmt (build_function_call (TREE_VALUE (static_ctors),
333 NULL_TREE));
334
335 finish_cdtor (body);
336 }
337
338 if (static_dtors)
339 {
340 tree body = start_cdtor ('D');
341
342 for (; static_dtors; static_dtors = TREE_CHAIN (static_dtors))
343 c_expand_expr_stmt (build_function_call (TREE_VALUE (static_dtors),
344 NULL_TREE));
345
346 finish_cdtor (body);
347 }
348
349 {
350 int flags;
351 FILE *stream = dump_begin (TDI_all, &flags);
352
353 if (stream)
354 {
355 dump_node (getdecls (), flags & ~TDF_SLIM, stream);
356 dump_end (TDI_all, stream);
357 }
358 }
359 }
360
361 /* Called during diagnostic message formatting process to print a
362 source-level entity onto BUFFER. The meaning of the format specifiers
363 is as follows:
364 %D: a general decl,
365 %E: An expression,
366 %F: a function declaration,
367 %T: a type.
368
369 These format specifiers form a subset of the format specifiers set used
370 by the C++ front-end.
371 Please notice when called, the `%' part was already skipped by the
372 diagnostic machinery. */
373 static bool
374 c_tree_printer (pretty_printer *pp, text_info *text)
375 {
376 tree t = va_arg (*text->args_ptr, tree);
377
378 switch (*text->format_spec)
379 {
380 case 'D':
381 case 'F':
382 case 'T':
383 {
384 const char *n = DECL_NAME (t)
385 ? (*lang_hooks.decl_printable_name) (t, 2)
386 : "({anonymous})";
387 pp_string (pp, n);
388 }
389 return true;
390
391 case 'E':
392 if (TREE_CODE (t) == IDENTIFIER_NODE)
393 {
394 pp_string (pp, IDENTIFIER_POINTER (t));
395 return true;
396 }
397 return false;
398
399 default:
400 return false;
401 }
402 }