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