1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 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 GCC.
12 GCC 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 GCC 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 GCC; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
29 #include "coretypes.h"
33 #include "tree-inline.h"
44 /* There routines provide a modular interface to perform many parsing
45 operations. They may therefore be used during actual parsing, or
46 during template instantiation, which may be regarded as a
47 degenerate form of parsing. Since the current g++ parser is
48 lacking in several respects, and will be reimplemented, we are
49 attempting to move most code that is not directly related to
50 parsing into this file; that will make implementing the new parser
51 much easier since it will be able to make use of these routines. */
53 static tree maybe_convert_cond PARAMS ((tree));
54 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
55 static void deferred_type_access_control PARAMS ((void));
56 static void emit_associated_thunks PARAMS ((tree));
57 static void genrtl_try_block PARAMS ((tree));
58 static void genrtl_eh_spec_block PARAMS ((tree));
59 static void genrtl_handler PARAMS ((tree));
60 static void cp_expand_stmt PARAMS ((tree));
61 static void genrtl_start_function PARAMS ((tree));
62 static void genrtl_finish_function PARAMS ((tree));
63 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
65 /* Finish processing the COND, the SUBSTMT condition for STMT. */
67 #define FINISH_COND(COND, STMT, SUBSTMT) \
69 if (last_tree != (STMT)) \
71 RECHAIN_STMTS (STMT, SUBSTMT); \
72 if (!processing_template_decl) \
74 (COND) = build_tree_list (SUBSTMT, COND); \
82 /* Returns nonzero if the current statement is a full expression,
83 i.e. temporaries created during that statement should be destroyed
84 at the end of the statement. */
87 stmts_are_full_exprs_p ()
89 return current_stmt_tree ()->stmts_are_full_exprs_p;
92 /* Returns the stmt_tree (if any) to which statements are currently
93 being added. If there is no active statement-tree, NULL is
100 ? &cfun->language->base.x_stmt_tree
101 : &scope_chain->x_stmt_tree);
104 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
105 flag for this because "A union for which objects or pointers are
106 declared is not an anonymous union" [class.union]. */
109 anon_aggr_type_p (node)
112 return ANON_AGGR_TYPE_P (node);
115 /* Finish a scope. */
120 tree block = NULL_TREE;
122 if (stmts_are_full_exprs_p ())
124 tree scope_stmts = NULL_TREE;
126 block = poplevel (kept_level_p (), 1, 0);
127 if (!processing_template_decl)
129 /* This needs to come after the poplevel so that partial scopes
130 are properly nested. */
131 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
134 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
135 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
143 /* Begin a new scope. */
146 do_pushlevel (scope_kind sk)
148 if (stmts_are_full_exprs_p ())
150 if (!processing_template_decl)
151 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
156 /* Finish a goto-statement. */
159 finish_goto_stmt (destination)
162 if (TREE_CODE (destination) == IDENTIFIER_NODE)
163 destination = lookup_label (destination);
165 /* We warn about unused labels with -Wunused. That means we have to
166 mark the used labels as used. */
167 if (TREE_CODE (destination) == LABEL_DECL)
168 TREE_USED (destination) = 1;
170 if (TREE_CODE (destination) != LABEL_DECL)
171 /* We don't inline calls to functions with computed gotos.
172 Those functions are typically up to some funny business,
173 and may be depending on the labels being at particular
174 addresses, or some such. */
175 DECL_UNINLINABLE (current_function_decl) = 1;
177 check_goto (destination);
179 return add_stmt (build_stmt (GOTO_STMT, destination));
182 /* COND is the condition-expression for an if, while, etc.,
183 statement. Convert it to a boolean value, if appropriate. */
186 maybe_convert_cond (cond)
189 /* Empty conditions remain empty. */
193 /* Wait until we instantiate templates before doing conversion. */
194 if (processing_template_decl)
197 /* Do the conversion. */
198 cond = convert_from_reference (cond);
199 return condition_conversion (cond);
202 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
205 finish_expr_stmt (expr)
209 tree expr_type = NULL_TREE;;
211 if (expr != NULL_TREE)
213 if (!processing_template_decl
214 && !(stmts_are_full_exprs_p ())
215 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
217 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
218 expr = default_conversion (expr);
220 /* Remember the type of the expression. */
221 expr_type = TREE_TYPE (expr);
223 if (stmts_are_full_exprs_p ())
224 expr = convert_to_void (expr, "statement");
226 r = add_stmt (build_stmt (EXPR_STMT, expr));
231 /* This was an expression-statement, so we save the type of the
233 last_expr_type = expr_type;
239 /* Begin an if-statement. Returns a newly created IF_STMT if
246 do_pushlevel (sk_block);
247 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
252 /* Process the COND of an if-statement, which may be given by
256 finish_if_stmt_cond (cond, if_stmt)
260 cond = maybe_convert_cond (cond);
261 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
264 /* Finish the then-clause of an if-statement, which may be given by
268 finish_then_clause (if_stmt)
271 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
275 /* Begin the else-clause of an if-statement. */
282 /* Finish the else-clause of an if-statement, which may be given by
286 finish_else_clause (if_stmt)
289 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
292 /* Finish an if-statement. */
301 /* Begin a while-statement. Returns a newly created WHILE_STMT if
308 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
310 do_pushlevel (sk_block);
314 /* Process the COND of a while-statement, which may be given by
318 finish_while_stmt_cond (cond, while_stmt)
322 cond = maybe_convert_cond (cond);
323 if (processing_template_decl)
324 /* Don't mess with condition decls in a template. */
325 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
326 else if (getdecls () == NULL_TREE)
327 /* It was a simple condition; install it. */
328 WHILE_COND (while_stmt) = cond;
331 /* If there was a declaration in the condition, we can't leave it
335 while (true) { A x = 42; if (!x) break; } */
337 WHILE_COND (while_stmt) = boolean_true_node;
339 if_stmt = begin_if_stmt ();
340 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
341 finish_if_stmt_cond (cond, if_stmt);
342 finish_break_stmt ();
343 finish_then_clause (if_stmt);
348 /* Finish a while-statement, which may be given by WHILE_STMT. */
351 finish_while_stmt (while_stmt)
355 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
359 /* Begin a do-statement. Returns a newly created DO_STMT if
365 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
370 /* Finish the body of a do-statement, which may be given by DO_STMT. */
373 finish_do_body (do_stmt)
376 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
379 /* Finish a do-statement, which may be given by DO_STMT, and whose
380 COND is as indicated. */
383 finish_do_stmt (cond, do_stmt)
387 cond = maybe_convert_cond (cond);
388 DO_COND (do_stmt) = cond;
392 /* Finish a return-statement. The EXPRESSION returned, if any, is as
396 finish_return_stmt (expr)
401 expr = check_return_expr (expr);
402 if (!processing_template_decl)
404 if (DECL_DESTRUCTOR_P (current_function_decl))
406 /* Similarly, all destructors must run destructors for
407 base-classes before returning. So, all returns in a
408 destructor get sent to the DTOR_LABEL; finish_function emits
409 code to return a value there. */
410 return finish_goto_stmt (dtor_label);
413 r = add_stmt (build_stmt (RETURN_STMT, expr));
419 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
426 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
427 NULL_TREE, NULL_TREE);
428 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
429 if (NEW_FOR_SCOPE_P (r))
430 do_pushlevel (sk_for);
436 /* Finish the for-init-statement of a for-statement, which may be
437 given by FOR_STMT. */
440 finish_for_init_stmt (for_stmt)
443 if (last_tree != for_stmt)
444 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
445 do_pushlevel (sk_block);
448 /* Finish the COND of a for-statement, which may be given by
452 finish_for_cond (cond, for_stmt)
456 cond = maybe_convert_cond (cond);
457 if (processing_template_decl)
458 /* Don't mess with condition decls in a template. */
459 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
460 else if (getdecls () == NULL_TREE)
461 /* It was a simple condition; install it. */
462 FOR_COND (for_stmt) = cond;
465 /* If there was a declaration in the condition, we can't leave it
467 for (; A x = 42;) { }
469 for (;;) { A x = 42; if (!x) break; } */
471 FOR_COND (for_stmt) = NULL_TREE;
473 if_stmt = begin_if_stmt ();
474 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
475 finish_if_stmt_cond (cond, if_stmt);
476 finish_break_stmt ();
477 finish_then_clause (if_stmt);
482 /* Finish the increment-EXPRESSION in a for-statement, which may be
483 given by FOR_STMT. */
486 finish_for_expr (expr, for_stmt)
490 FOR_EXPR (for_stmt) = expr;
493 /* Finish the body of a for-statement, which may be given by
494 FOR_STMT. The increment-EXPR for the loop must be
498 finish_for_stmt (for_stmt)
501 /* Pop the scope for the body of the loop. */
503 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
504 if (NEW_FOR_SCOPE_P (for_stmt))
509 /* Finish a break-statement. */
514 return add_stmt (build_break_stmt ());
517 /* Finish a continue-statement. */
520 finish_continue_stmt ()
522 return add_stmt (build_continue_stmt ());
525 /* Begin a switch-statement. Returns a new SWITCH_STMT if
532 do_pushlevel (sk_block);
533 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
538 /* Finish the cond of a switch-statement. */
541 finish_switch_cond (cond, switch_stmt)
545 tree orig_type = NULL;
546 if (!processing_template_decl)
550 /* Convert the condition to an integer or enumeration type. */
551 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
552 if (cond == NULL_TREE)
554 error ("switch quantity not an integer");
555 cond = error_mark_node;
557 orig_type = TREE_TYPE (cond);
558 if (cond != error_mark_node)
560 cond = default_conversion (cond);
561 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
564 if (cond != error_mark_node)
566 index = get_unwidened (cond, NULL_TREE);
567 /* We can't strip a conversion from a signed type to an unsigned,
568 because if we did, int_fits_type_p would do the wrong thing
569 when checking case values for being in range,
570 and it's too hard to do the right thing. */
571 if (TREE_UNSIGNED (TREE_TYPE (cond))
572 == TREE_UNSIGNED (TREE_TYPE (index)))
576 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
577 SWITCH_TYPE (switch_stmt) = orig_type;
578 push_switch (switch_stmt);
581 /* Finish the body of a switch-statement, which may be given by
582 SWITCH_STMT. The COND to switch on is indicated. */
585 finish_switch_stmt (switch_stmt)
588 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
594 /* Generate the RTL for T, which is a TRY_BLOCK. */
602 expand_eh_region_start ();
603 expand_stmt (TRY_STMTS (t));
604 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
608 if (!FN_TRY_BLOCK_P (t))
609 emit_line_note (input_filename, lineno);
611 expand_eh_region_start ();
612 expand_stmt (TRY_STMTS (t));
614 if (FN_TRY_BLOCK_P (t))
616 expand_start_all_catch ();
617 in_function_try_handler = 1;
618 expand_stmt (TRY_HANDLERS (t));
619 in_function_try_handler = 0;
620 expand_end_all_catch ();
624 expand_start_all_catch ();
625 expand_stmt (TRY_HANDLERS (t));
626 expand_end_all_catch ();
631 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
634 genrtl_eh_spec_block (t)
637 expand_eh_region_start ();
638 expand_stmt (EH_SPEC_STMTS (t));
639 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
640 build_call (call_unexpected_node,
641 tree_cons (NULL_TREE,
646 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
652 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
657 /* Likewise, for a function-try-block. */
660 begin_function_try_block ()
662 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
663 FN_TRY_BLOCK_P (r) = 1;
668 /* Finish a try-block, which may be given by TRY_BLOCK. */
671 finish_try_block (try_block)
674 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
677 /* Finish the body of a cleanup try-block, which may be given by
681 finish_cleanup_try_block (try_block)
684 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
687 /* Finish an implicitly generated try-block, with a cleanup is given
691 finish_cleanup (cleanup, try_block)
695 TRY_HANDLERS (try_block) = cleanup;
696 CLEANUP_P (try_block) = 1;
699 /* Likewise, for a function-try-block. */
702 finish_function_try_block (try_block)
705 if (TREE_CHAIN (try_block)
706 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
708 /* Chain the compound statement after the CTOR_INITIALIZER. */
709 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
710 /* And make the CTOR_INITIALIZER the body of the try-block. */
711 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
714 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
715 in_function_try_handler = 1;
718 /* Finish a handler-sequence for a try-block, which may be given by
722 finish_handler_sequence (try_block)
725 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
726 check_handlers (TRY_HANDLERS (try_block));
729 /* Likewise, for a function-try-block. */
732 finish_function_handler_sequence (try_block)
735 in_function_try_handler = 0;
736 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
737 check_handlers (TRY_HANDLERS (try_block));
740 /* Generate the RTL for T, which is a HANDLER. */
746 genrtl_do_pushlevel ();
747 if (!processing_template_decl)
748 expand_start_catch (HANDLER_TYPE (t));
749 expand_stmt (HANDLER_BODY (t));
750 if (!processing_template_decl)
754 /* Begin a handler. Returns a HANDLER if appropriate. */
760 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
762 /* Create a binding level for the eh_info and the exception object
764 do_pushlevel (sk_catch);
768 /* Finish the handler-parameters for a handler, which may be given by
769 HANDLER. DECL is the declaration for the catch parameter, or NULL
770 if this is a `catch (...)' clause. */
773 finish_handler_parms (decl, handler)
777 tree type = NULL_TREE;
778 if (processing_template_decl)
782 decl = pushdecl (decl);
783 decl = push_template_decl (decl);
784 add_decl_stmt (decl);
785 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
786 type = TREE_TYPE (decl);
790 type = expand_start_catch_block (decl);
792 HANDLER_TYPE (handler) = type;
795 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
796 the return value from the matching call to finish_handler_parms. */
799 finish_handler (handler)
802 if (!processing_template_decl)
803 expand_end_catch_block ();
805 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
808 /* Begin a compound-statement. If HAS_NO_SCOPE is nonzero, the
809 compound-statement does not define a scope. Returns a new
810 COMPOUND_STMT if appropriate. */
813 begin_compound_stmt (has_no_scope)
819 r = build_stmt (COMPOUND_STMT, NULL_TREE);
821 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
826 COMPOUND_STMT_NO_SCOPE (r) = 1;
828 last_expr_type = NULL_TREE;
831 do_pushlevel (is_try ? sk_try : sk_block);
833 /* Normally, we try hard to keep the BLOCK for a
834 statement-expression. But, if it's a statement-expression with
835 a scopeless block, there's nothing to keep, and we don't want
836 to accidentally keep a block *inside* the scopeless block. */
842 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
843 If HAS_NO_SCOPE is nonzero, the compound statement does not define
847 finish_compound_stmt (has_no_scope, compound_stmt)
859 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
861 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
862 the precise purpose of that variable is store the type of the
863 last expression statement within the last compound statement, we
864 preserve the value. */
872 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
873 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
877 finish_asm_stmt (cv_qualifier, string, output_operands,
878 input_operands, clobbers)
881 tree output_operands;
888 if (cv_qualifier != NULL_TREE
889 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
891 warning ("%s qualifier ignored on asm",
892 IDENTIFIER_POINTER (cv_qualifier));
893 cv_qualifier = NULL_TREE;
896 if (!processing_template_decl)
902 for (t = input_operands; t; t = TREE_CHAIN (t))
904 tree converted_operand
905 = decay_conversion (TREE_VALUE (t));
907 /* If the type of the operand hasn't been determined (e.g.,
908 because it involves an overloaded function), then issue
909 an error message. There's no context available to
910 resolve the overloading. */
911 if (TREE_TYPE (converted_operand) == unknown_type_node)
913 error ("type of asm operand `%E' could not be determined",
915 converted_operand = error_mark_node;
917 TREE_VALUE (t) = converted_operand;
920 ninputs = list_length (input_operands);
921 noutputs = list_length (output_operands);
923 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
928 const char *constraint;
931 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
932 operand = TREE_VALUE (t);
934 if (!parse_output_constraint (&constraint,
935 i, ninputs, noutputs,
940 /* By marking this operand as erroneous, we will not try
941 to process this operand again in expand_asm_operands. */
942 TREE_VALUE (t) = error_mark_node;
946 /* If the operand is a DECL that is going to end up in
947 memory, assume it is addressable. This is a bit more
948 conservative than it would ideally be; the exact test is
949 buried deep in expand_asm_operands and depends on the
950 DECL_RTL for the OPERAND -- which we don't have at this
952 if (!allows_reg && DECL_P (operand))
953 cxx_mark_addressable (operand);
957 r = build_stmt (ASM_STMT, cv_qualifier, string,
958 output_operands, input_operands,
963 /* Finish a label with the indicated NAME. */
966 finish_label_stmt (name)
969 tree decl = define_label (input_filename, lineno, name);
970 return add_stmt (build_stmt (LABEL_STMT, decl));
973 /* Finish a series of declarations for local labels. G++ allows users
974 to declare "local" labels, i.e., labels with scope. This extension
975 is useful when writing code involving statement-expressions. */
978 finish_label_decl (name)
981 tree decl = declare_local_label (name);
982 add_decl_stmt (decl);
985 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
988 finish_decl_cleanup (decl, cleanup)
992 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
995 /* If the current scope exits with an exception, run CLEANUP. */
998 finish_eh_cleanup (cleanup)
1001 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1002 CLEANUP_EH_ONLY (r) = 1;
1006 /* Begin processing a mem-initializer-list. */
1009 begin_mem_initializers ()
1011 if (! DECL_CONSTRUCTOR_P (current_function_decl))
1012 error ("only constructors take base initializers");
1015 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1016 order they were written by the user. Each node is as for
1017 emit_mem_initializers. */
1020 finish_mem_initializers (tree mem_inits)
1022 /* Reorder the MEM_INITS so that they are in the order they appeared
1023 in the source program. */
1024 mem_inits = nreverse (mem_inits);
1026 if (processing_template_decl)
1027 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1029 emit_mem_initializers (mem_inits);
1032 /* Returns the stack of SCOPE_STMTs for the current function. */
1035 current_scope_stmt_stack ()
1037 return &cfun->language->base.x_scope_stmt_stack;
1040 /* Finish a parenthesized expression EXPR. */
1043 finish_parenthesized_expr (expr)
1046 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1047 /* This inhibits warnings in c_common_truthvalue_conversion. */
1048 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1050 if (TREE_CODE (expr) == OFFSET_REF)
1051 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1052 enclosed in parentheses. */
1053 PTRMEM_OK_P (expr) = 0;
1057 /* Finish a reference to a non-static data member (DECL) that is not
1058 preceded by `.' or `->'. */
1061 finish_non_static_data_member (tree decl, tree qualifying_scope)
1063 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1065 if (current_class_ptr == NULL_TREE)
1067 if (current_function_decl
1068 && DECL_STATIC_FUNCTION_P (current_function_decl))
1069 cp_error_at ("invalid use of member `%D' in static member function",
1072 cp_error_at ("invalid use of non-static data member `%D'", decl);
1073 error ("from this location");
1075 return error_mark_node;
1077 TREE_USED (current_class_ptr) = 1;
1078 if (processing_template_decl)
1079 return build_min_nt (COMPONENT_REF, current_class_ref, DECL_NAME (decl));
1082 tree access_type = current_class_type;
1083 tree object = current_class_ref;
1085 while (!DERIVED_FROM_P (context_for_name_lookup (decl), access_type))
1087 access_type = TYPE_CONTEXT (access_type);
1088 while (DECL_P (access_type))
1089 access_type = DECL_CONTEXT (access_type);
1092 enforce_access (access_type, decl);
1094 /* If the data member was named `C::M', convert `*this' to `C'
1096 if (qualifying_scope)
1098 tree binfo = NULL_TREE;
1099 object = build_scoped_ref (object, qualifying_scope,
1103 return build_class_member_access_expr (object, decl,
1104 /*access_path=*/NULL_TREE,
1105 /*preserve_reference=*/false);
1109 /* Begin a statement-expression. The value returned must be passed to
1110 finish_stmt_expr. */
1115 /* If we're outside a function, we won't have a statement-tree to
1116 work with. But, if we see a statement-expression we need to
1118 if (! cfun && !last_tree)
1119 begin_stmt_tree (&scope_chain->x_saved_tree);
1121 keep_next_level (1);
1122 /* If we're building a statement tree, then the upcoming compound
1123 statement will be chained onto the tree structure, starting at
1124 last_tree. We return last_tree so that we can later unhook the
1125 compound statement. */
1129 /* Used when beginning a statement-expression outside function scope.
1130 For example, when handling a file-scope initializer, we use this
1134 begin_global_stmt_expr ()
1136 if (! cfun && !last_tree)
1137 begin_stmt_tree (&scope_chain->x_saved_tree);
1139 keep_next_level (1);
1141 return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1);
1144 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1147 finish_global_stmt_expr (stmt_expr)
1150 stmt_expr = expand_end_stmt_expr (stmt_expr);
1153 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1154 finish_stmt_tree (&scope_chain->x_saved_tree);
1159 /* Finish a statement-expression. RTL_EXPR should be the value
1160 returned by the previous begin_stmt_expr; EXPR is the
1161 statement-expression. Returns an expression representing the
1162 statement-expression. */
1165 finish_stmt_expr (rtl_expr)
1170 /* If the last thing in the statement-expression was not an
1171 expression-statement, then it has type `void'. */
1172 if (!last_expr_type)
1173 last_expr_type = void_type_node;
1174 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1175 TREE_SIDE_EFFECTS (result) = 1;
1177 /* Remove the compound statement from the tree structure; it is
1178 now saved in the STMT_EXPR. */
1179 last_tree = rtl_expr;
1180 TREE_CHAIN (last_tree) = NULL_TREE;
1182 /* If we created a statement-tree for this statement-expression,
1185 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1186 finish_stmt_tree (&scope_chain->x_saved_tree);
1191 /* Generate an expression for `FN (ARGS)'.
1193 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1194 as a virtual call, even if FN is virtual. (This flag is set when
1195 encountering an expression where the function name is explicitly
1196 qualified. For example a call to `X::f' never generates a virtual
1199 Returns code for the call. */
1202 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1204 if (fn == error_mark_node || args == error_mark_node)
1205 return error_mark_node;
1207 if (processing_template_decl)
1208 return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1210 /* ARGS should be a list of arguments. */
1211 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1214 /* A reference to a member function will appear as an overloaded
1215 function (rather than a BASELINK) if an unqualified name was used
1217 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1221 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1222 f = get_first_fn (TREE_OPERAND (fn, 0));
1224 f = get_first_fn (fn);
1225 if (DECL_FUNCTION_MEMBER_P (f))
1227 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1228 fn = build_baselink (TYPE_BINFO (type),
1230 fn, /*optype=*/NULL_TREE);
1234 if (BASELINK_P (fn))
1238 /* A call to a member function. From [over.call.func]:
1240 If the keyword this is in scope and refers to the class of
1241 that member function, or a derived class thereof, then the
1242 function call is transformed into a qualified function call
1243 using (*this) as the postfix-expression to the left of the
1244 . operator.... [Otherwise] a contrived object of type T
1245 becomes the implied object argument.
1247 This paragraph is unclear about this situation:
1249 struct A { void f(); };
1250 struct B : public A {};
1251 struct C : public A { void g() { B::f(); }};
1253 In particular, for `B::f', this paragraph does not make clear
1254 whether "the class of that member function" refers to `A' or
1255 to `B'. We believe it refers to `B'. */
1256 if (current_class_type
1257 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1259 && current_class_ref)
1260 object = current_class_ref;
1263 tree representative_fn;
1265 representative_fn = BASELINK_FUNCTIONS (fn);
1266 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1267 representative_fn = TREE_OPERAND (representative_fn, 0);
1268 representative_fn = get_first_fn (representative_fn);
1269 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1272 return build_new_method_call (object, fn, args, NULL_TREE,
1274 ? LOOKUP_NONVIRTUAL : 0));
1276 else if (is_overloaded_fn (fn))
1277 /* A call to a namespace-scope function. */
1278 return build_new_function_call (fn, args);
1279 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1284 error ("arguments to destructor are not allowed");
1285 /* Mark the pseudo-destructor call as having side-effects so
1286 that we do not issue warnings about its use. */
1287 result = build1 (NOP_EXPR,
1289 TREE_OPERAND (fn, 0));
1290 TREE_SIDE_EFFECTS (result) = 1;
1293 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1295 /* If the "function" is really an object of class type, it might
1296 have an overloaded `operator ()'. */
1298 result = build_opfncall (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1303 /* A call where the function is unknown. */
1304 return build_function_call (fn, args);
1307 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1308 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1309 POSTDECREMENT_EXPR.) */
1312 finish_increment_expr (expr, code)
1314 enum tree_code code;
1316 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1317 a COMPONENT_REF). This way if we've got, say, a reference to a
1318 static member that's being operated on, we don't end up trying to
1319 find a member operator for the class it's in. */
1321 if (TREE_CODE (expr) == OFFSET_REF)
1322 expr = resolve_offset_ref (expr);
1323 return build_x_unary_op (code, expr);
1326 /* Finish a use of `this'. Returns an expression for `this'. */
1333 if (current_class_ptr)
1335 result = current_class_ptr;
1337 else if (current_function_decl
1338 && DECL_STATIC_FUNCTION_P (current_function_decl))
1340 error ("`this' is unavailable for static member functions");
1341 result = error_mark_node;
1345 if (current_function_decl)
1346 error ("invalid use of `this' in non-member function");
1348 error ("invalid use of `this' at top level");
1349 result = error_mark_node;
1355 /* Finish a member function call using OBJECT and ARGS as arguments to
1356 FN. Returns an expression for the call. */
1359 finish_object_call_expr (fn, object, args)
1364 if (DECL_DECLARES_TYPE_P (fn))
1366 if (processing_template_decl)
1367 /* This can happen on code like:
1370 template <class T> void f(T t) {
1374 We just grab the underlying IDENTIFIER. */
1375 fn = DECL_NAME (fn);
1378 error ("calling type `%T' like a method", fn);
1379 return error_mark_node;
1383 if (processing_template_decl)
1384 return build_nt (CALL_EXPR,
1385 build_nt (COMPONENT_REF, object, fn),
1389 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1391 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1394 /* Finish a qualified member function call using OBJECT and ARGS as
1395 arguments to FN. Returns an expression for the call. */
1398 finish_qualified_object_call_expr (fn, object, args)
1403 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1404 TREE_OPERAND (fn, 1), args);
1407 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1408 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1409 the TYPE for the type given. If SCOPE is non-NULL, the expression
1410 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1413 finish_pseudo_destructor_expr (object, scope, destructor)
1418 if (destructor == error_mark_node)
1419 return error_mark_node;
1421 my_friendly_assert (TYPE_P (destructor), 20010905);
1423 if (!processing_template_decl)
1425 if (scope == error_mark_node)
1427 error ("invalid qualifying scope in pseudo-destructor name");
1428 return error_mark_node;
1431 if (!same_type_p (TREE_TYPE (object), destructor))
1433 error ("`%E' is not of type `%T'", object, destructor);
1434 return error_mark_node;
1438 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1441 /* Finish an expression of the form CODE EXPR. */
1444 finish_unary_op_expr (code, expr)
1445 enum tree_code code;
1448 tree result = build_x_unary_op (code, expr);
1449 /* Inside a template, build_x_unary_op does not fold the
1450 expression. So check whether the result is folded before
1451 setting TREE_NEGATED_INT. */
1452 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1453 && TREE_CODE (result) == INTEGER_CST
1454 && !TREE_UNSIGNED (TREE_TYPE (result))
1455 && INT_CST_LT (result, integer_zero_node))
1456 TREE_NEGATED_INT (result) = 1;
1457 overflow_warning (result);
1461 /* Finish a compound-literal expression. TYPE is the type to which
1462 the INITIALIZER_LIST is being cast. */
1465 finish_compound_literal (type, initializer_list)
1467 tree initializer_list;
1469 tree compound_literal;
1471 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1472 compound_literal = build_nt (CONSTRUCTOR, NULL_TREE,
1474 /* Mark it as a compound-literal. */
1475 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1476 if (processing_template_decl)
1477 TREE_TYPE (compound_literal) = type;
1480 /* Check the initialization. */
1481 compound_literal = digest_init (type, compound_literal, NULL);
1482 /* If the TYPE was an array type with an unknown bound, then we can
1483 figure out the dimension now. For example, something like:
1487 implies that the array has two elements. */
1488 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1489 complete_array_type (type, compound_literal, 1);
1492 return compound_literal;
1495 /* Return the declaration for the function-name variable indicated by
1499 finish_fname (tree id)
1503 decl = fname_decl (C_RID_CODE (id), id);
1504 if (processing_template_decl)
1505 decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1509 static tree current_type_lookups;
1511 /* Perform deferred access control for types used in the type of a
1515 deferred_type_access_control ()
1517 tree lookup = type_lookups;
1519 if (lookup == error_mark_node)
1522 for (; lookup; lookup = TREE_CHAIN (lookup))
1523 enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1527 decl_type_access_control (decl)
1532 if (type_lookups == error_mark_node)
1535 save_fn = current_function_decl;
1537 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1538 current_function_decl = decl;
1540 deferred_type_access_control ();
1542 current_function_decl = save_fn;
1544 /* Now strip away the checks for the current declarator; they were
1545 added to type_lookups after typed_declspecs saved the copy that
1546 ended up in current_type_lookups. */
1547 type_lookups = current_type_lookups;
1551 save_type_access_control (lookups)
1554 current_type_lookups = lookups;
1557 /* Reset the deferred access control. */
1560 reset_type_access_control ()
1562 type_lookups = NULL_TREE;
1563 current_type_lookups = NULL_TREE;
1566 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1567 and DECLARATOR. Returns nonzero if the function-declaration is
1571 begin_function_definition (decl_specs, attributes, declarator)
1576 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1579 deferred_type_access_control ();
1580 type_lookups = error_mark_node;
1582 /* The things we're about to see are not directly qualified by any
1583 template headers we've seen thus far. */
1584 reset_specialization ();
1589 /* Begin a constructor declarator of the form `SCOPE::NAME'. Returns
1593 begin_constructor_declarator (scope, name)
1597 tree result = build_nt (SCOPE_REF, scope, name);
1598 enter_scope_of (result);
1602 /* Finish an init-declarator. Returns a DECL. */
1605 finish_declarator (declarator, declspecs, attributes,
1606 prefix_attributes, initialized)
1610 tree prefix_attributes;
1613 return start_decl (declarator, declspecs, initialized, attributes,
1617 /* Finish a translation unit. */
1620 finish_translation_unit ()
1622 /* In case there were missing closebraces,
1623 get us back to the global binding level. */
1625 while (current_namespace != global_namespace)
1628 /* Do file scope __FUNCTION__ et al. */
1629 finish_fname_decls ();
1632 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1633 Returns the parameter. */
1636 finish_template_type_parm (aggr, identifier)
1640 if (aggr != class_type_node)
1642 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1643 aggr = class_type_node;
1646 return build_tree_list (aggr, identifier);
1649 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1650 Returns the parameter. */
1653 finish_template_template_parm (aggr, identifier)
1657 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1658 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1659 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1660 DECL_TEMPLATE_RESULT (tmpl) = decl;
1661 DECL_ARTIFICIAL (decl) = 1;
1662 end_template_decl ();
1664 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1666 return finish_template_type_parm (aggr, tmpl);
1669 /* ARGUMENT is the default-argument value for a template template
1670 parameter. If ARGUMENT is invalid, issue error messages and return
1671 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1674 check_template_template_default_arg (tree argument)
1676 if (TREE_CODE (argument) != TEMPLATE_DECL
1677 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1678 && TREE_CODE (argument) != TYPE_DECL
1679 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1681 error ("invalid default template argument");
1682 return error_mark_node;
1688 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1689 nonzero, the parameter list was terminated by a `...'. */
1692 finish_parmlist (parms, ellipsis)
1698 /* We mark the PARMS as a parmlist so that declarator processing can
1699 disambiguate certain constructs. */
1700 TREE_PARMLIST (parms) = 1;
1701 /* We do not append void_list_node here, but leave it to grokparms
1703 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1708 /* Begin a class definition, as indicated by T. */
1711 begin_class_definition (t)
1714 if (t == error_mark_node)
1715 return error_mark_node;
1717 /* Check the bases are accessible. */
1718 decl_type_access_control (TYPE_NAME (t));
1719 reset_type_access_control ();
1721 if (processing_template_parmlist)
1723 error ("definition of `%#T' inside template parameter list", t);
1724 return error_mark_node;
1727 /* In a definition of a member class template, we will get here with
1728 an implicit typename. */
1729 if (IMPLICIT_TYPENAME_P (t))
1731 /* A non-implicit typename comes from code like:
1733 template <typename T> struct A {
1734 template <typename U> struct A<T>::B ...
1736 This is erroneous. */
1737 else if (TREE_CODE (t) == TYPENAME_TYPE)
1739 error ("invalid definition of qualified type `%T'", t);
1740 t = error_mark_node;
1743 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1745 t = make_aggr_type (RECORD_TYPE);
1746 pushtag (make_anon_name (), t, 0);
1749 /* If we generated a partial instantiation of this type, but now
1750 we're seeing a real definition, we're actually looking at a
1751 partial specialization. Consider:
1753 template <class T, class U>
1759 template <class T, class U>
1762 typename X<Y<T, U> >::A a;
1765 template <class T, class U>
1770 We have to undo the effects of the previous partial
1772 if (PARTIAL_INSTANTIATION_P (t))
1776 /* Unfortunately, when we're not in pedantic mode, we
1777 attempt to actually fill in some of the fields of the
1778 partial instantiation, in order to support the implicit
1779 typename extension. Clear those fields now, in
1780 preparation for the definition here. The fields cleared
1781 here must match those set in instantiate_class_template.
1782 Look for a comment mentioning begin_class_definition
1784 TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1785 TYPE_FIELDS (t) = NULL_TREE;
1786 TYPE_METHODS (t) = NULL_TREE;
1787 CLASSTYPE_DECL_LIST (t) = NULL_TREE;
1788 CLASSTYPE_TAGS (t) = NULL_TREE;
1789 CLASSTYPE_VBASECLASSES (t) = NULL_TREE;
1790 TYPE_SIZE (t) = NULL_TREE;
1793 /* This isn't a partial instantiation any more. */
1794 PARTIAL_INSTANTIATION_P (t) = 0;
1796 /* If this type was already complete, and we see another definition,
1798 else if (COMPLETE_TYPE_P (t))
1799 duplicate_tag_error (t);
1801 /* Update the location of the decl. */
1802 DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1803 DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1805 if (TYPE_BEING_DEFINED (t))
1807 t = make_aggr_type (TREE_CODE (t));
1808 pushtag (TYPE_IDENTIFIER (t), t, 0);
1810 maybe_process_partial_specialization (t);
1812 TYPE_BEING_DEFINED (t) = 1;
1813 TYPE_PACKED (t) = flag_pack_struct;
1814 /* Reset the interface data, at the earliest possible
1815 moment, as it might have been set via a class foo;
1817 if (! TYPE_ANONYMOUS_P (t))
1819 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1820 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1821 (t, interface_unknown);
1823 reset_specialization();
1825 /* Make a declaration for this class in its own scope. */
1826 build_self_reference ();
1831 /* Finish the member declaration given by DECL. */
1834 finish_member_declaration (decl)
1837 if (decl == error_mark_node || decl == NULL_TREE)
1840 if (decl == void_type_node)
1841 /* The COMPONENT was a friend, not a member, and so there's
1842 nothing for us to do. */
1845 /* We should see only one DECL at a time. */
1846 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1848 /* Set up access control for DECL. */
1850 = (current_access_specifier == access_private_node);
1851 TREE_PROTECTED (decl)
1852 = (current_access_specifier == access_protected_node);
1853 if (TREE_CODE (decl) == TEMPLATE_DECL)
1855 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1856 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1859 /* Mark the DECL as a member of the current class. */
1860 DECL_CONTEXT (decl) = current_class_type;
1864 A C language linkage is ignored for the names of class members
1865 and the member function type of class member functions. */
1866 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1867 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1869 maybe_add_class_template_decl_list (current_class_type, decl, /*friend_p=*/0);
1871 /* Put functions on the TYPE_METHODS list and everything else on the
1872 TYPE_FIELDS list. Note that these are built up in reverse order.
1873 We reverse them (to obtain declaration order) in finish_struct. */
1874 if (TREE_CODE (decl) == FUNCTION_DECL
1875 || DECL_FUNCTION_TEMPLATE_P (decl))
1877 /* We also need to add this function to the
1878 CLASSTYPE_METHOD_VEC. */
1879 add_method (current_class_type, decl, /*error_p=*/0);
1881 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1882 TYPE_METHODS (current_class_type) = decl;
1886 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1887 go at the beginning. The reason is that lookup_field_1
1888 searches the list in order, and we want a field name to
1889 override a type name so that the "struct stat hack" will
1890 work. In particular:
1892 struct S { enum E { }; int E } s;
1895 is valid. In addition, the FIELD_DECLs must be maintained in
1896 declaration order so that class layout works as expected.
1897 However, we don't need that order until class layout, so we
1898 save a little time by putting FIELD_DECLs on in reverse order
1899 here, and then reversing them in finish_struct_1. (We could
1900 also keep a pointer to the correct insertion points in the
1903 if (TREE_CODE (decl) == TYPE_DECL)
1904 TYPE_FIELDS (current_class_type)
1905 = chainon (TYPE_FIELDS (current_class_type), decl);
1908 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1909 TYPE_FIELDS (current_class_type) = decl;
1912 /* Enter the DECL into the scope of the class. */
1913 if (TREE_CODE (decl) != USING_DECL)
1914 pushdecl_class_level (decl);
1918 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1919 the definition is immediately followed by a semicolon. Returns the
1923 finish_class_definition (t, attributes, semi, pop_scope_p)
1929 if (t == error_mark_node)
1930 return error_mark_node;
1932 /* finish_struct nukes this anyway; if finish_exception does too,
1935 note_got_semicolon (t);
1937 /* If we got any attributes in class_head, xref_tag will stick them in
1938 TREE_TYPE of the type. Grab them now. */
1939 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1940 TYPE_ATTRIBUTES (t) = NULL_TREE;
1942 if (TREE_CODE (t) == ENUMERAL_TYPE)
1946 t = finish_struct (t, attributes);
1948 note_got_semicolon (t);
1952 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1957 /* Finish processing the declaration of a member class template
1958 TYPES whose template parameters are given by PARMS. */
1961 finish_member_class_template (types)
1966 /* If there are declared, but undefined, partial specializations
1967 mixed in with the typespecs they will not yet have passed through
1968 maybe_process_partial_specialization, so we do that here. */
1969 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1970 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1971 maybe_process_partial_specialization (TREE_VALUE (t));
1973 note_list_got_semicolon (types);
1974 grok_x_components (types);
1975 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1976 /* The component was in fact a friend declaration. We avoid
1977 finish_member_template_decl performing certain checks by
1981 finish_member_template_decl (types);
1983 /* As with other component type declarations, we do
1984 not store the new DECL on the list of
1989 /* Finish processing a complete template declaration. The PARMS are
1990 the template parameters. */
1993 finish_template_decl (parms)
1997 end_template_decl ();
1999 end_specialization ();
2002 /* Finish processing a template-id (which names a type) of the form
2003 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2004 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2005 the scope of template-id indicated. */
2008 finish_template_type (name, args, entering_scope)
2015 decl = lookup_template_class (name, args,
2016 NULL_TREE, NULL_TREE,
2017 entering_scope, /*complain=*/1);
2018 if (decl != error_mark_node)
2019 decl = TYPE_STUB_DECL (decl);
2024 /* SR is a SCOPE_REF node. Enter the scope of SR, whether it is a
2025 namespace scope or a class scope. */
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 ACCESS_SPECIFIER is one of
2056 access_{default,public,protected_private}[_virtual]_node.*/
2059 finish_base_specifier (access_specifier, base_class)
2060 tree access_specifier;
2065 if (base_class == error_mark_node)
2067 error ("invalid base-class specification");
2070 else if (! is_aggr_type (base_class, 1))
2074 if (cp_type_quals (base_class) != 0)
2076 error ("base class `%T' has cv qualifiers", base_class);
2077 base_class = TYPE_MAIN_VARIANT (base_class);
2079 result = build_tree_list (access_specifier, base_class);
2085 /* Called when multiple declarators are processed. If that is not
2086 premitted in this context, an error is issued. */
2089 check_multiple_declarators ()
2093 In a template-declaration, explicit specialization, or explicit
2094 instantiation the init-declarator-list in the declaration shall
2095 contain at most one declarator.
2097 We don't just use PROCESSING_TEMPLATE_DECL for the first
2098 condition since that would disallow the perfectly valid code,
2099 like `template <class T> struct S { int i, j; };'. */
2100 if (at_function_scope_p ())
2101 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2104 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2105 || processing_explicit_instantiation
2106 || processing_specialization)
2107 error ("multiple declarators in template declaration");
2110 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2111 use as a type-specifier. */
2114 finish_typeof (expr)
2117 if (processing_template_decl)
2121 t = make_aggr_type (TYPEOF_TYPE);
2122 TYPE_FIELDS (t) = expr;
2127 if (TREE_CODE (expr) == OFFSET_REF)
2128 expr = resolve_offset_ref (expr);
2130 return TREE_TYPE (expr);
2133 /* Compute the value of the `sizeof' operator. */
2139 return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2142 /* Implement the __alignof keyword: Return the minimum required
2143 alignment of T, measured in bytes. */
2149 if (processing_template_decl)
2150 return build_min (ALIGNOF_EXPR, size_type_node, t);
2152 return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2155 /* Generate RTL for the statement T, and its substatements, and any
2156 other statements at its nesting level. */
2162 switch (TREE_CODE (t))
2165 genrtl_try_block (t);
2169 genrtl_eh_spec_block (t);
2185 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2186 will equivalent CALL_EXPRs. */
2189 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2191 int *walk_subtrees ATTRIBUTE_UNUSED;
2192 void *data ATTRIBUTE_UNUSED;
2194 tree aggr_init_expr;
2200 enum style_t { ctor, arg, pcc } style;
2202 aggr_init_expr = *tp;
2203 /* We don't need to walk into types; there's nothing in a type that
2204 needs simplification. (And, furthermore, there are places we
2205 actively don't want to go. For example, we don't want to wander
2206 into the default arguments for a FUNCTION_DECL that appears in a
2208 if (TYPE_P (aggr_init_expr))
2213 /* Only AGGR_INIT_EXPRs are interesting. */
2214 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2217 /* Form an appropriate CALL_EXPR. */
2218 fn = TREE_OPERAND (aggr_init_expr, 0);
2219 args = TREE_OPERAND (aggr_init_expr, 1);
2220 slot = TREE_OPERAND (aggr_init_expr, 2);
2221 type = TREE_TYPE (aggr_init_expr);
2223 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2225 #ifdef PCC_STATIC_STRUCT_RETURN
2229 else if (TREE_ADDRESSABLE (type))
2232 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2233 handling. See build_cplus_new. */
2236 if (style == ctor || style == arg)
2238 /* Pass the address of the slot. If this is a constructor, we
2239 replace the first argument; otherwise, we tack on a new one. */
2241 args = TREE_CHAIN (args);
2243 cxx_mark_addressable (slot);
2244 args = tree_cons (NULL_TREE,
2246 build_pointer_type (TREE_TYPE (slot)),
2251 call_expr = build (CALL_EXPR,
2252 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2253 fn, args, NULL_TREE);
2254 TREE_SIDE_EFFECTS (call_expr) = 1;
2257 /* Tell the backend that we've added our return slot to the argument
2259 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2260 else if (style == pcc)
2262 /* If we're using the non-reentrant PCC calling convention, then we
2263 need to copy the returned value out of the static buffer into the
2265 int old_ac = flag_access_control;
2267 flag_access_control = 0;
2268 call_expr = build_aggr_init (slot, call_expr,
2269 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2270 flag_access_control = old_ac;
2273 /* We want to use the value of the initialized location as the
2275 call_expr = build (COMPOUND_EXPR, type,
2278 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2279 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2282 /* Keep iterating. */
2286 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2289 emit_associated_thunks (fn)
2292 /* When we use vcall offsets, we emit thunks with the virtual
2293 functions to which they thunk. The whole point of vcall offsets
2294 is so that you can know statically the entire set of thunks that
2295 will ever be needed for a given virtual function, thereby
2296 enabling you to output all the thunks with the function itself. */
2297 if (DECL_VIRTUAL_P (fn))
2301 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2303 use_thunk (thunk, /*emit_p=*/1);
2304 if (DECL_RESULT_THUNK_P (thunk))
2308 for (probe = DECL_THUNKS (thunk);
2309 probe; probe = TREE_CHAIN (probe))
2310 use_thunk (probe, /*emit_p=*/1);
2316 /* Generate RTL for FN. */
2323 const char *saved_input_filename;
2324 tree saved_function;
2326 /* When the parser calls us after finishing the body of a template
2327 function, we don't really want to expand the body. When we're
2328 processing an in-class definition of an inline function,
2329 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2330 to look at the function itself. */
2331 if (processing_template_decl
2332 || (DECL_LANG_SPECIFIC (fn)
2333 && DECL_TEMPLATE_INFO (fn)
2334 && uses_template_parms (DECL_TI_ARGS (fn))))
2336 /* Normally, collection only occurs in rest_of_compilation. So,
2337 if we don't collect here, we never collect junk generated
2338 during the processing of templates until we hit a
2339 non-template function. */
2344 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2345 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2346 simplify_aggr_init_exprs_r,
2349 /* If this is a constructor or destructor body, we have to clone
2351 if (maybe_clone_body (fn))
2353 /* We don't want to process FN again, so pretend we've written
2354 it out, even though we haven't. */
2355 TREE_ASM_WRITTEN (fn) = 1;
2359 /* There's no reason to do any of the work here if we're only doing
2360 semantic analysis; this code just generates RTL. */
2361 if (flag_syntax_only)
2364 /* If possible, avoid generating RTL for this function. Instead,
2365 just record it as an inline function, and wait until end-of-file
2366 to decide whether to write it out or not. */
2367 if (/* We have to generate RTL if it's not an inline function. */
2368 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2369 /* Or if we have to emit code for inline functions anyhow. */
2370 && !flag_keep_inline_functions
2371 /* Or if we actually have a reference to the function. */
2372 && !DECL_NEEDED_P (fn))
2374 /* Set DECL_EXTERNAL so that assemble_external will be called as
2375 necessary. We'll clear it again in finish_file. */
2376 if (!DECL_EXTERNAL (fn))
2378 DECL_NOT_REALLY_EXTERN (fn) = 1;
2379 DECL_EXTERNAL (fn) = 1;
2381 /* Remember this function. In finish_file we'll decide if
2382 we actually need to write this function out. */
2384 /* Let the back-end know that this function exists. */
2385 (*debug_hooks->deferred_inline_function) (fn);
2389 /* Compute the appropriate object-file linkage for inline
2391 if (DECL_DECLARED_INLINE_P (fn))
2392 import_export_decl (fn);
2394 /* If FN is external, then there's no point in generating RTL for
2395 it. This situation can arise with an inline function under
2396 `-fexternal-templates'; we instantiate the function, even though
2397 we're not planning on emitting it, in case we get a chance to
2399 if (DECL_EXTERNAL (fn))
2402 /* Save the current file name and line number. When we expand the
2403 body of the function, we'll set LINENO and INPUT_FILENAME so that
2404 error-mesages come out in the right places. */
2405 saved_lineno = lineno;
2406 saved_input_filename = input_filename;
2407 saved_function = current_function_decl;
2408 lineno = DECL_SOURCE_LINE (fn);
2409 input_filename = DECL_SOURCE_FILE (fn);
2410 current_function_decl = fn;
2412 timevar_push (TV_INTEGRATION);
2414 /* Optimize the body of the function before expanding it. */
2415 optimize_function (fn);
2417 timevar_pop (TV_INTEGRATION);
2418 timevar_push (TV_EXPAND);
2420 genrtl_start_function (fn);
2421 current_function_is_thunk = DECL_THUNK_P (fn);
2423 /* Expand the body. */
2424 expand_stmt (DECL_SAVED_TREE (fn));
2426 /* Statements should always be full-expressions at the outermost set
2427 of curly braces for a function. */
2428 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2430 /* The outermost statement for a function contains the line number
2431 recorded when we finished processing the function. */
2432 lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2434 /* Generate code for the function. */
2435 genrtl_finish_function (fn);
2437 /* If possible, obliterate the body of the function so that it can
2438 be garbage collected. */
2439 if (dump_enabled_p (TDI_all))
2440 /* Keep the body; we're going to dump it. */
2442 else if (DECL_INLINE (fn) && flag_inline_trees)
2443 /* We might need the body of this function so that we can expand
2444 it inline somewhere else. */
2447 /* We don't need the body; blow it away. */
2448 DECL_SAVED_TREE (fn) = NULL_TREE;
2450 /* And restore the current source position. */
2451 current_function_decl = saved_function;
2452 lineno = saved_lineno;
2453 input_filename = saved_input_filename;
2454 extract_interface_info ();
2456 timevar_pop (TV_EXPAND);
2458 /* Emit any thunks that should be emitted at the same time as FN. */
2459 emit_associated_thunks (fn);
2462 /* Helper function for walk_tree, used by finish_function to override all
2463 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2464 value optimization. */
2467 nullify_returns_r (tp, walk_subtrees, data)
2472 tree nrv = (tree) data;
2474 /* No need to walk into types. There wouldn't be any need to walk into
2475 non-statements, except that we have to consider STMT_EXPRs. */
2478 else if (TREE_CODE (*tp) == RETURN_STMT)
2479 RETURN_STMT_EXPR (*tp) = NULL_TREE;
2480 else if (TREE_CODE (*tp) == CLEANUP_STMT
2481 && CLEANUP_DECL (*tp) == nrv)
2482 CLEANUP_EH_ONLY (*tp) = 1;
2484 /* Keep iterating. */
2488 /* Start generating the RTL for FN. */
2491 genrtl_start_function (fn)
2494 /* Tell everybody what function we're processing. */
2495 current_function_decl = fn;
2496 /* Get the RTL machinery going for this function. */
2497 init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2498 /* Let everybody know that we're expanding this function, not doing
2499 semantic analysis. */
2502 /* Even though we're inside a function body, we still don't want to
2503 call expand_expr to calculate the size of a variable-sized array.
2504 We haven't necessarily assigned RTL to all variables yet, so it's
2505 not safe to try to expand expressions involving them. */
2506 immediate_size_expand = 0;
2507 cfun->x_dont_save_pending_sizes_p = 1;
2509 /* Let the user know we're compiling this function. */
2510 announce_function (fn);
2512 /* Initialize the per-function data. */
2513 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2514 if (DECL_SAVED_FUNCTION_DATA (fn))
2516 /* If we already parsed this function, and we're just expanding it
2517 now, restore saved state. */
2518 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2520 /* This function is being processed in whole-function mode; we
2521 already did semantic analysis. */
2522 cfun->x_whole_function_mode_p = 1;
2524 /* If we decided that we didn't want to inline this function,
2525 make sure the back-end knows that. */
2526 if (!current_function_cannot_inline)
2527 current_function_cannot_inline = cp_function_chain->cannot_inline;
2529 /* We don't need the saved data anymore. Unless this is an inline
2530 function; we need the named return value info for
2531 cp_copy_res_decl_for_inlining. */
2532 if (! DECL_INLINE (fn))
2533 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2536 /* Keep track of how many functions we're presently expanding. */
2539 /* Create a binding level for the parameters. */
2540 expand_function_start (fn, /*parms_have_cleanups=*/0);
2541 /* If this function is `main'. */
2542 if (DECL_MAIN_P (fn))
2543 expand_main_function ();
2545 /* Give our named return value the same RTL as our RESULT_DECL. */
2546 if (current_function_return_value)
2547 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2550 /* Finish generating the RTL for FN. */
2553 genrtl_finish_function (fn)
2559 if (write_symbols != NO_DEBUG)
2561 /* Keep this code around in case we later want to control debug info
2562 based on whether a type is "used". (jason 1999-11-11) */
2564 tree ttype = target_type (fntype);
2567 if (IS_AGGR_TYPE (ttype))
2568 /* Let debugger know it should output info for this type. */
2569 note_debug_info_needed (ttype);
2571 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2573 ttype = target_type (TREE_TYPE (parmdecl));
2574 if (IS_AGGR_TYPE (ttype))
2575 /* Let debugger know it should output info for this type. */
2576 note_debug_info_needed (ttype);
2581 /* Clean house because we will need to reorder insns here. */
2582 do_pending_stack_adjust ();
2584 /* If we have a named return value, we need to force a return so that
2585 the return register is USEd. */
2586 if (DECL_NAME (DECL_RESULT (fn)))
2587 emit_jump (return_label);
2589 /* We hard-wired immediate_size_expand to zero in start_function.
2590 Expand_function_end will decrement this variable. So, we set the
2591 variable to one here, so that after the decrement it will remain
2593 immediate_size_expand = 1;
2595 /* Generate rtl for function exit. */
2596 expand_function_end (input_filename, lineno, 0);
2598 /* If this is a nested function (like a template instantiation that
2599 we're compiling in the midst of compiling something else), push a
2600 new GC context. That will keep local variables on the stack from
2601 being collected while we're doing the compilation of this
2603 if (function_depth > 1)
2604 ggc_push_context ();
2606 /* There's no need to defer outputting this function any more; we
2607 know we want to output it. */
2608 DECL_DEFER_OUTPUT (fn) = 0;
2610 /* Run the optimizers and output the assembler code for this
2612 rest_of_compilation (fn);
2614 /* Undo the call to ggc_push_context above. */
2615 if (function_depth > 1)
2619 /* Keep this code around in case we later want to control debug info
2620 based on whether a type is "used". (jason 1999-11-11) */
2622 if (ctype && TREE_ASM_WRITTEN (fn))
2623 note_debug_info_needed (ctype);
2626 /* If this function is marked with the constructor attribute, add it
2627 to the list of functions to be called along with constructors
2628 from static duration objects. */
2629 if (DECL_STATIC_CONSTRUCTOR (fn))
2630 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2632 /* If this function is marked with the destructor attribute, add it
2633 to the list of functions to be called along with destructors from
2634 static duration objects. */
2635 if (DECL_STATIC_DESTRUCTOR (fn))
2636 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2640 /* In C++, we should never be saving RTL for the function. */
2641 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2643 /* Since we don't need the RTL for this function anymore, stop
2644 pointing to it. That's especially important for LABEL_DECLs,
2645 since you can reach all the instructions in the function from the
2646 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2647 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2649 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2653 /* Clear out the RTL for the arguments. */
2654 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2656 SET_DECL_RTL (t, NULL_RTX);
2657 DECL_INCOMING_RTL (t) = NULL_RTX;
2660 if (!(flag_inline_trees && DECL_INLINE (fn)))
2661 /* DECL_INITIAL must remain nonzero so we know this was an
2662 actual function definition. */
2663 DECL_INITIAL (fn) = error_mark_node;
2665 /* Let the error reporting routines know that we're outside a
2666 function. For a nested function, this value is used in
2667 pop_cp_function_context and then reset via pop_function_context. */
2668 current_function_decl = NULL_TREE;
2671 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2675 clear_decl_rtl (tp, walk_subtrees, data)
2677 int *walk_subtrees ATTRIBUTE_UNUSED;
2678 void *data ATTRIBUTE_UNUSED;
2680 if (nonstatic_local_decl_p (*tp))
2681 SET_DECL_RTL (*tp, NULL_RTX);
2686 /* Perform initialization related to this module. */
2689 init_cp_semantics ()
2691 lang_expand_stmt = cp_expand_stmt;