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