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