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)
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
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 /* When parsing a template, LAST_TREE contains the last statement
45 parsed. These are chained together through the TREE_CHAIN field,
46 but often need to be re-organized since the parse is performed
47 bottom-up. This macro makes LAST_TREE the indicated SUBSTMT of
50 #define RECHAIN_STMTS(stmt, substmt, last) \
53 TREE_CHAIN (stmt) = NULL_TREE; \
57 #define RECHAIN_STMTS_FROM_LAST(stmt, substmt) \
58 RECHAIN_STMTS (stmt, substmt, last_tree)
60 #define RECHAIN_STMTS_FROM_CHAIN(stmt, substmt) \
61 RECHAIN_STMTS (stmt, substmt, TREE_CHAIN (stmt))
63 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
66 finish_expr_stmt (expr)
69 if (expr != NULL_TREE)
71 if (!processing_template_decl)
73 emit_line_note (input_filename, lineno);
74 /* Do default conversion if safe and possibly important,
75 in case within ({...}). */
76 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
78 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
79 expr = default_conversion (expr);
82 cplus_expand_expr_stmt (expr);
89 /* Begin an if-statement. Returns a newly created IF_STMT if
97 if (processing_template_decl)
99 r = build_min_nt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
110 /* Process the COND of an if-statement, which may be given by
114 finish_if_stmt_cond (cond, if_stmt)
118 if (processing_template_decl)
120 if (last_tree != if_stmt)
121 RECHAIN_STMTS_FROM_LAST (if_stmt, IF_COND (if_stmt));
123 IF_COND (if_stmt) = copy_to_permanent (cond);
127 emit_line_note (input_filename, lineno);
128 expand_start_cond (condition_conversion (cond), 0);
132 /* Finish the then-clause of an if-statement, which may be given by
136 finish_then_clause (if_stmt)
139 if (processing_template_decl)
141 RECHAIN_STMTS_FROM_CHAIN (if_stmt,
142 THEN_CLAUSE (if_stmt));
150 /* Begin the else-clause of an if-statement. */
155 if (!processing_template_decl)
156 expand_start_else ();
159 /* Finish the else-clause of an if-statement, which may be given by
163 finish_else_clause (if_stmt)
166 if (processing_template_decl)
167 RECHAIN_STMTS_FROM_CHAIN (if_stmt, ELSE_CLAUSE (if_stmt));
170 /* Finsh an if-statement. */
175 if (!processing_template_decl)
182 /* Begin a while-statement. Returns a newly created WHILE_STMT if
190 if (processing_template_decl)
192 r = build_min_nt (WHILE_STMT, NULL_TREE, NULL_TREE);
198 emit_line_note (input_filename, lineno);
199 expand_start_loop (1);
208 /* Process the COND of an if-statement, which may be given by
212 finish_while_stmt_cond (cond, while_stmt)
216 if (processing_template_decl)
218 if (last_tree != while_stmt)
219 RECHAIN_STMTS_FROM_LAST (while_stmt,
220 WHILE_COND (while_stmt));
222 TREE_OPERAND (while_stmt, 0) = copy_to_permanent (cond);
226 emit_line_note (input_filename, lineno);
227 expand_exit_loop_if_false (0, condition_conversion (cond));
230 /* If COND wasn't a declaration, clear out the
231 block we made for it and start a new one here so the
232 optimization in expand_end_loop will work. */
233 if (getdecls () == NULL_TREE)
240 /* Finish a while-statement, which may be given by WHILE_STMT. */
243 finish_while_stmt (while_stmt)
248 if (processing_template_decl)
249 RECHAIN_STMTS_FROM_CHAIN (while_stmt, WHILE_BODY (while_stmt));
255 /* Begin a do-statement. Returns a newly created DO_STMT if
261 if (processing_template_decl)
263 tree r = build_min_nt (DO_STMT, NULL_TREE, NULL_TREE);
270 emit_line_note (input_filename, lineno);
271 expand_start_loop_continue_elsewhere (1);
276 /* Finish the body of a do-statement, which may be given by DO_STMT. */
279 finish_do_body (do_stmt)
282 if (processing_template_decl)
283 RECHAIN_STMTS_FROM_CHAIN (do_stmt, DO_BODY (do_stmt));
285 expand_loop_continue_here ();
288 /* Finish a do-statement, which may be given by DO_STMT, and whose
289 COND is as indicated. */
292 finish_do_stmt (cond, do_stmt)
296 if (processing_template_decl)
297 DO_COND (do_stmt) = copy_to_permanent (cond);
300 emit_line_note (input_filename, lineno);
301 expand_exit_loop_if_false (0, condition_conversion (cond));
309 /* Finish a return-statement. The EXPRESSION returned, if any, is as
313 finish_return_stmt (expr)
316 emit_line_note (input_filename, lineno);
317 c_expand_return (expr);
321 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
328 if (processing_template_decl)
330 r = build_min_nt (FOR_STMT, NULL_TREE, NULL_TREE,
331 NULL_TREE, NULL_TREE);
337 if (flag_new_for_scope > 0)
340 note_level_for_for ();
346 /* Finish the for-init-statement of a for-statement, which may be
347 given by FOR_STMT. */
350 finish_for_init_stmt (for_stmt)
353 if (processing_template_decl)
355 if (last_tree != for_stmt)
356 RECHAIN_STMTS_FROM_CHAIN (for_stmt, FOR_INIT_STMT (for_stmt));
361 emit_line_note (input_filename, lineno);
362 expand_start_loop_continue_elsewhere (1);
368 /* Finish the COND of a for-statement, which may be given by
372 finish_for_cond (cond, for_stmt)
376 if (processing_template_decl)
378 if (last_tree != for_stmt)
379 RECHAIN_STMTS_FROM_LAST (for_stmt, FOR_COND (for_stmt));
381 FOR_COND (for_stmt) = copy_to_permanent (cond);
385 emit_line_note (input_filename, lineno);
387 expand_exit_loop_if_false (0, condition_conversion (cond));
390 /* If the cond wasn't a declaration, clear out the
391 block we made for it and start a new one here so the
392 optimization in expand_end_loop will work. */
393 if (getdecls () == NULL_TREE)
400 /* Finish the increment-EXPRESSION in a for-statement, which may be
401 given by FOR_STMT. */
404 finish_for_expr (expr, for_stmt)
408 if (processing_template_decl)
409 FOR_EXPR (for_stmt) = expr;
411 /* Don't let the tree nodes for EXPR be discarded
412 by clear_momentary during the parsing of the next stmt. */
416 /* Finish the body of a for-statement, which may be given by
417 FOR_STMT. The increment-EXPR for the loop must be
421 finish_for_stmt (expr, for_stmt)
425 /* Pop the scope for the body of the loop. */
428 if (processing_template_decl)
429 RECHAIN_STMTS_FROM_CHAIN (for_stmt, FOR_BODY (for_stmt));
432 emit_line_note (input_filename, lineno);
433 expand_loop_continue_here ();
435 cplus_expand_expr_stmt (expr);
441 if (flag_new_for_scope > 0)
447 /* Finish a break-statement. */
452 emit_line_note (input_filename, lineno);
453 if (processing_template_decl)
454 add_tree (build_min_nt (BREAK_STMT));
455 else if ( ! expand_exit_something ())
456 cp_error ("break statement not within loop or switch");
459 /* Finish a continue-statement. */
462 finish_continue_stmt ()
464 emit_line_note (input_filename, lineno);
465 if (processing_template_decl)
466 add_tree (build_min_nt (CONTINUE_STMT));
467 else if (! expand_continue_loop (0))
468 cp_error ("continue statement not within a loop");
471 /* Begin a switch-statement. */
479 /* Finish the cond of a switch-statement. Returns a new
480 SWITCH_STMT if appropriate. */
483 finish_switch_cond (cond)
488 if (processing_template_decl)
490 r = build_min_nt (SWITCH_STMT, cond, NULL_TREE);
493 else if (cond != error_mark_node)
495 emit_line_note (input_filename, lineno);
496 c_expand_start_case (cond);
501 /* The code is in error, but we don't want expand_end_case to
503 c_expand_start_case (boolean_false_node);
509 /* Don't let the tree nodes for COND be discarded by
510 clear_momentary during the parsing of the next stmt. */
516 /* Finish the body of a switch-statement, which may be given by
517 SWITCH_STMT. The COND to switch on is indicated. */
520 finish_switch_stmt (cond, switch_stmt)
524 if (processing_template_decl)
525 RECHAIN_STMTS_FROM_CHAIN (switch_stmt, SWITCH_BODY (switch_stmt));
527 expand_end_case (cond);
534 /* Finish a case-label. */
537 finish_case_label (low_value, high_value)
541 do_case (low_value, high_value);
545 /* Finish a goto-statement. */
548 finish_goto_stmt (destination)
551 if (processing_template_decl)
552 add_tree (build_min_nt (GOTO_STMT, destination));
555 emit_line_note (input_filename, lineno);
557 if (TREE_CODE (destination) == IDENTIFIER_NODE)
559 tree decl = lookup_label (destination);
560 TREE_USED (decl) = 1;
564 expand_computed_goto (destination);
568 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
574 if (processing_template_decl)
576 tree r = build_min_nt (TRY_BLOCK, NULL_TREE,
583 emit_line_note (input_filename, lineno);
584 expand_start_try_stmts ();
589 /* Finish a try-block, which may be given by TRY_BLOCK. */
592 finish_try_block (try_block)
595 if (processing_template_decl)
596 RECHAIN_STMTS_FROM_LAST (try_block, TRY_STMTS (try_block));
599 expand_start_all_catch ();
603 /* Finish a handler-sequence for a try-block, which may be given by
607 finish_handler_sequence (try_block)
610 if (processing_template_decl)
611 RECHAIN_STMTS_FROM_CHAIN (try_block, TRY_HANDLERS (try_block));
614 expand_end_all_catch ();
618 /* Begin a handler. Returns a HANDLER if appropriate. */
625 if (processing_template_decl)
627 r = build_min_nt (HANDLER, NULL_TREE, NULL_TREE);
638 /* Finish the handler-parameters for a handler, which may be given by
642 finish_handler_parms (handler)
645 if (processing_template_decl)
646 RECHAIN_STMTS_FROM_CHAIN (handler, HANDLER_PARMS (handler));
649 /* Finish a handler, which may be given by HANDLER. */
652 finish_handler (handler)
655 if (processing_template_decl)
656 RECHAIN_STMTS_FROM_CHAIN (handler, HANDLER_BODY (handler));
658 expand_end_catch_block ();
663 /* Begin a compound-statement. If HAS_NO_SCOPE is non-zero, the
664 compound-statement does not define a scope. Returns a new
665 COMPOUND_STMT if appropriate. */
668 begin_compound_stmt (has_no_scope)
673 if (processing_template_decl)
675 r = build_min_nt (COMPOUND_STMT, NULL_TREE);
678 COMPOUND_STMT_NO_SCOPE (r) = 1;
690 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
691 If HAS_NO_SCOPE is non-zero, the compound statement does not define
695 finish_compound_stmt (has_no_scope, compound_stmt)
706 if (processing_template_decl)
707 RECHAIN_STMTS_FROM_CHAIN (compound_stmt,
708 COMPOUND_BODY (compound_stmt));
715 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
716 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
720 finish_asm_stmt (cv_qualifier, string, output_operands,
721 input_operands, clobbers)
724 tree output_operands;
728 if (TREE_CHAIN (string))
729 string = combine_strings (string);
731 if (processing_template_decl)
733 tree r = build_min_nt (ASM_STMT, cv_qualifier, string,
734 output_operands, input_operands,
740 emit_line_note (input_filename, lineno);
741 if (output_operands != NULL_TREE || input_operands != NULL_TREE
742 || clobbers != NULL_TREE)
746 if (cv_qualifier != NULL_TREE
747 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
748 cp_warning ("%s qualifier ignored on asm",
749 IDENTIFIER_POINTER (cv_qualifier));
751 for (t = input_operands; t; t = TREE_CHAIN (t))
752 TREE_VALUE (t) = decay_conversion (TREE_VALUE (t));
754 c_expand_asm_operands (string, output_operands,
758 == ridpointers[(int) RID_VOLATILE],
759 input_filename, lineno);
763 /* Don't warn about redundant specification of 'volatile' here. */
764 if (cv_qualifier != NULL_TREE
765 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
766 cp_warning ("%s qualifier ignored on asm",
767 IDENTIFIER_POINTER (cv_qualifier));
775 /* Finish a parenthesized expression EXPR. */
778 finish_parenthesized_expr (expr)
781 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
782 /* This inhibits warnings in truthvalue_conversion. */
783 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
788 /* Begin a statement-expression. The value returned must be passed to
795 /* If we're processing_template_decl, then the upcoming compound
796 statement will be chained onto the tree structure, starting at
797 last_tree. We return last_tree so that we can later unhook the
798 compound statement. */
799 return processing_template_decl ? last_tree : expand_start_stmt_expr();
802 /* Finish a statement-expression. RTL_EXPR should be the value
803 returned by the previous begin_stmt_expr; EXPR is the
804 statement-expression. Returns an expression representing the
805 statement-expression. */
808 finish_stmt_expr (rtl_expr, expr)
814 if (!processing_template_decl)
816 rtl_expr = expand_end_stmt_expr (rtl_expr);
817 /* The statements have side effects, so the group does. */
818 TREE_SIDE_EFFECTS (rtl_expr) = 1;
821 if (TREE_CODE (expr) == BLOCK)
823 /* Make a BIND_EXPR for the BLOCK already made. */
824 if (processing_template_decl)
825 result = build_min_nt (BIND_EXPR, NULL_TREE, last_tree,
828 result = build (BIND_EXPR, TREE_TYPE (rtl_expr),
829 NULL_TREE, rtl_expr, expr);
831 /* Remove the block from the tree at this point.
832 It gets put back at the proper place
833 when the BIND_EXPR is expanded. */
839 if (processing_template_decl)
841 /* Remove the compound statement from the tree structure; it is
842 now saved in the BIND_EXPR. */
843 last_tree = rtl_expr;
844 TREE_CHAIN (last_tree) = NULL_TREE;
850 /* Finish a call to FN with ARGS. Returns a representation of the
854 finish_call_expr (fn, args, koenig)
863 if (TREE_CODE (fn) == BIT_NOT_EXPR)
864 fn = build_x_unary_op (BIT_NOT_EXPR, TREE_OPERAND (fn, 0));
865 else if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
866 fn = do_identifier (fn, 2, args);
868 result = build_x_function_call (fn, args, current_class_ref);
870 if (TREE_CODE (result) == CALL_EXPR
871 && (! TREE_TYPE (result)
872 || TREE_CODE (TREE_TYPE (result)) != VOID_TYPE))
873 result = require_complete_type (result);
878 /* Finish a call to a postfix increment or decrement or EXPR. (Which
879 is indicated by CODE, which should be POSTINCREMENT_EXPR or
880 POSTDECREMENT_EXPR.) */
883 finish_increment_expr (expr, code)
887 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
888 a COMPONENT_REF). This way if we've got, say, a reference to a
889 static member that's being operated on, we don't end up trying to
890 find a member operator for the class it's in. */
892 if (TREE_CODE (expr) == OFFSET_REF)
893 expr = resolve_offset_ref (expr);
894 return build_x_unary_op (code, expr);
897 /* Finish a use of `this'. Returns an expression for `this'. */
904 if (current_class_ptr)
906 #ifdef WARNING_ABOUT_CCD
907 TREE_USED (current_class_ptr) = 1;
909 result = current_class_ptr;
911 else if (current_function_decl
912 && DECL_STATIC_FUNCTION_P (current_function_decl))
914 error ("`this' is unavailable for static member functions");
915 result = error_mark_node;
919 if (current_function_decl)
920 error ("invalid use of `this' in non-member function");
922 error ("invalid use of `this' at top level");
923 result = error_mark_node;
929 /* Finish a member function call using OBJECT and ARGS as arguments to
930 FN. Returns an expression for the call. */
933 finish_object_call_expr (fn, object, args)
939 /* This is a future direction of this code, but because
940 build_x_function_call cannot always undo what is done in
941 build_component_ref entirely yet, we cannot do this. */
943 tree real_fn = build_component_ref (object, fn, NULL_TREE, 1);
944 return finish_call_expr (real_fn, args);
946 if (TREE_CODE (fn) == TYPE_DECL)
948 if (processing_template_decl)
949 /* This can happen on code like:
952 template <class T> void f(T t) {
956 We just grab the underlying IDENTIFIER. */
960 cp_error ("calling type `%T' like a method", fn);
961 return error_mark_node;
965 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
969 /* Finish a qualified member function call using OBJECT and ARGS as
970 arguments to FN. Returns an expressino for the call. */
973 finish_qualified_object_call_expr (fn, object, args)
978 if (IS_SIGNATURE (TREE_OPERAND (fn, 0)))
980 warning ("signature name in scope resolution ignored");
981 return finish_object_call_expr (TREE_OPERAND (fn, 1), object, args);
984 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
985 TREE_OPERAND (fn, 1), args);
988 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
989 being the scope, if any, of DESTRUCTOR. Returns an expression for
993 finish_pseudo_destructor_call_expr (object, scope, destructor)
998 if (scope && scope != destructor)
999 cp_error ("destructor specifier `%T::~%T()' must have matching names",
1002 if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1003 && (TREE_CODE (TREE_TYPE (object)) !=
1004 TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1005 cp_error ("`%E' is not of type `%T'", object, destructor);
1007 return cp_convert (void_type_node, object);
1010 /* Finish a call to a globally qualified member function FN using
1011 ARGS. Returns an expression for the call. */
1014 finish_qualified_call_expr (fn, args)
1018 if (processing_template_decl)
1019 return build_min_nt (CALL_EXPR, copy_to_permanent (fn), args,
1022 return build_member_call (TREE_OPERAND (fn, 0),
1023 TREE_OPERAND (fn, 1),
1027 /* Finish an expression taking the address of LABEL. Returns an
1028 expression for the address. */
1031 finish_label_address_expr (label)
1036 label = lookup_label (label);
1037 if (label == NULL_TREE)
1038 result = null_pointer_node;
1041 TREE_USED (label) = 1;
1042 result = build1 (ADDR_EXPR, ptr_type_node, label);
1043 TREE_CONSTANT (result) = 1;
1049 /* Finish an expression of the form CODE EXPR. */
1052 finish_unary_op_expr (code, expr)
1053 enum tree_code code;
1056 tree result = build_x_unary_op (code, expr);
1057 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST)
1058 TREE_NEGATED_INT (result) = 1;
1059 overflow_warning (result);
1063 /* Finish an id-expression. */
1066 finish_id_expr (expr)
1069 if (TREE_CODE (expr) == IDENTIFIER_NODE)
1070 expr = do_identifier (expr, 1, NULL_TREE);
1075 /* Begin a new-placement. */
1078 begin_new_placement ()
1080 /* The arguments to a placement new might be passed to a
1081 deallocation function, in the event that the allocation throws an
1082 exception. Since we don't expand exception handlers until the
1083 end of a function, we must make sure the arguments stay around
1085 return suspend_momentary ();
1088 /* Finish a new-placement. The ARGS are the placement arguments. The
1089 COOKIE is the value returned by the previous call to
1090 begin_new_placement. */
1093 finish_new_placement (args, cookie)
1097 resume_momentary (cookie);
1101 /* Begin a function defniition declared with DECL_SPECS and
1102 DECLARATOR. Returns non-zero if the function-declaration is
1106 begin_function_definition (decl_specs, declarator)
1112 split_specs_attrs (decl_specs, &specs, &attrs);
1113 if (!start_function (specs, declarator, attrs, 0))
1116 reinit_parse_for_function ();
1117 /* The things we're about to see are not directly qualified by any
1118 template headers we've seen thus far. */
1119 reset_specialization ();
1124 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1128 begin_constructor_declarator (scope, name)
1132 tree result = build_parse_node (SCOPE_REF, scope, name);
1133 enter_scope_of (result);
1137 /* Finish an init-declarator. Returns a DECL. */
1140 finish_declarator (declarator, declspecs, attributes,
1141 prefix_attributes, initialized)
1145 tree prefix_attributes;
1148 return start_decl (declarator, declspecs, initialized, attributes,
1152 /* Finish a translation unit. */
1155 finish_translation_unit ()
1157 /* In case there were missing closebraces,
1158 get us back to the global binding level. */
1159 while (! toplevel_bindings_p ())
1161 while (current_namespace != global_namespace)
1166 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1167 Returns the parameter. */
1170 finish_template_type_parm (aggr, identifier)
1174 if (aggr == signature_type_node)
1175 sorry ("signature as template type parameter");
1176 else if (aggr != class_type_node)
1178 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1179 aggr = class_type_node;
1182 return build_tree_list (aggr, identifier);
1185 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1186 Returns the parameter. */
1189 finish_template_template_parm (aggr, identifier)
1193 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1194 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1195 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1196 DECL_TEMPLATE_RESULT (tmpl) = decl;
1197 SET_DECL_ARTIFICIAL (decl);
1198 end_template_decl ();
1200 return finish_template_type_parm (aggr, tmpl);
1203 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1204 non-zero, the parameter list was terminated by a `...'. */
1207 finish_parmlist (parms, ellipsis)
1212 chainon (parms, void_list_node);
1213 /* We mark the PARMS as a parmlist so that declarator processing can
1214 disambiguate certain constructs. */
1215 if (parms != NULL_TREE)
1216 TREE_PARMLIST (parms) = 1;
1221 /* Begin a class definition, as indicated by T. */
1224 begin_class_definition (t)
1227 push_obstacks_nochange ();
1228 end_temporary_allocation ();
1230 if (t == error_mark_node
1231 || ! IS_AGGR_TYPE (t))
1233 t = make_lang_type (RECORD_TYPE);
1234 pushtag (make_anon_name (), t, 0);
1237 /* In a definition of a member class template, we will get here with an
1238 implicit typename, a TYPENAME_TYPE with a type. */
1239 if (TREE_CODE (t) == TYPENAME_TYPE)
1242 /* If we generated a partial instantiation of this type, but now
1243 we're seeing a real definition, we're actually looking at a
1244 partial specialization. Consider:
1246 template <class T, class U>
1252 template <class T, class U>
1255 typename X<Y<T, U> >::A a;
1258 template <class T, class U>
1263 We have to undo the effects of the previous partial
1265 if (PARTIAL_INSTANTIATION_P (t))
1269 /* Unfortunately, when we're not in pedantic mode, we
1270 attempt to actually fill in some of the fields of the
1271 partial instantiation, in order to support the implicit
1272 typename extension. Clear those fields now, in
1273 preparation for the definition here. The fields cleared
1274 here must match those set in instantiate_class_template.
1275 Look for a comment mentioning begin_class_definition
1277 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1278 TYPE_FIELDS (t) = NULL_TREE;
1279 TYPE_METHODS (t) = NULL_TREE;
1280 CLASSTYPE_TAGS (t) = NULL_TREE;
1281 TYPE_SIZE (t) = NULL_TREE;
1284 /* This isn't a partial instantiation any more. */
1285 PARTIAL_INSTANTIATION_P (t) = 0;
1287 /* If this type was already complete, and we see another definition,
1289 else if (TYPE_SIZE (t))
1290 duplicate_tag_error (t);
1292 if (TYPE_BEING_DEFINED (t))
1294 t = make_lang_type (TREE_CODE (t));
1295 pushtag (TYPE_IDENTIFIER (t), t, 0);
1297 maybe_process_partial_specialization (t);
1299 TYPE_BEING_DEFINED (t) = 1;
1300 /* Reset the interface data, at the earliest possible
1301 moment, as it might have been set via a class foo;
1303 /* Don't change signatures. */
1304 if (! IS_SIGNATURE (t))
1307 tree name = TYPE_IDENTIFIER (t);
1309 if (! ANON_AGGRNAME_P (name))
1311 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1312 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1313 (t, interface_unknown);
1316 /* Record how to set the access of this class's
1317 virtual functions. If write_virtuals == 3, then
1318 inline virtuals are ``extern inline''. */
1319 if (write_virtuals == 3)
1320 needs_writing = ! CLASSTYPE_INTERFACE_ONLY (t)
1321 && CLASSTYPE_INTERFACE_KNOWN (t);
1324 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = needs_writing;
1327 tmp = TYPE_IDENTIFIER ($<ttype>0);
1328 if (tmp && IDENTIFIER_TEMPLATE (tmp))
1329 overload_template_name (tmp, 1);
1331 reset_specialization();
1333 /* In case this is a local class within a template
1334 function, we save the current tree structure so
1335 that we can get it back later. */
1338 /* Make a declaration for this class in its own scope. */
1339 build_self_reference ();
1344 /* Finish the member declaration given by DECL. */
1347 finish_member_declaration (decl)
1350 if (decl == error_mark_node || decl == NULL_TREE)
1353 if (decl == void_type_node)
1354 /* The COMPONENT was a friend, not a member, and so there's
1355 nothing for us to do. */
1358 /* We should see only one DECL at a time. */
1359 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1361 /* Set up access control for DECL. */
1363 = (current_access_specifier == access_private_node);
1364 TREE_PROTECTED (decl)
1365 = (current_access_specifier == access_protected_node);
1366 if (TREE_CODE (decl) == TEMPLATE_DECL)
1368 TREE_PRIVATE (DECL_RESULT (decl)) = TREE_PRIVATE (decl);
1369 TREE_PROTECTED (DECL_RESULT (decl)) = TREE_PROTECTED (decl);
1372 /* Mark the DECL as a member of the current class. */
1373 if (TREE_CODE (decl) == FUNCTION_DECL
1374 || DECL_FUNCTION_TEMPLATE_P (decl))
1375 /* Historically, DECL_CONTEXT was not set for a FUNCTION_DECL in
1376 finish_struct. Presumably it is already set as the function is
1377 parsed. Perhaps DECL_CLASS_CONTEXT is already set, too? */
1378 DECL_CLASS_CONTEXT (decl) = current_class_type;
1380 DECL_CONTEXT (decl) = current_class_type;
1382 /* Put functions on the TYPE_METHODS list and everything else on the
1383 TYPE_FIELDS list. Note that these are built up in reverse order.
1384 We reverse them (to obtain declaration order) in finish_struct. */
1385 if (TREE_CODE (decl) == FUNCTION_DECL
1386 || DECL_FUNCTION_TEMPLATE_P (decl))
1388 /* We also need to add this function to the
1389 CLASSTYPE_METHOD_VEC. */
1390 add_method (current_class_type, 0, decl);
1392 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1393 TYPE_METHODS (current_class_type) = decl;
1397 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1398 go at the beginning. The reason is that lookup_field_1
1399 searches the list in order, and we want a field name to
1400 override a type name so that the "struct stat hack" will
1401 work. In particular:
1403 struct S { enum E { }; int E } s;
1406 is legal. In addition, the FIELD_DECLs must be maintained in
1407 declaration order so that class layout works as expected.
1408 However, we don't need that order until class layout, so we
1409 save a little time by putting FIELD_DECLs on in reverse order
1410 here, and then reversing them in finish_struct_1. (We could
1411 also keep a pointer to the correct insertion points in the
1414 if (TREE_CODE (decl) == TYPE_DECL)
1415 TYPE_FIELDS (current_class_type)
1416 = chainon (TYPE_FIELDS (current_class_type), decl);
1419 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1420 TYPE_FIELDS (current_class_type) = decl;
1423 /* Enter the DECL into the scope of the class. */
1424 if (TREE_CODE (decl) != USING_DECL)
1425 pushdecl_class_level (decl);
1429 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1430 the definition is immediately followed by a semicolon. Returns the
1434 finish_class_definition (t, attributes, semi, pop_scope_p)
1440 /* finish_struct nukes this anyway; if finish_exception does too,
1443 note_got_semicolon (t);
1445 /* If we got any attributes in class_head, xref_tag will stick them in
1446 TREE_TYPE of the type. Grab them now. */
1447 attributes = chainon (TREE_TYPE (t), attributes);
1448 TREE_TYPE (t) = NULL_TREE;
1450 if (TREE_CODE (t) == ENUMERAL_TYPE)
1454 t = finish_struct (t, attributes, semi);
1456 note_got_semicolon (t);
1462 check_for_missing_semicolon (t);
1464 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1465 if (current_scope () == current_function_decl)
1466 do_pending_defargs ();
1471 /* Finish processing the default argument expressions cached during
1472 the processing of a class definition. */
1475 begin_inline_definitions ()
1478 && current_scope () == current_function_decl)
1479 do_pending_inlines ();
1482 /* Finish processing the inline function definitions cached during the
1483 processing of a class definition. */
1486 finish_inline_definitions ()
1488 if (current_class_type == NULL_TREE)
1489 clear_inline_text_obstack ();
1491 /* Undo the begin_tree in begin_class_definition. */
1495 /* Finish processing the declaration of a member class template
1496 TYPES whose template parameters are given by PARMS. */
1499 finish_member_class_template (types)
1504 /* If there are declared, but undefined, partial specializations
1505 mixed in with the typespecs they will not yet have passed through
1506 maybe_process_partial_specialization, so we do that here. */
1507 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1508 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1509 maybe_process_partial_specialization (TREE_VALUE (t));
1511 note_list_got_semicolon (types);
1512 grok_x_components (types);
1513 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1514 /* The component was in fact a friend declaration. We avoid
1515 finish_member_template_decl performing certain checks by
1519 finish_member_template_decl (types);
1521 /* As with other component type declarations, we do
1522 not store the new DECL on the list of
1527 /* Finish processsing a complete template declaration. The PARMS are
1528 the template parameters. */
1531 finish_template_decl (parms)
1535 end_template_decl ();
1537 end_specialization ();
1540 /* Finish processing a a template-id (which names a type) of the form
1541 NAME < ARGS >. Return the TYPE_DECL for the type named by the
1542 template-id. If ENTERING_SCOPE is non-zero we are about to enter
1543 the scope of template-id indicated. */
1546 finish_template_type (name, args, entering_scope)
1553 decl = lookup_template_class (name, args,
1554 NULL_TREE, NULL_TREE, entering_scope);
1555 if (decl != error_mark_node)
1556 decl = TYPE_STUB_DECL (decl);
1561 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
1562 namespace scope or a class scope. */
1568 tree scope = TREE_OPERAND (sr, 0);
1570 if (TREE_CODE (scope) == NAMESPACE_DECL)
1572 push_decl_namespace (scope);
1573 TREE_COMPLEXITY (sr) = -1;
1575 else if (scope != current_class_type)
1577 if (TREE_CODE (scope) == TYPENAME_TYPE)
1579 /* In a declarator for a template class member, the scope will
1580 get here as an implicit typename, a TYPENAME_TYPE with a type. */
1581 scope = TREE_TYPE (scope);
1582 TREE_OPERAND (sr, 0) = scope;
1584 push_nested_class (scope, 3);
1585 TREE_COMPLEXITY (sr) = current_class_depth;
1589 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
1590 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
1591 BASE_CLASS, or NULL_TREE if an error occurred. The
1592 ACCESSS_SPECIFIER is one of
1593 access_{default,public,protected_private}[_virtual]_node.*/
1596 finish_base_specifier (access_specifier, base_class,
1597 current_aggr_is_signature)
1598 tree access_specifier;
1600 int current_aggr_is_signature;
1605 if (base_class == NULL_TREE)
1607 error ("invalid base class");
1608 type = error_mark_node;
1611 type = TREE_TYPE (base_class);
1612 if (current_aggr_is_signature && access_specifier)
1613 error ("access and source specifiers not allowed in signature");
1614 if (! is_aggr_type (type, 1))
1616 else if (current_aggr_is_signature
1617 && (! type) && (! IS_SIGNATURE (type)))
1619 error ("class name not allowed as base signature");
1622 else if (current_aggr_is_signature)
1624 sorry ("signature inheritance, base type `%s' ignored",
1625 IDENTIFIER_POINTER (access_specifier));
1626 result = build_tree_list (access_public_node, type);
1628 else if (type && IS_SIGNATURE (type))
1630 error ("signature name not allowed as base class");
1634 result = build_tree_list (access_specifier, type);
1639 /* Called when multiple declarators are processed. If that is not
1640 premitted in this context, an error is issued. */
1643 check_multiple_declarators ()
1647 In a template-declaration, explicit specialization, or explicit
1648 instantiation the init-declarator-list in the declaration shall
1649 contain at most one declarator.
1651 We don't just use PROCESSING_TEMPLATE_DECL for the first
1652 condition since that would disallow the perfectly legal code,
1653 like `template <class T> struct S { int i, j; };'. */
1654 tree scope = current_scope ();
1656 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1657 /* It's OK to write `template <class T> void f() { int i, j;}'. */
1660 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
1661 || processing_explicit_instantiation
1662 || processing_specialization)
1663 cp_error ("multiple declarators in template declaration");
1667 finish_typeof (expr)
1670 if (processing_template_decl)
1674 push_obstacks_nochange ();
1675 end_temporary_allocation ();
1677 t = make_lang_type (TYPEOF_TYPE);
1678 TYPE_FIELDS (t) = expr;
1685 return TREE_TYPE (expr);