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