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, 2011, 2012 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"
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);
59 /* Deferred Access Checking Overview
60 ---------------------------------
62 Most C++ expressions and declarations require access checking
63 to be performed during parsing. However, in several cases,
64 this has to be treated differently.
66 For member declarations, access checking has to be deferred
67 until more information about the declaration is known. For
79 When we are parsing the function return type `A::X', we don't
80 really know if this is allowed until we parse the function name.
82 Furthermore, some contexts require that access checking is
83 never performed at all. These include class heads, and template
86 Typical use of access checking functions is described here:
88 1. When we enter a context that requires certain access checking
89 mode, the function `push_deferring_access_checks' is called with
90 DEFERRING argument specifying the desired mode. Access checking
91 may be performed immediately (dk_no_deferred), deferred
92 (dk_deferred), or not performed (dk_no_check).
94 2. When a declaration such as a type, or a variable, is encountered,
95 the function `perform_or_defer_access_check' is called. It
96 maintains a VEC of all deferred checks.
98 3. The global `current_class_type' or `current_function_decl' is then
99 setup by the parser. `enforce_access' relies on these information
102 4. Upon exiting the context mentioned in step 1,
103 `perform_deferred_access_checks' is called to check all declaration
104 stored in the VEC. `pop_deferring_access_checks' is then
105 called to restore the previous access checking mode.
107 In case of parsing error, we simply call `pop_deferring_access_checks'
108 without `perform_deferred_access_checks'. */
110 typedef struct GTY(()) deferred_access {
111 /* A VEC representing name-lookups for which we have deferred
112 checking access controls. We cannot check the accessibility of
113 names used in a decl-specifier-seq until we know what is being
114 declared because code like:
121 A::B* A::f() { return 0; }
123 is valid, even though `A::B' is not generally accessible. */
124 VEC (deferred_access_check,gc)* GTY(()) deferred_access_checks;
126 /* The current mode of access checks. */
127 enum deferring_kind deferring_access_checks_kind;
130 DEF_VEC_O (deferred_access);
131 DEF_VEC_ALLOC_O (deferred_access,gc);
133 /* Data for deferred access checking. */
134 static GTY(()) VEC(deferred_access,gc) *deferred_access_stack;
135 static GTY(()) unsigned deferred_access_no_check;
137 /* Save the current deferred access states and start deferred
138 access checking iff DEFER_P is true. */
141 push_deferring_access_checks (deferring_kind deferring)
143 /* For context like template instantiation, access checking
144 disabling applies to all nested context. */
145 if (deferred_access_no_check || deferring == dk_no_check)
146 deferred_access_no_check++;
149 deferred_access *ptr;
151 ptr = VEC_safe_push (deferred_access, gc, deferred_access_stack, NULL);
152 ptr->deferred_access_checks = NULL;
153 ptr->deferring_access_checks_kind = deferring;
157 /* Resume deferring access checks again after we stopped doing
161 resume_deferring_access_checks (void)
163 if (!deferred_access_no_check)
164 VEC_last (deferred_access, deferred_access_stack)
165 ->deferring_access_checks_kind = dk_deferred;
168 /* Stop deferring access checks. */
171 stop_deferring_access_checks (void)
173 if (!deferred_access_no_check)
174 VEC_last (deferred_access, deferred_access_stack)
175 ->deferring_access_checks_kind = dk_no_deferred;
178 /* Discard the current deferred access checks and restore the
182 pop_deferring_access_checks (void)
184 if (deferred_access_no_check)
185 deferred_access_no_check--;
187 VEC_pop (deferred_access, deferred_access_stack);
190 /* Returns a TREE_LIST representing the deferred checks.
191 The TREE_PURPOSE of each node is the type through which the
192 access occurred; the TREE_VALUE is the declaration named.
195 VEC (deferred_access_check,gc)*
196 get_deferred_access_checks (void)
198 if (deferred_access_no_check)
201 return (VEC_last (deferred_access, deferred_access_stack)
202 ->deferred_access_checks);
205 /* Take current deferred checks and combine with the
206 previous states if we also defer checks previously.
207 Otherwise perform checks now. */
210 pop_to_parent_deferring_access_checks (void)
212 if (deferred_access_no_check)
213 deferred_access_no_check--;
216 VEC (deferred_access_check,gc) *checks;
217 deferred_access *ptr;
219 checks = (VEC_last (deferred_access, deferred_access_stack)
220 ->deferred_access_checks);
222 VEC_pop (deferred_access, deferred_access_stack);
223 ptr = VEC_last (deferred_access, deferred_access_stack);
224 if (ptr->deferring_access_checks_kind == dk_no_deferred)
227 perform_access_checks (checks);
231 /* Merge with parent. */
233 deferred_access_check *chk, *probe;
235 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
237 FOR_EACH_VEC_ELT (deferred_access_check,
238 ptr->deferred_access_checks, j, probe)
240 if (probe->binfo == chk->binfo &&
241 probe->decl == chk->decl &&
242 probe->diag_decl == chk->diag_decl)
245 /* Insert into parent's checks. */
246 VEC_safe_push (deferred_access_check, gc,
247 ptr->deferred_access_checks, chk);
254 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
255 is the BINFO indicating the qualifying scope used to access the
256 DECL node stored in the TREE_VALUE of the node. */
259 perform_access_checks (VEC (deferred_access_check,gc)* checks)
262 deferred_access_check *chk;
267 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
268 enforce_access (chk->binfo, chk->decl, chk->diag_decl);
271 /* Perform the deferred access checks.
273 After performing the checks, we still have to keep the list
274 `deferred_access_stack->deferred_access_checks' since we may want
275 to check access for them again later in a different context.
282 A::X A::a, x; // No error for `A::a', error for `x'
284 We have to perform deferred access of `A::X', first with `A::a',
288 perform_deferred_access_checks (void)
290 perform_access_checks (get_deferred_access_checks ());
293 /* Defer checking the accessibility of DECL, when looked up in
294 BINFO. DIAG_DECL is the declaration to use to print diagnostics. */
297 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl)
300 deferred_access *ptr;
301 deferred_access_check *chk;
302 deferred_access_check *new_access;
305 /* Exit if we are in a context that no access checking is performed.
307 if (deferred_access_no_check)
310 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
312 ptr = VEC_last (deferred_access, deferred_access_stack);
314 /* If we are not supposed to defer access checks, just check now. */
315 if (ptr->deferring_access_checks_kind == dk_no_deferred)
317 enforce_access (binfo, decl, diag_decl);
321 /* See if we are already going to perform this check. */
322 FOR_EACH_VEC_ELT (deferred_access_check,
323 ptr->deferred_access_checks, i, chk)
325 if (chk->decl == decl && chk->binfo == binfo &&
326 chk->diag_decl == diag_decl)
331 /* If not, record the check. */
333 VEC_safe_push (deferred_access_check, gc,
334 ptr->deferred_access_checks, 0);
335 new_access->binfo = binfo;
336 new_access->decl = decl;
337 new_access->diag_decl = diag_decl;
340 /* Used by build_over_call in LOOKUP_SPECULATIVE mode: return whether DECL
341 is accessible in BINFO, and possibly complain if not. If we're not
342 checking access, everything is accessible. */
345 speculative_access_check (tree binfo, tree decl, tree diag_decl,
348 if (deferred_access_no_check)
351 /* If we're checking for implicit delete, we don't want access
353 if (!accessible_p (binfo, decl, true))
355 /* Unless we're under maybe_explain_implicit_delete. */
357 enforce_access (binfo, decl, diag_decl);
364 /* Returns nonzero if the current statement is a full expression,
365 i.e. temporaries created during that statement should be destroyed
366 at the end of the statement. */
369 stmts_are_full_exprs_p (void)
371 return current_stmt_tree ()->stmts_are_full_exprs_p;
374 /* T is a statement. Add it to the statement-tree. This is the C++
375 version. The C/ObjC frontends have a slightly different version of
381 enum tree_code code = TREE_CODE (t);
383 if (EXPR_P (t) && code != LABEL_EXPR)
385 if (!EXPR_HAS_LOCATION (t))
386 SET_EXPR_LOCATION (t, input_location);
388 /* When we expand a statement-tree, we must know whether or not the
389 statements are full-expressions. We record that fact here. */
390 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
393 /* Add T to the statement-tree. Non-side-effect statements need to be
394 recorded during statement expressions. */
395 gcc_checking_assert (!VEC_empty (tree, stmt_list_stack));
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))
574 check_goto (destination);
576 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
579 /* COND is the condition-expression for an if, while, etc.,
580 statement. Convert it to a boolean value, if appropriate.
581 In addition, verify sequence points if -Wsequence-point is enabled. */
584 maybe_convert_cond (tree cond)
586 /* Empty conditions remain empty. */
590 /* Wait until we instantiate templates before doing conversion. */
591 if (processing_template_decl)
594 if (warn_sequence_point)
595 verify_sequence_points (cond);
597 /* Do the conversion. */
598 cond = convert_from_reference (cond);
600 if (TREE_CODE (cond) == MODIFY_EXPR
601 && !TREE_NO_WARNING (cond)
604 warning (OPT_Wparentheses,
605 "suggest parentheses around assignment used as truth value");
606 TREE_NO_WARNING (cond) = 1;
609 return condition_conversion (cond);
612 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
615 finish_expr_stmt (tree expr)
619 if (expr != NULL_TREE)
621 if (!processing_template_decl)
623 if (warn_sequence_point)
624 verify_sequence_points (expr);
625 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
627 else if (!type_dependent_expression_p (expr))
628 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
629 tf_warning_or_error);
631 if (check_for_bare_parameter_packs (expr))
632 expr = error_mark_node;
634 /* Simplification of inner statement expressions, compound exprs,
635 etc can result in us already having an EXPR_STMT. */
636 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
638 if (TREE_CODE (expr) != EXPR_STMT)
639 expr = build_stmt (input_location, EXPR_STMT, expr);
640 expr = maybe_cleanup_point_expr_void (expr);
652 /* Begin an if-statement. Returns a newly created IF_STMT if
659 scope = do_pushlevel (sk_cond);
660 r = build_stmt (input_location, IF_STMT, NULL_TREE,
661 NULL_TREE, NULL_TREE, scope);
662 begin_cond (&IF_COND (r));
666 /* Process the COND of an if-statement, which may be given by
670 finish_if_stmt_cond (tree cond, tree if_stmt)
672 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
674 THEN_CLAUSE (if_stmt) = push_stmt_list ();
677 /* Finish the then-clause of an if-statement, which may be given by
681 finish_then_clause (tree if_stmt)
683 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
687 /* Begin the else-clause of an if-statement. */
690 begin_else_clause (tree if_stmt)
692 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
695 /* Finish the else-clause of an if-statement, which may be given by
699 finish_else_clause (tree if_stmt)
701 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
704 /* Finish an if-statement. */
707 finish_if_stmt (tree if_stmt)
709 tree scope = IF_SCOPE (if_stmt);
710 IF_SCOPE (if_stmt) = NULL;
711 add_stmt (do_poplevel (scope));
715 /* Begin a while-statement. Returns a newly created WHILE_STMT if
719 begin_while_stmt (void)
722 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
724 WHILE_BODY (r) = do_pushlevel (sk_block);
725 begin_cond (&WHILE_COND (r));
729 /* Process the COND of a while-statement, which may be given by
733 finish_while_stmt_cond (tree cond, tree while_stmt)
735 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
736 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
739 /* Finish a while-statement, which may be given by WHILE_STMT. */
742 finish_while_stmt (tree while_stmt)
744 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
748 /* Begin a do-statement. Returns a newly created DO_STMT if
754 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
756 DO_BODY (r) = push_stmt_list ();
760 /* Finish the body of a do-statement, which may be given by DO_STMT. */
763 finish_do_body (tree do_stmt)
765 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
767 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
768 body = STATEMENT_LIST_TAIL (body)->stmt;
770 if (IS_EMPTY_STMT (body))
771 warning (OPT_Wempty_body,
772 "suggest explicit braces around empty body in %<do%> statement");
775 /* Finish a do-statement, which may be given by DO_STMT, and whose
776 COND is as indicated. */
779 finish_do_stmt (tree cond, tree do_stmt)
781 cond = maybe_convert_cond (cond);
782 DO_COND (do_stmt) = cond;
786 /* Finish a return-statement. The EXPRESSION returned, if any, is as
790 finish_return_stmt (tree expr)
795 expr = check_return_expr (expr, &no_warning);
797 if (flag_openmp && !check_omp_return ())
798 return error_mark_node;
799 if (!processing_template_decl)
801 if (warn_sequence_point)
802 verify_sequence_points (expr);
804 if (DECL_DESTRUCTOR_P (current_function_decl)
805 || (DECL_CONSTRUCTOR_P (current_function_decl)
806 && targetm.cxx.cdtor_returns_this ()))
808 /* Similarly, all destructors must run destructors for
809 base-classes before returning. So, all returns in a
810 destructor get sent to the DTOR_LABEL; finish_function emits
811 code to return a value there. */
812 return finish_goto_stmt (cdtor_label);
816 r = build_stmt (input_location, RETURN_EXPR, expr);
817 TREE_NO_WARNING (r) |= no_warning;
818 r = maybe_cleanup_point_expr_void (r);
825 /* Begin the scope of a for-statement or a range-for-statement.
826 Both the returned trees are to be used in a call to
827 begin_for_stmt or begin_range_for_stmt. */
830 begin_for_scope (tree *init)
832 tree scope = NULL_TREE;
833 if (flag_new_for_scope > 0)
834 scope = do_pushlevel (sk_for);
836 if (processing_template_decl)
837 *init = push_stmt_list ();
844 /* Begin a for-statement. Returns a new FOR_STMT.
845 SCOPE and INIT should be the return of begin_for_scope,
849 begin_for_stmt (tree scope, tree init)
853 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
854 NULL_TREE, NULL_TREE, NULL_TREE);
856 if (scope == NULL_TREE)
858 gcc_assert (!init || !(flag_new_for_scope > 0));
860 scope = begin_for_scope (&init);
862 FOR_INIT_STMT (r) = init;
863 FOR_SCOPE (r) = scope;
868 /* Finish the for-init-statement of a for-statement, which may be
869 given by FOR_STMT. */
872 finish_for_init_stmt (tree for_stmt)
874 if (processing_template_decl)
875 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
877 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
878 begin_cond (&FOR_COND (for_stmt));
881 /* Finish the COND of a for-statement, which may be given by
885 finish_for_cond (tree cond, tree for_stmt)
887 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
888 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
891 /* Finish the increment-EXPRESSION in a for-statement, which may be
892 given by FOR_STMT. */
895 finish_for_expr (tree expr, tree for_stmt)
899 /* If EXPR is an overloaded function, issue an error; there is no
900 context available to use to perform overload resolution. */
901 if (type_unknown_p (expr))
903 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
904 expr = error_mark_node;
906 if (!processing_template_decl)
908 if (warn_sequence_point)
909 verify_sequence_points (expr);
910 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
911 tf_warning_or_error);
913 else if (!type_dependent_expression_p (expr))
914 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
915 tf_warning_or_error);
916 expr = maybe_cleanup_point_expr_void (expr);
917 if (check_for_bare_parameter_packs (expr))
918 expr = error_mark_node;
919 FOR_EXPR (for_stmt) = expr;
922 /* Finish the body of a for-statement, which may be given by
923 FOR_STMT. The increment-EXPR for the loop must be
925 It can also finish RANGE_FOR_STMT. */
928 finish_for_stmt (tree for_stmt)
930 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
931 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
933 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
935 /* Pop the scope for the body of the loop. */
936 if (flag_new_for_scope > 0)
939 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
940 ? &RANGE_FOR_SCOPE (for_stmt)
941 : &FOR_SCOPE (for_stmt));
944 add_stmt (do_poplevel (scope));
950 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
951 SCOPE and INIT should be the return of begin_for_scope,
953 To finish it call finish_for_stmt(). */
956 begin_range_for_stmt (tree scope, tree init)
960 r = build_stmt (input_location, RANGE_FOR_STMT,
961 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
963 if (scope == NULL_TREE)
965 gcc_assert (!init || !(flag_new_for_scope > 0));
967 scope = begin_for_scope (&init);
970 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
973 pop_stmt_list (init);
974 RANGE_FOR_SCOPE (r) = scope;
979 /* Finish the head of a range-based for statement, which may
980 be given by RANGE_FOR_STMT. DECL must be the declaration
981 and EXPR must be the loop expression. */
984 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
986 RANGE_FOR_DECL (range_for_stmt) = decl;
987 RANGE_FOR_EXPR (range_for_stmt) = expr;
988 add_stmt (range_for_stmt);
989 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
992 /* Finish a break-statement. */
995 finish_break_stmt (void)
997 /* In switch statements break is sometimes stylistically used after
998 a return statement. This can lead to spurious warnings about
999 control reaching the end of a non-void function when it is
1000 inlined. Note that we are calling block_may_fallthru with
1001 language specific tree nodes; this works because
1002 block_may_fallthru returns true when given something it does not
1004 if (!block_may_fallthru (cur_stmt_list))
1005 return void_zero_node;
1006 return add_stmt (build_stmt (input_location, BREAK_STMT));
1009 /* Finish a continue-statement. */
1012 finish_continue_stmt (void)
1014 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1017 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1021 begin_switch_stmt (void)
1025 scope = do_pushlevel (sk_cond);
1026 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1028 begin_cond (&SWITCH_STMT_COND (r));
1033 /* Finish the cond of a switch-statement. */
1036 finish_switch_cond (tree cond, tree switch_stmt)
1038 tree orig_type = NULL;
1039 if (!processing_template_decl)
1041 /* Convert the condition to an integer or enumeration type. */
1042 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1043 if (cond == NULL_TREE)
1045 error ("switch quantity not an integer");
1046 cond = error_mark_node;
1048 orig_type = TREE_TYPE (cond);
1049 if (cond != error_mark_node)
1053 Integral promotions are performed. */
1054 cond = perform_integral_promotions (cond);
1055 cond = maybe_cleanup_point_expr (cond);
1058 if (check_for_bare_parameter_packs (cond))
1059 cond = error_mark_node;
1060 else if (!processing_template_decl && warn_sequence_point)
1061 verify_sequence_points (cond);
1063 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1064 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1065 add_stmt (switch_stmt);
1066 push_switch (switch_stmt);
1067 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1070 /* Finish the body of a switch-statement, which may be given by
1071 SWITCH_STMT. The COND to switch on is indicated. */
1074 finish_switch_stmt (tree switch_stmt)
1078 SWITCH_STMT_BODY (switch_stmt) =
1079 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1083 scope = SWITCH_STMT_SCOPE (switch_stmt);
1084 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1085 add_stmt (do_poplevel (scope));
1088 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1092 begin_try_block (void)
1094 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1096 TRY_STMTS (r) = push_stmt_list ();
1100 /* Likewise, for a function-try-block. The block returned in
1101 *COMPOUND_STMT is an artificial outer scope, containing the
1102 function-try-block. */
1105 begin_function_try_block (tree *compound_stmt)
1108 /* This outer scope does not exist in the C++ standard, but we need
1109 a place to put __FUNCTION__ and similar variables. */
1110 *compound_stmt = begin_compound_stmt (0);
1111 r = begin_try_block ();
1112 FN_TRY_BLOCK_P (r) = 1;
1116 /* Finish a try-block, which may be given by TRY_BLOCK. */
1119 finish_try_block (tree try_block)
1121 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1122 TRY_HANDLERS (try_block) = push_stmt_list ();
1125 /* Finish the body of a cleanup try-block, which may be given by
1129 finish_cleanup_try_block (tree try_block)
1131 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1134 /* Finish an implicitly generated try-block, with a cleanup is given
1138 finish_cleanup (tree cleanup, tree try_block)
1140 TRY_HANDLERS (try_block) = cleanup;
1141 CLEANUP_P (try_block) = 1;
1144 /* Likewise, for a function-try-block. */
1147 finish_function_try_block (tree try_block)
1149 finish_try_block (try_block);
1150 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1151 the try block, but moving it inside. */
1152 in_function_try_handler = 1;
1155 /* Finish a handler-sequence for a try-block, which may be given by
1159 finish_handler_sequence (tree try_block)
1161 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1162 check_handlers (TRY_HANDLERS (try_block));
1165 /* Finish the handler-seq for a function-try-block, given by
1166 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1167 begin_function_try_block. */
1170 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1172 in_function_try_handler = 0;
1173 finish_handler_sequence (try_block);
1174 finish_compound_stmt (compound_stmt);
1177 /* Begin a handler. Returns a HANDLER if appropriate. */
1180 begin_handler (void)
1184 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1187 /* Create a binding level for the eh_info and the exception object
1189 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1194 /* Finish the handler-parameters for a handler, which may be given by
1195 HANDLER. DECL is the declaration for the catch parameter, or NULL
1196 if this is a `catch (...)' clause. */
1199 finish_handler_parms (tree decl, tree handler)
1201 tree type = NULL_TREE;
1202 if (processing_template_decl)
1206 decl = pushdecl (decl);
1207 decl = push_template_decl (decl);
1208 HANDLER_PARMS (handler) = decl;
1209 type = TREE_TYPE (decl);
1213 type = expand_start_catch_block (decl);
1214 HANDLER_TYPE (handler) = type;
1215 if (!processing_template_decl && type)
1216 mark_used (eh_type_info (type));
1219 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1220 the return value from the matching call to finish_handler_parms. */
1223 finish_handler (tree handler)
1225 if (!processing_template_decl)
1226 expand_end_catch_block ();
1227 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1230 /* Begin a compound statement. FLAGS contains some bits that control the
1231 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1232 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1233 block of a function. If BCS_TRY_BLOCK is set, this is the block
1234 created on behalf of a TRY statement. Returns a token to be passed to
1235 finish_compound_stmt. */
1238 begin_compound_stmt (unsigned int flags)
1242 if (flags & BCS_NO_SCOPE)
1244 r = push_stmt_list ();
1245 STATEMENT_LIST_NO_SCOPE (r) = 1;
1247 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1248 But, if it's a statement-expression with a scopeless block, there's
1249 nothing to keep, and we don't want to accidentally keep a block
1250 *inside* the scopeless block. */
1251 keep_next_level (false);
1254 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1256 /* When processing a template, we need to remember where the braces were,
1257 so that we can set up identical scopes when instantiating the template
1258 later. BIND_EXPR is a handy candidate for this.
1259 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1260 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1261 processing templates. */
1262 if (processing_template_decl)
1264 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1265 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1266 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1267 TREE_SIDE_EFFECTS (r) = 1;
1273 /* Finish a compound-statement, which is given by STMT. */
1276 finish_compound_stmt (tree stmt)
1278 if (TREE_CODE (stmt) == BIND_EXPR)
1280 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1281 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1282 discard the BIND_EXPR so it can be merged with the containing
1284 if (TREE_CODE (body) == STATEMENT_LIST
1285 && STATEMENT_LIST_HEAD (body) == NULL
1286 && !BIND_EXPR_BODY_BLOCK (stmt)
1287 && !BIND_EXPR_TRY_BLOCK (stmt))
1290 BIND_EXPR_BODY (stmt) = body;
1292 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1293 stmt = pop_stmt_list (stmt);
1296 /* Destroy any ObjC "super" receivers that may have been
1298 objc_clear_super_receiver ();
1300 stmt = do_poplevel (stmt);
1303 /* ??? See c_end_compound_stmt wrt statement expressions. */
1308 /* Finish an asm-statement, whose components are a STRING, some
1309 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1310 LABELS. Also note whether the asm-statement should be
1311 considered volatile. */
1314 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1315 tree input_operands, tree clobbers, tree labels)
1319 int ninputs = list_length (input_operands);
1320 int noutputs = list_length (output_operands);
1322 if (!processing_template_decl)
1324 const char *constraint;
1325 const char **oconstraints;
1326 bool allows_mem, allows_reg, is_inout;
1330 oconstraints = XALLOCAVEC (const char *, noutputs);
1332 string = resolve_asm_operand_names (string, output_operands,
1333 input_operands, labels);
1335 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1337 operand = TREE_VALUE (t);
1339 /* ??? Really, this should not be here. Users should be using a
1340 proper lvalue, dammit. But there's a long history of using
1341 casts in the output operands. In cases like longlong.h, this
1342 becomes a primitive form of typechecking -- if the cast can be
1343 removed, then the output operand had a type of the proper width;
1344 otherwise we'll get an error. Gross, but ... */
1345 STRIP_NOPS (operand);
1347 operand = mark_lvalue_use (operand);
1349 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1350 operand = error_mark_node;
1352 if (operand != error_mark_node
1353 && (TREE_READONLY (operand)
1354 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1355 /* Functions are not modifiable, even though they are
1357 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1358 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1359 /* If it's an aggregate and any field is const, then it is
1360 effectively const. */
1361 || (CLASS_TYPE_P (TREE_TYPE (operand))
1362 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1363 cxx_readonly_error (operand, lv_asm);
1365 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1366 oconstraints[i] = constraint;
1368 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1369 &allows_mem, &allows_reg, &is_inout))
1371 /* If the operand is going to end up in memory,
1372 mark it addressable. */
1373 if (!allows_reg && !cxx_mark_addressable (operand))
1374 operand = error_mark_node;
1377 operand = error_mark_node;
1379 TREE_VALUE (t) = operand;
1382 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1384 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1385 operand = decay_conversion (TREE_VALUE (t));
1387 /* If the type of the operand hasn't been determined (e.g.,
1388 because it involves an overloaded function), then issue
1389 an error message. There's no context available to
1390 resolve the overloading. */
1391 if (TREE_TYPE (operand) == unknown_type_node)
1393 error ("type of asm operand %qE could not be determined",
1395 operand = error_mark_node;
1398 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1399 oconstraints, &allows_mem, &allows_reg))
1401 /* If the operand is going to end up in memory,
1402 mark it addressable. */
1403 if (!allows_reg && allows_mem)
1405 /* Strip the nops as we allow this case. FIXME, this really
1406 should be rejected or made deprecated. */
1407 STRIP_NOPS (operand);
1408 if (!cxx_mark_addressable (operand))
1409 operand = error_mark_node;
1413 operand = error_mark_node;
1415 TREE_VALUE (t) = operand;
1419 r = build_stmt (input_location, ASM_EXPR, string,
1420 output_operands, input_operands,
1422 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1423 r = maybe_cleanup_point_expr_void (r);
1424 return add_stmt (r);
1427 /* Finish a label with the indicated NAME. Returns the new label. */
1430 finish_label_stmt (tree name)
1432 tree decl = define_label (input_location, name);
1434 if (decl == error_mark_node)
1435 return error_mark_node;
1437 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1442 /* Finish a series of declarations for local labels. G++ allows users
1443 to declare "local" labels, i.e., labels with scope. This extension
1444 is useful when writing code involving statement-expressions. */
1447 finish_label_decl (tree name)
1449 if (!at_function_scope_p ())
1451 error ("__label__ declarations are only allowed in function scopes");
1455 add_decl_expr (declare_local_label (name));
1458 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1461 finish_decl_cleanup (tree decl, tree cleanup)
1463 push_cleanup (decl, cleanup, false);
1466 /* If the current scope exits with an exception, run CLEANUP. */
1469 finish_eh_cleanup (tree cleanup)
1471 push_cleanup (NULL, cleanup, true);
1474 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1475 order they were written by the user. Each node is as for
1476 emit_mem_initializers. */
1479 finish_mem_initializers (tree mem_inits)
1481 /* Reorder the MEM_INITS so that they are in the order they appeared
1482 in the source program. */
1483 mem_inits = nreverse (mem_inits);
1485 if (processing_template_decl)
1489 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1491 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1492 check for bare parameter packs in the TREE_VALUE, because
1493 any parameter packs in the TREE_VALUE have already been
1494 bound as part of the TREE_PURPOSE. See
1495 make_pack_expansion for more information. */
1496 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1497 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1498 TREE_VALUE (mem) = error_mark_node;
1501 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1504 emit_mem_initializers (mem_inits);
1507 /* Finish a parenthesized expression EXPR. */
1510 finish_parenthesized_expr (tree expr)
1513 /* This inhibits warnings in c_common_truthvalue_conversion. */
1514 TREE_NO_WARNING (expr) = 1;
1516 if (TREE_CODE (expr) == OFFSET_REF
1517 || TREE_CODE (expr) == SCOPE_REF)
1518 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1519 enclosed in parentheses. */
1520 PTRMEM_OK_P (expr) = 0;
1522 if (TREE_CODE (expr) == STRING_CST)
1523 PAREN_STRING_LITERAL_P (expr) = 1;
1528 /* Finish a reference to a non-static data member (DECL) that is not
1529 preceded by `.' or `->'. */
1532 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1534 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1538 tree scope = qualifying_scope;
1539 if (scope == NULL_TREE)
1540 scope = context_for_name_lookup (decl);
1541 object = maybe_dummy_object (scope, NULL);
1544 if (object == error_mark_node)
1545 return error_mark_node;
1547 /* DR 613: Can use non-static data members without an associated
1548 object in sizeof/decltype/alignof. */
1549 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1550 && (!processing_template_decl || !current_class_ref))
1552 if (current_function_decl
1553 && DECL_STATIC_FUNCTION_P (current_function_decl))
1554 error ("invalid use of member %q+D in static member function", decl);
1556 error ("invalid use of non-static data member %q+D", decl);
1557 error ("from this location");
1559 return error_mark_node;
1562 if (current_class_ptr)
1563 TREE_USED (current_class_ptr) = 1;
1564 if (processing_template_decl && !qualifying_scope)
1566 tree type = TREE_TYPE (decl);
1568 if (TREE_CODE (type) == REFERENCE_TYPE)
1569 /* Quals on the object don't matter. */;
1572 /* Set the cv qualifiers. */
1573 int quals = cp_type_quals (TREE_TYPE (object));
1575 if (DECL_MUTABLE_P (decl))
1576 quals &= ~TYPE_QUAL_CONST;
1578 quals |= cp_type_quals (TREE_TYPE (decl));
1579 type = cp_build_qualified_type (type, quals);
1582 return (convert_from_reference
1583 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1585 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1586 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1588 else if (processing_template_decl)
1589 return build_qualified_name (TREE_TYPE (decl),
1592 /*template_p=*/false);
1595 tree access_type = TREE_TYPE (object);
1597 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1600 /* If the data member was named `C::M', convert `*this' to `C'
1602 if (qualifying_scope)
1604 tree binfo = NULL_TREE;
1605 object = build_scoped_ref (object, qualifying_scope,
1609 return build_class_member_access_expr (object, decl,
1610 /*access_path=*/NULL_TREE,
1611 /*preserve_reference=*/false,
1612 tf_warning_or_error);
1616 /* If we are currently parsing a template and we encountered a typedef
1617 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1618 adds the typedef to a list tied to the current template.
1619 At tempate instantiatin time, that list is walked and access check
1620 performed for each typedef.
1621 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1624 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1626 location_t location)
1628 tree template_info = NULL;
1629 tree cs = current_scope ();
1631 if (!is_typedef_decl (typedef_decl)
1633 || !CLASS_TYPE_P (context)
1637 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1638 template_info = get_template_info (cs);
1641 && TI_TEMPLATE (template_info)
1642 && !currently_open_class (context))
1643 append_type_to_template_for_access_check (cs, typedef_decl,
1647 /* DECL was the declaration to which a qualified-id resolved. Issue
1648 an error message if it is not accessible. If OBJECT_TYPE is
1649 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1650 type of `*x', or `x', respectively. If the DECL was named as
1651 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1654 check_accessibility_of_qualified_id (tree decl,
1656 tree nested_name_specifier)
1659 tree qualifying_type = NULL_TREE;
1661 /* If we are parsing a template declaration and if decl is a typedef,
1662 add it to a list tied to the template.
1663 At template instantiation time, that list will be walked and
1664 access check performed. */
1665 add_typedef_to_current_template_for_access_check (decl,
1666 nested_name_specifier
1667 ? nested_name_specifier
1668 : DECL_CONTEXT (decl),
1671 /* If we're not checking, return immediately. */
1672 if (deferred_access_no_check)
1675 /* Determine the SCOPE of DECL. */
1676 scope = context_for_name_lookup (decl);
1677 /* If the SCOPE is not a type, then DECL is not a member. */
1678 if (!TYPE_P (scope))
1680 /* Compute the scope through which DECL is being accessed. */
1682 /* OBJECT_TYPE might not be a class type; consider:
1684 class A { typedef int I; };
1688 In this case, we will have "A::I" as the DECL, but "I" as the
1690 && CLASS_TYPE_P (object_type)
1691 && DERIVED_FROM_P (scope, object_type))
1692 /* If we are processing a `->' or `.' expression, use the type of the
1694 qualifying_type = object_type;
1695 else if (nested_name_specifier)
1697 /* If the reference is to a non-static member of the
1698 current class, treat it as if it were referenced through
1700 if (DECL_NONSTATIC_MEMBER_P (decl)
1701 && current_class_ptr
1702 && DERIVED_FROM_P (scope, current_class_type))
1703 qualifying_type = current_class_type;
1704 /* Otherwise, use the type indicated by the
1705 nested-name-specifier. */
1707 qualifying_type = nested_name_specifier;
1710 /* Otherwise, the name must be from the current class or one of
1712 qualifying_type = currently_open_derived_class (scope);
1715 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1716 or similar in a default argument value. */
1717 && CLASS_TYPE_P (qualifying_type)
1718 && !dependent_type_p (qualifying_type))
1719 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1723 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1724 class named to the left of the "::" operator. DONE is true if this
1725 expression is a complete postfix-expression; it is false if this
1726 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1727 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1728 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1729 is true iff this qualified name appears as a template argument. */
1732 finish_qualified_id_expr (tree qualifying_class,
1737 bool template_arg_p)
1739 gcc_assert (TYPE_P (qualifying_class));
1741 if (error_operand_p (expr))
1742 return error_mark_node;
1744 if (DECL_P (expr) || BASELINK_P (expr))
1748 check_template_keyword (expr);
1750 /* If EXPR occurs as the operand of '&', use special handling that
1751 permits a pointer-to-member. */
1752 if (address_p && done)
1754 if (TREE_CODE (expr) == SCOPE_REF)
1755 expr = TREE_OPERAND (expr, 1);
1756 expr = build_offset_ref (qualifying_class, expr,
1757 /*address_p=*/true);
1761 /* Within the scope of a class, turn references to non-static
1762 members into expression of the form "this->...". */
1764 /* But, within a template argument, we do not want make the
1765 transformation, as there is no "this" pointer. */
1767 else if (TREE_CODE (expr) == FIELD_DECL)
1769 push_deferring_access_checks (dk_no_check);
1770 expr = finish_non_static_data_member (expr, NULL_TREE,
1772 pop_deferring_access_checks ();
1774 else if (BASELINK_P (expr) && !processing_template_decl)
1778 /* See if any of the functions are non-static members. */
1779 /* If so, the expression may be relative to 'this'. */
1780 if (!shared_member_p (expr)
1781 && (ob = maybe_dummy_object (qualifying_class, NULL),
1782 !is_dummy_object (ob)))
1783 expr = (build_class_member_access_expr
1786 BASELINK_ACCESS_BINFO (expr),
1787 /*preserve_reference=*/false,
1788 tf_warning_or_error));
1790 /* The expression is a qualified name whose address is not
1792 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1798 /* Begin a statement-expression. The value returned must be passed to
1799 finish_stmt_expr. */
1802 begin_stmt_expr (void)
1804 return push_stmt_list ();
1807 /* Process the final expression of a statement expression. EXPR can be
1808 NULL, if the final expression is empty. Return a STATEMENT_LIST
1809 containing all the statements in the statement-expression, or
1810 ERROR_MARK_NODE if there was an error. */
1813 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1815 if (error_operand_p (expr))
1817 /* The type of the statement-expression is the type of the last
1819 TREE_TYPE (stmt_expr) = error_mark_node;
1820 return error_mark_node;
1823 /* If the last statement does not have "void" type, then the value
1824 of the last statement is the value of the entire expression. */
1827 tree type = TREE_TYPE (expr);
1829 if (processing_template_decl)
1831 expr = build_stmt (input_location, EXPR_STMT, expr);
1832 expr = add_stmt (expr);
1833 /* Mark the last statement so that we can recognize it as such at
1834 template-instantiation time. */
1835 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1837 else if (VOID_TYPE_P (type))
1839 /* Just treat this like an ordinary statement. */
1840 expr = finish_expr_stmt (expr);
1844 /* It actually has a value we need to deal with. First, force it
1845 to be an rvalue so that we won't need to build up a copy
1846 constructor call later when we try to assign it to something. */
1847 expr = force_rvalue (expr, tf_warning_or_error);
1848 if (error_operand_p (expr))
1849 return error_mark_node;
1851 /* Update for array-to-pointer decay. */
1852 type = TREE_TYPE (expr);
1854 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1855 normal statement, but don't convert to void or actually add
1857 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1858 expr = maybe_cleanup_point_expr (expr);
1862 /* The type of the statement-expression is the type of the last
1864 TREE_TYPE (stmt_expr) = type;
1870 /* Finish a statement-expression. EXPR should be the value returned
1871 by the previous begin_stmt_expr. Returns an expression
1872 representing the statement-expression. */
1875 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1880 if (error_operand_p (stmt_expr))
1882 pop_stmt_list (stmt_expr);
1883 return error_mark_node;
1886 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1888 type = TREE_TYPE (stmt_expr);
1889 result = pop_stmt_list (stmt_expr);
1890 TREE_TYPE (result) = type;
1892 if (processing_template_decl)
1894 result = build_min (STMT_EXPR, type, result);
1895 TREE_SIDE_EFFECTS (result) = 1;
1896 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1898 else if (CLASS_TYPE_P (type))
1900 /* Wrap the statement-expression in a TARGET_EXPR so that the
1901 temporary object created by the final expression is destroyed at
1902 the end of the full-expression containing the
1903 statement-expression. */
1904 result = force_target_expr (type, result, tf_warning_or_error);
1910 /* Returns the expression which provides the value of STMT_EXPR. */
1913 stmt_expr_value_expr (tree stmt_expr)
1915 tree t = STMT_EXPR_STMT (stmt_expr);
1917 if (TREE_CODE (t) == BIND_EXPR)
1918 t = BIND_EXPR_BODY (t);
1920 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
1921 t = STATEMENT_LIST_TAIL (t)->stmt;
1923 if (TREE_CODE (t) == EXPR_STMT)
1924 t = EXPR_STMT_EXPR (t);
1929 /* Return TRUE iff EXPR_STMT is an empty list of
1930 expression statements. */
1933 empty_expr_stmt_p (tree expr_stmt)
1935 tree body = NULL_TREE;
1937 if (expr_stmt == void_zero_node)
1942 if (TREE_CODE (expr_stmt) == EXPR_STMT)
1943 body = EXPR_STMT_EXPR (expr_stmt);
1944 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
1950 if (TREE_CODE (body) == STATEMENT_LIST)
1951 return tsi_end_p (tsi_start (body));
1953 return empty_expr_stmt_p (body);
1958 /* Perform Koenig lookup. FN is the postfix-expression representing
1959 the function (or functions) to call; ARGS are the arguments to the
1960 call; if INCLUDE_STD then the `std' namespace is automatically
1961 considered an associated namespace (used in range-based for loops).
1962 Returns the functions to be considered by overload resolution. */
1965 perform_koenig_lookup (tree fn, VEC(tree,gc) *args, bool include_std,
1966 tsubst_flags_t complain)
1968 tree identifier = NULL_TREE;
1969 tree functions = NULL_TREE;
1970 tree tmpl_args = NULL_TREE;
1971 bool template_id = false;
1973 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1975 /* Use a separate flag to handle null args. */
1977 tmpl_args = TREE_OPERAND (fn, 1);
1978 fn = TREE_OPERAND (fn, 0);
1981 /* Find the name of the overloaded function. */
1982 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1984 else if (is_overloaded_fn (fn))
1987 identifier = DECL_NAME (get_first_fn (functions));
1989 else if (DECL_P (fn))
1992 identifier = DECL_NAME (fn);
1995 /* A call to a namespace-scope function using an unqualified name.
1997 Do Koenig lookup -- unless any of the arguments are
1999 if (!any_type_dependent_arguments_p (args)
2000 && !any_dependent_template_arguments_p (tmpl_args))
2002 fn = lookup_arg_dependent (identifier, functions, args, include_std);
2005 /* The unqualified name could not be resolved. */
2007 fn = unqualified_fn_lookup_error (identifier);
2013 if (fn && template_id)
2014 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2019 /* Generate an expression for `FN (ARGS)'. This may change the
2022 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2023 as a virtual call, even if FN is virtual. (This flag is set when
2024 encountering an expression where the function name is explicitly
2025 qualified. For example a call to `X::f' never generates a virtual
2028 Returns code for the call. */
2031 finish_call_expr (tree fn, VEC(tree,gc) **args, bool disallow_virtual,
2032 bool koenig_p, tsubst_flags_t complain)
2036 VEC(tree,gc) *orig_args = NULL;
2038 if (fn == error_mark_node)
2039 return error_mark_node;
2041 gcc_assert (!TYPE_P (fn));
2045 if (processing_template_decl)
2047 /* If the call expression is dependent, build a CALL_EXPR node
2048 with no type; type_dependent_expression_p recognizes
2049 expressions with no type as being dependent. */
2050 if (type_dependent_expression_p (fn)
2051 || any_type_dependent_arguments_p (*args)
2052 /* For a non-static member function that doesn't have an
2053 explicit object argument, we need to specifically
2054 test the type dependency of the "this" pointer because it
2055 is not included in *ARGS even though it is considered to
2056 be part of the list of arguments. Note that this is
2057 related to CWG issues 515 and 1005. */
2058 || (TREE_CODE (fn) != COMPONENT_REF
2059 && non_static_member_function_p (fn)
2060 && current_class_ref
2061 && type_dependent_expression_p (current_class_ref)))
2063 result = build_nt_call_vec (fn, *args);
2064 KOENIG_LOOKUP_P (result) = koenig_p;
2069 tree fndecl = OVL_CURRENT (fn);
2070 if (TREE_CODE (fndecl) != FUNCTION_DECL
2071 || !TREE_THIS_VOLATILE (fndecl))
2077 current_function_returns_abnormally = 1;
2081 orig_args = make_tree_vector_copy (*args);
2082 if (!BASELINK_P (fn)
2083 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2084 && TREE_TYPE (fn) != unknown_type_node)
2085 fn = build_non_dependent_expr (fn);
2086 make_args_non_dependent (*args);
2089 if (TREE_CODE (fn) == COMPONENT_REF)
2091 tree member = TREE_OPERAND (fn, 1);
2092 if (BASELINK_P (member))
2094 tree object = TREE_OPERAND (fn, 0);
2095 return build_new_method_call (object, member,
2098 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2105 if (is_overloaded_fn (fn))
2106 fn = baselink_for_fns (fn);
2109 if (BASELINK_P (fn))
2113 /* A call to a member function. From [over.call.func]:
2115 If the keyword this is in scope and refers to the class of
2116 that member function, or a derived class thereof, then the
2117 function call is transformed into a qualified function call
2118 using (*this) as the postfix-expression to the left of the
2119 . operator.... [Otherwise] a contrived object of type T
2120 becomes the implied object argument.
2124 struct A { void f(); };
2125 struct B : public A {};
2126 struct C : public A { void g() { B::f(); }};
2128 "the class of that member function" refers to `A'. But 11.2
2129 [class.access.base] says that we need to convert 'this' to B* as
2130 part of the access, so we pass 'B' to maybe_dummy_object. */
2132 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2135 if (processing_template_decl)
2137 if (type_dependent_expression_p (object))
2139 tree ret = build_nt_call_vec (orig_fn, orig_args);
2140 release_tree_vector (orig_args);
2143 object = build_non_dependent_expr (object);
2146 result = build_new_method_call (object, fn, args, NULL_TREE,
2148 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2153 else if (is_overloaded_fn (fn))
2155 /* If the function is an overloaded builtin, resolve it. */
2156 if (TREE_CODE (fn) == FUNCTION_DECL
2157 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2158 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2159 result = resolve_overloaded_builtin (input_location, fn, *args);
2162 /* A call to a namespace-scope function. */
2163 result = build_new_function_call (fn, args, koenig_p, complain);
2165 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2167 if (!VEC_empty (tree, *args))
2168 error ("arguments to destructor are not allowed");
2169 /* Mark the pseudo-destructor call as having side-effects so
2170 that we do not issue warnings about its use. */
2171 result = build1 (NOP_EXPR,
2173 TREE_OPERAND (fn, 0));
2174 TREE_SIDE_EFFECTS (result) = 1;
2176 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2177 /* If the "function" is really an object of class type, it might
2178 have an overloaded `operator ()'. */
2179 result = build_op_call (fn, args, complain);
2182 /* A call where the function is unknown. */
2183 result = cp_build_function_call_vec (fn, args, complain);
2185 if (processing_template_decl && result != error_mark_node)
2187 if (TREE_CODE (result) == INDIRECT_REF)
2188 result = TREE_OPERAND (result, 0);
2189 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2190 SET_EXPR_LOCATION (result, input_location);
2191 KOENIG_LOOKUP_P (result) = koenig_p;
2192 release_tree_vector (orig_args);
2193 result = convert_from_reference (result);
2198 /* Free garbage OVERLOADs from arg-dependent lookup. */
2199 tree next = NULL_TREE;
2201 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2204 if (processing_template_decl)
2205 /* In a template, we'll re-use them at instantiation time. */
2206 OVL_ARG_DEPENDENT (fn) = false;
2209 next = OVL_CHAIN (fn);
2218 /* Finish a call to a postfix increment or decrement or EXPR. (Which
2219 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2220 POSTDECREMENT_EXPR.) */
2223 finish_increment_expr (tree expr, enum tree_code code)
2225 return build_x_unary_op (code, expr, tf_warning_or_error);
2228 /* Finish a use of `this'. Returns an expression for `this'. */
2231 finish_this_expr (void)
2235 if (current_class_ptr)
2237 tree type = TREE_TYPE (current_class_ref);
2239 /* In a lambda expression, 'this' refers to the captured 'this'. */
2240 if (LAMBDA_TYPE_P (type))
2241 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2243 result = current_class_ptr;
2246 else if (current_function_decl
2247 && DECL_STATIC_FUNCTION_P (current_function_decl))
2249 error ("%<this%> is unavailable for static member functions");
2250 result = error_mark_node;
2254 if (current_function_decl)
2255 error ("invalid use of %<this%> in non-member function");
2257 error ("invalid use of %<this%> at top level");
2258 result = error_mark_node;
2264 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2265 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2266 the TYPE for the type given. If SCOPE is non-NULL, the expression
2267 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2270 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
2272 if (object == error_mark_node || destructor == error_mark_node)
2273 return error_mark_node;
2275 gcc_assert (TYPE_P (destructor));
2277 if (!processing_template_decl)
2279 if (scope == error_mark_node)
2281 error ("invalid qualifying scope in pseudo-destructor name");
2282 return error_mark_node;
2284 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2286 error ("qualified type %qT does not match destructor name ~%qT",
2288 return error_mark_node;
2292 /* [expr.pseudo] says both:
2294 The type designated by the pseudo-destructor-name shall be
2295 the same as the object type.
2299 The cv-unqualified versions of the object type and of the
2300 type designated by the pseudo-destructor-name shall be the
2303 We implement the more generous second sentence, since that is
2304 what most other compilers do. */
2305 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2308 error ("%qE is not of type %qT", object, destructor);
2309 return error_mark_node;
2313 return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
2316 /* Finish an expression of the form CODE EXPR. */
2319 finish_unary_op_expr (enum tree_code code, tree expr)
2321 tree result = build_x_unary_op (code, expr, tf_warning_or_error);
2322 if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2323 overflow_warning (input_location, result);
2328 /* Finish a compound-literal expression. TYPE is the type to which
2329 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2332 finish_compound_literal (tree type, tree compound_literal,
2333 tsubst_flags_t complain)
2335 if (type == error_mark_node)
2336 return error_mark_node;
2338 if (TREE_CODE (type) == REFERENCE_TYPE)
2341 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2343 return cp_build_c_cast (type, compound_literal, complain);
2346 if (!TYPE_OBJ_P (type))
2348 if (complain & tf_error)
2349 error ("compound literal of non-object type %qT", type);
2350 return error_mark_node;
2353 if (processing_template_decl)
2355 TREE_TYPE (compound_literal) = type;
2356 /* Mark the expression as a compound literal. */
2357 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2358 return compound_literal;
2361 type = complete_type (type);
2363 if (TYPE_NON_AGGREGATE_CLASS (type))
2365 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2366 everywhere that deals with function arguments would be a pain, so
2367 just wrap it in a TREE_LIST. The parser set a flag so we know
2368 that it came from T{} rather than T({}). */
2369 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2370 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2371 return build_functional_cast (type, compound_literal, complain);
2374 if (TREE_CODE (type) == ARRAY_TYPE
2375 && check_array_initializer (NULL_TREE, type, compound_literal))
2376 return error_mark_node;
2377 compound_literal = reshape_init (type, compound_literal, complain);
2378 if (SCALAR_TYPE_P (type)
2379 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2380 && (complain & tf_warning_or_error))
2381 check_narrowing (type, compound_literal);
2382 if (TREE_CODE (type) == ARRAY_TYPE
2383 && TYPE_DOMAIN (type) == NULL_TREE)
2385 cp_complete_array_type_or_error (&type, compound_literal,
2387 if (type == error_mark_node)
2388 return error_mark_node;
2390 compound_literal = digest_init (type, compound_literal, complain);
2391 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2392 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2393 /* Put static/constant array temporaries in static variables, but always
2394 represent class temporaries with TARGET_EXPR so we elide copies. */
2395 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2396 && TREE_CODE (type) == ARRAY_TYPE
2397 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2398 && initializer_constant_valid_p (compound_literal, type))
2400 tree decl = create_temporary_var (type);
2401 DECL_INITIAL (decl) = compound_literal;
2402 TREE_STATIC (decl) = 1;
2403 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2405 /* 5.19 says that a constant expression can include an
2406 lvalue-rvalue conversion applied to "a glvalue of literal type
2407 that refers to a non-volatile temporary object initialized
2408 with a constant expression". Rather than try to communicate
2409 that this VAR_DECL is a temporary, just mark it constexpr. */
2410 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2411 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2412 TREE_CONSTANT (decl) = true;
2414 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2415 decl = pushdecl_top_level (decl);
2416 DECL_NAME (decl) = make_anon_name ();
2417 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2421 return get_target_expr_sfinae (compound_literal, complain);
2424 /* Return the declaration for the function-name variable indicated by
2428 finish_fname (tree id)
2432 decl = fname_decl (input_location, C_RID_CODE (id), id);
2433 if (processing_template_decl && current_function_decl)
2434 decl = DECL_NAME (decl);
2438 /* Finish a translation unit. */
2441 finish_translation_unit (void)
2443 /* In case there were missing closebraces,
2444 get us back to the global binding level. */
2446 while (current_namespace != global_namespace)
2449 /* Do file scope __FUNCTION__ et al. */
2450 finish_fname_decls ();
2453 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2454 Returns the parameter. */
2457 finish_template_type_parm (tree aggr, tree identifier)
2459 if (aggr != class_type_node)
2461 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2462 aggr = class_type_node;
2465 return build_tree_list (aggr, identifier);
2468 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2469 Returns the parameter. */
2472 finish_template_template_parm (tree aggr, tree identifier)
2474 tree decl = build_decl (input_location,
2475 TYPE_DECL, identifier, NULL_TREE);
2476 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2477 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2478 DECL_TEMPLATE_RESULT (tmpl) = decl;
2479 DECL_ARTIFICIAL (decl) = 1;
2480 end_template_decl ();
2482 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2484 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2485 /*is_primary=*/true, /*is_partial=*/false,
2488 return finish_template_type_parm (aggr, tmpl);
2491 /* ARGUMENT is the default-argument value for a template template
2492 parameter. If ARGUMENT is invalid, issue error messages and return
2493 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2496 check_template_template_default_arg (tree argument)
2498 if (TREE_CODE (argument) != TEMPLATE_DECL
2499 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2500 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2502 if (TREE_CODE (argument) == TYPE_DECL)
2503 error ("invalid use of type %qT as a default value for a template "
2504 "template-parameter", TREE_TYPE (argument));
2506 error ("invalid default argument for a template template parameter");
2507 return error_mark_node;
2513 /* Begin a class definition, as indicated by T. */
2516 begin_class_definition (tree t)
2518 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2519 return error_mark_node;
2521 if (processing_template_parmlist)
2523 error ("definition of %q#T inside template parameter list", t);
2524 return error_mark_node;
2527 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2528 are passed the same as decimal scalar types. */
2529 if (TREE_CODE (t) == RECORD_TYPE
2530 && !processing_template_decl)
2532 tree ns = TYPE_CONTEXT (t);
2533 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2534 && DECL_CONTEXT (ns) == std_node
2536 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2538 const char *n = TYPE_NAME_STRING (t);
2539 if ((strcmp (n, "decimal32") == 0)
2540 || (strcmp (n, "decimal64") == 0)
2541 || (strcmp (n, "decimal128") == 0))
2542 TYPE_TRANSPARENT_AGGR (t) = 1;
2546 /* A non-implicit typename comes from code like:
2548 template <typename T> struct A {
2549 template <typename U> struct A<T>::B ...
2551 This is erroneous. */
2552 else if (TREE_CODE (t) == TYPENAME_TYPE)
2554 error ("invalid definition of qualified type %qT", t);
2555 t = error_mark_node;
2558 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2560 t = make_class_type (RECORD_TYPE);
2561 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2564 if (TYPE_BEING_DEFINED (t))
2566 t = make_class_type (TREE_CODE (t));
2567 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2569 maybe_process_partial_specialization (t);
2571 TYPE_BEING_DEFINED (t) = 1;
2573 if (flag_pack_struct)
2576 TYPE_PACKED (t) = 1;
2577 /* Even though the type is being defined for the first time
2578 here, there might have been a forward declaration, so there
2579 might be cv-qualified variants of T. */
2580 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2581 TYPE_PACKED (v) = 1;
2583 /* Reset the interface data, at the earliest possible
2584 moment, as it might have been set via a class foo;
2586 if (! TYPE_ANONYMOUS_P (t))
2588 struct c_fileinfo *finfo = get_fileinfo (input_filename);
2589 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2590 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2591 (t, finfo->interface_unknown);
2593 reset_specialization();
2595 /* Make a declaration for this class in its own scope. */
2596 build_self_reference ();
2601 /* Finish the member declaration given by DECL. */
2604 finish_member_declaration (tree decl)
2606 if (decl == error_mark_node || decl == NULL_TREE)
2609 if (decl == void_type_node)
2610 /* The COMPONENT was a friend, not a member, and so there's
2611 nothing for us to do. */
2614 /* We should see only one DECL at a time. */
2615 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2617 /* Set up access control for DECL. */
2619 = (current_access_specifier == access_private_node);
2620 TREE_PROTECTED (decl)
2621 = (current_access_specifier == access_protected_node);
2622 if (TREE_CODE (decl) == TEMPLATE_DECL)
2624 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2625 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2628 /* Mark the DECL as a member of the current class. */
2629 DECL_CONTEXT (decl) = current_class_type;
2631 /* Check for bare parameter packs in the member variable declaration. */
2632 if (TREE_CODE (decl) == FIELD_DECL)
2634 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2635 TREE_TYPE (decl) = error_mark_node;
2636 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2637 DECL_ATTRIBUTES (decl) = NULL_TREE;
2642 A C language linkage is ignored for the names of class members
2643 and the member function type of class member functions. */
2644 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2645 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2647 /* Put functions on the TYPE_METHODS list and everything else on the
2648 TYPE_FIELDS list. Note that these are built up in reverse order.
2649 We reverse them (to obtain declaration order) in finish_struct. */
2650 if (TREE_CODE (decl) == FUNCTION_DECL
2651 || DECL_FUNCTION_TEMPLATE_P (decl))
2653 /* We also need to add this function to the
2654 CLASSTYPE_METHOD_VEC. */
2655 if (add_method (current_class_type, decl, NULL_TREE))
2657 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2658 TYPE_METHODS (current_class_type) = decl;
2660 maybe_add_class_template_decl_list (current_class_type, decl,
2664 /* Enter the DECL into the scope of the class. */
2665 else if (pushdecl_class_level (decl))
2667 if (TREE_CODE (decl) == USING_DECL)
2669 /* For now, ignore class-scope USING_DECLS, so that
2670 debugging backends do not see them. */
2671 DECL_IGNORED_P (decl) = 1;
2674 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2675 go at the beginning. The reason is that lookup_field_1
2676 searches the list in order, and we want a field name to
2677 override a type name so that the "struct stat hack" will
2678 work. In particular:
2680 struct S { enum E { }; int E } s;
2683 is valid. In addition, the FIELD_DECLs must be maintained in
2684 declaration order so that class layout works as expected.
2685 However, we don't need that order until class layout, so we
2686 save a little time by putting FIELD_DECLs on in reverse order
2687 here, and then reversing them in finish_struct_1. (We could
2688 also keep a pointer to the correct insertion points in the
2691 if (TREE_CODE (decl) == TYPE_DECL)
2692 TYPE_FIELDS (current_class_type)
2693 = chainon (TYPE_FIELDS (current_class_type), decl);
2696 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2697 TYPE_FIELDS (current_class_type) = decl;
2700 maybe_add_class_template_decl_list (current_class_type, decl,
2705 note_decl_for_pch (decl);
2708 /* DECL has been declared while we are building a PCH file. Perform
2709 actions that we might normally undertake lazily, but which can be
2710 performed now so that they do not have to be performed in
2711 translation units which include the PCH file. */
2714 note_decl_for_pch (tree decl)
2716 gcc_assert (pch_file);
2718 /* There's a good chance that we'll have to mangle names at some
2719 point, even if only for emission in debugging information. */
2720 if ((TREE_CODE (decl) == VAR_DECL
2721 || TREE_CODE (decl) == FUNCTION_DECL)
2722 && !processing_template_decl)
2726 /* Finish processing a complete template declaration. The PARMS are
2727 the template parameters. */
2730 finish_template_decl (tree parms)
2733 end_template_decl ();
2735 end_specialization ();
2738 /* Finish processing a template-id (which names a type) of the form
2739 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2740 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2741 the scope of template-id indicated. */
2744 finish_template_type (tree name, tree args, int entering_scope)
2748 type = lookup_template_class (name, args,
2749 NULL_TREE, NULL_TREE, entering_scope,
2750 tf_warning_or_error | tf_user);
2751 if (type == error_mark_node)
2753 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2754 return TYPE_STUB_DECL (type);
2756 return TYPE_NAME (type);
2759 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2760 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2761 BASE_CLASS, or NULL_TREE if an error occurred. The
2762 ACCESS_SPECIFIER is one of
2763 access_{default,public,protected_private}_node. For a virtual base
2764 we set TREE_TYPE. */
2767 finish_base_specifier (tree base, tree access, bool virtual_p)
2771 if (base == error_mark_node)
2773 error ("invalid base-class specification");
2776 else if (! MAYBE_CLASS_TYPE_P (base))
2778 error ("%qT is not a class type", base);
2783 if (cp_type_quals (base) != 0)
2785 /* DR 484: Can a base-specifier name a cv-qualified
2787 base = TYPE_MAIN_VARIANT (base);
2789 result = build_tree_list (access, base);
2791 TREE_TYPE (result) = integer_type_node;
2797 /* If FNS is a member function, a set of member functions, or a
2798 template-id referring to one or more member functions, return a
2799 BASELINK for FNS, incorporating the current access context.
2800 Otherwise, return FNS unchanged. */
2803 baselink_for_fns (tree fns)
2808 if (BASELINK_P (fns)
2809 || error_operand_p (fns))
2812 scope = ovl_scope (fns);
2813 if (!CLASS_TYPE_P (scope))
2816 cl = currently_open_derived_class (scope);
2819 cl = TYPE_BINFO (cl);
2820 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2823 /* Returns true iff DECL is a variable from a function outside
2827 outer_var_p (tree decl)
2829 return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
2830 && DECL_FUNCTION_SCOPE_P (decl)
2831 && DECL_CONTEXT (decl) != current_function_decl);
2834 /* As above, but also checks that DECL is automatic. */
2837 outer_automatic_var_p (tree decl)
2839 return (outer_var_p (decl)
2840 && !TREE_STATIC (decl));
2843 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2844 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2845 if non-NULL, is the type or namespace used to explicitly qualify
2846 ID_EXPRESSION. DECL is the entity to which that name has been
2849 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2850 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2851 be set to true if this expression isn't permitted in a
2852 constant-expression, but it is otherwise not set by this function.
2853 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2854 constant-expression, but a non-constant expression is also
2857 DONE is true if this expression is a complete postfix-expression;
2858 it is false if this expression is followed by '->', '[', '(', etc.
2859 ADDRESS_P is true iff this expression is the operand of '&'.
2860 TEMPLATE_P is true iff the qualified-id was of the form
2861 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2862 appears as a template argument.
2864 If an error occurs, and it is the kind of error that might cause
2865 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2866 is the caller's responsibility to issue the message. *ERROR_MSG
2867 will be a string with static storage duration, so the caller need
2870 Return an expression for the entity, after issuing appropriate
2871 diagnostics. This function is also responsible for transforming a
2872 reference to a non-static member into a COMPONENT_REF that makes
2873 the use of "this" explicit.
2875 Upon return, *IDK will be filled in appropriately. */
2877 finish_id_expression (tree id_expression,
2881 bool integral_constant_expression_p,
2882 bool allow_non_integral_constant_expression_p,
2883 bool *non_integral_constant_expression_p,
2887 bool template_arg_p,
2888 const char **error_msg,
2889 location_t location)
2891 decl = strip_using_decl (decl);
2893 /* Initialize the output parameters. */
2894 *idk = CP_ID_KIND_NONE;
2897 if (id_expression == error_mark_node)
2898 return error_mark_node;
2899 /* If we have a template-id, then no further lookup is
2900 required. If the template-id was for a template-class, we
2901 will sometimes have a TYPE_DECL at this point. */
2902 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2903 || TREE_CODE (decl) == TYPE_DECL)
2905 /* Look up the name. */
2908 if (decl == error_mark_node)
2910 /* Name lookup failed. */
2913 || (!dependent_type_p (scope)
2914 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2915 && IDENTIFIER_TYPENAME_P (id_expression)
2916 && dependent_type_p (TREE_TYPE (id_expression))))))
2918 /* If the qualifying type is non-dependent (and the name
2919 does not name a conversion operator to a dependent
2920 type), issue an error. */
2921 qualified_name_lookup_error (scope, id_expression, decl, location);
2922 return error_mark_node;
2926 /* It may be resolved via Koenig lookup. */
2927 *idk = CP_ID_KIND_UNQUALIFIED;
2928 return id_expression;
2931 decl = id_expression;
2933 /* If DECL is a variable that would be out of scope under
2934 ANSI/ISO rules, but in scope in the ARM, name lookup
2935 will succeed. Issue a diagnostic here. */
2937 decl = check_for_out_of_scope_variable (decl);
2939 /* Remember that the name was used in the definition of
2940 the current class so that we can check later to see if
2941 the meaning would have been different after the class
2942 was entirely defined. */
2943 if (!scope && decl != error_mark_node
2944 && TREE_CODE (id_expression) == IDENTIFIER_NODE)
2945 maybe_note_name_used_in_class (id_expression, decl);
2947 /* Disallow uses of local variables from containing functions, except
2948 within lambda-expressions. */
2949 if (!outer_var_p (decl)
2950 /* It's not a use (3.2) if we're in an unevaluated context. */
2951 || cp_unevaluated_operand)
2953 else if (TREE_STATIC (decl))
2955 if (processing_template_decl)
2956 /* For a use of an outer static var, return the identifier so
2957 that we'll look it up again in the instantiation. */
2958 return id_expression;
2962 tree context = DECL_CONTEXT (decl);
2963 tree containing_function = current_function_decl;
2964 tree lambda_stack = NULL_TREE;
2965 tree lambda_expr = NULL_TREE;
2966 tree initializer = convert_from_reference (decl);
2968 /* Mark it as used now even if the use is ill-formed. */
2971 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
2972 support for an approach in which a reference to a local
2973 [constant] automatic variable in a nested class or lambda body
2974 would enter the expression as an rvalue, which would reduce
2975 the complexity of the problem"
2977 FIXME update for final resolution of core issue 696. */
2978 if (decl_constant_var_p (decl))
2980 if (processing_template_decl)
2981 /* In a template, the constant value may not be in a usable
2982 form, so look it up again at instantiation time. */
2983 return id_expression;
2985 return integral_constant_value (decl);
2988 /* If we are in a lambda function, we can move out until we hit
2990 2. a non-lambda function, or
2991 3. a non-default capturing lambda function. */
2992 while (context != containing_function
2993 && LAMBDA_FUNCTION_P (containing_function))
2995 lambda_expr = CLASSTYPE_LAMBDA_EXPR
2996 (DECL_CONTEXT (containing_function));
2998 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3002 lambda_stack = tree_cons (NULL_TREE,
3007 = decl_function_context (containing_function);
3010 if (context == containing_function)
3012 decl = add_default_capture (lambda_stack,
3013 /*id=*/DECL_NAME (decl),
3016 else if (lambda_expr)
3018 error ("%qD is not captured", decl);
3019 return error_mark_node;
3023 error (TREE_CODE (decl) == VAR_DECL
3024 ? G_("use of %<auto%> variable from containing function")
3025 : G_("use of parameter from containing function"));
3026 error (" %q+#D declared here", decl);
3027 return error_mark_node;
3031 /* Also disallow uses of function parameters outside the function
3032 body, except inside an unevaluated context (i.e. decltype). */
3033 if (TREE_CODE (decl) == PARM_DECL
3034 && DECL_CONTEXT (decl) == NULL_TREE
3035 && !cp_unevaluated_operand)
3037 error ("use of parameter %qD outside function body", decl);
3038 return error_mark_node;
3042 /* If we didn't find anything, or what we found was a type,
3043 then this wasn't really an id-expression. */
3044 if (TREE_CODE (decl) == TEMPLATE_DECL
3045 && !DECL_FUNCTION_TEMPLATE_P (decl))
3047 *error_msg = "missing template arguments";
3048 return error_mark_node;
3050 else if (TREE_CODE (decl) == TYPE_DECL
3051 || TREE_CODE (decl) == NAMESPACE_DECL)
3053 *error_msg = "expected primary-expression";
3054 return error_mark_node;
3057 /* If the name resolved to a template parameter, there is no
3058 need to look it up again later. */
3059 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3060 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3064 *idk = CP_ID_KIND_NONE;
3065 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3066 decl = TEMPLATE_PARM_DECL (decl);
3067 r = convert_from_reference (DECL_INITIAL (decl));
3069 if (integral_constant_expression_p
3070 && !dependent_type_p (TREE_TYPE (decl))
3071 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3073 if (!allow_non_integral_constant_expression_p)
3074 error ("template parameter %qD of type %qT is not allowed in "
3075 "an integral constant expression because it is not of "
3076 "integral or enumeration type", decl, TREE_TYPE (decl));
3077 *non_integral_constant_expression_p = true;
3081 /* Similarly, we resolve enumeration constants to their
3082 underlying values. */
3083 else if (TREE_CODE (decl) == CONST_DECL)
3085 *idk = CP_ID_KIND_NONE;
3086 if (!processing_template_decl)
3088 used_types_insert (TREE_TYPE (decl));
3089 return DECL_INITIAL (decl);
3097 /* If the declaration was explicitly qualified indicate
3098 that. The semantics of `A::f(3)' are different than
3099 `f(3)' if `f' is virtual. */
3101 ? CP_ID_KIND_QUALIFIED
3102 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3103 ? CP_ID_KIND_TEMPLATE_ID
3104 : CP_ID_KIND_UNQUALIFIED));
3109 An id-expression is type-dependent if it contains an
3110 identifier that was declared with a dependent type.
3112 The standard is not very specific about an id-expression that
3113 names a set of overloaded functions. What if some of them
3114 have dependent types and some of them do not? Presumably,
3115 such a name should be treated as a dependent name. */
3116 /* Assume the name is not dependent. */
3117 dependent_p = false;
3118 if (!processing_template_decl)
3119 /* No names are dependent outside a template. */
3121 /* A template-id where the name of the template was not resolved
3122 is definitely dependent. */
3123 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3124 && (TREE_CODE (TREE_OPERAND (decl, 0))
3125 == IDENTIFIER_NODE))
3127 /* For anything except an overloaded function, just check its
3129 else if (!is_overloaded_fn (decl))
3131 = dependent_type_p (TREE_TYPE (decl));
3132 /* For a set of overloaded functions, check each of the
3138 if (BASELINK_P (fns))
3139 fns = BASELINK_FUNCTIONS (fns);
3141 /* For a template-id, check to see if the template
3142 arguments are dependent. */
3143 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3145 tree args = TREE_OPERAND (fns, 1);
3146 dependent_p = any_dependent_template_arguments_p (args);
3147 /* The functions are those referred to by the
3149 fns = TREE_OPERAND (fns, 0);
3152 /* If there are no dependent template arguments, go through
3153 the overloaded functions. */
3154 while (fns && !dependent_p)
3156 tree fn = OVL_CURRENT (fns);
3158 /* Member functions of dependent classes are
3160 if (TREE_CODE (fn) == FUNCTION_DECL
3161 && type_dependent_expression_p (fn))
3163 else if (TREE_CODE (fn) == TEMPLATE_DECL
3164 && dependent_template_p (fn))
3167 fns = OVL_NEXT (fns);
3171 /* If the name was dependent on a template parameter, we will
3172 resolve the name at instantiation time. */
3175 /* Create a SCOPE_REF for qualified names, if the scope is
3181 if (address_p && done)
3182 decl = finish_qualified_id_expr (scope, decl,
3188 tree type = NULL_TREE;
3189 if (DECL_P (decl) && !dependent_scope_p (scope))
3190 type = TREE_TYPE (decl);
3191 decl = build_qualified_name (type,
3197 if (TREE_TYPE (decl))
3198 decl = convert_from_reference (decl);
3201 /* A TEMPLATE_ID already contains all the information we
3203 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3204 return id_expression;
3205 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3206 /* If we found a variable, then name lookup during the
3207 instantiation will always resolve to the same VAR_DECL
3208 (or an instantiation thereof). */
3209 if (TREE_CODE (decl) == VAR_DECL
3210 || TREE_CODE (decl) == PARM_DECL)
3213 return convert_from_reference (decl);
3215 /* The same is true for FIELD_DECL, but we also need to
3216 make sure that the syntax is correct. */
3217 else if (TREE_CODE (decl) == FIELD_DECL)
3219 /* Since SCOPE is NULL here, this is an unqualified name.
3220 Access checking has been performed during name lookup
3221 already. Turn off checking to avoid duplicate errors. */
3222 push_deferring_access_checks (dk_no_check);
3223 decl = finish_non_static_data_member
3225 /*qualifying_scope=*/NULL_TREE);
3226 pop_deferring_access_checks ();
3229 return id_expression;
3232 if (TREE_CODE (decl) == NAMESPACE_DECL)
3234 error ("use of namespace %qD as expression", decl);
3235 return error_mark_node;
3237 else if (DECL_CLASS_TEMPLATE_P (decl))
3239 error ("use of class template %qT as expression", decl);
3240 return error_mark_node;
3242 else if (TREE_CODE (decl) == TREE_LIST)
3244 /* Ambiguous reference to base members. */
3245 error ("request for member %qD is ambiguous in "
3246 "multiple inheritance lattice", id_expression);
3247 print_candidates (decl);
3248 return error_mark_node;
3251 /* Mark variable-like entities as used. Functions are similarly
3252 marked either below or after overload resolution. */
3253 if (TREE_CODE (decl) == VAR_DECL
3254 || TREE_CODE (decl) == PARM_DECL
3255 || TREE_CODE (decl) == RESULT_DECL)
3258 /* Only certain kinds of names are allowed in constant
3259 expression. Enumerators and template parameters have already
3260 been handled above. */
3261 if (! error_operand_p (decl)
3262 && integral_constant_expression_p
3263 && ! decl_constant_var_p (decl)
3264 && ! builtin_valid_in_constant_expr_p (decl))
3266 if (!allow_non_integral_constant_expression_p)
3268 error ("%qD cannot appear in a constant-expression", decl);
3269 return error_mark_node;
3271 *non_integral_constant_expression_p = true;
3276 decl = (adjust_result_of_qualified_name_lookup
3277 (decl, scope, current_nonlambda_class_type()));
3279 if (TREE_CODE (decl) == FUNCTION_DECL)
3282 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3283 decl = finish_qualified_id_expr (scope,
3291 tree r = convert_from_reference (decl);
3293 /* In a template, return a SCOPE_REF for most qualified-ids
3294 so that we can check access at instantiation time. But if
3295 we're looking at a member of the current instantiation, we
3296 know we have access and building up the SCOPE_REF confuses
3297 non-type template argument handling. */
3298 if (processing_template_decl && TYPE_P (scope)
3299 && !currently_open_class (scope))
3300 r = build_qualified_name (TREE_TYPE (r),
3306 else if (TREE_CODE (decl) == FIELD_DECL)
3308 /* Since SCOPE is NULL here, this is an unqualified name.
3309 Access checking has been performed during name lookup
3310 already. Turn off checking to avoid duplicate errors. */
3311 push_deferring_access_checks (dk_no_check);
3312 decl = finish_non_static_data_member (decl, NULL_TREE,
3313 /*qualifying_scope=*/NULL_TREE);
3314 pop_deferring_access_checks ();
3316 else if (is_overloaded_fn (decl))
3320 first_fn = get_first_fn (decl);
3321 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3322 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3324 if (!really_overloaded_fn (decl)
3325 && !mark_used (first_fn))
3326 return error_mark_node;
3329 && TREE_CODE (first_fn) == FUNCTION_DECL
3330 && DECL_FUNCTION_MEMBER_P (first_fn)
3331 && !shared_member_p (decl))
3333 /* A set of member functions. */
3334 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3335 return finish_class_member_access_expr (decl, id_expression,
3336 /*template_p=*/false,
3337 tf_warning_or_error);
3340 decl = baselink_for_fns (decl);
3344 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3345 && DECL_CLASS_SCOPE_P (decl))
3347 tree context = context_for_name_lookup (decl);
3348 if (context != current_class_type)
3350 tree path = currently_open_derived_class (context);
3351 perform_or_defer_access_check (TYPE_BINFO (path),
3356 decl = convert_from_reference (decl);
3360 if (TREE_DEPRECATED (decl))
3361 warn_deprecated_use (decl, NULL_TREE);
3366 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3367 use as a type-specifier. */
3370 finish_typeof (tree expr)
3374 if (type_dependent_expression_p (expr))
3376 type = cxx_make_type (TYPEOF_TYPE);
3377 TYPEOF_TYPE_EXPR (type) = expr;
3378 SET_TYPE_STRUCTURAL_EQUALITY (type);
3383 expr = mark_type_use (expr);
3385 type = unlowered_expr_type (expr);
3387 if (!type || type == unknown_type_node)
3389 error ("type of %qE is unknown", expr);
3390 return error_mark_node;
3396 /* Implement the __underlying_type keyword: Return the underlying
3397 type of TYPE, suitable for use as a type-specifier. */
3400 finish_underlying_type (tree type)
3402 tree underlying_type;
3404 if (processing_template_decl)
3406 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3407 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3408 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3410 return underlying_type;
3413 complete_type (type);
3415 if (TREE_CODE (type) != ENUMERAL_TYPE)
3417 error ("%qT is not an enumeration type", type);
3418 return error_mark_node;
3421 underlying_type = ENUM_UNDERLYING_TYPE (type);
3423 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3424 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3425 See finish_enum_value_list for details. */
3426 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3428 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3429 TYPE_UNSIGNED (underlying_type));
3431 return underlying_type;
3434 /* Implement the __direct_bases keyword: Return the direct base classes
3438 calculate_direct_bases (tree type)
3440 VEC(tree, gc) *vector = make_tree_vector();
3441 tree bases_vec = NULL_TREE;
3442 VEC(tree, none) *base_binfos;
3446 complete_type (type);
3448 if (!NON_UNION_CLASS_TYPE_P (type))
3449 return make_tree_vec (0);
3451 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3453 /* Virtual bases are initialized first */
3454 for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3456 if (BINFO_VIRTUAL_P (binfo))
3458 VEC_safe_push (tree, gc, vector, binfo);
3462 /* Now non-virtuals */
3463 for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3465 if (!BINFO_VIRTUAL_P (binfo))
3467 VEC_safe_push (tree, gc, vector, binfo);
3472 bases_vec = make_tree_vec (VEC_length (tree, vector));
3474 for (i = 0; i < VEC_length (tree, vector); ++i)
3476 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE (VEC_index (tree, vector, i));
3481 /* Implement the __bases keyword: Return the base classes
3484 /* Find morally non-virtual base classes by walking binfo hierarchy */
3485 /* Virtual base classes are handled separately in finish_bases */
3488 dfs_calculate_bases_pre (tree binfo, ATTRIBUTE_UNUSED void *data_)
3490 /* Don't walk bases of virtual bases */
3491 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3495 dfs_calculate_bases_post (tree binfo, void *data_)
3497 VEC(tree, gc) **data = (VEC(tree, gc) **) data_;
3498 if (!BINFO_VIRTUAL_P (binfo))
3500 VEC_safe_push (tree, gc, *data, BINFO_TYPE (binfo));
3505 /* Calculates the morally non-virtual base classes of a class */
3506 static VEC(tree, gc) *
3507 calculate_bases_helper (tree type)
3509 VEC(tree, gc) *vector = make_tree_vector();
3511 /* Now add non-virtual base classes in order of construction */
3512 dfs_walk_all (TYPE_BINFO (type),
3513 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3518 calculate_bases (tree type)
3520 VEC(tree, gc) *vector = make_tree_vector();
3521 tree bases_vec = NULL_TREE;
3523 VEC(tree, gc) *vbases;
3524 VEC(tree, gc) *nonvbases;
3527 complete_type (type);
3529 if (!NON_UNION_CLASS_TYPE_P (type))
3530 return make_tree_vec (0);
3532 /* First go through virtual base classes */
3533 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3534 VEC_iterate (tree, vbases, i, binfo); i++)
3536 VEC(tree, gc) *vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3537 VEC_safe_splice (tree, gc, vector, vbase_bases);
3538 release_tree_vector (vbase_bases);
3541 /* Now for the non-virtual bases */
3542 nonvbases = calculate_bases_helper (type);
3543 VEC_safe_splice (tree, gc, vector, nonvbases);
3544 release_tree_vector (nonvbases);
3546 /* Last element is entire class, so don't copy */
3547 bases_vec = make_tree_vec (VEC_length (tree, vector) - 1);
3549 for (i = 0; i < VEC_length (tree, vector) - 1; ++i)
3551 TREE_VEC_ELT (bases_vec, i) = VEC_index (tree, vector, i);