]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/semantics.c
cleanup2.C: New test.
[thirdparty/gcc.git] / gcc / cp / semantics.c
CommitLineData
ad321293
MM
1/* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
5
4c571114 6 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
ad321293
MM
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.c.
9
10 This file is part of GNU CC.
11
12 GNU CC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
15 any later version.
16
17 GNU CC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GNU CC; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 02111-1307, USA. */
26
27#include "config.h"
8d052bc7 28#include "system.h"
ad321293
MM
29#include "tree.h"
30#include "cp-tree.h"
31#include "except.h"
32#include "lex.h"
12027a89 33#include "toplev.h"
84df082b 34#include "flags.h"
d658cd4c 35#include "ggc.h"
ad321293
MM
36
37/* There routines provide a modular interface to perform many parsing
38 operations. They may therefore be used during actual parsing, or
39 during template instantiation, which may be regarded as a
40 degenerate form of parsing. Since the current g++ parser is
41 lacking in several respects, and will be reimplemented, we are
42 attempting to move most code that is not directly related to
43 parsing into this file; that will make implementing the new parser
44 much easier since it will be able to make use of these routines. */
45
527f0080 46static tree expand_cond PROTO((tree));
ed5511d9 47static tree maybe_convert_cond PROTO((tree));
558475f0 48
6f80451c
MM
49/* Record the fact that STMT was the last statement added to the
50 statement tree. */
51
52#define SET_LAST_STMT(stmt) \
53 (current_stmt_tree->x_last_stmt = (stmt))
54
ad321293
MM
55/* When parsing a template, LAST_TREE contains the last statement
56 parsed. These are chained together through the TREE_CHAIN field,
57 but often need to be re-organized since the parse is performed
58 bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of
59 STMT. */
60
cb39191d
MM
61#define RECHAIN_STMTS(stmt, substmt) \
62 do { \
63 substmt = TREE_CHAIN (stmt); \
64 TREE_CHAIN (stmt) = NULL_TREE; \
6f80451c 65 SET_LAST_STMT (stmt); \
ad321293
MM
66 } while (0)
67
527f0080
MM
68/* Finish processing the COND, the SUBSTMT condition for STMT. */
69
ed5511d9
MM
70#define FINISH_COND(cond, stmt, substmt) \
71 do { \
72 if (last_tree != stmt) \
73 { \
74 RECHAIN_STMTS (stmt, substmt); \
75 if (!processing_template_decl) \
76 { \
77 cond = build_tree_list (substmt, cond); \
78 substmt = cond; \
79 } \
80 } \
81 else \
82 substmt = cond; \
527f0080
MM
83 } while (0)
84
a7e4cfa0 85/* T is a statement. Add it to the statement-tree. */
ad321293 86
a7e4cfa0
MM
87void
88add_tree (t)
89 tree t;
90{
91 /* Add T to the statement-tree. */
6f80451c
MM
92 TREE_CHAIN (last_tree) = t;
93 SET_LAST_STMT (t);
a7e4cfa0
MM
94
95 /* When we expand a statement-tree, we must know whether or not the
96 statements are full-expresions. We record that fact here. */
97 if (building_stmt_tree ())
98 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p;
99}
100
ed5511d9
MM
101/* COND is the condition-expression for an if, while, etc.,
102 statement. Convert it to a boolean value, if appropriate. */
103
104static tree
105maybe_convert_cond (cond)
106 tree cond;
107{
108 /* Empty conditions remain empty. */
109 if (!cond)
110 return NULL_TREE;
111
112 /* Wait until we instantiate templates before doing conversion. */
113 if (processing_template_decl)
114 return cond;
115
116 /* Do the conversion. */
117 cond = convert_from_reference (cond);
118 return condition_conversion (cond);
119}
120
9bfadf57 121/* Finish an expression-statement, whose EXPRESSION is as indicated. */
a7e4cfa0 122
9bfadf57
MM
123void
124finish_expr_stmt (expr)
ad321293
MM
125 tree expr;
126{
ce4a0391 127 if (expr != NULL_TREE)
ad321293 128 {
558475f0 129 if (building_stmt_tree ())
ce4a0391 130 {
ce4a0391
MM
131 /* Do default conversion if safe and possibly important,
132 in case within ({...}). */
364460b6
MM
133 if (!processing_template_decl
134 && !stmts_are_full_exprs_p
135 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
136 && lvalue_p (expr))
137 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
ce4a0391 138 expr = default_conversion (expr);
f1dedc31 139
364460b6
MM
140 if (!processing_template_decl)
141 expr = break_out_cleanups (expr);
142
143 add_tree (build_min_nt (EXPR_STMT, expr));
144 }
145 else
146 {
147 emit_line_note (input_filename, lineno);
148
f1dedc31
MM
149 if (stmts_are_full_exprs_p)
150 expand_start_target_temps ();
151
558475f0 152 cplus_expand_expr_stmt (expr);
558475f0 153
f1dedc31 154 if (stmts_are_full_exprs_p)
f31c0a32 155 expand_end_target_temps ();
f1dedc31 156 }
ad321293 157 }
ce4a0391 158
ad321293 159 finish_stmt ();
558475f0
MM
160
161 /* This was an expression-statement, so we save the type of the
162 expression. */
163 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
ad321293
MM
164}
165
166/* Begin an if-statement. Returns a newly created IF_STMT if
167 appropriate. */
168
169tree
170begin_if_stmt ()
171{
172 tree r;
173
9bfadf57
MM
174 do_pushlevel ();
175
558475f0 176 if (building_stmt_tree ())
ad321293
MM
177 {
178 r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
179 add_tree (r);
180 }
181 else
182 r = NULL_TREE;
183
ad321293
MM
184 return r;
185}
186
187/* Process the COND of an if-statement, which may be given by
188 IF_STMT. */
189
190void
191finish_if_stmt_cond (cond, if_stmt)
192 tree cond;
193 tree if_stmt;
194{
ed5511d9
MM
195 cond = maybe_convert_cond (cond);
196
558475f0 197 if (building_stmt_tree ())
a7e4cfa0 198 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
ad321293
MM
199 else
200 {
201 emit_line_note (input_filename, lineno);
ed5511d9 202 expand_start_cond (cond, 0);
ad321293
MM
203 }
204}
205
206/* Finish the then-clause of an if-statement, which may be given by
207 IF_STMT. */
208
209tree
210finish_then_clause (if_stmt)
211 tree if_stmt;
212{
558475f0 213 if (building_stmt_tree ())
ad321293 214 {
cb39191d 215 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
6f80451c 216 SET_LAST_STMT (if_stmt);
ad321293
MM
217 return if_stmt;
218 }
219 else
220 return NULL_TREE;
221}
222
223/* Begin the else-clause of an if-statement. */
224
225void
226begin_else_clause ()
227{
558475f0 228 if (!building_stmt_tree ())
ad321293
MM
229 expand_start_else ();
230}
231
232/* Finish the else-clause of an if-statement, which may be given by
233 IF_STMT. */
234
235void
236finish_else_clause (if_stmt)
237 tree if_stmt;
238{
558475f0 239 if (building_stmt_tree ())
cb39191d 240 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
ad321293
MM
241}
242
243/* Finsh an if-statement. */
244
245void
246finish_if_stmt ()
247{
558475f0 248 if (!building_stmt_tree ())
ad321293
MM
249 expand_end_cond ();
250
251 do_poplevel ();
252 finish_stmt ();
253}
254
255/* Begin a while-statement. Returns a newly created WHILE_STMT if
256 appropriate. */
257
258tree
259begin_while_stmt ()
260{
261 tree r;
262
558475f0 263 if (building_stmt_tree ())
ad321293
MM
264 {
265 r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE);
266 add_tree (r);
267 }
268 else
269 {
270 emit_nop ();
271 emit_line_note (input_filename, lineno);
272 expand_start_loop (1);
273 r = NULL_TREE;
274 }
275
276 do_pushlevel ();
277
278 return r;
279}
280
281/* Process the COND of an if-statement, which may be given by
282 WHILE_STMT. */
283
284void
285finish_while_stmt_cond (cond, while_stmt)
286 tree cond;
287 tree while_stmt;
288{
ed5511d9
MM
289 cond = maybe_convert_cond (cond);
290
558475f0 291 if (building_stmt_tree ())
a7e4cfa0 292 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
ad321293
MM
293 else
294 {
295 emit_line_note (input_filename, lineno);
ed5511d9 296 expand_exit_loop_if_false (0, cond);
ad321293
MM
297 }
298
299 /* If COND wasn't a declaration, clear out the
300 block we made for it and start a new one here so the
301 optimization in expand_end_loop will work. */
302 if (getdecls () == NULL_TREE)
303 {
304 do_poplevel ();
305 do_pushlevel ();
306 }
307}
308
309/* Finish a while-statement, which may be given by WHILE_STMT. */
310
311void
312finish_while_stmt (while_stmt)
313 tree while_stmt;
314{
315 do_poplevel ();
316
558475f0 317 if (building_stmt_tree ())
cb39191d 318 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
ad321293
MM
319 else
320 expand_end_loop ();
321 finish_stmt ();
322}
323
324/* Begin a do-statement. Returns a newly created DO_STMT if
325 appropriate. */
326
327tree
328begin_do_stmt ()
329{
558475f0 330 if (building_stmt_tree ())
ad321293
MM
331 {
332 tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE);
333 add_tree (r);
334 return r;
335 }
336 else
337 {
338 emit_nop ();
339 emit_line_note (input_filename, lineno);
340 expand_start_loop_continue_elsewhere (1);
341 return NULL_TREE;
342 }
343}
344
345/* Finish the body of a do-statement, which may be given by DO_STMT. */
346
347void
348finish_do_body (do_stmt)
349 tree do_stmt;
350{
558475f0 351 if (building_stmt_tree ())
cb39191d 352 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
ad321293
MM
353 else
354 expand_loop_continue_here ();
355}
356
357/* Finish a do-statement, which may be given by DO_STMT, and whose
358 COND is as indicated. */
359
360void
361finish_do_stmt (cond, do_stmt)
362 tree cond;
363 tree do_stmt;
364{
ed5511d9
MM
365 cond = maybe_convert_cond (cond);
366
558475f0 367 if (building_stmt_tree ())
2a1e9fdd 368 DO_COND (do_stmt) = cond;
ad321293
MM
369 else
370 {
371 emit_line_note (input_filename, lineno);
ed5511d9 372 expand_exit_loop_if_false (0, cond);
ad321293
MM
373 expand_end_loop ();
374 }
375
ad321293
MM
376 finish_stmt ();
377}
378
379/* Finish a return-statement. The EXPRESSION returned, if any, is as
380 indicated. */
381
382void
383finish_return_stmt (expr)
384 tree expr;
385{
efee38a9
MM
386 if (doing_semantic_analysis_p () && !processing_template_decl)
387 expr = check_return_expr (expr);
388
389 if (doing_semantic_analysis_p () && !processing_template_decl)
390 {
391 if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
392 {
393 /* Even returns without a value in a constructor must return
394 `this'. We accomplish this by sending all returns in a
395 constructor to the CTOR_LABEL; finish_function emits code to
396 return a value there. When we finally generate the real
397 return statement, CTOR_LABEL is no longer set, and we fall
398 through into the normal return-processing code below. */
399 finish_goto_stmt (ctor_label);
400 return;
401 }
402 else if (DECL_DESTRUCTOR_P (current_function_decl))
403 {
404 /* Similarly, all destructors must run destructors for
405 base-classes before returning. So, all returns in a
406 destructor get sent to the DTOR_LABEL; finsh_function emits
407 code to return a value there. */
408 finish_goto_stmt (dtor_label);
409 return;
410 }
411 }
412
558475f0
MM
413 if (building_stmt_tree ())
414 add_tree (build_min_nt (RETURN_STMT, expr));
415 else
416 {
417 emit_line_note (input_filename, lineno);
418 c_expand_return (expr);
419 }
420
ad321293
MM
421 finish_stmt ();
422}
423
424/* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
425
426tree
427begin_for_stmt ()
428{
429 tree r;
430
558475f0 431 if (building_stmt_tree ())
ad321293
MM
432 {
433 r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE,
434 NULL_TREE, NULL_TREE);
435 add_tree (r);
436 }
437 else
438 r = NULL_TREE;
439
440 if (flag_new_for_scope > 0)
441 {
442 do_pushlevel ();
443 note_level_for_for ();
444 }
445
446 return r;
447}
448
449/* Finish the for-init-statement of a for-statement, which may be
450 given by FOR_STMT. */
451
452void
453finish_for_init_stmt (for_stmt)
454 tree for_stmt;
455{
558475f0 456 if (building_stmt_tree ())
ad321293
MM
457 {
458 if (last_tree != for_stmt)
cb39191d 459 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
ad321293
MM
460 }
461 else
462 {
463 emit_nop ();
464 emit_line_note (input_filename, lineno);
465 expand_start_loop_continue_elsewhere (1);
466 }
467
468 do_pushlevel ();
469}
470
471/* Finish the COND of a for-statement, which may be given by
472 FOR_STMT. */
473
474void
475finish_for_cond (cond, for_stmt)
476 tree cond;
477 tree for_stmt;
478{
ed5511d9
MM
479 cond = maybe_convert_cond (cond);
480
558475f0 481 if (building_stmt_tree ())
a7e4cfa0 482 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
ad321293
MM
483 else
484 {
485 emit_line_note (input_filename, lineno);
1dcf683e 486 if (cond)
ed5511d9 487 expand_exit_loop_if_false (0, cond);
ad321293
MM
488 }
489
490 /* If the cond wasn't a declaration, clear out the
491 block we made for it and start a new one here so the
492 optimization in expand_end_loop will work. */
493 if (getdecls () == NULL_TREE)
494 {
495 do_poplevel ();
496 do_pushlevel ();
497 }
498}
499
500/* Finish the increment-EXPRESSION in a for-statement, which may be
501 given by FOR_STMT. */
502
503void
504finish_for_expr (expr, for_stmt)
505 tree expr;
506 tree for_stmt;
507{
558475f0 508 if (building_stmt_tree ())
2a1e9fdd 509 FOR_EXPR (for_stmt) = expr;
ad321293
MM
510}
511
512/* Finish the body of a for-statement, which may be given by
513 FOR_STMT. The increment-EXPR for the loop must be
514 provided. */
515
516void
517finish_for_stmt (expr, for_stmt)
518 tree expr;
519 tree for_stmt;
520{
521 /* Pop the scope for the body of the loop. */
522 do_poplevel ();
523
558475f0 524 if (building_stmt_tree ())
cb39191d 525 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
ad321293
MM
526 else
527 {
528 emit_line_note (input_filename, lineno);
529 expand_loop_continue_here ();
530 if (expr)
f1dedc31 531 finish_expr_stmt (expr);
ad321293
MM
532 expand_end_loop ();
533 }
534
ad321293
MM
535 if (flag_new_for_scope > 0)
536 do_poplevel ();
537
538 finish_stmt ();
539}
540
541/* Finish a break-statement. */
542
543void
544finish_break_stmt ()
545{
546 emit_line_note (input_filename, lineno);
558475f0 547 if (building_stmt_tree ())
ad321293
MM
548 add_tree (build_min_nt (BREAK_STMT));
549 else if ( ! expand_exit_something ())
8251199e 550 cp_error ("break statement not within loop or switch");
ad321293
MM
551}
552
553/* Finish a continue-statement. */
554
555void
556finish_continue_stmt ()
557{
558 emit_line_note (input_filename, lineno);
558475f0 559 if (building_stmt_tree ())
ad321293
MM
560 add_tree (build_min_nt (CONTINUE_STMT));
561 else if (! expand_continue_loop (0))
8251199e 562 cp_error ("continue statement not within a loop");
ad321293
MM
563}
564
527f0080
MM
565/* Begin a switch-statement. Returns a new SWITCH_STMT if
566 appropriate. */
ad321293 567
527f0080 568tree
ad321293
MM
569begin_switch_stmt ()
570{
527f0080
MM
571 tree r;
572
573 if (building_stmt_tree ())
574 {
575 r = build_min_nt (SWITCH_STMT, NULL_TREE, NULL_TREE);
576 add_tree (r);
577 }
578 else
579 r = NULL_TREE;
580
ad321293 581 do_pushlevel ();
527f0080
MM
582
583 return r;
ad321293
MM
584}
585
527f0080 586/* Finish the cond of a switch-statement. */
ad321293 587
527f0080
MM
588void
589finish_switch_cond (cond, switch_stmt)
ad321293 590 tree cond;
527f0080 591 tree switch_stmt;
ad321293 592{
558475f0 593 if (building_stmt_tree ())
527f0080 594 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
0db982be 595 else if (cond != error_mark_node)
ad321293
MM
596 {
597 emit_line_note (input_filename, lineno);
598 c_expand_start_case (cond);
ad321293 599 }
0db982be 600 else
527f0080
MM
601 /* The code is in error, but we don't want expand_end_case to
602 crash. */
603 c_expand_start_case (boolean_false_node);
0db982be 604
ad321293 605 push_switch ();
ad321293
MM
606}
607
608/* Finish the body of a switch-statement, which may be given by
609 SWITCH_STMT. The COND to switch on is indicated. */
610
611void
612finish_switch_stmt (cond, switch_stmt)
613 tree cond;
614 tree switch_stmt;
615{
558475f0 616 if (building_stmt_tree ())
cb39191d 617 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
ad321293
MM
618 else
619 expand_end_case (cond);
ad321293
MM
620 pop_switch ();
621 do_poplevel ();
622 finish_stmt ();
623}
624
625/* Finish a case-label. */
626
627void
628finish_case_label (low_value, high_value)
629 tree low_value;
630 tree high_value;
631{
558475f0
MM
632 if (building_stmt_tree ())
633 {
b35d4555
MM
634 /* Add a representation for the case label to the statement
635 tree. */
558475f0 636 add_tree (build_min_nt (CASE_LABEL, low_value, high_value));
b35d4555
MM
637 /* And warn about crossing initializations, etc. */
638 if (!processing_template_decl)
639 define_case_label ();
558475f0
MM
640 return;
641 }
642
ad321293
MM
643 do_case (low_value, high_value);
644}
645
ad321293
MM
646/* Finish a goto-statement. */
647
648void
649finish_goto_stmt (destination)
650 tree destination;
651{
3fa56191
MM
652 if (TREE_CODE (destination) == IDENTIFIER_NODE)
653 destination = lookup_label (destination);
654
88848bde
MM
655 /* We warn about unused labels with -Wunused. That means we have to
656 mark the used labels as used. */
657 if (TREE_CODE (destination) == LABEL_DECL)
658 TREE_USED (destination) = 1;
659
558475f0 660 if (building_stmt_tree ())
ad321293
MM
661 add_tree (build_min_nt (GOTO_STMT, destination));
662 else
663 {
664 emit_line_note (input_filename, lineno);
665
3fa56191 666 if (TREE_CODE (destination) == LABEL_DECL)
ad321293 667 {
b35d4555 668 label_rtx (destination);
3fa56191 669 expand_goto (destination);
ad321293
MM
670 }
671 else
672 expand_computed_goto (destination);
673 }
674}
675
676/* Begin a try-block. Returns a newly-created TRY_BLOCK if
677 appropriate. */
678
679tree
680begin_try_block ()
681{
558475f0 682 if (building_stmt_tree ())
ad321293
MM
683 {
684 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
685 NULL_TREE);
686 add_tree (r);
687 return r;
688 }
689 else
690 {
691 emit_line_note (input_filename, lineno);
692 expand_start_try_stmts ();
693 return NULL_TREE;
694 }
695}
696
0dde4175
JM
697/* Likewise, for a function-try-block. */
698
699tree
700begin_function_try_block ()
701{
558475f0 702 if (building_stmt_tree ())
0dde4175
JM
703 {
704 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
705 NULL_TREE);
62409b39 706 FN_TRY_BLOCK_P (r) = 1;
0dde4175
JM
707 add_tree (r);
708 return r;
709 }
710 else
711 {
712 if (! current_function_parms_stored)
713 store_parm_decls ();
714 expand_start_early_try_stmts ();
715 return NULL_TREE;
716 }
717}
718
ad321293
MM
719/* Finish a try-block, which may be given by TRY_BLOCK. */
720
721void
722finish_try_block (try_block)
723 tree try_block;
724{
558475f0 725 if (building_stmt_tree ())
cb39191d 726 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
ad321293 727 else
558475f0 728 expand_start_all_catch ();
ad321293
MM
729}
730
efa8eda3
MM
731/* Finish the body of a cleanup try-block, which may be given by
732 TRY_BLOCK. */
733
62409b39
MM
734void
735finish_cleanup_try_block (try_block)
736 tree try_block;
737{
738 if (building_stmt_tree ())
739 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
740}
741
f1dedc31
MM
742/* Finish an implicitly generated try-block, with a cleanup is given
743 by CLEANUP. */
744
745void
746finish_cleanup (cleanup, try_block)
747 tree cleanup;
748 tree try_block;
749{
750 if (building_stmt_tree ())
751 {
2a1e9fdd 752 TRY_HANDLERS (try_block) = cleanup;
f1dedc31
MM
753 CLEANUP_P (try_block) = 1;
754 }
755 else
756 expand_eh_region_end (protect_with_terminate (cleanup));
757}
758
0dde4175
JM
759/* Likewise, for a function-try-block. */
760
761void
762finish_function_try_block (try_block)
f1dedc31 763 tree try_block;
0dde4175 764{
558475f0 765 if (building_stmt_tree ())
62409b39
MM
766 {
767 if (TREE_CHAIN (try_block)
768 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
769 {
770 /* Chain the compound statement after the CTOR_INITIALIZER. */
771 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
772 /* And make the CTOR_INITIALIZER the body of the try-block. */
773 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
774 }
775 else
776 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
777 }
0dde4175
JM
778 else
779 {
780 end_protect_partials ();
781 expand_start_all_catch ();
0dde4175 782 }
b35d4555
MM
783
784 in_function_try_handler = 1;
0dde4175
JM
785}
786
ad321293
MM
787/* Finish a handler-sequence for a try-block, which may be given by
788 TRY_BLOCK. */
789
790void
791finish_handler_sequence (try_block)
792 tree try_block;
793{
558475f0 794 if (building_stmt_tree ())
cb39191d 795 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
ad321293 796 else
f1dedc31 797 expand_end_all_catch ();
ad321293
MM
798}
799
0dde4175
JM
800/* Likewise, for a function-try-block. */
801
802void
803finish_function_handler_sequence (try_block)
804 tree try_block;
805{
b35d4555
MM
806 in_function_try_handler = 0;
807
558475f0 808 if (building_stmt_tree ())
cb39191d 809 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
0dde4175 810 else
b35d4555 811 expand_end_all_catch ();
0dde4175
JM
812}
813
ad321293
MM
814/* Begin a handler. Returns a HANDLER if appropriate. */
815
816tree
817begin_handler ()
818{
819 tree r;
820
558475f0 821 if (building_stmt_tree ())
ad321293
MM
822 {
823 r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
824 add_tree (r);
825 }
826 else
827 r = NULL_TREE;
828
829 do_pushlevel ();
830
831 return r;
832}
833
834/* Finish the handler-parameters for a handler, which may be given by
b35d4555
MM
835 HANDLER. DECL is the declaration for the catch parameter, or NULL
836 if this is a `catch (...)' clause. */
ad321293 837
b35d4555
MM
838tree
839finish_handler_parms (decl, handler)
840 tree decl;
ad321293 841 tree handler;
b35d4555
MM
842{
843 tree blocks = NULL_TREE;
844
845 if (processing_template_decl)
846 {
847 if (decl)
848 {
849 decl = pushdecl (decl);
850 decl = push_template_decl (decl);
851 add_decl_stmt (decl);
852 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
853 }
854 }
855 else if (building_stmt_tree ())
856 blocks = expand_start_catch_block (decl);
857
858 return blocks;
859}
860
861/* Note the beginning of a handler for TYPE. This function is called
862 at the point to which control should be transferred when an
863 appropriately-typed exception is thrown. */
864
865void
866begin_catch_block (type)
867 tree type;
ad321293 868{
558475f0 869 if (building_stmt_tree ())
b35d4555
MM
870 add_tree (build (START_CATCH_STMT, type));
871 else
872 start_catch_handler (type);
ad321293
MM
873}
874
b35d4555
MM
875/* Finish a handler, which may be given by HANDLER. The BLOCKs are
876 the return value from the matching call to finish_handler_parms. */
ad321293
MM
877
878void
b35d4555
MM
879finish_handler (blocks, handler)
880 tree blocks;
ad321293
MM
881 tree handler;
882{
b35d4555
MM
883 if (!processing_template_decl)
884 {
885 if (building_stmt_tree ())
886 expand_end_catch_block (blocks);
887
888 if (!building_stmt_tree ())
889 {
890 /* Fall to outside the try statement when done executing
891 handler and we fall off end of handler. This is jump
892 Lresume in the documentation. */
893 expand_goto (top_label_entry (&caught_return_label_stack));
894 end_catch_handler ();
895 }
896 }
897
13e8cf82
MM
898 do_poplevel ();
899
558475f0 900 if (building_stmt_tree ())
cb39191d 901 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
ad321293
MM
902}
903
904/* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
905 compound-statement does not define a scope. Returns a new
906 COMPOUND_STMT if appropriate. */
907
908tree
909begin_compound_stmt (has_no_scope)
910 int has_no_scope;
911{
912 tree r;
913
558475f0 914 if (building_stmt_tree ())
ad321293
MM
915 {
916 r = build_min_nt (COMPOUND_STMT, NULL_TREE);
917 add_tree (r);
918 if (has_no_scope)
919 COMPOUND_STMT_NO_SCOPE (r) = 1;
920 }
921 else
922 r = NULL_TREE;
923
558475f0
MM
924 last_expr_type = NULL_TREE;
925
ad321293
MM
926 if (!has_no_scope)
927 do_pushlevel ();
f1dedc31
MM
928 else
929 /* Normally, we try hard to keep the BLOCK for a
930 statement-expression. But, if it's a statement-expression with
931 a scopeless block, there's nothing to keep, and we don't want
932 to accidentally keep a block *inside* the scopeless block. */
933 keep_next_level (0);
ad321293 934
24bef158
MM
935 /* If this is the outermost block of the function, declare the
936 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
6f80451c
MM
937 if (current_function
938 && !current_function_name_declared
9bfadf57
MM
939 && !processing_template_decl
940 && !has_no_scope)
24bef158
MM
941 {
942 declare_function_name ();
943 current_function_name_declared = 1;
944 }
945
ad321293
MM
946 return r;
947}
948
949
950/* Finish a compound-statement, which may be given by COMPOUND_STMT.
951 If HAS_NO_SCOPE is non-zero, the compound statement does not define
952 a scope. */
953
954tree
955finish_compound_stmt (has_no_scope, compound_stmt)
956 int has_no_scope;
957 tree compound_stmt;
958{
959 tree r;
558475f0 960 tree t;
ad321293
MM
961
962 if (!has_no_scope)
963 r = do_poplevel ();
964 else
965 r = NULL_TREE;
966
558475f0 967 if (building_stmt_tree ())
cb39191d 968 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
ad321293 969
cb39191d 970 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
558475f0
MM
971 the precise purpose of that variable is store the type of the
972 last expression statement within the last compound statement, we
973 preserve the value. */
974 t = last_expr_type;
ad321293 975 finish_stmt ();
558475f0 976 last_expr_type = t;
ad321293
MM
977
978 return r;
979}
980
981/* Finish an asm-statement, whose components are a CV_QUALIFIER, a
982 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
983 CLOBBERS. */
984
985void
986finish_asm_stmt (cv_qualifier, string, output_operands,
3ebc5c52 987 input_operands, clobbers)
ad321293
MM
988 tree cv_qualifier;
989 tree string;
990 tree output_operands;
991 tree input_operands;
992 tree clobbers;
993{
994 if (TREE_CHAIN (string))
cd9f6678 995 string = combine_strings (string);
ad321293 996
f71f87f9
MM
997 if (cv_qualifier != NULL_TREE
998 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
999 {
1000 cp_warning ("%s qualifier ignored on asm",
1001 IDENTIFIER_POINTER (cv_qualifier));
1002 cv_qualifier = NULL_TREE;
1003 }
1004
558475f0 1005 if (building_stmt_tree ())
ad321293
MM
1006 {
1007 tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
1008 output_operands, input_operands,
1009 clobbers);
1010 add_tree (r);
1011 }
1012 else
1013 {
1014 emit_line_note (input_filename, lineno);
6e9438cf
AG
1015 if (output_operands != NULL_TREE || input_operands != NULL_TREE
1016 || clobbers != NULL_TREE)
1017 {
7dc5bd62
MM
1018 tree t;
1019
7dc5bd62
MM
1020 for (t = input_operands; t; t = TREE_CHAIN (t))
1021 TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
1022
6e9438cf
AG
1023 c_expand_asm_operands (string, output_operands,
1024 input_operands,
1025 clobbers,
f71f87f9 1026 cv_qualifier != NULL_TREE,
6e9438cf
AG
1027 input_filename, lineno);
1028 }
1029 else
f71f87f9 1030 expand_asm (string);
a64c757e 1031
ad321293
MM
1032 finish_stmt ();
1033 }
1034}
b4c4a9ec 1035
f01b0acb
MM
1036/* Finish a label with the indicated NAME. */
1037
1038void
1039finish_label_stmt (name)
1040 tree name;
1041{
3fa56191 1042 tree decl = define_label (input_filename, lineno, name);
f01b0acb 1043
558475f0 1044 if (building_stmt_tree ())
acef433b 1045 add_tree (build_min_nt (LABEL_STMT, decl));
3fa56191
MM
1046 else if (decl)
1047 expand_label (decl);
f01b0acb
MM
1048}
1049
acef433b
MM
1050/* Finish a series of declarations for local labels. G++ allows users
1051 to declare "local" labels, i.e., labels with scope. This extension
1052 is useful when writing code involving statement-expressions. */
1053
1054void
1055finish_label_decl (name)
1056 tree name;
1057{
1058 tree decl = declare_local_label (name);
1059 if (building_stmt_tree ())
1060 add_decl_stmt (decl);
1061}
1062
9188c363
MM
1063/* Create a declaration statement for the declaration given by the
1064 DECL. */
1065
1066void
1067add_decl_stmt (decl)
1068 tree decl;
1069{
1070 tree decl_stmt;
1071
1072 /* We need the type to last until instantiation time. */
9188c363
MM
1073 decl_stmt = build_min_nt (DECL_STMT, decl);
1074 add_tree (decl_stmt);
1075}
1076
f1dedc31
MM
1077/* We're in a constructor, and have just constructed a a subobject of
1078 *THIS. CLEANUP is code to run if an exception is thrown before the
1079 end of the current function is reached. */
1080
1081void
1082finish_subobject (cleanup)
1083 tree cleanup;
1084{
1085 if (building_stmt_tree ())
1086 {
1087 tree r = build_min_nt (SUBOBJECT, cleanup);
1088 add_tree (r);
1089 }
1090 else
1091 add_partial_entry (cleanup);
1092}
1093
24bef158
MM
1094/* When DECL goes out of scope, make sure that CLEANUP is executed. */
1095
1096void
1097finish_decl_cleanup (decl, cleanup)
1098 tree decl;
1099 tree cleanup;
1100{
1101 if (building_stmt_tree ())
1102 add_tree (build_min_nt (CLEANUP_STMT, decl, cleanup));
b35d4555
MM
1103 else if (!decl
1104 || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
24bef158
MM
1105 expand_decl_cleanup (decl, cleanup);
1106}
1107
558475f0
MM
1108/* Bind a name and initialization to the return value of
1109 the current function. */
1110
1111void
1112finish_named_return_value (return_id, init)
1113 tree return_id, init;
1114{
1115 tree decl = DECL_RESULT (current_function_decl);
1116
1117 if (pedantic)
1118 /* Give this error as many times as there are occurrences,
1119 so that users can use Emacs compilation buffers to find
1120 and fix all such places. */
1121 pedwarn ("ANSI C++ does not permit named return values");
1122
1123 if (return_id != NULL_TREE)
1124 {
1125 if (DECL_NAME (decl) == NULL_TREE)
1126 {
1127 DECL_NAME (decl) = return_id;
1128 DECL_ASSEMBLER_NAME (decl) = return_id;
1129 }
1130 else
1131 {
1132 cp_error ("return identifier `%D' already in place", return_id);
1133 return;
1134 }
1135 }
1136
1137 /* Can't let this happen for constructors. */
1138 if (DECL_CONSTRUCTOR_P (current_function_decl))
1139 {
1140 error ("can't redefine default return value for constructors");
1141 return;
1142 }
1143
1144 /* If we have a named return value, put that in our scope as well. */
1145 if (DECL_NAME (decl) != NULL_TREE)
1146 {
1147 /* Let `cp_finish_decl' know that this initializer is ok. */
1148 DECL_INITIAL (decl) = init;
b35d4555
MM
1149 if (doing_semantic_analysis_p ())
1150 pushdecl (decl);
558475f0
MM
1151
1152 if (building_stmt_tree ())
2a1e9fdd 1153 add_tree (build_min_nt (RETURN_INIT, return_id, init));
558475f0
MM
1154 else
1155 {
cd9f6678 1156 cp_finish_decl (decl, init, NULL_TREE, 0);
558475f0
MM
1157 store_return_init (decl);
1158 }
1159 }
1160}
1161
1162/* Cache the value of this class's main virtual function table pointer
1163 in a register variable. This will save one indirection if a
1164 more than one virtual function call is made this function. */
1165
1166void
1167setup_vtbl_ptr ()
1168{
9bfadf57
MM
1169 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1170
1171 /* If we've already done this, there's no need to do it again. */
1172 if (vtbls_set_up_p)
1173 return;
1174
1175 if (DECL_CONSTRUCTOR_P (current_function_decl))
558475f0 1176 {
9bfadf57 1177 if (processing_template_decl)
558475f0
MM
1178 add_tree (build_min_nt
1179 (CTOR_INITIALIZER,
1180 current_member_init_list, current_base_init_list));
1181 else
9bfadf57
MM
1182 finish_expr_stmt (emit_base_init (current_class_type));
1183 }
1184 else if (DECL_DESTRUCTOR_P (current_function_decl)
1185 && !processing_template_decl)
1186 {
1187 tree binfo = TYPE_BINFO (current_class_type);
1188 tree if_stmt;
1189 tree compound_stmt;
1190
1191 /* If the dtor is empty, and we know there is not possible way we
1192 could use any vtable entries, before they are possibly set by
1193 a base class dtor, we don't have to setup the vtables, as we
1194 know that any base class dtoring will set up any vtables it
1195 needs. We avoid MI, because one base class dtor can do a
1196 virtual dispatch to an overridden function that would need to
1197 have a non-related vtable set up, we cannot avoid setting up
1198 vtables in that case. We could change this to see if there is
1199 just one vtable. */
1200 if_stmt = begin_if_stmt ();
1201
1202 /* If it is not safe to avoid setting up the vtables, then
1203 someone will change the condition to be boolean_true_node.
1204 (Actually, for now, we do not have code to set the condition
1205 appropriate, so we just assume that we always need to
1206 initialize the vtables.) */
1207 finish_if_stmt_cond (boolean_true_node, if_stmt);
1208 current_vcalls_possible_p = &IF_COND (if_stmt);
1209 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1210
1211 /* Make all virtual function table pointers in non-virtual base
1212 classes point to CURRENT_CLASS_TYPE's virtual function
1213 tables. */
1214 expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
1215
1216 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1217 expand_indirect_vtbls_init (binfo, current_class_ref,
1218 current_class_ptr);
1219
1220 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1221 finish_then_clause (if_stmt);
1222 finish_if_stmt ();
558475f0 1223 }
f1dedc31
MM
1224
1225 /* Always keep the BLOCK node associated with the outermost pair of
9bfadf57 1226 curly braces of a function. These are needed for correct
f1dedc31
MM
1227 operation of dwarfout.c. */
1228 keep_next_level (1);
9bfadf57
MM
1229
1230 /* The virtual function tables are set up now. */
1231 vtbls_set_up_p = 1;
558475f0
MM
1232}
1233
8f471b0d
MM
1234/* Add a scope-statement to the statement-tree. BEGIN_P indicates
1235 whether this statements opens or closes a scope. PARTIAL_P is true
1236 for a partial scope, i.e, the scope that begins after a label when
1237 an object that needs a cleanup is created. */
1238
1239void
1240add_scope_stmt (begin_p, partial_p)
1241 int begin_p;
1242 int partial_p;
1243{
1244 tree ss;
1245
1246 /* Build the statement. */
1247 ss = build_min_nt (SCOPE_STMT);
1248 SCOPE_BEGIN_P (ss) = begin_p;
1249 SCOPE_PARTIAL_P (ss) = partial_p;
1250
1251 /* If we're finishing a scope, figure out whether the scope was
1252 really necessary. */
1253 if (!begin_p)
1254 {
1255 SCOPE_NULLIFIED_P (ss) = !kept_level_p ();
1256 SCOPE_NULLIFIED_P (TREE_VALUE (current_scope_stmt_stack))
1257 = SCOPE_NULLIFIED_P (ss);
1258 }
1259
1260 /* Keep the scope stack up to date. */
1261 if (begin_p)
1262 current_scope_stmt_stack
1263 = tree_cons (NULL_TREE, ss, current_scope_stmt_stack);
1264 else
1265 current_scope_stmt_stack = TREE_CHAIN (current_scope_stmt_stack);
1266
1267 /* Add the new statement to the statement-tree. */
1268 add_tree (ss);
1269}
1270
558475f0
MM
1271/* Begin a new scope. */
1272
b35d4555 1273void
558475f0
MM
1274do_pushlevel ()
1275{
1276 if (!building_stmt_tree ())
1277 {
1278 emit_line_note (input_filename, lineno);
1279 clear_last_expr ();
1280 }
f1dedc31 1281 if (stmts_are_full_exprs_p)
b35d4555
MM
1282 {
1283 pushlevel (0);
1284 if (!building_stmt_tree ()
1285 && !current_function->x_whole_function_mode_p)
1286 expand_start_bindings (0);
1287 else if (building_stmt_tree () && !processing_template_decl)
8f471b0d 1288 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
b35d4555 1289 }
f1dedc31 1290}
558475f0
MM
1291
1292/* Finish a scope. */
1293
b35d4555 1294tree
558475f0
MM
1295do_poplevel ()
1296{
b35d4555 1297 tree t = NULL_TREE;
558475f0 1298
f1dedc31 1299 if (stmts_are_full_exprs_p)
b35d4555
MM
1300 {
1301 if (!building_stmt_tree ()
1302 && !current_function->x_whole_function_mode_p)
1303 expand_end_bindings (getdecls (), kept_level_p (), 0);
1304 else if (building_stmt_tree () && !processing_template_decl)
1305 {
8f471b0d 1306 add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
b35d4555
MM
1307
1308 /* When not in function-at-a-time mode, expand_end_bindings
1309 will warn about unused variables. But, in
1310 function-at-a-time mode expand_end_bindings is not passed
1311 the list of variables in the current scope, and therefore
1312 no warning is emitted. So, we explicitly warn here. */
1313 warn_about_unused_variables (getdecls ());
1314 }
1315
1316 t = poplevel (kept_level_p (), 1, 0);
1317 }
558475f0
MM
1318 return t;
1319}
1320
b4c4a9ec
MM
1321/* Finish a parenthesized expression EXPR. */
1322
1323tree
1324finish_parenthesized_expr (expr)
1325 tree expr;
1326{
1327 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1328 /* This inhibits warnings in truthvalue_conversion. */
1329 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1330
1331 return expr;
1332}
1333
b69b1501
MM
1334/* Begin a statement-expression. The value returned must be passed to
1335 finish_stmt_expr. */
b4c4a9ec
MM
1336
1337tree
1338begin_stmt_expr ()
1339{
6f80451c
MM
1340 /* If we're outside a function, we won't have a statement-tree to
1341 work with. But, if we see a statement-expression we need to
1342 create one. */
1343 if (!current_function && !last_tree)
1344 begin_stmt_tree (&scope_chain->x_saved_tree);
1345
f1dedc31 1346 keep_next_level (1);
558475f0 1347 /* If we're building a statement tree, then the upcoming compound
b69b1501
MM
1348 statement will be chained onto the tree structure, starting at
1349 last_tree. We return last_tree so that we can later unhook the
1350 compound statement. */
558475f0 1351 return building_stmt_tree () ? last_tree : expand_start_stmt_expr();
b4c4a9ec
MM
1352}
1353
1354/* Finish a statement-expression. RTL_EXPR should be the value
1355 returned by the previous begin_stmt_expr; EXPR is the
1356 statement-expression. Returns an expression representing the
1357 statement-expression. */
1358
1359tree
5ba57b55 1360finish_stmt_expr (rtl_expr)
b4c4a9ec 1361 tree rtl_expr;
b4c4a9ec
MM
1362{
1363 tree result;
1364
558475f0 1365 if (!building_stmt_tree ())
c557501d 1366 rtl_expr = expand_end_stmt_expr (rtl_expr);
b4c4a9ec 1367
558475f0 1368 if (building_stmt_tree ())
b69b1501 1369 {
f1dedc31
MM
1370 /* If the last thing in the statement-expression was not an
1371 expression-statement, then it has type `void'. */
1372 if (!last_expr_type)
1373 last_expr_type = void_type_node;
1374 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1375 TREE_SIDE_EFFECTS (result) = 1;
1376
b69b1501 1377 /* Remove the compound statement from the tree structure; it is
558475f0 1378 now saved in the STMT_EXPR. */
6f80451c 1379 SET_LAST_STMT (rtl_expr);
b69b1501
MM
1380 TREE_CHAIN (last_tree) = NULL_TREE;
1381 }
5ba57b55 1382 else
f1dedc31
MM
1383 result = rtl_expr;
1384
6f80451c
MM
1385 /* If we created a statement-tree for this statement-expression,
1386 remove it now. */
1387 if (!current_function
1388 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1389 finish_stmt_tree (&scope_chain->x_saved_tree);
1390
b4c4a9ec
MM
1391 return result;
1392}
1393
1394/* Finish a call to FN with ARGS. Returns a representation of the
1395 call. */
1396
1397tree
a759e627 1398finish_call_expr (fn, args, koenig)
b4c4a9ec
MM
1399 tree fn;
1400 tree args;
a759e627 1401 int koenig;
b4c4a9ec 1402{
a759e627
ML
1403 tree result;
1404
1405 if (koenig)
03d82991
JM
1406 {
1407 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1408 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1409 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
e13a4123 1410 fn = do_identifier (fn, 2, args);
03d82991 1411 }
a759e627 1412 result = build_x_function_call (fn, args, current_class_ref);
b4c4a9ec
MM
1413
1414 if (TREE_CODE (result) == CALL_EXPR
66543169
NS
1415 && (! TREE_TYPE (result)
1416 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
b4c4a9ec
MM
1417 result = require_complete_type (result);
1418
1419 return result;
1420}
1421
1422/* Finish a call to a postfix increment or decrement or EXPR. (Which
1423 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1424 POSTDECREMENT_EXPR.) */
1425
1426tree
1427finish_increment_expr (expr, code)
1428 tree expr;
1429 enum tree_code code;
1430{
1431 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1432 a COMPONENT_REF). This way if we've got, say, a reference to a
1433 static member that's being operated on, we don't end up trying to
1434 find a member operator for the class it's in. */
1435
1436 if (TREE_CODE (expr) == OFFSET_REF)
1437 expr = resolve_offset_ref (expr);
1438 return build_x_unary_op (code, expr);
1439}
1440
1441/* Finish a use of `this'. Returns an expression for `this'. */
1442
1443tree
1444finish_this_expr ()
1445{
1446 tree result;
1447
1448 if (current_class_ptr)
1449 {
1450#ifdef WARNING_ABOUT_CCD
1451 TREE_USED (current_class_ptr) = 1;
1452#endif
1453 result = current_class_ptr;
1454 }
1455 else if (current_function_decl
1456 && DECL_STATIC_FUNCTION_P (current_function_decl))
1457 {
8251199e 1458 error ("`this' is unavailable for static member functions");
b4c4a9ec
MM
1459 result = error_mark_node;
1460 }
1461 else
1462 {
1463 if (current_function_decl)
8251199e 1464 error ("invalid use of `this' in non-member function");
b4c4a9ec 1465 else
8251199e 1466 error ("invalid use of `this' at top level");
b4c4a9ec
MM
1467 result = error_mark_node;
1468 }
1469
1470 return result;
1471}
1472
1473/* Finish a member function call using OBJECT and ARGS as arguments to
1474 FN. Returns an expression for the call. */
1475
1476tree
1477finish_object_call_expr (fn, object, args)
1478 tree fn;
1479 tree object;
1480 tree args;
1481{
1482#if 0
1483 /* This is a future direction of this code, but because
1484 build_x_function_call cannot always undo what is done in
1485 build_component_ref entirely yet, we cannot do this. */
1486
1487 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1488 return finish_call_expr (real_fn, args);
1489#else
c219b878 1490 if (DECL_DECLARES_TYPE_P (fn))
c68c56f7
MM
1491 {
1492 if (processing_template_decl)
1493 /* This can happen on code like:
1494
1495 class X;
1496 template <class T> void f(T t) {
1497 t.X();
1498 }
1499
1500 We just grab the underlying IDENTIFIER. */
1501 fn = DECL_NAME (fn);
1502 else
1503 {
8251199e 1504 cp_error ("calling type `%T' like a method", fn);
c68c56f7
MM
1505 return error_mark_node;
1506 }
1507 }
1508
b4c4a9ec
MM
1509 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1510#endif
1511}
1512
1513/* Finish a qualified member function call using OBJECT and ARGS as
1514 arguments to FN. Returns an expressino for the call. */
1515
1516tree
1517finish_qualified_object_call_expr (fn, object, args)
1518 tree fn;
1519 tree object;
1520 tree args;
1521{
6eabb241
MM
1522 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1523 TREE_OPERAND (fn, 1), args);
b4c4a9ec
MM
1524}
1525
1526/* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1527 being the scope, if any, of DESTRUCTOR. Returns an expression for
1528 the call. */
1529
1530tree
1531finish_pseudo_destructor_call_expr (object, scope, destructor)
1532 tree object;
1533 tree scope;
1534 tree destructor;
1535{
40242ccf
MM
1536 if (processing_template_decl)
1537 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1538
b4c4a9ec 1539 if (scope && scope != destructor)
8251199e 1540 cp_error ("destructor specifier `%T::~%T()' must have matching names",
b4c4a9ec
MM
1541 scope, destructor);
1542
1543 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1544 && (TREE_CODE (TREE_TYPE (object)) !=
1545 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
8251199e 1546 cp_error ("`%E' is not of type `%T'", object, destructor);
b4c4a9ec
MM
1547
1548 return cp_convert (void_type_node, object);
1549}
1550
1551/* Finish a call to a globally qualified member function FN using
1552 ARGS. Returns an expression for the call. */
1553
1554tree
75d587eb 1555finish_qualified_call_expr (fn, args)
b4c4a9ec
MM
1556 tree fn;
1557 tree args;
1558{
1559 if (processing_template_decl)
2a1e9fdd 1560 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
b4c4a9ec
MM
1561 else
1562 return build_member_call (TREE_OPERAND (fn, 0),
1563 TREE_OPERAND (fn, 1),
1564 args);
1565}
1566
1567/* Finish an expression taking the address of LABEL. Returns an
1568 expression for the address. */
1569
1570tree
1571finish_label_address_expr (label)
1572 tree label;
1573{
1574 tree result;
1575
1576 label = lookup_label (label);
1577 if (label == NULL_TREE)
1578 result = null_pointer_node;
1579 else
1580 {
1581 TREE_USED (label) = 1;
1582 result = build1 (ADDR_EXPR, ptr_type_node, label);
1583 TREE_CONSTANT (result) = 1;
1584 }
1585
1586 return result;
1587}
1588
ce4a0391
MM
1589/* Finish an expression of the form CODE EXPR. */
1590
1591tree
1592finish_unary_op_expr (code, expr)
1593 enum tree_code code;
1594 tree expr;
1595{
1596 tree result = build_x_unary_op (code, expr);
7c355bca
ML
1597 /* Inside a template, build_x_unary_op does not fold the
1598 expression. So check whether the result is folded before
1599 setting TREE_NEGATED_INT. */
1600 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1601 && TREE_CODE (result) == INTEGER_CST)
ce4a0391
MM
1602 TREE_NEGATED_INT (result) = 1;
1603 overflow_warning (result);
1604 return result;
1605}
1606
1607/* Finish an id-expression. */
1608
1609tree
1610finish_id_expr (expr)
1611 tree expr;
1612{
1613 if (TREE_CODE (expr) == IDENTIFIER_NODE)
a759e627 1614 expr = do_identifier (expr, 1, NULL_TREE);
ce4a0391
MM
1615
1616 return expr;
1617}
1618
b4c4a9ec
MM
1619/* Begin a function defniition declared with DECL_SPECS and
1620 DECLARATOR. Returns non-zero if the function-declaration is
1621 legal. */
1622
1623int
1624begin_function_definition (decl_specs, declarator)
1625 tree decl_specs;
1626 tree declarator;
1627{
1628 tree specs;
1629 tree attrs;
1630 split_specs_attrs (decl_specs, &specs, &attrs);
a8f73d4b 1631 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
b4c4a9ec
MM
1632 return 0;
1633
1634 reinit_parse_for_function ();
39c01e4c
MM
1635 /* The things we're about to see are not directly qualified by any
1636 template headers we've seen thus far. */
1637 reset_specialization ();
1638
b4c4a9ec
MM
1639 return 1;
1640}
1641
1642/* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1643 a SCOPE_REF. */
1644
1645tree
1646begin_constructor_declarator (scope, name)
1647 tree scope;
1648 tree name;
1649{
1650 tree result = build_parse_node (SCOPE_REF, scope, name);
830fcda8 1651 enter_scope_of (result);
b4c4a9ec
MM
1652 return result;
1653}
1654
ce4a0391
MM
1655/* Finish an init-declarator. Returns a DECL. */
1656
1657tree
1658finish_declarator (declarator, declspecs, attributes,
1659 prefix_attributes, initialized)
1660 tree declarator;
1661 tree declspecs;
1662 tree attributes;
1663 tree prefix_attributes;
1664 int initialized;
1665{
1666 return start_decl (declarator, declspecs, initialized, attributes,
1667 prefix_attributes);
1668}
1669
8014a339 1670/* Finish a translation unit. */
ce4a0391
MM
1671
1672void
1673finish_translation_unit ()
1674{
1675 /* In case there were missing closebraces,
1676 get us back to the global binding level. */
273a708f 1677 pop_everything ();
ce4a0391
MM
1678 while (current_namespace != global_namespace)
1679 pop_namespace ();
1680 finish_file ();
1681}
1682
b4c4a9ec
MM
1683/* Finish a template type parameter, specified as AGGR IDENTIFIER.
1684 Returns the parameter. */
1685
1686tree
1687finish_template_type_parm (aggr, identifier)
1688 tree aggr;
1689 tree identifier;
1690{
6eabb241 1691 if (aggr != class_type_node)
b4c4a9ec 1692 {
8251199e 1693 pedwarn ("template type parameters must use the keyword `class' or `typename'");
b4c4a9ec
MM
1694 aggr = class_type_node;
1695 }
1696
1697 return build_tree_list (aggr, identifier);
1698}
1699
1700/* Finish a template template parameter, specified as AGGR IDENTIFIER.
1701 Returns the parameter. */
1702
1703tree
1704finish_template_template_parm (aggr, identifier)
1705 tree aggr;
1706 tree identifier;
1707{
1708 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1709 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1710 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1711 DECL_TEMPLATE_RESULT (tmpl) = decl;
1712 SET_DECL_ARTIFICIAL (decl);
1713 end_template_decl ();
1714
1715 return finish_template_type_parm (aggr, tmpl);
1716}
ce4a0391
MM
1717
1718/* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1719 non-zero, the parameter list was terminated by a `...'. */
1720
1721tree
1722finish_parmlist (parms, ellipsis)
1723 tree parms;
1724 int ellipsis;
1725{
1726 if (!ellipsis)
1727 chainon (parms, void_list_node);
1728 /* We mark the PARMS as a parmlist so that declarator processing can
1729 disambiguate certain constructs. */
1730 if (parms != NULL_TREE)
1731 TREE_PARMLIST (parms) = 1;
1732
1733 return parms;
1734}
1735
1736/* Begin a class definition, as indicated by T. */
1737
1738tree
1739begin_class_definition (t)
1740 tree t;
1741{
ce4a0391
MM
1742 if (t == error_mark_node
1743 || ! IS_AGGR_TYPE (t))
1744 {
830fcda8 1745 t = make_lang_type (RECORD_TYPE);
ce4a0391
MM
1746 pushtag (make_anon_name (), t, 0);
1747 }
830fcda8
JM
1748
1749 /* In a definition of a member class template, we will get here with an
1750 implicit typename, a TYPENAME_TYPE with a type. */
1751 if (TREE_CODE (t) == TYPENAME_TYPE)
1752 t = TREE_TYPE (t);
4c571114
MM
1753
1754 /* If we generated a partial instantiation of this type, but now
1755 we're seeing a real definition, we're actually looking at a
1756 partial specialization. Consider:
1757
1758 template <class T, class U>
1759 struct Y {};
1760
1761 template <class T>
1762 struct X {};
1763
1764 template <class T, class U>
1765 void f()
1766 {
1767 typename X<Y<T, U> >::A a;
1768 }
1769
1770 template <class T, class U>
1771 struct X<Y<T, U> >
1772 {
1773 };
1774
1775 We have to undo the effects of the previous partial
1776 instantiation. */
1777 if (PARTIAL_INSTANTIATION_P (t))
1778 {
1779 if (!pedantic)
1780 {
1781 /* Unfortunately, when we're not in pedantic mode, we
1782 attempt to actually fill in some of the fields of the
1783 partial instantiation, in order to support the implicit
1784 typename extension. Clear those fields now, in
1785 preparation for the definition here. The fields cleared
1786 here must match those set in instantiate_class_template.
1787 Look for a comment mentioning begin_class_definition
1788 there. */
1789 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1790 TYPE_FIELDS (t) = NULL_TREE;
1791 TYPE_METHODS (t) = NULL_TREE;
1792 CLASSTYPE_TAGS (t) = NULL_TREE;
1793 TYPE_SIZE (t) = NULL_TREE;
1794 }
830fcda8 1795
4c571114
MM
1796 /* This isn't a partial instantiation any more. */
1797 PARTIAL_INSTANTIATION_P (t) = 0;
1798 }
1799 /* If this type was already complete, and we see another definition,
1800 that's an error. */
1801 else if (TYPE_SIZE (t))
ce4a0391 1802 duplicate_tag_error (t);
4c571114 1803
b4f70b3d
NS
1804 /* Update the location of the decl. */
1805 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1806 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1807
4c571114 1808 if (TYPE_BEING_DEFINED (t))
ce4a0391
MM
1809 {
1810 t = make_lang_type (TREE_CODE (t));
1811 pushtag (TYPE_IDENTIFIER (t), t, 0);
ce4a0391 1812 }
ff350acd 1813 maybe_process_partial_specialization (t);
8f032717 1814 pushclass (t, 1);
ce4a0391 1815 TYPE_BEING_DEFINED (t) = 1;
ce4a0391
MM
1816 /* Reset the interface data, at the earliest possible
1817 moment, as it might have been set via a class foo;
1818 before. */
6eabb241
MM
1819 {
1820 tree name = TYPE_IDENTIFIER (t);
1821
1822 if (! ANON_AGGRNAME_P (name))
1823 {
1824 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1825 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1826 (t, interface_unknown);
1827 }
1828
1829 /* Only leave this bit clear if we know this
1830 class is part of an interface-only specification. */
1831 if (! CLASSTYPE_INTERFACE_KNOWN (t)
1832 || ! CLASSTYPE_INTERFACE_ONLY (t))
1833 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1834 }
ce4a0391 1835#if 0
830fcda8
JM
1836 tmp = TYPE_IDENTIFIER ($<ttype>0);
1837 if (tmp && IDENTIFIER_TEMPLATE (tmp))
1838 overload_template_name (tmp, 1);
ce4a0391
MM
1839#endif
1840 reset_specialization();
1841
b7975aed
MM
1842 /* Make a declaration for this class in its own scope. */
1843 build_self_reference ();
1844
830fcda8 1845 return t;
ce4a0391
MM
1846}
1847
61a127b3
MM
1848/* Finish the member declaration given by DECL. */
1849
1850void
1851finish_member_declaration (decl)
1852 tree decl;
1853{
1854 if (decl == error_mark_node || decl == NULL_TREE)
1855 return;
1856
1857 if (decl == void_type_node)
1858 /* The COMPONENT was a friend, not a member, and so there's
1859 nothing for us to do. */
1860 return;
1861
1862 /* We should see only one DECL at a time. */
1863 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1864
1865 /* Set up access control for DECL. */
1866 TREE_PRIVATE (decl)
1867 = (current_access_specifier == access_private_node);
1868 TREE_PROTECTED (decl)
1869 = (current_access_specifier == access_protected_node);
1870 if (TREE_CODE (decl) == TEMPLATE_DECL)
1871 {
1872 TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1873 TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1874 }
1875
1876 /* Mark the DECL as a member of the current class. */
1877 if (TREE_CODE (decl) == FUNCTION_DECL
1878 || DECL_FUNCTION_TEMPLATE_P (decl))
1879 /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in
1880 finish_struct. Presumably it is already set as the function is
1881 parsed. Perhaps DECL_CLASS_CONTEXT is already set, too? */
1882 DECL_CLASS_CONTEXT (decl) = current_class_type;
61a127b3
MM
1883 else
1884 DECL_CONTEXT (decl) = current_class_type;
1885
1886 /* Put functions on the TYPE_METHODS list and everything else on the
1887 TYPE_FIELDS list. Note that these are built up in reverse order.
1888 We reverse them (to obtain declaration order) in finish_struct. */
1889 if (TREE_CODE (decl) == FUNCTION_DECL
1890 || DECL_FUNCTION_TEMPLATE_P (decl))
1891 {
1892 /* We also need to add this function to the
1893 CLASSTYPE_METHOD_VEC. */
1894 add_method (current_class_type, 0, decl);
1895
1896 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1897 TYPE_METHODS (current_class_type) = decl;
1898 }
1899 else
1900 {
1901 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1902 go at the beginning. The reason is that lookup_field_1
1903 searches the list in order, and we want a field name to
1904 override a type name so that the "struct stat hack" will
1905 work. In particular:
1906
1907 struct S { enum E { }; int E } s;
1908 s.E = 3;
1909
1910 is legal. In addition, the FIELD_DECLs must be maintained in
1911 declaration order so that class layout works as expected.
1912 However, we don't need that order until class layout, so we
1913 save a little time by putting FIELD_DECLs on in reverse order
1914 here, and then reversing them in finish_struct_1. (We could
1915 also keep a pointer to the correct insertion points in the
1916 list.) */
1917
1918 if (TREE_CODE (decl) == TYPE_DECL)
1919 TYPE_FIELDS (current_class_type)
1920 = chainon (TYPE_FIELDS (current_class_type), decl);
1921 else
1922 {
1923 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1924 TYPE_FIELDS (current_class_type) = decl;
1925 }
8f032717
MM
1926
1927 /* Enter the DECL into the scope of the class. */
1928 if (TREE_CODE (decl) != USING_DECL)
1929 pushdecl_class_level (decl);
61a127b3
MM
1930 }
1931}
1932
1933/* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1934 the definition is immediately followed by a semicolon. Returns the
1935 type. */
ce4a0391
MM
1936
1937tree
fbdd0024 1938finish_class_definition (t, attributes, semi, pop_scope_p)
ce4a0391 1939 tree t;
ce4a0391
MM
1940 tree attributes;
1941 int semi;
fbdd0024 1942 int pop_scope_p;
ce4a0391 1943{
ce4a0391
MM
1944 /* finish_struct nukes this anyway; if finish_exception does too,
1945 then it can go. */
1946 if (semi)
1947 note_got_semicolon (t);
1948
dc8263bc
JM
1949 /* If we got any attributes in class_head, xref_tag will stick them in
1950 TREE_TYPE of the type. Grab them now. */
1951 attributes = chainon (TREE_TYPE (t), attributes);
1952 TREE_TYPE (t) = NULL_TREE;
1953
ce4a0391
MM
1954 if (TREE_CODE (t) == ENUMERAL_TYPE)
1955 ;
1956 else
1957 {
9f33663b 1958 t = finish_struct (t, attributes);
ce4a0391
MM
1959 if (semi)
1960 note_got_semicolon (t);
1961 }
1962
ce4a0391
MM
1963 if (! semi)
1964 check_for_missing_semicolon (t);
fbdd0024
MM
1965 if (pop_scope_p)
1966 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
ce4a0391
MM
1967 if (current_scope () == current_function_decl)
1968 do_pending_defargs ();
1969
1970 return t;
1971}
1972
1973/* Finish processing the default argument expressions cached during
1974 the processing of a class definition. */
1975
1976void
51632249 1977begin_inline_definitions ()
ce4a0391
MM
1978{
1979 if (pending_inlines
1980 && current_scope () == current_function_decl)
1981 do_pending_inlines ();
1982}
1983
1984/* Finish processing the inline function definitions cached during the
1985 processing of a class definition. */
1986
1987void
51632249 1988finish_inline_definitions ()
ce4a0391
MM
1989{
1990 if (current_class_type == NULL_TREE)
1991 clear_inline_text_obstack ();
ce4a0391 1992}
35acd3f2
MM
1993
1994/* Finish processing the declaration of a member class template
1995 TYPES whose template parameters are given by PARMS. */
1996
1997tree
61a127b3 1998finish_member_class_template (types)
35acd3f2
MM
1999 tree types;
2000{
36a117a5
MM
2001 tree t;
2002
2003 /* If there are declared, but undefined, partial specializations
2004 mixed in with the typespecs they will not yet have passed through
2005 maybe_process_partial_specialization, so we do that here. */
2006 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2007 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2008 maybe_process_partial_specialization (TREE_VALUE (t));
2009
35acd3f2 2010 note_list_got_semicolon (types);
61a127b3 2011 grok_x_components (types);
35acd3f2
MM
2012 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2013 /* The component was in fact a friend declaration. We avoid
2014 finish_member_template_decl performing certain checks by
2015 unsetting TYPES. */
2016 types = NULL_TREE;
61a127b3
MM
2017
2018 finish_member_template_decl (types);
2019
35acd3f2
MM
2020 /* As with other component type declarations, we do
2021 not store the new DECL on the list of
2022 component_decls. */
2023 return NULL_TREE;
2024}
36a117a5
MM
2025
2026/* Finish processsing a complete template declaration. The PARMS are
2027 the template parameters. */
2028
2029void
2030finish_template_decl (parms)
2031 tree parms;
2032{
2033 if (parms)
2034 end_template_decl ();
2035 else
2036 end_specialization ();
2037}
2038
2039/* Finish processing a a template-id (which names a type) of the form
2040 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2041 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2042 the scope of template-id indicated. */
2043
2044tree
2045finish_template_type (name, args, entering_scope)
2046 tree name;
2047 tree args;
2048 int entering_scope;
2049{
2050 tree decl;
2051
2052 decl = lookup_template_class (name, args,
2053 NULL_TREE, NULL_TREE, entering_scope);
2054 if (decl != error_mark_node)
2055 decl = TYPE_STUB_DECL (decl);
2056
2057 return decl;
2058}
648f19f6
MM
2059
2060/* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2061 namespace scope or a class scope. */
2062
2063void
2064enter_scope_of (sr)
2065 tree sr;
2066{
2067 tree scope = TREE_OPERAND (sr, 0);
2068
2069 if (TREE_CODE (scope) == NAMESPACE_DECL)
2070 {
2071 push_decl_namespace (scope);
2072 TREE_COMPLEXITY (sr) = -1;
2073 }
2074 else if (scope != current_class_type)
2075 {
830fcda8
JM
2076 if (TREE_CODE (scope) == TYPENAME_TYPE)
2077 {
2078 /* In a declarator for a template class member, the scope will
2079 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2080 scope = TREE_TYPE (scope);
2081 TREE_OPERAND (sr, 0) = scope;
2082 }
648f19f6
MM
2083 push_nested_class (scope, 3);
2084 TREE_COMPLEXITY (sr) = current_class_depth;
2085 }
2086}
ea6021e8
MM
2087
2088/* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2089 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2090 BASE_CLASS, or NULL_TREE if an error occurred. The
2091 ACCESSS_SPECIFIER is one of
2092 access_{default,public,protected_private}[_virtual]_node.*/
2093
2094tree
6eabb241 2095finish_base_specifier (access_specifier, base_class)
ea6021e8
MM
2096 tree access_specifier;
2097 tree base_class;
ea6021e8
MM
2098{
2099 tree type;
2100 tree result;
2101
2102 if (base_class == NULL_TREE)
2103 {
8251199e 2104 error ("invalid base class");
ea6021e8
MM
2105 type = error_mark_node;
2106 }
2107 else
2108 type = TREE_TYPE (base_class);
6eabb241 2109
ea6021e8
MM
2110 if (! is_aggr_type (type, 1))
2111 result = NULL_TREE;
ea6021e8
MM
2112 else
2113 result = build_tree_list (access_specifier, type);
2114
2115 return result;
2116}
61a127b3
MM
2117
2118/* Called when multiple declarators are processed. If that is not
2119 premitted in this context, an error is issued. */
2120
2121void
2122check_multiple_declarators ()
2123{
2124 /* [temp]
2125
2126 In a template-declaration, explicit specialization, or explicit
2127 instantiation the init-declarator-list in the declaration shall
2128 contain at most one declarator.
2129
2130 We don't just use PROCESSING_TEMPLATE_DECL for the first
2131 condition since that would disallow the perfectly legal code,
2132 like `template <class T> struct S { int i, j; };'. */
2133 tree scope = current_scope ();
2134
2135 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2136 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2137 return;
2138
2139 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2140 || processing_explicit_instantiation
2141 || processing_specialization)
2142 cp_error ("multiple declarators in template declaration");
2143}
2144
b894fc05
JM
2145tree
2146finish_typeof (expr)
2147 tree expr;
2148{
2149 if (processing_template_decl)
2150 {
2151 tree t;
2152
b894fc05 2153 t = make_lang_type (TYPEOF_TYPE);
b894fc05 2154 TYPE_FIELDS (t) = expr;
b894fc05
JM
2155
2156 return t;
2157 }
2158
2159 return TREE_TYPE (expr);
2160}
558475f0 2161
6f80451c 2162/* Create an empty statement tree rooted at T. */
558475f0
MM
2163
2164void
6f80451c
MM
2165begin_stmt_tree (t)
2166 tree *t;
558475f0
MM
2167{
2168 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2169 what follows. We remove the extraneous statement in
2170 finish_stmt_tree. */
6f80451c
MM
2171 *t = build_nt (EXPR_STMT, void_zero_node);
2172 SET_LAST_STMT (*t);
558475f0
MM
2173 last_expr_type = NULL_TREE;
2174}
2175
6f80451c 2176/* Finish the statement tree rooted at T. */
558475f0
MM
2177
2178void
6f80451c
MM
2179finish_stmt_tree (t)
2180 tree *t;
558475f0 2181{
a7e4cfa0
MM
2182 tree stmt;
2183
2184 /* Remove the fake extra statement added in begin_stmt_tree. */
6f80451c
MM
2185 stmt = TREE_CHAIN (*t);
2186 *t = stmt;
2187 SET_LAST_STMT (NULL_TREE);
a7e4cfa0 2188
6f80451c
MM
2189 if (current_function)
2190 {
2191 /* The line-number recorded in the outermost statement in a function
2192 is the line number of the end of the function. */
2193 STMT_LINENO (stmt) = lineno;
2194 STMT_LINENO_FOR_FN_P (stmt) = 1;
2195 }
a7e4cfa0
MM
2196}
2197
2198/* We're about to expand T, a statement. Set up appropriate context
2199 for the substitution. */
2200
2201void
2202prep_stmt (t)
2203 tree t;
2204{
2205 if (!STMT_LINENO_FOR_FN_P (t))
2206 lineno = STMT_LINENO (t);
2207 stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
558475f0
MM
2208}
2209
527f0080
MM
2210/* Some statements, like for-statements or if-statements, require a
2211 condition. This condition can be a declaration. If T is such a
2212 declaration it is processed, and an expression appropriate to use
2213 as the condition is returned. Otherwise, T itself is returned. */
2214
2215static tree
2216expand_cond (t)
2217 tree t;
2218{
ed5511d9 2219 if (t && TREE_CODE (t) == TREE_LIST)
527f0080 2220 {
ed5511d9
MM
2221 expand_stmt (TREE_PURPOSE (t));
2222 return TREE_VALUE (t);
527f0080
MM
2223 }
2224 else
2225 return t;
2226}
2227
62409b39
MM
2228/* Generate RTL for the statement T, and its substatements, and any
2229 other statements at its nesting level. */
558475f0
MM
2230
2231tree
2232expand_stmt (t)
2233 tree t;
2234{
47d7090e 2235 tree rval = NULL_TREE;
a7e4cfa0 2236
62409b39
MM
2237 while (t && t != error_mark_node)
2238 {
2239 int saved_stmts_are_full_exprs_p;
558475f0 2240
62409b39
MM
2241 /* Assume we'll have nothing to return. */
2242 rval = NULL_TREE;
a7e4cfa0 2243
62409b39
MM
2244 /* Set up context appropriately for handling this statement. */
2245 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
2246 prep_stmt (t);
a7e4cfa0 2247
62409b39
MM
2248 switch (TREE_CODE (t))
2249 {
2250 case RETURN_STMT:
2251 finish_return_stmt (RETURN_EXPR (t));
2252 break;
558475f0 2253
62409b39 2254 case EXPR_STMT:
9bfadf57 2255 finish_expr_stmt (EXPR_STMT_EXPR (t));
62409b39 2256 break;
558475f0 2257
62409b39 2258 case DECL_STMT:
acef433b 2259 {
62409b39 2260 tree decl;
62409b39
MM
2261
2262 emit_line_note (input_filename, lineno);
2263 decl = DECL_STMT_DECL (t);
b35d4555
MM
2264 /* If this is a declaration for an automatic local
2265 variable, initialize it. Note that we might also see a
2266 declaration for a namespace-scope object (declared with
9ed9e79a
MM
2267 `extern'). We don't have to handle the initialization
2268 of those objects here; they can only be declarations,
2269 rather than definitions. */
b35d4555
MM
2270 if (TREE_CODE (decl) == VAR_DECL
2271 && !TREE_STATIC (decl)
2272 && !DECL_EXTERNAL (decl))
0fa5e05c
MM
2273 {
2274 /* Let the back-end know about this variable. */
2275 if (!ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
2276 emit_local_var (decl);
2277 else
2278 expand_anon_union_decl (decl, NULL_TREE,
2279 DECL_ANON_UNION_ELEMS (decl));
2280 }
9ed9e79a 2281 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
f3010146 2282 make_rtl_for_local_static (decl);
acef433b 2283 }
62409b39 2284 break;
558475f0 2285
24bef158
MM
2286 case CLEANUP_STMT:
2287 finish_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2288 break;
2289
b35d4555
MM
2290 case START_CATCH_STMT:
2291 begin_catch_block (TREE_TYPE (t));
2292 break;
2293
62409b39
MM
2294 case FOR_STMT:
2295 {
2296 tree tmp;
2297
2298 begin_for_stmt ();
2299 expand_stmt (FOR_INIT_STMT (t));
2300 finish_for_init_stmt (NULL_TREE);
2301 finish_for_cond (expand_cond (FOR_COND (t)), NULL_TREE);
2302 tmp = FOR_EXPR (t);
2303 finish_for_expr (tmp, NULL_TREE);
2304 expand_stmt (FOR_BODY (t));
2305 finish_for_stmt (tmp, NULL_TREE);
2306 }
2307 break;
558475f0 2308
62409b39
MM
2309 case WHILE_STMT:
2310 {
2311 begin_while_stmt ();
2312 finish_while_stmt_cond (expand_cond (WHILE_COND (t)), NULL_TREE);
2313 expand_stmt (WHILE_BODY (t));
2314 finish_while_stmt (NULL_TREE);
2315 }
2316 break;
558475f0 2317
62409b39
MM
2318 case DO_STMT:
2319 {
2320 begin_do_stmt ();
2321 expand_stmt (DO_BODY (t));
2322 finish_do_body (NULL_TREE);
2323 finish_do_stmt (DO_COND (t), NULL_TREE);
2324 }
2325 break;
527f0080 2326
62409b39
MM
2327 case IF_STMT:
2328 begin_if_stmt ();
2329 finish_if_stmt_cond (expand_cond (IF_COND (t)), NULL_TREE);
2330 if (THEN_CLAUSE (t))
2331 {
2332 expand_stmt (THEN_CLAUSE (t));
2333 finish_then_clause (NULL_TREE);
2334 }
2335 if (ELSE_CLAUSE (t))
2336 {
2337 begin_else_clause ();
2338 expand_stmt (ELSE_CLAUSE (t));
2339 finish_else_clause (NULL_TREE);
2340 }
2341 finish_if_stmt ();
2342 break;
558475f0 2343
62409b39
MM
2344 case COMPOUND_STMT:
2345 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2346 expand_stmt (COMPOUND_BODY (t));
2347 rval = finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
2348 NULL_TREE);
2349 break;
558475f0 2350
62409b39
MM
2351 case BREAK_STMT:
2352 finish_break_stmt ();
2353 break;
558475f0 2354
62409b39
MM
2355 case CONTINUE_STMT:
2356 finish_continue_stmt ();
2357 break;
558475f0 2358
62409b39
MM
2359 case SWITCH_STMT:
2360 {
2361 tree cond;
558475f0 2362
62409b39
MM
2363 begin_switch_stmt ();
2364 cond = expand_cond (SWITCH_COND (t));
2365 finish_switch_cond (cond, NULL_TREE);
2366 expand_stmt (SWITCH_BODY (t));
2367 finish_switch_stmt (cond, NULL_TREE);
2368 }
2369 break;
2370
2371 case CASE_LABEL:
2372 finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2373 break;
2374
2375 case LABEL_STMT:
b35d4555 2376 expand_label (LABEL_STMT_LABEL (t));
62409b39
MM
2377 break;
2378
2379 case GOTO_STMT:
b35d4555 2380 finish_goto_stmt (GOTO_DESTINATION (t));
62409b39
MM
2381 break;
2382
2383 case ASM_STMT:
2384 finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2385 (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2386 break;
2387
2388 case TRY_BLOCK:
2389 if (CLEANUP_P (t))
2390 {
2391 expand_eh_region_start ();
2392 expand_stmt (TRY_STMTS (t));
2393 finish_cleanup_try_block (NULL_TREE);
2394 finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2395 }
2396 else
2397 {
b35d4555
MM
2398 if (FN_TRY_BLOCK_P (t))
2399 begin_function_try_block ();
2400 else
2401 begin_try_block ();
2402
62409b39 2403 expand_stmt (TRY_STMTS (t));
b35d4555
MM
2404
2405 if (FN_TRY_BLOCK_P (t))
2406 {
2407 finish_function_try_block (NULL_TREE);
2408 expand_stmt (TRY_HANDLERS (t));
2409 finish_function_handler_sequence (NULL_TREE);
2410 }
2411 else
2412 {
2413 finish_try_block (NULL_TREE);
2414 expand_stmt (TRY_HANDLERS (t));
2415 finish_handler_sequence (NULL_TREE);
2416 }
62409b39
MM
2417 }
2418 break;
2419
2420 case HANDLER:
2421 begin_handler ();
62409b39 2422 expand_stmt (HANDLER_BODY (t));
b35d4555 2423 finish_handler (NULL_TREE, NULL_TREE);
62409b39
MM
2424 break;
2425
2426 case SUBOBJECT:
2427 finish_subobject (SUBOBJECT_CLEANUP (t));
2428 break;
2429
b35d4555
MM
2430 case SCOPE_STMT:
2431 if (SCOPE_BEGIN_P (t))
2432 expand_start_bindings (2 * SCOPE_NULLIFIED_P (t));
2433 else if (SCOPE_END_P (t))
8f471b0d
MM
2434 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t),
2435 SCOPE_PARTIAL_P (t));
b35d4555
MM
2436 break;
2437
b35d4555
MM
2438 case RETURN_INIT:
2439 /* Clear this out so that finish_named_return_value can set it
2440 again. */
2441 DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
2442 finish_named_return_value (TREE_OPERAND (t, 0),
2443 TREE_OPERAND (t, 1));
2444 break;
2445
62409b39
MM
2446 default:
2447 my_friendly_abort (19990810);
2448 break;
f1dedc31 2449 }
558475f0 2450
62409b39
MM
2451 /* Restore saved state. */
2452 stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
558475f0 2453
62409b39
MM
2454 /* Go on to the next statement in this scope. */
2455 t = TREE_CHAIN (t);
558475f0
MM
2456 }
2457
a7e4cfa0 2458 return rval;
558475f0
MM
2459}
2460
2461/* Generate RTL for FN. */
2462
2463void
2464expand_body (fn)
2465 tree fn;
2466{
62409b39
MM
2467 int saved_lineno;
2468 char *saved_input_filename;
558475f0 2469
62409b39
MM
2470 /* When the parser calls us after finishing the body of a template
2471 function, we don't really want to expand the body. When we're
2472 processing an in-class definition of an inline function,
2473 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2474 to look at the function itself. */
2475 if (processing_template_decl
2476 || (DECL_LANG_SPECIFIC (fn)
2477 && DECL_TEMPLATE_INFO (fn)
2478 && uses_template_parms (DECL_TI_ARGS (fn))))
d658cd4c
MM
2479 {
2480 /* Normally, collection only occurs in rest_of_compilation. So,
2481 if we don't collect here, we never collect junk generated
2482 during the processing of templates until we hit a
2483 non-template function. */
2484 ggc_collect ();
2485 return;
2486 }
62409b39 2487
84df082b
MM
2488 /* There's no reason to do any of the work here if we're only doing
2489 semantic analysis; this code just generates RTL. */
2490 if (flag_syntax_only)
2491 return;
2492
62409b39 2493 /* Save the current file name and line number. When we expand the
84df082b 2494 body of the function, we'll set LINENO and INPUT_FILENAME so that
62409b39
MM
2495 error-mesages come out in the right places. */
2496 saved_lineno = lineno;
2497 saved_input_filename = input_filename;
2498 lineno = DECL_SOURCE_LINE (fn);
2499 input_filename = DECL_SOURCE_FILE (fn);
2500
a8f73d4b 2501 start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
558475f0
MM
2502 store_parm_decls ();
2503
24bef158
MM
2504 /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2505 any of the other magic variables we set up when starting a
2506 function body. */
2507 current_function_name_declared = 1;
2508
558475f0 2509 /* Expand the body. */
b35d4555 2510 expand_stmt (DECL_SAVED_TREE (fn));
558475f0 2511
62409b39
MM
2512 /* Statements should always be full-expressions at the outermost set
2513 of curly braces for a function. */
2514 my_friendly_assert (stmts_are_full_exprs_p, 19990831);
2515
2516 /* The outermost statement for a function contains the line number
2517 recorded when we finished processing the function. */
2518 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2519
2520 /* Generate code for the function. */
fdfcc44c 2521 finish_function (lineno, 0);
62409b39 2522
d658cd4c
MM
2523 /* We don't need the body any more. Allow it to be garbage
2524 collected. We can't do this if we're going to dump everything. */
2525 if (!flag_dump_translation_unit)
2526 DECL_SAVED_TREE (fn) = NULL_TREE;
2527
62409b39
MM
2528 /* And restore the current source position. */
2529 lineno = saved_lineno;
2530 input_filename = saved_input_filename;
558475f0 2531}