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_STMT, 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);
658 /* Finish the for-init-statement of a for-statement, which may be
659 given by FOR_STMT. */
662 finish_for_init_stmt (tree for_stmt)
665 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
666 FOR_COND (for_stmt) = push_stmt_list ();
669 /* Finish the COND of a for-statement, which may be given by
673 finish_for_cond (tree cond, tree for_stmt)
675 cond = maybe_convert_cond (cond);
676 finish_cond (cond, &FOR_COND (for_stmt));
677 if (FOR_COND (for_stmt))
678 simplify_loop_decl_cond (&FOR_COND (for_stmt));
681 /* Finish the increment-EXPRESSION in a for-statement, which may be
682 given by FOR_STMT. */
685 finish_for_expr (tree expr, tree for_stmt)
687 /* If EXPR is an overloaded function, issue an error; there is no
688 context available to use to perform overload resolution. */
689 if (expr && type_unknown_p (expr))
691 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
692 expr = error_mark_node;
694 FOR_EXPR (for_stmt) = expr;
697 /* Finish the body of a for-statement, which may be given by
698 FOR_STMT. The increment-EXPR for the loop must be
702 finish_for_stmt (tree for_stmt)
704 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
706 /* Pop the scope for the body of the loop. */
707 if (flag_new_for_scope > 0)
709 tree scope = TREE_CHAIN (for_stmt);
710 TREE_CHAIN (for_stmt) = NULL;
711 add_stmt (do_poplevel (scope));
717 /* Finish a break-statement. */
720 finish_break_stmt (void)
722 return add_stmt (build_break_stmt ());
725 /* Finish a continue-statement. */
728 finish_continue_stmt (void)
730 return add_stmt (build_continue_stmt ());
733 /* Begin a switch-statement. Returns a new SWITCH_STMT if
737 begin_switch_stmt (void)
741 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
743 scope = do_pushlevel (sk_block);
744 TREE_CHAIN (r) = scope;
747 SWITCH_COND (r) = push_stmt_list ();
752 /* Finish the cond of a switch-statement. */
755 finish_switch_cond (tree cond, tree switch_stmt)
757 tree orig_type = NULL;
758 if (!processing_template_decl)
762 /* Convert the condition to an integer or enumeration type. */
763 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
764 if (cond == NULL_TREE)
766 error ("switch quantity not an integer");
767 cond = error_mark_node;
769 orig_type = TREE_TYPE (cond);
770 if (cond != error_mark_node)
774 Integral promotions are performed. */
775 cond = perform_integral_promotions (cond);
776 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
779 if (cond != error_mark_node)
781 index = get_unwidened (cond, NULL_TREE);
782 /* We can't strip a conversion from a signed type to an unsigned,
783 because if we did, int_fits_type_p would do the wrong thing
784 when checking case values for being in range,
785 and it's too hard to do the right thing. */
786 if (TYPE_UNSIGNED (TREE_TYPE (cond))
787 == TYPE_UNSIGNED (TREE_TYPE (index)))
791 finish_cond (cond, &SWITCH_COND (switch_stmt));
792 SWITCH_TYPE (switch_stmt) = orig_type;
793 push_switch (switch_stmt);
794 SWITCH_BODY (switch_stmt) = push_stmt_list ();
797 /* Finish the body of a switch-statement, which may be given by
798 SWITCH_STMT. The COND to switch on is indicated. */
801 finish_switch_stmt (tree switch_stmt)
805 SWITCH_BODY (switch_stmt) = pop_stmt_list (SWITCH_BODY (switch_stmt));
809 scope = TREE_CHAIN (switch_stmt);
810 TREE_CHAIN (switch_stmt) = NULL;
811 add_stmt (do_poplevel (scope));
814 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
818 begin_try_block (void)
820 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
822 TRY_STMTS (r) = push_stmt_list ();
826 /* Likewise, for a function-try-block. */
829 begin_function_try_block (void)
831 tree r = begin_try_block ();
832 FN_TRY_BLOCK_P (r) = 1;
836 /* Finish a try-block, which may be given by TRY_BLOCK. */
839 finish_try_block (tree try_block)
841 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
842 TRY_HANDLERS (try_block) = push_stmt_list ();
845 /* Finish the body of a cleanup try-block, which may be given by
849 finish_cleanup_try_block (tree try_block)
851 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
854 /* Finish an implicitly generated try-block, with a cleanup is given
858 finish_cleanup (tree cleanup, tree try_block)
860 TRY_HANDLERS (try_block) = cleanup;
861 CLEANUP_P (try_block) = 1;
864 /* Likewise, for a function-try-block. */
867 finish_function_try_block (tree try_block)
869 finish_try_block (try_block);
870 /* FIXME : something queer about CTOR_INITIALIZER somehow following
871 the try block, but moving it inside. */
872 in_function_try_handler = 1;
875 /* Finish a handler-sequence for a try-block, which may be given by
879 finish_handler_sequence (tree try_block)
881 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
882 check_handlers (TRY_HANDLERS (try_block));
885 /* Likewise, for a function-try-block. */
888 finish_function_handler_sequence (tree try_block)
890 in_function_try_handler = 0;
891 finish_handler_sequence (try_block);
894 /* Begin a handler. Returns a HANDLER if appropriate. */
901 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
904 /* Create a binding level for the eh_info and the exception object
906 HANDLER_BODY (r) = do_pushlevel (sk_catch);
911 /* Finish the handler-parameters for a handler, which may be given by
912 HANDLER. DECL is the declaration for the catch parameter, or NULL
913 if this is a `catch (...)' clause. */
916 finish_handler_parms (tree decl, tree handler)
918 tree type = NULL_TREE;
919 if (processing_template_decl)
923 decl = pushdecl (decl);
924 decl = push_template_decl (decl);
925 HANDLER_PARMS (handler) = decl;
926 type = TREE_TYPE (decl);
930 type = expand_start_catch_block (decl);
932 HANDLER_TYPE (handler) = type;
933 if (!processing_template_decl && type)
934 mark_used (eh_type_info (type));
937 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
938 the return value from the matching call to finish_handler_parms. */
941 finish_handler (tree handler)
943 if (!processing_template_decl)
944 expand_end_catch_block ();
945 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
948 /* Begin a compound-statement. If HAS_NO_SCOPE is true, the
949 compound-statement does not define a scope. Returns a new
953 begin_compound_stmt (unsigned int flags)
957 if (flags & BCS_NO_SCOPE)
959 r = push_stmt_list ();
960 STATEMENT_LIST_NO_SCOPE (r) = 1;
962 /* Normally, we try hard to keep the BLOCK for a statement-expression.
963 But, if it's a statement-expression with a scopeless block, there's
964 nothing to keep, and we don't want to accidentally keep a block
965 *inside* the scopeless block. */
966 keep_next_level (false);
969 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
971 if (flags & BCS_FN_BODY || processing_template_decl)
973 r = build (COMPOUND_STMT, NULL_TREE, r);
974 COMPOUND_STMT_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
975 COMPOUND_STMT_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
976 TREE_SIDE_EFFECTS (r) = 1;
982 /* Finish a compound-statement, which is given by COMPOUND_STMT. */
985 finish_compound_stmt (tree stmt)
987 if (TREE_CODE (stmt) == COMPOUND_STMT)
988 COMPOUND_BODY (stmt) = do_poplevel (COMPOUND_BODY (stmt));
989 else if (STATEMENT_LIST_NO_SCOPE (stmt))
990 stmt = pop_stmt_list (stmt);
992 stmt = do_poplevel (stmt);
994 /* ??? See c_end_compound_stmt wrt statement expressions. */
999 /* Finish an asm-statement, whose components are a STRING, some
1000 OUTPUT_OPERANDS, some INPUT_OPERANDS, and some CLOBBERS. Also note
1001 whether the asm-statement should be considered volatile. */
1004 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1005 tree input_operands, tree clobbers)
1010 if (!processing_template_decl)
1016 for (t = input_operands; t; t = TREE_CHAIN (t))
1018 tree converted_operand
1019 = decay_conversion (TREE_VALUE (t));
1021 /* If the type of the operand hasn't been determined (e.g.,
1022 because it involves an overloaded function), then issue
1023 an error message. There's no context available to
1024 resolve the overloading. */
1025 if (TREE_TYPE (converted_operand) == unknown_type_node)
1027 error ("type of asm operand `%E' could not be determined",
1029 converted_operand = error_mark_node;
1031 TREE_VALUE (t) = converted_operand;
1034 ninputs = list_length (input_operands);
1035 noutputs = list_length (output_operands);
1037 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1042 const char *constraint;
1045 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1046 operand = TREE_VALUE (t);
1048 if (!parse_output_constraint (&constraint,
1049 i, ninputs, noutputs,
1054 /* By marking this operand as erroneous, we will not try
1055 to process this operand again in expand_asm_operands. */
1056 TREE_VALUE (t) = error_mark_node;
1060 /* If the operand is a DECL that is going to end up in
1061 memory, assume it is addressable. This is a bit more
1062 conservative than it would ideally be; the exact test is
1063 buried deep in expand_asm_operands and depends on the
1064 DECL_RTL for the OPERAND -- which we don't have at this
1066 if (!allows_reg && DECL_P (operand))
1067 cxx_mark_addressable (operand);
1071 r = build_stmt (ASM_STMT, string,
1072 output_operands, input_operands,
1074 ASM_VOLATILE_P (r) = volatile_p;
1075 return add_stmt (r);
1078 /* Finish a label with the indicated NAME. */
1081 finish_label_stmt (tree name)
1083 tree decl = define_label (input_location, name);
1084 return add_stmt (build_stmt (LABEL_STMT, decl));
1087 /* Finish a series of declarations for local labels. G++ allows users
1088 to declare "local" labels, i.e., labels with scope. This extension
1089 is useful when writing code involving statement-expressions. */
1092 finish_label_decl (tree name)
1094 tree decl = declare_local_label (name);
1095 add_decl_stmt (decl);
1098 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1101 finish_decl_cleanup (tree decl, tree cleanup)
1103 push_cleanup (decl, cleanup, false);
1106 /* If the current scope exits with an exception, run CLEANUP. */
1109 finish_eh_cleanup (tree cleanup)
1111 push_cleanup (NULL, cleanup, true);
1114 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1115 order they were written by the user. Each node is as for
1116 emit_mem_initializers. */
1119 finish_mem_initializers (tree mem_inits)
1121 /* Reorder the MEM_INITS so that they are in the order they appeared
1122 in the source program. */
1123 mem_inits = nreverse (mem_inits);
1125 if (processing_template_decl)
1126 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1128 emit_mem_initializers (mem_inits);
1131 /* Finish a parenthesized expression EXPR. */
1134 finish_parenthesized_expr (tree expr)
1136 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1137 /* This inhibits warnings in c_common_truthvalue_conversion. */
1138 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1140 if (TREE_CODE (expr) == OFFSET_REF)
1141 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1142 enclosed in parentheses. */
1143 PTRMEM_OK_P (expr) = 0;
1147 /* Finish a reference to a non-static data member (DECL) that is not
1148 preceded by `.' or `->'. */
1151 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1153 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1157 if (current_function_decl
1158 && DECL_STATIC_FUNCTION_P (current_function_decl))
1159 cp_error_at ("invalid use of member `%D' in static member function",
1162 cp_error_at ("invalid use of non-static data member `%D'", decl);
1163 error ("from this location");
1165 return error_mark_node;
1167 TREE_USED (current_class_ptr) = 1;
1168 if (processing_template_decl && !qualifying_scope)
1170 tree type = TREE_TYPE (decl);
1172 if (TREE_CODE (type) == REFERENCE_TYPE)
1173 type = TREE_TYPE (type);
1176 /* Set the cv qualifiers. */
1177 int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1179 if (DECL_MUTABLE_P (decl))
1180 quals &= ~TYPE_QUAL_CONST;
1182 quals |= cp_type_quals (TREE_TYPE (decl));
1183 type = cp_build_qualified_type (type, quals);
1186 return build_min (COMPONENT_REF, type, object, decl);
1190 tree access_type = TREE_TYPE (object);
1191 tree lookup_context = context_for_name_lookup (decl);
1193 while (!DERIVED_FROM_P (lookup_context, access_type))
1195 access_type = TYPE_CONTEXT (access_type);
1196 while (access_type && DECL_P (access_type))
1197 access_type = DECL_CONTEXT (access_type);
1201 cp_error_at ("object missing in reference to `%D'", decl);
1202 error ("from this location");
1203 return error_mark_node;
1207 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1208 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1210 if (processing_template_decl)
1211 return build_min (SCOPE_REF, TREE_TYPE (decl),
1212 qualifying_scope, DECL_NAME (decl));
1214 perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1216 /* If the data member was named `C::M', convert `*this' to `C'
1218 if (qualifying_scope)
1220 tree binfo = NULL_TREE;
1221 object = build_scoped_ref (object, qualifying_scope,
1225 return build_class_member_access_expr (object, decl,
1226 /*access_path=*/NULL_TREE,
1227 /*preserve_reference=*/false);
1231 /* DECL was the declaration to which a qualified-id resolved. Issue
1232 an error message if it is not accessible. If OBJECT_TYPE is
1233 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1234 type of `*x', or `x', respectively. If the DECL was named as
1235 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1238 check_accessibility_of_qualified_id (tree decl,
1240 tree nested_name_specifier)
1243 tree qualifying_type = NULL_TREE;
1245 /* Determine the SCOPE of DECL. */
1246 scope = context_for_name_lookup (decl);
1247 /* If the SCOPE is not a type, then DECL is not a member. */
1248 if (!TYPE_P (scope))
1250 /* Compute the scope through which DECL is being accessed. */
1252 /* OBJECT_TYPE might not be a class type; consider:
1254 class A { typedef int I; };
1258 In this case, we will have "A::I" as the DECL, but "I" as the
1260 && CLASS_TYPE_P (object_type)
1261 && DERIVED_FROM_P (scope, object_type))
1262 /* If we are processing a `->' or `.' expression, use the type of the
1264 qualifying_type = object_type;
1265 else if (nested_name_specifier)
1267 /* If the reference is to a non-static member of the
1268 current class, treat it as if it were referenced through
1270 if (DECL_NONSTATIC_MEMBER_P (decl)
1271 && current_class_ptr
1272 && DERIVED_FROM_P (scope, current_class_type))
1273 qualifying_type = current_class_type;
1274 /* Otherwise, use the type indicated by the
1275 nested-name-specifier. */
1277 qualifying_type = nested_name_specifier;
1280 /* Otherwise, the name must be from the current class or one of
1282 qualifying_type = currently_open_derived_class (scope);
1284 if (qualifying_type)
1285 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1288 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1289 class named to the left of the "::" operator. DONE is true if this
1290 expression is a complete postfix-expression; it is false if this
1291 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1292 iff this expression is the operand of '&'. */
1295 finish_qualified_id_expr (tree qualifying_class, tree expr, bool done,
1298 if (error_operand_p (expr))
1299 return error_mark_node;
1301 /* If EXPR occurs as the operand of '&', use special handling that
1302 permits a pointer-to-member. */
1303 if (address_p && done)
1305 if (TREE_CODE (expr) == SCOPE_REF)
1306 expr = TREE_OPERAND (expr, 1);
1307 expr = build_offset_ref (qualifying_class, expr,
1308 /*address_p=*/true);
1312 if (TREE_CODE (expr) == FIELD_DECL)
1313 expr = finish_non_static_data_member (expr, current_class_ref,
1315 else if (BASELINK_P (expr) && !processing_template_decl)
1320 /* See if any of the functions are non-static members. */
1321 fns = BASELINK_FUNCTIONS (expr);
1322 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1323 fns = TREE_OPERAND (fns, 0);
1324 for (fn = fns; fn; fn = OVL_NEXT (fn))
1325 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1327 /* If so, the expression may be relative to the current
1329 if (fn && current_class_type
1330 && DERIVED_FROM_P (qualifying_class, current_class_type))
1331 expr = (build_class_member_access_expr
1332 (maybe_dummy_object (qualifying_class, NULL),
1334 BASELINK_ACCESS_BINFO (expr),
1335 /*preserve_reference=*/false));
1337 /* The expression is a qualified name whose address is not
1339 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1345 /* Begin a statement-expression. The value returned must be passed to
1346 finish_stmt_expr. */
1349 begin_stmt_expr (void)
1351 return push_stmt_list ();
1354 /* Process the final expression of a statement expression. EXPR can be
1355 NULL, if the final expression is empty. Build up a TARGET_EXPR so
1356 that the result value can be safely returned to the enclosing
1360 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1362 tree result = NULL_TREE;
1363 tree type = void_type_node;
1367 type = TREE_TYPE (expr);
1369 if (!processing_template_decl && !VOID_TYPE_P (TREE_TYPE (expr)))
1371 if (TREE_CODE (type) == ARRAY_TYPE
1372 || TREE_CODE (type) == FUNCTION_TYPE)
1373 expr = decay_conversion (expr);
1375 expr = convert_from_reference (expr);
1376 expr = require_complete_type (expr);
1378 /* Build a TARGET_EXPR for this aggregate. finish_stmt_expr
1379 will then pull it apart so the lifetime of the target is
1380 within the scope of the expression containing this statement
1382 if (TREE_CODE (expr) == TARGET_EXPR)
1384 else if (!IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_INIT_REF (type))
1385 expr = build_target_expr_with_type (expr, type);
1388 /* Copy construct. */
1389 expr = build_special_member_call
1390 (NULL_TREE, complete_ctor_identifier,
1391 build_tree_list (NULL_TREE, expr),
1392 TYPE_BINFO (type), LOOKUP_NORMAL);
1393 expr = build_cplus_new (type, expr);
1394 my_friendly_assert (TREE_CODE (expr) == TARGET_EXPR, 20030729);
1398 if (expr != error_mark_node)
1400 result = build_stmt (EXPR_STMT, expr);
1401 EXPR_STMT_STMT_EXPR_RESULT (result) = 1;
1408 /* Remember the last expression so that finish_stmt_expr
1409 can pull it apart. */
1410 TREE_TYPE (stmt_expr) = result;
1415 /* Finish a statement-expression. EXPR should be the value returned
1416 by the previous begin_stmt_expr. Returns an expression
1417 representing the statement-expression. */
1420 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1422 tree result, result_stmt, type;
1423 tree *result_stmt_p = NULL;
1425 result_stmt = TREE_TYPE (stmt_expr);
1426 TREE_TYPE (stmt_expr) = void_type_node;
1427 result = pop_stmt_list (stmt_expr);
1429 if (!result_stmt || VOID_TYPE_P (result_stmt))
1430 type = void_type_node;
1433 /* We need to search the statement expression for the result_stmt,
1434 since we'll need to replace it entirely. */
1436 result_stmt_p = &result;
1440 if (t == result_stmt)
1443 switch (TREE_CODE (t))
1445 case STATEMENT_LIST:
1447 tree_stmt_iterator i = tsi_last (t);
1448 result_stmt_p = tsi_stmt_ptr (i);
1452 result_stmt_p = &BIND_EXPR_BODY (t);
1455 result_stmt_p = &COMPOUND_BODY (t);
1457 case TRY_FINALLY_EXPR:
1458 case TRY_CATCH_EXPR:
1460 result_stmt_p = &TREE_OPERAND (t, 0);
1466 type = TREE_TYPE (EXPR_STMT_EXPR (result_stmt));
1469 if (processing_template_decl)
1471 result = build_min (STMT_EXPR, type, result);
1472 TREE_SIDE_EFFECTS (result) = 1;
1473 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1475 else if (!VOID_TYPE_P (type))
1477 /* Pull out the TARGET_EXPR that is the final expression. Put
1478 the target's init_expr as the final expression and then put
1479 the statement expression itself as the target's init
1480 expr. Finally, return the target expression. */
1481 tree last_expr = EXPR_STMT_EXPR (result_stmt);
1483 my_friendly_assert (TREE_CODE (last_expr) == TARGET_EXPR, 20030729);
1484 *result_stmt_p = TREE_OPERAND (last_expr, 1);
1486 if (TREE_CODE (result) == BIND_EXPR)
1488 if (VOID_TYPE_P (TREE_TYPE (result)))
1489 TREE_TYPE (result) = TREE_TYPE (last_expr);
1490 else if (same_type_p (TREE_TYPE (result), TREE_TYPE (last_expr)))
1495 else if (TREE_CODE (result) == STATEMENT_LIST)
1496 result = build (BIND_EXPR, TREE_TYPE (last_expr), NULL, result, NULL);
1498 TREE_OPERAND (last_expr, 1) = result;
1505 /* Perform Koenig lookup. FN is the postfix-expression representing
1506 the function (or functions) to call; ARGS are the arguments to the
1507 call. Returns the functions to be considered by overload
1511 perform_koenig_lookup (tree fn, tree args)
1513 tree identifier = NULL_TREE;
1514 tree functions = NULL_TREE;
1516 /* Find the name of the overloaded function. */
1517 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1519 else if (is_overloaded_fn (fn))
1522 identifier = DECL_NAME (get_first_fn (functions));
1524 else if (DECL_P (fn))
1527 identifier = DECL_NAME (fn);
1530 /* A call to a namespace-scope function using an unqualified name.
1532 Do Koenig lookup -- unless any of the arguments are
1534 if (!any_type_dependent_arguments_p (args))
1536 fn = lookup_arg_dependent (identifier, functions, args);
1538 /* The unqualified name could not be resolved. */
1539 fn = unqualified_fn_lookup_error (identifier);
1547 /* Generate an expression for `FN (ARGS)'.
1549 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1550 as a virtual call, even if FN is virtual. (This flag is set when
1551 encountering an expression where the function name is explicitly
1552 qualified. For example a call to `X::f' never generates a virtual
1555 Returns code for the call. */
1558 finish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p)
1564 if (fn == error_mark_node || args == error_mark_node)
1565 return error_mark_node;
1567 /* ARGS should be a list of arguments. */
1568 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1574 if (processing_template_decl)
1576 if (type_dependent_expression_p (fn)
1577 || any_type_dependent_arguments_p (args))
1579 result = build_nt (CALL_EXPR, fn, args, NULL_TREE);
1580 KOENIG_LOOKUP_P (result) = koenig_p;
1583 if (!BASELINK_P (fn)
1584 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1585 && TREE_TYPE (fn) != unknown_type_node)
1586 fn = build_non_dependent_expr (fn);
1587 args = build_non_dependent_args (orig_args);
1590 /* A reference to a member function will appear as an overloaded
1591 function (rather than a BASELINK) if an unqualified name was used
1593 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1597 if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
1598 f = TREE_OPERAND (f, 0);
1599 f = get_first_fn (f);
1600 if (DECL_FUNCTION_MEMBER_P (f))
1602 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1604 type = DECL_CONTEXT (f);
1605 fn = build_baselink (TYPE_BINFO (type),
1607 fn, /*optype=*/NULL_TREE);
1612 if (BASELINK_P (fn))
1616 /* A call to a member function. From [over.call.func]:
1618 If the keyword this is in scope and refers to the class of
1619 that member function, or a derived class thereof, then the
1620 function call is transformed into a qualified function call
1621 using (*this) as the postfix-expression to the left of the
1622 . operator.... [Otherwise] a contrived object of type T
1623 becomes the implied object argument.
1625 This paragraph is unclear about this situation:
1627 struct A { void f(); };
1628 struct B : public A {};
1629 struct C : public A { void g() { B::f(); }};
1631 In particular, for `B::f', this paragraph does not make clear
1632 whether "the class of that member function" refers to `A' or
1633 to `B'. We believe it refers to `B'. */
1634 if (current_class_type
1635 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1637 && current_class_ref)
1638 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1642 tree representative_fn;
1644 representative_fn = BASELINK_FUNCTIONS (fn);
1645 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1646 representative_fn = TREE_OPERAND (representative_fn, 0);
1647 representative_fn = get_first_fn (representative_fn);
1648 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1651 if (processing_template_decl)
1653 if (type_dependent_expression_p (object))
1654 return build_nt (CALL_EXPR, orig_fn, orig_args, NULL_TREE);
1655 object = build_non_dependent_expr (object);
1658 result = build_new_method_call (object, fn, args, NULL_TREE,
1660 ? LOOKUP_NONVIRTUAL : 0));
1662 else if (is_overloaded_fn (fn))
1663 /* A call to a namespace-scope function. */
1664 result = build_new_function_call (fn, args);
1665 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1668 error ("arguments to destructor are not allowed");
1669 /* Mark the pseudo-destructor call as having side-effects so
1670 that we do not issue warnings about its use. */
1671 result = build1 (NOP_EXPR,
1673 TREE_OPERAND (fn, 0));
1674 TREE_SIDE_EFFECTS (result) = 1;
1676 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1677 /* If the "function" is really an object of class type, it might
1678 have an overloaded `operator ()'. */
1679 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE,
1680 /*overloaded_p=*/NULL);
1682 /* A call where the function is unknown. */
1683 result = build_function_call (fn, args);
1685 if (processing_template_decl)
1687 result = build (CALL_EXPR, TREE_TYPE (result), orig_fn,
1688 orig_args, NULL_TREE);
1689 KOENIG_LOOKUP_P (result) = koenig_p;
1694 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1695 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1696 POSTDECREMENT_EXPR.) */
1699 finish_increment_expr (tree expr, enum tree_code code)
1701 return build_x_unary_op (code, expr);
1704 /* Finish a use of `this'. Returns an expression for `this'. */
1707 finish_this_expr (void)
1711 if (current_class_ptr)
1713 result = current_class_ptr;
1715 else if (current_function_decl
1716 && DECL_STATIC_FUNCTION_P (current_function_decl))
1718 error ("`this' is unavailable for static member functions");
1719 result = error_mark_node;
1723 if (current_function_decl)
1724 error ("invalid use of `this' in non-member function");
1726 error ("invalid use of `this' at top level");
1727 result = error_mark_node;
1733 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1734 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1735 the TYPE for the type given. If SCOPE is non-NULL, the expression
1736 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1739 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1741 if (destructor == error_mark_node)
1742 return error_mark_node;
1744 my_friendly_assert (TYPE_P (destructor), 20010905);
1746 if (!processing_template_decl)
1748 if (scope == error_mark_node)
1750 error ("invalid qualifying scope in pseudo-destructor name");
1751 return error_mark_node;
1754 /* [expr.pseudo] says both:
1756 The type designated by the pseudo-destructor-name shall be
1757 the same as the object type.
1761 The cv-unqualified versions of the object type and of the
1762 type designated by the pseudo-destructor-name shall be the
1765 We implement the more generous second sentence, since that is
1766 what most other compilers do. */
1767 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
1770 error ("`%E' is not of type `%T'", object, destructor);
1771 return error_mark_node;
1775 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1778 /* Finish an expression of the form CODE EXPR. */
1781 finish_unary_op_expr (enum tree_code code, tree expr)
1783 tree result = build_x_unary_op (code, expr);
1784 /* Inside a template, build_x_unary_op does not fold the
1785 expression. So check whether the result is folded before
1786 setting TREE_NEGATED_INT. */
1787 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1788 && TREE_CODE (result) == INTEGER_CST
1789 && !TYPE_UNSIGNED (TREE_TYPE (result))
1790 && INT_CST_LT (result, integer_zero_node))
1791 TREE_NEGATED_INT (result) = 1;
1792 overflow_warning (result);
1796 /* Finish a compound-literal expression. TYPE is the type to which
1797 the INITIALIZER_LIST is being cast. */
1800 finish_compound_literal (tree type, tree initializer_list)
1802 tree compound_literal;
1804 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1805 compound_literal = build_constructor (NULL_TREE, initializer_list);
1806 /* Mark it as a compound-literal. */
1807 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1808 if (processing_template_decl)
1809 TREE_TYPE (compound_literal) = type;
1812 /* Check the initialization. */
1813 compound_literal = digest_init (type, compound_literal, NULL);
1814 /* If the TYPE was an array type with an unknown bound, then we can
1815 figure out the dimension now. For example, something like:
1819 implies that the array has two elements. */
1820 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1821 complete_array_type (type, compound_literal, 1);
1824 return compound_literal;
1827 /* Return the declaration for the function-name variable indicated by
1831 finish_fname (tree id)
1835 decl = fname_decl (C_RID_CODE (id), id);
1836 if (processing_template_decl)
1837 decl = DECL_NAME (decl);
1841 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1842 and DECLARATOR. Returns nonzero if the function-declaration is
1846 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1848 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1851 /* The things we're about to see are not directly qualified by any
1852 template headers we've seen thus far. */
1853 reset_specialization ();
1858 /* Finish a translation unit. */
1861 finish_translation_unit (void)
1863 /* In case there were missing closebraces,
1864 get us back to the global binding level. */
1866 while (current_namespace != global_namespace)
1869 /* Do file scope __FUNCTION__ et al. */
1870 finish_fname_decls ();
1873 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1874 Returns the parameter. */
1877 finish_template_type_parm (tree aggr, tree identifier)
1879 if (aggr != class_type_node)
1881 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1882 aggr = class_type_node;
1885 return build_tree_list (aggr, identifier);
1888 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1889 Returns the parameter. */
1892 finish_template_template_parm (tree aggr, tree identifier)
1894 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1895 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1896 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1897 DECL_TEMPLATE_RESULT (tmpl) = decl;
1898 DECL_ARTIFICIAL (decl) = 1;
1899 end_template_decl ();
1901 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1903 return finish_template_type_parm (aggr, tmpl);
1906 /* ARGUMENT is the default-argument value for a template template
1907 parameter. If ARGUMENT is invalid, issue error messages and return
1908 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1911 check_template_template_default_arg (tree argument)
1913 if (TREE_CODE (argument) != TEMPLATE_DECL
1914 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1915 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1917 if (TREE_CODE (argument) == TYPE_DECL)
1919 tree t = TREE_TYPE (argument);
1921 /* Try to emit a slightly smarter error message if we detect
1922 that the user is using a template instantiation. */
1923 if (CLASSTYPE_TEMPLATE_INFO (t)
1924 && CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1925 error ("invalid use of type `%T' as a default value for a "
1926 "template template-parameter", t);
1928 error ("invalid use of `%D' as a default value for a template "
1929 "template-parameter", argument);
1932 error ("invalid default argument for a template template parameter");
1933 return error_mark_node;
1939 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1940 nonzero, the parameter list was terminated by a `...'. */
1943 finish_parmlist (tree parms, int ellipsis)
1947 /* We mark the PARMS as a parmlist so that declarator processing can
1948 disambiguate certain constructs. */
1949 TREE_PARMLIST (parms) = 1;
1950 /* We do not append void_list_node here, but leave it to grokparms
1952 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1957 /* Begin a class definition, as indicated by T. */
1960 begin_class_definition (tree t)
1962 if (t == error_mark_node)
1963 return error_mark_node;
1965 if (processing_template_parmlist)
1967 error ("definition of `%#T' inside template parameter list", t);
1968 return error_mark_node;
1970 /* A non-implicit typename comes from code like:
1972 template <typename T> struct A {
1973 template <typename U> struct A<T>::B ...
1975 This is erroneous. */
1976 else if (TREE_CODE (t) == TYPENAME_TYPE)
1978 error ("invalid definition of qualified type `%T'", t);
1979 t = error_mark_node;
1982 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1984 t = make_aggr_type (RECORD_TYPE);
1985 pushtag (make_anon_name (), t, 0);
1988 /* If this type was already complete, and we see another definition,
1990 if (COMPLETE_TYPE_P (t))
1992 error ("redefinition of `%#T'", t);
1993 cp_error_at ("previous definition of `%#T'", t);
1994 return error_mark_node;
1997 /* Update the location of the decl. */
1998 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
2000 if (TYPE_BEING_DEFINED (t))
2002 t = make_aggr_type (TREE_CODE (t));
2003 pushtag (TYPE_IDENTIFIER (t), t, 0);
2005 maybe_process_partial_specialization (t);
2007 TYPE_BEING_DEFINED (t) = 1;
2008 if (flag_pack_struct)
2011 TYPE_PACKED (t) = 1;
2012 /* Even though the type is being defined for the first time
2013 here, there might have been a forward declaration, so there
2014 might be cv-qualified variants of T. */
2015 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2016 TYPE_PACKED (v) = 1;
2018 /* Reset the interface data, at the earliest possible
2019 moment, as it might have been set via a class foo;
2021 if (! TYPE_ANONYMOUS_P (t))
2023 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2024 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2025 (t, interface_unknown);
2027 reset_specialization();
2029 /* Make a declaration for this class in its own scope. */
2030 build_self_reference ();
2035 /* Finish the member declaration given by DECL. */
2038 finish_member_declaration (tree decl)
2040 if (decl == error_mark_node || decl == NULL_TREE)
2043 if (decl == void_type_node)
2044 /* The COMPONENT was a friend, not a member, and so there's
2045 nothing for us to do. */
2048 /* We should see only one DECL at a time. */
2049 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
2051 /* Set up access control for DECL. */
2053 = (current_access_specifier == access_private_node);
2054 TREE_PROTECTED (decl)
2055 = (current_access_specifier == access_protected_node);
2056 if (TREE_CODE (decl) == TEMPLATE_DECL)
2058 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2059 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2062 /* Mark the DECL as a member of the current class. */
2063 DECL_CONTEXT (decl) = current_class_type;
2067 A C language linkage is ignored for the names of class members
2068 and the member function type of class member functions. */
2069 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2070 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2072 /* Put functions on the TYPE_METHODS list and everything else on the
2073 TYPE_FIELDS list. Note that these are built up in reverse order.
2074 We reverse them (to obtain declaration order) in finish_struct. */
2075 if (TREE_CODE (decl) == FUNCTION_DECL
2076 || DECL_FUNCTION_TEMPLATE_P (decl))
2078 /* We also need to add this function to the
2079 CLASSTYPE_METHOD_VEC. */
2080 add_method (current_class_type, decl, /*error_p=*/0);
2082 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2083 TYPE_METHODS (current_class_type) = decl;
2085 maybe_add_class_template_decl_list (current_class_type, decl,
2088 /* Enter the DECL into the scope of the class. */
2089 else if ((TREE_CODE (decl) == USING_DECL && TREE_TYPE (decl))
2090 || pushdecl_class_level (decl))
2092 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2093 go at the beginning. The reason is that lookup_field_1
2094 searches the list in order, and we want a field name to
2095 override a type name so that the "struct stat hack" will
2096 work. In particular:
2098 struct S { enum E { }; int E } s;
2101 is valid. In addition, the FIELD_DECLs must be maintained in
2102 declaration order so that class layout works as expected.
2103 However, we don't need that order until class layout, so we
2104 save a little time by putting FIELD_DECLs on in reverse order
2105 here, and then reversing them in finish_struct_1. (We could
2106 also keep a pointer to the correct insertion points in the
2109 if (TREE_CODE (decl) == TYPE_DECL)
2110 TYPE_FIELDS (current_class_type)
2111 = chainon (TYPE_FIELDS (current_class_type), decl);
2114 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2115 TYPE_FIELDS (current_class_type) = decl;
2118 maybe_add_class_template_decl_list (current_class_type, decl,
2123 /* Finish processing the declaration of a member class template
2124 TYPES whose template parameters are given by PARMS. */
2127 finish_member_class_template (tree types)
2131 /* If there are declared, but undefined, partial specializations
2132 mixed in with the typespecs they will not yet have passed through
2133 maybe_process_partial_specialization, so we do that here. */
2134 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2135 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2136 maybe_process_partial_specialization (TREE_VALUE (t));
2138 grok_x_components (types);
2139 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2140 /* The component was in fact a friend declaration. We avoid
2141 finish_member_template_decl performing certain checks by
2145 finish_member_template_decl (types);
2147 /* As with other component type declarations, we do
2148 not store the new DECL on the list of
2153 /* Finish processing a complete template declaration. The PARMS are
2154 the template parameters. */
2157 finish_template_decl (tree parms)
2160 end_template_decl ();
2162 end_specialization ();
2165 /* Finish processing a template-id (which names a type) of the form
2166 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2167 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2168 the scope of template-id indicated. */
2171 finish_template_type (tree name, tree args, int entering_scope)
2175 decl = lookup_template_class (name, args,
2176 NULL_TREE, NULL_TREE, entering_scope,
2177 tf_error | tf_warning | tf_user);
2178 if (decl != error_mark_node)
2179 decl = TYPE_STUB_DECL (decl);
2184 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2185 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2186 BASE_CLASS, or NULL_TREE if an error occurred. The
2187 ACCESS_SPECIFIER is one of
2188 access_{default,public,protected_private}[_virtual]_node.*/
2191 finish_base_specifier (tree base, tree access, bool virtual_p)
2195 if (base == error_mark_node)
2197 error ("invalid base-class specification");
2200 else if (! is_aggr_type (base, 1))
2204 if (cp_type_quals (base) != 0)
2206 error ("base class `%T' has cv qualifiers", base);
2207 base = TYPE_MAIN_VARIANT (base);
2209 result = build_tree_list (access, base);
2210 TREE_VIA_VIRTUAL (result) = virtual_p;
2216 /* Called when multiple declarators are processed. If that is not
2217 permitted in this context, an error is issued. */
2220 check_multiple_declarators (void)
2224 In a template-declaration, explicit specialization, or explicit
2225 instantiation the init-declarator-list in the declaration shall
2226 contain at most one declarator.
2228 We don't just use PROCESSING_TEMPLATE_DECL for the first
2229 condition since that would disallow the perfectly valid code,
2230 like `template <class T> struct S { int i, j; };'. */
2231 if (at_function_scope_p ())
2232 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2235 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2236 || processing_explicit_instantiation
2237 || processing_specialization)
2238 error ("multiple declarators in template declaration");
2241 /* Issue a diagnostic that NAME cannot be found in SCOPE. */
2244 qualified_name_lookup_error (tree scope, tree name)
2248 if (!COMPLETE_TYPE_P (scope))
2249 error ("incomplete type `%T' used in nested name specifier", scope);
2251 error ("`%D' is not a member of `%T'", name, scope);
2253 else if (scope != global_namespace)
2254 error ("`%D' is not a member of `%D'", name, scope);
2256 error ("`::%D' has not been declared", name);
2259 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2260 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2261 if non-NULL, is the type or namespace used to explicitly qualify
2262 ID_EXPRESSION. DECL is the entity to which that name has been
2265 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2266 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2267 be set to true if this expression isn't permitted in a
2268 constant-expression, but it is otherwise not set by this function.
2269 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2270 constant-expression, but a non-constant expression is also
2273 If an error occurs, and it is the kind of error that might cause
2274 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2275 is the caller's responsibility to issue the message. *ERROR_MSG
2276 will be a string with static storage duration, so the caller need
2279 Return an expression for the entity, after issuing appropriate
2280 diagnostics. This function is also responsible for transforming a
2281 reference to a non-static member into a COMPONENT_REF that makes
2282 the use of "this" explicit.
2284 Upon return, *IDK will be filled in appropriately. */
2287 finish_id_expression (tree id_expression,
2291 tree *qualifying_class,
2292 bool integral_constant_expression_p,
2293 bool allow_non_integral_constant_expression_p,
2294 bool *non_integral_constant_expression_p,
2295 const char **error_msg)
2297 /* Initialize the output parameters. */
2298 *idk = CP_ID_KIND_NONE;
2301 if (id_expression == error_mark_node)
2302 return error_mark_node;
2303 /* If we have a template-id, then no further lookup is
2304 required. If the template-id was for a template-class, we
2305 will sometimes have a TYPE_DECL at this point. */
2306 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2307 || TREE_CODE (decl) == TYPE_DECL)
2309 /* Look up the name. */
2312 if (decl == error_mark_node)
2314 /* Name lookup failed. */
2317 || (!dependent_type_p (scope)
2318 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2319 && IDENTIFIER_TYPENAME_P (id_expression)
2320 && dependent_type_p (TREE_TYPE (id_expression))))))
2322 /* If the qualifying type is non-dependent (and the name
2323 does not name a conversion operator to a dependent
2324 type), issue an error. */
2325 qualified_name_lookup_error (scope, id_expression);
2326 return error_mark_node;
2330 /* It may be resolved via Koenig lookup. */
2331 *idk = CP_ID_KIND_UNQUALIFIED;
2332 return id_expression;
2335 decl = id_expression;
2337 /* If DECL is a variable that would be out of scope under
2338 ANSI/ISO rules, but in scope in the ARM, name lookup
2339 will succeed. Issue a diagnostic here. */
2341 decl = check_for_out_of_scope_variable (decl);
2343 /* Remember that the name was used in the definition of
2344 the current class so that we can check later to see if
2345 the meaning would have been different after the class
2346 was entirely defined. */
2347 if (!scope && decl != error_mark_node)
2348 maybe_note_name_used_in_class (id_expression, decl);
2351 /* If we didn't find anything, or what we found was a type,
2352 then this wasn't really an id-expression. */
2353 if (TREE_CODE (decl) == TEMPLATE_DECL
2354 && !DECL_FUNCTION_TEMPLATE_P (decl))
2356 *error_msg = "missing template arguments";
2357 return error_mark_node;
2359 else if (TREE_CODE (decl) == TYPE_DECL
2360 || TREE_CODE (decl) == NAMESPACE_DECL)
2362 *error_msg = "expected primary-expression";
2363 return error_mark_node;
2366 /* If the name resolved to a template parameter, there is no
2367 need to look it up again later. */
2368 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
2369 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2371 *idk = CP_ID_KIND_NONE;
2372 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2373 decl = TEMPLATE_PARM_DECL (decl);
2374 if (integral_constant_expression_p
2375 && !dependent_type_p (TREE_TYPE (decl))
2376 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2378 if (!allow_non_integral_constant_expression_p)
2379 error ("template parameter `%D' of type `%T' is not allowed in "
2380 "an integral constant expression because it is not of "
2381 "integral or enumeration type", decl, TREE_TYPE (decl));
2382 *non_integral_constant_expression_p = true;
2384 return DECL_INITIAL (decl);
2386 /* Similarly, we resolve enumeration constants to their
2387 underlying values. */
2388 else if (TREE_CODE (decl) == CONST_DECL)
2390 *idk = CP_ID_KIND_NONE;
2391 if (!processing_template_decl)
2392 return DECL_INITIAL (decl);
2399 /* If the declaration was explicitly qualified indicate
2400 that. The semantics of `A::f(3)' are different than
2401 `f(3)' if `f' is virtual. */
2403 ? CP_ID_KIND_QUALIFIED
2404 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2405 ? CP_ID_KIND_TEMPLATE_ID
2406 : CP_ID_KIND_UNQUALIFIED));
2411 An id-expression is type-dependent if it contains an
2412 identifier that was declared with a dependent type.
2414 The standard is not very specific about an id-expression that
2415 names a set of overloaded functions. What if some of them
2416 have dependent types and some of them do not? Presumably,
2417 such a name should be treated as a dependent name. */
2418 /* Assume the name is not dependent. */
2419 dependent_p = false;
2420 if (!processing_template_decl)
2421 /* No names are dependent outside a template. */
2423 /* A template-id where the name of the template was not resolved
2424 is definitely dependent. */
2425 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2426 && (TREE_CODE (TREE_OPERAND (decl, 0))
2427 == IDENTIFIER_NODE))
2429 /* For anything except an overloaded function, just check its
2431 else if (!is_overloaded_fn (decl))
2433 = dependent_type_p (TREE_TYPE (decl));
2434 /* For a set of overloaded functions, check each of the
2440 if (BASELINK_P (fns))
2441 fns = BASELINK_FUNCTIONS (fns);
2443 /* For a template-id, check to see if the template
2444 arguments are dependent. */
2445 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2447 tree args = TREE_OPERAND (fns, 1);
2448 dependent_p = any_dependent_template_arguments_p (args);
2449 /* The functions are those referred to by the
2451 fns = TREE_OPERAND (fns, 0);
2454 /* If there are no dependent template arguments, go through
2455 the overloaded functions. */
2456 while (fns && !dependent_p)
2458 tree fn = OVL_CURRENT (fns);
2460 /* Member functions of dependent classes are
2462 if (TREE_CODE (fn) == FUNCTION_DECL
2463 && type_dependent_expression_p (fn))
2465 else if (TREE_CODE (fn) == TEMPLATE_DECL
2466 && dependent_template_p (fn))
2469 fns = OVL_NEXT (fns);
2473 /* If the name was dependent on a template parameter, we will
2474 resolve the name at instantiation time. */
2477 /* Create a SCOPE_REF for qualified names, if the scope is
2482 *qualifying_class = scope;
2483 /* Since this name was dependent, the expression isn't
2484 constant -- yet. No error is issued because it might
2485 be constant when things are instantiated. */
2486 if (integral_constant_expression_p)
2487 *non_integral_constant_expression_p = true;
2488 if (TYPE_P (scope) && dependent_type_p (scope))
2489 return build_nt (SCOPE_REF, scope, id_expression);
2490 else if (TYPE_P (scope) && DECL_P (decl))
2491 return build (SCOPE_REF, TREE_TYPE (decl), scope,
2496 /* A TEMPLATE_ID already contains all the information we
2498 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2499 return id_expression;
2500 /* Since this name was dependent, the expression isn't
2501 constant -- yet. No error is issued because it might be
2502 constant when things are instantiated. */
2503 if (integral_constant_expression_p)
2504 *non_integral_constant_expression_p = true;
2505 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2506 /* If we found a variable, then name lookup during the
2507 instantiation will always resolve to the same VAR_DECL
2508 (or an instantiation thereof). */
2509 if (TREE_CODE (decl) == VAR_DECL
2510 || TREE_CODE (decl) == PARM_DECL)
2512 return id_expression;
2515 /* Only certain kinds of names are allowed in constant
2516 expression. Enumerators and template parameters
2517 have already been handled above. */
2518 if (integral_constant_expression_p)
2520 /* Const variables or static data members of integral or
2521 enumeration types initialized with constant expressions
2523 if (TREE_CODE (decl) == VAR_DECL
2524 && CP_TYPE_CONST_P (TREE_TYPE (decl))
2525 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2526 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2530 if (!allow_non_integral_constant_expression_p)
2532 error ("`%D' cannot appear in a constant-expression", decl);
2533 return error_mark_node;
2535 *non_integral_constant_expression_p = true;
2539 if (TREE_CODE (decl) == NAMESPACE_DECL)
2541 error ("use of namespace `%D' as expression", decl);
2542 return error_mark_node;
2544 else if (DECL_CLASS_TEMPLATE_P (decl))
2546 error ("use of class template `%T' as expression", decl);
2547 return error_mark_node;
2549 else if (TREE_CODE (decl) == TREE_LIST)
2551 /* Ambiguous reference to base members. */
2552 error ("request for member `%D' is ambiguous in "
2553 "multiple inheritance lattice", id_expression);
2554 print_candidates (decl);
2555 return error_mark_node;
2558 /* Mark variable-like entities as used. Functions are similarly
2559 marked either below or after overload resolution. */
2560 if (TREE_CODE (decl) == VAR_DECL
2561 || TREE_CODE (decl) == PARM_DECL
2562 || TREE_CODE (decl) == RESULT_DECL)
2567 decl = (adjust_result_of_qualified_name_lookup
2568 (decl, scope, current_class_type));
2570 if (TREE_CODE (decl) == FUNCTION_DECL)
2573 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2574 *qualifying_class = scope;
2575 else if (!processing_template_decl)
2576 decl = convert_from_reference (decl);
2577 else if (TYPE_P (scope))
2578 decl = build (SCOPE_REF, TREE_TYPE (decl), scope, decl);
2580 else if (TREE_CODE (decl) == FIELD_DECL)
2581 decl = finish_non_static_data_member (decl, current_class_ref,
2582 /*qualifying_scope=*/NULL_TREE);
2583 else if (is_overloaded_fn (decl))
2585 tree first_fn = OVL_CURRENT (decl);
2587 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2588 first_fn = DECL_TEMPLATE_RESULT (first_fn);
2590 if (!really_overloaded_fn (decl))
2591 mark_used (first_fn);
2593 if (TREE_CODE (first_fn) == FUNCTION_DECL
2594 && DECL_FUNCTION_MEMBER_P (first_fn))
2596 /* A set of member functions. */
2597 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2598 return finish_class_member_access_expr (decl, id_expression);
2603 if (TREE_CODE (decl) == VAR_DECL
2604 || TREE_CODE (decl) == PARM_DECL
2605 || TREE_CODE (decl) == RESULT_DECL)
2607 tree context = decl_function_context (decl);
2609 if (context != NULL_TREE && context != current_function_decl
2610 && ! TREE_STATIC (decl))
2612 error ("use of %s from containing function",
2613 (TREE_CODE (decl) == VAR_DECL
2614 ? "`auto' variable" : "parameter"));
2615 cp_error_at (" `%#D' declared here", decl);
2616 return error_mark_node;
2620 if (DECL_P (decl) && DECL_NONLOCAL (decl)
2621 && DECL_CLASS_SCOPE_P (decl)
2622 && DECL_CONTEXT (decl) != current_class_type)
2626 path = currently_open_derived_class (DECL_CONTEXT (decl));
2627 perform_or_defer_access_check (TYPE_BINFO (path), decl);
2630 if (! processing_template_decl)
2631 decl = convert_from_reference (decl);
2634 /* Resolve references to variables of anonymous unions
2635 into COMPONENT_REFs. */
2636 if (TREE_CODE (decl) == ALIAS_DECL)
2637 decl = unshare_expr (DECL_INITIAL (decl));
2640 if (TREE_DEPRECATED (decl))
2641 warn_deprecated_use (decl);
2646 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2647 use as a type-specifier. */
2650 finish_typeof (tree expr)
2654 if (type_dependent_expression_p (expr))
2656 type = make_aggr_type (TYPEOF_TYPE);
2657 TYPEOF_TYPE_EXPR (type) = expr;
2662 type = TREE_TYPE (expr);
2664 if (!type || type == unknown_type_node)
2666 error ("type of `%E' is unknown", expr);
2667 return error_mark_node;
2673 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2674 with equivalent CALL_EXPRs. */
2677 simplify_aggr_init_exprs_r (tree* tp,
2679 void* data ATTRIBUTE_UNUSED)
2681 /* We don't need to walk into types; there's nothing in a type that
2682 needs simplification. (And, furthermore, there are places we
2683 actively don't want to go. For example, we don't want to wander
2684 into the default arguments for a FUNCTION_DECL that appears in a
2691 /* Only AGGR_INIT_EXPRs are interesting. */
2692 else if (TREE_CODE (*tp) != AGGR_INIT_EXPR)
2695 simplify_aggr_init_expr (tp);
2697 /* Keep iterating. */
2701 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
2702 function is broken out from the above for the benefit of the tree-ssa
2706 simplify_aggr_init_expr (tree *tp)
2708 tree aggr_init_expr = *tp;
2710 /* Form an appropriate CALL_EXPR. */
2711 tree fn = TREE_OPERAND (aggr_init_expr, 0);
2712 tree args = TREE_OPERAND (aggr_init_expr, 1);
2713 tree slot = TREE_OPERAND (aggr_init_expr, 2);
2714 tree type = TREE_TYPE (aggr_init_expr);
2717 enum style_t { ctor, arg, pcc } style;
2719 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2721 #ifdef PCC_STATIC_STRUCT_RETURN
2725 else if (TREE_ADDRESSABLE (type))
2728 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2729 handling. See build_cplus_new. */
2732 if (style == ctor || style == arg)
2734 /* Pass the address of the slot. If this is a constructor, we
2735 replace the first argument; otherwise, we tack on a new one. */
2739 args = TREE_CHAIN (args);
2741 cxx_mark_addressable (slot);
2742 addr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (slot)), slot);
2745 /* The return type might have different cv-quals from the slot. */
2746 tree fntype = TREE_TYPE (TREE_TYPE (fn));
2747 #ifdef ENABLE_CHECKING
2748 if (TREE_CODE (fntype) != FUNCTION_TYPE
2749 && TREE_CODE (fntype) != METHOD_TYPE)
2752 addr = convert (build_pointer_type (TREE_TYPE (fntype)), addr);
2755 args = tree_cons (NULL_TREE, addr, args);
2758 call_expr = build (CALL_EXPR,
2759 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2760 fn, args, NULL_TREE);
2763 /* Tell the backend that we've added our return slot to the argument
2765 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2766 else if (style == pcc)
2768 /* If we're using the non-reentrant PCC calling convention, then we
2769 need to copy the returned value out of the static buffer into the
2771 push_deferring_access_checks (dk_no_check);
2772 call_expr = build_aggr_init (slot, call_expr,
2773 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2774 pop_deferring_access_checks ();
2777 /* We want to use the value of the initialized location as the
2779 call_expr = build (COMPOUND_EXPR, type, call_expr, slot);
2784 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2787 emit_associated_thunks (tree fn)
2789 /* When we use vcall offsets, we emit thunks with the virtual
2790 functions to which they thunk. The whole point of vcall offsets
2791 is so that you can know statically the entire set of thunks that
2792 will ever be needed for a given virtual function, thereby
2793 enabling you to output all the thunks with the function itself. */
2794 if (DECL_VIRTUAL_P (fn))
2798 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2800 if (!THUNK_ALIAS (thunk))
2802 use_thunk (thunk, /*emit_p=*/1);
2803 if (DECL_RESULT_THUNK_P (thunk))
2807 for (probe = DECL_THUNKS (thunk);
2808 probe; probe = TREE_CHAIN (probe))
2809 use_thunk (probe, /*emit_p=*/1);
2813 my_friendly_assert (!DECL_THUNKS (thunk), 20031023);
2818 /* Generate RTL for FN. */
2821 expand_body (tree fn)
2823 tree saved_function;
2825 /* Compute the appropriate object-file linkage for inline
2827 if (DECL_DECLARED_INLINE_P (fn))
2828 import_export_decl (fn);
2830 /* If FN is external, then there's no point in generating RTL for
2831 it. This situation can arise with an inline function under
2832 `-fexternal-templates'; we instantiate the function, even though
2833 we're not planning on emitting it, in case we get a chance to
2835 if (DECL_EXTERNAL (fn))
2838 /* ??? When is this needed? */
2839 saved_function = current_function_decl;
2841 /* Emit any thunks that should be emitted at the same time as FN. */
2842 emit_associated_thunks (fn);
2844 tree_rest_of_compilation (fn, function_depth > 1);
2846 current_function_decl = saved_function;
2848 extract_interface_info ();
2850 /* If this function is marked with the constructor attribute, add it
2851 to the list of functions to be called along with constructors
2852 from static duration objects. */
2853 if (DECL_STATIC_CONSTRUCTOR (fn))
2854 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2856 /* If this function is marked with the destructor attribute, add it
2857 to the list of functions to be called along with destructors from
2858 static duration objects. */
2859 if (DECL_STATIC_DESTRUCTOR (fn))
2860 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2862 if (DECL_CLONED_FUNCTION_P (fn))
2864 /* If this is a clone, go through the other clones now and mark
2865 their parameters used. We have to do that here, as we don't
2866 know whether any particular clone will be expanded, and
2867 therefore cannot pick one arbitrarily. */
2870 for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn));
2871 probe && DECL_CLONED_FUNCTION_P (probe);
2872 probe = TREE_CHAIN (probe))
2876 for (parms = DECL_ARGUMENTS (probe);
2877 parms; parms = TREE_CHAIN (parms))
2878 TREE_USED (parms) = 1;
2883 /* Generate RTL for FN. */
2886 expand_or_defer_fn (tree fn)
2888 /* When the parser calls us after finishing the body of a template
2889 function, we don't really want to expand the body. */
2890 if (processing_template_decl)
2892 /* Normally, collection only occurs in rest_of_compilation. So,
2893 if we don't collect here, we never collect junk generated
2894 during the processing of templates until we hit a
2895 non-template function. */
2900 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2901 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2902 simplify_aggr_init_exprs_r,
2905 /* If this is a constructor or destructor body, we have to clone
2907 if (maybe_clone_body (fn))
2909 /* We don't want to process FN again, so pretend we've written
2910 it out, even though we haven't. */
2911 TREE_ASM_WRITTEN (fn) = 1;
2915 /* There's no reason to do any of the work here if we're only doing
2916 semantic analysis; this code just generates RTL. */
2917 if (flag_syntax_only)
2920 /* Compute the appropriate object-file linkage for inline functions. */
2921 if (DECL_DECLARED_INLINE_P (fn))
2922 import_export_decl (fn);
2926 /* Expand or defer, at the whim of the compilation unit manager. */
2927 cgraph_finalize_function (fn, function_depth > 1);
2939 /* Helper function for walk_tree, used by finalize_nrv below. */
2942 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
2944 struct nrv_data *dp = (struct nrv_data *)data;
2947 /* No need to walk into types. There wouldn't be any need to walk into
2948 non-statements, except that we have to consider STMT_EXPRs. */
2951 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
2952 but differs from using NULL_TREE in that it indicates that we care
2953 about the value of the RESULT_DECL. */
2954 else if (TREE_CODE (*tp) == RETURN_STMT)
2955 RETURN_STMT_EXPR (*tp) = dp->result;
2956 /* Change all cleanups for the NRV to only run when an exception is
2958 else if (TREE_CODE (*tp) == CLEANUP_STMT
2959 && CLEANUP_DECL (*tp) == dp->var)
2960 CLEANUP_EH_ONLY (*tp) = 1;
2961 /* Replace the DECL_STMT for the NRV with an initialization of the
2962 RESULT_DECL, if needed. */
2963 else if (TREE_CODE (*tp) == DECL_STMT
2964 && DECL_STMT_DECL (*tp) == dp->var)
2967 if (DECL_INITIAL (dp->var)
2968 && DECL_INITIAL (dp->var) != error_mark_node)
2970 init = build (INIT_EXPR, void_type_node, dp->result,
2971 DECL_INITIAL (dp->var));
2972 DECL_INITIAL (dp->var) = error_mark_node;
2976 init = build_stmt (EXPR_STMT, init);
2977 SET_EXPR_LOCUS (init, EXPR_LOCUS (*tp));
2978 TREE_CHAIN (init) = TREE_CHAIN (*tp);
2981 /* And replace all uses of the NRV with the RESULT_DECL. */
2982 else if (*tp == dp->var)
2985 /* Avoid walking into the same tree more than once. Unfortunately, we
2986 can't just use walk_tree_without duplicates because it would only call
2987 us for the first occurrence of dp->var in the function body. */
2988 slot = htab_find_slot (dp->visited, *tp, INSERT);
2994 /* Keep iterating. */
2998 /* Called from finish_function to implement the named return value
2999 optimization by overriding all the RETURN_STMTs and pertinent
3000 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3001 RESULT_DECL for the function. */
3004 finalize_nrv (tree *tp, tree var, tree result)
3006 struct nrv_data data;
3008 /* Copy debugging information from VAR to RESULT. */
3009 DECL_NAME (result) = DECL_NAME (var);
3010 DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (var);
3011 DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (var);
3012 /* Don't forget that we take its address. */
3013 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3016 data.result = result;
3017 data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3018 walk_tree (tp, finalize_nrv_r, &data, 0);
3019 htab_delete (data.visited);
3022 /* Perform initialization related to this module. */
3025 init_cp_semantics (void)
3029 #include "gt-cp-semantics.h"