2005-01-13 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / gcc / cp / semantics.c
blob74a513a9cd672b32b08c6836840b0c26f4308112
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)
16 any later version.
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
26 02111-1307, USA. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "tree-inline.h"
35 #include "tree-mudflap.h"
36 #include "except.h"
37 #include "lex.h"
38 #include "toplev.h"
39 #include "flags.h"
40 #include "rtl.h"
41 #include "expr.h"
42 #include "output.h"
43 #include "timevar.h"
44 #include "debug.h"
45 #include "diagnostic.h"
46 #include "cgraph.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
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
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
89 instantiations.
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
105 to check access.
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. */
122 void
123 push_deferring_access_checks (deferring_kind deferring)
125 deferred_access *d;
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;
139 else
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
149 this previously. */
151 void
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. */
160 void
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
168 previous states. */
170 void
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.
189 tree
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. */
199 void
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
209 processing below. */
210 deferred_access_stack = d2;
211 d2->next = d1;
212 d1->next = d3;
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;
220 d1->next = d2;
221 d2->next = d3;
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.
230 For example:
232 class A {
233 typedef int X;
234 static X a;
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',
239 next with `x'. */
241 void
242 perform_deferred_access_checks (void)
244 tree deferred_check;
245 for (deferred_check = deferred_access_stack->deferred_access_checks;
246 deferred_check;
247 deferred_check = TREE_CHAIN (deferred_check))
248 /* Check access. */
249 enforce_access (TREE_PURPOSE (deferred_check),
250 TREE_VALUE (deferred_check));
253 /* Defer checking the accessibility of DECL, when looked up in
254 BINFO. */
256 void
257 perform_or_defer_access_check (tree binfo, tree decl)
259 tree check;
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);
267 return;
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)
271 return;
273 /* See if we are already going to perform this check. */
274 for (check = deferred_access_stack->deferred_access_checks;
275 check;
276 check = TREE_CHAIN (check))
277 if (TREE_VALUE (check) == decl && TREE_PURPOSE (check) == binfo)
278 return;
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
297 returned. */
299 stmt_tree
300 current_stmt_tree (void)
302 return (cfun
303 ? &cfun->language->base.x_stmt_tree
304 : &scope_chain->x_stmt_tree);
307 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
308 flag for this because "A union for which objects or pointers are
309 declared is not an anonymous union" [class.union]. */
312 anon_aggr_type_p (tree node)
314 return ANON_AGGR_TYPE_P (node);
317 /* Finish a scope. */
319 static tree
320 do_poplevel (tree stmt_list)
322 tree block = NULL;
324 if (stmts_are_full_exprs_p ())
325 block = poplevel (kept_level_p (), 1, 0);
327 stmt_list = pop_stmt_list (stmt_list);
329 if (!processing_template_decl)
331 stmt_list = c_build_bind_expr (block, stmt_list);
332 /* ??? See c_end_compound_stmt re statement expressions. */
335 return stmt_list;
338 /* Begin a new scope. */
340 static tree
341 do_pushlevel (scope_kind sk)
343 tree ret = push_stmt_list ();
344 if (stmts_are_full_exprs_p ())
345 begin_scope (sk, NULL);
346 return ret;
349 /* Begin a conditional that might contain a declaration. When generating
350 normal code, we want the declaration to appear before the statement
351 containing the conditional. When generating template code, we want the
352 conditional to be rendered as the raw DECL_STMT. */
354 static void
355 begin_cond (tree *cond_p)
357 if (processing_template_decl)
358 *cond_p = push_stmt_list ();
361 /* Finish such a conditional. */
363 static void
364 finish_cond (tree *cond_p, tree expr)
366 if (processing_template_decl)
368 tree cond = pop_stmt_list (*cond_p);
369 if (TREE_CODE (cond) == DECL_STMT)
370 expr = cond;
372 *cond_p = expr;
375 /* If *COND_P specifies a conditional with a declaration, transform the
376 loop such that
377 while (A x = 42) { }
378 for (; A x = 42;) { }
379 becomes
380 while (true) { A x = 42; if (!x) break; }
381 for (;;) { A x = 42; if (!x) break; }
382 The statement list for BODY will be empty if the conditional did
383 not declare anything. */
385 static void
386 simplify_loop_decl_cond (tree *cond_p, tree body)
388 tree cond, if_stmt;
390 if (!TREE_SIDE_EFFECTS (body))
391 return;
393 cond = *cond_p;
394 *cond_p = boolean_true_node;
396 if_stmt = begin_if_stmt ();
397 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
398 finish_if_stmt_cond (cond, if_stmt);
399 finish_break_stmt ();
400 finish_then_clause (if_stmt);
401 finish_if_stmt (if_stmt);
404 /* Finish a goto-statement. */
406 tree
407 finish_goto_stmt (tree destination)
409 if (TREE_CODE (destination) == IDENTIFIER_NODE)
410 destination = lookup_label (destination);
412 /* We warn about unused labels with -Wunused. That means we have to
413 mark the used labels as used. */
414 if (TREE_CODE (destination) == LABEL_DECL)
415 TREE_USED (destination) = 1;
416 else
418 /* The DESTINATION is being used as an rvalue. */
419 if (!processing_template_decl)
420 destination = decay_conversion (destination);
421 /* We don't inline calls to functions with computed gotos.
422 Those functions are typically up to some funny business,
423 and may be depending on the labels being at particular
424 addresses, or some such. */
425 DECL_UNINLINABLE (current_function_decl) = 1;
428 check_goto (destination);
430 return add_stmt (build_stmt (GOTO_EXPR, destination));
433 /* COND is the condition-expression for an if, while, etc.,
434 statement. Convert it to a boolean value, if appropriate. */
436 static tree
437 maybe_convert_cond (tree cond)
439 /* Empty conditions remain empty. */
440 if (!cond)
441 return NULL_TREE;
443 /* Wait until we instantiate templates before doing conversion. */
444 if (processing_template_decl)
445 return cond;
447 /* Do the conversion. */
448 cond = convert_from_reference (cond);
449 return condition_conversion (cond);
452 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
454 tree
455 finish_expr_stmt (tree expr)
457 tree r = NULL_TREE;
459 if (expr != NULL_TREE)
461 if (!processing_template_decl)
462 expr = convert_to_void (expr, "statement");
463 else if (!type_dependent_expression_p (expr))
464 convert_to_void (build_non_dependent_expr (expr), "statement");
466 /* Simplification of inner statement expressions, compound exprs,
467 etc can result in the us already having an EXPR_STMT. */
468 if (TREE_CODE (expr) != EXPR_STMT)
469 expr = build_stmt (EXPR_STMT, expr);
470 r = add_stmt (expr);
473 finish_stmt ();
475 return r;
479 /* Begin an if-statement. Returns a newly created IF_STMT if
480 appropriate. */
482 tree
483 begin_if_stmt (void)
485 tree r, scope;
486 scope = do_pushlevel (sk_block);
487 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
488 TREE_CHAIN (r) = scope;
489 begin_cond (&IF_COND (r));
490 return r;
493 /* Process the COND of an if-statement, which may be given by
494 IF_STMT. */
496 void
497 finish_if_stmt_cond (tree cond, tree if_stmt)
499 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
500 add_stmt (if_stmt);
501 THEN_CLAUSE (if_stmt) = push_stmt_list ();
504 /* Finish the then-clause of an if-statement, which may be given by
505 IF_STMT. */
507 tree
508 finish_then_clause (tree if_stmt)
510 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
511 return if_stmt;
514 /* Begin the else-clause of an if-statement. */
516 void
517 begin_else_clause (tree if_stmt)
519 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
522 /* Finish the else-clause of an if-statement, which may be given by
523 IF_STMT. */
525 void
526 finish_else_clause (tree if_stmt)
528 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
531 /* Finish an if-statement. */
533 void
534 finish_if_stmt (tree if_stmt)
536 tree scope = TREE_CHAIN (if_stmt);
537 TREE_CHAIN (if_stmt) = NULL;
538 add_stmt (do_poplevel (scope));
539 finish_stmt ();
542 /* Begin a while-statement. Returns a newly created WHILE_STMT if
543 appropriate. */
545 tree
546 begin_while_stmt (void)
548 tree r;
549 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
550 add_stmt (r);
551 WHILE_BODY (r) = do_pushlevel (sk_block);
552 begin_cond (&WHILE_COND (r));
553 return r;
556 /* Process the COND of a while-statement, which may be given by
557 WHILE_STMT. */
559 void
560 finish_while_stmt_cond (tree cond, tree while_stmt)
562 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
563 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
566 /* Finish a while-statement, which may be given by WHILE_STMT. */
568 void
569 finish_while_stmt (tree while_stmt)
571 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
572 finish_stmt ();
575 /* Begin a do-statement. Returns a newly created DO_STMT if
576 appropriate. */
578 tree
579 begin_do_stmt (void)
581 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
582 add_stmt (r);
583 DO_BODY (r) = push_stmt_list ();
584 return r;
587 /* Finish the body of a do-statement, which may be given by DO_STMT. */
589 void
590 finish_do_body (tree do_stmt)
592 DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
595 /* Finish a do-statement, which may be given by DO_STMT, and whose
596 COND is as indicated. */
598 void
599 finish_do_stmt (tree cond, tree do_stmt)
601 cond = maybe_convert_cond (cond);
602 DO_COND (do_stmt) = cond;
603 finish_stmt ();
606 /* Finish a return-statement. The EXPRESSION returned, if any, is as
607 indicated. */
609 tree
610 finish_return_stmt (tree expr)
612 tree r;
614 expr = check_return_expr (expr);
615 if (!processing_template_decl)
617 if (DECL_DESTRUCTOR_P (current_function_decl))
619 /* Similarly, all destructors must run destructors for
620 base-classes before returning. So, all returns in a
621 destructor get sent to the DTOR_LABEL; finish_function emits
622 code to return a value there. */
623 return finish_goto_stmt (dtor_label);
626 r = add_stmt (build_stmt (RETURN_STMT, expr));
627 finish_stmt ();
629 return r;
632 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
634 tree
635 begin_for_stmt (void)
637 tree r;
639 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
640 NULL_TREE, NULL_TREE);
642 if (flag_new_for_scope > 0)
643 TREE_CHAIN (r) = do_pushlevel (sk_for);
645 if (processing_template_decl)
646 FOR_INIT_STMT (r) = push_stmt_list ();
648 return r;
651 /* Finish the for-init-statement of a for-statement, which may be
652 given by FOR_STMT. */
654 void
655 finish_for_init_stmt (tree for_stmt)
657 if (processing_template_decl)
658 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
659 add_stmt (for_stmt);
660 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
661 begin_cond (&FOR_COND (for_stmt));
664 /* Finish the COND of a for-statement, which may be given by
665 FOR_STMT. */
667 void
668 finish_for_cond (tree cond, tree for_stmt)
670 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
671 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
674 /* Finish the increment-EXPRESSION in a for-statement, which may be
675 given by FOR_STMT. */
677 void
678 finish_for_expr (tree expr, tree for_stmt)
680 /* If EXPR is an overloaded function, issue an error; there is no
681 context available to use to perform overload resolution. */
682 if (expr && type_unknown_p (expr))
684 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
685 expr = error_mark_node;
687 FOR_EXPR (for_stmt) = expr;
690 /* Finish the body of a for-statement, which may be given by
691 FOR_STMT. The increment-EXPR for the loop must be
692 provided. */
694 void
695 finish_for_stmt (tree for_stmt)
697 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
699 /* Pop the scope for the body of the loop. */
700 if (flag_new_for_scope > 0)
702 tree scope = TREE_CHAIN (for_stmt);
703 TREE_CHAIN (for_stmt) = NULL;
704 add_stmt (do_poplevel (scope));
707 finish_stmt ();
710 /* Finish a break-statement. */
712 tree
713 finish_break_stmt (void)
715 return add_stmt (build_break_stmt ());
718 /* Finish a continue-statement. */
720 tree
721 finish_continue_stmt (void)
723 return add_stmt (build_continue_stmt ());
726 /* Begin a switch-statement. Returns a new SWITCH_STMT if
727 appropriate. */
729 tree
730 begin_switch_stmt (void)
732 tree r, scope;
734 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
736 scope = do_pushlevel (sk_block);
737 TREE_CHAIN (r) = scope;
738 begin_cond (&SWITCH_COND (r));
740 return r;
743 /* Finish the cond of a switch-statement. */
745 void
746 finish_switch_cond (tree cond, tree switch_stmt)
748 tree orig_type = NULL;
749 if (!processing_template_decl)
751 tree index;
753 /* Convert the condition to an integer or enumeration type. */
754 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
755 if (cond == NULL_TREE)
757 error ("switch quantity not an integer");
758 cond = error_mark_node;
760 orig_type = TREE_TYPE (cond);
761 if (cond != error_mark_node)
763 /* [stmt.switch]
765 Integral promotions are performed. */
766 cond = perform_integral_promotions (cond);
767 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
770 if (cond != error_mark_node)
772 index = get_unwidened (cond, NULL_TREE);
773 /* We can't strip a conversion from a signed type to an unsigned,
774 because if we did, int_fits_type_p would do the wrong thing
775 when checking case values for being in range,
776 and it's too hard to do the right thing. */
777 if (TYPE_UNSIGNED (TREE_TYPE (cond))
778 == TYPE_UNSIGNED (TREE_TYPE (index)))
779 cond = index;
782 finish_cond (&SWITCH_COND (switch_stmt), cond);
783 SWITCH_TYPE (switch_stmt) = orig_type;
784 add_stmt (switch_stmt);
785 push_switch (switch_stmt);
786 SWITCH_BODY (switch_stmt) = push_stmt_list ();
789 /* Finish the body of a switch-statement, which may be given by
790 SWITCH_STMT. The COND to switch on is indicated. */
792 void
793 finish_switch_stmt (tree switch_stmt)
795 tree scope;
797 SWITCH_BODY (switch_stmt) = pop_stmt_list (SWITCH_BODY (switch_stmt));
798 pop_switch ();
799 finish_stmt ();
801 scope = TREE_CHAIN (switch_stmt);
802 TREE_CHAIN (switch_stmt) = NULL;
803 add_stmt (do_poplevel (scope));
806 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
807 appropriate. */
809 tree
810 begin_try_block (void)
812 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
813 add_stmt (r);
814 TRY_STMTS (r) = push_stmt_list ();
815 return r;
818 /* Likewise, for a function-try-block. */
820 tree
821 begin_function_try_block (void)
823 tree r = begin_try_block ();
824 FN_TRY_BLOCK_P (r) = 1;
825 return r;
828 /* Finish a try-block, which may be given by TRY_BLOCK. */
830 void
831 finish_try_block (tree try_block)
833 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
834 TRY_HANDLERS (try_block) = push_stmt_list ();
837 /* Finish the body of a cleanup try-block, which may be given by
838 TRY_BLOCK. */
840 void
841 finish_cleanup_try_block (tree try_block)
843 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
846 /* Finish an implicitly generated try-block, with a cleanup is given
847 by CLEANUP. */
849 void
850 finish_cleanup (tree cleanup, tree try_block)
852 TRY_HANDLERS (try_block) = cleanup;
853 CLEANUP_P (try_block) = 1;
856 /* Likewise, for a function-try-block. */
858 void
859 finish_function_try_block (tree try_block)
861 finish_try_block (try_block);
862 /* FIXME : something queer about CTOR_INITIALIZER somehow following
863 the try block, but moving it inside. */
864 in_function_try_handler = 1;
867 /* Finish a handler-sequence for a try-block, which may be given by
868 TRY_BLOCK. */
870 void
871 finish_handler_sequence (tree try_block)
873 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
874 check_handlers (TRY_HANDLERS (try_block));
877 /* Likewise, for a function-try-block. */
879 void
880 finish_function_handler_sequence (tree try_block)
882 in_function_try_handler = 0;
883 finish_handler_sequence (try_block);
886 /* Begin a handler. Returns a HANDLER if appropriate. */
888 tree
889 begin_handler (void)
891 tree r;
893 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
894 add_stmt (r);
896 /* Create a binding level for the eh_info and the exception object
897 cleanup. */
898 HANDLER_BODY (r) = do_pushlevel (sk_catch);
900 return r;
903 /* Finish the handler-parameters for a handler, which may be given by
904 HANDLER. DECL is the declaration for the catch parameter, or NULL
905 if this is a `catch (...)' clause. */
907 void
908 finish_handler_parms (tree decl, tree handler)
910 tree type = NULL_TREE;
911 if (processing_template_decl)
913 if (decl)
915 decl = pushdecl (decl);
916 decl = push_template_decl (decl);
917 HANDLER_PARMS (handler) = decl;
918 type = TREE_TYPE (decl);
921 else
922 type = expand_start_catch_block (decl);
924 HANDLER_TYPE (handler) = type;
925 if (!processing_template_decl && type)
926 mark_used (eh_type_info (type));
929 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
930 the return value from the matching call to finish_handler_parms. */
932 void
933 finish_handler (tree handler)
935 if (!processing_template_decl)
936 expand_end_catch_block ();
937 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
940 /* Begin a compound statement. FLAGS contains some bits that control the
941 behaviour and context. If BCS_NO_SCOPE is set, the compound statement
942 does not define a scope. If BCS_FN_BODY is set, this is the outermost
943 block of a function. If BCS_TRY_BLOCK is set, this is the block
944 created on behalf of a TRY statement. Returns a token to be passed to
945 finish_compound_stmt. */
947 tree
948 begin_compound_stmt (unsigned int flags)
950 tree r;
952 if (flags & BCS_NO_SCOPE)
954 r = push_stmt_list ();
955 STATEMENT_LIST_NO_SCOPE (r) = 1;
957 /* Normally, we try hard to keep the BLOCK for a statement-expression.
958 But, if it's a statement-expression with a scopeless block, there's
959 nothing to keep, and we don't want to accidentally keep a block
960 *inside* the scopeless block. */
961 keep_next_level (false);
963 else
964 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
966 /* When processing a template, we need to remember where the braces were,
967 so that we can set up identical scopes when instantiating the template
968 later. BIND_EXPR is a handy candidate for this.
969 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
970 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
971 processing templates. */
972 if (processing_template_decl)
974 r = build (BIND_EXPR, NULL, NULL, r, NULL);
975 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
976 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
977 TREE_SIDE_EFFECTS (r) = 1;
980 return r;
983 /* Finish a compound-statement, which is given by STMT. */
985 void
986 finish_compound_stmt (tree stmt)
988 if (TREE_CODE (stmt) == BIND_EXPR)
989 BIND_EXPR_BODY (stmt) = do_poplevel (BIND_EXPR_BODY (stmt));
990 else if (STATEMENT_LIST_NO_SCOPE (stmt))
991 stmt = pop_stmt_list (stmt);
992 else
993 stmt = do_poplevel (stmt);
995 /* ??? See c_end_compound_stmt wrt statement expressions. */
996 add_stmt (stmt);
997 finish_stmt ();
1000 /* Finish an asm-statement, whose components are a STRING, some
1001 OUTPUT_OPERANDS, some INPUT_OPERANDS, and some CLOBBERS. Also note
1002 whether the asm-statement should be considered volatile. */
1004 tree
1005 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1006 tree input_operands, tree clobbers)
1008 tree r;
1009 tree t;
1011 if (!processing_template_decl)
1013 int i;
1014 int ninputs;
1015 int noutputs;
1017 for (t = input_operands; t; t = TREE_CHAIN (t))
1019 tree converted_operand
1020 = decay_conversion (TREE_VALUE (t));
1022 /* If the type of the operand hasn't been determined (e.g.,
1023 because it involves an overloaded function), then issue
1024 an error message. There's no context available to
1025 resolve the overloading. */
1026 if (TREE_TYPE (converted_operand) == unknown_type_node)
1028 error ("type of asm operand `%E' could not be determined",
1029 TREE_VALUE (t));
1030 converted_operand = error_mark_node;
1032 TREE_VALUE (t) = converted_operand;
1035 ninputs = list_length (input_operands);
1036 noutputs = list_length (output_operands);
1038 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1040 bool allows_mem;
1041 bool allows_reg;
1042 bool is_inout;
1043 const char *constraint;
1044 tree operand;
1046 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1047 operand = TREE_VALUE (t);
1049 if (!parse_output_constraint (&constraint,
1050 i, ninputs, noutputs,
1051 &allows_mem,
1052 &allows_reg,
1053 &is_inout))
1055 /* By marking this operand as erroneous, we will not try
1056 to process this operand again in expand_asm_operands. */
1057 TREE_VALUE (t) = error_mark_node;
1058 continue;
1061 /* If the operand is a DECL that is going to end up in
1062 memory, assume it is addressable. This is a bit more
1063 conservative than it would ideally be; the exact test is
1064 buried deep in expand_asm_operands and depends on the
1065 DECL_RTL for the OPERAND -- which we don't have at this
1066 point. */
1067 if (!allows_reg && DECL_P (operand))
1068 cxx_mark_addressable (operand);
1072 r = build_stmt (ASM_EXPR, string,
1073 output_operands, input_operands,
1074 clobbers);
1075 ASM_VOLATILE_P (r) = volatile_p;
1076 return add_stmt (r);
1079 /* Finish a label with the indicated NAME. */
1081 tree
1082 finish_label_stmt (tree name)
1084 tree decl = define_label (input_location, name);
1085 return add_stmt (build_stmt (LABEL_EXPR, decl));
1088 /* Finish a series of declarations for local labels. G++ allows users
1089 to declare "local" labels, i.e., labels with scope. This extension
1090 is useful when writing code involving statement-expressions. */
1092 void
1093 finish_label_decl (tree name)
1095 tree decl = declare_local_label (name);
1096 add_decl_stmt (decl);
1099 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1101 void
1102 finish_decl_cleanup (tree decl, tree cleanup)
1104 push_cleanup (decl, cleanup, false);
1107 /* If the current scope exits with an exception, run CLEANUP. */
1109 void
1110 finish_eh_cleanup (tree cleanup)
1112 push_cleanup (NULL, cleanup, true);
1115 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1116 order they were written by the user. Each node is as for
1117 emit_mem_initializers. */
1119 void
1120 finish_mem_initializers (tree mem_inits)
1122 /* Reorder the MEM_INITS so that they are in the order they appeared
1123 in the source program. */
1124 mem_inits = nreverse (mem_inits);
1126 if (processing_template_decl)
1127 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1128 else
1129 emit_mem_initializers (mem_inits);
1132 /* Finish a parenthesized expression EXPR. */
1134 tree
1135 finish_parenthesized_expr (tree expr)
1137 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1138 /* This inhibits warnings in c_common_truthvalue_conversion. */
1139 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1141 if (TREE_CODE (expr) == OFFSET_REF)
1142 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1143 enclosed in parentheses. */
1144 PTRMEM_OK_P (expr) = 0;
1145 return expr;
1148 /* Finish a reference to a non-static data member (DECL) that is not
1149 preceded by `.' or `->'. */
1151 tree
1152 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1154 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1156 if (!object)
1158 if (current_function_decl
1159 && DECL_STATIC_FUNCTION_P (current_function_decl))
1160 cp_error_at ("invalid use of member `%D' in static member function",
1161 decl);
1162 else
1163 cp_error_at ("invalid use of non-static data member `%D'", decl);
1164 error ("from this location");
1166 return error_mark_node;
1168 TREE_USED (current_class_ptr) = 1;
1169 if (processing_template_decl && !qualifying_scope)
1171 tree type = TREE_TYPE (decl);
1173 if (TREE_CODE (type) == REFERENCE_TYPE)
1174 type = TREE_TYPE (type);
1175 else
1177 /* Set the cv qualifiers. */
1178 int quals = cp_type_quals (TREE_TYPE (current_class_ref));
1180 if (DECL_MUTABLE_P (decl))
1181 quals &= ~TYPE_QUAL_CONST;
1183 quals |= cp_type_quals (TREE_TYPE (decl));
1184 type = cp_build_qualified_type (type, quals);
1187 return build_min (COMPONENT_REF, type, object, decl);
1189 else
1191 tree access_type = TREE_TYPE (object);
1192 tree lookup_context = context_for_name_lookup (decl);
1194 while (!DERIVED_FROM_P (lookup_context, access_type))
1196 access_type = TYPE_CONTEXT (access_type);
1197 while (access_type && DECL_P (access_type))
1198 access_type = DECL_CONTEXT (access_type);
1200 if (!access_type)
1202 cp_error_at ("object missing in reference to `%D'", decl);
1203 error ("from this location");
1204 return error_mark_node;
1208 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1209 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1210 for now. */
1211 if (processing_template_decl)
1212 return build_min (SCOPE_REF, TREE_TYPE (decl),
1213 qualifying_scope, DECL_NAME (decl));
1215 perform_or_defer_access_check (TYPE_BINFO (access_type), decl);
1217 /* If the data member was named `C::M', convert `*this' to `C'
1218 first. */
1219 if (qualifying_scope)
1221 tree binfo = NULL_TREE;
1222 object = build_scoped_ref (object, qualifying_scope,
1223 &binfo);
1226 return build_class_member_access_expr (object, decl,
1227 /*access_path=*/NULL_TREE,
1228 /*preserve_reference=*/false);
1232 /* DECL was the declaration to which a qualified-id resolved. Issue
1233 an error message if it is not accessible. If OBJECT_TYPE is
1234 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1235 type of `*x', or `x', respectively. If the DECL was named as
1236 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1238 void
1239 check_accessibility_of_qualified_id (tree decl,
1240 tree object_type,
1241 tree nested_name_specifier)
1243 tree scope;
1244 tree qualifying_type = NULL_TREE;
1246 /* Determine the SCOPE of DECL. */
1247 scope = context_for_name_lookup (decl);
1248 /* If the SCOPE is not a type, then DECL is not a member. */
1249 if (!TYPE_P (scope))
1250 return;
1251 /* Compute the scope through which DECL is being accessed. */
1252 if (object_type
1253 /* OBJECT_TYPE might not be a class type; consider:
1255 class A { typedef int I; };
1256 I *p;
1257 p->A::I::~I();
1259 In this case, we will have "A::I" as the DECL, but "I" as the
1260 OBJECT_TYPE. */
1261 && CLASS_TYPE_P (object_type)
1262 && DERIVED_FROM_P (scope, object_type))
1263 /* If we are processing a `->' or `.' expression, use the type of the
1264 left-hand side. */
1265 qualifying_type = object_type;
1266 else if (nested_name_specifier)
1268 /* If the reference is to a non-static member of the
1269 current class, treat it as if it were referenced through
1270 `this'. */
1271 if (DECL_NONSTATIC_MEMBER_P (decl)
1272 && current_class_ptr
1273 && DERIVED_FROM_P (scope, current_class_type))
1274 qualifying_type = current_class_type;
1275 /* Otherwise, use the type indicated by the
1276 nested-name-specifier. */
1277 else
1278 qualifying_type = nested_name_specifier;
1280 else
1281 /* Otherwise, the name must be from the current class or one of
1282 its bases. */
1283 qualifying_type = currently_open_derived_class (scope);
1285 if (qualifying_type)
1286 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
1289 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1290 class named to the left of the "::" operator. DONE is true if this
1291 expression is a complete postfix-expression; it is false if this
1292 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1293 iff this expression is the operand of '&'. */
1295 tree
1296 finish_qualified_id_expr (tree qualifying_class, tree expr, bool done,
1297 bool address_p)
1299 if (error_operand_p (expr))
1300 return error_mark_node;
1302 /* If EXPR occurs as the operand of '&', use special handling that
1303 permits a pointer-to-member. */
1304 if (address_p && done)
1306 if (TREE_CODE (expr) == SCOPE_REF)
1307 expr = TREE_OPERAND (expr, 1);
1308 expr = build_offset_ref (qualifying_class, expr,
1309 /*address_p=*/true);
1310 return expr;
1313 if (TREE_CODE (expr) == FIELD_DECL)
1314 expr = finish_non_static_data_member (expr, current_class_ref,
1315 qualifying_class);
1316 else if (BASELINK_P (expr) && !processing_template_decl)
1318 tree fn;
1319 tree fns;
1321 /* See if any of the functions are non-static members. */
1322 fns = BASELINK_FUNCTIONS (expr);
1323 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
1324 fns = TREE_OPERAND (fns, 0);
1325 for (fn = fns; fn; fn = OVL_NEXT (fn))
1326 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1327 break;
1328 /* If so, the expression may be relative to the current
1329 class. */
1330 if (fn && current_class_type
1331 && DERIVED_FROM_P (qualifying_class, current_class_type))
1332 expr = (build_class_member_access_expr
1333 (maybe_dummy_object (qualifying_class, NULL),
1334 expr,
1335 BASELINK_ACCESS_BINFO (expr),
1336 /*preserve_reference=*/false));
1337 else if (done)
1338 /* The expression is a qualified name whose address is not
1339 being taken. */
1340 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1343 return expr;
1346 /* Begin a statement-expression. The value returned must be passed to
1347 finish_stmt_expr. */
1349 tree
1350 begin_stmt_expr (void)
1352 return push_stmt_list ();
1355 /* Process the final expression of a statement expression. EXPR can be
1356 NULL, if the final expression is empty. Build up a TARGET_EXPR so
1357 that the result value can be safely returned to the enclosing
1358 expression. */
1360 tree
1361 finish_stmt_expr_expr (tree expr, tree stmt_expr)
1363 tree result = NULL_TREE;
1365 if (expr)
1367 if (!processing_template_decl && !VOID_TYPE_P (TREE_TYPE (expr)))
1369 tree type = TREE_TYPE (expr);
1371 if (TREE_CODE (type) == ARRAY_TYPE
1372 || TREE_CODE (type) == FUNCTION_TYPE)
1373 expr = decay_conversion (expr);
1375 expr = convert_from_reference (expr);
1376 expr = require_complete_type (expr);
1378 type = TREE_TYPE (expr);
1380 /* Build a TARGET_EXPR for this aggregate. finish_stmt_expr
1381 will then pull it apart so the lifetime of the target is
1382 within the scope of the expression containing this statement
1383 expression. */
1384 if (TREE_CODE (expr) == TARGET_EXPR)
1386 else if (!IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_INIT_REF (type))
1387 expr = build_target_expr_with_type (expr, type);
1388 else
1390 /* Copy construct. */
1391 expr = build_special_member_call
1392 (NULL_TREE, complete_ctor_identifier,
1393 build_tree_list (NULL_TREE, expr),
1394 TYPE_BINFO (type), LOOKUP_NORMAL);
1395 expr = build_cplus_new (type, expr);
1396 my_friendly_assert (TREE_CODE (expr) == TARGET_EXPR, 20030729);
1400 if (expr != error_mark_node)
1402 result = build_stmt (EXPR_STMT, expr);
1403 EXPR_STMT_STMT_EXPR_RESULT (result) = 1;
1404 add_stmt (result);
1408 finish_stmt ();
1410 /* Remember the last expression so that finish_stmt_expr
1411 can pull it apart. */
1412 TREE_TYPE (stmt_expr) = result;
1414 return result;
1417 /* Finish a statement-expression. EXPR should be the value returned
1418 by the previous begin_stmt_expr. Returns an expression
1419 representing the statement-expression. */
1421 tree
1422 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1424 tree result, result_stmt, type;
1425 tree *result_stmt_p = NULL;
1427 result_stmt = TREE_TYPE (stmt_expr);
1428 TREE_TYPE (stmt_expr) = void_type_node;
1429 result = pop_stmt_list (stmt_expr);
1431 if (!result_stmt || VOID_TYPE_P (result_stmt))
1432 type = void_type_node;
1433 else
1435 /* We need to search the statement expression for the result_stmt,
1436 since we'll need to replace it entirely. */
1437 tree t;
1438 result_stmt_p = &result;
1439 while (1)
1441 t = *result_stmt_p;
1442 if (t == result_stmt)
1443 break;
1445 switch (TREE_CODE (t))
1447 case STATEMENT_LIST:
1449 tree_stmt_iterator i = tsi_last (t);
1450 result_stmt_p = tsi_stmt_ptr (i);
1451 break;
1453 case BIND_EXPR:
1454 result_stmt_p = &BIND_EXPR_BODY (t);
1455 break;
1456 case TRY_FINALLY_EXPR:
1457 case TRY_CATCH_EXPR:
1458 case CLEANUP_STMT:
1459 result_stmt_p = &TREE_OPERAND (t, 0);
1460 break;
1461 default:
1462 abort ();
1465 type = TREE_TYPE (EXPR_STMT_EXPR (result_stmt));
1468 if (processing_template_decl)
1470 result = build_min (STMT_EXPR, type, result);
1471 TREE_SIDE_EFFECTS (result) = 1;
1472 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1474 else if (!VOID_TYPE_P (type))
1476 /* Pull out the TARGET_EXPR that is the final expression. Put
1477 the target's init_expr as the final expression and then put
1478 the statement expression itself as the target's init
1479 expr. Finally, return the target expression. */
1480 tree init, target_expr = EXPR_STMT_EXPR (result_stmt);
1481 my_friendly_assert (TREE_CODE (target_expr) == TARGET_EXPR, 20030729);
1483 /* The initializer will be void if the initialization is done by
1484 AGGR_INIT_EXPR; propagate that out to the statement-expression as
1485 a whole. */
1486 init = TREE_OPERAND (target_expr, 1);
1487 type = TREE_TYPE (init);
1489 if (stmts_are_full_exprs_p ())
1490 init = fold (build1 (CLEANUP_POINT_EXPR, type, init));
1491 *result_stmt_p = init;
1493 if (VOID_TYPE_P (type))
1494 /* No frobbing needed. */;
1495 else if (TREE_CODE (result) == BIND_EXPR)
1497 /* The BIND_EXPR created in finish_compound_stmt is void; if we're
1498 returning a value directly, give it the appropriate type. */
1499 if (VOID_TYPE_P (TREE_TYPE (result)))
1500 TREE_TYPE (result) = type;
1501 else if (same_type_p (TREE_TYPE (result), type))
1503 else
1504 abort ();
1506 else if (TREE_CODE (result) == STATEMENT_LIST)
1507 /* We need to wrap a STATEMENT_LIST in a BIND_EXPR so it can have a
1508 type other than void. FIXME why can't we just return a value
1509 from STATEMENT_LIST? */
1510 result = build3 (BIND_EXPR, type, NULL, result, NULL);
1512 TREE_OPERAND (target_expr, 1) = result;
1513 result = target_expr;
1516 return result;
1519 /* Perform Koenig lookup. FN is the postfix-expression representing
1520 the function (or functions) to call; ARGS are the arguments to the
1521 call. Returns the functions to be considered by overload
1522 resolution. */
1524 tree
1525 perform_koenig_lookup (tree fn, tree args)
1527 tree identifier = NULL_TREE;
1528 tree functions = NULL_TREE;
1530 /* Find the name of the overloaded function. */
1531 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1532 identifier = fn;
1533 else if (is_overloaded_fn (fn))
1535 functions = fn;
1536 identifier = DECL_NAME (get_first_fn (functions));
1538 else if (DECL_P (fn))
1540 functions = fn;
1541 identifier = DECL_NAME (fn);
1544 /* A call to a namespace-scope function using an unqualified name.
1546 Do Koenig lookup -- unless any of the arguments are
1547 type-dependent. */
1548 if (!any_type_dependent_arguments_p (args))
1550 fn = lookup_arg_dependent (identifier, functions, args);
1551 if (!fn)
1552 /* The unqualified name could not be resolved. */
1553 fn = unqualified_fn_lookup_error (identifier);
1555 else
1556 fn = identifier;
1558 return fn;
1561 /* Generate an expression for `FN (ARGS)'.
1563 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1564 as a virtual call, even if FN is virtual. (This flag is set when
1565 encountering an expression where the function name is explicitly
1566 qualified. For example a call to `X::f' never generates a virtual
1567 call.)
1569 Returns code for the call. */
1571 tree
1572 finish_call_expr (tree fn, tree args, bool disallow_virtual, bool koenig_p)
1574 tree result;
1575 tree orig_fn;
1576 tree orig_args;
1578 if (fn == error_mark_node || args == error_mark_node)
1579 return error_mark_node;
1581 /* ARGS should be a list of arguments. */
1582 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1583 20020712);
1585 orig_fn = fn;
1586 orig_args = args;
1588 if (processing_template_decl)
1590 if (type_dependent_expression_p (fn)
1591 || any_type_dependent_arguments_p (args))
1593 result = build_nt (CALL_EXPR, fn, args, NULL_TREE);
1594 KOENIG_LOOKUP_P (result) = koenig_p;
1595 return result;
1597 if (!BASELINK_P (fn)
1598 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
1599 && TREE_TYPE (fn) != unknown_type_node)
1600 fn = build_non_dependent_expr (fn);
1601 args = build_non_dependent_args (orig_args);
1604 /* A reference to a member function will appear as an overloaded
1605 function (rather than a BASELINK) if an unqualified name was used
1606 to refer to it. */
1607 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1609 tree f = fn;
1611 if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
1612 f = TREE_OPERAND (f, 0);
1613 f = get_first_fn (f);
1614 if (DECL_FUNCTION_MEMBER_P (f))
1616 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1617 if (!type)
1618 type = DECL_CONTEXT (f);
1619 fn = build_baselink (TYPE_BINFO (type),
1620 TYPE_BINFO (type),
1621 fn, /*optype=*/NULL_TREE);
1625 result = NULL_TREE;
1626 if (BASELINK_P (fn))
1628 tree object;
1630 /* A call to a member function. From [over.call.func]:
1632 If the keyword this is in scope and refers to the class of
1633 that member function, or a derived class thereof, then the
1634 function call is transformed into a qualified function call
1635 using (*this) as the postfix-expression to the left of the
1636 . operator.... [Otherwise] a contrived object of type T
1637 becomes the implied object argument.
1639 This paragraph is unclear about this situation:
1641 struct A { void f(); };
1642 struct B : public A {};
1643 struct C : public A { void g() { B::f(); }};
1645 In particular, for `B::f', this paragraph does not make clear
1646 whether "the class of that member function" refers to `A' or
1647 to `B'. We believe it refers to `B'. */
1648 if (current_class_type
1649 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1650 current_class_type)
1651 && current_class_ref)
1652 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1653 NULL);
1654 else
1656 tree representative_fn;
1658 representative_fn = BASELINK_FUNCTIONS (fn);
1659 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1660 representative_fn = TREE_OPERAND (representative_fn, 0);
1661 representative_fn = get_first_fn (representative_fn);
1662 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1665 if (processing_template_decl)
1667 if (type_dependent_expression_p (object))
1668 return build_nt (CALL_EXPR, orig_fn, orig_args, NULL_TREE);
1669 object = build_non_dependent_expr (object);
1672 result = build_new_method_call (object, fn, args, NULL_TREE,
1673 (disallow_virtual
1674 ? LOOKUP_NONVIRTUAL : 0));
1676 else if (is_overloaded_fn (fn))
1677 /* A call to a namespace-scope function. */
1678 result = build_new_function_call (fn, args);
1679 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1681 if (args)
1682 error ("arguments to destructor are not allowed");
1683 /* Mark the pseudo-destructor call as having side-effects so
1684 that we do not issue warnings about its use. */
1685 result = build1 (NOP_EXPR,
1686 void_type_node,
1687 TREE_OPERAND (fn, 0));
1688 TREE_SIDE_EFFECTS (result) = 1;
1690 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1691 /* If the "function" is really an object of class type, it might
1692 have an overloaded `operator ()'. */
1693 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE,
1694 /*overloaded_p=*/NULL);
1695 if (!result)
1696 /* A call where the function is unknown. */
1697 result = build_function_call (fn, args);
1699 if (processing_template_decl)
1701 result = build (CALL_EXPR, TREE_TYPE (result), orig_fn,
1702 orig_args, NULL_TREE);
1703 KOENIG_LOOKUP_P (result) = koenig_p;
1705 return result;
1708 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1709 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1710 POSTDECREMENT_EXPR.) */
1712 tree
1713 finish_increment_expr (tree expr, enum tree_code code)
1715 return build_x_unary_op (code, expr);
1718 /* Finish a use of `this'. Returns an expression for `this'. */
1720 tree
1721 finish_this_expr (void)
1723 tree result;
1725 if (current_class_ptr)
1727 result = current_class_ptr;
1729 else if (current_function_decl
1730 && DECL_STATIC_FUNCTION_P (current_function_decl))
1732 error ("`this' is unavailable for static member functions");
1733 result = error_mark_node;
1735 else
1737 if (current_function_decl)
1738 error ("invalid use of `this' in non-member function");
1739 else
1740 error ("invalid use of `this' at top level");
1741 result = error_mark_node;
1744 return result;
1747 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1748 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1749 the TYPE for the type given. If SCOPE is non-NULL, the expression
1750 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1752 tree
1753 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
1755 if (destructor == error_mark_node)
1756 return error_mark_node;
1758 my_friendly_assert (TYPE_P (destructor), 20010905);
1760 if (!processing_template_decl)
1762 if (scope == error_mark_node)
1764 error ("invalid qualifying scope in pseudo-destructor name");
1765 return error_mark_node;
1768 /* [expr.pseudo] says both:
1770 The type designated by the pseudo-destructor-name shall be
1771 the same as the object type.
1773 and:
1775 The cv-unqualified versions of the object type and of the
1776 type designated by the pseudo-destructor-name shall be the
1777 same type.
1779 We implement the more generous second sentence, since that is
1780 what most other compilers do. */
1781 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
1782 destructor))
1784 error ("`%E' is not of type `%T'", object, destructor);
1785 return error_mark_node;
1789 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1792 /* Finish an expression of the form CODE EXPR. */
1794 tree
1795 finish_unary_op_expr (enum tree_code code, tree expr)
1797 tree result = build_x_unary_op (code, expr);
1798 /* Inside a template, build_x_unary_op does not fold the
1799 expression. So check whether the result is folded before
1800 setting TREE_NEGATED_INT. */
1801 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1802 && TREE_CODE (result) == INTEGER_CST
1803 && !TYPE_UNSIGNED (TREE_TYPE (result))
1804 && INT_CST_LT (result, integer_zero_node))
1805 TREE_NEGATED_INT (result) = 1;
1806 overflow_warning (result);
1807 return result;
1810 /* Finish a compound-literal expression. TYPE is the type to which
1811 the INITIALIZER_LIST is being cast. */
1813 tree
1814 finish_compound_literal (tree type, tree initializer_list)
1816 tree compound_literal;
1818 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1819 compound_literal = build_constructor (NULL_TREE, initializer_list);
1820 /* Mark it as a compound-literal. */
1821 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1822 if (processing_template_decl)
1823 TREE_TYPE (compound_literal) = type;
1824 else
1826 /* Check the initialization. */
1827 compound_literal = digest_init (type, compound_literal, NULL);
1828 /* If the TYPE was an array type with an unknown bound, then we can
1829 figure out the dimension now. For example, something like:
1831 `(int []) { 2, 3 }'
1833 implies that the array has two elements. */
1834 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1835 complete_array_type (type, compound_literal, 1);
1838 return compound_literal;
1841 /* Return the declaration for the function-name variable indicated by
1842 ID. */
1844 tree
1845 finish_fname (tree id)
1847 tree decl;
1849 decl = fname_decl (C_RID_CODE (id), id);
1850 if (processing_template_decl)
1851 decl = DECL_NAME (decl);
1852 return decl;
1855 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1856 and DECLARATOR. Returns nonzero if the function-declaration is
1857 valid. */
1860 begin_function_definition (tree decl_specs, tree attributes, tree declarator)
1862 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1863 return 0;
1865 /* The things we're about to see are not directly qualified by any
1866 template headers we've seen thus far. */
1867 reset_specialization ();
1869 return 1;
1872 /* Finish a translation unit. */
1874 void
1875 finish_translation_unit (void)
1877 /* In case there were missing closebraces,
1878 get us back to the global binding level. */
1879 pop_everything ();
1880 while (current_namespace != global_namespace)
1881 pop_namespace ();
1883 /* Do file scope __FUNCTION__ et al. */
1884 finish_fname_decls ();
1887 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1888 Returns the parameter. */
1890 tree
1891 finish_template_type_parm (tree aggr, tree identifier)
1893 if (aggr != class_type_node)
1895 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1896 aggr = class_type_node;
1899 return build_tree_list (aggr, identifier);
1902 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1903 Returns the parameter. */
1905 tree
1906 finish_template_template_parm (tree aggr, tree identifier)
1908 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1909 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1910 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1911 DECL_TEMPLATE_RESULT (tmpl) = decl;
1912 DECL_ARTIFICIAL (decl) = 1;
1913 end_template_decl ();
1915 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1917 return finish_template_type_parm (aggr, tmpl);
1920 /* ARGUMENT is the default-argument value for a template template
1921 parameter. If ARGUMENT is invalid, issue error messages and return
1922 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1924 tree
1925 check_template_template_default_arg (tree argument)
1927 if (TREE_CODE (argument) != TEMPLATE_DECL
1928 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1929 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1931 if (TREE_CODE (argument) == TYPE_DECL)
1933 tree t = TREE_TYPE (argument);
1935 /* Try to emit a slightly smarter error message if we detect
1936 that the user is using a template instantiation. */
1937 if (CLASSTYPE_TEMPLATE_INFO (t)
1938 && CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1939 error ("invalid use of type `%T' as a default value for a "
1940 "template template-parameter", t);
1941 else
1942 error ("invalid use of `%D' as a default value for a template "
1943 "template-parameter", argument);
1945 else
1946 error ("invalid default argument for a template template parameter");
1947 return error_mark_node;
1950 return argument;
1953 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1954 nonzero, the parameter list was terminated by a `...'. */
1956 tree
1957 finish_parmlist (tree parms, int ellipsis)
1959 if (parms)
1961 /* We mark the PARMS as a parmlist so that declarator processing can
1962 disambiguate certain constructs. */
1963 TREE_PARMLIST (parms) = 1;
1964 /* We do not append void_list_node here, but leave it to grokparms
1965 to do that. */
1966 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1968 return parms;
1971 /* Begin a class definition, as indicated by T. */
1973 tree
1974 begin_class_definition (tree t)
1976 if (t == error_mark_node)
1977 return error_mark_node;
1979 if (processing_template_parmlist)
1981 error ("definition of `%#T' inside template parameter list", t);
1982 return error_mark_node;
1984 /* A non-implicit typename comes from code like:
1986 template <typename T> struct A {
1987 template <typename U> struct A<T>::B ...
1989 This is erroneous. */
1990 else if (TREE_CODE (t) == TYPENAME_TYPE)
1992 error ("invalid definition of qualified type `%T'", t);
1993 t = error_mark_node;
1996 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1998 t = make_aggr_type (RECORD_TYPE);
1999 pushtag (make_anon_name (), t, 0);
2002 /* If this type was already complete, and we see another definition,
2003 that's an error. */
2004 if (COMPLETE_TYPE_P (t))
2006 error ("redefinition of `%#T'", t);
2007 cp_error_at ("previous definition of `%#T'", t);
2008 return error_mark_node;
2011 /* Update the location of the decl. */
2012 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
2014 if (TYPE_BEING_DEFINED (t))
2016 t = make_aggr_type (TREE_CODE (t));
2017 pushtag (TYPE_IDENTIFIER (t), t, 0);
2019 maybe_process_partial_specialization (t);
2020 pushclass (t);
2021 TYPE_BEING_DEFINED (t) = 1;
2022 if (flag_pack_struct)
2024 tree v;
2025 TYPE_PACKED (t) = 1;
2026 /* Even though the type is being defined for the first time
2027 here, there might have been a forward declaration, so there
2028 might be cv-qualified variants of T. */
2029 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2030 TYPE_PACKED (v) = 1;
2032 /* Reset the interface data, at the earliest possible
2033 moment, as it might have been set via a class foo;
2034 before. */
2035 if (! TYPE_ANONYMOUS_P (t))
2037 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2038 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2039 (t, interface_unknown);
2041 reset_specialization();
2043 /* Make a declaration for this class in its own scope. */
2044 build_self_reference ();
2046 return t;
2049 /* Finish the member declaration given by DECL. */
2051 void
2052 finish_member_declaration (tree decl)
2054 if (decl == error_mark_node || decl == NULL_TREE)
2055 return;
2057 if (decl == void_type_node)
2058 /* The COMPONENT was a friend, not a member, and so there's
2059 nothing for us to do. */
2060 return;
2062 /* We should see only one DECL at a time. */
2063 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
2065 /* Set up access control for DECL. */
2066 TREE_PRIVATE (decl)
2067 = (current_access_specifier == access_private_node);
2068 TREE_PROTECTED (decl)
2069 = (current_access_specifier == access_protected_node);
2070 if (TREE_CODE (decl) == TEMPLATE_DECL)
2072 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2073 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2076 /* Mark the DECL as a member of the current class. */
2077 DECL_CONTEXT (decl) = current_class_type;
2079 /* [dcl.link]
2081 A C language linkage is ignored for the names of class members
2082 and the member function type of class member functions. */
2083 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2084 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2086 /* Put functions on the TYPE_METHODS list and everything else on the
2087 TYPE_FIELDS list. Note that these are built up in reverse order.
2088 We reverse them (to obtain declaration order) in finish_struct. */
2089 if (TREE_CODE (decl) == FUNCTION_DECL
2090 || DECL_FUNCTION_TEMPLATE_P (decl))
2092 /* We also need to add this function to the
2093 CLASSTYPE_METHOD_VEC. */
2094 add_method (current_class_type, decl, /*error_p=*/0);
2096 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
2097 TYPE_METHODS (current_class_type) = decl;
2099 maybe_add_class_template_decl_list (current_class_type, decl,
2100 /*friend_p=*/0);
2102 /* Enter the DECL into the scope of the class. */
2103 else if ((TREE_CODE (decl) == USING_DECL && TREE_TYPE (decl))
2104 || pushdecl_class_level (decl))
2106 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2107 go at the beginning. The reason is that lookup_field_1
2108 searches the list in order, and we want a field name to
2109 override a type name so that the "struct stat hack" will
2110 work. In particular:
2112 struct S { enum E { }; int E } s;
2113 s.E = 3;
2115 is valid. In addition, the FIELD_DECLs must be maintained in
2116 declaration order so that class layout works as expected.
2117 However, we don't need that order until class layout, so we
2118 save a little time by putting FIELD_DECLs on in reverse order
2119 here, and then reversing them in finish_struct_1. (We could
2120 also keep a pointer to the correct insertion points in the
2121 list.) */
2123 if (TREE_CODE (decl) == TYPE_DECL)
2124 TYPE_FIELDS (current_class_type)
2125 = chainon (TYPE_FIELDS (current_class_type), decl);
2126 else
2128 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2129 TYPE_FIELDS (current_class_type) = decl;
2132 maybe_add_class_template_decl_list (current_class_type, decl,
2133 /*friend_p=*/0);
2137 /* Finish processing the declaration of a member class template
2138 TYPES whose template parameters are given by PARMS. */
2140 tree
2141 finish_member_class_template (tree types)
2143 tree t;
2145 /* If there are declared, but undefined, partial specializations
2146 mixed in with the typespecs they will not yet have passed through
2147 maybe_process_partial_specialization, so we do that here. */
2148 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2149 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2150 maybe_process_partial_specialization (TREE_VALUE (t));
2152 grok_x_components (types);
2153 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2154 /* The component was in fact a friend declaration. We avoid
2155 finish_member_template_decl performing certain checks by
2156 unsetting TYPES. */
2157 types = NULL_TREE;
2159 finish_member_template_decl (types);
2161 /* As with other component type declarations, we do
2162 not store the new DECL on the list of
2163 component_decls. */
2164 return NULL_TREE;
2167 /* Finish processing a complete template declaration. The PARMS are
2168 the template parameters. */
2170 void
2171 finish_template_decl (tree parms)
2173 if (parms)
2174 end_template_decl ();
2175 else
2176 end_specialization ();
2179 /* Finish processing a template-id (which names a type) of the form
2180 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2181 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2182 the scope of template-id indicated. */
2184 tree
2185 finish_template_type (tree name, tree args, int entering_scope)
2187 tree decl;
2189 decl = lookup_template_class (name, args,
2190 NULL_TREE, NULL_TREE, entering_scope,
2191 tf_error | tf_warning | tf_user);
2192 if (decl != error_mark_node)
2193 decl = TYPE_STUB_DECL (decl);
2195 return decl;
2198 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2199 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2200 BASE_CLASS, or NULL_TREE if an error occurred. The
2201 ACCESS_SPECIFIER is one of
2202 access_{default,public,protected_private}[_virtual]_node.*/
2204 tree
2205 finish_base_specifier (tree base, tree access, bool virtual_p)
2207 tree result;
2209 if (base == error_mark_node)
2211 error ("invalid base-class specification");
2212 result = NULL_TREE;
2214 else if (! is_aggr_type (base, 1))
2215 result = NULL_TREE;
2216 else
2218 if (cp_type_quals (base) != 0)
2220 error ("base class `%T' has cv qualifiers", base);
2221 base = TYPE_MAIN_VARIANT (base);
2223 result = build_tree_list (access, base);
2224 TREE_VIA_VIRTUAL (result) = virtual_p;
2227 return result;
2230 /* Called when multiple declarators are processed. If that is not
2231 permitted in this context, an error is issued. */
2233 void
2234 check_multiple_declarators (void)
2236 /* [temp]
2238 In a template-declaration, explicit specialization, or explicit
2239 instantiation the init-declarator-list in the declaration shall
2240 contain at most one declarator.
2242 We don't just use PROCESSING_TEMPLATE_DECL for the first
2243 condition since that would disallow the perfectly valid code,
2244 like `template <class T> struct S { int i, j; };'. */
2245 if (at_function_scope_p ())
2246 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2247 return;
2249 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2250 || processing_explicit_instantiation
2251 || processing_specialization)
2252 error ("multiple declarators in template declaration");
2255 /* Issue a diagnostic that NAME cannot be found in SCOPE. */
2257 void
2258 qualified_name_lookup_error (tree scope, tree name)
2260 if (TYPE_P (scope))
2262 if (!COMPLETE_TYPE_P (scope))
2263 error ("incomplete type `%T' used in nested name specifier", scope);
2264 else
2265 error ("`%D' is not a member of `%T'", name, scope);
2267 else if (scope != global_namespace)
2268 error ("`%D' is not a member of `%D'", name, scope);
2269 else
2270 error ("`::%D' has not been declared", name);
2273 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
2274 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2275 if non-NULL, is the type or namespace used to explicitly qualify
2276 ID_EXPRESSION. DECL is the entity to which that name has been
2277 resolved.
2279 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2280 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2281 be set to true if this expression isn't permitted in a
2282 constant-expression, but it is otherwise not set by this function.
2283 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2284 constant-expression, but a non-constant expression is also
2285 permissible.
2287 If an error occurs, and it is the kind of error that might cause
2288 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2289 is the caller's responsibility to issue the message. *ERROR_MSG
2290 will be a string with static storage duration, so the caller need
2291 not "free" it.
2293 Return an expression for the entity, after issuing appropriate
2294 diagnostics. This function is also responsible for transforming a
2295 reference to a non-static member into a COMPONENT_REF that makes
2296 the use of "this" explicit.
2298 Upon return, *IDK will be filled in appropriately. */
2300 tree
2301 finish_id_expression (tree id_expression,
2302 tree decl,
2303 tree scope,
2304 cp_id_kind *idk,
2305 tree *qualifying_class,
2306 bool integral_constant_expression_p,
2307 bool allow_non_integral_constant_expression_p,
2308 bool *non_integral_constant_expression_p,
2309 const char **error_msg)
2311 /* Initialize the output parameters. */
2312 *idk = CP_ID_KIND_NONE;
2313 *error_msg = NULL;
2315 if (id_expression == error_mark_node)
2316 return error_mark_node;
2317 /* If we have a template-id, then no further lookup is
2318 required. If the template-id was for a template-class, we
2319 will sometimes have a TYPE_DECL at this point. */
2320 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2321 || TREE_CODE (decl) == TYPE_DECL)
2323 /* Look up the name. */
2324 else
2326 if (decl == error_mark_node)
2328 /* Name lookup failed. */
2329 if (scope
2330 && (!TYPE_P (scope)
2331 || (!dependent_type_p (scope)
2332 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2333 && IDENTIFIER_TYPENAME_P (id_expression)
2334 && dependent_type_p (TREE_TYPE (id_expression))))))
2336 /* If the qualifying type is non-dependent (and the name
2337 does not name a conversion operator to a dependent
2338 type), issue an error. */
2339 qualified_name_lookup_error (scope, id_expression);
2340 return error_mark_node;
2342 else if (!scope)
2344 /* It may be resolved via Koenig lookup. */
2345 *idk = CP_ID_KIND_UNQUALIFIED;
2346 return id_expression;
2348 else
2349 decl = id_expression;
2351 /* If DECL is a variable that would be out of scope under
2352 ANSI/ISO rules, but in scope in the ARM, name lookup
2353 will succeed. Issue a diagnostic here. */
2354 else
2355 decl = check_for_out_of_scope_variable (decl);
2357 /* Remember that the name was used in the definition of
2358 the current class so that we can check later to see if
2359 the meaning would have been different after the class
2360 was entirely defined. */
2361 if (!scope && decl != error_mark_node)
2362 maybe_note_name_used_in_class (id_expression, decl);
2365 /* If we didn't find anything, or what we found was a type,
2366 then this wasn't really an id-expression. */
2367 if (TREE_CODE (decl) == TEMPLATE_DECL
2368 && !DECL_FUNCTION_TEMPLATE_P (decl))
2370 *error_msg = "missing template arguments";
2371 return error_mark_node;
2373 else if (TREE_CODE (decl) == TYPE_DECL
2374 || TREE_CODE (decl) == NAMESPACE_DECL)
2376 *error_msg = "expected primary-expression";
2377 return error_mark_node;
2380 /* If the name resolved to a template parameter, there is no
2381 need to look it up again later. */
2382 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
2383 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2385 *idk = CP_ID_KIND_NONE;
2386 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
2387 decl = TEMPLATE_PARM_DECL (decl);
2388 if (integral_constant_expression_p
2389 && !dependent_type_p (TREE_TYPE (decl))
2390 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2392 if (!allow_non_integral_constant_expression_p)
2393 error ("template parameter `%D' of type `%T' is not allowed in "
2394 "an integral constant expression because it is not of "
2395 "integral or enumeration type", decl, TREE_TYPE (decl));
2396 *non_integral_constant_expression_p = true;
2398 return DECL_INITIAL (decl);
2400 /* Similarly, we resolve enumeration constants to their
2401 underlying values. */
2402 else if (TREE_CODE (decl) == CONST_DECL)
2404 *idk = CP_ID_KIND_NONE;
2405 if (!processing_template_decl)
2406 return DECL_INITIAL (decl);
2407 return decl;
2409 else
2411 bool dependent_p;
2413 /* If the declaration was explicitly qualified indicate
2414 that. The semantics of `A::f(3)' are different than
2415 `f(3)' if `f' is virtual. */
2416 *idk = (scope
2417 ? CP_ID_KIND_QUALIFIED
2418 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2419 ? CP_ID_KIND_TEMPLATE_ID
2420 : CP_ID_KIND_UNQUALIFIED));
2423 /* [temp.dep.expr]
2425 An id-expression is type-dependent if it contains an
2426 identifier that was declared with a dependent type.
2428 The standard is not very specific about an id-expression that
2429 names a set of overloaded functions. What if some of them
2430 have dependent types and some of them do not? Presumably,
2431 such a name should be treated as a dependent name. */
2432 /* Assume the name is not dependent. */
2433 dependent_p = false;
2434 if (!processing_template_decl)
2435 /* No names are dependent outside a template. */
2437 /* A template-id where the name of the template was not resolved
2438 is definitely dependent. */
2439 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2440 && (TREE_CODE (TREE_OPERAND (decl, 0))
2441 == IDENTIFIER_NODE))
2442 dependent_p = true;
2443 /* For anything except an overloaded function, just check its
2444 type. */
2445 else if (!is_overloaded_fn (decl))
2446 dependent_p
2447 = dependent_type_p (TREE_TYPE (decl));
2448 /* For a set of overloaded functions, check each of the
2449 functions. */
2450 else
2452 tree fns = decl;
2454 if (BASELINK_P (fns))
2455 fns = BASELINK_FUNCTIONS (fns);
2457 /* For a template-id, check to see if the template
2458 arguments are dependent. */
2459 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2461 tree args = TREE_OPERAND (fns, 1);
2462 dependent_p = any_dependent_template_arguments_p (args);
2463 /* The functions are those referred to by the
2464 template-id. */
2465 fns = TREE_OPERAND (fns, 0);
2468 /* If there are no dependent template arguments, go through
2469 the overloaded functions. */
2470 while (fns && !dependent_p)
2472 tree fn = OVL_CURRENT (fns);
2474 /* Member functions of dependent classes are
2475 dependent. */
2476 if (TREE_CODE (fn) == FUNCTION_DECL
2477 && type_dependent_expression_p (fn))
2478 dependent_p = true;
2479 else if (TREE_CODE (fn) == TEMPLATE_DECL
2480 && dependent_template_p (fn))
2481 dependent_p = true;
2483 fns = OVL_NEXT (fns);
2487 /* If the name was dependent on a template parameter, we will
2488 resolve the name at instantiation time. */
2489 if (dependent_p)
2491 /* Create a SCOPE_REF for qualified names, if the scope is
2492 dependent. */
2493 if (scope)
2495 if (TYPE_P (scope))
2496 *qualifying_class = scope;
2497 /* Since this name was dependent, the expression isn't
2498 constant -- yet. No error is issued because it might
2499 be constant when things are instantiated. */
2500 if (integral_constant_expression_p)
2501 *non_integral_constant_expression_p = true;
2502 if (TYPE_P (scope) && dependent_type_p (scope))
2503 return build_nt (SCOPE_REF, scope, id_expression);
2504 else if (TYPE_P (scope) && DECL_P (decl))
2505 return build (SCOPE_REF, TREE_TYPE (decl), scope,
2506 id_expression);
2507 else
2508 return decl;
2510 /* A TEMPLATE_ID already contains all the information we
2511 need. */
2512 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2513 return id_expression;
2514 /* Since this name was dependent, the expression isn't
2515 constant -- yet. No error is issued because it might be
2516 constant when things are instantiated. */
2517 if (integral_constant_expression_p)
2518 *non_integral_constant_expression_p = true;
2519 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
2520 /* If we found a variable, then name lookup during the
2521 instantiation will always resolve to the same VAR_DECL
2522 (or an instantiation thereof). */
2523 if (TREE_CODE (decl) == VAR_DECL
2524 || TREE_CODE (decl) == PARM_DECL)
2525 return decl;
2526 return id_expression;
2529 /* Only certain kinds of names are allowed in constant
2530 expression. Enumerators and template parameters
2531 have already been handled above. */
2532 if (integral_constant_expression_p)
2534 /* Const variables or static data members of integral or
2535 enumeration types initialized with constant expressions
2536 are OK. */
2537 if (TREE_CODE (decl) == VAR_DECL
2538 && CP_TYPE_CONST_P (TREE_TYPE (decl))
2539 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2540 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
2542 else
2544 if (!allow_non_integral_constant_expression_p)
2546 error ("`%D' cannot appear in a constant-expression", decl);
2547 return error_mark_node;
2549 *non_integral_constant_expression_p = true;
2553 if (TREE_CODE (decl) == NAMESPACE_DECL)
2555 error ("use of namespace `%D' as expression", decl);
2556 return error_mark_node;
2558 else if (DECL_CLASS_TEMPLATE_P (decl))
2560 error ("use of class template `%T' as expression", decl);
2561 return error_mark_node;
2563 else if (TREE_CODE (decl) == TREE_LIST)
2565 /* Ambiguous reference to base members. */
2566 error ("request for member `%D' is ambiguous in "
2567 "multiple inheritance lattice", id_expression);
2568 print_candidates (decl);
2569 return error_mark_node;
2572 /* Mark variable-like entities as used. Functions are similarly
2573 marked either below or after overload resolution. */
2574 if (TREE_CODE (decl) == VAR_DECL
2575 || TREE_CODE (decl) == PARM_DECL
2576 || TREE_CODE (decl) == RESULT_DECL)
2577 mark_used (decl);
2579 if (scope)
2581 decl = (adjust_result_of_qualified_name_lookup
2582 (decl, scope, current_class_type));
2584 if (TREE_CODE (decl) == FUNCTION_DECL)
2585 mark_used (decl);
2587 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2588 *qualifying_class = scope;
2589 else if (!processing_template_decl)
2590 decl = convert_from_reference (decl);
2591 else if (TYPE_P (scope))
2592 decl = build (SCOPE_REF, TREE_TYPE (decl), scope, decl);
2594 else if (TREE_CODE (decl) == FIELD_DECL)
2595 decl = finish_non_static_data_member (decl, current_class_ref,
2596 /*qualifying_scope=*/NULL_TREE);
2597 else if (is_overloaded_fn (decl))
2599 tree first_fn = OVL_CURRENT (decl);
2601 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
2602 first_fn = DECL_TEMPLATE_RESULT (first_fn);
2604 if (!really_overloaded_fn (decl))
2605 mark_used (first_fn);
2607 if (TREE_CODE (first_fn) == FUNCTION_DECL
2608 && DECL_FUNCTION_MEMBER_P (first_fn))
2610 /* A set of member functions. */
2611 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
2612 return finish_class_member_access_expr (decl, id_expression);
2615 else
2617 if (TREE_CODE (decl) == VAR_DECL
2618 || TREE_CODE (decl) == PARM_DECL
2619 || TREE_CODE (decl) == RESULT_DECL)
2621 tree context = decl_function_context (decl);
2623 if (context != NULL_TREE && context != current_function_decl
2624 && ! TREE_STATIC (decl))
2626 error ("use of %s from containing function",
2627 (TREE_CODE (decl) == VAR_DECL
2628 ? "`auto' variable" : "parameter"));
2629 cp_error_at (" `%#D' declared here", decl);
2630 return error_mark_node;
2634 if (DECL_P (decl) && DECL_NONLOCAL (decl)
2635 && DECL_CLASS_SCOPE_P (decl)
2636 && DECL_CONTEXT (decl) != current_class_type)
2638 tree path;
2640 path = currently_open_derived_class (DECL_CONTEXT (decl));
2641 perform_or_defer_access_check (TYPE_BINFO (path), decl);
2644 if (! processing_template_decl)
2645 decl = convert_from_reference (decl);
2648 /* Resolve references to variables of anonymous unions
2649 into COMPONENT_REFs. */
2650 if (TREE_CODE (decl) == ALIAS_DECL)
2651 decl = unshare_expr (DECL_INITIAL (decl));
2654 if (TREE_DEPRECATED (decl))
2655 warn_deprecated_use (decl);
2657 return decl;
2660 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2661 use as a type-specifier. */
2663 tree
2664 finish_typeof (tree expr)
2666 tree type;
2668 if (type_dependent_expression_p (expr))
2670 type = make_aggr_type (TYPEOF_TYPE);
2671 TYPEOF_TYPE_EXPR (type) = expr;
2673 return type;
2676 type = TREE_TYPE (expr);
2678 if (!type || type == unknown_type_node)
2680 error ("type of `%E' is unknown", expr);
2681 return error_mark_node;
2684 return type;
2687 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2688 with equivalent CALL_EXPRs. */
2690 static tree
2691 simplify_aggr_init_exprs_r (tree* tp,
2692 int* walk_subtrees,
2693 void* data ATTRIBUTE_UNUSED)
2695 /* We don't need to walk into types; there's nothing in a type that
2696 needs simplification. (And, furthermore, there are places we
2697 actively don't want to go. For example, we don't want to wander
2698 into the default arguments for a FUNCTION_DECL that appears in a
2699 CALL_EXPR.) */
2700 if (TYPE_P (*tp))
2702 *walk_subtrees = 0;
2703 return NULL_TREE;
2705 /* Only AGGR_INIT_EXPRs are interesting. */
2706 else if (TREE_CODE (*tp) != AGGR_INIT_EXPR)
2707 return NULL_TREE;
2709 simplify_aggr_init_expr (tp);
2711 /* Keep iterating. */
2712 return NULL_TREE;
2715 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
2716 function is broken out from the above for the benefit of the tree-ssa
2717 project. */
2719 void
2720 simplify_aggr_init_expr (tree *tp)
2722 tree aggr_init_expr = *tp;
2724 /* Form an appropriate CALL_EXPR. */
2725 tree fn = TREE_OPERAND (aggr_init_expr, 0);
2726 tree args = TREE_OPERAND (aggr_init_expr, 1);
2727 tree slot = TREE_OPERAND (aggr_init_expr, 2);
2728 tree type = TREE_TYPE (slot);
2730 tree call_expr;
2731 enum style_t { ctor, arg, pcc } style;
2733 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2734 style = ctor;
2735 #ifdef PCC_STATIC_STRUCT_RETURN
2736 else if (1)
2737 style = pcc;
2738 #endif
2739 else if (TREE_ADDRESSABLE (type))
2740 style = arg;
2741 else
2742 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2743 handling. See build_cplus_new. */
2744 abort ();
2746 if (style == ctor || style == arg)
2748 /* Pass the address of the slot. If this is a constructor, we
2749 replace the first argument; otherwise, we tack on a new one. */
2750 tree addr;
2752 if (style == ctor)
2753 args = TREE_CHAIN (args);
2755 cxx_mark_addressable (slot);
2756 addr = build1 (ADDR_EXPR, build_pointer_type (type), slot);
2757 if (style == arg)
2759 /* The return type might have different cv-quals from the slot. */
2760 tree fntype = TREE_TYPE (TREE_TYPE (fn));
2761 #ifdef ENABLE_CHECKING
2762 if (TREE_CODE (fntype) != FUNCTION_TYPE
2763 && TREE_CODE (fntype) != METHOD_TYPE)
2764 abort ();
2765 #endif
2766 addr = convert (build_pointer_type (TREE_TYPE (fntype)), addr);
2769 args = tree_cons (NULL_TREE, addr, args);
2772 call_expr = build (CALL_EXPR,
2773 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2774 fn, args, NULL_TREE);
2776 if (style == arg)
2777 /* Tell the backend that we've added our return slot to the argument
2778 list. */
2779 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2780 else if (style == pcc)
2782 /* If we're using the non-reentrant PCC calling convention, then we
2783 need to copy the returned value out of the static buffer into the
2784 SLOT. */
2785 push_deferring_access_checks (dk_no_check);
2786 call_expr = build_aggr_init (slot, call_expr,
2787 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2788 pop_deferring_access_checks ();
2791 *tp = call_expr;
2794 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2796 static void
2797 emit_associated_thunks (tree fn)
2799 /* When we use vcall offsets, we emit thunks with the virtual
2800 functions to which they thunk. The whole point of vcall offsets
2801 is so that you can know statically the entire set of thunks that
2802 will ever be needed for a given virtual function, thereby
2803 enabling you to output all the thunks with the function itself. */
2804 if (DECL_VIRTUAL_P (fn))
2806 tree thunk;
2808 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2810 if (!THUNK_ALIAS (thunk))
2812 use_thunk (thunk, /*emit_p=*/1);
2813 if (DECL_RESULT_THUNK_P (thunk))
2815 tree probe;
2817 for (probe = DECL_THUNKS (thunk);
2818 probe; probe = TREE_CHAIN (probe))
2819 use_thunk (probe, /*emit_p=*/1);
2822 else
2823 my_friendly_assert (!DECL_THUNKS (thunk), 20031023);
2828 /* Generate RTL for FN. */
2830 void
2831 expand_body (tree fn)
2833 tree saved_function;
2835 /* Compute the appropriate object-file linkage for inline
2836 functions. */
2837 if (DECL_DECLARED_INLINE_P (fn))
2838 import_export_decl (fn);
2840 /* If FN is external, then there's no point in generating RTL for
2841 it. This situation can arise with an inline function under
2842 `-fexternal-templates'; we instantiate the function, even though
2843 we're not planning on emitting it, in case we get a chance to
2844 inline it. */
2845 if (DECL_EXTERNAL (fn))
2846 return;
2848 /* ??? When is this needed? */
2849 saved_function = current_function_decl;
2851 /* Emit any thunks that should be emitted at the same time as FN. */
2852 emit_associated_thunks (fn);
2854 tree_rest_of_compilation (fn, function_depth > 1);
2856 current_function_decl = saved_function;
2858 extract_interface_info ();
2860 /* If this function is marked with the constructor attribute, add it
2861 to the list of functions to be called along with constructors
2862 from static duration objects. */
2863 if (DECL_STATIC_CONSTRUCTOR (fn))
2864 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2866 /* If this function is marked with the destructor attribute, add it
2867 to the list of functions to be called along with destructors from
2868 static duration objects. */
2869 if (DECL_STATIC_DESTRUCTOR (fn))
2870 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2872 if (DECL_CLONED_FUNCTION_P (fn))
2874 /* If this is a clone, go through the other clones now and mark
2875 their parameters used. We have to do that here, as we don't
2876 know whether any particular clone will be expanded, and
2877 therefore cannot pick one arbitrarily. */
2878 tree probe;
2880 for (probe = TREE_CHAIN (DECL_CLONED_FUNCTION (fn));
2881 probe && DECL_CLONED_FUNCTION_P (probe);
2882 probe = TREE_CHAIN (probe))
2884 tree parms;
2886 for (parms = DECL_ARGUMENTS (probe);
2887 parms; parms = TREE_CHAIN (parms))
2888 TREE_USED (parms) = 1;
2893 /* Generate RTL for FN. */
2895 void
2896 expand_or_defer_fn (tree fn)
2898 /* When the parser calls us after finishing the body of a template
2899 function, we don't really want to expand the body. */
2900 if (processing_template_decl)
2902 /* Normally, collection only occurs in rest_of_compilation. So,
2903 if we don't collect here, we never collect junk generated
2904 during the processing of templates until we hit a
2905 non-template function. */
2906 ggc_collect ();
2907 return;
2910 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2911 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2912 simplify_aggr_init_exprs_r,
2913 NULL);
2915 /* If this is a constructor or destructor body, we have to clone
2916 it. */
2917 if (maybe_clone_body (fn))
2919 /* We don't want to process FN again, so pretend we've written
2920 it out, even though we haven't. */
2921 TREE_ASM_WRITTEN (fn) = 1;
2922 return;
2925 /* There's no reason to do any of the work here if we're only doing
2926 semantic analysis; this code just generates RTL. */
2927 if (flag_syntax_only)
2928 return;
2930 /* Compute the appropriate object-file linkage for inline functions. */
2931 if (DECL_DECLARED_INLINE_P (fn))
2932 import_export_decl (fn);
2934 function_depth++;
2936 /* Expand or defer, at the whim of the compilation unit manager. */
2937 cgraph_finalize_function (fn, function_depth > 1);
2939 function_depth--;
2942 struct nrv_data
2944 tree var;
2945 tree result;
2946 htab_t visited;
2949 /* Helper function for walk_tree, used by finalize_nrv below. */
2951 static tree
2952 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
2954 struct nrv_data *dp = (struct nrv_data *)data;
2955 void **slot;
2957 /* No need to walk into types. There wouldn't be any need to walk into
2958 non-statements, except that we have to consider STMT_EXPRs. */
2959 if (TYPE_P (*tp))
2960 *walk_subtrees = 0;
2961 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
2962 but differs from using NULL_TREE in that it indicates that we care
2963 about the value of the RESULT_DECL. */
2964 else if (TREE_CODE (*tp) == RETURN_STMT)
2965 RETURN_STMT_EXPR (*tp) = dp->result;
2966 /* Change all cleanups for the NRV to only run when an exception is
2967 thrown. */
2968 else if (TREE_CODE (*tp) == CLEANUP_STMT
2969 && CLEANUP_DECL (*tp) == dp->var)
2970 CLEANUP_EH_ONLY (*tp) = 1;
2971 /* Replace the DECL_STMT for the NRV with an initialization of the
2972 RESULT_DECL, if needed. */
2973 else if (TREE_CODE (*tp) == DECL_STMT
2974 && DECL_STMT_DECL (*tp) == dp->var)
2976 tree init;
2977 if (DECL_INITIAL (dp->var)
2978 && DECL_INITIAL (dp->var) != error_mark_node)
2980 init = build (INIT_EXPR, void_type_node, dp->result,
2981 DECL_INITIAL (dp->var));
2982 DECL_INITIAL (dp->var) = error_mark_node;
2984 else
2985 init = NULL_TREE;
2986 init = build_stmt (EXPR_STMT, init);
2987 SET_EXPR_LOCUS (init, EXPR_LOCUS (*tp));
2988 TREE_CHAIN (init) = TREE_CHAIN (*tp);
2989 *tp = init;
2991 /* And replace all uses of the NRV with the RESULT_DECL. */
2992 else if (*tp == dp->var)
2993 *tp = dp->result;
2995 /* Avoid walking into the same tree more than once. Unfortunately, we
2996 can't just use walk_tree_without duplicates because it would only call
2997 us for the first occurrence of dp->var in the function body. */
2998 slot = htab_find_slot (dp->visited, *tp, INSERT);
2999 if (*slot)
3000 *walk_subtrees = 0;
3001 else
3002 *slot = *tp;
3004 /* Keep iterating. */
3005 return NULL_TREE;
3008 /* Called from finish_function to implement the named return value
3009 optimization by overriding all the RETURN_STMTs and pertinent
3010 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
3011 RESULT_DECL for the function. */
3013 void
3014 finalize_nrv (tree *tp, tree var, tree result)
3016 struct nrv_data data;
3018 /* Copy debugging information from VAR to RESULT. */
3019 DECL_NAME (result) = DECL_NAME (var);
3020 DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (var);
3021 DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (var);
3022 /* Don't forget that we take its address. */
3023 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
3025 data.var = var;
3026 data.result = result;
3027 data.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3028 walk_tree (tp, finalize_nrv_r, &data, 0);
3029 htab_delete (data.visited);
3032 /* Perform initialization related to this module. */
3034 void
3035 init_cp_semantics (void)
3039 #include "gt-cp-semantics.h"