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