* stmt.c (parse_output_constraint): New function, split out
[official-gcc.git] / gcc / cp / semantics.c
blob2bb4051affc64317ce3f5b21ed9fbb6e48578ea2
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.
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.
10 This file is part of GNU CC.
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.
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.
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. */
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 "expr.h"
38 #include "output.h"
39 #include "timevar.h"
41 /* There routines provide a modular interface to perform many parsing
42 operations. They may therefore be used during actual parsing, or
43 during template instantiation, which may be regarded as a
44 degenerate form of parsing. Since the current g++ parser is
45 lacking in several respects, and will be reimplemented, we are
46 attempting to move most code that is not directly related to
47 parsing into this file; that will make implementing the new parser
48 much easier since it will be able to make use of these routines. */
50 static tree maybe_convert_cond PARAMS ((tree));
51 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
52 static tree nullify_returns_r PARAMS ((tree *, int *, void *));
53 static void deferred_type_access_control PARAMS ((void));
54 static void emit_associated_thunks PARAMS ((tree));
55 static void genrtl_try_block PARAMS ((tree));
56 static void genrtl_eh_spec_block PARAMS ((tree));
57 static void genrtl_handler PARAMS ((tree));
58 static void genrtl_ctor_stmt PARAMS ((tree));
59 static void genrtl_subobject PARAMS ((tree));
60 static void genrtl_named_return_value PARAMS ((void));
61 static void cp_expand_stmt PARAMS ((tree));
62 static void genrtl_start_function PARAMS ((tree));
63 static void genrtl_finish_function PARAMS ((tree));
64 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
66 /* Finish processing the COND, the SUBSTMT condition for STMT. */
68 #define FINISH_COND(cond, stmt, substmt) \
69 do { \
70 if (last_tree != stmt) \
71 { \
72 RECHAIN_STMTS (stmt, substmt); \
73 if (!processing_template_decl) \
74 { \
75 cond = build_tree_list (substmt, cond); \
76 substmt = cond; \
77 } \
78 } \
79 else \
80 substmt = cond; \
81 } while (0)
83 /* Returns non-zero if the current statement is a full expression,
84 i.e. temporaries created during that statement should be destroyed
85 at the end of the statement. */
87 int
88 stmts_are_full_exprs_p ()
90 return current_stmt_tree ()->stmts_are_full_exprs_p;
93 /* Returns the stmt_tree (if any) to which statements are currently
94 being added. If there is no active statement-tree, NULL is
95 returned. */
97 stmt_tree
98 current_stmt_tree ()
100 return (cfun
101 ? &cfun->language->x_stmt_tree
102 : &scope_chain->x_stmt_tree);
105 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
106 flag for this because "A union for which objects or pointers are
107 declared is not an anonymous union" [class.union]. */
110 anon_aggr_type_p (node)
111 tree node;
113 return (CLASS_TYPE_P (node) && TYPE_LANG_SPECIFIC(node)->anon_aggr);
116 /* Finish a scope. */
118 tree
119 do_poplevel ()
121 tree block = NULL_TREE;
123 if (stmts_are_full_exprs_p ())
125 tree scope_stmts = NULL_TREE;
127 if (!processing_template_decl)
128 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
130 block = poplevel (kept_level_p (), 1, 0);
131 if (block && !processing_template_decl)
133 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
134 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
138 return block;
141 /* Begin a new scope. */
143 void
144 do_pushlevel ()
146 if (stmts_are_full_exprs_p ())
148 pushlevel (0);
149 if (!processing_template_decl)
150 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
154 /* Finish a goto-statement. */
156 tree
157 finish_goto_stmt (destination)
158 tree destination;
160 if (TREE_CODE (destination) == IDENTIFIER_NODE)
161 destination = lookup_label (destination);
163 /* We warn about unused labels with -Wunused. That means we have to
164 mark the used labels as used. */
165 if (TREE_CODE (destination) == LABEL_DECL)
166 TREE_USED (destination) = 1;
168 if (TREE_CODE (destination) != LABEL_DECL)
169 /* We don't inline calls to functions with computed gotos.
170 Those functions are typically up to some funny business,
171 and may be depending on the labels being at particular
172 addresses, or some such. */
173 DECL_UNINLINABLE (current_function_decl) = 1;
175 check_goto (destination);
177 return add_stmt (build_stmt (GOTO_STMT, destination));
180 /* COND is the condition-expression for an if, while, etc.,
181 statement. Convert it to a boolean value, if appropriate. */
183 tree
184 maybe_convert_cond (cond)
185 tree cond;
187 /* Empty conditions remain empty. */
188 if (!cond)
189 return NULL_TREE;
191 /* Wait until we instantiate templates before doing conversion. */
192 if (processing_template_decl)
193 return cond;
195 /* Do the conversion. */
196 cond = convert_from_reference (cond);
197 return condition_conversion (cond);
200 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
202 tree
203 finish_expr_stmt (expr)
204 tree expr;
206 tree r = NULL_TREE;
208 if (expr != NULL_TREE)
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);
217 if (stmts_are_full_exprs_p ())
218 expr = convert_to_void (expr, "statement");
220 r = add_stmt (build_stmt (EXPR_STMT, expr));
223 finish_stmt ();
225 /* This was an expression-statement, so we save the type of the
226 expression. */
227 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
229 return r;
233 /* Begin an if-statement. Returns a newly created IF_STMT if
234 appropriate. */
236 tree
237 begin_if_stmt ()
239 tree r;
240 do_pushlevel ();
241 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
242 add_stmt (r);
243 return r;
246 /* Process the COND of an if-statement, which may be given by
247 IF_STMT. */
249 void
250 finish_if_stmt_cond (cond, if_stmt)
251 tree cond;
252 tree if_stmt;
254 cond = maybe_convert_cond (cond);
255 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
258 /* Finish the then-clause of an if-statement, which may be given by
259 IF_STMT. */
261 tree
262 finish_then_clause (if_stmt)
263 tree if_stmt;
265 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
266 last_tree = if_stmt;
267 return if_stmt;
270 /* Begin the else-clause of an if-statement. */
272 void
273 begin_else_clause ()
277 /* Finish the else-clause of an if-statement, which may be given by
278 IF_STMT. */
280 void
281 finish_else_clause (if_stmt)
282 tree if_stmt;
284 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
287 /* Finish an if-statement. */
289 void
290 finish_if_stmt ()
292 do_poplevel ();
293 finish_stmt ();
296 void
297 clear_out_block ()
299 /* If COND wasn't a declaration, clear out the
300 block we made for it and start a new one here so the
301 optimization in expand_end_loop will work. */
302 if (getdecls () == NULL_TREE)
304 do_poplevel ();
305 do_pushlevel ();
309 /* Begin a while-statement. Returns a newly created WHILE_STMT if
310 appropriate. */
312 tree
313 begin_while_stmt ()
315 tree r;
316 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
317 add_stmt (r);
318 do_pushlevel ();
319 return r;
322 /* Process the COND of a while-statement, which may be given by
323 WHILE_STMT. */
325 void
326 finish_while_stmt_cond (cond, while_stmt)
327 tree cond;
328 tree while_stmt;
330 cond = maybe_convert_cond (cond);
331 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
332 clear_out_block ();
335 /* Finish a while-statement, which may be given by WHILE_STMT. */
337 void
338 finish_while_stmt (while_stmt)
339 tree while_stmt;
341 do_poplevel ();
342 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
343 finish_stmt ();
346 /* Begin a do-statement. Returns a newly created DO_STMT if
347 appropriate. */
349 tree
350 begin_do_stmt ()
352 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
353 add_stmt (r);
354 return r;
357 /* Finish the body of a do-statement, which may be given by DO_STMT. */
359 void
360 finish_do_body (do_stmt)
361 tree do_stmt;
363 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
366 /* Finish a do-statement, which may be given by DO_STMT, and whose
367 COND is as indicated. */
369 void
370 finish_do_stmt (cond, do_stmt)
371 tree cond;
372 tree do_stmt;
374 cond = maybe_convert_cond (cond);
375 DO_COND (do_stmt) = cond;
376 finish_stmt ();
379 /* Finish a return-statement. The EXPRESSION returned, if any, is as
380 indicated. */
382 tree
383 finish_return_stmt (expr)
384 tree expr;
386 tree r;
388 if (!processing_template_decl)
389 expr = check_return_expr (expr);
390 if (!processing_template_decl)
392 if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
394 /* Even returns without a value in a constructor must return
395 `this'. We accomplish this by sending all returns in a
396 constructor to the CTOR_LABEL; finish_function emits code to
397 return a value there. When we finally generate the real
398 return statement, CTOR_LABEL is no longer set, and we fall
399 through into the normal return-processing code below. */
400 return finish_goto_stmt (ctor_label);
402 else if (DECL_DESTRUCTOR_P (current_function_decl))
404 /* Similarly, all destructors must run destructors for
405 base-classes before returning. So, all returns in a
406 destructor get sent to the DTOR_LABEL; finish_function emits
407 code to return a value there. */
408 return finish_goto_stmt (dtor_label);
411 r = add_stmt (build_stmt (RETURN_STMT, expr));
412 finish_stmt ();
414 return r;
417 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
419 tree
420 begin_for_stmt ()
422 tree r;
424 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
425 NULL_TREE, NULL_TREE);
426 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
427 add_stmt (r);
428 if (NEW_FOR_SCOPE_P (r))
430 do_pushlevel ();
431 note_level_for_for ();
434 return r;
437 /* Finish the for-init-statement of a for-statement, which may be
438 given by FOR_STMT. */
440 void
441 finish_for_init_stmt (for_stmt)
442 tree for_stmt;
444 if (last_tree != for_stmt)
445 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
446 do_pushlevel ();
449 /* Finish the COND of a for-statement, which may be given by
450 FOR_STMT. */
452 void
453 finish_for_cond (cond, for_stmt)
454 tree cond;
455 tree for_stmt;
457 cond = maybe_convert_cond (cond);
458 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
459 clear_out_block ();
462 /* Finish the increment-EXPRESSION in a for-statement, which may be
463 given by FOR_STMT. */
465 void
466 finish_for_expr (expr, for_stmt)
467 tree expr;
468 tree for_stmt;
470 FOR_EXPR (for_stmt) = expr;
473 /* Finish the body of a for-statement, which may be given by
474 FOR_STMT. The increment-EXPR for the loop must be
475 provided. */
477 void
478 finish_for_stmt (for_stmt)
479 tree for_stmt;
481 /* Pop the scope for the body of the loop. */
482 do_poplevel ();
483 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
484 if (NEW_FOR_SCOPE_P (for_stmt))
485 do_poplevel ();
486 finish_stmt ();
489 /* Finish a break-statement. */
491 tree
492 finish_break_stmt ()
494 return add_stmt (build_break_stmt ());
497 /* Finish a continue-statement. */
499 tree
500 finish_continue_stmt ()
502 return add_stmt (build_continue_stmt ());
505 /* Begin a switch-statement. Returns a new SWITCH_STMT if
506 appropriate. */
508 tree
509 begin_switch_stmt ()
511 tree r;
512 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE);
513 add_stmt (r);
514 do_pushlevel ();
515 return r;
518 /* Finish the cond of a switch-statement. */
520 void
521 finish_switch_cond (cond, switch_stmt)
522 tree cond;
523 tree switch_stmt;
525 if (!processing_template_decl)
527 tree type;
528 tree index;
530 /* Convert the condition to an integer or enumeration type. */
531 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, 1);
532 if (cond == NULL_TREE)
534 error ("switch quantity not an integer");
535 cond = error_mark_node;
537 if (cond != error_mark_node)
539 cond = default_conversion (cond);
540 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
543 type = TREE_TYPE (cond);
544 index = get_unwidened (cond, NULL_TREE);
545 /* We can't strip a conversion from a signed type to an unsigned,
546 because if we did, int_fits_type_p would do the wrong thing
547 when checking case values for being in range,
548 and it's too hard to do the right thing. */
549 if (TREE_UNSIGNED (TREE_TYPE (cond))
550 == TREE_UNSIGNED (TREE_TYPE (index)))
551 cond = index;
553 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
554 push_switch (switch_stmt);
557 /* Finish the body of a switch-statement, which may be given by
558 SWITCH_STMT. The COND to switch on is indicated. */
560 void
561 finish_switch_stmt (switch_stmt)
562 tree switch_stmt;
564 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
565 pop_switch ();
566 do_poplevel ();
567 finish_stmt ();
570 /* Generate the RTL for T, which is a TRY_BLOCK. */
572 static void
573 genrtl_try_block (t)
574 tree t;
576 if (CLEANUP_P (t))
578 expand_eh_region_start ();
579 expand_stmt (TRY_STMTS (t));
580 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
582 else
584 if (!FN_TRY_BLOCK_P (t))
585 emit_line_note (input_filename, lineno);
587 expand_eh_region_start ();
588 expand_stmt (TRY_STMTS (t));
590 if (FN_TRY_BLOCK_P (t))
592 end_protect_partials ();
593 expand_start_all_catch ();
594 in_function_try_handler = 1;
595 expand_stmt (TRY_HANDLERS (t));
596 in_function_try_handler = 0;
597 expand_end_all_catch ();
599 else
601 expand_start_all_catch ();
602 expand_stmt (TRY_HANDLERS (t));
603 expand_end_all_catch ();
608 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
610 static void
611 genrtl_eh_spec_block (t)
612 tree t;
614 expand_eh_region_start ();
615 expand_stmt (EH_SPEC_STMTS (t));
616 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
617 build_call (call_unexpected_node,
618 tree_cons (NULL_TREE,
619 build_exc_ptr (),
620 NULL_TREE)));
623 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
624 appropriate. */
626 tree
627 begin_try_block ()
629 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
630 add_stmt (r);
631 return r;
634 /* Likewise, for a function-try-block. */
636 tree
637 begin_function_try_block ()
639 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
640 FN_TRY_BLOCK_P (r) = 1;
641 add_stmt (r);
642 return r;
645 /* Finish a try-block, which may be given by TRY_BLOCK. */
647 void
648 finish_try_block (try_block)
649 tree try_block;
651 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
654 /* Finish the body of a cleanup try-block, which may be given by
655 TRY_BLOCK. */
657 void
658 finish_cleanup_try_block (try_block)
659 tree try_block;
661 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
664 /* Finish an implicitly generated try-block, with a cleanup is given
665 by CLEANUP. */
667 void
668 finish_cleanup (cleanup, try_block)
669 tree cleanup;
670 tree try_block;
672 TRY_HANDLERS (try_block) = cleanup;
673 CLEANUP_P (try_block) = 1;
676 /* Likewise, for a function-try-block. */
678 void
679 finish_function_try_block (try_block)
680 tree try_block;
682 if (TREE_CHAIN (try_block)
683 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
685 /* Chain the compound statement after the CTOR_INITIALIZER. */
686 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
687 /* And make the CTOR_INITIALIZER the body of the try-block. */
688 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
690 else
691 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
692 in_function_try_handler = 1;
695 /* Finish a handler-sequence for a try-block, which may be given by
696 TRY_BLOCK. */
698 void
699 finish_handler_sequence (try_block)
700 tree try_block;
702 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
703 check_handlers (TRY_HANDLERS (try_block));
706 /* Likewise, for a function-try-block. */
708 void
709 finish_function_handler_sequence (try_block)
710 tree try_block;
712 in_function_try_handler = 0;
713 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
714 check_handlers (TRY_HANDLERS (try_block));
717 /* Generate the RTL for T, which is a HANDLER. */
719 static void
720 genrtl_handler (t)
721 tree t;
723 genrtl_do_pushlevel ();
724 if (!processing_template_decl)
725 expand_start_catch (HANDLER_TYPE (t));
726 expand_stmt (HANDLER_BODY (t));
727 if (!processing_template_decl)
728 expand_end_catch ();
731 /* Begin a handler. Returns a HANDLER if appropriate. */
733 tree
734 begin_handler ()
736 tree r;
737 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
738 add_stmt (r);
739 /* Create a binding level for the eh_info and the exception object
740 cleanup. */
741 do_pushlevel ();
742 note_level_for_catch ();
743 return r;
746 /* Finish the handler-parameters for a handler, which may be given by
747 HANDLER. DECL is the declaration for the catch parameter, or NULL
748 if this is a `catch (...)' clause. */
750 void
751 finish_handler_parms (decl, handler)
752 tree decl;
753 tree handler;
755 tree type = NULL_TREE;
756 if (processing_template_decl)
758 if (decl)
760 decl = pushdecl (decl);
761 decl = push_template_decl (decl);
762 add_decl_stmt (decl);
763 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
764 type = TREE_TYPE (decl);
767 else
768 type = expand_start_catch_block (decl);
770 HANDLER_TYPE (handler) = type;
773 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
774 the return value from the matching call to finish_handler_parms. */
776 void
777 finish_handler (handler)
778 tree handler;
780 if (!processing_template_decl)
781 expand_end_catch_block ();
782 do_poplevel ();
783 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
786 /* Generate the RTL for T, which is a CTOR_STMT. */
788 static void
789 genrtl_ctor_stmt (t)
790 tree t;
792 if (CTOR_BEGIN_P (t))
793 begin_protect_partials ();
794 else
795 /* After this point, any exceptions will cause the
796 destructor to be executed, so we no longer need to worry
797 about destroying the various subobjects ourselves. */
798 end_protect_partials ();
801 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
802 compound-statement does not define a scope. Returns a new
803 COMPOUND_STMT if appropriate. */
805 tree
806 begin_compound_stmt (has_no_scope)
807 int has_no_scope;
809 tree r;
810 int is_try = 0;
812 r = build_stmt (COMPOUND_STMT, NULL_TREE);
814 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
815 is_try = 1;
817 add_stmt (r);
818 if (has_no_scope)
819 COMPOUND_STMT_NO_SCOPE (r) = 1;
821 last_expr_type = NULL_TREE;
823 if (!has_no_scope)
825 do_pushlevel ();
826 if (is_try)
827 note_level_for_try ();
829 else
830 /* Normally, we try hard to keep the BLOCK for a
831 statement-expression. But, if it's a statement-expression with
832 a scopeless block, there's nothing to keep, and we don't want
833 to accidentally keep a block *inside* the scopeless block. */
834 keep_next_level (0);
836 return r;
839 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
840 If HAS_NO_SCOPE is non-zero, the compound statement does not define
841 a scope. */
843 tree
844 finish_compound_stmt (has_no_scope, compound_stmt)
845 int has_no_scope;
846 tree compound_stmt;
848 tree r;
849 tree t;
851 if (!has_no_scope)
852 r = do_poplevel ();
853 else
854 r = NULL_TREE;
856 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
858 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
859 the precise purpose of that variable is store the type of the
860 last expression statement within the last compound statement, we
861 preserve the value. */
862 t = last_expr_type;
863 finish_stmt ();
864 last_expr_type = t;
866 return r;
869 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
870 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
871 CLOBBERS. */
873 tree
874 finish_asm_stmt (cv_qualifier, string, output_operands,
875 input_operands, clobbers)
876 tree cv_qualifier;
877 tree string;
878 tree output_operands;
879 tree input_operands;
880 tree clobbers;
882 tree r;
883 tree t;
885 if (TREE_CHAIN (string))
886 string = combine_strings (string);
888 if (cv_qualifier != NULL_TREE
889 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
891 cp_warning ("%s qualifier ignored on asm",
892 IDENTIFIER_POINTER (cv_qualifier));
893 cv_qualifier = NULL_TREE;
896 if (!processing_template_decl)
898 int i;
899 int ninputs;
900 int noutputs;
902 for (t = input_operands; t; t = TREE_CHAIN (t))
904 tree converted_operand
905 = decay_conversion (TREE_VALUE (t));
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)
913 cp_error ("type of asm operand `%E' could not be determined",
914 TREE_VALUE (t));
915 converted_operand = error_mark_node;
917 TREE_VALUE (t) = converted_operand;
920 ninputs = list_length (input_operands);
921 noutputs = list_length (output_operands);
923 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
925 bool allows_mem;
926 bool allows_reg;
927 bool is_inout;
928 const char *constraint;
929 tree operand;
931 constraint = TREE_STRING_POINTER (TREE_PURPOSE (t));
932 operand = TREE_VALUE (output_operands);
934 if (!parse_output_constraint (&constraint,
935 i, ninputs, noutputs,
936 &allows_mem,
937 &allows_reg,
938 &is_inout))
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;
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 mark_addressable (operand);
957 r = build_stmt (ASM_STMT, cv_qualifier, string,
958 output_operands, input_operands,
959 clobbers);
960 return add_stmt (r);
963 /* Finish a label with the indicated NAME. */
965 void
966 finish_label_stmt (name)
967 tree name;
969 tree decl = define_label (input_filename, lineno, name);
970 add_stmt (build_stmt (LABEL_STMT, decl));
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. */
977 void
978 finish_label_decl (name)
979 tree name;
981 tree decl = declare_local_label (name);
982 add_decl_stmt (decl);
985 /* Generate the RTL for a SUBOBJECT. */
987 static void
988 genrtl_subobject (cleanup)
989 tree cleanup;
991 add_partial_entry (cleanup);
994 /* We're in a constructor, and have just constructed a a subobject of
995 *THIS. CLEANUP is code to run if an exception is thrown before the
996 end of the current function is reached. */
998 void
999 finish_subobject (cleanup)
1000 tree cleanup;
1002 tree r = build_stmt (SUBOBJECT, cleanup);
1003 add_stmt (r);
1006 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1008 void
1009 finish_decl_cleanup (decl, cleanup)
1010 tree decl;
1011 tree cleanup;
1013 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1016 /* Generate the RTL for a RETURN_INIT. */
1018 static void
1019 genrtl_named_return_value ()
1021 tree decl = DECL_RESULT (current_function_decl);
1023 /* If this named return value comes in a register, put it in a
1024 pseudo-register. */
1025 if (DECL_REGISTER (decl))
1027 /* Note that the mode of the old DECL_RTL may be wider than the
1028 mode of DECL_RESULT, depending on the calling conventions for
1029 the processor. For example, on the Alpha, a 32-bit integer
1030 is returned in a DImode register -- the DECL_RESULT has
1031 SImode but the DECL_RTL for the DECL_RESULT has DImode. So,
1032 here, we use the mode the back-end has already assigned for
1033 the return value. */
1034 SET_DECL_RTL (decl, gen_reg_rtx (GET_MODE (DECL_RTL (decl))));
1035 if (TREE_ADDRESSABLE (decl))
1036 put_var_into_stack (decl);
1039 emit_local_var (decl);
1042 /* Bind a name and initialization to the return value of
1043 the current function. */
1045 void
1046 finish_named_return_value (return_id, init)
1047 tree return_id, init;
1049 tree decl = DECL_RESULT (current_function_decl);
1051 /* Give this error as many times as there are occurrences, so that
1052 users can use Emacs compilation buffers to find and fix all such
1053 places. */
1054 if (pedantic)
1055 pedwarn ("ISO C++ does not permit named return values");
1056 cp_deprecated ("the named return value extension");
1058 if (return_id != NULL_TREE)
1060 if (DECL_NAME (decl) == NULL_TREE)
1061 DECL_NAME (decl) = return_id;
1062 else
1064 cp_error ("return identifier `%D' already in place", return_id);
1065 return;
1069 /* Can't let this happen for constructors. */
1070 if (DECL_CONSTRUCTOR_P (current_function_decl))
1072 error ("can't redefine default return value for constructors");
1073 return;
1076 /* If we have a named return value, put that in our scope as well. */
1077 if (DECL_NAME (decl) != NULL_TREE)
1079 /* Let `cp_finish_decl' know that this initializer is ok. */
1080 DECL_INITIAL (decl) = init;
1081 if (doing_semantic_analysis_p ())
1082 pushdecl (decl);
1083 if (!processing_template_decl)
1085 cp_finish_decl (decl, init, NULL_TREE, 0);
1086 add_stmt (build_stmt (RETURN_INIT, NULL_TREE, NULL_TREE));
1088 else
1089 add_stmt (build_stmt (RETURN_INIT, return_id, init));
1092 /* Don't use tree-inlining for functions with named return values.
1093 That doesn't work properly because we don't do any translation of
1094 the RETURN_INITs when they are copied. */
1095 DECL_UNINLINABLE (current_function_decl) = 1;
1098 /* The INIT_LIST is a list of mem-initializers, in the order they were
1099 written by the user. The TREE_VALUE of each node is a list of
1100 initializers for a particular subobject. The TREE_PURPOSE is a
1101 FIELD_DECL is the initializer is for a non-static data member, and
1102 a class type if the initializer is for a base class. */
1104 void
1105 finish_mem_initializers (init_list)
1106 tree init_list;
1108 tree member_init_list;
1109 tree base_init_list;
1110 tree last_base_warned_about;
1111 tree next;
1112 tree init;
1114 member_init_list = NULL_TREE;
1115 base_init_list = NULL_TREE;
1116 last_base_warned_about = NULL_TREE;
1118 for (init = init_list; init; init = next)
1120 next = TREE_CHAIN (init);
1121 if (TREE_CODE (TREE_PURPOSE (init)) == FIELD_DECL)
1123 TREE_CHAIN (init) = member_init_list;
1124 member_init_list = init;
1126 /* We're running through the initializers from right to left
1127 as we process them here. So, if we see a data member
1128 initializer after we see a base initializer, that
1129 actually means that the base initializer preceeded the
1130 data member initializer. */
1131 if (warn_reorder && last_base_warned_about != base_init_list)
1133 tree base;
1135 for (base = base_init_list;
1136 base != last_base_warned_about;
1137 base = TREE_CHAIN (base))
1139 cp_warning ("base initializer for `%T'",
1140 TREE_PURPOSE (base));
1141 warning (" will be re-ordered to precede member initializations");
1144 last_base_warned_about = base_init_list;
1147 else
1149 TREE_CHAIN (init) = base_init_list;
1150 base_init_list = init;
1154 setup_vtbl_ptr (member_init_list, base_init_list);
1157 /* Cache the value of this class's main virtual function table pointer
1158 in a register variable. This will save one indirection if a
1159 more than one virtual function call is made this function. */
1161 void
1162 setup_vtbl_ptr (member_init_list, base_init_list)
1163 tree member_init_list;
1164 tree base_init_list;
1166 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1168 /* If we've already done this, there's no need to do it again. */
1169 if (vtbls_set_up_p)
1170 return;
1172 if (DECL_CONSTRUCTOR_P (current_function_decl))
1174 if (processing_template_decl)
1175 add_stmt (build_min_nt
1176 (CTOR_INITIALIZER,
1177 member_init_list, base_init_list));
1178 else
1180 tree ctor_stmt;
1182 /* Mark the beginning of the constructor. */
1183 ctor_stmt = build_stmt (CTOR_STMT);
1184 CTOR_BEGIN_P (ctor_stmt) = 1;
1185 add_stmt (ctor_stmt);
1187 /* And actually initialize the base-classes and members. */
1188 emit_base_init (member_init_list, base_init_list);
1191 else if (DECL_DESTRUCTOR_P (current_function_decl)
1192 && !processing_template_decl)
1194 tree if_stmt;
1195 tree compound_stmt;
1197 /* If the dtor is empty, and we know there is not any possible
1198 way we could use any vtable entries, before they are possibly
1199 set by a base class dtor, we don't have to setup the vtables,
1200 as we know that any base class dtor will set up any vtables
1201 it needs. We avoid MI, because one base class dtor can do a
1202 virtual dispatch to an overridden function that would need to
1203 have a non-related vtable set up, we cannot avoid setting up
1204 vtables in that case. We could change this to see if there
1205 is just one vtable. */
1206 if_stmt = begin_if_stmt ();
1208 /* If it is not safe to avoid setting up the vtables, then
1209 someone will change the condition to be boolean_true_node.
1210 (Actually, for now, we do not have code to set the condition
1211 appropriately, so we just assume that we always need to
1212 initialize the vtables.) */
1213 finish_if_stmt_cond (boolean_true_node, if_stmt);
1214 current_vcalls_possible_p = &IF_COND (if_stmt);
1216 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1218 /* Make all virtual function table pointers in non-virtual base
1219 classes point to CURRENT_CLASS_TYPE's virtual function
1220 tables. */
1221 initialize_vtbl_ptrs (current_class_ptr);
1223 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1224 finish_then_clause (if_stmt);
1225 finish_if_stmt ();
1228 /* Always keep the BLOCK node associated with the outermost pair of
1229 curly braces of a function. These are needed for correct
1230 operation of dwarfout.c. */
1231 keep_next_level (1);
1233 /* The virtual function tables are set up now. */
1234 vtbls_set_up_p = 1;
1237 /* Returns the stack of SCOPE_STMTs for the current function. */
1239 tree *
1240 current_scope_stmt_stack ()
1242 return &cfun->language->x_scope_stmt_stack;
1245 /* Finish a parenthesized expression EXPR. */
1247 tree
1248 finish_parenthesized_expr (expr)
1249 tree expr;
1251 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1252 /* This inhibits warnings in truthvalue_conversion. */
1253 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1255 if (TREE_CODE (expr) == OFFSET_REF)
1256 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1257 enclosed in parentheses. */
1258 PTRMEM_OK_P (expr) = 0;
1259 return expr;
1262 /* Begin a statement-expression. The value returned must be passed to
1263 finish_stmt_expr. */
1265 tree
1266 begin_stmt_expr ()
1268 /* If we're outside a function, we won't have a statement-tree to
1269 work with. But, if we see a statement-expression we need to
1270 create one. */
1271 if (! cfun && !last_tree)
1272 begin_stmt_tree (&scope_chain->x_saved_tree);
1274 keep_next_level (1);
1275 /* If we're building a statement tree, then the upcoming compound
1276 statement will be chained onto the tree structure, starting at
1277 last_tree. We return last_tree so that we can later unhook the
1278 compound statement. */
1279 return last_tree;
1282 /* Used when beginning a statement-expression outside function scope.
1283 For example, when handling a file-scope initializer, we use this
1284 function. */
1286 tree
1287 begin_global_stmt_expr ()
1289 if (! cfun && !last_tree)
1290 begin_stmt_tree (&scope_chain->x_saved_tree);
1292 keep_next_level (1);
1294 return (last_tree != NULL_TREE) ? last_tree : expand_start_stmt_expr();
1297 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1299 tree
1300 finish_global_stmt_expr (stmt_expr)
1301 tree stmt_expr;
1303 stmt_expr = expand_end_stmt_expr (stmt_expr);
1305 if (! cfun
1306 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1307 finish_stmt_tree (&scope_chain->x_saved_tree);
1309 return stmt_expr;
1312 /* Finish a statement-expression. RTL_EXPR should be the value
1313 returned by the previous begin_stmt_expr; EXPR is the
1314 statement-expression. Returns an expression representing the
1315 statement-expression. */
1317 tree
1318 finish_stmt_expr (rtl_expr)
1319 tree rtl_expr;
1321 tree result;
1323 /* If the last thing in the statement-expression was not an
1324 expression-statement, then it has type `void'. */
1325 if (!last_expr_type)
1326 last_expr_type = void_type_node;
1327 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1328 TREE_SIDE_EFFECTS (result) = 1;
1330 /* Remove the compound statement from the tree structure; it is
1331 now saved in the STMT_EXPR. */
1332 last_tree = rtl_expr;
1333 TREE_CHAIN (last_tree) = NULL_TREE;
1335 /* If we created a statement-tree for this statement-expression,
1336 remove it now. */
1337 if (! cfun
1338 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1339 finish_stmt_tree (&scope_chain->x_saved_tree);
1341 return result;
1344 /* Finish a call to FN with ARGS. Returns a representation of the
1345 call. */
1347 tree
1348 finish_call_expr (fn, args, koenig)
1349 tree fn;
1350 tree args;
1351 int koenig;
1353 tree result;
1355 if (koenig)
1357 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1358 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1359 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1360 fn = do_identifier (fn, 2, args);
1362 result = build_x_function_call (fn, args, current_class_ref);
1364 if (TREE_CODE (result) == CALL_EXPR
1365 && (! TREE_TYPE (result)
1366 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1367 result = require_complete_type (result);
1369 return result;
1372 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1373 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1374 POSTDECREMENT_EXPR.) */
1376 tree
1377 finish_increment_expr (expr, code)
1378 tree expr;
1379 enum tree_code code;
1381 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1382 a COMPONENT_REF). This way if we've got, say, a reference to a
1383 static member that's being operated on, we don't end up trying to
1384 find a member operator for the class it's in. */
1386 if (TREE_CODE (expr) == OFFSET_REF)
1387 expr = resolve_offset_ref (expr);
1388 return build_x_unary_op (code, expr);
1391 /* Finish a use of `this'. Returns an expression for `this'. */
1393 tree
1394 finish_this_expr ()
1396 tree result;
1398 if (current_class_ptr)
1400 #ifdef WARNING_ABOUT_CCD
1401 TREE_USED (current_class_ptr) = 1;
1402 #endif
1403 result = current_class_ptr;
1405 else if (current_function_decl
1406 && DECL_STATIC_FUNCTION_P (current_function_decl))
1408 error ("`this' is unavailable for static member functions");
1409 result = error_mark_node;
1411 else
1413 if (current_function_decl)
1414 error ("invalid use of `this' in non-member function");
1415 else
1416 error ("invalid use of `this' at top level");
1417 result = error_mark_node;
1420 return result;
1423 /* Finish a member function call using OBJECT and ARGS as arguments to
1424 FN. Returns an expression for the call. */
1426 tree
1427 finish_object_call_expr (fn, object, args)
1428 tree fn;
1429 tree object;
1430 tree args;
1432 #if 0
1433 /* This is a future direction of this code, but because
1434 build_x_function_call cannot always undo what is done in
1435 build_component_ref entirely yet, we cannot do this. */
1437 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1438 return finish_call_expr (real_fn, args);
1439 #else
1440 if (DECL_DECLARES_TYPE_P (fn))
1442 if (processing_template_decl)
1443 /* This can happen on code like:
1445 class X;
1446 template <class T> void f(T t) {
1447 t.X();
1450 We just grab the underlying IDENTIFIER. */
1451 fn = DECL_NAME (fn);
1452 else
1454 cp_error ("calling type `%T' like a method", fn);
1455 return error_mark_node;
1459 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1460 #endif
1463 /* Finish a qualified member function call using OBJECT and ARGS as
1464 arguments to FN. Returns an expression for the call. */
1466 tree
1467 finish_qualified_object_call_expr (fn, object, args)
1468 tree fn;
1469 tree object;
1470 tree args;
1472 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1473 TREE_OPERAND (fn, 1), args);
1476 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1477 being the scope, if any, of DESTRUCTOR. Returns an expression for
1478 the call. */
1480 tree
1481 finish_pseudo_destructor_call_expr (object, scope, destructor)
1482 tree object;
1483 tree scope;
1484 tree destructor;
1486 if (processing_template_decl)
1487 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1489 if (scope && scope != destructor)
1490 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1491 scope, destructor);
1493 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1494 && (TREE_CODE (TREE_TYPE (object)) !=
1495 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1496 cp_error ("`%E' is not of type `%T'", object, destructor);
1498 return cp_convert (void_type_node, object);
1501 /* Finish a call to a globally qualified member function FN using
1502 ARGS. Returns an expression for the call. */
1504 tree
1505 finish_qualified_call_expr (fn, args)
1506 tree fn;
1507 tree args;
1509 if (processing_template_decl)
1510 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1511 else
1512 return build_member_call (TREE_OPERAND (fn, 0),
1513 TREE_OPERAND (fn, 1),
1514 args);
1517 /* Finish an expression of the form CODE EXPR. */
1519 tree
1520 finish_unary_op_expr (code, expr)
1521 enum tree_code code;
1522 tree expr;
1524 tree result = build_x_unary_op (code, expr);
1525 /* Inside a template, build_x_unary_op does not fold the
1526 expression. So check whether the result is folded before
1527 setting TREE_NEGATED_INT. */
1528 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1529 && TREE_CODE (result) == INTEGER_CST
1530 && !TREE_UNSIGNED (TREE_TYPE (result))
1531 && INT_CST_LT (result, integer_zero_node))
1532 TREE_NEGATED_INT (result) = 1;
1533 overflow_warning (result);
1534 return result;
1537 /* Finish an id-expression. */
1539 tree
1540 finish_id_expr (expr)
1541 tree expr;
1543 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1544 expr = do_identifier (expr, 1, NULL_TREE);
1546 if (TREE_TYPE (expr) == error_mark_node)
1547 expr = error_mark_node;
1548 return expr;
1551 static tree current_type_lookups;
1553 /* Perform deferred access control for types used in the type of a
1554 declaration. */
1556 static void
1557 deferred_type_access_control ()
1559 tree lookup = type_lookups;
1561 if (lookup == error_mark_node)
1562 return;
1564 for (; lookup; lookup = TREE_CHAIN (lookup))
1565 enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1568 void
1569 decl_type_access_control (decl)
1570 tree decl;
1572 tree save_fn;
1574 if (type_lookups == error_mark_node)
1575 return;
1577 save_fn = current_function_decl;
1579 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1580 current_function_decl = decl;
1582 deferred_type_access_control ();
1584 current_function_decl = save_fn;
1586 /* Now strip away the checks for the current declarator; they were
1587 added to type_lookups after typed_declspecs saved the copy that
1588 ended up in current_type_lookups. */
1589 type_lookups = current_type_lookups;
1591 current_type_lookups = NULL_TREE;
1594 /* Record the lookups, if we're doing deferred access control. */
1596 void
1597 save_type_access_control (lookups)
1598 tree lookups;
1600 if (type_lookups != error_mark_node)
1602 my_friendly_assert (!current_type_lookups, 20010301);
1603 current_type_lookups = lookups;
1605 else
1606 my_friendly_assert (!lookups || lookups == error_mark_node, 20010301);
1609 /* Set things up so that the next deferred access control will succeed.
1610 This is needed for friend declarations see grokdeclarator for details. */
1612 void
1613 skip_type_access_control ()
1615 type_lookups = NULL_TREE;
1618 /* Reset the deferred access control. */
1620 void
1621 reset_type_access_control ()
1623 type_lookups = NULL_TREE;
1624 current_type_lookups = NULL_TREE;
1627 /* Begin a function definition declared with DECL_SPECS and
1628 DECLARATOR. Returns non-zero if the function-declaration is
1629 legal. */
1632 begin_function_definition (decl_specs, declarator)
1633 tree decl_specs;
1634 tree declarator;
1636 tree specs;
1637 tree attrs;
1639 split_specs_attrs (decl_specs, &specs, &attrs);
1640 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1641 return 0;
1643 deferred_type_access_control ();
1644 type_lookups = error_mark_node;
1646 /* The things we're about to see are not directly qualified by any
1647 template headers we've seen thus far. */
1648 reset_specialization ();
1650 return 1;
1653 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1654 a SCOPE_REF. */
1656 tree
1657 begin_constructor_declarator (scope, name)
1658 tree scope;
1659 tree name;
1661 tree result = build_nt (SCOPE_REF, scope, name);
1662 enter_scope_of (result);
1663 return result;
1666 /* Finish an init-declarator. Returns a DECL. */
1668 tree
1669 finish_declarator (declarator, declspecs, attributes,
1670 prefix_attributes, initialized)
1671 tree declarator;
1672 tree declspecs;
1673 tree attributes;
1674 tree prefix_attributes;
1675 int initialized;
1677 return start_decl (declarator, declspecs, initialized, attributes,
1678 prefix_attributes);
1681 /* Finish a translation unit. */
1683 void
1684 finish_translation_unit ()
1686 /* In case there were missing closebraces,
1687 get us back to the global binding level. */
1688 pop_everything ();
1689 while (current_namespace != global_namespace)
1690 pop_namespace ();
1692 /* Do file scope __FUNCTION__ et al. */
1693 finish_fname_decls ();
1695 finish_file ();
1698 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1699 Returns the parameter. */
1701 tree
1702 finish_template_type_parm (aggr, identifier)
1703 tree aggr;
1704 tree identifier;
1706 if (aggr != class_type_node)
1708 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1709 aggr = class_type_node;
1712 return build_tree_list (aggr, identifier);
1715 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1716 Returns the parameter. */
1718 tree
1719 finish_template_template_parm (aggr, identifier)
1720 tree aggr;
1721 tree identifier;
1723 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1724 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1725 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1726 DECL_TEMPLATE_RESULT (tmpl) = decl;
1727 DECL_ARTIFICIAL (decl) = 1;
1728 end_template_decl ();
1730 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1732 return finish_template_type_parm (aggr, tmpl);
1735 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1736 non-zero, the parameter list was terminated by a `...'. */
1738 tree
1739 finish_parmlist (parms, ellipsis)
1740 tree parms;
1741 int ellipsis;
1743 if (parms)
1745 /* We mark the PARMS as a parmlist so that declarator processing can
1746 disambiguate certain constructs. */
1747 TREE_PARMLIST (parms) = 1;
1748 /* We do not append void_list_node here, but leave it to grokparms
1749 to do that. */
1750 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1752 return parms;
1755 /* Begin a class definition, as indicated by T. */
1757 tree
1758 begin_class_definition (t)
1759 tree t;
1761 /* Check the bases are accessible. */
1762 decl_type_access_control (TYPE_NAME (t));
1763 reset_type_access_control ();
1765 if (processing_template_parmlist)
1767 cp_error ("definition of `%#T' inside template parameter list", t);
1768 return error_mark_node;
1771 /* In a definition of a member class template, we will get here with
1772 an implicit typename. */
1773 if (IMPLICIT_TYPENAME_P (t))
1774 t = TREE_TYPE (t);
1775 /* A non-implicit typename comes from code like:
1777 template <typename T> struct A {
1778 template <typename U> struct A<T>::B ...
1780 This is erroneous. */
1781 else if (TREE_CODE (t) == TYPENAME_TYPE)
1783 cp_error ("invalid definition of qualified type `%T'", t);
1784 t = error_mark_node;
1787 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1789 t = make_aggr_type (RECORD_TYPE);
1790 pushtag (make_anon_name (), t, 0);
1793 /* If we generated a partial instantiation of this type, but now
1794 we're seeing a real definition, we're actually looking at a
1795 partial specialization. Consider:
1797 template <class T, class U>
1798 struct Y {};
1800 template <class T>
1801 struct X {};
1803 template <class T, class U>
1804 void f()
1806 typename X<Y<T, U> >::A a;
1809 template <class T, class U>
1810 struct X<Y<T, U> >
1814 We have to undo the effects of the previous partial
1815 instantiation. */
1816 if (PARTIAL_INSTANTIATION_P (t))
1818 if (!pedantic)
1820 /* Unfortunately, when we're not in pedantic mode, we
1821 attempt to actually fill in some of the fields of the
1822 partial instantiation, in order to support the implicit
1823 typename extension. Clear those fields now, in
1824 preparation for the definition here. The fields cleared
1825 here must match those set in instantiate_class_template.
1826 Look for a comment mentioning begin_class_definition
1827 there. */
1828 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1829 TYPE_FIELDS (t) = NULL_TREE;
1830 TYPE_METHODS (t) = NULL_TREE;
1831 CLASSTYPE_TAGS (t) = NULL_TREE;
1832 CLASSTYPE_VBASECLASSES (t) = NULL_TREE;
1833 TYPE_SIZE (t) = NULL_TREE;
1836 /* This isn't a partial instantiation any more. */
1837 PARTIAL_INSTANTIATION_P (t) = 0;
1839 /* If this type was already complete, and we see another definition,
1840 that's an error. */
1841 else if (COMPLETE_TYPE_P (t))
1842 duplicate_tag_error (t);
1844 /* Update the location of the decl. */
1845 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1846 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1848 if (TYPE_BEING_DEFINED (t))
1850 t = make_aggr_type (TREE_CODE (t));
1851 pushtag (TYPE_IDENTIFIER (t), t, 0);
1853 maybe_process_partial_specialization (t);
1854 pushclass (t, 1);
1855 TYPE_BEING_DEFINED (t) = 1;
1856 TYPE_PACKED (t) = flag_pack_struct;
1857 /* Reset the interface data, at the earliest possible
1858 moment, as it might have been set via a class foo;
1859 before. */
1860 if (! TYPE_ANONYMOUS_P (t))
1862 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1863 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1864 (t, interface_unknown);
1866 reset_specialization();
1868 /* Make a declaration for this class in its own scope. */
1869 build_self_reference ();
1871 return t;
1874 /* Finish the member declaration given by DECL. */
1876 void
1877 finish_member_declaration (decl)
1878 tree decl;
1880 if (decl == error_mark_node || decl == NULL_TREE)
1881 return;
1883 if (decl == void_type_node)
1884 /* The COMPONENT was a friend, not a member, and so there's
1885 nothing for us to do. */
1886 return;
1888 /* We should see only one DECL at a time. */
1889 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1891 /* Set up access control for DECL. */
1892 TREE_PRIVATE (decl)
1893 = (current_access_specifier == access_private_node);
1894 TREE_PROTECTED (decl)
1895 = (current_access_specifier == access_protected_node);
1896 if (TREE_CODE (decl) == TEMPLATE_DECL)
1898 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1899 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1902 /* Mark the DECL as a member of the current class. */
1903 DECL_CONTEXT (decl) = current_class_type;
1905 /* [dcl.link]
1907 A C language linkage is ignored for the names of class members
1908 and the member function type of class member functions. */
1909 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1910 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1912 /* Put functions on the TYPE_METHODS list and everything else on the
1913 TYPE_FIELDS list. Note that these are built up in reverse order.
1914 We reverse them (to obtain declaration order) in finish_struct. */
1915 if (TREE_CODE (decl) == FUNCTION_DECL
1916 || DECL_FUNCTION_TEMPLATE_P (decl))
1918 /* We also need to add this function to the
1919 CLASSTYPE_METHOD_VEC. */
1920 add_method (current_class_type, decl, /*error_p=*/0);
1922 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1923 TYPE_METHODS (current_class_type) = decl;
1925 else
1927 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1928 go at the beginning. The reason is that lookup_field_1
1929 searches the list in order, and we want a field name to
1930 override a type name so that the "struct stat hack" will
1931 work. In particular:
1933 struct S { enum E { }; int E } s;
1934 s.E = 3;
1936 is legal. In addition, the FIELD_DECLs must be maintained in
1937 declaration order so that class layout works as expected.
1938 However, we don't need that order until class layout, so we
1939 save a little time by putting FIELD_DECLs on in reverse order
1940 here, and then reversing them in finish_struct_1. (We could
1941 also keep a pointer to the correct insertion points in the
1942 list.) */
1944 if (TREE_CODE (decl) == TYPE_DECL)
1945 TYPE_FIELDS (current_class_type)
1946 = chainon (TYPE_FIELDS (current_class_type), decl);
1947 else
1949 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1950 TYPE_FIELDS (current_class_type) = decl;
1953 /* Enter the DECL into the scope of the class. */
1954 if (TREE_CODE (decl) != USING_DECL)
1955 pushdecl_class_level (decl);
1959 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1960 the definition is immediately followed by a semicolon. Returns the
1961 type. */
1963 tree
1964 finish_class_definition (t, attributes, semi, pop_scope_p)
1965 tree t;
1966 tree attributes;
1967 int semi;
1968 int pop_scope_p;
1970 /* finish_struct nukes this anyway; if finish_exception does too,
1971 then it can go. */
1972 if (semi)
1973 note_got_semicolon (t);
1975 /* If we got any attributes in class_head, xref_tag will stick them in
1976 TREE_TYPE of the type. Grab them now. */
1977 attributes = chainon (TREE_TYPE (t), attributes);
1978 TREE_TYPE (t) = NULL_TREE;
1980 if (TREE_CODE (t) == ENUMERAL_TYPE)
1982 else
1984 t = finish_struct (t, attributes);
1985 if (semi)
1986 note_got_semicolon (t);
1989 if (! semi)
1990 check_for_missing_semicolon (t);
1991 if (pop_scope_p)
1992 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1993 if (current_function_decl)
1994 type_lookups = error_mark_node;
1995 if (current_scope () == current_function_decl)
1996 do_pending_defargs ();
1998 return t;
2001 /* Finish processing the default argument expressions cached during
2002 the processing of a class definition. */
2004 void
2005 begin_inline_definitions ()
2007 if (current_scope () == current_function_decl)
2008 do_pending_inlines ();
2011 /* Finish processing the inline function definitions cached during the
2012 processing of a class definition. */
2014 void
2015 finish_inline_definitions ()
2017 if (current_class_type == NULL_TREE)
2018 clear_inline_text_obstack ();
2021 /* Finish processing the declaration of a member class template
2022 TYPES whose template parameters are given by PARMS. */
2024 tree
2025 finish_member_class_template (types)
2026 tree types;
2028 tree t;
2030 /* If there are declared, but undefined, partial specializations
2031 mixed in with the typespecs they will not yet have passed through
2032 maybe_process_partial_specialization, so we do that here. */
2033 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2034 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2035 maybe_process_partial_specialization (TREE_VALUE (t));
2037 note_list_got_semicolon (types);
2038 grok_x_components (types);
2039 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2040 /* The component was in fact a friend declaration. We avoid
2041 finish_member_template_decl performing certain checks by
2042 unsetting TYPES. */
2043 types = NULL_TREE;
2045 finish_member_template_decl (types);
2047 /* As with other component type declarations, we do
2048 not store the new DECL on the list of
2049 component_decls. */
2050 return NULL_TREE;
2053 /* Finish processsing a complete template declaration. The PARMS are
2054 the template parameters. */
2056 void
2057 finish_template_decl (parms)
2058 tree parms;
2060 if (parms)
2061 end_template_decl ();
2062 else
2063 end_specialization ();
2066 /* Finish processing a template-id (which names a type) of the form
2067 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2068 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2069 the scope of template-id indicated. */
2071 tree
2072 finish_template_type (name, args, entering_scope)
2073 tree name;
2074 tree args;
2075 int entering_scope;
2077 tree decl;
2079 decl = lookup_template_class (name, args,
2080 NULL_TREE, NULL_TREE,
2081 entering_scope, /*complain=*/1);
2082 if (decl != error_mark_node)
2083 decl = TYPE_STUB_DECL (decl);
2085 return decl;
2088 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2089 namespace scope or a class scope. */
2091 void
2092 enter_scope_of (sr)
2093 tree sr;
2095 tree scope = TREE_OPERAND (sr, 0);
2097 if (TREE_CODE (scope) == NAMESPACE_DECL)
2099 push_decl_namespace (scope);
2100 TREE_COMPLEXITY (sr) = -1;
2102 else if (scope != current_class_type)
2104 if (TREE_CODE (scope) == TYPENAME_TYPE)
2106 /* In a declarator for a template class member, the scope will
2107 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2108 scope = TREE_TYPE (scope);
2109 TREE_OPERAND (sr, 0) = scope;
2111 push_nested_class (scope, 3);
2112 TREE_COMPLEXITY (sr) = current_class_depth;
2116 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2117 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2118 BASE_CLASS, or NULL_TREE if an error occurred. The
2119 ACCESSS_SPECIFIER is one of
2120 access_{default,public,protected_private}[_virtual]_node.*/
2122 tree
2123 finish_base_specifier (access_specifier, base_class)
2124 tree access_specifier;
2125 tree base_class;
2127 tree result;
2129 if (! is_aggr_type (base_class, 1))
2130 result = NULL_TREE;
2131 else
2133 if (CP_TYPE_QUALS (base_class) != 0)
2135 cp_error ("base class `%T' has cv qualifiers", base_class);
2136 base_class = TYPE_MAIN_VARIANT (base_class);
2138 result = build_tree_list (access_specifier, base_class);
2141 return result;
2144 /* Called when multiple declarators are processed. If that is not
2145 premitted in this context, an error is issued. */
2147 void
2148 check_multiple_declarators ()
2150 /* [temp]
2152 In a template-declaration, explicit specialization, or explicit
2153 instantiation the init-declarator-list in the declaration shall
2154 contain at most one declarator.
2156 We don't just use PROCESSING_TEMPLATE_DECL for the first
2157 condition since that would disallow the perfectly legal code,
2158 like `template <class T> struct S { int i, j; };'. */
2159 tree scope = current_scope ();
2161 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2162 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2163 return;
2165 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2166 || processing_explicit_instantiation
2167 || processing_specialization)
2168 cp_error ("multiple declarators in template declaration");
2171 tree
2172 finish_typeof (expr)
2173 tree expr;
2175 if (processing_template_decl)
2177 tree t;
2179 t = make_aggr_type (TYPEOF_TYPE);
2180 TYPE_FIELDS (t) = expr;
2182 return t;
2185 if (TREE_CODE (expr) == OFFSET_REF)
2186 expr = resolve_offset_ref (expr);
2188 return TREE_TYPE (expr);
2191 /* Generate RTL for the statement T, and its substatements, and any
2192 other statements at its nesting level. */
2194 static void
2195 cp_expand_stmt (t)
2196 tree t;
2198 switch (TREE_CODE (t))
2200 case CLEANUP_STMT:
2201 if (CLEANUP_DECL (t)
2202 && CLEANUP_DECL (t) == current_function_return_value)
2203 /* Don't destroy the chosen named return value. */;
2204 else
2205 genrtl_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2206 break;
2208 case CTOR_STMT:
2209 genrtl_ctor_stmt (t);
2210 break;
2212 case TRY_BLOCK:
2213 genrtl_try_block (t);
2214 break;
2216 case EH_SPEC_BLOCK:
2217 genrtl_eh_spec_block (t);
2218 break;
2220 case HANDLER:
2221 genrtl_handler (t);
2222 break;
2224 case SUBOBJECT:
2225 genrtl_subobject (SUBOBJECT_CLEANUP (t));
2226 break;
2228 case RETURN_INIT:
2229 genrtl_named_return_value ();
2230 break;
2232 case USING_STMT:
2233 break;
2235 default:
2236 my_friendly_abort (19990810);
2237 break;
2241 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2242 will equivalent CALL_EXPRs. */
2244 static tree
2245 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2246 tree *tp;
2247 int *walk_subtrees ATTRIBUTE_UNUSED;
2248 void *data ATTRIBUTE_UNUSED;
2250 tree aggr_init_expr;
2251 tree call_expr;
2252 tree fn;
2253 tree args;
2254 tree slot;
2255 tree type;
2256 int copy_from_buffer_p;
2258 aggr_init_expr = *tp;
2259 /* We don't need to walk into types; there's nothing in a type that
2260 needs simplification. (And, furthermore, there are places we
2261 actively don't want to go. For example, we don't want to wander
2262 into the default arguments for a FUNCTION_DECL that appears in a
2263 CALL_EXPR.) */
2264 if (TYPE_P (aggr_init_expr))
2266 *walk_subtrees = 0;
2267 return NULL_TREE;
2269 /* Only AGGR_INIT_EXPRs are interesting. */
2270 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2271 return NULL_TREE;
2273 /* Form an appropriate CALL_EXPR. */
2274 fn = TREE_OPERAND (aggr_init_expr, 0);
2275 args = TREE_OPERAND (aggr_init_expr, 1);
2276 slot = TREE_OPERAND (aggr_init_expr, 2);
2277 type = TREE_TYPE (aggr_init_expr);
2278 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2280 /* Replace the first argument with the address of the third
2281 argument to the AGGR_INIT_EXPR. */
2282 mark_addressable (slot);
2283 args = tree_cons (NULL_TREE,
2284 build1 (ADDR_EXPR,
2285 build_pointer_type (TREE_TYPE (slot)),
2286 slot),
2287 TREE_CHAIN (args));
2289 call_expr = build (CALL_EXPR,
2290 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2291 fn, args, NULL_TREE);
2292 TREE_SIDE_EFFECTS (call_expr) = 1;
2294 /* If we're using the non-reentrant PCC calling convention, then we
2295 need to copy the returned value out of the static buffer into the
2296 SLOT. */
2297 copy_from_buffer_p = 0;
2298 #ifdef PCC_STATIC_STRUCT_RETURN
2299 if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2301 int old_ac = flag_access_control;
2303 flag_access_control = 0;
2304 call_expr = build_aggr_init (slot, call_expr, LOOKUP_ONLYCONVERTING);
2305 flag_access_control = old_ac;
2306 copy_from_buffer_p = 1;
2308 #endif
2310 /* If this AGGR_INIT_EXPR indicates the value returned by a
2311 function, then we want to use the value of the initialized
2312 location as the result. */
2313 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2315 call_expr = build (COMPOUND_EXPR, type,
2316 call_expr, slot);
2317 TREE_SIDE_EFFECTS (call_expr) = 1;
2320 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2321 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2322 *tp = call_expr;
2324 /* Keep iterating. */
2325 return NULL_TREE;
2328 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2330 static void
2331 emit_associated_thunks (fn)
2332 tree fn;
2334 /* When we use vcall offsets, we emit thunks with the virtual
2335 functions to which they thunk. The whole point of vcall offsets
2336 is so that you can know statically the entire set of thunks that
2337 will ever be needed for a given virtual function, thereby
2338 enabling you to output all the thunks with the function itself. */
2339 if (vcall_offsets_in_vtable_p () && DECL_VIRTUAL_P (fn))
2341 tree binfo;
2342 tree v;
2344 for (binfo = TYPE_BINFO (DECL_CONTEXT (fn));
2345 binfo;
2346 binfo = TREE_CHAIN (binfo))
2347 for (v = BINFO_VIRTUALS (binfo); v; v = TREE_CHAIN (v))
2348 if (BV_FN (v) == fn
2349 && (!integer_zerop (BV_DELTA (v))
2350 || BV_USE_VCALL_INDEX_P (v)))
2352 tree thunk;
2353 tree vcall_index;
2355 if (BV_USE_VCALL_INDEX_P (v))
2357 vcall_index = BV_VCALL_INDEX (v);
2358 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
2360 else
2361 vcall_index = NULL_TREE;
2363 thunk = make_thunk (build1 (ADDR_EXPR,
2364 vfunc_ptr_type_node,
2365 fn),
2366 BV_DELTA (v),
2367 vcall_index);
2368 use_thunk (thunk, /*emit_p=*/1);
2373 /* Generate RTL for FN. */
2375 void
2376 expand_body (fn)
2377 tree fn;
2379 int saved_lineno;
2380 const char *saved_input_filename;
2382 /* When the parser calls us after finishing the body of a template
2383 function, we don't really want to expand the body. When we're
2384 processing an in-class definition of an inline function,
2385 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2386 to look at the function itself. */
2387 if (processing_template_decl
2388 || (DECL_LANG_SPECIFIC (fn)
2389 && DECL_TEMPLATE_INFO (fn)
2390 && uses_template_parms (DECL_TI_ARGS (fn))))
2392 /* Normally, collection only occurs in rest_of_compilation. So,
2393 if we don't collect here, we never collect junk generated
2394 during the processing of templates until we hit a
2395 non-template function. */
2396 ggc_collect ();
2397 return;
2400 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2401 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2402 simplify_aggr_init_exprs_r,
2403 NULL);
2405 /* If this is a constructor or destructor body, we have to clone it
2406 under the new ABI. */
2407 if (maybe_clone_body (fn))
2409 /* We don't want to process FN again, so pretend we've written
2410 it out, even though we haven't. */
2411 TREE_ASM_WRITTEN (fn) = 1;
2412 return;
2415 /* There's no reason to do any of the work here if we're only doing
2416 semantic analysis; this code just generates RTL. */
2417 if (flag_syntax_only)
2418 return;
2420 /* If possible, avoid generating RTL for this function. Instead,
2421 just record it as an inline function, and wait until end-of-file
2422 to decide whether to write it out or not. */
2423 if (/* We have to generate RTL if it's not an inline function. */
2424 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2425 /* Or if we have to keep all inline functions anyhow. */
2426 && !flag_keep_inline_functions
2427 /* Or if we actually have a reference to the function. */
2428 && !DECL_NEEDED_P (fn)
2429 /* Or if this is a nested function. */
2430 && !decl_function_context (fn))
2432 /* Set DECL_EXTERNAL so that assemble_external will be called as
2433 necessary. We'll clear it again in finish_file. */
2434 if (!DECL_EXTERNAL (fn))
2436 DECL_NOT_REALLY_EXTERN (fn) = 1;
2437 DECL_EXTERNAL (fn) = 1;
2439 /* Remember this function. In finish_file we'll decide if
2440 we actually need to write this function out. */
2441 defer_fn (fn);
2442 /* Let the back-end know that this funtion exists. */
2443 note_deferral_of_defined_inline_function (fn);
2444 return;
2447 /* Compute the appropriate object-file linkage for inline
2448 functions. */
2449 if (DECL_DECLARED_INLINE_P (fn))
2450 import_export_decl (fn);
2452 /* Emit any thunks that should be emitted at the same time as FN. */
2453 emit_associated_thunks (fn);
2455 timevar_push (TV_INTEGRATION);
2457 /* Optimize the body of the function before expanding it. */
2458 optimize_function (fn);
2460 timevar_pop (TV_INTEGRATION);
2461 timevar_push (TV_EXPAND);
2463 /* Save the current file name and line number. When we expand the
2464 body of the function, we'll set LINENO and INPUT_FILENAME so that
2465 error-mesages come out in the right places. */
2466 saved_lineno = lineno;
2467 saved_input_filename = input_filename;
2468 lineno = DECL_SOURCE_LINE (fn);
2469 input_filename = DECL_SOURCE_FILE (fn);
2471 genrtl_start_function (fn);
2472 current_function_is_thunk = DECL_THUNK_P (fn);
2474 /* Expand the body. */
2475 expand_stmt (DECL_SAVED_TREE (fn));
2477 /* Statements should always be full-expressions at the outermost set
2478 of curly braces for a function. */
2479 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2481 /* The outermost statement for a function contains the line number
2482 recorded when we finished processing the function. */
2483 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2485 /* Generate code for the function. */
2486 genrtl_finish_function (fn);
2488 /* If possible, obliterate the body of the function so that it can
2489 be garbage collected. */
2490 if (dump_enabled_p (TDI_all))
2491 /* Keep the body; we're going to dump it. */
2493 else if (DECL_INLINE (fn) && flag_inline_trees)
2494 /* We might need the body of this function so that we can expand
2495 it inline somewhere else. */
2497 else
2498 /* We don't need the body; blow it away. */
2499 DECL_SAVED_TREE (fn) = NULL_TREE;
2501 /* And restore the current source position. */
2502 lineno = saved_lineno;
2503 input_filename = saved_input_filename;
2504 extract_interface_info ();
2506 timevar_pop (TV_EXPAND);
2509 /* Helper function for walk_tree, used by genrtl_start_function to override
2510 all the RETURN_STMTs for the named return value optimization. */
2512 static tree
2513 nullify_returns_r (tp, walk_subtrees, data)
2514 tree *tp;
2515 int *walk_subtrees;
2516 void *data ATTRIBUTE_UNUSED;
2518 /* No need to walk into types. */
2519 if (TYPE_P (*tp))
2520 *walk_subtrees = 0;
2521 else if (TREE_CODE (*tp) == RETURN_STMT)
2522 RETURN_NULLIFIED_P (*tp) = 1;
2524 /* Keep iterating. */
2525 return NULL_TREE;
2528 /* Start generating the RTL for FN. */
2530 static void
2531 genrtl_start_function (fn)
2532 tree fn;
2534 tree parm;
2536 /* Tell everybody what function we're processing. */
2537 current_function_decl = fn;
2538 /* Get the RTL machinery going for this function. */
2539 init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2540 /* Let everybody know that we're expanding this function, not doing
2541 semantic analysis. */
2542 expanding_p = 1;
2544 /* Even though we're inside a function body, we still don't want to
2545 call expand_expr to calculate the size of a variable-sized array.
2546 We haven't necessarily assigned RTL to all variables yet, so it's
2547 not safe to try to expand expressions involving them. */
2548 immediate_size_expand = 0;
2549 cfun->x_dont_save_pending_sizes_p = 1;
2551 /* Let the user know we're compiling this function. */
2552 announce_function (fn);
2554 /* Initialize the per-function data. */
2555 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2556 if (DECL_SAVED_FUNCTION_DATA (fn))
2558 /* If we already parsed this function, and we're just expanding it
2559 now, restore saved state. */
2560 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2562 /* This function is being processed in whole-function mode; we
2563 already did semantic analysis. */
2564 cfun->x_whole_function_mode_p = 1;
2566 /* If we decided that we didn't want to inline this function,
2567 make sure the back-end knows that. */
2568 if (!current_function_cannot_inline)
2569 current_function_cannot_inline = cp_function_chain->cannot_inline;
2571 /* We don't need the saved data anymore. */
2572 free (DECL_SAVED_FUNCTION_DATA (fn));
2573 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2576 /* Tell the cross-reference machinery that we're defining this
2577 function. */
2578 GNU_xref_function (fn, DECL_ARGUMENTS (fn));
2580 /* Keep track of how many functions we're presently expanding. */
2581 ++function_depth;
2583 /* Create a binding level for the parameters. */
2584 expand_start_bindings (2);
2585 /* Clear out any previously saved instructions for this function, in
2586 case it was defined more than once. */
2587 DECL_SAVED_INSNS (fn) = NULL;
2588 /* Go through the PARM_DECLs for this function to see if any need
2589 cleanups. */
2590 for (parm = DECL_ARGUMENTS (fn); parm; parm = TREE_CHAIN (parm))
2591 if (TREE_TYPE (parm) != error_mark_node
2592 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (parm)))
2594 expand_function_start (fn, /*parms_have_cleanups=*/1);
2595 break;
2597 if (!parm)
2598 expand_function_start (fn, /*parms_have_cleanups=*/0);
2599 /* If this function is `main'. */
2600 if (DECL_MAIN_P (fn))
2601 expand_main_function ();
2602 /* Create a binding contour which can be used to catch
2603 cleanup-generated temporaries. */
2604 expand_start_bindings (2);
2606 /* Set up the named return value optimization, if we can. */
2607 if (current_function_return_value
2608 && current_function_return_value != error_mark_node)
2610 tree r = current_function_return_value;
2611 /* This is only worth doing for fns that return in memory--and
2612 simpler, since we don't have to worry about promoted modes. */
2613 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (fn))))
2615 COPY_DECL_RTL (DECL_RESULT (fn), r);
2616 DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fn));
2617 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2618 nullify_returns_r, NULL_TREE);
2623 /* Finish generating the RTL for FN. */
2625 static void
2626 genrtl_finish_function (fn)
2627 tree fn;
2629 tree no_return_label = NULL_TREE;
2631 #if 0
2632 if (write_symbols != NO_DEBUG)
2634 /* Keep this code around in case we later want to control debug info
2635 based on whether a type is "used". (jason 1999-11-11) */
2637 tree ttype = target_type (fntype);
2638 tree parmdecl;
2640 if (IS_AGGR_TYPE (ttype))
2641 /* Let debugger know it should output info for this type. */
2642 note_debug_info_needed (ttype);
2644 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2646 ttype = target_type (TREE_TYPE (parmdecl));
2647 if (IS_AGGR_TYPE (ttype))
2648 /* Let debugger know it should output info for this type. */
2649 note_debug_info_needed (ttype);
2652 #endif
2654 /* Clean house because we will need to reorder insns here. */
2655 do_pending_stack_adjust ();
2657 if (!dtor_label && !DECL_CONSTRUCTOR_P (fn)
2658 && return_label != NULL_RTX
2659 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
2660 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2662 /* If this function is supposed to return a value, ensure that
2663 we do not fall into the cleanups by mistake. The end of our
2664 function will look like this:
2666 user code (may have return stmt somewhere)
2667 goto no_return_label
2668 cleanup_label:
2669 cleanups
2670 goto return_label
2671 no_return_label:
2672 NOTE_INSN_FUNCTION_END
2673 return_label:
2674 things for return
2676 If the user omits a return stmt in the USER CODE section, we
2677 will have a control path which reaches NOTE_INSN_FUNCTION_END.
2678 Otherwise, we won't. */
2679 if (no_return_label)
2681 DECL_CONTEXT (no_return_label) = fn;
2682 DECL_INITIAL (no_return_label) = error_mark_node;
2683 DECL_SOURCE_FILE (no_return_label) = input_filename;
2684 DECL_SOURCE_LINE (no_return_label) = lineno;
2685 expand_goto (no_return_label);
2688 if (cleanup_label)
2690 /* Remove the binding contour which is used to catch
2691 cleanup-generated temporaries. */
2692 expand_end_bindings (0, 0, 0);
2693 poplevel (0, 0, 0);
2695 /* Emit label at beginning of cleanup code for parameters. */
2696 emit_label (cleanup_label);
2699 /* Finish building code that will trigger warnings if users forget
2700 to make their functions return values. */
2701 if (return_label)
2702 emit_jump (return_label);
2703 if (no_return_label)
2705 /* We don't need to call `expand_*_return' here because we don't
2706 need any cleanups here--this path of code is only for error
2707 checking purposes. */
2708 expand_label (no_return_label);
2711 /* We hard-wired immediate_size_expand to zero in start_function.
2712 Expand_function_end will decrement this variable. So, we set the
2713 variable to one here, so that after the decrement it will remain
2714 zero. */
2715 immediate_size_expand = 1;
2717 /* Generate rtl for function exit. */
2718 expand_function_end (input_filename, lineno, 1);
2720 /* If this is a nested function (like a template instantiation that
2721 we're compiling in the midst of compiling something else), push a
2722 new GC context. That will keep local variables on the stack from
2723 being collected while we're doing the compilation of this
2724 function. */
2725 if (function_depth > 1)
2726 ggc_push_context ();
2728 /* There's no need to defer outputting this function any more; we
2729 know we want to output it. */
2730 DECL_DEFER_OUTPUT (fn) = 0;
2732 /* Run the optimizers and output the assembler code for this
2733 function. */
2734 rest_of_compilation (fn);
2736 /* Undo the call to ggc_push_context above. */
2737 if (function_depth > 1)
2738 ggc_pop_context ();
2740 if (DECL_SAVED_INSNS (fn) && ! TREE_ASM_WRITTEN (fn))
2742 /* Set DECL_EXTERNAL so that assemble_external will be called as
2743 necessary. We'll clear it again in finish_file. */
2744 if (! DECL_EXTERNAL (fn))
2745 DECL_NOT_REALLY_EXTERN (fn) = 1;
2746 DECL_EXTERNAL (fn) = 1;
2747 defer_fn (fn);
2750 #if 0
2751 /* Keep this code around in case we later want to control debug info
2752 based on whether a type is "used". (jason 1999-11-11) */
2754 if (ctype && TREE_ASM_WRITTEN (fn))
2755 note_debug_info_needed (ctype);
2756 #endif
2758 /* If this function is marked with the constructor attribute, add it
2759 to the list of functions to be called along with constructors
2760 from static duration objects. */
2761 if (DECL_STATIC_CONSTRUCTOR (fn))
2762 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2764 /* If this function is marked with the destructor attribute, add it
2765 to the list of functions to be called along with destructors from
2766 static duration objects. */
2767 if (DECL_STATIC_DESTRUCTOR (fn))
2768 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2770 --function_depth;
2772 /* If we don't need the RTL for this function anymore, stop pointing
2773 to it. That's especially important for LABEL_DECLs, since you
2774 can reach all the instructions in the function from the
2775 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. */
2776 if (!DECL_SAVED_INSNS (fn))
2778 tree t;
2780 /* Walk the BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and
2781 non-static local variables. */
2782 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2783 clear_decl_rtl,
2784 NULL);
2786 /* Clear out the RTL for the arguments. */
2787 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2789 SET_DECL_RTL (t, NULL_RTX);
2790 DECL_INCOMING_RTL (t) = NULL_RTX;
2793 if (!(flag_inline_trees && DECL_INLINE (fn)))
2794 /* DECL_INITIAL must remain nonzero so we know this was an
2795 actual function definition. */
2796 DECL_INITIAL (fn) = error_mark_node;
2799 /* Let the error reporting routines know that we're outside a
2800 function. For a nested function, this value is used in
2801 pop_cp_function_context and then reset via pop_function_context. */
2802 current_function_decl = NULL_TREE;
2805 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2806 its sub-blocks. */
2808 static tree
2809 clear_decl_rtl (tp, walk_subtrees, data)
2810 tree *tp;
2811 int *walk_subtrees ATTRIBUTE_UNUSED;
2812 void *data ATTRIBUTE_UNUSED;
2814 if (nonstatic_local_decl_p (*tp))
2815 SET_DECL_RTL (*tp, NULL_RTX);
2817 return NULL_TREE;
2820 /* Perform initialization related to this module. */
2822 void
2823 init_cp_semantics ()
2825 lang_expand_stmt = cp_expand_stmt;