Merge branch 'vendor/GCC47'
[dragonfly.git] / contrib / gcc-4.7 / gcc / cp / semantics.c
CommitLineData
e4b17023
JM
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
6 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
7 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
8 Written by Mark Mitchell (mmitchell@usa.net) based on code found
9 formerly in parse.y and pt.c.
10
11 This file is part of GCC.
12
13 GCC is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3, or (at your option)
16 any later version.
17
18 GCC is distributed in the hope that it will be useful, but
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
23You should have received a copy of the GNU General Public License
24along with GCC; see the file COPYING3. If not see
25<http://www.gnu.org/licenses/>. */
26
27#include "config.h"
28#include "system.h"
29#include "coretypes.h"
30#include "tm.h"
31#include "tree.h"
32#include "cp-tree.h"
33#include "c-family/c-common.h"
34#include "c-family/c-objc.h"
35#include "tree-inline.h"
36#include "intl.h"
37#include "toplev.h"
38#include "flags.h"
39#include "output.h"
40#include "timevar.h"
41#include "diagnostic.h"
42#include "cgraph.h"
43#include "tree-iterator.h"
44#include "vec.h"
45#include "target.h"
46#include "gimple.h"
47#include "bitmap.h"
48
49/* There routines provide a modular interface to perform many parsing
50 operations. They may therefore be used during actual parsing, or
51 during template instantiation, which may be regarded as a
52 degenerate form of parsing. */
53
54static tree maybe_convert_cond (tree);
55static tree finalize_nrv_r (tree *, int *, void *);
56static tree capture_decltype (tree);
57
58
59/* Deferred Access Checking Overview
60 ---------------------------------
61
62 Most C++ expressions and declarations require access checking
63 to be performed during parsing. However, in several cases,
64 this has to be treated differently.
65
66 For member declarations, access checking has to be deferred
67 until more information about the declaration is known. For
68 example:
69
70 class A {
71 typedef int X;
72 public:
73 X f();
74 };
75
76 A::X A::f();
77 A::X g();
78
79 When we are parsing the function return type `A::X', we don't
80 really know if this is allowed until we parse the function name.
81
82 Furthermore, some contexts require that access checking is
83 never performed at all. These include class heads, and template
84 instantiations.
85
86 Typical use of access checking functions is described here:
87
88 1. When we enter a context that requires certain access checking
89 mode, the function `push_deferring_access_checks' is called with
90 DEFERRING argument specifying the desired mode. Access checking
91 may be performed immediately (dk_no_deferred), deferred
92 (dk_deferred), or not performed (dk_no_check).
93
94 2. When a declaration such as a type, or a variable, is encountered,
95 the function `perform_or_defer_access_check' is called. It
96 maintains a VEC of all deferred checks.
97
98 3. The global `current_class_type' or `current_function_decl' is then
99 setup by the parser. `enforce_access' relies on these information
100 to check access.
101
102 4. Upon exiting the context mentioned in step 1,
103 `perform_deferred_access_checks' is called to check all declaration
104 stored in the VEC. `pop_deferring_access_checks' is then
105 called to restore the previous access checking mode.
106
107 In case of parsing error, we simply call `pop_deferring_access_checks'
108 without `perform_deferred_access_checks'. */
109
110typedef struct GTY(()) deferred_access {
111 /* A VEC representing name-lookups for which we have deferred
112 checking access controls. We cannot check the accessibility of
113 names used in a decl-specifier-seq until we know what is being
114 declared because code like:
115
116 class A {
117 class B {};
118 B* f();
119 }
120
121 A::B* A::f() { return 0; }
122
123 is valid, even though `A::B' is not generally accessible. */
124 VEC (deferred_access_check,gc)* GTY(()) deferred_access_checks;
125
126 /* The current mode of access checks. */
127 enum deferring_kind deferring_access_checks_kind;
128
129} deferred_access;
130DEF_VEC_O (deferred_access);
131DEF_VEC_ALLOC_O (deferred_access,gc);
132
133/* Data for deferred access checking. */
134static GTY(()) VEC(deferred_access,gc) *deferred_access_stack;
135static GTY(()) unsigned deferred_access_no_check;
136
137/* Save the current deferred access states and start deferred
138 access checking iff DEFER_P is true. */
139
140void
141push_deferring_access_checks (deferring_kind deferring)
142{
143 /* For context like template instantiation, access checking
144 disabling applies to all nested context. */
145 if (deferred_access_no_check || deferring == dk_no_check)
146 deferred_access_no_check++;
147 else
148 {
149 deferred_access *ptr;
150
151 ptr = VEC_safe_push (deferred_access, gc, deferred_access_stack, NULL);
152 ptr->deferred_access_checks = NULL;
153 ptr->deferring_access_checks_kind = deferring;
154 }
155}
156
157/* Resume deferring access checks again after we stopped doing
158 this previously. */
159
160void
161resume_deferring_access_checks (void)
162{
163 if (!deferred_access_no_check)
164 VEC_last (deferred_access, deferred_access_stack)
165 ->deferring_access_checks_kind = dk_deferred;
166}
167
168/* Stop deferring access checks. */
169
170void
171stop_deferring_access_checks (void)
172{
173 if (!deferred_access_no_check)
174 VEC_last (deferred_access, deferred_access_stack)
175 ->deferring_access_checks_kind = dk_no_deferred;
176}
177
178/* Discard the current deferred access checks and restore the
179 previous states. */
180
181void
182pop_deferring_access_checks (void)
183{
184 if (deferred_access_no_check)
185 deferred_access_no_check--;
186 else
187 VEC_pop (deferred_access, deferred_access_stack);
188}
189
190/* Returns a TREE_LIST representing the deferred checks.
191 The TREE_PURPOSE of each node is the type through which the
192 access occurred; the TREE_VALUE is the declaration named.
193 */
194
195VEC (deferred_access_check,gc)*
196get_deferred_access_checks (void)
197{
198 if (deferred_access_no_check)
199 return NULL;
200 else
201 return (VEC_last (deferred_access, deferred_access_stack)
202 ->deferred_access_checks);
203}
204
205/* Take current deferred checks and combine with the
206 previous states if we also defer checks previously.
207 Otherwise perform checks now. */
208
209void
210pop_to_parent_deferring_access_checks (void)
211{
212 if (deferred_access_no_check)
213 deferred_access_no_check--;
214 else
215 {
216 VEC (deferred_access_check,gc) *checks;
217 deferred_access *ptr;
218
219 checks = (VEC_last (deferred_access, deferred_access_stack)
220 ->deferred_access_checks);
221
222 VEC_pop (deferred_access, deferred_access_stack);
223 ptr = VEC_last (deferred_access, deferred_access_stack);
224 if (ptr->deferring_access_checks_kind == dk_no_deferred)
225 {
226 /* Check access. */
227 perform_access_checks (checks);
228 }
229 else
230 {
231 /* Merge with parent. */
232 int i, j;
233 deferred_access_check *chk, *probe;
234
235 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
236 {
237 FOR_EACH_VEC_ELT (deferred_access_check,
238 ptr->deferred_access_checks, j, probe)
239 {
240 if (probe->binfo == chk->binfo &&
241 probe->decl == chk->decl &&
242 probe->diag_decl == chk->diag_decl)
243 goto found;
244 }
245 /* Insert into parent's checks. */
246 VEC_safe_push (deferred_access_check, gc,
247 ptr->deferred_access_checks, chk);
248 found:;
249 }
250 }
251 }
252}
253
254/* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
255 is the BINFO indicating the qualifying scope used to access the
256 DECL node stored in the TREE_VALUE of the node. */
257
258void
259perform_access_checks (VEC (deferred_access_check,gc)* checks)
260{
261 int i;
262 deferred_access_check *chk;
263
264 if (!checks)
265 return;
266
267 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
268 enforce_access (chk->binfo, chk->decl, chk->diag_decl);
269}
270
271/* Perform the deferred access checks.
272
273 After performing the checks, we still have to keep the list
274 `deferred_access_stack->deferred_access_checks' since we may want
275 to check access for them again later in a different context.
276 For example:
277
278 class A {
279 typedef int X;
280 static X a;
281 };
282 A::X A::a, x; // No error for `A::a', error for `x'
283
284 We have to perform deferred access of `A::X', first with `A::a',
285 next with `x'. */
286
287void
288perform_deferred_access_checks (void)
289{
290 perform_access_checks (get_deferred_access_checks ());
291}
292
293/* Defer checking the accessibility of DECL, when looked up in
294 BINFO. DIAG_DECL is the declaration to use to print diagnostics. */
295
296void
297perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl)
298{
299 int i;
300 deferred_access *ptr;
301 deferred_access_check *chk;
302 deferred_access_check *new_access;
303
304
305 /* Exit if we are in a context that no access checking is performed.
306 */
307 if (deferred_access_no_check)
308 return;
309
310 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
311
312 ptr = VEC_last (deferred_access, deferred_access_stack);
313
314 /* If we are not supposed to defer access checks, just check now. */
315 if (ptr->deferring_access_checks_kind == dk_no_deferred)
316 {
317 enforce_access (binfo, decl, diag_decl);
318 return;
319 }
320
321 /* See if we are already going to perform this check. */
322 FOR_EACH_VEC_ELT (deferred_access_check,
323 ptr->deferred_access_checks, i, chk)
324 {
325 if (chk->decl == decl && chk->binfo == binfo &&
326 chk->diag_decl == diag_decl)
327 {
328 return;
329 }
330 }
331 /* If not, record the check. */
332 new_access =
333 VEC_safe_push (deferred_access_check, gc,
334 ptr->deferred_access_checks, 0);
335 new_access->binfo = binfo;
336 new_access->decl = decl;
337 new_access->diag_decl = diag_decl;
338}
339
340/* Used by build_over_call in LOOKUP_SPECULATIVE mode: return whether DECL
341 is accessible in BINFO, and possibly complain if not. If we're not
342 checking access, everything is accessible. */
343
344bool
345speculative_access_check (tree binfo, tree decl, tree diag_decl,
346 bool complain)
347{
348 if (deferred_access_no_check)
349 return true;
350
351 /* If we're checking for implicit delete, we don't want access
352 control errors. */
353 if (!accessible_p (binfo, decl, true))
354 {
355 /* Unless we're under maybe_explain_implicit_delete. */
356 if (complain)
357 enforce_access (binfo, decl, diag_decl);
358 return false;
359 }
360
361 return true;
362}
363
364/* Returns nonzero if the current statement is a full expression,
365 i.e. temporaries created during that statement should be destroyed
366 at the end of the statement. */
367
368int
369stmts_are_full_exprs_p (void)
370{
371 return current_stmt_tree ()->stmts_are_full_exprs_p;
372}
373
374/* T is a statement. Add it to the statement-tree. This is the C++
375 version. The C/ObjC frontends have a slightly different version of
376 this function. */
377
378tree
379add_stmt (tree t)
380{
381 enum tree_code code = TREE_CODE (t);
382
383 if (EXPR_P (t) && code != LABEL_EXPR)
384 {
385 if (!EXPR_HAS_LOCATION (t))
386 SET_EXPR_LOCATION (t, input_location);
387
388 /* When we expand a statement-tree, we must know whether or not the
389 statements are full-expressions. We record that fact here. */
390 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
391 }
392
393 /* Add T to the statement-tree. Non-side-effect statements need to be
394 recorded during statement expressions. */
395 gcc_checking_assert (!VEC_empty (tree, stmt_list_stack));
396 append_to_statement_list_force (t, &cur_stmt_list);
397
398 return t;
399}
400
401/* Returns the stmt_tree to which statements are currently being added. */
402
403stmt_tree
404current_stmt_tree (void)
405{
406 return (cfun
407 ? &cfun->language->base.x_stmt_tree
408 : &scope_chain->x_stmt_tree);
409}
410
411/* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
412
413static tree
414maybe_cleanup_point_expr (tree expr)
415{
416 if (!processing_template_decl && stmts_are_full_exprs_p ())
417 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
418 return expr;
419}
420
421/* Like maybe_cleanup_point_expr except have the type of the new expression be
422 void so we don't need to create a temporary variable to hold the inner
423 expression. The reason why we do this is because the original type might be
424 an aggregate and we cannot create a temporary variable for that type. */
425
426tree
427maybe_cleanup_point_expr_void (tree expr)
428{
429 if (!processing_template_decl && stmts_are_full_exprs_p ())
430 expr = fold_build_cleanup_point_expr (void_type_node, expr);
431 return expr;
432}
433
434
435
436/* Create a declaration statement for the declaration given by the DECL. */
437
438void
439add_decl_expr (tree decl)
440{
441 tree r = build_stmt (input_location, DECL_EXPR, decl);
442 if (DECL_INITIAL (decl)
443 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
444 r = maybe_cleanup_point_expr_void (r);
445 add_stmt (r);
446}
447
448/* Finish a scope. */
449
450tree
451do_poplevel (tree stmt_list)
452{
453 tree block = NULL;
454
455 if (stmts_are_full_exprs_p ())
456 block = poplevel (kept_level_p (), 1, 0);
457
458 stmt_list = pop_stmt_list (stmt_list);
459
460 if (!processing_template_decl)
461 {
462 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
463 /* ??? See c_end_compound_stmt re statement expressions. */
464 }
465
466 return stmt_list;
467}
468
469/* Begin a new scope. */
470
471static tree
472do_pushlevel (scope_kind sk)
473{
474 tree ret = push_stmt_list ();
475 if (stmts_are_full_exprs_p ())
476 begin_scope (sk, NULL);
477 return ret;
478}
479
480/* Queue a cleanup. CLEANUP is an expression/statement to be executed
481 when the current scope is exited. EH_ONLY is true when this is not
482 meant to apply to normal control flow transfer. */
483
484void
485push_cleanup (tree decl, tree cleanup, bool eh_only)
486{
487 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
488 CLEANUP_EH_ONLY (stmt) = eh_only;
489 add_stmt (stmt);
490 CLEANUP_BODY (stmt) = push_stmt_list ();
491}
492
493/* Begin a conditional that might contain a declaration. When generating
494 normal code, we want the declaration to appear before the statement
495 containing the conditional. When generating template code, we want the
496 conditional to be rendered as the raw DECL_EXPR. */
497
498static void
499begin_cond (tree *cond_p)
500{
501 if (processing_template_decl)
502 *cond_p = push_stmt_list ();
503}
504
505/* Finish such a conditional. */
506
507static void
508finish_cond (tree *cond_p, tree expr)
509{
510 if (processing_template_decl)
511 {
512 tree cond = pop_stmt_list (*cond_p);
513 if (TREE_CODE (cond) == DECL_EXPR)
514 expr = cond;
515
516 if (check_for_bare_parameter_packs (expr))
517 *cond_p = error_mark_node;
518 }
519 *cond_p = expr;
520}
521
522/* If *COND_P specifies a conditional with a declaration, transform the
523 loop such that
524 while (A x = 42) { }
525 for (; A x = 42;) { }
526 becomes
527 while (true) { A x = 42; if (!x) break; }
528 for (;;) { A x = 42; if (!x) break; }
529 The statement list for BODY will be empty if the conditional did
530 not declare anything. */
531
532static void
533simplify_loop_decl_cond (tree *cond_p, tree body)
534{
535 tree cond, if_stmt;
536
537 if (!TREE_SIDE_EFFECTS (body))
538 return;
539
540 cond = *cond_p;
541 *cond_p = boolean_true_node;
542
543 if_stmt = begin_if_stmt ();
544 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
545 finish_if_stmt_cond (cond, if_stmt);
546 finish_break_stmt ();
547 finish_then_clause (if_stmt);
548 finish_if_stmt (if_stmt);
549}
550
551/* Finish a goto-statement. */
552
553tree
554finish_goto_stmt (tree destination)
555{
556 if (TREE_CODE (destination) == IDENTIFIER_NODE)
557 destination = lookup_label (destination);
558
559 /* We warn about unused labels with -Wunused. That means we have to
560 mark the used labels as used. */
561 if (TREE_CODE (destination) == LABEL_DECL)
562 TREE_USED (destination) = 1;
563 else
564 {
565 destination = mark_rvalue_use (destination);
566 if (!processing_template_decl)
567 {
568 destination = cp_convert (ptr_type_node, destination);
569 if (error_operand_p (destination))
570 return NULL_TREE;
571 }
572 }
573
574 check_goto (destination);
575
576 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
577}
578
579/* COND is the condition-expression for an if, while, etc.,
580 statement. Convert it to a boolean value, if appropriate.
581 In addition, verify sequence points if -Wsequence-point is enabled. */
582
583static tree
584maybe_convert_cond (tree cond)
585{
586 /* Empty conditions remain empty. */
587 if (!cond)
588 return NULL_TREE;
589
590 /* Wait until we instantiate templates before doing conversion. */
591 if (processing_template_decl)
592 return cond;
593
594 if (warn_sequence_point)
595 verify_sequence_points (cond);
596
597 /* Do the conversion. */
598 cond = convert_from_reference (cond);
599
600 if (TREE_CODE (cond) == MODIFY_EXPR
601 && !TREE_NO_WARNING (cond)
602 && warn_parentheses)
603 {
604 warning (OPT_Wparentheses,
605 "suggest parentheses around assignment used as truth value");
606 TREE_NO_WARNING (cond) = 1;
607 }
608
609 return condition_conversion (cond);
610}
611
612/* Finish an expression-statement, whose EXPRESSION is as indicated. */
613
614tree
615finish_expr_stmt (tree expr)
616{
617 tree r = NULL_TREE;
618
619 if (expr != NULL_TREE)
620 {
621 if (!processing_template_decl)
622 {
623 if (warn_sequence_point)
624 verify_sequence_points (expr);
625 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
626 }
627 else if (!type_dependent_expression_p (expr))
628 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
629 tf_warning_or_error);
630
631 if (check_for_bare_parameter_packs (expr))
632 expr = error_mark_node;
633
634 /* Simplification of inner statement expressions, compound exprs,
635 etc can result in us already having an EXPR_STMT. */
636 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
637 {
638 if (TREE_CODE (expr) != EXPR_STMT)
639 expr = build_stmt (input_location, EXPR_STMT, expr);
640 expr = maybe_cleanup_point_expr_void (expr);
641 }
642
643 r = add_stmt (expr);
644 }
645
646 finish_stmt ();
647
648 return r;
649}
650
651
652/* Begin an if-statement. Returns a newly created IF_STMT if
653 appropriate. */
654
655tree
656begin_if_stmt (void)
657{
658 tree r, scope;
659 scope = do_pushlevel (sk_cond);
660 r = build_stmt (input_location, IF_STMT, NULL_TREE,
661 NULL_TREE, NULL_TREE, scope);
662 begin_cond (&IF_COND (r));
663 return r;
664}
665
666/* Process the COND of an if-statement, which may be given by
667 IF_STMT. */
668
669void
670finish_if_stmt_cond (tree cond, tree if_stmt)
671{
672 finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
673 add_stmt (if_stmt);
674 THEN_CLAUSE (if_stmt) = push_stmt_list ();
675}
676
677/* Finish the then-clause of an if-statement, which may be given by
678 IF_STMT. */
679
680tree
681finish_then_clause (tree if_stmt)
682{
683 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
684 return if_stmt;
685}
686
687/* Begin the else-clause of an if-statement. */
688
689void
690begin_else_clause (tree if_stmt)
691{
692 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
693}
694
695/* Finish the else-clause of an if-statement, which may be given by
696 IF_STMT. */
697
698void
699finish_else_clause (tree if_stmt)
700{
701 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
702}
703
704/* Finish an if-statement. */
705
706void
707finish_if_stmt (tree if_stmt)
708{
709 tree scope = IF_SCOPE (if_stmt);
710 IF_SCOPE (if_stmt) = NULL;
711 add_stmt (do_poplevel (scope));
712 finish_stmt ();
713}
714
715/* Begin a while-statement. Returns a newly created WHILE_STMT if
716 appropriate. */
717
718tree
719begin_while_stmt (void)
720{
721 tree r;
722 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
723 add_stmt (r);
724 WHILE_BODY (r) = do_pushlevel (sk_block);
725 begin_cond (&WHILE_COND (r));
726 return r;
727}
728
729/* Process the COND of a while-statement, which may be given by
730 WHILE_STMT. */
731
732void
733finish_while_stmt_cond (tree cond, tree while_stmt)
734{
735 finish_cond (&WHILE_COND (while_stmt), maybe_convert_cond (cond));
736 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
737}
738
739/* Finish a while-statement, which may be given by WHILE_STMT. */
740
741void
742finish_while_stmt (tree while_stmt)
743{
744 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
745 finish_stmt ();
746}
747
748/* Begin a do-statement. Returns a newly created DO_STMT if
749 appropriate. */
750
751tree
752begin_do_stmt (void)
753{
754 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
755 add_stmt (r);
756 DO_BODY (r) = push_stmt_list ();
757 return r;
758}
759
760/* Finish the body of a do-statement, which may be given by DO_STMT. */
761
762void
763finish_do_body (tree do_stmt)
764{
765 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
766
767 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
768 body = STATEMENT_LIST_TAIL (body)->stmt;
769
770 if (IS_EMPTY_STMT (body))
771 warning (OPT_Wempty_body,
772 "suggest explicit braces around empty body in %<do%> statement");
773}
774
775/* Finish a do-statement, which may be given by DO_STMT, and whose
776 COND is as indicated. */
777
778void
779finish_do_stmt (tree cond, tree do_stmt)
780{
781 cond = maybe_convert_cond (cond);
782 DO_COND (do_stmt) = cond;
783 finish_stmt ();
784}
785
786/* Finish a return-statement. The EXPRESSION returned, if any, is as
787 indicated. */
788
789tree
790finish_return_stmt (tree expr)
791{
792 tree r;
793 bool no_warning;
794
795 expr = check_return_expr (expr, &no_warning);
796
797 if (flag_openmp && !check_omp_return ())
798 return error_mark_node;
799 if (!processing_template_decl)
800 {
801 if (warn_sequence_point)
802 verify_sequence_points (expr);
803
804 if (DECL_DESTRUCTOR_P (current_function_decl)
805 || (DECL_CONSTRUCTOR_P (current_function_decl)
806 && targetm.cxx.cdtor_returns_this ()))
807 {
808 /* Similarly, all destructors must run destructors for
809 base-classes before returning. So, all returns in a
810 destructor get sent to the DTOR_LABEL; finish_function emits
811 code to return a value there. */
812 return finish_goto_stmt (cdtor_label);
813 }
814 }
815
816 r = build_stmt (input_location, RETURN_EXPR, expr);
817 TREE_NO_WARNING (r) |= no_warning;
818 r = maybe_cleanup_point_expr_void (r);
819 r = add_stmt (r);
820 finish_stmt ();
821
822 return r;
823}
824
825/* Begin the scope of a for-statement or a range-for-statement.
826 Both the returned trees are to be used in a call to
827 begin_for_stmt or begin_range_for_stmt. */
828
829tree
830begin_for_scope (tree *init)
831{
832 tree scope = NULL_TREE;
833 if (flag_new_for_scope > 0)
834 scope = do_pushlevel (sk_for);
835
836 if (processing_template_decl)
837 *init = push_stmt_list ();
838 else
839 *init = NULL_TREE;
840
841 return scope;
842}
843
844/* Begin a for-statement. Returns a new FOR_STMT.
845 SCOPE and INIT should be the return of begin_for_scope,
846 or both NULL_TREE */
847
848tree
849begin_for_stmt (tree scope, tree init)
850{
851 tree r;
852
853 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
854 NULL_TREE, NULL_TREE, NULL_TREE);
855
856 if (scope == NULL_TREE)
857 {
858 gcc_assert (!init || !(flag_new_for_scope > 0));
859 if (!init)
860 scope = begin_for_scope (&init);
861 }
862 FOR_INIT_STMT (r) = init;
863 FOR_SCOPE (r) = scope;
864
865 return r;
866}
867
868/* Finish the for-init-statement of a for-statement, which may be
869 given by FOR_STMT. */
870
871void
872finish_for_init_stmt (tree for_stmt)
873{
874 if (processing_template_decl)
875 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
876 add_stmt (for_stmt);
877 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
878 begin_cond (&FOR_COND (for_stmt));
879}
880
881/* Finish the COND of a for-statement, which may be given by
882 FOR_STMT. */
883
884void
885finish_for_cond (tree cond, tree for_stmt)
886{
887 finish_cond (&FOR_COND (for_stmt), maybe_convert_cond (cond));
888 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
889}
890
891/* Finish the increment-EXPRESSION in a for-statement, which may be
892 given by FOR_STMT. */
893
894void
895finish_for_expr (tree expr, tree for_stmt)
896{
897 if (!expr)
898 return;
899 /* If EXPR is an overloaded function, issue an error; there is no
900 context available to use to perform overload resolution. */
901 if (type_unknown_p (expr))
902 {
903 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
904 expr = error_mark_node;
905 }
906 if (!processing_template_decl)
907 {
908 if (warn_sequence_point)
909 verify_sequence_points (expr);
910 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
911 tf_warning_or_error);
912 }
913 else if (!type_dependent_expression_p (expr))
914 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
915 tf_warning_or_error);
916 expr = maybe_cleanup_point_expr_void (expr);
917 if (check_for_bare_parameter_packs (expr))
918 expr = error_mark_node;
919 FOR_EXPR (for_stmt) = expr;
920}
921
922/* Finish the body of a for-statement, which may be given by
923 FOR_STMT. The increment-EXPR for the loop must be
924 provided.
925 It can also finish RANGE_FOR_STMT. */
926
927void
928finish_for_stmt (tree for_stmt)
929{
930 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
931 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
932 else
933 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
934
935 /* Pop the scope for the body of the loop. */
936 if (flag_new_for_scope > 0)
937 {
938 tree scope;
939 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
940 ? &RANGE_FOR_SCOPE (for_stmt)
941 : &FOR_SCOPE (for_stmt));
942 scope = *scope_ptr;
943 *scope_ptr = NULL;
944 add_stmt (do_poplevel (scope));
945 }
946
947 finish_stmt ();
948}
949
950/* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
951 SCOPE and INIT should be the return of begin_for_scope,
952 or both NULL_TREE .
953 To finish it call finish_for_stmt(). */
954
955tree
956begin_range_for_stmt (tree scope, tree init)
957{
958 tree r;
959
960 r = build_stmt (input_location, RANGE_FOR_STMT,
961 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
962
963 if (scope == NULL_TREE)
964 {
965 gcc_assert (!init || !(flag_new_for_scope > 0));
966 if (!init)
967 scope = begin_for_scope (&init);
968 }
969
970 /* RANGE_FOR_STMTs do not use nor save the init tree, so we
971 pop it now. */
972 if (init)
973 pop_stmt_list (init);
974 RANGE_FOR_SCOPE (r) = scope;
975
976 return r;
977}
978
979/* Finish the head of a range-based for statement, which may
980 be given by RANGE_FOR_STMT. DECL must be the declaration
981 and EXPR must be the loop expression. */
982
983void
984finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
985{
986 RANGE_FOR_DECL (range_for_stmt) = decl;
987 RANGE_FOR_EXPR (range_for_stmt) = expr;
988 add_stmt (range_for_stmt);
989 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
990}
991
992/* Finish a break-statement. */
993
994tree
995finish_break_stmt (void)
996{
997 /* In switch statements break is sometimes stylistically used after
998 a return statement. This can lead to spurious warnings about
999 control reaching the end of a non-void function when it is
1000 inlined. Note that we are calling block_may_fallthru with
1001 language specific tree nodes; this works because
1002 block_may_fallthru returns true when given something it does not
1003 understand. */
1004 if (!block_may_fallthru (cur_stmt_list))
1005 return void_zero_node;
1006 return add_stmt (build_stmt (input_location, BREAK_STMT));
1007}
1008
1009/* Finish a continue-statement. */
1010
1011tree
1012finish_continue_stmt (void)
1013{
1014 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1015}
1016
1017/* Begin a switch-statement. Returns a new SWITCH_STMT if
1018 appropriate. */
1019
1020tree
1021begin_switch_stmt (void)
1022{
1023 tree r, scope;
1024
1025 scope = do_pushlevel (sk_cond);
1026 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1027
1028 begin_cond (&SWITCH_STMT_COND (r));
1029
1030 return r;
1031}
1032
1033/* Finish the cond of a switch-statement. */
1034
1035void
1036finish_switch_cond (tree cond, tree switch_stmt)
1037{
1038 tree orig_type = NULL;
1039 if (!processing_template_decl)
1040 {
1041 /* Convert the condition to an integer or enumeration type. */
1042 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1043 if (cond == NULL_TREE)
1044 {
1045 error ("switch quantity not an integer");
1046 cond = error_mark_node;
1047 }
1048 orig_type = TREE_TYPE (cond);
1049 if (cond != error_mark_node)
1050 {
1051 /* [stmt.switch]
1052
1053 Integral promotions are performed. */
1054 cond = perform_integral_promotions (cond);
1055 cond = maybe_cleanup_point_expr (cond);
1056 }
1057 }
1058 if (check_for_bare_parameter_packs (cond))
1059 cond = error_mark_node;
1060 else if (!processing_template_decl && warn_sequence_point)
1061 verify_sequence_points (cond);
1062
1063 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1064 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1065 add_stmt (switch_stmt);
1066 push_switch (switch_stmt);
1067 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1068}
1069
1070/* Finish the body of a switch-statement, which may be given by
1071 SWITCH_STMT. The COND to switch on is indicated. */
1072
1073void
1074finish_switch_stmt (tree switch_stmt)
1075{
1076 tree scope;
1077
1078 SWITCH_STMT_BODY (switch_stmt) =
1079 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1080 pop_switch ();
1081 finish_stmt ();
1082
1083 scope = SWITCH_STMT_SCOPE (switch_stmt);
1084 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1085 add_stmt (do_poplevel (scope));
1086}
1087
1088/* Begin a try-block. Returns a newly-created TRY_BLOCK if
1089 appropriate. */
1090
1091tree
1092begin_try_block (void)
1093{
1094 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1095 add_stmt (r);
1096 TRY_STMTS (r) = push_stmt_list ();
1097 return r;
1098}
1099
1100/* Likewise, for a function-try-block. The block returned in
1101 *COMPOUND_STMT is an artificial outer scope, containing the
1102 function-try-block. */
1103
1104tree
1105begin_function_try_block (tree *compound_stmt)
1106{
1107 tree r;
1108 /* This outer scope does not exist in the C++ standard, but we need
1109 a place to put __FUNCTION__ and similar variables. */
1110 *compound_stmt = begin_compound_stmt (0);
1111 r = begin_try_block ();
1112 FN_TRY_BLOCK_P (r) = 1;
1113 return r;
1114}
1115
1116/* Finish a try-block, which may be given by TRY_BLOCK. */
1117
1118void
1119finish_try_block (tree try_block)
1120{
1121 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1122 TRY_HANDLERS (try_block) = push_stmt_list ();
1123}
1124
1125/* Finish the body of a cleanup try-block, which may be given by
1126 TRY_BLOCK. */
1127
1128void
1129finish_cleanup_try_block (tree try_block)
1130{
1131 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1132}
1133
1134/* Finish an implicitly generated try-block, with a cleanup is given
1135 by CLEANUP. */
1136
1137void
1138finish_cleanup (tree cleanup, tree try_block)
1139{
1140 TRY_HANDLERS (try_block) = cleanup;
1141 CLEANUP_P (try_block) = 1;
1142}
1143
1144/* Likewise, for a function-try-block. */
1145
1146void
1147finish_function_try_block (tree try_block)
1148{
1149 finish_try_block (try_block);
1150 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1151 the try block, but moving it inside. */
1152 in_function_try_handler = 1;
1153}
1154
1155/* Finish a handler-sequence for a try-block, which may be given by
1156 TRY_BLOCK. */
1157
1158void
1159finish_handler_sequence (tree try_block)
1160{
1161 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1162 check_handlers (TRY_HANDLERS (try_block));
1163}
1164
1165/* Finish the handler-seq for a function-try-block, given by
1166 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1167 begin_function_try_block. */
1168
1169void
1170finish_function_handler_sequence (tree try_block, tree compound_stmt)
1171{
1172 in_function_try_handler = 0;
1173 finish_handler_sequence (try_block);
1174 finish_compound_stmt (compound_stmt);
1175}
1176
1177/* Begin a handler. Returns a HANDLER if appropriate. */
1178
1179tree
1180begin_handler (void)
1181{
1182 tree r;
1183
1184 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1185 add_stmt (r);
1186
1187 /* Create a binding level for the eh_info and the exception object
1188 cleanup. */
1189 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1190
1191 return r;
1192}
1193
1194/* Finish the handler-parameters for a handler, which may be given by
1195 HANDLER. DECL is the declaration for the catch parameter, or NULL
1196 if this is a `catch (...)' clause. */
1197
1198void
1199finish_handler_parms (tree decl, tree handler)
1200{
1201 tree type = NULL_TREE;
1202 if (processing_template_decl)
1203 {
1204 if (decl)
1205 {
1206 decl = pushdecl (decl);
1207 decl = push_template_decl (decl);
1208 HANDLER_PARMS (handler) = decl;
1209 type = TREE_TYPE (decl);
1210 }
1211 }
1212 else
1213 type = expand_start_catch_block (decl);
1214 HANDLER_TYPE (handler) = type;
1215 if (!processing_template_decl && type)
1216 mark_used (eh_type_info (type));
1217}
1218
1219/* Finish a handler, which may be given by HANDLER. The BLOCKs are
1220 the return value from the matching call to finish_handler_parms. */
1221
1222void
1223finish_handler (tree handler)
1224{
1225 if (!processing_template_decl)
1226 expand_end_catch_block ();
1227 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1228}
1229
1230/* Begin a compound statement. FLAGS contains some bits that control the
1231 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1232 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1233 block of a function. If BCS_TRY_BLOCK is set, this is the block
1234 created on behalf of a TRY statement. Returns a token to be passed to
1235 finish_compound_stmt. */
1236
1237tree
1238begin_compound_stmt (unsigned int flags)
1239{
1240 tree r;
1241
1242 if (flags & BCS_NO_SCOPE)
1243 {
1244 r = push_stmt_list ();
1245 STATEMENT_LIST_NO_SCOPE (r) = 1;
1246
1247 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1248 But, if it's a statement-expression with a scopeless block, there's
1249 nothing to keep, and we don't want to accidentally keep a block
1250 *inside* the scopeless block. */
1251 keep_next_level (false);
1252 }
1253 else
1254 r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1255
1256 /* When processing a template, we need to remember where the braces were,
1257 so that we can set up identical scopes when instantiating the template
1258 later. BIND_EXPR is a handy candidate for this.
1259 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1260 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1261 processing templates. */
1262 if (processing_template_decl)
1263 {
1264 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1265 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1266 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1267 TREE_SIDE_EFFECTS (r) = 1;
1268 }
1269
1270 return r;
1271}
1272
1273/* Finish a compound-statement, which is given by STMT. */
1274
1275void
1276finish_compound_stmt (tree stmt)
1277{
1278 if (TREE_CODE (stmt) == BIND_EXPR)
1279 {
1280 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1281 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1282 discard the BIND_EXPR so it can be merged with the containing
1283 STATEMENT_LIST. */
1284 if (TREE_CODE (body) == STATEMENT_LIST
1285 && STATEMENT_LIST_HEAD (body) == NULL
1286 && !BIND_EXPR_BODY_BLOCK (stmt)
1287 && !BIND_EXPR_TRY_BLOCK (stmt))
1288 stmt = body;
1289 else
1290 BIND_EXPR_BODY (stmt) = body;
1291 }
1292 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1293 stmt = pop_stmt_list (stmt);
1294 else
1295 {
1296 /* Destroy any ObjC "super" receivers that may have been
1297 created. */
1298 objc_clear_super_receiver ();
1299
1300 stmt = do_poplevel (stmt);
1301 }
1302
1303 /* ??? See c_end_compound_stmt wrt statement expressions. */
1304 add_stmt (stmt);
1305 finish_stmt ();
1306}
1307
1308/* Finish an asm-statement, whose components are a STRING, some
1309 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1310 LABELS. Also note whether the asm-statement should be
1311 considered volatile. */
1312
1313tree
1314finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1315 tree input_operands, tree clobbers, tree labels)
1316{
1317 tree r;
1318 tree t;
1319 int ninputs = list_length (input_operands);
1320 int noutputs = list_length (output_operands);
1321
1322 if (!processing_template_decl)
1323 {
1324 const char *constraint;
1325 const char **oconstraints;
1326 bool allows_mem, allows_reg, is_inout;
1327 tree operand;
1328 int i;
1329
1330 oconstraints = XALLOCAVEC (const char *, noutputs);
1331
1332 string = resolve_asm_operand_names (string, output_operands,
1333 input_operands, labels);
1334
1335 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1336 {
1337 operand = TREE_VALUE (t);
1338
1339 /* ??? Really, this should not be here. Users should be using a
1340 proper lvalue, dammit. But there's a long history of using
1341 casts in the output operands. In cases like longlong.h, this
1342 becomes a primitive form of typechecking -- if the cast can be
1343 removed, then the output operand had a type of the proper width;
1344 otherwise we'll get an error. Gross, but ... */
1345 STRIP_NOPS (operand);
1346
1347 operand = mark_lvalue_use (operand);
1348
1349 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1350 operand = error_mark_node;
1351
1352 if (operand != error_mark_node
1353 && (TREE_READONLY (operand)
1354 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1355 /* Functions are not modifiable, even though they are
1356 lvalues. */
1357 || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1358 || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1359 /* If it's an aggregate and any field is const, then it is
1360 effectively const. */
1361 || (CLASS_TYPE_P (TREE_TYPE (operand))
1362 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1363 cxx_readonly_error (operand, lv_asm);
1364
1365 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1366 oconstraints[i] = constraint;
1367
1368 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1369 &allows_mem, &allows_reg, &is_inout))
1370 {
1371 /* If the operand is going to end up in memory,
1372 mark it addressable. */
1373 if (!allows_reg && !cxx_mark_addressable (operand))
1374 operand = error_mark_node;
1375 }
1376 else
1377 operand = error_mark_node;
1378
1379 TREE_VALUE (t) = operand;
1380 }
1381
1382 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1383 {
1384 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1385 operand = decay_conversion (TREE_VALUE (t));
1386
1387 /* If the type of the operand hasn't been determined (e.g.,
1388 because it involves an overloaded function), then issue
1389 an error message. There's no context available to
1390 resolve the overloading. */
1391 if (TREE_TYPE (operand) == unknown_type_node)
1392 {
1393 error ("type of asm operand %qE could not be determined",
1394 TREE_VALUE (t));
1395 operand = error_mark_node;
1396 }
1397
1398 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1399 oconstraints, &allows_mem, &allows_reg))
1400 {
1401 /* If the operand is going to end up in memory,
1402 mark it addressable. */
1403 if (!allows_reg && allows_mem)
1404 {
1405 /* Strip the nops as we allow this case. FIXME, this really
1406 should be rejected or made deprecated. */
1407 STRIP_NOPS (operand);
1408 if (!cxx_mark_addressable (operand))
1409 operand = error_mark_node;
1410 }
1411 }
1412 else
1413 operand = error_mark_node;
1414
1415 TREE_VALUE (t) = operand;
1416 }
1417 }
1418
1419 r = build_stmt (input_location, ASM_EXPR, string,
1420 output_operands, input_operands,
1421 clobbers, labels);
1422 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1423 r = maybe_cleanup_point_expr_void (r);
1424 return add_stmt (r);
1425}
1426
1427/* Finish a label with the indicated NAME. Returns the new label. */
1428
1429tree
1430finish_label_stmt (tree name)
1431{
1432 tree decl = define_label (input_location, name);
1433
1434 if (decl == error_mark_node)
1435 return error_mark_node;
1436
1437 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1438
1439 return decl;
1440}
1441
1442/* Finish a series of declarations for local labels. G++ allows users
1443 to declare "local" labels, i.e., labels with scope. This extension
1444 is useful when writing code involving statement-expressions. */
1445
1446void
1447finish_label_decl (tree name)
1448{
1449 if (!at_function_scope_p ())
1450 {
1451 error ("__label__ declarations are only allowed in function scopes");
1452 return;
1453 }
1454
1455 add_decl_expr (declare_local_label (name));
1456}
1457
1458/* When DECL goes out of scope, make sure that CLEANUP is executed. */
1459
1460void
1461finish_decl_cleanup (tree decl, tree cleanup)
1462{
1463 push_cleanup (decl, cleanup, false);
1464}
1465
1466/* If the current scope exits with an exception, run CLEANUP. */
1467
1468void
1469finish_eh_cleanup (tree cleanup)
1470{
1471 push_cleanup (NULL, cleanup, true);
1472}
1473
1474/* The MEM_INITS is a list of mem-initializers, in reverse of the
1475 order they were written by the user. Each node is as for
1476 emit_mem_initializers. */
1477
1478void
1479finish_mem_initializers (tree mem_inits)
1480{
1481 /* Reorder the MEM_INITS so that they are in the order they appeared
1482 in the source program. */
1483 mem_inits = nreverse (mem_inits);
1484
1485 if (processing_template_decl)
1486 {
1487 tree mem;
1488
1489 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1490 {
1491 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1492 check for bare parameter packs in the TREE_VALUE, because
1493 any parameter packs in the TREE_VALUE have already been
1494 bound as part of the TREE_PURPOSE. See
1495 make_pack_expansion for more information. */
1496 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1497 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1498 TREE_VALUE (mem) = error_mark_node;
1499 }
1500
1501 add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1502 }
1503 else
1504 emit_mem_initializers (mem_inits);
1505}
1506
1507/* Finish a parenthesized expression EXPR. */
1508
1509tree
1510finish_parenthesized_expr (tree expr)
1511{
1512 if (EXPR_P (expr))
1513 /* This inhibits warnings in c_common_truthvalue_conversion. */
1514 TREE_NO_WARNING (expr) = 1;
1515
1516 if (TREE_CODE (expr) == OFFSET_REF
1517 || TREE_CODE (expr) == SCOPE_REF)
1518 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1519 enclosed in parentheses. */
1520 PTRMEM_OK_P (expr) = 0;
1521
1522 if (TREE_CODE (expr) == STRING_CST)
1523 PAREN_STRING_LITERAL_P (expr) = 1;
1524
1525 return expr;
1526}
1527
1528/* Finish a reference to a non-static data member (DECL) that is not
1529 preceded by `.' or `->'. */
1530
1531tree
1532finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1533{
1534 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1535
1536 if (!object)
1537 {
1538 tree scope = qualifying_scope;
1539 if (scope == NULL_TREE)
1540 scope = context_for_name_lookup (decl);
1541 object = maybe_dummy_object (scope, NULL);
1542 }
1543
1544 if (object == error_mark_node)
1545 return error_mark_node;
1546
1547 /* DR 613: Can use non-static data members without an associated
1548 object in sizeof/decltype/alignof. */
1549 if (is_dummy_object (object) && cp_unevaluated_operand == 0
1550 && (!processing_template_decl || !current_class_ref))
1551 {
1552 if (current_function_decl
1553 && DECL_STATIC_FUNCTION_P (current_function_decl))
1554 error ("invalid use of member %q+D in static member function", decl);
1555 else
1556 error ("invalid use of non-static data member %q+D", decl);
1557 error ("from this location");
1558
1559 return error_mark_node;
1560 }
1561
1562 if (current_class_ptr)
1563 TREE_USED (current_class_ptr) = 1;
1564 if (processing_template_decl && !qualifying_scope)
1565 {
1566 tree type = TREE_TYPE (decl);
1567
1568 if (TREE_CODE (type) == REFERENCE_TYPE)
1569 /* Quals on the object don't matter. */;
1570 else
1571 {
1572 /* Set the cv qualifiers. */
5ce9237c 1573 int quals = cp_type_quals (TREE_TYPE (object));
e4b17023
JM
1574
1575 if (DECL_MUTABLE_P (decl))
1576 quals &= ~TYPE_QUAL_CONST;
1577
1578 quals |= cp_type_quals (TREE_TYPE (decl));
1579 type = cp_build_qualified_type (type, quals);
1580 }
1581
1582 return (convert_from_reference
1583 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1584 }
1585 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1586 QUALIFYING_SCOPE is also non-null. Wrap this in a SCOPE_REF
1587 for now. */
1588 else if (processing_template_decl)
1589 return build_qualified_name (TREE_TYPE (decl),
1590 qualifying_scope,
1591 DECL_NAME (decl),
1592 /*template_p=*/false);
1593 else
1594 {
1595 tree access_type = TREE_TYPE (object);
1596
1597 perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1598 decl);
1599
1600 /* If the data member was named `C::M', convert `*this' to `C'
1601 first. */
1602 if (qualifying_scope)
1603 {
1604 tree binfo = NULL_TREE;
1605 object = build_scoped_ref (object, qualifying_scope,
1606 &binfo);
1607 }
1608
1609 return build_class_member_access_expr (object, decl,
1610 /*access_path=*/NULL_TREE,
1611 /*preserve_reference=*/false,
1612 tf_warning_or_error);
1613 }
1614}
1615
1616/* If we are currently parsing a template and we encountered a typedef
1617 TYPEDEF_DECL that is being accessed though CONTEXT, this function
1618 adds the typedef to a list tied to the current template.
1619 At tempate instantiatin time, that list is walked and access check
1620 performed for each typedef.
1621 LOCATION is the location of the usage point of TYPEDEF_DECL. */
1622
1623void
1624add_typedef_to_current_template_for_access_check (tree typedef_decl,
1625 tree context,
1626 location_t location)
1627{
1628 tree template_info = NULL;
1629 tree cs = current_scope ();
1630
1631 if (!is_typedef_decl (typedef_decl)
1632 || !context
1633 || !CLASS_TYPE_P (context)
1634 || !cs)
1635 return;
1636
1637 if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1638 template_info = get_template_info (cs);
1639
1640 if (template_info
1641 && TI_TEMPLATE (template_info)
1642 && !currently_open_class (context))
1643 append_type_to_template_for_access_check (cs, typedef_decl,
1644 context, location);
1645}
1646
1647/* DECL was the declaration to which a qualified-id resolved. Issue
1648 an error message if it is not accessible. If OBJECT_TYPE is
1649 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1650 type of `*x', or `x', respectively. If the DECL was named as
1651 `A::B' then NESTED_NAME_SPECIFIER is `A'. */
1652
1653void
1654check_accessibility_of_qualified_id (tree decl,
1655 tree object_type,
1656 tree nested_name_specifier)
1657{
1658 tree scope;
1659 tree qualifying_type = NULL_TREE;
1660
1661 /* If we are parsing a template declaration and if decl is a typedef,
1662 add it to a list tied to the template.
1663 At template instantiation time, that list will be walked and
1664 access check performed. */
1665 add_typedef_to_current_template_for_access_check (decl,
1666 nested_name_specifier
1667 ? nested_name_specifier
1668 : DECL_CONTEXT (decl),
1669 input_location);
1670
1671 /* If we're not checking, return immediately. */
1672 if (deferred_access_no_check)
1673 return;
1674
1675 /* Determine the SCOPE of DECL. */
1676 scope = context_for_name_lookup (decl);
1677 /* If the SCOPE is not a type, then DECL is not a member. */
1678 if (!TYPE_P (scope))
1679 return;
1680 /* Compute the scope through which DECL is being accessed. */
1681 if (object_type
1682 /* OBJECT_TYPE might not be a class type; consider:
1683
1684 class A { typedef int I; };
1685 I *p;
1686 p->A::I::~I();
1687
1688 In this case, we will have "A::I" as the DECL, but "I" as the
1689 OBJECT_TYPE. */
1690 && CLASS_TYPE_P (object_type)
1691 && DERIVED_FROM_P (scope, object_type))
1692 /* If we are processing a `->' or `.' expression, use the type of the
1693 left-hand side. */
1694 qualifying_type = object_type;
1695 else if (nested_name_specifier)
1696 {
1697 /* If the reference is to a non-static member of the
1698 current class, treat it as if it were referenced through
1699 `this'. */
1700 if (DECL_NONSTATIC_MEMBER_P (decl)
1701 && current_class_ptr
1702 && DERIVED_FROM_P (scope, current_class_type))
1703 qualifying_type = current_class_type;
1704 /* Otherwise, use the type indicated by the
1705 nested-name-specifier. */
1706 else
1707 qualifying_type = nested_name_specifier;
1708 }
1709 else
1710 /* Otherwise, the name must be from the current class or one of
1711 its bases. */
1712 qualifying_type = currently_open_derived_class (scope);
1713
1714 if (qualifying_type
1715 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1716 or similar in a default argument value. */
1717 && CLASS_TYPE_P (qualifying_type)
1718 && !dependent_type_p (qualifying_type))
1719 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1720 decl);
1721}
1722
1723/* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
1724 class named to the left of the "::" operator. DONE is true if this
1725 expression is a complete postfix-expression; it is false if this
1726 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
1727 iff this expression is the operand of '&'. TEMPLATE_P is true iff
1728 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
1729 is true iff this qualified name appears as a template argument. */
1730
1731tree
1732finish_qualified_id_expr (tree qualifying_class,
1733 tree expr,
1734 bool done,
1735 bool address_p,
1736 bool template_p,
1737 bool template_arg_p)
1738{
1739 gcc_assert (TYPE_P (qualifying_class));
1740
1741 if (error_operand_p (expr))
1742 return error_mark_node;
1743
1744 if (DECL_P (expr) || BASELINK_P (expr))
1745 mark_used (expr);
1746
1747 if (template_p)
1748 check_template_keyword (expr);
1749
1750 /* If EXPR occurs as the operand of '&', use special handling that
1751 permits a pointer-to-member. */
1752 if (address_p && done)
1753 {
1754 if (TREE_CODE (expr) == SCOPE_REF)
1755 expr = TREE_OPERAND (expr, 1);
1756 expr = build_offset_ref (qualifying_class, expr,
1757 /*address_p=*/true);
1758 return expr;
1759 }
1760
1761 /* Within the scope of a class, turn references to non-static
1762 members into expression of the form "this->...". */
1763 if (template_arg_p)
1764 /* But, within a template argument, we do not want make the
1765 transformation, as there is no "this" pointer. */
1766 ;
1767 else if (TREE_CODE (expr) == FIELD_DECL)
1768 {
1769 push_deferring_access_checks (dk_no_check);
1770 expr = finish_non_static_data_member (expr, NULL_TREE,
1771 qualifying_class);
1772 pop_deferring_access_checks ();
1773 }
1774 else if (BASELINK_P (expr) && !processing_template_decl)
1775 {
1776 tree ob;
1777
1778 /* See if any of the functions are non-static members. */
1779 /* If so, the expression may be relative to 'this'. */
1780 if (!shared_member_p (expr)
1781 && (ob = maybe_dummy_object (qualifying_class, NULL),
1782 !is_dummy_object (ob)))
1783 expr = (build_class_member_access_expr
1784 (ob,
1785 expr,
1786 BASELINK_ACCESS_BINFO (expr),
1787 /*preserve_reference=*/false,
1788 tf_warning_or_error));
1789 else if (done)
1790 /* The expression is a qualified name whose address is not
1791 being taken. */
1792 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false);
1793 }
1794
1795 return expr;
1796}
1797
1798/* Begin a statement-expression. The value returned must be passed to
1799 finish_stmt_expr. */
1800
1801tree
1802begin_stmt_expr (void)
1803{
1804 return push_stmt_list ();
1805}
1806
1807/* Process the final expression of a statement expression. EXPR can be
1808 NULL, if the final expression is empty. Return a STATEMENT_LIST
1809 containing all the statements in the statement-expression, or
1810 ERROR_MARK_NODE if there was an error. */
1811
1812tree
1813finish_stmt_expr_expr (tree expr, tree stmt_expr)
1814{
1815 if (error_operand_p (expr))
1816 {
1817 /* The type of the statement-expression is the type of the last
1818 expression. */
1819 TREE_TYPE (stmt_expr) = error_mark_node;
1820 return error_mark_node;
1821 }
1822
1823 /* If the last statement does not have "void" type, then the value
1824 of the last statement is the value of the entire expression. */
1825 if (expr)
1826 {
1827 tree type = TREE_TYPE (expr);
1828
1829 if (processing_template_decl)
1830 {
1831 expr = build_stmt (input_location, EXPR_STMT, expr);
1832 expr = add_stmt (expr);
1833 /* Mark the last statement so that we can recognize it as such at
1834 template-instantiation time. */
1835 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
1836 }
1837 else if (VOID_TYPE_P (type))
1838 {
1839 /* Just treat this like an ordinary statement. */
1840 expr = finish_expr_stmt (expr);
1841 }
1842 else
1843 {
1844 /* It actually has a value we need to deal with. First, force it
1845 to be an rvalue so that we won't need to build up a copy
1846 constructor call later when we try to assign it to something. */
1847 expr = force_rvalue (expr, tf_warning_or_error);
1848 if (error_operand_p (expr))
1849 return error_mark_node;
1850
1851 /* Update for array-to-pointer decay. */
1852 type = TREE_TYPE (expr);
1853
1854 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
1855 normal statement, but don't convert to void or actually add
1856 the EXPR_STMT. */
1857 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1858 expr = maybe_cleanup_point_expr (expr);
1859 add_stmt (expr);
1860 }
1861
1862 /* The type of the statement-expression is the type of the last
1863 expression. */
1864 TREE_TYPE (stmt_expr) = type;
1865 }
1866
1867 return stmt_expr;
1868}
1869
1870/* Finish a statement-expression. EXPR should be the value returned
1871 by the previous begin_stmt_expr. Returns an expression
1872 representing the statement-expression. */
1873
1874tree
1875finish_stmt_expr (tree stmt_expr, bool has_no_scope)
1876{
1877 tree type;
1878 tree result;
1879
1880 if (error_operand_p (stmt_expr))
1881 {
1882 pop_stmt_list (stmt_expr);
1883 return error_mark_node;
1884 }
1885
1886 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
1887
1888 type = TREE_TYPE (stmt_expr);
1889 result = pop_stmt_list (stmt_expr);
1890 TREE_TYPE (result) = type;
1891
1892 if (processing_template_decl)
1893 {
1894 result = build_min (STMT_EXPR, type, result);
1895 TREE_SIDE_EFFECTS (result) = 1;
1896 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
1897 }
1898 else if (CLASS_TYPE_P (type))
1899 {
1900 /* Wrap the statement-expression in a TARGET_EXPR so that the
1901 temporary object created by the final expression is destroyed at
1902 the end of the full-expression containing the
1903 statement-expression. */
1904 result = force_target_expr (type, result, tf_warning_or_error);
1905 }
1906
1907 return result;
1908}
1909
1910/* Returns the expression which provides the value of STMT_EXPR. */
1911
1912tree
1913stmt_expr_value_expr (tree stmt_expr)
1914{
1915 tree t = STMT_EXPR_STMT (stmt_expr);
1916
1917 if (TREE_CODE (t) == BIND_EXPR)
1918 t = BIND_EXPR_BODY (t);
1919
1920 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
1921 t = STATEMENT_LIST_TAIL (t)->stmt;
1922
1923 if (TREE_CODE (t) == EXPR_STMT)
1924 t = EXPR_STMT_EXPR (t);
1925
1926 return t;
1927}
1928
1929/* Return TRUE iff EXPR_STMT is an empty list of
1930 expression statements. */
1931
1932bool
1933empty_expr_stmt_p (tree expr_stmt)
1934{
1935 tree body = NULL_TREE;
1936
1937 if (expr_stmt == void_zero_node)
1938 return true;
1939
1940 if (expr_stmt)
1941 {
1942 if (TREE_CODE (expr_stmt) == EXPR_STMT)
1943 body = EXPR_STMT_EXPR (expr_stmt);
1944 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
1945 body = expr_stmt;
1946 }
1947
1948 if (body)
1949 {
1950 if (TREE_CODE (body) == STATEMENT_LIST)
1951 return tsi_end_p (tsi_start (body));
1952 else
1953 return empty_expr_stmt_p (body);
1954 }
1955 return false;
1956}
1957
1958/* Perform Koenig lookup. FN is the postfix-expression representing
1959 the function (or functions) to call; ARGS are the arguments to the
1960 call; if INCLUDE_STD then the `std' namespace is automatically
1961 considered an associated namespace (used in range-based for loops).
1962 Returns the functions to be considered by overload resolution. */
1963
1964tree
1965perform_koenig_lookup (tree fn, VEC(tree,gc) *args, bool include_std,
1966 tsubst_flags_t complain)
1967{
1968 tree identifier = NULL_TREE;
1969 tree functions = NULL_TREE;
1970 tree tmpl_args = NULL_TREE;
1971 bool template_id = false;
1972
1973 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1974 {
1975 /* Use a separate flag to handle null args. */
1976 template_id = true;
1977 tmpl_args = TREE_OPERAND (fn, 1);
1978 fn = TREE_OPERAND (fn, 0);
1979 }
1980
1981 /* Find the name of the overloaded function. */
1982 if (TREE_CODE (fn) == IDENTIFIER_NODE)
1983 identifier = fn;
1984 else if (is_overloaded_fn (fn))
1985 {
1986 functions = fn;
1987 identifier = DECL_NAME (get_first_fn (functions));
1988 }
1989 else if (DECL_P (fn))
1990 {
1991 functions = fn;
1992 identifier = DECL_NAME (fn);
1993 }
1994
1995 /* A call to a namespace-scope function using an unqualified name.
1996
1997 Do Koenig lookup -- unless any of the arguments are
1998 type-dependent. */
1999 if (!any_type_dependent_arguments_p (args)
2000 && !any_dependent_template_arguments_p (tmpl_args))
2001 {
2002 fn = lookup_arg_dependent (identifier, functions, args, include_std);
2003 if (!fn)
2004 {
2005 /* The unqualified name could not be resolved. */
2006 if (complain)
2007 fn = unqualified_fn_lookup_error (identifier);
2008 else
2009 fn = identifier;
2010 }
2011 }
2012
2013 if (fn && template_id)
2014 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2015
2016 return fn;
2017}
2018
2019/* Generate an expression for `FN (ARGS)'. This may change the
2020 contents of ARGS.
2021
2022 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2023 as a virtual call, even if FN is virtual. (This flag is set when
2024 encountering an expression where the function name is explicitly
2025 qualified. For example a call to `X::f' never generates a virtual
2026 call.)
2027
2028 Returns code for the call. */
2029
2030tree
2031finish_call_expr (tree fn, VEC(tree,gc) **args, bool disallow_virtual,
2032 bool koenig_p, tsubst_flags_t complain)
2033{
2034 tree result;
2035 tree orig_fn;
2036 VEC(tree,gc) *orig_args = NULL;
2037
2038 if (fn == error_mark_node)
2039 return error_mark_node;
2040
2041 gcc_assert (!TYPE_P (fn));
2042
2043 orig_fn = fn;
2044
2045 if (processing_template_decl)
2046 {
2047 /* If the call expression is dependent, build a CALL_EXPR node
2048 with no type; type_dependent_expression_p recognizes
2049 expressions with no type as being dependent. */
2050 if (type_dependent_expression_p (fn)
2051 || any_type_dependent_arguments_p (*args)
2052 /* For a non-static member function that doesn't have an
2053 explicit object argument, we need to specifically
2054 test the type dependency of the "this" pointer because it
2055 is not included in *ARGS even though it is considered to
2056 be part of the list of arguments. Note that this is
2057 related to CWG issues 515 and 1005. */
2058 || (TREE_CODE (fn) != COMPONENT_REF
2059 && non_static_member_function_p (fn)
2060 && current_class_ref
2061 && type_dependent_expression_p (current_class_ref)))
2062 {
2063 result = build_nt_call_vec (fn, *args);
2064 KOENIG_LOOKUP_P (result) = koenig_p;
2065 if (cfun)
2066 {
2067 do
2068 {
2069 tree fndecl = OVL_CURRENT (fn);
2070 if (TREE_CODE (fndecl) != FUNCTION_DECL
2071 || !TREE_THIS_VOLATILE (fndecl))
2072 break;
2073 fn = OVL_NEXT (fn);
2074 }
2075 while (fn);
2076 if (!fn)
2077 current_function_returns_abnormally = 1;
2078 }
2079 return result;
2080 }
2081 orig_args = make_tree_vector_copy (*args);
2082 if (!BASELINK_P (fn)
2083 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2084 && TREE_TYPE (fn) != unknown_type_node)
2085 fn = build_non_dependent_expr (fn);
2086 make_args_non_dependent (*args);
2087 }
2088
2089 if (TREE_CODE (fn) == COMPONENT_REF)
2090 {
2091 tree member = TREE_OPERAND (fn, 1);
2092 if (BASELINK_P (member))
2093 {
2094 tree object = TREE_OPERAND (fn, 0);
2095 return build_new_method_call (object, member,
2096 args, NULL_TREE,
2097 (disallow_virtual
2098 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2099 : LOOKUP_NORMAL),
2100 /*fn_p=*/NULL,
2101 complain);
2102 }
2103 }
2104
2105 if (is_overloaded_fn (fn))
2106 fn = baselink_for_fns (fn);
2107
2108 result = NULL_TREE;
2109 if (BASELINK_P (fn))
2110 {
2111 tree object;
2112
2113 /* A call to a member function. From [over.call.func]:
2114
2115 If the keyword this is in scope and refers to the class of
2116 that member function, or a derived class thereof, then the
2117 function call is transformed into a qualified function call
2118 using (*this) as the postfix-expression to the left of the
2119 . operator.... [Otherwise] a contrived object of type T
2120 becomes the implied object argument.
2121
2122 In this situation:
2123
2124 struct A { void f(); };
2125 struct B : public A {};
2126 struct C : public A { void g() { B::f(); }};
2127
2128 "the class of that member function" refers to `A'. But 11.2
2129 [class.access.base] says that we need to convert 'this' to B* as
2130 part of the access, so we pass 'B' to maybe_dummy_object. */
2131
2132 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2133 NULL);
2134
2135 if (processing_template_decl)
2136 {
2137 if (type_dependent_expression_p (object))
2138 {
2139 tree ret = build_nt_call_vec (orig_fn, orig_args);
2140 release_tree_vector (orig_args);
2141 return ret;
2142 }
2143 object = build_non_dependent_expr (object);
2144 }
2145
2146 result = build_new_method_call (object, fn, args, NULL_TREE,
2147 (disallow_virtual
2148 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2149 : LOOKUP_NORMAL),
2150 /*fn_p=*/NULL,
2151 complain);
2152 }
2153 else if (is_overloaded_fn (fn))
2154 {
2155 /* If the function is an overloaded builtin, resolve it. */
2156 if (TREE_CODE (fn) == FUNCTION_DECL
2157 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2158 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2159 result = resolve_overloaded_builtin (input_location, fn, *args);
2160
2161 if (!result)
2162 /* A call to a namespace-scope function. */
2163 result = build_new_function_call (fn, args, koenig_p, complain);
2164 }
2165 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2166 {
2167 if (!VEC_empty (tree, *args))
2168 error ("arguments to destructor are not allowed");
2169 /* Mark the pseudo-destructor call as having side-effects so
2170 that we do not issue warnings about its use. */
2171 result = build1 (NOP_EXPR,
2172 void_type_node,
2173 TREE_OPERAND (fn, 0));
2174 TREE_SIDE_EFFECTS (result) = 1;
2175 }
2176 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2177 /* If the "function" is really an object of class type, it might
2178 have an overloaded `operator ()'. */
2179 result = build_op_call (fn, args, complain);
2180
2181 if (!result)
2182 /* A call where the function is unknown. */
2183 result = cp_build_function_call_vec (fn, args, complain);
2184
2185 if (processing_template_decl && result != error_mark_node)
2186 {
2187 if (TREE_CODE (result) == INDIRECT_REF)
2188 result = TREE_OPERAND (result, 0);
2189 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2190 SET_EXPR_LOCATION (result, input_location);
2191 KOENIG_LOOKUP_P (result) = koenig_p;
2192 release_tree_vector (orig_args);
2193 result = convert_from_reference (result);
2194 }
2195
2196 if (koenig_p)
2197 {
2198 /* Free garbage OVERLOADs from arg-dependent lookup. */
2199 tree next = NULL_TREE;
2200 for (fn = orig_fn;
2201 fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2202 fn = next)
2203 {
2204 if (processing_template_decl)
2205 /* In a template, we'll re-use them at instantiation time. */
2206 OVL_ARG_DEPENDENT (fn) = false;
2207 else
2208 {
2209 next = OVL_CHAIN (fn);
2210 ggc_free (fn);
2211 }
2212 }
2213 }
2214
2215 return result;
2216}
2217
2218/* Finish a call to a postfix increment or decrement or EXPR. (Which
2219 is indicated by CODE, which should be POSTINCREMENT_EXPR or
2220 POSTDECREMENT_EXPR.) */
2221
2222tree
2223finish_increment_expr (tree expr, enum tree_code code)
2224{
2225 return build_x_unary_op (code, expr, tf_warning_or_error);
2226}
2227
2228/* Finish a use of `this'. Returns an expression for `this'. */
2229
2230tree
2231finish_this_expr (void)
2232{
2233 tree result;
2234
2235 if (current_class_ptr)
2236 {
2237 tree type = TREE_TYPE (current_class_ref);
2238
2239 /* In a lambda expression, 'this' refers to the captured 'this'. */
2240 if (LAMBDA_TYPE_P (type))
2241 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type));
2242 else
2243 result = current_class_ptr;
2244
2245 }
2246 else if (current_function_decl
2247 && DECL_STATIC_FUNCTION_P (current_function_decl))
2248 {
2249 error ("%<this%> is unavailable for static member functions");
2250 result = error_mark_node;
2251 }
2252 else
2253 {
2254 if (current_function_decl)
2255 error ("invalid use of %<this%> in non-member function");
2256 else
2257 error ("invalid use of %<this%> at top level");
2258 result = error_mark_node;
2259 }
2260
2261 return result;
2262}
2263
2264/* Finish a pseudo-destructor expression. If SCOPE is NULL, the
2265 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2266 the TYPE for the type given. If SCOPE is non-NULL, the expression
2267 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
2268
2269tree
2270finish_pseudo_destructor_expr (tree object, tree scope, tree destructor)
2271{
2272 if (object == error_mark_node || destructor == error_mark_node)
2273 return error_mark_node;
2274
2275 gcc_assert (TYPE_P (destructor));
2276
2277 if (!processing_template_decl)
2278 {
2279 if (scope == error_mark_node)
2280 {
2281 error ("invalid qualifying scope in pseudo-destructor name");
2282 return error_mark_node;
2283 }
2284 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2285 {
2286 error ("qualified type %qT does not match destructor name ~%qT",
2287 scope, destructor);
2288 return error_mark_node;
2289 }
2290
2291
2292 /* [expr.pseudo] says both:
2293
2294 The type designated by the pseudo-destructor-name shall be
2295 the same as the object type.
2296
2297 and:
2298
2299 The cv-unqualified versions of the object type and of the
2300 type designated by the pseudo-destructor-name shall be the
2301 same type.
2302
2303 We implement the more generous second sentence, since that is
2304 what most other compilers do. */
2305 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2306 destructor))
2307 {
2308 error ("%qE is not of type %qT", object, destructor);
2309 return error_mark_node;
2310 }
2311 }
2312
2313 return build3 (PSEUDO_DTOR_EXPR, void_type_node, object, scope, destructor);
2314}
2315
2316/* Finish an expression of the form CODE EXPR. */
2317
2318tree
2319finish_unary_op_expr (enum tree_code code, tree expr)
2320{
2321 tree result = build_x_unary_op (code, expr, tf_warning_or_error);
2322 if (TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2323 overflow_warning (input_location, result);
2324
2325 return result;
2326}
2327
2328/* Finish a compound-literal expression. TYPE is the type to which
2329 the CONSTRUCTOR in COMPOUND_LITERAL is being cast. */
2330
2331tree
2332finish_compound_literal (tree type, tree compound_literal,
2333 tsubst_flags_t complain)
2334{
2335 if (type == error_mark_node)
2336 return error_mark_node;
2337
2338 if (TREE_CODE (type) == REFERENCE_TYPE)
2339 {
2340 compound_literal
2341 = finish_compound_literal (TREE_TYPE (type), compound_literal,
2342 complain);
2343 return cp_build_c_cast (type, compound_literal, complain);
2344 }
2345
2346 if (!TYPE_OBJ_P (type))
2347 {
2348 if (complain & tf_error)
2349 error ("compound literal of non-object type %qT", type);
2350 return error_mark_node;
2351 }
2352
2353 if (processing_template_decl)
2354 {
2355 TREE_TYPE (compound_literal) = type;
2356 /* Mark the expression as a compound literal. */
2357 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2358 return compound_literal;
2359 }
2360
2361 type = complete_type (type);
2362
2363 if (TYPE_NON_AGGREGATE_CLASS (type))
2364 {
2365 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2366 everywhere that deals with function arguments would be a pain, so
2367 just wrap it in a TREE_LIST. The parser set a flag so we know
2368 that it came from T{} rather than T({}). */
2369 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2370 compound_literal = build_tree_list (NULL_TREE, compound_literal);
2371 return build_functional_cast (type, compound_literal, complain);
2372 }
2373
2374 if (TREE_CODE (type) == ARRAY_TYPE
2375 && check_array_initializer (NULL_TREE, type, compound_literal))
2376 return error_mark_node;
2377 compound_literal = reshape_init (type, compound_literal, complain);
2378 if (SCALAR_TYPE_P (type)
2379 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2380 && (complain & tf_warning_or_error))
2381 check_narrowing (type, compound_literal);
2382 if (TREE_CODE (type) == ARRAY_TYPE
2383 && TYPE_DOMAIN (type) == NULL_TREE)
2384 {
2385 cp_complete_array_type_or_error (&type, compound_literal,
2386 false, complain);
2387 if (type == error_mark_node)
2388 return error_mark_node;
2389 }
2390 compound_literal = digest_init (type, compound_literal, complain);
2391 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2392 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2393 /* Put static/constant array temporaries in static variables, but always
2394 represent class temporaries with TARGET_EXPR so we elide copies. */
2395 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2396 && TREE_CODE (type) == ARRAY_TYPE
2397 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2398 && initializer_constant_valid_p (compound_literal, type))
2399 {
2400 tree decl = create_temporary_var (type);
2401 DECL_INITIAL (decl) = compound_literal;
2402 TREE_STATIC (decl) = 1;
2403 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2404 {
2405 /* 5.19 says that a constant expression can include an
2406 lvalue-rvalue conversion applied to "a glvalue of literal type
2407 that refers to a non-volatile temporary object initialized
2408 with a constant expression". Rather than try to communicate
2409 that this VAR_DECL is a temporary, just mark it constexpr. */
2410 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2411 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2412 TREE_CONSTANT (decl) = true;
2413 }
2414 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2415 decl = pushdecl_top_level (decl);
2416 DECL_NAME (decl) = make_anon_name ();
2417 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2418 return decl;
2419 }
2420 else
2421 return get_target_expr_sfinae (compound_literal, complain);
2422}
2423
2424/* Return the declaration for the function-name variable indicated by
2425 ID. */
2426
2427tree
2428finish_fname (tree id)
2429{
2430 tree decl;
2431
2432 decl = fname_decl (input_location, C_RID_CODE (id), id);
95d28233
JM
2433 if (processing_template_decl && current_function_decl
2434 && decl != error_mark_node)
e4b17023
JM
2435 decl = DECL_NAME (decl);
2436 return decl;
2437}
2438
2439/* Finish a translation unit. */
2440
2441void
2442finish_translation_unit (void)
2443{
2444 /* In case there were missing closebraces,
2445 get us back to the global binding level. */
2446 pop_everything ();
2447 while (current_namespace != global_namespace)
2448 pop_namespace ();
2449
2450 /* Do file scope __FUNCTION__ et al. */
2451 finish_fname_decls ();
2452}
2453
2454/* Finish a template type parameter, specified as AGGR IDENTIFIER.
2455 Returns the parameter. */
2456
2457tree
2458finish_template_type_parm (tree aggr, tree identifier)
2459{
2460 if (aggr != class_type_node)
2461 {
2462 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2463 aggr = class_type_node;
2464 }
2465
2466 return build_tree_list (aggr, identifier);
2467}
2468
2469/* Finish a template template parameter, specified as AGGR IDENTIFIER.
2470 Returns the parameter. */
2471
2472tree
2473finish_template_template_parm (tree aggr, tree identifier)
2474{
2475 tree decl = build_decl (input_location,
2476 TYPE_DECL, identifier, NULL_TREE);
2477 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2478 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2479 DECL_TEMPLATE_RESULT (tmpl) = decl;
2480 DECL_ARTIFICIAL (decl) = 1;
2481 end_template_decl ();
2482
2483 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2484
2485 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
2486 /*is_primary=*/true, /*is_partial=*/false,
2487 /*is_friend=*/0);
2488
2489 return finish_template_type_parm (aggr, tmpl);
2490}
2491
2492/* ARGUMENT is the default-argument value for a template template
2493 parameter. If ARGUMENT is invalid, issue error messages and return
2494 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
2495
2496tree
2497check_template_template_default_arg (tree argument)
2498{
2499 if (TREE_CODE (argument) != TEMPLATE_DECL
2500 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2501 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2502 {
2503 if (TREE_CODE (argument) == TYPE_DECL)
2504 error ("invalid use of type %qT as a default value for a template "
2505 "template-parameter", TREE_TYPE (argument));
2506 else
2507 error ("invalid default argument for a template template parameter");
2508 return error_mark_node;
2509 }
2510
2511 return argument;
2512}
2513
2514/* Begin a class definition, as indicated by T. */
2515
2516tree
2517begin_class_definition (tree t)
2518{
2519 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2520 return error_mark_node;
2521
2522 if (processing_template_parmlist)
2523 {
2524 error ("definition of %q#T inside template parameter list", t);
2525 return error_mark_node;
2526 }
2527
2528 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2529 are passed the same as decimal scalar types. */
2530 if (TREE_CODE (t) == RECORD_TYPE
2531 && !processing_template_decl)
2532 {
2533 tree ns = TYPE_CONTEXT (t);
2534 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2535 && DECL_CONTEXT (ns) == std_node
2536 && DECL_NAME (ns)
2537 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2538 {
2539 const char *n = TYPE_NAME_STRING (t);
2540 if ((strcmp (n, "decimal32") == 0)
2541 || (strcmp (n, "decimal64") == 0)
2542 || (strcmp (n, "decimal128") == 0))
2543 TYPE_TRANSPARENT_AGGR (t) = 1;
2544 }
2545 }
2546
2547 /* A non-implicit typename comes from code like:
2548
2549 template <typename T> struct A {
2550 template <typename U> struct A<T>::B ...
2551
2552 This is erroneous. */
2553 else if (TREE_CODE (t) == TYPENAME_TYPE)
2554 {
2555 error ("invalid definition of qualified type %qT", t);
2556 t = error_mark_node;
2557 }
2558
2559 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2560 {
2561 t = make_class_type (RECORD_TYPE);
2562 pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2563 }
2564
2565 if (TYPE_BEING_DEFINED (t))
2566 {
2567 t = make_class_type (TREE_CODE (t));
2568 pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2569 }
2570 maybe_process_partial_specialization (t);
2571 pushclass (t);
2572 TYPE_BEING_DEFINED (t) = 1;
2573
2574 if (flag_pack_struct)
2575 {
2576 tree v;
2577 TYPE_PACKED (t) = 1;
2578 /* Even though the type is being defined for the first time
2579 here, there might have been a forward declaration, so there
2580 might be cv-qualified variants of T. */
2581 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2582 TYPE_PACKED (v) = 1;
2583 }
2584 /* Reset the interface data, at the earliest possible
2585 moment, as it might have been set via a class foo;
2586 before. */
2587 if (! TYPE_ANONYMOUS_P (t))
2588 {
2589 struct c_fileinfo *finfo = get_fileinfo (input_filename);
2590 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2591 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2592 (t, finfo->interface_unknown);
2593 }
2594 reset_specialization();
2595
2596 /* Make a declaration for this class in its own scope. */
2597 build_self_reference ();
2598
2599 return t;
2600}
2601
2602/* Finish the member declaration given by DECL. */
2603
2604void
2605finish_member_declaration (tree decl)
2606{
2607 if (decl == error_mark_node || decl == NULL_TREE)
2608 return;
2609
2610 if (decl == void_type_node)
2611 /* The COMPONENT was a friend, not a member, and so there's
2612 nothing for us to do. */
2613 return;
2614
2615 /* We should see only one DECL at a time. */
2616 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2617
2618 /* Set up access control for DECL. */
2619 TREE_PRIVATE (decl)
2620 = (current_access_specifier == access_private_node);
2621 TREE_PROTECTED (decl)
2622 = (current_access_specifier == access_protected_node);
2623 if (TREE_CODE (decl) == TEMPLATE_DECL)
2624 {
2625 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2626 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2627 }
2628
2629 /* Mark the DECL as a member of the current class. */
2630 DECL_CONTEXT (decl) = current_class_type;
2631
2632 /* Check for bare parameter packs in the member variable declaration. */
2633 if (TREE_CODE (decl) == FIELD_DECL)
2634 {
2635 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2636 TREE_TYPE (decl) = error_mark_node;
2637 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2638 DECL_ATTRIBUTES (decl) = NULL_TREE;
2639 }
2640
2641 /* [dcl.link]
2642
2643 A C language linkage is ignored for the names of class members
2644 and the member function type of class member functions. */
2645 if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2646 SET_DECL_LANGUAGE (decl, lang_cplusplus);
2647
2648 /* Put functions on the TYPE_METHODS list and everything else on the
2649 TYPE_FIELDS list. Note that these are built up in reverse order.
2650 We reverse them (to obtain declaration order) in finish_struct. */
2651 if (TREE_CODE (decl) == FUNCTION_DECL
2652 || DECL_FUNCTION_TEMPLATE_P (decl))
2653 {
2654 /* We also need to add this function to the
2655 CLASSTYPE_METHOD_VEC. */
2656 if (add_method (current_class_type, decl, NULL_TREE))
2657 {
2658 DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2659 TYPE_METHODS (current_class_type) = decl;
2660
2661 maybe_add_class_template_decl_list (current_class_type, decl,
2662 /*friend_p=*/0);
2663 }
2664 }
2665 /* Enter the DECL into the scope of the class. */
2666 else if (pushdecl_class_level (decl))
2667 {
2668 if (TREE_CODE (decl) == USING_DECL)
2669 {
2670 /* For now, ignore class-scope USING_DECLS, so that
2671 debugging backends do not see them. */
2672 DECL_IGNORED_P (decl) = 1;
2673 }
2674
2675 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
2676 go at the beginning. The reason is that lookup_field_1
2677 searches the list in order, and we want a field name to
2678 override a type name so that the "struct stat hack" will
2679 work. In particular:
2680
2681 struct S { enum E { }; int E } s;
2682 s.E = 3;
2683
2684 is valid. In addition, the FIELD_DECLs must be maintained in
2685 declaration order so that class layout works as expected.
2686 However, we don't need that order until class layout, so we
2687 save a little time by putting FIELD_DECLs on in reverse order
2688 here, and then reversing them in finish_struct_1. (We could
2689 also keep a pointer to the correct insertion points in the
2690 list.) */
2691
2692 if (TREE_CODE (decl) == TYPE_DECL)
2693 TYPE_FIELDS (current_class_type)
2694 = chainon (TYPE_FIELDS (current_class_type), decl);
2695 else
2696 {
2697 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2698 TYPE_FIELDS (current_class_type) = decl;
2699 }
2700
2701 maybe_add_class_template_decl_list (current_class_type, decl,
2702 /*friend_p=*/0);
2703 }
2704
2705 if (pch_file)
2706 note_decl_for_pch (decl);
2707}
2708
2709/* DECL has been declared while we are building a PCH file. Perform
2710 actions that we might normally undertake lazily, but which can be
2711 performed now so that they do not have to be performed in
2712 translation units which include the PCH file. */
2713
2714void
2715note_decl_for_pch (tree decl)
2716{
2717 gcc_assert (pch_file);
2718
2719 /* There's a good chance that we'll have to mangle names at some
2720 point, even if only for emission in debugging information. */
2721 if ((TREE_CODE (decl) == VAR_DECL
2722 || TREE_CODE (decl) == FUNCTION_DECL)
2723 && !processing_template_decl)
2724 mangle_decl (decl);
2725}
2726
2727/* Finish processing a complete template declaration. The PARMS are
2728 the template parameters. */
2729
2730void
2731finish_template_decl (tree parms)
2732{
2733 if (parms)
2734 end_template_decl ();
2735 else
2736 end_specialization ();
2737}
2738
2739/* Finish processing a template-id (which names a type) of the form
2740 NAME < ARGS >. Return the TYPE_DECL for the type named by the
2741 template-id. If ENTERING_SCOPE is nonzero we are about to enter
2742 the scope of template-id indicated. */
2743
2744tree
2745finish_template_type (tree name, tree args, int entering_scope)
2746{
2747 tree type;
2748
2749 type = lookup_template_class (name, args,
2750 NULL_TREE, NULL_TREE, entering_scope,
2751 tf_warning_or_error | tf_user);
2752 if (type == error_mark_node)
2753 return type;
2754 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
2755 return TYPE_STUB_DECL (type);
2756 else
2757 return TYPE_NAME (type);
2758}
2759
2760/* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2761 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2762 BASE_CLASS, or NULL_TREE if an error occurred. The
2763 ACCESS_SPECIFIER is one of
2764 access_{default,public,protected_private}_node. For a virtual base
2765 we set TREE_TYPE. */
2766
2767tree
2768finish_base_specifier (tree base, tree access, bool virtual_p)
2769{
2770 tree result;
2771
2772 if (base == error_mark_node)
2773 {
2774 error ("invalid base-class specification");
2775 result = NULL_TREE;
2776 }
2777 else if (! MAYBE_CLASS_TYPE_P (base))
2778 {
2779 error ("%qT is not a class type", base);
2780 result = NULL_TREE;
2781 }
2782 else
2783 {
2784 if (cp_type_quals (base) != 0)
2785 {
2786 /* DR 484: Can a base-specifier name a cv-qualified
2787 class type? */
2788 base = TYPE_MAIN_VARIANT (base);
2789 }
2790 result = build_tree_list (access, base);
2791 if (virtual_p)
2792 TREE_TYPE (result) = integer_type_node;
2793 }
2794
2795 return result;
2796}
2797
2798/* If FNS is a member function, a set of member functions, or a
2799 template-id referring to one or more member functions, return a
2800 BASELINK for FNS, incorporating the current access context.
2801 Otherwise, return FNS unchanged. */
2802
2803tree
2804baselink_for_fns (tree fns)
2805{
2806 tree scope;
2807 tree cl;
2808
2809 if (BASELINK_P (fns)
2810 || error_operand_p (fns))
2811 return fns;
2812
2813 scope = ovl_scope (fns);
2814 if (!CLASS_TYPE_P (scope))
2815 return fns;
2816
2817 cl = currently_open_derived_class (scope);
2818 if (!cl)
2819 cl = scope;
2820 cl = TYPE_BINFO (cl);
2821 return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
2822}
2823
5ce9237c 2824/* Returns true iff DECL is a variable from a function outside
e4b17023
JM
2825 the current one. */
2826
2827static bool
5ce9237c 2828outer_var_p (tree decl)
e4b17023
JM
2829{
2830 return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
2831 && DECL_FUNCTION_SCOPE_P (decl)
e4b17023
JM
2832 && DECL_CONTEXT (decl) != current_function_decl);
2833}
2834
5ce9237c
JM
2835/* As above, but also checks that DECL is automatic. */
2836
2837static bool
2838outer_automatic_var_p (tree decl)
2839{
2840 return (outer_var_p (decl)
2841 && !TREE_STATIC (decl));
2842}
2843
e4b17023
JM
2844/* ID_EXPRESSION is a representation of parsed, but unprocessed,
2845 id-expression. (See cp_parser_id_expression for details.) SCOPE,
2846 if non-NULL, is the type or namespace used to explicitly qualify
2847 ID_EXPRESSION. DECL is the entity to which that name has been
2848 resolved.
2849
2850 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
2851 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
2852 be set to true if this expression isn't permitted in a
2853 constant-expression, but it is otherwise not set by this function.
2854 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
2855 constant-expression, but a non-constant expression is also
2856 permissible.
2857
2858 DONE is true if this expression is a complete postfix-expression;
2859 it is false if this expression is followed by '->', '[', '(', etc.
2860 ADDRESS_P is true iff this expression is the operand of '&'.
2861 TEMPLATE_P is true iff the qualified-id was of the form
2862 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
2863 appears as a template argument.
2864
2865 If an error occurs, and it is the kind of error that might cause
2866 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
2867 is the caller's responsibility to issue the message. *ERROR_MSG
2868 will be a string with static storage duration, so the caller need
2869 not "free" it.
2870
2871 Return an expression for the entity, after issuing appropriate
2872 diagnostics. This function is also responsible for transforming a
2873 reference to a non-static member into a COMPONENT_REF that makes
2874 the use of "this" explicit.
2875
2876 Upon return, *IDK will be filled in appropriately. */
2877tree
2878finish_id_expression (tree id_expression,
2879 tree decl,
2880 tree scope,
2881 cp_id_kind *idk,
2882 bool integral_constant_expression_p,
2883 bool allow_non_integral_constant_expression_p,
2884 bool *non_integral_constant_expression_p,
2885 bool template_p,
2886 bool done,
2887 bool address_p,
2888 bool template_arg_p,
2889 const char **error_msg,
2890 location_t location)
2891{
2892 decl = strip_using_decl (decl);
2893
2894 /* Initialize the output parameters. */
2895 *idk = CP_ID_KIND_NONE;
2896 *error_msg = NULL;
2897
2898 if (id_expression == error_mark_node)
2899 return error_mark_node;
2900 /* If we have a template-id, then no further lookup is
2901 required. If the template-id was for a template-class, we
2902 will sometimes have a TYPE_DECL at this point. */
2903 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2904 || TREE_CODE (decl) == TYPE_DECL)
2905 ;
2906 /* Look up the name. */
2907 else
2908 {
2909 if (decl == error_mark_node)
2910 {
2911 /* Name lookup failed. */
2912 if (scope
2913 && (!TYPE_P (scope)
2914 || (!dependent_type_p (scope)
2915 && !(TREE_CODE (id_expression) == IDENTIFIER_NODE
2916 && IDENTIFIER_TYPENAME_P (id_expression)
2917 && dependent_type_p (TREE_TYPE (id_expression))))))
2918 {
2919 /* If the qualifying type is non-dependent (and the name
2920 does not name a conversion operator to a dependent
2921 type), issue an error. */
2922 qualified_name_lookup_error (scope, id_expression, decl, location);
2923 return error_mark_node;
2924 }
2925 else if (!scope)
2926 {
2927 /* It may be resolved via Koenig lookup. */
2928 *idk = CP_ID_KIND_UNQUALIFIED;
2929 return id_expression;
2930 }
2931 else
2932 decl = id_expression;
2933 }
2934 /* If DECL is a variable that would be out of scope under
2935 ANSI/ISO rules, but in scope in the ARM, name lookup
2936 will succeed. Issue a diagnostic here. */
2937 else
2938 decl = check_for_out_of_scope_variable (decl);
2939
2940 /* Remember that the name was used in the definition of
2941 the current class so that we can check later to see if
2942 the meaning would have been different after the class
2943 was entirely defined. */
2944 if (!scope && decl != error_mark_node
2945 && TREE_CODE (id_expression) == IDENTIFIER_NODE)
2946 maybe_note_name_used_in_class (id_expression, decl);
2947
2948 /* Disallow uses of local variables from containing functions, except
2949 within lambda-expressions. */
5ce9237c 2950 if (!outer_var_p (decl)
e4b17023 2951 /* It's not a use (3.2) if we're in an unevaluated context. */
5ce9237c
JM
2952 || cp_unevaluated_operand)
2953 /* OK. */;
2954 else if (TREE_STATIC (decl))
2955 {
2956 if (processing_template_decl)
2957 /* For a use of an outer static var, return the identifier so
2958 that we'll look it up again in the instantiation. */
2959 return id_expression;
2960 }
2961 else
e4b17023
JM
2962 {
2963 tree context = DECL_CONTEXT (decl);
2964 tree containing_function = current_function_decl;
2965 tree lambda_stack = NULL_TREE;
2966 tree lambda_expr = NULL_TREE;
2967 tree initializer = convert_from_reference (decl);
2968
2969 /* Mark it as used now even if the use is ill-formed. */
2970 mark_used (decl);
2971
2972 /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
2973 support for an approach in which a reference to a local
2974 [constant] automatic variable in a nested class or lambda body
2975 would enter the expression as an rvalue, which would reduce
2976 the complexity of the problem"
2977
2978 FIXME update for final resolution of core issue 696. */
2979 if (decl_constant_var_p (decl))
5ce9237c
JM
2980 {
2981 if (processing_template_decl)
2982 /* In a template, the constant value may not be in a usable
2983 form, so look it up again at instantiation time. */
2984 return id_expression;
2985 else
2986 return integral_constant_value (decl);
2987 }
e4b17023
JM
2988
2989 /* If we are in a lambda function, we can move out until we hit
2990 1. the context,
2991 2. a non-lambda function, or
2992 3. a non-default capturing lambda function. */
2993 while (context != containing_function
2994 && LAMBDA_FUNCTION_P (containing_function))
2995 {
2996 lambda_expr = CLASSTYPE_LAMBDA_EXPR
2997 (DECL_CONTEXT (containing_function));
2998
2999 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3000 == CPLD_NONE)
3001 break;
3002
3003 lambda_stack = tree_cons (NULL_TREE,
3004 lambda_expr,
3005 lambda_stack);
3006
3007 containing_function
3008 = decl_function_context (containing_function);
3009 }
3010
3011 if (context == containing_function)
3012 {
3013 decl = add_default_capture (lambda_stack,
3014 /*id=*/DECL_NAME (decl),
3015 initializer);
3016 }
3017 else if (lambda_expr)
3018 {
3019 error ("%qD is not captured", decl);
3020 return error_mark_node;
3021 }
3022 else
3023 {
3024 error (TREE_CODE (decl) == VAR_DECL
3025 ? G_("use of %<auto%> variable from containing function")
3026 : G_("use of parameter from containing function"));
3027 error (" %q+#D declared here", decl);
3028 return error_mark_node;
3029 }
3030 }
3031
3032 /* Also disallow uses of function parameters outside the function
3033 body, except inside an unevaluated context (i.e. decltype). */
3034 if (TREE_CODE (decl) == PARM_DECL
3035 && DECL_CONTEXT (decl) == NULL_TREE
3036 && !cp_unevaluated_operand)
3037 {
3038 error ("use of parameter %qD outside function body", decl);
3039 return error_mark_node;
3040 }
3041 }
3042
3043 /* If we didn't find anything, or what we found was a type,
3044 then this wasn't really an id-expression. */
3045 if (TREE_CODE (decl) == TEMPLATE_DECL
3046 && !DECL_FUNCTION_TEMPLATE_P (decl))
3047 {
3048 *error_msg = "missing template arguments";
3049 return error_mark_node;
3050 }
3051 else if (TREE_CODE (decl) == TYPE_DECL
3052 || TREE_CODE (decl) == NAMESPACE_DECL)
3053 {
3054 *error_msg = "expected primary-expression";
3055 return error_mark_node;
3056 }
3057
3058 /* If the name resolved to a template parameter, there is no
3059 need to look it up again later. */
3060 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3061 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3062 {
3063 tree r;
3064
3065 *idk = CP_ID_KIND_NONE;
3066 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3067 decl = TEMPLATE_PARM_DECL (decl);
3068 r = convert_from_reference (DECL_INITIAL (decl));
3069
3070 if (integral_constant_expression_p
3071 && !dependent_type_p (TREE_TYPE (decl))
3072 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3073 {
3074 if (!allow_non_integral_constant_expression_p)
3075 error ("template parameter %qD of type %qT is not allowed in "
3076 "an integral constant expression because it is not of "
3077 "integral or enumeration type", decl, TREE_TYPE (decl));
3078 *non_integral_constant_expression_p = true;
3079 }
3080 return r;
3081 }
3082 /* Similarly, we resolve enumeration constants to their
3083 underlying values. */
3084 else if (TREE_CODE (decl) == CONST_DECL)
3085 {
3086 *idk = CP_ID_KIND_NONE;
3087 if (!processing_template_decl)
3088 {
3089 used_types_insert (TREE_TYPE (decl));
3090 return DECL_INITIAL (decl);
3091 }
3092 return decl;
3093 }
3094 else
3095 {
3096 bool dependent_p;
3097
3098 /* If the declaration was explicitly qualified indicate
3099 that. The semantics of `A::f(3)' are different than
3100 `f(3)' if `f' is virtual. */
3101 *idk = (scope
3102 ? CP_ID_KIND_QUALIFIED
3103 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3104 ? CP_ID_KIND_TEMPLATE_ID
3105 : CP_ID_KIND_UNQUALIFIED));
3106
3107
3108 /* [temp.dep.expr]
3109
3110 An id-expression is type-dependent if it contains an
3111 identifier that was declared with a dependent type.
3112
3113 The standard is not very specific about an id-expression that
3114 names a set of overloaded functions. What if some of them
3115 have dependent types and some of them do not? Presumably,
3116 such a name should be treated as a dependent name. */
3117 /* Assume the name is not dependent. */
3118 dependent_p = false;
3119 if (!processing_template_decl)
3120 /* No names are dependent outside a template. */
3121 ;
3122 /* A template-id where the name of the template was not resolved
3123 is definitely dependent. */
3124 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3125 && (TREE_CODE (TREE_OPERAND (decl, 0))
3126 == IDENTIFIER_NODE))
3127 dependent_p = true;
3128 /* For anything except an overloaded function, just check its
3129 type. */
3130 else if (!is_overloaded_fn (decl))
3131 dependent_p
3132 = dependent_type_p (TREE_TYPE (decl));
3133 /* For a set of overloaded functions, check each of the
3134 functions. */
3135 else
3136 {
3137 tree fns = decl;
3138
3139 if (BASELINK_P (fns))
3140 fns = BASELINK_FUNCTIONS (fns);
3141
3142 /* For a template-id, check to see if the template
3143 arguments are dependent. */
3144 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3145 {
3146 tree args = TREE_OPERAND (fns, 1);
3147 dependent_p = any_dependent_template_arguments_p (args);
3148 /* The functions are those referred to by the
3149 template-id. */
3150 fns = TREE_OPERAND (fns, 0);
3151 }
3152
3153 /* If there are no dependent template arguments, go through
3154 the overloaded functions. */
3155 while (fns && !dependent_p)
3156 {
3157 tree fn = OVL_CURRENT (fns);
3158
3159 /* Member functions of dependent classes are
3160 dependent. */
3161 if (TREE_CODE (fn) == FUNCTION_DECL
3162 && type_dependent_expression_p (fn))
3163 dependent_p = true;
3164 else if (TREE_CODE (fn) == TEMPLATE_DECL
3165 && dependent_template_p (fn))
3166 dependent_p = true;
3167
3168 fns = OVL_NEXT (fns);
3169 }
3170 }
3171
3172 /* If the name was dependent on a template parameter, we will
3173 resolve the name at instantiation time. */
3174 if (dependent_p)
3175 {
3176 /* Create a SCOPE_REF for qualified names, if the scope is
3177 dependent. */
3178 if (scope)
3179 {
3180 if (TYPE_P (scope))
3181 {
3182 if (address_p && done)
3183 decl = finish_qualified_id_expr (scope, decl,
3184 done, address_p,
3185 template_p,
3186 template_arg_p);
3187 else
3188 {
3189 tree type = NULL_TREE;
3190 if (DECL_P (decl) && !dependent_scope_p (scope))
3191 type = TREE_TYPE (decl);
3192 decl = build_qualified_name (type,
3193 scope,
3194 id_expression,
3195 template_p);
3196 }
3197 }
3198 if (TREE_TYPE (decl))
3199 decl = convert_from_reference (decl);
3200 return decl;
3201 }
3202 /* A TEMPLATE_ID already contains all the information we
3203 need. */
3204 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3205 return id_expression;
3206 *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3207 /* If we found a variable, then name lookup during the
3208 instantiation will always resolve to the same VAR_DECL
3209 (or an instantiation thereof). */
3210 if (TREE_CODE (decl) == VAR_DECL
3211 || TREE_CODE (decl) == PARM_DECL)
3212 {
3213 mark_used (decl);
3214 return convert_from_reference (decl);
3215 }
3216 /* The same is true for FIELD_DECL, but we also need to
3217 make sure that the syntax is correct. */
3218 else if (TREE_CODE (decl) == FIELD_DECL)
3219 {
3220 /* Since SCOPE is NULL here, this is an unqualified name.
3221 Access checking has been performed during name lookup
3222 already. Turn off checking to avoid duplicate errors. */
3223 push_deferring_access_checks (dk_no_check);
3224 decl = finish_non_static_data_member
3225 (decl, NULL_TREE,
3226 /*qualifying_scope=*/NULL_TREE);
3227 pop_deferring_access_checks ();
3228 return decl;
3229 }
3230 return id_expression;
3231 }
3232
3233 if (TREE_CODE (decl) == NAMESPACE_DECL)
3234 {
3235 error ("use of namespace %qD as expression", decl);
3236 return error_mark_node;
3237 }
3238 else if (DECL_CLASS_TEMPLATE_P (decl))
3239 {
3240 error ("use of class template %qT as expression", decl);
3241 return error_mark_node;
3242 }
3243 else if (TREE_CODE (decl) == TREE_LIST)
3244 {
3245 /* Ambiguous reference to base members. */
3246 error ("request for member %qD is ambiguous in "
3247 "multiple inheritance lattice", id_expression);
3248 print_candidates (decl);
3249 return error_mark_node;
3250 }
3251
3252 /* Mark variable-like entities as used. Functions are similarly
3253 marked either below or after overload resolution. */
3254 if (TREE_CODE (decl) == VAR_DECL
3255 || TREE_CODE (decl) == PARM_DECL
3256 || TREE_CODE (decl) == RESULT_DECL)
3257 mark_used (decl);
3258
3259 /* Only certain kinds of names are allowed in constant
3260 expression. Enumerators and template parameters have already
3261 been handled above. */
3262 if (! error_operand_p (decl)
3263 && integral_constant_expression_p
3264 && ! decl_constant_var_p (decl)
3265 && ! builtin_valid_in_constant_expr_p (decl))
3266 {
3267 if (!allow_non_integral_constant_expression_p)
3268 {
3269 error ("%qD cannot appear in a constant-expression", decl);
3270 return error_mark_node;
3271 }
3272 *non_integral_constant_expression_p = true;
3273 }
3274
3275 if (scope)
3276 {
3277 decl = (adjust_result_of_qualified_name_lookup
3278 (decl, scope, current_nonlambda_class_type()));
3279
3280 if (TREE_CODE (decl) == FUNCTION_DECL)
3281 mark_used (decl);
3282
3283 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3284 decl = finish_qualified_id_expr (scope,
3285 decl,
3286 done,
3287 address_p,
3288 template_p,
3289 template_arg_p);
3290 else
3291 {
3292 tree r = convert_from_reference (decl);
3293
3294 /* In a template, return a SCOPE_REF for most qualified-ids
3295 so that we can check access at instantiation time. But if
3296 we're looking at a member of the current instantiation, we
3297 know we have access and building up the SCOPE_REF confuses
3298 non-type template argument handling. */
3299 if (processing_template_decl && TYPE_P (scope)
3300 && !currently_open_class (scope))
3301 r = build_qualified_name (TREE_TYPE (r),
3302 scope, decl,
3303 template_p);
3304 decl = r;
3305 }
3306 }
3307 else if (TREE_CODE (decl) == FIELD_DECL)
3308 {
3309 /* Since SCOPE is NULL here, this is an unqualified name.
3310 Access checking has been performed during name lookup
3311 already. Turn off checking to avoid duplicate errors. */
3312 push_deferring_access_checks (dk_no_check);
3313 decl = finish_non_static_data_member (decl, NULL_TREE,
3314 /*qualifying_scope=*/NULL_TREE);
3315 pop_deferring_access_checks ();
3316 }
3317 else if (is_overloaded_fn (decl))
3318 {
3319 tree first_fn;
3320
3321 first_fn = get_first_fn (decl);
3322 if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3323 first_fn = DECL_TEMPLATE_RESULT (first_fn);
3324
3325 if (!really_overloaded_fn (decl)
3326 && !mark_used (first_fn))
3327 return error_mark_node;
3328
3329 if (!template_arg_p
3330 && TREE_CODE (first_fn) == FUNCTION_DECL
3331 && DECL_FUNCTION_MEMBER_P (first_fn)
3332 && !shared_member_p (decl))
3333 {
3334 /* A set of member functions. */
3335 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3336 return finish_class_member_access_expr (decl, id_expression,
3337 /*template_p=*/false,
3338 tf_warning_or_error);
3339 }
3340
3341 decl = baselink_for_fns (decl);
3342 }
3343 else
3344 {
3345 if (DECL_P (decl) && DECL_NONLOCAL (decl)
3346 && DECL_CLASS_SCOPE_P (decl))
3347 {
3348 tree context = context_for_name_lookup (decl);
3349 if (context != current_class_type)
3350 {
3351 tree path = currently_open_derived_class (context);
3352 perform_or_defer_access_check (TYPE_BINFO (path),
3353 decl, decl);
3354 }
3355 }
3356
3357 decl = convert_from_reference (decl);
3358 }
3359 }
3360
3361 if (TREE_DEPRECATED (decl))
3362 warn_deprecated_use (decl, NULL_TREE);
3363
3364 return decl;
3365}
3366
3367/* Implement the __typeof keyword: Return the type of EXPR, suitable for
3368 use as a type-specifier. */
3369
3370tree
3371finish_typeof (tree expr)
3372{
3373 tree type;
3374
3375 if (type_dependent_expression_p (expr))
3376 {
3377 type = cxx_make_type (TYPEOF_TYPE);
3378 TYPEOF_TYPE_EXPR (type) = expr;
3379 SET_TYPE_STRUCTURAL_EQUALITY (type);
3380
3381 return type;
3382 }
3383
3384 expr = mark_type_use (expr);
3385
3386 type = unlowered_expr_type (expr);
3387
3388 if (!type || type == unknown_type_node)
3389 {
3390 error ("type of %qE is unknown", expr);
3391 return error_mark_node;
3392 }
3393
3394 return type;
3395}
3396
3397/* Implement the __underlying_type keyword: Return the underlying
3398 type of TYPE, suitable for use as a type-specifier. */
3399
3400tree
3401finish_underlying_type (tree type)
3402{
3403 tree underlying_type;
3404
3405 if (processing_template_decl)
3406 {
3407 underlying_type = cxx_make_type (UNDERLYING_TYPE);
3408 UNDERLYING_TYPE_TYPE (underlying_type) = type;
3409 SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3410
3411 return underlying_type;
3412 }
3413
3414 complete_type (type);
3415
3416 if (TREE_CODE (type) != ENUMERAL_TYPE)
3417 {
3418 error ("%qT is not an enumeration type", type);
3419 return error_mark_node;
3420 }
3421
3422 underlying_type = ENUM_UNDERLYING_TYPE (type);
3423
3424 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3425 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3426 See finish_enum_value_list for details. */
3427 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3428 underlying_type
3429 = c_common_type_for_mode (TYPE_MODE (underlying_type),
3430 TYPE_UNSIGNED (underlying_type));
3431
3432 return underlying_type;
3433}
3434
3435/* Implement the __direct_bases keyword: Return the direct base classes
3436 of type */
3437
3438tree
3439calculate_direct_bases (tree type)
3440{
3441 VEC(tree, gc) *vector = make_tree_vector();
3442 tree bases_vec = NULL_TREE;
3443 VEC(tree, none) *base_binfos;
3444 tree binfo;
3445 unsigned i;
3446
3447 complete_type (type);
3448
3449 if (!NON_UNION_CLASS_TYPE_P (type))
3450 return make_tree_vec (0);
3451
3452 base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3453
3454 /* Virtual bases are initialized first */
3455 for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3456 {
3457 if (BINFO_VIRTUAL_P (binfo))
3458 {
3459 VEC_safe_push (tree, gc, vector, binfo);
3460 }
3461 }
3462
3463 /* Now non-virtuals */
3464 for (i = 0; VEC_iterate (tree, base_binfos, i, binfo); i++)
3465 {
3466 if (!BINFO_VIRTUAL_P (binfo))
3467 {
3468 VEC_safe_push (tree, gc, vector, binfo);
3469 }
3470 }
3471
3472
3473 bases_vec = make_tree_vec (VEC_length (tree, vector));
3474
3475 for (i = 0; i < VEC_length (tree, vector); ++i)
3476 {
3477 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE (VEC_index (tree, vector, i));
3478 }
3479 return bases_vec;
3480}
3481
3482/* Implement the __bases keyword: Return the base classes
3483 of type */
3484
3485/* Find morally non-virtual base classes by walking binfo hierarchy */
3486/* Virtual base classes are handled separately in finish_bases */
3487
3488static tree
3489dfs_calculate_bases_pre (tree binfo, ATTRIBUTE_UNUSED void *data_)
3490{
3491 /* Don't walk bases of virtual bases */
3492 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3493}
3494
3495static tree
3496dfs_calculate_bases_post (tree binfo, void *data_)
3497{
3498 VEC(tree, gc) **data = (VEC(tree, gc) **) data_;
3499 if (!BINFO_VIRTUAL_P (binfo))
3500 {
3501 VEC_safe_push (tree, gc, *data, BINFO_TYPE (binfo));
3502 }
3503 return NULL_TREE;
3504}
3505
3506/* Calculates the morally non-virtual base classes of a class */
3507static VEC(tree, gc) *
3508calculate_bases_helper (tree type)
3509{
3510 VEC(tree, gc) *vector = make_tree_vector();
3511
3512 /* Now add non-virtual base classes in order of construction */
3513 dfs_walk_all (TYPE_BINFO (type),
3514 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3515 return vector;
3516}
3517
3518tree
3519calculate_bases (tree type)
3520{
3521 VEC(tree, gc) *vector = make_tree_vector();
3522 tree bases_vec = NULL_TREE;
3523 unsigned i;
3524 VEC(tree, gc) *vbases;
3525 VEC(tree, gc) *nonvbases;
3526 tree binfo;
3527
3528 complete_type (type);
3529
3530 if (!NON_UNION_CLASS_TYPE_P (type))
3531 return make_tree_vec (0);
3532
3533 /* First go through virtual base classes */
3534 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3535 VEC_iterate (tree, vbases, i, binfo); i++)
3536 {
3537 VEC(tree, gc) *vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3538 VEC_safe_splice (tree, gc, vector, vbase_bases);
3539 release_tree_vector (vbase_bases);
3540 }
3541
3542 /* Now for the non-virtual bases */
3543 nonvbases = calculate_bases_helper (type);
3544 VEC_safe_splice (tree, gc, vector, nonvbases);
3545 release_tree_vector (nonvbases);
3546
3547 /* Last element is entire class, so don't copy */
3548 bases_vec = make_tree_vec (VEC_length (tree, vector) - 1);
3549
3550 for (i = 0; i < VEC_length (tree, vector) - 1; ++i)
3551 {
3552 TREE_VEC_ELT (bases_vec, i) = VEC_index (tree, vector, i);
3553 }
3554 release_tree_vector (vector);
3555 return bases_vec;
3556}
3557
3558tree
3559finish_bases (tree type, bool direct)
3560{
3561 tree bases = NULL_TREE;
3562
3563 if (!processing_template_decl)
3564 {
3565 /* Parameter packs can only be used in templates */
3566 error ("Parameter pack __bases only valid in template declaration");
3567 return error_mark_node;
3568 }
3569
3570 bases = cxx_make_type (BASES);
3571 BASES_TYPE (bases) = type;
3572 BASES_DIRECT (bases) = direct;
3573 SET_TYPE_STRUCTURAL_EQUALITY (bases);
3574
3575 return bases;
3576}
3577
3578/* Perform C++-specific checks for __builtin_offsetof before calling
3579 fold_offsetof. */
3580
3581tree
3582finish_offsetof (tree expr)
3583{
3584 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3585 {
3586 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3587 TREE_OPERAND (expr, 2));
3588 return error_mark_node;
3589 }
3590 if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3591 || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3592 || TREE_TYPE (expr) == unknown_type_node)
3593 {
3594 if (TREE_CODE (expr) == COMPONENT_REF
3595 || TREE_CODE (expr) == COMPOUND_EXPR)
3596 expr = TREE_OPERAND (expr, 1);
3597 error ("cannot apply %<offsetof%> to member function %qD", expr);
3598 return error_mark_node;
3599 }
3600 if (REFERENCE_REF_P (expr))
3601 expr = TREE_OPERAND (expr, 0);
3602 if (TREE_CODE (expr) == COMPONENT_REF)
3603 {
3604 tree object = TREE_OPERAND (expr, 0);
3605 if (!complete_type_or_else (TREE_TYPE (object), object))
3606 return error_mark_node;
3607 }
3608 return fold_offsetof (expr);
3609}
3610
3611/* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
3612 function is broken out from the above for the benefit of the tree-ssa
3613 project. */
3614
3615void
3616simplify_aggr_init_expr (tree *tp)
3617{
3618 tree aggr_init_expr = *tp;
3619
3620 /* Form an appropriate CALL_EXPR. */
3621 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3622 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3623 tree type = TREE_TYPE (slot);
3624
3625 tree call_expr;
3626 enum style_t { ctor, arg, pcc } style;
3627
3628 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3629 style = ctor;
3630#ifdef PCC_STATIC_STRUCT_RETURN
3631 else if (1)
3632 style = pcc;
3633#endif
3634 else
3635 {
3636 gcc_assert (TREE_ADDRESSABLE (type));
3637 style = arg;
3638 }
3639
3640 call_expr = build_call_array_loc (input_location,
3641 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3642 fn,
3643 aggr_init_expr_nargs (aggr_init_expr),
3644 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3645 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3646
3647 if (style == ctor)
3648 {
3649 /* Replace the first argument to the ctor with the address of the
3650 slot. */
3651 cxx_mark_addressable (slot);
3652 CALL_EXPR_ARG (call_expr, 0) =
3653 build1 (ADDR_EXPR, build_pointer_type (type), slot);
3654 }
3655 else if (style == arg)
3656 {
3657 /* Just mark it addressable here, and leave the rest to
3658 expand_call{,_inline}. */
3659 cxx_mark_addressable (slot);
3660 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3661 call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3662 }
3663 else if (style == pcc)
3664 {
3665 /* If we're using the non-reentrant PCC calling convention, then we
3666 need to copy the returned value out of the static buffer into the
3667 SLOT. */
3668 push_deferring_access_checks (dk_no_check);
3669 call_expr = build_aggr_init (slot, call_expr,
3670 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3671 tf_warning_or_error);
3672 pop_deferring_access_checks ();
3673 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3674 }
3675
3676 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
3677 {
3678 tree init = build_zero_init (type, NULL_TREE,
3679 /*static_storage_p=*/false);
3680 init = build2 (INIT_EXPR, void_type_node, slot, init);
3681 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
3682 init, call_expr);
3683 }
3684
3685 *tp = call_expr;
3686}
3687
3688/* Emit all thunks to FN that should be emitted when FN is emitted. */
3689
3690void
3691emit_associated_thunks (tree fn)
3692{
3693 /* When we use vcall offsets, we emit thunks with the virtual
3694 functions to which they thunk. The whole point of vcall offsets
3695 is so that you can know statically the entire set of thunks that
3696 will ever be needed for a given virtual function, thereby
3697 enabling you to output all the thunks with the function itself. */
3698 if (DECL_VIRTUAL_P (fn)
3699 /* Do not emit thunks for extern template instantiations. */
3700 && ! DECL_REALLY_EXTERN (fn))
3701 {
3702 tree thunk;
3703
3704 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
3705 {
3706 if (!THUNK_ALIAS (thunk))
3707 {
3708 use_thunk (thunk, /*emit_p=*/1);
3709 if (DECL_RESULT_THUNK_P (thunk))
3710 {
3711 tree probe;
3712
3713 for (probe = DECL_THUNKS (thunk);
3714 probe; probe = DECL_CHAIN (probe))
3715 use_thunk (probe, /*emit_p=*/1);
3716 }
3717 }
3718 else
3719 gcc_assert (!DECL_THUNKS (thunk));
3720 }
3721 }
3722}
3723
3724/* Returns true iff FUN is an instantiation of a constexpr function
3725 template. */
3726
3727static inline bool
3728is_instantiation_of_constexpr (tree fun)
3729{
3730 return (DECL_TEMPLOID_INSTANTIATION (fun)
3731 && DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT
3732 (DECL_TI_TEMPLATE (fun))));
3733}
3734
3735/* Generate RTL for FN. */
3736
3737bool
3738expand_or_defer_fn_1 (tree fn)
3739{
3740 /* When the parser calls us after finishing the body of a template
3741 function, we don't really want to expand the body. */
3742 if (processing_template_decl)
3743 {
3744 /* Normally, collection only occurs in rest_of_compilation. So,
3745 if we don't collect here, we never collect junk generated
3746 during the processing of templates until we hit a
3747 non-template function. It's not safe to do this inside a
3748 nested class, though, as the parser may have local state that
3749 is not a GC root. */
3750 if (!function_depth)
3751 ggc_collect ();
3752 return false;
3753 }
3754
3755 gcc_assert (DECL_SAVED_TREE (fn));
3756
3757 /* If this is a constructor or destructor body, we have to clone
3758 it. */
3759 if (maybe_clone_body (fn))
3760 {
3761 /* We don't want to process FN again, so pretend we've written
3762 it out, even though we haven't. */
3763 TREE_ASM_WRITTEN (fn) = 1;
3764 /* If this is an instantiation of a constexpr function, keep
3765 DECL_SAVED_TREE for explain_invalid_constexpr_fn. */
3766 if (!is_instantiation_of_constexpr (fn))
3767 DECL_SAVED_TREE (fn) = NULL_TREE;
3768 return false;
3769 }
3770
3771 /* We make a decision about linkage for these functions at the end
3772 of the compilation. Until that point, we do not want the back
3773 end to output them -- but we do want it to see the bodies of