* combine.c (simplify_and_const_int): Simplify (AND (PLUS X Y) C)
[official-gcc.git] / gcc / cp / semantics.c
blobb0bb25b94f4764c22772e8c5530f2f6981040674
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 "tree-inline.h"
32 #include "except.h"
33 #include "lex.h"
34 #include "toplev.h"
35 #include "flags.h"
36 #include "ggc.h"
37 #include "rtl.h"
38 #include "expr.h"
39 #include "output.h"
40 #include "timevar.h"
41 #include "debug.h"
43 /* There routines provide a modular interface to perform many parsing
44 operations. They may therefore be used during actual parsing, or
45 during template instantiation, which may be regarded as a
46 degenerate form of parsing. Since the current g++ parser is
47 lacking in several respects, and will be reimplemented, we are
48 attempting to move most code that is not directly related to
49 parsing into this file; that will make implementing the new parser
50 much easier since it will be able to make use of these routines. */
52 static tree maybe_convert_cond PARAMS ((tree));
53 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
54 static void deferred_type_access_control PARAMS ((void));
55 static void emit_associated_thunks PARAMS ((tree));
56 static void genrtl_try_block PARAMS ((tree));
57 static void genrtl_eh_spec_block PARAMS ((tree));
58 static void genrtl_handler PARAMS ((tree));
59 static void genrtl_ctor_stmt PARAMS ((tree));
60 static void genrtl_subobject PARAMS ((tree));
61 static void genrtl_named_return_value PARAMS ((void));
62 static void cp_expand_stmt PARAMS ((tree));
63 static void genrtl_start_function PARAMS ((tree));
64 static void genrtl_finish_function PARAMS ((tree));
65 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
67 /* Finish processing the COND, the SUBSTMT condition for STMT. */
69 #define FINISH_COND(cond, stmt, substmt) \
70 do { \
71 if (last_tree != stmt) \
72 { \
73 RECHAIN_STMTS (stmt, substmt); \
74 if (!processing_template_decl) \
75 { \
76 cond = build_tree_list (substmt, cond); \
77 substmt = cond; \
78 } \
79 } \
80 else \
81 substmt = cond; \
82 } while (0)
84 /* Returns non-zero if the current statement is a full expression,
85 i.e. temporaries created during that statement should be destroyed
86 at the end of the statement. */
88 int
89 stmts_are_full_exprs_p ()
91 return current_stmt_tree ()->stmts_are_full_exprs_p;
94 /* Returns the stmt_tree (if any) to which statements are currently
95 being added. If there is no active statement-tree, NULL is
96 returned. */
98 stmt_tree
99 current_stmt_tree ()
101 return (cfun
102 ? &cfun->language->x_stmt_tree
103 : &scope_chain->x_stmt_tree);
106 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
107 flag for this because "A union for which objects or pointers are
108 declared is not an anonymous union" [class.union]. */
111 anon_aggr_type_p (node)
112 tree node;
114 return (CLASS_TYPE_P (node) && TYPE_LANG_SPECIFIC(node)->anon_aggr);
117 /* Finish a scope. */
119 tree
120 do_poplevel ()
122 tree block = NULL_TREE;
124 if (stmts_are_full_exprs_p ())
126 tree scope_stmts = NULL_TREE;
128 if (!processing_template_decl)
129 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
131 block = poplevel (kept_level_p (), 1, 0);
132 if (block && !processing_template_decl)
134 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
135 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
139 return block;
142 /* Begin a new scope. */
144 void
145 do_pushlevel ()
147 if (stmts_are_full_exprs_p ())
149 pushlevel (0);
150 if (!processing_template_decl)
151 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
155 /* Finish a goto-statement. */
157 tree
158 finish_goto_stmt (destination)
159 tree destination;
161 if (TREE_CODE (destination) == IDENTIFIER_NODE)
162 destination = lookup_label (destination);
164 /* We warn about unused labels with -Wunused. That means we have to
165 mark the used labels as used. */
166 if (TREE_CODE (destination) == LABEL_DECL)
167 TREE_USED (destination) = 1;
169 if (TREE_CODE (destination) != LABEL_DECL)
170 /* We don't inline calls to functions with computed gotos.
171 Those functions are typically up to some funny business,
172 and may be depending on the labels being at particular
173 addresses, or some such. */
174 DECL_UNINLINABLE (current_function_decl) = 1;
176 check_goto (destination);
178 return add_stmt (build_stmt (GOTO_STMT, destination));
181 /* COND is the condition-expression for an if, while, etc.,
182 statement. Convert it to a boolean value, if appropriate. */
184 tree
185 maybe_convert_cond (cond)
186 tree cond;
188 /* Empty conditions remain empty. */
189 if (!cond)
190 return NULL_TREE;
192 /* Wait until we instantiate templates before doing conversion. */
193 if (processing_template_decl)
194 return cond;
196 /* Do the conversion. */
197 cond = convert_from_reference (cond);
198 return condition_conversion (cond);
201 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
203 tree
204 finish_expr_stmt (expr)
205 tree expr;
207 tree r = NULL_TREE;
209 if (expr != NULL_TREE)
211 if (!processing_template_decl
212 && !(stmts_are_full_exprs_p ())
213 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
214 && lvalue_p (expr))
215 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
216 expr = default_conversion (expr);
218 if (stmts_are_full_exprs_p ())
219 expr = convert_to_void (expr, "statement");
221 r = add_stmt (build_stmt (EXPR_STMT, expr));
224 finish_stmt ();
226 /* This was an expression-statement, so we save the type of the
227 expression. */
228 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
230 return r;
234 /* Begin an if-statement. Returns a newly created IF_STMT if
235 appropriate. */
237 tree
238 begin_if_stmt ()
240 tree r;
241 do_pushlevel ();
242 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
243 add_stmt (r);
244 return r;
247 /* Process the COND of an if-statement, which may be given by
248 IF_STMT. */
250 void
251 finish_if_stmt_cond (cond, if_stmt)
252 tree cond;
253 tree if_stmt;
255 cond = maybe_convert_cond (cond);
256 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
259 /* Finish the then-clause of an if-statement, which may be given by
260 IF_STMT. */
262 tree
263 finish_then_clause (if_stmt)
264 tree if_stmt;
266 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
267 last_tree = if_stmt;
268 return if_stmt;
271 /* Begin the else-clause of an if-statement. */
273 void
274 begin_else_clause ()
278 /* Finish the else-clause of an if-statement, which may be given by
279 IF_STMT. */
281 void
282 finish_else_clause (if_stmt)
283 tree if_stmt;
285 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
288 /* Finish an if-statement. */
290 void
291 finish_if_stmt ()
293 do_poplevel ();
294 finish_stmt ();
297 void
298 clear_out_block ()
300 /* If COND wasn't a declaration, clear out the
301 block we made for it and start a new one here so the
302 optimization in expand_end_loop will work. */
303 if (getdecls () == NULL_TREE)
305 do_poplevel ();
306 do_pushlevel ();
310 /* Begin a while-statement. Returns a newly created WHILE_STMT if
311 appropriate. */
313 tree
314 begin_while_stmt ()
316 tree r;
317 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
318 add_stmt (r);
319 do_pushlevel ();
320 return r;
323 /* Process the COND of a while-statement, which may be given by
324 WHILE_STMT. */
326 void
327 finish_while_stmt_cond (cond, while_stmt)
328 tree cond;
329 tree while_stmt;
331 cond = maybe_convert_cond (cond);
332 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
333 clear_out_block ();
336 /* Finish a while-statement, which may be given by WHILE_STMT. */
338 void
339 finish_while_stmt (while_stmt)
340 tree while_stmt;
342 do_poplevel ();
343 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
344 finish_stmt ();
347 /* Begin a do-statement. Returns a newly created DO_STMT if
348 appropriate. */
350 tree
351 begin_do_stmt ()
353 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
354 add_stmt (r);
355 return r;
358 /* Finish the body of a do-statement, which may be given by DO_STMT. */
360 void
361 finish_do_body (do_stmt)
362 tree do_stmt;
364 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
367 /* Finish a do-statement, which may be given by DO_STMT, and whose
368 COND is as indicated. */
370 void
371 finish_do_stmt (cond, do_stmt)
372 tree cond;
373 tree do_stmt;
375 cond = maybe_convert_cond (cond);
376 DO_COND (do_stmt) = cond;
377 finish_stmt ();
380 /* Finish a return-statement. The EXPRESSION returned, if any, is as
381 indicated. */
383 tree
384 finish_return_stmt (expr)
385 tree expr;
387 tree r;
389 if (!processing_template_decl)
390 expr = check_return_expr (expr);
391 if (!processing_template_decl)
393 if (DECL_CONSTRUCTOR_P (current_function_decl) && ctor_label)
395 /* Even returns without a value in a constructor must return
396 `this'. We accomplish this by sending all returns in a
397 constructor to the CTOR_LABEL; finish_function emits code to
398 return a value there. When we finally generate the real
399 return statement, CTOR_LABEL is no longer set, and we fall
400 through into the normal return-processing code below. */
401 return finish_goto_stmt (ctor_label);
403 else if (DECL_DESTRUCTOR_P (current_function_decl))
405 /* Similarly, all destructors must run destructors for
406 base-classes before returning. So, all returns in a
407 destructor get sent to the DTOR_LABEL; finish_function emits
408 code to return a value there. */
409 return finish_goto_stmt (dtor_label);
412 r = add_stmt (build_stmt (RETURN_STMT, expr));
413 finish_stmt ();
415 return r;
418 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
420 tree
421 begin_for_stmt ()
423 tree r;
425 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
426 NULL_TREE, NULL_TREE);
427 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
428 add_stmt (r);
429 if (NEW_FOR_SCOPE_P (r))
431 do_pushlevel ();
432 note_level_for_for ();
435 return r;
438 /* Finish the for-init-statement of a for-statement, which may be
439 given by FOR_STMT. */
441 void
442 finish_for_init_stmt (for_stmt)
443 tree for_stmt;
445 if (last_tree != for_stmt)
446 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
447 do_pushlevel ();
450 /* Finish the COND of a for-statement, which may be given by
451 FOR_STMT. */
453 void
454 finish_for_cond (cond, for_stmt)
455 tree cond;
456 tree for_stmt;
458 cond = maybe_convert_cond (cond);
459 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
460 clear_out_block ();
463 /* Finish the increment-EXPRESSION in a for-statement, which may be
464 given by FOR_STMT. */
466 void
467 finish_for_expr (expr, for_stmt)
468 tree expr;
469 tree for_stmt;
471 FOR_EXPR (for_stmt) = expr;
474 /* Finish the body of a for-statement, which may be given by
475 FOR_STMT. The increment-EXPR for the loop must be
476 provided. */
478 void
479 finish_for_stmt (for_stmt)
480 tree for_stmt;
482 /* Pop the scope for the body of the loop. */
483 do_poplevel ();
484 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
485 if (NEW_FOR_SCOPE_P (for_stmt))
486 do_poplevel ();
487 finish_stmt ();
490 /* Finish a break-statement. */
492 tree
493 finish_break_stmt ()
495 return add_stmt (build_break_stmt ());
498 /* Finish a continue-statement. */
500 tree
501 finish_continue_stmt ()
503 return add_stmt (build_continue_stmt ());
506 /* Begin a switch-statement. Returns a new SWITCH_STMT if
507 appropriate. */
509 tree
510 begin_switch_stmt ()
512 tree r;
513 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE);
514 add_stmt (r);
515 do_pushlevel ();
516 return r;
519 /* Finish the cond of a switch-statement. */
521 void
522 finish_switch_cond (cond, switch_stmt)
523 tree cond;
524 tree switch_stmt;
526 if (!processing_template_decl)
528 tree type;
529 tree index;
531 /* Convert the condition to an integer or enumeration type. */
532 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, 1);
533 if (cond == NULL_TREE)
535 error ("switch quantity not an integer");
536 cond = error_mark_node;
538 if (cond != error_mark_node)
540 cond = default_conversion (cond);
541 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
544 type = TREE_TYPE (cond);
545 index = get_unwidened (cond, NULL_TREE);
546 /* We can't strip a conversion from a signed type to an unsigned,
547 because if we did, int_fits_type_p would do the wrong thing
548 when checking case values for being in range,
549 and it's too hard to do the right thing. */
550 if (TREE_UNSIGNED (TREE_TYPE (cond))
551 == TREE_UNSIGNED (TREE_TYPE (index)))
552 cond = index;
554 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
555 push_switch (switch_stmt);
558 /* Finish the body of a switch-statement, which may be given by
559 SWITCH_STMT. The COND to switch on is indicated. */
561 void
562 finish_switch_stmt (switch_stmt)
563 tree switch_stmt;
565 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
566 pop_switch ();
567 do_poplevel ();
568 finish_stmt ();
571 /* Generate the RTL for T, which is a TRY_BLOCK. */
573 static void
574 genrtl_try_block (t)
575 tree t;
577 if (CLEANUP_P (t))
579 expand_eh_region_start ();
580 expand_stmt (TRY_STMTS (t));
581 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
583 else
585 if (!FN_TRY_BLOCK_P (t))
586 emit_line_note (input_filename, lineno);
588 expand_eh_region_start ();
589 expand_stmt (TRY_STMTS (t));
591 if (FN_TRY_BLOCK_P (t))
593 end_protect_partials ();
594 expand_start_all_catch ();
595 in_function_try_handler = 1;
596 expand_stmt (TRY_HANDLERS (t));
597 in_function_try_handler = 0;
598 expand_end_all_catch ();
600 else
602 expand_start_all_catch ();
603 expand_stmt (TRY_HANDLERS (t));
604 expand_end_all_catch ();
609 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
611 static void
612 genrtl_eh_spec_block (t)
613 tree t;
615 expand_eh_region_start ();
616 expand_stmt (EH_SPEC_STMTS (t));
617 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
618 build_call (call_unexpected_node,
619 tree_cons (NULL_TREE,
620 build_exc_ptr (),
621 NULL_TREE)));
624 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
625 appropriate. */
627 tree
628 begin_try_block ()
630 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
631 add_stmt (r);
632 return r;
635 /* Likewise, for a function-try-block. */
637 tree
638 begin_function_try_block ()
640 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
641 FN_TRY_BLOCK_P (r) = 1;
642 add_stmt (r);
643 return r;
646 /* Finish a try-block, which may be given by TRY_BLOCK. */
648 void
649 finish_try_block (try_block)
650 tree try_block;
652 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
655 /* Finish the body of a cleanup try-block, which may be given by
656 TRY_BLOCK. */
658 void
659 finish_cleanup_try_block (try_block)
660 tree try_block;
662 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
665 /* Finish an implicitly generated try-block, with a cleanup is given
666 by CLEANUP. */
668 void
669 finish_cleanup (cleanup, try_block)
670 tree cleanup;
671 tree try_block;
673 TRY_HANDLERS (try_block) = cleanup;
674 CLEANUP_P (try_block) = 1;
677 /* Likewise, for a function-try-block. */
679 void
680 finish_function_try_block (try_block)
681 tree try_block;
683 if (TREE_CHAIN (try_block)
684 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
686 /* Chain the compound statement after the CTOR_INITIALIZER. */
687 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
688 /* And make the CTOR_INITIALIZER the body of the try-block. */
689 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
691 else
692 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
693 in_function_try_handler = 1;
696 /* Finish a handler-sequence for a try-block, which may be given by
697 TRY_BLOCK. */
699 void
700 finish_handler_sequence (try_block)
701 tree try_block;
703 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
704 check_handlers (TRY_HANDLERS (try_block));
707 /* Likewise, for a function-try-block. */
709 void
710 finish_function_handler_sequence (try_block)
711 tree try_block;
713 in_function_try_handler = 0;
714 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
715 check_handlers (TRY_HANDLERS (try_block));
718 /* Generate the RTL for T, which is a HANDLER. */
720 static void
721 genrtl_handler (t)
722 tree t;
724 genrtl_do_pushlevel ();
725 if (!processing_template_decl)
726 expand_start_catch (HANDLER_TYPE (t));
727 expand_stmt (HANDLER_BODY (t));
728 if (!processing_template_decl)
729 expand_end_catch ();
732 /* Begin a handler. Returns a HANDLER if appropriate. */
734 tree
735 begin_handler ()
737 tree r;
738 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
739 add_stmt (r);
740 /* Create a binding level for the eh_info and the exception object
741 cleanup. */
742 do_pushlevel ();
743 note_level_for_catch ();
744 return r;
747 /* Finish the handler-parameters for a handler, which may be given by
748 HANDLER. DECL is the declaration for the catch parameter, or NULL
749 if this is a `catch (...)' clause. */
751 void
752 finish_handler_parms (decl, handler)
753 tree decl;
754 tree handler;
756 tree type = NULL_TREE;
757 if (processing_template_decl)
759 if (decl)
761 decl = pushdecl (decl);
762 decl = push_template_decl (decl);
763 add_decl_stmt (decl);
764 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
765 type = TREE_TYPE (decl);
768 else
769 type = expand_start_catch_block (decl);
771 HANDLER_TYPE (handler) = type;
774 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
775 the return value from the matching call to finish_handler_parms. */
777 void
778 finish_handler (handler)
779 tree handler;
781 if (!processing_template_decl)
782 expand_end_catch_block ();
783 do_poplevel ();
784 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
787 /* Generate the RTL for T, which is a CTOR_STMT. */
789 static void
790 genrtl_ctor_stmt (t)
791 tree t;
793 if (CTOR_BEGIN_P (t))
794 begin_protect_partials ();
795 else
796 /* After this point, any exceptions will cause the
797 destructor to be executed, so we no longer need to worry
798 about destroying the various subobjects ourselves. */
799 end_protect_partials ();
802 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
803 compound-statement does not define a scope. Returns a new
804 COMPOUND_STMT if appropriate. */
806 tree
807 begin_compound_stmt (has_no_scope)
808 int has_no_scope;
810 tree r;
811 int is_try = 0;
813 r = build_stmt (COMPOUND_STMT, NULL_TREE);
815 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
816 is_try = 1;
818 add_stmt (r);
819 if (has_no_scope)
820 COMPOUND_STMT_NO_SCOPE (r) = 1;
822 last_expr_type = NULL_TREE;
824 if (!has_no_scope)
826 do_pushlevel ();
827 if (is_try)
828 note_level_for_try ();
830 else
831 /* Normally, we try hard to keep the BLOCK for a
832 statement-expression. But, if it's a statement-expression with
833 a scopeless block, there's nothing to keep, and we don't want
834 to accidentally keep a block *inside* the scopeless block. */
835 keep_next_level (0);
837 return r;
840 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
841 If HAS_NO_SCOPE is non-zero, the compound statement does not define
842 a scope. */
844 tree
845 finish_compound_stmt (has_no_scope, compound_stmt)
846 int has_no_scope;
847 tree compound_stmt;
849 tree r;
850 tree t;
852 if (!has_no_scope)
853 r = do_poplevel ();
854 else
855 r = NULL_TREE;
857 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
859 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
860 the precise purpose of that variable is store the type of the
861 last expression statement within the last compound statement, we
862 preserve the value. */
863 t = last_expr_type;
864 finish_stmt ();
865 last_expr_type = t;
867 return r;
870 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
871 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
872 CLOBBERS. */
874 tree
875 finish_asm_stmt (cv_qualifier, string, output_operands,
876 input_operands, clobbers)
877 tree cv_qualifier;
878 tree string;
879 tree output_operands;
880 tree input_operands;
881 tree clobbers;
883 tree r;
884 tree t;
886 if (TREE_CHAIN (string))
887 string = combine_strings (string);
889 if (cv_qualifier != NULL_TREE
890 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
892 cp_warning ("%s qualifier ignored on asm",
893 IDENTIFIER_POINTER (cv_qualifier));
894 cv_qualifier = NULL_TREE;
897 if (!processing_template_decl)
899 int i;
900 int ninputs;
901 int noutputs;
903 for (t = input_operands; t; t = TREE_CHAIN (t))
905 tree converted_operand
906 = decay_conversion (TREE_VALUE (t));
908 /* If the type of the operand hasn't been determined (e.g.,
909 because it involves an overloaded function), then issue
910 an error message. There's no context available to
911 resolve the overloading. */
912 if (TREE_TYPE (converted_operand) == unknown_type_node)
914 cp_error ("type of asm operand `%E' could not be determined",
915 TREE_VALUE (t));
916 converted_operand = error_mark_node;
918 TREE_VALUE (t) = converted_operand;
921 ninputs = list_length (input_operands);
922 noutputs = list_length (output_operands);
924 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
926 bool allows_mem;
927 bool allows_reg;
928 bool is_inout;
929 const char *constraint;
930 tree operand;
932 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
933 operand = TREE_VALUE (output_operands);
935 if (!parse_output_constraint (&constraint,
936 i, ninputs, noutputs,
937 &allows_mem,
938 &allows_reg,
939 &is_inout))
941 /* By marking the type as erroneous, we will not try to
942 process this operand again in expand_asm_operands. */
943 TREE_TYPE (operand) = error_mark_node;
944 continue;
947 /* If the operand is a DECL that is going to end up in
948 memory, assume it is addressable. This is a bit more
949 conservative than it would ideally be; the exact test is
950 buried deep in expand_asm_operands and depends on the
951 DECL_RTL for the OPERAND -- which we don't have at this
952 point. */
953 if (!allows_reg && DECL_P (operand))
954 mark_addressable (operand);
958 r = build_stmt (ASM_STMT, cv_qualifier, string,
959 output_operands, input_operands,
960 clobbers);
961 return add_stmt (r);
964 /* Finish a label with the indicated NAME. */
966 void
967 finish_label_stmt (name)
968 tree name;
970 tree decl = define_label (input_filename, lineno, name);
971 add_stmt (build_stmt (LABEL_STMT, decl));
974 /* Finish a series of declarations for local labels. G++ allows users
975 to declare "local" labels, i.e., labels with scope. This extension
976 is useful when writing code involving statement-expressions. */
978 void
979 finish_label_decl (name)
980 tree name;
982 tree decl = declare_local_label (name);
983 add_decl_stmt (decl);
986 /* Generate the RTL for a SUBOBJECT. */
988 static void
989 genrtl_subobject (cleanup)
990 tree cleanup;
992 add_partial_entry (cleanup);
995 /* We're in a constructor, and have just constructed a a subobject of
996 *THIS. CLEANUP is code to run if an exception is thrown before the
997 end of the current function is reached. */
999 void
1000 finish_subobject (cleanup)
1001 tree cleanup;
1003 tree r = build_stmt (SUBOBJECT, cleanup);
1004 add_stmt (r);
1007 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1009 void
1010 finish_decl_cleanup (decl, cleanup)
1011 tree decl;
1012 tree cleanup;
1014 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1017 /* Generate the RTL for a RETURN_INIT. */
1019 static void
1020 genrtl_named_return_value ()
1022 tree decl = DECL_RESULT (current_function_decl);
1024 /* If this named return value comes in a register, put it in a
1025 pseudo-register. */
1026 if (DECL_REGISTER (decl))
1028 /* Note that the mode of the old DECL_RTL may be wider than the
1029 mode of DECL_RESULT, depending on the calling conventions for
1030 the processor. For example, on the Alpha, a 32-bit integer
1031 is returned in a DImode register -- the DECL_RESULT has
1032 SImode but the DECL_RTL for the DECL_RESULT has DImode. So,
1033 here, we use the mode the back-end has already assigned for
1034 the return value. */
1035 SET_DECL_RTL (decl, gen_reg_rtx (GET_MODE (DECL_RTL (decl))));
1036 if (TREE_ADDRESSABLE (decl))
1037 put_var_into_stack (decl);
1040 emit_local_var (decl);
1043 /* Bind a name and initialization to the return value of
1044 the current function. */
1046 void
1047 finish_named_return_value (return_id, init)
1048 tree return_id, init;
1050 tree decl = DECL_RESULT (current_function_decl);
1052 /* Give this error as many times as there are occurrences, so that
1053 users can use Emacs compilation buffers to find and fix all such
1054 places. */
1055 if (pedantic)
1056 pedwarn ("ISO C++ does not permit named return values");
1057 cp_deprecated ("the named return value extension");
1059 if (return_id != NULL_TREE)
1061 if (DECL_NAME (decl) == NULL_TREE)
1062 DECL_NAME (decl) = return_id;
1063 else
1065 cp_error ("return identifier `%D' already in place", return_id);
1066 return;
1070 /* Can't let this happen for constructors. */
1071 if (DECL_CONSTRUCTOR_P (current_function_decl))
1073 error ("can't redefine default return value for constructors");
1074 return;
1077 /* If we have a named return value, put that in our scope as well. */
1078 if (DECL_NAME (decl) != NULL_TREE)
1080 /* Let `cp_finish_decl' know that this initializer is ok. */
1081 DECL_INITIAL (decl) = init;
1082 if (doing_semantic_analysis_p ())
1083 pushdecl (decl);
1084 if (!processing_template_decl)
1086 cp_finish_decl (decl, init, NULL_TREE, 0);
1087 add_stmt (build_stmt (RETURN_INIT, NULL_TREE, NULL_TREE));
1089 else
1090 add_stmt (build_stmt (RETURN_INIT, return_id, init));
1093 /* Don't use tree-inlining for functions with named return values.
1094 That doesn't work properly because we don't do any translation of
1095 the RETURN_INITs when they are copied. */
1096 DECL_UNINLINABLE (current_function_decl) = 1;
1099 /* The INIT_LIST is a list of mem-initializers, in the order they were
1100 written by the user. The TREE_VALUE of each node is a list of
1101 initializers for a particular subobject. The TREE_PURPOSE is a
1102 FIELD_DECL is the initializer is for a non-static data member, and
1103 a class type if the initializer is for a base class. */
1105 void
1106 finish_mem_initializers (init_list)
1107 tree init_list;
1109 tree member_init_list;
1110 tree base_init_list;
1111 tree last_base_warned_about;
1112 tree next;
1113 tree init;
1115 member_init_list = NULL_TREE;
1116 base_init_list = NULL_TREE;
1117 last_base_warned_about = NULL_TREE;
1119 for (init = init_list; init; init = next)
1121 next = TREE_CHAIN (init);
1122 if (TREE_CODE (TREE_PURPOSE (init)) == FIELD_DECL)
1124 TREE_CHAIN (init) = member_init_list;
1125 member_init_list = init;
1127 /* We're running through the initializers from right to left
1128 as we process them here. So, if we see a data member
1129 initializer after we see a base initializer, that
1130 actually means that the base initializer preceeded the
1131 data member initializer. */
1132 if (warn_reorder && last_base_warned_about != base_init_list)
1134 tree base;
1136 for (base = base_init_list;
1137 base != last_base_warned_about;
1138 base = TREE_CHAIN (base))
1140 cp_warning ("base initializer for `%T'",
1141 TREE_PURPOSE (base));
1142 warning (" will be re-ordered to precede member initializations");
1145 last_base_warned_about = base_init_list;
1148 else
1150 TREE_CHAIN (init) = base_init_list;
1151 base_init_list = init;
1155 setup_vtbl_ptr (member_init_list, base_init_list);
1158 /* Cache the value of this class's main virtual function table pointer
1159 in a register variable. This will save one indirection if a
1160 more than one virtual function call is made this function. */
1162 void
1163 setup_vtbl_ptr (member_init_list, base_init_list)
1164 tree member_init_list;
1165 tree base_init_list;
1167 my_friendly_assert (doing_semantic_analysis_p (), 19990919);
1169 /* If we've already done this, there's no need to do it again. */
1170 if (vtbls_set_up_p)
1171 return;
1173 if (DECL_CONSTRUCTOR_P (current_function_decl))
1175 if (processing_template_decl)
1176 add_stmt (build_min_nt
1177 (CTOR_INITIALIZER,
1178 member_init_list, base_init_list));
1179 else
1181 tree ctor_stmt;
1183 /* Mark the beginning of the constructor. */
1184 ctor_stmt = build_stmt (CTOR_STMT);
1185 CTOR_BEGIN_P (ctor_stmt) = 1;
1186 add_stmt (ctor_stmt);
1188 /* And actually initialize the base-classes and members. */
1189 emit_base_init (member_init_list, base_init_list);
1192 else if (DECL_DESTRUCTOR_P (current_function_decl)
1193 && !processing_template_decl)
1195 tree if_stmt;
1196 tree compound_stmt;
1198 /* If the dtor is empty, and we know there is not any possible
1199 way we could use any vtable entries, before they are possibly
1200 set by a base class dtor, we don't have to setup the vtables,
1201 as we know that any base class dtor will set up any vtables
1202 it needs. We avoid MI, because one base class dtor can do a
1203 virtual dispatch to an overridden function that would need to
1204 have a non-related vtable set up, we cannot avoid setting up
1205 vtables in that case. We could change this to see if there
1206 is just one vtable. */
1207 if_stmt = begin_if_stmt ();
1209 /* If it is not safe to avoid setting up the vtables, then
1210 someone will change the condition to be boolean_true_node.
1211 (Actually, for now, we do not have code to set the condition
1212 appropriately, so we just assume that we always need to
1213 initialize the vtables.) */
1214 finish_if_stmt_cond (boolean_true_node, if_stmt);
1215 current_vcalls_possible_p = &IF_COND (if_stmt);
1217 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
1219 /* Make all virtual function table pointers in non-virtual base
1220 classes point to CURRENT_CLASS_TYPE's virtual function
1221 tables. */
1222 initialize_vtbl_ptrs (current_class_ptr);
1224 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
1225 finish_then_clause (if_stmt);
1226 finish_if_stmt ();
1229 /* Always keep the BLOCK node associated with the outermost pair of
1230 curly braces of a function. These are needed for correct
1231 operation of dwarfout.c. */
1232 keep_next_level (1);
1234 /* The virtual function tables are set up now. */
1235 vtbls_set_up_p = 1;
1238 /* Returns the stack of SCOPE_STMTs for the current function. */
1240 tree *
1241 current_scope_stmt_stack ()
1243 return &cfun->language->x_scope_stmt_stack;
1246 /* Finish a parenthesized expression EXPR. */
1248 tree
1249 finish_parenthesized_expr (expr)
1250 tree expr;
1252 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1253 /* This inhibits warnings in truthvalue_conversion. */
1254 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1256 if (TREE_CODE (expr) == OFFSET_REF)
1257 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1258 enclosed in parentheses. */
1259 PTRMEM_OK_P (expr) = 0;
1260 return expr;
1263 /* Begin a statement-expression. The value returned must be passed to
1264 finish_stmt_expr. */
1266 tree
1267 begin_stmt_expr ()
1269 /* If we're outside a function, we won't have a statement-tree to
1270 work with. But, if we see a statement-expression we need to
1271 create one. */
1272 if (! cfun && !last_tree)
1273 begin_stmt_tree (&scope_chain->x_saved_tree);
1275 keep_next_level (1);
1276 /* If we're building a statement tree, then the upcoming compound
1277 statement will be chained onto the tree structure, starting at
1278 last_tree. We return last_tree so that we can later unhook the
1279 compound statement. */
1280 return last_tree;
1283 /* Used when beginning a statement-expression outside function scope.
1284 For example, when handling a file-scope initializer, we use this
1285 function. */
1287 tree
1288 begin_global_stmt_expr ()
1290 if (! cfun && !last_tree)
1291 begin_stmt_tree (&scope_chain->x_saved_tree);
1293 keep_next_level (1);
1295 return (last_tree != NULL_TREE) ? last_tree : expand_start_stmt_expr();
1298 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1300 tree
1301 finish_global_stmt_expr (stmt_expr)
1302 tree stmt_expr;
1304 stmt_expr = expand_end_stmt_expr (stmt_expr);
1306 if (! cfun
1307 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1308 finish_stmt_tree (&scope_chain->x_saved_tree);
1310 return stmt_expr;
1313 /* Finish a statement-expression. RTL_EXPR should be the value
1314 returned by the previous begin_stmt_expr; EXPR is the
1315 statement-expression. Returns an expression representing the
1316 statement-expression. */
1318 tree
1319 finish_stmt_expr (rtl_expr)
1320 tree rtl_expr;
1322 tree result;
1324 /* If the last thing in the statement-expression was not an
1325 expression-statement, then it has type `void'. */
1326 if (!last_expr_type)
1327 last_expr_type = void_type_node;
1328 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1329 TREE_SIDE_EFFECTS (result) = 1;
1331 /* Remove the compound statement from the tree structure; it is
1332 now saved in the STMT_EXPR. */
1333 last_tree = rtl_expr;
1334 TREE_CHAIN (last_tree) = NULL_TREE;
1336 /* If we created a statement-tree for this statement-expression,
1337 remove it now. */
1338 if (! cfun
1339 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1340 finish_stmt_tree (&scope_chain->x_saved_tree);
1342 return result;
1345 /* Finish a call to FN with ARGS. Returns a representation of the
1346 call. */
1348 tree
1349 finish_call_expr (fn, args, koenig)
1350 tree fn;
1351 tree args;
1352 int koenig;
1354 tree result;
1356 if (koenig)
1358 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1359 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1360 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1361 fn = do_identifier (fn, 2, args);
1363 result = build_x_function_call (fn, args, current_class_ref);
1365 if (TREE_CODE (result) == CALL_EXPR
1366 && (! TREE_TYPE (result)
1367 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1368 result = require_complete_type (result);
1370 return result;
1373 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1374 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1375 POSTDECREMENT_EXPR.) */
1377 tree
1378 finish_increment_expr (expr, code)
1379 tree expr;
1380 enum tree_code code;
1382 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1383 a COMPONENT_REF). This way if we've got, say, a reference to a
1384 static member that's being operated on, we don't end up trying to
1385 find a member operator for the class it's in. */
1387 if (TREE_CODE (expr) == OFFSET_REF)
1388 expr = resolve_offset_ref (expr);
1389 return build_x_unary_op (code, expr);
1392 /* Finish a use of `this'. Returns an expression for `this'. */
1394 tree
1395 finish_this_expr ()
1397 tree result;
1399 if (current_class_ptr)
1401 result = current_class_ptr;
1403 else if (current_function_decl
1404 && DECL_STATIC_FUNCTION_P (current_function_decl))
1406 error ("`this' is unavailable for static member functions");
1407 result = error_mark_node;
1409 else
1411 if (current_function_decl)
1412 error ("invalid use of `this' in non-member function");
1413 else
1414 error ("invalid use of `this' at top level");
1415 result = error_mark_node;
1418 return result;
1421 /* Finish a member function call using OBJECT and ARGS as arguments to
1422 FN. Returns an expression for the call. */
1424 tree
1425 finish_object_call_expr (fn, object, args)
1426 tree fn;
1427 tree object;
1428 tree args;
1430 #if 0
1431 /* This is a future direction of this code, but because
1432 build_x_function_call cannot always undo what is done in
1433 build_component_ref entirely yet, we cannot do this. */
1435 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1436 return finish_call_expr (real_fn, args);
1437 #else
1438 if (DECL_DECLARES_TYPE_P (fn))
1440 if (processing_template_decl)
1441 /* This can happen on code like:
1443 class X;
1444 template <class T> void f(T t) {
1445 t.X();
1448 We just grab the underlying IDENTIFIER. */
1449 fn = DECL_NAME (fn);
1450 else
1452 cp_error ("calling type `%T' like a method", fn);
1453 return error_mark_node;
1457 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1458 #endif
1461 /* Finish a qualified member function call using OBJECT and ARGS as
1462 arguments to FN. Returns an expression for the call. */
1464 tree
1465 finish_qualified_object_call_expr (fn, object, args)
1466 tree fn;
1467 tree object;
1468 tree args;
1470 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1471 TREE_OPERAND (fn, 1), args);
1474 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1475 being the scope, if any, of DESTRUCTOR. Returns an expression for
1476 the call. */
1478 tree
1479 finish_pseudo_destructor_call_expr (object, scope, destructor)
1480 tree object;
1481 tree scope;
1482 tree destructor;
1484 if (processing_template_decl)
1485 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1487 if (scope && scope != destructor)
1488 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1489 scope, destructor);
1491 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1492 && (TREE_CODE (TREE_TYPE (object)) !=
1493 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1494 cp_error ("`%E' is not of type `%T'", object, destructor);
1496 return cp_convert (void_type_node, object);
1499 /* Finish a call to a globally qualified member function FN using
1500 ARGS. Returns an expression for the call. */
1502 tree
1503 finish_qualified_call_expr (fn, args)
1504 tree fn;
1505 tree args;
1507 if (processing_template_decl)
1508 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1509 else
1510 return build_member_call (TREE_OPERAND (fn, 0),
1511 TREE_OPERAND (fn, 1),
1512 args);
1515 /* Finish an expression of the form CODE EXPR. */
1517 tree
1518 finish_unary_op_expr (code, expr)
1519 enum tree_code code;
1520 tree expr;
1522 tree result = build_x_unary_op (code, expr);
1523 /* Inside a template, build_x_unary_op does not fold the
1524 expression. So check whether the result is folded before
1525 setting TREE_NEGATED_INT. */
1526 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1527 && TREE_CODE (result) == INTEGER_CST
1528 && !TREE_UNSIGNED (TREE_TYPE (result))
1529 && INT_CST_LT (result, integer_zero_node))
1530 TREE_NEGATED_INT (result) = 1;
1531 overflow_warning (result);
1532 return result;
1535 /* Finish an id-expression. */
1537 tree
1538 finish_id_expr (expr)
1539 tree expr;
1541 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1542 expr = do_identifier (expr, 1, NULL_TREE);
1544 if (TREE_TYPE (expr) == error_mark_node)
1545 expr = error_mark_node;
1546 return expr;
1549 static tree current_type_lookups;
1551 /* Perform deferred access control for types used in the type of a
1552 declaration. */
1554 static void
1555 deferred_type_access_control ()
1557 tree lookup = type_lookups;
1559 if (lookup == error_mark_node)
1560 return;
1562 for (; lookup; lookup = TREE_CHAIN (lookup))
1563 enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1566 void
1567 decl_type_access_control (decl)
1568 tree decl;
1570 tree save_fn;
1572 if (type_lookups == error_mark_node)
1573 return;
1575 save_fn = current_function_decl;
1577 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1578 current_function_decl = decl;
1580 deferred_type_access_control ();
1582 current_function_decl = save_fn;
1584 /* Now strip away the checks for the current declarator; they were
1585 added to type_lookups after typed_declspecs saved the copy that
1586 ended up in current_type_lookups. */
1587 type_lookups = current_type_lookups;
1589 current_type_lookups = NULL_TREE;
1592 /* Record the lookups, if we're doing deferred access control. */
1594 void
1595 save_type_access_control (lookups)
1596 tree lookups;
1598 if (type_lookups != error_mark_node)
1600 my_friendly_assert (!current_type_lookups, 20010301);
1601 current_type_lookups = lookups;
1603 else
1604 my_friendly_assert (!lookups || lookups == error_mark_node, 20010301);
1607 /* Set things up so that the next deferred access control will succeed.
1608 This is needed for friend declarations see grokdeclarator for details. */
1610 void
1611 skip_type_access_control ()
1613 type_lookups = NULL_TREE;
1616 /* Reset the deferred access control. */
1618 void
1619 reset_type_access_control ()
1621 type_lookups = NULL_TREE;
1622 current_type_lookups = NULL_TREE;
1625 /* Begin a function definition declared with DECL_SPECS and
1626 DECLARATOR. Returns non-zero if the function-declaration is
1627 legal. */
1630 begin_function_definition (decl_specs, declarator)
1631 tree decl_specs;
1632 tree declarator;
1634 tree specs;
1635 tree attrs;
1637 split_specs_attrs (decl_specs, &specs, &attrs);
1638 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1639 return 0;
1641 deferred_type_access_control ();
1642 type_lookups = error_mark_node;
1644 /* The things we're about to see are not directly qualified by any
1645 template headers we've seen thus far. */
1646 reset_specialization ();
1648 return 1;
1651 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1652 a SCOPE_REF. */
1654 tree
1655 begin_constructor_declarator (scope, name)
1656 tree scope;
1657 tree name;
1659 tree result = build_nt (SCOPE_REF, scope, name);
1660 enter_scope_of (result);
1661 return result;
1664 /* Finish an init-declarator. Returns a DECL. */
1666 tree
1667 finish_declarator (declarator, declspecs, attributes,
1668 prefix_attributes, initialized)
1669 tree declarator;
1670 tree declspecs;
1671 tree attributes;
1672 tree prefix_attributes;
1673 int initialized;
1675 return start_decl (declarator, declspecs, initialized, attributes,
1676 prefix_attributes);
1679 /* Finish a translation unit. */
1681 void
1682 finish_translation_unit ()
1684 /* In case there were missing closebraces,
1685 get us back to the global binding level. */
1686 pop_everything ();
1687 while (current_namespace != global_namespace)
1688 pop_namespace ();
1690 /* Do file scope __FUNCTION__ et al. */
1691 finish_fname_decls ();
1693 finish_file ();
1696 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1697 Returns the parameter. */
1699 tree
1700 finish_template_type_parm (aggr, identifier)
1701 tree aggr;
1702 tree identifier;
1704 if (aggr != class_type_node)
1706 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1707 aggr = class_type_node;
1710 return build_tree_list (aggr, identifier);
1713 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1714 Returns the parameter. */
1716 tree
1717 finish_template_template_parm (aggr, identifier)
1718 tree aggr;
1719 tree identifier;
1721 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1722 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1723 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1724 DECL_TEMPLATE_RESULT (tmpl) = decl;
1725 DECL_ARTIFICIAL (decl) = 1;
1726 end_template_decl ();
1728 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1730 return finish_template_type_parm (aggr, tmpl);
1733 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1734 non-zero, the parameter list was terminated by a `...'. */
1736 tree
1737 finish_parmlist (parms, ellipsis)
1738 tree parms;
1739 int ellipsis;
1741 if (parms)
1743 /* We mark the PARMS as a parmlist so that declarator processing can
1744 disambiguate certain constructs. */
1745 TREE_PARMLIST (parms) = 1;
1746 /* We do not append void_list_node here, but leave it to grokparms
1747 to do that. */
1748 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1750 return parms;
1753 /* Begin a class definition, as indicated by T. */
1755 tree
1756 begin_class_definition (t)
1757 tree t;
1759 /* Check the bases are accessible. */
1760 decl_type_access_control (TYPE_NAME (t));
1761 reset_type_access_control ();
1763 if (processing_template_parmlist)
1765 cp_error ("definition of `%#T' inside template parameter list", t);
1766 return error_mark_node;
1769 /* In a definition of a member class template, we will get here with
1770 an implicit typename. */
1771 if (IMPLICIT_TYPENAME_P (t))
1772 t = TREE_TYPE (t);
1773 /* A non-implicit typename comes from code like:
1775 template <typename T> struct A {
1776 template <typename U> struct A<T>::B ...
1778 This is erroneous. */
1779 else if (TREE_CODE (t) == TYPENAME_TYPE)
1781 cp_error ("invalid definition of qualified type `%T'", t);
1782 t = error_mark_node;
1785 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1787 t = make_aggr_type (RECORD_TYPE);
1788 pushtag (make_anon_name (), t, 0);
1791 /* If we generated a partial instantiation of this type, but now
1792 we're seeing a real definition, we're actually looking at a
1793 partial specialization. Consider:
1795 template <class T, class U>
1796 struct Y {};
1798 template <class T>
1799 struct X {};
1801 template <class T, class U>
1802 void f()
1804 typename X<Y<T, U> >::A a;
1807 template <class T, class U>
1808 struct X<Y<T, U> >
1812 We have to undo the effects of the previous partial
1813 instantiation. */
1814 if (PARTIAL_INSTANTIATION_P (t))
1816 if (!pedantic)
1818 /* Unfortunately, when we're not in pedantic mode, we
1819 attempt to actually fill in some of the fields of the
1820 partial instantiation, in order to support the implicit
1821 typename extension. Clear those fields now, in
1822 preparation for the definition here. The fields cleared
1823 here must match those set in instantiate_class_template.
1824 Look for a comment mentioning begin_class_definition
1825 there. */
1826 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1827 TYPE_FIELDS (t) = NULL_TREE;
1828 TYPE_METHODS (t) = NULL_TREE;
1829 CLASSTYPE_TAGS (t) = NULL_TREE;
1830 CLASSTYPE_VBASECLASSES (t) = NULL_TREE;
1831 TYPE_SIZE (t) = NULL_TREE;
1834 /* This isn't a partial instantiation any more. */
1835 PARTIAL_INSTANTIATION_P (t) = 0;
1837 /* If this type was already complete, and we see another definition,
1838 that's an error. */
1839 else if (COMPLETE_TYPE_P (t))
1840 duplicate_tag_error (t);
1842 /* Update the location of the decl. */
1843 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1844 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1846 if (TYPE_BEING_DEFINED (t))
1848 t = make_aggr_type (TREE_CODE (t));
1849 pushtag (TYPE_IDENTIFIER (t), t, 0);
1851 maybe_process_partial_specialization (t);
1852 pushclass (t, 1);
1853 TYPE_BEING_DEFINED (t) = 1;
1854 TYPE_PACKED (t) = flag_pack_struct;
1855 /* Reset the interface data, at the earliest possible
1856 moment, as it might have been set via a class foo;
1857 before. */
1858 if (! TYPE_ANONYMOUS_P (t))
1860 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1861 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1862 (t, interface_unknown);
1864 reset_specialization();
1866 /* Make a declaration for this class in its own scope. */
1867 build_self_reference ();
1869 return t;
1872 /* Finish the member declaration given by DECL. */
1874 void
1875 finish_member_declaration (decl)
1876 tree decl;
1878 if (decl == error_mark_node || decl == NULL_TREE)
1879 return;
1881 if (decl == void_type_node)
1882 /* The COMPONENT was a friend, not a member, and so there's
1883 nothing for us to do. */
1884 return;
1886 /* We should see only one DECL at a time. */
1887 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1889 /* Set up access control for DECL. */
1890 TREE_PRIVATE (decl)
1891 = (current_access_specifier == access_private_node);
1892 TREE_PROTECTED (decl)
1893 = (current_access_specifier == access_protected_node);
1894 if (TREE_CODE (decl) == TEMPLATE_DECL)
1896 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1897 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1900 /* Mark the DECL as a member of the current class. */
1901 DECL_CONTEXT (decl) = current_class_type;
1903 /* [dcl.link]
1905 A C language linkage is ignored for the names of class members
1906 and the member function type of class member functions. */
1907 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1908 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1910 /* Put functions on the TYPE_METHODS list and everything else on the
1911 TYPE_FIELDS list. Note that these are built up in reverse order.
1912 We reverse them (to obtain declaration order) in finish_struct. */
1913 if (TREE_CODE (decl) == FUNCTION_DECL
1914 || DECL_FUNCTION_TEMPLATE_P (decl))
1916 /* We also need to add this function to the
1917 CLASSTYPE_METHOD_VEC. */
1918 add_method (current_class_type, decl, /*error_p=*/0);
1920 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1921 TYPE_METHODS (current_class_type) = decl;
1923 else
1925 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1926 go at the beginning. The reason is that lookup_field_1
1927 searches the list in order, and we want a field name to
1928 override a type name so that the "struct stat hack" will
1929 work. In particular:
1931 struct S { enum E { }; int E } s;
1932 s.E = 3;
1934 is legal. In addition, the FIELD_DECLs must be maintained in
1935 declaration order so that class layout works as expected.
1936 However, we don't need that order until class layout, so we
1937 save a little time by putting FIELD_DECLs on in reverse order
1938 here, and then reversing them in finish_struct_1. (We could
1939 also keep a pointer to the correct insertion points in the
1940 list.) */
1942 if (TREE_CODE (decl) == TYPE_DECL)
1943 TYPE_FIELDS (current_class_type)
1944 = chainon (TYPE_FIELDS (current_class_type), decl);
1945 else
1947 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1948 TYPE_FIELDS (current_class_type) = decl;
1951 /* Enter the DECL into the scope of the class. */
1952 if (TREE_CODE (decl) != USING_DECL)
1953 pushdecl_class_level (decl);
1957 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1958 the definition is immediately followed by a semicolon. Returns the
1959 type. */
1961 tree
1962 finish_class_definition (t, attributes, semi, pop_scope_p)
1963 tree t;
1964 tree attributes;
1965 int semi;
1966 int pop_scope_p;
1968 /* finish_struct nukes this anyway; if finish_exception does too,
1969 then it can go. */
1970 if (semi)
1971 note_got_semicolon (t);
1973 /* If we got any attributes in class_head, xref_tag will stick them in
1974 TREE_TYPE of the type. Grab them now. */
1975 attributes = chainon (TREE_TYPE (t), attributes);
1976 TREE_TYPE (t) = NULL_TREE;
1978 if (TREE_CODE (t) == ENUMERAL_TYPE)
1980 else
1982 t = finish_struct (t, attributes);
1983 if (semi)
1984 note_got_semicolon (t);
1987 if (! semi)
1988 check_for_missing_semicolon (t);
1989 if (pop_scope_p)
1990 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1991 if (current_function_decl)
1992 type_lookups = error_mark_node;
1993 if (current_scope () == current_function_decl)
1994 do_pending_defargs ();
1996 return t;
1999 /* Finish processing the default argument expressions cached during
2000 the processing of a class definition. */
2002 void
2003 begin_inline_definitions ()
2005 if (current_scope () == current_function_decl)
2006 do_pending_inlines ();
2009 /* Finish processing the inline function definitions cached during the
2010 processing of a class definition. */
2012 void
2013 finish_inline_definitions ()
2015 if (current_class_type == NULL_TREE)
2016 clear_inline_text_obstack ();
2019 /* Finish processing the declaration of a member class template
2020 TYPES whose template parameters are given by PARMS. */
2022 tree
2023 finish_member_class_template (types)
2024 tree types;
2026 tree t;
2028 /* If there are declared, but undefined, partial specializations
2029 mixed in with the typespecs they will not yet have passed through
2030 maybe_process_partial_specialization, so we do that here. */
2031 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2032 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2033 maybe_process_partial_specialization (TREE_VALUE (t));
2035 note_list_got_semicolon (types);
2036 grok_x_components (types);
2037 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2038 /* The component was in fact a friend declaration. We avoid
2039 finish_member_template_decl performing certain checks by
2040 unsetting TYPES. */
2041 types = NULL_TREE;
2043 finish_member_template_decl (types);
2045 /* As with other component type declarations, we do
2046 not store the new DECL on the list of
2047 component_decls. */
2048 return NULL_TREE;
2051 /* Finish processing a complete template declaration. The PARMS are
2052 the template parameters. */
2054 void
2055 finish_template_decl (parms)
2056 tree parms;
2058 if (parms)
2059 end_template_decl ();
2060 else
2061 end_specialization ();
2064 /* Finish processing a template-id (which names a type) of the form
2065 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2066 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2067 the scope of template-id indicated. */
2069 tree
2070 finish_template_type (name, args, entering_scope)
2071 tree name;
2072 tree args;
2073 int entering_scope;
2075 tree decl;
2077 decl = lookup_template_class (name, args,
2078 NULL_TREE, NULL_TREE,
2079 entering_scope, /*complain=*/1);
2080 if (decl != error_mark_node)
2081 decl = TYPE_STUB_DECL (decl);
2083 return decl;
2086 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2087 namespace scope or a class scope. */
2089 void
2090 enter_scope_of (sr)
2091 tree sr;
2093 tree scope = TREE_OPERAND (sr, 0);
2095 if (TREE_CODE (scope) == NAMESPACE_DECL)
2097 push_decl_namespace (scope);
2098 TREE_COMPLEXITY (sr) = -1;
2100 else if (scope != current_class_type)
2102 if (TREE_CODE (scope) == TYPENAME_TYPE)
2104 /* In a declarator for a template class member, the scope will
2105 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2106 scope = TREE_TYPE (scope);
2107 TREE_OPERAND (sr, 0) = scope;
2109 push_nested_class (scope, 3);
2110 TREE_COMPLEXITY (sr) = current_class_depth;
2114 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2115 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2116 BASE_CLASS, or NULL_TREE if an error occurred. The
2117 ACCESSS_SPECIFIER is one of
2118 access_{default,public,protected_private}[_virtual]_node.*/
2120 tree
2121 finish_base_specifier (access_specifier, base_class)
2122 tree access_specifier;
2123 tree base_class;
2125 tree result;
2127 if (! is_aggr_type (base_class, 1))
2128 result = NULL_TREE;
2129 else
2131 if (cp_type_quals (base_class) != 0)
2133 cp_error ("base class `%T' has cv qualifiers", base_class);
2134 base_class = TYPE_MAIN_VARIANT (base_class);
2136 result = build_tree_list (access_specifier, base_class);
2139 return result;
2142 /* Called when multiple declarators are processed. If that is not
2143 premitted in this context, an error is issued. */
2145 void
2146 check_multiple_declarators ()
2148 /* [temp]
2150 In a template-declaration, explicit specialization, or explicit
2151 instantiation the init-declarator-list in the declaration shall
2152 contain at most one declarator.
2154 We don't just use PROCESSING_TEMPLATE_DECL for the first
2155 condition since that would disallow the perfectly legal code,
2156 like `template <class T> struct S { int i, j; };'. */
2157 tree scope = current_scope ();
2159 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2160 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2161 return;
2163 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2164 || processing_explicit_instantiation
2165 || processing_specialization)
2166 cp_error ("multiple declarators in template declaration");
2169 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2170 use as a type-specifier. */
2172 tree
2173 finish_typeof (expr)
2174 tree expr;
2176 if (processing_template_decl)
2178 tree t;
2180 t = make_aggr_type (TYPEOF_TYPE);
2181 TYPE_FIELDS (t) = expr;
2183 return t;
2186 if (TREE_CODE (expr) == OFFSET_REF)
2187 expr = resolve_offset_ref (expr);
2189 return TREE_TYPE (expr);
2192 /* Compute the value of the `sizeof' operator. */
2194 tree
2195 finish_sizeof (t)
2196 tree t;
2198 if (processing_template_decl)
2199 return build_min_nt (SIZEOF_EXPR, t);
2201 return TYPE_P (t) ? c_sizeof (t) : expr_sizeof (t);
2204 /* Implement the __alignof keyword: Return the minimum required
2205 alignment of T, measured in bytes. */
2207 tree
2208 finish_alignof (t)
2209 tree t;
2211 if (processing_template_decl)
2212 return build_min_nt (ALIGNOF_EXPR, t);
2214 return TYPE_P (t) ? c_alignof (t) : c_alignof_expr (t);
2217 /* Generate RTL for the statement T, and its substatements, and any
2218 other statements at its nesting level. */
2220 static void
2221 cp_expand_stmt (t)
2222 tree t;
2224 switch (TREE_CODE (t))
2226 case CLEANUP_STMT:
2227 genrtl_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2228 break;
2230 case CTOR_STMT:
2231 genrtl_ctor_stmt (t);
2232 break;
2234 case TRY_BLOCK:
2235 genrtl_try_block (t);
2236 break;
2238 case EH_SPEC_BLOCK:
2239 genrtl_eh_spec_block (t);
2240 break;
2242 case HANDLER:
2243 genrtl_handler (t);
2244 break;
2246 case SUBOBJECT:
2247 genrtl_subobject (SUBOBJECT_CLEANUP (t));
2248 break;
2250 case RETURN_INIT:
2251 genrtl_named_return_value ();
2252 break;
2254 case USING_STMT:
2255 break;
2257 default:
2258 my_friendly_abort (19990810);
2259 break;
2263 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2264 will equivalent CALL_EXPRs. */
2266 static tree
2267 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2268 tree *tp;
2269 int *walk_subtrees ATTRIBUTE_UNUSED;
2270 void *data ATTRIBUTE_UNUSED;
2272 tree aggr_init_expr;
2273 tree call_expr;
2274 tree fn;
2275 tree args;
2276 tree slot;
2277 tree type;
2278 int copy_from_buffer_p;
2280 aggr_init_expr = *tp;
2281 /* We don't need to walk into types; there's nothing in a type that
2282 needs simplification. (And, furthermore, there are places we
2283 actively don't want to go. For example, we don't want to wander
2284 into the default arguments for a FUNCTION_DECL that appears in a
2285 CALL_EXPR.) */
2286 if (TYPE_P (aggr_init_expr))
2288 *walk_subtrees = 0;
2289 return NULL_TREE;
2291 /* Only AGGR_INIT_EXPRs are interesting. */
2292 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2293 return NULL_TREE;
2295 /* Form an appropriate CALL_EXPR. */
2296 fn = TREE_OPERAND (aggr_init_expr, 0);
2297 args = TREE_OPERAND (aggr_init_expr, 1);
2298 slot = TREE_OPERAND (aggr_init_expr, 2);
2299 type = TREE_TYPE (aggr_init_expr);
2300 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2302 /* Replace the first argument with the address of the third
2303 argument to the AGGR_INIT_EXPR. */
2304 mark_addressable (slot);
2305 args = tree_cons (NULL_TREE,
2306 build1 (ADDR_EXPR,
2307 build_pointer_type (TREE_TYPE (slot)),
2308 slot),
2309 TREE_CHAIN (args));
2311 call_expr = build (CALL_EXPR,
2312 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2313 fn, args, NULL_TREE);
2314 TREE_SIDE_EFFECTS (call_expr) = 1;
2316 /* If we're using the non-reentrant PCC calling convention, then we
2317 need to copy the returned value out of the static buffer into the
2318 SLOT. */
2319 copy_from_buffer_p = 0;
2320 #ifdef PCC_STATIC_STRUCT_RETURN
2321 if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2323 int old_ac = flag_access_control;
2325 flag_access_control = 0;
2326 call_expr = build_aggr_init (slot, call_expr,
2327 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2328 flag_access_control = old_ac;
2329 copy_from_buffer_p = 1;
2331 #endif
2333 /* If this AGGR_INIT_EXPR indicates the value returned by a
2334 function, then we want to use the value of the initialized
2335 location as the result. */
2336 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2338 call_expr = build (COMPOUND_EXPR, type,
2339 call_expr, slot);
2340 TREE_SIDE_EFFECTS (call_expr) = 1;
2343 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2344 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2345 *tp = call_expr;
2347 /* Keep iterating. */
2348 return NULL_TREE;
2351 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2353 static void
2354 emit_associated_thunks (fn)
2355 tree fn;
2357 /* When we use vcall offsets, we emit thunks with the virtual
2358 functions to which they thunk. The whole point of vcall offsets
2359 is so that you can know statically the entire set of thunks that
2360 will ever be needed for a given virtual function, thereby
2361 enabling you to output all the thunks with the function itself. */
2362 if (DECL_VIRTUAL_P (fn))
2364 tree binfo;
2365 tree v;
2367 for (binfo = TYPE_BINFO (DECL_CONTEXT (fn));
2368 binfo;
2369 binfo = TREE_CHAIN (binfo))
2370 for (v = BINFO_VIRTUALS (binfo); v; v = TREE_CHAIN (v))
2371 if (BV_FN (v) == fn
2372 && (!integer_zerop (BV_DELTA (v))
2373 || BV_USE_VCALL_INDEX_P (v)))
2375 tree thunk;
2376 tree vcall_index;
2378 if (BV_USE_VCALL_INDEX_P (v))
2380 vcall_index = BV_VCALL_INDEX (v);
2381 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
2383 else
2384 vcall_index = NULL_TREE;
2386 thunk = make_thunk (build1 (ADDR_EXPR,
2387 vfunc_ptr_type_node,
2388 fn),
2389 BV_DELTA (v),
2390 vcall_index);
2391 use_thunk (thunk, /*emit_p=*/1);
2396 /* Generate RTL for FN. */
2398 void
2399 expand_body (fn)
2400 tree fn;
2402 int saved_lineno;
2403 const char *saved_input_filename;
2405 /* When the parser calls us after finishing the body of a template
2406 function, we don't really want to expand the body. When we're
2407 processing an in-class definition of an inline function,
2408 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2409 to look at the function itself. */
2410 if (processing_template_decl
2411 || (DECL_LANG_SPECIFIC (fn)
2412 && DECL_TEMPLATE_INFO (fn)
2413 && uses_template_parms (DECL_TI_ARGS (fn))))
2415 /* Normally, collection only occurs in rest_of_compilation. So,
2416 if we don't collect here, we never collect junk generated
2417 during the processing of templates until we hit a
2418 non-template function. */
2419 ggc_collect ();
2420 return;
2423 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2424 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2425 simplify_aggr_init_exprs_r,
2426 NULL);
2428 /* If this is a constructor or destructor body, we have to clone
2429 it. */
2430 if (maybe_clone_body (fn))
2432 /* We don't want to process FN again, so pretend we've written
2433 it out, even though we haven't. */
2434 TREE_ASM_WRITTEN (fn) = 1;
2435 return;
2438 /* There's no reason to do any of the work here if we're only doing
2439 semantic analysis; this code just generates RTL. */
2440 if (flag_syntax_only)
2441 return;
2443 /* If possible, avoid generating RTL for this function. Instead,
2444 just record it as an inline function, and wait until end-of-file
2445 to decide whether to write it out or not. */
2446 if (/* We have to generate RTL if it's not an inline function. */
2447 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2448 /* Or if we have to emit code for inline functions anyhow. */
2449 && !flag_keep_inline_functions
2450 /* Or if we actually have a reference to the function. */
2451 && !DECL_NEEDED_P (fn))
2453 /* Set DECL_EXTERNAL so that assemble_external will be called as
2454 necessary. We'll clear it again in finish_file. */
2455 if (!DECL_EXTERNAL (fn))
2457 DECL_NOT_REALLY_EXTERN (fn) = 1;
2458 DECL_EXTERNAL (fn) = 1;
2460 /* Remember this function. In finish_file we'll decide if
2461 we actually need to write this function out. */
2462 defer_fn (fn);
2463 /* Let the back-end know that this funtion exists. */
2464 (*debug_hooks->deferred_inline_function) (fn);
2465 return;
2468 /* Compute the appropriate object-file linkage for inline
2469 functions. */
2470 if (DECL_DECLARED_INLINE_P (fn))
2471 import_export_decl (fn);
2473 /* If FN is external, then there's no point in generating RTL for
2474 it. This situation can arise with an inline function under
2475 `-fexternal-templates'; we instantiate the function, even though
2476 we're not planning on emitting it, in case we get a chance to
2477 inline it. */
2478 if (DECL_EXTERNAL (fn))
2479 return;
2481 /* Emit any thunks that should be emitted at the same time as FN. */
2482 emit_associated_thunks (fn);
2484 timevar_push (TV_INTEGRATION);
2486 /* Optimize the body of the function before expanding it. */
2487 optimize_function (fn);
2489 timevar_pop (TV_INTEGRATION);
2490 timevar_push (TV_EXPAND);
2492 /* Save the current file name and line number. When we expand the
2493 body of the function, we'll set LINENO and INPUT_FILENAME so that
2494 error-mesages come out in the right places. */
2495 saved_lineno = lineno;
2496 saved_input_filename = input_filename;
2497 lineno = DECL_SOURCE_LINE (fn);
2498 input_filename = DECL_SOURCE_FILE (fn);
2500 genrtl_start_function (fn);
2501 current_function_is_thunk = DECL_THUNK_P (fn);
2503 /* Expand the body. */
2504 expand_stmt (DECL_SAVED_TREE (fn));
2506 /* Statements should always be full-expressions at the outermost set
2507 of curly braces for a function. */
2508 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2510 /* The outermost statement for a function contains the line number
2511 recorded when we finished processing the function. */
2512 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2514 /* Generate code for the function. */
2515 genrtl_finish_function (fn);
2517 /* If possible, obliterate the body of the function so that it can
2518 be garbage collected. */
2519 if (dump_enabled_p (TDI_all))
2520 /* Keep the body; we're going to dump it. */
2522 else if (DECL_INLINE (fn) && flag_inline_trees)
2523 /* We might need the body of this function so that we can expand
2524 it inline somewhere else. */
2526 else
2527 /* We don't need the body; blow it away. */
2528 DECL_SAVED_TREE (fn) = NULL_TREE;
2530 /* And restore the current source position. */
2531 lineno = saved_lineno;
2532 input_filename = saved_input_filename;
2533 extract_interface_info ();
2535 timevar_pop (TV_EXPAND);
2538 /* Helper function for walk_tree, used by finish_function to override all
2539 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2540 value optimization. */
2542 tree
2543 nullify_returns_r (tp, walk_subtrees, data)
2544 tree *tp;
2545 int *walk_subtrees;
2546 void *data;
2548 tree nrv = (tree) data;
2550 /* No need to walk into types. There wouldn't be any need to walk into
2551 non-statements, except that we have to consider STMT_EXPRs. */
2552 if (TYPE_P (*tp))
2553 *walk_subtrees = 0;
2554 else if (TREE_CODE (*tp) == RETURN_STMT)
2555 RETURN_EXPR (*tp) = NULL_TREE;
2556 else if (TREE_CODE (*tp) == CLEANUP_STMT
2557 && CLEANUP_DECL (*tp) == nrv)
2558 CLEANUP_EXPR (*tp) = NULL_TREE;
2560 /* Keep iterating. */
2561 return NULL_TREE;
2564 /* Start generating the RTL for FN. */
2566 static void
2567 genrtl_start_function (fn)
2568 tree fn;
2570 tree parm;
2572 /* Tell everybody what function we're processing. */
2573 current_function_decl = fn;
2574 /* Get the RTL machinery going for this function. */
2575 init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2576 /* Let everybody know that we're expanding this function, not doing
2577 semantic analysis. */
2578 expanding_p = 1;
2580 /* Even though we're inside a function body, we still don't want to
2581 call expand_expr to calculate the size of a variable-sized array.
2582 We haven't necessarily assigned RTL to all variables yet, so it's
2583 not safe to try to expand expressions involving them. */
2584 immediate_size_expand = 0;
2585 cfun->x_dont_save_pending_sizes_p = 1;
2587 /* Let the user know we're compiling this function. */
2588 announce_function (fn);
2590 /* Initialize the per-function data. */
2591 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2592 if (DECL_SAVED_FUNCTION_DATA (fn))
2594 /* If we already parsed this function, and we're just expanding it
2595 now, restore saved state. */
2596 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2598 /* This function is being processed in whole-function mode; we
2599 already did semantic analysis. */
2600 cfun->x_whole_function_mode_p = 1;
2602 /* If we decided that we didn't want to inline this function,
2603 make sure the back-end knows that. */
2604 if (!current_function_cannot_inline)
2605 current_function_cannot_inline = cp_function_chain->cannot_inline;
2607 /* We don't need the saved data anymore. */
2608 free (DECL_SAVED_FUNCTION_DATA (fn));
2609 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2612 /* Tell the cross-reference machinery that we're defining this
2613 function. */
2614 GNU_xref_function (fn, DECL_ARGUMENTS (fn));
2616 /* Keep track of how many functions we're presently expanding. */
2617 ++function_depth;
2619 /* Create a binding level for the parameters. */
2620 expand_start_bindings (2);
2621 /* Go through the PARM_DECLs for this function to see if any need
2622 cleanups. */
2623 for (parm = DECL_ARGUMENTS (fn); parm; parm = TREE_CHAIN (parm))
2624 if (TREE_TYPE (parm) != error_mark_node
2625 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (parm)))
2627 expand_function_start (fn, /*parms_have_cleanups=*/1);
2628 break;
2630 if (!parm)
2631 expand_function_start (fn, /*parms_have_cleanups=*/0);
2632 /* If this function is `main'. */
2633 if (DECL_MAIN_P (fn))
2634 expand_main_function ();
2635 /* Create a binding contour which can be used to catch
2636 cleanup-generated temporaries. */
2637 expand_start_bindings (2);
2639 /* Give our named return value the same RTL as our RESULT_DECL. */
2640 if (current_function_return_value)
2641 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2644 /* Finish generating the RTL for FN. */
2646 static void
2647 genrtl_finish_function (fn)
2648 tree fn;
2650 tree no_return_label = NULL_TREE;
2651 tree t;
2653 #if 0
2654 if (write_symbols != NO_DEBUG)
2656 /* Keep this code around in case we later want to control debug info
2657 based on whether a type is "used". (jason 1999-11-11) */
2659 tree ttype = target_type (fntype);
2660 tree parmdecl;
2662 if (IS_AGGR_TYPE (ttype))
2663 /* Let debugger know it should output info for this type. */
2664 note_debug_info_needed (ttype);
2666 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2668 ttype = target_type (TREE_TYPE (parmdecl));
2669 if (IS_AGGR_TYPE (ttype))
2670 /* Let debugger know it should output info for this type. */
2671 note_debug_info_needed (ttype);
2674 #endif
2676 /* Clean house because we will need to reorder insns here. */
2677 do_pending_stack_adjust ();
2679 if (!dtor_label && !DECL_CONSTRUCTOR_P (fn)
2680 && return_label != NULL_RTX
2681 && ! DECL_NAME (DECL_RESULT (current_function_decl)))
2682 no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2684 /* If this function is supposed to return a value, ensure that
2685 we do not fall into the cleanups by mistake. The end of our
2686 function will look like this:
2688 user code (may have return stmt somewhere)
2689 goto no_return_label
2690 cleanup_label:
2691 cleanups
2692 goto return_label
2693 no_return_label:
2694 NOTE_INSN_FUNCTION_END
2695 return_label:
2696 things for return
2698 If the user omits a return stmt in the USER CODE section, we
2699 will have a control path which reaches NOTE_INSN_FUNCTION_END.
2700 Otherwise, we won't. */
2701 if (no_return_label)
2703 DECL_CONTEXT (no_return_label) = fn;
2704 DECL_INITIAL (no_return_label) = error_mark_node;
2705 DECL_SOURCE_FILE (no_return_label) = input_filename;
2706 DECL_SOURCE_LINE (no_return_label) = lineno;
2707 expand_goto (no_return_label);
2710 if (cleanup_label)
2712 /* Remove the binding contour which is used to catch
2713 cleanup-generated temporaries. */
2714 expand_end_bindings (0, 0, 0);
2715 poplevel (0, 0, 0);
2717 /* Emit label at beginning of cleanup code for parameters. */
2718 emit_label (cleanup_label);
2721 /* Finish building code that will trigger warnings if users forget
2722 to make their functions return values. */
2723 if (return_label)
2724 emit_jump (return_label);
2725 if (no_return_label)
2727 /* We don't need to call `expand_*_return' here because we don't
2728 need any cleanups here--this path of code is only for error
2729 checking purposes. */
2730 expand_label (no_return_label);
2733 /* We hard-wired immediate_size_expand to zero in start_function.
2734 Expand_function_end will decrement this variable. So, we set the
2735 variable to one here, so that after the decrement it will remain
2736 zero. */
2737 immediate_size_expand = 1;
2739 /* Generate rtl for function exit. */
2740 expand_function_end (input_filename, lineno, 1);
2742 /* If this is a nested function (like a template instantiation that
2743 we're compiling in the midst of compiling something else), push a
2744 new GC context. That will keep local variables on the stack from
2745 being collected while we're doing the compilation of this
2746 function. */
2747 if (function_depth > 1)
2748 ggc_push_context ();
2750 /* There's no need to defer outputting this function any more; we
2751 know we want to output it. */
2752 DECL_DEFER_OUTPUT (fn) = 0;
2754 /* Run the optimizers and output the assembler code for this
2755 function. */
2756 rest_of_compilation (fn);
2758 /* Undo the call to ggc_push_context above. */
2759 if (function_depth > 1)
2760 ggc_pop_context ();
2762 #if 0
2763 /* Keep this code around in case we later want to control debug info
2764 based on whether a type is "used". (jason 1999-11-11) */
2766 if (ctype && TREE_ASM_WRITTEN (fn))
2767 note_debug_info_needed (ctype);
2768 #endif
2770 /* If this function is marked with the constructor attribute, add it
2771 to the list of functions to be called along with constructors
2772 from static duration objects. */
2773 if (DECL_STATIC_CONSTRUCTOR (fn))
2774 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2776 /* If this function is marked with the destructor attribute, add it
2777 to the list of functions to be called along with destructors from
2778 static duration objects. */
2779 if (DECL_STATIC_DESTRUCTOR (fn))
2780 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2782 --function_depth;
2784 /* In C++, we should never be saving RTL for the function. */
2785 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2787 /* Since we don't need the RTL for this function anymore, stop
2788 pointing to it. That's especially important for LABEL_DECLs,
2789 since you can reach all the instructions in the function from the
2790 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2791 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2792 local variables. */
2793 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2794 clear_decl_rtl,
2795 NULL);
2797 /* Clear out the RTL for the arguments. */
2798 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2800 SET_DECL_RTL (t, NULL_RTX);
2801 DECL_INCOMING_RTL (t) = NULL_RTX;
2804 if (!(flag_inline_trees && DECL_INLINE (fn)))
2805 /* DECL_INITIAL must remain nonzero so we know this was an
2806 actual function definition. */
2807 DECL_INITIAL (fn) = error_mark_node;
2809 /* Let the error reporting routines know that we're outside a
2810 function. For a nested function, this value is used in
2811 pop_cp_function_context and then reset via pop_function_context. */
2812 current_function_decl = NULL_TREE;
2815 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2816 its sub-blocks. */
2818 static tree
2819 clear_decl_rtl (tp, walk_subtrees, data)
2820 tree *tp;
2821 int *walk_subtrees ATTRIBUTE_UNUSED;
2822 void *data ATTRIBUTE_UNUSED;
2824 if (nonstatic_local_decl_p (*tp))
2825 SET_DECL_RTL (*tp, NULL_RTX);
2827 return NULL_TREE;
2830 /* Perform initialization related to this module. */
2832 void
2833 init_cp_semantics ()
2835 lang_expand_stmt = cp_expand_stmt;