]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/semantics.c
Inet6Address.java (isAnyLocalAddress): Don't use "==" on arrays.
[thirdparty/gcc.git] / gcc / cp / semantics.c
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.
5
6 Copyright (C) 1998, 1999, 2000, 2001, 2002,
7 2003 Free Software Foundation, Inc.
8 Written by Mark Mitchell (mmitchell@usa.net) based on code found
9 formerly in parse.y and pt.c.
10
11 This file is part of GCC.
12
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.
17
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.
22
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. */
27
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 "except.h"
36 #include "lex.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "rtl.h"
40 #include "expr.h"
41 #include "output.h"
42 #include "timevar.h"
43 #include "debug.h"
44
45 /* There routines provide a modular interface to perform many parsing
46 operations. They may therefore be used during actual parsing, or
47 during template instantiation, which may be regarded as a
48 degenerate form of parsing. Since the current g++ parser is
49 lacking in several respects, and will be reimplemented, we are
50 attempting to move most code that is not directly related to
51 parsing into this file; that will make implementing the new parser
52 much easier since it will be able to make use of these routines. */
53
54 static tree maybe_convert_cond PARAMS ((tree));
55 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
56 static void emit_associated_thunks PARAMS ((tree));
57 static void genrtl_try_block PARAMS ((tree));
58 static void genrtl_eh_spec_block PARAMS ((tree));
59 static void genrtl_handler PARAMS ((tree));
60 static void cp_expand_stmt PARAMS ((tree));
61 static void genrtl_start_function PARAMS ((tree));
62 static void genrtl_finish_function PARAMS ((tree));
63 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
64
65 /* Finish processing the COND, the SUBSTMT condition for STMT. */
66
67 #define FINISH_COND(COND, STMT, SUBSTMT) \
68 do { \
69 if (last_tree != (STMT)) \
70 { \
71 RECHAIN_STMTS (STMT, SUBSTMT); \
72 if (!processing_template_decl) \
73 { \
74 (COND) = build_tree_list (SUBSTMT, COND); \
75 (SUBSTMT) = (COND); \
76 } \
77 } \
78 else \
79 (SUBSTMT) = (COND); \
80 } while (0)
81
82 /* Deferred Access Checking Overview
83 ---------------------------------
84
85 Most C++ expressions and declarations require access checking
86 to be performed during parsing. However, in several cases,
87 this has to be treated differently.
88
89 For member declarations, access checking has to be deferred
90 until more information about the declaration is known. For
91 example:
92
93 class A {
94 typedef int X;
95 public:
96 X f();
97 };
98
99 A::X A::f();
100 A::X g();
101
102 When we are parsing the function return type `A::X', we don't
103 really know if this is allowed until we parse the function name.
104
105 Furthermore, some contexts require that access checking is
106 never performed at all. These include class heads, and template
107 instantiations.
108
109 Typical use of access checking functions is described here:
110
111 1. When we enter a context that requires certain access checking
112 mode, the function `push_deferring_access_checks' is called with
113 DEFERRING argument specifying the desired mode. Access checking
114 may be performed immediately (dk_no_deferred), deferred
115 (dk_deferred), or not performed (dk_no_check).
116
117 2. When a declaration such as a type, or a variable, is encountered,
118 the function `perform_or_defer_access_check' is called. It
119 maintains a TREE_LIST of all deferred checks.
120
121 3. The global `current_class_type' or `current_function_decl' is then
122 setup by the parser. `enforce_access' relies on these information
123 to check access.
124
125 4. Upon exiting the context mentioned in step 1,
126 `perform_deferred_access_checks' is called to check all declaration
127 stored in the TREE_LIST. `pop_deferring_access_checks' is then
128 called to restore the previous access checking mode.
129
130 In case of parsing error, we simply call `pop_deferring_access_checks'
131 without `perform_deferred_access_checks'. */
132
133 /* Data for deferred access checking. */
134 static GTY(()) deferred_access *deferred_access_stack;
135 static GTY(()) deferred_access *deferred_access_free_list;
136
137 /* Save the current deferred access states and start deferred
138 access checking iff DEFER_P is true. */
139
140 void push_deferring_access_checks (deferring_kind deferring)
141 {
142 deferred_access *d;
143
144 /* For context like template instantiation, access checking
145 disabling applies to all nested context. */
146 if (deferred_access_stack
147 && deferred_access_stack->deferring_access_checks_kind == dk_no_check)
148 deferring = dk_no_check;
149
150 /* Recycle previously used free store if available. */
151 if (deferred_access_free_list)
152 {
153 d = deferred_access_free_list;
154 deferred_access_free_list = d->next;
155 }
156 else
157 d = (deferred_access *) ggc_alloc (sizeof (deferred_access));
158
159 d->next = deferred_access_stack;
160 d->deferred_access_checks = NULL_TREE;
161 d->deferring_access_checks_kind = deferring;
162 deferred_access_stack = d;
163 }
164
165 /* Resume deferring access checks again after we stopped doing
166 this previously. */
167
168 void resume_deferring_access_checks (void)
169 {
170 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
171 deferred_access_stack->deferring_access_checks_kind = dk_deferred;
172 }
173
174 /* Stop deferring access checks. */
175
176 void stop_deferring_access_checks (void)
177 {
178 if (deferred_access_stack->deferring_access_checks_kind == dk_deferred)
179 deferred_access_stack->deferring_access_checks_kind = dk_no_deferred;
180 }
181
182 /* Discard the current deferred access checks and restore the
183 previous states. */
184
185 void pop_deferring_access_checks (void)
186 {
187 deferred_access *d = deferred_access_stack;
188 deferred_access_stack = d->next;
189
190 /* Remove references to access checks TREE_LIST. */
191 d->deferred_access_checks = NULL_TREE;
192
193 /* Store in free list for later use. */
194 d->next = deferred_access_free_list;
195 deferred_access_free_list = d;
196 }
197
198 /* Returns a TREE_LIST representing the deferred checks.
199 The TREE_PURPOSE of each node is the type through which the
200 access occurred; the TREE_VALUE is the declaration named.
201 */
202
203 tree get_deferred_access_checks (void)
204 {
205 return deferred_access_stack->deferred_access_checks;
206 }
207
208 /* Take current deferred checks and combine with the
209 previous states if we also defer checks previously.
210 Otherwise perform checks now. */
211
212 void pop_to_parent_deferring_access_checks (void)
213 {
214 tree deferred_check = get_deferred_access_checks ();
215 deferred_access *d1 = deferred_access_stack;
216 deferred_access *d2 = deferred_access_stack->next;
217 deferred_access *d3 = deferred_access_stack->next->next;
218
219 /* Temporary swap the order of the top two states, just to make
220 sure the garbage collector will not reclaim the memory during
221 processing below. */
222 deferred_access_stack = d2;
223 d2->next = d1;
224 d1->next = d3;
225
226 for ( ; deferred_check; deferred_check = TREE_CHAIN (deferred_check))
227 /* Perform deferred check if required. */
228 perform_or_defer_access_check (TREE_PURPOSE (deferred_check),
229 TREE_VALUE (deferred_check));
230
231 deferred_access_stack = d1;
232 d1->next = d2;
233 d2->next = d3;
234 pop_deferring_access_checks ();
235 }
236
237 /* Perform the deferred access checks.
238
239 After performing the checks, we still have to keep the list
240 `deferred_access_stack->deferred_access_checks' since we may want
241 to check access for them again later in a different context.
242 For example:
243
244 class A {
245 typedef int X;
246 static X a;
247 };
248 A::X A::a, x; // No error for `A::a', error for `x'
249
250 We have to perform deferred access of `A::X', first with `A::a',
251 next with `x'. */
252
253 void perform_deferred_access_checks (void)
254 {
255 tree deferred_check;
256 for (deferred_check = deferred_access_stack->deferred_access_checks;
257 deferred_check;
258 deferred_check = TREE_CHAIN (deferred_check))
259 /* Check access. */
260 enforce_access (TREE_PURPOSE (deferred_check),
261 TREE_VALUE (deferred_check));
262 }
263
264 /* Defer checking the accessibility of DECL, when looked up in
265 CLASS_TYPE. */
266
267 void perform_or_defer_access_check (tree class_type, tree decl)
268 {
269 tree check;
270
271 /* If we are not supposed to defer access checks, just check now. */
272 if (deferred_access_stack->deferring_access_checks_kind == dk_no_deferred)
273 {
274 enforce_access (class_type, decl);
275 return;
276 }
277 /* Exit if we are in a context that no access checking is performed. */
278 else if (deferred_access_stack->deferring_access_checks_kind == dk_no_check)
279 return;
280
281 /* See if we are already going to perform this check. */
282 for (check = deferred_access_stack->deferred_access_checks;
283 check;
284 check = TREE_CHAIN (check))
285 if (TREE_VALUE (check) == decl
286 && TYPE_P (TREE_PURPOSE (check))
287 && same_type_p (TREE_PURPOSE (check), class_type))
288 return;
289 /* If not, record the check. */
290 deferred_access_stack->deferred_access_checks
291 = tree_cons (class_type, decl,
292 deferred_access_stack->deferred_access_checks);
293 }
294
295 /* Returns nonzero if the current statement is a full expression,
296 i.e. temporaries created during that statement should be destroyed
297 at the end of the statement. */
298
299 int
300 stmts_are_full_exprs_p ()
301 {
302 return current_stmt_tree ()->stmts_are_full_exprs_p;
303 }
304
305 /* Returns the stmt_tree (if any) to which statements are currently
306 being added. If there is no active statement-tree, NULL is
307 returned. */
308
309 stmt_tree
310 current_stmt_tree ()
311 {
312 return (cfun
313 ? &cfun->language->base.x_stmt_tree
314 : &scope_chain->x_stmt_tree);
315 }
316
317 /* Nonzero if TYPE is an anonymous union or struct type. We have to use a
318 flag for this because "A union for which objects or pointers are
319 declared is not an anonymous union" [class.union]. */
320
321 int
322 anon_aggr_type_p (node)
323 tree node;
324 {
325 return ANON_AGGR_TYPE_P (node);
326 }
327
328 /* Finish a scope. */
329
330 tree
331 do_poplevel ()
332 {
333 tree block = NULL_TREE;
334
335 if (stmts_are_full_exprs_p ())
336 {
337 tree scope_stmts = NULL_TREE;
338
339 block = poplevel (kept_level_p (), 1, 0);
340 if (!processing_template_decl)
341 {
342 /* This needs to come after the poplevel so that partial scopes
343 are properly nested. */
344 scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
345 if (block)
346 {
347 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
348 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
349 }
350 }
351 }
352
353 return block;
354 }
355
356 /* Begin a new scope. */
357
358 void
359 do_pushlevel (scope_kind sk)
360 {
361 if (stmts_are_full_exprs_p ())
362 {
363 if (!processing_template_decl)
364 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
365 begin_scope (sk);
366 }
367 }
368
369 /* Finish a goto-statement. */
370
371 tree
372 finish_goto_stmt (destination)
373 tree destination;
374 {
375 if (TREE_CODE (destination) == IDENTIFIER_NODE)
376 destination = lookup_label (destination);
377
378 /* We warn about unused labels with -Wunused. That means we have to
379 mark the used labels as used. */
380 if (TREE_CODE (destination) == LABEL_DECL)
381 TREE_USED (destination) = 1;
382
383 if (TREE_CODE (destination) != LABEL_DECL)
384 /* We don't inline calls to functions with computed gotos.
385 Those functions are typically up to some funny business,
386 and may be depending on the labels being at particular
387 addresses, or some such. */
388 DECL_UNINLINABLE (current_function_decl) = 1;
389
390 check_goto (destination);
391
392 return add_stmt (build_stmt (GOTO_STMT, destination));
393 }
394
395 /* COND is the condition-expression for an if, while, etc.,
396 statement. Convert it to a boolean value, if appropriate. */
397
398 tree
399 maybe_convert_cond (cond)
400 tree cond;
401 {
402 /* Empty conditions remain empty. */
403 if (!cond)
404 return NULL_TREE;
405
406 /* Wait until we instantiate templates before doing conversion. */
407 if (processing_template_decl)
408 return cond;
409
410 /* Do the conversion. */
411 cond = convert_from_reference (cond);
412 return condition_conversion (cond);
413 }
414
415 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
416
417 tree
418 finish_expr_stmt (expr)
419 tree expr;
420 {
421 tree r = NULL_TREE;
422 tree expr_type = NULL_TREE;;
423
424 if (expr != NULL_TREE)
425 {
426 if (!processing_template_decl
427 && !(stmts_are_full_exprs_p ())
428 && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
429 && lvalue_p (expr))
430 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
431 expr = default_conversion (expr);
432
433 /* Remember the type of the expression. */
434 expr_type = TREE_TYPE (expr);
435
436 if (stmts_are_full_exprs_p ())
437 expr = convert_to_void (expr, "statement");
438
439 r = add_stmt (build_stmt (EXPR_STMT, expr));
440 }
441
442 finish_stmt ();
443
444 /* This was an expression-statement, so we save the type of the
445 expression. */
446 last_expr_type = expr_type;
447
448 return r;
449 }
450
451
452 /* Begin an if-statement. Returns a newly created IF_STMT if
453 appropriate. */
454
455 tree
456 begin_if_stmt ()
457 {
458 tree r;
459 do_pushlevel (sk_block);
460 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
461 add_stmt (r);
462 return r;
463 }
464
465 /* Process the COND of an if-statement, which may be given by
466 IF_STMT. */
467
468 void
469 finish_if_stmt_cond (cond, if_stmt)
470 tree cond;
471 tree if_stmt;
472 {
473 cond = maybe_convert_cond (cond);
474 FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
475 }
476
477 /* Finish the then-clause of an if-statement, which may be given by
478 IF_STMT. */
479
480 tree
481 finish_then_clause (if_stmt)
482 tree if_stmt;
483 {
484 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
485 return if_stmt;
486 }
487
488 /* Begin the else-clause of an if-statement. */
489
490 void
491 begin_else_clause ()
492 {
493 }
494
495 /* Finish the else-clause of an if-statement, which may be given by
496 IF_STMT. */
497
498 void
499 finish_else_clause (if_stmt)
500 tree if_stmt;
501 {
502 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
503 }
504
505 /* Finish an if-statement. */
506
507 void
508 finish_if_stmt ()
509 {
510 finish_stmt ();
511 do_poplevel ();
512 }
513
514 /* Begin a while-statement. Returns a newly created WHILE_STMT if
515 appropriate. */
516
517 tree
518 begin_while_stmt ()
519 {
520 tree r;
521 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
522 add_stmt (r);
523 do_pushlevel (sk_block);
524 return r;
525 }
526
527 /* Process the COND of a while-statement, which may be given by
528 WHILE_STMT. */
529
530 void
531 finish_while_stmt_cond (cond, while_stmt)
532 tree cond;
533 tree while_stmt;
534 {
535 cond = maybe_convert_cond (cond);
536 if (processing_template_decl)
537 /* Don't mess with condition decls in a template. */
538 FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
539 else if (getdecls () == NULL_TREE)
540 /* It was a simple condition; install it. */
541 WHILE_COND (while_stmt) = cond;
542 else
543 {
544 /* If there was a declaration in the condition, we can't leave it
545 there; transform
546 while (A x = 42) { }
547 to
548 while (true) { A x = 42; if (!x) break; } */
549 tree if_stmt;
550 WHILE_COND (while_stmt) = boolean_true_node;
551
552 if_stmt = begin_if_stmt ();
553 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
554 finish_if_stmt_cond (cond, if_stmt);
555 finish_break_stmt ();
556 finish_then_clause (if_stmt);
557 finish_if_stmt ();
558 }
559 }
560
561 /* Finish a while-statement, which may be given by WHILE_STMT. */
562
563 void
564 finish_while_stmt (while_stmt)
565 tree while_stmt;
566 {
567 do_poplevel ();
568 RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
569 finish_stmt ();
570 }
571
572 /* Begin a do-statement. Returns a newly created DO_STMT if
573 appropriate. */
574
575 tree
576 begin_do_stmt ()
577 {
578 tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
579 add_stmt (r);
580 return r;
581 }
582
583 /* Finish the body of a do-statement, which may be given by DO_STMT. */
584
585 void
586 finish_do_body (do_stmt)
587 tree do_stmt;
588 {
589 RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
590 }
591
592 /* Finish a do-statement, which may be given by DO_STMT, and whose
593 COND is as indicated. */
594
595 void
596 finish_do_stmt (cond, do_stmt)
597 tree cond;
598 tree do_stmt;
599 {
600 cond = maybe_convert_cond (cond);
601 DO_COND (do_stmt) = cond;
602 finish_stmt ();
603 }
604
605 /* Finish a return-statement. The EXPRESSION returned, if any, is as
606 indicated. */
607
608 tree
609 finish_return_stmt (expr)
610 tree expr;
611 {
612 tree r;
613
614 expr = check_return_expr (expr);
615 if (!processing_template_decl)
616 {
617 if (DECL_DESTRUCTOR_P (current_function_decl))
618 {
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);
624 }
625 }
626 r = add_stmt (build_stmt (RETURN_STMT, expr));
627 finish_stmt ();
628
629 return r;
630 }
631
632 /* Begin a for-statement. Returns a new FOR_STMT if appropriate. */
633
634 tree
635 begin_for_stmt ()
636 {
637 tree r;
638
639 r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
640 NULL_TREE, NULL_TREE);
641 NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
642 if (NEW_FOR_SCOPE_P (r))
643 do_pushlevel (sk_for);
644 add_stmt (r);
645
646 return r;
647 }
648
649 /* Finish the for-init-statement of a for-statement, which may be
650 given by FOR_STMT. */
651
652 void
653 finish_for_init_stmt (for_stmt)
654 tree for_stmt;
655 {
656 if (last_tree != for_stmt)
657 RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
658 do_pushlevel (sk_block);
659 }
660
661 /* Finish the COND of a for-statement, which may be given by
662 FOR_STMT. */
663
664 void
665 finish_for_cond (cond, for_stmt)
666 tree cond;
667 tree for_stmt;
668 {
669 cond = maybe_convert_cond (cond);
670 if (processing_template_decl)
671 /* Don't mess with condition decls in a template. */
672 FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
673 else if (getdecls () == NULL_TREE)
674 /* It was a simple condition; install it. */
675 FOR_COND (for_stmt) = cond;
676 else
677 {
678 /* If there was a declaration in the condition, we can't leave it
679 there; transform
680 for (; A x = 42;) { }
681 to
682 for (;;) { A x = 42; if (!x) break; } */
683 tree if_stmt;
684 FOR_COND (for_stmt) = NULL_TREE;
685
686 if_stmt = begin_if_stmt ();
687 cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
688 finish_if_stmt_cond (cond, if_stmt);
689 finish_break_stmt ();
690 finish_then_clause (if_stmt);
691 finish_if_stmt ();
692 }
693 }
694
695 /* Finish the increment-EXPRESSION in a for-statement, which may be
696 given by FOR_STMT. */
697
698 void
699 finish_for_expr (expr, for_stmt)
700 tree expr;
701 tree for_stmt;
702 {
703 FOR_EXPR (for_stmt) = expr;
704 }
705
706 /* Finish the body of a for-statement, which may be given by
707 FOR_STMT. The increment-EXPR for the loop must be
708 provided. */
709
710 void
711 finish_for_stmt (for_stmt)
712 tree for_stmt;
713 {
714 /* Pop the scope for the body of the loop. */
715 do_poplevel ();
716 RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
717 if (NEW_FOR_SCOPE_P (for_stmt))
718 do_poplevel ();
719 finish_stmt ();
720 }
721
722 /* Finish a break-statement. */
723
724 tree
725 finish_break_stmt ()
726 {
727 return add_stmt (build_break_stmt ());
728 }
729
730 /* Finish a continue-statement. */
731
732 tree
733 finish_continue_stmt ()
734 {
735 return add_stmt (build_continue_stmt ());
736 }
737
738 /* Begin a switch-statement. Returns a new SWITCH_STMT if
739 appropriate. */
740
741 tree
742 begin_switch_stmt ()
743 {
744 tree r;
745 do_pushlevel (sk_block);
746 r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
747 add_stmt (r);
748 return r;
749 }
750
751 /* Finish the cond of a switch-statement. */
752
753 void
754 finish_switch_cond (cond, switch_stmt)
755 tree cond;
756 tree switch_stmt;
757 {
758 tree orig_type = NULL;
759 if (!processing_template_decl)
760 {
761 tree index;
762
763 /* Convert the condition to an integer or enumeration type. */
764 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
765 if (cond == NULL_TREE)
766 {
767 error ("switch quantity not an integer");
768 cond = error_mark_node;
769 }
770 orig_type = TREE_TYPE (cond);
771 if (cond != error_mark_node)
772 {
773 cond = default_conversion (cond);
774 cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
775 }
776
777 if (cond != error_mark_node)
778 {
779 index = get_unwidened (cond, NULL_TREE);
780 /* We can't strip a conversion from a signed type to an unsigned,
781 because if we did, int_fits_type_p would do the wrong thing
782 when checking case values for being in range,
783 and it's too hard to do the right thing. */
784 if (TREE_UNSIGNED (TREE_TYPE (cond))
785 == TREE_UNSIGNED (TREE_TYPE (index)))
786 cond = index;
787 }
788 }
789 FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
790 SWITCH_TYPE (switch_stmt) = orig_type;
791 push_switch (switch_stmt);
792 }
793
794 /* Finish the body of a switch-statement, which may be given by
795 SWITCH_STMT. The COND to switch on is indicated. */
796
797 void
798 finish_switch_stmt (switch_stmt)
799 tree switch_stmt;
800 {
801 RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
802 pop_switch ();
803 finish_stmt ();
804 do_poplevel ();
805 }
806
807 /* Generate the RTL for T, which is a TRY_BLOCK. */
808
809 static void
810 genrtl_try_block (t)
811 tree t;
812 {
813 if (CLEANUP_P (t))
814 {
815 expand_eh_region_start ();
816 expand_stmt (TRY_STMTS (t));
817 expand_eh_region_end_cleanup (TRY_HANDLERS (t));
818 }
819 else
820 {
821 if (!FN_TRY_BLOCK_P (t))
822 emit_line_note (input_filename, input_line);
823
824 expand_eh_region_start ();
825 expand_stmt (TRY_STMTS (t));
826
827 if (FN_TRY_BLOCK_P (t))
828 {
829 expand_start_all_catch ();
830 in_function_try_handler = 1;
831 expand_stmt (TRY_HANDLERS (t));
832 in_function_try_handler = 0;
833 expand_end_all_catch ();
834 }
835 else
836 {
837 expand_start_all_catch ();
838 expand_stmt (TRY_HANDLERS (t));
839 expand_end_all_catch ();
840 }
841 }
842 }
843
844 /* Generate the RTL for T, which is an EH_SPEC_BLOCK. */
845
846 static void
847 genrtl_eh_spec_block (t)
848 tree t;
849 {
850 expand_eh_region_start ();
851 expand_stmt (EH_SPEC_STMTS (t));
852 expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
853 build_call (call_unexpected_node,
854 tree_cons (NULL_TREE,
855 build_exc_ptr (),
856 NULL_TREE)));
857 }
858
859 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
860 appropriate. */
861
862 tree
863 begin_try_block ()
864 {
865 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
866 add_stmt (r);
867 return r;
868 }
869
870 /* Likewise, for a function-try-block. */
871
872 tree
873 begin_function_try_block ()
874 {
875 tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
876 FN_TRY_BLOCK_P (r) = 1;
877 add_stmt (r);
878 return r;
879 }
880
881 /* Finish a try-block, which may be given by TRY_BLOCK. */
882
883 void
884 finish_try_block (try_block)
885 tree try_block;
886 {
887 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
888 }
889
890 /* Finish the body of a cleanup try-block, which may be given by
891 TRY_BLOCK. */
892
893 void
894 finish_cleanup_try_block (try_block)
895 tree try_block;
896 {
897 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
898 }
899
900 /* Finish an implicitly generated try-block, with a cleanup is given
901 by CLEANUP. */
902
903 void
904 finish_cleanup (cleanup, try_block)
905 tree cleanup;
906 tree try_block;
907 {
908 TRY_HANDLERS (try_block) = cleanup;
909 CLEANUP_P (try_block) = 1;
910 }
911
912 /* Likewise, for a function-try-block. */
913
914 void
915 finish_function_try_block (try_block)
916 tree try_block;
917 {
918 if (TREE_CHAIN (try_block)
919 && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
920 {
921 /* Chain the compound statement after the CTOR_INITIALIZER. */
922 TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
923 /* And make the CTOR_INITIALIZER the body of the try-block. */
924 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
925 }
926 else
927 RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
928 in_function_try_handler = 1;
929 }
930
931 /* Finish a handler-sequence for a try-block, which may be given by
932 TRY_BLOCK. */
933
934 void
935 finish_handler_sequence (try_block)
936 tree try_block;
937 {
938 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
939 check_handlers (TRY_HANDLERS (try_block));
940 }
941
942 /* Likewise, for a function-try-block. */
943
944 void
945 finish_function_handler_sequence (try_block)
946 tree try_block;
947 {
948 in_function_try_handler = 0;
949 RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
950 check_handlers (TRY_HANDLERS (try_block));
951 }
952
953 /* Generate the RTL for T, which is a HANDLER. */
954
955 static void
956 genrtl_handler (t)
957 tree t;
958 {
959 genrtl_do_pushlevel ();
960 if (!processing_template_decl)
961 expand_start_catch (HANDLER_TYPE (t));
962 expand_stmt (HANDLER_BODY (t));
963 if (!processing_template_decl)
964 expand_end_catch ();
965 }
966
967 /* Begin a handler. Returns a HANDLER if appropriate. */
968
969 tree
970 begin_handler ()
971 {
972 tree r;
973 r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
974 add_stmt (r);
975 /* Create a binding level for the eh_info and the exception object
976 cleanup. */
977 do_pushlevel (sk_catch);
978 return r;
979 }
980
981 /* Finish the handler-parameters for a handler, which may be given by
982 HANDLER. DECL is the declaration for the catch parameter, or NULL
983 if this is a `catch (...)' clause. */
984
985 void
986 finish_handler_parms (decl, handler)
987 tree decl;
988 tree handler;
989 {
990 tree type = NULL_TREE;
991 if (processing_template_decl)
992 {
993 if (decl)
994 {
995 decl = pushdecl (decl);
996 decl = push_template_decl (decl);
997 add_decl_stmt (decl);
998 RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
999 type = TREE_TYPE (decl);
1000 }
1001 }
1002 else
1003 type = expand_start_catch_block (decl);
1004
1005 HANDLER_TYPE (handler) = type;
1006 }
1007
1008 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1009 the return value from the matching call to finish_handler_parms. */
1010
1011 void
1012 finish_handler (handler)
1013 tree handler;
1014 {
1015 if (!processing_template_decl)
1016 expand_end_catch_block ();
1017 do_poplevel ();
1018 RECHAIN_STMTS (handler, HANDLER_BODY (handler));
1019 }
1020
1021 /* Begin a compound-statement. If HAS_NO_SCOPE is nonzero, the
1022 compound-statement does not define a scope. Returns a new
1023 COMPOUND_STMT if appropriate. */
1024
1025 tree
1026 begin_compound_stmt (has_no_scope)
1027 int has_no_scope;
1028 {
1029 tree r;
1030 int is_try = 0;
1031
1032 r = build_stmt (COMPOUND_STMT, NULL_TREE);
1033
1034 if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
1035 is_try = 1;
1036
1037 add_stmt (r);
1038 if (has_no_scope)
1039 COMPOUND_STMT_NO_SCOPE (r) = 1;
1040
1041 last_expr_type = NULL_TREE;
1042
1043 if (!has_no_scope)
1044 do_pushlevel (is_try ? sk_try : sk_block);
1045 else
1046 /* Normally, we try hard to keep the BLOCK for a
1047 statement-expression. But, if it's a statement-expression with
1048 a scopeless block, there's nothing to keep, and we don't want
1049 to accidentally keep a block *inside* the scopeless block. */
1050 keep_next_level (0);
1051
1052 return r;
1053 }
1054
1055 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
1056 If HAS_NO_SCOPE is nonzero, the compound statement does not define
1057 a scope. */
1058
1059 tree
1060 finish_compound_stmt (has_no_scope, compound_stmt)
1061 int has_no_scope;
1062 tree compound_stmt;
1063 {
1064 tree r;
1065 tree t;
1066
1067 if (!has_no_scope)
1068 r = do_poplevel ();
1069 else
1070 r = NULL_TREE;
1071
1072 RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
1073
1074 /* When we call finish_stmt we will lose LAST_EXPR_TYPE. But, since
1075 the precise purpose of that variable is store the type of the
1076 last expression statement within the last compound statement, we
1077 preserve the value. */
1078 t = last_expr_type;
1079 finish_stmt ();
1080 last_expr_type = t;
1081
1082 return r;
1083 }
1084
1085 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
1086 STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
1087 CLOBBERS. */
1088
1089 tree
1090 finish_asm_stmt (cv_qualifier, string, output_operands,
1091 input_operands, clobbers)
1092 tree cv_qualifier;
1093 tree string;
1094 tree output_operands;
1095 tree input_operands;
1096 tree clobbers;
1097 {
1098 tree r;
1099 tree t;
1100
1101 if (cv_qualifier != NULL_TREE
1102 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
1103 {
1104 warning ("%s qualifier ignored on asm",
1105 IDENTIFIER_POINTER (cv_qualifier));
1106 cv_qualifier = NULL_TREE;
1107 }
1108
1109 if (!processing_template_decl)
1110 {
1111 int i;
1112 int ninputs;
1113 int noutputs;
1114
1115 for (t = input_operands; t; t = TREE_CHAIN (t))
1116 {
1117 tree converted_operand
1118 = decay_conversion (TREE_VALUE (t));
1119
1120 /* If the type of the operand hasn't been determined (e.g.,
1121 because it involves an overloaded function), then issue
1122 an error message. There's no context available to
1123 resolve the overloading. */
1124 if (TREE_TYPE (converted_operand) == unknown_type_node)
1125 {
1126 error ("type of asm operand `%E' could not be determined",
1127 TREE_VALUE (t));
1128 converted_operand = error_mark_node;
1129 }
1130 TREE_VALUE (t) = converted_operand;
1131 }
1132
1133 ninputs = list_length (input_operands);
1134 noutputs = list_length (output_operands);
1135
1136 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1137 {
1138 bool allows_mem;
1139 bool allows_reg;
1140 bool is_inout;
1141 const char *constraint;
1142 tree operand;
1143
1144 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1145 operand = TREE_VALUE (t);
1146
1147 if (!parse_output_constraint (&constraint,
1148 i, ninputs, noutputs,
1149 &allows_mem,
1150 &allows_reg,
1151 &is_inout))
1152 {
1153 /* By marking this operand as erroneous, we will not try
1154 to process this operand again in expand_asm_operands. */
1155 TREE_VALUE (t) = error_mark_node;
1156 continue;
1157 }
1158
1159 /* If the operand is a DECL that is going to end up in
1160 memory, assume it is addressable. This is a bit more
1161 conservative than it would ideally be; the exact test is
1162 buried deep in expand_asm_operands and depends on the
1163 DECL_RTL for the OPERAND -- which we don't have at this
1164 point. */
1165 if (!allows_reg && DECL_P (operand))
1166 cxx_mark_addressable (operand);
1167 }
1168 }
1169
1170 r = build_stmt (ASM_STMT, cv_qualifier, string,
1171 output_operands, input_operands,
1172 clobbers);
1173 return add_stmt (r);
1174 }
1175
1176 /* Finish a label with the indicated NAME. */
1177
1178 tree
1179 finish_label_stmt (name)
1180 tree name;
1181 {
1182 tree decl = define_label (input_filename, input_line, name);
1183 return add_stmt (build_stmt (LABEL_STMT, decl));
1184 }
1185
1186 /* Finish a series of declarations for local labels. G++ allows users
1187 to declare "local" labels, i.e., labels with scope. This extension
1188 is useful when writing code involving statement-expressions. */
1189
1190 void
1191 finish_label_decl (name)
1192 tree name;
1193 {
1194 tree decl = declare_local_label (name);
1195 add_decl_stmt (decl);
1196 }
1197
1198 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
1199
1200 void
1201 finish_decl_cleanup (decl, cleanup)
1202 tree decl;
1203 tree cleanup;
1204 {
1205 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
1206 }
1207
1208 /* If the current scope exits with an exception, run CLEANUP. */
1209
1210 void
1211 finish_eh_cleanup (cleanup)
1212 tree cleanup;
1213 {
1214 tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1215 CLEANUP_EH_ONLY (r) = 1;
1216 add_stmt (r);
1217 }
1218
1219 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1220 order they were written by the user. Each node is as for
1221 emit_mem_initializers. */
1222
1223 void
1224 finish_mem_initializers (tree mem_inits)
1225 {
1226 /* Reorder the MEM_INITS so that they are in the order they appeared
1227 in the source program. */
1228 mem_inits = nreverse (mem_inits);
1229
1230 if (processing_template_decl)
1231 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1232 else
1233 emit_mem_initializers (mem_inits);
1234 }
1235
1236 /* Returns the stack of SCOPE_STMTs for the current function. */
1237
1238 tree *
1239 current_scope_stmt_stack ()
1240 {
1241 return &cfun->language->base.x_scope_stmt_stack;
1242 }
1243
1244 /* Finish a parenthesized expression EXPR. */
1245
1246 tree
1247 finish_parenthesized_expr (expr)
1248 tree expr;
1249 {
1250 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1251 /* This inhibits warnings in c_common_truthvalue_conversion. */
1252 C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1253
1254 if (TREE_CODE (expr) == OFFSET_REF)
1255 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1256 enclosed in parentheses. */
1257 PTRMEM_OK_P (expr) = 0;
1258 return expr;
1259 }
1260
1261 /* Finish a reference to a non-static data member (DECL) that is not
1262 preceded by `.' or `->'. */
1263
1264 tree
1265 finish_non_static_data_member (tree decl, tree qualifying_scope)
1266 {
1267 my_friendly_assert (TREE_CODE (decl) == FIELD_DECL, 20020909);
1268
1269 if (current_class_ptr == NULL_TREE)
1270 {
1271 if (current_function_decl
1272 && DECL_STATIC_FUNCTION_P (current_function_decl))
1273 cp_error_at ("invalid use of member `%D' in static member function",
1274 decl);
1275 else
1276 cp_error_at ("invalid use of non-static data member `%D'", decl);
1277 error ("from this location");
1278
1279 return error_mark_node;
1280 }
1281 TREE_USED (current_class_ptr) = 1;
1282 if (processing_template_decl)
1283 return build_min (COMPONENT_REF, TREE_TYPE (decl),
1284 current_class_ref, DECL_NAME (decl));
1285 else
1286 {
1287 tree access_type = current_class_type;
1288 tree object = current_class_ref;
1289
1290 while (access_type
1291 && !DERIVED_FROM_P (context_for_name_lookup (decl), access_type))
1292 {
1293 access_type = TYPE_CONTEXT (access_type);
1294 while (access_type && DECL_P (access_type))
1295 access_type = DECL_CONTEXT (access_type);
1296 }
1297
1298 if (!access_type)
1299 {
1300 cp_error_at ("object missing in reference to `%D'",
1301 decl);
1302 error ("from this location");
1303 return error_mark_node;
1304 }
1305
1306 perform_or_defer_access_check (access_type, decl);
1307
1308 /* If the data member was named `C::M', convert `*this' to `C'
1309 first. */
1310 if (qualifying_scope)
1311 {
1312 tree binfo = NULL_TREE;
1313 object = build_scoped_ref (object, qualifying_scope,
1314 &binfo);
1315 }
1316
1317 return build_class_member_access_expr (object, decl,
1318 /*access_path=*/NULL_TREE,
1319 /*preserve_reference=*/false);
1320 }
1321 }
1322
1323 /* Begin a statement-expression. The value returned must be passed to
1324 finish_stmt_expr. */
1325
1326 tree
1327 begin_stmt_expr ()
1328 {
1329 /* If we're outside a function, we won't have a statement-tree to
1330 work with. But, if we see a statement-expression we need to
1331 create one. */
1332 if (! cfun && !last_tree)
1333 begin_stmt_tree (&scope_chain->x_saved_tree);
1334
1335 keep_next_level (1);
1336 /* If we're building a statement tree, then the upcoming compound
1337 statement will be chained onto the tree structure, starting at
1338 last_tree. We return last_tree so that we can later unhook the
1339 compound statement. */
1340 return last_tree;
1341 }
1342
1343 /* Used when beginning a statement-expression outside function scope.
1344 For example, when handling a file-scope initializer, we use this
1345 function. */
1346
1347 tree
1348 begin_global_stmt_expr ()
1349 {
1350 if (! cfun && !last_tree)
1351 begin_stmt_tree (&scope_chain->x_saved_tree);
1352
1353 keep_next_level (1);
1354
1355 return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1);
1356 }
1357
1358 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr. */
1359
1360 tree
1361 finish_global_stmt_expr (stmt_expr)
1362 tree stmt_expr;
1363 {
1364 stmt_expr = expand_end_stmt_expr (stmt_expr);
1365
1366 if (! cfun
1367 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1368 finish_stmt_tree (&scope_chain->x_saved_tree);
1369
1370 return stmt_expr;
1371 }
1372
1373 /* Finish a statement-expression. RTL_EXPR should be the value
1374 returned by the previous begin_stmt_expr; EXPR is the
1375 statement-expression. Returns an expression representing the
1376 statement-expression. */
1377
1378 tree
1379 finish_stmt_expr (rtl_expr)
1380 tree rtl_expr;
1381 {
1382 tree result;
1383
1384 /* If the last thing in the statement-expression was not an
1385 expression-statement, then it has type `void'. */
1386 if (!last_expr_type)
1387 last_expr_type = void_type_node;
1388 result = build_min (STMT_EXPR, last_expr_type, last_tree);
1389 TREE_SIDE_EFFECTS (result) = 1;
1390
1391 /* Remove the compound statement from the tree structure; it is
1392 now saved in the STMT_EXPR. */
1393 last_tree = rtl_expr;
1394 TREE_CHAIN (last_tree) = NULL_TREE;
1395
1396 /* If we created a statement-tree for this statement-expression,
1397 remove it now. */
1398 if (! cfun
1399 && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1400 finish_stmt_tree (&scope_chain->x_saved_tree);
1401
1402 return result;
1403 }
1404
1405 /* Generate an expression for `FN (ARGS)'.
1406
1407 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1408 as a virtual call, even if FN is virtual. (This flag is set when
1409 encountering an expression where the function name is explicitly
1410 qualified. For example a call to `X::f' never generates a virtual
1411 call.)
1412
1413 Returns code for the call. */
1414
1415 tree
1416 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1417 {
1418 if (fn == error_mark_node || args == error_mark_node)
1419 return error_mark_node;
1420
1421 if (processing_template_decl)
1422 return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1423
1424 /* ARGS should be a list of arguments. */
1425 my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1426 20020712);
1427
1428 /* A reference to a member function will appear as an overloaded
1429 function (rather than a BASELINK) if an unqualified name was used
1430 to refer to it. */
1431 if (!BASELINK_P (fn) && is_overloaded_fn (fn))
1432 {
1433 tree f;
1434
1435 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1436 f = get_first_fn (TREE_OPERAND (fn, 0));
1437 else
1438 f = get_first_fn (fn);
1439 if (DECL_FUNCTION_MEMBER_P (f))
1440 {
1441 tree type = currently_open_derived_class (DECL_CONTEXT (f));
1442 fn = build_baselink (TYPE_BINFO (type),
1443 TYPE_BINFO (type),
1444 fn, /*optype=*/NULL_TREE);
1445 }
1446 }
1447
1448 if (BASELINK_P (fn))
1449 {
1450 tree object;
1451
1452 /* A call to a member function. From [over.call.func]:
1453
1454 If the keyword this is in scope and refers to the class of
1455 that member function, or a derived class thereof, then the
1456 function call is transformed into a qualified function call
1457 using (*this) as the postfix-expression to the left of the
1458 . operator.... [Otherwise] a contrived object of type T
1459 becomes the implied object argument.
1460
1461 This paragraph is unclear about this situation:
1462
1463 struct A { void f(); };
1464 struct B : public A {};
1465 struct C : public A { void g() { B::f(); }};
1466
1467 In particular, for `B::f', this paragraph does not make clear
1468 whether "the class of that member function" refers to `A' or
1469 to `B'. We believe it refers to `B'. */
1470 if (current_class_type
1471 && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1472 current_class_type)
1473 && current_class_ref)
1474 object = current_class_ref;
1475 else
1476 {
1477 tree representative_fn;
1478
1479 representative_fn = BASELINK_FUNCTIONS (fn);
1480 if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1481 representative_fn = TREE_OPERAND (representative_fn, 0);
1482 representative_fn = get_first_fn (representative_fn);
1483 object = build_dummy_object (DECL_CONTEXT (representative_fn));
1484 }
1485
1486 return build_new_method_call (object, fn, args, NULL_TREE,
1487 (disallow_virtual
1488 ? LOOKUP_NONVIRTUAL : 0));
1489 }
1490 else if (is_overloaded_fn (fn))
1491 /* A call to a namespace-scope function. */
1492 return build_new_function_call (fn, args);
1493 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
1494 {
1495 tree result;
1496
1497 if (args)
1498 error ("arguments to destructor are not allowed");
1499 /* Mark the pseudo-destructor call as having side-effects so
1500 that we do not issue warnings about its use. */
1501 result = build1 (NOP_EXPR,
1502 void_type_node,
1503 TREE_OPERAND (fn, 0));
1504 TREE_SIDE_EFFECTS (result) = 1;
1505 return result;
1506 }
1507 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1508 {
1509 /* If the "function" is really an object of class type, it might
1510 have an overloaded `operator ()'. */
1511 tree result;
1512 result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1513 if (result)
1514 return result;
1515 }
1516
1517 /* A call where the function is unknown. */
1518 return build_function_call (fn, args);
1519 }
1520
1521 /* Finish a call to a postfix increment or decrement or EXPR. (Which
1522 is indicated by CODE, which should be POSTINCREMENT_EXPR or
1523 POSTDECREMENT_EXPR.) */
1524
1525 tree
1526 finish_increment_expr (expr, code)
1527 tree expr;
1528 enum tree_code code;
1529 {
1530 /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1531 a COMPONENT_REF). This way if we've got, say, a reference to a
1532 static member that's being operated on, we don't end up trying to
1533 find a member operator for the class it's in. */
1534
1535 if (TREE_CODE (expr) == OFFSET_REF)
1536 expr = resolve_offset_ref (expr);
1537 return build_x_unary_op (code, expr);
1538 }
1539
1540 /* Finish a use of `this'. Returns an expression for `this'. */
1541
1542 tree
1543 finish_this_expr ()
1544 {
1545 tree result;
1546
1547 if (current_class_ptr)
1548 {
1549 result = current_class_ptr;
1550 }
1551 else if (current_function_decl
1552 && DECL_STATIC_FUNCTION_P (current_function_decl))
1553 {
1554 error ("`this' is unavailable for static member functions");
1555 result = error_mark_node;
1556 }
1557 else
1558 {
1559 if (current_function_decl)
1560 error ("invalid use of `this' in non-member function");
1561 else
1562 error ("invalid use of `this' at top level");
1563 result = error_mark_node;
1564 }
1565
1566 return result;
1567 }
1568
1569 /* Finish a member function call using OBJECT and ARGS as arguments to
1570 FN. Returns an expression for the call. */
1571
1572 tree
1573 finish_object_call_expr (fn, object, args)
1574 tree fn;
1575 tree object;
1576 tree args;
1577 {
1578 if (DECL_DECLARES_TYPE_P (fn))
1579 {
1580 if (processing_template_decl)
1581 /* This can happen on code like:
1582
1583 class X;
1584 template <class T> void f(T t) {
1585 t.X();
1586 }
1587
1588 We just grab the underlying IDENTIFIER. */
1589 fn = DECL_NAME (fn);
1590 else
1591 {
1592 error ("calling type `%T' like a method", fn);
1593 return error_mark_node;
1594 }
1595 }
1596
1597 if (processing_template_decl)
1598 return build_nt (CALL_EXPR,
1599 build_nt (COMPONENT_REF, object, fn),
1600 args);
1601
1602 if (name_p (fn))
1603 return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1604 else
1605 return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1606 }
1607
1608 /* Finish a qualified member function call using OBJECT and ARGS as
1609 arguments to FN. Returns an expression for the call. */
1610
1611 tree
1612 finish_qualified_object_call_expr (fn, object, args)
1613 tree fn;
1614 tree object;
1615 tree args;
1616 {
1617 return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1618 TREE_OPERAND (fn, 1), args);
1619 }
1620
1621 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
1622 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
1623 the TYPE for the type given. If SCOPE is non-NULL, the expression
1624 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
1625
1626 tree
1627 finish_pseudo_destructor_expr (object, scope, destructor)
1628 tree object;
1629 tree scope;
1630 tree destructor;
1631 {
1632 if (destructor == error_mark_node)
1633 return error_mark_node;
1634
1635 my_friendly_assert (TYPE_P (destructor), 20010905);
1636
1637 if (!processing_template_decl)
1638 {
1639 if (scope == error_mark_node)
1640 {
1641 error ("invalid qualifying scope in pseudo-destructor name");
1642 return error_mark_node;
1643 }
1644
1645 if (!same_type_p (TREE_TYPE (object), destructor))
1646 {
1647 error ("`%E' is not of type `%T'", object, destructor);
1648 return error_mark_node;
1649 }
1650 }
1651
1652 return build (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
1653 }
1654
1655 /* Finish an expression of the form CODE EXPR. */
1656
1657 tree
1658 finish_unary_op_expr (code, expr)
1659 enum tree_code code;
1660 tree expr;
1661 {
1662 tree result = build_x_unary_op (code, expr);
1663 /* Inside a template, build_x_unary_op does not fold the
1664 expression. So check whether the result is folded before
1665 setting TREE_NEGATED_INT. */
1666 if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1667 && TREE_CODE (result) == INTEGER_CST
1668 && !TREE_UNSIGNED (TREE_TYPE (result))
1669 && INT_CST_LT (result, integer_zero_node))
1670 TREE_NEGATED_INT (result) = 1;
1671 overflow_warning (result);
1672 return result;
1673 }
1674
1675 /* Finish a compound-literal expression. TYPE is the type to which
1676 the INITIALIZER_LIST is being cast. */
1677
1678 tree
1679 finish_compound_literal (type, initializer_list)
1680 tree type;
1681 tree initializer_list;
1682 {
1683 tree compound_literal;
1684
1685 /* Build a CONSTRUCTOR for the INITIALIZER_LIST. */
1686 compound_literal = build_constructor (NULL_TREE, initializer_list);
1687 /* Mark it as a compound-literal. */
1688 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
1689 if (processing_template_decl)
1690 TREE_TYPE (compound_literal) = type;
1691 else
1692 {
1693 /* Check the initialization. */
1694 compound_literal = digest_init (type, compound_literal, NULL);
1695 /* If the TYPE was an array type with an unknown bound, then we can
1696 figure out the dimension now. For example, something like:
1697
1698 `(int []) { 2, 3 }'
1699
1700 implies that the array has two elements. */
1701 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
1702 complete_array_type (type, compound_literal, 1);
1703 }
1704
1705 return compound_literal;
1706 }
1707
1708 /* Return the declaration for the function-name variable indicated by
1709 ID. */
1710
1711 tree
1712 finish_fname (tree id)
1713 {
1714 tree decl;
1715
1716 decl = fname_decl (C_RID_CODE (id), id);
1717 if (processing_template_decl)
1718 decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1719 return decl;
1720 }
1721
1722 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1723 and DECLARATOR. Returns nonzero if the function-declaration is
1724 valid. */
1725
1726 int
1727 begin_function_definition (decl_specs, attributes, declarator)
1728 tree decl_specs;
1729 tree attributes;
1730 tree declarator;
1731 {
1732 if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1733 return 0;
1734
1735 /* The things we're about to see are not directly qualified by any
1736 template headers we've seen thus far. */
1737 reset_specialization ();
1738
1739 return 1;
1740 }
1741
1742 /* Finish a translation unit. */
1743
1744 void
1745 finish_translation_unit ()
1746 {
1747 /* In case there were missing closebraces,
1748 get us back to the global binding level. */
1749 pop_everything ();
1750 while (current_namespace != global_namespace)
1751 pop_namespace ();
1752
1753 /* Do file scope __FUNCTION__ et al. */
1754 finish_fname_decls ();
1755 }
1756
1757 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1758 Returns the parameter. */
1759
1760 tree
1761 finish_template_type_parm (aggr, identifier)
1762 tree aggr;
1763 tree identifier;
1764 {
1765 if (aggr != class_type_node)
1766 {
1767 pedwarn ("template type parameters must use the keyword `class' or `typename'");
1768 aggr = class_type_node;
1769 }
1770
1771 return build_tree_list (aggr, identifier);
1772 }
1773
1774 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1775 Returns the parameter. */
1776
1777 tree
1778 finish_template_template_parm (aggr, identifier)
1779 tree aggr;
1780 tree identifier;
1781 {
1782 tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1783 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1784 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1785 DECL_TEMPLATE_RESULT (tmpl) = decl;
1786 DECL_ARTIFICIAL (decl) = 1;
1787 end_template_decl ();
1788
1789 my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1790
1791 return finish_template_type_parm (aggr, tmpl);
1792 }
1793
1794 /* ARGUMENT is the default-argument value for a template template
1795 parameter. If ARGUMENT is invalid, issue error messages and return
1796 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
1797
1798 tree
1799 check_template_template_default_arg (tree argument)
1800 {
1801 if (TREE_CODE (argument) != TEMPLATE_DECL
1802 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1803 && TREE_CODE (argument) != TYPE_DECL
1804 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1805 {
1806 error ("invalid default template argument");
1807 return error_mark_node;
1808 }
1809
1810 return argument;
1811 }
1812
1813 /* Finish a parameter list, indicated by PARMS. If ELLIPSIS is
1814 nonzero, the parameter list was terminated by a `...'. */
1815
1816 tree
1817 finish_parmlist (parms, ellipsis)
1818 tree parms;
1819 int ellipsis;
1820 {
1821 if (parms)
1822 {
1823 /* We mark the PARMS as a parmlist so that declarator processing can
1824 disambiguate certain constructs. */
1825 TREE_PARMLIST (parms) = 1;
1826 /* We do not append void_list_node here, but leave it to grokparms
1827 to do that. */
1828 PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1829 }
1830 return parms;
1831 }
1832
1833 /* Begin a class definition, as indicated by T. */
1834
1835 tree
1836 begin_class_definition (t)
1837 tree t;
1838 {
1839 if (t == error_mark_node)
1840 return error_mark_node;
1841
1842 if (processing_template_parmlist)
1843 {
1844 error ("definition of `%#T' inside template parameter list", t);
1845 return error_mark_node;
1846 }
1847 /* A non-implicit typename comes from code like:
1848
1849 template <typename T> struct A {
1850 template <typename U> struct A<T>::B ...
1851
1852 This is erroneous. */
1853 else if (TREE_CODE (t) == TYPENAME_TYPE)
1854 {
1855 error ("invalid definition of qualified type `%T'", t);
1856 t = error_mark_node;
1857 }
1858
1859 if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1860 {
1861 t = make_aggr_type (RECORD_TYPE);
1862 pushtag (make_anon_name (), t, 0);
1863 }
1864
1865 /* If this type was already complete, and we see another definition,
1866 that's an error. */
1867 if (COMPLETE_TYPE_P (t))
1868 {
1869 error ("redefinition of `%#T'", t);
1870 cp_error_at ("previous definition of `%#T'", t);
1871 return error_mark_node;
1872 }
1873
1874 /* Update the location of the decl. */
1875 DECL_SOURCE_LOCATION (TYPE_NAME (t)) = input_location;
1876
1877 if (TYPE_BEING_DEFINED (t))
1878 {
1879 t = make_aggr_type (TREE_CODE (t));
1880 pushtag (TYPE_IDENTIFIER (t), t, 0);
1881 }
1882 maybe_process_partial_specialization (t);
1883 pushclass (t, true);
1884 TYPE_BEING_DEFINED (t) = 1;
1885 TYPE_PACKED (t) = flag_pack_struct;
1886 /* Reset the interface data, at the earliest possible
1887 moment, as it might have been set via a class foo;
1888 before. */
1889 if (! TYPE_ANONYMOUS_P (t))
1890 {
1891 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1892 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1893 (t, interface_unknown);
1894 }
1895 reset_specialization();
1896
1897 /* Make a declaration for this class in its own scope. */
1898 build_self_reference ();
1899
1900 return t;
1901 }
1902
1903 /* Finish the member declaration given by DECL. */
1904
1905 void
1906 finish_member_declaration (decl)
1907 tree decl;
1908 {
1909 if (decl == error_mark_node || decl == NULL_TREE)
1910 return;
1911
1912 if (decl == void_type_node)
1913 /* The COMPONENT was a friend, not a member, and so there's
1914 nothing for us to do. */
1915 return;
1916
1917 /* We should see only one DECL at a time. */
1918 my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1919
1920 /* Set up access control for DECL. */
1921 TREE_PRIVATE (decl)
1922 = (current_access_specifier == access_private_node);
1923 TREE_PROTECTED (decl)
1924 = (current_access_specifier == access_protected_node);
1925 if (TREE_CODE (decl) == TEMPLATE_DECL)
1926 {
1927 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1928 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1929 }
1930
1931 /* Mark the DECL as a member of the current class. */
1932 DECL_CONTEXT (decl) = current_class_type;
1933
1934 /* [dcl.link]
1935
1936 A C language linkage is ignored for the names of class members
1937 and the member function type of class member functions. */
1938 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1939 SET_DECL_LANGUAGE (decl, lang_cplusplus);
1940
1941 maybe_add_class_template_decl_list (current_class_type, decl, /*friend_p=*/0);
1942
1943 /* Put functions on the TYPE_METHODS list and everything else on the
1944 TYPE_FIELDS list. Note that these are built up in reverse order.
1945 We reverse them (to obtain declaration order) in finish_struct. */
1946 if (TREE_CODE (decl) == FUNCTION_DECL
1947 || DECL_FUNCTION_TEMPLATE_P (decl))
1948 {
1949 /* We also need to add this function to the
1950 CLASSTYPE_METHOD_VEC. */
1951 add_method (current_class_type, decl, /*error_p=*/0);
1952
1953 TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1954 TYPE_METHODS (current_class_type) = decl;
1955 }
1956 else
1957 {
1958 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
1959 go at the beginning. The reason is that lookup_field_1
1960 searches the list in order, and we want a field name to
1961 override a type name so that the "struct stat hack" will
1962 work. In particular:
1963
1964 struct S { enum E { }; int E } s;
1965 s.E = 3;
1966
1967 is valid. In addition, the FIELD_DECLs must be maintained in
1968 declaration order so that class layout works as expected.
1969 However, we don't need that order until class layout, so we
1970 save a little time by putting FIELD_DECLs on in reverse order
1971 here, and then reversing them in finish_struct_1. (We could
1972 also keep a pointer to the correct insertion points in the
1973 list.) */
1974
1975 if (TREE_CODE (decl) == TYPE_DECL)
1976 TYPE_FIELDS (current_class_type)
1977 = chainon (TYPE_FIELDS (current_class_type), decl);
1978 else
1979 {
1980 TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1981 TYPE_FIELDS (current_class_type) = decl;
1982 }
1983
1984 /* Enter the DECL into the scope of the class. */
1985 if (TREE_CODE (decl) != USING_DECL)
1986 pushdecl_class_level (decl);
1987 }
1988 }
1989
1990 /* Finish a class definition T with the indicate ATTRIBUTES. If SEMI,
1991 the definition is immediately followed by a semicolon. Returns the
1992 type. */
1993
1994 tree
1995 finish_class_definition (t, attributes, semi, pop_scope_p)
1996 tree t;
1997 tree attributes;
1998 int semi;
1999 int pop_scope_p;
2000 {
2001 if (t == error_mark_node)
2002 return error_mark_node;
2003
2004 /* finish_struct nukes this anyway; if finish_exception does too,
2005 then it can go. */
2006 if (semi)
2007 note_got_semicolon (t);
2008
2009 /* If we got any attributes in class_head, xref_tag will stick them in
2010 TREE_TYPE of the type. Grab them now. */
2011 attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
2012 TYPE_ATTRIBUTES (t) = NULL_TREE;
2013
2014 if (TREE_CODE (t) == ENUMERAL_TYPE)
2015 ;
2016 else
2017 {
2018 t = finish_struct (t, attributes);
2019 if (semi)
2020 note_got_semicolon (t);
2021 }
2022
2023 if (pop_scope_p)
2024 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
2025
2026 return t;
2027 }
2028
2029 /* Finish processing the declaration of a member class template
2030 TYPES whose template parameters are given by PARMS. */
2031
2032 tree
2033 finish_member_class_template (types)
2034 tree types;
2035 {
2036 tree t;
2037
2038 /* If there are declared, but undefined, partial specializations
2039 mixed in with the typespecs they will not yet have passed through
2040 maybe_process_partial_specialization, so we do that here. */
2041 for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
2042 if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
2043 maybe_process_partial_specialization (TREE_VALUE (t));
2044
2045 note_list_got_semicolon (types);
2046 grok_x_components (types);
2047 if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
2048 /* The component was in fact a friend declaration. We avoid
2049 finish_member_template_decl performing certain checks by
2050 unsetting TYPES. */
2051 types = NULL_TREE;
2052
2053 finish_member_template_decl (types);
2054
2055 /* As with other component type declarations, we do
2056 not store the new DECL on the list of
2057 component_decls. */
2058 return NULL_TREE;
2059 }
2060
2061 /* Finish processing a complete template declaration. The PARMS are
2062 the template parameters. */
2063
2064 void
2065 finish_template_decl (parms)
2066 tree parms;
2067 {
2068 if (parms)
2069 end_template_decl ();
2070 else
2071 end_specialization ();
2072 }
2073
2074 /* Finish processing a template-id (which names a type) of the form
2075 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2076 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2077 the scope of template-id indicated. */
2078
2079 tree
2080 finish_template_type (name, args, entering_scope)
2081 tree name;
2082 tree args;
2083 int entering_scope;
2084 {
2085 tree decl;
2086
2087 decl = lookup_template_class (name, args,
2088 NULL_TREE, NULL_TREE,
2089 entering_scope, /*complain=*/1);
2090 if (decl != error_mark_node)
2091 decl = TYPE_STUB_DECL (decl);
2092
2093 return decl;
2094 }
2095
2096 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2097 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2098 BASE_CLASS, or NULL_TREE if an error occurred. The
2099 ACCESS_SPECIFIER is one of
2100 access_{default,public,protected_private}[_virtual]_node.*/
2101
2102 tree
2103 finish_base_specifier (tree base, tree access, bool virtual_p)
2104 {
2105 tree result;
2106
2107 if (base == error_mark_node)
2108 {
2109 error ("invalid base-class specification");
2110 result = NULL_TREE;
2111 }
2112 else if (! is_aggr_type (base, 1))
2113 result = NULL_TREE;
2114 else
2115 {
2116 if (cp_type_quals (base) != 0)
2117 {
2118 error ("base class `%T' has cv qualifiers", base);
2119 base = TYPE_MAIN_VARIANT (base);
2120 }
2121 result = build_tree_list (access, base);
2122 TREE_VIA_VIRTUAL (result) = virtual_p;
2123 }
2124
2125 return result;
2126 }
2127
2128 /* Called when multiple declarators are processed. If that is not
2129 premitted in this context, an error is issued. */
2130
2131 void
2132 check_multiple_declarators ()
2133 {
2134 /* [temp]
2135
2136 In a template-declaration, explicit specialization, or explicit
2137 instantiation the init-declarator-list in the declaration shall
2138 contain at most one declarator.
2139
2140 We don't just use PROCESSING_TEMPLATE_DECL for the first
2141 condition since that would disallow the perfectly valid code,
2142 like `template <class T> struct S { int i, j; };'. */
2143 if (at_function_scope_p ())
2144 /* It's OK to write `template <class T> void f() { int i, j;}'. */
2145 return;
2146
2147 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2148 || processing_explicit_instantiation
2149 || processing_specialization)
2150 error ("multiple declarators in template declaration");
2151 }
2152
2153 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2154 use as a type-specifier. */
2155
2156 tree
2157 finish_typeof (expr)
2158 tree expr;
2159 {
2160 tree type;
2161
2162 if (type_dependent_expression_p (expr))
2163 {
2164 type = make_aggr_type (TYPEOF_TYPE);
2165 TYPE_FIELDS (type) = expr;
2166
2167 return type;
2168 }
2169
2170 if (TREE_CODE (expr) == OFFSET_REF)
2171 expr = resolve_offset_ref (expr);
2172
2173 type = TREE_TYPE (expr);
2174
2175 if (!type || type == unknown_type_node)
2176 {
2177 error ("type of `%E' is unknown", expr);
2178 return error_mark_node;
2179 }
2180
2181 return type;
2182 }
2183
2184 /* Compute the value of the `sizeof' operator. */
2185
2186 tree
2187 finish_sizeof (t)
2188 tree t;
2189 {
2190 return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2191 }
2192
2193 /* Implement the __alignof keyword: Return the minimum required
2194 alignment of T, measured in bytes. */
2195
2196 tree
2197 finish_alignof (t)
2198 tree t;
2199 {
2200 if (processing_template_decl)
2201 return build_min (ALIGNOF_EXPR, size_type_node, t);
2202
2203 return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2204 }
2205
2206 /* Generate RTL for the statement T, and its substatements, and any
2207 other statements at its nesting level. */
2208
2209 static void
2210 cp_expand_stmt (t)
2211 tree t;
2212 {
2213 switch (TREE_CODE (t))
2214 {
2215 case TRY_BLOCK:
2216 genrtl_try_block (t);
2217 break;
2218
2219 case EH_SPEC_BLOCK:
2220 genrtl_eh_spec_block (t);
2221 break;
2222
2223 case HANDLER:
2224 genrtl_handler (t);
2225 break;
2226
2227 case USING_STMT:
2228 break;
2229
2230 default:
2231 abort ();
2232 break;
2233 }
2234 }
2235
2236 /* Called from expand_body via walk_tree. Replace all AGGR_INIT_EXPRs
2237 will equivalent CALL_EXPRs. */
2238
2239 static tree
2240 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2241 tree *tp;
2242 int *walk_subtrees ATTRIBUTE_UNUSED;
2243 void *data ATTRIBUTE_UNUSED;
2244 {
2245 tree aggr_init_expr;
2246 tree call_expr;
2247 tree fn;
2248 tree args;
2249 tree slot;
2250 tree type;
2251 enum style_t { ctor, arg, pcc } style;
2252
2253 aggr_init_expr = *tp;
2254 /* We don't need to walk into types; there's nothing in a type that
2255 needs simplification. (And, furthermore, there are places we
2256 actively don't want to go. For example, we don't want to wander
2257 into the default arguments for a FUNCTION_DECL that appears in a
2258 CALL_EXPR.) */
2259 if (TYPE_P (aggr_init_expr))
2260 {
2261 *walk_subtrees = 0;
2262 return NULL_TREE;
2263 }
2264 /* Only AGGR_INIT_EXPRs are interesting. */
2265 else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2266 return NULL_TREE;
2267
2268 /* Form an appropriate CALL_EXPR. */
2269 fn = TREE_OPERAND (aggr_init_expr, 0);
2270 args = TREE_OPERAND (aggr_init_expr, 1);
2271 slot = TREE_OPERAND (aggr_init_expr, 2);
2272 type = TREE_TYPE (aggr_init_expr);
2273
2274 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2275 style = ctor;
2276 #ifdef PCC_STATIC_STRUCT_RETURN
2277 else if (1)
2278 style = pcc;
2279 #endif
2280 else if (TREE_ADDRESSABLE (type))
2281 style = arg;
2282 else
2283 /* We shouldn't build an AGGR_INIT_EXPR if we don't need any special
2284 handling. See build_cplus_new. */
2285 abort ();
2286
2287 if (style == ctor || style == arg)
2288 {
2289 /* Pass the address of the slot. If this is a constructor, we
2290 replace the first argument; otherwise, we tack on a new one. */
2291 if (style == ctor)
2292 args = TREE_CHAIN (args);
2293
2294 cxx_mark_addressable (slot);
2295 args = tree_cons (NULL_TREE,
2296 build1 (ADDR_EXPR,
2297 build_pointer_type (TREE_TYPE (slot)),
2298 slot),
2299 args);
2300 }
2301
2302 call_expr = build (CALL_EXPR,
2303 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2304 fn, args, NULL_TREE);
2305 TREE_SIDE_EFFECTS (call_expr) = 1;
2306
2307 if (style == arg)
2308 /* Tell the backend that we've added our return slot to the argument
2309 list. */
2310 CALL_EXPR_HAS_RETURN_SLOT_ADDR (call_expr) = 1;
2311 else if (style == pcc)
2312 {
2313 /* If we're using the non-reentrant PCC calling convention, then we
2314 need to copy the returned value out of the static buffer into the
2315 SLOT. */
2316 push_deferring_access_checks (dk_no_check);
2317 call_expr = build_aggr_init (slot, call_expr,
2318 DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2319 pop_deferring_access_checks ();
2320 }
2321
2322 /* We want to use the value of the initialized location as the
2323 result. */
2324 call_expr = build (COMPOUND_EXPR, type,
2325 call_expr, slot);
2326
2327 /* Replace the AGGR_INIT_EXPR with the CALL_EXPR. */
2328 TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2329 *tp = call_expr;
2330
2331 /* Keep iterating. */
2332 return NULL_TREE;
2333 }
2334
2335 /* Emit all thunks to FN that should be emitted when FN is emitted. */
2336
2337 static void
2338 emit_associated_thunks (fn)
2339 tree fn;
2340 {
2341 /* When we use vcall offsets, we emit thunks with the virtual
2342 functions to which they thunk. The whole point of vcall offsets
2343 is so that you can know statically the entire set of thunks that
2344 will ever be needed for a given virtual function, thereby
2345 enabling you to output all the thunks with the function itself. */
2346 if (DECL_VIRTUAL_P (fn))
2347 {
2348 tree thunk;
2349
2350 for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2351 {
2352 use_thunk (thunk, /*emit_p=*/1);
2353 if (DECL_RESULT_THUNK_P (thunk))
2354 {
2355 tree probe;
2356
2357 for (probe = DECL_THUNKS (thunk);
2358 probe; probe = TREE_CHAIN (probe))
2359 use_thunk (probe, /*emit_p=*/1);
2360 }
2361 }
2362 }
2363 }
2364
2365 /* Generate RTL for FN. */
2366
2367 void
2368 expand_body (fn)
2369 tree fn;
2370 {
2371 location_t saved_loc;
2372 tree saved_function;
2373
2374 /* When the parser calls us after finishing the body of a template
2375 function, we don't really want to expand the body. When we're
2376 processing an in-class definition of an inline function,
2377 PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2378 to look at the function itself. */
2379 if (processing_template_decl
2380 || (DECL_LANG_SPECIFIC (fn)
2381 && DECL_TEMPLATE_INFO (fn)
2382 && uses_template_parms (DECL_TI_ARGS (fn))))
2383 {
2384 /* Normally, collection only occurs in rest_of_compilation. So,
2385 if we don't collect here, we never collect junk generated
2386 during the processing of templates until we hit a
2387 non-template function. */
2388 ggc_collect ();
2389 return;
2390 }
2391
2392 /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs. */
2393 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2394 simplify_aggr_init_exprs_r,
2395 NULL);
2396
2397 /* If this is a constructor or destructor body, we have to clone
2398 it. */
2399 if (maybe_clone_body (fn))
2400 {
2401 /* We don't want to process FN again, so pretend we've written
2402 it out, even though we haven't. */
2403 TREE_ASM_WRITTEN (fn) = 1;
2404 return;
2405 }
2406
2407 /* There's no reason to do any of the work here if we're only doing
2408 semantic analysis; this code just generates RTL. */
2409 if (flag_syntax_only)
2410 return;
2411
2412 /* If possible, avoid generating RTL for this function. Instead,
2413 just record it as an inline function, and wait until end-of-file
2414 to decide whether to write it out or not. */
2415 if (/* We have to generate RTL if it's not an inline function. */
2416 (DECL_INLINE (fn) || DECL_COMDAT (fn))
2417 /* Or if we have to emit code for inline functions anyhow. */
2418 && !flag_keep_inline_functions
2419 /* Or if we actually have a reference to the function. */
2420 && !DECL_NEEDED_P (fn))
2421 {
2422 /* Set DECL_EXTERNAL so that assemble_external will be called as
2423 necessary. We'll clear it again in finish_file. */
2424 if (!DECL_EXTERNAL (fn))
2425 {
2426 DECL_NOT_REALLY_EXTERN (fn) = 1;
2427 DECL_EXTERNAL (fn) = 1;
2428 }
2429 /* Remember this function. In finish_file we'll decide if
2430 we actually need to write this function out. */
2431 defer_fn (fn);
2432 /* Let the back-end know that this function exists. */
2433 (*debug_hooks->deferred_inline_function) (fn);
2434 return;
2435 }
2436
2437 /* Compute the appropriate object-file linkage for inline
2438 functions. */
2439 if (DECL_DECLARED_INLINE_P (fn))
2440 import_export_decl (fn);
2441
2442 /* If FN is external, then there's no point in generating RTL for
2443 it. This situation can arise with an inline function under
2444 `-fexternal-templates'; we instantiate the function, even though
2445 we're not planning on emitting it, in case we get a chance to
2446 inline it. */
2447 if (DECL_EXTERNAL (fn))
2448 return;
2449
2450 /* Save the current file name and line number. When we expand the
2451 body of the function, we'll set INPUT_LOCATION so that
2452 error-mesages come out in the right places. */
2453 saved_loc = input_location;
2454 saved_function = current_function_decl;
2455 input_location = DECL_SOURCE_LOCATION (fn);
2456 current_function_decl = fn;
2457
2458 timevar_push (TV_INTEGRATION);
2459
2460 /* Optimize the body of the function before expanding it. */
2461 optimize_function (fn);
2462
2463 timevar_pop (TV_INTEGRATION);
2464 timevar_push (TV_EXPAND);
2465
2466 genrtl_start_function (fn);
2467 current_function_is_thunk = DECL_THUNK_P (fn);
2468
2469 /* Expand the body. */
2470 expand_stmt (DECL_SAVED_TREE (fn));
2471
2472 /* Statements should always be full-expressions at the outermost set
2473 of curly braces for a function. */
2474 my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2475
2476 /* The outermost statement for a function contains the line number
2477 recorded when we finished processing the function. */
2478 input_line = STMT_LINENO (DECL_SAVED_TREE (fn));
2479
2480 /* Generate code for the function. */
2481 genrtl_finish_function (fn);
2482
2483 /* If possible, obliterate the body of the function so that it can
2484 be garbage collected. */
2485 if (dump_enabled_p (TDI_all))
2486 /* Keep the body; we're going to dump it. */
2487 ;
2488 else if (DECL_INLINE (fn) && flag_inline_trees)
2489 /* We might need the body of this function so that we can expand
2490 it inline somewhere else. */
2491 ;
2492 else
2493 /* We don't need the body; blow it away. */
2494 DECL_SAVED_TREE (fn) = NULL_TREE;
2495
2496 /* And restore the current source position. */
2497 current_function_decl = saved_function;
2498 input_location = saved_loc;
2499 extract_interface_info ();
2500
2501 timevar_pop (TV_EXPAND);
2502
2503 /* Emit any thunks that should be emitted at the same time as FN. */
2504 emit_associated_thunks (fn);
2505 }
2506
2507 /* Helper function for walk_tree, used by finish_function to override all
2508 the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2509 value optimization. */
2510
2511 tree
2512 nullify_returns_r (tp, walk_subtrees, data)
2513 tree *tp;
2514 int *walk_subtrees;
2515 void *data;
2516 {
2517 tree nrv = (tree) data;
2518
2519 /* No need to walk into types. There wouldn't be any need to walk into
2520 non-statements, except that we have to consider STMT_EXPRs. */
2521 if (TYPE_P (*tp))
2522 *walk_subtrees = 0;
2523 else if (TREE_CODE (*tp) == RETURN_STMT)
2524 RETURN_STMT_EXPR (*tp) = NULL_TREE;
2525 else if (TREE_CODE (*tp) == CLEANUP_STMT
2526 && CLEANUP_DECL (*tp) == nrv)
2527 CLEANUP_EH_ONLY (*tp) = 1;
2528
2529 /* Keep iterating. */
2530 return NULL_TREE;
2531 }
2532
2533 /* Start generating the RTL for FN. */
2534
2535 static void
2536 genrtl_start_function (fn)
2537 tree fn;
2538 {
2539 /* Tell everybody what function we're processing. */
2540 current_function_decl = fn;
2541 /* Get the RTL machinery going for this function. */
2542 init_function_start (fn);
2543 /* Let everybody know that we're expanding this function, not doing
2544 semantic analysis. */
2545 expanding_p = 1;
2546
2547 /* Even though we're inside a function body, we still don't want to
2548 call expand_expr to calculate the size of a variable-sized array.
2549 We haven't necessarily assigned RTL to all variables yet, so it's
2550 not safe to try to expand expressions involving them. */
2551 immediate_size_expand = 0;
2552 cfun->x_dont_save_pending_sizes_p = 1;
2553
2554 /* Let the user know we're compiling this function. */
2555 announce_function (fn);
2556
2557 /* Initialize the per-function data. */
2558 my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2559 if (DECL_SAVED_FUNCTION_DATA (fn))
2560 {
2561 /* If we already parsed this function, and we're just expanding it
2562 now, restore saved state. */
2563 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2564
2565 /* This function is being processed in whole-function mode; we
2566 already did semantic analysis. */
2567 cfun->x_whole_function_mode_p = 1;
2568
2569 /* If we decided that we didn't want to inline this function,
2570 make sure the back-end knows that. */
2571 if (!current_function_cannot_inline)
2572 current_function_cannot_inline = cp_function_chain->cannot_inline;
2573
2574 /* We don't need the saved data anymore. Unless this is an inline
2575 function; we need the named return value info for
2576 cp_copy_res_decl_for_inlining. */
2577 if (! DECL_INLINE (fn))
2578 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2579 }
2580
2581 /* Keep track of how many functions we're presently expanding. */
2582 ++function_depth;
2583
2584 /* Create a binding level for the parameters. */
2585 expand_function_start (fn, /*parms_have_cleanups=*/0);
2586 /* If this function is `main'. */
2587 if (DECL_MAIN_P (fn))
2588 expand_main_function ();
2589
2590 /* Give our named return value the same RTL as our RESULT_DECL. */
2591 if (current_function_return_value)
2592 COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2593 }
2594
2595 /* Finish generating the RTL for FN. */
2596
2597 static void
2598 genrtl_finish_function (fn)
2599 tree fn;
2600 {
2601 tree t;
2602
2603 #if 0
2604 if (write_symbols != NO_DEBUG)
2605 {
2606 /* Keep this code around in case we later want to control debug info
2607 based on whether a type is "used". (jason 1999-11-11) */
2608
2609 tree ttype = target_type (fntype);
2610 tree parmdecl;
2611
2612 if (IS_AGGR_TYPE (ttype))
2613 /* Let debugger know it should output info for this type. */
2614 note_debug_info_needed (ttype);
2615
2616 for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2617 {
2618 ttype = target_type (TREE_TYPE (parmdecl));
2619 if (IS_AGGR_TYPE (ttype))
2620 /* Let debugger know it should output info for this type. */
2621 note_debug_info_needed (ttype);
2622 }
2623 }
2624 #endif
2625
2626 /* Clean house because we will need to reorder insns here. */
2627 do_pending_stack_adjust ();
2628
2629 /* If we have a named return value, we need to force a return so that
2630 the return register is USEd. */
2631 if (DECL_NAME (DECL_RESULT (fn)))
2632 emit_jump (return_label);
2633
2634 /* We hard-wired immediate_size_expand to zero in start_function.
2635 Expand_function_end will decrement this variable. So, we set the
2636 variable to one here, so that after the decrement it will remain
2637 zero. */
2638 immediate_size_expand = 1;
2639
2640 /* Generate rtl for function exit. */
2641 expand_function_end (input_filename, input_line, 0);
2642
2643 /* If this is a nested function (like a template instantiation that
2644 we're compiling in the midst of compiling something else), push a
2645 new GC context. That will keep local variables on the stack from
2646 being collected while we're doing the compilation of this
2647 function. */
2648 if (function_depth > 1)
2649 ggc_push_context ();
2650
2651 /* There's no need to defer outputting this function any more; we
2652 know we want to output it. */
2653 DECL_DEFER_OUTPUT (fn) = 0;
2654
2655 /* Run the optimizers and output the assembler code for this
2656 function. */
2657 rest_of_compilation (fn);
2658
2659 /* Undo the call to ggc_push_context above. */
2660 if (function_depth > 1)
2661 ggc_pop_context ();
2662
2663 #if 0
2664 /* Keep this code around in case we later want to control debug info
2665 based on whether a type is "used". (jason 1999-11-11) */
2666
2667 if (ctype && TREE_ASM_WRITTEN (fn))
2668 note_debug_info_needed (ctype);
2669 #endif
2670
2671 /* If this function is marked with the constructor attribute, add it
2672 to the list of functions to be called along with constructors
2673 from static duration objects. */
2674 if (DECL_STATIC_CONSTRUCTOR (fn))
2675 static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2676
2677 /* If this function is marked with the destructor attribute, add it
2678 to the list of functions to be called along with destructors from
2679 static duration objects. */
2680 if (DECL_STATIC_DESTRUCTOR (fn))
2681 static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2682
2683 --function_depth;
2684
2685 /* In C++, we should never be saving RTL for the function. */
2686 my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2687
2688 /* Since we don't need the RTL for this function anymore, stop
2689 pointing to it. That's especially important for LABEL_DECLs,
2690 since you can reach all the instructions in the function from the
2691 CODE_LABEL stored in the DECL_RTL for the LABEL_DECL. Walk the
2692 BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2693 local variables. */
2694 walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2695 clear_decl_rtl,
2696 NULL);
2697
2698 /* Clear out the RTL for the arguments. */
2699 for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2700 {
2701 SET_DECL_RTL (t, NULL_RTX);
2702 DECL_INCOMING_RTL (t) = NULL_RTX;
2703 }
2704
2705 if (!(flag_inline_trees && DECL_INLINE (fn)))
2706 /* DECL_INITIAL must remain nonzero so we know this was an
2707 actual function definition. */
2708 DECL_INITIAL (fn) = error_mark_node;
2709
2710 /* Let the error reporting routines know that we're outside a
2711 function. For a nested function, this value is used in
2712 pop_cp_function_context and then reset via pop_function_context. */
2713 current_function_decl = NULL_TREE;
2714 }
2715
2716 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2717 its sub-blocks. */
2718
2719 static tree
2720 clear_decl_rtl (tp, walk_subtrees, data)
2721 tree *tp;
2722 int *walk_subtrees ATTRIBUTE_UNUSED;
2723 void *data ATTRIBUTE_UNUSED;
2724 {
2725 if (nonstatic_local_decl_p (*tp))
2726 SET_DECL_RTL (*tp, NULL_RTX);
2727
2728 return NULL_TREE;
2729 }
2730
2731 /* Perform initialization related to this module. */
2732
2733 void
2734 init_cp_semantics ()
2735 {
2736 lang_expand_stmt = cp_expand_stmt;
2737 }
2738
2739 #include "gt-cp-semantics.h"