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