1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 2008, 2009, 2010 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 3, 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 COPYING3. If not see
25 <http://www.gnu.org/licenses/>. */
29 #include "coretypes.h"
33 #include "c-family/c-common.h"
34 #include "c-family/c-objc.h"
35 #include "tree-inline.h"
36 #include "tree-mudflap.h"
41 #include "diagnostic.h"
43 #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. */
54 static tree maybe_convert_cond (tree);
55 static tree finalize_nrv_r (tree *, int *, void *);
56 static tree capture_decltype (tree);
57 static tree thisify_lambda_field (tree);
60 /* Deferred Access Checking Overview
61 ---------------------------------
63 Most C++ expressions and declarations require access checking
64 to be performed during parsing. However, in several cases,
65 this has to be treated differently.
67 For member declarations, access checking has to be deferred
68 until more information about the declaration is known. For
80 When we are parsing the function return type `A::X', we don't
81 really know if this is allowed until we parse the function name.
83 Furthermore, some contexts require that access checking is
84 never performed at all. These include class heads, and template
87 Typical use of access checking functions is described here:
89 1. When we enter a context that requires certain access checking
90 mode, the function `push_deferring_access_checks' is called with
91 DEFERRING argument specifying the desired mode. Access checking
92 may be performed immediately (dk_no_deferred), deferred
93 (dk_deferred), or not performed (dk_no_check).
95 2. When a declaration such as a type, or a variable, is encountered,
96 the function `perform_or_defer_access_check' is called. It
97 maintains a VEC of all deferred checks.
99 3. The global `current_class_type' or `current_function_decl' is then
100 setup by the parser. `enforce_access' relies on these information
103 4. Upon exiting the context mentioned in step 1,
104 `perform_deferred_access_checks' is called to check all declaration
105 stored in the VEC. `pop_deferring_access_checks' is then
106 called to restore the previous access checking mode.
108 In case of parsing error, we simply call `pop_deferring_access_checks'
109 without `perform_deferred_access_checks'. */
111 typedef struct GTY(()) deferred_access {
112 /* A VEC representing name-lookups for which we have deferred
113 checking access controls. We cannot check the accessibility of
114 names used in a decl-specifier-seq until we know what is being
115 declared because code like:
122 A::B* A::f() { return 0; }
124 is valid, even though `A::B' is not generally accessible. */
125 VEC (deferred_access_check,gc)* GTY(()) deferred_access_checks;
127 /* The current mode of access checks. */
128 enum deferring_kind deferring_access_checks_kind;
131 DEF_VEC_O (deferred_access);
132 DEF_VEC_ALLOC_O (deferred_access,gc);
134 /* Data for deferred access checking. */
135 static GTY(()) VEC(deferred_access,gc) *deferred_access_stack;
136 static GTY(()) unsigned deferred_access_no_check;
138 /* Save the current deferred access states and start deferred
139 access checking iff DEFER_P is true. */
142 push_deferring_access_checks (deferring_kind deferring)
144 /* For context like template instantiation, access checking
145 disabling applies to all nested context. */
146 if (deferred_access_no_check || deferring == dk_no_check)
147 deferred_access_no_check++;
150 deferred_access *ptr;
152 ptr = VEC_safe_push (deferred_access, gc, deferred_access_stack, NULL);
153 ptr->deferred_access_checks = NULL;
154 ptr->deferring_access_checks_kind = deferring;
158 /* Resume deferring access checks again after we stopped doing
162 resume_deferring_access_checks (void)
164 if (!deferred_access_no_check)
165 VEC_last (deferred_access, deferred_access_stack)
166 ->deferring_access_checks_kind = dk_deferred;
169 /* Stop deferring access checks. */
172 stop_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 VEC_last (deferred_access, deferred_access_stack)
176 ->deferring_access_checks_kind = dk_no_deferred;
179 /* Discard the current deferred access checks and restore the
183 pop_deferring_access_checks (void)
185 if (deferred_access_no_check)
186 deferred_access_no_check--;
188 VEC_pop (deferred_access, deferred_access_stack);
191 /* Returns a TREE_LIST representing the deferred checks.
192 The TREE_PURPOSE of each node is the type through which the
193 access occurred; the TREE_VALUE is the declaration named.
196 VEC (deferred_access_check,gc)*
197 get_deferred_access_checks (void)
199 if (deferred_access_no_check)
202 return (VEC_last (deferred_access, deferred_access_stack)
203 ->deferred_access_checks);
206 /* Take current deferred checks and combine with the
207 previous states if we also defer checks previously.
208 Otherwise perform checks now. */
211 pop_to_parent_deferring_access_checks (void)
213 if (deferred_access_no_check)
214 deferred_access_no_check--;
217 VEC (deferred_access_check,gc) *checks;
218 deferred_access *ptr;
220 checks = (VEC_last (deferred_access, deferred_access_stack)
221 ->deferred_access_checks);
223 VEC_pop (deferred_access, deferred_access_stack);
224 ptr = VEC_last (deferred_access, deferred_access_stack);
225 if (ptr->deferring_access_checks_kind == dk_no_deferred)
228 perform_access_checks (checks);
232 /* Merge with parent. */
234 deferred_access_check *chk, *probe;
236 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
238 FOR_EACH_VEC_ELT (deferred_access_check,
239 ptr->deferred_access_checks, j, probe)
241 if (probe->binfo == chk->binfo &&
242 probe->decl == chk->decl &&
243 probe->diag_decl == chk->diag_decl)
246 /* Insert into parent's checks. */
247 VEC_safe_push (deferred_access_check, gc,
248 ptr->deferred_access_checks, chk);
255 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
256 is the BINFO indicating the qualifying scope used to access the
257 DECL node stored in the TREE_VALUE of the node. */
260 perform_access_checks (VEC (deferred_access_check,gc)* checks)
263 deferred_access_check *chk;
268 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
269 enforce_access (chk->binfo, chk->decl, chk->diag_decl);
272 /* Perform the deferred access checks.
274 After performing the checks, we still have to keep the list
275 `deferred_access_stack->deferred_access_checks' since we may want
276 to check access for them again later in a different context.
283 A::X A::a, x; // No error for `A::a', error for `x'
285 We have to perform deferred access of `A::X', first with `A::a',
289 perform_deferred_access_checks (void)
291 perform_access_checks (get_deferred_access_checks ());
294 /* Defer checking the accessibility of DECL, when looked up in
295 BINFO. DIAG_DECL is the declaration to use to print diagnostics. */
298 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl)
301 deferred_access *ptr;
302 deferred_access_check *chk;
303 deferred_access_check *new_access;
306 /* Exit if we are in a context that no access checking is performed.
308 if (deferred_access_no_check)
311 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
313 ptr = VEC_last (deferred_access, deferred_access_stack);
315 /* If we are not supposed to defer access checks, just check now. */
316 if (ptr->deferring_access_checks_kind == dk_no_deferred)
318 enforce_access (binfo, decl, diag_decl);
322 /* See if we are already going to perform this check. */
323 FOR_EACH_VEC_ELT (deferred_access_check,
324 ptr->deferred_access_checks, i, chk)
326 if (chk->decl == decl && chk->binfo == binfo &&
327 chk->diag_decl == diag_decl)
332 /* If not, record the check. */
334 VEC_safe_push (deferred_access_check, gc,
335 ptr->deferred_access_checks, 0);
336 new_access->binfo = binfo;
337 new_access->decl = decl;
338 new_access->diag_decl = diag_decl;
341 /* Used by build_over_call in LOOKUP_SPECULATIVE mode: return whether DECL
342 is accessible in BINFO, and possibly complain if not. If we're not
343 checking access, everything is accessible. */
346 speculative_access_check (tree binfo, tree decl, tree diag_decl,
349 if (deferred_access_no_check)
352 /* If we're checking for implicit delete, we don't want access
354 if (!accessible_p (binfo, decl, true))
356 /* Unless we're under maybe_explain_implicit_delete. */
358 enforce_access (binfo, decl, diag_decl);
365 /* Returns nonzero if the current statement is a full expression,
366 i.e. temporaries created during that statement should be destroyed
367 at the end of the statement. */
370 stmts_are_full_exprs_p (void)
372 return current_stmt_tree ()->stmts_are_full_exprs_p;
375 /* T is a statement. Add it to the statement-tree. This is the C++
376 version. The C/ObjC frontends have a slightly different version of
382 enum tree_code code = TREE_CODE (t);
384 if (EXPR_P (t) && code != LABEL_EXPR)
386 if (!EXPR_HAS_LOCATION (t))
387 SET_EXPR_LOCATION (t, input_location);
389 /* When we expand a statement-tree, we must know whether or not the
390 statements are full-expressions. We record that fact here. */
391 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
394 /* Add T to the statement-tree. Non-side-effect statements need to be
395 recorded during statement expressions. */
396 append_to_statement_list_force (t, &cur_stmt_list);
401 /* Returns the stmt_tree to which statements are currently being added. */
404 current_stmt_tree (void)
407 ? &cfun->language->base.x_stmt_tree
408 : &scope_chain->x_stmt_tree);
411 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
414 maybe_cleanup_point_expr (tree expr)
416 if (!processing_template_decl && stmts_are_full_exprs_p ())
417 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
421 /* Like maybe_cleanup_point_expr except have the type of the new expression be
422 void so we don't need to create a temporary variable to hold the inner
423 expression. The reason why we do this is because the original type might be
424 an aggregate and we cannot create a temporary variable for that type. */
427 maybe_cleanup_point_expr_void (tree expr)
429 if (!processing_template_decl && stmts_are_full_exprs_p ())
430 expr = fold_build_cleanup_point_expr (void_type_node, expr);
436 /* Create a declaration statement for the declaration given by the DECL. */
439 add_decl_expr (tree decl)
441 tree r = build_stmt (input_location, DECL_EXPR, decl);
442 if (DECL_INITIAL (decl)
443 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
444 r = maybe_cleanup_point_expr_void (r);
448 /* Finish a scope. */
451 do_poplevel (tree stmt_list)
455 if (stmts_are_full_exprs_p ())
456 block = poplevel (kept_level_p (), 1, 0);
458 stmt_list = pop_stmt_list (stmt_list);
460 if (!processing_template_decl)
462 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
463 /* ??? See c_end_compound_stmt re statement expressions. */
469 /* Begin a new scope. */
472 do_pushlevel (scope_kind sk)
474 tree ret = push_stmt_list ();
475 if (stmts_are_full_exprs_p ())
476 begin_scope (sk, NULL);
480 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
481 when the current scope is exited. EH_ONLY is true when this is not
482 meant to apply to normal control flow transfer. */
485 push_cleanup (tree decl, tree cleanup, bool eh_only)
487 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
488 CLEANUP_EH_ONLY (stmt) = eh_only;
490 CLEANUP_BODY (stmt) = push_stmt_list ();
493 /* Begin a conditional that might contain a declaration. When generating
494 normal code, we want the declaration to appear before the statement
495 containing the conditional. When generating template code, we want the
496 conditional to be rendered as the raw DECL_EXPR. */
499 begin_cond (tree *cond_p)
501 if (processing_template_decl)
502 *cond_p = push_stmt_list ();
505 /* Finish such a conditional. */
508 finish_cond (tree *cond_p, tree expr)
510 if (processing_template_decl)
512 tree cond = pop_stmt_list (*cond_p);
513 if (TREE_CODE (cond) == DECL_EXPR)
516 if (check_for_bare_parameter_packs (expr))
517 *cond_p = error_mark_node;
522 /* If *COND_P specifies a conditional with a declaration, transform the
525 for (; A x = 42;) { }
527 while (true) { A x = 42; if (!x) break; }
528 for (;;) { A x = 42; if (!x) break; }
529 The statement list for BODY will be empty if the conditional did
530 not declare anything. */
533 simplify_loop_decl_cond (tree *cond_p, tree body)
537 if (!TREE_SIDE_EFFECTS (body))
541 *cond_p = boolean_true_node;
543 if_stmt = begin_if_stmt ();
544 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
545 finish_if_stmt_cond (cond, if_stmt);
546 finish_break_stmt ();
547 finish_then_clause (if_stmt);
548 finish_if_stmt (if_stmt);
551 /* Finish a goto-statement. */
554 finish_goto_stmt (tree destination)
556 if (TREE_CODE (destination) == IDENTIFIER_NODE)
557 destination = lookup_label (destination);
559 /* We warn about unused labels with -Wunused. That means we have to
560 mark the used labels as used. */
561 if (TREE_CODE (destination) == LABEL_DECL)
562 TREE_USED (destination) = 1;
565 destination = mark_rvalue_use (destination);
566 if (!processing_template_decl)
568 destination = cp_convert (ptr_type_node, destination);
569 if (error_operand_p (destination))
572 /* We don't inline calls to functions with computed gotos.
573 Those functions are typically up to some funny business,
574 and may be depending on the labels being at particular
575 addresses, or some such. */
576 DECL_UNINLINABLE (current_function_decl) = 1;
579 check_goto (destination);
581 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
584 /* COND is the condition-expression for an if, while, etc.,
585 statement. Convert it to a boolean value, if appropriate.
586 In addition, verify sequence points if -Wsequence-point is enabled. */
589 maybe_convert_cond (tree cond)
591 /* Empty conditions remain empty. */
595 /* Wait until we instantiate templates before doing conversion. */
596 if (processing_template_decl)
599 if (warn_sequence_point)
600 verify_sequence_points (cond);
602 /* Do the conversion. */
603 cond = convert_from_reference (cond);
605 if (TREE_CODE (cond) == MODIFY_EXPR
606 && !TREE_NO_WARNING (cond)
609 warning (OPT_Wparentheses,
610 "suggest parentheses around assignment used as truth value");
611 TREE_NO_WARNING (cond) = 1;
614 return condition_conversion (cond);
617 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
620 finish_expr_stmt (tree expr)
624 if (expr != NULL_TREE)
626 if (!processing_template_decl)
628 if (warn_sequence_point)
629 verify_sequence_points (expr);
630 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
632 else if (!type_dependent_expression_p (expr))
633 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
634 tf_warning_or_error);
636 if (check_for_bare_parameter_packs (expr))
637 expr = error_mark_node;
639 /* Simplification of inner statement expressions, compound exprs,
640 etc can result in us already having an EXPR_STMT. */
641 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
643 if (TREE_CODE (expr) != EXPR_STMT)
644 expr = build_stmt (input_location, EXPR_STMT, expr);
645 expr = maybe_cleanup_point_expr_void (expr);
657 /* Begin an if-statement. Returns a newly created IF_STMT if
664 scope = do_pushlevel (sk_block);
665 r = build_stmt (input_location, IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
666 TREE_CHAIN (r) = scope;
667 begin_cond (&IF_COND (r));
671 /* Process the COND of an if-statement, which may be given by
675 finish_if_stmt_cond (tree cond, tree if_stmt)
677 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
679 THEN_CLAUSE (if_stmt) = push_stmt_list ();
682 /* Finish the then-clause of an if-statement, which may be given by
686 finish_then_clause (tree if_stmt)
688 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
692 /* Begin the else-clause of an if-statement. */
695 begin_else_clause (tree if_stmt)
697 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
700 /* Finish the else-clause of an if-statement, which may be given by
704 finish_else_clause (tree if_stmt)
706 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
709 /* Finish an if-statement. */
712 finish_if_stmt (tree if_stmt)
714 tree scope = TREE_CHAIN (if_stmt);
715 TREE_CHAIN (if_stmt) = NULL;
716 add_stmt (do_poplevel (scope));
720 /* Begin a while-statement. Returns a newly created WHILE_STMT if
724 begin_while_stmt (void)
727 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
729 WHILE_BODY (r) = do_pushlevel (sk_block);
730 begin_cond (&WHILE_COND (r));
734 /* Process the COND of a while-statement, which may be given by
738 finish_while_stmt_cond (tree cond, tree while_stmt)
740 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
741 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
744 /* Finish a while-statement, which may be given by WHILE_STMT. */
747 finish_while_stmt (tree while_stmt)
749 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
753 /* Begin a do-statement. Returns a newly created DO_STMT if
759 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
761 DO_BODY (r) = push_stmt_list ();
765 /* Finish the body of a do-statement, which may be given by DO_STMT. */
768 finish_do_body (tree do_stmt)
770 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
772 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
773 body = STATEMENT_LIST_TAIL (body)->stmt;
775 if (IS_EMPTY_STMT (body))
776 warning (OPT_Wempty_body,
777 "suggest explicit braces around empty body in %<do%> statement");
780 /* Finish a do-statement, which may be given by DO_STMT, and whose
781 COND is as indicated. */
784 finish_do_stmt (tree cond, tree do_stmt)
786 cond = maybe_convert_cond (cond);
787 DO_COND (do_stmt) = cond;
791 /* Finish a return-statement. The EXPRESSION returned, if any, is as
795 finish_return_stmt (tree expr)
800 expr = check_return_expr (expr, &no_warning);
802 if (flag_openmp && !check_omp_return ())
803 return error_mark_node;
804 if (!processing_template_decl)
806 if (warn_sequence_point)
807 verify_sequence_points (expr);
809 if (DECL_DESTRUCTOR_P (current_function_decl)
810 || (DECL_CONSTRUCTOR_P (current_function_decl)
811 && targetm.cxx.cdtor_returns_this ()))
813 /* Similarly, all destructors must run destructors for
814 base-classes before returning. So, all returns in a
815 destructor get sent to the DTOR_LABEL; finish_function emits
816 code to return a value there. */
817 return finish_goto_stmt (cdtor_label);
821 r = build_stmt (input_location, RETURN_EXPR, expr);
822 TREE_NO_WARNING (r) |= no_warning;
823 r = maybe_cleanup_point_expr_void (r);
830 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
833 begin_for_stmt (void)
837 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
838 NULL_TREE, NULL_TREE);
840 if (flag_new_for_scope > 0)
841 TREE_CHAIN (r) = do_pushlevel (sk_for);
843 if (processing_template_decl)
844 FOR_INIT_STMT (r) = push_stmt_list ();
849 /* Finish the for-init-statement of a for-statement, which may be
850 given by FOR_STMT. */
853 finish_for_init_stmt (tree for_stmt)
855 if (processing_template_decl)
856 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
858 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
859 begin_cond (&FOR_COND (for_stmt));
862 /* Finish the COND of a for-statement, which may be given by
866 finish_for_cond (tree cond, tree for_stmt)
868 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
869 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
872 /* Finish the increment-EXPRESSION in a for-statement, which may be
873 given by FOR_STMT. */
876 finish_for_expr (tree expr, tree for_stmt)
880 /* If EXPR is an overloaded function, issue an error; there is no
881 context available to use to perform overload resolution. */
882 if (type_unknown_p (expr))
884 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
885 expr = error_mark_node;
887 if (!processing_template_decl)
889 if (warn_sequence_point)
890 verify_sequence_points (expr);
891 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
892 tf_warning_or_error);
894 else if (!type_dependent_expression_p (expr))
895 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
896 tf_warning_or_error);
897 expr = maybe_cleanup_point_expr_void (expr);
898 if (check_for_bare_parameter_packs (expr))
899 expr = error_mark_node;
900 FOR_EXPR (for_stmt) = expr;
903 /* Finish the body of a for-statement, which may be given by
904 FOR_STMT. The increment-EXPR for the loop must be
906 It can also finish RANGE_FOR_STMT. */
909 finish_for_stmt (tree for_stmt)
911 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
912 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
914 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
916 /* Pop the scope for the body of the loop. */
917 if (flag_new_for_scope > 0)
919 tree scope = TREE_CHAIN (for_stmt);
920 TREE_CHAIN (for_stmt) = NULL;
921 add_stmt (do_poplevel (scope));
927 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
928 To finish it call finish_for_stmt(). */
931 begin_range_for_stmt (void)
935 r = build_stmt (input_location, RANGE_FOR_STMT,
936 NULL_TREE, NULL_TREE, NULL_TREE);
938 if (flag_new_for_scope > 0)
939 TREE_CHAIN (r) = do_pushlevel (sk_for);
944 /* Finish the head of a range-based for statement, which may
945 be given by RANGE_FOR_STMT. DECL must be the declaration
946 and EXPR must be the loop expression. */
949 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
951 RANGE_FOR_DECL (range_for_stmt) = decl;
952 RANGE_FOR_EXPR (range_for_stmt) = expr;
953 add_stmt (range_for_stmt);
954 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
957 /* Finish a break-statement. */
960 finish_break_stmt (void)
962 return add_stmt (build_stmt (input_location, BREAK_STMT));
965 /* Finish a continue-statement. */
968 finish_continue_stmt (void)
970 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
973 /* Begin a switch-statement. Returns a new SWITCH_STMT if
977 begin_switch_stmt (void)
981 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
983 scope = do_pushlevel (sk_block);
984 TREE_CHAIN (r) = scope;
985 begin_cond (&SWITCH_STMT_COND (r));
990 /* Finish the cond of a switch-statement. */
993 finish_switch_cond (tree cond, tree switch_stmt)
995 tree orig_type = NULL;
996 if (!processing_template_decl)
998 /* Convert the condition to an integer or enumeration type. */
999 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1000 if (cond == NULL_TREE)
1002 error ("switch quantity not an integer");
1003 cond = error_mark_node;
1005 orig_type = TREE_TYPE (cond);
1006 if (cond != error_mark_node)
1010 Integral promotions are performed. */
1011 cond = perform_integral_promotions (cond);
1012 cond = maybe_cleanup_point_expr (cond);
1015 if (check_for_bare_parameter_packs (cond))
1016 cond = error_mark_node;
1017 else if (!processing_template_decl && warn_sequence_point)
1018 verify_sequence_points (cond);
1020 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1021 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1022 add_stmt (switch_stmt);
1023 push_switch (switch_stmt);
1024 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1027 /* Finish the body of a switch-statement, which may be given by
1028 SWITCH_STMT. The COND to switch on is indicated. */
1031 finish_switch_stmt (tree switch_stmt)
1035 SWITCH_STMT_BODY (switch_stmt) =
1036 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1040 scope = TREE_CHAIN (switch_stmt);
1041 TREE_CHAIN (switch_stmt) = NULL;
1042 add_stmt (do_poplevel (scope));
1045 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1049 begin_try_block (void)
1051 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1053 TRY_STMTS (r) = push_stmt_list ();
1057 /* Likewise, for a function-try-block. The block returned in
1058 *COMPOUND_STMT is an artificial outer scope, containing the
1059 function-try-block. */
1062 begin_function_try_block (tree *compound_stmt)
1065 /* This outer scope does not exist in the C++ standard, but we need
1066 a place to put __FUNCTION__ and similar variables. */
1067 *compound_stmt = begin_compound_stmt (0);
1068 r = begin_try_block ();
1069 FN_TRY_BLOCK_P (r) = 1;
1073 /* Finish a try-block, which may be given by TRY_BLOCK. */
1076 finish_try_block (tree try_block)
1078 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1079 TRY_HANDLERS (try_block) = push_stmt_list ();
1082 /* Finish the body of a cleanup try-block, which may be given by
1086 finish_cleanup_try_block (tree try_block)
1088 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1091 /* Finish an implicitly generated try-block, with a cleanup is given
1095 finish_cleanup (tree cleanup, tree try_block)
1097 TRY_HANDLERS (try_block) = cleanup;
1098 CLEANUP_P (try_block) = 1;
1101 /* Likewise, for a function-try-block. */
1104 finish_function_try_block (tree try_block)
1106 finish_try_block (try_block);
1107 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1108 the try block, but moving it inside. */
1109 in_function_try_handler = 1;
1112 /* Finish a handler-sequence for a try-block, which may be given by
1116 finish_handler_sequence (tree try_block)
1118 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1119 check_handlers (TRY_HANDLERS (try_block));
1122 /* Finish the handler-seq for a function-try-block, given by
1123 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1124 begin_function_try_block. */
1127 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1129 in_function_try_handler = 0;
1130 finish_handler_sequence (try_block);
1131 finish_compound_stmt (compound_stmt);
1134 /* Begin a handler. Returns a HANDLER if appropriate. */
1137 begin_handler (void)
1141 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1144 /* Create a binding level for the eh_info and the exception object
1146 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1151 /* Finish the handler-parameters for a handler, which may be given by
1152 HANDLER. DECL is the declaration for the catch parameter, or NULL
1153 if this is a `catch (...)' clause. */
1156 finish_handler_parms (tree decl, tree handler)
1158 tree type = NULL_TREE;
1159 if (processing_template_decl)
1163 decl = pushdecl (decl);
1164 decl = push_template_decl (decl);
1165 HANDLER_PARMS (handler) = decl;
1166 type = TREE_TYPE (decl);
1170 type = expand_start_catch_block (decl);
1171 HANDLER_TYPE (handler) = type;
1172 if (!processing_template_decl && type)
1173 mark_used (eh_type_info (type));
1176 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1177 the return value from the matching call to finish_handler_parms. */
1180 finish_handler (tree handler)
1182 if (!processing_template_decl)
1183 expand_end_catch_block ();
1184 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1187 /* Begin a compound statement. FLAGS contains some bits that control the
1188 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1189 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1190 block of a function. If BCS_TRY_BLOCK is set, this is the block
1191 created on behalf of a TRY statement. Returns a token to be passed to
1192 finish_compound_stmt. */
1195 begin_compound_stmt (unsigned int flags)
1199 if (flags & BCS_NO_SCOPE)
1201 r = push_stmt_list ();
1202 STATEMENT_LIST_NO_SCOPE (r) = 1;
1204 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1205 But, if it's a statement-expression with a scopeless block, there's
1206 nothing to keep, and we don't want to accidentally keep a block
1207 *inside* the scopeless block. */
1208 keep_next_level (false);
1211 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1213 /* When processing a template, we need to remember where the braces were,
1214 so that we can set up identical scopes when instantiating the template
1215 later. BIND_EXPR is a handy candidate for this.
1216 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1217 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1218 processing templates. */
1219 if (processing_template_decl)
1221 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1222 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1223 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1224 TREE_SIDE_EFFECTS (r) = 1;
1230 /* Finish a compound-statement, which is given by STMT. */
1233 finish_compound_stmt (tree stmt)
1235 if (TREE_CODE (stmt) == BIND_EXPR)
1237 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1238 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1239 discard the BIND_EXPR so it can be merged with the containing
1241 if (TREE_CODE (body) == STATEMENT_LIST
1242 && STATEMENT_LIST_HEAD (body) == NULL
1243 && !BIND_EXPR_BODY_BLOCK (stmt)
1244 && !BIND_EXPR_TRY_BLOCK (stmt))
1247 BIND_EXPR_BODY (stmt) = body;
1249 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1250 stmt = pop_stmt_list (stmt);
1253 /* Destroy any ObjC "super" receivers that may have been
1255 objc_clear_super_receiver ();
1257 stmt = do_poplevel (stmt);
1260 /* ??? See c_end_compound_stmt wrt statement expressions. */
1265 /* Finish an asm-statement, whose components are a STRING, some
1266 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1267 LABELS. Also note whether the asm-statement should be
1268 considered volatile. */
1271 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1272 tree input_operands, tree clobbers, tree labels)
1276 int ninputs = list_length (input_operands);
1277 int noutputs = list_length (output_operands);
1279 if (!processing_template_decl)
1281 const char *constraint;
1282 const char **oconstraints;
1283 bool allows_mem, allows_reg, is_inout;
1287 oconstraints = XALLOCAVEC (const char *, noutputs);
1289 string = resolve_asm_operand_names (string, output_operands,
1290 input_operands, labels);
1292 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1294 operand = TREE_VALUE (t);
1296 /* ??? Really, this should not be here. Users should be using a
1297 proper lvalue, dammit. But there's a long history of using
1298 casts in the output operands. In cases like longlong.h, this
1299 becomes a primitive form of typechecking -- if the cast can be
1300 removed, then the output operand had a type of the proper width;
1301 otherwise we'll get an error. Gross, but ... */
1302 STRIP_NOPS (operand);
1304 operand = mark_lvalue_use (operand);
1306 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1307 operand = error_mark_node;
1309 if (operand != error_mark_node
1310 && (TREE_READONLY (operand)
1311 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1312 /* Functions are not modifiable, even though they are
1314 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1315 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1316 /* If it's an aggregate and any field is const, then it is
1317 effectively const. */
1318 || (CLASS_TYPE_P (TREE_TYPE (operand))
1319 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1320 readonly_error (operand, REK_ASSIGNMENT_ASM);
1322 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1323 oconstraints[i] = constraint;
1325 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1326 &allows_mem, &allows_reg, &is_inout))
1328 /* If the operand is going to end up in memory,
1329 mark it addressable. */
1330 if (!allows_reg && !cxx_mark_addressable (operand))
1331 operand = error_mark_node;
1334 operand = error_mark_node;
1336 TREE_VALUE (t) = operand;
1339 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1341 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1342 operand = decay_conversion (TREE_VALUE (t));
1344 /* If the type of the operand hasn't been determined (e.g.,
1345 because it involves an overloaded function), then issue
1346 an error message. There's no context available to
1347 resolve the overloading. */
1348 if (TREE_TYPE (operand) == unknown_type_node)
1350 error ("type of asm operand %qE could not be determined",
1352 operand = error_mark_node;
1355 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1356 oconstraints, &allows_mem, &allows_reg))
1358 /* If the operand is going to end up in memory,
1359 mark it addressable. */
1360 if (!allows_reg && allows_mem)
1362 /* Strip the nops as we allow this case. FIXME, this really
1363 should be rejected or made deprecated. */
1364 STRIP_NOPS (operand);
1365 if (!cxx_mark_addressable (operand))
1366 operand = error_mark_node;
1370 operand = error_mark_node;
1372 TREE_VALUE (t) = operand;
1376 r = build_stmt (input_location, ASM_EXPR, string,
1377 output_operands, input_operands,
1379 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1380 r = maybe_cleanup_point_expr_void (r);
1381 return add_stmt (r);
1384 /* Finish a label with the indicated NAME. Returns the new label. */
1387 finish_label_stmt (tree name)
1389 tree decl = define_label (input_location, name);
1391 if (decl == error_mark_node)
1392 return error_mark_node;
1394 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1399 /* Finish a series of declarations for local labels. G++ allows users
1400 to declare "local" labels, i.e., labels with scope. This extension
1401 is useful when writing code involving statement-expressions. */
1404 finish_label_decl (tree name)
1406 if (!at_function_scope_p ())
1408 error ("__label__ declarations are only allowed in function scopes");
1412 add_decl_expr (declare_local_label (name));
1415 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1418 finish_decl_cleanup (tree decl, tree cleanup)
1420 push_cleanup (decl, cleanup, false);
1423 /* If the current scope exits with an exception, run CLEANUP. */
1426 finish_eh_cleanup (tree cleanup)
1428 push_cleanup (NULL, cleanup, true);
1431 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1432 order they were written by the user. Each node is as for
1433 emit_mem_initializers. */
1436 finish_mem_initializers (tree mem_inits)
1438 /* Reorder the MEM_INITS so that they are in the order they appeared
1439 in the source program. */
1440 mem_inits = nreverse (mem_inits);
1442 if (processing_template_decl)
1446 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1448 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1449 check for bare parameter packs in the TREE_VALUE, because
1450 any parameter packs in the TREE_VALUE have already been
1451 bound as part of the TREE_PURPOSE. See
1452 make_pack_expansion for more information. */
1453 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1454 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1455 TREE_VALUE (mem) = error_mark_node;
1458 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1461 emit_mem_initializers (mem_inits);
1464 /* Finish a parenthesized expression EXPR. */
1467 finish_parenthesized_expr (tree expr)
1470 /* This inhibits warnings in c_common_truthvalue_conversion. */
1471 TREE_NO_WARNING (expr) = 1;
1473 if (TREE_CODE (expr) == OFFSET_REF)
1474 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1475 enclosed in parentheses. */
1476 PTRMEM_OK_P (expr) = 0;
1478 if (TREE_CODE (expr) == STRING_CST)
1479 PAREN_STRING_LITERAL_P (expr) = 1;
1484 /* Finish a reference to a non-static data member (DECL) that is not
1485 preceded by `.' or `->'. */
1488 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1490 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1494 tree scope = qualifying_scope;
1495 if (scope == NULL_TREE)
1496 scope = context_for_name_lookup (decl);
1497 object = maybe_dummy_object (scope, NULL);
1500 /* DR 613: Can use non-static data members without an associated
1501 object in sizeof/decltype/alignof. */
1502 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1503 && (!processing_template_decl || !current_class_ref))
1505 if (current_function_decl
1506 && DECL_STATIC_FUNCTION_P (current_function_decl))
1507 error ("invalid use of member %q+D in static member function", decl);
1509 error ("invalid use of non-static data member %q+D", decl);
1510 error ("from this location");
1512 return error_mark_node;
1515 if (current_class_ptr)
1516 TREE_USED (current_class_ptr) = 1;
1517 if (processing_template_decl && !qualifying_scope)
1519 tree type = TREE_TYPE (decl);
1521 if (TREE_CODE (type) == REFERENCE_TYPE)
1522 type = TREE_TYPE (type);
1525 /* Set the cv qualifiers. */
1526 int quals = (current_class_ref
1527 ? cp_type_quals (TREE_TYPE (current_class_ref))
1528 : TYPE_UNQUALIFIED);
1530 if (DECL_MUTABLE_P (decl))
1531 quals &= ~TYPE_QUAL_CONST;
1533 quals |= cp_type_quals (TREE_TYPE (decl));
1534 type = cp_build_qualified_type (type, quals);
1537 return build_min (COMPONENT_REF, type, object, decl, NULL_TREE);
1539 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1540 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1542 else if (processing_template_decl)
1543 return build_qualified_name (TREE_TYPE (decl),
1546 /*template_p=*/false);
1549 tree access_type = TREE_TYPE (object);
1551 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1554 /* If the data member was named `C::M', convert `*this' to `C'
1556 if (qualifying_scope)
1558 tree binfo = NULL_TREE;
1559 object = build_scoped_ref (object, qualifying_scope,
1563 return build_class_member_access_expr (object, decl,
1564 /*access_path=*/NULL_TREE,
1565 /*preserve_reference=*/false,
1566 tf_warning_or_error);
1570 /* If we are currently parsing a template and we encountered a typedef
1571 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1572 adds the typedef to a list tied to the current template.
1573 At tempate instantiatin time, that list is walked and access check
1574 performed for each typedef.
1575 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1578 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1580 location_t location)
1582 tree template_info = NULL;
1583 tree cs = current_scope ();
1585 if (!is_typedef_decl (typedef_decl)
1587 || !CLASS_TYPE_P (context)
1591 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1592 template_info = get_template_info (cs);
1595 && TI_TEMPLATE (template_info)
1596 && !currently_open_class (context))
1597 append_type_to_template_for_access_check (cs, typedef_decl,
1601 /* DECL was the declaration to which a qualified-id resolved. Issue
1602 an error message if it is not accessible. If OBJECT_TYPE is
1603 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1604 type of `*x', or `x', respectively. If the DECL was named as
1605 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1608 check_accessibility_of_qualified_id (tree decl,
1610 tree nested_name_specifier)
1613 tree qualifying_type = NULL_TREE;
1615 /* If we are parsing a template declaration and if decl is a typedef,
1616 add it to a list tied to the template.
1617 At template instantiation time, that list will be walked and
1618 access check performed. */
1619 add_typedef_to_current_template_for_access_check (decl,
1620 nested_name_specifier
1621 ? nested_name_specifier
1622 : DECL_CONTEXT (decl),
1625 /* If we're not checking, return immediately. */
1626 if (deferred_access_no_check)
1629 /* Determine the SCOPE of DECL. */
1630 scope = context_for_name_lookup (decl);
1631 /* If the SCOPE is not a type, then DECL is not a member. */
1632 if (!TYPE_P (scope))
1634 /* Compute the scope through which DECL is being accessed. */
1636 /* OBJECT_TYPE might not be a class type; consider:
1638 class A { typedef int I; };
1642 In this case, we will have "A::I" as the DECL, but "I" as the
1644 && CLASS_TYPE_P (object_type)
1645 && DERIVED_FROM_P (scope, object_type))
1646 /* If we are processing a `->' or `.' expression, use the type of the
1648 qualifying_type = object_type;
1649 else if (nested_name_specifier)
1651 /* If the reference is to a non-static member of the
1652 current class, treat it as if it were referenced through
1654 if (DECL_NONSTATIC_MEMBER_P (decl)
1655 && current_class_ptr
1656 && DERIVED_FROM_P (scope, current_class_type))
1657 qualifying_type = current_class_type;
1658 /* Otherwise, use the type indicated by the
1659 nested-name-specifier. */
1661 qualifying_type = nested_name_specifier;
1664 /* Otherwise, the name must be from the current class or one of
1666 qualifying_type = currently_open_derived_class (scope);
1669 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1670 or similar in a default argument value. */
1671 && CLASS_TYPE_P (qualifying_type)
1672 && !dependent_type_p (qualifying_type))
1673 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1677 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1678 class named to the left of the "::" operator. DONE is true if this
1679 expression is a complete postfix-expression; it is false if this
1680 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1681 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1682 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1683 is true iff this qualified name appears as a template argument. */
1686 finish_qualified_id_expr (tree qualifying_class,
1691 bool template_arg_p)
1693 gcc_assert (TYPE_P (qualifying_class));
1695 if (error_operand_p (expr))
1696 return error_mark_node;
1698 if (DECL_P (expr) || BASELINK_P (expr))
1702 check_template_keyword (expr);
1704 /* If EXPR occurs as the operand of '&', use special handling that
1705 permits a pointer-to-member. */
1706 if (address_p && done)
1708 if (TREE_CODE (expr) == SCOPE_REF)
1709 expr = TREE_OPERAND (expr, 1);
1710 expr = build_offset_ref (qualifying_class, expr,
1711 /*address_p=*/true);
1715 /* Within the scope of a class, turn references to non-static
1716 members into expression of the form "this->...". */
1718 /* But, within a template argument, we do not want make the
1719 transformation, as there is no "this" pointer. */
1721 else if (TREE_CODE (expr) == FIELD_DECL)
1723 push_deferring_access_checks (dk_no_check);
1724 expr = finish_non_static_data_member (expr, NULL_TREE,
1726 pop_deferring_access_checks ();
1728 else if (BASELINK_P (expr) && !processing_template_decl)
1732 /* See if any of the functions are non-static members. */
1733 /* If so, the expression may be relative to 'this'. */
1734 if (!shared_member_p (expr)
1735 && (ob = maybe_dummy_object (qualifying_class, NULL),
1736 !is_dummy_object (ob)))
1737 expr = (build_class_member_access_expr
1740 BASELINK_ACCESS_BINFO (expr),
1741 /*preserve_reference=*/false,
1742 tf_warning_or_error));
1744 /* The expression is a qualified name whose address is not
1746 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1752 /* Begin a statement-expression. The value returned must be passed to
1753 finish_stmt_expr. */
1756 begin_stmt_expr (void)
1758 return push_stmt_list ();
1761 /* Process the final expression of a statement expression. EXPR can be
1762 NULL, if the final expression is empty. Return a STATEMENT_LIST
1763 containing all the statements in the statement-expression, or
1764 ERROR_MARK_NODE if there was an error. */
1767 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1769 if (error_operand_p (expr))
1771 /* The type of the statement-expression is the type of the last
1773 TREE_TYPE (stmt_expr) = error_mark_node;
1774 return error_mark_node;
1777 /* If the last statement does not have "void" type, then the value
1778 of the last statement is the value of the entire expression. */
1781 tree type = TREE_TYPE (expr);
1783 if (processing_template_decl)
1785 expr = build_stmt (input_location, EXPR_STMT, expr);
1786 expr = add_stmt (expr);
1787 /* Mark the last statement so that we can recognize it as such at
1788 template-instantiation time. */
1789 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1791 else if (VOID_TYPE_P (type))
1793 /* Just treat this like an ordinary statement. */
1794 expr = finish_expr_stmt (expr);
1798 /* It actually has a value we need to deal with. First, force it
1799 to be an rvalue so that we won't need to build up a copy
1800 constructor call later when we try to assign it to something. */
1801 expr = force_rvalue (expr);
1802 if (error_operand_p (expr))
1803 return error_mark_node;
1805 /* Update for array-to-pointer decay. */
1806 type = TREE_TYPE (expr);
1808 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1809 normal statement, but don't convert to void or actually add
1811 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1812 expr = maybe_cleanup_point_expr (expr);
1816 /* The type of the statement-expression is the type of the last
1818 TREE_TYPE (stmt_expr) = type;
1824 /* Finish a statement-expression. EXPR should be the value returned
1825 by the previous begin_stmt_expr. Returns an expression
1826 representing the statement-expression. */
1829 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1834 if (error_operand_p (stmt_expr))
1836 pop_stmt_list (stmt_expr);
1837 return error_mark_node;
1840 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1842 type = TREE_TYPE (stmt_expr);
1843 result = pop_stmt_list (stmt_expr);
1844 TREE_TYPE (result) = type;
1846 if (processing_template_decl)
1848 result = build_min (STMT_EXPR, type, result);
1849 TREE_SIDE_EFFECTS (result) = 1;
1850 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1852 else if (CLASS_TYPE_P (type))
1854 /* Wrap the statement-expression in a TARGET_EXPR so that the
1855 temporary object created by the final expression is destroyed at
1856 the end of the full-expression containing the
1857 statement-expression. */
1858 result = force_target_expr (type, result);
1864 /* Returns the expression which provides the value of STMT_EXPR. */
1867 stmt_expr_value_expr (tree stmt_expr)
1869 tree t = STMT_EXPR_STMT (stmt_expr);
1871 if (TREE_CODE (t) == BIND_EXPR)
1872 t = BIND_EXPR_BODY (t);
1874 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
1875 t = STATEMENT_LIST_TAIL (t)->stmt;
1877 if (TREE_CODE (t) == EXPR_STMT)
1878 t = EXPR_STMT_EXPR (t);
1883 /* Return TRUE iff EXPR_STMT is an empty list of
1884 expression statements. */
1887 empty_expr_stmt_p (tree expr_stmt)
1889 tree body = NULL_TREE;
1891 if (expr_stmt == void_zero_node)
1896 if (TREE_CODE (expr_stmt) == EXPR_STMT)
1897 body = EXPR_STMT_EXPR (expr_stmt);
1898 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
1904 if (TREE_CODE (body) == STATEMENT_LIST)
1905 return tsi_end_p (tsi_start (body));
1907 return empty_expr_stmt_p (body);
1912 /* Perform Koenig lookup. FN is the postfix-expression representing
1913 the function (or functions) to call; ARGS are the arguments to the
1914 call; if INCLUDE_STD then the `std' namespace is automatically
1915 considered an associated namespace (used in range-based for loops).
1916 Returns the functions to be considered by overload resolution. */
1919 perform_koenig_lookup (tree fn, VEC(tree,gc) *args, bool include_std)
1921 tree identifier = NULL_TREE;
1922 tree functions = NULL_TREE;
1923 tree tmpl_args = NULL_TREE;
1924 bool template_id = false;
1926 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1928 /* Use a separate flag to handle null args. */
1930 tmpl_args = TREE_OPERAND (fn, 1);
1931 fn = TREE_OPERAND (fn, 0);
1934 /* Find the name of the overloaded function. */
1935 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1937 else if (is_overloaded_fn (fn))
1940 identifier = DECL_NAME (get_first_fn (functions));
1942 else if (DECL_P (fn))
1945 identifier = DECL_NAME (fn);
1948 /* A call to a namespace-scope function using an unqualified name.
1950 Do Koenig lookup -- unless any of the arguments are
1952 if (!any_type_dependent_arguments_p (args)
1953 && !any_dependent_template_arguments_p (tmpl_args))
1955 fn = lookup_arg_dependent (identifier, functions, args, include_std);
1957 /* The unqualified name could not be resolved. */
1958 fn = unqualified_fn_lookup_error (identifier);
1961 if (fn && template_id)
1962 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
1967 /* Generate an expression for `FN (ARGS)'. This may change the
1970 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1971 as a virtual call, even if FN is virtual. (This flag is set when
1972 encountering an expression where the function name is explicitly
1973 qualified. For example a call to `X::f' never generates a virtual
1976 Returns code for the call. */
1979 finish_call_expr (tree fn, VEC(tree,gc) **args, bool disallow_virtual,
1980 bool koenig_p, tsubst_flags_t complain)
1984 VEC(tree,gc) *orig_args = NULL;
1986 if (fn == error_mark_node)
1987 return error_mark_node;
1989 gcc_assert (!TYPE_P (fn));
1993 if (processing_template_decl)
1995 if (type_dependent_expression_p (fn)
1996 || any_type_dependent_arguments_p (*args))
1998 result = build_nt_call_vec (fn, *args);
1999 KOENIG_LOOKUP_P (result) = koenig_p;
2004 tree fndecl = OVL_CURRENT (fn);
2005 if (TREE_CODE (fndecl) != FUNCTION_DECL
2006 || !TREE_THIS_VOLATILE (fndecl))
2012 current_function_returns_abnormally = 1;
2016 orig_args = make_tree_vector_copy (*args);
2017 if (!BASELINK_P (fn)
2018 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2019 && TREE_TYPE (fn) != unknown_type_node)
2020 fn = build_non_dependent_expr (fn);
2021 make_args_non_dependent (*args);
2024 if (is_overloaded_fn (fn))
2025 fn = baselink_for_fns (fn);
2028 if (BASELINK_P (fn))
2032 /* A call to a member function. From [over.call.func]:
2034 If the keyword this is in scope and refers to the class of
2035 that member function, or a derived class thereof, then the
2036 function call is transformed into a qualified function call
2037 using (*this) as the postfix-expression to the left of the
2038 . operator.... [Otherwise] a contrived object of type T
2039 becomes the implied object argument.
2043 struct A { void f(); };
2044 struct B : public A {};
2045 struct C : public A { void g() { B::f(); }};
2047 "the class of that member function" refers to `A'. But 11.2
2048 [class.access.base] says that we need to convert 'this' to B* as
2049 part of the access, so we pass 'B' to maybe_dummy_object. */
2051 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2054 if (processing_template_decl)
2056 if (type_dependent_expression_p (object))
2058 tree ret = build_nt_call_vec (orig_fn, orig_args);
2059 release_tree_vector (orig_args);
2062 object = build_non_dependent_expr (object);
2065 result = build_new_method_call (object, fn, args, NULL_TREE,
2067 ? LOOKUP_NONVIRTUAL : 0),
2071 else if (is_overloaded_fn (fn))
2073 /* If the function is an overloaded builtin, resolve it. */
2074 if (TREE_CODE (fn) == FUNCTION_DECL
2075 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2076 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2077 result = resolve_overloaded_builtin (input_location, fn, *args);
2080 /* A call to a namespace-scope function. */
2081 result = build_new_function_call (fn, args, koenig_p, complain);
2083 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2085 if (!VEC_empty (tree, *args))
2086 error ("arguments to destructor are not allowed");
2087 /* Mark the pseudo-destructor call as having side-effects so
2088 that we do not issue warnings about its use. */
2089 result = build1 (NOP_EXPR,
2091 TREE_OPERAND (fn, 0));
2092 TREE_SIDE_EFFECTS (result) = 1;
2094 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2095 /* If the "function" is really an object of class type, it might
2096 have an overloaded `operator ()'. */
2097 result = build_op_call (fn, args, complain);
2100 /* A call where the function is unknown. */
2101 result = cp_build_function_call_vec (fn, args, complain);
2103 if (processing_template_decl)
2105 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2106 KOENIG_LOOKUP_P (result) = koenig_p;
2107 release_tree_vector (orig_args);
2113 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2114 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2115 POSTDECREMENT_EXPR.) */
2118 finish_increment_expr (tree expr, enum tree_code code)
2120 return build_x_unary_op (code, expr, tf_warning_or_error);
2123 /* Finish a use of `this'. Returns an expression for `this'. */
2126 finish_this_expr (void)
2130 if (current_class_ptr)
2132 tree type = TREE_TYPE (current_class_ref);
2134 /* In a lambda expression, 'this' refers to the captured 'this'. */
2135 if (LAMBDA_TYPE_P (type))
2136 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2138 result = current_class_ptr;
2141 else if (current_function_decl
2142 && DECL_STATIC_FUNCTION_P (current_function_decl))
2144 error ("%<this%> is unavailable for static member functions");
2145 result = error_mark_node;
2149 if (current_function_decl)
2150 error ("invalid use of %<this%> in non-member function");
2152 error ("invalid use of %<this%> at top level");
2153 result = error_mark_node;
2159 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2160 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2161 the TYPE for the type given. If SCOPE is non-NULL, the expression
2162 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2165 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
2167 if (object == error_mark_node || destructor == error_mark_node)
2168 return error_mark_node;
2170 gcc_assert (TYPE_P (destructor));
2172 if (!processing_template_decl)
2174 if (scope == error_mark_node)
2176 error ("invalid qualifying scope in pseudo-destructor name");
2177 return error_mark_node;
2179 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2181 error ("qualified type %qT does not match destructor name ~%qT",
2183 return error_mark_node;
2187 /* [expr.pseudo] says both:
2189 The type designated by the pseudo-destructor-name shall be
2190 the same as the object type.
2194 The cv-unqualified versions of the object type and of the
2195 type designated by the pseudo-destructor-name shall be the
2198 We implement the more generous second sentence, since that is
2199 what most other compilers do. */
2200 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2203 error ("%qE is not of type %qT", object, destructor);
2204 return error_mark_node;
2208 return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
2211 /* Finish an expression of the form CODE EXPR. */
2214 finish_unary_op_expr (enum tree_code code, tree expr)
2216 tree result = build_x_unary_op (code, expr, tf_warning_or_error);
2217 /* Inside a template, build_x_unary_op does not fold the
2218 expression. So check whether the result is folded before
2219 setting TREE_NEGATED_INT. */
2220 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
2221 && TREE_CODE (result) == INTEGER_CST
2222 && !TYPE_UNSIGNED (TREE_TYPE (result))
2223 && INT_CST_LT (result, integer_zero_node))
2225 /* RESULT may be a cached INTEGER_CST, so we must copy it before
2226 setting TREE_NEGATED_INT. */
2227 result = copy_node (result);
2228 TREE_NEGATED_INT (result) = 1;
2230 if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2231 overflow_warning (input_location, result);
2236 /* Finish a compound-literal expression. TYPE is the type to which
2237 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2240 finish_compound_literal (tree type, tree compound_literal)
2242 if (type == error_mark_node)
2243 return error_mark_node;
2245 if (!TYPE_OBJ_P (type))
2247 error ("compound literal of non-object type %qT", type);
2248 return error_mark_node;
2251 if (processing_template_decl)
2253 TREE_TYPE (compound_literal) = type;
2254 /* Mark the expression as a compound literal. */
2255 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2256 return compound_literal;
2259 type = complete_type (type);
2261 if (TYPE_NON_AGGREGATE_CLASS (type))
2263 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2264 everywhere that deals with function arguments would be a pain, so
2265 just wrap it in a TREE_LIST. The parser set a flag so we know
2266 that it came from T{} rather than T({}). */
2267 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2268 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2269 return build_functional_cast (type, compound_literal, tf_error);
2272 if (TREE_CODE (type) == ARRAY_TYPE
2273 && check_array_initializer (NULL_TREE, type, compound_literal))
2274 return error_mark_node;
2275 compound_literal = reshape_init (type, compound_literal);
2276 if (TREE_CODE (type) == ARRAY_TYPE)
2277 cp_complete_array_type (&type, compound_literal, false);
2278 compound_literal = digest_init (type, compound_literal);
2279 return get_target_expr (compound_literal);
2282 /* Return the declaration for the function-name variable indicated by
2286 finish_fname (tree id)
2290 decl = fname_decl (input_location, C_RID_CODE (id), id);
2291 if (processing_template_decl)
2292 decl = DECL_NAME (decl);
2296 /* Finish a translation unit. */
2299 finish_translation_unit (void)
2301 /* In case there were missing closebraces,
2302 get us back to the global binding level. */
2304 while (current_namespace != global_namespace)
2307 /* Do file scope __FUNCTION__ et al. */
2308 finish_fname_decls ();
2311 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2312 Returns the parameter. */
2315 finish_template_type_parm (tree aggr, tree identifier)
2317 if (aggr != class_type_node)
2319 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2320 aggr = class_type_node;
2323 return build_tree_list (aggr, identifier);
2326 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2327 Returns the parameter. */
2330 finish_template_template_parm (tree aggr, tree identifier)
2332 tree decl = build_decl (input_location,
2333 TYPE_DECL, identifier, NULL_TREE);
2334 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2335 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2336 DECL_TEMPLATE_RESULT (tmpl) = decl;
2337 DECL_ARTIFICIAL (decl) = 1;
2338 end_template_decl ();
2340 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2342 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2343 /*is_primary=*/true, /*is_partial=*/false,
2346 return finish_template_type_parm (aggr, tmpl);
2349 /* ARGUMENT is the default-argument value for a template template
2350 parameter. If ARGUMENT is invalid, issue error messages and return
2351 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2354 check_template_template_default_arg (tree argument)
2356 if (TREE_CODE (argument) != TEMPLATE_DECL
2357 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2358 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2360 if (TREE_CODE (argument) == TYPE_DECL)
2361 error ("invalid use of type %qT as a default value for a template "
2362 "template-parameter", TREE_TYPE (argument));
2364 error ("invalid default argument for a template template parameter");
2365 return error_mark_node;
2371 /* Begin a class definition, as indicated by T. */
2374 begin_class_definition (tree t, tree attributes)
2376 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2377 return error_mark_node;
2379 if (processing_template_parmlist)
2381 error ("definition of %q#T inside template parameter list", t);
2382 return error_mark_node;
2385 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2386 are passed the same as decimal scalar types. */
2387 if (TREE_CODE (t) == RECORD_TYPE
2388 && !processing_template_decl)
2390 tree ns = TYPE_CONTEXT (t);
2391 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2392 && DECL_CONTEXT (ns) == std_node
2394 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2396 const char *n = TYPE_NAME_STRING (t);
2397 if ((strcmp (n, "decimal32") == 0)
2398 || (strcmp (n, "decimal64") == 0)
2399 || (strcmp (n, "decimal128") == 0))
2400 TYPE_TRANSPARENT_AGGR (t) = 1;
2404 /* A non-implicit typename comes from code like:
2406 template <typename T> struct A {
2407 template <typename U> struct A<T>::B ...
2409 This is erroneous. */
2410 else if (TREE_CODE (t) == TYPENAME_TYPE)
2412 error ("invalid definition of qualified type %qT", t);
2413 t = error_mark_node;
2416 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2418 t = make_class_type (RECORD_TYPE);
2419 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2422 if (TYPE_BEING_DEFINED (t))
2424 t = make_class_type (TREE_CODE (t));
2425 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2427 maybe_process_partial_specialization (t);
2429 TYPE_BEING_DEFINED (t) = 1;
2431 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
2432 fixup_attribute_variants (t);
2434 if (flag_pack_struct)
2437 TYPE_PACKED (t) = 1;
2438 /* Even though the type is being defined for the first time
2439 here, there might have been a forward declaration, so there
2440 might be cv-qualified variants of T. */
2441 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2442 TYPE_PACKED (v) = 1;
2444 /* Reset the interface data, at the earliest possible
2445 moment, as it might have been set via a class foo;
2447 if (! TYPE_ANONYMOUS_P (t))
2449 struct c_fileinfo *finfo = get_fileinfo (input_filename);
2450 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2451 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2452 (t, finfo->interface_unknown);
2454 reset_specialization();
2456 /* Make a declaration for this class in its own scope. */
2457 build_self_reference ();
2462 /* Finish the member declaration given by DECL. */
2465 finish_member_declaration (tree decl)
2467 if (decl == error_mark_node || decl == NULL_TREE)
2470 if (decl == void_type_node)
2471 /* The COMPONENT was a friend, not a member, and so there's
2472 nothing for us to do. */
2475 /* We should see only one DECL at a time. */
2476 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2478 /* Set up access control for DECL. */
2480 = (current_access_specifier == access_private_node);
2481 TREE_PROTECTED (decl)
2482 = (current_access_specifier == access_protected_node);
2483 if (TREE_CODE (decl) == TEMPLATE_DECL)
2485 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2486 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2489 /* Mark the DECL as a member of the current class. */
2490 DECL_CONTEXT (decl) = current_class_type;
2492 /* Check for bare parameter packs in the member variable declaration. */
2493 if (TREE_CODE (decl) == FIELD_DECL)
2495 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2496 TREE_TYPE (decl) = error_mark_node;
2497 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2498 DECL_ATTRIBUTES (decl) = NULL_TREE;
2503 A C language linkage is ignored for the names of class members
2504 and the member function type of class member functions. */
2505 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2506 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2508 /* Put functions on the TYPE_METHODS list and everything else on the
2509 TYPE_FIELDS list. Note that these are built up in reverse order.
2510 We reverse them (to obtain declaration order) in finish_struct. */
2511 if (TREE_CODE (decl) == FUNCTION_DECL
2512 || DECL_FUNCTION_TEMPLATE_P (decl))
2514 /* We also need to add this function to the
2515 CLASSTYPE_METHOD_VEC. */
2516 if (add_method (current_class_type, decl, NULL_TREE))
2518 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2519 TYPE_METHODS (current_class_type) = decl;
2521 maybe_add_class_template_decl_list (current_class_type, decl,
2525 /* Enter the DECL into the scope of the class. */
2526 else if ((TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2527 || pushdecl_class_level (decl))
2529 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2530 go at the beginning. The reason is that lookup_field_1
2531 searches the list in order, and we want a field name to
2532 override a type name so that the "struct stat hack" will
2533 work. In particular:
2535 struct S { enum E { }; int E } s;
2538 is valid. In addition, the FIELD_DECLs must be maintained in
2539 declaration order so that class layout works as expected.
2540 However, we don't need that order until class layout, so we
2541 save a little time by putting FIELD_DECLs on in reverse order
2542 here, and then reversing them in finish_struct_1. (We could
2543 also keep a pointer to the correct insertion points in the
2546 if (TREE_CODE (decl) == TYPE_DECL)
2547 TYPE_FIELDS (current_class_type)
2548 = chainon (TYPE_FIELDS (current_class_type), decl);
2551 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2552 TYPE_FIELDS (current_class_type) = decl;
2555 maybe_add_class_template_decl_list (current_class_type, decl,
2560 note_decl_for_pch (decl);
2563 /* DECL has been declared while we are building a PCH file. Perform
2564 actions that we might normally undertake lazily, but which can be
2565 performed now so that they do not have to be performed in
2566 translation units which include the PCH file. */
2569 note_decl_for_pch (tree decl)
2571 gcc_assert (pch_file);
2573 /* There's a good chance that we'll have to mangle names at some
2574 point, even if only for emission in debugging information. */
2575 if ((TREE_CODE (decl) == VAR_DECL
2576 || TREE_CODE (decl) == FUNCTION_DECL)
2577 && !processing_template_decl)
2581 /* Finish processing a complete template declaration. The PARMS are
2582 the template parameters. */
2585 finish_template_decl (tree parms)
2588 end_template_decl ();
2590 end_specialization ();
2593 /* Finish processing a template-id (which names a type) of the form
2594 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2595 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2596 the scope of template-id indicated. */
2599 finish_template_type (tree name, tree args, int entering_scope)
2603 decl = lookup_template_class (name, args,
2604 NULL_TREE, NULL_TREE, entering_scope,
2605 tf_warning_or_error | tf_user);
2606 if (decl != error_mark_node)
2607 decl = TYPE_STUB_DECL (decl);
2612 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2613 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2614 BASE_CLASS, or NULL_TREE if an error occurred. The
2615 ACCESS_SPECIFIER is one of
2616 access_{default,public,protected_private}_node. For a virtual base
2617 we set TREE_TYPE. */
2620 finish_base_specifier (tree base, tree access, bool virtual_p)
2624 if (base == error_mark_node)
2626 error ("invalid base-class specification");
2629 else if (! MAYBE_CLASS_TYPE_P (base))
2631 error ("%qT is not a class type", base);
2636 if (cp_type_quals (base) != 0)
2638 error ("base class %qT has cv qualifiers", base);
2639 base = TYPE_MAIN_VARIANT (base);
2641 result = build_tree_list (access, base);
2643 TREE_TYPE (result) = integer_type_node;
2649 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is
2650 what we found when we tried to do the lookup.
2651 LOCATION is the location of the NAME identifier;
2652 The location is used in the error message*/
2655 qualified_name_lookup_error (tree scope, tree name,
2656 tree decl, location_t location)
2658 if (scope == error_mark_node)
2659 ; /* We already complained. */
2660 else if (TYPE_P (scope))
2662 if (!COMPLETE_TYPE_P (scope))
2663 error_at (location, "incomplete type %qT used in nested name specifier",
2665 else if (TREE_CODE (decl) == TREE_LIST)
2667 error_at (location, "reference to %<%T::%D%> is ambiguous",
2669 print_candidates (decl);
2672 error_at (location, "%qD is not a member of %qT", name, scope);
2674 else if (scope != global_namespace)
2675 error_at (location, "%qD is not a member of %qD", name, scope);
2677 error_at (location, "%<::%D%> has not been declared", name);
2680 /* If FNS is a member function, a set of member functions, or a
2681 template-id referring to one or more member functions, return a
2682 BASELINK for FNS, incorporating the current access context.
2683 Otherwise, return FNS unchanged. */
2686 baselink_for_fns (tree fns)
2691 if (BASELINK_P (fns)
2692 || error_operand_p (fns))
2696 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2697 fn = TREE_OPERAND (fn, 0);
2698 fn = get_first_fn (fn);
2699 if (!DECL_FUNCTION_MEMBER_P (fn))
2702 cl = currently_open_derived_class (DECL_CONTEXT (fn));
2704 cl = DECL_CONTEXT (fn);
2705 cl = TYPE_BINFO (cl);
2706 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2709 /* Returns true iff DECL is an automatic variable from a function outside
2713 outer_automatic_var_p (tree decl)
2715 return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
2716 && DECL_FUNCTION_SCOPE_P (decl)
2717 && !TREE_STATIC (decl)
2718 && DECL_CONTEXT (decl) != current_function_decl);
2721 /* Returns true iff DECL is a capture field from a lambda that is not our
2722 immediate context. */
2725 outer_lambda_capture_p (tree decl)
2727 return (TREE_CODE (decl) == FIELD_DECL
2728 && LAMBDA_TYPE_P (DECL_CONTEXT (decl))
2729 && (!current_class_type
2730 || !DERIVED_FROM_P (DECL_CONTEXT (decl), current_class_type)));
2733 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2734 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2735 if non-NULL, is the type or namespace used to explicitly qualify
2736 ID_EXPRESSION. DECL is the entity to which that name has been
2739 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2740 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2741 be set to true if this expression isn't permitted in a
2742 constant-expression, but it is otherwise not set by this function.
2743 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2744 constant-expression, but a non-constant expression is also
2747 DONE is true if this expression is a complete postfix-expression;
2748 it is false if this expression is followed by '->', '[', '(', etc.
2749 ADDRESS_P is true iff this expression is the operand of '&'.
2750 TEMPLATE_P is true iff the qualified-id was of the form
2751 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2752 appears as a template argument.
2754 If an error occurs, and it is the kind of error that might cause
2755 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2756 is the caller's responsibility to issue the message. *ERROR_MSG
2757 will be a string with static storage duration, so the caller need
2760 Return an expression for the entity, after issuing appropriate
2761 diagnostics. This function is also responsible for transforming a
2762 reference to a non-static member into a COMPONENT_REF that makes
2763 the use of "this" explicit.
2765 Upon return, *IDK will be filled in appropriately. */
2767 finish_id_expression (tree id_expression,
2771 bool integral_constant_expression_p,
2772 bool allow_non_integral_constant_expression_p,
2773 bool *non_integral_constant_expression_p,
2777 bool template_arg_p,
2778 const char **error_msg,
2779 location_t location)
2781 /* Initialize the output parameters. */
2782 *idk = CP_ID_KIND_NONE;
2785 if (id_expression == error_mark_node)
2786 return error_mark_node;
2787 /* If we have a template-id, then no further lookup is
2788 required. If the template-id was for a template-class, we
2789 will sometimes have a TYPE_DECL at this point. */
2790 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2791 || TREE_CODE (decl) == TYPE_DECL)
2793 /* Look up the name. */
2796 if (decl == error_mark_node)
2798 /* Name lookup failed. */
2801 || (!dependent_type_p (scope)
2802 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2803 && IDENTIFIER_TYPENAME_P (id_expression)
2804 && dependent_type_p (TREE_TYPE (id_expression))))))
2806 /* If the qualifying type is non-dependent (and the name
2807 does not name a conversion operator to a dependent
2808 type), issue an error. */
2809 qualified_name_lookup_error (scope, id_expression, decl, location);
2810 return error_mark_node;
2814 /* It may be resolved via Koenig lookup. */
2815 *idk = CP_ID_KIND_UNQUALIFIED;
2816 return id_expression;
2819 decl = id_expression;
2821 /* If DECL is a variable that would be out of scope under
2822 ANSI/ISO rules, but in scope in the ARM, name lookup
2823 will succeed. Issue a diagnostic here. */
2825 decl = check_for_out_of_scope_variable (decl);
2827 /* Remember that the name was used in the definition of
2828 the current class so that we can check later to see if
2829 the meaning would have been different after the class
2830 was entirely defined. */
2831 if (!scope && decl != error_mark_node)
2832 maybe_note_name_used_in_class (id_expression, decl);
2834 /* Disallow uses of local variables from containing functions, except
2835 within lambda-expressions. */
2836 if ((outer_automatic_var_p (decl)
2837 || outer_lambda_capture_p (decl))
2838 /* It's not a use (3.2) if we're in an unevaluated context. */
2839 && !cp_unevaluated_operand)
2841 tree context = DECL_CONTEXT (decl);
2842 tree containing_function = current_function_decl;
2843 tree lambda_stack = NULL_TREE;
2844 tree lambda_expr = NULL_TREE;
2845 tree initializer = decl;
2847 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
2848 support for an approach in which a reference to a local
2849 [constant] automatic variable in a nested class or lambda body
2850 would enter the expression as an rvalue, which would reduce
2851 the complexity of the problem"
2853 FIXME update for final resolution of core issue 696. */
2854 if (decl_constant_var_p (decl))
2855 return integral_constant_value (decl);
2857 if (TYPE_P (context))
2859 /* Implicit capture of an explicit capture. */
2860 context = lambda_function (context);
2861 initializer = thisify_lambda_field (decl);
2864 /* If we are in a lambda function, we can move out until we hit
2866 2. a non-lambda function, or
2867 3. a non-default capturing lambda function. */
2868 while (context != containing_function
2869 && LAMBDA_FUNCTION_P (containing_function))
2871 lambda_expr = CLASSTYPE_LAMBDA_EXPR
2872 (DECL_CONTEXT (containing_function));
2874 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
2878 lambda_stack = tree_cons (NULL_TREE,
2883 = decl_function_context (containing_function);
2886 if (context == containing_function)
2888 decl = add_default_capture (lambda_stack,
2889 /*id=*/DECL_NAME (decl),
2892 else if (lambda_expr)
2894 error ("%qD is not captured", decl);
2895 return error_mark_node;
2899 error (TREE_CODE (decl) == VAR_DECL
2900 ? "use of %<auto%> variable from containing function"
2901 : "use of parameter from containing function");
2902 error (" %q+#D declared here", decl);
2903 return error_mark_node;
2907 /* Also disallow uses of function parameters outside the function
2908 body, except inside an unevaluated context (i.e. decltype). */
2909 if (TREE_CODE (decl) == PARM_DECL
2910 && DECL_CONTEXT (decl) == NULL_TREE
2911 && !cp_unevaluated_operand)
2913 error ("use of parameter %qD outside function body", decl);
2914 return error_mark_node;
2918 /* If we didn't find anything, or what we found was a type,
2919 then this wasn't really an id-expression. */
2920 if (TREE_CODE (decl) == TEMPLATE_DECL
2921 && !DECL_FUNCTION_TEMPLATE_P (decl))
2923 *error_msg = "missing template arguments";
2924 return error_mark_node;
2926 else if (TREE_CODE (decl) == TYPE_DECL
2927 || TREE_CODE (decl) == NAMESPACE_DECL)
2929 *error_msg = "expected primary-expression";
2930 return error_mark_node;
2933 /* If the name resolved to a template parameter, there is no
2934 need to look it up again later. */
2935 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
2936 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2940 *idk = CP_ID_KIND_NONE;
2941 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2942 decl = TEMPLATE_PARM_DECL (decl);
2943 r = convert_from_reference (DECL_INITIAL (decl));
2945 if (integral_constant_expression_p
2946 && !dependent_type_p (TREE_TYPE (decl))
2947 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
2949 if (!allow_non_integral_constant_expression_p)
2950 error ("template parameter %qD of type %qT is not allowed in "
2951 "an integral constant expression because it is not of "
2952 "integral or enumeration type", decl, TREE_TYPE (decl));
2953 *non_integral_constant_expression_p = true;
2957 /* Similarly, we resolve enumeration constants to their
2958 underlying values. */
2959 else if (TREE_CODE (decl) == CONST_DECL)
2961 *idk = CP_ID_KIND_NONE;
2962 if (!processing_template_decl)
2964 used_types_insert (TREE_TYPE (decl));
2965 return DECL_INITIAL (decl);
2973 /* If the declaration was explicitly qualified indicate
2974 that. The semantics of `A::f(3)' are different than
2975 `f(3)' if `f' is virtual. */
2977 ? CP_ID_KIND_QUALIFIED
2978 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2979 ? CP_ID_KIND_TEMPLATE_ID
2980 : CP_ID_KIND_UNQUALIFIED));
2985 An id-expression is type-dependent if it contains an
2986 identifier that was declared with a dependent type.
2988 The standard is not very specific about an id-expression that
2989 names a set of overloaded functions. What if some of them
2990 have dependent types and some of them do not? Presumably,
2991 such a name should be treated as a dependent name. */
2992 /* Assume the name is not dependent. */
2993 dependent_p = false;
2994 if (!processing_template_decl)
2995 /* No names are dependent outside a template. */
2997 /* A template-id where the name of the template was not resolved
2998 is definitely dependent. */
2999 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3000 && (TREE_CODE (TREE_OPERAND (decl, 0))
3001 == IDENTIFIER_NODE))
3003 /* For anything except an overloaded function, just check its
3005 else if (!is_overloaded_fn (decl))
3007 = dependent_type_p (TREE_TYPE (decl));
3008 /* For a set of overloaded functions, check each of the
3014 if (BASELINK_P (fns))
3015 fns = BASELINK_FUNCTIONS (fns);
3017 /* For a template-id, check to see if the template
3018 arguments are dependent. */
3019 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3021 tree args = TREE_OPERAND (fns, 1);
3022 dependent_p = any_dependent_template_arguments_p (args);
3023 /* The functions are those referred to by the
3025 fns = TREE_OPERAND (fns, 0);
3028 /* If there are no dependent template arguments, go through
3029 the overloaded functions. */
3030 while (fns && !dependent_p)
3032 tree fn = OVL_CURRENT (fns);
3034 /* Member functions of dependent classes are
3036 if (TREE_CODE (fn) == FUNCTION_DECL
3037 && type_dependent_expression_p (fn))
3039 else if (TREE_CODE (fn) == TEMPLATE_DECL
3040 && dependent_template_p (fn))
3043 fns = OVL_NEXT (fns);
3047 /* If the name was dependent on a template parameter, we will
3048 resolve the name at instantiation time. */
3051 /* Create a SCOPE_REF for qualified names, if the scope is
3057 if (address_p && done)
3058 decl = finish_qualified_id_expr (scope, decl,
3064 tree type = NULL_TREE;
3065 if (DECL_P (decl) && !dependent_scope_p (scope))
3066 type = TREE_TYPE (decl);
3067 decl = build_qualified_name (type,
3073 if (TREE_TYPE (decl))
3074 decl = convert_from_reference (decl);
3077 /* A TEMPLATE_ID already contains all the information we
3079 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3080 return id_expression;
3081 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3082 /* If we found a variable, then name lookup during the
3083 instantiation will always resolve to the same VAR_DECL
3084 (or an instantiation thereof). */
3085 if (TREE_CODE (decl) == VAR_DECL
3086 || TREE_CODE (decl) == PARM_DECL)
3087 return convert_from_reference (decl);
3088 /* The same is true for FIELD_DECL, but we also need to
3089 make sure that the syntax is correct. */
3090 else if (TREE_CODE (decl) == FIELD_DECL)
3092 /* Since SCOPE is NULL here, this is an unqualified name.
3093 Access checking has been performed during name lookup
3094 already. Turn off checking to avoid duplicate errors. */
3095 push_deferring_access_checks (dk_no_check);
3096 decl = finish_non_static_data_member
3098 /*qualifying_scope=*/NULL_TREE);
3099 pop_deferring_access_checks ();
3102 return id_expression;
3105 if (TREE_CODE (decl) == NAMESPACE_DECL)
3107 error ("use of namespace %qD as expression", decl);
3108 return error_mark_node;
3110 else if (DECL_CLASS_TEMPLATE_P (decl))
3112 error ("use of class template %qT as expression", decl);
3113 return error_mark_node;
3115 else if (TREE_CODE (decl) == TREE_LIST)
3117 /* Ambiguous reference to base members. */
3118 error ("request for member %qD is ambiguous in "
3119 "multiple inheritance lattice", id_expression);
3120 print_candidates (decl);
3121 return error_mark_node;
3124 /* Mark variable-like entities as used. Functions are similarly
3125 marked either below or after overload resolution. */
3126 if (TREE_CODE (decl) == VAR_DECL
3127 || TREE_CODE (decl) == PARM_DECL
3128 || TREE_CODE (decl) == RESULT_DECL)
3131 /* Only certain kinds of names are allowed in constant
3132 expression. Enumerators and template parameters have already
3133 been handled above. */
3134 if (integral_constant_expression_p
3135 && ! decl_constant_var_p (decl)
3136 && ! builtin_valid_in_constant_expr_p (decl))
3138 if (!allow_non_integral_constant_expression_p)
3140 error ("%qD cannot appear in a constant-expression", decl);
3141 return error_mark_node;
3143 *non_integral_constant_expression_p = true;
3148 decl = (adjust_result_of_qualified_name_lookup
3149 (decl, scope, current_class_type));
3151 if (TREE_CODE (decl) == FUNCTION_DECL)
3154 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3155 decl = finish_qualified_id_expr (scope,
3163 tree r = convert_from_reference (decl);
3165 /* In a template, return a SCOPE_REF for most qualified-ids
3166 so that we can check access at instantiation time. But if
3167 we're looking at a member of the current instantiation, we
3168 know we have access and building up the SCOPE_REF confuses
3169 non-type template argument handling. */
3170 if (processing_template_decl && TYPE_P (scope)
3171 && !currently_open_class (scope))
3172 r = build_qualified_name (TREE_TYPE (r),
3178 else if (TREE_CODE (decl) == FIELD_DECL)
3180 /* Since SCOPE is NULL here, this is an unqualified name.
3181 Access checking has been performed during name lookup
3182 already. Turn off checking to avoid duplicate errors. */
3183 push_deferring_access_checks (dk_no_check);
3184 decl = finish_non_static_data_member (decl, NULL_TREE,
3185 /*qualifying_scope=*/NULL_TREE);
3186 pop_deferring_access_checks ();
3188 else if (is_overloaded_fn (decl))
3192 first_fn = get_first_fn (decl);
3193 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3194 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3196 if (!really_overloaded_fn (decl))
3197 mark_used (first_fn);
3200 && TREE_CODE (first_fn) == FUNCTION_DECL
3201 && DECL_FUNCTION_MEMBER_P (first_fn)
3202 && !shared_member_p (decl))
3204 /* A set of member functions. */
3205 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3206 return finish_class_member_access_expr (decl, id_expression,
3207 /*template_p=*/false,
3208 tf_warning_or_error);
3211 decl = baselink_for_fns (decl);
3215 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3216 && DECL_CLASS_SCOPE_P (decl))
3218 tree context = context_for_name_lookup (decl);
3219 if (context != current_class_type)
3221 tree path = currently_open_derived_class (context);
3222 perform_or_defer_access_check (TYPE_BINFO (path),
3227 decl = convert_from_reference (decl);
3231 if (TREE_DEPRECATED (decl))
3232 warn_deprecated_use (decl, NULL_TREE);
3237 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3238 use as a type-specifier. */
3241 finish_typeof (tree expr)
3245 if (type_dependent_expression_p (expr))
3247 type = cxx_make_type (TYPEOF_TYPE);
3248 TYPEOF_TYPE_EXPR (type) = expr;
3249 SET_TYPE_STRUCTURAL_EQUALITY (type);
3254 expr = mark_type_use (expr);
3256 type = unlowered_expr_type (expr);
3258 if (!type || type == unknown_type_node)
3260 error ("type of %qE is unknown", expr);
3261 return error_mark_node;
3267 /* Perform C++-specific checks for __builtin_offsetof before calling
3271 finish_offsetof (tree expr)
3273 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3275 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3276 TREE_OPERAND (expr, 2));
3277 return error_mark_node;
3279 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3280 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3281 || TREE_TYPE (expr) == unknown_type_node)
3283 if (TREE_CODE (expr) == COMPONENT_REF
3284 || TREE_CODE (expr) == COMPOUND_EXPR)
3285 expr = TREE_OPERAND (expr, 1);
3286 error ("cannot apply %<offsetof%> to member function %qD", expr);
3287 return error_mark_node;
3289 if (TREE_CODE (expr) == INDIRECT_REF && REFERENCE_REF_P (expr))
3290 expr = TREE_OPERAND (expr, 0);
3291 return fold_offsetof (expr, NULL_TREE);
3294 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3295 function is broken out from the above for the benefit of the tree-ssa
3299 simplify_aggr_init_expr (tree *tp)
3301 tree aggr_init_expr = *tp;
3303 /* Form an appropriate CALL_EXPR. */
3304 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3305 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3306 tree type = TREE_TYPE (slot);
3309 enum style_t { ctor, arg, pcc } style;
3311 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3313 #ifdef PCC_STATIC_STRUCT_RETURN
3319 gcc_assert (TREE_ADDRESSABLE (type));
3323 call_expr = build_call_array_loc (input_location,
3324 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3326 aggr_init_expr_nargs (aggr_init_expr),
3327 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3328 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3332 /* Replace the first argument to the ctor with the address of the
3334 cxx_mark_addressable (slot);
3335 CALL_EXPR_ARG (call_expr, 0) =
3336 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3338 else if (style == arg)
3340 /* Just mark it addressable here, and leave the rest to
3341 expand_call{,_inline}. */
3342 cxx_mark_addressable (slot);
3343 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3344 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3346 else if (style == pcc)
3348 /* If we're using the non-reentrant PCC calling convention, then we
3349 need to copy the returned value out of the static buffer into the
3351 push_deferring_access_checks (dk_no_check);
3352 call_expr = build_aggr_init (slot, call_expr,
3353 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3354 tf_warning_or_error);
3355 pop_deferring_access_checks ();
3356 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3359 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3361 tree init = build_zero_init (type, NULL_TREE,
3362 /*static_storage_p=*/false);
3363 init = build2 (INIT_EXPR, void_type_node, slot, init);
3364 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3371 /* Emit all thunks to FN that should be emitted when FN is emitted. */
3374 emit_associated_thunks (tree fn)
3376 /* When we use vcall offsets, we emit thunks with the virtual
3377 functions to which they thunk. The whole point of vcall offsets
3378 is so that you can know statically the entire set of thunks that
3379 will ever be needed for a given virtual function, thereby
3380 enabling you to output all the thunks with the function itself. */
3381 if (DECL_VIRTUAL_P (fn)
3382 /* Do not emit thunks for extern template instantiations. */
3383 && ! DECL_REALLY_EXTERN (fn))
3387 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3389 if (!THUNK_ALIAS (thunk))
3391 use_thunk (thunk, /*emit_p=*/1);
3392 if (DECL_RESULT_THUNK_P (thunk))
3396 for (probe = DECL_THUNKS (thunk);
3397 probe; probe = DECL_CHAIN (probe))
3398 use_thunk (probe, /*emit_p=*/1);
3402 gcc_assert (!DECL_THUNKS (thunk));
3407 /* Generate RTL for FN. */
3410 expand_or_defer_fn_1 (tree fn)
3412 /* When the parser calls us after finishing the body of a template
3413 function, we don't really want to expand the body. */
3414 if (processing_template_decl)
3416 /* Normally, collection only occurs in rest_of_compilation. So,
3417 if we don't collect here, we never collect junk generated
3418 during the processing of templates until we hit a
3419 non-template function. It's not safe to do this inside a
3420 nested class, though, as the parser may have local state that
3421 is not a GC root. */
3422 if (!function_depth)
3427 gcc_assert (DECL_SAVED_TREE (fn));
3429 /* If this is a constructor or destructor body, we have to clone
3431 if (maybe_clone_body (fn))
3433 /* We don't want to process FN again, so pretend we've written
3434 it out, even though we haven't. */
3435 TREE_ASM_WRITTEN (fn) = 1;
3436 DECL_SAVED_TREE (fn) = NULL_TREE;
3440 /* We make a decision about linkage for these functions at the end
3441 of the compilation. Until that point, we do not want the back
3442 end to output them -- but we do want it to see the bodies of
3443 these functions so that it can inline them as appropriate. */
3444 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
3446 if (DECL_INTERFACE_KNOWN (fn))
3447 /* We've already made a decision as to how this function will
3451 DECL_EXTERNAL (fn) = 1;
3452 DECL_NOT_REALLY_EXTERN (fn) = 1;
3453 note_vague_linkage_fn (fn);
3454 /* A non-template inline function with external linkage will
3455 always be COMDAT. As we must eventually determine the
3456 linkage of all functions, and as that causes writes to
3457 the data mapped in from the PCH file, it's advantageous
3458 to mark the functions at this point. */
3459 if (!DECL_IMPLICIT_INSTANTIATION (fn))
3461 /* This function must have external linkage, as
3462 otherwise DECL_INTERFACE_KNOWN would have been
3464 gcc_assert (TREE_PUBLIC (fn));
3465 comdat_linkage (fn);
3466 DECL_INTERFACE_KNOWN (fn) = 1;
3470 import_export_decl (fn);
3472 /* If the user wants us to keep all inline functions, then mark
3473 this function as needed so that finish_file will make sure to
3474 output it later. Similarly, all dllexport'd functions must
3475 be emitted; there may be callers in other DLLs. */
3476 if ((flag_keep_inline_functions
3477 && DECL_DECLARED_INLINE_P (fn)
3478 && !DECL_REALLY_EXTERN (fn))
3479 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))
3483 /* There's no reason to do any of the work here if we're only doing
3484 semantic analysis; this code just generates RTL. */
3485 if (flag_syntax_only)
3492 expand_or_defer_fn (tree fn)
3494 if (expand_or_defer_fn_1 (fn))
3498 /* Expand or defer, at the whim of the compilation unit manager. */
3499 cgraph_finalize_function (fn, function_depth > 1);
3500 emit_associated_thunks (fn);
3513 /* Helper function for walk_tree, used by finalize_nrv below. */
3516 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
3518 struct nrv_data *dp = (struct nrv_data *)data;
3521 /* No need to walk into types. There wouldn't be any need to walk into
3522 non-statements, except that we have to consider STMT_EXPRs. */
3525 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
3526 but differs from using NULL_TREE in that it indicates that we care
3527 about the value of the RESULT_DECL. */
3528 else if (TREE_CODE (*tp) == RETURN_EXPR)
3529 TREE_OPERAND (*tp, 0) = dp->result;
3530 /* Change all cleanups for the NRV to only run when an exception is
3532 else if (TREE_CODE (*tp) == CLEANUP_STMT
3533 && CLEANUP_DECL (*tp) == dp->var)
3534 CLEANUP_EH_ONLY (*tp) = 1;
3535 /* Replace the DECL_EXPR for the NRV with an initialization of the
3536 RESULT_DECL, if needed. */
3537 else if (TREE_CODE (*tp) == DECL_EXPR
3538 && DECL_EXPR_DECL (*tp) == dp->var)
3541 if (DECL_INITIAL (dp->var)
3542 && DECL_INITIAL (dp->var) != error_mark_node)
3543 init = build2 (INIT_EXPR, void_type_node, dp->result,
3544 DECL_INITIAL (dp->var));
3546 init = build_empty_stmt (EXPR_LOCATION (*tp));
3547 DECL_INITIAL (dp->var) = NULL_TREE;
3548 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
3551 /* And replace all uses of the NRV with the RESULT_DECL. */
3552 else if (*tp == dp->var)
3555 /* Avoid walking into the same tree more than once. Unfortunately, we
3556 can't just use walk_tree_without duplicates because it would only call
3557 us for the first occurrence of dp->var in the function body. */
3558 slot = htab_find_slot (dp->visited, *tp, INSERT);
3564 /* Keep iterating. */
3568 /* Called from finish_function to implement the named return value
3569 optimization by overriding all the RETURN_EXPRs and pertinent
3570 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3571 RESULT_DECL for the function. */
3574 finalize_nrv (tree *tp, tree var, tree result)
3576 struct nrv_data data;
3578 /* Copy name from VAR to RESULT. */
3579 DECL_NAME (result) = DECL_NAME (var);
3580 /* Don't forget that we take its address. */
3581 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3582 /* Finally set DECL_VALUE_EXPR to avoid assigning
3583 a stack slot at -O0 for the original var and debug info
3584 uses RESULT location for VAR. */
3585 SET_DECL_VALUE_EXPR (var, result);
3586 DECL_HAS_VALUE_EXPR_P (var) = 1;
3589 data.result = result;
3590 data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3591 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
3592 htab_delete (data.visited);
3595 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
3598 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
3599 bool need_copy_ctor, bool need_copy_assignment)
3601 int save_errorcount = errorcount;
3604 /* Always allocate 3 elements for simplicity. These are the
3605 function decls for the ctor, dtor, and assignment op.
3606 This layout is known to the three lang hooks,
3607 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
3608 and cxx_omp_clause_assign_op. */
3609 info = make_tree_vec (3);
3610 CP_OMP_CLAUSE_INFO (c) = info;
3612 if (need_default_ctor || need_copy_ctor)
3614 if (need_default_ctor)
3615 t = get_default_ctor (type);
3617 t = get_copy_ctor (type);
3619 if (t && !trivial_fn_p (t))
3620 TREE_VEC_ELT (info, 0) = t;
3623 if ((need_default_ctor || need_copy_ctor)
3624 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3625 TREE_VEC_ELT (info, 1) = get_dtor (type);
3627 if (need_copy_assignment)
3629 t = get_copy_assign (type);
3631 if (t && !trivial_fn_p (t))
3632 TREE_VEC_ELT (info, 2) = t;
3635 return errorcount != save_errorcount;
3638 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
3639 Remove any elements from the list that are invalid. */
3642 finish_omp_clauses (tree clauses)
3644 bitmap_head generic_head, firstprivate_head, lastprivate_head;
3645 tree c, t, *pc = &clauses;
3648 bitmap_obstack_initialize (NULL);
3649 bitmap_initialize (&generic_head, &bitmap_default_obstack);
3650 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
3651 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
3653 for (pc = &clauses, c = clauses; c ; c = *pc)
3655 bool remove = false;
3657 switch (OMP_CLAUSE_CODE (c))
3659 case OMP_CLAUSE_SHARED:
3661 goto check_dup_generic;
3662 case OMP_CLAUSE_PRIVATE:
3664 goto check_dup_generic;
3665 case OMP_CLAUSE_REDUCTION:
3667 goto check_dup_generic;
3668 case OMP_CLAUSE_COPYPRIVATE:
3669 name = "copyprivate";
3670 goto check_dup_generic;
3671 case OMP_CLAUSE_COPYIN:
3673 goto check_dup_generic;
3675 t = OMP_CLAUSE_DECL (c);
3676 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
3678 if (processing_template_decl)
3681 error ("%qD is not a variable in clause %qs", t, name);
3683 error ("%qE is not a variable in clause %qs", t, name);
3686 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
3687 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
3688 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
3690 error ("%qD appears more than once in data clauses", t);
3694 bitmap_set_bit (&generic_head, DECL_UID (t));
3697 case OMP_CLAUSE_FIRSTPRIVATE:
3698 t = OMP_CLAUSE_DECL (c);
3699 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
3701 if (processing_template_decl)
3704 error ("%qD is not a variable in clause %<firstprivate%>", t);
3706 error ("%qE is not a variable in clause %<firstprivate%>", t);
3709 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
3710 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
3712 error ("%qD appears more than once in data clauses", t);
3716 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
3719 case OMP_CLAUSE_LASTPRIVATE:
3720 t = OMP_CLAUSE_DECL (c);
3721 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
3723 if (processing_template_decl)
3726 error ("%qD is not a variable in clause %<lastprivate%>", t);
3728 error ("%qE is not a variable in clause %<lastprivate%>", t);
3731 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
3732 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
3734 error ("%qD appears more than once in data clauses", t);
3738 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
3742 t = OMP_CLAUSE_IF_EXPR (c);
3743 t = maybe_convert_cond (t);
3744 if (t == error_mark_node)
3746 OMP_CLAUSE_IF_EXPR (c) = t;
3749 case OMP_CLAUSE_NUM_THREADS:
3750 t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
3751 if (t == error_mark_node)
3753 else if (!type_dependent_expression_p (t)
3754 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
3756 error ("num_threads expression must be integral");
3761 case OMP_CLAUSE_SCHEDULE:
3762 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
3765 else if (t == error_mark_node)
3767 else if (!type_dependent_expression_p (t)
3768 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
3770 error ("schedule chunk size expression must be integral");
3775 case OMP_CLAUSE_NOWAIT:
3776 case OMP_CLAUSE_ORDERED:
3777 case OMP_CLAUSE_DEFAULT:
3778 case OMP_CLAUSE_UNTIED:
3779 case OMP_CLAUSE_COLLAPSE:
3787 *pc = OMP_CLAUSE_CHAIN (c);
3789 pc = &OMP_CLAUSE_CHAIN (c);
3792 for (pc = &clauses, c = clauses; c ; c = *pc)
3794 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
3795 bool remove = false;
3796 bool need_complete_non_reference = false;
3797 bool need_default_ctor = false;
3798 bool need_copy_ctor = false;
3799 bool need_copy_assignment = false;
3800 bool need_implicitly_determined = false;
3801 tree type, inner_type;
3805 case OMP_CLAUSE_SHARED:
3807 need_implicitly_determined = true;
3809 case OMP_CLAUSE_PRIVATE:
3811 need_complete_non_reference = true;
3812 need_default_ctor = true;
3813 need_implicitly_determined = true;
3815 case OMP_CLAUSE_FIRSTPRIVATE:
3816 name = "firstprivate";
3817 need_complete_non_reference = true;
3818 need_copy_ctor = true;
3819 need_implicitly_determined = true;
3821 case OMP_CLAUSE_LASTPRIVATE:
3822 name = "lastprivate";
3823 need_complete_non_reference = true;
3824 need_copy_assignment = true;
3825 need_implicitly_determined = true;
3827 case OMP_CLAUSE_REDUCTION:
3829 need_implicitly_determined = true;
3831 case OMP_CLAUSE_COPYPRIVATE:
3832 name = "copyprivate";
3833 need_copy_assignment = true;
3835 case OMP_CLAUSE_COPYIN:
3837 need_copy_assignment = true;
3840 pc = &OMP_CLAUSE_CHAIN (c);
3844 t = OMP_CLAUSE_DECL (c);
3845 if (processing_template_decl
3846 && TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
3848 pc = &OMP_CLAUSE_CHAIN (c);
3854 case OMP_CLAUSE_LASTPRIVATE:
3855 if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
3856 need_default_ctor = true;
3859 case OMP_CLAUSE_REDUCTION:
3860 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
3861 || POINTER_TYPE_P (TREE_TYPE (t)))
3863 error ("%qE has invalid type for %<reduction%>", t);
3866 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
3868 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
3876 error ("%qE has invalid type for %<reduction(%s)%>",
3877 t, operator_name_info[r_code].name);
3883 case OMP_CLAUSE_COPYIN:
3884 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
3886 error ("%qE must be %<threadprivate%> for %<copyin%>", t);
3895 if (need_complete_non_reference)
3897 t = require_complete_type (t);
3898 if (t == error_mark_node)
3900 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
3902 error ("%qE has reference type for %qs", t, name);
3906 if (need_implicitly_determined)
3908 const char *share_name = NULL;
3910 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
3911 share_name = "threadprivate";
3912 else switch (cxx_omp_predetermined_sharing (t))
3914 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
3916 case OMP_CLAUSE_DEFAULT_SHARED:
3917 share_name = "shared";
3919 case OMP_CLAUSE_DEFAULT_PRIVATE:
3920 share_name = "private";
3927 error ("%qE is predetermined %qs for %qs",
3928 t, share_name, name);
3933 /* We're interested in the base element, not arrays. */
3934 inner_type = type = TREE_TYPE (t);
3935 while (TREE_CODE (inner_type) == ARRAY_TYPE)
3936 inner_type = TREE_TYPE (inner_type);
3938 /* Check for special function availability by building a call to one.
3939 Save the results, because later we won't be in the right context
3940 for making these queries. */
3941 if (CLASS_TYPE_P (inner_type)
3942 && (need_default_ctor || need_copy_ctor || need_copy_assignment)
3943 && !type_dependent_expression_p (t)
3944 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
3945 need_copy_ctor, need_copy_assignment))
3949 *pc = OMP_CLAUSE_CHAIN (c);
3951 pc = &OMP_CLAUSE_CHAIN (c);
3954 bitmap_obstack_release (NULL);
3958 /* For all variables in the tree_list VARS, mark them as thread local. */
3961 finish_omp_threadprivate (tree vars)
3965 /* Mark every variable in VARS to be assigned thread local storage. */
3966 for (t = vars; t; t = TREE_CHAIN (t))
3968 tree v = TREE_PURPOSE (t);
3970 if (error_operand_p (v))
3972 else if (TREE_CODE (v) != VAR_DECL)
3973 error ("%<threadprivate%> %qD is not file, namespace "
3974 "or block scope variable", v);
3975 /* If V had already been marked threadprivate, it doesn't matter
3976 whether it had been used prior to this point. */
3977 else if (TREE_USED (v)
3978 && (DECL_LANG_SPECIFIC (v) == NULL
3979 || !CP_DECL_THREADPRIVATE_P (v)))
3980 error ("%qE declared %<threadprivate%> after first use", v);
3981 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
3982 error ("automatic variable %qE cannot be %<threadprivate%>", v);
3983 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
3984 error ("%<threadprivate%> %qE has incomplete type", v);
3985 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
3986 && CP_DECL_CONTEXT (v) != current_class_type)
3987 error ("%<threadprivate%> %qE directive not "
3988 "in %qT definition", v, CP_DECL_CONTEXT (v));
3991 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
3992 if (DECL_LANG_SPECIFIC (v) == NULL)
3994 retrofit_lang_decl (v);
3996 /* Make sure that DECL_DISCRIMINATOR_P continues to be true
3997 after the allocation of the lang_decl structure. */
3998 if (DECL_DISCRIMINATOR_P (v))
3999 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
4002 if (! DECL_THREAD_LOCAL_P (v))
4004 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
4005 /* If rtl has been already set for this var, call
4006 make_decl_rtl once again, so that encode_section_info
4007 has a chance to look at the new decl flags. */
4008 if (DECL_RTL_SET_P (v))
4011 CP_DECL_THREADPRIVATE_P (v) = 1;
4016 /* Build an OpenMP structured block. */
4019 begin_omp_structured_block (void)
4021 return do_pushlevel (sk_omp);
4025 finish_omp_structured_block (tree block)
4027 return do_poplevel (block);
4030 /* Similarly, except force the retention of the BLOCK. */
4033 begin_omp_parallel (void)
4035 keep_next_level (true);
4036 return begin_omp_structured_block ();
4040 finish_omp_parallel (tree clauses, tree body)
4044 body = finish_omp_structured_block (body);
4046 stmt = make_node (OMP_PARALLEL);
4047 TREE_TYPE (stmt) = void_type_node;
4048 OMP_PARALLEL_CLAUSES (stmt) = clauses;
4049 OMP_PARALLEL_BODY (stmt) = body;
4051 return add_stmt (stmt);
4055 begin_omp_task (void)
4057 keep_next_level (true);
4058 return begin_omp_structured_block ();
4062 finish_omp_task (tree clauses, tree body)
4066 body = finish_omp_structured_block (body);
4068 stmt = make_node (OMP_TASK);
4069 TREE_TYPE (stmt) = void_type_node;
4070 OMP_TASK_CLAUSES (stmt) = clauses;
4071 OMP_TASK_BODY (stmt) = body;
4073 return add_stmt (stmt);
4076 /* Helper function for finish_omp_for. Convert Ith random access iterator
4077 into integral iterator. Return FALSE if successful. */
4080 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
4081 tree condv, tree incrv, tree *body,
4082 tree *pre_body, tree clauses)
4084 tree diff, iter_init, iter_incr = NULL, last;
4085 tree incr_var = NULL, orig_pre_body, orig_body, c;
4086 tree decl = TREE_VEC_ELT (declv, i);
4087 tree init = TREE_VEC_ELT (initv, i);
4088 tree cond = TREE_VEC_ELT (condv, i);
4089 tree incr = TREE_VEC_ELT (incrv, i);
4091 location_t elocus = locus;
4093 if (init && EXPR_HAS_LOCATION (init))
4094 elocus = EXPR_LOCATION (init);
4096 switch (TREE_CODE (cond))
4102 if (TREE_OPERAND (cond, 1) == iter)
4103 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
4104 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
4105 if (TREE_OPERAND (cond, 0) != iter)
4106 cond = error_mark_node;
4109 tree tem = build_x_binary_op (TREE_CODE (cond), iter, ERROR_MARK,
4110 TREE_OPERAND (cond, 1), ERROR_MARK,
4111 NULL, tf_warning_or_error);
4112 if (error_operand_p (tem))
4117 cond = error_mark_node;
4120 if (cond == error_mark_node)
4122 error_at (elocus, "invalid controlling predicate");
4125 diff = build_x_binary_op (MINUS_EXPR, TREE_OPERAND (cond, 1),
4126 ERROR_MARK, iter, ERROR_MARK, NULL,
4127 tf_warning_or_error);
4128 if (error_operand_p (diff))
4130 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
4132 error_at (elocus, "difference between %qE and %qD does not have integer type",
4133 TREE_OPERAND (cond, 1), iter);
4137 switch (TREE_CODE (incr))
4139 case PREINCREMENT_EXPR:
4140 case PREDECREMENT_EXPR:
4141 case POSTINCREMENT_EXPR:
4142 case POSTDECREMENT_EXPR:
4143 if (TREE_OPERAND (incr, 0) != iter)
4145 incr = error_mark_node;
4148 iter_incr = build_x_unary_op (TREE_CODE (incr), iter,
4149 tf_warning_or_error);
4150 if (error_operand_p (iter_incr))
4152 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
4153 || TREE_CODE (incr) == POSTINCREMENT_EXPR)