]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/semantics.c
ButtonUI.java, [...]: New versions from classpath.
[thirdparty/gcc.git] / gcc / cp / semantics.c
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
6 Copyright (C) 1998, 1999, 2000, 2001, 2002,
7 2003 Free Software Foundation, Inc.
8 Written by Mark Mitchell (mmitchell@usa.net) based on code found
9 formerly in parse.y and pt.c.
10
11 This file is part of GCC.
12
13 GCC is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
16 any later version.
17
18 GCC is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING. If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 02111-1307, USA. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "tree-inline.h"
35 #include "except.h"
36 #include "lex.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "rtl.h"
40 #include "expr.h"
41 #include "output.h"
42 #include "timevar.h"
43 #include "debug.h"
44
45 /* There routines provide a modular interface to perform many parsing
46 operations. They may therefore be used during actual parsing, or
47 during template instantiation, which may be regarded as a
48 degenerate form of parsing. Since the current g++ parser is
49 lacking in several respects, and will be reimplemented, we are
50 attempting to move most code that is not directly related to
51 parsing into this file; that will make implementing the new parser
52 much easier since it will be able to make use of these routines. */
53
54 static tree maybe_convert_cond PARAMS ((tree));
55 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
56 static void emit_associated_thunks PARAMS ((tree));
57 static void genrtl_try_block PARAMS ((tree));
58 static void genrtl_eh_spec_block PARAMS ((tree));
59 static void genrtl_handler PARAMS ((tree));
60 static void cp_expand_stmt PARAMS ((tree));
61 static void genrtl_start_function PARAMS ((tree));
62 static void genrtl_finish_function PARAMS ((tree));
63 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
64
65 /* Finish processing the COND, the SUBSTMT condition for STMT. */
66
67 #define FINISH_COND(COND, STMT, SUBSTMT) \
68 do { \
69 if (last_tree != (STMT)) \
70 { \
71 RECHAIN_STMTS (STMT, SUBSTMT); \
72 if (!processing_template_decl) \
73 { \
74 (COND) = build_tree_list (SUBSTMT, COND); \
75 (SUBSTMT) = (COND); \
76 } \
77 } \
78 else \
79 (SUBSTMT) = (COND); \
80 } while (0)
81
82 /* Data for deferred access checking. */
83 static GTY(()) deferred_access *deferred_access_stack;
84 static GTY(()) deferred_access *deferred_access_free_list;
85
86 /* Save the current deferred access states and start deferred
87 access checking iff DEFER_P is true. */
88
89 void push_deferring_access_checks (bool deferring_p)
90 {
91 deferred_access *d;
92
93 /* Recycle previously used free store if available. */
94 if (deferred_access_free_list)
95 {
96 d = deferred_access_free_list;
97 deferred_access_free_list = d->next;
98 }
99 else
100 d = (deferred_access *) ggc_alloc (sizeof (deferred_access));
101
102 d->next = deferred_access_stack;
103 d->deferred_access_checks = NULL_TREE;
104 d->deferring_access_checks_p = deferring_p;
105 deferred_access_stack = d;
106 }
107
108 /* Resume deferring access checks again after we stopped doing
109 this previously. */
110
111 void resume_deferring_access_checks (void)
112 {
113 deferred_access_stack->deferring_access_checks_p = true;
114 }
115
116 /* Stop deferring access checks. */
117
118 void stop_deferring_access_checks (void)
119 {
120 deferred_access_stack->deferring_access_checks_p = false;
121 }
122
123 /* Discard the current deferred access checks and restore the
124 previous states. */
125
126 void pop_deferring_access_checks (void)
127 {
128 deferred_access *d = deferred_access_stack;
129 deferred_access_stack = d->next;
130
131 /* Remove references to access checks TREE_LIST. */
132 d->deferred_access_checks = NULL_TREE;
133
134 /* Store in free list for later use. */
135 d->next = deferred_access_free_list;
136 deferred_access_free_list = d;
137 }
138
139 /* Returns a TREE_LIST representing the deferred checks.
140 The TREE_PURPOSE of each node is the type through which the
141 access occurred; the TREE_VALUE is the declaration named.
142 */
143
144 tree get_deferred_access_checks (void)
145 {
146 return deferred_access_stack->deferred_access_checks;
147 }
148
149 /* Take current deferred checks and combine with the
150 previous states if we also defer checks previously.
151 Otherwise perform checks now. */
152
153 void pop_to_parent_deferring_access_checks (void)
154 {
155 tree deferred_check = get_deferred_access_checks ();
156 deferred_access *d1 = deferred_access_stack;
157 deferred_access *d2 = deferred_access_stack->next;
158 deferred_access *d3 = deferred_access_stack->next->next;
159
160 /* Temporary swap the order of the top two states, just to make
161 sure the garbage collector will not reclaim the memory during
162 processing below. */
163 deferred_access_stack = d2;
164 d2->next = d1;
165 d1->next = d3;
166
167 for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
168 /* Perform deferred check if required. */
169 perform_or_defer_access_check (TREE_PURPOSE (deferred_check),
170 TREE_VALUE (deferred_check));
171
172 deferred_access_stack = d1;
173 d1->next = d2;
174 d2->next = d3;
175 pop_deferring_access_checks ();
176 }
177
178 /* Perform the deferred access checks. */
179
180 void perform_deferred_access_checks (void)
181 {
182 tree deferred_check;
183 for (deferred_check = deferred_access_stack->deferred_access_checks;
184 deferred_check;
185 deferred_check = TREE_CHAIN (deferred_check))
186 /* Check access. */
187 enforce_access (TREE_PURPOSE (deferred_check),
188 TREE_VALUE (deferred_check));
189
190 /* No more deferred checks. */
191 deferred_access_stack->deferred_access_checks = NULL_TREE;
192 }
193
194 /* Defer checking the accessibility of DECL, when looked up in
195 CLASS_TYPE. */
196
197 void perform_or_defer_access_check (tree class_type, tree decl)
198 {
199 tree check;
200
201 /* If we are not supposed to defer access checks, just check now. */
202 if (!deferred_access_stack->deferring_access_checks_p)
203 {
204 enforce_access (class_type, decl);
205 return;
206 }
207
208 /* See if we are already going to perform this check. */
209 for (check = deferred_access_stack->deferred_access_checks;
210 check;
211 check = TREE_CHAIN (check))
212 if (TREE_VALUE (check) == decl
213 && same_type_p (TREE_PURPOSE (check), class_type))
214 return;
215 /* If not, record the check. */
216 deferred_access_stack->deferred_access_checks
217 = tree_cons (class_type, decl,
218 deferred_access_stack->deferred_access_checks);
219 }
220
221 /* Returns nonzero if the current statement is a full expression,
222 i.e. temporaries created during that statement should be destroyed
223 at the end of the statement. */
224
225 int
226 stmts_are_full_exprs_p ()
227 {
228 return current_stmt_tree ()->stmts_are_full_exprs_p;
229 }
230
231 /* Returns the stmt_tree (if any) to which statements are currently
232 being added. If there is no active statement-tree, NULL is
233 returned. */
234
235 stmt_tree
236 current_stmt_tree ()
237 {
238 return (cfun
239 ? &cfun->language->base.x_stmt_tree
240 : &scope_chain->x_stmt_tree);
241 }
242
243 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
244 flag for this because "A union for which objects or pointers are
245 declared is not an anonymous union" [class.union]. */
246
247 int
248 anon_aggr_type_p (node)
249 tree node;
250 {
251 return ANON_AGGR_TYPE_P (node);
252 }
253
254 /* Finish a scope. */
255
256 tree
257 do_poplevel ()
258 {
259 tree block = NULL_TREE;
260
261 if (stmts_are_full_exprs_p ())
262 {
263 tree scope_stmts = NULL_TREE;
264
265 block = poplevel (kept_level_p (), 1, 0);
266 if (!processing_template_decl)
267 {
268 /* This needs to come after the poplevel so that partial scopes
269 are properly nested. */
270 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
271 if (block)
272 {
273 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
274 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
275 }
276 }
277 }
278
279 return block;
280 }
281
282 /* Begin a new scope. */
283
284 void
285 do_pushlevel (scope_kind sk)
286 {
287 if (stmts_are_full_exprs_p ())
288 {
289 if (!processing_template_decl)
290 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
291 begin_scope (sk);
292 }
293 }
294
295 /* Finish a goto-statement. */
296
297 tree
298 finish_goto_stmt (destination)
299 tree destination;
300 {
301 if (TREE_CODE (destination) == IDENTIFIER_NODE)
302 destination = lookup_label (destination);
303
304 /* We warn about unused labels with -Wunused. That means we have to
305 mark the used labels as used. */
306 if (TREE_CODE (destination) == LABEL_DECL)
307 TREE_USED (destination) = 1;
308
309 if (TREE_CODE (destination) != LABEL_DECL)
310 /* We don't inline calls to functions with computed gotos.
311 Those functions are typically up to some funny business,
312 and may be depending on the labels being at particular
313 addresses, or some such. */
314 DECL_UNINLINABLE (current_function_decl) = 1;
315
316 check_goto (destination);
317
318 return add_stmt (build_stmt (GOTO_STMT, destination));
319 }
320
321 /* COND is the condition-expression for an if, while, etc.,
322 statement. Convert it to a boolean value, if appropriate. */
323
324 tree
325 maybe_convert_cond (cond)
326 tree cond;
327 {
328 /* Empty conditions remain empty. */
329 if (!cond)
330 return NULL_TREE;
331
332 /* Wait until we instantiate templates before doing conversion. */
333 if (processing_template_decl)
334 return cond;
335
336 /* Do the conversion. */
337 cond = convert_from_reference (cond);
338 return condition_conversion (cond);
339 }
340
341 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
342
343 tree
344 finish_expr_stmt (expr)
345 tree expr;
346 {
347 tree r = NULL_TREE;
348 tree expr_type = NULL_TREE;;
349
350 if (expr != NULL_TREE)
351 {
352 if (!processing_template_decl
353 && !(stmts_are_full_exprs_p ())
354 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
355 && lvalue_p (expr))
356 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
357 expr = default_conversion (expr);
358
359 /* Remember the type of the expression. */
360 expr_type = TREE_TYPE (expr);
361
362 if (stmts_are_full_exprs_p ())
363 expr = convert_to_void (expr, "statement");
364
365 r = add_stmt (build_stmt (EXPR_STMT, expr));
366 }
367
368 finish_stmt ();
369
370 /* This was an expression-statement, so we save the type of the
371 expression. */
372 last_expr_type = expr_type;
373
374 return r;
375 }
376
377
378 /* Begin an if-statement. Returns a newly created IF_STMT if
379 appropriate. */
380
381 tree
382 begin_if_stmt ()
383 {
384 tree r;
385 do_pushlevel (sk_block);
386 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
387 add_stmt (r);
388 return r;
389 }
390
391 /* Process the COND of an if-statement, which may be given by
392 IF_STMT. */
393
394 void
395 finish_if_stmt_cond (cond, if_stmt)
396 tree cond;
397 tree if_stmt;
398 {
399 cond = maybe_convert_cond (cond);
400 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
401 }
402
403 /* Finish the then-clause of an if-statement, which may be given by
404 IF_STMT. */
405
406 tree
407 finish_then_clause (if_stmt)
408 tree if_stmt;
409 {
410 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
411 return if_stmt;
412 }
413
414 /* Begin the else-clause of an if-statement. */
415
416 void
417 begin_else_clause ()
418 {
419 }
420
421 /* Finish the else-clause of an if-statement, which may be given by
422 IF_STMT. */
423
424 void
425 finish_else_clause (if_stmt)
426 tree if_stmt;
427 {
428 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
429 }
430
431 /* Finish an if-statement. */
432
433 void
434 finish_if_stmt ()
435 {
436 finish_stmt ();
437 do_poplevel ();
438 }
439
440 /* Begin a while-statement. Returns a newly created WHILE_STMT if
441 appropriate. */
442
443 tree
444 begin_while_stmt ()
445 {
446 tree r;
447 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
448 add_stmt (r);
449 do_pushlevel (sk_block);
450 return r;
451 }
452
453 /* Process the COND of a while-statement, which may be given by
454 WHILE_STMT. */
455
456 void
457 finish_while_stmt_cond (cond, while_stmt)
458 tree cond;
459 tree while_stmt;
460 {
461 cond = maybe_convert_cond (cond);
462 if (processing_template_decl)
463 /* Don't mess with condition decls in a template. */
464 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
465 else if (getdecls () == NULL_TREE)
466 /* It was a simple condition; install it. */
467 WHILE_COND (while_stmt) = cond;
468 else
469 {
470 /* If there was a declaration in the condition, we can't leave it
471 there; transform
472 while (A x = 42) { }
473 to
474 while (true) { A x = 42; if (!x) break; } */
475 tree if_stmt;
476 WHILE_COND (while_stmt) = boolean_true_node;
477
478 if_stmt = begin_if_stmt ();
479 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
480 finish_if_stmt_cond (cond, if_stmt);
481 finish_break_stmt ();
482 finish_then_clause (if_stmt);
483 finish_if_stmt ();
484 }
485 }
486
487 /* Finish a while-statement, which may be given by WHILE_STMT. */
488
489 void
490 finish_while_stmt (while_stmt)
491 tree while_stmt;
492 {
493 do_poplevel ();
494 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
495 finish_stmt ();
496 }
497
498 /* Begin a do-statement. Returns a newly created DO_STMT if
499 appropriate. */
500
501 tree
502 begin_do_stmt ()
503 {
504 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
505 add_stmt (r);
506 return r;
507 }
508
509 /* Finish the body of a do-statement, which may be given by DO_STMT. */
510
511 void
512 finish_do_body (do_stmt)
513 tree do_stmt;
514 {
515 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
516 }
517
518 /* Finish a do-statement, which may be given by DO_STMT, and whose
519 COND is as indicated. */
520
521 void
522 finish_do_stmt (cond, do_stmt)
523 tree cond;
524 tree do_stmt;
525 {
526 cond = maybe_convert_cond (cond);
527 DO_COND (do_stmt) = cond;
528 finish_stmt ();
529 }
530
531 /* Finish a return-statement. The EXPRESSION returned, if any, is as
532 indicated. */
533
534 tree
535 finish_return_stmt (expr)
536 tree expr;
537 {
538 tree r;
539
540 expr = check_return_expr (expr);
541 if (!processing_template_decl)
542 {
543 if (DECL_DESTRUCTOR_P (current_function_decl))
544 {
545 /* Similarly, all destructors must run destructors for
546 base-classes before returning. So, all returns in a
547 destructor get sent to the DTOR_LABEL; finish_function emits
548 code to return a value there. */
549 return finish_goto_stmt (dtor_label);
550 }
551 }
552 r = add_stmt (build_stmt (RETURN_STMT, expr));
553 finish_stmt ();
554
555 return r;
556 }
557
558 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
559
560 tree
561 begin_for_stmt ()
562 {
563 tree r;
564
565 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
566 NULL_TREE, NULL_TREE);
567 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
568 if (NEW_FOR_SCOPE_P (r))
569 do_pushlevel (sk_for);
570 add_stmt (r);
571
572 return r;
573 }
574
575 /* Finish the for-init-statement of a for-statement, which may be
576 given by FOR_STMT. */
577
578 void
579 finish_for_init_stmt (for_stmt)
580 tree for_stmt;
581 {
582 if (last_tree != for_stmt)
583 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
584 do_pushlevel (sk_block);
585 }
586
587 /* Finish the COND of a for-statement, which may be given by
588 FOR_STMT. */
589
590 void
591 finish_for_cond (cond, for_stmt)
592 tree cond;
593 tree for_stmt;
594 {
595 cond = maybe_convert_cond (cond);
596 if (processing_template_decl)
597 /* Don't mess with condition decls in a template. */
598 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
599 else if (getdecls () == NULL_TREE)
600 /* It was a simple condition; install it. */
601 FOR_COND (for_stmt) = cond;
602 else
603 {
604 /* If there was a declaration in the condition, we can't leave it
605 there; transform
606 for (; A x = 42;) { }
607 to
608 for (;;) { A x = 42; if (!x) break; } */
609 tree if_stmt;
610 FOR_COND (for_stmt) = NULL_TREE;
611
612 if_stmt = begin_if_stmt ();
613 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
614 finish_if_stmt_cond (cond, if_stmt);
615 finish_break_stmt ();
616 finish_then_clause (if_stmt);
617 finish_if_stmt ();
618 }
619 }
620
621 /* Finish the increment-EXPRESSION in a for-statement, which may be
622 given by FOR_STMT. */
623
624 void
625 finish_for_expr (expr, for_stmt)
626 tree expr;
627 tree for_stmt;
628 {
629 FOR_EXPR (for_stmt) = expr;
630 }
631
632 /* Finish the body of a for-statement, which may be given by
633 FOR_STMT. The increment-EXPR for the loop must be
634 provided. */
635
636 void
637 finish_for_stmt (for_stmt)
638 tree for_stmt;
639 {
640 /* Pop the scope for the body of the loop. */
641 do_poplevel ();
642 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
643 if (NEW_FOR_SCOPE_P (for_stmt))
644 do_poplevel ();
645 finish_stmt ();
646 }
647
648 /* Finish a break-statement. */
649
650 tree
651 finish_break_stmt ()
652 {
653 return add_stmt (build_break_stmt ());
654 }
655
656 /* Finish a continue-statement. */
657
658 tree
659 finish_continue_stmt ()
660 {
661 return add_stmt (build_continue_stmt ());
662 }
663
664 /* Begin a switch-statement. Returns a new SWITCH_STMT if
665 appropriate. */
666
667 tree
668 begin_switch_stmt ()
669 {
670 tree r;
671 do_pushlevel (sk_block);
672 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
673 add_stmt (r);
674 return r;
675 }
676
677 /* Finish the cond of a switch-statement. */
678
679 void
680 finish_switch_cond (cond, switch_stmt)
681 tree cond;
682 tree switch_stmt;
683 {
684 tree orig_type = NULL;
685 if (!processing_template_decl)
686 {
687 tree index;
688
689 /* Convert the condition to an integer or enumeration type. */
690 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
691 if (cond == NULL_TREE)
692 {
693 error ("switch quantity not an integer");
694 cond = error_mark_node;
695 }
696 orig_type = TREE_TYPE (cond);
697 if (cond != error_mark_node)
698 {
699 cond = default_conversion (cond);
700 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
701 }
702
703 if (cond != error_mark_node)
704 {
705 index = get_unwidened (cond, NULL_TREE);
706 /* We can't strip a conversion from a signed type to an unsigned,
707 because if we did, int_fits_type_p would do the wrong thing
708 when checking case values for being in range,
709 and it's too hard to do the right thing. */
710 if (TREE_UNSIGNED (TREE_TYPE (cond))
711 == TREE_UNSIGNED (TREE_TYPE (index)))
712 cond = index;
713 }
714 }
715 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
716 SWITCH_TYPE (switch_stmt) = orig_type;
717 push_switch (switch_stmt);
718 }
719
720 /* Finish the body of a switch-statement, which may be given by
721 SWITCH_STMT. The COND to switch on is indicated. */
722
723 void
724 finish_switch_stmt (switch_stmt)
725 tree switch_stmt;
726 {
727 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
728 pop_switch ();
729 finish_stmt ();
730 do_poplevel ();
731 }
732
733 /* Generate the RTL for T, which is a TRY_BLOCK. */
734
735 static void
736 genrtl_try_block (t)
737 tree t;
738 {
739 if (CLEANUP_P (t))
740 {
741 expand_eh_region_start ();
742 expand_stmt (TRY_STMTS (t));
743 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
744 }
745 else
746 {
747 if (!FN_TRY_BLOCK_P (t))
748 emit_line_note (input_filename, input_line);
749
750 expand_eh_region_start ();
751 expand_stmt (TRY_STMTS (t));
752
753 if (FN_TRY_BLOCK_P (t))
754 {
755 expand_start_all_catch ();
756 in_function_try_handler = 1;
757 expand_stmt (TRY_HANDLERS (t));
758 in_function_try_handler = 0;
759 expand_end_all_catch ();
760 }
761 else
762 {
763 expand_start_all_catch ();
764 expand_stmt (TRY_HANDLERS (t));
765 expand_end_all_catch ();
766 }
767 }
768 }
769
770 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
771
772 static void
773 genrtl_eh_spec_block (t)
774 tree t;
775 {
776 expand_eh_region_start ();
777 expand_stmt (EH_SPEC_STMTS (t));
778 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
779 build_call (call_unexpected_node,
780 tree_cons (NULL_TREE,
781 build_exc_ptr (),
782 NULL_TREE)));
783 }
784
785 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
786 appropriate. */
787
788 tree
789 begin_try_block ()
790 {
791 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
792 add_stmt (r);
793 return r;
794 }
795
796 /* Likewise, for a function-try-block. */
797
798 tree
799 begin_function_try_block ()
800 {
801 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
802 FN_TRY_BLOCK_P (r) = 1;
803 add_stmt (r);
804 return r;
805 }
806
807 /* Finish a try-block, which may be given by TRY_BLOCK. */
808
809 void
810 finish_try_block (try_block)
811 tree try_block;
812 {
813 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
814 }
815
816 /* Finish the body of a cleanup try-block, which may be given by
817 TRY_BLOCK. */
818
819 void
820 finish_cleanup_try_block (try_block)
821 tree try_block;
822 {
823 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
824 }
825
826 /* Finish an implicitly generated try-block, with a cleanup is given
827 by CLEANUP. */
828
829 void
830 finish_cleanup (cleanup, try_block)
831 tree cleanup;
832 tree try_block;
833 {
834 TRY_HANDLERS (try_block) = cleanup;
835 CLEANUP_P (try_block) = 1;
836 }
837
838 /* Likewise, for a function-try-block. */
839
840 void
841 finish_function_try_block (try_block)
842 tree try_block;
843 {
844 if (TREE_CHAIN (try_block)
845 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
846 {
847 /* Chain the compound statement after the CTOR_INITIALIZER. */
848 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
849 /* And make the CTOR_INITIALIZER the body of the try-block. */
850 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
851 }
852 else
853 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
854 in_function_try_handler = 1;
855 }
856
857 /* Finish a handler-sequence for a try-block, which may be given by
858 TRY_BLOCK. */
859
860 void
861 finish_handler_sequence (try_block)
862 tree try_block;
863 {
864 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
865 check_handlers (TRY_HANDLERS (try_block));
866 }
867
868 /* Likewise, for a function-try-block. */
869
870 void
871 finish_function_handler_sequence (try_block)
872 tree try_block;
873 {
874 in_function_try_handler = 0;
875 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
876 check_handlers (TRY_HANDLERS (try_block));
877 }
878
879 /* Generate the RTL for T, which is a HANDLER. */
880
881 static void
882 genrtl_handler (t)
883 tree t;
884 {
885 genrtl_do_pushlevel ();
886 if (!processing_template_decl)
887 expand_start_catch (HANDLER_TYPE (t));
888 expand_stmt (HANDLER_BODY (t));
889 if (!processing_template_decl)
890 expand_end_catch ();
891 }
892
893 /* Begin a handler. Returns a HANDLER if appropriate. */
894
895 tree
896 begin_handler ()
897 {
898 tree r;
899 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
900 add_stmt (r);
901 /* Create a binding level for the eh_info and the exception object
902 cleanup. */
903 do_pushlevel (sk_catch);
904 return r;
905 }
906
907 /* Finish the handler-parameters for a handler, which may be given by
908 HANDLER. DECL is the declaration for the catch parameter, or NULL
909 if this is a `catch (...)' clause. */
910
911 void
912 finish_handler_parms (decl, handler)
913 tree decl;
914 tree handler;
915 {
916 tree type = NULL_TREE;
917 if (processing_template_decl)
918 {
919 if (decl)
920 {
921 decl = pushdecl (decl);
922 decl = push_template_decl (decl);
923 add_decl_stmt (decl);
924 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
925 type = TREE_TYPE (decl);
926 }
927 }
928 else
929 type = expand_start_catch_block (decl);
930
931 HANDLER_TYPE (handler) = type;
932 }
933
934 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
935 the return value from the matching call to finish_handler_parms. */
936
937 void
938 finish_handler (handler)
939 tree handler;
940 {
941 if (!processing_template_decl)
942 expand_end_catch_block ();
943 do_poplevel ();
944 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
945 }
946
947 /* Begin a compound-statement. If HAS_NO_SCOPE is nonzero, the
948 compound-statement does not define a scope. Returns a new
949 COMPOUND_STMT if appropriate. */
950
951 tree
952 begin_compound_stmt (has_no_scope)
953 int has_no_scope;
954 {
955 tree r;
956 int is_try = 0;
957
958 r = build_stmt (COMPOUND_STMT, NULL_TREE);
959
960 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
961 is_try = 1;
962
963 add_stmt (r);
964 if (has_no_scope)
965 COMPOUND_STMT_NO_SCOPE (r) = 1;
966
967 last_expr_type = NULL_TREE;
968
969 if (!has_no_scope)
970 do_pushlevel (is_try ? sk_try : sk_block);
971 else
972 /* Normally, we try hard to keep the BLOCK for a
973 statement-expression. But, if it's a statement-expression with
974 a scopeless block, there's nothing to keep, and we don't want
975 to accidentally keep a block *inside* the scopeless block. */
976 keep_next_level (0);
977
978 return r;
979 }
980
981 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
982 If HAS_NO_SCOPE is nonzero, the compound statement does not define
983 a scope. */
984
985 tree
986 finish_compound_stmt (has_no_scope, compound_stmt)
987 int has_no_scope;
988 tree compound_stmt;
989 {
990 tree r;
991 tree t;
992
993 if (!has_no_scope)
994 r = do_poplevel ();
995 else
996 r = NULL_TREE;
997
998 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
999
1000 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1001 the precise purpose of that variable is store the type of the
1002 last expression statement within the last compound statement, we
1003 preserve the value. */
1004 t = last_expr_type;
1005 finish_stmt ();
1006 last_expr_type = t;
1007
1008 return r;
1009 }
1010
1011 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1012 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1013 CLOBBERS. */
1014
1015 tree
1016 finish_asm_stmt (cv_qualifier, string, output_operands,
1017 input_operands, clobbers)
1018 tree cv_qualifier;
1019 tree string;
1020 tree output_operands;
1021 tree input_operands;
1022 tree clobbers;
1023 {
1024 tree r;
1025 tree t;
1026
1027 if (cv_qualifier != NULL_TREE
1028 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1029 {
1030 warning ("%s qualifier ignored on asm",
1031 IDENTIFIER_POINTER (cv_qualifier));
1032 cv_qualifier = NULL_TREE;
1033 }
1034
1035 if (!processing_template_decl)
1036 {
1037 int i;
1038 int ninputs;
1039 int noutputs;
1040
1041 for (t = input_operands; t; t = TREE_CHAIN (t))
1042 {
1043 tree converted_operand
1044 = decay_conversion (TREE_VALUE (t));
1045
1046 /* If the type of the operand hasn't been determined (e.g.,
1047 because it involves an overloaded function), then issue
1048 an error message. There's no context available to
1049 resolve the overloading. */
1050 if (TREE_TYPE (converted_operand) == unknown_type_node)
1051 {
1052 error ("type of asm operand `%E' could not be determined",
1053 TREE_VALUE (t));
1054 converted_operand = error_mark_node;
1055 }
1056 TREE_VALUE (t) = converted_operand;
1057 }
1058
1059 ninputs = list_length (input_operands);
1060 noutputs = list_length (output_operands);
1061
1062 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1063 {
1064 bool allows_mem;
1065 bool allows_reg;
1066 bool is_inout;
1067 const char *constraint;
1068 tree operand;
1069
1070 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1071 operand = TREE_VALUE (t);
1072
1073 if (!parse_output_constraint (&constraint,
1074 i, ninputs, noutputs,
1075 &allows_mem,
1076 &allows_reg,
1077 &is_inout))
1078 {
1079 /* By marking this operand as erroneous, we will not try
1080 to process this operand again in expand_asm_operands. */
1081 TREE_VALUE (t) = error_mark_node;
1082 continue;
1083 }
1084
1085 /* If the operand is a DECL that is going to end up in
1086 memory, assume it is addressable. This is a bit more
1087 conservative than it would ideally be; the exact test is
1088 buried deep in expand_asm_operands and depends on the
1089 DECL_RTL for the OPERAND -- which we don't have at this
1090 point. */
1091 if (!allows_reg && DECL_P (operand))
1092 cxx_mark_addressable (operand);
1093 }
1094 }
1095
1096 r = build_stmt (ASM_STMT, cv_qualifier, string,
1097 output_operands, input_operands,
1098 clobbers);
1099 return add_stmt (r);
1100 }
1101
1102 /* Finish a label with the indicated NAME. */
1103
1104 tree
1105 finish_label_stmt (name)
1106 tree name;
1107 {
1108 tree decl = define_label (input_filename, input_line, name);
1109 return add_stmt (build_stmt (LABEL_STMT, decl));
1110 }
1111
1112 /* Finish a series of declarations for local labels. G++ allows users
1113 to declare "local" labels, i.e., labels with scope. This extension
1114 is useful when writing code involving statement-expressions. */
1115
1116 void
1117 finish_label_decl (name)
1118 tree name;
1119 {
1120 tree decl = declare_local_label (name);
1121 add_decl_stmt (decl);
1122 }
1123
1124 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1125
1126 void
1127 finish_decl_cleanup (decl, cleanup)
1128 tree decl;
1129 tree cleanup;
1130 {
1131 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1132 }
1133
1134 /* If the current scope exits with an exception, run CLEANUP. */
1135
1136 void
1137 finish_eh_cleanup (cleanup)
1138 tree cleanup;
1139 {
1140 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1141 CLEANUP_EH_ONLY (r) = 1;
1142 add_stmt (r);
1143 }
1144
1145 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1146 order they were written by the user. Each node is as for
1147 emit_mem_initializers. */
1148
1149 void
1150 finish_mem_initializers (tree mem_inits)
1151 {
1152 /* Reorder the MEM_INITS so that they are in the order they appeared
1153 in the source program. */
1154 mem_inits = nreverse (mem_inits);
1155
1156 if (processing_template_decl)
1157 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1158 else
1159 emit_mem_initializers (mem_inits);
1160 }
1161
1162 /* Returns the stack of SCOPE_STMTs for the current function. */
1163
1164 tree *
1165 current_scope_stmt_stack ()
1166 {
1167 return &cfun->language->base.x_scope_stmt_stack;
1168 }
1169
1170 /* Finish a parenthesized expression EXPR. */
1171
1172 tree
1173 finish_parenthesized_expr (expr)
1174 tree expr;
1175 {
1176 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1177 /* This inhibits warnings in c_common_truthvalue_conversion. */
1178 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1179
1180 if (TREE_CODE (expr) == OFFSET_REF)
1181 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1182 enclosed in parentheses. */
1183 PTRMEM_OK_P (expr) = 0;
1184 return expr;
1185 }
1186
1187 /* Finish a reference to a non-static data member (DECL) that is not
1188 preceded by `.' or `->'. */
1189
1190 tree
1191 finish_non_static_data_member (tree decl, tree qualifying_scope)
1192 {
1193 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1194
1195 if (current_class_ptr == NULL_TREE)
1196 {
1197 if (current_function_decl
1198 && DECL_STATIC_FUNCTION_P (current_function_decl))
1199 cp_error_at ("invalid use of member `%D' in static member function",
1200 decl);
1201 else
1202 cp_error_at ("invalid use of non-static data member `%D'", decl);
1203 error ("from this location");
1204
1205 return error_mark_node;
1206 }
1207 TREE_USED (current_class_ptr) = 1;
1208 if (processing_template_decl)
1209 return build_min (COMPONENT_REF, TREE_TYPE (decl),
1210 current_class_ref, DECL_NAME (decl));
1211 else
1212 {
1213 tree access_type = current_class_type;
1214 tree object = current_class_ref;
1215
1216 while (!DERIVED_FROM_P (context_for_name_lookup (decl), access_type))
1217 {
1218 access_type = TYPE_CONTEXT (access_type);
1219 while (DECL_P (access_type))
1220 access_type = DECL_CONTEXT (access_type);
1221 }
1222
1223 enforce_access (access_type, decl);
1224
1225 /* If the data member was named `C::M', convert `*this' to `C'
1226 first. */
1227 if (qualifying_scope)
1228 {
1229 tree binfo = NULL_TREE;
1230 object = build_scoped_ref (object, qualifying_scope,
1231 &binfo);
1232 }
1233
1234 return build_class_member_access_expr (object, decl,
1235 /*access_path=*/NULL_TREE,
1236 /*preserve_reference=*/false);
1237 }
1238 }
1239
1240 /* Begin a statement-expression. The value returned must be passed to
1241 finish_stmt_expr. */
1242
1243 tree
1244 begin_stmt_expr ()
1245 {
1246 /* If we're outside a function, we won't have a statement-tree to
1247 work with. But, if we see a statement-expression we need to
1248 create one. */
1249 if (! cfun && !last_tree)
1250 begin_stmt_tree (&scope_chain->x_saved_tree);
1251
1252 keep_next_level (1);
1253 /* If we're building a statement tree, then the upcoming compound
1254 statement will be chained onto the tree structure, starting at
1255 last_tree. We return last_tree so that we can later unhook the
1256 compound statement. */
1257 return last_tree;
1258 }
1259
1260 /* Used when beginning a statement-expression outside function scope.
1261 For example, when handling a file-scope initializer, we use this
1262 function. */
1263
1264 tree
1265 begin_global_stmt_expr ()
1266 {
1267 if (! cfun && !last_tree)
1268 begin_stmt_tree (&scope_chain->x_saved_tree);
1269
1270 keep_next_level (1);
1271
1272 return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1);
1273 }
1274
1275 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1276
1277 tree
1278 finish_global_stmt_expr (stmt_expr)
1279 tree stmt_expr;
1280 {
1281 stmt_expr = expand_end_stmt_expr (stmt_expr);
1282
1283 if (! cfun
1284 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1285 finish_stmt_tree (&scope_chain->x_saved_tree);
1286
1287 return stmt_expr;
1288 }
1289
1290 /* Finish a statement-expression. RTL_EXPR should be the value
1291 returned by the previous begin_stmt_expr; EXPR is the
1292 statement-expression. Returns an expression representing the
1293 statement-expression. */
1294
1295 tree
1296 finish_stmt_expr (rtl_expr)
1297 tree rtl_expr;
1298 {
1299 tree result;
1300
1301 /* If the last thing in the statement-expression was not an
1302 expression-statement, then it has type `void'. */
1303 if (!last_expr_type)
1304 last_expr_type = void_type_node;
1305 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1306 TREE_SIDE_EFFECTS (result) = 1;
1307
1308 /* Remove the compound statement from the tree structure; it is
1309 now saved in the STMT_EXPR. */
1310 last_tree = rtl_expr;
1311 TREE_CHAIN (last_tree) = NULL_TREE;
1312
1313 /* If we created a statement-tree for this statement-expression,
1314 remove it now. */
1315 if (! cfun
1316 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1317 finish_stmt_tree (&scope_chain->x_saved_tree);
1318
1319 return result;
1320 }
1321
1322 /* Generate an expression for `FN (ARGS)'.
1323
1324 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1325 as a virtual call, even if FN is virtual. (This flag is set when
1326 encountering an expression where the function name is explicitly
1327 qualified. For example a call to `X::f' never generates a virtual
1328 call.)
1329
1330 Returns code for the call. */
1331
1332 tree
1333 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1334 {
1335 if (fn == error_mark_node || args == error_mark_node)
1336 return error_mark_node;
1337
1338 if (processing_template_decl)
1339 return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1340
1341 /* ARGS should be a list of arguments. */
1342 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1343 20020712);
1344
1345 /* A reference to a member function will appear as an overloaded
1346 function (rather than a BASELINK) if an unqualified name was used
1347 to refer to it. */
1348 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1349 {
1350 tree f;
1351
1352 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1353 f = get_first_fn (TREE_OPERAND (fn, 0));
1354 else
1355 f = get_first_fn (fn);
1356 if (DECL_FUNCTION_MEMBER_P (f))
1357 {
1358 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1359 fn = build_baselink (TYPE_BINFO (type),
1360 TYPE_BINFO (type),
1361 fn, /*optype=*/NULL_TREE);
1362 }
1363 }
1364
1365 if (BASELINK_P (fn))
1366 {
1367 tree object;
1368
1369 /* A call to a member function. From [over.call.func]:
1370
1371 If the keyword this is in scope and refers to the class of
1372 that member function, or a derived class thereof, then the
1373 function call is transformed into a qualified function call
1374 using (*this) as the postfix-expression to the left of the
1375 . operator.... [Otherwise] a contrived object of type T
1376 becomes the implied object argument.
1377
1378 This paragraph is unclear about this situation:
1379
1380 struct A { void f(); };
1381 struct B : public A {};
1382 struct C : public A { void g() { B::f(); }};
1383
1384 In particular, for `B::f', this paragraph does not make clear
1385 whether "the class of that member function" refers to `A' or
1386 to `B'. We believe it refers to `B'. */
1387 if (current_class_type
1388 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1389 current_class_type)
1390 && current_class_ref)
1391 object = current_class_ref;
1392 else
1393 {
1394 tree representative_fn;
1395
1396 representative_fn = BASELINK_FUNCTIONS (fn);
1397 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1398 representative_fn = TREE_OPERAND (representative_fn, 0);
1399 representative_fn = get_first_fn (representative_fn);
1400 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1401 }
1402
1403 return build_new_method_call (object, fn, args, NULL_TREE,
1404 (disallow_virtual
1405 ? LOOKUP_NONVIRTUAL : 0));
1406 }
1407 else if (is_overloaded_fn (fn))
1408 /* A call to a namespace-scope function. */
1409 return build_new_function_call (fn, args);
1410 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1411 {
1412 tree result;
1413
1414 if (args)
1415 error ("arguments to destructor are not allowed");
1416 /* Mark the pseudo-destructor call as having side-effects so
1417 that we do not issue warnings about its use. */
1418 result = build1 (NOP_EXPR,
1419 void_type_node,
1420 TREE_OPERAND (fn, 0));
1421 TREE_SIDE_EFFECTS (result) = 1;
1422 return result;
1423 }
1424 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1425 {
1426 /* If the "function" is really an object of class type, it might
1427 have an overloaded `operator ()'. */
1428 tree result;
1429 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1430 if (result)
1431 return result;
1432 }
1433
1434 /* A call where the function is unknown. */
1435 return build_function_call (fn, args);
1436 }
1437
1438 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1439 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1440 POSTDECREMENT_EXPR.) */
1441
1442 tree
1443 finish_increment_expr (expr, code)
1444 tree expr;
1445 enum tree_code code;
1446 {
1447 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1448 a COMPONENT_REF). This way if we've got, say, a reference to a
1449 static member that's being operated on, we don't end up trying to
1450 find a member operator for the class it's in. */
1451
1452 if (TREE_CODE (expr) == OFFSET_REF)
1453 expr = resolve_offset_ref (expr);
1454 return build_x_unary_op (code, expr);
1455 }
1456
1457 /* Finish a use of `this'. Returns an expression for `this'. */
1458
1459 tree
1460 finish_this_expr ()
1461 {
1462 tree result;
1463
1464 if (current_class_ptr)
1465 {
1466 result = current_class_ptr;
1467 }
1468 else if (current_function_decl
1469 && DECL_STATIC_FUNCTION_P (current_function_decl))
1470 {
1471 error ("`this' is unavailable for static member functions");
1472 result = error_mark_node;
1473 }
1474 else
1475 {
1476 if (current_function_decl)
1477 error ("invalid use of `this' in non-member function");
1478 else
1479 error ("invalid use of `this' at top level");
1480 result = error_mark_node;
1481 }
1482
1483 return result;
1484 }
1485
1486 /* Finish a member function call using OBJECT and ARGS as arguments to
1487 FN. Returns an expression for the call. */
1488
1489 tree
1490 finish_object_call_expr (fn, object, args)
1491 tree fn;
1492 tree object;
1493 tree args;
1494 {
1495 if (DECL_DECLARES_TYPE_P (fn))
1496 {
1497 if (processing_template_decl)
1498 /* This can happen on code like:
1499
1500 class X;
1501 template <class T> void f(T t) {
1502 t.X();
1503 }
1504
1505 We just grab the underlying IDENTIFIER. */
1506 fn = DECL_NAME (fn);
1507 else
1508 {
1509 error ("calling type `%T' like a method", fn);
1510 return error_mark_node;
1511 }
1512 }
1513
1514 if (processing_template_decl)
1515 return build_nt (CALL_EXPR,
1516 build_nt (COMPONENT_REF, object, fn),
1517 args);
1518
1519 if (name_p (fn))
1520 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1521 else
1522 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1523 }
1524
1525 /* Finish a qualified member function call using OBJECT and ARGS as
1526 arguments to FN. Returns an expression for the call. */
1527
1528 tree
1529 finish_qualified_object_call_expr (fn, object, args)
1530 tree fn;
1531 tree object;
1532 tree args;
1533 {
1534 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1535 TREE_OPERAND (fn, 1), args);
1536 }
1537
1538 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1539 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1540 the TYPE for the type given. If SCOPE is non-NULL, the expression
1541 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1542
1543 tree
1544 finish_pseudo_destructor_expr (object, scope, destructor)
1545 tree object;
1546 tree scope;
1547 tree destructor;
1548 {
1549 if (destructor == error_mark_node)
1550 return error_mark_node;
1551
1552 my_friendly_assert (TYPE_P (destructor), 20010905);
1553
1554 if (!processing_template_decl)
1555 {
1556 if (scope == error_mark_node)
1557 {
1558 error ("invalid qualifying scope in pseudo-destructor name");
1559 return error_mark_node;
1560 }
1561
1562 if (!same_type_p (TREE_TYPE (object), destructor))
1563 {
1564 error ("`%E' is not of type `%T'", object, destructor);
1565 return error_mark_node;
1566 }
1567 }
1568
1569 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1570 }
1571
1572 /* Finish an expression of the form CODE EXPR. */
1573
1574 tree
1575 finish_unary_op_expr (code, expr)
1576 enum tree_code code;
1577 tree expr;
1578 {
1579 tree result = build_x_unary_op (code, expr);
1580 /* Inside a template, build_x_unary_op does not fold the
1581 expression. So check whether the result is folded before
1582 setting TREE_NEGATED_INT. */
1583 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1584 && TREE_CODE (result) == INTEGER_CST
1585 && !TREE_UNSIGNED (TREE_TYPE (result))
1586 && INT_CST_LT (result, integer_zero_node))
1587 TREE_NEGATED_INT (result) = 1;
1588 overflow_warning (result);
1589 return result;
1590 }
1591
1592 /* Finish a compound-literal expression. TYPE is the type to which
1593 the INITIALIZER_LIST is being cast. */
1594
1595 tree
1596 finish_compound_literal (type, initializer_list)
1597 tree type;
1598 tree initializer_list;
1599 {
1600 tree compound_literal;
1601
1602 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1603 compound_literal = build_constructor (NULL_TREE, initializer_list);
1604 /* Mark it as a compound-literal. */
1605 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1606 if (processing_template_decl)
1607 TREE_TYPE (compound_literal) = type;
1608 else
1609 {
1610 /* Check the initialization. */
1611 compound_literal = digest_init (type, compound_literal, NULL);
1612 /* If the TYPE was an array type with an unknown bound, then we can
1613 figure out the dimension now. For example, something like:
1614
1615 `(int []) { 2, 3 }'
1616
1617 implies that the array has two elements. */
1618 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1619 complete_array_type (type, compound_literal, 1);
1620 }
1621
1622 return compound_literal;
1623 }
1624
1625 /* Return the declaration for the function-name variable indicated by
1626 ID. */
1627
1628 tree
1629 finish_fname (tree id)
1630 {
1631 tree decl;
1632
1633 decl = fname_decl (C_RID_CODE (id), id);
1634 if (processing_template_decl)
1635 decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1636 return decl;
1637 }
1638
1639 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1640 and DECLARATOR. Returns nonzero if the function-declaration is
1641 valid. */
1642
1643 int
1644 begin_function_definition (decl_specs, attributes, declarator)
1645 tree decl_specs;
1646 tree attributes;
1647 tree declarator;
1648 {
1649 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1650 return 0;
1651
1652 /* The things we're about to see are not directly qualified by any
1653 template headers we've seen thus far. */
1654 reset_specialization ();
1655
1656 return 1;
1657 }
1658
1659 /* Finish an init-declarator. Returns a DECL. */
1660
1661 tree
1662 finish_declarator (declarator, declspecs, attributes,
1663 prefix_attributes, initialized)
1664 tree declarator;
1665 tree declspecs;
1666 tree attributes;
1667 tree prefix_attributes;
1668 int initialized;
1669 {
1670 return start_decl (declarator, declspecs, initialized, attributes,
1671 prefix_attributes);
1672 }
1673
1674 /* Finish a translation unit. */
1675
1676 void
1677 finish_translation_unit ()
1678 {
1679 /* In case there were missing closebraces,
1680 get us back to the global binding level. */
1681 pop_everything ();
1682 while (current_namespace != global_namespace)
1683 pop_namespace ();
1684
1685 /* Do file scope __FUNCTION__ et al. */
1686 finish_fname_decls ();
1687 }
1688
1689 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1690 Returns the parameter. */
1691
1692 tree
1693 finish_template_type_parm (aggr, identifier)
1694 tree aggr;
1695 tree identifier;
1696 {
1697 if (aggr != class_type_node)
1698 {
1699 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1700 aggr = class_type_node;
1701 }
1702
1703 return build_tree_list (aggr, identifier);
1704 }
1705
1706 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1707 Returns the parameter. */
1708
1709 tree
1710 finish_template_template_parm (aggr, identifier)
1711 tree aggr;
1712 tree identifier;
1713 {
1714 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1715 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1716 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1717 DECL_TEMPLATE_RESULT (tmpl) = decl;
1718 DECL_ARTIFICIAL (decl) = 1;
1719 end_template_decl ();
1720
1721 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1722
1723 return finish_template_type_parm (aggr, tmpl);
1724 }
1725
1726 /* ARGUMENT is the default-argument value for a template template
1727 parameter. If ARGUMENT is invalid, issue error messages and return
1728 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1729
1730 tree
1731 check_template_template_default_arg (tree argument)
1732 {
1733 if (TREE_CODE (argument) != TEMPLATE_DECL
1734 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1735 && TREE_CODE (argument) != TYPE_DECL
1736 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1737 {
1738 error ("invalid default template argument");
1739 return error_mark_node;
1740 }
1741
1742 return argument;
1743 }
1744
1745 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1746 nonzero, the parameter list was terminated by a `...'. */
1747
1748 tree
1749 finish_parmlist (parms, ellipsis)
1750 tree parms;
1751 int ellipsis;
1752 {
1753 if (parms)
1754 {
1755 /* We mark the PARMS as a parmlist so that declarator processing can
1756 disambiguate certain constructs. */
1757 TREE_PARMLIST (parms) = 1;
1758 /* We do not append void_list_node here, but leave it to grokparms
1759 to do that. */
1760 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1761 }
1762 return parms;
1763 }
1764
1765 /* Begin a class definition, as indicated by T. */
1766
1767 tree
1768 begin_class_definition (t)
1769 tree t;
1770 {
1771 if (t == error_mark_node)
1772 return error_mark_node;
1773
1774 if (processing_template_parmlist)
1775 {
1776 error ("definition of `%#T' inside template parameter list", t);
1777 return error_mark_node;
1778 }
1779 /* A non-implicit typename comes from code like:
1780
1781 template <typename T> struct A {
1782 template <typename U> struct A<T>::B ...
1783
1784 This is erroneous. */
1785 else if (TREE_CODE (t) == TYPENAME_TYPE)
1786 {
1787 error ("invalid definition of qualified type `%T'", t);
1788 t = error_mark_node;
1789 }
1790
1791 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1792 {
1793 t = make_aggr_type (RECORD_TYPE);
1794 pushtag (make_anon_name (), t, 0);
1795 }
1796
1797 /* If this type was already complete, and we see another definition,
1798 that's an error. */
1799 if (COMPLETE_TYPE_P (t))
1800 {
1801 error ("redefinition of `%#T'", t);
1802 cp_error_at ("previous definition of `%#T'", t);
1803 return error_mark_node;
1804 }
1805
1806 /* Update the location of the decl. */
1807 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
1808
1809 if (TYPE_BEING_DEFINED (t))
1810 {
1811 t = make_aggr_type (TREE_CODE (t));
1812 pushtag (TYPE_IDENTIFIER (t), t, 0);
1813 }
1814 maybe_process_partial_specialization (t);
1815 pushclass (t, true);
1816 TYPE_BEING_DEFINED (t) = 1;
1817 TYPE_PACKED (t) = flag_pack_struct;
1818 /* Reset the interface data, at the earliest possible
1819 moment, as it might have been set via a class foo;
1820 before. */
1821 if (! TYPE_ANONYMOUS_P (t))
1822 {
1823 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1824 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1825 (t, interface_unknown);
1826 }
1827 reset_specialization();
1828
1829 /* Make a declaration for this class in its own scope. */
1830 build_self_reference ();
1831
1832 return t;
1833 }
1834
1835 /* Finish the member declaration given by DECL. */
1836
1837 void
1838 finish_member_declaration (decl)
1839 tree decl;
1840 {
1841 if (decl == error_mark_node || decl == NULL_TREE)
1842 return;
1843
1844 if (decl == void_type_node)
1845 /* The COMPONENT was a friend, not a member, and so there's
1846 nothing for us to do. */
1847 return;
1848
1849 /* We should see only one DECL at a time. */
1850 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1851
1852 /* Set up access control for DECL. */
1853 TREE_PRIVATE (decl)
1854 = (current_access_specifier == access_private_node);
1855 TREE_PROTECTED (decl)
1856 = (current_access_specifier == access_protected_node);
1857 if (TREE_CODE (decl) == TEMPLATE_DECL)
1858 {
1859 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1860 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1861 }
1862
1863 /* Mark the DECL as a member of the current class. */
1864 DECL_CONTEXT (decl) = current_class_type;
1865
1866 /* [dcl.link]
1867
1868 A C language linkage is ignored for the names of class members
1869 and the member function type of class member functions. */
1870 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1871 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1872
1873 maybe_add_class_template_decl_list (current_class_type, decl, /*friend_p=*/0);
1874
1875 /* Put functions on the TYPE_METHODS list and everything else on the
1876 TYPE_FIELDS list. Note that these are built up in reverse order.
1877 We reverse them (to obtain declaration order) in finish_struct. */
1878 if (TREE_CODE (decl) == FUNCTION_DECL
1879 || DECL_FUNCTION_TEMPLATE_P (decl))
1880 {
1881 /* We also need to add this function to the
1882 CLASSTYPE_METHOD_VEC. */
1883 add_method (current_class_type, decl, /*error_p=*/0);
1884
1885 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1886 TYPE_METHODS (current_class_type) = decl;
1887 }
1888 else
1889 {
1890 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1891 go at the beginning. The reason is that lookup_field_1
1892 searches the list in order, and we want a field name to
1893 override a type name so that the "struct stat hack" will
1894 work. In particular:
1895
1896 struct S { enum E { }; int E } s;
1897 s.E = 3;
1898
1899 is valid. In addition, the FIELD_DECLs must be maintained in
1900 declaration order so that class layout works as expected.
1901 However, we don't need that order until class layout, so we
1902 save a little time by putting FIELD_DECLs on in reverse order
1903 here, and then reversing them in finish_struct_1. (We could
1904 also keep a pointer to the correct insertion points in the
1905 list.) */
1906
1907 if (TREE_CODE (decl) == TYPE_DECL)
1908 TYPE_FIELDS (current_class_type)
1909 = chainon (TYPE_FIELDS (current_class_type), decl);
1910 else
1911 {
1912 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1913 TYPE_FIELDS (current_class_type) = decl;
1914 }
1915
1916 /* Enter the DECL into the scope of the class. */
1917 if (TREE_CODE (decl) != USING_DECL)
1918 pushdecl_class_level (decl);
1919 }
1920 }
1921
1922 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1923 the definition is immediately followed by a semicolon. Returns the
1924 type. */
1925
1926 tree
1927 finish_class_definition (t, attributes, semi, pop_scope_p)
1928 tree t;
1929 tree attributes;
1930 int semi;
1931 int pop_scope_p;
1932 {
1933 if (t == error_mark_node)
1934 return error_mark_node;
1935
1936 /* finish_struct nukes this anyway; if finish_exception does too,
1937 then it can go. */
1938 if (semi)
1939 note_got_semicolon (t);
1940
1941 /* If we got any attributes in class_head, xref_tag will stick them in
1942 TREE_TYPE of the type. Grab them now. */
1943 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1944 TYPE_ATTRIBUTES (t) = NULL_TREE;
1945
1946 if (TREE_CODE (t) == ENUMERAL_TYPE)
1947 ;
1948 else
1949 {
1950 t = finish_struct (t, attributes);
1951 if (semi)
1952 note_got_semicolon (t);
1953 }
1954
1955 if (pop_scope_p)
1956 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1957
1958 return t;
1959 }
1960
1961 /* Finish processing the declaration of a member class template
1962 TYPES whose template parameters are given by PARMS. */
1963
1964 tree
1965 finish_member_class_template (types)
1966 tree types;
1967 {
1968 tree t;
1969
1970 /* If there are declared, but undefined, partial specializations
1971 mixed in with the typespecs they will not yet have passed through
1972 maybe_process_partial_specialization, so we do that here. */
1973 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1974 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1975 maybe_process_partial_specialization (TREE_VALUE (t));
1976
1977 note_list_got_semicolon (types);
1978 grok_x_components (types);
1979 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1980 /* The component was in fact a friend declaration. We avoid
1981 finish_member_template_decl performing certain checks by
1982 unsetting TYPES. */
1983 types = NULL_TREE;
1984
1985 finish_member_template_decl (types);
1986
1987 /* As with other component type declarations, we do
1988 not store the new DECL on the list of
1989 component_decls. */
1990 return NULL_TREE;
1991 }
1992
1993 /* Finish processing a complete template declaration. The PARMS are
1994 the template parameters. */
1995
1996 void
1997 finish_template_decl (parms)
1998 tree parms;
1999 {
2000 if (parms)
2001 end_template_decl ();
2002 else
2003 end_specialization ();
2004 }
2005
2006 /* Finish processing a template-id (which names a type) of the form
2007 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2008 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2009 the scope of template-id indicated. */
2010
2011 tree
2012 finish_template_type (name, args, entering_scope)
2013 tree name;
2014 tree args;
2015 int entering_scope;
2016 {
2017 tree decl;
2018
2019 decl = lookup_template_class (name, args,
2020 NULL_TREE, NULL_TREE,
2021 entering_scope, /*complain=*/1);
2022 if (decl != error_mark_node)
2023 decl = TYPE_STUB_DECL (decl);
2024
2025 return decl;
2026 }
2027
2028 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2029 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2030 BASE_CLASS, or NULL_TREE if an error occurred. The
2031 ACCESS_SPECIFIER is one of
2032 access_{default,public,protected_private}[_virtual]_node.*/
2033
2034 tree
2035 finish_base_specifier (tree base, tree access, bool virtual_p)
2036 {
2037 tree result;
2038
2039 if (base == error_mark_node)
2040 {
2041 error ("invalid base-class specification");
2042 result = NULL_TREE;
2043 }
2044 else if (! is_aggr_type (base, 1))
2045 result = NULL_TREE;
2046 else
2047 {
2048 if (cp_type_quals (base) != 0)
2049 {
2050 error ("base class `%T' has cv qualifiers", base);
2051 base = TYPE_MAIN_VARIANT (base);
2052 }
2053 result = build_tree_list (access, base);
2054 TREE_VIA_VIRTUAL (result) = virtual_p;
2055 }
2056
2057 return result;
2058 }
2059
2060 /* Called when multiple declarators are processed. If that is not
2061 premitted in this context, an error is issued. */
2062
2063 void
2064 check_multiple_declarators ()
2065 {
2066 /* [temp]
2067
2068 In a template-declaration, explicit specialization, or explicit
2069 instantiation the init-declarator-list in the declaration shall
2070 contain at most one declarator.
2071
2072 We don't just use PROCESSING_TEMPLATE_DECL for the first
2073 condition since that would disallow the perfectly valid code,
2074 like `template <class T> struct S { int i, j; };'. */
2075 if (at_function_scope_p ())
2076 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2077 return;
2078
2079 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2080 || processing_explicit_instantiation
2081 || processing_specialization)
2082 error ("multiple declarators in template declaration");
2083 }
2084
2085 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2086 use as a type-specifier. */
2087
2088 tree
2089 finish_typeof (expr)
2090 tree expr;
2091 {
2092 tree type;
2093
2094 if (processing_template_decl)
2095 {
2096 type = make_aggr_type (TYPEOF_TYPE);
2097 TYPE_FIELDS (type) = expr;
2098
2099 return type;
2100 }
2101
2102 if (TREE_CODE (expr) == OFFSET_REF)
2103 expr = resolve_offset_ref (expr);
2104
2105 type = TREE_TYPE (expr);
2106
2107 if (!type || type == unknown_type_node)
2108 {
2109 error ("type of `%E' is unknown", expr);
2110 return error_mark_node;
2111 }
2112
2113 return type;
2114 }
2115
2116 /* Compute the value of the `sizeof' operator. */
2117
2118 tree
2119 finish_sizeof (t)
2120 tree t;
2121 {
2122 return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2123 }
2124
2125 /* Implement the __alignof keyword: Return the minimum required
2126 alignment of T, measured in bytes. */
2127
2128 tree
2129 finish_alignof (t)
2130 tree t;
2131 {
2132 if (processing_template_decl)
2133 return build_min (ALIGNOF_EXPR, size_type_node, t);
2134
2135 return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2136 }
2137
2138 /* Generate RTL for the statement T, and its substatements, and any
2139 other statements at its nesting level. */
2140
2141 static void
2142 cp_expand_stmt (t)
2143 tree t;
2144 {
2145 switch (TREE_CODE (t))
2146 {
2147 case TRY_BLOCK:
2148 genrtl_try_block (t);
2149 break;
2150
2151 case EH_SPEC_BLOCK:
2152 genrtl_eh_spec_block (t);
2153 break;
2154
2155 case HANDLER:
2156 genrtl_handler (t);
2157 break;
2158
2159 case USING_STMT:
2160 break;
2161
2162 default:
2163 abort ();
2164 break;
2165 }
2166 }
2167
2168 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2169 will equivalent CALL_EXPRs. */
2170
2171 static tree
2172 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2173 tree *tp;
2174 int *walk_subtrees ATTRIBUTE_UNUSED;
2175 void *data ATTRIBUTE_UNUSED;
2176 {
2177 tree aggr_init_expr;
2178 tree call_expr;
2179 tree fn;
2180 tree args;
2181 tree slot;
2182 tree type;
2183 enum style_t { ctor, arg, pcc } style;
2184
2185 aggr_init_expr = *tp;
2186 /* We don't need to walk into types; there's nothing in a type that
2187 needs simplification. (And, furthermore, there are places we
2188 actively don't want to go. For example, we don't want to wander
2189 into the default arguments for a FUNCTION_DECL that appears in a
2190 CALL_EXPR.) */
2191 if (TYPE_P (aggr_init_expr))
2192 {
2193 *walk_subtrees = 0;
2194 return NULL_TREE;
2195 }
2196 /* Only AGGR_INIT_EXPRs are interesting. */
2197 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2198 return NULL_TREE;
2199
2200 /* Form an appropriate CALL_EXPR. */
2201 fn = TREE_OPERAND (aggr_init_expr, 0);
2202 args = TREE_OPERAND (aggr_init_expr, 1);
2203 slot = TREE_OPERAND (aggr_init_expr, 2);
2204 type = TREE_TYPE (aggr_init_expr);
2205
2206 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2207 style = ctor;
2208 #ifdef PCC_STATIC_STRUCT_RETURN
2209 else if (1)
2210 style = pcc;
2211 #endif
2212 else if (TREE_ADDRESSABLE (type))
2213 style = arg;
2214 else
2215 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2216 handling. See build_cplus_new. */
2217 abort ();
2218
2219 if (style == ctor || style == arg)
2220 {
2221 /* Pass the address of the slot. If this is a constructor, we
2222 replace the first argument; otherwise, we tack on a new one. */
2223 if (style == ctor)
2224 args = TREE_CHAIN (args);
2225
2226 cxx_mark_addressable (slot);
2227 args = tree_cons (NULL_TREE,
2228 build1 (ADDR_EXPR,
2229 build_pointer_type (TREE_TYPE (slot)),
2230 slot),
2231 args);
2232 }
2233
2234 call_expr = build (CALL_EXPR,
2235 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2236 fn, args, NULL_TREE);
2237 TREE_SIDE_EFFECTS (call_expr) = 1;
2238
2239 if (style == arg)
2240 /* Tell the backend that we've added our return slot to the argument
2241 list. */
2242 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2243 else if (style == pcc)
2244 {
2245 /* If we're using the non-reentrant PCC calling convention, then we
2246 need to copy the returned value out of the static buffer into the
2247 SLOT. */
2248 int old_ac = flag_access_control;
2249
2250 flag_access_control = 0;
2251 call_expr = build_aggr_init (slot, call_expr,
2252 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2253 flag_access_control = old_ac;
2254 }
2255
2256 /* We want to use the value of the initialized location as the
2257 result. */
2258 call_expr = build (COMPOUND_EXPR, type,
2259 call_expr, slot);
2260
2261 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2262 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2263 *tp = call_expr;
2264
2265 /* Keep iterating. */
2266 return NULL_TREE;
2267 }
2268
2269 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2270
2271 static void
2272 emit_associated_thunks (fn)
2273 tree fn;
2274 {
2275 /* When we use vcall offsets, we emit thunks with the virtual
2276 functions to which they thunk. The whole point of vcall offsets
2277 is so that you can know statically the entire set of thunks that
2278 will ever be needed for a given virtual function, thereby
2279 enabling you to output all the thunks with the function itself. */
2280 if (DECL_VIRTUAL_P (fn))
2281 {
2282 tree thunk;
2283
2284 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2285 {
2286 use_thunk (thunk, /*emit_p=*/1);
2287 if (DECL_RESULT_THUNK_P (thunk))
2288 {
2289 tree probe;
2290
2291 for (probe = DECL_THUNKS (thunk);
2292 probe; probe = TREE_CHAIN (probe))
2293 use_thunk (probe, /*emit_p=*/1);
2294 }
2295 }
2296 }
2297 }
2298
2299 /* Generate RTL for FN. */
2300
2301 void
2302 expand_body (fn)
2303 tree fn;
2304 {
2305 location_t saved_loc;
2306 tree saved_function;
2307
2308 /* When the parser calls us after finishing the body of a template
2309 function, we don't really want to expand the body. When we're
2310 processing an in-class definition of an inline function,
2311 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2312 to look at the function itself. */
2313 if (processing_template_decl
2314 || (DECL_LANG_SPECIFIC (fn)
2315 && DECL_TEMPLATE_INFO (fn)
2316 && uses_template_parms (DECL_TI_ARGS (fn))))
2317 {
2318 /* Normally, collection only occurs in rest_of_compilation. So,
2319 if we don't collect here, we never collect junk generated
2320 during the processing of templates until we hit a
2321 non-template function. */
2322 ggc_collect ();
2323 return;
2324 }
2325
2326 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2327 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2328 simplify_aggr_init_exprs_r,
2329 NULL);
2330
2331 /* If this is a constructor or destructor body, we have to clone
2332 it. */
2333 if (maybe_clone_body (fn))
2334 {
2335 /* We don't want to process FN again, so pretend we've written
2336 it out, even though we haven't. */
2337 TREE_ASM_WRITTEN (fn) = 1;
2338 return;
2339 }
2340
2341 /* There's no reason to do any of the work here if we're only doing
2342 semantic analysis; this code just generates RTL. */
2343 if (flag_syntax_only)
2344 return;
2345
2346 /* If possible, avoid generating RTL for this function. Instead,
2347 just record it as an inline function, and wait until end-of-file
2348 to decide whether to write it out or not. */
2349 if (/* We have to generate RTL if it's not an inline function. */
2350 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2351 /* Or if we have to emit code for inline functions anyhow. */
2352 && !flag_keep_inline_functions
2353 /* Or if we actually have a reference to the function. */
2354 && !DECL_NEEDED_P (fn))
2355 {
2356 /* Set DECL_EXTERNAL so that assemble_external will be called as
2357 necessary. We'll clear it again in finish_file. */
2358 if (!DECL_EXTERNAL (fn))
2359 {
2360 DECL_NOT_REALLY_EXTERN (fn) = 1;
2361 DECL_EXTERNAL (fn) = 1;
2362 }
2363 /* Remember this function. In finish_file we'll decide if
2364 we actually need to write this function out. */
2365 defer_fn (fn);
2366 /* Let the back-end know that this function exists. */
2367 (*debug_hooks->deferred_inline_function) (fn);
2368 return;
2369 }
2370
2371 /* Compute the appropriate object-file linkage for inline
2372 functions. */
2373 if (DECL_DECLARED_INLINE_P (fn))
2374 import_export_decl (fn);
2375
2376 /* If FN is external, then there's no point in generating RTL for
2377 it. This situation can arise with an inline function under
2378 `-fexternal-templates'; we instantiate the function, even though
2379 we're not planning on emitting it, in case we get a chance to
2380 inline it. */
2381 if (DECL_EXTERNAL (fn))
2382 return;
2383
2384 /* Save the current file name and line number. When we expand the
2385 body of the function, we'll set INPUT_LOCATION so that
2386 error-mesages come out in the right places. */
2387 saved_loc = input_location;
2388 saved_function = current_function_decl;
2389 input_location = DECL_SOURCE_LOCATION (fn);
2390 current_function_decl = fn;
2391
2392 timevar_push (TV_INTEGRATION);
2393
2394 /* Optimize the body of the function before expanding it. */
2395 optimize_function (fn);
2396
2397 timevar_pop (TV_INTEGRATION);
2398 timevar_push (TV_EXPAND);
2399
2400 genrtl_start_function (fn);
2401 current_function_is_thunk = DECL_THUNK_P (fn);
2402
2403 /* Expand the body. */
2404 expand_stmt (DECL_SAVED_TREE (fn));
2405
2406 /* Statements should always be full-expressions at the outermost set
2407 of curly braces for a function. */
2408 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2409
2410 /* The outermost statement for a function contains the line number
2411 recorded when we finished processing the function. */
2412 input_line = STMT_LINENO (DECL_SAVED_TREE (fn));
2413
2414 /* Generate code for the function. */
2415 genrtl_finish_function (fn);
2416
2417 /* If possible, obliterate the body of the function so that it can
2418 be garbage collected. */
2419 if (dump_enabled_p (TDI_all))
2420 /* Keep the body; we're going to dump it. */
2421 ;
2422 else if (DECL_INLINE (fn) && flag_inline_trees)
2423 /* We might need the body of this function so that we can expand
2424 it inline somewhere else. */
2425 ;
2426 else
2427 /* We don't need the body; blow it away. */
2428 DECL_SAVED_TREE (fn) = NULL_TREE;
2429
2430 /* And restore the current source position. */
2431 current_function_decl = saved_function;
2432 input_location = saved_loc;
2433 extract_interface_info ();
2434
2435 timevar_pop (TV_EXPAND);
2436
2437 /* Emit any thunks that should be emitted at the same time as FN. */
2438 emit_associated_thunks (fn);
2439 }
2440
2441 /* Helper function for walk_tree, used by finish_function to override all
2442 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2443 value optimization. */
2444
2445 tree
2446 nullify_returns_r (tp, walk_subtrees, data)
2447 tree *tp;
2448 int *walk_subtrees;
2449 void *data;
2450 {
2451 tree nrv = (tree) data;
2452
2453 /* No need to walk into types. There wouldn't be any need to walk into
2454 non-statements, except that we have to consider STMT_EXPRs. */
2455 if (TYPE_P (*tp))
2456 *walk_subtrees = 0;
2457 else if (TREE_CODE (*tp) == RETURN_STMT)
2458 RETURN_STMT_EXPR (*tp) = NULL_TREE;
2459 else if (TREE_CODE (*tp) == CLEANUP_STMT
2460 && CLEANUP_DECL (*tp) == nrv)
2461 CLEANUP_EH_ONLY (*tp) = 1;
2462
2463 /* Keep iterating. */
2464 return NULL_TREE;
2465 }
2466
2467 /* Start generating the RTL for FN. */
2468
2469 static void
2470 genrtl_start_function (fn)
2471 tree fn;
2472 {
2473 /* Tell everybody what function we're processing. */
2474 current_function_decl = fn;
2475 /* Get the RTL machinery going for this function. */
2476 init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2477 /* Let everybody know that we're expanding this function, not doing
2478 semantic analysis. */
2479 expanding_p = 1;
2480
2481 /* Even though we're inside a function body, we still don't want to
2482 call expand_expr to calculate the size of a variable-sized array.
2483 We haven't necessarily assigned RTL to all variables yet, so it's
2484 not safe to try to expand expressions involving them. */
2485 immediate_size_expand = 0;
2486 cfun->x_dont_save_pending_sizes_p = 1;
2487
2488 /* Let the user know we're compiling this function. */
2489 announce_function (fn);
2490
2491 /* Initialize the per-function data. */
2492 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2493 if (DECL_SAVED_FUNCTION_DATA (fn))
2494 {
2495 /* If we already parsed this function, and we're just expanding it
2496 now, restore saved state. */
2497 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2498
2499 /* This function is being processed in whole-function mode; we
2500 already did semantic analysis. */
2501 cfun->x_whole_function_mode_p = 1;
2502
2503 /* If we decided that we didn't want to inline this function,
2504 make sure the back-end knows that. */
2505 if (!current_function_cannot_inline)
2506 current_function_cannot_inline = cp_function_chain->cannot_inline;
2507
2508 /* We don't need the saved data anymore. Unless this is an inline
2509 function; we need the named return value info for
2510 cp_copy_res_decl_for_inlining. */
2511 if (! DECL_INLINE (fn))
2512 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2513 }
2514
2515 /* Keep track of how many functions we're presently expanding. */
2516 ++function_depth;
2517
2518 /* Create a binding level for the parameters. */
2519 expand_function_start (fn, /*parms_have_cleanups=*/0);
2520 /* If this function is `main'. */
2521 if (DECL_MAIN_P (fn))
2522 expand_main_function ();
2523
2524 /* Give our named return value the same RTL as our RESULT_DECL. */
2525 if (current_function_return_value)
2526 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2527 }
2528
2529 /* Finish generating the RTL for FN. */
2530
2531 static void
2532 genrtl_finish_function (fn)
2533 tree fn;
2534 {
2535 tree t;
2536
2537 #if 0
2538 if (write_symbols != NO_DEBUG)
2539 {
2540 /* Keep this code around in case we later want to control debug info
2541 based on whether a type is "used". (jason 1999-11-11) */
2542
2543 tree ttype = target_type (fntype);
2544 tree parmdecl;
2545
2546 if (IS_AGGR_TYPE (ttype))
2547 /* Let debugger know it should output info for this type. */
2548 note_debug_info_needed (ttype);
2549
2550 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2551 {
2552 ttype = target_type (TREE_TYPE (parmdecl));
2553 if (IS_AGGR_TYPE (ttype))
2554 /* Let debugger know it should output info for this type. */
2555 note_debug_info_needed (ttype);
2556 }
2557 }
2558 #endif
2559
2560 /* Clean house because we will need to reorder insns here. */
2561 do_pending_stack_adjust ();
2562
2563 /* If we have a named return value, we need to force a return so that
2564 the return register is USEd. */
2565 if (DECL_NAME (DECL_RESULT (fn)))
2566 emit_jump (return_label);
2567
2568 /* We hard-wired immediate_size_expand to zero in start_function.
2569 Expand_function_end will decrement this variable. So, we set the
2570 variable to one here, so that after the decrement it will remain
2571 zero. */
2572 immediate_size_expand = 1;
2573
2574 /* Generate rtl for function exit. */
2575 expand_function_end (input_filename, input_line, 0);
2576
2577 /* If this is a nested function (like a template instantiation that
2578 we're compiling in the midst of compiling something else), push a
2579 new GC context. That will keep local variables on the stack from
2580 being collected while we're doing the compilation of this
2581 function. */
2582 if (function_depth > 1)
2583 ggc_push_context ();
2584
2585 /* There's no need to defer outputting this function any more; we
2586 know we want to output it. */
2587 DECL_DEFER_OUTPUT (fn) = 0;
2588
2589 /* Run the optimizers and output the assembler code for this
2590 function. */
2591 rest_of_compilation (fn);
2592
2593 /* Undo the call to ggc_push_context above. */
2594 if (function_depth > 1)
2595 ggc_pop_context ();
2596
2597 #if 0
2598 /* Keep this code around in case we later want to control debug info
2599 based on whether a type is "used". (jason 1999-11-11) */
2600
2601 if (ctype && TREE_ASM_WRITTEN (fn))
2602 note_debug_info_needed (ctype);
2603 #endif
2604
2605 /* If this function is marked with the constructor attribute, add it
2606 to the list of functions to be called along with constructors
2607 from static duration objects. */
2608 if (DECL_STATIC_CONSTRUCTOR (fn))
2609 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2610
2611 /* If this function is marked with the destructor attribute, add it
2612 to the list of functions to be called along with destructors from
2613 static duration objects. */
2614 if (DECL_STATIC_DESTRUCTOR (fn))
2615 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2616
2617 --function_depth;
2618
2619 /* In C++, we should never be saving RTL for the function. */
2620 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2621
2622 /* Since we don't need the RTL for this function anymore, stop
2623 pointing to it. That's especially important for LABEL_DECLs,
2624 since you can reach all the instructions in the function from the
2625 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2626 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2627 local variables. */
2628 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2629 clear_decl_rtl,
2630 NULL);
2631
2632 /* Clear out the RTL for the arguments. */
2633 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2634 {
2635 SET_DECL_RTL (t, NULL_RTX);
2636 DECL_INCOMING_RTL (t) = NULL_RTX;
2637 }
2638
2639 if (!(flag_inline_trees && DECL_INLINE (fn)))
2640 /* DECL_INITIAL must remain nonzero so we know this was an
2641 actual function definition. */
2642 DECL_INITIAL (fn) = error_mark_node;
2643
2644 /* Let the error reporting routines know that we're outside a
2645 function. For a nested function, this value is used in
2646 pop_cp_function_context and then reset via pop_function_context. */
2647 current_function_decl = NULL_TREE;
2648 }
2649
2650 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2651 its sub-blocks. */
2652
2653 static tree
2654 clear_decl_rtl (tp, walk_subtrees, data)
2655 tree *tp;
2656 int *walk_subtrees ATTRIBUTE_UNUSED;
2657 void *data ATTRIBUTE_UNUSED;
2658 {
2659 if (nonstatic_local_decl_p (*tp))
2660 SET_DECL_RTL (*tp, NULL_RTX);
2661
2662 return NULL_TREE;
2663 }
2664
2665 /* Perform initialization related to this module. */
2666
2667 void
2668 init_cp_semantics ()
2669 {
2670 lang_expand_stmt = cp_expand_stmt;
2671 }
2672
2673 #include "gt-cp-semantics.h"