OSDN Git Service

42cdf08eb47e5449f35786b7e7363bb027c916ff
[pf3gnuchains/gcc-fork.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 /* Some statements, like for-statements or if-statements, require a
40    condition.  This condition can be a declaration.  If T is such a
41    declaration it is processed, and an expression appropriate to use
42    as the condition is returned.  Otherwise, T itself is returned.  */
43
44 tree
45 expand_cond (t)
46      tree t;
47 {
48   if (t && TREE_CODE (t) == TREE_LIST)
49     {
50       expand_stmt (TREE_PURPOSE (t));
51       return TREE_VALUE (t);
52     }
53   else 
54     return t;
55 }
56
57 /* Create RTL for the local static variable DECL.  */
58
59 void
60 make_rtl_for_local_static (decl)
61      tree decl;
62 {
63   const char *asmspec = NULL;
64
65   /* If we inlined this variable, we could see it's declaration
66      again.  */
67   if (DECL_RTL (decl))
68     return;
69
70   if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
71     {
72       /* The only way this situaton can occur is if the
73          user specified a name for this DECL using the
74          `attribute' syntax.  */
75       asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
76       DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
77     }
78
79   rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
80 }
81
82 /* Let the back-end know about DECL.  */
83
84 void
85 emit_local_var (decl)
86      tree decl;
87 {
88   /* Create RTL for this variable.  */
89   if (!DECL_RTL (decl))
90     {
91       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
92         /* The user must have specified an assembler name for this
93            variable.  Set that up now.  */
94         rest_of_decl_compilation
95           (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
96            /*top_level=*/0, /*at_end=*/0);
97       else
98         expand_decl (decl);
99     }
100
101   /* Actually do the initialization.  */
102   if (stmts_are_full_exprs_p ())
103     expand_start_target_temps ();
104
105   expand_decl_init (decl);
106
107   if (stmts_are_full_exprs_p ())
108     expand_end_target_temps ();
109 }
110
111 /* Helper for generating the RTL at the beginning of a scope. */
112
113 void
114 genrtl_do_pushlevel ()
115 {
116   emit_line_note (input_filename, lineno);
117   clear_last_expr ();
118 }
119
120 /* Helper for generating the RTL. */
121
122 void
123 genrtl_clear_out_block ()
124 {
125   /* If COND wasn't a declaration, clear out the
126      block we made for it and start a new one here so the
127      optimization in expand_end_loop will work.  */
128   if (getdecls () == NULL_TREE)
129     genrtl_do_pushlevel ();
130 }
131
132 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
133
134 void
135 genrtl_goto_stmt (destination)
136      tree destination;
137 {
138   if (TREE_CODE (destination) == IDENTIFIER_NODE)
139     abort ();
140   
141   /* We warn about unused labels with -Wunused.  That means we have to
142      mark the used labels as used.  */
143   if (TREE_CODE (destination) == LABEL_DECL)
144     TREE_USED (destination) = 1;
145   
146   emit_line_note (input_filename, lineno);
147   
148   if (TREE_CODE (destination) == LABEL_DECL)
149     {
150       label_rtx (destination);
151       expand_goto (destination); 
152     }
153   else
154     expand_computed_goto (destination);
155 }
156
157 /* Generate the RTL for EXPR, which is an EXPR_STMT. */
158
159 void 
160 genrtl_expr_stmt (expr)
161      tree expr;
162 {
163   if (expr != NULL_TREE)
164     {
165       emit_line_note (input_filename, lineno);
166       
167       if (stmts_are_full_exprs_p ())
168         expand_start_target_temps ();
169       
170       lang_expand_expr_stmt (expr);
171       
172       if (stmts_are_full_exprs_p ())
173         expand_end_target_temps ();
174     }
175 }
176
177 /* Generate the RTL for T, which is a DECL_STMT. */
178
179 void
180 genrtl_decl_stmt (t)
181      tree t;
182 {
183   tree decl;
184   emit_line_note (input_filename, lineno);
185   decl = DECL_STMT_DECL (t);
186   /* If this is a declaration for an automatic local
187      variable, initialize it.  Note that we might also see a
188      declaration for a namespace-scope object (declared with
189      `extern').  We don't have to handle the initialization
190      of those objects here; they can only be declarations,
191      rather than definitions.  */
192   if (TREE_CODE (decl) == VAR_DECL 
193       && !TREE_STATIC (decl)
194       && !DECL_EXTERNAL (decl))
195     {
196       /* Let the back-end know about this variable.  */
197       if (!anon_aggr_type_p (TREE_TYPE (decl)))
198         emit_local_var (decl);
199       else
200         expand_anon_union_decl (decl, NULL_TREE, 
201                                 DECL_ANON_UNION_ELEMS (decl));
202     }
203   else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
204     {
205       if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
206         /* Do not emit unused decls. This is not just an
207            optimization. We really do not want to emit
208            __PRETTY_FUNCTION__ etc, if they're never used.  */
209         DECL_IGNORED_P (decl) = 1;
210       else
211         make_rtl_for_local_static (decl);
212     }
213 }
214
215 /* Generate the RTL for T, which is an IF_STMT. */
216
217 void
218 genrtl_if_stmt (t)
219      tree t;
220 {
221   tree cond;
222   genrtl_do_pushlevel ();
223   cond = expand_cond (IF_COND (t));
224   emit_line_note (input_filename, lineno);
225   expand_start_cond (cond, 0);
226   if (THEN_CLAUSE (t))
227     expand_stmt (THEN_CLAUSE (t));
228   if (ELSE_CLAUSE (t))
229     {
230       expand_start_else ();
231       expand_stmt (ELSE_CLAUSE (t));
232     }
233   expand_end_cond ();
234 }
235
236 /* Generate the RTL for T, which is a WHILE_STMT. */
237
238 void
239 genrtl_while_stmt (t)
240      tree t;
241 {
242   tree cond;
243   emit_nop ();
244   emit_line_note (input_filename, lineno);
245   expand_start_loop (1); 
246   genrtl_do_pushlevel ();
247
248   cond = expand_cond (WHILE_COND (t));
249   emit_line_note (input_filename, lineno);
250   expand_exit_loop_if_false (0, cond);
251   genrtl_clear_out_block ();
252   
253   expand_stmt (WHILE_BODY (t));
254
255   expand_end_loop ();
256 }
257
258 /* Generate the RTL for T, which is a DO_STMT. */
259
260 void
261 genrtl_do_stmt (t)
262      tree t;
263 {
264   tree cond;
265   emit_nop ();
266   emit_line_note (input_filename, lineno);
267   expand_start_loop_continue_elsewhere (1);
268
269   expand_stmt (DO_BODY (t));
270
271   expand_loop_continue_here ();
272
273   cond = expand_cond (DO_COND (t));
274   emit_line_note (input_filename, lineno);
275   expand_exit_loop_if_false (0, cond);
276   expand_end_loop ();
277 }
278
279 /* Generate the RTL for EXPR, which is a RETURN_STMT. */
280
281 void
282 genrtl_return_stmt (expr)
283      tree expr;
284 {
285   emit_line_note (input_filename, lineno);
286   c_expand_return (expr);
287 }
288
289 /* Generate the RTL for T, which is a FOR_STMT. */
290
291 void
292 genrtl_for_stmt (t)
293      tree t;
294 {
295   tree tmp;
296   tree cond;
297   if (NEW_FOR_SCOPE_P (t))
298     genrtl_do_pushlevel ();
299
300   expand_stmt (FOR_INIT_STMT (t));
301
302   emit_nop ();
303   emit_line_note (input_filename, lineno);
304   expand_start_loop_continue_elsewhere (1); 
305   genrtl_do_pushlevel ();
306   cond = expand_cond (FOR_COND (t));
307   emit_line_note (input_filename, lineno);
308   if (cond)
309     expand_exit_loop_if_false (0, cond);
310   genrtl_clear_out_block ();
311   tmp = FOR_EXPR (t);
312
313   expand_stmt (FOR_BODY (t));
314
315   emit_line_note (input_filename, lineno);
316   expand_loop_continue_here ();
317   if (tmp) 
318     genrtl_expr_stmt (tmp);
319   expand_end_loop ();
320 }
321
322 /* Generate the RTL for a BREAK_STMT. */
323
324 void
325 genrtl_break_stmt ()
326 {
327   emit_line_note (input_filename, lineno);
328   if ( ! expand_exit_something ())
329     error ("break statement not within loop or switch");
330 }
331
332 /* Generate the RTL for a CONTINUE_STMT. */
333
334 void
335 genrtl_continue_stmt ()
336 {
337   emit_line_note (input_filename, lineno);
338   if (! expand_continue_loop (0))
339     error ("continue statement not within a loop");   
340 }
341
342 /* Generate the RTL for T, which is a SCOPE_STMT. */
343
344 void
345 genrtl_scope_stmt (t)
346      tree t;
347 {
348   if (!SCOPE_NO_CLEANUPS_P (t))
349     {
350       if (SCOPE_BEGIN_P (t))
351         expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
352                                          SCOPE_STMT_BLOCK (t));
353       else if (SCOPE_END_P (t))
354         expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
355     }
356   else if (!SCOPE_NULLIFIED_P (t))
357     {
358       rtx note = emit_note (NULL,
359                             (SCOPE_BEGIN_P (t) 
360                              ? NOTE_INSN_BLOCK_BEG
361                              : NOTE_INSN_BLOCK_END));
362       NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
363     }
364 }
365
366 /* Generate the RTL for T, which is a SWITCH_STMT. */
367
368 void
369 genrtl_switch_stmt (t)
370      tree t;
371 {
372   tree cond;
373   genrtl_do_pushlevel ();
374  
375   cond = expand_cond (SWITCH_COND (t));
376   if (cond != error_mark_node)
377     {
378       emit_line_note (input_filename, lineno);
379       c_expand_start_case (cond);
380     }
381   else
382     /* The code is in error, but we don't want expand_end_case to
383        crash. */
384     c_expand_start_case (boolean_false_node);
385
386   expand_stmt (SWITCH_BODY (t));
387
388   expand_end_case (cond);
389 }
390
391 /* Generate the RTL for a CASE_LABEL. */
392
393 void 
394 genrtl_case_label (low_value, high_value)
395      tree low_value;
396      tree high_value;
397 {
398   do_case (low_value, high_value);
399 }
400
401 /* Generate the RTL for the start of a COMPOUND_STMT. */
402
403 tree 
404 genrtl_begin_compound_stmt (has_no_scope)
405      int has_no_scope;
406 {
407   /* If this is the outermost block of the function, declare the
408      variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth.  */
409   if (cfun
410       && !current_function_name_declared () 
411       && !has_no_scope)
412     {
413       set_current_function_name_declared (1);
414       declare_function_name ();
415     }
416
417   return NULL_TREE;
418 }
419
420 /* Generate the RTL for the end of a COMPOUND_STMT. */
421
422 tree genrtl_finish_compound_stmt (has_no_scope)
423      int has_no_scope;
424 {
425   return NULL_TREE;
426 }
427
428 /* Generate the RTL for T, which is a COMPOUND_STMT. */
429
430 tree 
431 genrtl_compound_stmt (t)
432     tree t;
433 {
434   genrtl_begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
435   expand_stmt (COMPOUND_BODY (t));
436   return (genrtl_finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)));
437 }
438
439 /* Generate the RTL for an ASM_STMT. */
440
441 void
442 genrtl_asm_stmt (cv_qualifier, string, output_operands,
443                  input_operands, clobbers)
444      tree cv_qualifier;
445      tree string;
446      tree output_operands;
447      tree input_operands;
448      tree clobbers;
449 {
450   if (TREE_CHAIN (string))
451     string = combine_strings (string);
452
453   if (cv_qualifier != NULL_TREE
454       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
455     {
456       warning ("%s qualifier ignored on asm",
457                IDENTIFIER_POINTER (cv_qualifier));
458       cv_qualifier = NULL_TREE;
459     }
460
461   emit_line_note (input_filename, lineno);
462   if (output_operands != NULL_TREE || input_operands != NULL_TREE
463       || clobbers != NULL_TREE)
464       c_expand_asm_operands (string, output_operands,
465                              input_operands, 
466                              clobbers,
467                              cv_qualifier != NULL_TREE,
468                              input_filename, lineno);
469   else
470     expand_asm (string);
471 }
472
473 /* Generate the RTL for a DECL_CLEANUP. */
474
475 void 
476 genrtl_decl_cleanup (decl, cleanup)
477      tree decl;
478      tree cleanup;
479 {
480   if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
481     expand_decl_cleanup (decl, cleanup);
482 }
483
484 tree
485 expand_stmt (t)
486      tree t;
487 {
488   tree rval;
489   rval = lang_expand_stmt (t);
490   return rval;
491 }