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