OSDN Git Service

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