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