* gxxint.texi: G++ now implements namespaces.
[official-gcc.git] / gcc / cp / semantics.c
blob6943b966efc33b5f69ec0ff67a235931bbde16cd
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 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"
35 /* There routines provide a modular interface to perform many parsing
36 operations. They may therefore be used during actual parsing, or
37 during template instantiation, which may be regarded as a
38 degenerate form of parsing. Since the current g++ parser is
39 lacking in several respects, and will be reimplemented, we are
40 attempting to move most code that is not directly related to
41 parsing into this file; that will make implementing the new parser
42 much easier since it will be able to make use of these routines. */
44 static void finish_expr_stmt_real PROTO((tree, int));
45 static tree expand_cond PROTO((tree));
46 static tree maybe_convert_cond PROTO((tree));
48 /* When parsing a template, LAST_TREE contains the last statement
49 parsed. These are chained together through the TREE_CHAIN field,
50 but often need to be re-organized since the parse is performed
51 bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of
52 STMT. */
54 #define RECHAIN_STMTS(stmt, substmt) \
55 do { \
56 substmt = TREE_CHAIN (stmt); \
57 TREE_CHAIN (stmt) = NULL_TREE; \
58 last_tree = stmt; \
59 } while (0)
61 /* Finish processing the COND, the SUBSTMT condition for STMT. */
63 #define FINISH_COND(cond, stmt, substmt) \
64 do { \
65 if (last_tree != stmt) \
66 { \
67 RECHAIN_STMTS (stmt, substmt); \
68 if (!processing_template_decl) \
69 { \
70 cond = build_tree_list (substmt, cond); \
71 substmt = cond; \
72 } \
73 } \
74 else \
75 substmt = cond; \
76 } while (0)
78 /* T is a statement. Add it to the statement-tree. */
80 void
81 add_tree (t)
82 tree t;
84 /* Add T to the statement-tree. */
85 last_tree = TREE_CHAIN (last_tree) = t;
87 /* When we expand a statement-tree, we must know whether or not the
88 statements are full-expresions. We record that fact here. */
89 if (building_stmt_tree ())
90 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p;
93 /* COND is the condition-expression for an if, while, etc.,
94 statement. Convert it to a boolean value, if appropriate. */
96 static tree
97 maybe_convert_cond (cond)
98 tree cond;
100 /* Empty conditions remain empty. */
101 if (!cond)
102 return NULL_TREE;
104 /* Wait until we instantiate templates before doing conversion. */
105 if (processing_template_decl)
106 return cond;
108 /* Do the conversion. */
109 cond = convert_from_reference (cond);
110 return condition_conversion (cond);
113 /* Finish an expression-statement, whose EXPRESSION is as indicated.
114 If ASSIGNED_THIS is non-zero, then this statement just assigned to
115 the `this' pointer. */
117 static void
118 finish_expr_stmt_real (expr, assigned_this)
119 tree expr;
120 int assigned_this;
122 if (expr != NULL_TREE)
124 if (building_stmt_tree ())
125 add_tree (build_min_nt (EXPR_STMT, expr));
126 else
128 emit_line_note (input_filename, lineno);
129 /* Do default conversion if safe and possibly important,
130 in case within ({...}). */
131 if (!stmts_are_full_exprs_p &&
132 ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
133 && lvalue_p (expr))
134 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
135 expr = default_conversion (expr);
137 if (stmts_are_full_exprs_p)
138 expand_start_target_temps ();
140 cplus_expand_expr_stmt (expr);
142 if (stmts_are_full_exprs_p)
144 expand_end_target_temps ();
145 clear_momentary ();
150 /* If this statement assigned to the `this' pointer, record that
151 fact for finish_stmt. */
152 if (assigned_this)
153 current_function_just_assigned_this = 1;
155 finish_stmt ();
157 /* This was an expression-statement, so we save the type of the
158 expression. */
159 last_expr_type = expr ? TREE_TYPE (expr) : NULL_TREE;
162 /* Like finish_expr_stmt_real, but ASSIGNS_THIS is always zero. */
164 void
165 finish_expr_stmt (expr)
166 tree expr;
168 finish_expr_stmt_real (expr, /*assigns_this=*/0);
171 /* Begin an if-statement. Returns a newly created IF_STMT if
172 appropriate. */
174 tree
175 begin_if_stmt ()
177 tree r;
179 if (building_stmt_tree ())
181 r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
182 add_tree (r);
184 else
185 r = NULL_TREE;
187 do_pushlevel ();
189 return r;
192 /* Process the COND of an if-statement, which may be given by
193 IF_STMT. */
195 void
196 finish_if_stmt_cond (cond, if_stmt)
197 tree cond;
198 tree if_stmt;
200 cond = maybe_convert_cond (cond);
202 if (building_stmt_tree ())
203 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
204 else
206 emit_line_note (input_filename, lineno);
207 expand_start_cond (cond, 0);
211 /* Finish the then-clause of an if-statement, which may be given by
212 IF_STMT. */
214 tree
215 finish_then_clause (if_stmt)
216 tree if_stmt;
218 if (building_stmt_tree ())
220 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
221 last_tree = if_stmt;
222 return if_stmt;
224 else
225 return NULL_TREE;
228 /* Begin the else-clause of an if-statement. */
230 void
231 begin_else_clause ()
233 if (!building_stmt_tree ())
234 expand_start_else ();
237 /* Finish the else-clause of an if-statement, which may be given by
238 IF_STMT. */
240 void
241 finish_else_clause (if_stmt)
242 tree if_stmt;
244 if (building_stmt_tree ())
245 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
248 /* Finsh an if-statement. */
250 void
251 finish_if_stmt ()
253 if (!building_stmt_tree ())
254 expand_end_cond ();
256 do_poplevel ();
257 finish_stmt ();
260 /* Begin a while-statement. Returns a newly created WHILE_STMT if
261 appropriate. */
263 tree
264 begin_while_stmt ()
266 tree r;
268 if (building_stmt_tree ())
270 r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE);
271 add_tree (r);
273 else
275 emit_nop ();
276 emit_line_note (input_filename, lineno);
277 expand_start_loop (1);
278 r = NULL_TREE;
281 do_pushlevel ();
283 return r;
286 /* Process the COND of an if-statement, which may be given by
287 WHILE_STMT. */
289 void
290 finish_while_stmt_cond (cond, while_stmt)
291 tree cond;
292 tree while_stmt;
294 cond = maybe_convert_cond (cond);
296 if (building_stmt_tree ())
297 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
298 else
300 emit_line_note (input_filename, lineno);
301 expand_exit_loop_if_false (0, cond);
304 /* If COND wasn't a declaration, clear out the
305 block we made for it and start a new one here so the
306 optimization in expand_end_loop will work. */
307 if (getdecls () == NULL_TREE)
309 do_poplevel ();
310 do_pushlevel ();
314 /* Finish a while-statement, which may be given by WHILE_STMT. */
316 void
317 finish_while_stmt (while_stmt)
318 tree while_stmt;
320 do_poplevel ();
322 if (building_stmt_tree ())
323 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
324 else
325 expand_end_loop ();
326 finish_stmt ();
329 /* Begin a do-statement. Returns a newly created DO_STMT if
330 appropriate. */
332 tree
333 begin_do_stmt ()
335 if (building_stmt_tree ())
337 tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE);
338 add_tree (r);
339 return r;
341 else
343 emit_nop ();
344 emit_line_note (input_filename, lineno);
345 expand_start_loop_continue_elsewhere (1);
346 return NULL_TREE;
350 /* Finish the body of a do-statement, which may be given by DO_STMT. */
352 void
353 finish_do_body (do_stmt)
354 tree do_stmt;
356 if (building_stmt_tree ())
357 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
358 else
359 expand_loop_continue_here ();
362 /* Finish a do-statement, which may be given by DO_STMT, and whose
363 COND is as indicated. */
365 void
366 finish_do_stmt (cond, do_stmt)
367 tree cond;
368 tree do_stmt;
370 cond = maybe_convert_cond (cond);
372 if (building_stmt_tree ())
373 DO_COND (do_stmt) = cond;
374 else
376 emit_line_note (input_filename, lineno);
377 expand_exit_loop_if_false (0, cond);
378 expand_end_loop ();
381 clear_momentary ();
382 finish_stmt ();
385 /* Finish a return-statement. The EXPRESSION returned, if any, is as
386 indicated. */
388 void
389 finish_return_stmt (expr)
390 tree expr;
392 if (building_stmt_tree ())
393 add_tree (build_min_nt (RETURN_STMT, expr));
394 else
396 emit_line_note (input_filename, lineno);
397 c_expand_return (expr);
400 finish_stmt ();
403 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
405 tree
406 begin_for_stmt ()
408 tree r;
410 if (building_stmt_tree ())
412 r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE,
413 NULL_TREE, NULL_TREE);
414 add_tree (r);
416 else
417 r = NULL_TREE;
419 if (flag_new_for_scope > 0)
421 do_pushlevel ();
422 note_level_for_for ();
425 return r;
428 /* Finish the for-init-statement of a for-statement, which may be
429 given by FOR_STMT. */
431 void
432 finish_for_init_stmt (for_stmt)
433 tree for_stmt;
435 if (building_stmt_tree ())
437 if (last_tree != for_stmt)
438 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
440 else
442 emit_nop ();
443 emit_line_note (input_filename, lineno);
444 expand_start_loop_continue_elsewhere (1);
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);
460 if (building_stmt_tree ())
461 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
462 else
464 emit_line_note (input_filename, lineno);
465 if (cond)
466 expand_exit_loop_if_false (0, cond);
469 /* If the cond wasn't a declaration, clear out the
470 block we made for it and start a new one here so the
471 optimization in expand_end_loop will work. */
472 if (getdecls () == NULL_TREE)
474 do_poplevel ();
475 do_pushlevel ();
479 /* Finish the increment-EXPRESSION in a for-statement, which may be
480 given by FOR_STMT. */
482 void
483 finish_for_expr (expr, for_stmt)
484 tree expr;
485 tree for_stmt;
487 if (building_stmt_tree ())
488 FOR_EXPR (for_stmt) = expr;
490 /* Don't let the tree nodes for EXPR be discarded
491 by clear_momentary during the parsing of the next stmt. */
492 push_momentary ();
495 /* Finish the body of a for-statement, which may be given by
496 FOR_STMT. The increment-EXPR for the loop must be
497 provided. */
499 void
500 finish_for_stmt (expr, for_stmt)
501 tree expr;
502 tree for_stmt;
504 /* Pop the scope for the body of the loop. */
505 do_poplevel ();
507 if (building_stmt_tree ())
508 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
509 else
511 emit_line_note (input_filename, lineno);
512 expand_loop_continue_here ();
513 if (expr)
514 finish_expr_stmt (expr);
515 expand_end_loop ();
518 pop_momentary ();
520 if (flag_new_for_scope > 0)
521 do_poplevel ();
523 finish_stmt ();
526 /* Finish a break-statement. */
528 void
529 finish_break_stmt ()
531 emit_line_note (input_filename, lineno);
532 if (building_stmt_tree ())
533 add_tree (build_min_nt (BREAK_STMT));
534 else if ( ! expand_exit_something ())
535 cp_error ("break statement not within loop or switch");
538 /* Finish a continue-statement. */
540 void
541 finish_continue_stmt ()
543 emit_line_note (input_filename, lineno);
544 if (building_stmt_tree ())
545 add_tree (build_min_nt (CONTINUE_STMT));
546 else if (! expand_continue_loop (0))
547 cp_error ("continue statement not within a loop");
550 /* Begin a switch-statement. Returns a new SWITCH_STMT if
551 appropriate. */
553 tree
554 begin_switch_stmt ()
556 tree r;
558 if (building_stmt_tree ())
560 r = build_min_nt (SWITCH_STMT, NULL_TREE, NULL_TREE);
561 add_tree (r);
563 else
564 r = NULL_TREE;
566 do_pushlevel ();
568 return r;
571 /* Finish the cond of a switch-statement. */
573 void
574 finish_switch_cond (cond, switch_stmt)
575 tree cond;
576 tree switch_stmt;
578 if (building_stmt_tree ())
579 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
580 else if (cond != error_mark_node)
582 emit_line_note (input_filename, lineno);
583 c_expand_start_case (cond);
585 else
586 /* The code is in error, but we don't want expand_end_case to
587 crash. */
588 c_expand_start_case (boolean_false_node);
590 push_switch ();
592 /* Don't let the tree nodes for COND be discarded by
593 clear_momentary during the parsing of the next stmt. */
594 push_momentary ();
597 /* Finish the body of a switch-statement, which may be given by
598 SWITCH_STMT. The COND to switch on is indicated. */
600 void
601 finish_switch_stmt (cond, switch_stmt)
602 tree cond;
603 tree switch_stmt;
605 if (building_stmt_tree ())
606 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
607 else
608 expand_end_case (cond);
609 pop_momentary ();
610 pop_switch ();
611 do_poplevel ();
612 finish_stmt ();
615 /* Finish a case-label. */
617 void
618 finish_case_label (low_value, high_value)
619 tree low_value;
620 tree high_value;
622 if (building_stmt_tree ())
624 /* Add a representation for the case label to the statement
625 tree. */
626 add_tree (build_min_nt (CASE_LABEL, low_value, high_value));
627 /* And warn about crossing initializations, etc. */
628 if (!processing_template_decl)
629 define_case_label ();
630 return;
633 do_case (low_value, high_value);
636 /* Finish a goto-statement. */
638 void
639 finish_goto_stmt (destination)
640 tree destination;
642 if (TREE_CODE (destination) == IDENTIFIER_NODE)
643 destination = lookup_label (destination);
645 /* We warn about unused labels with -Wunused. That means we have to
646 mark the used labels as used. */
647 if (TREE_CODE (destination) == LABEL_DECL)
648 TREE_USED (destination) = 1;
650 if (building_stmt_tree ())
651 add_tree (build_min_nt (GOTO_STMT, destination));
652 else
654 emit_line_note (input_filename, lineno);
656 if (TREE_CODE (destination) == LABEL_DECL)
658 label_rtx (destination);
659 expand_goto (destination);
661 else
662 expand_computed_goto (destination);
666 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
667 appropriate. */
669 tree
670 begin_try_block ()
672 if (building_stmt_tree ())
674 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
675 NULL_TREE);
676 add_tree (r);
677 return r;
679 else
681 emit_line_note (input_filename, lineno);
682 expand_start_try_stmts ();
683 return NULL_TREE;
687 /* Likewise, for a function-try-block. */
689 tree
690 begin_function_try_block ()
692 if (building_stmt_tree ())
694 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
695 NULL_TREE);
696 FN_TRY_BLOCK_P (r) = 1;
697 add_tree (r);
698 return r;
700 else
702 if (! current_function_parms_stored)
703 store_parm_decls ();
704 expand_start_early_try_stmts ();
705 return NULL_TREE;
709 /* Finish a try-block, which may be given by TRY_BLOCK. */
711 void
712 finish_try_block (try_block)
713 tree try_block;
715 if (building_stmt_tree ())
716 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
717 else
718 expand_start_all_catch ();
721 /* Finish the body of a cleanup try-block, which may be given by
722 TRY_BLOCK. */
724 void
725 finish_cleanup_try_block (try_block)
726 tree try_block;
728 if (building_stmt_tree ())
729 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
732 /* Finish an implicitly generated try-block, with a cleanup is given
733 by CLEANUP. */
735 void
736 finish_cleanup (cleanup, try_block)
737 tree cleanup;
738 tree try_block;
740 if (building_stmt_tree ())
742 TRY_HANDLERS (try_block) = cleanup;
743 CLEANUP_P (try_block) = 1;
745 else
746 expand_eh_region_end (protect_with_terminate (cleanup));
749 /* Likewise, for a function-try-block. */
751 void
752 finish_function_try_block (try_block)
753 tree try_block;
755 if (building_stmt_tree ())
757 if (TREE_CHAIN (try_block)
758 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
760 /* Chain the compound statement after the CTOR_INITIALIZER. */
761 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
762 /* And make the CTOR_INITIALIZER the body of the try-block. */
763 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
765 else
766 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
768 else
770 end_protect_partials ();
771 expand_start_all_catch ();
774 in_function_try_handler = 1;
777 /* Finish a handler-sequence for a try-block, which may be given by
778 TRY_BLOCK. */
780 void
781 finish_handler_sequence (try_block)
782 tree try_block;
784 if (building_stmt_tree ())
785 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
786 else
787 expand_end_all_catch ();
790 /* Likewise, for a function-try-block. */
792 void
793 finish_function_handler_sequence (try_block)
794 tree try_block;
796 in_function_try_handler = 0;
798 if (building_stmt_tree ())
799 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
800 else
801 expand_end_all_catch ();
804 /* Begin a handler. Returns a HANDLER if appropriate. */
806 tree
807 begin_handler ()
809 tree r;
811 if (building_stmt_tree ())
813 r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
814 add_tree (r);
816 else
817 r = NULL_TREE;
819 do_pushlevel ();
821 return r;
824 /* Finish the handler-parameters for a handler, which may be given by
825 HANDLER. DECL is the declaration for the catch parameter, or NULL
826 if this is a `catch (...)' clause. */
828 tree
829 finish_handler_parms (decl, handler)
830 tree decl;
831 tree handler;
833 tree blocks = NULL_TREE;
835 if (processing_template_decl)
837 if (decl)
839 decl = pushdecl (decl);
840 decl = push_template_decl (decl);
841 add_decl_stmt (decl);
842 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
845 else if (building_stmt_tree ())
846 blocks = expand_start_catch_block (decl);
848 return blocks;
851 /* Note the beginning of a handler for TYPE. This function is called
852 at the point to which control should be transferred when an
853 appropriately-typed exception is thrown. */
855 void
856 begin_catch_block (type)
857 tree type;
859 if (building_stmt_tree ())
860 add_tree (build (START_CATCH_STMT, type));
861 else
862 start_catch_handler (type);
865 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
866 the return value from the matching call to finish_handler_parms. */
868 void
869 finish_handler (blocks, handler)
870 tree blocks;
871 tree handler;
873 if (!processing_template_decl)
875 if (building_stmt_tree ())
876 expand_end_catch_block (blocks);
878 if (!building_stmt_tree ())
880 /* Fall to outside the try statement when done executing
881 handler and we fall off end of handler. This is jump
882 Lresume in the documentation. */
883 expand_goto (top_label_entry (&caught_return_label_stack));
884 end_catch_handler ();
888 if (building_stmt_tree ())
889 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
891 do_poplevel ();
894 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
895 compound-statement does not define a scope. Returns a new
896 COMPOUND_STMT if appropriate. */
898 tree
899 begin_compound_stmt (has_no_scope)
900 int has_no_scope;
902 tree r;
904 if (building_stmt_tree ())
906 r = build_min_nt (COMPOUND_STMT, NULL_TREE);
907 add_tree (r);
908 if (has_no_scope)
909 COMPOUND_STMT_NO_SCOPE (r) = 1;
911 else
912 r = NULL_TREE;
914 last_expr_type = NULL_TREE;
916 if (!has_no_scope)
917 do_pushlevel ();
918 else
919 /* Normally, we try hard to keep the BLOCK for a
920 statement-expression. But, if it's a statement-expression with
921 a scopeless block, there's nothing to keep, and we don't want
922 to accidentally keep a block *inside* the scopeless block. */
923 keep_next_level (0);
925 /* If this is the outermost block of the function, declare the
926 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
927 if (!current_function_name_declared && !processing_template_decl)
929 declare_function_name ();
930 current_function_name_declared = 1;
933 return r;
937 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
938 If HAS_NO_SCOPE is non-zero, the compound statement does not define
939 a scope. */
941 tree
942 finish_compound_stmt (has_no_scope, compound_stmt)
943 int has_no_scope;
944 tree compound_stmt;
946 tree r;
947 tree t;
949 if (!has_no_scope)
950 r = do_poplevel ();
951 else
952 r = NULL_TREE;
954 if (building_stmt_tree ())
955 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
957 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
958 the precise purpose of that variable is store the type of the
959 last expression statement within the last compound statement, we
960 preserve the value. */
961 t = last_expr_type;
962 finish_stmt ();
963 last_expr_type = t;
965 return r;
968 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
969 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
970 CLOBBERS. */
972 void
973 finish_asm_stmt (cv_qualifier, string, output_operands,
974 input_operands, clobbers)
975 tree cv_qualifier;
976 tree string;
977 tree output_operands;
978 tree input_operands;
979 tree clobbers;
981 if (TREE_CHAIN (string))
983 if (building_stmt_tree ())
984 /* We need to build the combined string on the permanent
985 obstack so that we can use it during instantiations. */
986 push_permanent_obstack ();
988 string = combine_strings (string);
990 if (building_stmt_tree ())
991 pop_obstacks ();
994 if (cv_qualifier != NULL_TREE
995 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
997 cp_warning ("%s qualifier ignored on asm",
998 IDENTIFIER_POINTER (cv_qualifier));
999 cv_qualifier = NULL_TREE;
1002 if (building_stmt_tree ())
1004 tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
1005 output_operands, input_operands,
1006 clobbers);
1007 add_tree (r);
1009 else
1011 emit_line_note (input_filename, lineno);
1012 if (output_operands != NULL_TREE || input_operands != NULL_TREE
1013 || clobbers != NULL_TREE)
1015 tree t;
1017 for (t = input_operands; t; t = TREE_CHAIN (t))
1018 TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
1020 c_expand_asm_operands (string, output_operands,
1021 input_operands,
1022 clobbers,
1023 cv_qualifier != NULL_TREE,
1024 input_filename, lineno);
1026 else
1027 expand_asm (string);
1029 finish_stmt ();
1033 /* Finish a label with the indicated NAME. */
1035 void
1036 finish_label_stmt (name)
1037 tree name;
1039 tree decl = define_label (input_filename, lineno, name);
1041 if (building_stmt_tree ())
1042 add_tree (build_min_nt (LABEL_STMT, decl));
1043 else if (decl)
1044 expand_label (decl);
1047 /* Finish a series of declarations for local labels. G++ allows users
1048 to declare "local" labels, i.e., labels with scope. This extension
1049 is useful when writing code involving statement-expressions. */
1051 void
1052 finish_label_decl (name)
1053 tree name;
1055 tree decl = declare_local_label (name);
1056 if (building_stmt_tree ())
1057 add_decl_stmt (decl);
1060 /* Create a declaration statement for the declaration given by the
1061 DECL. */
1063 void
1064 add_decl_stmt (decl)
1065 tree decl;
1067 tree decl_stmt;
1069 /* We need the type to last until instantiation time. */
1070 decl_stmt = build_min_nt (DECL_STMT, decl);
1071 add_tree (decl_stmt);
1074 /* We're in a constructor, and have just constructed a a subobject of
1075 *THIS. CLEANUP is code to run if an exception is thrown before the
1076 end of the current function is reached. */
1078 void
1079 finish_subobject (cleanup)
1080 tree cleanup;
1082 if (building_stmt_tree ())
1084 tree r = build_min_nt (SUBOBJECT, cleanup);
1085 add_tree (r);
1087 else
1088 add_partial_entry (cleanup);
1091 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1093 void
1094 finish_decl_cleanup (decl, cleanup)
1095 tree decl;
1096 tree cleanup;
1098 if (building_stmt_tree ())
1099 add_tree (build_min_nt (CLEANUP_STMT, decl, cleanup));
1100 else if (!decl
1101 || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
1102 expand_decl_cleanup (decl, cleanup);
1105 /* Bind a name and initialization to the return value of
1106 the current function. */
1108 void
1109 finish_named_return_value (return_id, init)
1110 tree return_id, init;
1112 tree decl = DECL_RESULT (current_function_decl);
1114 if (pedantic)
1115 /* Give this error as many times as there are occurrences,
1116 so that users can use Emacs compilation buffers to find
1117 and fix all such places. */
1118 pedwarn ("ANSI C++ does not permit named return values");
1120 if (return_id != NULL_TREE)
1122 if (DECL_NAME (decl) == NULL_TREE)
1124 DECL_NAME (decl) = return_id;
1125 DECL_ASSEMBLER_NAME (decl) = return_id;
1127 else
1129 cp_error ("return identifier `%D' already in place", return_id);
1130 return;
1134 /* Can't let this happen for constructors. */
1135 if (DECL_CONSTRUCTOR_P (current_function_decl))
1137 error ("can't redefine default return value for constructors");
1138 return;
1141 /* If we have a named return value, put that in our scope as well. */
1142 if (DECL_NAME (decl) != NULL_TREE)
1144 /* Let `cp_finish_decl' know that this initializer is ok. */
1145 DECL_INITIAL (decl) = init;
1146 if (doing_semantic_analysis_p ())
1147 pushdecl (decl);
1149 if (building_stmt_tree ())
1150 add_tree (build_min_nt (RETURN_INIT, return_id, init));
1151 else
1153 cp_finish_decl (decl, init, NULL_TREE, 0, 0);
1154 store_return_init (decl);
1159 /* Cache the value of this class's main virtual function table pointer
1160 in a register variable. This will save one indirection if a
1161 more than one virtual function call is made this function. */
1163 void
1164 setup_vtbl_ptr ()
1166 if (base_init_expr == 0
1167 && DECL_CONSTRUCTOR_P (current_function_decl))
1169 if (building_stmt_tree ())
1170 add_tree (build_min_nt
1171 (CTOR_INITIALIZER,
1172 current_member_init_list, current_base_init_list));
1173 else
1174 emit_base_init (current_class_type);
1177 /* Always keep the BLOCK node associated with the outermost pair of
1178 curley braces of a function. These are needed for correct
1179 operation of dwarfout.c. */
1180 keep_next_level (1);
1183 /* Begin a new scope. */
1185 void
1186 do_pushlevel ()
1188 if (!building_stmt_tree ())
1190 emit_line_note (input_filename, lineno);
1191 clear_last_expr ();
1193 push_momentary ();
1194 if (stmts_are_full_exprs_p)
1196 pushlevel (0);
1197 if (!building_stmt_tree ()
1198 && !current_function->x_whole_function_mode_p)
1199 expand_start_bindings (0);
1200 else if (building_stmt_tree () && !processing_template_decl)
1202 tree ss = build_min_nt (SCOPE_STMT);
1203 SCOPE_BEGIN_P (ss) = 1;
1204 add_tree (ss);
1205 current_scope_stmt_stack
1206 = tree_cons (NULL_TREE, ss, current_scope_stmt_stack);
1211 /* Finish a scope. */
1213 tree
1214 do_poplevel ()
1216 tree t = NULL_TREE;
1218 if (stmts_are_full_exprs_p)
1220 if (!building_stmt_tree ()
1221 && !current_function->x_whole_function_mode_p)
1222 expand_end_bindings (getdecls (), kept_level_p (), 0);
1223 else if (building_stmt_tree () && !processing_template_decl)
1225 tree ss = build_min_nt (SCOPE_STMT);
1226 SCOPE_NULLIFIED_P (ss) = !kept_level_p ();
1227 SCOPE_NULLIFIED_P (TREE_VALUE (current_scope_stmt_stack))
1228 = SCOPE_NULLIFIED_P (ss);
1229 add_tree (ss);
1230 current_scope_stmt_stack = TREE_CHAIN (current_scope_stmt_stack);
1232 /* When not in function-at-a-time mode, expand_end_bindings
1233 will warn about unused variables. But, in
1234 function-at-a-time mode expand_end_bindings is not passed
1235 the list of variables in the current scope, and therefore
1236 no warning is emitted. So, we explicitly warn here. */
1237 warn_about_unused_variables (getdecls ());
1240 t = poplevel (kept_level_p (), 1, 0);
1242 pop_momentary ();
1243 return t;
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 return expr;
1259 /* Begin a statement-expression. The value returned must be passed to
1260 finish_stmt_expr. */
1262 tree
1263 begin_stmt_expr ()
1265 keep_next_level (1);
1266 /* If we're building a statement tree, then the upcoming compound
1267 statement will be chained onto the tree structure, starting at
1268 last_tree. We return last_tree so that we can later unhook the
1269 compound statement. */
1270 return building_stmt_tree () ? last_tree : expand_start_stmt_expr();
1273 /* Finish a statement-expression. RTL_EXPR should be the value
1274 returned by the previous begin_stmt_expr; EXPR is the
1275 statement-expression. Returns an expression representing the
1276 statement-expression. */
1278 tree
1279 finish_stmt_expr (rtl_expr, expr)
1280 tree rtl_expr;
1281 tree expr;
1283 tree result;
1285 if (!building_stmt_tree ())
1287 rtl_expr = expand_end_stmt_expr (rtl_expr);
1288 /* The statements have side effects, so the group does. */
1289 TREE_SIDE_EFFECTS (rtl_expr) = 1;
1292 if (building_stmt_tree ())
1294 /* If the last thing in the statement-expression was not an
1295 expression-statement, then it has type `void'. */
1296 if (!last_expr_type)
1297 last_expr_type = void_type_node;
1298 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1299 TREE_SIDE_EFFECTS (result) = 1;
1301 /* Remove the compound statement from the tree structure; it is
1302 now saved in the STMT_EXPR. */
1303 last_tree = rtl_expr;
1304 TREE_CHAIN (last_tree) = NULL_TREE;
1306 else if (expr && TREE_CODE (expr) == BLOCK)
1308 result = build (BIND_EXPR, TREE_TYPE (rtl_expr),
1309 NULL_TREE, rtl_expr, expr);
1310 delete_block (expr);
1312 else
1313 result = rtl_expr;
1315 if (expr && TREE_CODE (expr) == BLOCK)
1316 /* Remove the block from the tree at this point. It gets put back
1317 at the proper place when the STMT_EXPR or BIND_EXPR is
1318 expanded. */
1319 delete_block (expr);
1321 return result;
1324 /* Finish a call to FN with ARGS. Returns a representation of the
1325 call. */
1327 tree
1328 finish_call_expr (fn, args, koenig)
1329 tree fn;
1330 tree args;
1331 int koenig;
1333 tree result;
1335 if (koenig)
1337 if (TREE_CODE (fn) == BIT_NOT_EXPR)
1338 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
1339 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
1340 fn = do_identifier (fn, 2, args);
1342 result = build_x_function_call (fn, args, current_class_ref);
1344 if (TREE_CODE (result) == CALL_EXPR
1345 && (! TREE_TYPE (result)
1346 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
1347 result = require_complete_type (result);
1349 return result;
1352 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1353 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1354 POSTDECREMENT_EXPR.) */
1356 tree
1357 finish_increment_expr (expr, code)
1358 tree expr;
1359 enum tree_code code;
1361 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1362 a COMPONENT_REF). This way if we've got, say, a reference to a
1363 static member that's being operated on, we don't end up trying to
1364 find a member operator for the class it's in. */
1366 if (TREE_CODE (expr) == OFFSET_REF)
1367 expr = resolve_offset_ref (expr);
1368 return build_x_unary_op (code, expr);
1371 /* Finish a use of `this'. Returns an expression for `this'. */
1373 tree
1374 finish_this_expr ()
1376 tree result;
1378 if (current_class_ptr)
1380 #ifdef WARNING_ABOUT_CCD
1381 TREE_USED (current_class_ptr) = 1;
1382 #endif
1383 result = current_class_ptr;
1385 else if (current_function_decl
1386 && DECL_STATIC_FUNCTION_P (current_function_decl))
1388 error ("`this' is unavailable for static member functions");
1389 result = error_mark_node;
1391 else
1393 if (current_function_decl)
1394 error ("invalid use of `this' in non-member function");
1395 else
1396 error ("invalid use of `this' at top level");
1397 result = error_mark_node;
1400 return result;
1403 /* Finish a member function call using OBJECT and ARGS as arguments to
1404 FN. Returns an expression for the call. */
1406 tree
1407 finish_object_call_expr (fn, object, args)
1408 tree fn;
1409 tree object;
1410 tree args;
1412 #if 0
1413 /* This is a future direction of this code, but because
1414 build_x_function_call cannot always undo what is done in
1415 build_component_ref entirely yet, we cannot do this. */
1417 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
1418 return finish_call_expr (real_fn, args);
1419 #else
1420 if (TREE_CODE (fn) == TYPE_DECL)
1422 if (processing_template_decl)
1423 /* This can happen on code like:
1425 class X;
1426 template <class T> void f(T t) {
1427 t.X();
1430 We just grab the underlying IDENTIFIER. */
1431 fn = DECL_NAME (fn);
1432 else
1434 cp_error ("calling type `%T' like a method", fn);
1435 return error_mark_node;
1439 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1440 #endif
1443 /* Finish a qualified member function call using OBJECT and ARGS as
1444 arguments to FN. Returns an expressino for the call. */
1446 tree
1447 finish_qualified_object_call_expr (fn, object, args)
1448 tree fn;
1449 tree object;
1450 tree args;
1452 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1453 TREE_OPERAND (fn, 1), args);
1456 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1457 being the scope, if any, of DESTRUCTOR. Returns an expression for
1458 the call. */
1460 tree
1461 finish_pseudo_destructor_call_expr (object, scope, destructor)
1462 tree object;
1463 tree scope;
1464 tree destructor;
1466 if (processing_template_decl)
1467 return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1469 if (scope && scope != destructor)
1470 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1471 scope, destructor);
1473 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1474 && (TREE_CODE (TREE_TYPE (object)) !=
1475 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1476 cp_error ("`%E' is not of type `%T'", object, destructor);
1478 return cp_convert (void_type_node, object);
1481 /* Finish a call to a globally qualified member function FN using
1482 ARGS. Returns an expression for the call. */
1484 tree
1485 finish_qualified_call_expr (fn, args)
1486 tree fn;
1487 tree args;
1489 if (processing_template_decl)
1490 return build_min_nt (CALL_EXPR, fn, args, NULL_TREE);
1491 else
1492 return build_member_call (TREE_OPERAND (fn, 0),
1493 TREE_OPERAND (fn, 1),
1494 args);
1497 /* Finish an expression taking the address of LABEL. Returns an
1498 expression for the address. */
1500 tree
1501 finish_label_address_expr (label)
1502 tree label;
1504 tree result;
1506 label = lookup_label (label);
1507 if (label == NULL_TREE)
1508 result = null_pointer_node;
1509 else
1511 TREE_USED (label) = 1;
1512 result = build1 (ADDR_EXPR, ptr_type_node, label);
1513 TREE_CONSTANT (result) = 1;
1516 return result;
1519 /* Finish an expression of the form CODE EXPR. */
1521 tree
1522 finish_unary_op_expr (code, expr)
1523 enum tree_code code;
1524 tree expr;
1526 tree result = build_x_unary_op (code, expr);
1527 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST)
1528 TREE_NEGATED_INT (result) = 1;
1529 overflow_warning (result);
1530 return result;
1533 /* Finish an id-expression. */
1535 tree
1536 finish_id_expr (expr)
1537 tree expr;
1539 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1540 expr = do_identifier (expr, 1, NULL_TREE);
1542 return expr;
1545 /* Begin a new-placement. */
1548 begin_new_placement ()
1550 /* The arguments to a placement new might be passed to a
1551 deallocation function, in the event that the allocation throws an
1552 exception. Since we don't expand exception handlers until the
1553 end of a function, we must make sure the arguments stay around
1554 that long. */
1555 return suspend_momentary ();
1558 /* Finish a new-placement. The ARGS are the placement arguments. The
1559 COOKIE is the value returned by the previous call to
1560 begin_new_placement. */
1562 tree
1563 finish_new_placement (args, cookie)
1564 tree args;
1565 int cookie;
1567 resume_momentary (cookie);
1568 return args;
1571 /* Begin a function defniition declared with DECL_SPECS and
1572 DECLARATOR. Returns non-zero if the function-declaration is
1573 legal. */
1576 begin_function_definition (decl_specs, declarator)
1577 tree decl_specs;
1578 tree declarator;
1580 tree specs;
1581 tree attrs;
1582 split_specs_attrs (decl_specs, &specs, &attrs);
1583 if (!start_function (specs, declarator, attrs, SF_DEFAULT))
1584 return 0;
1586 reinit_parse_for_function ();
1587 /* The things we're about to see are not directly qualified by any
1588 template headers we've seen thus far. */
1589 reset_specialization ();
1591 return 1;
1594 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1595 a SCOPE_REF. */
1597 tree
1598 begin_constructor_declarator (scope, name)
1599 tree scope;
1600 tree name;
1602 tree result = build_parse_node (SCOPE_REF, scope, name);
1603 enter_scope_of (result);
1604 return result;
1607 /* Finish an init-declarator. Returns a DECL. */
1609 tree
1610 finish_declarator (declarator, declspecs, attributes,
1611 prefix_attributes, initialized)
1612 tree declarator;
1613 tree declspecs;
1614 tree attributes;
1615 tree prefix_attributes;
1616 int initialized;
1618 return start_decl (declarator, declspecs, initialized, attributes,
1619 prefix_attributes);
1622 /* Finish a translation unit. */
1624 void
1625 finish_translation_unit ()
1627 /* In case there were missing closebraces,
1628 get us back to the global binding level. */
1629 pop_everything ();
1630 while (current_namespace != global_namespace)
1631 pop_namespace ();
1632 finish_file ();
1635 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1636 Returns the parameter. */
1638 tree
1639 finish_template_type_parm (aggr, identifier)
1640 tree aggr;
1641 tree identifier;
1643 if (aggr != class_type_node)
1645 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1646 aggr = class_type_node;
1649 return build_tree_list (aggr, identifier);
1652 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1653 Returns the parameter. */
1655 tree
1656 finish_template_template_parm (aggr, identifier)
1657 tree aggr;
1658 tree identifier;
1660 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1661 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1662 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1663 DECL_TEMPLATE_RESULT (tmpl) = decl;
1664 SET_DECL_ARTIFICIAL (decl);
1665 end_template_decl ();
1667 return finish_template_type_parm (aggr, tmpl);
1670 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1671 non-zero, the parameter list was terminated by a `...'. */
1673 tree
1674 finish_parmlist (parms, ellipsis)
1675 tree parms;
1676 int ellipsis;
1678 if (!ellipsis)
1679 chainon (parms, void_list_node);
1680 /* We mark the PARMS as a parmlist so that declarator processing can
1681 disambiguate certain constructs. */
1682 if (parms != NULL_TREE)
1683 TREE_PARMLIST (parms) = 1;
1685 return parms;
1688 /* Begin a class definition, as indicated by T. */
1690 tree
1691 begin_class_definition (t)
1692 tree t;
1694 push_permanent_obstack ();
1696 if (t == error_mark_node
1697 || ! IS_AGGR_TYPE (t))
1699 t = make_lang_type (RECORD_TYPE);
1700 pushtag (make_anon_name (), t, 0);
1703 /* In a definition of a member class template, we will get here with an
1704 implicit typename, a TYPENAME_TYPE with a type. */
1705 if (TREE_CODE (t) == TYPENAME_TYPE)
1706 t = TREE_TYPE (t);
1708 /* If we generated a partial instantiation of this type, but now
1709 we're seeing a real definition, we're actually looking at a
1710 partial specialization. Consider:
1712 template <class T, class U>
1713 struct Y {};
1715 template <class T>
1716 struct X {};
1718 template <class T, class U>
1719 void f()
1721 typename X<Y<T, U> >::A a;
1724 template <class T, class U>
1725 struct X<Y<T, U> >
1729 We have to undo the effects of the previous partial
1730 instantiation. */
1731 if (PARTIAL_INSTANTIATION_P (t))
1733 if (!pedantic)
1735 /* Unfortunately, when we're not in pedantic mode, we
1736 attempt to actually fill in some of the fields of the
1737 partial instantiation, in order to support the implicit
1738 typename extension. Clear those fields now, in
1739 preparation for the definition here. The fields cleared
1740 here must match those set in instantiate_class_template.
1741 Look for a comment mentioning begin_class_definition
1742 there. */
1743 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1744 TYPE_FIELDS (t) = NULL_TREE;
1745 TYPE_METHODS (t) = NULL_TREE;
1746 CLASSTYPE_TAGS (t) = NULL_TREE;
1747 TYPE_SIZE (t) = NULL_TREE;
1750 /* This isn't a partial instantiation any more. */
1751 PARTIAL_INSTANTIATION_P (t) = 0;
1753 /* If this type was already complete, and we see another definition,
1754 that's an error. */
1755 else if (TYPE_SIZE (t))
1756 duplicate_tag_error (t);
1758 /* Update the location of the decl. */
1759 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1760 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1762 if (TYPE_BEING_DEFINED (t))
1764 t = make_lang_type (TREE_CODE (t));
1765 pushtag (TYPE_IDENTIFIER (t), t, 0);
1767 maybe_process_partial_specialization (t);
1768 pushclass (t, 1);
1769 TYPE_BEING_DEFINED (t) = 1;
1770 /* Reset the interface data, at the earliest possible
1771 moment, as it might have been set via a class foo;
1772 before. */
1774 tree name = TYPE_IDENTIFIER (t);
1776 if (! ANON_AGGRNAME_P (name))
1778 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1779 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1780 (t, interface_unknown);
1783 /* Only leave this bit clear if we know this
1784 class is part of an interface-only specification. */
1785 if (! CLASSTYPE_INTERFACE_KNOWN (t)
1786 || ! CLASSTYPE_INTERFACE_ONLY (t))
1787 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
1789 #if 0
1790 tmp = TYPE_IDENTIFIER ($<ttype>0);
1791 if (tmp && IDENTIFIER_TEMPLATE (tmp))
1792 overload_template_name (tmp, 1);
1793 #endif
1794 reset_specialization();
1796 /* In case this is a local class within a template
1797 function, we save the current tree structure so
1798 that we can get it back later. */
1799 begin_tree ();
1801 /* Make a declaration for this class in its own scope. */
1802 build_self_reference ();
1804 return t;
1807 /* Finish the member declaration given by DECL. */
1809 void
1810 finish_member_declaration (decl)
1811 tree decl;
1813 if (decl == error_mark_node || decl == NULL_TREE)
1814 return;
1816 if (decl == void_type_node)
1817 /* The COMPONENT was a friend, not a member, and so there's
1818 nothing for us to do. */
1819 return;
1821 /* We should see only one DECL at a time. */
1822 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1824 /* Set up access control for DECL. */
1825 TREE_PRIVATE (decl)
1826 = (current_access_specifier == access_private_node);
1827 TREE_PROTECTED (decl)
1828 = (current_access_specifier == access_protected_node);
1829 if (TREE_CODE (decl) == TEMPLATE_DECL)
1831 TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1832 TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1835 /* Mark the DECL as a member of the current class. */
1836 if (TREE_CODE (decl) == FUNCTION_DECL
1837 || DECL_FUNCTION_TEMPLATE_P (decl))
1838 /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in
1839 finish_struct. Presumably it is already set as the function is
1840 parsed. Perhaps DECL_CLASS_CONTEXT is already set, too? */
1841 DECL_CLASS_CONTEXT (decl) = current_class_type;
1842 else
1843 DECL_CONTEXT (decl) = current_class_type;
1845 /* Put functions on the TYPE_METHODS list and everything else on the
1846 TYPE_FIELDS list. Note that these are built up in reverse order.
1847 We reverse them (to obtain declaration order) in finish_struct. */
1848 if (TREE_CODE (decl) == FUNCTION_DECL
1849 || DECL_FUNCTION_TEMPLATE_P (decl))
1851 /* We also need to add this function to the
1852 CLASSTYPE_METHOD_VEC. */
1853 add_method (current_class_type, 0, decl);
1855 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1856 TYPE_METHODS (current_class_type) = decl;
1858 else
1860 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1861 go at the beginning. The reason is that lookup_field_1
1862 searches the list in order, and we want a field name to
1863 override a type name so that the "struct stat hack" will
1864 work. In particular:
1866 struct S { enum E { }; int E } s;
1867 s.E = 3;
1869 is legal. In addition, the FIELD_DECLs must be maintained in
1870 declaration order so that class layout works as expected.
1871 However, we don't need that order until class layout, so we
1872 save a little time by putting FIELD_DECLs on in reverse order
1873 here, and then reversing them in finish_struct_1. (We could
1874 also keep a pointer to the correct insertion points in the
1875 list.) */
1877 if (TREE_CODE (decl) == TYPE_DECL)
1878 TYPE_FIELDS (current_class_type)
1879 = chainon (TYPE_FIELDS (current_class_type), decl);
1880 else
1882 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1883 TYPE_FIELDS (current_class_type) = decl;
1886 /* Enter the DECL into the scope of the class. */
1887 if (TREE_CODE (decl) != USING_DECL)
1888 pushdecl_class_level (decl);
1892 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1893 the definition is immediately followed by a semicolon. Returns the
1894 type. */
1896 tree
1897 finish_class_definition (t, attributes, semi, pop_scope_p)
1898 tree t;
1899 tree attributes;
1900 int semi;
1901 int pop_scope_p;
1903 /* finish_struct nukes this anyway; if finish_exception does too,
1904 then it can go. */
1905 if (semi)
1906 note_got_semicolon (t);
1908 /* If we got any attributes in class_head, xref_tag will stick them in
1909 TREE_TYPE of the type. Grab them now. */
1910 attributes = chainon (TREE_TYPE (t), attributes);
1911 TREE_TYPE (t) = NULL_TREE;
1913 if (TREE_CODE (t) == ENUMERAL_TYPE)
1915 else
1917 t = finish_struct (t, attributes);
1918 if (semi)
1919 note_got_semicolon (t);
1922 pop_obstacks ();
1924 if (! semi)
1925 check_for_missing_semicolon (t);
1926 if (pop_scope_p)
1927 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1928 if (current_scope () == current_function_decl)
1929 do_pending_defargs ();
1931 return t;
1934 /* Finish processing the default argument expressions cached during
1935 the processing of a class definition. */
1937 void
1938 begin_inline_definitions ()
1940 if (pending_inlines
1941 && current_scope () == current_function_decl)
1942 do_pending_inlines ();
1945 /* Finish processing the inline function definitions cached during the
1946 processing of a class definition. */
1948 void
1949 finish_inline_definitions ()
1951 if (current_class_type == NULL_TREE)
1952 clear_inline_text_obstack ();
1954 /* Undo the begin_tree in begin_class_definition. */
1955 end_tree ();
1958 /* Finish processing the declaration of a member class template
1959 TYPES whose template parameters are given by PARMS. */
1961 tree
1962 finish_member_class_template (types)
1963 tree types;
1965 tree t;
1967 /* If there are declared, but undefined, partial specializations
1968 mixed in with the typespecs they will not yet have passed through
1969 maybe_process_partial_specialization, so we do that here. */
1970 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1971 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1972 maybe_process_partial_specialization (TREE_VALUE (t));
1974 note_list_got_semicolon (types);
1975 grok_x_components (types);
1976 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1977 /* The component was in fact a friend declaration. We avoid
1978 finish_member_template_decl performing certain checks by
1979 unsetting TYPES. */
1980 types = NULL_TREE;
1982 finish_member_template_decl (types);
1984 /* As with other component type declarations, we do
1985 not store the new DECL on the list of
1986 component_decls. */
1987 return NULL_TREE;
1990 /* Finish processsing a complete template declaration. The PARMS are
1991 the template parameters. */
1993 void
1994 finish_template_decl (parms)
1995 tree parms;
1997 if (parms)
1998 end_template_decl ();
1999 else
2000 end_specialization ();
2003 /* Finish processing a a template-id (which names a type) of the form
2004 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2005 template-id. If ENTERING_SCOPE is non-zero we are about to enter
2006 the scope of template-id indicated. */
2008 tree
2009 finish_template_type (name, args, entering_scope)
2010 tree name;
2011 tree args;
2012 int entering_scope;
2014 tree decl;
2016 decl = lookup_template_class (name, args,
2017 NULL_TREE, NULL_TREE, entering_scope);
2018 if (decl != error_mark_node)
2019 decl = TYPE_STUB_DECL (decl);
2021 return decl;
2024 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2025 namespace scope or a class scope. */
2027 void
2028 enter_scope_of (sr)
2029 tree sr;
2031 tree scope = TREE_OPERAND (sr, 0);
2033 if (TREE_CODE (scope) == NAMESPACE_DECL)
2035 push_decl_namespace (scope);
2036 TREE_COMPLEXITY (sr) = -1;
2038 else if (scope != current_class_type)
2040 if (TREE_CODE (scope) == TYPENAME_TYPE)
2042 /* In a declarator for a template class member, the scope will
2043 get here as an implicit typename, a TYPENAME_TYPE with a type. */
2044 scope = TREE_TYPE (scope);
2045 TREE_OPERAND (sr, 0) = scope;
2047 push_nested_class (scope, 3);
2048 TREE_COMPLEXITY (sr) = current_class_depth;
2052 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2053 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2054 BASE_CLASS, or NULL_TREE if an error occurred. The
2055 ACCESSS_SPECIFIER is one of
2056 access_{default,public,protected_private}[_virtual]_node.*/
2058 tree
2059 finish_base_specifier (access_specifier, base_class)
2060 tree access_specifier;
2061 tree base_class;
2063 tree type;
2064 tree result;
2066 if (base_class == NULL_TREE)
2068 error ("invalid base class");
2069 type = error_mark_node;
2071 else
2072 type = TREE_TYPE (base_class);
2074 if (! is_aggr_type (type, 1))
2075 result = NULL_TREE;
2076 else
2077 result = build_tree_list (access_specifier, type);
2079 return result;
2082 /* Called when multiple declarators are processed. If that is not
2083 premitted in this context, an error is issued. */
2085 void
2086 check_multiple_declarators ()
2088 /* [temp]
2090 In a template-declaration, explicit specialization, or explicit
2091 instantiation the init-declarator-list in the declaration shall
2092 contain at most one declarator.
2094 We don't just use PROCESSING_TEMPLATE_DECL for the first
2095 condition since that would disallow the perfectly legal code,
2096 like `template <class T> struct S { int i, j; };'. */
2097 tree scope = current_scope ();
2099 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
2100 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2101 return;
2103 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2104 || processing_explicit_instantiation
2105 || processing_specialization)
2106 cp_error ("multiple declarators in template declaration");
2109 tree
2110 finish_typeof (expr)
2111 tree expr;
2113 if (processing_template_decl)
2115 tree t;
2117 push_permanent_obstack ();
2118 t = make_lang_type (TYPEOF_TYPE);
2119 TYPE_FIELDS (t) = expr;
2120 pop_obstacks ();
2122 return t;
2125 return TREE_TYPE (expr);
2128 /* Create an empty statement tree for FN. */
2130 void
2131 begin_stmt_tree (fn)
2132 tree fn;
2134 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
2135 what follows. We remove the extraneous statement in
2136 finish_stmt_tree. */
2137 DECL_SAVED_TREE (fn) = build_nt (EXPR_STMT, void_zero_node);
2138 last_tree = DECL_SAVED_TREE (fn);
2139 last_expr_type = NULL_TREE;
2142 /* Finish the statement tree for FN. */
2144 void
2145 finish_stmt_tree (fn)
2146 tree fn;
2148 tree stmt;
2150 /* Remove the fake extra statement added in begin_stmt_tree. */
2151 stmt = TREE_CHAIN (DECL_SAVED_TREE (fn));
2152 DECL_SAVED_TREE (fn) = stmt;
2154 /* The line-number recorded in the outermost statement in a function
2155 is the line number of the end of the function. */
2156 STMT_LINENO (stmt) = lineno;
2157 STMT_LINENO_FOR_FN_P (stmt) = 1;
2160 /* We're about to expand T, a statement. Set up appropriate context
2161 for the substitution. */
2163 void
2164 prep_stmt (t)
2165 tree t;
2167 if (!STMT_LINENO_FOR_FN_P (t))
2168 lineno = STMT_LINENO (t);
2169 stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
2172 /* Some statements, like for-statements or if-statements, require a
2173 condition. This condition can be a declaration. If T is such a
2174 declaration it is processed, and an expression appropriate to use
2175 as the condition is returned. Otherwise, T itself is returned. */
2177 static tree
2178 expand_cond (t)
2179 tree t;
2181 if (t && TREE_CODE (t) == TREE_LIST)
2183 expand_stmt (TREE_PURPOSE (t));
2184 return TREE_VALUE (t);
2186 else
2187 return t;
2190 /* Generate RTL for the statement T, and its substatements, and any
2191 other statements at its nesting level. */
2193 tree
2194 expand_stmt (t)
2195 tree t;
2197 tree rval;
2199 while (t && t != error_mark_node)
2201 int saved_stmts_are_full_exprs_p;
2203 /* Assume we'll have nothing to return. */
2204 rval = NULL_TREE;
2206 /* Set up context appropriately for handling this statement. */
2207 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p;
2208 prep_stmt (t);
2210 switch (TREE_CODE (t))
2212 case RETURN_STMT:
2213 finish_return_stmt (RETURN_EXPR (t));
2214 break;
2216 case EXPR_STMT:
2217 finish_expr_stmt_real (EXPR_STMT_EXPR (t),
2218 EXPR_STMT_ASSIGNS_THIS (t));
2219 break;
2221 case DECL_STMT:
2223 tree decl;
2224 int i = suspend_momentary ();
2226 emit_line_note (input_filename, lineno);
2227 decl = DECL_STMT_DECL (t);
2228 /* If this is a declaration for an automatic local
2229 variable, initialize it. Note that we might also see a
2230 declaration for a namespace-scope object (declared with
2231 `extern') or an object with static storage duration
2232 (declared with `static'). We don't have to handle the
2233 initialization of those objects here; the former can
2234 never be a definition (only a declaration), and the
2235 latter is handled in finish_file. */
2236 if (TREE_CODE (decl) == VAR_DECL
2237 && !TREE_STATIC (decl)
2238 && !DECL_EXTERNAL (decl))
2239 /* Let the back-end know about this variable. */
2240 emit_local_var (decl);
2242 resume_momentary (i);
2244 break;
2246 case CLEANUP_STMT:
2247 finish_decl_cleanup (CLEANUP_DECL (t), CLEANUP_EXPR (t));
2248 break;
2250 case START_CATCH_STMT:
2251 begin_catch_block (TREE_TYPE (t));
2252 break;
2254 case FOR_STMT:
2256 tree tmp;
2258 begin_for_stmt ();
2259 expand_stmt (FOR_INIT_STMT (t));
2260 finish_for_init_stmt (NULL_TREE);
2261 finish_for_cond (expand_cond (FOR_COND (t)), NULL_TREE);
2262 tmp = FOR_EXPR (t);
2263 finish_for_expr (tmp, NULL_TREE);
2264 expand_stmt (FOR_BODY (t));
2265 finish_for_stmt (tmp, NULL_TREE);
2267 break;
2269 case WHILE_STMT:
2271 begin_while_stmt ();
2272 finish_while_stmt_cond (expand_cond (WHILE_COND (t)), NULL_TREE);
2273 expand_stmt (WHILE_BODY (t));
2274 finish_while_stmt (NULL_TREE);
2276 break;
2278 case DO_STMT:
2280 begin_do_stmt ();
2281 expand_stmt (DO_BODY (t));
2282 finish_do_body (NULL_TREE);
2283 finish_do_stmt (DO_COND (t), NULL_TREE);
2285 break;
2287 case IF_STMT:
2288 begin_if_stmt ();
2289 finish_if_stmt_cond (expand_cond (IF_COND (t)), NULL_TREE);
2290 if (THEN_CLAUSE (t))
2292 expand_stmt (THEN_CLAUSE (t));
2293 finish_then_clause (NULL_TREE);
2295 if (ELSE_CLAUSE (t))
2297 begin_else_clause ();
2298 expand_stmt (ELSE_CLAUSE (t));
2299 finish_else_clause (NULL_TREE);
2301 finish_if_stmt ();
2302 break;
2304 case COMPOUND_STMT:
2305 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
2306 expand_stmt (COMPOUND_BODY (t));
2307 rval = finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
2308 NULL_TREE);
2309 break;
2311 case BREAK_STMT:
2312 finish_break_stmt ();
2313 break;
2315 case CONTINUE_STMT:
2316 finish_continue_stmt ();
2317 break;
2319 case SWITCH_STMT:
2321 tree cond;
2323 begin_switch_stmt ();
2324 cond = expand_cond (SWITCH_COND (t));
2325 finish_switch_cond (cond, NULL_TREE);
2326 expand_stmt (SWITCH_BODY (t));
2327 finish_switch_stmt (cond, NULL_TREE);
2329 break;
2331 case CASE_LABEL:
2332 finish_case_label (CASE_LOW (t), CASE_HIGH (t));
2333 break;
2335 case LABEL_STMT:
2336 expand_label (LABEL_STMT_LABEL (t));
2337 break;
2339 case GOTO_STMT:
2340 finish_goto_stmt (GOTO_DESTINATION (t));
2341 break;
2343 case ASM_STMT:
2344 finish_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t), ASM_OUTPUTS
2345 (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
2346 break;
2348 case TRY_BLOCK:
2349 if (CLEANUP_P (t))
2351 expand_eh_region_start ();
2352 expand_stmt (TRY_STMTS (t));
2353 finish_cleanup_try_block (NULL_TREE);
2354 finish_cleanup (TRY_HANDLERS (t), NULL_TREE);
2356 else
2358 if (FN_TRY_BLOCK_P (t))
2359 begin_function_try_block ();
2360 else
2361 begin_try_block ();
2363 expand_stmt (TRY_STMTS (t));
2365 if (FN_TRY_BLOCK_P (t))
2367 finish_function_try_block (NULL_TREE);
2368 expand_stmt (TRY_HANDLERS (t));
2369 finish_function_handler_sequence (NULL_TREE);
2371 else
2373 finish_try_block (NULL_TREE);
2374 expand_stmt (TRY_HANDLERS (t));
2375 finish_handler_sequence (NULL_TREE);
2378 break;
2380 case HANDLER:
2381 begin_handler ();
2382 expand_stmt (HANDLER_BODY (t));
2383 finish_handler (NULL_TREE, NULL_TREE);
2384 break;
2386 case SUBOBJECT:
2387 finish_subobject (SUBOBJECT_CLEANUP (t));
2388 break;
2390 case SCOPE_STMT:
2391 if (SCOPE_BEGIN_P (t))
2392 expand_start_bindings (2 * SCOPE_NULLIFIED_P (t));
2393 else if (SCOPE_END_P (t))
2394 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
2395 break;
2397 case CTOR_INITIALIZER:
2398 current_member_init_list = TREE_OPERAND (t, 0);
2399 current_base_init_list = TREE_OPERAND (t, 1);
2400 setup_vtbl_ptr ();
2401 break;
2403 case RETURN_INIT:
2404 /* Clear this out so that finish_named_return_value can set it
2405 again. */
2406 DECL_NAME (DECL_RESULT (current_function_decl)) = NULL_TREE;
2407 finish_named_return_value (TREE_OPERAND (t, 0),
2408 TREE_OPERAND (t, 1));
2409 break;
2411 default:
2412 my_friendly_abort (19990810);
2413 break;
2416 /* Restore saved state. */
2417 stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p;
2419 /* Go on to the next statement in this scope. */
2420 t = TREE_CHAIN (t);
2423 return rval;
2426 /* Generate RTL for FN. */
2428 void
2429 expand_body (fn)
2430 tree fn;
2432 int saved_lineno;
2433 char *saved_input_filename;
2435 /* When the parser calls us after finishing the body of a template
2436 function, we don't really want to expand the body. When we're
2437 processing an in-class definition of an inline function,
2438 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2439 to look at the function itself. */
2440 if (processing_template_decl
2441 || (DECL_LANG_SPECIFIC (fn)
2442 && DECL_TEMPLATE_INFO (fn)
2443 && uses_template_parms (DECL_TI_ARGS (fn))))
2444 return;
2446 /* Save the current file name and line number. When we expand the
2447 body of the funciton, we'll set LINENO and INPUT_FILENAME so that
2448 error-mesages come out in the right places. */
2449 saved_lineno = lineno;
2450 saved_input_filename = input_filename;
2451 lineno = DECL_SOURCE_LINE (fn);
2452 input_filename = DECL_SOURCE_FILE (fn);
2454 start_function (NULL_TREE, fn, NULL_TREE, SF_PRE_PARSED | SF_EXPAND);
2455 store_parm_decls ();
2457 /* We don't need to redeclare __FUNCTION__, __PRETTY_FUNCTION__, or
2458 any of the other magic variables we set up when starting a
2459 function body. */
2460 current_function_name_declared = 1;
2462 /* Expand the body. */
2463 expand_stmt (DECL_SAVED_TREE (fn));
2465 /* Statements should always be full-expressions at the outermost set
2466 of curly braces for a function. */
2467 my_friendly_assert (stmts_are_full_exprs_p, 19990831);
2469 /* The outermost statement for a function contains the line number
2470 recorded when we finished processing the function. */
2471 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2473 /* Generate code for the function. */
2474 finish_function (lineno, 0);
2476 /* And restore the current source position. */
2477 lineno = saved_lineno;
2478 input_filename = saved_input_filename;