c-common.c (c_common_lang_init): New function.
[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 GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "tree.h"
26 #include "function.h"
27 #include "input.h"
28 #include "toplev.h"
29 #include "diagnostic.h"
30 #include "output.h"
31 #include "flags.h"
32 #include "ggc.h"
33 #include "rtl.h"
34 #include "expr.h"
35 #include "c-tree.h"
36 #include "c-lex.h"
37 #include "cpplib.h"
38
39 static int c_tree_printer PARAMS ((output_buffer *));
40 static int c_missing_noreturn_ok_p PARAMS ((tree));
41
42 /* Each of the functions defined here
43 is an alternative to a function in objc-actions.c. */
44
45 int
46 lang_decode_option (argc, argv)
47 int argc;
48 char **argv;
49 {
50 return c_decode_option (argc, argv);
51 }
52
53 void
54 lang_init_options ()
55 {
56 parse_in = cpp_create_reader (CLK_GNUC89);
57
58 /* Mark as "unspecified". */
59 flag_bounds_check = -1;
60 }
61
62 void
63 lang_init ()
64 {
65 c_common_lang_init ();
66
67 /* If still unspecified, make it match pedantic && -std=c99. */
68 if (mesg_implicit_function_declaration < 0)
69 {
70 if (pedantic && flag_isoc99)
71 mesg_implicit_function_declaration = flag_pedantic_errors ? 2 : 1;
72 else
73 mesg_implicit_function_declaration = 0;
74 }
75
76 save_lang_status = &push_c_function_context;
77 restore_lang_status = &pop_c_function_context;
78 mark_lang_status = &mark_c_function_context;
79 lang_expand_expr = &c_expand_expr;
80 lang_safe_from_p = &c_safe_from_p;
81 lang_printer = &c_tree_printer;
82 lang_expand_decl_stmt = &c_expand_decl_stmt;
83 lang_missing_noreturn_ok_p = &c_missing_noreturn_ok_p;
84
85 c_parse_init ();
86 }
87
88 void
89 lang_finish ()
90 {
91 }
92
93 const char *
94 lang_identify ()
95 {
96 return "c";
97 }
98
99 void
100 print_lang_statistics ()
101 {
102 }
103
104 /* used by print-tree.c */
105
106 void
107 lang_print_xnode (file, node, indent)
108 FILE *file ATTRIBUTE_UNUSED;
109 tree node ATTRIBUTE_UNUSED;
110 int indent ATTRIBUTE_UNUSED;
111 {
112 }
113
114 /* Used by c-lex.c, but only for objc. */
115
116 tree
117 lookup_interface (arg)
118 tree arg ATTRIBUTE_UNUSED;
119 {
120 return 0;
121 }
122
123 tree
124 is_class_name (arg)
125 tree arg ATTRIBUTE_UNUSED;
126 {
127 return 0;
128 }
129
130 void
131 maybe_objc_check_decl (decl)
132 tree decl ATTRIBUTE_UNUSED;
133 {
134 }
135
136 int
137 maybe_objc_comptypes (lhs, rhs, reflexive)
138 tree lhs ATTRIBUTE_UNUSED;
139 tree rhs ATTRIBUTE_UNUSED;
140 int reflexive ATTRIBUTE_UNUSED;
141 {
142 return -1;
143 }
144
145 tree
146 maybe_objc_method_name (decl)
147 tree decl ATTRIBUTE_UNUSED;
148 {
149 return 0;
150 }
151
152 tree
153 maybe_building_objc_message_expr ()
154 {
155 return 0;
156 }
157
158 int
159 recognize_objc_keyword ()
160 {
161 return 0;
162 }
163
164 /* Used by c-typeck.c (build_external_ref), but only for objc. */
165
166 tree
167 lookup_objc_ivar (id)
168 tree id ATTRIBUTE_UNUSED;
169 {
170 return 0;
171 }
172
173 #if !defined(ASM_OUTPUT_CONSTRUCTOR) || !defined(ASM_OUTPUT_DESTRUCTOR)
174 extern tree static_ctors;
175 extern tree static_dtors;
176
177 static tree start_cdtor PARAMS ((int));
178 static void finish_cdtor PARAMS ((tree));
179
180 static tree
181 start_cdtor (method_type)
182 int method_type;
183 {
184 tree fnname = get_file_function_name (method_type);
185 tree void_list_node_1 = build_tree_list (NULL_TREE, void_type_node);
186 tree body;
187
188 start_function (void_list_node_1,
189 build_parse_node (CALL_EXPR, fnname,
190 tree_cons (NULL_TREE, NULL_TREE,
191 void_list_node_1),
192 NULL_TREE),
193 NULL_TREE, NULL_TREE);
194 store_parm_decls ();
195
196 current_function_cannot_inline
197 = "static constructors and destructors cannot be inlined";
198
199 body = c_begin_compound_stmt ();
200
201 pushlevel (0);
202 clear_last_expr ();
203 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
204
205 return body;
206 }
207
208 static void
209 finish_cdtor (body)
210 tree body;
211 {
212 tree scope;
213 tree block;
214
215 scope = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
216 block = poplevel (0, 0, 0);
217 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope)) = block;
218 SCOPE_STMT_BLOCK (TREE_VALUE (scope)) = block;
219
220 RECHAIN_STMTS (body, COMPOUND_BODY (body));
221
222 finish_function (0);
223 }
224 #endif
225
226 /* Called at end of parsing, but before end-of-file processing. */
227
228 void
229 finish_file ()
230 {
231 #ifndef ASM_OUTPUT_CONSTRUCTOR
232 if (static_ctors)
233 {
234 tree body = start_cdtor ('I');
235
236 for (; static_ctors; static_ctors = TREE_CHAIN (static_ctors))
237 c_expand_expr_stmt (build_function_call (TREE_VALUE (static_ctors),
238 NULL_TREE));
239
240 finish_cdtor (body);
241 }
242 #endif
243 #ifndef ASM_OUTPUT_DESTRUCTOR
244 if (static_dtors)
245 {
246 tree body = start_cdtor ('D');
247
248 for (; static_dtors; static_dtors = TREE_CHAIN (static_dtors))
249 c_expand_expr_stmt (build_function_call (TREE_VALUE (static_dtors),
250 NULL_TREE));
251
252 finish_cdtor (body);
253 }
254 #endif
255
256 if (back_end_hook)
257 (*back_end_hook) (getdecls ());
258
259 if (flag_dump_translation_unit)
260 dump_node_to_file (getdecls (), flag_dump_translation_unit);
261 }
262
263 /* Called during diagnostic message formatting process to print a
264 source-level entity onto BUFFER. The meaning of the format specifiers
265 is as follows:
266 %D: a general decl,
267 %F: a function declaration,
268 %T: a type.
269
270 These format specifiers form a subset of the format specifiers set used
271 by the C++ front-end.
272 Please notice when called, the `%' part was already skipped by the
273 diagnostic machinery. */
274 static int
275 c_tree_printer (buffer)
276 output_buffer *buffer;
277 {
278 tree t = va_arg (output_buffer_format_args (buffer), tree);
279
280 switch (*output_buffer_text_cursor (buffer))
281 {
282 case 'D':
283 case 'F':
284 case 'T':
285 {
286 const char *n = DECL_NAME (t)
287 ? (*decl_printable_name) (t, 2)
288 : "({anonymous})";
289 output_add_string (buffer, n);
290 }
291 return 1;
292
293 default:
294 return 0;
295 }
296 }
297
298 static int
299 c_missing_noreturn_ok_p (decl)
300 tree decl;
301 {
302 /* A missing noreturn is not ok for freestanding implementations and
303 ok for the `main' function in hosted implementations. */
304 return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl));
305 }