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,
7 2003 Free Software Foundation, Inc.
8 Written by Mark Mitchell (mmitchell@usa.net) based on code found
9 formerly in parse.y and pt.c.
11 This file is part of GCC.
13 GCC is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
18 GCC is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING. If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 #include "coretypes.h"
34 #include "tree-inline.h"
46 /* There routines provide a modular interface to perform many parsing
47 operations. They may therefore be used during actual parsing, or
48 during template instantiation, which may be regarded as a
49 degenerate form of parsing. Since the current g++ parser is
50 lacking in several respects, and will be reimplemented, we are
51 attempting to move most code that is not directly related to
52 parsing into this file; that will make implementing the new parser
53 much easier since it will be able to make use of these routines. */
55 static tree maybe_convert_cond (tree);
56 static tree simplify_aggr_init_exprs_r (tree *, int *, void *);
57 static void emit_associated_thunks (tree);
58 static void genrtl_try_block (tree);
59 static void genrtl_eh_spec_block (tree);
60 static void genrtl_handler (tree);
61 static void cp_expand_stmt (tree);
62 static void genrtl_start_function (tree);
63 static void genrtl_finish_function (tree);
64 static tree clear_decl_rtl (tree *, int *, void *);
66 /* Finish processing the COND, the SUBSTMT condition for STMT. */
68 #define FINISH_COND(COND, STMT, SUBSTMT) \
70 if (last_tree != (STMT)) \
72 RECHAIN_STMTS (STMT, SUBSTMT); \
73 if (!processing_template_decl) \
75 (COND) = build_tree_list (SUBSTMT, COND); \
83 /* Deferred Access Checking Overview
84 ---------------------------------
86 Most C++ expressions and declarations require access checking
87 to be performed during parsing. However, in several cases,
88 this has to be treated differently.
90 For member declarations, access checking has to be deferred
91 until more information about the declaration is known. For
103 When we are parsing the function return type `A::X', we don't
104 really know if this is allowed until we parse the function name.
106 Furthermore, some contexts require that access checking is
107 never performed at all. These include class heads, and template
110 Typical use of access checking functions is described here:
112 1. When we enter a context that requires certain access checking
113 mode, the function `push_deferring_access_checks' is called with
114 DEFERRING argument specifying the desired mode. Access checking
115 may be performed immediately (dk_no_deferred), deferred
116 (dk_deferred), or not performed (dk_no_check).
118 2. When a declaration such as a type, or a variable, is encountered,
119 the function `perform_or_defer_access_check' is called. It
120 maintains a TREE_LIST of all deferred checks.
122 3. The global `current_class_type' or `current_function_decl' is then
123 setup by the parser. `enforce_access' relies on these information
126 4. Upon exiting the context mentioned in step 1,
127 `perform_deferred_access_checks' is called to check all declaration
128 stored in the TREE_LIST. `pop_deferring_access_checks' is then
129 called to restore the previous access checking mode.
131 In case of parsing error, we simply call `pop_deferring_access_checks'
132 without `perform_deferred_access_checks'. */
134 /* Data for deferred access checking. */
135 static GTY(()) deferred_access *deferred_access_stack;
136 static GTY(()) deferred_access *deferred_access_free_list;
138 /* Save the current deferred access states and start deferred
139 access checking iff DEFER_P is true. */
141 void push_deferring_access_checks (deferring_kind deferring)
145 /* For context like template instantiation, access checking
146 disabling applies to all nested context. */
147 if (deferred_access_stack
148 && deferred_access_stack->deferring_access_checks_kind == dk_no_check)
149 deferring = dk_no_check;
151 /* Recycle previously used free store if available. */
152 if (deferred_access_free_list)
154 d = deferred_access_free_list;
155 deferred_access_free_list = d->next;
158 d = ggc_alloc (sizeof (deferred_access));
160 d->next = deferred_access_stack;
161 d->deferred_access_checks = NULL_TREE;
162 d->deferring_access_checks_kind = deferring;
163 deferred_access_stack = d;
166 /* Resume deferring access checks again after we stopped doing
169 void resume_deferring_access_checks (void)
171 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
172 deferred_access_stack->deferring_access_checks_kind = dk_deferred;
175 /* Stop deferring access checks. */
177 void stop_deferring_access_checks (void)
179 if (deferred_access_stack->deferring_access_checks_kind == dk_deferred)
180 deferred_access_stack->deferring_access_checks_kind = dk_no_deferred;
183 /* Discard the current deferred access checks and restore the
186 void pop_deferring_access_checks (void)
188 deferred_access *d = deferred_access_stack;
189 deferred_access_stack = d->next;
191 /* Remove references to access checks TREE_LIST. */
192 d->deferred_access_checks = NULL_TREE;
194 /* Store in free list for later use. */
195 d->next = deferred_access_free_list;
196 deferred_access_free_list = d;
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 tree get_deferred_access_checks (void)
206 return deferred_access_stack->deferred_access_checks;
209 /* Take current deferred checks and combine with the
210 previous states if we also defer checks previously.
211 Otherwise perform checks now. */
213 void pop_to_parent_deferring_access_checks (void)
215 tree deferred_check = get_deferred_access_checks ();
216 deferred_access *d1 = deferred_access_stack;
217 deferred_access *d2 = deferred_access_stack->next;
218 deferred_access *d3 = deferred_access_stack->next->next;
220 /* Temporary swap the order of the top two states, just to make
221 sure the garbage collector will not reclaim the memory during
223 deferred_access_stack = d2;
227 for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
228 /* Perform deferred check if required. */
229 perform_or_defer_access_check (TREE_PURPOSE (deferred_check),
230 TREE_VALUE (deferred_check));
232 deferred_access_stack = d1;
235 pop_deferring_access_checks ();
238 /* Perform the deferred access checks.
240 After performing the checks, we still have to keep the list
241 `deferred_access_stack->deferred_access_checks' since we may want
242 to check access for them again later in a different context.
249 A::X A::a, x; // No error for `A::a', error for `x'
251 We have to perform deferred access of `A::X', first with `A::a',
254 void perform_deferred_access_checks (void)
257 for (deferred_check = deferred_access_stack->deferred_access_checks;
259 deferred_check = TREE_CHAIN (deferred_check))
261 enforce_access (TREE_PURPOSE (deferred_check),
262 TREE_VALUE (deferred_check));
265 /* Defer checking the accessibility of DECL, when looked up in
268 void perform_or_defer_access_check (tree binfo, tree decl)
272 my_friendly_assert (TREE_CODE (binfo) == TREE_VEC, 20030623);
274 /* If we are not supposed to defer access checks, just check now. */
275 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
277 enforce_access (binfo, decl);
280 /* Exit if we are in a context that no access checking is performed. */
281 else if (deferred_access_stack->deferring_access_checks_kind == dk_no_check)
284 /* See if we are already going to perform this check. */
285 for (check = deferred_access_stack->deferred_access_checks;
287 check = TREE_CHAIN (check))
288 if (TREE_VALUE (check) == decl && TREE_PURPOSE (check) == binfo)
290 /* If not, record the check. */
291 deferred_access_stack->deferred_access_checks
292 = tree_cons (binfo, decl,
293 deferred_access_stack->deferred_access_checks);
296 /* Returns nonzero if the current statement is a full expression,
297 i.e. temporaries created during that statement should be destroyed
298 at the end of the statement. */
301 stmts_are_full_exprs_p (void)
303 return current_stmt_tree ()->stmts_are_full_exprs_p;
306 /* Returns the stmt_tree (if any) to which statements are currently
307 being added. If there is no active statement-tree, NULL is
311 current_stmt_tree (void)
314 ? &cfun->language->base.x_stmt_tree
315 : &scope_chain->x_stmt_tree);
318 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
319 flag for this because "A union for which objects or pointers are
320 declared is not an anonymous union" [class.union]. */
323 anon_aggr_type_p (tree node)
325 return ANON_AGGR_TYPE_P (node);
328 /* Finish a scope. */
333 tree block = NULL_TREE;
335 if (stmts_are_full_exprs_p ())
337 tree scope_stmts = NULL_TREE;
339 block = poplevel (kept_level_p (), 1, 0);
340 if (!processing_template_decl)
342 /* This needs to come after the poplevel so that partial scopes
343 are properly nested. */
344 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
347 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
348 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
356 /* Begin a new scope. */
359 do_pushlevel (scope_kind sk)
361 if (stmts_are_full_exprs_p ())
363 if (!processing_template_decl)
364 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
369 /* Finish a goto-statement. */
372 finish_goto_stmt (tree destination)
374 if (TREE_CODE (destination) == IDENTIFIER_NODE)
375 destination = lookup_label (destination);
377 /* We warn about unused labels with -Wunused. That means we have to
378 mark the used labels as used. */
379 if (TREE_CODE (destination) == LABEL_DECL)
380 TREE_USED (destination) = 1;
382 if (TREE_CODE (destination) != LABEL_DECL)
383 /* We don't inline calls to functions with computed gotos.
384 Those functions are typically up to some funny business,
385 and may be depending on the labels being at particular
386 addresses, or some such. */
387 DECL_UNINLINABLE (current_function_decl) = 1;
389 check_goto (destination);
391 return add_stmt (build_stmt (GOTO_STMT, destination));
394 /* COND is the condition-expression for an if, while, etc.,
395 statement. Convert it to a boolean value, if appropriate. */
398 maybe_convert_cond (tree cond)
400 /* Empty conditions remain empty. */
404 /* Wait until we instantiate templates before doing conversion. */
405 if (processing_template_decl)
408 /* Do the conversion. */
409 cond = convert_from_reference (cond);
410 return condition_conversion (cond);
413 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
416 finish_expr_stmt (tree expr)
420 if (expr != NULL_TREE)
422 if (!processing_template_decl)
423 expr = convert_to_void (expr, "statement");
425 r = add_stmt (build_stmt (EXPR_STMT, expr));
434 /* Begin an if-statement. Returns a newly created IF_STMT if
441 do_pushlevel (sk_block);
442 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
447 /* Process the COND of an if-statement, which may be given by
451 finish_if_stmt_cond (tree cond, tree if_stmt)
453 cond = maybe_convert_cond (cond);
454 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
457 /* Finish the then-clause of an if-statement, which may be given by
461 finish_then_clause (tree if_stmt)
463 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
467 /* Begin the else-clause of an if-statement. */
470 begin_else_clause (void)
474 /* Finish the else-clause of an if-statement, which may be given by
478 finish_else_clause (tree if_stmt)
480 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
483 /* Finish an if-statement. */
486 finish_if_stmt (void)
492 /* Begin a while-statement. Returns a newly created WHILE_STMT if
496 begin_while_stmt (void)
499 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
501 do_pushlevel (sk_block);
505 /* Process the COND of a while-statement, which may be given by
509 finish_while_stmt_cond (tree cond, tree while_stmt)
511 cond = maybe_convert_cond (cond);
512 if (processing_template_decl)
513 /* Don't mess with condition decls in a template. */
514 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
515 else if (getdecls () == NULL_TREE)
516 /* It was a simple condition; install it. */
517 WHILE_COND (while_stmt) = cond;
520 /* If there was a declaration in the condition, we can't leave it
524 while (true) { A x = 42; if (!x) break; } */
526 WHILE_COND (while_stmt) = boolean_true_node;
528 if_stmt = begin_if_stmt ();
529 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
530 finish_if_stmt_cond (cond, if_stmt);
531 finish_break_stmt ();
532 finish_then_clause (if_stmt);
537 /* Finish a while-statement, which may be given by WHILE_STMT. */
540 finish_while_stmt (tree while_stmt)
543 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
547 /* Begin a do-statement. Returns a newly created DO_STMT if
553 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
558 /* Finish the body of a do-statement, which may be given by DO_STMT. */
561 finish_do_body (tree do_stmt)
563 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
566 /* Finish a do-statement, which may be given by DO_STMT, and whose
567 COND is as indicated. */
570 finish_do_stmt (tree cond, tree do_stmt)
572 cond = maybe_convert_cond (cond);
573 DO_COND (do_stmt) = cond;
577 /* Finish a return-statement. The EXPRESSION returned, if any, is as
581 finish_return_stmt (tree expr)
585 expr = check_return_expr (expr);
586 if (!processing_template_decl)
588 if (DECL_DESTRUCTOR_P (current_function_decl))
590 /* Similarly, all destructors must run destructors for
591 base-classes before returning. So, all returns in a
592 destructor get sent to the DTOR_LABEL; finish_function emits
593 code to return a value there. */
594 return finish_goto_stmt (dtor_label);
597 r = add_stmt (build_stmt (RETURN_STMT, expr));
603 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
606 begin_for_stmt (void)
610 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
611 NULL_TREE, NULL_TREE);
612 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
613 if (NEW_FOR_SCOPE_P (r))
614 do_pushlevel (sk_for);
620 /* Finish the for-init-statement of a for-statement, which may be
621 given by FOR_STMT. */
624 finish_for_init_stmt (tree for_stmt)
626 if (last_tree != for_stmt)
627 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
628 do_pushlevel (sk_block);
631 /* Finish the COND of a for-statement, which may be given by
635 finish_for_cond (tree cond, tree for_stmt)
637 cond = maybe_convert_cond (cond);
638 if (processing_template_decl)
639 /* Don't mess with condition decls in a template. */
640 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
641 else if (getdecls () == NULL_TREE)
642 /* It was a simple condition; install it. */
643 FOR_COND (for_stmt) = cond;
646 /* If there was a declaration in the condition, we can't leave it
648 for (; A x = 42;) { }
650 for (;;) { A x = 42; if (!x) break; } */
652 FOR_COND (for_stmt) = NULL_TREE;
654 if_stmt = begin_if_stmt ();
655 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
656 finish_if_stmt_cond (cond, if_stmt);
657 finish_break_stmt ();
658 finish_then_clause (if_stmt);
663 /* Finish the increment-EXPRESSION in a for-statement, which may be
664 given by FOR_STMT. */
667 finish_for_expr (tree expr, tree for_stmt)
669 FOR_EXPR (for_stmt) = expr;
672 /* Finish the body of a for-statement, which may be given by
673 FOR_STMT. The increment-EXPR for the loop must be
677 finish_for_stmt (tree for_stmt)
679 /* Pop the scope for the body of the loop. */
681 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
682 if (NEW_FOR_SCOPE_P (for_stmt))
687 /* Finish a break-statement. */
690 finish_break_stmt (void)
692 return add_stmt (build_break_stmt ());
695 /* Finish a continue-statement. */
698 finish_continue_stmt (void)
700 return add_stmt (build_continue_stmt ());
703 /* Begin a switch-statement. Returns a new SWITCH_STMT if
707 begin_switch_stmt (void)
710 do_pushlevel (sk_block);
711 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
716 /* Finish the cond of a switch-statement. */
719 finish_switch_cond (tree cond, tree switch_stmt)
721 tree orig_type = NULL;
722 if (!processing_template_decl)
726 /* Convert the condition to an integer or enumeration type. */
727 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
728 if (cond == NULL_TREE)
730 error ("switch quantity not an integer");
731 cond = error_mark_node;
733 orig_type = TREE_TYPE (cond);
734 if (cond != error_mark_node)
738 Integral promotions are performed. */
739 cond = perform_integral_promotions (cond);
740 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
743 if (cond != error_mark_node)
745 index = get_unwidened (cond, NULL_TREE);
746 /* We can't strip a conversion from a signed type to an unsigned,
747 because if we did, int_fits_type_p would do the wrong thing
748 when checking case values for being in range,
749 and it's too hard to do the right thing. */
750 if (TREE_UNSIGNED (TREE_TYPE (cond))
751 == TREE_UNSIGNED (TREE_TYPE (index)))
755 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
756 SWITCH_TYPE (switch_stmt) = orig_type;
757 push_switch (switch_stmt);
760 /* Finish the body of a switch-statement, which may be given by
761 SWITCH_STMT. The COND to switch on is indicated. */
764 finish_switch_stmt (tree switch_stmt)
766 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
772 /* Generate the RTL for T, which is a TRY_BLOCK. */
775 genrtl_try_block (tree t)
779 expand_eh_region_start ();
780 expand_stmt (TRY_STMTS (t));
781 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
785 if (!FN_TRY_BLOCK_P (t))
786 emit_line_note (input_location);
788 expand_eh_region_start ();
789 expand_stmt (TRY_STMTS (t));
791 if (FN_TRY_BLOCK_P (t))
793 expand_start_all_catch ();
794 in_function_try_handler = 1;
795 expand_stmt (TRY_HANDLERS (t));
796 in_function_try_handler = 0;
797 expand_end_all_catch ();
801 expand_start_all_catch ();
802 expand_stmt (TRY_HANDLERS (t));
803 expand_end_all_catch ();
808 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
811 genrtl_eh_spec_block (tree t)
813 expand_eh_region_start ();
814 expand_stmt (EH_SPEC_STMTS (t));
815 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
816 build_call (call_unexpected_node,
817 tree_cons (NULL_TREE,
822 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
826 begin_try_block (void)
828 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
833 /* Likewise, for a function-try-block. */
836 begin_function_try_block (void)
838 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
839 FN_TRY_BLOCK_P (r) = 1;
844 /* Finish a try-block, which may be given by TRY_BLOCK. */
847 finish_try_block (tree try_block)
849 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
852 /* Finish the body of a cleanup try-block, which may be given by
856 finish_cleanup_try_block (tree try_block)
858 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
861 /* Finish an implicitly generated try-block, with a cleanup is given
865 finish_cleanup (tree cleanup, tree try_block)
867 TRY_HANDLERS (try_block) = cleanup;
868 CLEANUP_P (try_block) = 1;
871 /* Likewise, for a function-try-block. */
874 finish_function_try_block (tree try_block)
876 if (TREE_CHAIN (try_block)
877 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
879 /* Chain the compound statement after the CTOR_INITIALIZER. */
880 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
881 /* And make the CTOR_INITIALIZER the body of the try-block. */
882 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
885 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
886 in_function_try_handler = 1;
889 /* Finish a handler-sequence for a try-block, which may be given by
893 finish_handler_sequence (tree try_block)
895 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
896 check_handlers (TRY_HANDLERS (try_block));
899 /* Likewise, for a function-try-block. */
902 finish_function_handler_sequence (tree try_block)
904 in_function_try_handler = 0;
905 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
906 check_handlers (TRY_HANDLERS (try_block));
909 /* Generate the RTL for T, which is a HANDLER. */
912 genrtl_handler (tree t)
914 genrtl_do_pushlevel ();
915 if (!processing_template_decl)
916 expand_start_catch (HANDLER_TYPE (t));
917 expand_stmt (HANDLER_BODY (t));
918 if (!processing_template_decl)
922 /* Begin a handler. Returns a HANDLER if appropriate. */
928 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
930 /* Create a binding level for the eh_info and the exception object
932 do_pushlevel (sk_catch);
936 /* Finish the handler-parameters for a handler, which may be given by
937 HANDLER. DECL is the declaration for the catch parameter, or NULL
938 if this is a `catch (...)' clause. */
941 finish_handler_parms (tree decl, tree handler)
943 tree type = NULL_TREE;
944 if (processing_template_decl)
948 decl = pushdecl (decl);
949 decl = push_template_decl (decl);
950 add_decl_stmt (decl);
951 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
952 type = TREE_TYPE (decl);
956 type = expand_start_catch_block (decl);
958 HANDLER_TYPE (handler) = type;
960 mark_used (eh_type_info (type));
963 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
964 the return value from the matching call to finish_handler_parms. */
967 finish_handler (tree handler)
969 if (!processing_template_decl)
970 expand_end_catch_block ();
972 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
975 /* Begin a compound-statement. If HAS_NO_SCOPE is true, the
976 compound-statement does not define a scope. Returns a new
980 begin_compound_stmt (bool has_no_scope)
985 r = build_stmt (COMPOUND_STMT, NULL_TREE);
987 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
992 COMPOUND_STMT_NO_SCOPE (r) = 1;
994 last_expr_type = NULL_TREE;
997 do_pushlevel (is_try ? sk_try : sk_block);
999 /* Normally, we try hard to keep the BLOCK for a
1000 statement-expression. But, if it's a statement-expression with
1001 a scopeless block, there's nothing to keep, and we don't want
1002 to accidentally keep a block *inside* the scopeless block. */
1003 keep_next_level (0);
1008 /* Finish a compound-statement, which is given by COMPOUND_STMT. */
1011 finish_compound_stmt (tree compound_stmt)
1016 if (COMPOUND_STMT_NO_SCOPE (compound_stmt))
1021 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1023 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1024 the precise purpose of that variable is store the type of the
1025 last expression statement within the last compound statement, we
1026 preserve the value. */
1034 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1035 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1039 finish_asm_stmt (tree cv_qualifier,
1041 tree output_operands,
1042 tree input_operands,
1048 if (cv_qualifier != NULL_TREE
1049 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1051 warning ("%s qualifier ignored on asm",
1052 IDENTIFIER_POINTER (cv_qualifier));
1053 cv_qualifier = NULL_TREE;
1056 if (!processing_template_decl)
1062 for (t = input_operands; t; t = TREE_CHAIN (t))
1064 tree converted_operand
1065 = decay_conversion (TREE_VALUE (t));
1067 /* If the type of the operand hasn't been determined (e.g.,
1068 because it involves an overloaded function), then issue
1069 an error message. There's no context available to
1070 resolve the overloading. */
1071 if (TREE_TYPE (converted_operand) == unknown_type_node)
1073 error ("type of asm operand `%E' could not be determined",
1075 converted_operand = error_mark_node;
1077 TREE_VALUE (t) = converted_operand;
1080 ninputs = list_length (input_operands);
1081 noutputs = list_length (output_operands);
1083 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1088 const char *constraint;
1091 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1092 operand = TREE_VALUE (t);
1094 if (!parse_output_constraint (&constraint,
1095 i, ninputs, noutputs,
1100 /* By marking this operand as erroneous, we will not try
1101 to process this operand again in expand_asm_operands. */
1102 TREE_VALUE (t) = error_mark_node;
1106 /* If the operand is a DECL that is going to end up in
1107 memory, assume it is addressable. This is a bit more
1108 conservative than it would ideally be; the exact test is
1109 buried deep in expand_asm_operands and depends on the
1110 DECL_RTL for the OPERAND -- which we don't have at this
1112 if (!allows_reg && DECL_P (operand))
1113 cxx_mark_addressable (operand);
1117 r = build_stmt (ASM_STMT, cv_qualifier, string,
1118 output_operands, input_operands,
1120 return add_stmt (r);
1123 /* Finish a label with the indicated NAME. */
1126 finish_label_stmt (tree name)
1128 tree decl = define_label (input_location, name);
1129 return add_stmt (build_stmt (LABEL_STMT, decl));
1132 /* Finish a series of declarations for local labels. G++ allows users
1133 to declare "local" labels, i.e., labels with scope. This extension
1134 is useful when writing code involving statement-expressions. */
1137 finish_label_decl (tree name)
1139 tree decl = declare_local_label (name);
1140 add_decl_stmt (decl);
1143 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1146 finish_decl_cleanup (tree decl, tree cleanup)
1148 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1151 /* If the current scope exits with an exception, run CLEANUP. */
1154 finish_eh_cleanup (tree cleanup)
1156 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1157 CLEANUP_EH_ONLY (r) = 1;
1161 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1162 order they were written by the user. Each node is as for
1163 emit_mem_initializers. */
1166 finish_mem_initializers (tree mem_inits)
1168 /* Reorder the MEM_INITS so that they are in the order they appeared
1169 in the source program. */
1170 mem_inits = nreverse (mem_inits);
1172 if (processing_template_decl)
1173 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1175 emit_mem_initializers (mem_inits);
1178 /* Returns the stack of SCOPE_STMTs for the current function. */
1181 current_scope_stmt_stack (void)
1183 return &cfun->language->base.x_scope_stmt_stack;
1186 /* Finish a parenthesized expression EXPR. */
1189 finish_parenthesized_expr (tree expr)
1191 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1192 /* This inhibits warnings in c_common_truthvalue_conversion. */
1193 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1195 if (TREE_CODE (expr) == OFFSET_REF)
1196 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1197 enclosed in parentheses. */
1198 PTRMEM_OK_P (expr) = 0;
1202 /* Finish a reference to a non-static data member (DECL) that is not
1203 preceded by `.' or `->'. */
1206 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1208 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1212 if (current_function_decl
1213 && DECL_STATIC_FUNCTION_P (current_function_decl))
1214 cp_error_at ("invalid use of member `%D' in static member function",
1217 cp_error_at ("invalid use of non-static data member `%D'", decl);
1218 error ("from this location");
1220 return error_mark_node;
1222 TREE_USED (current_class_ptr) = 1;
1223 if (processing_template_decl)
1225 tree type = TREE_TYPE (decl);
1227 if (TREE_CODE (type) == REFERENCE_TYPE)
1228 type = TREE_TYPE (type);
1231 /* Set the cv qualifiers */
1232 int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1234 if (DECL_MUTABLE_P (decl))
1235 quals &= ~TYPE_QUAL_CONST;
1237 quals |= cp_type_quals (TREE_TYPE (decl));
1238 type = cp_build_qualified_type (type, quals);
1241 return build_min (COMPONENT_REF, type, object, decl);
1245 tree access_type = TREE_TYPE (object);
1246 tree lookup_context = context_for_name_lookup (decl);
1248 while (!DERIVED_FROM_P (lookup_context, access_type))
1250 access_type = TYPE_CONTEXT (access_type);
1251 while (access_type && DECL_P (access_type))
1252 access_type = DECL_CONTEXT (access_type);
1256 cp_error_at ("object missing in reference to `%D'", decl);
1257 error ("from this location");
1258 return error_mark_node;
1262 perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1264 /* If the data member was named `C::M', convert `*this' to `C'
1266 if (qualifying_scope)
1268 tree binfo = NULL_TREE;
1269 object = build_scoped_ref (object, qualifying_scope,
1273 return build_class_member_access_expr (object, decl,
1274 /*access_path=*/NULL_TREE,
1275 /*preserve_reference=*/false);
1279 /* DECL was the declaration to which a qualified-id resolved. Issue
1280 an error message if it is not accessible. If OBJECT_TYPE is
1281 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1282 type of `*x', or `x', respectively. If the DECL was named as
1283 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1286 check_accessibility_of_qualified_id (tree decl,
1288 tree nested_name_specifier)
1291 tree qualifying_type = NULL_TREE;
1293 /* Determine the SCOPE of DECL. */
1294 scope = context_for_name_lookup (decl);
1295 /* If the SCOPE is not a type, then DECL is not a member. */
1296 if (!TYPE_P (scope))
1298 /* Compute the scope through which DECL is being accessed. */
1300 /* OBJECT_TYPE might not be a class type; consider:
1302 class A { typedef int I; };
1306 In this case, we will have "A::I" as the DECL, but "I" as the
1308 && CLASS_TYPE_P (object_type)
1309 && DERIVED_FROM_P (scope, object_type))
1310 /* If we are processing a `->' or `.' expression, use the type of the
1312 qualifying_type = object_type;
1313 else if (nested_name_specifier)
1315 /* If the reference is to a non-static member of the
1316 current class, treat it as if it were referenced through
1318 if (DECL_NONSTATIC_MEMBER_P (decl)
1319 && current_class_ptr
1320 && DERIVED_FROM_P (scope, current_class_type))
1321 qualifying_type = current_class_type;
1322 /* Otherwise, use the type indicated by the
1323 nested-name-specifier. */
1325 qualifying_type = nested_name_specifier;
1328 /* Otherwise, the name must be from the current class or one of
1330 qualifying_type = currently_open_derived_class (scope);
1332 if (qualifying_type)
1333 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1336 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1337 class named to the left of the "::" operator. DONE is true if this
1338 expression is a complete postfix-expression; it is false if this
1339 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1340 iff this expression is the operand of '&'. */
1343 finish_qualified_id_expr (tree qualifying_class, tree expr, bool done,
1346 if (error_operand_p (expr))
1347 return error_mark_node;
1349 /* If EXPR occurs as the operand of '&', use special handling that
1350 permits a pointer-to-member. */
1351 if (address_p && done)
1353 if (TREE_CODE (expr) == SCOPE_REF)
1354 expr = TREE_OPERAND (expr, 1);
1355 expr = build_offset_ref (qualifying_class, expr,
1356 /*address_p=*/true);
1360 if (TREE_CODE (expr) == FIELD_DECL)
1361 expr = finish_non_static_data_member (expr, current_class_ref,
1363 else if (BASELINK_P (expr) && !processing_template_decl)
1368 /* See if any of the functions are non-static members. */
1369 fns = BASELINK_FUNCTIONS (expr);
1370 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1371 fns = TREE_OPERAND (fns, 0);
1372 for (fn = fns; fn; fn = OVL_NEXT (fn))
1373 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1375 /* If so, the expression may be relative to the current
1377 if (fn && current_class_type
1378 && DERIVED_FROM_P (qualifying_class, current_class_type))
1379 expr = (build_class_member_access_expr
1380 (maybe_dummy_object (qualifying_class, NULL),
1382 BASELINK_ACCESS_BINFO (expr),
1383 /*preserve_reference=*/false));
1385 /* The expression is a qualified name whose address is not
1387 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1393 /* Begin a statement-expression. The value returned must be passed to
1394 finish_stmt_expr. */
1397 begin_stmt_expr (void)
1399 /* If we're outside a function, we won't have a statement-tree to
1400 work with. But, if we see a statement-expression we need to
1402 if (! cfun && !last_tree)
1403 begin_stmt_tree (&scope_chain->x_saved_tree);
1405 last_expr_type = NULL_TREE;
1407 keep_next_level (1);
1412 /* Process the final expression of a statement expression. EXPR can be
1413 NULL, if the final expression is empty. Build up a TARGET_EXPR so
1414 that the result value can be safely returned to the enclosing
1418 finish_stmt_expr_expr (tree expr)
1420 tree result = NULL_TREE;
1421 tree type = void_type_node;
1425 type = TREE_TYPE (expr);
1427 if (!processing_template_decl && !VOID_TYPE_P (TREE_TYPE (expr)))
1429 if (TREE_CODE (type) == ARRAY_TYPE
1430 || TREE_CODE (type) == FUNCTION_TYPE)
1431 expr = decay_conversion (expr);
1433 expr = convert_from_reference (expr);
1434 expr = require_complete_type (expr);
1436 /* Build a TARGET_EXPR for this aggregate. finish_stmt_expr
1437 will then pull it apart so the lifetime of the target is
1438 within the scope of the expresson containing this statement
1440 if (TREE_CODE (expr) == TARGET_EXPR)
1442 else if (!IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_INIT_REF (type))
1443 expr = build_target_expr_with_type (expr, type);
1446 /* Copy construct. */
1447 expr = build_special_member_call
1448 (NULL_TREE, complete_ctor_identifier,
1449 build_tree_list (NULL_TREE, expr),
1450 TYPE_BINFO (type), LOOKUP_NORMAL);
1451 expr = build_cplus_new (type, expr);
1452 my_friendly_assert (TREE_CODE (expr) == TARGET_EXPR, 20030729);
1456 if (expr != error_mark_node)
1458 result = build_stmt (EXPR_STMT, expr);
1465 /* Remember the last expression so that finish_stmt_expr can pull it
1467 last_expr_type = result ? result : void_type_node;
1472 /* Finish a statement-expression. EXPR should be the value returned
1473 by the previous begin_stmt_expr. Returns an expression
1474 representing the statement-expression. */
1477 finish_stmt_expr (tree rtl_expr, bool has_no_scope)
1480 tree result_stmt = last_expr_type;
1483 if (!last_expr_type)
1484 type = void_type_node;
1487 if (result_stmt == void_type_node)
1489 type = void_type_node;
1490 result_stmt = NULL_TREE;
1493 type = TREE_TYPE (EXPR_STMT_EXPR (result_stmt));
1496 result = build_min (STMT_EXPR, type, last_tree);
1497 TREE_SIDE_EFFECTS (result) = 1;
1498 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1500 last_expr_type = NULL_TREE;
1502 /* Remove the compound statement from the tree structure; it is
1503 now saved in the STMT_EXPR. */
1504 last_tree = rtl_expr;
1505 TREE_CHAIN (last_tree) = NULL_TREE;
1507 /* If we created a statement-tree for this statement-expression,
1510 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1511 finish_stmt_tree (&scope_chain->x_saved_tree);
1513 if (processing_template_decl)
1516 if (!VOID_TYPE_P (type))
1518 /* Pull out the TARGET_EXPR that is the final expression. Put
1519 the target's init_expr as the final expression and then put
1520 the statement expression itself as the target's init
1521 expr. Finally, return the target expression. */
1522 tree last_expr = EXPR_STMT_EXPR (result_stmt);
1524 my_friendly_assert (TREE_CODE (last_expr) == TARGET_EXPR, 20030729);
1525 EXPR_STMT_EXPR (result_stmt) = TREE_OPERAND (last_expr, 1);
1526 TREE_OPERAND (last_expr, 1) = result;
1532 /* Perform Koenig lookup. FN is the postfix-expression representing
1533 the call; ARGS are the arguments to the call. Returns the
1534 functions to be considered by overload resolution. */
1537 perform_koenig_lookup (tree fn, tree args)
1539 tree identifier = NULL_TREE;
1540 tree functions = NULL_TREE;
1542 /* Find the name of the overloaded function. */
1543 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1545 else if (is_overloaded_fn (fn))
1548 identifier = DECL_NAME (get_first_fn (functions));
1550 else if (DECL_P (fn))
1553 identifier = DECL_NAME (fn);
1556 /* A call to a namespace-scope function using an unqualified name.
1558 Do Koenig lookup -- unless any of the arguments are
1560 if (!any_type_dependent_arguments_p (args))
1562 fn = lookup_arg_dependent (identifier, functions, args);
1564 /* The unqualified name could not be resolved. */
1565 fn = unqualified_fn_lookup_error (identifier);
1573 /* Generate an expression for `FN (ARGS)'.
1575 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1576 as a virtual call, even if FN is virtual. (This flag is set when
1577 encountering an expression where the function name is explicitly
1578 qualified. For example a call to `X::f' never generates a virtual
1581 Returns code for the call. */
1584 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1590 if (fn == error_mark_node || args == error_mark_node)
1591 return error_mark_node;
1593 /* ARGS should be a list of arguments. */
1594 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1600 if (processing_template_decl)
1602 if (type_dependent_expression_p (fn)
1603 || any_type_dependent_arguments_p (args))
1604 return build_nt (CALL_EXPR, fn, args);
1605 if (!BASELINK_P (fn)
1606 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1607 && TREE_TYPE (fn) != unknown_type_node)
1608 fn = build_non_dependent_expr (fn);
1609 args = build_non_dependent_args (orig_args);
1612 /* A reference to a member function will appear as an overloaded
1613 function (rather than a BASELINK) if an unqualified name was used
1615 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1619 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1620 f = get_first_fn (TREE_OPERAND (fn, 0));
1622 f = get_first_fn (fn);
1623 if (DECL_FUNCTION_MEMBER_P (f))
1625 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1626 fn = build_baselink (TYPE_BINFO (type),
1628 fn, /*optype=*/NULL_TREE);
1633 if (BASELINK_P (fn))
1637 /* A call to a member function. From [over.call.func]:
1639 If the keyword this is in scope and refers to the class of
1640 that member function, or a derived class thereof, then the
1641 function call is transformed into a qualified function call
1642 using (*this) as the postfix-expression to the left of the
1643 . operator.... [Otherwise] a contrived object of type T
1644 becomes the implied object argument.
1646 This paragraph is unclear about this situation:
1648 struct A { void f(); };
1649 struct B : public A {};
1650 struct C : public A { void g() { B::f(); }};
1652 In particular, for `B::f', this paragraph does not make clear
1653 whether "the class of that member function" refers to `A' or
1654 to `B'. We believe it refers to `B'. */
1655 if (current_class_type
1656 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1658 && current_class_ref)
1659 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1663 tree representative_fn;
1665 representative_fn = BASELINK_FUNCTIONS (fn);
1666 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1667 representative_fn = TREE_OPERAND (representative_fn, 0);
1668 representative_fn = get_first_fn (representative_fn);
1669 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1672 if (processing_template_decl)
1674 if (type_dependent_expression_p (object))
1675 return build_nt (CALL_EXPR, orig_fn, orig_args);
1676 object = build_non_dependent_expr (object);
1679 result = build_new_method_call (object, fn, args, NULL_TREE,
1681 ? LOOKUP_NONVIRTUAL : 0));
1683 else if (is_overloaded_fn (fn))
1684 /* A call to a namespace-scope function. */
1685 result = build_new_function_call (fn, args);
1686 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1689 error ("arguments to destructor are not allowed");
1690 /* Mark the pseudo-destructor call as having side-effects so
1691 that we do not issue warnings about its use. */
1692 result = build1 (NOP_EXPR,
1694 TREE_OPERAND (fn, 0));
1695 TREE_SIDE_EFFECTS (result) = 1;
1697 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1698 /* If the "function" is really an object of class type, it might
1699 have an overloaded `operator ()'. */
1700 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1702 /* A call where the function is unknown. */
1703 result = build_function_call (fn, args);
1705 if (processing_template_decl)
1706 return build (CALL_EXPR, TREE_TYPE (result), orig_fn, orig_args);
1710 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1711 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1712 POSTDECREMENT_EXPR.) */
1715 finish_increment_expr (tree expr, enum tree_code code)
1717 return build_x_unary_op (code, expr);
1720 /* Finish a use of `this'. Returns an expression for `this'. */
1723 finish_this_expr (void)
1727 if (current_class_ptr)
1729 result = current_class_ptr;
1731 else if (current_function_decl
1732 && DECL_STATIC_FUNCTION_P (current_function_decl))
1734 error ("`this' is unavailable for static member functions");
1735 result = error_mark_node;
1739 if (current_function_decl)
1740 error ("invalid use of `this' in non-member function");
1742 error ("invalid use of `this' at top level");
1743 result = error_mark_node;
1749 /* Finish a member function call using OBJECT and ARGS as arguments to
1750 FN. Returns an expression for the call. */
1753 finish_object_call_expr (tree fn, tree object, tree args)
1755 if (DECL_DECLARES_TYPE_P (fn))
1757 if (processing_template_decl)
1758 /* This can happen on code like:
1761 template <class T> void f(T t) {
1765 We just grab the underlying IDENTIFIER. */
1766 fn = DECL_NAME (fn);
1769 error ("calling type `%T' like a method", fn);
1770 return error_mark_node;
1774 if (processing_template_decl)
1775 return build_nt (CALL_EXPR,
1776 build_nt (COMPONENT_REF, object, fn),
1780 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1782 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1785 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1786 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1787 the TYPE for the type given. If SCOPE is non-NULL, the expression
1788 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1791 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1793 if (destructor == error_mark_node)
1794 return error_mark_node;
1796 my_friendly_assert (TYPE_P (destructor), 20010905);
1798 if (!processing_template_decl)
1800 if (scope == error_mark_node)
1802 error ("invalid qualifying scope in pseudo-destructor name");
1803 return error_mark_node;
1806 if (!same_type_p (TREE_TYPE (object), destructor))
1808 error ("`%E' is not of type `%T'", object, destructor);
1809 return error_mark_node;
1813 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1816 /* Finish an expression of the form CODE EXPR. */
1819 finish_unary_op_expr (enum tree_code code, tree expr)
1821 tree result = build_x_unary_op (code, expr);
1822 /* Inside a template, build_x_unary_op does not fold the
1823 expression. So check whether the result is folded before
1824 setting TREE_NEGATED_INT. */
1825 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1826 && TREE_CODE (result) == INTEGER_CST
1827 && !TREE_UNSIGNED (TREE_TYPE (result))
1828 && INT_CST_LT (result, integer_zero_node))
1829 TREE_NEGATED_INT (result) = 1;
1830 overflow_warning (result);
1834 /* Finish a compound-literal expression. TYPE is the type to which
1835 the INITIALIZER_LIST is being cast. */
1838 finish_compound_literal (tree type, tree initializer_list)
1840 tree compound_literal;
1842 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1843 compound_literal = build_constructor (NULL_TREE, initializer_list);
1844 /* Mark it as a compound-literal. */
1845 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1846 if (processing_template_decl)
1847 TREE_TYPE (compound_literal) = type;
1850 /* Check the initialization. */
1851 compound_literal = digest_init (type, compound_literal, NULL);
1852 /* If the TYPE was an array type with an unknown bound, then we can
1853 figure out the dimension now. For example, something like:
1857 implies that the array has two elements. */
1858 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1859 complete_array_type (type, compound_literal, 1);
1862 return compound_literal;
1865 /* Return the declaration for the function-name variable indicated by
1869 finish_fname (tree id)
1873 decl = fname_decl (C_RID_CODE (id), id);
1874 if (processing_template_decl)
1875 decl = DECL_NAME (decl);
1879 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1880 and DECLARATOR. Returns nonzero if the function-declaration is
1884 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1886 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1889 /* The things we're about to see are not directly qualified by any
1890 template headers we've seen thus far. */
1891 reset_specialization ();
1896 /* Finish a translation unit. */
1899 finish_translation_unit (void)
1901 /* In case there were missing closebraces,
1902 get us back to the global binding level. */
1904 while (current_namespace != global_namespace)
1907 /* Do file scope __FUNCTION__ et al. */
1908 finish_fname_decls ();
1911 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1912 Returns the parameter. */
1915 finish_template_type_parm (tree aggr, tree identifier)
1917 if (aggr != class_type_node)
1919 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1920 aggr = class_type_node;
1923 return build_tree_list (aggr, identifier);
1926 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1927 Returns the parameter. */
1930 finish_template_template_parm (tree aggr, tree identifier)
1932 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1933 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1934 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1935 DECL_TEMPLATE_RESULT (tmpl) = decl;
1936 DECL_ARTIFICIAL (decl) = 1;
1937 end_template_decl ();
1939 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1941 return finish_template_type_parm (aggr, tmpl);
1944 /* ARGUMENT is the default-argument value for a template template
1945 parameter. If ARGUMENT is invalid, issue error messages and return
1946 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1949 check_template_template_default_arg (tree argument)
1951 if (TREE_CODE (argument) != TEMPLATE_DECL
1952 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1953 && TREE_CODE (argument) != TYPE_DECL
1954 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1956 error ("invalid default template argument");
1957 return error_mark_node;
1963 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1964 nonzero, the parameter list was terminated by a `...'. */
1967 finish_parmlist (tree parms, int ellipsis)
1971 /* We mark the PARMS as a parmlist so that declarator processing can
1972 disambiguate certain constructs. */
1973 TREE_PARMLIST (parms) = 1;
1974 /* We do not append void_list_node here, but leave it to grokparms
1976 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1981 /* Begin a class definition, as indicated by T. */
1984 begin_class_definition (tree t)
1986 if (t == error_mark_node)
1987 return error_mark_node;
1989 if (processing_template_parmlist)
1991 error ("definition of `%#T' inside template parameter list", t);
1992 return error_mark_node;
1994 /* A non-implicit typename comes from code like:
1996 template <typename T> struct A {
1997 template <typename U> struct A<T>::B ...
1999 This is erroneous. */
2000 else if (TREE_CODE (t) == TYPENAME_TYPE)
2002 error ("invalid definition of qualified type `%T'", t);
2003 t = error_mark_node;
2006 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
2008 t = make_aggr_type (RECORD_TYPE);
2009 pushtag (make_anon_name (), t, 0);
2012 /* If this type was already complete, and we see another definition,
2014 if (COMPLETE_TYPE_P (t))
2016 error ("redefinition of `%#T'", t);
2017 cp_error_at ("previous definition of `%#T'", t);
2018 return error_mark_node;
2021 /* Update the location of the decl. */
2022 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
2024 if (TYPE_BEING_DEFINED (t))
2026 t = make_aggr_type (TREE_CODE (t));
2027 pushtag (TYPE_IDENTIFIER (t), t, 0);
2029 maybe_process_partial_specialization (t);
2031 TYPE_BEING_DEFINED (t) = 1;
2032 TYPE_PACKED (t) = flag_pack_struct;
2033 /* Reset the interface data, at the earliest possible
2034 moment, as it might have been set via a class foo;
2036 if (! TYPE_ANONYMOUS_P (t))
2038 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2039 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2040 (t, interface_unknown);
2042 reset_specialization();
2044 /* Make a declaration for this class in its own scope. */
2045 build_self_reference ();
2050 /* Finish the member declaration given by DECL. */
2053 finish_member_declaration (tree decl)
2055 if (decl == error_mark_node || decl == NULL_TREE)
2058 if (decl == void_type_node)
2059 /* The COMPONENT was a friend, not a member, and so there's
2060 nothing for us to do. */
2063 /* We should see only one DECL at a time. */
2064 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
2066 /* Set up access control for DECL. */
2068 = (current_access_specifier == access_private_node);
2069 TREE_PROTECTED (decl)
2070 = (current_access_specifier == access_protected_node);
2071 if (TREE_CODE (decl) == TEMPLATE_DECL)
2073 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2074 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2077 /* Mark the DECL as a member of the current class. */
2078 DECL_CONTEXT (decl) = current_class_type;
2082 A C language linkage is ignored for the names of class members
2083 and the member function type of class member functions. */
2084 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2085 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2087 /* Put functions on the TYPE_METHODS list and everything else on the
2088 TYPE_FIELDS list. Note that these are built up in reverse order.
2089 We reverse them (to obtain declaration order) in finish_struct. */
2090 if (TREE_CODE (decl) == FUNCTION_DECL
2091 || DECL_FUNCTION_TEMPLATE_P (decl))
2093 /* We also need to add this function to the
2094 CLASSTYPE_METHOD_VEC. */
2095 add_method (current_class_type, decl, /*error_p=*/0);
2097 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2098 TYPE_METHODS (current_class_type) = decl;
2100 maybe_add_class_template_decl_list (current_class_type, decl,
2103 /* Enter the DECL into the scope of the class. */
2104 else if ((TREE_CODE (decl) == USING_DECL && TREE_TYPE (decl))
2105 || pushdecl_class_level (decl))
2107 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2108 go at the beginning. The reason is that lookup_field_1
2109 searches the list in order, and we want a field name to
2110 override a type name so that the "struct stat hack" will
2111 work. In particular:
2113 struct S { enum E { }; int E } s;
2116 is valid. In addition, the FIELD_DECLs must be maintained in
2117 declaration order so that class layout works as expected.
2118 However, we don't need that order until class layout, so we
2119 save a little time by putting FIELD_DECLs on in reverse order
2120 here, and then reversing them in finish_struct_1. (We could
2121 also keep a pointer to the correct insertion points in the
2124 if (TREE_CODE (decl) == TYPE_DECL)
2125 TYPE_FIELDS (current_class_type)
2126 = chainon (TYPE_FIELDS (current_class_type), decl);
2129 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2130 TYPE_FIELDS (current_class_type) = decl;
2133 maybe_add_class_template_decl_list (current_class_type, decl,
2138 /* Finish processing the declaration of a member class template
2139 TYPES whose template parameters are given by PARMS. */
2142 finish_member_class_template (tree types)
2146 /* If there are declared, but undefined, partial specializations
2147 mixed in with the typespecs they will not yet have passed through
2148 maybe_process_partial_specialization, so we do that here. */
2149 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2150 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2151 maybe_process_partial_specialization (TREE_VALUE (t));
2153 grok_x_components (types);
2154 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2155 /* The component was in fact a friend declaration. We avoid
2156 finish_member_template_decl performing certain checks by
2160 finish_member_template_decl (types);
2162 /* As with other component type declarations, we do
2163 not store the new DECL on the list of
2168 /* Finish processing a complete template declaration. The PARMS are
2169 the template parameters. */
2172 finish_template_decl (tree parms)
2175 end_template_decl ();
2177 end_specialization ();
2180 /* Finish processing a template-id (which names a type) of the form
2181 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2182 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2183 the scope of template-id indicated. */
2186 finish_template_type (tree name, tree args, int entering_scope)
2190 decl = lookup_template_class (name, args,
2191 NULL_TREE, NULL_TREE, entering_scope,
2192 tf_error | tf_warning | tf_user);
2193 if (decl != error_mark_node)
2194 decl = TYPE_STUB_DECL (decl);
2199 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2200 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2201 BASE_CLASS, or NULL_TREE if an error occurred. The
2202 ACCESS_SPECIFIER is one of
2203 access_{default,public,protected_private}[_virtual]_node.*/
2206 finish_base_specifier (tree base, tree access, bool virtual_p)
2210 if (base == error_mark_node)
2212 error ("invalid base-class specification");
2215 else if (! is_aggr_type (base, 1))
2219 if (cp_type_quals (base) != 0)
2221 error ("base class `%T' has cv qualifiers", base);
2222 base = TYPE_MAIN_VARIANT (base);
2224 result = build_tree_list (access, base);
2225 TREE_VIA_VIRTUAL (result) = virtual_p;
2231 /* Called when multiple declarators are processed. If that is not
2232 premitted in this context, an error is issued. */
2235 check_multiple_declarators (void)
2239 In a template-declaration, explicit specialization, or explicit
2240 instantiation the init-declarator-list in the declaration shall
2241 contain at most one declarator.
2243 We don't just use PROCESSING_TEMPLATE_DECL for the first
2244 condition since that would disallow the perfectly valid code,
2245 like `template <class T> struct S { int i, j; };'. */
2246 if (at_function_scope_p ())
2247 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2250 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2251 || processing_explicit_instantiation
2252 || processing_specialization)
2253 error ("multiple declarators in template declaration");
2256 /* Issue a diagnostic that NAME cannot be found in SCOPE. */
2259 qualified_name_lookup_error (tree scope, tree name)
2263 if (!COMPLETE_TYPE_P (scope))
2264 error ("incomplete type `%T' used in nested name specifier", scope);
2266 error ("`%D' is not a member of `%T'", name, scope);
2268 else if (scope != global_namespace)
2269 error ("`%D' is not a member of `%D'", name, scope);
2271 error ("`::%D' has not been declared", name);
2274 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2275 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2276 if non-NULL, is the type or namespace used to explicitly qualify
2277 ID_EXPRESSION. DECL is the entity to which that name has been
2280 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2281 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2282 be set to true if this expression isn't permitted in a
2283 constant-expression, but it is otherwise not set by this function.
2284 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2285 constant-expression, but a non-constant expression is also
2288 If an error occurs, and it is the kind of error that might cause
2289 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2290 is the caller's responsibility to issue the message. *ERROR_MSG
2291 will be a string with static storage duration, so the caller need
2294 Return an expression for the entity, after issuing appropriate
2295 diagnostics. This function is also responsible for transforming a
2296 reference to a non-static member into a COMPONENT_REF that makes
2297 the use of "this" explicit.
2299 Upon return, *IDK will be filled in appropriately. */
2302 finish_id_expression (tree id_expression,
2306 tree *qualifying_class,
2307 bool constant_expression_p,
2308 bool allow_non_constant_expression_p,
2309 bool *non_constant_expression_p,
2310 const char **error_msg)
2312 /* Initialize the output parameters. */
2313 *idk = CP_ID_KIND_NONE;
2316 if (id_expression == error_mark_node)
2317 return error_mark_node;
2318 /* If we have a template-id, then no further lookup is
2319 required. If the template-id was for a template-class, we
2320 will sometimes have a TYPE_DECL at this point. */
2321 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2322 || TREE_CODE (decl) == TYPE_DECL)
2324 /* Look up the name. */
2327 if (decl == error_mark_node)
2329 /* Name lookup failed. */
2330 if (scope && (!TYPE_P (scope) || !dependent_type_p (scope)))
2332 /* Qualified name lookup failed, and the qualifying name
2333 was not a dependent type. That is always an
2335 qualified_name_lookup_error (scope, id_expression);
2336 return error_mark_node;
2340 /* It may be resolved via Koenig lookup. */
2341 *idk = CP_ID_KIND_UNQUALIFIED;
2342 return id_expression;
2345 /* If DECL is a variable that would be out of scope under
2346 ANSI/ISO rules, but in scope in the ARM, name lookup
2347 will succeed. Issue a diagnostic here. */
2349 decl = check_for_out_of_scope_variable (decl);
2351 /* Remember that the name was used in the definition of
2352 the current class so that we can check later to see if
2353 the meaning would have been different after the class
2354 was entirely defined. */
2355 if (!scope && decl != error_mark_node)
2356 maybe_note_name_used_in_class (id_expression, decl);
2359 /* If we didn't find anything, or what we found was a type,
2360 then this wasn't really an id-expression. */
2361 if (TREE_CODE (decl) == TEMPLATE_DECL
2362 && !DECL_FUNCTION_TEMPLATE_P (decl))
2364 *error_msg = "missing template arguments";
2365 return error_mark_node;
2367 else if (TREE_CODE (decl) == TYPE_DECL
2368 || TREE_CODE (decl) == NAMESPACE_DECL)
2370 *error_msg = "expected primary-expression";
2371 return error_mark_node;
2374 /* If the name resolved to a template parameter, there is no
2375 need to look it up again later. Similarly, we resolve
2376 enumeration constants to their underlying values. */
2377 if (TREE_CODE (decl) == CONST_DECL)
2379 *idk = CP_ID_KIND_NONE;
2380 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2381 return DECL_INITIAL (decl);
2388 /* If the declaration was explicitly qualified indicate
2389 that. The semantics of `A::f(3)' are different than
2390 `f(3)' if `f' is virtual. */
2392 ? CP_ID_KIND_QUALIFIED
2393 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2394 ? CP_ID_KIND_TEMPLATE_ID
2395 : CP_ID_KIND_UNQUALIFIED));
2400 An id-expression is type-dependent if it contains an
2401 identifier that was declared with a dependent type.
2403 The standard is not very specific about an id-expression that
2404 names a set of overloaded functions. What if some of them
2405 have dependent types and some of them do not? Presumably,
2406 such a name should be treated as a dependent name. */
2407 /* Assume the name is not dependent. */
2408 dependent_p = false;
2409 if (!processing_template_decl)
2410 /* No names are dependent outside a template. */
2412 /* A template-id where the name of the template was not resolved
2413 is definitely dependent. */
2414 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2415 && (TREE_CODE (TREE_OPERAND (decl, 0))
2416 == IDENTIFIER_NODE))
2418 /* For anything except an overloaded function, just check its
2420 else if (!is_overloaded_fn (decl))
2422 = dependent_type_p (TREE_TYPE (decl));
2423 /* For a set of overloaded functions, check each of the
2429 if (BASELINK_P (fns))
2430 fns = BASELINK_FUNCTIONS (fns);
2432 /* For a template-id, check to see if the template
2433 arguments are dependent. */
2434 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2436 tree args = TREE_OPERAND (fns, 1);
2437 dependent_p = any_dependent_template_arguments_p (args);
2438 /* The functions are those referred to by the
2440 fns = TREE_OPERAND (fns, 0);
2443 /* If there are no dependent template arguments, go through
2444 the overlaoded functions. */
2445 while (fns && !dependent_p)
2447 tree fn = OVL_CURRENT (fns);
2449 /* Member functions of dependent classes are
2451 if (TREE_CODE (fn) == FUNCTION_DECL
2452 && type_dependent_expression_p (fn))
2454 else if (TREE_CODE (fn) == TEMPLATE_DECL
2455 && dependent_template_p (fn))
2458 fns = OVL_NEXT (fns);
2462 /* If the name was dependent on a template parameter, we will
2463 resolve the name at instantiation time. */
2466 /* Create a SCOPE_REF for qualified names, if the scope is
2471 *qualifying_class = scope;
2472 /* Since this name was dependent, the expression isn't
2473 constant -- yet. No error is issued because it might
2474 be constant when things are instantiated. */
2475 if (constant_expression_p)
2476 *non_constant_expression_p = true;
2477 if (TYPE_P (scope) && dependent_type_p (scope))
2478 return build_nt (SCOPE_REF, scope, id_expression);
2479 else if (TYPE_P (scope) && DECL_P (decl))
2480 return build (SCOPE_REF, TREE_TYPE (decl), scope,
2485 /* A TEMPLATE_ID already contains all the information we
2487 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2488 return id_expression;
2489 /* Since this name was dependent, the expression isn't
2490 constant -- yet. No error is issued because it might be
2491 constant when things are instantiated. */
2492 if (constant_expression_p)
2493 *non_constant_expression_p = true;
2494 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2495 return id_expression;
2498 /* Only certain kinds of names are allowed in constant
2499 expression. Enumerators have already been handled above. */
2500 if (constant_expression_p)
2502 /* Non-type template parameters of integral or enumeration
2504 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX
2505 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2507 /* Const variables or static data members of integral or
2508 enumeration types initialized with constant expressions
2510 else if (TREE_CODE (decl) == VAR_DECL
2511 && CP_TYPE_CONST_P (TREE_TYPE (decl))
2512 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2513 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2517 if (!allow_non_constant_expression_p)
2519 error ("`%D' cannot appear in a constant-expression", decl);
2520 return error_mark_node;
2522 *non_constant_expression_p = true;
2526 if (TREE_CODE (decl) == NAMESPACE_DECL)
2528 error ("use of namespace `%D' as expression", decl);
2529 return error_mark_node;
2531 else if (DECL_CLASS_TEMPLATE_P (decl))
2533 error ("use of class template `%T' as expression", decl);
2534 return error_mark_node;
2536 else if (TREE_CODE (decl) == TREE_LIST)
2538 /* Ambiguous reference to base members. */
2539 error ("request for member `%D' is ambiguous in "
2540 "multiple inheritance lattice", id_expression);
2541 print_candidates (decl);
2542 return error_mark_node;
2545 /* Mark variable-like entities as used. Functions are similarly
2546 marked either below or after overload resolution. */
2547 if (TREE_CODE (decl) == VAR_DECL
2548 || TREE_CODE (decl) == PARM_DECL
2549 || TREE_CODE (decl) == RESULT_DECL)
2554 decl = (adjust_result_of_qualified_name_lookup
2555 (decl, scope, current_class_type));
2557 if (TREE_CODE (decl) == FUNCTION_DECL)
2560 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2561 *qualifying_class = scope;
2562 else if (!processing_template_decl)
2563 decl = convert_from_reference (decl);
2564 else if (TYPE_P (scope))
2565 decl = build (SCOPE_REF, TREE_TYPE (decl), scope, decl);
2567 else if (TREE_CODE (decl) == FIELD_DECL)
2568 decl = finish_non_static_data_member (decl, current_class_ref,
2569 /*qualifying_scope=*/NULL_TREE);
2570 else if (is_overloaded_fn (decl))
2572 tree first_fn = OVL_CURRENT (decl);
2574 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2575 first_fn = DECL_TEMPLATE_RESULT (first_fn);
2577 if (!really_overloaded_fn (decl))
2578 mark_used (first_fn);
2580 if (TREE_CODE (first_fn) == FUNCTION_DECL
2581 && DECL_FUNCTION_MEMBER_P (first_fn))
2583 /* A set of member functions. */
2584 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2585 return finish_class_member_access_expr (decl, id_expression);
2590 if (TREE_CODE (decl) == VAR_DECL
2591 || TREE_CODE (decl) == PARM_DECL
2592 || TREE_CODE (decl) == RESULT_DECL)
2594 tree context = decl_function_context (decl);
2596 if (context != NULL_TREE && context != current_function_decl
2597 && ! TREE_STATIC (decl))
2599 error ("use of %s from containing function",
2600 (TREE_CODE (decl) == VAR_DECL
2601 ? "`auto' variable" : "parameter"));
2602 cp_error_at (" `%#D' declared here", decl);
2603 return error_mark_node;
2607 if (DECL_P (decl) && DECL_NONLOCAL (decl)
2608 && DECL_CLASS_SCOPE_P (decl)
2609 && DECL_CONTEXT (decl) != current_class_type)
2613 path = currently_open_derived_class (DECL_CONTEXT (decl));
2614 perform_or_defer_access_check (TYPE_BINFO (path), decl);
2617 if (! processing_template_decl)
2618 decl = convert_from_reference (decl);
2621 /* Resolve references to variables of anonymous unions
2622 into COMPONENT_REFs. */
2623 if (TREE_CODE (decl) == ALIAS_DECL)
2624 decl = DECL_INITIAL (decl);
2627 if (TREE_DEPRECATED (decl))
2628 warn_deprecated_use (decl);
2633 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2634 use as a type-specifier. */
2637 finish_typeof (tree expr)
2641 if (type_dependent_expression_p (expr))
2643 type = make_aggr_type (TYPEOF_TYPE);
2644 TYPE_FIELDS (type) = expr;
2649 type = TREE_TYPE (expr);
2651 if (!type || type == unknown_type_node)
2653 error ("type of `%E' is unknown", expr);
2654 return error_mark_node;
2660 /* Compute the value of the `sizeof' operator. */
2663 finish_sizeof (tree t)
2665 return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2668 /* Implement the __alignof keyword: Return the minimum required
2669 alignment of T, measured in bytes. */
2672 finish_alignof (tree t)
2674 if (processing_template_decl)
2675 return build_min (ALIGNOF_EXPR, size_type_node, t);
2677 return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2680 /* Generate RTL for the statement T, and its substatements, and any
2681 other statements at its nesting level. */
2684 cp_expand_stmt (tree t)
2686 switch (TREE_CODE (t))
2689 genrtl_try_block (t);
2693 genrtl_eh_spec_block (t);
2709 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2710 will equivalent CALL_EXPRs. */
2713 simplify_aggr_init_exprs_r (tree* tp,
2714 int* walk_subtrees ATTRIBUTE_UNUSED ,
2715 void* data ATTRIBUTE_UNUSED )
2717 tree aggr_init_expr;
2723 enum style_t { ctor, arg, pcc } style;
2725 aggr_init_expr = *tp;
2726 /* We don't need to walk into types; there's nothing in a type that
2727 needs simplification. (And, furthermore, there are places we
2728 actively don't want to go. For example, we don't want to wander
2729 into the default arguments for a FUNCTION_DECL that appears in a
2731 if (TYPE_P (aggr_init_expr))
2736 /* Only AGGR_INIT_EXPRs are interesting. */
2737 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2740 /* Form an appropriate CALL_EXPR. */
2741 fn = TREE_OPERAND (aggr_init_expr, 0);
2742 args = TREE_OPERAND (aggr_init_expr, 1);
2743 slot = TREE_OPERAND (aggr_init_expr, 2);
2744 type = TREE_TYPE (aggr_init_expr);
2746 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2748 #ifdef PCC_STATIC_STRUCT_RETURN
2752 else if (TREE_ADDRESSABLE (type))
2755 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2756 handling. See build_cplus_new. */
2759 if (style == ctor || style == arg)
2761 /* Pass the address of the slot. If this is a constructor, we
2762 replace the first argument; otherwise, we tack on a new one. */
2764 args = TREE_CHAIN (args);
2766 cxx_mark_addressable (slot);
2767 args = tree_cons (NULL_TREE,
2769 build_pointer_type (TREE_TYPE (slot)),
2774 call_expr = build (CALL_EXPR,
2775 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2776 fn, args, NULL_TREE);
2777 TREE_SIDE_EFFECTS (call_expr) = 1;
2780 /* Tell the backend that we've added our return slot to the argument
2782 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2783 else if (style == pcc)
2785 /* If we're using the non-reentrant PCC calling convention, then we
2786 need to copy the returned value out of the static buffer into the
2788 push_deferring_access_checks (dk_no_check);
2789 call_expr = build_aggr_init (slot, call_expr,
2790 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2791 pop_deferring_access_checks ();
2794 /* We want to use the value of the initialized location as the
2796 call_expr = build (COMPOUND_EXPR, type,
2799 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2800 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2803 /* Keep iterating. */
2807 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2810 emit_associated_thunks (tree fn)
2812 /* When we use vcall offsets, we emit thunks with the virtual
2813 functions to which they thunk. The whole point of vcall offsets
2814 is so that you can know statically the entire set of thunks that
2815 will ever be needed for a given virtual function, thereby
2816 enabling you to output all the thunks with the function itself. */
2817 if (DECL_VIRTUAL_P (fn))
2821 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2823 use_thunk (thunk, /*emit_p=*/1);
2824 if (DECL_RESULT_THUNK_P (thunk))
2828 for (probe = DECL_THUNKS (thunk);
2829 probe; probe = TREE_CHAIN (probe))
2830 use_thunk (probe, /*emit_p=*/1);
2836 /* Generate RTL for FN. */
2839 expand_body (tree fn)
2841 location_t saved_loc;
2842 tree saved_function;
2844 if (flag_unit_at_a_time && !cgraph_global_info_ready)
2847 /* Compute the appropriate object-file linkage for inline
2849 if (DECL_DECLARED_INLINE_P (fn))
2850 import_export_decl (fn);
2852 /* If FN is external, then there's no point in generating RTL for
2853 it. This situation can arise with an inline function under
2854 `-fexternal-templates'; we instantiate the function, even though
2855 we're not planning on emitting it, in case we get a chance to
2857 if (DECL_EXTERNAL (fn))
2860 /* Save the current file name and line number. When we expand the
2861 body of the function, we'll set INPUT_LOCATION so that
2862 error-messages come out in the right places. */
2863 saved_loc = input_location;
2864 saved_function = current_function_decl;
2865 input_location = DECL_SOURCE_LOCATION (fn);
2866 current_function_decl = fn;
2868 timevar_push (TV_INTEGRATION);
2870 /* Optimize the body of the function before expanding it. */
2871 optimize_function (fn);
2873 timevar_pop (TV_INTEGRATION);
2874 timevar_push (TV_EXPAND);
2876 genrtl_start_function (fn);
2877 current_function_is_thunk = DECL_THUNK_P (fn);
2879 /* Expand the body. */
2880 expand_stmt (DECL_SAVED_TREE (fn));
2882 /* Statements should always be full-expressions at the outermost set
2883 of curly braces for a function. */
2884 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2886 /* The outermost statement for a function contains the line number
2887 recorded when we finished processing the function. */
2888 input_line = STMT_LINENO (DECL_SAVED_TREE (fn));
2890 /* Generate code for the function. */
2891 genrtl_finish_function (fn);
2893 /* If possible, obliterate the body of the function so that it can
2894 be garbage collected. */
2895 if (dump_enabled_p (TDI_all))
2896 /* Keep the body; we're going to dump it. */
2898 else if (DECL_INLINE (fn) && flag_inline_trees)
2899 /* We might need the body of this function so that we can expand
2900 it inline somewhere else. */
2903 /* We don't need the body; blow it away. */
2904 DECL_SAVED_TREE (fn) = NULL_TREE;
2906 /* And restore the current source position. */
2907 current_function_decl = saved_function;
2908 input_location = saved_loc;
2909 extract_interface_info ();
2911 timevar_pop (TV_EXPAND);
2913 /* Emit any thunks that should be emitted at the same time as FN. */
2914 emit_associated_thunks (fn);
2917 /* Generate RTL for FN. */
2920 expand_or_defer_fn (tree fn)
2922 /* When the parser calls us after finishing the body of a template
2923 function, we don't really want to expand the body. When we're
2924 processing an in-class definition of an inline function,
2925 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2926 to look at the function itself. */
2927 if (processing_template_decl
2928 || (DECL_LANG_SPECIFIC (fn)
2929 && DECL_TEMPLATE_INFO (fn)
2930 && uses_template_parms (DECL_TI_ARGS (fn))))
2932 /* Normally, collection only occurs in rest_of_compilation. So,
2933 if we don't collect here, we never collect junk generated
2934 during the processing of templates until we hit a
2935 non-template function. */
2940 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2941 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2942 simplify_aggr_init_exprs_r,
2945 /* If this is a constructor or destructor body, we have to clone
2947 if (maybe_clone_body (fn))
2949 /* We don't want to process FN again, so pretend we've written
2950 it out, even though we haven't. */
2951 TREE_ASM_WRITTEN (fn) = 1;
2955 /* There's no reason to do any of the work here if we're only doing
2956 semantic analysis; this code just generates RTL. */
2957 if (flag_syntax_only)
2960 if (flag_unit_at_a_time && cgraph_global_info_ready)
2963 if (flag_unit_at_a_time && !cgraph_global_info_ready)
2967 /* Compute the appropriate object-file linkage for inline
2969 if (DECL_DECLARED_INLINE_P (fn))
2970 import_export_decl (fn);
2971 cgraph_finalize_function (fn, DECL_SAVED_TREE (fn));
2975 if (!DECL_EXTERNAL (fn))
2977 DECL_NOT_REALLY_EXTERN (fn) = 1;
2978 DECL_EXTERNAL (fn) = 1;
2980 /* Remember this function. In finish_file we'll decide if
2981 we actually need to write this function out. */
2983 /* Let the back-end know that this function exists. */
2984 (*debug_hooks->deferred_inline_function) (fn);
2990 /* If possible, avoid generating RTL for this function. Instead,
2991 just record it as an inline function, and wait until end-of-file
2992 to decide whether to write it out or not. */
2993 if (/* We have to generate RTL if it's not an inline function. */
2994 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2995 /* Or if we have to emit code for inline functions anyhow. */
2996 && !flag_keep_inline_functions
2997 /* Or if we actually have a reference to the function. */
2998 && !DECL_NEEDED_P (fn))
3000 /* Set DECL_EXTERNAL so that assemble_external will be called as
3001 necessary. We'll clear it again in finish_file. */
3002 if (!DECL_EXTERNAL (fn))
3004 DECL_NOT_REALLY_EXTERN (fn) = 1;
3005 DECL_EXTERNAL (fn) = 1;
3007 /* Remember this function. In finish_file we'll decide if
3008 we actually need to write this function out. */
3010 /* Let the back-end know that this function exists. */
3011 (*debug_hooks->deferred_inline_function) (fn);
3018 /* Helper function for walk_tree, used by finish_function to override all
3019 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
3020 value optimization. */
3023 nullify_returns_r (tree* tp, int* walk_subtrees, void* data)
3025 tree nrv = (tree) data;
3027 /* No need to walk into types. There wouldn't be any need to walk into
3028 non-statements, except that we have to consider STMT_EXPRs. */
3031 else if (TREE_CODE (*tp) == RETURN_STMT)
3032 RETURN_STMT_EXPR (*tp) = NULL_TREE;
3033 else if (TREE_CODE (*tp) == CLEANUP_STMT
3034 && CLEANUP_DECL (*tp) == nrv)
3035 CLEANUP_EH_ONLY (*tp) = 1;
3037 /* Keep iterating. */
3041 /* Start generating the RTL for FN. */
3044 genrtl_start_function (tree fn)
3046 /* Tell everybody what function we're processing. */
3047 current_function_decl = fn;
3048 /* Get the RTL machinery going for this function. */
3049 init_function_start (fn);
3050 /* Let everybody know that we're expanding this function, not doing
3051 semantic analysis. */
3054 /* Even though we're inside a function body, we still don't want to
3055 call expand_expr to calculate the size of a variable-sized array.
3056 We haven't necessarily assigned RTL to all variables yet, so it's
3057 not safe to try to expand expressions involving them. */
3058 immediate_size_expand = 0;
3059 cfun->x_dont_save_pending_sizes_p = 1;
3061 /* Let the user know we're compiling this function. */
3062 announce_function (fn);
3064 /* Initialize the per-function data. */
3065 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
3066 if (DECL_SAVED_FUNCTION_DATA (fn))
3068 /* If we already parsed this function, and we're just expanding it
3069 now, restore saved state. */
3070 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
3072 /* This function is being processed in whole-function mode; we
3073 already did semantic analysis. */
3074 cfun->x_whole_function_mode_p = 1;
3076 /* If we decided that we didn't want to inline this function,
3077 make sure the back-end knows that. */
3078 if (!current_function_cannot_inline)
3079 current_function_cannot_inline = cp_function_chain->cannot_inline;
3081 /* We don't need the saved data anymore. Unless this is an inline
3082 function; we need the named return value info for
3083 cp_copy_res_decl_for_inlining. */
3084 if (! DECL_INLINE (fn))
3085 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
3088 /* Keep track of how many functions we're presently expanding. */
3091 /* Create a binding level for the parameters. */
3092 expand_function_start (fn, /*parms_have_cleanups=*/0);
3093 /* If this function is `main'. */
3094 if (DECL_MAIN_P (fn))
3095 expand_main_function ();
3097 /* Give our named return value the same RTL as our RESULT_DECL. */
3098 if (current_function_return_value)
3099 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
3102 /* Finish generating the RTL for FN. */
3105 genrtl_finish_function (tree fn)
3110 if (write_symbols != NO_DEBUG)
3112 /* Keep this code around in case we later want to control debug info
3113 based on whether a type is "used". (jason 1999-11-11) */
3115 tree ttype = target_type (fntype);
3118 if (IS_AGGR_TYPE (ttype))
3119 /* Let debugger know it should output info for this type. */
3120 note_debug_info_needed (ttype);
3122 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
3124 ttype = target_type (TREE_TYPE (parmdecl));
3125 if (IS_AGGR_TYPE (ttype))
3126 /* Let debugger know it should output info for this type. */
3127 note_debug_info_needed (ttype);
3132 /* Clean house because we will need to reorder insns here. */
3133 do_pending_stack_adjust ();
3135 /* If we have a named return value, we need to force a return so that
3136 the return register is USEd. */
3137 if (DECL_NAME (DECL_RESULT (fn)))
3138 emit_jump (return_label);
3140 /* We hard-wired immediate_size_expand to zero in start_function.
3141 Expand_function_end will decrement this variable. So, we set the
3142 variable to one here, so that after the decrement it will remain
3144 immediate_size_expand = 1;
3146 /* Generate rtl for function exit. */
3147 expand_function_end ();
3149 /* If this is a nested function (like a template instantiation that
3150 we're compiling in the midst of compiling something else), push a
3151 new GC context. That will keep local variables on the stack from
3152 being collected while we're doing the compilation of this
3154 if (function_depth > 1)
3155 ggc_push_context ();
3157 /* There's no need to defer outputting this function any more; we
3158 know we want to output it. */
3159 DECL_DEFER_OUTPUT (fn) = 0;
3161 /* Run the optimizers and output the assembler code for this
3163 rest_of_compilation (fn);
3165 /* Undo the call to ggc_push_context above. */
3166 if (function_depth > 1)
3170 /* Keep this code around in case we later want to control debug info
3171 based on whether a type is "used". (jason 1999-11-11) */
3173 if (ctype && TREE_ASM_WRITTEN (fn))
3174 note_debug_info_needed (ctype);
3177 /* If this function is marked with the constructor attribute, add it
3178 to the list of functions to be called along with constructors
3179 from static duration objects. */
3180 if (DECL_STATIC_CONSTRUCTOR (fn))
3181 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
3183 /* If this function is marked with the destructor attribute, add it
3184 to the list of functions to be called along with destructors from
3185 static duration objects. */
3186 if (DECL_STATIC_DESTRUCTOR (fn))
3187 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
3191 /* In C++, we should never be saving RTL for the function. */
3192 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
3194 /* Since we don't need the RTL for this function anymore, stop
3195 pointing to it. That's especially important for LABEL_DECLs,
3196 since you can reach all the instructions in the function from the
3197 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
3198 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
3200 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
3204 /* Clear out the RTL for the arguments. */
3205 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
3207 SET_DECL_RTL (t, NULL_RTX);
3208 DECL_INCOMING_RTL (t) = NULL_RTX;
3211 if (!(flag_inline_trees && DECL_INLINE (fn)))
3212 /* DECL_INITIAL must remain nonzero so we know this was an
3213 actual function definition. */
3214 DECL_INITIAL (fn) = error_mark_node;
3216 /* Let the error reporting routines know that we're outside a
3217 function. For a nested function, this value is used in
3218 pop_cp_function_context and then reset via pop_function_context. */
3219 current_function_decl = NULL_TREE;
3222 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
3226 clear_decl_rtl (tree* tp,
3227 int* walk_subtrees ATTRIBUTE_UNUSED ,
3228 void* data ATTRIBUTE_UNUSED )
3230 if (nonstatic_local_decl_p (*tp))
3231 SET_DECL_RTL (*tp, NULL_RTX);
3236 /* Perform initialization related to this module. */
3239 init_cp_semantics (void)
3241 lang_expand_stmt = cp_expand_stmt;
3244 #include "gt-cp-semantics.h"