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