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