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
7 Free Software Foundation, Inc.
8 Written by Mark Mitchell (mmitchell@usa.net) based on code found
9 formerly in parse.y and pt.c.
11 This file is part of GCC.
13 GCC is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
18 GCC is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING. If not, write to the Free
25 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
30 #include "coretypes.h"
35 #include "tree-inline.h"
36 #include "tree-mudflap.h"
45 #include "diagnostic.h"
47 #include "tree-iterator.h"
51 /* There routines provide a modular interface to perform many parsing
52 operations. They may therefore be used during actual parsing, or
53 during template instantiation, which may be regarded as a
54 degenerate form of parsing. Since the current g++ parser is
55 lacking in several respects, and will be reimplemented, we are
56 attempting to move most code that is not directly related to
57 parsing into this file; that will make implementing the new parser
58 much easier since it will be able to make use of these routines. */
60 static tree do_poplevel (tree);
61 static tree maybe_convert_cond (tree);
62 static tree simplify_aggr_init_exprs_r (tree *, int *, void *);
63 static void emit_associated_thunks (tree);
64 static tree finalize_nrv_r (tree *, int *, void *);
67 /* Deferred Access Checking Overview
68 ---------------------------------
70 Most C++ expressions and declarations require access checking
71 to be performed during parsing. However, in several cases,
72 this has to be treated differently.
74 For member declarations, access checking has to be deferred
75 until more information about the declaration is known. For
87 When we are parsing the function return type `A::X', we don't
88 really know if this is allowed until we parse the function name.
90 Furthermore, some contexts require that access checking is
91 never performed at all. These include class heads, and template
94 Typical use of access checking functions is described here:
96 1. When we enter a context that requires certain access checking
97 mode, the function `push_deferring_access_checks' is called with
98 DEFERRING argument specifying the desired mode. Access checking
99 may be performed immediately (dk_no_deferred), deferred
100 (dk_deferred), or not performed (dk_no_check).
102 2. When a declaration such as a type, or a variable, is encountered,
103 the function `perform_or_defer_access_check' is called. It
104 maintains a TREE_LIST of all deferred checks.
106 3. The global `current_class_type' or `current_function_decl' is then
107 setup by the parser. `enforce_access' relies on these information
110 4. Upon exiting the context mentioned in step 1,
111 `perform_deferred_access_checks' is called to check all declaration
112 stored in the TREE_LIST. `pop_deferring_access_checks' is then
113 called to restore the previous access checking mode.
115 In case of parsing error, we simply call `pop_deferring_access_checks'
116 without `perform_deferred_access_checks'. */
118 typedef struct deferred_access GTY(())
120 /* A TREE_LIST representing name-lookups for which we have deferred
121 checking access controls. We cannot check the accessibility of
122 names used in a decl-specifier-seq until we know what is being
123 declared because code like:
130 A::B* A::f() { return 0; }
132 is valid, even though `A::B' is not generally accessible.
134 The TREE_PURPOSE of each node is the scope used to qualify the
135 name being looked up; the TREE_VALUE is the DECL to which the
136 name was resolved. */
137 tree deferred_access_checks;
139 /* The current mode of access checks. */
140 enum deferring_kind deferring_access_checks_kind;
143 DEF_VEC_O (deferred_access);
144 DEF_VEC_ALLOC_O (deferred_access,gc);
146 /* Data for deferred access checking. */
147 static GTY(()) VEC(deferred_access,gc) *deferred_access_stack;
148 static GTY(()) unsigned deferred_access_no_check;
150 /* Save the current deferred access states and start deferred
151 access checking iff DEFER_P is true. */
154 push_deferring_access_checks (deferring_kind deferring)
156 /* For context like template instantiation, access checking
157 disabling applies to all nested context. */
158 if (deferred_access_no_check || deferring == dk_no_check)
159 deferred_access_no_check++;
162 deferred_access *ptr;
164 ptr = VEC_safe_push (deferred_access, gc, deferred_access_stack, NULL);
165 ptr->deferred_access_checks = NULL_TREE;
166 ptr->deferring_access_checks_kind = deferring;
170 /* Resume deferring access checks again after we stopped doing
174 resume_deferring_access_checks (void)
176 if (!deferred_access_no_check)
177 VEC_last (deferred_access, deferred_access_stack)
178 ->deferring_access_checks_kind = dk_deferred;
181 /* Stop deferring access checks. */
184 stop_deferring_access_checks (void)
186 if (!deferred_access_no_check)
187 VEC_last (deferred_access, deferred_access_stack)
188 ->deferring_access_checks_kind = dk_no_deferred;
191 /* Discard the current deferred access checks and restore the
195 pop_deferring_access_checks (void)
197 if (deferred_access_no_check)
198 deferred_access_no_check--;
200 VEC_pop (deferred_access, deferred_access_stack);
203 /* Returns a TREE_LIST representing the deferred checks.
204 The TREE_PURPOSE of each node is the type through which the
205 access occurred; the TREE_VALUE is the declaration named.
209 get_deferred_access_checks (void)
211 if (deferred_access_no_check)
214 return (VEC_last (deferred_access, deferred_access_stack)
215 ->deferred_access_checks);
218 /* Take current deferred checks and combine with the
219 previous states if we also defer checks previously.
220 Otherwise perform checks now. */
223 pop_to_parent_deferring_access_checks (void)
225 if (deferred_access_no_check)
226 deferred_access_no_check--;
230 deferred_access *ptr;
232 checks = (VEC_last (deferred_access, deferred_access_stack)
233 ->deferred_access_checks);
235 VEC_pop (deferred_access, deferred_access_stack);
236 ptr = VEC_last (deferred_access, deferred_access_stack);
237 if (ptr->deferring_access_checks_kind == dk_no_deferred)
240 for (; checks; checks = TREE_CHAIN (checks))
241 enforce_access (TREE_PURPOSE (checks),
242 TREE_VALUE (checks));
246 /* Merge with parent. */
248 tree original = ptr->deferred_access_checks;
250 for (; checks; checks = next)
254 next = TREE_CHAIN (checks);
256 for (probe = original; probe; probe = TREE_CHAIN (probe))
257 if (TREE_VALUE (probe) == TREE_VALUE (checks)
258 && TREE_PURPOSE (probe) == TREE_PURPOSE (checks))
260 /* Insert into parent's checks. */
261 TREE_CHAIN (checks) = ptr->deferred_access_checks;
262 ptr->deferred_access_checks = checks;
269 /* Perform the deferred access checks.
271 After performing the checks, we still have to keep the list
272 `deferred_access_stack->deferred_access_checks' since we may want
273 to check access for them again later in a different context.
280 A::X A::a, x; // No error for `A::a', error for `x'
282 We have to perform deferred access of `A::X', first with `A::a',
286 perform_deferred_access_checks (void)
290 for (deferred_check = get_deferred_access_checks ();
292 deferred_check = TREE_CHAIN (deferred_check))
294 enforce_access (TREE_PURPOSE (deferred_check),
295 TREE_VALUE (deferred_check));
298 /* Defer checking the accessibility of DECL, when looked up in
302 perform_or_defer_access_check (tree binfo, tree decl)
305 deferred_access *ptr;
307 /* Exit if we are in a context that no access checking is performed.
309 if (deferred_access_no_check)
312 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
314 ptr = VEC_last (deferred_access, deferred_access_stack);
316 /* If we are not supposed to defer access checks, just check now. */
317 if (ptr->deferring_access_checks_kind == dk_no_deferred)
319 enforce_access (binfo, decl);
323 /* See if we are already going to perform this check. */
324 for (check = ptr->deferred_access_checks;
326 check = TREE_CHAIN (check))
327 if (TREE_VALUE (check) == decl && TREE_PURPOSE (check) == binfo)
329 /* If not, record the check. */
330 ptr->deferred_access_checks
331 = tree_cons (binfo, decl, ptr->deferred_access_checks);
334 /* Returns nonzero if the current statement is a full expression,
335 i.e. temporaries created during that statement should be destroyed
336 at the end of the statement. */
339 stmts_are_full_exprs_p (void)
341 return current_stmt_tree ()->stmts_are_full_exprs_p;
344 /* T is a statement. Add it to the statement-tree. This is the C++
345 version. The C/ObjC frontends have a slightly different version of
351 enum tree_code code = TREE_CODE (t);
353 if (EXPR_P (t) && code != LABEL_EXPR)
355 if (!EXPR_HAS_LOCATION (t))
356 SET_EXPR_LOCATION (t, input_location);
358 /* When we expand a statement-tree, we must know whether or not the
359 statements are full-expressions. We record that fact here. */
360 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
363 /* Add T to the statement-tree. Non-side-effect statements need to be
364 recorded during statement expressions. */
365 append_to_statement_list_force (t, &cur_stmt_list);
370 /* Returns the stmt_tree (if any) to which statements are currently
371 being added. If there is no active statement-tree, NULL is
375 current_stmt_tree (void)
378 ? &cfun->language->base.x_stmt_tree
379 : &scope_chain->x_stmt_tree);
382 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
385 maybe_cleanup_point_expr (tree expr)
387 if (!processing_template_decl && stmts_are_full_exprs_p ())
388 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
392 /* Like maybe_cleanup_point_expr except have the type of the new expression be
393 void so we don't need to create a temporary variable to hold the inner
394 expression. The reason why we do this is because the original type might be
395 an aggregate and we cannot create a temporary variable for that type. */
398 maybe_cleanup_point_expr_void (tree expr)
400 if (!processing_template_decl && stmts_are_full_exprs_p ())
401 expr = fold_build_cleanup_point_expr (void_type_node, expr);
407 /* Create a declaration statement for the declaration given by the DECL. */
410 add_decl_expr (tree decl)
412 tree r = build_stmt (DECL_EXPR, decl);
413 if (DECL_INITIAL (decl)
414 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
415 r = maybe_cleanup_point_expr_void (r);
419 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
420 flag for this because "A union for which objects or pointers are
421 declared is not an anonymous union" [class.union]. */
424 anon_aggr_type_p (tree node)
426 return ANON_AGGR_TYPE_P (node);
429 /* Finish a scope. */
432 do_poplevel (tree stmt_list)
436 if (stmts_are_full_exprs_p ())
437 block = poplevel (kept_level_p (), 1, 0);
439 stmt_list = pop_stmt_list (stmt_list);
441 if (!processing_template_decl)
443 stmt_list = c_build_bind_expr (block, stmt_list);
444 /* ??? See c_end_compound_stmt re statement expressions. */
450 /* Begin a new scope. */
453 do_pushlevel (scope_kind sk)
455 tree ret = push_stmt_list ();
456 if (stmts_are_full_exprs_p ())
457 begin_scope (sk, NULL);
461 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
462 when the current scope is exited. EH_ONLY is true when this is not
463 meant to apply to normal control flow transfer. */
466 push_cleanup (tree decl, tree cleanup, bool eh_only)
468 tree stmt = build_stmt (CLEANUP_STMT, NULL, cleanup, decl);
469 CLEANUP_EH_ONLY (stmt) = eh_only;
471 CLEANUP_BODY (stmt) = push_stmt_list ();
474 /* Begin a conditional that might contain a declaration. When generating
475 normal code, we want the declaration to appear before the statement
476 containing the conditional. When generating template code, we want the
477 conditional to be rendered as the raw DECL_EXPR. */
480 begin_cond (tree *cond_p)
482 if (processing_template_decl)
483 *cond_p = push_stmt_list ();
486 /* Finish such a conditional. */
489 finish_cond (tree *cond_p, tree expr)
491 if (processing_template_decl)
493 tree cond = pop_stmt_list (*cond_p);
494 if (TREE_CODE (cond) == DECL_EXPR)
500 /* If *COND_P specifies a conditional with a declaration, transform the
503 for (; A x = 42;) { }
505 while (true) { A x = 42; if (!x) break; }
506 for (;;) { A x = 42; if (!x) break; }
507 The statement list for BODY will be empty if the conditional did
508 not declare anything. */
511 simplify_loop_decl_cond (tree *cond_p, tree body)
515 if (!TREE_SIDE_EFFECTS (body))
519 *cond_p = boolean_true_node;
521 if_stmt = begin_if_stmt ();
522 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
523 finish_if_stmt_cond (cond, if_stmt);
524 finish_break_stmt ();
525 finish_then_clause (if_stmt);
526 finish_if_stmt (if_stmt);
529 /* Finish a goto-statement. */
532 finish_goto_stmt (tree destination)
534 if (TREE_CODE (destination) == IDENTIFIER_NODE)
535 destination = lookup_label (destination);
537 /* We warn about unused labels with -Wunused. That means we have to
538 mark the used labels as used. */
539 if (TREE_CODE (destination) == LABEL_DECL)
540 TREE_USED (destination) = 1;
543 /* The DESTINATION is being used as an rvalue. */
544 if (!processing_template_decl)
545 destination = decay_conversion (destination);
546 /* We don't inline calls to functions with computed gotos.
547 Those functions are typically up to some funny business,
548 and may be depending on the labels being at particular
549 addresses, or some such. */
550 DECL_UNINLINABLE (current_function_decl) = 1;
553 check_goto (destination);
555 return add_stmt (build_stmt (GOTO_EXPR, destination));
558 /* COND is the condition-expression for an if, while, etc.,
559 statement. Convert it to a boolean value, if appropriate. */
562 maybe_convert_cond (tree cond)
564 /* Empty conditions remain empty. */
568 /* Wait until we instantiate templates before doing conversion. */
569 if (processing_template_decl)
572 /* Do the conversion. */
573 cond = convert_from_reference (cond);
574 return condition_conversion (cond);
577 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
580 finish_expr_stmt (tree expr)
584 if (expr != NULL_TREE)
586 if (!processing_template_decl)
588 if (warn_sequence_point)
589 verify_sequence_points (expr);
590 expr = convert_to_void (expr, "statement");
592 else if (!type_dependent_expression_p (expr))
593 convert_to_void (build_non_dependent_expr (expr), "statement");
595 /* Simplification of inner statement expressions, compound exprs,
596 etc can result in us already having an EXPR_STMT. */
597 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
599 if (TREE_CODE (expr) != EXPR_STMT)
600 expr = build_stmt (EXPR_STMT, expr);
601 expr = maybe_cleanup_point_expr_void (expr);
613 /* Begin an if-statement. Returns a newly created IF_STMT if
620 scope = do_pushlevel (sk_block);
621 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
622 TREE_CHAIN (r) = scope;
623 begin_cond (&IF_COND (r));
627 /* Process the COND of an if-statement, which may be given by
631 finish_if_stmt_cond (tree cond, tree if_stmt)
633 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
635 THEN_CLAUSE (if_stmt) = push_stmt_list ();
638 /* Finish the then-clause of an if-statement, which may be given by
642 finish_then_clause (tree if_stmt)
644 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
648 /* Begin the else-clause of an if-statement. */
651 begin_else_clause (tree if_stmt)
653 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
656 /* Finish the else-clause of an if-statement, which may be given by
660 finish_else_clause (tree if_stmt)
662 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
665 /* Finish an if-statement. */
668 finish_if_stmt (tree if_stmt)
670 tree scope = TREE_CHAIN (if_stmt);
671 TREE_CHAIN (if_stmt) = NULL;
672 add_stmt (do_poplevel (scope));
674 empty_body_warning (THEN_CLAUSE (if_stmt), ELSE_CLAUSE (if_stmt));
677 /* Begin a while-statement. Returns a newly created WHILE_STMT if
681 begin_while_stmt (void)
684 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
686 WHILE_BODY (r) = do_pushlevel (sk_block);
687 begin_cond (&WHILE_COND (r));
691 /* Process the COND of a while-statement, which may be given by
695 finish_while_stmt_cond (tree cond, tree while_stmt)
697 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
698 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
701 /* Finish a while-statement, which may be given by WHILE_STMT. */
704 finish_while_stmt (tree while_stmt)
706 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
710 /* Begin a do-statement. Returns a newly created DO_STMT if
716 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
718 DO_BODY (r) = push_stmt_list ();
722 /* Finish the body of a do-statement, which may be given by DO_STMT. */
725 finish_do_body (tree do_stmt)
727 DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
730 /* Finish a do-statement, which may be given by DO_STMT, and whose
731 COND is as indicated. */
734 finish_do_stmt (tree cond, tree do_stmt)
736 cond = maybe_convert_cond (cond);
737 DO_COND (do_stmt) = cond;
741 /* Finish a return-statement. The EXPRESSION returned, if any, is as
745 finish_return_stmt (tree expr)
750 expr = check_return_expr (expr, &no_warning);
751 if (!processing_template_decl)
753 if (DECL_DESTRUCTOR_P (current_function_decl)
754 || (DECL_CONSTRUCTOR_P (current_function_decl)
755 && targetm.cxx.cdtor_returns_this ()))
757 /* Similarly, all destructors must run destructors for
758 base-classes before returning. So, all returns in a
759 destructor get sent to the DTOR_LABEL; finish_function emits
760 code to return a value there. */
761 return finish_goto_stmt (cdtor_label);
765 r = build_stmt (RETURN_EXPR, expr);
766 TREE_NO_WARNING (r) |= no_warning;
767 r = maybe_cleanup_point_expr_void (r);
774 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
777 begin_for_stmt (void)
781 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
782 NULL_TREE, NULL_TREE);
784 if (flag_new_for_scope > 0)
785 TREE_CHAIN (r) = do_pushlevel (sk_for);
787 if (processing_template_decl)
788 FOR_INIT_STMT (r) = push_stmt_list ();
793 /* Finish the for-init-statement of a for-statement, which may be
794 given by FOR_STMT. */
797 finish_for_init_stmt (tree for_stmt)
799 if (processing_template_decl)
800 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
802 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
803 begin_cond (&FOR_COND (for_stmt));
806 /* Finish the COND of a for-statement, which may be given by
810 finish_for_cond (tree cond, tree for_stmt)
812 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
813 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
816 /* Finish the increment-EXPRESSION in a for-statement, which may be
817 given by FOR_STMT. */
820 finish_for_expr (tree expr, tree for_stmt)
824 /* If EXPR is an overloaded function, issue an error; there is no
825 context available to use to perform overload resolution. */
826 if (type_unknown_p (expr))
828 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
829 expr = error_mark_node;
831 if (!processing_template_decl)
833 if (warn_sequence_point)
834 verify_sequence_points (expr);
835 expr = convert_to_void (expr, "3rd expression in for");
837 else if (!type_dependent_expression_p (expr))
838 convert_to_void (build_non_dependent_expr (expr), "3rd expression in for");
839 expr = maybe_cleanup_point_expr_void (expr);
840 FOR_EXPR (for_stmt) = expr;
843 /* Finish the body of a for-statement, which may be given by
844 FOR_STMT. The increment-EXPR for the loop must be
848 finish_for_stmt (tree for_stmt)
850 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
852 /* Pop the scope for the body of the loop. */
853 if (flag_new_for_scope > 0)
855 tree scope = TREE_CHAIN (for_stmt);
856 TREE_CHAIN (for_stmt) = NULL;
857 add_stmt (do_poplevel (scope));
863 /* Finish a break-statement. */
866 finish_break_stmt (void)
868 return add_stmt (build_stmt (BREAK_STMT));
871 /* Finish a continue-statement. */
874 finish_continue_stmt (void)
876 return add_stmt (build_stmt (CONTINUE_STMT));
879 /* Begin a switch-statement. Returns a new SWITCH_STMT if
883 begin_switch_stmt (void)
887 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
889 scope = do_pushlevel (sk_block);
890 TREE_CHAIN (r) = scope;
891 begin_cond (&SWITCH_STMT_COND (r));
896 /* Finish the cond of a switch-statement. */
899 finish_switch_cond (tree cond, tree switch_stmt)
901 tree orig_type = NULL;
902 if (!processing_template_decl)
906 /* Convert the condition to an integer or enumeration type. */
907 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
908 if (cond == NULL_TREE)
910 error ("switch quantity not an integer");
911 cond = error_mark_node;
913 orig_type = TREE_TYPE (cond);
914 if (cond != error_mark_node)
918 Integral promotions are performed. */
919 cond = perform_integral_promotions (cond);
920 cond = maybe_cleanup_point_expr (cond);
923 if (cond != error_mark_node)
925 index = get_unwidened (cond, NULL_TREE);
926 /* We can't strip a conversion from a signed type to an unsigned,
927 because if we did, int_fits_type_p would do the wrong thing
928 when checking case values for being in range,
929 and it's too hard to do the right thing. */
930 if (TYPE_UNSIGNED (TREE_TYPE (cond))
931 == TYPE_UNSIGNED (TREE_TYPE (index)))
935 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
936 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
937 add_stmt (switch_stmt);
938 push_switch (switch_stmt);
939 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
942 /* Finish the body of a switch-statement, which may be given by
943 SWITCH_STMT. The COND to switch on is indicated. */
946 finish_switch_stmt (tree switch_stmt)
950 SWITCH_STMT_BODY (switch_stmt) =
951 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
955 scope = TREE_CHAIN (switch_stmt);
956 TREE_CHAIN (switch_stmt) = NULL;
957 add_stmt (do_poplevel (scope));
960 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
964 begin_try_block (void)
966 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
968 TRY_STMTS (r) = push_stmt_list ();
972 /* Likewise, for a function-try-block. */
975 begin_function_try_block (void)
977 tree r = begin_try_block ();
978 FN_TRY_BLOCK_P (r) = 1;
982 /* Finish a try-block, which may be given by TRY_BLOCK. */
985 finish_try_block (tree try_block)
987 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
988 TRY_HANDLERS (try_block) = push_stmt_list ();
991 /* Finish the body of a cleanup try-block, which may be given by
995 finish_cleanup_try_block (tree try_block)
997 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1000 /* Finish an implicitly generated try-block, with a cleanup is given
1004 finish_cleanup (tree cleanup, tree try_block)
1006 TRY_HANDLERS (try_block) = cleanup;
1007 CLEANUP_P (try_block) = 1;
1010 /* Likewise, for a function-try-block. */
1013 finish_function_try_block (tree try_block)
1015 finish_try_block (try_block);
1016 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1017 the try block, but moving it inside. */
1018 in_function_try_handler = 1;
1021 /* Finish a handler-sequence for a try-block, which may be given by
1025 finish_handler_sequence (tree try_block)
1027 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1028 check_handlers (TRY_HANDLERS (try_block));
1031 /* Likewise, for a function-try-block. */
1034 finish_function_handler_sequence (tree try_block)
1036 in_function_try_handler = 0;
1037 finish_handler_sequence (try_block);
1040 /* Begin a handler. Returns a HANDLER if appropriate. */
1043 begin_handler (void)
1047 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
1050 /* Create a binding level for the eh_info and the exception object
1052 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1057 /* Finish the handler-parameters for a handler, which may be given by
1058 HANDLER. DECL is the declaration for the catch parameter, or NULL
1059 if this is a `catch (...)' clause. */
1062 finish_handler_parms (tree decl, tree handler)
1064 tree type = NULL_TREE;
1065 if (processing_template_decl)
1069 decl = pushdecl (decl);
1070 decl = push_template_decl (decl);
1071 HANDLER_PARMS (handler) = decl;
1072 type = TREE_TYPE (decl);
1076 type = expand_start_catch_block (decl);
1078 HANDLER_TYPE (handler) = type;
1079 if (!processing_template_decl && type)
1080 mark_used (eh_type_info (type));
1083 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1084 the return value from the matching call to finish_handler_parms. */
1087 finish_handler (tree handler)
1089 if (!processing_template_decl)
1090 expand_end_catch_block ();
1091 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1094 /* Begin a compound statement. FLAGS contains some bits that control the
1095 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1096 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1097 block of a function. If BCS_TRY_BLOCK is set, this is the block
1098 created on behalf of a TRY statement. Returns a token to be passed to
1099 finish_compound_stmt. */
1102 begin_compound_stmt (unsigned int flags)
1106 if (flags & BCS_NO_SCOPE)
1108 r = push_stmt_list ();
1109 STATEMENT_LIST_NO_SCOPE (r) = 1;
1111 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1112 But, if it's a statement-expression with a scopeless block, there's
1113 nothing to keep, and we don't want to accidentally keep a block
1114 *inside* the scopeless block. */
1115 keep_next_level (false);
1118 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1120 /* When processing a template, we need to remember where the braces were,
1121 so that we can set up identical scopes when instantiating the template
1122 later. BIND_EXPR is a handy candidate for this.
1123 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1124 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1125 processing templates. */
1126 if (processing_template_decl)
1128 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1129 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1130 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1131 TREE_SIDE_EFFECTS (r) = 1;
1137 /* Finish a compound-statement, which is given by STMT. */
1140 finish_compound_stmt (tree stmt)
1142 if (TREE_CODE (stmt) == BIND_EXPR)
1143 BIND_EXPR_BODY (stmt) = do_poplevel (BIND_EXPR_BODY (stmt));
1144 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1145 stmt = pop_stmt_list (stmt);
1148 /* Destroy any ObjC "super" receivers that may have been
1150 objc_clear_super_receiver ();
1152 stmt = do_poplevel (stmt);
1155 /* ??? See c_end_compound_stmt wrt statement expressions. */
1160 /* Finish an asm-statement, whose components are a STRING, some
1161 OUTPUT_OPERANDS, some INPUT_OPERANDS, and some CLOBBERS. Also note
1162 whether the asm-statement should be considered volatile. */
1165 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1166 tree input_operands, tree clobbers)
1170 int ninputs = list_length (input_operands);
1171 int noutputs = list_length (output_operands);
1173 if (!processing_template_decl)
1175 const char *constraint;
1176 const char **oconstraints;
1177 bool allows_mem, allows_reg, is_inout;
1181 oconstraints = (const char **) alloca (noutputs * sizeof (char *));
1183 string = resolve_asm_operand_names (string, output_operands,
1186 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1188 operand = TREE_VALUE (t);
1190 /* ??? Really, this should not be here. Users should be using a
1191 proper lvalue, dammit. But there's a long history of using
1192 casts in the output operands. In cases like longlong.h, this
1193 becomes a primitive form of typechecking -- if the cast can be
1194 removed, then the output operand had a type of the proper width;
1195 otherwise we'll get an error. Gross, but ... */
1196 STRIP_NOPS (operand);
1198 if (!lvalue_or_else (operand, lv_asm))
1199 operand = error_mark_node;
1201 if (operand != error_mark_node
1202 && (TREE_READONLY (operand)
1203 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1204 /* Functions are not modifiable, even though they are
1206 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1207 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1208 /* If it's an aggregate and any field is const, then it is
1209 effectively const. */
1210 || (CLASS_TYPE_P (TREE_TYPE (operand))
1211 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1212 readonly_error (operand, "assignment (via 'asm' output)", 0);
1214 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1215 oconstraints[i] = constraint;
1217 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1218 &allows_mem, &allows_reg, &is_inout))
1220 /* If the operand is going to end up in memory,
1221 mark it addressable. */
1222 if (!allows_reg && !cxx_mark_addressable (operand))
1223 operand = error_mark_node;
1226 operand = error_mark_node;
1228 TREE_VALUE (t) = operand;
1231 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1233 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1234 operand = decay_conversion (TREE_VALUE (t));
1236 /* If the type of the operand hasn't been determined (e.g.,
1237 because it involves an overloaded function), then issue
1238 an error message. There's no context available to
1239 resolve the overloading. */
1240 if (TREE_TYPE (operand) == unknown_type_node)
1242 error ("type of asm operand %qE could not be determined",
1244 operand = error_mark_node;
1247 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1248 oconstraints, &allows_mem, &allows_reg))
1250 /* If the operand is going to end up in memory,
1251 mark it addressable. */
1252 if (!allows_reg && allows_mem)
1254 /* Strip the nops as we allow this case. FIXME, this really
1255 should be rejected or made deprecated. */
1256 STRIP_NOPS (operand);
1257 if (!cxx_mark_addressable (operand))
1258 operand = error_mark_node;
1262 operand = error_mark_node;
1264 TREE_VALUE (t) = operand;
1268 r = build_stmt (ASM_EXPR, string,
1269 output_operands, input_operands,
1271 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1272 r = maybe_cleanup_point_expr_void (r);
1273 return add_stmt (r);
1276 /* Finish a label with the indicated NAME. */
1279 finish_label_stmt (tree name)
1281 tree decl = define_label (input_location, name);
1282 return add_stmt (build_stmt (LABEL_EXPR, decl));
1285 /* Finish a series of declarations for local labels. G++ allows users
1286 to declare "local" labels, i.e., labels with scope. This extension
1287 is useful when writing code involving statement-expressions. */
1290 finish_label_decl (tree name)
1292 tree decl = declare_local_label (name);
1293 add_decl_expr (decl);
1296 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1299 finish_decl_cleanup (tree decl, tree cleanup)
1301 push_cleanup (decl, cleanup, false);
1304 /* If the current scope exits with an exception, run CLEANUP. */
1307 finish_eh_cleanup (tree cleanup)
1309 push_cleanup (NULL, cleanup, true);
1312 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1313 order they were written by the user. Each node is as for
1314 emit_mem_initializers. */
1317 finish_mem_initializers (tree mem_inits)
1319 /* Reorder the MEM_INITS so that they are in the order they appeared
1320 in the source program. */
1321 mem_inits = nreverse (mem_inits);
1323 if (processing_template_decl)
1324 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1326 emit_mem_initializers (mem_inits);
1329 /* Finish a parenthesized expression EXPR. */
1332 finish_parenthesized_expr (tree expr)
1335 /* This inhibits warnings in c_common_truthvalue_conversion. */
1336 TREE_NO_WARNING (expr) = 1;
1338 if (TREE_CODE (expr) == OFFSET_REF)
1339 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1340 enclosed in parentheses. */
1341 PTRMEM_OK_P (expr) = 0;
1343 if (TREE_CODE (expr) == STRING_CST)
1344 PAREN_STRING_LITERAL_P (expr) = 1;
1349 /* Finish a reference to a non-static data member (DECL) that is not
1350 preceded by `.' or `->'. */
1353 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1355 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1359 if (current_function_decl
1360 && DECL_STATIC_FUNCTION_P (current_function_decl))
1361 error ("invalid use of member %q+D in static member function", decl);
1363 error ("invalid use of non-static data member %q+D", decl);
1364 error ("from this location");
1366 return error_mark_node;
1368 TREE_USED (current_class_ptr) = 1;
1369 if (processing_template_decl && !qualifying_scope)
1371 tree type = TREE_TYPE (decl);
1373 if (TREE_CODE (type) == REFERENCE_TYPE)
1374 type = TREE_TYPE (type);
1377 /* Set the cv qualifiers. */
1378 int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1380 if (DECL_MUTABLE_P (decl))
1381 quals &= ~TYPE_QUAL_CONST;
1383 quals |= cp_type_quals (TREE_TYPE (decl));
1384 type = cp_build_qualified_type (type, quals);
1387 return build_min (COMPONENT_REF, type, object, decl, NULL_TREE);
1391 tree access_type = TREE_TYPE (object);
1392 tree lookup_context = context_for_name_lookup (decl);
1394 while (!DERIVED_FROM_P (lookup_context, access_type))
1396 access_type = TYPE_CONTEXT (access_type);
1397 while (access_type && DECL_P (access_type))
1398 access_type = DECL_CONTEXT (access_type);
1402 error ("object missing in reference to %q+D", decl);
1403 error ("from this location");
1404 return error_mark_node;
1408 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1409 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1411 if (processing_template_decl)
1412 return build_qualified_name (TREE_TYPE (decl),
1415 /*template_p=*/false);
1417 perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1419 /* If the data member was named `C::M', convert `*this' to `C'
1421 if (qualifying_scope)
1423 tree binfo = NULL_TREE;
1424 object = build_scoped_ref (object, qualifying_scope,
1428 return build_class_member_access_expr (object, decl,
1429 /*access_path=*/NULL_TREE,
1430 /*preserve_reference=*/false);
1434 /* DECL was the declaration to which a qualified-id resolved. Issue
1435 an error message if it is not accessible. If OBJECT_TYPE is
1436 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1437 type of `*x', or `x', respectively. If the DECL was named as
1438 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1441 check_accessibility_of_qualified_id (tree decl,
1443 tree nested_name_specifier)
1446 tree qualifying_type = NULL_TREE;
1448 /* If we're not checking, return immediately. */
1449 if (deferred_access_no_check)
1452 /* Determine the SCOPE of DECL. */
1453 scope = context_for_name_lookup (decl);
1454 /* If the SCOPE is not a type, then DECL is not a member. */
1455 if (!TYPE_P (scope))
1457 /* Compute the scope through which DECL is being accessed. */
1459 /* OBJECT_TYPE might not be a class type; consider:
1461 class A { typedef int I; };
1465 In this case, we will have "A::I" as the DECL, but "I" as the
1467 && CLASS_TYPE_P (object_type)
1468 && DERIVED_FROM_P (scope, object_type))
1469 /* If we are processing a `->' or `.' expression, use the type of the
1471 qualifying_type = object_type;
1472 else if (nested_name_specifier)
1474 /* If the reference is to a non-static member of the
1475 current class, treat it as if it were referenced through
1477 if (DECL_NONSTATIC_MEMBER_P (decl)
1478 && current_class_ptr
1479 && DERIVED_FROM_P (scope, current_class_type))
1480 qualifying_type = current_class_type;
1481 /* Otherwise, use the type indicated by the
1482 nested-name-specifier. */
1484 qualifying_type = nested_name_specifier;
1487 /* Otherwise, the name must be from the current class or one of
1489 qualifying_type = currently_open_derived_class (scope);
1491 if (qualifying_type && IS_AGGR_TYPE_CODE (TREE_CODE (qualifying_type)))
1492 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1493 or similar in a default argument value. */
1494 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1497 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1498 class named to the left of the "::" operator. DONE is true if this
1499 expression is a complete postfix-expression; it is false if this
1500 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1501 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1502 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1503 is true iff this qualified name appears as a template argument. */
1506 finish_qualified_id_expr (tree qualifying_class,
1511 bool template_arg_p)
1513 gcc_assert (TYPE_P (qualifying_class));
1515 if (error_operand_p (expr))
1516 return error_mark_node;
1518 if (DECL_P (expr) || BASELINK_P (expr))
1522 check_template_keyword (expr);
1524 /* If EXPR occurs as the operand of '&', use special handling that
1525 permits a pointer-to-member. */
1526 if (address_p && done)
1528 if (TREE_CODE (expr) == SCOPE_REF)
1529 expr = TREE_OPERAND (expr, 1);
1530 expr = build_offset_ref (qualifying_class, expr,
1531 /*address_p=*/true);
1535 /* Within the scope of a class, turn references to non-static
1536 members into expression of the form "this->...". */
1538 /* But, within a template argument, we do not want make the
1539 transformation, as there is no "this" pointer. */
1541 else if (TREE_CODE (expr) == FIELD_DECL)
1542 expr = finish_non_static_data_member (expr, current_class_ref,
1544 else if (BASELINK_P (expr) && !processing_template_decl)
1548 /* See if any of the functions are non-static members. */
1549 fns = BASELINK_FUNCTIONS (expr);
1550 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1551 fns = TREE_OPERAND (fns, 0);
1552 /* If so, the expression may be relative to the current
1554 if (!shared_member_p (fns)
1555 && current_class_type
1556 && DERIVED_FROM_P (qualifying_class, current_class_type))
1557 expr = (build_class_member_access_expr
1558 (maybe_dummy_object (qualifying_class, NULL),
1560 BASELINK_ACCESS_BINFO (expr),
1561 /*preserve_reference=*/false));
1563 /* The expression is a qualified name whose address is not
1565 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1571 /* Begin a statement-expression. The value returned must be passed to
1572 finish_stmt_expr. */
1575 begin_stmt_expr (void)
1577 return push_stmt_list ();
1580 /* Process the final expression of a statement expression. EXPR can be
1581 NULL, if the final expression is empty. Return a STATEMENT_LIST
1582 containing all the statements in the statement-expression, or
1583 ERROR_MARK_NODE if there was an error. */
1586 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1588 if (error_operand_p (expr))
1589 return error_mark_node;
1591 /* If the last statement does not have "void" type, then the value
1592 of the last statement is the value of the entire expression. */
1596 type = TREE_TYPE (expr);
1597 if (!dependent_type_p (type) && !VOID_TYPE_P (type))
1599 expr = decay_conversion (expr);
1600 if (error_operand_p (expr))
1601 return error_mark_node;
1602 type = TREE_TYPE (expr);
1604 /* The type of the statement-expression is the type of the last
1606 TREE_TYPE (stmt_expr) = type;
1607 /* We must take particular care if TYPE is a class type. In
1608 particular if EXPR creates a temporary of class type, then it
1609 must be destroyed at the semicolon terminating the last
1610 statement -- but we must make a copy before that happens.
1612 This problem is solved by using a TARGET_EXPR to initialize a
1613 new temporary variable. The TARGET_EXPR itself is placed
1614 outside the statement-expression. However, the last
1615 statement in the statement-expression is transformed from
1616 EXPR to (approximately) T = EXPR, where T is the new
1617 temporary variable. Thus, the lifetime of the new temporary
1618 extends to the full-expression surrounding the
1619 statement-expression. */
1620 if (!processing_template_decl && !VOID_TYPE_P (type))
1623 if (CLASS_TYPE_P (type)
1624 && !TYPE_HAS_TRIVIAL_INIT_REF (type))
1626 target_expr = build_target_expr_with_type (expr, type);
1627 expr = TARGET_EXPR_INITIAL (target_expr);
1631 /* Normally, build_target_expr will not create a
1632 TARGET_EXPR for scalars. However, we need the
1633 temporary here, in order to solve the scoping
1634 problem described above. */
1635 target_expr = force_target_expr (type, expr);
1636 expr = TARGET_EXPR_INITIAL (target_expr);
1637 expr = build2 (INIT_EXPR,
1639 TARGET_EXPR_SLOT (target_expr),
1642 TARGET_EXPR_INITIAL (target_expr) = NULL_TREE;
1643 /* Save away the TARGET_EXPR in the TREE_TYPE field of the
1644 STATEMENT_EXPR. We will retrieve it in
1645 finish_stmt_expr. */
1646 TREE_TYPE (stmt_expr) = target_expr;
1650 /* Having modified EXPR to reflect the extra initialization, we now
1651 treat it just like an ordinary statement. */
1652 expr = finish_expr_stmt (expr);
1654 /* Mark the last statement so that we can recognize it as such at
1655 template-instantiation time. */
1656 if (expr && processing_template_decl)
1657 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1662 /* Finish a statement-expression. EXPR should be the value returned
1663 by the previous begin_stmt_expr. Returns an expression
1664 representing the statement-expression. */
1667 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1672 if (error_operand_p (stmt_expr))
1673 return error_mark_node;
1675 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1677 type = TREE_TYPE (stmt_expr);
1678 result = pop_stmt_list (stmt_expr);
1680 if (processing_template_decl)
1682 result = build_min (STMT_EXPR, type, result);
1683 TREE_SIDE_EFFECTS (result) = 1;
1684 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1686 else if (!TYPE_P (type))
1688 gcc_assert (TREE_CODE (type) == TARGET_EXPR);
1689 TARGET_EXPR_INITIAL (type) = result;
1690 TREE_TYPE (result) = void_type_node;
1697 /* Perform Koenig lookup. FN is the postfix-expression representing
1698 the function (or functions) to call; ARGS are the arguments to the
1699 call. Returns the functions to be considered by overload
1703 perform_koenig_lookup (tree fn, tree args)
1705 tree identifier = NULL_TREE;
1706 tree functions = NULL_TREE;
1708 /* Find the name of the overloaded function. */
1709 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1711 else if (is_overloaded_fn (fn))
1714 identifier = DECL_NAME (get_first_fn (functions));
1716 else if (DECL_P (fn))
1719 identifier = DECL_NAME (fn);
1722 /* A call to a namespace-scope function using an unqualified name.
1724 Do Koenig lookup -- unless any of the arguments are
1726 if (!any_type_dependent_arguments_p (args))
1728 fn = lookup_arg_dependent (identifier, functions, args);
1730 /* The unqualified name could not be resolved. */
1731 fn = unqualified_fn_lookup_error (identifier);
1737 /* Generate an expression for `FN (ARGS)'.
1739 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1740 as a virtual call, even if FN is virtual. (This flag is set when
1741 encountering an expression where the function name is explicitly
1742 qualified. For example a call to `X::f' never generates a virtual
1745 Returns code for the call. */
1748 finish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p)
1754 if (fn == error_mark_node || args == error_mark_node)
1755 return error_mark_node;
1757 /* ARGS should be a list of arguments. */
1758 gcc_assert (!args || TREE_CODE (args) == TREE_LIST);
1763 if (processing_template_decl)
1765 if (type_dependent_expression_p (fn)
1766 || any_type_dependent_arguments_p (args))
1768 result = build_nt (CALL_EXPR, fn, args, NULL_TREE);
1769 KOENIG_LOOKUP_P (result) = koenig_p;
1772 if (!BASELINK_P (fn)
1773 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1774 && TREE_TYPE (fn) != unknown_type_node)
1775 fn = build_non_dependent_expr (fn);
1776 args = build_non_dependent_args (orig_args);
1779 /* A reference to a member function will appear as an overloaded
1780 function (rather than a BASELINK) if an unqualified name was used
1782 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1786 if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
1787 f = TREE_OPERAND (f, 0);
1788 f = get_first_fn (f);
1789 if (DECL_FUNCTION_MEMBER_P (f))
1791 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1793 type = DECL_CONTEXT (f);
1794 fn = build_baselink (TYPE_BINFO (type),
1796 fn, /*optype=*/NULL_TREE);
1801 if (BASELINK_P (fn))
1805 /* A call to a member function. From [over.call.func]:
1807 If the keyword this is in scope and refers to the class of
1808 that member function, or a derived class thereof, then the
1809 function call is transformed into a qualified function call
1810 using (*this) as the postfix-expression to the left of the
1811 . operator.... [Otherwise] a contrived object of type T
1812 becomes the implied object argument.
1814 This paragraph is unclear about this situation:
1816 struct A { void f(); };
1817 struct B : public A {};
1818 struct C : public A { void g() { B::f(); }};
1820 In particular, for `B::f', this paragraph does not make clear
1821 whether "the class of that member function" refers to `A' or
1822 to `B'. We believe it refers to `B'. */
1823 if (current_class_type
1824 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1826 && current_class_ref)
1827 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1831 tree representative_fn;
1833 representative_fn = BASELINK_FUNCTIONS (fn);
1834 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1835 representative_fn = TREE_OPERAND (representative_fn, 0);
1836 representative_fn = get_first_fn (representative_fn);
1837 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1840 if (processing_template_decl)
1842 if (type_dependent_expression_p (object))
1843 return build_nt (CALL_EXPR, orig_fn, orig_args, NULL_TREE);
1844 object = build_non_dependent_expr (object);
1847 result = build_new_method_call (object, fn, args, NULL_TREE,
1849 ? LOOKUP_NONVIRTUAL : 0));
1851 else if (is_overloaded_fn (fn))
1853 /* If the function is an overloaded builtin, resolve it. */
1854 if (TREE_CODE (fn) == FUNCTION_DECL
1855 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
1856 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
1857 result = resolve_overloaded_builtin (fn, args);
1860 /* A call to a namespace-scope function. */
1861 result = build_new_function_call (fn, args, koenig_p);
1863 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1866 error ("arguments to destructor are not allowed");
1867 /* Mark the pseudo-destructor call as having side-effects so
1868 that we do not issue warnings about its use. */
1869 result = build1 (NOP_EXPR,
1871 TREE_OPERAND (fn, 0));
1872 TREE_SIDE_EFFECTS (result) = 1;
1874 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1875 /* If the "function" is really an object of class type, it might
1876 have an overloaded `operator ()'. */
1877 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE,
1878 /*overloaded_p=*/NULL);
1881 /* A call where the function is unknown. */
1882 result = build_function_call (fn, args);
1884 if (processing_template_decl)
1886 result = build3 (CALL_EXPR, TREE_TYPE (result), orig_fn,
1887 orig_args, NULL_TREE);
1888 KOENIG_LOOKUP_P (result) = koenig_p;
1893 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1894 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1895 POSTDECREMENT_EXPR.) */
1898 finish_increment_expr (tree expr, enum tree_code code)
1900 return build_x_unary_op (code, expr);
1903 /* Finish a use of `this'. Returns an expression for `this'. */
1906 finish_this_expr (void)
1910 if (current_class_ptr)
1912 result = current_class_ptr;
1914 else if (current_function_decl
1915 && DECL_STATIC_FUNCTION_P (current_function_decl))
1917 error ("%<this%> is unavailable for static member functions");
1918 result = error_mark_node;
1922 if (current_function_decl)
1923 error ("invalid use of %<this%> in non-member function");
1925 error ("invalid use of %<this%> at top level");
1926 result = error_mark_node;
1932 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1933 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1934 the TYPE for the type given. If SCOPE is non-NULL, the expression
1935 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1938 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1940 if (destructor == error_mark_node)
1941 return error_mark_node;
1943 gcc_assert (TYPE_P (destructor));
1945 if (!processing_template_decl)
1947 if (scope == error_mark_node)
1949 error ("invalid qualifying scope in pseudo-destructor name");
1950 return error_mark_node;
1953 /* [expr.pseudo] says both:
1955 The type designated by the pseudo-destructor-name shall be
1956 the same as the object type.
1960 The cv-unqualified versions of the object type and of the
1961 type designated by the pseudo-destructor-name shall be the
1964 We implement the more generous second sentence, since that is
1965 what most other compilers do. */
1966 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
1969 error ("%qE is not of type %qT", object, destructor);
1970 return error_mark_node;
1974 return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1977 /* Finish an expression of the form CODE EXPR. */
1980 finish_unary_op_expr (enum tree_code code, tree expr)
1982 tree result = build_x_unary_op (code, expr);
1983 /* Inside a template, build_x_unary_op does not fold the
1984 expression. So check whether the result is folded before
1985 setting TREE_NEGATED_INT. */
1986 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1987 && TREE_CODE (result) == INTEGER_CST
1988 && !TYPE_UNSIGNED (TREE_TYPE (result))
1989 && INT_CST_LT (result, integer_zero_node))
1991 /* RESULT may be a cached INTEGER_CST, so we must copy it before
1992 setting TREE_NEGATED_INT. */
1993 result = copy_node (result);
1994 TREE_NEGATED_INT (result) = 1;
1996 overflow_warning (result);
2000 /* Finish a compound-literal expression. TYPE is the type to which
2001 the INITIALIZER_LIST is being cast. */
2004 finish_compound_literal (tree type, VEC(constructor_elt,gc) *initializer_list)
2006 tree compound_literal;
2008 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
2009 compound_literal = build_constructor (NULL_TREE, initializer_list);
2010 if (processing_template_decl)
2011 TREE_TYPE (compound_literal) = type;
2014 /* Check the initialization. */
2015 compound_literal = reshape_init (type, compound_literal);
2016 compound_literal = digest_init (type, compound_literal);
2017 /* If the TYPE was an array type with an unknown bound, then we can
2018 figure out the dimension now. For example, something like:
2022 implies that the array has two elements. */
2023 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
2024 cp_complete_array_type (&TREE_TYPE (compound_literal),
2025 compound_literal, 1);
2028 /* Mark it as a compound-literal. */
2029 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2031 return compound_literal;
2034 /* Return the declaration for the function-name variable indicated by
2038 finish_fname (tree id)
2042 decl = fname_decl (C_RID_CODE (id), id);
2043 if (processing_template_decl)
2044 decl = DECL_NAME (decl);
2048 /* Finish a translation unit. */
2051 finish_translation_unit (void)
2053 /* In case there were missing closebraces,
2054 get us back to the global binding level. */
2056 while (current_namespace != global_namespace)
2059 /* Do file scope __FUNCTION__ et al. */
2060 finish_fname_decls ();
2063 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2064 Returns the parameter. */
2067 finish_template_type_parm (tree aggr, tree identifier)
2069 if (aggr != class_type_node)
2071 pedwarn ("template type parameters must use the keyword %<class%> or %<typename%>");
2072 aggr = class_type_node;
2075 return build_tree_list (aggr, identifier);
2078 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2079 Returns the parameter. */
2082 finish_template_template_parm (tree aggr, tree identifier)
2084 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
2085 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2086 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2087 DECL_TEMPLATE_RESULT (tmpl) = decl;
2088 DECL_ARTIFICIAL (decl) = 1;
2089 end_template_decl ();
2091 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2093 return finish_template_type_parm (aggr, tmpl);
2096 /* ARGUMENT is the default-argument value for a template template
2097 parameter. If ARGUMENT is invalid, issue error messages and return
2098 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2101 check_template_template_default_arg (tree argument)
2103 if (TREE_CODE (argument) != TEMPLATE_DECL
2104 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2105 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2107 if (TREE_CODE (argument) == TYPE_DECL)
2109 tree t = TREE_TYPE (argument);
2111 /* Try to emit a slightly smarter error message if we detect
2112 that the user is using a template instantiation. */
2113 if (CLASSTYPE_TEMPLATE_INFO (t)
2114 && CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2115 error ("invalid use of type %qT as a default value for a "
2116 "template template-parameter", t);
2118 error ("invalid use of %qD as a default value for a template "
2119 "template-parameter", argument);
2122 error ("invalid default argument for a template template parameter");
2123 return error_mark_node;
2129 /* Begin a class definition, as indicated by T. */
2132 begin_class_definition (tree t)
2134 if (t == error_mark_node)
2135 return error_mark_node;
2137 if (processing_template_parmlist)
2139 error ("definition of %q#T inside template parameter list", t);
2140 return error_mark_node;
2142 /* A non-implicit typename comes from code like:
2144 template <typename T> struct A {
2145 template <typename U> struct A<T>::B ...
2147 This is erroneous. */
2148 else if (TREE_CODE (t) == TYPENAME_TYPE)
2150 error ("invalid definition of qualified type %qT", t);
2151 t = error_mark_node;
2154 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
2156 t = make_aggr_type (RECORD_TYPE);
2157 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2160 /* Update the location of the decl. */
2161 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
2163 if (TYPE_BEING_DEFINED (t))
2165 t = make_aggr_type (TREE_CODE (t));
2166 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2168 maybe_process_partial_specialization (t);
2170 TYPE_BEING_DEFINED (t) = 1;
2171 if (flag_pack_struct)
2174 TYPE_PACKED (t) = 1;
2175 /* Even though the type is being defined for the first time
2176 here, there might have been a forward declaration, so there
2177 might be cv-qualified variants of T. */
2178 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2179 TYPE_PACKED (v) = 1;
2181 /* Reset the interface data, at the earliest possible
2182 moment, as it might have been set via a class foo;
2184 if (! TYPE_ANONYMOUS_P (t))
2186 struct c_fileinfo *finfo = get_fileinfo (lbasename (input_filename));
2187 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2188 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2189 (t, finfo->interface_unknown);
2191 reset_specialization();
2193 /* Make a declaration for this class in its own scope. */
2194 build_self_reference ();
2199 /* Finish the member declaration given by DECL. */
2202 finish_member_declaration (tree decl)
2204 if (decl == error_mark_node || decl == NULL_TREE)
2207 if (decl == void_type_node)
2208 /* The COMPONENT was a friend, not a member, and so there's
2209 nothing for us to do. */
2212 /* We should see only one DECL at a time. */
2213 gcc_assert (TREE_CHAIN (decl) == NULL_TREE);
2215 /* Set up access control for DECL. */
2217 = (current_access_specifier == access_private_node);
2218 TREE_PROTECTED (decl)
2219 = (current_access_specifier == access_protected_node);
2220 if (TREE_CODE (decl) == TEMPLATE_DECL)
2222 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2223 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2226 /* Mark the DECL as a member of the current class. */
2227 DECL_CONTEXT (decl) = current_class_type;
2231 A C language linkage is ignored for the names of class members
2232 and the member function type of class member functions. */
2233 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2234 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2236 /* Put functions on the TYPE_METHODS list and everything else on the
2237 TYPE_FIELDS list. Note that these are built up in reverse order.
2238 We reverse them (to obtain declaration order) in finish_struct. */
2239 if (TREE_CODE (decl) == FUNCTION_DECL
2240 || DECL_FUNCTION_TEMPLATE_P (decl))
2242 /* We also need to add this function to the
2243 CLASSTYPE_METHOD_VEC. */
2244 if (add_method (current_class_type, decl, NULL_TREE))
2246 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2247 TYPE_METHODS (current_class_type) = decl;
2249 maybe_add_class_template_decl_list (current_class_type, decl,
2253 /* Enter the DECL into the scope of the class. */
2254 else if ((TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2255 || pushdecl_class_level (decl))
2257 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2258 go at the beginning. The reason is that lookup_field_1
2259 searches the list in order, and we want a field name to
2260 override a type name so that the "struct stat hack" will
2261 work. In particular:
2263 struct S { enum E { }; int E } s;
2266 is valid. In addition, the FIELD_DECLs must be maintained in
2267 declaration order so that class layout works as expected.
2268 However, we don't need that order until class layout, so we
2269 save a little time by putting FIELD_DECLs on in reverse order
2270 here, and then reversing them in finish_struct_1. (We could
2271 also keep a pointer to the correct insertion points in the
2274 if (TREE_CODE (decl) == TYPE_DECL)
2275 TYPE_FIELDS (current_class_type)
2276 = chainon (TYPE_FIELDS (current_class_type), decl);
2279 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2280 TYPE_FIELDS (current_class_type) = decl;
2283 maybe_add_class_template_decl_list (current_class_type, decl,
2288 note_decl_for_pch (decl);
2291 /* DECL has been declared while we are building a PCH file. Perform
2292 actions that we might normally undertake lazily, but which can be
2293 performed now so that they do not have to be performed in
2294 translation units which include the PCH file. */
2297 note_decl_for_pch (tree decl)
2299 gcc_assert (pch_file);
2301 /* There's a good chance that we'll have to mangle names at some
2302 point, even if only for emission in debugging information. */
2303 if (TREE_CODE (decl) == VAR_DECL
2304 || TREE_CODE (decl) == FUNCTION_DECL)
2308 /* Finish processing a complete template declaration. The PARMS are
2309 the template parameters. */
2312 finish_template_decl (tree parms)
2315 end_template_decl ();
2317 end_specialization ();
2320 /* Finish processing a template-id (which names a type) of the form
2321 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2322 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2323 the scope of template-id indicated. */
2326 finish_template_type (tree name, tree args, int entering_scope)
2330 decl = lookup_template_class (name, args,
2331 NULL_TREE, NULL_TREE, entering_scope,
2332 tf_error | tf_warning | tf_user);
2333 if (decl != error_mark_node)
2334 decl = TYPE_STUB_DECL (decl);
2339 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2340 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2341 BASE_CLASS, or NULL_TREE if an error occurred. The
2342 ACCESS_SPECIFIER is one of
2343 access_{default,public,protected_private}_node. For a virtual base
2344 we set TREE_TYPE. */
2347 finish_base_specifier (tree base, tree access, bool virtual_p)
2351 if (base == error_mark_node)
2353 error ("invalid base-class specification");
2356 else if (! is_aggr_type (base, 1))
2360 if (cp_type_quals (base) != 0)
2362 error ("base class %qT has cv qualifiers", base);
2363 base = TYPE_MAIN_VARIANT (base);
2365 result = build_tree_list (access, base);
2367 TREE_TYPE (result) = integer_type_node;
2373 /* Issue a diagnostic that NAME cannot be found in SCOPE. DECL is
2374 what we found when we tried to do the lookup. */
2377 qualified_name_lookup_error (tree scope, tree name, tree decl)
2379 if (scope == error_mark_node)
2380 ; /* We already complained. */
2381 else if (TYPE_P (scope))
2383 if (!COMPLETE_TYPE_P (scope))
2384 error ("incomplete type %qT used in nested name specifier", scope);
2385 else if (TREE_CODE (decl) == TREE_LIST)
2387 error ("reference to %<%T::%D%> is ambiguous", scope, name);
2388 print_candidates (decl);
2391 error ("%qD is not a member of %qT", name, scope);
2393 else if (scope != global_namespace)
2394 error ("%qD is not a member of %qD", name, scope);
2396 error ("%<::%D%> has not been declared", name);
2399 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2400 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2401 if non-NULL, is the type or namespace used to explicitly qualify
2402 ID_EXPRESSION. DECL is the entity to which that name has been
2405 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2406 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2407 be set to true if this expression isn't permitted in a
2408 constant-expression, but it is otherwise not set by this function.
2409 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2410 constant-expression, but a non-constant expression is also
2413 DONE is true if this expression is a complete postfix-expression;
2414 it is false if this expression is followed by '->', '[', '(', etc.
2415 ADDRESS_P is true iff this expression is the operand of '&'.
2416 TEMPLATE_P is true iff the qualified-id was of the form
2417 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2418 appears as a template argument.
2420 If an error occurs, and it is the kind of error that might cause
2421 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2422 is the caller's responsibility to issue the message. *ERROR_MSG
2423 will be a string with static storage duration, so the caller need
2426 Return an expression for the entity, after issuing appropriate
2427 diagnostics. This function is also responsible for transforming a
2428 reference to a non-static member into a COMPONENT_REF that makes
2429 the use of "this" explicit.
2431 Upon return, *IDK will be filled in appropriately. */
2434 finish_id_expression (tree id_expression,
2438 bool integral_constant_expression_p,
2439 bool allow_non_integral_constant_expression_p,
2440 bool *non_integral_constant_expression_p,
2444 bool template_arg_p,
2445 const char **error_msg)
2447 /* Initialize the output parameters. */
2448 *idk = CP_ID_KIND_NONE;
2451 if (id_expression == error_mark_node)
2452 return error_mark_node;
2453 /* If we have a template-id, then no further lookup is
2454 required. If the template-id was for a template-class, we
2455 will sometimes have a TYPE_DECL at this point. */
2456 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2457 || TREE_CODE (decl) == TYPE_DECL)
2459 /* Look up the name. */
2462 if (decl == error_mark_node)
2464 /* Name lookup failed. */
2467 || (!dependent_type_p (scope)
2468 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2469 && IDENTIFIER_TYPENAME_P (id_expression)
2470 && dependent_type_p (TREE_TYPE (id_expression))))))
2472 /* If the qualifying type is non-dependent (and the name
2473 does not name a conversion operator to a dependent
2474 type), issue an error. */
2475 qualified_name_lookup_error (scope, id_expression, decl);
2476 return error_mark_node;
2480 /* It may be resolved via Koenig lookup. */
2481 *idk = CP_ID_KIND_UNQUALIFIED;
2482 return id_expression;
2485 decl = id_expression;
2487 /* If DECL is a variable that would be out of scope under
2488 ANSI/ISO rules, but in scope in the ARM, name lookup
2489 will succeed. Issue a diagnostic here. */
2491 decl = check_for_out_of_scope_variable (decl);
2493 /* Remember that the name was used in the definition of
2494 the current class so that we can check later to see if
2495 the meaning would have been different after the class
2496 was entirely defined. */
2497 if (!scope && decl != error_mark_node)
2498 maybe_note_name_used_in_class (id_expression, decl);
2500 /* Disallow uses of local variables from containing functions. */
2501 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
2503 tree context = decl_function_context (decl);
2504 if (context != NULL_TREE && context != current_function_decl
2505 && ! TREE_STATIC (decl))
2507 error (TREE_CODE (decl) == VAR_DECL
2508 ? "use of %<auto%> variable from containing function"
2509 : "use of parameter from containing function");
2510 error (" %q+#D declared here", decl);
2511 return error_mark_node;
2516 /* If we didn't find anything, or what we found was a type,
2517 then this wasn't really an id-expression. */
2518 if (TREE_CODE (decl) == TEMPLATE_DECL
2519 && !DECL_FUNCTION_TEMPLATE_P (decl))
2521 *error_msg = "missing template arguments";
2522 return error_mark_node;
2524 else if (TREE_CODE (decl) == TYPE_DECL
2525 || TREE_CODE (decl) == NAMESPACE_DECL)
2527 *error_msg = "expected primary-expression";
2528 return error_mark_node;
2531 /* If the name resolved to a template parameter, there is no
2532 need to look it up again later. */
2533 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
2534 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2538 *idk = CP_ID_KIND_NONE;
2539 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2540 decl = TEMPLATE_PARM_DECL (decl);
2541 r = convert_from_reference (DECL_INITIAL (decl));
2543 if (integral_constant_expression_p
2544 && !dependent_type_p (TREE_TYPE (decl))
2545 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
2547 if (!allow_non_integral_constant_expression_p)
2548 error ("template parameter %qD of type %qT is not allowed in "
2549 "an integral constant expression because it is not of "
2550 "integral or enumeration type", decl, TREE_TYPE (decl));
2551 *non_integral_constant_expression_p = true;
2555 /* Similarly, we resolve enumeration constants to their
2556 underlying values. */
2557 else if (TREE_CODE (decl) == CONST_DECL)
2559 *idk = CP_ID_KIND_NONE;
2560 if (!processing_template_decl)
2561 return DECL_INITIAL (decl);
2568 /* If the declaration was explicitly qualified indicate
2569 that. The semantics of `A::f(3)' are different than
2570 `f(3)' if `f' is virtual. */
2572 ? CP_ID_KIND_QUALIFIED
2573 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2574 ? CP_ID_KIND_TEMPLATE_ID
2575 : CP_ID_KIND_UNQUALIFIED));
2580 An id-expression is type-dependent if it contains an
2581 identifier that was declared with a dependent type.
2583 The standard is not very specific about an id-expression that
2584 names a set of overloaded functions. What if some of them
2585 have dependent types and some of them do not? Presumably,
2586 such a name should be treated as a dependent name. */
2587 /* Assume the name is not dependent. */
2588 dependent_p = false;
2589 if (!processing_template_decl)
2590 /* No names are dependent outside a template. */
2592 /* A template-id where the name of the template was not resolved
2593 is definitely dependent. */
2594 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2595 && (TREE_CODE (TREE_OPERAND (decl, 0))
2596 == IDENTIFIER_NODE))
2598 /* For anything except an overloaded function, just check its
2600 else if (!is_overloaded_fn (decl))
2602 = dependent_type_p (TREE_TYPE (decl));
2603 /* For a set of overloaded functions, check each of the
2609 if (BASELINK_P (fns))
2610 fns = BASELINK_FUNCTIONS (fns);
2612 /* For a template-id, check to see if the template
2613 arguments are dependent. */
2614 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2616 tree args = TREE_OPERAND (fns, 1);
2617 dependent_p = any_dependent_template_arguments_p (args);
2618 /* The functions are those referred to by the
2620 fns = TREE_OPERAND (fns, 0);
2623 /* If there are no dependent template arguments, go through
2624 the overloaded functions. */
2625 while (fns && !dependent_p)
2627 tree fn = OVL_CURRENT (fns);
2629 /* Member functions of dependent classes are
2631 if (TREE_CODE (fn) == FUNCTION_DECL
2632 && type_dependent_expression_p (fn))
2634 else if (TREE_CODE (fn) == TEMPLATE_DECL
2635 && dependent_template_p (fn))
2638 fns = OVL_NEXT (fns);
2642 /* If the name was dependent on a template parameter, we will
2643 resolve the name at instantiation time. */
2646 /* Create a SCOPE_REF for qualified names, if the scope is
2650 /* Since this name was dependent, the expression isn't
2651 constant -- yet. No error is issued because it might
2652 be constant when things are instantiated. */
2653 if (integral_constant_expression_p)
2654 *non_integral_constant_expression_p = true;
2657 if (address_p && done)
2658 decl = finish_qualified_id_expr (scope, decl,
2662 else if (dependent_type_p (scope))
2663 decl = build_qualified_name (/*type=*/NULL_TREE,
2667 else if (DECL_P (decl))
2668 decl = build_qualified_name (TREE_TYPE (decl),
2673 if (TREE_TYPE (decl))
2674 decl = convert_from_reference (decl);
2677 /* A TEMPLATE_ID already contains all the information we
2679 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2680 return id_expression;
2681 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2682 /* If we found a variable, then name lookup during the
2683 instantiation will always resolve to the same VAR_DECL
2684 (or an instantiation thereof). */
2685 if (TREE_CODE (decl) == VAR_DECL
2686 || TREE_CODE (decl) == PARM_DECL)
2687 return convert_from_reference (decl);
2688 /* The same is true for FIELD_DECL, but we also need to
2689 make sure that the syntax is correct. */
2690 else if (TREE_CODE (decl) == FIELD_DECL)
2692 /* Since SCOPE is NULL here, this is an unqualified name.
2693 Access checking has been performed during name lookup
2694 already. Turn off checking to avoid duplicate errors. */
2695 push_deferring_access_checks (dk_no_check);
2696 decl = finish_non_static_data_member
2697 (decl, current_class_ref,
2698 /*qualifying_scope=*/NULL_TREE);
2699 pop_deferring_access_checks ();
2702 return id_expression;
2705 /* Only certain kinds of names are allowed in constant
2706 expression. Enumerators and template parameters have already
2707 been handled above. */
2708 if (integral_constant_expression_p
2709 && ! DECL_INTEGRAL_CONSTANT_VAR_P (decl)
2710 && ! builtin_valid_in_constant_expr_p (decl))
2712 if (!allow_non_integral_constant_expression_p)
2714 error ("%qD cannot appear in a constant-expression", decl);
2715 return error_mark_node;
2717 *non_integral_constant_expression_p = true;
2720 if (TREE_CODE (decl) == NAMESPACE_DECL)
2722 error ("use of namespace %qD as expression", decl);
2723 return error_mark_node;
2725 else if (DECL_CLASS_TEMPLATE_P (decl))
2727 error ("use of class template %qT as expression", decl);
2728 return error_mark_node;
2730 else if (TREE_CODE (decl) == TREE_LIST)
2732 /* Ambiguous reference to base members. */
2733 error ("request for member %qD is ambiguous in "
2734 "multiple inheritance lattice", id_expression);
2735 print_candidates (decl);
2736 return error_mark_node;
2739 /* Mark variable-like entities as used. Functions are similarly
2740 marked either below or after overload resolution. */
2741 if (TREE_CODE (decl) == VAR_DECL
2742 || TREE_CODE (decl) == PARM_DECL
2743 || TREE_CODE (decl) == RESULT_DECL)
2748 decl = (adjust_result_of_qualified_name_lookup
2749 (decl, scope, current_class_type));
2751 if (TREE_CODE (decl) == FUNCTION_DECL)
2754 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2755 decl = finish_qualified_id_expr (scope,
2763 tree r = convert_from_reference (decl);
2765 if (processing_template_decl && TYPE_P (scope))
2766 r = build_qualified_name (TREE_TYPE (r),
2772 else if (TREE_CODE (decl) == FIELD_DECL)
2774 /* Since SCOPE is NULL here, this is an unqualified name.
2775 Access checking has been performed during name lookup
2776 already. Turn off checking to avoid duplicate errors. */
2777 push_deferring_access_checks (dk_no_check);
2778 decl = finish_non_static_data_member (decl, current_class_ref,
2779 /*qualifying_scope=*/NULL_TREE);
2780 pop_deferring_access_checks ();
2782 else if (is_overloaded_fn (decl))
2784 tree first_fn = OVL_CURRENT (decl);
2786 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2787 first_fn = DECL_TEMPLATE_RESULT (first_fn);
2789 if (!really_overloaded_fn (decl))
2790 mark_used (first_fn);
2793 && TREE_CODE (first_fn) == FUNCTION_DECL
2794 && DECL_FUNCTION_MEMBER_P (first_fn)
2795 && !shared_member_p (decl))
2797 /* A set of member functions. */
2798 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2799 return finish_class_member_access_expr (decl, id_expression,
2800 /*template_p=*/false);
2805 if (DECL_P (decl) && DECL_NONLOCAL (decl)
2806 && DECL_CLASS_SCOPE_P (decl)
2807 && DECL_CONTEXT (decl) != current_class_type)
2811 path = currently_open_derived_class (DECL_CONTEXT (decl));
2812 perform_or_defer_access_check (TYPE_BINFO (path), decl);
2815 decl = convert_from_reference (decl);
2819 if (TREE_DEPRECATED (decl))
2820 warn_deprecated_use (decl);
2825 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2826 use as a type-specifier. */
2829 finish_typeof (tree expr)
2833 if (type_dependent_expression_p (expr))
2835 type = make_aggr_type (TYPEOF_TYPE);
2836 TYPEOF_TYPE_EXPR (type) = expr;
2841 type = TREE_TYPE (expr);
2843 if (!type || type == unknown_type_node)
2845 error ("type of %qE is unknown", expr);
2846 return error_mark_node;
2852 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2853 with equivalent CALL_EXPRs. */
2856 simplify_aggr_init_exprs_r (tree* tp,
2858 void* data ATTRIBUTE_UNUSED)
2860 /* We don't need to walk into types; there's nothing in a type that
2861 needs simplification. (And, furthermore, there are places we
2862 actively don't want to go. For example, we don't want to wander
2863 into the default arguments for a FUNCTION_DECL that appears in a
2870 /* Only AGGR_INIT_EXPRs are interesting. */
2871 else if (TREE_CODE (*tp) != AGGR_INIT_EXPR)
2874 simplify_aggr_init_expr (tp);
2876 /* Keep iterating. */
2880 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
2881 function is broken out from the above for the benefit of the tree-ssa
2885 simplify_aggr_init_expr (tree *tp)
2887 tree aggr_init_expr = *tp;
2889 /* Form an appropriate CALL_EXPR. */
2890 tree fn = TREE_OPERAND (aggr_init_expr, 0);
2891 tree args = TREE_OPERAND (aggr_init_expr, 1);
2892 tree slot = TREE_OPERAND (aggr_init_expr, 2);
2893 tree type = TREE_TYPE (slot);
2896 enum style_t { ctor, arg, pcc } style;
2898 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2900 #ifdef PCC_STATIC_STRUCT_RETURN
2906 gcc_assert (TREE_ADDRESSABLE (type));
2912 /* Replace the first argument to the ctor with the address of the
2916 args = TREE_CHAIN (args);
2917 cxx_mark_addressable (slot);
2918 addr = build1 (ADDR_EXPR, build_pointer_type (type), slot);
2919 args = tree_cons (NULL_TREE, addr, args);
2922 call_expr = build3 (CALL_EXPR,
2923 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2924 fn, args, NULL_TREE);
2928 /* Just mark it addressable here, and leave the rest to
2929 expand_call{,_inline}. */
2930 cxx_mark_addressable (slot);
2931 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
2932 call_expr = build2 (MODIFY_EXPR, TREE_TYPE (call_expr), slot, call_expr);
2934 else if (style == pcc)
2936 /* If we're using the non-reentrant PCC calling convention, then we
2937 need to copy the returned value out of the static buffer into the
2939 push_deferring_access_checks (dk_no_check);
2940 call_expr = build_aggr_init (slot, call_expr,
2941 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2942 pop_deferring_access_checks ();
2943 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
2949 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2952 emit_associated_thunks (tree fn)
2954 /* When we use vcall offsets, we emit thunks with the virtual
2955 functions to which they thunk. The whole point of vcall offsets
2956 is so that you can know statically the entire set of thunks that
2957 will ever be needed for a given virtual function, thereby
2958 enabling you to output all the thunks with the function itself. */
2959 if (DECL_VIRTUAL_P (fn))
2963 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2965 if (!THUNK_ALIAS (thunk))
2967 use_thunk (thunk, /*emit_p=*/1);
2968 if (DECL_RESULT_THUNK_P (thunk))
2972 for (probe = DECL_THUNKS (thunk);
2973 probe; probe = TREE_CHAIN (probe))
2974 use_thunk (probe, /*emit_p=*/1);
2978 gcc_assert (!DECL_THUNKS (thunk));
2983 /* Generate RTL for FN. */
2986 expand_body (tree fn)
2988 tree saved_function;
2990 /* Compute the appropriate object-file linkage for inline
2992 if (DECL_DECLARED_INLINE_P (fn))
2993 import_export_decl (fn);
2995 /* If FN is external, then there's no point in generating RTL for
2996 it. This situation can arise with an inline function under
2997 `-fexternal-templates'; we instantiate the function, even though
2998 we're not planning on emitting it, in case we get a chance to
3000 if (DECL_EXTERNAL (fn))
3003 /* ??? When is this needed? */
3004 saved_function = current_function_decl;
3006 /* Emit any thunks that should be emitted at the same time as FN. */
3007 emit_associated_thunks (fn);
3009 /* This function is only called from cgraph, or recursively from
3010 emit_associated_thunks. In neither case should we be currently
3011 generating trees for a function. */
3012 gcc_assert (function_depth == 0);
3014 tree_rest_of_compilation (fn);
3016 current_function_decl = saved_function;
3018 if (DECL_CLONED_FUNCTION_P (fn))
3020 /* If this is a clone, go through the other clones now and mark
3021 their parameters used. We have to do that here, as we don't
3022 know whether any particular clone will be expanded, and
3023 therefore cannot pick one arbitrarily. */
3026 for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn));
3027 probe && DECL_CLONED_FUNCTION_P (probe);
3028 probe = TREE_CHAIN (probe))
3032 for (parms = DECL_ARGUMENTS (probe);
3033 parms; parms = TREE_CHAIN (parms))
3034 TREE_USED (parms) = 1;
3039 /* Generate RTL for FN. */
3042 expand_or_defer_fn (tree fn)
3044 /* When the parser calls us after finishing the body of a template
3045 function, we don't really want to expand the body. */
3046 if (processing_template_decl)
3048 /* Normally, collection only occurs in rest_of_compilation. So,
3049 if we don't collect here, we never collect junk generated
3050 during the processing of templates until we hit a
3051 non-template function. It's not safe to do this inside a
3052 nested class, though, as the parser may have local state that
3053 is not a GC root. */
3054 if (!function_depth)
3059 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
3060 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
3061 simplify_aggr_init_exprs_r,
3064 /* If this is a constructor or destructor body, we have to clone
3066 if (maybe_clone_body (fn))
3068 /* We don't want to process FN again, so pretend we've written
3069 it out, even though we haven't. */
3070 TREE_ASM_WRITTEN (fn) = 1;
3074 /* If this function is marked with the constructor attribute, add it
3075 to the list of functions to be called along with constructors
3076 from static duration objects. */
3077 if (DECL_STATIC_CONSTRUCTOR (fn))
3078 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
3080 /* If this function is marked with the destructor attribute, add it
3081 to the list of functions to be called along with destructors from
3082 static duration objects. */
3083 if (DECL_STATIC_DESTRUCTOR (fn))
3084 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
3086 /* We make a decision about linkage for these functions at the end
3087 of the compilation. Until that point, we do not want the back
3088 end to output them -- but we do want it to see the bodies of
3089 these functions so that it can inline them as appropriate. */
3090 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
3092 if (DECL_INTERFACE_KNOWN (fn))
3093 /* We've already made a decision as to how this function will
3097 DECL_EXTERNAL (fn) = 1;
3098 DECL_NOT_REALLY_EXTERN (fn) = 1;
3099 note_vague_linkage_fn (fn);
3100 /* A non-template inline function with external linkage will
3101 always be COMDAT. As we must eventually determine the
3102 linkage of all functions, and as that causes writes to
3103 the data mapped in from the PCH file, it's advantageous
3104 to mark the functions at this point. */
3105 if (!DECL_IMPLICIT_INSTANTIATION (fn))
3107 /* This function must have external linkage, as
3108 otherwise DECL_INTERFACE_KNOWN would have been
3110 gcc_assert (TREE_PUBLIC (fn));
3111 comdat_linkage (fn);
3112 DECL_INTERFACE_KNOWN (fn) = 1;
3116 import_export_decl (fn);
3118 /* If the user wants us to keep all inline functions, then mark
3119 this function as needed so that finish_file will make sure to
3121 if (flag_keep_inline_functions && DECL_DECLARED_INLINE_P (fn))
3125 /* There's no reason to do any of the work here if we're only doing
3126 semantic analysis; this code just generates RTL. */
3127 if (flag_syntax_only)
3132 /* Expand or defer, at the whim of the compilation unit manager. */
3133 cgraph_finalize_function (fn, function_depth > 1);
3145 /* Helper function for walk_tree, used by finalize_nrv below. */
3148 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
3150 struct nrv_data *dp = (struct nrv_data *)data;
3153 /* No need to walk into types. There wouldn't be any need to walk into
3154 non-statements, except that we have to consider STMT_EXPRs. */
3157 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
3158 but differs from using NULL_TREE in that it indicates that we care
3159 about the value of the RESULT_DECL. */
3160 else if (TREE_CODE (*tp) == RETURN_EXPR)
3161 TREE_OPERAND (*tp, 0) = dp->result;
3162 /* Change all cleanups for the NRV to only run when an exception is
3164 else if (TREE_CODE (*tp) == CLEANUP_STMT
3165 && CLEANUP_DECL (*tp) == dp->var)
3166 CLEANUP_EH_ONLY (*tp) = 1;
3167 /* Replace the DECL_EXPR for the NRV with an initialization of the
3168 RESULT_DECL, if needed. */
3169 else if (TREE_CODE (*tp) == DECL_EXPR
3170 && DECL_EXPR_DECL (*tp) == dp->var)
3173 if (DECL_INITIAL (dp->var)
3174 && DECL_INITIAL (dp->var) != error_mark_node)
3176 init = build2 (INIT_EXPR, void_type_node, dp->result,
3177 DECL_INITIAL (dp->var));
3178 DECL_INITIAL (dp->var) = error_mark_node;
3181 init = build_empty_stmt ();
3182 SET_EXPR_LOCUS (init, EXPR_LOCUS (*tp));
3185 /* And replace all uses of the NRV with the RESULT_DECL. */
3186 else if (*tp == dp->var)
3189 /* Avoid walking into the same tree more than once. Unfortunately, we
3190 can't just use walk_tree_without duplicates because it would only call
3191 us for the first occurrence of dp->var in the function body. */
3192 slot = htab_find_slot (dp->visited, *tp, INSERT);
3198 /* Keep iterating. */
3202 /* Called from finish_function to implement the named return value
3203 optimization by overriding all the RETURN_EXPRs and pertinent
3204 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3205 RESULT_DECL for the function. */
3208 finalize_nrv (tree *tp, tree var, tree result)
3210 struct nrv_data data;
3212 /* Copy debugging information from VAR to RESULT. */
3213 DECL_NAME (result) = DECL_NAME (var);
3214 DECL_ARTIFICIAL (result) = DECL_ARTIFICIAL (var);
3215 DECL_IGNORED_P (result) = DECL_IGNORED_P (var);
3216 DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (var);
3217 DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (var);
3218 /* Don't forget that we take its address. */
3219 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3222 data.result = result;
3223 data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3224 walk_tree (tp, finalize_nrv_r, &data, 0);
3225 htab_delete (data.visited);
3228 /* Perform initialization related to this module. */
3231 init_cp_semantics (void)
3235 #include "gt-cp-semantics.h"