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