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, 2004 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"
35 #include "tree-mudflap.h"
45 #include "diagnostic.h"
47 #include "tree-iterator.h"
49 /* There routines provide a modular interface to perform many parsing
50 operations. They may therefore be used during actual parsing, or
51 during template instantiation, which may be regarded as a
52 degenerate form of parsing. Since the current g++ parser is
53 lacking in several respects, and will be reimplemented, we are
54 attempting to move most code that is not directly related to
55 parsing into this file; that will make implementing the new parser
56 much easier since it will be able to make use of these routines. */
58 static tree maybe_convert_cond (tree);
59 static tree simplify_aggr_init_exprs_r (tree *, int *, void *);
60 static void emit_associated_thunks (tree);
61 static tree finalize_nrv_r (tree *, int *, void *);
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a TREE_LIST of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the TREE_LIST. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 /* Data for deferred access checking. */
116 static GTY(()) deferred_access *deferred_access_stack;
117 static GTY(()) deferred_access *deferred_access_free_list;
119 /* Save the current deferred access states and start deferred
120 access checking iff DEFER_P is true. */
123 push_deferring_access_checks (deferring_kind deferring)
127 /* For context like template instantiation, access checking
128 disabling applies to all nested context. */
129 if (deferred_access_stack
130 && deferred_access_stack->deferring_access_checks_kind == dk_no_check)
131 deferring = dk_no_check;
133 /* Recycle previously used free store if available. */
134 if (deferred_access_free_list)
136 d = deferred_access_free_list;
137 deferred_access_free_list = d->next;
140 d = ggc_alloc (sizeof (deferred_access));
142 d->next = deferred_access_stack;
143 d->deferred_access_checks = NULL_TREE;
144 d->deferring_access_checks_kind = deferring;
145 deferred_access_stack = d;
148 /* Resume deferring access checks again after we stopped doing
152 resume_deferring_access_checks (void)
154 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
155 deferred_access_stack->deferring_access_checks_kind = dk_deferred;
158 /* Stop deferring access checks. */
161 stop_deferring_access_checks (void)
163 if (deferred_access_stack->deferring_access_checks_kind == dk_deferred)
164 deferred_access_stack->deferring_access_checks_kind = dk_no_deferred;
167 /* Discard the current deferred access checks and restore the
171 pop_deferring_access_checks (void)
173 deferred_access *d = deferred_access_stack;
174 deferred_access_stack = d->next;
176 /* Remove references to access checks TREE_LIST. */
177 d->deferred_access_checks = NULL_TREE;
179 /* Store in free list for later use. */
180 d->next = deferred_access_free_list;
181 deferred_access_free_list = d;
184 /* Returns a TREE_LIST representing the deferred checks.
185 The TREE_PURPOSE of each node is the type through which the
186 access occurred; the TREE_VALUE is the declaration named.
190 get_deferred_access_checks (void)
192 return deferred_access_stack->deferred_access_checks;
195 /* Take current deferred checks and combine with the
196 previous states if we also defer checks previously.
197 Otherwise perform checks now. */
200 pop_to_parent_deferring_access_checks (void)
202 tree deferred_check = get_deferred_access_checks ();
203 deferred_access *d1 = deferred_access_stack;
204 deferred_access *d2 = deferred_access_stack->next;
205 deferred_access *d3 = deferred_access_stack->next->next;
207 /* Temporary swap the order of the top two states, just to make
208 sure the garbage collector will not reclaim the memory during
210 deferred_access_stack = d2;
214 for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
215 /* Perform deferred check if required. */
216 perform_or_defer_access_check (TREE_PURPOSE (deferred_check),
217 TREE_VALUE (deferred_check));
219 deferred_access_stack = d1;
222 pop_deferring_access_checks ();
225 /* Perform the deferred access checks.
227 After performing the checks, we still have to keep the list
228 `deferred_access_stack->deferred_access_checks' since we may want
229 to check access for them again later in a different context.
236 A::X A::a, x; // No error for `A::a', error for `x'
238 We have to perform deferred access of `A::X', first with `A::a',
242 perform_deferred_access_checks (void)
245 for (deferred_check = deferred_access_stack->deferred_access_checks;
247 deferred_check = TREE_CHAIN (deferred_check))
249 enforce_access (TREE_PURPOSE (deferred_check),
250 TREE_VALUE (deferred_check));
253 /* Defer checking the accessibility of DECL, when looked up in
257 perform_or_defer_access_check (tree binfo, tree decl)
261 my_friendly_assert (TREE_CODE (binfo) == TREE_VEC, 20030623);
263 /* If we are not supposed to defer access checks, just check now. */
264 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
266 enforce_access (binfo, decl);
269 /* Exit if we are in a context that no access checking is performed. */
270 else if (deferred_access_stack->deferring_access_checks_kind == dk_no_check)
273 /* See if we are already going to perform this check. */
274 for (check = deferred_access_stack->deferred_access_checks;
276 check = TREE_CHAIN (check))
277 if (TREE_VALUE (check) == decl && TREE_PURPOSE (check) == binfo)
279 /* If not, record the check. */
280 deferred_access_stack->deferred_access_checks
281 = tree_cons (binfo, decl,
282 deferred_access_stack->deferred_access_checks);
285 /* Returns nonzero if the current statement is a full expression,
286 i.e. temporaries created during that statement should be destroyed
287 at the end of the statement. */
290 stmts_are_full_exprs_p (void)
292 return current_stmt_tree ()->stmts_are_full_exprs_p;
295 /* Returns the stmt_tree (if any) to which statements are currently
296 being added. If there is no active statement-tree, NULL is
300 current_stmt_tree (void)
303 ? &cfun->language->base.x_stmt_tree
304 : &scope_chain->x_stmt_tree);
307 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
308 flag for this because "A union for which objects or pointers are
309 declared is not an anonymous union" [class.union]. */
312 anon_aggr_type_p (tree node)
314 return ANON_AGGR_TYPE_P (node);
317 /* Finish a scope. */
320 do_poplevel (tree stmt_list)
324 if (stmts_are_full_exprs_p ())
325 block = poplevel (kept_level_p (), 1, 0);
327 stmt_list = pop_stmt_list (stmt_list);
329 if (!processing_template_decl)
331 stmt_list = c_build_bind_expr (block, stmt_list);
332 /* ??? See c_end_compound_stmt re statement expressions. */
338 /* Begin a new scope. */
341 do_pushlevel (scope_kind sk)
343 tree ret = push_stmt_list ();
344 if (stmts_are_full_exprs_p ())
345 begin_scope (sk, NULL);
349 /* Finish processing a conditional. COND contains the raw expression;
350 STMT_P is a stacked statement list that will contain any other stmts
351 emitting during the processing of this conditional. Place the
352 resulting conditional back in STMT_P. */
355 finish_cond (tree cond, tree *stmt_p)
358 stmt = pop_stmt_list (stmt);
359 if (TREE_SIDE_EFFECTS (stmt))
361 /* If stmt is set, it will be a DECL_STMT. When processing a template,
362 using this is enough, because tsubst_expr considers the result of a
363 DECL_STMT to be the DECL. When generating real code, we build a
364 funny little TREE_LIST thingy that's handled by the gimplifier. */
365 /* ??? The object of this thingy is to get the DECL declared in the
366 proper scope. Seems like this oughtn't be terribly hard with the
367 new explicit uses of BIND_EXPR and such. */
368 if (processing_template_decl)
370 stmt = expr_only (stmt);
375 stmt = build_tree_list (stmt, cond);
382 /* If *COND_P specifies a conditional with a declaration, transform the
385 for (; A x = 42;) { }
387 while (true) { A x = 42; if (!x) break; }
388 for (;;) { A x = 42; if (!x) break; }
389 The statement list for the loop body should have been pushed. */
392 simplify_loop_decl_cond (tree *cond_p)
395 if (TREE_CODE (cond) == TREE_LIST)
399 *cond_p = boolean_true_node;
401 if_stmt = begin_if_stmt ();
402 add_stmt (TREE_PURPOSE (cond));
403 cond = build_unary_op (TRUTH_NOT_EXPR, TREE_VALUE (cond), 0);
404 finish_if_stmt_cond (cond, if_stmt);
405 finish_break_stmt ();
406 finish_then_clause (if_stmt);
407 finish_if_stmt (if_stmt);
412 /* Finish a goto-statement. */
415 finish_goto_stmt (tree destination)
417 if (TREE_CODE (destination) == IDENTIFIER_NODE)
418 destination = lookup_label (destination);
420 /* We warn about unused labels with -Wunused. That means we have to
421 mark the used labels as used. */
422 if (TREE_CODE (destination) == LABEL_DECL)
423 TREE_USED (destination) = 1;
426 /* The DESTINATION is being used as an rvalue. */
427 if (!processing_template_decl)
428 destination = decay_conversion (destination);
429 /* We don't inline calls to functions with computed gotos.
430 Those functions are typically up to some funny business,
431 and may be depending on the labels being at particular
432 addresses, or some such. */
433 DECL_UNINLINABLE (current_function_decl) = 1;
436 check_goto (destination);
438 return add_stmt (build_stmt (GOTO_EXPR, destination));
441 /* COND is the condition-expression for an if, while, etc.,
442 statement. Convert it to a boolean value, if appropriate. */
445 maybe_convert_cond (tree cond)
447 /* Empty conditions remain empty. */
451 /* Wait until we instantiate templates before doing conversion. */
452 if (processing_template_decl)
455 /* Do the conversion. */
456 cond = convert_from_reference (cond);
457 return condition_conversion (cond);
460 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
463 finish_expr_stmt (tree expr)
467 if (expr != NULL_TREE)
469 if (!processing_template_decl)
470 expr = convert_to_void (expr, "statement");
471 else if (!type_dependent_expression_p (expr))
472 convert_to_void (build_non_dependent_expr (expr), "statement");
474 /* Simplification of inner statement expressions, compound exprs,
475 etc can result in the us already having an EXPR_STMT. */
476 if (TREE_CODE (expr) != EXPR_STMT)
477 expr = build_stmt (EXPR_STMT, expr);
487 /* Begin an if-statement. Returns a newly created IF_STMT if
494 scope = do_pushlevel (sk_block);
495 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
496 TREE_CHAIN (r) = scope;
498 IF_COND (r) = push_stmt_list ();
502 /* Process the COND of an if-statement, which may be given by
506 finish_if_stmt_cond (tree cond, tree if_stmt)
508 cond = maybe_convert_cond (cond);
509 finish_cond (cond, &IF_COND (if_stmt));
510 THEN_CLAUSE (if_stmt) = push_stmt_list ();
513 /* Finish the then-clause of an if-statement, which may be given by
517 finish_then_clause (tree if_stmt)
519 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
523 /* Begin the else-clause of an if-statement. */
526 begin_else_clause (tree if_stmt)
528 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
531 /* Finish the else-clause of an if-statement, which may be given by
535 finish_else_clause (tree if_stmt)
537 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
540 /* Finish an if-statement. */
543 finish_if_stmt (tree if_stmt)
545 tree scope = TREE_CHAIN (if_stmt);
546 TREE_CHAIN (if_stmt) = NULL;
547 add_stmt (do_poplevel (scope));
551 /* Begin a while-statement. Returns a newly created WHILE_STMT if
555 begin_while_stmt (void)
558 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
560 WHILE_BODY (r) = do_pushlevel (sk_block);
561 WHILE_COND (r) = push_stmt_list ();
565 /* Process the COND of a while-statement, which may be given by
569 finish_while_stmt_cond (tree cond, tree while_stmt)
571 cond = maybe_convert_cond (cond);
572 finish_cond (cond, &WHILE_COND (while_stmt));
573 simplify_loop_decl_cond (&WHILE_COND (while_stmt));
576 /* Finish a while-statement, which may be given by WHILE_STMT. */
579 finish_while_stmt (tree while_stmt)
581 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
585 /* Begin a do-statement. Returns a newly created DO_STMT if
591 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
593 DO_BODY (r) = push_stmt_list ();
597 /* Finish the body of a do-statement, which may be given by DO_STMT. */
600 finish_do_body (tree do_stmt)
602 DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
605 /* Finish a do-statement, which may be given by DO_STMT, and whose
606 COND is as indicated. */
609 finish_do_stmt (tree cond, tree do_stmt)
611 cond = maybe_convert_cond (cond);
612 DO_COND (do_stmt) = cond;
616 /* Finish a return-statement. The EXPRESSION returned, if any, is as
620 finish_return_stmt (tree expr)
624 expr = check_return_expr (expr);
625 if (!processing_template_decl)
627 if (DECL_DESTRUCTOR_P (current_function_decl))
629 /* Similarly, all destructors must run destructors for
630 base-classes before returning. So, all returns in a
631 destructor get sent to the DTOR_LABEL; finish_function emits
632 code to return a value there. */
633 return finish_goto_stmt (dtor_label);
636 r = add_stmt (build_stmt (RETURN_STMT, expr));
642 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
645 begin_for_stmt (void)
649 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
650 NULL_TREE, NULL_TREE);
652 if (flag_new_for_scope > 0)
653 TREE_CHAIN (r) = do_pushlevel (sk_for);
655 if (processing_template_decl)
656 FOR_INIT_STMT (r) = push_stmt_list ();
661 /* Finish the for-init-statement of a for-statement, which may be
662 given by FOR_STMT. */
665 finish_for_init_stmt (tree for_stmt)
667 if (processing_template_decl)
668 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
670 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
671 FOR_COND (for_stmt) = push_stmt_list ();
674 /* Finish the COND of a for-statement, which may be given by
678 finish_for_cond (tree cond, tree for_stmt)
680 cond = maybe_convert_cond (cond);
681 finish_cond (cond, &FOR_COND (for_stmt));
682 if (FOR_COND (for_stmt))
683 simplify_loop_decl_cond (&FOR_COND (for_stmt));
686 /* Finish the increment-EXPRESSION in a for-statement, which may be
687 given by FOR_STMT. */
690 finish_for_expr (tree expr, tree for_stmt)
692 /* If EXPR is an overloaded function, issue an error; there is no
693 context available to use to perform overload resolution. */
694 if (expr && type_unknown_p (expr))
696 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
697 expr = error_mark_node;
699 FOR_EXPR (for_stmt) = expr;
702 /* Finish the body of a for-statement, which may be given by
703 FOR_STMT. The increment-EXPR for the loop must be
707 finish_for_stmt (tree for_stmt)
709 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
711 /* Pop the scope for the body of the loop. */
712 if (flag_new_for_scope > 0)
714 tree scope = TREE_CHAIN (for_stmt);
715 TREE_CHAIN (for_stmt) = NULL;
716 add_stmt (do_poplevel (scope));
722 /* Finish a break-statement. */
725 finish_break_stmt (void)
727 return add_stmt (build_break_stmt ());
730 /* Finish a continue-statement. */
733 finish_continue_stmt (void)
735 return add_stmt (build_continue_stmt ());
738 /* Begin a switch-statement. Returns a new SWITCH_STMT if
742 begin_switch_stmt (void)
746 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
748 scope = do_pushlevel (sk_block);
749 TREE_CHAIN (r) = scope;
752 SWITCH_COND (r) = push_stmt_list ();
757 /* Finish the cond of a switch-statement. */
760 finish_switch_cond (tree cond, tree switch_stmt)
762 tree orig_type = NULL;
763 if (!processing_template_decl)
767 /* Convert the condition to an integer or enumeration type. */
768 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
769 if (cond == NULL_TREE)
771 error ("switch quantity not an integer");
772 cond = error_mark_node;
774 orig_type = TREE_TYPE (cond);
775 if (cond != error_mark_node)
779 Integral promotions are performed. */
780 cond = perform_integral_promotions (cond);
781 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
784 if (cond != error_mark_node)
786 index = get_unwidened (cond, NULL_TREE);
787 /* We can't strip a conversion from a signed type to an unsigned,
788 because if we did, int_fits_type_p would do the wrong thing
789 when checking case values for being in range,
790 and it's too hard to do the right thing. */
791 if (TYPE_UNSIGNED (TREE_TYPE (cond))
792 == TYPE_UNSIGNED (TREE_TYPE (index)))
796 finish_cond (cond, &SWITCH_COND (switch_stmt));
797 SWITCH_TYPE (switch_stmt) = orig_type;
798 push_switch (switch_stmt);
799 SWITCH_BODY (switch_stmt) = push_stmt_list ();
802 /* Finish the body of a switch-statement, which may be given by
803 SWITCH_STMT. The COND to switch on is indicated. */
806 finish_switch_stmt (tree switch_stmt)
810 SWITCH_BODY (switch_stmt) = pop_stmt_list (SWITCH_BODY (switch_stmt));
814 scope = TREE_CHAIN (switch_stmt);
815 TREE_CHAIN (switch_stmt) = NULL;
816 add_stmt (do_poplevel (scope));
819 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
823 begin_try_block (void)
825 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
827 TRY_STMTS (r) = push_stmt_list ();
831 /* Likewise, for a function-try-block. */
834 begin_function_try_block (void)
836 tree r = begin_try_block ();
837 FN_TRY_BLOCK_P (r) = 1;
841 /* Finish a try-block, which may be given by TRY_BLOCK. */
844 finish_try_block (tree try_block)
846 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
847 TRY_HANDLERS (try_block) = push_stmt_list ();
850 /* Finish the body of a cleanup try-block, which may be given by
854 finish_cleanup_try_block (tree try_block)
856 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
859 /* Finish an implicitly generated try-block, with a cleanup is given
863 finish_cleanup (tree cleanup, tree try_block)
865 TRY_HANDLERS (try_block) = cleanup;
866 CLEANUP_P (try_block) = 1;
869 /* Likewise, for a function-try-block. */
872 finish_function_try_block (tree try_block)
874 finish_try_block (try_block);
875 /* FIXME : something queer about CTOR_INITIALIZER somehow following
876 the try block, but moving it inside. */
877 in_function_try_handler = 1;
880 /* Finish a handler-sequence for a try-block, which may be given by
884 finish_handler_sequence (tree try_block)
886 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
887 check_handlers (TRY_HANDLERS (try_block));
890 /* Likewise, for a function-try-block. */
893 finish_function_handler_sequence (tree try_block)
895 in_function_try_handler = 0;
896 finish_handler_sequence (try_block);
899 /* Begin a handler. Returns a HANDLER if appropriate. */
906 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
909 /* Create a binding level for the eh_info and the exception object
911 HANDLER_BODY (r) = do_pushlevel (sk_catch);
916 /* Finish the handler-parameters for a handler, which may be given by
917 HANDLER. DECL is the declaration for the catch parameter, or NULL
918 if this is a `catch (...)' clause. */
921 finish_handler_parms (tree decl, tree handler)
923 tree type = NULL_TREE;
924 if (processing_template_decl)
928 decl = pushdecl (decl);
929 decl = push_template_decl (decl);
930 HANDLER_PARMS (handler) = decl;
931 type = TREE_TYPE (decl);
935 type = expand_start_catch_block (decl);
937 HANDLER_TYPE (handler) = type;
938 if (!processing_template_decl && type)
939 mark_used (eh_type_info (type));
942 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
943 the return value from the matching call to finish_handler_parms. */
946 finish_handler (tree handler)
948 if (!processing_template_decl)
949 expand_end_catch_block ();
950 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
953 /* Begin a compound statement. FLAGS contains some bits that control the
954 behaviour and context. If BCS_NO_SCOPE is set, the compound statement
955 does not define a scope. If BCS_FN_BODY is set, this is the outermost
956 block of a function. If BCS_TRY_BLOCK is set, this is the block
957 created on behalf of a TRY statement. Returns a token to be passed to
958 finish_compound_stmt. */
961 begin_compound_stmt (unsigned int flags)
965 if (flags & BCS_NO_SCOPE)
967 r = push_stmt_list ();
968 STATEMENT_LIST_NO_SCOPE (r) = 1;
970 /* Normally, we try hard to keep the BLOCK for a statement-expression.
971 But, if it's a statement-expression with a scopeless block, there's
972 nothing to keep, and we don't want to accidentally keep a block
973 *inside* the scopeless block. */
974 keep_next_level (false);
977 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
979 /* When processing a template, we need to remember where the braces were,
980 so that we can set up identical scopes when instantiating the template
981 later. BIND_EXPR is a handy candidate for this.
982 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
983 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
984 processing templates. */
985 if (processing_template_decl)
987 r = build (BIND_EXPR, NULL, NULL, r, NULL);
988 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
989 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
990 TREE_SIDE_EFFECTS (r) = 1;
996 /* Finish a compound-statement, which is given by STMT. */
999 finish_compound_stmt (tree stmt)
1001 if (TREE_CODE (stmt) == BIND_EXPR)
1002 BIND_EXPR_BODY (stmt) = do_poplevel (BIND_EXPR_BODY (stmt));
1003 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1004 stmt = pop_stmt_list (stmt);
1006 stmt = do_poplevel (stmt);
1008 /* ??? See c_end_compound_stmt wrt statement expressions. */
1013 /* Finish an asm-statement, whose components are a STRING, some
1014 OUTPUT_OPERANDS, some INPUT_OPERANDS, and some CLOBBERS. Also note
1015 whether the asm-statement should be considered volatile. */
1018 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1019 tree input_operands, tree clobbers)
1024 if (!processing_template_decl)
1030 for (t = input_operands; t; t = TREE_CHAIN (t))
1032 tree converted_operand
1033 = decay_conversion (TREE_VALUE (t));
1035 /* If the type of the operand hasn't been determined (e.g.,
1036 because it involves an overloaded function), then issue
1037 an error message. There's no context available to
1038 resolve the overloading. */
1039 if (TREE_TYPE (converted_operand) == unknown_type_node)
1041 error ("type of asm operand `%E' could not be determined",
1043 converted_operand = error_mark_node;
1045 TREE_VALUE (t) = converted_operand;
1048 ninputs = list_length (input_operands);
1049 noutputs = list_length (output_operands);
1051 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1056 const char *constraint;
1059 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1060 operand = TREE_VALUE (t);
1062 if (!parse_output_constraint (&constraint,
1063 i, ninputs, noutputs,
1068 /* By marking this operand as erroneous, we will not try
1069 to process this operand again in expand_asm_operands. */
1070 TREE_VALUE (t) = error_mark_node;
1074 /* If the operand is a DECL that is going to end up in
1075 memory, assume it is addressable. This is a bit more
1076 conservative than it would ideally be; the exact test is
1077 buried deep in expand_asm_operands and depends on the
1078 DECL_RTL for the OPERAND -- which we don't have at this
1080 if (!allows_reg && DECL_P (operand))
1081 cxx_mark_addressable (operand);
1085 r = build_stmt (ASM_EXPR, string,
1086 output_operands, input_operands,
1088 ASM_VOLATILE_P (r) = volatile_p;
1089 return add_stmt (r);
1092 /* Finish a label with the indicated NAME. */
1095 finish_label_stmt (tree name)
1097 tree decl = define_label (input_location, name);
1098 return add_stmt (build_stmt (LABEL_EXPR, decl));
1101 /* Finish a series of declarations for local labels. G++ allows users
1102 to declare "local" labels, i.e., labels with scope. This extension
1103 is useful when writing code involving statement-expressions. */
1106 finish_label_decl (tree name)
1108 tree decl = declare_local_label (name);
1109 add_decl_stmt (decl);
1112 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1115 finish_decl_cleanup (tree decl, tree cleanup)
1117 push_cleanup (decl, cleanup, false);
1120 /* If the current scope exits with an exception, run CLEANUP. */
1123 finish_eh_cleanup (tree cleanup)
1125 push_cleanup (NULL, cleanup, true);
1128 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1129 order they were written by the user. Each node is as for
1130 emit_mem_initializers. */
1133 finish_mem_initializers (tree mem_inits)
1135 /* Reorder the MEM_INITS so that they are in the order they appeared
1136 in the source program. */
1137 mem_inits = nreverse (mem_inits);
1139 if (processing_template_decl)
1140 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1142 emit_mem_initializers (mem_inits);
1145 /* Finish a parenthesized expression EXPR. */
1148 finish_parenthesized_expr (tree expr)
1150 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1151 /* This inhibits warnings in c_common_truthvalue_conversion. */
1152 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1154 if (TREE_CODE (expr) == OFFSET_REF)
1155 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1156 enclosed in parentheses. */
1157 PTRMEM_OK_P (expr) = 0;
1161 /* Finish a reference to a non-static data member (DECL) that is not
1162 preceded by `.' or `->'. */
1165 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1167 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1171 if (current_function_decl
1172 && DECL_STATIC_FUNCTION_P (current_function_decl))
1173 cp_error_at ("invalid use of member `%D' in static member function",
1176 cp_error_at ("invalid use of non-static data member `%D'", decl);
1177 error ("from this location");
1179 return error_mark_node;
1181 TREE_USED (current_class_ptr) = 1;
1182 if (processing_template_decl && !qualifying_scope)
1184 tree type = TREE_TYPE (decl);
1186 if (TREE_CODE (type) == REFERENCE_TYPE)
1187 type = TREE_TYPE (type);
1190 /* Set the cv qualifiers. */
1191 int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1193 if (DECL_MUTABLE_P (decl))
1194 quals &= ~TYPE_QUAL_CONST;
1196 quals |= cp_type_quals (TREE_TYPE (decl));
1197 type = cp_build_qualified_type (type, quals);
1200 return build_min (COMPONENT_REF, type, object, decl);
1204 tree access_type = TREE_TYPE (object);
1205 tree lookup_context = context_for_name_lookup (decl);
1207 while (!DERIVED_FROM_P (lookup_context, access_type))
1209 access_type = TYPE_CONTEXT (access_type);
1210 while (access_type && DECL_P (access_type))
1211 access_type = DECL_CONTEXT (access_type);
1215 cp_error_at ("object missing in reference to `%D'", decl);
1216 error ("from this location");
1217 return error_mark_node;
1221 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1222 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1224 if (processing_template_decl)
1225 return build_min (SCOPE_REF, TREE_TYPE (decl),
1226 qualifying_scope, DECL_NAME (decl));
1228 perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1230 /* If the data member was named `C::M', convert `*this' to `C'
1232 if (qualifying_scope)
1234 tree binfo = NULL_TREE;
1235 object = build_scoped_ref (object, qualifying_scope,
1239 return build_class_member_access_expr (object, decl,
1240 /*access_path=*/NULL_TREE,
1241 /*preserve_reference=*/false);
1245 /* DECL was the declaration to which a qualified-id resolved. Issue
1246 an error message if it is not accessible. If OBJECT_TYPE is
1247 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1248 type of `*x', or `x', respectively. If the DECL was named as
1249 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1252 check_accessibility_of_qualified_id (tree decl,
1254 tree nested_name_specifier)
1257 tree qualifying_type = NULL_TREE;
1259 /* Determine the SCOPE of DECL. */
1260 scope = context_for_name_lookup (decl);
1261 /* If the SCOPE is not a type, then DECL is not a member. */
1262 if (!TYPE_P (scope))
1264 /* Compute the scope through which DECL is being accessed. */
1266 /* OBJECT_TYPE might not be a class type; consider:
1268 class A { typedef int I; };
1272 In this case, we will have "A::I" as the DECL, but "I" as the
1274 && CLASS_TYPE_P (object_type)
1275 && DERIVED_FROM_P (scope, object_type))
1276 /* If we are processing a `->' or `.' expression, use the type of the
1278 qualifying_type = object_type;
1279 else if (nested_name_specifier)
1281 /* If the reference is to a non-static member of the
1282 current class, treat it as if it were referenced through
1284 if (DECL_NONSTATIC_MEMBER_P (decl)
1285 && current_class_ptr
1286 && DERIVED_FROM_P (scope, current_class_type))
1287 qualifying_type = current_class_type;
1288 /* Otherwise, use the type indicated by the
1289 nested-name-specifier. */
1291 qualifying_type = nested_name_specifier;
1294 /* Otherwise, the name must be from the current class or one of
1296 qualifying_type = currently_open_derived_class (scope);
1298 if (qualifying_type)
1299 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1302 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1303 class named to the left of the "::" operator. DONE is true if this
1304 expression is a complete postfix-expression; it is false if this
1305 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1306 iff this expression is the operand of '&'. */
1309 finish_qualified_id_expr (tree qualifying_class, tree expr, bool done,
1312 if (error_operand_p (expr))
1313 return error_mark_node;
1315 /* If EXPR occurs as the operand of '&', use special handling that
1316 permits a pointer-to-member. */
1317 if (address_p && done)
1319 if (TREE_CODE (expr) == SCOPE_REF)
1320 expr = TREE_OPERAND (expr, 1);
1321 expr = build_offset_ref (qualifying_class, expr,
1322 /*address_p=*/true);
1326 if (TREE_CODE (expr) == FIELD_DECL)
1327 expr = finish_non_static_data_member (expr, current_class_ref,
1329 else if (BASELINK_P (expr) && !processing_template_decl)
1334 /* See if any of the functions are non-static members. */
1335 fns = BASELINK_FUNCTIONS (expr);
1336 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1337 fns = TREE_OPERAND (fns, 0);
1338 for (fn = fns; fn; fn = OVL_NEXT (fn))
1339 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1341 /* If so, the expression may be relative to the current
1343 if (fn && current_class_type
1344 && DERIVED_FROM_P (qualifying_class, current_class_type))
1345 expr = (build_class_member_access_expr
1346 (maybe_dummy_object (qualifying_class, NULL),
1348 BASELINK_ACCESS_BINFO (expr),
1349 /*preserve_reference=*/false));
1351 /* The expression is a qualified name whose address is not
1353 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1359 /* Begin a statement-expression. The value returned must be passed to
1360 finish_stmt_expr. */
1363 begin_stmt_expr (void)
1365 return push_stmt_list ();
1368 /* Process the final expression of a statement expression. EXPR can be
1369 NULL, if the final expression is empty. Build up a TARGET_EXPR so
1370 that the result value can be safely returned to the enclosing
1374 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1376 tree result = NULL_TREE;
1380 if (!processing_template_decl && !VOID_TYPE_P (TREE_TYPE (expr)))
1382 tree type = TREE_TYPE (expr);
1384 if (TREE_CODE (type) == ARRAY_TYPE
1385 || TREE_CODE (type) == FUNCTION_TYPE)
1386 expr = decay_conversion (expr);
1388 expr = convert_from_reference (expr);
1389 expr = require_complete_type (expr);
1391 type = TREE_TYPE (expr);
1393 /* Build a TARGET_EXPR for this aggregate. finish_stmt_expr
1394 will then pull it apart so the lifetime of the target is
1395 within the scope of the expression containing this statement
1397 if (TREE_CODE (expr) == TARGET_EXPR)
1399 else if (!IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_INIT_REF (type))
1400 expr = build_target_expr_with_type (expr, type);
1403 /* Copy construct. */
1404 expr = build_special_member_call
1405 (NULL_TREE, complete_ctor_identifier,
1406 build_tree_list (NULL_TREE, expr),
1407 TYPE_BINFO (type), LOOKUP_NORMAL);
1408 expr = build_cplus_new (type, expr);
1409 my_friendly_assert (TREE_CODE (expr) == TARGET_EXPR, 20030729);
1413 if (expr != error_mark_node)
1415 result = build_stmt (EXPR_STMT, expr);
1416 EXPR_STMT_STMT_EXPR_RESULT (result) = 1;
1423 /* Remember the last expression so that finish_stmt_expr
1424 can pull it apart. */
1425 TREE_TYPE (stmt_expr) = result;
1430 /* Finish a statement-expression. EXPR should be the value returned
1431 by the previous begin_stmt_expr. Returns an expression
1432 representing the statement-expression. */
1435 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1437 tree result, result_stmt, type;
1438 tree *result_stmt_p = NULL;
1440 result_stmt = TREE_TYPE (stmt_expr);
1441 TREE_TYPE (stmt_expr) = void_type_node;
1442 result = pop_stmt_list (stmt_expr);
1444 if (!result_stmt || VOID_TYPE_P (result_stmt))
1445 type = void_type_node;
1448 /* We need to search the statement expression for the result_stmt,
1449 since we'll need to replace it entirely. */
1451 result_stmt_p = &result;
1455 if (t == result_stmt)
1458 switch (TREE_CODE (t))
1460 case STATEMENT_LIST:
1462 tree_stmt_iterator i = tsi_last (t);
1463 result_stmt_p = tsi_stmt_ptr (i);
1467 result_stmt_p = &BIND_EXPR_BODY (t);
1469 case TRY_FINALLY_EXPR:
1470 case TRY_CATCH_EXPR:
1472 result_stmt_p = &TREE_OPERAND (t, 0);
1478 type = TREE_TYPE (EXPR_STMT_EXPR (result_stmt));
1481 if (processing_template_decl)
1483 result = build_min (STMT_EXPR, type, result);
1484 TREE_SIDE_EFFECTS (result) = 1;
1485 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1487 else if (!VOID_TYPE_P (type))
1489 /* Pull out the TARGET_EXPR that is the final expression. Put
1490 the target's init_expr as the final expression and then put
1491 the statement expression itself as the target's init
1492 expr. Finally, return the target expression. */
1493 tree init, target_expr = EXPR_STMT_EXPR (result_stmt);
1494 my_friendly_assert (TREE_CODE (target_expr) == TARGET_EXPR, 20030729);
1496 /* The initializer will be void if the initialization is done by
1497 AGGR_INIT_EXPR; propagate that out to the statement-expression as
1499 init = TREE_OPERAND (target_expr, 1);
1500 type = TREE_TYPE (init);
1502 if (stmts_are_full_exprs_p ())
1503 init = fold (build1 (CLEANUP_POINT_EXPR, type, init));
1504 *result_stmt_p = init;
1506 if (VOID_TYPE_P (type))
1507 /* No frobbing needed. */;
1508 else if (TREE_CODE (result) == BIND_EXPR)
1510 /* The BIND_EXPR created in finish_compound_stmt is void; if we're
1511 returning a value directly, give it the appropriate type. */
1512 if (VOID_TYPE_P (TREE_TYPE (result)))
1513 TREE_TYPE (result) = type;
1514 else if (same_type_p (TREE_TYPE (result), type))
1519 else if (TREE_CODE (result) == STATEMENT_LIST)
1520 /* We need to wrap a STATEMENT_LIST in a BIND_EXPR so it can have a
1521 type other than void. FIXME why can't we just return a value
1522 from STATEMENT_LIST? */
1523 result = build3 (BIND_EXPR, type, NULL, result, NULL);
1525 TREE_OPERAND (target_expr, 1) = result;
1526 result = target_expr;
1532 /* Perform Koenig lookup. FN is the postfix-expression representing
1533 the function (or functions) to call; ARGS are the arguments to the
1534 call. Returns the functions to be considered by overload
1538 perform_koenig_lookup (tree fn, tree args)
1540 tree identifier = NULL_TREE;
1541 tree functions = NULL_TREE;
1543 /* Find the name of the overloaded function. */
1544 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1546 else if (is_overloaded_fn (fn))
1549 identifier = DECL_NAME (get_first_fn (functions));
1551 else if (DECL_P (fn))
1554 identifier = DECL_NAME (fn);
1557 /* A call to a namespace-scope function using an unqualified name.
1559 Do Koenig lookup -- unless any of the arguments are
1561 if (!any_type_dependent_arguments_p (args))
1563 fn = lookup_arg_dependent (identifier, functions, args);
1565 /* The unqualified name could not be resolved. */
1566 fn = unqualified_fn_lookup_error (identifier);
1574 /* Generate an expression for `FN (ARGS)'.
1576 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1577 as a virtual call, even if FN is virtual. (This flag is set when
1578 encountering an expression where the function name is explicitly
1579 qualified. For example a call to `X::f' never generates a virtual
1582 Returns code for the call. */
1585 finish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p)
1591 if (fn == error_mark_node || args == error_mark_node)
1592 return error_mark_node;
1594 /* ARGS should be a list of arguments. */
1595 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1601 if (processing_template_decl)
1603 if (type_dependent_expression_p (fn)
1604 || any_type_dependent_arguments_p (args))
1606 result = build_nt (CALL_EXPR, fn, args, NULL_TREE);
1607 KOENIG_LOOKUP_P (result) = koenig_p;
1610 if (!BASELINK_P (fn)
1611 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1612 && TREE_TYPE (fn) != unknown_type_node)
1613 fn = build_non_dependent_expr (fn);
1614 args = build_non_dependent_args (orig_args);
1617 /* A reference to a member function will appear as an overloaded
1618 function (rather than a BASELINK) if an unqualified name was used
1620 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1624 if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
1625 f = TREE_OPERAND (f, 0);
1626 f = get_first_fn (f);
1627 if (DECL_FUNCTION_MEMBER_P (f))
1629 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1631 type = DECL_CONTEXT (f);
1632 fn = build_baselink (TYPE_BINFO (type),
1634 fn, /*optype=*/NULL_TREE);
1639 if (BASELINK_P (fn))
1643 /* A call to a member function. From [over.call.func]:
1645 If the keyword this is in scope and refers to the class of
1646 that member function, or a derived class thereof, then the
1647 function call is transformed into a qualified function call
1648 using (*this) as the postfix-expression to the left of the
1649 . operator.... [Otherwise] a contrived object of type T
1650 becomes the implied object argument.
1652 This paragraph is unclear about this situation:
1654 struct A { void f(); };
1655 struct B : public A {};
1656 struct C : public A { void g() { B::f(); }};
1658 In particular, for `B::f', this paragraph does not make clear
1659 whether "the class of that member function" refers to `A' or
1660 to `B'. We believe it refers to `B'. */
1661 if (current_class_type
1662 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1664 && current_class_ref)
1665 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1669 tree representative_fn;
1671 representative_fn = BASELINK_FUNCTIONS (fn);
1672 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1673 representative_fn = TREE_OPERAND (representative_fn, 0);
1674 representative_fn = get_first_fn (representative_fn);
1675 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1678 if (processing_template_decl)
1680 if (type_dependent_expression_p (object))
1681 return build_nt (CALL_EXPR, orig_fn, orig_args, NULL_TREE);
1682 object = build_non_dependent_expr (object);
1685 result = build_new_method_call (object, fn, args, NULL_TREE,
1687 ? LOOKUP_NONVIRTUAL : 0));
1689 else if (is_overloaded_fn (fn))
1690 /* A call to a namespace-scope function. */
1691 result = build_new_function_call (fn, args);
1692 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1695 error ("arguments to destructor are not allowed");
1696 /* Mark the pseudo-destructor call as having side-effects so
1697 that we do not issue warnings about its use. */
1698 result = build1 (NOP_EXPR,
1700 TREE_OPERAND (fn, 0));
1701 TREE_SIDE_EFFECTS (result) = 1;
1703 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1704 /* If the "function" is really an object of class type, it might
1705 have an overloaded `operator ()'. */
1706 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE,
1707 /*overloaded_p=*/NULL);
1709 /* A call where the function is unknown. */
1710 result = build_function_call (fn, args);
1712 if (processing_template_decl)
1714 result = build (CALL_EXPR, TREE_TYPE (result), orig_fn,
1715 orig_args, NULL_TREE);
1716 KOENIG_LOOKUP_P (result) = koenig_p;
1721 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1722 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1723 POSTDECREMENT_EXPR.) */
1726 finish_increment_expr (tree expr, enum tree_code code)
1728 return build_x_unary_op (code, expr);
1731 /* Finish a use of `this'. Returns an expression for `this'. */
1734 finish_this_expr (void)
1738 if (current_class_ptr)
1740 result = current_class_ptr;
1742 else if (current_function_decl
1743 && DECL_STATIC_FUNCTION_P (current_function_decl))
1745 error ("`this' is unavailable for static member functions");
1746 result = error_mark_node;
1750 if (current_function_decl)
1751 error ("invalid use of `this' in non-member function");
1753 error ("invalid use of `this' at top level");
1754 result = error_mark_node;
1760 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1761 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1762 the TYPE for the type given. If SCOPE is non-NULL, the expression
1763 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1766 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1768 if (destructor == error_mark_node)
1769 return error_mark_node;
1771 my_friendly_assert (TYPE_P (destructor), 20010905);
1773 if (!processing_template_decl)
1775 if (scope == error_mark_node)
1777 error ("invalid qualifying scope in pseudo-destructor name");
1778 return error_mark_node;
1781 /* [expr.pseudo] says both:
1783 The type designated by the pseudo-destructor-name shall be
1784 the same as the object type.
1788 The cv-unqualified versions of the object type and of the
1789 type designated by the pseudo-destructor-name shall be the
1792 We implement the more generous second sentence, since that is
1793 what most other compilers do. */
1794 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
1797 error ("`%E' is not of type `%T'", object, destructor);
1798 return error_mark_node;
1802 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1805 /* Finish an expression of the form CODE EXPR. */
1808 finish_unary_op_expr (enum tree_code code, tree expr)
1810 tree result = build_x_unary_op (code, expr);
1811 /* Inside a template, build_x_unary_op does not fold the
1812 expression. So check whether the result is folded before
1813 setting TREE_NEGATED_INT. */
1814 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1815 && TREE_CODE (result) == INTEGER_CST
1816 && !TYPE_UNSIGNED (TREE_TYPE (result))
1817 && INT_CST_LT (result, integer_zero_node))
1818 TREE_NEGATED_INT (result) = 1;
1819 overflow_warning (result);
1823 /* Finish a compound-literal expression. TYPE is the type to which
1824 the INITIALIZER_LIST is being cast. */
1827 finish_compound_literal (tree type, tree initializer_list)
1829 tree compound_literal;
1831 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1832 compound_literal = build_constructor (NULL_TREE, initializer_list);
1833 /* Mark it as a compound-literal. */
1834 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1835 if (processing_template_decl)
1836 TREE_TYPE (compound_literal) = type;
1839 /* Check the initialization. */
1840 compound_literal = digest_init (type, compound_literal, NULL);
1841 /* If the TYPE was an array type with an unknown bound, then we can
1842 figure out the dimension now. For example, something like:
1846 implies that the array has two elements. */
1847 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1848 complete_array_type (type, compound_literal, 1);
1851 return compound_literal;
1854 /* Return the declaration for the function-name variable indicated by
1858 finish_fname (tree id)
1862 decl = fname_decl (C_RID_CODE (id), id);
1863 if (processing_template_decl)
1864 decl = DECL_NAME (decl);
1868 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1869 and DECLARATOR. Returns nonzero if the function-declaration is
1873 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1875 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1878 /* The things we're about to see are not directly qualified by any
1879 template headers we've seen thus far. */
1880 reset_specialization ();
1885 /* Finish a translation unit. */
1888 finish_translation_unit (void)
1890 /* In case there were missing closebraces,
1891 get us back to the global binding level. */
1893 while (current_namespace != global_namespace)
1896 /* Do file scope __FUNCTION__ et al. */
1897 finish_fname_decls ();
1900 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1901 Returns the parameter. */
1904 finish_template_type_parm (tree aggr, tree identifier)
1906 if (aggr != class_type_node)
1908 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1909 aggr = class_type_node;
1912 return build_tree_list (aggr, identifier);
1915 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1916 Returns the parameter. */
1919 finish_template_template_parm (tree aggr, tree identifier)
1921 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1922 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1923 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1924 DECL_TEMPLATE_RESULT (tmpl) = decl;
1925 DECL_ARTIFICIAL (decl) = 1;
1926 end_template_decl ();
1928 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1930 return finish_template_type_parm (aggr, tmpl);
1933 /* ARGUMENT is the default-argument value for a template template
1934 parameter. If ARGUMENT is invalid, issue error messages and return
1935 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1938 check_template_template_default_arg (tree argument)
1940 if (TREE_CODE (argument) != TEMPLATE_DECL
1941 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1942 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1944 if (TREE_CODE (argument) == TYPE_DECL)
1946 tree t = TREE_TYPE (argument);
1948 /* Try to emit a slightly smarter error message if we detect
1949 that the user is using a template instantiation. */
1950 if (CLASSTYPE_TEMPLATE_INFO (t)
1951 && CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1952 error ("invalid use of type `%T' as a default value for a "
1953 "template template-parameter", t);
1955 error ("invalid use of `%D' as a default value for a template "
1956 "template-parameter", argument);
1959 error ("invalid default argument for a template template parameter");
1960 return error_mark_node;
1966 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1967 nonzero, the parameter list was terminated by a `...'. */
1970 finish_parmlist (tree parms, int ellipsis)
1974 /* We mark the PARMS as a parmlist so that declarator processing can
1975 disambiguate certain constructs. */
1976 TREE_PARMLIST (parms) = 1;
1977 /* We do not append void_list_node here, but leave it to grokparms
1979 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1984 /* Begin a class definition, as indicated by T. */
1987 begin_class_definition (tree t)
1989 if (t == error_mark_node)
1990 return error_mark_node;
1992 if (processing_template_parmlist)
1994 error ("definition of `%#T' inside template parameter list", t);
1995 return error_mark_node;
1997 /* A non-implicit typename comes from code like:
1999 template <typename T> struct A {
2000 template <typename U> struct A<T>::B ...
2002 This is erroneous. */
2003 else if (TREE_CODE (t) == TYPENAME_TYPE)
2005 error ("invalid definition of qualified type `%T'", t);
2006 t = error_mark_node;
2009 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
2011 t = make_aggr_type (RECORD_TYPE);
2012 pushtag (make_anon_name (), t, 0);
2015 /* If this type was already complete, and we see another definition,
2017 if (COMPLETE_TYPE_P (t))
2019 error ("redefinition of `%#T'", t);
2020 cp_error_at ("previous definition of `%#T'", t);
2021 return error_mark_node;
2024 /* Update the location of the decl. */
2025 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
2027 if (TYPE_BEING_DEFINED (t))
2029 t = make_aggr_type (TREE_CODE (t));
2030 pushtag (TYPE_IDENTIFIER (t), t, 0);
2032 maybe_process_partial_specialization (t);
2034 TYPE_BEING_DEFINED (t) = 1;
2035 if (flag_pack_struct)
2038 TYPE_PACKED (t) = 1;
2039 /* Even though the type is being defined for the first time
2040 here, there might have been a forward declaration, so there
2041 might be cv-qualified variants of T. */
2042 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2043 TYPE_PACKED (v) = 1;
2045 /* Reset the interface data, at the earliest possible
2046 moment, as it might have been set via a class foo;
2048 if (! TYPE_ANONYMOUS_P (t))
2050 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2051 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2052 (t, interface_unknown);
2054 reset_specialization();
2056 /* Make a declaration for this class in its own scope. */
2057 build_self_reference ();
2062 /* Finish the member declaration given by DECL. */
2065 finish_member_declaration (tree decl)
2067 if (decl == error_mark_node || decl == NULL_TREE)
2070 if (decl == void_type_node)
2071 /* The COMPONENT was a friend, not a member, and so there's
2072 nothing for us to do. */
2075 /* We should see only one DECL at a time. */
2076 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
2078 /* Set up access control for DECL. */
2080 = (current_access_specifier == access_private_node);
2081 TREE_PROTECTED (decl)
2082 = (current_access_specifier == access_protected_node);
2083 if (TREE_CODE (decl) == TEMPLATE_DECL)
2085 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2086 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2089 /* Mark the DECL as a member of the current class. */
2090 DECL_CONTEXT (decl) = current_class_type;
2094 A C language linkage is ignored for the names of class members
2095 and the member function type of class member functions. */
2096 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2097 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2099 /* Put functions on the TYPE_METHODS list and everything else on the
2100 TYPE_FIELDS list. Note that these are built up in reverse order.
2101 We reverse them (to obtain declaration order) in finish_struct. */
2102 if (TREE_CODE (decl) == FUNCTION_DECL
2103 || DECL_FUNCTION_TEMPLATE_P (decl))
2105 /* We also need to add this function to the
2106 CLASSTYPE_METHOD_VEC. */
2107 add_method (current_class_type, decl, /*error_p=*/0);
2109 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2110 TYPE_METHODS (current_class_type) = decl;
2112 maybe_add_class_template_decl_list (current_class_type, decl,
2115 /* Enter the DECL into the scope of the class. */
2116 else if ((TREE_CODE (decl) == USING_DECL && TREE_TYPE (decl))
2117 || pushdecl_class_level (decl))
2119 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2120 go at the beginning. The reason is that lookup_field_1
2121 searches the list in order, and we want a field name to
2122 override a type name so that the "struct stat hack" will
2123 work. In particular:
2125 struct S { enum E { }; int E } s;
2128 is valid. In addition, the FIELD_DECLs must be maintained in
2129 declaration order so that class layout works as expected.
2130 However, we don't need that order until class layout, so we
2131 save a little time by putting FIELD_DECLs on in reverse order
2132 here, and then reversing them in finish_struct_1. (We could
2133 also keep a pointer to the correct insertion points in the
2136 if (TREE_CODE (decl) == TYPE_DECL)
2137 TYPE_FIELDS (current_class_type)
2138 = chainon (TYPE_FIELDS (current_class_type), decl);
2141 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2142 TYPE_FIELDS (current_class_type) = decl;
2145 maybe_add_class_template_decl_list (current_class_type, decl,
2150 /* Finish processing the declaration of a member class template
2151 TYPES whose template parameters are given by PARMS. */
2154 finish_member_class_template (tree types)
2158 /* If there are declared, but undefined, partial specializations
2159 mixed in with the typespecs they will not yet have passed through
2160 maybe_process_partial_specialization, so we do that here. */
2161 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2162 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2163 maybe_process_partial_specialization (TREE_VALUE (t));
2165 grok_x_components (types);
2166 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2167 /* The component was in fact a friend declaration. We avoid
2168 finish_member_template_decl performing certain checks by
2172 finish_member_template_decl (types);
2174 /* As with other component type declarations, we do
2175 not store the new DECL on the list of
2180 /* Finish processing a complete template declaration. The PARMS are
2181 the template parameters. */
2184 finish_template_decl (tree parms)
2187 end_template_decl ();
2189 end_specialization ();
2192 /* Finish processing a template-id (which names a type) of the form
2193 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2194 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2195 the scope of template-id indicated. */
2198 finish_template_type (tree name, tree args, int entering_scope)
2202 decl = lookup_template_class (name, args,
2203 NULL_TREE, NULL_TREE, entering_scope,
2204 tf_error | tf_warning | tf_user);
2205 if (decl != error_mark_node)
2206 decl = TYPE_STUB_DECL (decl);
2211 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2212 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2213 BASE_CLASS, or NULL_TREE if an error occurred. The
2214 ACCESS_SPECIFIER is one of
2215 access_{default,public,protected_private}[_virtual]_node.*/
2218 finish_base_specifier (tree base, tree access, bool virtual_p)
2222 if (base == error_mark_node)
2224 error ("invalid base-class specification");
2227 else if (! is_aggr_type (base, 1))
2231 if (cp_type_quals (base) != 0)
2233 error ("base class `%T' has cv qualifiers", base);
2234 base = TYPE_MAIN_VARIANT (base);
2236 result = build_tree_list (access, base);
2237 TREE_VIA_VIRTUAL (result) = virtual_p;
2243 /* Called when multiple declarators are processed. If that is not
2244 permitted in this context, an error is issued. */
2247 check_multiple_declarators (void)
2251 In a template-declaration, explicit specialization, or explicit
2252 instantiation the init-declarator-list in the declaration shall
2253 contain at most one declarator.
2255 We don't just use PROCESSING_TEMPLATE_DECL for the first
2256 condition since that would disallow the perfectly valid code,
2257 like `template <class T> struct S { int i, j; };'. */
2258 if (at_function_scope_p ())
2259 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2262 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2263 || processing_explicit_instantiation
2264 || processing_specialization)
2265 error ("multiple declarators in template declaration");
2268 /* Issue a diagnostic that NAME cannot be found in SCOPE. */
2271 qualified_name_lookup_error (tree scope, tree name)
2275 if (!COMPLETE_TYPE_P (scope))
2276 error ("incomplete type `%T' used in nested name specifier", scope);
2278 error ("`%D' is not a member of `%T'", name, scope);
2280 else if (scope != global_namespace)
2281 error ("`%D' is not a member of `%D'", name, scope);
2283 error ("`::%D' has not been declared", name);
2286 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2287 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2288 if non-NULL, is the type or namespace used to explicitly qualify
2289 ID_EXPRESSION. DECL is the entity to which that name has been
2292 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2293 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2294 be set to true if this expression isn't permitted in a
2295 constant-expression, but it is otherwise not set by this function.
2296 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2297 constant-expression, but a non-constant expression is also
2300 If an error occurs, and it is the kind of error that might cause
2301 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2302 is the caller's responsibility to issue the message. *ERROR_MSG
2303 will be a string with static storage duration, so the caller need
2306 Return an expression for the entity, after issuing appropriate
2307 diagnostics. This function is also responsible for transforming a
2308 reference to a non-static member into a COMPONENT_REF that makes
2309 the use of "this" explicit.
2311 Upon return, *IDK will be filled in appropriately. */
2314 finish_id_expression (tree id_expression,
2318 tree *qualifying_class,
2319 bool integral_constant_expression_p,
2320 bool allow_non_integral_constant_expression_p,
2321 bool *non_integral_constant_expression_p,
2322 const char **error_msg)
2324 /* Initialize the output parameters. */
2325 *idk = CP_ID_KIND_NONE;
2328 if (id_expression == error_mark_node)
2329 return error_mark_node;
2330 /* If we have a template-id, then no further lookup is
2331 required. If the template-id was for a template-class, we
2332 will sometimes have a TYPE_DECL at this point. */
2333 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2334 || TREE_CODE (decl) == TYPE_DECL)
2336 /* Look up the name. */
2339 if (decl == error_mark_node)
2341 /* Name lookup failed. */
2344 || (!dependent_type_p (scope)
2345 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2346 && IDENTIFIER_TYPENAME_P (id_expression)
2347 && dependent_type_p (TREE_TYPE (id_expression))))))
2349 /* If the qualifying type is non-dependent (and the name
2350 does not name a conversion operator to a dependent
2351 type), issue an error. */
2352 qualified_name_lookup_error (scope, id_expression);
2353 return error_mark_node;
2357 /* It may be resolved via Koenig lookup. */
2358 *idk = CP_ID_KIND_UNQUALIFIED;
2359 return id_expression;
2362 decl = id_expression;
2364 /* If DECL is a variable that would be out of scope under
2365 ANSI/ISO rules, but in scope in the ARM, name lookup
2366 will succeed. Issue a diagnostic here. */
2368 decl = check_for_out_of_scope_variable (decl);
2370 /* Remember that the name was used in the definition of
2371 the current class so that we can check later to see if
2372 the meaning would have been different after the class
2373 was entirely defined. */
2374 if (!scope && decl != error_mark_node)
2375 maybe_note_name_used_in_class (id_expression, decl);
2378 /* If we didn't find anything, or what we found was a type,
2379 then this wasn't really an id-expression. */
2380 if (TREE_CODE (decl) == TEMPLATE_DECL
2381 && !DECL_FUNCTION_TEMPLATE_P (decl))
2383 *error_msg = "missing template arguments";
2384 return error_mark_node;
2386 else if (TREE_CODE (decl) == TYPE_DECL
2387 || TREE_CODE (decl) == NAMESPACE_DECL)
2389 *error_msg = "expected primary-expression";
2390 return error_mark_node;
2393 /* If the name resolved to a template parameter, there is no
2394 need to look it up again later. */
2395 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
2396 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2398 *idk = CP_ID_KIND_NONE;
2399 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2400 decl = TEMPLATE_PARM_DECL (decl);
2401 if (integral_constant_expression_p
2402 && !dependent_type_p (TREE_TYPE (decl))
2403 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2405 if (!allow_non_integral_constant_expression_p)
2406 error ("template parameter `%D' of type `%T' is not allowed in "
2407 "an integral constant expression because it is not of "
2408 "integral or enumeration type", decl, TREE_TYPE (decl));
2409 *non_integral_constant_expression_p = true;
2411 return DECL_INITIAL (decl);
2413 /* Similarly, we resolve enumeration constants to their
2414 underlying values. */
2415 else if (TREE_CODE (decl) == CONST_DECL)
2417 *idk = CP_ID_KIND_NONE;
2418 if (!processing_template_decl)
2419 return DECL_INITIAL (decl);
2426 /* If the declaration was explicitly qualified indicate
2427 that. The semantics of `A::f(3)' are different than
2428 `f(3)' if `f' is virtual. */
2430 ? CP_ID_KIND_QUALIFIED
2431 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2432 ? CP_ID_KIND_TEMPLATE_ID
2433 : CP_ID_KIND_UNQUALIFIED));
2438 An id-expression is type-dependent if it contains an
2439 identifier that was declared with a dependent type.
2441 The standard is not very specific about an id-expression that
2442 names a set of overloaded functions. What if some of them
2443 have dependent types and some of them do not? Presumably,
2444 such a name should be treated as a dependent name. */
2445 /* Assume the name is not dependent. */
2446 dependent_p = false;
2447 if (!processing_template_decl)
2448 /* No names are dependent outside a template. */
2450 /* A template-id where the name of the template was not resolved
2451 is definitely dependent. */
2452 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2453 && (TREE_CODE (TREE_OPERAND (decl, 0))
2454 == IDENTIFIER_NODE))
2456 /* For anything except an overloaded function, just check its
2458 else if (!is_overloaded_fn (decl))
2460 = dependent_type_p (TREE_TYPE (decl));
2461 /* For a set of overloaded functions, check each of the
2467 if (BASELINK_P (fns))
2468 fns = BASELINK_FUNCTIONS (fns);
2470 /* For a template-id, check to see if the template
2471 arguments are dependent. */
2472 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2474 tree args = TREE_OPERAND (fns, 1);
2475 dependent_p = any_dependent_template_arguments_p (args);
2476 /* The functions are those referred to by the
2478 fns = TREE_OPERAND (fns, 0);
2481 /* If there are no dependent template arguments, go through
2482 the overloaded functions. */
2483 while (fns && !dependent_p)
2485 tree fn = OVL_CURRENT (fns);
2487 /* Member functions of dependent classes are
2489 if (TREE_CODE (fn) == FUNCTION_DECL
2490 && type_dependent_expression_p (fn))
2492 else if (TREE_CODE (fn) == TEMPLATE_DECL
2493 && dependent_template_p (fn))
2496 fns = OVL_NEXT (fns);
2500 /* If the name was dependent on a template parameter, we will
2501 resolve the name at instantiation time. */
2504 /* Create a SCOPE_REF for qualified names, if the scope is
2509 *qualifying_class = scope;
2510 /* Since this name was dependent, the expression isn't
2511 constant -- yet. No error is issued because it might
2512 be constant when things are instantiated. */
2513 if (integral_constant_expression_p)
2514 *non_integral_constant_expression_p = true;
2515 if (TYPE_P (scope) && dependent_type_p (scope))
2516 return build_nt (SCOPE_REF, scope, id_expression);
2517 else if (TYPE_P (scope) && DECL_P (decl))
2518 return build (SCOPE_REF, TREE_TYPE (decl), scope,
2523 /* A TEMPLATE_ID already contains all the information we
2525 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2526 return id_expression;
2527 /* Since this name was dependent, the expression isn't
2528 constant -- yet. No error is issued because it might be
2529 constant when things are instantiated. */
2530 if (integral_constant_expression_p)
2531 *non_integral_constant_expression_p = true;
2532 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2533 /* If we found a variable, then name lookup during the
2534 instantiation will always resolve to the same VAR_DECL
2535 (or an instantiation thereof). */
2536 if (TREE_CODE (decl) == VAR_DECL
2537 || TREE_CODE (decl) == PARM_DECL)
2539 return id_expression;
2542 /* Only certain kinds of names are allowed in constant
2543 expression. Enumerators and template parameters
2544 have already been handled above. */
2545 if (integral_constant_expression_p)
2547 /* Const variables or static data members of integral or
2548 enumeration types initialized with constant expressions
2550 if (TREE_CODE (decl) == VAR_DECL
2551 && CP_TYPE_CONST_P (TREE_TYPE (decl))
2552 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2553 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2557 if (!allow_non_integral_constant_expression_p)
2559 error ("`%D' cannot appear in a constant-expression", decl);
2560 return error_mark_node;
2562 *non_integral_constant_expression_p = true;
2566 if (TREE_CODE (decl) == NAMESPACE_DECL)
2568 error ("use of namespace `%D' as expression", decl);
2569 return error_mark_node;
2571 else if (DECL_CLASS_TEMPLATE_P (decl))
2573 error ("use of class template `%T' as expression", decl);
2574 return error_mark_node;
2576 else if (TREE_CODE (decl) == TREE_LIST)
2578 /* Ambiguous reference to base members. */
2579 error ("request for member `%D' is ambiguous in "
2580 "multiple inheritance lattice", id_expression);
2581 print_candidates (decl);
2582 return error_mark_node;
2585 /* Mark variable-like entities as used. Functions are similarly
2586 marked either below or after overload resolution. */
2587 if (TREE_CODE (decl) == VAR_DECL
2588 || TREE_CODE (decl) == PARM_DECL
2589 || TREE_CODE (decl) == RESULT_DECL)
2594 decl = (adjust_result_of_qualified_name_lookup
2595 (decl, scope, current_class_type));
2597 if (TREE_CODE (decl) == FUNCTION_DECL)
2600 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2601 *qualifying_class = scope;
2602 else if (!processing_template_decl)
2603 decl = convert_from_reference (decl);
2604 else if (TYPE_P (scope))
2605 decl = build (SCOPE_REF, TREE_TYPE (decl), scope, decl);
2607 else if (TREE_CODE (decl) == FIELD_DECL)
2608 decl = finish_non_static_data_member (decl, current_class_ref,
2609 /*qualifying_scope=*/NULL_TREE);
2610 else if (is_overloaded_fn (decl))
2612 tree first_fn = OVL_CURRENT (decl);
2614 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2615 first_fn = DECL_TEMPLATE_RESULT (first_fn);
2617 if (!really_overloaded_fn (decl))
2618 mark_used (first_fn);
2620 if (TREE_CODE (first_fn) == FUNCTION_DECL
2621 && DECL_FUNCTION_MEMBER_P (first_fn))
2623 /* A set of member functions. */
2624 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2625 return finish_class_member_access_expr (decl, id_expression);
2630 if (TREE_CODE (decl) == VAR_DECL
2631 || TREE_CODE (decl) == PARM_DECL
2632 || TREE_CODE (decl) == RESULT_DECL)
2634 tree context = decl_function_context (decl);
2636 if (context != NULL_TREE && context != current_function_decl
2637 && ! TREE_STATIC (decl))
2639 error ("use of %s from containing function",
2640 (TREE_CODE (decl) == VAR_DECL
2641 ? "`auto' variable" : "parameter"));
2642 cp_error_at (" `%#D' declared here", decl);
2643 return error_mark_node;
2647 if (DECL_P (decl) && DECL_NONLOCAL (decl)
2648 && DECL_CLASS_SCOPE_P (decl)
2649 && DECL_CONTEXT (decl) != current_class_type)
2653 path = currently_open_derived_class (DECL_CONTEXT (decl));
2654 perform_or_defer_access_check (TYPE_BINFO (path), decl);
2657 if (! processing_template_decl)
2658 decl = convert_from_reference (decl);
2661 /* Resolve references to variables of anonymous unions
2662 into COMPONENT_REFs. */
2663 if (TREE_CODE (decl) == ALIAS_DECL)
2664 decl = unshare_expr (DECL_INITIAL (decl));
2667 if (TREE_DEPRECATED (decl))
2668 warn_deprecated_use (decl);
2673 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2674 use as a type-specifier. */
2677 finish_typeof (tree expr)
2681 if (type_dependent_expression_p (expr))
2683 type = make_aggr_type (TYPEOF_TYPE);
2684 TYPEOF_TYPE_EXPR (type) = expr;
2689 type = TREE_TYPE (expr);
2691 if (!type || type == unknown_type_node)
2693 error ("type of `%E' is unknown", expr);
2694 return error_mark_node;
2700 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2701 with equivalent CALL_EXPRs. */
2704 simplify_aggr_init_exprs_r (tree* tp,
2706 void* data ATTRIBUTE_UNUSED)
2708 /* We don't need to walk into types; there's nothing in a type that
2709 needs simplification. (And, furthermore, there are places we
2710 actively don't want to go. For example, we don't want to wander
2711 into the default arguments for a FUNCTION_DECL that appears in a
2718 /* Only AGGR_INIT_EXPRs are interesting. */
2719 else if (TREE_CODE (*tp) != AGGR_INIT_EXPR)
2722 simplify_aggr_init_expr (tp);
2724 /* Keep iterating. */
2728 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
2729 function is broken out from the above for the benefit of the tree-ssa
2733 simplify_aggr_init_expr (tree *tp)
2735 tree aggr_init_expr = *tp;
2737 /* Form an appropriate CALL_EXPR. */
2738 tree fn = TREE_OPERAND (aggr_init_expr, 0);
2739 tree args = TREE_OPERAND (aggr_init_expr, 1);
2740 tree slot = TREE_OPERAND (aggr_init_expr, 2);
2741 tree type = TREE_TYPE (slot);
2744 enum style_t { ctor, arg, pcc } style;
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. */
2766 args = TREE_CHAIN (args);
2768 cxx_mark_addressable (slot);
2769 addr = build1 (ADDR_EXPR, build_pointer_type (type), slot);
2772 /* The return type might have different cv-quals from the slot. */
2773 tree fntype = TREE_TYPE (TREE_TYPE (fn));
2774 #ifdef ENABLE_CHECKING
2775 if (TREE_CODE (fntype) != FUNCTION_TYPE
2776 && TREE_CODE (fntype) != METHOD_TYPE)
2779 addr = convert (build_pointer_type (TREE_TYPE (fntype)), addr);
2782 args = tree_cons (NULL_TREE, addr, args);
2785 call_expr = build (CALL_EXPR,
2786 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2787 fn, args, NULL_TREE);
2790 /* Tell the backend that we've added our return slot to the argument
2792 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2793 else if (style == pcc)
2795 /* If we're using the non-reentrant PCC calling convention, then we
2796 need to copy the returned value out of the static buffer into the
2798 push_deferring_access_checks (dk_no_check);
2799 call_expr = build_aggr_init (slot, call_expr,
2800 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2801 pop_deferring_access_checks ();
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 if (!THUNK_ALIAS (thunk))
2825 use_thunk (thunk, /*emit_p=*/1);
2826 if (DECL_RESULT_THUNK_P (thunk))
2830 for (probe = DECL_THUNKS (thunk);
2831 probe; probe = TREE_CHAIN (probe))
2832 use_thunk (probe, /*emit_p=*/1);
2836 my_friendly_assert (!DECL_THUNKS (thunk), 20031023);
2841 /* Generate RTL for FN. */
2844 expand_body (tree fn)
2846 tree saved_function;
2848 /* Compute the appropriate object-file linkage for inline
2850 if (DECL_DECLARED_INLINE_P (fn))
2851 import_export_decl (fn);
2853 /* If FN is external, then there's no point in generating RTL for
2854 it. This situation can arise with an inline function under
2855 `-fexternal-templates'; we instantiate the function, even though
2856 we're not planning on emitting it, in case we get a chance to
2858 if (DECL_EXTERNAL (fn))
2861 /* ??? When is this needed? */
2862 saved_function = current_function_decl;
2864 /* Emit any thunks that should be emitted at the same time as FN. */
2865 emit_associated_thunks (fn);
2867 tree_rest_of_compilation (fn, function_depth > 1);
2869 current_function_decl = saved_function;
2871 extract_interface_info ();
2873 /* If this function is marked with the constructor attribute, add it
2874 to the list of functions to be called along with constructors
2875 from static duration objects. */
2876 if (DECL_STATIC_CONSTRUCTOR (fn))
2877 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2879 /* If this function is marked with the destructor attribute, add it
2880 to the list of functions to be called along with destructors from
2881 static duration objects. */
2882 if (DECL_STATIC_DESTRUCTOR (fn))
2883 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2885 if (DECL_CLONED_FUNCTION_P (fn))
2887 /* If this is a clone, go through the other clones now and mark
2888 their parameters used. We have to do that here, as we don't
2889 know whether any particular clone will be expanded, and
2890 therefore cannot pick one arbitrarily. */
2893 for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn));
2894 probe && DECL_CLONED_FUNCTION_P (probe);
2895 probe = TREE_CHAIN (probe))
2899 for (parms = DECL_ARGUMENTS (probe);
2900 parms; parms = TREE_CHAIN (parms))
2901 TREE_USED (parms) = 1;
2906 /* Generate RTL for FN. */
2909 expand_or_defer_fn (tree fn)
2911 /* When the parser calls us after finishing the body of a template
2912 function, we don't really want to expand the body. */
2913 if (processing_template_decl)
2915 /* Normally, collection only occurs in rest_of_compilation. So,
2916 if we don't collect here, we never collect junk generated
2917 during the processing of templates until we hit a
2918 non-template function. */
2923 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2924 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2925 simplify_aggr_init_exprs_r,
2928 /* If this is a constructor or destructor body, we have to clone
2930 if (maybe_clone_body (fn))
2932 /* We don't want to process FN again, so pretend we've written
2933 it out, even though we haven't. */
2934 TREE_ASM_WRITTEN (fn) = 1;
2938 /* There's no reason to do any of the work here if we're only doing
2939 semantic analysis; this code just generates RTL. */
2940 if (flag_syntax_only)
2943 /* Compute the appropriate object-file linkage for inline functions. */
2944 if (DECL_DECLARED_INLINE_P (fn))
2945 import_export_decl (fn);
2949 /* Expand or defer, at the whim of the compilation unit manager. */
2950 cgraph_finalize_function (fn, function_depth > 1);
2962 /* Helper function for walk_tree, used by finalize_nrv below. */
2965 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
2967 struct nrv_data *dp = (struct nrv_data *)data;
2970 /* No need to walk into types. There wouldn't be any need to walk into
2971 non-statements, except that we have to consider STMT_EXPRs. */
2974 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
2975 but differs from using NULL_TREE in that it indicates that we care
2976 about the value of the RESULT_DECL. */
2977 else if (TREE_CODE (*tp) == RETURN_STMT)
2978 RETURN_STMT_EXPR (*tp) = dp->result;
2979 /* Change all cleanups for the NRV to only run when an exception is
2981 else if (TREE_CODE (*tp) == CLEANUP_STMT
2982 && CLEANUP_DECL (*tp) == dp->var)
2983 CLEANUP_EH_ONLY (*tp) = 1;
2984 /* Replace the DECL_STMT for the NRV with an initialization of the
2985 RESULT_DECL, if needed. */
2986 else if (TREE_CODE (*tp) == DECL_STMT
2987 && DECL_STMT_DECL (*tp) == dp->var)
2990 if (DECL_INITIAL (dp->var)
2991 && DECL_INITIAL (dp->var) != error_mark_node)
2993 init = build (INIT_EXPR, void_type_node, dp->result,
2994 DECL_INITIAL (dp->var));
2995 DECL_INITIAL (dp->var) = error_mark_node;
2999 init = build_stmt (EXPR_STMT, init);
3000 SET_EXPR_LOCUS (init, EXPR_LOCUS (*tp));
3001 TREE_CHAIN (init) = TREE_CHAIN (*tp);
3004 /* And replace all uses of the NRV with the RESULT_DECL. */
3005 else if (*tp == dp->var)
3008 /* Avoid walking into the same tree more than once. Unfortunately, we
3009 can't just use walk_tree_without duplicates because it would only call
3010 us for the first occurrence of dp->var in the function body. */
3011 slot = htab_find_slot (dp->visited, *tp, INSERT);
3017 /* Keep iterating. */
3021 /* Called from finish_function to implement the named return value
3022 optimization by overriding all the RETURN_STMTs and pertinent
3023 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3024 RESULT_DECL for the function. */
3027 finalize_nrv (tree *tp, tree var, tree result)
3029 struct nrv_data data;
3031 /* Copy debugging information from VAR to RESULT. */
3032 DECL_NAME (result) = DECL_NAME (var);
3033 DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (var);
3034 DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (var);
3035 /* Don't forget that we take its address. */
3036 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3039 data.result = result;
3040 data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3041 walk_tree (tp, finalize_nrv_r, &data, 0);
3042 htab_delete (data.visited);
3045 /* Perform initialization related to this module. */
3048 init_cp_semantics (void)
3052 #include "gt-cp-semantics.h"