acconfig.h: _GLIBCPP_USING_THREADS and some workaround types added.
[gcc.git] / gcc / c-semantics.c
1 /* This file contains the definitions and documentation for the common
2 tree codes used in the GNU C and C++ compilers (see c-common.def
3 for the standard codes).
4 Copyright (C) 2000 Free Software Foundation, Inc. Written by
5 Benjamin Chelf (chelf@codesourcery.com).
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "tree.h"
27 #include "function.h"
28 #include "splay-tree.h"
29 #include "varray.h"
30 #include "c-common.h"
31 #include "except.h"
32 #include "toplev.h"
33 #include "flags.h"
34 #include "ggc.h"
35 #include "rtl.h"
36 #include "output.h"
37 #include "timevar.h"
38
39 /* Build a generic statement based on the given type of node and
40 arguments. Similar to `build_nt', except that we set
41 TREE_COMPLEXITY to be the current line number. */
42
43 tree
44 build_stmt VPARAMS ((enum tree_code code, ...))
45 {
46 #ifndef ANSI_PROTOTYPES
47 enum tree_code code;
48 #endif
49 va_list p;
50 register tree t;
51 register int length;
52 register int i;
53
54 VA_START (p, code);
55
56 #ifndef ANSI_PROTOTYPES
57 code = va_arg (p, enum tree_code);
58 #endif
59
60 t = make_node (code);
61 length = TREE_CODE_LENGTH (code);
62 TREE_COMPLEXITY (t) = lineno;
63
64 for (i = 0; i < length; i++)
65 TREE_OPERAND (t, i) = va_arg (p, tree);
66
67 va_end (p);
68 return t;
69 }
70
71 /* Some statements, like for-statements or if-statements, require a
72 condition. This condition can be a declaration. If T is such a
73 declaration it is processed, and an expression appropriate to use
74 as the condition is returned. Otherwise, T itself is returned. */
75
76 tree
77 expand_cond (t)
78 tree t;
79 {
80 if (t && TREE_CODE (t) == TREE_LIST)
81 {
82 expand_stmt (TREE_PURPOSE (t));
83 return TREE_VALUE (t);
84 }
85 else
86 return t;
87 }
88
89 /* Create RTL for the local static variable DECL. */
90
91 void
92 make_rtl_for_local_static (decl)
93 tree decl;
94 {
95 const char *asmspec = NULL;
96
97 /* If we inlined this variable, we could see it's declaration
98 again. */
99 if (TREE_ASM_WRITTEN (decl))
100 return;
101
102 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
103 {
104 /* The only way this situaton can occur is if the
105 user specified a name for this DECL using the
106 `attribute' syntax. */
107 asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
108 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
109 }
110
111 rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
112 }
113
114 /* Let the back-end know about DECL. */
115
116 void
117 emit_local_var (decl)
118 tree decl;
119 {
120 /* Create RTL for this variable. */
121 if (!DECL_RTL (decl))
122 {
123 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
124 /* The user must have specified an assembler name for this
125 variable. Set that up now. */
126 rest_of_decl_compilation
127 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
128 /*top_level=*/0, /*at_end=*/0);
129 else
130 expand_decl (decl);
131 }
132
133 /* Actually do the initialization. */
134 if (stmts_are_full_exprs_p ())
135 expand_start_target_temps ();
136
137 expand_decl_init (decl);
138
139 if (stmts_are_full_exprs_p ())
140 expand_end_target_temps ();
141 }
142
143 /* Helper for generating the RTL at the beginning of a scope. */
144
145 void
146 genrtl_do_pushlevel ()
147 {
148 emit_line_note (input_filename, lineno);
149 clear_last_expr ();
150 }
151
152 /* Helper for generating the RTL. */
153
154 void
155 genrtl_clear_out_block ()
156 {
157 /* If COND wasn't a declaration, clear out the
158 block we made for it and start a new one here so the
159 optimization in expand_end_loop will work. */
160 if (getdecls () == NULL_TREE)
161 genrtl_do_pushlevel ();
162 }
163
164 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
165
166 void
167 genrtl_goto_stmt (destination)
168 tree destination;
169 {
170 if (TREE_CODE (destination) == IDENTIFIER_NODE)
171 abort ();
172
173 /* We warn about unused labels with -Wunused. That means we have to
174 mark the used labels as used. */
175 if (TREE_CODE (destination) == LABEL_DECL)
176 TREE_USED (destination) = 1;
177
178 emit_line_note (input_filename, lineno);
179
180 if (TREE_CODE (destination) == LABEL_DECL)
181 {
182 label_rtx (destination);
183 expand_goto (destination);
184 }
185 else
186 expand_computed_goto (destination);
187 }
188
189 /* Generate the RTL for EXPR, which is an EXPR_STMT. */
190
191 void
192 genrtl_expr_stmt (expr)
193 tree expr;
194 {
195 if (expr != NULL_TREE)
196 {
197 emit_line_note (input_filename, lineno);
198
199 if (stmts_are_full_exprs_p ())
200 expand_start_target_temps ();
201
202 lang_expand_expr_stmt (expr);
203
204 if (stmts_are_full_exprs_p ())
205 expand_end_target_temps ();
206 }
207 }
208
209 /* Generate the RTL for T, which is a DECL_STMT. */
210
211 void
212 genrtl_decl_stmt (t)
213 tree t;
214 {
215 tree decl;
216 emit_line_note (input_filename, lineno);
217 decl = DECL_STMT_DECL (t);
218 /* If this is a declaration for an automatic local
219 variable, initialize it. Note that we might also see a
220 declaration for a namespace-scope object (declared with
221 `extern'). We don't have to handle the initialization
222 of those objects here; they can only be declarations,
223 rather than definitions. */
224 if (TREE_CODE (decl) == VAR_DECL
225 && !TREE_STATIC (decl)
226 && !DECL_EXTERNAL (decl))
227 {
228 /* Let the back-end know about this variable. */
229 if (!anon_aggr_type_p (TREE_TYPE (decl)))
230 emit_local_var (decl);
231 else
232 expand_anon_union_decl (decl, NULL_TREE,
233 DECL_ANON_UNION_ELEMS (decl));
234 }
235 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
236 {
237 if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
238 /* Do not emit unused decls. This is not just an
239 optimization. We really do not want to emit
240 __PRETTY_FUNCTION__ etc, if they're never used. */
241 DECL_IGNORED_P (decl) = 1;
242 else
243 make_rtl_for_local_static (decl);
244 }
245 }
246
247 /* Generate the RTL for T, which is an IF_STMT. */
248
249 void
250 genrtl_if_stmt (t)
251 tree t;
252 {
253 tree cond;
254 genrtl_do_pushlevel ();
255 cond = expand_cond (IF_COND (t));
256 emit_line_note (input_filename, lineno);
257 expand_start_cond (cond, 0);
258 if (THEN_CLAUSE (t))
259 expand_stmt (THEN_CLAUSE (t));
260 if (ELSE_CLAUSE (t))
261 {
262 expand_start_else ();
263 expand_stmt (ELSE_CLAUSE (t));
264 }
265 expand_end_cond ();
266 }
267
268 /* Generate the RTL for T, which is a WHILE_STMT. */
269
270 void
271 genrtl_while_stmt (t)
272 tree t;
273 {
274 tree cond;
275 emit_nop ();
276 emit_line_note (input_filename, lineno);
277 expand_start_loop (1);
278 genrtl_do_pushlevel ();
279
280 cond = expand_cond (WHILE_COND (t));
281 emit_line_note (input_filename, lineno);
282 expand_exit_loop_if_false (0, cond);
283 genrtl_clear_out_block ();
284
285 expand_stmt (WHILE_BODY (t));
286
287 expand_end_loop ();
288 }
289
290 /* Generate the RTL for T, which is a DO_STMT. */
291
292 void
293 genrtl_do_stmt (t)
294 tree t;
295 {
296 tree cond;
297 emit_nop ();
298 emit_line_note (input_filename, lineno);
299 expand_start_loop_continue_elsewhere (1);
300
301 expand_stmt (DO_BODY (t));
302
303 expand_loop_continue_here ();
304
305 cond = expand_cond (DO_COND (t));
306 emit_line_note (input_filename, lineno);
307 expand_exit_loop_if_false (0, cond);
308 expand_end_loop ();
309 }
310
311 /* Build the node for a return statement and return it. */
312
313 tree
314 build_return_stmt (expr)
315 tree expr;
316 {
317 return (build_stmt (RETURN_STMT, expr));
318 }
319
320 /* Generate the RTL for EXPR, which is a RETURN_STMT. */
321
322 void
323 genrtl_return_stmt (expr)
324 tree expr;
325 {
326 emit_line_note (input_filename, lineno);
327 c_expand_return (expr);
328 }
329
330 /* Generate the RTL for T, which is a FOR_STMT. */
331
332 void
333 genrtl_for_stmt (t)
334 tree t;
335 {
336 tree tmp;
337 tree cond;
338 if (NEW_FOR_SCOPE_P (t))
339 genrtl_do_pushlevel ();
340
341 expand_stmt (FOR_INIT_STMT (t));
342
343 emit_nop ();
344 emit_line_note (input_filename, lineno);
345 expand_start_loop_continue_elsewhere (1);
346 genrtl_do_pushlevel ();
347 cond = expand_cond (FOR_COND (t));
348 emit_line_note (input_filename, lineno);
349 if (cond)
350 expand_exit_loop_if_false (0, cond);
351 genrtl_clear_out_block ();
352 tmp = FOR_EXPR (t);
353
354 expand_stmt (FOR_BODY (t));
355
356 emit_line_note (input_filename, lineno);
357 expand_loop_continue_here ();
358 if (tmp)
359 genrtl_expr_stmt (tmp);
360 expand_end_loop ();
361 }
362
363 /* Build a break statement node and return it. */
364
365 tree
366 build_break_stmt ()
367 {
368 return (build_stmt (BREAK_STMT));
369 }
370
371 /* Generate the RTL for a BREAK_STMT. */
372
373 void
374 genrtl_break_stmt ()
375 {
376 emit_line_note (input_filename, lineno);
377 if ( ! expand_exit_something ())
378 error ("break statement not within loop or switch");
379 }
380
381 /* Build a continue statement node and return it. */
382
383 tree
384 build_continue_stmt ()
385 {
386 return (build_stmt (CONTINUE_STMT));
387 }
388
389 /* Generate the RTL for a CONTINUE_STMT. */
390
391 void
392 genrtl_continue_stmt ()
393 {
394 emit_line_note (input_filename, lineno);
395 if (! expand_continue_loop (0))
396 error ("continue statement not within a loop");
397 }
398
399 /* Generate the RTL for T, which is a SCOPE_STMT. */
400
401 void
402 genrtl_scope_stmt (t)
403 tree t;
404 {
405 if (!SCOPE_NO_CLEANUPS_P (t))
406 {
407 if (SCOPE_BEGIN_P (t))
408 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
409 SCOPE_STMT_BLOCK (t));
410 else if (SCOPE_END_P (t))
411 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
412 }
413 else if (!SCOPE_NULLIFIED_P (t))
414 {
415 rtx note = emit_note (NULL,
416 (SCOPE_BEGIN_P (t)
417 ? NOTE_INSN_BLOCK_BEG
418 : NOTE_INSN_BLOCK_END));
419 NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
420 }
421 }
422
423 /* Generate the RTL for T, which is a SWITCH_STMT. */
424
425 void
426 genrtl_switch_stmt (t)
427 tree t;
428 {
429 tree cond;
430 genrtl_do_pushlevel ();
431
432 cond = expand_cond (SWITCH_COND (t));
433 if (cond != error_mark_node)
434 {
435 emit_line_note (input_filename, lineno);
436 c_expand_start_case (cond);
437 }
438 else
439 /* The code is in error, but we don't want expand_end_case to
440 crash. */
441 c_expand_start_case (boolean_false_node);
442
443 expand_stmt (SWITCH_BODY (t));
444
445 expand_end_case (cond);
446 }
447
448 /* Create a CASE_LABEL tree node and return it. */
449
450 tree
451 build_case_label (low_value, high_value)
452 tree low_value;
453 tree high_value;
454 {
455 return build_stmt (CASE_LABEL, low_value, high_value);
456 }
457
458
459 /* Generate the RTL for a CASE_LABEL. */
460
461 void
462 genrtl_case_label (low_value, high_value)
463 tree low_value;
464 tree high_value;
465 {
466 do_case (low_value, high_value);
467 }
468
469 /* Generate the RTL for T, which is a COMPOUND_STMT. */
470
471 void
472 genrtl_compound_stmt (t)
473 tree t;
474 {
475 /* If this is the outermost block of the function, declare the
476 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
477 if (cfun
478 && !current_function_name_declared ()
479 && !COMPOUND_STMT_NO_SCOPE (t))
480 {
481 set_current_function_name_declared (1);
482 declare_function_name ();
483 }
484
485 expand_stmt (COMPOUND_BODY (t));
486 }
487
488 /* Generate the RTL for an ASM_STMT. */
489
490 void
491 genrtl_asm_stmt (cv_qualifier, string, output_operands,
492 input_operands, clobbers)
493 tree cv_qualifier;
494 tree string;
495 tree output_operands;
496 tree input_operands;
497 tree clobbers;
498 {
499 if (TREE_CHAIN (string))
500 string = combine_strings (string);
501
502 if (cv_qualifier != NULL_TREE
503 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
504 {
505 warning ("%s qualifier ignored on asm",
506 IDENTIFIER_POINTER (cv_qualifier));
507 cv_qualifier = NULL_TREE;
508 }
509
510 emit_line_note (input_filename, lineno);
511 if (output_operands != NULL_TREE || input_operands != NULL_TREE
512 || clobbers != NULL_TREE)
513 c_expand_asm_operands (string, output_operands,
514 input_operands,
515 clobbers,
516 cv_qualifier != NULL_TREE,
517 input_filename, lineno);
518 else
519 expand_asm (string);
520 }
521
522 /* Generate the RTL for a DECL_CLEANUP. */
523
524 void
525 genrtl_decl_cleanup (decl, cleanup)
526 tree decl;
527 tree cleanup;
528 {
529 if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
530 expand_decl_cleanup (decl, cleanup);
531 }
532
533 /* Generate the RTL for the statement T, its substatements, and any
534 other statements at its nesting level. */
535
536 tree
537 expand_stmt (t)
538 tree t;
539 {
540 tree rval;
541 rval = lang_expand_stmt (t);
542 return rval;
543 }
544