OSDN Git Service

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