1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998, 1999, 2000, 2001, 2002,
7 2003, 2004 Free Software Foundation, Inc.
8 Written by Mark Mitchell (mmitchell@usa.net) based on code found
9 formerly in parse.y and pt.c.
11 This file is part of GCC.
13 GCC is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
18 GCC is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING. If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 #include "coretypes.h"
34 #include "tree-inline.h"
35 #include "tree-mudflap.h"
45 #include "diagnostic.h"
47 #include "tree-iterator.h"
49 /* There routines provide a modular interface to perform many parsing
50 operations. They may therefore be used during actual parsing, or
51 during template instantiation, which may be regarded as a
52 degenerate form of parsing. Since the current g++ parser is
53 lacking in several respects, and will be reimplemented, we are
54 attempting to move most code that is not directly related to
55 parsing into this file; that will make implementing the new parser
56 much easier since it will be able to make use of these routines. */
58 static tree
maybe_convert_cond (tree
);
59 static tree
simplify_aggr_init_exprs_r (tree
*, int *, void *);
60 static void emit_associated_thunks (tree
);
61 static tree
finalize_nrv_r (tree
*, int *, void *);
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a TREE_LIST of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the TREE_LIST. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 /* Data for deferred access checking. */
116 static GTY(()) deferred_access
*deferred_access_stack
;
117 static GTY(()) deferred_access
*deferred_access_free_list
;
119 /* Save the current deferred access states and start deferred
120 access checking iff DEFER_P is true. */
123 push_deferring_access_checks (deferring_kind deferring
)
127 /* For context like template instantiation, access checking
128 disabling applies to all nested context. */
129 if (deferred_access_stack
130 && deferred_access_stack
->deferring_access_checks_kind
== dk_no_check
)
131 deferring
= dk_no_check
;
133 /* Recycle previously used free store if available. */
134 if (deferred_access_free_list
)
136 d
= deferred_access_free_list
;
137 deferred_access_free_list
= d
->next
;
140 d
= ggc_alloc (sizeof (deferred_access
));
142 d
->next
= deferred_access_stack
;
143 d
->deferred_access_checks
= NULL_TREE
;
144 d
->deferring_access_checks_kind
= deferring
;
145 deferred_access_stack
= d
;
148 /* Resume deferring access checks again after we stopped doing
152 resume_deferring_access_checks (void)
154 if (deferred_access_stack
->deferring_access_checks_kind
== dk_no_deferred
)
155 deferred_access_stack
->deferring_access_checks_kind
= dk_deferred
;
158 /* Stop deferring access checks. */
161 stop_deferring_access_checks (void)
163 if (deferred_access_stack
->deferring_access_checks_kind
== dk_deferred
)
164 deferred_access_stack
->deferring_access_checks_kind
= dk_no_deferred
;
167 /* Discard the current deferred access checks and restore the
171 pop_deferring_access_checks (void)
173 deferred_access
*d
= deferred_access_stack
;
174 deferred_access_stack
= d
->next
;
176 /* Remove references to access checks TREE_LIST. */
177 d
->deferred_access_checks
= NULL_TREE
;
179 /* Store in free list for later use. */
180 d
->next
= deferred_access_free_list
;
181 deferred_access_free_list
= d
;
184 /* Returns a TREE_LIST representing the deferred checks.
185 The TREE_PURPOSE of each node is the type through which the
186 access occurred; the TREE_VALUE is the declaration named.
190 get_deferred_access_checks (void)
192 return deferred_access_stack
->deferred_access_checks
;
195 /* Take current deferred checks and combine with the
196 previous states if we also defer checks previously.
197 Otherwise perform checks now. */
200 pop_to_parent_deferring_access_checks (void)
202 tree deferred_check
= get_deferred_access_checks ();
203 deferred_access
*d1
= deferred_access_stack
;
204 deferred_access
*d2
= deferred_access_stack
->next
;
205 deferred_access
*d3
= deferred_access_stack
->next
->next
;
207 /* Temporary swap the order of the top two states, just to make
208 sure the garbage collector will not reclaim the memory during
210 deferred_access_stack
= d2
;
214 for ( ; deferred_check
; deferred_check
= TREE_CHAIN (deferred_check
))
215 /* Perform deferred check if required. */
216 perform_or_defer_access_check (TREE_PURPOSE (deferred_check
),
217 TREE_VALUE (deferred_check
));
219 deferred_access_stack
= d1
;
222 pop_deferring_access_checks ();
225 /* Perform the deferred access checks.
227 After performing the checks, we still have to keep the list
228 `deferred_access_stack->deferred_access_checks' since we may want
229 to check access for them again later in a different context.
236 A::X A::a, x; // No error for `A::a', error for `x'
238 We have to perform deferred access of `A::X', first with `A::a',
242 perform_deferred_access_checks (void)
245 for (deferred_check
= deferred_access_stack
->deferred_access_checks
;
247 deferred_check
= TREE_CHAIN (deferred_check
))
249 enforce_access (TREE_PURPOSE (deferred_check
),
250 TREE_VALUE (deferred_check
));
253 /* Defer checking the accessibility of DECL, when looked up in
257 perform_or_defer_access_check (tree binfo
, tree decl
)
261 my_friendly_assert (TREE_CODE (binfo
) == TREE_VEC
, 20030623);
263 /* If we are not supposed to defer access checks, just check now. */
264 if (deferred_access_stack
->deferring_access_checks_kind
== dk_no_deferred
)
266 enforce_access (binfo
, decl
);
269 /* Exit if we are in a context that no access checking is performed. */
270 else if (deferred_access_stack
->deferring_access_checks_kind
== dk_no_check
)
273 /* See if we are already going to perform this check. */
274 for (check
= deferred_access_stack
->deferred_access_checks
;
276 check
= TREE_CHAIN (check
))
277 if (TREE_VALUE (check
) == decl
&& TREE_PURPOSE (check
) == binfo
)
279 /* If not, record the check. */
280 deferred_access_stack
->deferred_access_checks
281 = tree_cons (binfo
, decl
,
282 deferred_access_stack
->deferred_access_checks
);
285 /* Returns nonzero if the current statement is a full expression,
286 i.e. temporaries created during that statement should be destroyed
287 at the end of the statement. */
290 stmts_are_full_exprs_p (void)
292 return current_stmt_tree ()->stmts_are_full_exprs_p
;
295 /* Returns the stmt_tree (if any) to which statements are currently
296 being added. If there is no active statement-tree, NULL is
300 current_stmt_tree (void)
303 ? &cfun
->language
->base
.x_stmt_tree
304 : &scope_chain
->x_stmt_tree
);
307 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
310 maybe_cleanup_point_expr (tree expr
)
312 if (!processing_template_decl
&& stmts_are_full_exprs_p ())
313 expr
= fold (build1 (CLEANUP_POINT_EXPR
, TREE_TYPE (expr
), expr
));
317 /* Create a declaration statement for the declaration given by the DECL. */
320 add_decl_stmt (tree decl
)
322 tree r
= build_stmt (DECL_STMT
, decl
);
323 if (DECL_INITIAL (decl
))
324 r
= maybe_cleanup_point_expr (r
);
328 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
329 flag for this because "A union for which objects or pointers are
330 declared is not an anonymous union" [class.union]. */
333 anon_aggr_type_p (tree node
)
335 return ANON_AGGR_TYPE_P (node
);
338 /* Finish a scope. */
341 do_poplevel (tree stmt_list
)
345 if (stmts_are_full_exprs_p ())
346 block
= poplevel (kept_level_p (), 1, 0);
348 stmt_list
= pop_stmt_list (stmt_list
);
350 if (!processing_template_decl
)
352 stmt_list
= c_build_bind_expr (block
, stmt_list
);
353 /* ??? See c_end_compound_stmt re statement expressions. */
359 /* Begin a new scope. */
362 do_pushlevel (scope_kind sk
)
364 tree ret
= push_stmt_list ();
365 if (stmts_are_full_exprs_p ())
366 begin_scope (sk
, NULL
);
370 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
371 when the current scope is exited. EH_ONLY is true when this is not
372 meant to apply to normal control flow transfer. */
375 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
377 tree stmt
= build_stmt (CLEANUP_STMT
, NULL
, cleanup
, decl
);
378 CLEANUP_EH_ONLY (stmt
) = eh_only
;
380 CLEANUP_BODY (stmt
) = push_stmt_list ();
383 /* Begin a conditional that might contain a declaration. When generating
384 normal code, we want the declaration to appear before the statement
385 containing the conditional. When generating template code, we want the
386 conditional to be rendered as the raw DECL_STMT. */
389 begin_cond (tree
*cond_p
)
391 if (processing_template_decl
)
392 *cond_p
= push_stmt_list ();
395 /* Finish such a conditional. */
398 finish_cond (tree
*cond_p
, tree expr
)
400 if (processing_template_decl
)
402 tree cond
= pop_stmt_list (*cond_p
);
403 if (TREE_CODE (cond
) == DECL_STMT
)
409 /* If *COND_P specifies a conditional with a declaration, transform the
412 for (; A x = 42;) { }
414 while (true) { A x = 42; if (!x) break; }
415 for (;;) { A x = 42; if (!x) break; }
416 The statement list for BODY will be empty if the conditional did
417 not declare anything. */
420 simplify_loop_decl_cond (tree
*cond_p
, tree body
)
424 if (!TREE_SIDE_EFFECTS (body
))
428 *cond_p
= boolean_true_node
;
430 if_stmt
= begin_if_stmt ();
431 cond
= build_unary_op (TRUTH_NOT_EXPR
, cond
, 0);
432 finish_if_stmt_cond (cond
, if_stmt
);
433 finish_break_stmt ();
434 finish_then_clause (if_stmt
);
435 finish_if_stmt (if_stmt
);
438 /* Finish a goto-statement. */
441 finish_goto_stmt (tree destination
)
443 if (TREE_CODE (destination
) == IDENTIFIER_NODE
)
444 destination
= lookup_label (destination
);
446 /* We warn about unused labels with -Wunused. That means we have to
447 mark the used labels as used. */
448 if (TREE_CODE (destination
) == LABEL_DECL
)
449 TREE_USED (destination
) = 1;
452 /* The DESTINATION is being used as an rvalue. */
453 if (!processing_template_decl
)
454 destination
= decay_conversion (destination
);
455 /* We don't inline calls to functions with computed gotos.
456 Those functions are typically up to some funny business,
457 and may be depending on the labels being at particular
458 addresses, or some such. */
459 DECL_UNINLINABLE (current_function_decl
) = 1;
462 check_goto (destination
);
464 return add_stmt (build_stmt (GOTO_EXPR
, destination
));
467 /* COND is the condition-expression for an if, while, etc.,
468 statement. Convert it to a boolean value, if appropriate. */
471 maybe_convert_cond (tree cond
)
473 /* Empty conditions remain empty. */
477 /* Wait until we instantiate templates before doing conversion. */
478 if (processing_template_decl
)
481 /* Do the conversion. */
482 cond
= convert_from_reference (cond
);
483 return condition_conversion (cond
);
486 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
489 finish_expr_stmt (tree expr
)
493 if (expr
!= NULL_TREE
)
495 if (!processing_template_decl
)
497 if (warn_sequence_point
)
498 verify_sequence_points (expr
);
499 expr
= convert_to_void (expr
, "statement");
501 else if (!type_dependent_expression_p (expr
))
502 convert_to_void (build_non_dependent_expr (expr
), "statement");
504 /* Simplification of inner statement expressions, compound exprs,
505 etc can result in the us already having an EXPR_STMT. */
506 if (TREE_CODE (expr
) != CLEANUP_POINT_EXPR
)
508 if (TREE_CODE (expr
) != EXPR_STMT
)
509 expr
= build_stmt (EXPR_STMT
, expr
);
510 expr
= maybe_cleanup_point_expr (expr
);
522 /* Begin an if-statement. Returns a newly created IF_STMT if
529 scope
= do_pushlevel (sk_block
);
530 r
= build_stmt (IF_STMT
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
531 TREE_CHAIN (r
) = scope
;
532 begin_cond (&IF_COND (r
));
536 /* Process the COND of an if-statement, which may be given by
540 finish_if_stmt_cond (tree cond
, tree if_stmt
)
542 finish_cond (&IF_COND (if_stmt
), maybe_convert_cond (cond
));
544 THEN_CLAUSE (if_stmt
) = push_stmt_list ();
547 /* Finish the then-clause of an if-statement, which may be given by
551 finish_then_clause (tree if_stmt
)
553 THEN_CLAUSE (if_stmt
) = pop_stmt_list (THEN_CLAUSE (if_stmt
));
557 /* Begin the else-clause of an if-statement. */
560 begin_else_clause (tree if_stmt
)
562 ELSE_CLAUSE (if_stmt
) = push_stmt_list ();
565 /* Finish the else-clause of an if-statement, which may be given by
569 finish_else_clause (tree if_stmt
)
571 ELSE_CLAUSE (if_stmt
) = pop_stmt_list (ELSE_CLAUSE (if_stmt
));
574 /* Finish an if-statement. */
577 finish_if_stmt (tree if_stmt
)
579 tree scope
= TREE_CHAIN (if_stmt
);
580 TREE_CHAIN (if_stmt
) = NULL
;
581 add_stmt (do_poplevel (scope
));
585 /* Begin a while-statement. Returns a newly created WHILE_STMT if
589 begin_while_stmt (void)
592 r
= build_stmt (WHILE_STMT
, NULL_TREE
, NULL_TREE
);
594 WHILE_BODY (r
) = do_pushlevel (sk_block
);
595 begin_cond (&WHILE_COND (r
));
599 /* Process the COND of a while-statement, which may be given by
603 finish_while_stmt_cond (tree cond
, tree while_stmt
)
605 finish_cond (&WHILE_COND (while_stmt
), maybe_convert_cond (cond
));
606 simplify_loop_decl_cond (&WHILE_COND (while_stmt
), WHILE_BODY (while_stmt
));
609 /* Finish a while-statement, which may be given by WHILE_STMT. */
612 finish_while_stmt (tree while_stmt
)
614 WHILE_BODY (while_stmt
) = do_poplevel (WHILE_BODY (while_stmt
));
618 /* Begin a do-statement. Returns a newly created DO_STMT if
624 tree r
= build_stmt (DO_STMT
, NULL_TREE
, NULL_TREE
);
626 DO_BODY (r
) = push_stmt_list ();
630 /* Finish the body of a do-statement, which may be given by DO_STMT. */
633 finish_do_body (tree do_stmt
)
635 DO_BODY (do_stmt
) = pop_stmt_list (DO_BODY (do_stmt
));
638 /* Finish a do-statement, which may be given by DO_STMT, and whose
639 COND is as indicated. */
642 finish_do_stmt (tree cond
, tree do_stmt
)
644 cond
= maybe_convert_cond (cond
);
645 DO_COND (do_stmt
) = cond
;
649 /* Finish a return-statement. The EXPRESSION returned, if any, is as
653 finish_return_stmt (tree expr
)
657 expr
= check_return_expr (expr
);
658 if (!processing_template_decl
)
660 if (DECL_DESTRUCTOR_P (current_function_decl
))
662 /* Similarly, all destructors must run destructors for
663 base-classes before returning. So, all returns in a
664 destructor get sent to the DTOR_LABEL; finish_function emits
665 code to return a value there. */
666 return finish_goto_stmt (dtor_label
);
670 r
= build_stmt (RETURN_STMT
, expr
);
671 r
= maybe_cleanup_point_expr (r
);
678 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
681 begin_for_stmt (void)
685 r
= build_stmt (FOR_STMT
, NULL_TREE
, NULL_TREE
,
686 NULL_TREE
, NULL_TREE
);
688 if (flag_new_for_scope
> 0)
689 TREE_CHAIN (r
) = do_pushlevel (sk_for
);
691 if (processing_template_decl
)
692 FOR_INIT_STMT (r
) = push_stmt_list ();
697 /* Finish the for-init-statement of a for-statement, which may be
698 given by FOR_STMT. */
701 finish_for_init_stmt (tree for_stmt
)
703 if (processing_template_decl
)
704 FOR_INIT_STMT (for_stmt
) = pop_stmt_list (FOR_INIT_STMT (for_stmt
));
706 FOR_BODY (for_stmt
) = do_pushlevel (sk_block
);
707 begin_cond (&FOR_COND (for_stmt
));
710 /* Finish the COND of a for-statement, which may be given by
714 finish_for_cond (tree cond
, tree for_stmt
)
716 finish_cond (&FOR_COND (for_stmt
), maybe_convert_cond (cond
));
717 simplify_loop_decl_cond (&FOR_COND (for_stmt
), FOR_BODY (for_stmt
));
720 /* Finish the increment-EXPRESSION in a for-statement, which may be
721 given by FOR_STMT. */
724 finish_for_expr (tree expr
, tree for_stmt
)
728 /* If EXPR is an overloaded function, issue an error; there is no
729 context available to use to perform overload resolution. */
730 if (type_unknown_p (expr
))
732 cxx_incomplete_type_error (expr
, TREE_TYPE (expr
));
733 expr
= error_mark_node
;
735 expr
= maybe_cleanup_point_expr (expr
);
736 FOR_EXPR (for_stmt
) = expr
;
739 /* Finish the body of a for-statement, which may be given by
740 FOR_STMT. The increment-EXPR for the loop must be
744 finish_for_stmt (tree for_stmt
)
746 FOR_BODY (for_stmt
) = do_poplevel (FOR_BODY (for_stmt
));
748 /* Pop the scope for the body of the loop. */
749 if (flag_new_for_scope
> 0)
751 tree scope
= TREE_CHAIN (for_stmt
);
752 TREE_CHAIN (for_stmt
) = NULL
;
753 add_stmt (do_poplevel (scope
));
759 /* Finish a break-statement. */
762 finish_break_stmt (void)
764 return add_stmt (build_break_stmt ());
767 /* Finish a continue-statement. */
770 finish_continue_stmt (void)
772 return add_stmt (build_continue_stmt ());
775 /* Begin a switch-statement. Returns a new SWITCH_STMT if
779 begin_switch_stmt (void)
783 r
= build_stmt (SWITCH_STMT
, NULL_TREE
, NULL_TREE
, NULL_TREE
);
785 scope
= do_pushlevel (sk_block
);
786 TREE_CHAIN (r
) = scope
;
787 begin_cond (&SWITCH_COND (r
));
792 /* Finish the cond of a switch-statement. */
795 finish_switch_cond (tree cond
, tree switch_stmt
)
797 tree orig_type
= NULL
;
798 if (!processing_template_decl
)
802 /* Convert the condition to an integer or enumeration type. */
803 cond
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, cond
, true);
804 if (cond
== NULL_TREE
)
806 error ("switch quantity not an integer");
807 cond
= error_mark_node
;
809 orig_type
= TREE_TYPE (cond
);
810 if (cond
!= error_mark_node
)
814 Integral promotions are performed. */
815 cond
= perform_integral_promotions (cond
);
816 cond
= maybe_cleanup_point_expr (cond
);
819 if (cond
!= error_mark_node
)
821 index
= get_unwidened (cond
, NULL_TREE
);
822 /* We can't strip a conversion from a signed type to an unsigned,
823 because if we did, int_fits_type_p would do the wrong thing
824 when checking case values for being in range,
825 and it's too hard to do the right thing. */
826 if (TYPE_UNSIGNED (TREE_TYPE (cond
))
827 == TYPE_UNSIGNED (TREE_TYPE (index
)))
831 finish_cond (&SWITCH_COND (switch_stmt
), cond
);
832 SWITCH_TYPE (switch_stmt
) = orig_type
;
833 add_stmt (switch_stmt
);
834 push_switch (switch_stmt
);
835 SWITCH_BODY (switch_stmt
) = push_stmt_list ();
838 /* Finish the body of a switch-statement, which may be given by
839 SWITCH_STMT. The COND to switch on is indicated. */
842 finish_switch_stmt (tree switch_stmt
)
846 SWITCH_BODY (switch_stmt
) = pop_stmt_list (SWITCH_BODY (switch_stmt
));
850 scope
= TREE_CHAIN (switch_stmt
);
851 TREE_CHAIN (switch_stmt
) = NULL
;
852 add_stmt (do_poplevel (scope
));
855 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
859 begin_try_block (void)
861 tree r
= build_stmt (TRY_BLOCK
, NULL_TREE
, NULL_TREE
);
863 TRY_STMTS (r
) = push_stmt_list ();
867 /* Likewise, for a function-try-block. */
870 begin_function_try_block (void)
872 tree r
= begin_try_block ();
873 FN_TRY_BLOCK_P (r
) = 1;
877 /* Finish a try-block, which may be given by TRY_BLOCK. */
880 finish_try_block (tree try_block
)
882 TRY_STMTS (try_block
) = pop_stmt_list (TRY_STMTS (try_block
));
883 TRY_HANDLERS (try_block
) = push_stmt_list ();
886 /* Finish the body of a cleanup try-block, which may be given by
890 finish_cleanup_try_block (tree try_block
)
892 TRY_STMTS (try_block
) = pop_stmt_list (TRY_STMTS (try_block
));
895 /* Finish an implicitly generated try-block, with a cleanup is given
899 finish_cleanup (tree cleanup
, tree try_block
)
901 TRY_HANDLERS (try_block
) = cleanup
;
902 CLEANUP_P (try_block
) = 1;
905 /* Likewise, for a function-try-block. */
908 finish_function_try_block (tree try_block
)
910 finish_try_block (try_block
);
911 /* FIXME : something queer about CTOR_INITIALIZER somehow following
912 the try block, but moving it inside. */
913 in_function_try_handler
= 1;
916 /* Finish a handler-sequence for a try-block, which may be given by
920 finish_handler_sequence (tree try_block
)
922 TRY_HANDLERS (try_block
) = pop_stmt_list (TRY_HANDLERS (try_block
));
923 check_handlers (TRY_HANDLERS (try_block
));
926 /* Likewise, for a function-try-block. */
929 finish_function_handler_sequence (tree try_block
)
931 in_function_try_handler
= 0;
932 finish_handler_sequence (try_block
);
935 /* Begin a handler. Returns a HANDLER if appropriate. */
942 r
= build_stmt (HANDLER
, NULL_TREE
, NULL_TREE
);
945 /* Create a binding level for the eh_info and the exception object
947 HANDLER_BODY (r
) = do_pushlevel (sk_catch
);
952 /* Finish the handler-parameters for a handler, which may be given by
953 HANDLER. DECL is the declaration for the catch parameter, or NULL
954 if this is a `catch (...)' clause. */
957 finish_handler_parms (tree decl
, tree handler
)
959 tree type
= NULL_TREE
;
960 if (processing_template_decl
)
964 decl
= pushdecl (decl
);
965 decl
= push_template_decl (decl
);
966 HANDLER_PARMS (handler
) = decl
;
967 type
= TREE_TYPE (decl
);
971 type
= expand_start_catch_block (decl
);
973 HANDLER_TYPE (handler
) = type
;
974 if (!processing_template_decl
&& type
)
975 mark_used (eh_type_info (type
));
978 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
979 the return value from the matching call to finish_handler_parms. */
982 finish_handler (tree handler
)
984 if (!processing_template_decl
)
985 expand_end_catch_block ();
986 HANDLER_BODY (handler
) = do_poplevel (HANDLER_BODY (handler
));
989 /* Begin a compound statement. FLAGS contains some bits that control the
990 behaviour and context. If BCS_NO_SCOPE is set, the compound statement
991 does not define a scope. If BCS_FN_BODY is set, this is the outermost
992 block of a function. If BCS_TRY_BLOCK is set, this is the block
993 created on behalf of a TRY statement. Returns a token to be passed to
994 finish_compound_stmt. */
997 begin_compound_stmt (unsigned int flags
)
1001 if (flags
& BCS_NO_SCOPE
)
1003 r
= push_stmt_list ();
1004 STATEMENT_LIST_NO_SCOPE (r
) = 1;
1006 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1007 But, if it's a statement-expression with a scopeless block, there's
1008 nothing to keep, and we don't want to accidentally keep a block
1009 *inside* the scopeless block. */
1010 keep_next_level (false);
1013 r
= do_pushlevel (flags
& BCS_TRY_BLOCK
? sk_try
: sk_block
);
1015 /* When processing a template, we need to remember where the braces were,
1016 so that we can set up identical scopes when instantiating the template
1017 later. BIND_EXPR is a handy candidate for this.
1018 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1019 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1020 processing templates. */
1021 if (processing_template_decl
)
1023 r
= build (BIND_EXPR
, NULL
, NULL
, r
, NULL
);
1024 BIND_EXPR_TRY_BLOCK (r
) = (flags
& BCS_TRY_BLOCK
) != 0;
1025 BIND_EXPR_BODY_BLOCK (r
) = (flags
& BCS_FN_BODY
) != 0;
1026 TREE_SIDE_EFFECTS (r
) = 1;
1032 /* Finish a compound-statement, which is given by STMT. */
1035 finish_compound_stmt (tree stmt
)
1037 if (TREE_CODE (stmt
) == BIND_EXPR
)
1038 BIND_EXPR_BODY (stmt
) = do_poplevel (BIND_EXPR_BODY (stmt
));
1039 else if (STATEMENT_LIST_NO_SCOPE (stmt
))
1040 stmt
= pop_stmt_list (stmt
);
1042 stmt
= do_poplevel (stmt
);
1044 /* ??? See c_end_compound_stmt wrt statement expressions. */
1049 /* Finish an asm-statement, whose components are a STRING, some
1050 OUTPUT_OPERANDS, some INPUT_OPERANDS, and some CLOBBERS. Also note
1051 whether the asm-statement should be considered volatile. */
1054 finish_asm_stmt (int volatile_p
, tree string
, tree output_operands
,
1055 tree input_operands
, tree clobbers
)
1060 if (!processing_template_decl
)
1066 for (t
= input_operands
; t
; t
= TREE_CHAIN (t
))
1068 tree converted_operand
1069 = decay_conversion (TREE_VALUE (t
));
1071 /* If the type of the operand hasn't been determined (e.g.,
1072 because it involves an overloaded function), then issue
1073 an error message. There's no context available to
1074 resolve the overloading. */
1075 if (TREE_TYPE (converted_operand
) == unknown_type_node
)
1077 error ("type of asm operand `%E' could not be determined",
1079 converted_operand
= error_mark_node
;
1081 TREE_VALUE (t
) = converted_operand
;
1084 ninputs
= list_length (input_operands
);
1085 noutputs
= list_length (output_operands
);
1087 for (i
= 0, t
= output_operands
; t
; t
= TREE_CHAIN (t
), ++i
)
1092 const char *constraint
;
1095 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t
)));
1096 operand
= TREE_VALUE (t
);
1098 if (!parse_output_constraint (&constraint
,
1099 i
, ninputs
, noutputs
,
1104 /* By marking this operand as erroneous, we will not try
1105 to process this operand again in expand_asm_operands. */
1106 TREE_VALUE (t
) = error_mark_node
;
1110 /* If the operand is a DECL that is going to end up in
1111 memory, assume it is addressable. This is a bit more
1112 conservative than it would ideally be; the exact test is
1113 buried deep in expand_asm_operands and depends on the
1114 DECL_RTL for the OPERAND -- which we don't have at this
1116 if (!allows_reg
&& DECL_P (operand
))
1117 cxx_mark_addressable (operand
);
1121 r
= build_stmt (ASM_EXPR
, string
,
1122 output_operands
, input_operands
,
1124 ASM_VOLATILE_P (r
) = volatile_p
;
1125 return add_stmt (r
);
1128 /* Finish a label with the indicated NAME. */
1131 finish_label_stmt (tree name
)
1133 tree decl
= define_label (input_location
, name
);
1134 return add_stmt (build_stmt (LABEL_EXPR
, decl
));
1137 /* Finish a series of declarations for local labels. G++ allows users
1138 to declare "local" labels, i.e., labels with scope. This extension
1139 is useful when writing code involving statement-expressions. */
1142 finish_label_decl (tree name
)
1144 tree decl
= declare_local_label (name
);
1145 add_decl_stmt (decl
);
1148 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1151 finish_decl_cleanup (tree decl
, tree cleanup
)
1153 push_cleanup (decl
, cleanup
, false);
1156 /* If the current scope exits with an exception, run CLEANUP. */
1159 finish_eh_cleanup (tree cleanup
)
1161 push_cleanup (NULL
, cleanup
, true);
1164 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1165 order they were written by the user. Each node is as for
1166 emit_mem_initializers. */
1169 finish_mem_initializers (tree mem_inits
)
1171 /* Reorder the MEM_INITS so that they are in the order they appeared
1172 in the source program. */
1173 mem_inits
= nreverse (mem_inits
);
1175 if (processing_template_decl
)
1176 add_stmt (build_min_nt (CTOR_INITIALIZER
, mem_inits
));
1178 emit_mem_initializers (mem_inits
);
1181 /* Finish a parenthesized expression EXPR. */
1184 finish_parenthesized_expr (tree expr
)
1186 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr
))))
1187 /* This inhibits warnings in c_common_truthvalue_conversion. */
1188 C_SET_EXP_ORIGINAL_CODE (expr
, ERROR_MARK
);
1190 if (TREE_CODE (expr
) == OFFSET_REF
)
1191 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1192 enclosed in parentheses. */
1193 PTRMEM_OK_P (expr
) = 0;
1197 /* Finish a reference to a non-static data member (DECL) that is not
1198 preceded by `.' or `->'. */
1201 finish_non_static_data_member (tree decl
, tree object
, tree qualifying_scope
)
1203 my_friendly_assert (TREE_CODE (decl
) == FIELD_DECL
, 20020909);
1207 if (current_function_decl
1208 && DECL_STATIC_FUNCTION_P (current_function_decl
))
1209 cp_error_at ("invalid use of member `%D' in static member function",
1212 cp_error_at ("invalid use of non-static data member `%D'", decl
);
1213 error ("from this location");
1215 return error_mark_node
;
1217 TREE_USED (current_class_ptr
) = 1;
1218 if (processing_template_decl
&& !qualifying_scope
)
1220 tree type
= TREE_TYPE (decl
);
1222 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1223 type
= TREE_TYPE (type
);
1226 /* Set the cv qualifiers. */
1227 int quals
= cp_type_quals (TREE_TYPE (current_class_ref
));
1229 if (DECL_MUTABLE_P (decl
))
1230 quals
&= ~TYPE_QUAL_CONST
;
1232 quals
|= cp_type_quals (TREE_TYPE (decl
));
1233 type
= cp_build_qualified_type (type
, quals
);
1236 return build_min (COMPONENT_REF
, type
, object
, decl
);
1240 tree access_type
= TREE_TYPE (object
);
1241 tree lookup_context
= context_for_name_lookup (decl
);
1243 while (!DERIVED_FROM_P (lookup_context
, access_type
))
1245 access_type
= TYPE_CONTEXT (access_type
);
1246 while (access_type
&& DECL_P (access_type
))
1247 access_type
= DECL_CONTEXT (access_type
);
1251 cp_error_at ("object missing in reference to `%D'", decl
);
1252 error ("from this location");
1253 return error_mark_node
;
1257 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1258 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1260 if (processing_template_decl
)
1261 return build_min (SCOPE_REF
, TREE_TYPE (decl
),
1262 qualifying_scope
, DECL_NAME (decl
));
1264 perform_or_defer_access_check (TYPE_BINFO (access_type
), decl
);
1266 /* If the data member was named `C::M', convert `*this' to `C'
1268 if (qualifying_scope
)
1270 tree binfo
= NULL_TREE
;
1271 object
= build_scoped_ref (object
, qualifying_scope
,
1275 return build_class_member_access_expr (object
, decl
,
1276 /*access_path=*/NULL_TREE
,
1277 /*preserve_reference=*/false);
1281 /* DECL was the declaration to which a qualified-id resolved. Issue
1282 an error message if it is not accessible. If OBJECT_TYPE is
1283 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1284 type of `*x', or `x', respectively. If the DECL was named as
1285 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1288 check_accessibility_of_qualified_id (tree decl
,
1290 tree nested_name_specifier
)
1293 tree qualifying_type
= NULL_TREE
;
1295 /* Determine the SCOPE of DECL. */
1296 scope
= context_for_name_lookup (decl
);
1297 /* If the SCOPE is not a type, then DECL is not a member. */
1298 if (!TYPE_P (scope
))
1300 /* Compute the scope through which DECL is being accessed. */
1302 /* OBJECT_TYPE might not be a class type; consider:
1304 class A { typedef int I; };
1308 In this case, we will have "A::I" as the DECL, but "I" as the
1310 && CLASS_TYPE_P (object_type
)
1311 && DERIVED_FROM_P (scope
, object_type
))
1312 /* If we are processing a `->' or `.' expression, use the type of the
1314 qualifying_type
= object_type
;
1315 else if (nested_name_specifier
)
1317 /* If the reference is to a non-static member of the
1318 current class, treat it as if it were referenced through
1320 if (DECL_NONSTATIC_MEMBER_P (decl
)
1321 && current_class_ptr
1322 && DERIVED_FROM_P (scope
, current_class_type
))
1323 qualifying_type
= current_class_type
;
1324 /* Otherwise, use the type indicated by the
1325 nested-name-specifier. */
1327 qualifying_type
= nested_name_specifier
;
1330 /* Otherwise, the name must be from the current class or one of
1332 qualifying_type
= currently_open_derived_class (scope
);
1334 if (qualifying_type
)
1335 perform_or_defer_access_check (TYPE_BINFO (qualifying_type
), decl
);
1338 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1339 class named to the left of the "::" operator. DONE is true if this
1340 expression is a complete postfix-expression; it is false if this
1341 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1342 iff this expression is the operand of '&'. */
1345 finish_qualified_id_expr (tree qualifying_class
, tree expr
, bool done
,
1348 if (error_operand_p (expr
))
1349 return error_mark_node
;
1351 /* If EXPR occurs as the operand of '&', use special handling that
1352 permits a pointer-to-member. */
1353 if (address_p
&& done
)
1355 if (TREE_CODE (expr
) == SCOPE_REF
)
1356 expr
= TREE_OPERAND (expr
, 1);
1357 expr
= build_offset_ref (qualifying_class
, expr
,
1358 /*address_p=*/true);
1362 if (TREE_CODE (expr
) == FIELD_DECL
)
1363 expr
= finish_non_static_data_member (expr
, current_class_ref
,
1365 else if (BASELINK_P (expr
) && !processing_template_decl
)
1370 /* See if any of the functions are non-static members. */
1371 fns
= BASELINK_FUNCTIONS (expr
);
1372 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
1373 fns
= TREE_OPERAND (fns
, 0);
1374 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1375 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
1377 /* If so, the expression may be relative to the current
1379 if (fn
&& current_class_type
1380 && DERIVED_FROM_P (qualifying_class
, current_class_type
))
1381 expr
= (build_class_member_access_expr
1382 (maybe_dummy_object (qualifying_class
, NULL
),
1384 BASELINK_ACCESS_BINFO (expr
),
1385 /*preserve_reference=*/false));
1387 /* The expression is a qualified name whose address is not
1389 expr
= build_offset_ref (qualifying_class
, expr
, /*address_p=*/false);
1395 /* Begin a statement-expression. The value returned must be passed to
1396 finish_stmt_expr. */
1399 begin_stmt_expr (void)
1401 return push_stmt_list ();
1404 /* Process the final expression of a statement expression. EXPR can be
1405 NULL, if the final expression is empty. Build up a TARGET_EXPR so
1406 that the result value can be safely returned to the enclosing
1410 finish_stmt_expr_expr (tree expr
, tree stmt_expr
)
1412 tree result
= NULL_TREE
;
1416 if (!processing_template_decl
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
1418 tree type
= TREE_TYPE (expr
);
1420 if (TREE_CODE (type
) == ARRAY_TYPE
1421 || TREE_CODE (type
) == FUNCTION_TYPE
)
1422 expr
= decay_conversion (expr
);
1424 expr
= convert_from_reference (expr
);
1425 expr
= require_complete_type (expr
);
1427 type
= TREE_TYPE (expr
);
1429 /* Build a TARGET_EXPR for this aggregate. finish_stmt_expr
1430 will then pull it apart so the lifetime of the target is
1431 within the scope of the expression containing this statement
1433 if (TREE_CODE (expr
) == TARGET_EXPR
)
1435 else if (!IS_AGGR_TYPE (type
) || TYPE_HAS_TRIVIAL_INIT_REF (type
))
1436 expr
= build_target_expr_with_type (expr
, type
);
1439 /* Copy construct. */
1440 expr
= build_special_member_call
1441 (NULL_TREE
, complete_ctor_identifier
,
1442 build_tree_list (NULL_TREE
, expr
),
1443 TYPE_BINFO (type
), LOOKUP_NORMAL
);
1444 expr
= build_cplus_new (type
, expr
);
1445 my_friendly_assert (TREE_CODE (expr
) == TARGET_EXPR
, 20030729);
1449 if (expr
!= error_mark_node
)
1451 result
= build_stmt (EXPR_STMT
, expr
);
1452 EXPR_STMT_STMT_EXPR_RESULT (result
) = 1;
1459 /* Remember the last expression so that finish_stmt_expr
1460 can pull it apart. */
1461 TREE_TYPE (stmt_expr
) = result
;
1466 /* Finish a statement-expression. EXPR should be the value returned
1467 by the previous begin_stmt_expr. Returns an expression
1468 representing the statement-expression. */
1471 finish_stmt_expr (tree stmt_expr
, bool has_no_scope
)
1473 tree result
, result_stmt
, type
;
1474 tree
*result_stmt_p
= NULL
;
1476 result_stmt
= TREE_TYPE (stmt_expr
);
1477 TREE_TYPE (stmt_expr
) = void_type_node
;
1478 result
= pop_stmt_list (stmt_expr
);
1480 if (!result_stmt
|| VOID_TYPE_P (result_stmt
))
1481 type
= void_type_node
;
1484 /* We need to search the statement expression for the result_stmt,
1485 since we'll need to replace it entirely. */
1487 result_stmt_p
= &result
;
1491 if (t
== result_stmt
)
1494 switch (TREE_CODE (t
))
1496 case STATEMENT_LIST
:
1498 tree_stmt_iterator i
= tsi_last (t
);
1499 result_stmt_p
= tsi_stmt_ptr (i
);
1503 result_stmt_p
= &BIND_EXPR_BODY (t
);
1505 case TRY_FINALLY_EXPR
:
1506 case TRY_CATCH_EXPR
:
1508 result_stmt_p
= &TREE_OPERAND (t
, 0);
1514 type
= TREE_TYPE (EXPR_STMT_EXPR (result_stmt
));
1517 if (processing_template_decl
)
1519 result
= build_min (STMT_EXPR
, type
, result
);
1520 TREE_SIDE_EFFECTS (result
) = 1;
1521 STMT_EXPR_NO_SCOPE (result
) = has_no_scope
;
1523 else if (!VOID_TYPE_P (type
))
1525 /* Pull out the TARGET_EXPR that is the final expression. Put
1526 the target's init_expr as the final expression and then put
1527 the statement expression itself as the target's init
1528 expr. Finally, return the target expression. */
1529 tree init
, target_expr
= EXPR_STMT_EXPR (result_stmt
);
1530 my_friendly_assert (TREE_CODE (target_expr
) == TARGET_EXPR
, 20030729);
1532 /* The initializer will be void if the initialization is done by
1533 AGGR_INIT_EXPR; propagate that out to the statement-expression as
1535 init
= TREE_OPERAND (target_expr
, 1);
1536 type
= TREE_TYPE (init
);
1538 init
= maybe_cleanup_point_expr (init
);
1539 *result_stmt_p
= init
;
1541 if (VOID_TYPE_P (type
))
1542 /* No frobbing needed. */;
1543 else if (TREE_CODE (result
) == BIND_EXPR
)
1545 /* The BIND_EXPR created in finish_compound_stmt is void; if we're
1546 returning a value directly, give it the appropriate type. */
1547 if (VOID_TYPE_P (TREE_TYPE (result
)))
1548 TREE_TYPE (result
) = type
;
1549 else if (same_type_p (TREE_TYPE (result
), type
))
1554 else if (TREE_CODE (result
) == STATEMENT_LIST
)
1555 /* We need to wrap a STATEMENT_LIST in a BIND_EXPR so it can have a
1556 type other than void. FIXME why can't we just return a value
1557 from STATEMENT_LIST? */
1558 result
= build3 (BIND_EXPR
, type
, NULL
, result
, NULL
);
1560 TREE_OPERAND (target_expr
, 1) = result
;
1561 result
= target_expr
;
1567 /* Perform Koenig lookup. FN is the postfix-expression representing
1568 the function (or functions) to call; ARGS are the arguments to the
1569 call. Returns the functions to be considered by overload
1573 perform_koenig_lookup (tree fn
, tree args
)
1575 tree identifier
= NULL_TREE
;
1576 tree functions
= NULL_TREE
;
1578 /* Find the name of the overloaded function. */
1579 if (TREE_CODE (fn
) == IDENTIFIER_NODE
)
1581 else if (is_overloaded_fn (fn
))
1584 identifier
= DECL_NAME (get_first_fn (functions
));
1586 else if (DECL_P (fn
))
1589 identifier
= DECL_NAME (fn
);
1592 /* A call to a namespace-scope function using an unqualified name.
1594 Do Koenig lookup -- unless any of the arguments are
1596 if (!any_type_dependent_arguments_p (args
))
1598 fn
= lookup_arg_dependent (identifier
, functions
, args
);
1600 /* The unqualified name could not be resolved. */
1601 fn
= unqualified_fn_lookup_error (identifier
);
1609 /* Generate an expression for `FN (ARGS)'.
1611 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1612 as a virtual call, even if FN is virtual. (This flag is set when
1613 encountering an expression where the function name is explicitly
1614 qualified. For example a call to `X::f' never generates a virtual
1617 Returns code for the call. */
1620 finish_call_expr (tree fn
, tree args
, bool disallow_virtual
, bool koenig_p
)
1626 if (fn
== error_mark_node
|| args
== error_mark_node
)
1627 return error_mark_node
;
1629 /* ARGS should be a list of arguments. */
1630 my_friendly_assert (!args
|| TREE_CODE (args
) == TREE_LIST
,
1636 if (processing_template_decl
)
1638 if (type_dependent_expression_p (fn
)
1639 || any_type_dependent_arguments_p (args
))
1641 result
= build_nt (CALL_EXPR
, fn
, args
, NULL_TREE
);
1642 KOENIG_LOOKUP_P (result
) = koenig_p
;
1645 if (!BASELINK_P (fn
)
1646 && TREE_CODE (fn
) != PSEUDO_DTOR_EXPR
1647 && TREE_TYPE (fn
) != unknown_type_node
)
1648 fn
= build_non_dependent_expr (fn
);
1649 args
= build_non_dependent_args (orig_args
);
1652 /* A reference to a member function will appear as an overloaded
1653 function (rather than a BASELINK) if an unqualified name was used
1655 if (!BASELINK_P (fn
) && is_overloaded_fn (fn
))
1659 if (TREE_CODE (f
) == TEMPLATE_ID_EXPR
)
1660 f
= TREE_OPERAND (f
, 0);
1661 f
= get_first_fn (f
);
1662 if (DECL_FUNCTION_MEMBER_P (f
))
1664 tree type
= currently_open_derived_class (DECL_CONTEXT (f
));
1666 type
= DECL_CONTEXT (f
);
1667 fn
= build_baselink (TYPE_BINFO (type
),
1669 fn
, /*optype=*/NULL_TREE
);
1674 if (BASELINK_P (fn
))
1678 /* A call to a member function. From [over.call.func]:
1680 If the keyword this is in scope and refers to the class of
1681 that member function, or a derived class thereof, then the
1682 function call is transformed into a qualified function call
1683 using (*this) as the postfix-expression to the left of the
1684 . operator.... [Otherwise] a contrived object of type T
1685 becomes the implied object argument.
1687 This paragraph is unclear about this situation:
1689 struct A { void f(); };
1690 struct B : public A {};
1691 struct C : public A { void g() { B::f(); }};
1693 In particular, for `B::f', this paragraph does not make clear
1694 whether "the class of that member function" refers to `A' or
1695 to `B'. We believe it refers to `B'. */
1696 if (current_class_type
1697 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn
)),
1699 && current_class_ref
)
1700 object
= maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn
)),
1704 tree representative_fn
;
1706 representative_fn
= BASELINK_FUNCTIONS (fn
);
1707 if (TREE_CODE (representative_fn
) == TEMPLATE_ID_EXPR
)
1708 representative_fn
= TREE_OPERAND (representative_fn
, 0);
1709 representative_fn
= get_first_fn (representative_fn
);
1710 object
= build_dummy_object (DECL_CONTEXT (representative_fn
));
1713 if (processing_template_decl
)
1715 if (type_dependent_expression_p (object
))
1716 return build_nt (CALL_EXPR
, orig_fn
, orig_args
, NULL_TREE
);
1717 object
= build_non_dependent_expr (object
);
1720 result
= build_new_method_call (object
, fn
, args
, NULL_TREE
,
1722 ? LOOKUP_NONVIRTUAL
: 0));
1724 else if (is_overloaded_fn (fn
))
1725 /* A call to a namespace-scope function. */
1726 result
= build_new_function_call (fn
, args
);
1727 else if (TREE_CODE (fn
) == PSEUDO_DTOR_EXPR
)
1730 error ("arguments to destructor are not allowed");
1731 /* Mark the pseudo-destructor call as having side-effects so
1732 that we do not issue warnings about its use. */
1733 result
= build1 (NOP_EXPR
,
1735 TREE_OPERAND (fn
, 0));
1736 TREE_SIDE_EFFECTS (result
) = 1;
1738 else if (CLASS_TYPE_P (TREE_TYPE (fn
)))
1739 /* If the "function" is really an object of class type, it might
1740 have an overloaded `operator ()'. */
1741 result
= build_new_op (CALL_EXPR
, LOOKUP_NORMAL
, fn
, args
, NULL_TREE
,
1742 /*overloaded_p=*/NULL
);
1744 /* A call where the function is unknown. */
1745 result
= build_function_call (fn
, args
);
1747 if (processing_template_decl
)
1749 result
= build (CALL_EXPR
, TREE_TYPE (result
), orig_fn
,
1750 orig_args
, NULL_TREE
);
1751 KOENIG_LOOKUP_P (result
) = koenig_p
;
1756 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1757 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1758 POSTDECREMENT_EXPR.) */
1761 finish_increment_expr (tree expr
, enum tree_code code
)
1763 return build_x_unary_op (code
, expr
);
1766 /* Finish a use of `this'. Returns an expression for `this'. */
1769 finish_this_expr (void)
1773 if (current_class_ptr
)
1775 result
= current_class_ptr
;
1777 else if (current_function_decl
1778 && DECL_STATIC_FUNCTION_P (current_function_decl
))
1780 error ("`this' is unavailable for static member functions");
1781 result
= error_mark_node
;
1785 if (current_function_decl
)
1786 error ("invalid use of `this' in non-member function");
1788 error ("invalid use of `this' at top level");
1789 result
= error_mark_node
;
1795 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1796 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1797 the TYPE for the type given. If SCOPE is non-NULL, the expression
1798 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1801 finish_pseudo_destructor_expr (tree object
, tree scope
, tree destructor
)
1803 if (destructor
== error_mark_node
)
1804 return error_mark_node
;
1806 my_friendly_assert (TYPE_P (destructor
), 20010905);
1808 if (!processing_template_decl
)
1810 if (scope
== error_mark_node
)
1812 error ("invalid qualifying scope in pseudo-destructor name");
1813 return error_mark_node
;
1816 /* [expr.pseudo] says both:
1818 The type designated by the pseudo-destructor-name shall be
1819 the same as the object type.
1823 The cv-unqualified versions of the object type and of the
1824 type designated by the pseudo-destructor-name shall be the
1827 We implement the more generous second sentence, since that is
1828 what most other compilers do. */
1829 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object
),
1832 error ("`%E' is not of type `%T'", object
, destructor
);
1833 return error_mark_node
;
1837 return build (PSEUDO_DTOR_EXPR
, void_type_node
, object
, scope
, destructor
);
1840 /* Finish an expression of the form CODE EXPR. */
1843 finish_unary_op_expr (enum tree_code code
, tree expr
)
1845 tree result
= build_x_unary_op (code
, expr
);
1846 /* Inside a template, build_x_unary_op does not fold the
1847 expression. So check whether the result is folded before
1848 setting TREE_NEGATED_INT. */
1849 if (code
== NEGATE_EXPR
&& TREE_CODE (expr
) == INTEGER_CST
1850 && TREE_CODE (result
) == INTEGER_CST
1851 && !TYPE_UNSIGNED (TREE_TYPE (result
))
1852 && INT_CST_LT (result
, integer_zero_node
))
1853 TREE_NEGATED_INT (result
) = 1;
1854 overflow_warning (result
);
1858 /* Finish a compound-literal expression. TYPE is the type to which
1859 the INITIALIZER_LIST is being cast. */
1862 finish_compound_literal (tree type
, tree initializer_list
)
1864 tree compound_literal
;
1866 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1867 compound_literal
= build_constructor (NULL_TREE
, initializer_list
);
1868 /* Mark it as a compound-literal. */
1869 TREE_HAS_CONSTRUCTOR (compound_literal
) = 1;
1870 if (processing_template_decl
)
1871 TREE_TYPE (compound_literal
) = type
;
1874 /* Check the initialization. */
1875 compound_literal
= digest_init (type
, compound_literal
, NULL
);
1876 /* If the TYPE was an array type with an unknown bound, then we can
1877 figure out the dimension now. For example, something like:
1881 implies that the array has two elements. */
1882 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
1883 complete_array_type (type
, compound_literal
, 1);
1886 return compound_literal
;
1889 /* Return the declaration for the function-name variable indicated by
1893 finish_fname (tree id
)
1897 decl
= fname_decl (C_RID_CODE (id
), id
);
1898 if (processing_template_decl
)
1899 decl
= DECL_NAME (decl
);
1903 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1904 and DECLARATOR. Returns nonzero if the function-declaration is
1908 begin_function_definition (tree decl_specs
, tree attributes
, tree declarator
)
1910 if (!start_function (decl_specs
, declarator
, attributes
, SF_DEFAULT
))
1913 /* The things we're about to see are not directly qualified by any
1914 template headers we've seen thus far. */
1915 reset_specialization ();
1920 /* Finish a translation unit. */
1923 finish_translation_unit (void)
1925 /* In case there were missing closebraces,
1926 get us back to the global binding level. */
1928 while (current_namespace
!= global_namespace
)
1931 /* Do file scope __FUNCTION__ et al. */
1932 finish_fname_decls ();
1935 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1936 Returns the parameter. */
1939 finish_template_type_parm (tree aggr
, tree identifier
)
1941 if (aggr
!= class_type_node
)
1943 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1944 aggr
= class_type_node
;
1947 return build_tree_list (aggr
, identifier
);
1950 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1951 Returns the parameter. */
1954 finish_template_template_parm (tree aggr
, tree identifier
)
1956 tree decl
= build_decl (TYPE_DECL
, identifier
, NULL_TREE
);
1957 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, identifier
, NULL_TREE
);
1958 DECL_TEMPLATE_PARMS (tmpl
) = current_template_parms
;
1959 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
1960 DECL_ARTIFICIAL (decl
) = 1;
1961 end_template_decl ();
1963 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl
), 20010110);
1965 return finish_template_type_parm (aggr
, tmpl
);
1968 /* ARGUMENT is the default-argument value for a template template
1969 parameter. If ARGUMENT is invalid, issue error messages and return
1970 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1973 check_template_template_default_arg (tree argument
)
1975 if (TREE_CODE (argument
) != TEMPLATE_DECL
1976 && TREE_CODE (argument
) != TEMPLATE_TEMPLATE_PARM
1977 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
1979 if (TREE_CODE (argument
) == TYPE_DECL
)
1981 tree t
= TREE_TYPE (argument
);
1983 /* Try to emit a slightly smarter error message if we detect
1984 that the user is using a template instantiation. */
1985 if (CLASSTYPE_TEMPLATE_INFO (t
)
1986 && CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
1987 error ("invalid use of type `%T' as a default value for a "
1988 "template template-parameter", t
);
1990 error ("invalid use of `%D' as a default value for a template "
1991 "template-parameter", argument
);
1994 error ("invalid default argument for a template template parameter");
1995 return error_mark_node
;
2001 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
2002 nonzero, the parameter list was terminated by a `...'. */
2005 finish_parmlist (tree parms
, int ellipsis
)
2009 /* We mark the PARMS as a parmlist so that declarator processing can
2010 disambiguate certain constructs. */
2011 TREE_PARMLIST (parms
) = 1;
2012 /* We do not append void_list_node here, but leave it to grokparms
2014 PARMLIST_ELLIPSIS_P (parms
) = ellipsis
;
2019 /* Begin a class definition, as indicated by T. */
2022 begin_class_definition (tree t
)
2024 if (t
== error_mark_node
)
2025 return error_mark_node
;
2027 if (processing_template_parmlist
)
2029 error ("definition of `%#T' inside template parameter list", t
);
2030 return error_mark_node
;
2032 /* A non-implicit typename comes from code like:
2034 template <typename T> struct A {
2035 template <typename U> struct A<T>::B ...
2037 This is erroneous. */
2038 else if (TREE_CODE (t
) == TYPENAME_TYPE
)
2040 error ("invalid definition of qualified type `%T'", t
);
2041 t
= error_mark_node
;
2044 if (t
== error_mark_node
|| ! IS_AGGR_TYPE (t
))
2046 t
= make_aggr_type (RECORD_TYPE
);
2047 pushtag (make_anon_name (), t
, 0);
2050 /* If this type was already complete, and we see another definition,
2052 if (COMPLETE_TYPE_P (t
))
2054 error ("redefinition of `%#T'", t
);
2055 cp_error_at ("previous definition of `%#T'", t
);
2056 return error_mark_node
;
2059 /* Update the location of the decl. */
2060 DECL_SOURCE_LOCATION (TYPE_NAME (t
)) = input_location
;
2062 if (TYPE_BEING_DEFINED (t
))
2064 t
= make_aggr_type (TREE_CODE (t
));
2065 pushtag (TYPE_IDENTIFIER (t
), t
, 0);
2067 maybe_process_partial_specialization (t
);
2069 TYPE_BEING_DEFINED (t
) = 1;
2070 if (flag_pack_struct
)
2073 TYPE_PACKED (t
) = 1;
2074 /* Even though the type is being defined for the first time
2075 here, there might have been a forward declaration, so there
2076 might be cv-qualified variants of T. */
2077 for (v
= TYPE_NEXT_VARIANT (t
); v
; v
= TYPE_NEXT_VARIANT (v
))
2078 TYPE_PACKED (v
) = 1;
2080 /* Reset the interface data, at the earliest possible
2081 moment, as it might have been set via a class foo;
2083 if (! TYPE_ANONYMOUS_P (t
))
2085 CLASSTYPE_INTERFACE_ONLY (t
) = interface_only
;
2086 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2087 (t
, interface_unknown
);
2089 reset_specialization();
2091 /* Make a declaration for this class in its own scope. */
2092 build_self_reference ();
2097 /* Finish the member declaration given by DECL. */
2100 finish_member_declaration (tree decl
)
2102 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
2105 if (decl
== void_type_node
)
2106 /* The COMPONENT was a friend, not a member, and so there's
2107 nothing for us to do. */
2110 /* We should see only one DECL at a time. */
2111 my_friendly_assert (TREE_CHAIN (decl
) == NULL_TREE
, 0);
2113 /* Set up access control for DECL. */
2115 = (current_access_specifier
== access_private_node
);
2116 TREE_PROTECTED (decl
)
2117 = (current_access_specifier
== access_protected_node
);
2118 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2120 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl
)) = TREE_PRIVATE (decl
);
2121 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl
)) = TREE_PROTECTED (decl
);
2124 /* Mark the DECL as a member of the current class. */
2125 DECL_CONTEXT (decl
) = current_class_type
;
2129 A C language linkage is ignored for the names of class members
2130 and the member function type of class member functions. */
2131 if (DECL_LANG_SPECIFIC (decl
) && DECL_LANGUAGE (decl
) == lang_c
)
2132 SET_DECL_LANGUAGE (decl
, lang_cplusplus
);
2134 /* Put functions on the TYPE_METHODS list and everything else on the
2135 TYPE_FIELDS list. Note that these are built up in reverse order.
2136 We reverse them (to obtain declaration order) in finish_struct. */
2137 if (TREE_CODE (decl
) == FUNCTION_DECL
2138 || DECL_FUNCTION_TEMPLATE_P (decl
))
2140 /* We also need to add this function to the
2141 CLASSTYPE_METHOD_VEC. */
2142 add_method (current_class_type
, decl
, /*error_p=*/0);
2144 TREE_CHAIN (decl
) = TYPE_METHODS (current_class_type
);
2145 TYPE_METHODS (current_class_type
) = decl
;
2147 maybe_add_class_template_decl_list (current_class_type
, decl
,
2150 /* Enter the DECL into the scope of the class. */
2151 else if ((TREE_CODE (decl
) == USING_DECL
&& TREE_TYPE (decl
))
2152 || pushdecl_class_level (decl
))
2154 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2155 go at the beginning. The reason is that lookup_field_1
2156 searches the list in order, and we want a field name to
2157 override a type name so that the "struct stat hack" will
2158 work. In particular:
2160 struct S { enum E { }; int E } s;
2163 is valid. In addition, the FIELD_DECLs must be maintained in
2164 declaration order so that class layout works as expected.
2165 However, we don't need that order until class layout, so we
2166 save a little time by putting FIELD_DECLs on in reverse order
2167 here, and then reversing them in finish_struct_1. (We could
2168 also keep a pointer to the correct insertion points in the
2171 if (TREE_CODE (decl
) == TYPE_DECL
)
2172 TYPE_FIELDS (current_class_type
)
2173 = chainon (TYPE_FIELDS (current_class_type
), decl
);
2176 TREE_CHAIN (decl
) = TYPE_FIELDS (current_class_type
);
2177 TYPE_FIELDS (current_class_type
) = decl
;
2180 maybe_add_class_template_decl_list (current_class_type
, decl
,
2185 /* Finish processing the declaration of a member class template
2186 TYPES whose template parameters are given by PARMS. */
2189 finish_member_class_template (tree types
)
2193 /* If there are declared, but undefined, partial specializations
2194 mixed in with the typespecs they will not yet have passed through
2195 maybe_process_partial_specialization, so we do that here. */
2196 for (t
= types
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
2197 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t
))))
2198 maybe_process_partial_specialization (TREE_VALUE (t
));
2200 grok_x_components (types
);
2201 if (TYPE_CONTEXT (TREE_VALUE (types
)) != current_class_type
)
2202 /* The component was in fact a friend declaration. We avoid
2203 finish_member_template_decl performing certain checks by
2207 finish_member_template_decl (types
);
2209 /* As with other component type declarations, we do
2210 not store the new DECL on the list of
2215 /* Finish processing a complete template declaration. The PARMS are
2216 the template parameters. */
2219 finish_template_decl (tree parms
)
2222 end_template_decl ();
2224 end_specialization ();
2227 /* Finish processing a template-id (which names a type) of the form
2228 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2229 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2230 the scope of template-id indicated. */
2233 finish_template_type (tree name
, tree args
, int entering_scope
)
2237 decl
= lookup_template_class (name
, args
,
2238 NULL_TREE
, NULL_TREE
, entering_scope
,
2239 tf_error
| tf_warning
| tf_user
);
2240 if (decl
!= error_mark_node
)
2241 decl
= TYPE_STUB_DECL (decl
);
2246 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2247 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2248 BASE_CLASS, or NULL_TREE if an error occurred. The
2249 ACCESS_SPECIFIER is one of
2250 access_{default,public,protected_private}[_virtual]_node.*/
2253 finish_base_specifier (tree base
, tree access
, bool virtual_p
)
2257 if (base
== error_mark_node
)
2259 error ("invalid base-class specification");
2262 else if (! is_aggr_type (base
, 1))
2266 if (cp_type_quals (base
) != 0)
2268 error ("base class `%T' has cv qualifiers", base
);
2269 base
= TYPE_MAIN_VARIANT (base
);
2271 result
= build_tree_list (access
, base
);
2272 TREE_VIA_VIRTUAL (result
) = virtual_p
;
2278 /* Called when multiple declarators are processed. If that is not
2279 permitted in this context, an error is issued. */
2282 check_multiple_declarators (void)
2286 In a template-declaration, explicit specialization, or explicit
2287 instantiation the init-declarator-list in the declaration shall
2288 contain at most one declarator.
2290 We don't just use PROCESSING_TEMPLATE_DECL for the first
2291 condition since that would disallow the perfectly valid code,
2292 like `template <class T> struct S { int i, j; };'. */
2293 if (at_function_scope_p ())
2294 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2297 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2298 || processing_explicit_instantiation
2299 || processing_specialization
)
2300 error ("multiple declarators in template declaration");
2303 /* Issue a diagnostic that NAME cannot be found in SCOPE. */
2306 qualified_name_lookup_error (tree scope
, tree name
)
2310 if (!COMPLETE_TYPE_P (scope
))
2311 error ("incomplete type `%T' used in nested name specifier", scope
);
2313 error ("`%D' is not a member of `%T'", name
, scope
);
2315 else if (scope
!= global_namespace
)
2316 error ("`%D' is not a member of `%D'", name
, scope
);
2318 error ("`::%D' has not been declared", name
);
2321 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2322 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2323 if non-NULL, is the type or namespace used to explicitly qualify
2324 ID_EXPRESSION. DECL is the entity to which that name has been
2327 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2328 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2329 be set to true if this expression isn't permitted in a
2330 constant-expression, but it is otherwise not set by this function.
2331 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2332 constant-expression, but a non-constant expression is also
2335 If an error occurs, and it is the kind of error that might cause
2336 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2337 is the caller's responsibility to issue the message. *ERROR_MSG
2338 will be a string with static storage duration, so the caller need
2341 Return an expression for the entity, after issuing appropriate
2342 diagnostics. This function is also responsible for transforming a
2343 reference to a non-static member into a COMPONENT_REF that makes
2344 the use of "this" explicit.
2346 Upon return, *IDK will be filled in appropriately. */
2349 finish_id_expression (tree id_expression
,
2353 tree
*qualifying_class
,
2354 bool integral_constant_expression_p
,
2355 bool allow_non_integral_constant_expression_p
,
2356 bool *non_integral_constant_expression_p
,
2357 const char **error_msg
)
2359 /* Initialize the output parameters. */
2360 *idk
= CP_ID_KIND_NONE
;
2363 if (id_expression
== error_mark_node
)
2364 return error_mark_node
;
2365 /* If we have a template-id, then no further lookup is
2366 required. If the template-id was for a template-class, we
2367 will sometimes have a TYPE_DECL at this point. */
2368 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
2369 || TREE_CODE (decl
) == TYPE_DECL
)
2371 /* Look up the name. */
2374 if (decl
== error_mark_node
)
2376 /* Name lookup failed. */
2379 || (!dependent_type_p (scope
)
2380 && !(TREE_CODE (id_expression
) == IDENTIFIER_NODE
2381 && IDENTIFIER_TYPENAME_P (id_expression
)
2382 && dependent_type_p (TREE_TYPE (id_expression
))))))
2384 /* If the qualifying type is non-dependent (and the name
2385 does not name a conversion operator to a dependent
2386 type), issue an error. */
2387 qualified_name_lookup_error (scope
, id_expression
);
2388 return error_mark_node
;
2392 /* It may be resolved via Koenig lookup. */
2393 *idk
= CP_ID_KIND_UNQUALIFIED
;
2394 return id_expression
;
2397 decl
= id_expression
;
2399 /* If DECL is a variable that would be out of scope under
2400 ANSI/ISO rules, but in scope in the ARM, name lookup
2401 will succeed. Issue a diagnostic here. */
2403 decl
= check_for_out_of_scope_variable (decl
);
2405 /* Remember that the name was used in the definition of
2406 the current class so that we can check later to see if
2407 the meaning would have been different after the class
2408 was entirely defined. */
2409 if (!scope
&& decl
!= error_mark_node
)
2410 maybe_note_name_used_in_class (id_expression
, decl
);
2413 /* If we didn't find anything, or what we found was a type,
2414 then this wasn't really an id-expression. */
2415 if (TREE_CODE (decl
) == TEMPLATE_DECL
2416 && !DECL_FUNCTION_TEMPLATE_P (decl
))
2418 *error_msg
= "missing template arguments";
2419 return error_mark_node
;
2421 else if (TREE_CODE (decl
) == TYPE_DECL
2422 || TREE_CODE (decl
) == NAMESPACE_DECL
)
2424 *error_msg
= "expected primary-expression";
2425 return error_mark_node
;
2428 /* If the name resolved to a template parameter, there is no
2429 need to look it up again later. */
2430 if ((TREE_CODE (decl
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (decl
))
2431 || TREE_CODE (decl
) == TEMPLATE_PARM_INDEX
)
2433 *idk
= CP_ID_KIND_NONE
;
2434 if (TREE_CODE (decl
) == TEMPLATE_PARM_INDEX
)
2435 decl
= TEMPLATE_PARM_DECL (decl
);
2436 if (integral_constant_expression_p
2437 && !dependent_type_p (TREE_TYPE (decl
))
2438 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl
)))
2440 if (!allow_non_integral_constant_expression_p
)
2441 error ("template parameter `%D' of type `%T' is not allowed in "
2442 "an integral constant expression because it is not of "
2443 "integral or enumeration type", decl
, TREE_TYPE (decl
));
2444 *non_integral_constant_expression_p
= true;
2446 return DECL_INITIAL (decl
);
2448 /* Similarly, we resolve enumeration constants to their
2449 underlying values. */
2450 else if (TREE_CODE (decl
) == CONST_DECL
)
2452 *idk
= CP_ID_KIND_NONE
;
2453 if (!processing_template_decl
)
2454 return DECL_INITIAL (decl
);
2461 /* If the declaration was explicitly qualified indicate
2462 that. The semantics of `A::f(3)' are different than
2463 `f(3)' if `f' is virtual. */
2465 ? CP_ID_KIND_QUALIFIED
2466 : (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
2467 ? CP_ID_KIND_TEMPLATE_ID
2468 : CP_ID_KIND_UNQUALIFIED
));
2473 An id-expression is type-dependent if it contains an
2474 identifier that was declared with a dependent type.
2476 The standard is not very specific about an id-expression that
2477 names a set of overloaded functions. What if some of them
2478 have dependent types and some of them do not? Presumably,
2479 such a name should be treated as a dependent name. */
2480 /* Assume the name is not dependent. */
2481 dependent_p
= false;
2482 if (!processing_template_decl
)
2483 /* No names are dependent outside a template. */
2485 /* A template-id where the name of the template was not resolved
2486 is definitely dependent. */
2487 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
2488 && (TREE_CODE (TREE_OPERAND (decl
, 0))
2489 == IDENTIFIER_NODE
))
2491 /* For anything except an overloaded function, just check its
2493 else if (!is_overloaded_fn (decl
))
2495 = dependent_type_p (TREE_TYPE (decl
));
2496 /* For a set of overloaded functions, check each of the
2502 if (BASELINK_P (fns
))
2503 fns
= BASELINK_FUNCTIONS (fns
);
2505 /* For a template-id, check to see if the template
2506 arguments are dependent. */
2507 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
2509 tree args
= TREE_OPERAND (fns
, 1);
2510 dependent_p
= any_dependent_template_arguments_p (args
);
2511 /* The functions are those referred to by the
2513 fns
= TREE_OPERAND (fns
, 0);
2516 /* If there are no dependent template arguments, go through
2517 the overloaded functions. */
2518 while (fns
&& !dependent_p
)
2520 tree fn
= OVL_CURRENT (fns
);
2522 /* Member functions of dependent classes are
2524 if (TREE_CODE (fn
) == FUNCTION_DECL
2525 && type_dependent_expression_p (fn
))
2527 else if (TREE_CODE (fn
) == TEMPLATE_DECL
2528 && dependent_template_p (fn
))
2531 fns
= OVL_NEXT (fns
);
2535 /* If the name was dependent on a template parameter, we will
2536 resolve the name at instantiation time. */
2539 /* Create a SCOPE_REF for qualified names, if the scope is
2544 *qualifying_class
= scope
;
2545 /* Since this name was dependent, the expression isn't
2546 constant -- yet. No error is issued because it might
2547 be constant when things are instantiated. */
2548 if (integral_constant_expression_p
)
2549 *non_integral_constant_expression_p
= true;
2550 if (TYPE_P (scope
) && dependent_type_p (scope
))
2551 return build_nt (SCOPE_REF
, scope
, id_expression
);
2552 else if (TYPE_P (scope
) && DECL_P (decl
))
2553 return build (SCOPE_REF
, TREE_TYPE (decl
), scope
,
2558 /* A TEMPLATE_ID already contains all the information we
2560 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
)
2561 return id_expression
;
2562 /* Since this name was dependent, the expression isn't
2563 constant -- yet. No error is issued because it might be
2564 constant when things are instantiated. */
2565 if (integral_constant_expression_p
)
2566 *non_integral_constant_expression_p
= true;
2567 *idk
= CP_ID_KIND_UNQUALIFIED_DEPENDENT
;
2568 /* If we found a variable, then name lookup during the
2569 instantiation will always resolve to the same VAR_DECL
2570 (or an instantiation thereof). */
2571 if (TREE_CODE (decl
) == VAR_DECL
2572 || TREE_CODE (decl
) == PARM_DECL
)
2574 return id_expression
;
2577 /* Only certain kinds of names are allowed in constant
2578 expression. Enumerators and template parameters
2579 have already been handled above. */
2580 if (integral_constant_expression_p
)
2582 /* Const variables or static data members of integral or
2583 enumeration types initialized with constant expressions
2585 if (TREE_CODE (decl
) == VAR_DECL
2586 && CP_TYPE_CONST_P (TREE_TYPE (decl
))
2587 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl
))
2588 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
2592 if (!allow_non_integral_constant_expression_p
)
2594 error ("`%D' cannot appear in a constant-expression", decl
);
2595 return error_mark_node
;
2597 *non_integral_constant_expression_p
= true;
2601 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2603 error ("use of namespace `%D' as expression", decl
);
2604 return error_mark_node
;
2606 else if (DECL_CLASS_TEMPLATE_P (decl
))
2608 error ("use of class template `%T' as expression", decl
);
2609 return error_mark_node
;
2611 else if (TREE_CODE (decl
) == TREE_LIST
)
2613 /* Ambiguous reference to base members. */
2614 error ("request for member `%D' is ambiguous in "
2615 "multiple inheritance lattice", id_expression
);
2616 print_candidates (decl
);
2617 return error_mark_node
;
2620 /* Mark variable-like entities as used. Functions are similarly
2621 marked either below or after overload resolution. */
2622 if (TREE_CODE (decl
) == VAR_DECL
2623 || TREE_CODE (decl
) == PARM_DECL
2624 || TREE_CODE (decl
) == RESULT_DECL
)
2629 decl
= (adjust_result_of_qualified_name_lookup
2630 (decl
, scope
, current_class_type
));
2632 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2635 if (TREE_CODE (decl
) == FIELD_DECL
|| BASELINK_P (decl
))
2636 *qualifying_class
= scope
;
2637 else if (!processing_template_decl
)
2638 decl
= convert_from_reference (decl
);
2639 else if (TYPE_P (scope
))
2640 decl
= build (SCOPE_REF
, TREE_TYPE (decl
), scope
, decl
);
2642 else if (TREE_CODE (decl
) == FIELD_DECL
)
2643 decl
= finish_non_static_data_member (decl
, current_class_ref
,
2644 /*qualifying_scope=*/NULL_TREE
);
2645 else if (is_overloaded_fn (decl
))
2647 tree first_fn
= OVL_CURRENT (decl
);
2649 if (TREE_CODE (first_fn
) == TEMPLATE_DECL
)
2650 first_fn
= DECL_TEMPLATE_RESULT (first_fn
);
2652 if (!really_overloaded_fn (decl
))
2653 mark_used (first_fn
);
2655 if (TREE_CODE (first_fn
) == FUNCTION_DECL
2656 && DECL_FUNCTION_MEMBER_P (first_fn
))
2658 /* A set of member functions. */
2659 decl
= maybe_dummy_object (DECL_CONTEXT (first_fn
), 0);
2660 return finish_class_member_access_expr (decl
, id_expression
);
2665 if (TREE_CODE (decl
) == VAR_DECL
2666 || TREE_CODE (decl
) == PARM_DECL
2667 || TREE_CODE (decl
) == RESULT_DECL
)
2669 tree context
= decl_function_context (decl
);
2671 if (context
!= NULL_TREE
&& context
!= current_function_decl
2672 && ! TREE_STATIC (decl
))
2674 error ("use of %s from containing function",
2675 (TREE_CODE (decl
) == VAR_DECL
2676 ? "`auto' variable" : "parameter"));
2677 cp_error_at (" `%#D' declared here", decl
);
2678 return error_mark_node
;
2682 if (DECL_P (decl
) && DECL_NONLOCAL (decl
)
2683 && DECL_CLASS_SCOPE_P (decl
)
2684 && DECL_CONTEXT (decl
) != current_class_type
)
2688 path
= currently_open_derived_class (DECL_CONTEXT (decl
));
2689 perform_or_defer_access_check (TYPE_BINFO (path
), decl
);
2692 if (! processing_template_decl
)
2693 decl
= convert_from_reference (decl
);
2696 /* Resolve references to variables of anonymous unions
2697 into COMPONENT_REFs. */
2698 if (TREE_CODE (decl
) == ALIAS_DECL
)
2699 decl
= unshare_expr (DECL_INITIAL (decl
));
2702 if (TREE_DEPRECATED (decl
))
2703 warn_deprecated_use (decl
);
2708 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2709 use as a type-specifier. */
2712 finish_typeof (tree expr
)
2716 if (type_dependent_expression_p (expr
))
2718 type
= make_aggr_type (TYPEOF_TYPE
);
2719 TYPEOF_TYPE_EXPR (type
) = expr
;
2724 type
= TREE_TYPE (expr
);
2726 if (!type
|| type
== unknown_type_node
)
2728 error ("type of `%E' is unknown", expr
);
2729 return error_mark_node
;
2735 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2736 with equivalent CALL_EXPRs. */
2739 simplify_aggr_init_exprs_r (tree
* tp
,
2741 void* data ATTRIBUTE_UNUSED
)
2743 /* We don't need to walk into types; there's nothing in a type that
2744 needs simplification. (And, furthermore, there are places we
2745 actively don't want to go. For example, we don't want to wander
2746 into the default arguments for a FUNCTION_DECL that appears in a
2753 /* Only AGGR_INIT_EXPRs are interesting. */
2754 else if (TREE_CODE (*tp
) != AGGR_INIT_EXPR
)
2757 simplify_aggr_init_expr (tp
);
2759 /* Keep iterating. */
2763 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
2764 function is broken out from the above for the benefit of the tree-ssa
2768 simplify_aggr_init_expr (tree
*tp
)
2770 tree aggr_init_expr
= *tp
;
2772 /* Form an appropriate CALL_EXPR. */
2773 tree fn
= TREE_OPERAND (aggr_init_expr
, 0);
2774 tree args
= TREE_OPERAND (aggr_init_expr
, 1);
2775 tree slot
= TREE_OPERAND (aggr_init_expr
, 2);
2776 tree type
= TREE_TYPE (slot
);
2779 enum style_t
{ ctor
, arg
, pcc
} style
;
2781 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr
))
2783 #ifdef PCC_STATIC_STRUCT_RETURN
2787 else if (TREE_ADDRESSABLE (type
))
2790 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2791 handling. See build_cplus_new. */
2794 if (style
== ctor
|| style
== arg
)
2796 /* Pass the address of the slot. If this is a constructor, we
2797 replace the first argument; otherwise, we tack on a new one. */
2801 args
= TREE_CHAIN (args
);
2803 cxx_mark_addressable (slot
);
2804 addr
= build1 (ADDR_EXPR
, build_pointer_type (type
), slot
);
2807 /* The return type might have different cv-quals from the slot. */
2808 tree fntype
= TREE_TYPE (TREE_TYPE (fn
));
2809 #ifdef ENABLE_CHECKING
2810 if (TREE_CODE (fntype
) != FUNCTION_TYPE
2811 && TREE_CODE (fntype
) != METHOD_TYPE
)
2814 addr
= convert (build_pointer_type (TREE_TYPE (fntype
)), addr
);
2817 args
= tree_cons (NULL_TREE
, addr
, args
);
2820 call_expr
= build (CALL_EXPR
,
2821 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))),
2822 fn
, args
, NULL_TREE
);
2825 /* Tell the backend that we've added our return slot to the argument
2827 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr
) = 1;
2828 else if (style
== pcc
)
2830 /* If we're using the non-reentrant PCC calling convention, then we
2831 need to copy the returned value out of the static buffer into the
2833 push_deferring_access_checks (dk_no_check
);
2834 call_expr
= build_aggr_init (slot
, call_expr
,
2835 DIRECT_BIND
| LOOKUP_ONLYCONVERTING
);
2836 pop_deferring_access_checks ();
2842 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2845 emit_associated_thunks (tree fn
)
2847 /* When we use vcall offsets, we emit thunks with the virtual
2848 functions to which they thunk. The whole point of vcall offsets
2849 is so that you can know statically the entire set of thunks that
2850 will ever be needed for a given virtual function, thereby
2851 enabling you to output all the thunks with the function itself. */
2852 if (DECL_VIRTUAL_P (fn
))
2856 for (thunk
= DECL_THUNKS (fn
); thunk
; thunk
= TREE_CHAIN (thunk
))
2858 if (!THUNK_ALIAS (thunk
))
2860 use_thunk (thunk
, /*emit_p=*/1);
2861 if (DECL_RESULT_THUNK_P (thunk
))
2865 for (probe
= DECL_THUNKS (thunk
);
2866 probe
; probe
= TREE_CHAIN (probe
))
2867 use_thunk (probe
, /*emit_p=*/1);
2871 my_friendly_assert (!DECL_THUNKS (thunk
), 20031023);
2876 /* Generate RTL for FN. */
2879 expand_body (tree fn
)
2881 tree saved_function
;
2883 /* Compute the appropriate object-file linkage for inline
2885 if (DECL_DECLARED_INLINE_P (fn
))
2886 import_export_decl (fn
);
2888 /* If FN is external, then there's no point in generating RTL for
2889 it. This situation can arise with an inline function under
2890 `-fexternal-templates'; we instantiate the function, even though
2891 we're not planning on emitting it, in case we get a chance to
2893 if (DECL_EXTERNAL (fn
))
2896 /* ??? When is this needed? */
2897 saved_function
= current_function_decl
;
2899 /* Emit any thunks that should be emitted at the same time as FN. */
2900 emit_associated_thunks (fn
);
2902 tree_rest_of_compilation (fn
, function_depth
> 1);
2904 current_function_decl
= saved_function
;
2906 extract_interface_info ();
2908 /* If this function is marked with the constructor attribute, add it
2909 to the list of functions to be called along with constructors
2910 from static duration objects. */
2911 if (DECL_STATIC_CONSTRUCTOR (fn
))
2912 static_ctors
= tree_cons (NULL_TREE
, fn
, static_ctors
);
2914 /* If this function is marked with the destructor attribute, add it
2915 to the list of functions to be called along with destructors from
2916 static duration objects. */
2917 if (DECL_STATIC_DESTRUCTOR (fn
))
2918 static_dtors
= tree_cons (NULL_TREE
, fn
, static_dtors
);
2920 if (DECL_CLONED_FUNCTION_P (fn
))
2922 /* If this is a clone, go through the other clones now and mark
2923 their parameters used. We have to do that here, as we don't
2924 know whether any particular clone will be expanded, and
2925 therefore cannot pick one arbitrarily. */
2928 for (probe
= TREE_CHAIN (DECL_CLONED_FUNCTION (fn
));
2929 probe
&& DECL_CLONED_FUNCTION_P (probe
);
2930 probe
= TREE_CHAIN (probe
))
2934 for (parms
= DECL_ARGUMENTS (probe
);
2935 parms
; parms
= TREE_CHAIN (parms
))
2936 TREE_USED (parms
) = 1;
2941 /* Generate RTL for FN. */
2944 expand_or_defer_fn (tree fn
)
2946 /* When the parser calls us after finishing the body of a template
2947 function, we don't really want to expand the body. */
2948 if (processing_template_decl
)
2950 /* Normally, collection only occurs in rest_of_compilation. So,
2951 if we don't collect here, we never collect junk generated
2952 during the processing of templates until we hit a
2953 non-template function. */
2958 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2959 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn
),
2960 simplify_aggr_init_exprs_r
,
2963 /* If this is a constructor or destructor body, we have to clone
2965 if (maybe_clone_body (fn
))
2967 /* We don't want to process FN again, so pretend we've written
2968 it out, even though we haven't. */
2969 TREE_ASM_WRITTEN (fn
) = 1;
2973 /* There's no reason to do any of the work here if we're only doing
2974 semantic analysis; this code just generates RTL. */
2975 if (flag_syntax_only
)
2978 /* Compute the appropriate object-file linkage for inline functions. */
2979 if (DECL_DECLARED_INLINE_P (fn
))
2980 import_export_decl (fn
);
2984 /* Expand or defer, at the whim of the compilation unit manager. */
2985 cgraph_finalize_function (fn
, function_depth
> 1);
2997 /* Helper function for walk_tree, used by finalize_nrv below. */
3000 finalize_nrv_r (tree
* tp
, int* walk_subtrees
, void* data
)
3002 struct nrv_data
*dp
= (struct nrv_data
*)data
;
3005 /* No need to walk into types. There wouldn't be any need to walk into
3006 non-statements, except that we have to consider STMT_EXPRs. */
3009 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
3010 but differs from using NULL_TREE in that it indicates that we care
3011 about the value of the RESULT_DECL. */
3012 else if (TREE_CODE (*tp
) == RETURN_STMT
)
3013 RETURN_STMT_EXPR (*tp
) = dp
->result
;
3014 /* Change all cleanups for the NRV to only run when an exception is
3016 else if (TREE_CODE (*tp
) == CLEANUP_STMT
3017 && CLEANUP_DECL (*tp
) == dp
->var
)
3018 CLEANUP_EH_ONLY (*tp
) = 1;
3019 /* Replace the DECL_STMT for the NRV with an initialization of the
3020 RESULT_DECL, if needed. */
3021 else if (TREE_CODE (*tp
) == DECL_STMT
3022 && DECL_STMT_DECL (*tp
) == dp
->var
)
3025 if (DECL_INITIAL (dp
->var
)
3026 && DECL_INITIAL (dp
->var
) != error_mark_node
)
3028 init
= build (INIT_EXPR
, void_type_node
, dp
->result
,
3029 DECL_INITIAL (dp
->var
));
3030 DECL_INITIAL (dp
->var
) = error_mark_node
;
3033 init
= build_empty_stmt ();
3034 SET_EXPR_LOCUS (init
, EXPR_LOCUS (*tp
));
3037 /* And replace all uses of the NRV with the RESULT_DECL. */
3038 else if (*tp
== dp
->var
)
3041 /* Avoid walking into the same tree more than once. Unfortunately, we
3042 can't just use walk_tree_without duplicates because it would only call
3043 us for the first occurrence of dp->var in the function body. */
3044 slot
= htab_find_slot (dp
->visited
, *tp
, INSERT
);
3050 /* Keep iterating. */
3054 /* Called from finish_function to implement the named return value
3055 optimization by overriding all the RETURN_STMTs and pertinent
3056 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3057 RESULT_DECL for the function. */
3060 finalize_nrv (tree
*tp
, tree var
, tree result
)
3062 struct nrv_data data
;
3064 /* Copy debugging information from VAR to RESULT. */
3065 DECL_NAME (result
) = DECL_NAME (var
);
3066 DECL_SOURCE_LOCATION (result
) = DECL_SOURCE_LOCATION (var
);
3067 DECL_ABSTRACT_ORIGIN (result
) = DECL_ABSTRACT_ORIGIN (var
);
3068 /* Don't forget that we take its address. */
3069 TREE_ADDRESSABLE (result
) = TREE_ADDRESSABLE (var
);
3072 data
.result
= result
;
3073 data
.visited
= htab_create (37, htab_hash_pointer
, htab_eq_pointer
, NULL
);
3074 walk_tree (tp
, finalize_nrv_r
, &data
, 0);
3075 htab_delete (data
.visited
);
3078 /* Perform initialization related to this module. */
3081 init_cp_semantics (void)
3085 #include "gt-cp-semantics.h"