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