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