Update gcc-50 to SVN version 231263 (gcc-5-branch)
[dragonfly.git] / contrib / gcc-5.0 / 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-2015 Free Software Foundation, Inc.
7    Written by Mark Mitchell (mmitchell@usa.net) based on code found
8    formerly in parse.y and pt.c.
9
10    This file is part of GCC.
11
12    GCC is free software; you can redistribute it and/or modify it
13    under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3, or (at your option)
15    any later version.
16
17    GCC is distributed in the hope that it will be useful, but
18    WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3.  If not see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "vec.h"
33 #include "double-int.h"
34 #include "input.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "wide-int.h"
38 #include "inchash.h"
39 #include "tree.h"
40 #include "stmt.h"
41 #include "varasm.h"
42 #include "stor-layout.h"
43 #include "stringpool.h"
44 #include "cp-tree.h"
45 #include "c-family/c-common.h"
46 #include "c-family/c-objc.h"
47 #include "tree-inline.h"
48 #include "intl.h"
49 #include "toplev.h"
50 #include "flags.h"
51 #include "timevar.h"
52 #include "diagnostic.h"
53 #include "hash-map.h"
54 #include "is-a.h"
55 #include "plugin-api.h"
56 #include "hard-reg-set.h"
57 #include "input.h"
58 #include "function.h"
59 #include "ipa-ref.h"
60 #include "cgraph.h"
61 #include "tree-iterator.h"
62 #include "target.h"
63 #include "hash-table.h"
64 #include "gimplify.h"
65 #include "bitmap.h"
66 #include "omp-low.h"
67 #include "builtins.h"
68 #include "convert.h"
69 #include "gomp-constants.h"
70
71 /* There routines provide a modular interface to perform many parsing
72    operations.  They may therefore be used during actual parsing, or
73    during template instantiation, which may be regarded as a
74    degenerate form of parsing.  */
75
76 static tree maybe_convert_cond (tree);
77 static tree finalize_nrv_r (tree *, int *, void *);
78 static tree capture_decltype (tree);
79
80
81 /* Deferred Access Checking Overview
82    ---------------------------------
83
84    Most C++ expressions and declarations require access checking
85    to be performed during parsing.  However, in several cases,
86    this has to be treated differently.
87
88    For member declarations, access checking has to be deferred
89    until more information about the declaration is known.  For
90    example:
91
92      class A {
93          typedef int X;
94        public:
95          X f();
96      };
97
98      A::X A::f();
99      A::X g();
100
101    When we are parsing the function return type `A::X', we don't
102    really know if this is allowed until we parse the function name.
103
104    Furthermore, some contexts require that access checking is
105    never performed at all.  These include class heads, and template
106    instantiations.
107
108    Typical use of access checking functions is described here:
109
110    1. When we enter a context that requires certain access checking
111       mode, the function `push_deferring_access_checks' is called with
112       DEFERRING argument specifying the desired mode.  Access checking
113       may be performed immediately (dk_no_deferred), deferred
114       (dk_deferred), or not performed (dk_no_check).
115
116    2. When a declaration such as a type, or a variable, is encountered,
117       the function `perform_or_defer_access_check' is called.  It
118       maintains a vector of all deferred checks.
119
120    3. The global `current_class_type' or `current_function_decl' is then
121       setup by the parser.  `enforce_access' relies on these information
122       to check access.
123
124    4. Upon exiting the context mentioned in step 1,
125       `perform_deferred_access_checks' is called to check all declaration
126       stored in the vector. `pop_deferring_access_checks' is then
127       called to restore the previous access checking mode.
128
129       In case of parsing error, we simply call `pop_deferring_access_checks'
130       without `perform_deferred_access_checks'.  */
131
132 typedef struct GTY(()) deferred_access {
133   /* A vector representing name-lookups for which we have deferred
134      checking access controls.  We cannot check the accessibility of
135      names used in a decl-specifier-seq until we know what is being
136      declared because code like:
137
138        class A {
139          class B {};
140          B* f();
141        }
142
143        A::B* A::f() { return 0; }
144
145      is valid, even though `A::B' is not generally accessible.  */
146   vec<deferred_access_check, va_gc> * GTY(()) deferred_access_checks;
147
148   /* The current mode of access checks.  */
149   enum deferring_kind deferring_access_checks_kind;
150
151 } deferred_access;
152
153 /* Data for deferred access checking.  */
154 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
155 static GTY(()) unsigned deferred_access_no_check;
156
157 /* Save the current deferred access states and start deferred
158    access checking iff DEFER_P is true.  */
159
160 void
161 push_deferring_access_checks (deferring_kind deferring)
162 {
163   /* For context like template instantiation, access checking
164      disabling applies to all nested context.  */
165   if (deferred_access_no_check || deferring == dk_no_check)
166     deferred_access_no_check++;
167   else
168     {
169       deferred_access e = {NULL, deferring};
170       vec_safe_push (deferred_access_stack, e);
171     }
172 }
173
174 /* Save the current deferred access states and start deferred access
175    checking, continuing the set of deferred checks in CHECKS.  */
176
177 void
178 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
179 {
180   push_deferring_access_checks (dk_deferred);
181   if (!deferred_access_no_check)
182     deferred_access_stack->last().deferred_access_checks = checks;
183 }
184
185 /* Resume deferring access checks again after we stopped doing
186    this previously.  */
187
188 void
189 resume_deferring_access_checks (void)
190 {
191   if (!deferred_access_no_check)
192     deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
193 }
194
195 /* Stop deferring access checks.  */
196
197 void
198 stop_deferring_access_checks (void)
199 {
200   if (!deferred_access_no_check)
201     deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
202 }
203
204 /* Discard the current deferred access checks and restore the
205    previous states.  */
206
207 void
208 pop_deferring_access_checks (void)
209 {
210   if (deferred_access_no_check)
211     deferred_access_no_check--;
212   else
213     deferred_access_stack->pop ();
214 }
215
216 /* Returns a TREE_LIST representing the deferred checks.
217    The TREE_PURPOSE of each node is the type through which the
218    access occurred; the TREE_VALUE is the declaration named.
219    */
220
221 vec<deferred_access_check, va_gc> *
222 get_deferred_access_checks (void)
223 {
224   if (deferred_access_no_check)
225     return NULL;
226   else
227     return (deferred_access_stack->last().deferred_access_checks);
228 }
229
230 /* Take current deferred checks and combine with the
231    previous states if we also defer checks previously.
232    Otherwise perform checks now.  */
233
234 void
235 pop_to_parent_deferring_access_checks (void)
236 {
237   if (deferred_access_no_check)
238     deferred_access_no_check--;
239   else
240     {
241       vec<deferred_access_check, va_gc> *checks;
242       deferred_access *ptr;
243
244       checks = (deferred_access_stack->last ().deferred_access_checks);
245
246       deferred_access_stack->pop ();
247       ptr = &deferred_access_stack->last ();
248       if (ptr->deferring_access_checks_kind == dk_no_deferred)
249         {
250           /* Check access.  */
251           perform_access_checks (checks, tf_warning_or_error);
252         }
253       else
254         {
255           /* Merge with parent.  */
256           int i, j;
257           deferred_access_check *chk, *probe;
258
259           FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
260             {
261               FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
262                 {
263                   if (probe->binfo == chk->binfo &&
264                       probe->decl == chk->decl &&
265                       probe->diag_decl == chk->diag_decl)
266                     goto found;
267                 }
268               /* Insert into parent's checks.  */
269               vec_safe_push (ptr->deferred_access_checks, *chk);
270             found:;
271             }
272         }
273     }
274 }
275
276 /* Perform the access checks in CHECKS.  The TREE_PURPOSE of each node
277    is the BINFO indicating the qualifying scope used to access the
278    DECL node stored in the TREE_VALUE of the node.  If CHECKS is empty
279    or we aren't in SFINAE context or all the checks succeed return TRUE,
280    otherwise FALSE.  */
281
282 bool
283 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
284                        tsubst_flags_t complain)
285 {
286   int i;
287   deferred_access_check *chk;
288   location_t loc = input_location;
289   bool ok = true;
290
291   if (!checks)
292     return true;
293
294   FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
295     {
296       input_location = chk->loc;
297       ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
298     }
299
300   input_location = loc;
301   return (complain & tf_error) ? true : ok;
302 }
303
304 /* Perform the deferred access checks.
305
306    After performing the checks, we still have to keep the list
307    `deferred_access_stack->deferred_access_checks' since we may want
308    to check access for them again later in a different context.
309    For example:
310
311      class A {
312        typedef int X;
313        static X a;
314      };
315      A::X A::a, x;      // No error for `A::a', error for `x'
316
317    We have to perform deferred access of `A::X', first with `A::a',
318    next with `x'.  Return value like perform_access_checks above.  */
319
320 bool
321 perform_deferred_access_checks (tsubst_flags_t complain)
322 {
323   return perform_access_checks (get_deferred_access_checks (), complain);
324 }
325
326 /* Defer checking the accessibility of DECL, when looked up in
327    BINFO. DIAG_DECL is the declaration to use to print diagnostics.
328    Return value like perform_access_checks above.  */
329
330 bool
331 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
332                                tsubst_flags_t complain)
333 {
334   int i;
335   deferred_access *ptr;
336   deferred_access_check *chk;
337
338
339   /* Exit if we are in a context that no access checking is performed.
340      */
341   if (deferred_access_no_check)
342     return true;
343
344   gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
345
346   ptr = &deferred_access_stack->last ();
347
348   /* If we are not supposed to defer access checks, just check now.  */
349   if (ptr->deferring_access_checks_kind == dk_no_deferred)
350     {
351       bool ok = enforce_access (binfo, decl, diag_decl, complain);
352       return (complain & tf_error) ? true : ok;
353     }
354
355   /* See if we are already going to perform this check.  */
356   FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
357     {
358       if (chk->decl == decl && chk->binfo == binfo &&
359           chk->diag_decl == diag_decl)
360         {
361           return true;
362         }
363     }
364   /* If not, record the check.  */
365   deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
366   vec_safe_push (ptr->deferred_access_checks, new_access);
367
368   return true;
369 }
370
371 /* Returns nonzero if the current statement is a full expression,
372    i.e. temporaries created during that statement should be destroyed
373    at the end of the statement.  */
374
375 int
376 stmts_are_full_exprs_p (void)
377 {
378   return current_stmt_tree ()->stmts_are_full_exprs_p;
379 }
380
381 /* T is a statement.  Add it to the statement-tree.  This is the C++
382    version.  The C/ObjC frontends have a slightly different version of
383    this function.  */
384
385 tree
386 add_stmt (tree t)
387 {
388   enum tree_code code = TREE_CODE (t);
389
390   if (EXPR_P (t) && code != LABEL_EXPR)
391     {
392       if (!EXPR_HAS_LOCATION (t))
393         SET_EXPR_LOCATION (t, input_location);
394
395       /* When we expand a statement-tree, we must know whether or not the
396          statements are full-expressions.  We record that fact here.  */
397       STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
398     }
399
400   if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
401     STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
402
403   /* Add T to the statement-tree.  Non-side-effect statements need to be
404      recorded during statement expressions.  */
405   gcc_checking_assert (!stmt_list_stack->is_empty ());
406   append_to_statement_list_force (t, &cur_stmt_list);
407
408   return t;
409 }
410
411 /* Returns the stmt_tree to which statements are currently being added.  */
412
413 stmt_tree
414 current_stmt_tree (void)
415 {
416   return (cfun
417           ? &cfun->language->base.x_stmt_tree
418           : &scope_chain->x_stmt_tree);
419 }
420
421 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR.  */
422
423 static tree
424 maybe_cleanup_point_expr (tree expr)
425 {
426   if (!processing_template_decl && stmts_are_full_exprs_p ())
427     expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
428   return expr;
429 }
430
431 /* Like maybe_cleanup_point_expr except have the type of the new expression be
432    void so we don't need to create a temporary variable to hold the inner
433    expression.  The reason why we do this is because the original type might be
434    an aggregate and we cannot create a temporary variable for that type.  */
435
436 tree
437 maybe_cleanup_point_expr_void (tree expr)
438 {
439   if (!processing_template_decl && stmts_are_full_exprs_p ())
440     expr = fold_build_cleanup_point_expr (void_type_node, expr);
441   return expr;
442 }
443
444
445
446 /* Create a declaration statement for the declaration given by the DECL.  */
447
448 void
449 add_decl_expr (tree decl)
450 {
451   tree r = build_stmt (input_location, DECL_EXPR, decl);
452   if (DECL_INITIAL (decl)
453       || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
454     r = maybe_cleanup_point_expr_void (r);
455   add_stmt (r);
456 }
457
458 /* Finish a scope.  */
459
460 tree
461 do_poplevel (tree stmt_list)
462 {
463   tree block = NULL;
464
465   if (stmts_are_full_exprs_p ())
466     block = poplevel (kept_level_p (), 1, 0);
467
468   stmt_list = pop_stmt_list (stmt_list);
469
470   if (!processing_template_decl)
471     {
472       stmt_list = c_build_bind_expr (input_location, block, stmt_list);
473       /* ??? See c_end_compound_stmt re statement expressions.  */
474     }
475
476   return stmt_list;
477 }
478
479 /* Begin a new scope.  */
480
481 static tree
482 do_pushlevel (scope_kind sk)
483 {
484   tree ret = push_stmt_list ();
485   if (stmts_are_full_exprs_p ())
486     begin_scope (sk, NULL);
487   return ret;
488 }
489
490 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
491    when the current scope is exited.  EH_ONLY is true when this is not
492    meant to apply to normal control flow transfer.  */
493
494 void
495 push_cleanup (tree decl, tree cleanup, bool eh_only)
496 {
497   tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
498   CLEANUP_EH_ONLY (stmt) = eh_only;
499   add_stmt (stmt);
500   CLEANUP_BODY (stmt) = push_stmt_list ();
501 }
502
503 /* Simple infinite loop tracking for -Wreturn-type.  We keep a stack of all
504    the current loops, represented by 'NULL_TREE' if we've seen a possible
505    exit, and 'error_mark_node' if not.  This is currently used only to
506    suppress the warning about a function with no return statements, and
507    therefore we don't bother noting returns as possible exits.  We also
508    don't bother with gotos.  */
509
510 static void
511 begin_maybe_infinite_loop (tree cond)
512 {
513   /* Only track this while parsing a function, not during instantiation.  */
514   if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
515                 && !processing_template_decl))
516     return;
517   bool maybe_infinite = true;
518   if (cond)
519     {
520       cond = fold_non_dependent_expr (cond);
521       maybe_infinite = integer_nonzerop (cond);
522     }
523   vec_safe_push (cp_function_chain->infinite_loops,
524                  maybe_infinite ? error_mark_node : NULL_TREE);
525
526 }
527
528 /* A break is a possible exit for the current loop.  */
529
530 void
531 break_maybe_infinite_loop (void)
532 {
533   if (!cfun)
534     return;
535   cp_function_chain->infinite_loops->last() = NULL_TREE;
536 }
537
538 /* If we reach the end of the loop without seeing a possible exit, we have
539    an infinite loop.  */
540
541 static void
542 end_maybe_infinite_loop (tree cond)
543 {
544   if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
545                 && !processing_template_decl))
546     return;
547   tree current = cp_function_chain->infinite_loops->pop();
548   if (current != NULL_TREE)
549     {
550       cond = fold_non_dependent_expr (cond);
551       if (integer_nonzerop (cond))
552         current_function_infinite_loop = 1;
553     }
554 }
555
556
557 /* Begin a conditional that might contain a declaration.  When generating
558    normal code, we want the declaration to appear before the statement
559    containing the conditional.  When generating template code, we want the
560    conditional to be rendered as the raw DECL_EXPR.  */
561
562 static void
563 begin_cond (tree *cond_p)
564 {
565   if (processing_template_decl)
566     *cond_p = push_stmt_list ();
567 }
568
569 /* Finish such a conditional.  */
570
571 static void
572 finish_cond (tree *cond_p, tree expr)
573 {
574   if (processing_template_decl)
575     {
576       tree cond = pop_stmt_list (*cond_p);
577
578       if (expr == NULL_TREE)
579         /* Empty condition in 'for'.  */
580         gcc_assert (empty_expr_stmt_p (cond));
581       else if (check_for_bare_parameter_packs (expr))
582         expr = error_mark_node;
583       else if (!empty_expr_stmt_p (cond))
584         expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
585     }
586   *cond_p = expr;
587 }
588
589 /* If *COND_P specifies a conditional with a declaration, transform the
590    loop such that
591             while (A x = 42) { }
592             for (; A x = 42;) { }
593    becomes
594             while (true) { A x = 42; if (!x) break; }
595             for (;;) { A x = 42; if (!x) break; }
596    The statement list for BODY will be empty if the conditional did
597    not declare anything.  */
598
599 static void
600 simplify_loop_decl_cond (tree *cond_p, tree body)
601 {
602   tree cond, if_stmt;
603
604   if (!TREE_SIDE_EFFECTS (body))
605     return;
606
607   cond = *cond_p;
608   *cond_p = boolean_true_node;
609
610   if_stmt = begin_if_stmt ();
611   cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, 0, tf_warning_or_error);
612   finish_if_stmt_cond (cond, if_stmt);
613   finish_break_stmt ();
614   finish_then_clause (if_stmt);
615   finish_if_stmt (if_stmt);
616 }
617
618 /* Finish a goto-statement.  */
619
620 tree
621 finish_goto_stmt (tree destination)
622 {
623   if (identifier_p (destination))
624     destination = lookup_label (destination);
625
626   /* We warn about unused labels with -Wunused.  That means we have to
627      mark the used labels as used.  */
628   if (TREE_CODE (destination) == LABEL_DECL)
629     TREE_USED (destination) = 1;
630   else
631     {
632       if (check_no_cilk (destination,
633          "Cilk array notation cannot be used as a computed goto expression",
634          "%<_Cilk_spawn%> statement cannot be used as a computed goto expression"))
635         destination = error_mark_node;
636       destination = mark_rvalue_use (destination);
637       if (!processing_template_decl)
638         {
639           destination = cp_convert (ptr_type_node, destination,
640                                     tf_warning_or_error);
641           if (error_operand_p (destination))
642             return NULL_TREE;
643           destination
644             = fold_build_cleanup_point_expr (TREE_TYPE (destination),
645                                              destination);
646         }
647     }
648
649   check_goto (destination);
650
651   return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
652 }
653
654 /* COND is the condition-expression for an if, while, etc.,
655    statement.  Convert it to a boolean value, if appropriate.
656    In addition, verify sequence points if -Wsequence-point is enabled.  */
657
658 static tree
659 maybe_convert_cond (tree cond)
660 {
661   /* Empty conditions remain empty.  */
662   if (!cond)
663     return NULL_TREE;
664
665   /* Wait until we instantiate templates before doing conversion.  */
666   if (processing_template_decl)
667     return cond;
668
669   if (warn_sequence_point)
670     verify_sequence_points (cond);
671
672   /* Do the conversion.  */
673   cond = convert_from_reference (cond);
674
675   if (TREE_CODE (cond) == MODIFY_EXPR
676       && !TREE_NO_WARNING (cond)
677       && warn_parentheses)
678     {
679       warning (OPT_Wparentheses,
680                "suggest parentheses around assignment used as truth value");
681       TREE_NO_WARNING (cond) = 1;
682     }
683
684   return condition_conversion (cond);
685 }
686
687 /* Finish an expression-statement, whose EXPRESSION is as indicated.  */
688
689 tree
690 finish_expr_stmt (tree expr)
691 {
692   tree r = NULL_TREE;
693
694   if (expr != NULL_TREE)
695     {
696       if (!processing_template_decl)
697         {
698           if (warn_sequence_point)
699             verify_sequence_points (expr);
700           expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
701         }
702       else if (!type_dependent_expression_p (expr))
703         convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT, 
704                          tf_warning_or_error);
705
706       if (check_for_bare_parameter_packs (expr))
707         expr = error_mark_node;
708
709       /* Simplification of inner statement expressions, compound exprs,
710          etc can result in us already having an EXPR_STMT.  */
711       if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
712         {
713           if (TREE_CODE (expr) != EXPR_STMT)
714             expr = build_stmt (input_location, EXPR_STMT, expr);
715           expr = maybe_cleanup_point_expr_void (expr);
716         }
717
718       r = add_stmt (expr);
719     }
720
721   return r;
722 }
723
724
725 /* Begin an if-statement.  Returns a newly created IF_STMT if
726    appropriate.  */
727
728 tree
729 begin_if_stmt (void)
730 {
731   tree r, scope;
732   scope = do_pushlevel (sk_cond);
733   r = build_stmt (input_location, IF_STMT, NULL_TREE,
734                   NULL_TREE, NULL_TREE, scope);
735   begin_cond (&IF_COND (r));
736   return r;
737 }
738
739 /* Process the COND of an if-statement, which may be given by
740    IF_STMT.  */
741
742 void
743 finish_if_stmt_cond (tree cond, tree if_stmt)
744 {
745   finish_cond (&IF_COND (if_stmt), maybe_convert_cond (cond));
746   add_stmt (if_stmt);
747   THEN_CLAUSE (if_stmt) = push_stmt_list ();
748 }
749
750 /* Finish the then-clause of an if-statement, which may be given by
751    IF_STMT.  */
752
753 tree
754 finish_then_clause (tree if_stmt)
755 {
756   THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
757   return if_stmt;
758 }
759
760 /* Begin the else-clause of an if-statement.  */
761
762 void
763 begin_else_clause (tree if_stmt)
764 {
765   ELSE_CLAUSE (if_stmt) = push_stmt_list ();
766 }
767
768 /* Finish the else-clause of an if-statement, which may be given by
769    IF_STMT.  */
770
771 void
772 finish_else_clause (tree if_stmt)
773 {
774   ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
775 }
776
777 /* Finish an if-statement.  */
778
779 void
780 finish_if_stmt (tree if_stmt)
781 {
782   tree scope = IF_SCOPE (if_stmt);
783   IF_SCOPE (if_stmt) = NULL;
784   add_stmt (do_poplevel (scope));
785 }
786
787 /* Begin a while-statement.  Returns a newly created WHILE_STMT if
788    appropriate.  */
789
790 tree
791 begin_while_stmt (void)
792 {
793   tree r;
794   r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
795   add_stmt (r);
796   WHILE_BODY (r) = do_pushlevel (sk_block);
797   begin_cond (&WHILE_COND (r));
798   return r;
799 }
800
801 /* Process the COND of a while-statement, which may be given by
802    WHILE_STMT.  */
803
804 void
805 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep)
806 {
807   if (check_no_cilk (cond,
808       "Cilk array notation cannot be used as a condition for while statement",
809       "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
810     cond = error_mark_node;
811   cond = maybe_convert_cond (cond);
812   finish_cond (&WHILE_COND (while_stmt), cond);
813   begin_maybe_infinite_loop (cond);
814   if (ivdep && cond != error_mark_node)
815     WHILE_COND (while_stmt) = build2 (ANNOTATE_EXPR,
816                                       TREE_TYPE (WHILE_COND (while_stmt)),
817                                       WHILE_COND (while_stmt),
818                                       build_int_cst (integer_type_node,
819                                                      annot_expr_ivdep_kind));
820   simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
821 }
822
823 /* Finish a while-statement, which may be given by WHILE_STMT.  */
824
825 void
826 finish_while_stmt (tree while_stmt)
827 {
828   end_maybe_infinite_loop (boolean_true_node);
829   WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
830 }
831
832 /* Begin a do-statement.  Returns a newly created DO_STMT if
833    appropriate.  */
834
835 tree
836 begin_do_stmt (void)
837 {
838   tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
839   begin_maybe_infinite_loop (boolean_true_node);
840   add_stmt (r);
841   DO_BODY (r) = push_stmt_list ();
842   return r;
843 }
844
845 /* Finish the body of a do-statement, which may be given by DO_STMT.  */
846
847 void
848 finish_do_body (tree do_stmt)
849 {
850   tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
851
852   if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
853     body = STATEMENT_LIST_TAIL (body)->stmt;
854
855   if (IS_EMPTY_STMT (body))
856     warning (OPT_Wempty_body,
857             "suggest explicit braces around empty body in %<do%> statement");
858 }
859
860 /* Finish a do-statement, which may be given by DO_STMT, and whose
861    COND is as indicated.  */
862
863 void
864 finish_do_stmt (tree cond, tree do_stmt, bool ivdep)
865 {
866   if (check_no_cilk (cond,
867   "Cilk array notation cannot be used as a condition for a do-while statement",
868   "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
869     cond = error_mark_node;
870   cond = maybe_convert_cond (cond);
871   end_maybe_infinite_loop (cond);
872   if (ivdep && cond != error_mark_node)
873     cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
874                    build_int_cst (integer_type_node, annot_expr_ivdep_kind));
875   DO_COND (do_stmt) = cond;
876 }
877
878 /* Finish a return-statement.  The EXPRESSION returned, if any, is as
879    indicated.  */
880
881 tree
882 finish_return_stmt (tree expr)
883 {
884   tree r;
885   bool no_warning;
886
887   expr = check_return_expr (expr, &no_warning);
888
889   if (error_operand_p (expr)
890       || (flag_openmp && !check_omp_return ()))
891     {
892       /* Suppress -Wreturn-type for this function.  */
893       if (warn_return_type)
894         TREE_NO_WARNING (current_function_decl) = true;
895       return error_mark_node;
896     }
897
898   if (!processing_template_decl)
899     {
900       if (warn_sequence_point)
901         verify_sequence_points (expr);
902       
903       if (DECL_DESTRUCTOR_P (current_function_decl)
904           || (DECL_CONSTRUCTOR_P (current_function_decl)
905               && targetm.cxx.cdtor_returns_this ()))
906         {
907           /* Similarly, all destructors must run destructors for
908              base-classes before returning.  So, all returns in a
909              destructor get sent to the DTOR_LABEL; finish_function emits
910              code to return a value there.  */
911           return finish_goto_stmt (cdtor_label);
912         }
913     }
914
915   r = build_stmt (input_location, RETURN_EXPR, expr);
916   TREE_NO_WARNING (r) |= no_warning;
917   r = maybe_cleanup_point_expr_void (r);
918   r = add_stmt (r);
919
920   return r;
921 }
922
923 /* Begin the scope of a for-statement or a range-for-statement.
924    Both the returned trees are to be used in a call to
925    begin_for_stmt or begin_range_for_stmt.  */
926
927 tree
928 begin_for_scope (tree *init)
929 {
930   tree scope = NULL_TREE;
931   if (flag_new_for_scope > 0)
932     scope = do_pushlevel (sk_for);
933
934   if (processing_template_decl)
935     *init = push_stmt_list ();
936   else
937     *init = NULL_TREE;
938
939   return scope;
940 }
941
942 /* Begin a for-statement.  Returns a new FOR_STMT.
943    SCOPE and INIT should be the return of begin_for_scope,
944    or both NULL_TREE  */
945
946 tree
947 begin_for_stmt (tree scope, tree init)
948 {
949   tree r;
950
951   r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
952                   NULL_TREE, NULL_TREE, NULL_TREE);
953
954   if (scope == NULL_TREE)
955     {
956       gcc_assert (!init || !(flag_new_for_scope > 0));
957       if (!init)
958         scope = begin_for_scope (&init);
959     }
960   FOR_INIT_STMT (r) = init;
961   FOR_SCOPE (r) = scope;
962
963   return r;
964 }
965
966 /* Finish the for-init-statement of a for-statement, which may be
967    given by FOR_STMT.  */
968
969 void
970 finish_for_init_stmt (tree for_stmt)
971 {
972   if (processing_template_decl)
973     FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
974   add_stmt (for_stmt);
975   FOR_BODY (for_stmt) = do_pushlevel (sk_block);
976   begin_cond (&FOR_COND (for_stmt));
977 }
978
979 /* Finish the COND of a for-statement, which may be given by
980    FOR_STMT.  */
981
982 void
983 finish_for_cond (tree cond, tree for_stmt, bool ivdep)
984 {
985   if (check_no_cilk (cond,
986          "Cilk array notation cannot be used in a condition for a for-loop",
987          "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
988     cond = error_mark_node;
989   cond = maybe_convert_cond (cond);
990   finish_cond (&FOR_COND (for_stmt), cond);
991   begin_maybe_infinite_loop (cond);
992   if (ivdep && cond != error_mark_node)
993     FOR_COND (for_stmt) = build2 (ANNOTATE_EXPR,
994                                   TREE_TYPE (FOR_COND (for_stmt)),
995                                   FOR_COND (for_stmt),
996                                   build_int_cst (integer_type_node,
997                                                  annot_expr_ivdep_kind));
998   simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
999 }
1000
1001 /* Finish the increment-EXPRESSION in a for-statement, which may be
1002    given by FOR_STMT.  */
1003
1004 void
1005 finish_for_expr (tree expr, tree for_stmt)
1006 {
1007   if (!expr)
1008     return;
1009   /* If EXPR is an overloaded function, issue an error; there is no
1010      context available to use to perform overload resolution.  */
1011   if (type_unknown_p (expr))
1012     {
1013       cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1014       expr = error_mark_node;
1015     }
1016   if (!processing_template_decl)
1017     {
1018       if (warn_sequence_point)
1019         verify_sequence_points (expr);
1020       expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1021                               tf_warning_or_error);
1022     }
1023   else if (!type_dependent_expression_p (expr))
1024     convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1025                      tf_warning_or_error);
1026   expr = maybe_cleanup_point_expr_void (expr);
1027   if (check_for_bare_parameter_packs (expr))
1028     expr = error_mark_node;
1029   FOR_EXPR (for_stmt) = expr;
1030 }
1031
1032 /* Finish the body of a for-statement, which may be given by
1033    FOR_STMT.  The increment-EXPR for the loop must be
1034    provided.
1035    It can also finish RANGE_FOR_STMT. */
1036
1037 void
1038 finish_for_stmt (tree for_stmt)
1039 {
1040   end_maybe_infinite_loop (boolean_true_node);
1041
1042   if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1043     RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1044   else
1045     FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1046
1047   /* Pop the scope for the body of the loop.  */
1048   if (flag_new_for_scope > 0)
1049     {
1050       tree scope;
1051       tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1052                          ? &RANGE_FOR_SCOPE (for_stmt)
1053                          : &FOR_SCOPE (for_stmt));
1054       scope = *scope_ptr;
1055       *scope_ptr = NULL;
1056       add_stmt (do_poplevel (scope));
1057     }
1058 }
1059
1060 /* Begin a range-for-statement.  Returns a new RANGE_FOR_STMT.
1061    SCOPE and INIT should be the return of begin_for_scope,
1062    or both NULL_TREE  .
1063    To finish it call finish_for_stmt(). */
1064
1065 tree
1066 begin_range_for_stmt (tree scope, tree init)
1067 {
1068   tree r;
1069
1070   begin_maybe_infinite_loop (boolean_false_node);
1071
1072   r = build_stmt (input_location, RANGE_FOR_STMT,
1073                   NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1074
1075   if (scope == NULL_TREE)
1076     {
1077       gcc_assert (!init || !(flag_new_for_scope > 0));
1078       if (!init)
1079         scope = begin_for_scope (&init);
1080     }
1081
1082   /* RANGE_FOR_STMTs do not use nor save the init tree, so we
1083      pop it now.  */
1084   if (init)
1085     pop_stmt_list (init);
1086   RANGE_FOR_SCOPE (r) = scope;
1087
1088   return r;
1089 }
1090
1091 /* Finish the head of a range-based for statement, which may
1092    be given by RANGE_FOR_STMT. DECL must be the declaration
1093    and EXPR must be the loop expression. */
1094
1095 void
1096 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1097 {
1098   RANGE_FOR_DECL (range_for_stmt) = decl;
1099   RANGE_FOR_EXPR (range_for_stmt) = expr;
1100   add_stmt (range_for_stmt);
1101   RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1102 }
1103
1104 /* Finish a break-statement.  */
1105
1106 tree
1107 finish_break_stmt (void)
1108 {
1109   /* In switch statements break is sometimes stylistically used after
1110      a return statement.  This can lead to spurious warnings about
1111      control reaching the end of a non-void function when it is
1112      inlined.  Note that we are calling block_may_fallthru with
1113      language specific tree nodes; this works because
1114      block_may_fallthru returns true when given something it does not
1115      understand.  */
1116   if (!block_may_fallthru (cur_stmt_list))
1117     return void_node;
1118   return add_stmt (build_stmt (input_location, BREAK_STMT));
1119 }
1120
1121 /* Finish a continue-statement.  */
1122
1123 tree
1124 finish_continue_stmt (void)
1125 {
1126   return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1127 }
1128
1129 /* Begin a switch-statement.  Returns a new SWITCH_STMT if
1130    appropriate.  */
1131
1132 tree
1133 begin_switch_stmt (void)
1134 {
1135   tree r, scope;
1136
1137   scope = do_pushlevel (sk_cond);
1138   r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1139
1140   begin_cond (&SWITCH_STMT_COND (r));
1141
1142   return r;
1143 }
1144
1145 /* Finish the cond of a switch-statement.  */
1146
1147 void
1148 finish_switch_cond (tree cond, tree switch_stmt)
1149 {
1150   tree orig_type = NULL;
1151
1152   if (check_no_cilk (cond,
1153         "Cilk array notation cannot be used as a condition for switch statement",
1154         "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement"))
1155     cond = error_mark_node;
1156
1157   if (!processing_template_decl)
1158     {
1159       /* Convert the condition to an integer or enumeration type.  */
1160       cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1161       if (cond == NULL_TREE)
1162         {
1163           error ("switch quantity not an integer");
1164           cond = error_mark_node;
1165         }
1166       /* We want unlowered type here to handle enum bit-fields.  */
1167       orig_type = unlowered_expr_type (cond);
1168       if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1169         orig_type = TREE_TYPE (cond);
1170       if (cond != error_mark_node)
1171         {
1172           /* Warn if the condition has boolean value.  */
1173           if (TREE_CODE (orig_type) == BOOLEAN_TYPE)
1174             warning_at (input_location, OPT_Wswitch_bool,
1175                         "switch condition has type bool");
1176
1177           /* [stmt.switch]
1178
1179              Integral promotions are performed.  */
1180           cond = perform_integral_promotions (cond);
1181           cond = maybe_cleanup_point_expr (cond);
1182         }
1183     }
1184   if (check_for_bare_parameter_packs (cond))
1185     cond = error_mark_node;
1186   else if (!processing_template_decl && warn_sequence_point)
1187     verify_sequence_points (cond);
1188
1189   finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1190   SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1191   add_stmt (switch_stmt);
1192   push_switch (switch_stmt);
1193   SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1194 }
1195
1196 /* Finish the body of a switch-statement, which may be given by
1197    SWITCH_STMT.  The COND to switch on is indicated.  */
1198
1199 void
1200 finish_switch_stmt (tree switch_stmt)
1201 {
1202   tree scope;
1203
1204   SWITCH_STMT_BODY (switch_stmt) =
1205     pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1206   pop_switch ();
1207
1208   scope = SWITCH_STMT_SCOPE (switch_stmt);
1209   SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1210   add_stmt (do_poplevel (scope));
1211 }
1212
1213 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
1214    appropriate.  */
1215
1216 tree
1217 begin_try_block (void)
1218 {
1219   tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1220   add_stmt (r);
1221   TRY_STMTS (r) = push_stmt_list ();
1222   return r;
1223 }
1224
1225 /* Likewise, for a function-try-block.  The block returned in
1226    *COMPOUND_STMT is an artificial outer scope, containing the
1227    function-try-block.  */
1228
1229 tree
1230 begin_function_try_block (tree *compound_stmt)
1231 {
1232   tree r;
1233   /* This outer scope does not exist in the C++ standard, but we need
1234      a place to put __FUNCTION__ and similar variables.  */
1235   *compound_stmt = begin_compound_stmt (0);
1236   r = begin_try_block ();
1237   FN_TRY_BLOCK_P (r) = 1;
1238   return r;
1239 }
1240
1241 /* Finish a try-block, which may be given by TRY_BLOCK.  */
1242
1243 void
1244 finish_try_block (tree try_block)
1245 {
1246   TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1247   TRY_HANDLERS (try_block) = push_stmt_list ();
1248 }
1249
1250 /* Finish the body of a cleanup try-block, which may be given by
1251    TRY_BLOCK.  */
1252
1253 void
1254 finish_cleanup_try_block (tree try_block)
1255 {
1256   TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1257 }
1258
1259 /* Finish an implicitly generated try-block, with a cleanup is given
1260    by CLEANUP.  */
1261
1262 void
1263 finish_cleanup (tree cleanup, tree try_block)
1264 {
1265   TRY_HANDLERS (try_block) = cleanup;
1266   CLEANUP_P (try_block) = 1;
1267 }
1268
1269 /* Likewise, for a function-try-block.  */
1270
1271 void
1272 finish_function_try_block (tree try_block)
1273 {
1274   finish_try_block (try_block);
1275   /* FIXME : something queer about CTOR_INITIALIZER somehow following
1276      the try block, but moving it inside.  */
1277   in_function_try_handler = 1;
1278 }
1279
1280 /* Finish a handler-sequence for a try-block, which may be given by
1281    TRY_BLOCK.  */
1282
1283 void
1284 finish_handler_sequence (tree try_block)
1285 {
1286   TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1287   check_handlers (TRY_HANDLERS (try_block));
1288 }
1289
1290 /* Finish the handler-seq for a function-try-block, given by
1291    TRY_BLOCK.  COMPOUND_STMT is the outer block created by
1292    begin_function_try_block.  */
1293
1294 void
1295 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1296 {
1297   in_function_try_handler = 0;
1298   finish_handler_sequence (try_block);
1299   finish_compound_stmt (compound_stmt);
1300 }
1301
1302 /* Begin a handler.  Returns a HANDLER if appropriate.  */
1303
1304 tree
1305 begin_handler (void)
1306 {
1307   tree r;
1308
1309   r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1310   add_stmt (r);
1311
1312   /* Create a binding level for the eh_info and the exception object
1313      cleanup.  */
1314   HANDLER_BODY (r) = do_pushlevel (sk_catch);
1315
1316   return r;
1317 }
1318
1319 /* Finish the handler-parameters for a handler, which may be given by
1320    HANDLER.  DECL is the declaration for the catch parameter, or NULL
1321    if this is a `catch (...)' clause.  */
1322
1323 void
1324 finish_handler_parms (tree decl, tree handler)
1325 {
1326   tree type = NULL_TREE;
1327   if (processing_template_decl)
1328     {
1329       if (decl)
1330         {
1331           decl = pushdecl (decl);
1332           decl = push_template_decl (decl);
1333           HANDLER_PARMS (handler) = decl;
1334           type = TREE_TYPE (decl);
1335         }
1336     }
1337   else
1338     type = expand_start_catch_block (decl);
1339   HANDLER_TYPE (handler) = type;
1340 }
1341
1342 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
1343    the return value from the matching call to finish_handler_parms.  */
1344
1345 void
1346 finish_handler (tree handler)
1347 {
1348   if (!processing_template_decl)
1349     expand_end_catch_block ();
1350   HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1351 }
1352
1353 /* Begin a compound statement.  FLAGS contains some bits that control the
1354    behavior and context.  If BCS_NO_SCOPE is set, the compound statement
1355    does not define a scope.  If BCS_FN_BODY is set, this is the outermost
1356    block of a function.  If BCS_TRY_BLOCK is set, this is the block
1357    created on behalf of a TRY statement.  Returns a token to be passed to
1358    finish_compound_stmt.  */
1359
1360 tree
1361 begin_compound_stmt (unsigned int flags)
1362 {
1363   tree r;
1364
1365   if (flags & BCS_NO_SCOPE)
1366     {
1367       r = push_stmt_list ();
1368       STATEMENT_LIST_NO_SCOPE (r) = 1;
1369
1370       /* Normally, we try hard to keep the BLOCK for a statement-expression.
1371          But, if it's a statement-expression with a scopeless block, there's
1372          nothing to keep, and we don't want to accidentally keep a block
1373          *inside* the scopeless block.  */
1374       keep_next_level (false);
1375     }
1376   else
1377     r = do_pushlevel (flags & BCS_TRY_BLOCK ? sk_try : sk_block);
1378
1379   /* When processing a template, we need to remember where the braces were,
1380      so that we can set up identical scopes when instantiating the template
1381      later.  BIND_EXPR is a handy candidate for this.
1382      Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1383      result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1384      processing templates.  */
1385   if (processing_template_decl)
1386     {
1387       r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1388       BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1389       BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1390       TREE_SIDE_EFFECTS (r) = 1;
1391     }
1392
1393   return r;
1394 }
1395
1396 /* Finish a compound-statement, which is given by STMT.  */
1397
1398 void
1399 finish_compound_stmt (tree stmt)
1400 {
1401   if (TREE_CODE (stmt) == BIND_EXPR)
1402     {
1403       tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1404       /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1405          discard the BIND_EXPR so it can be merged with the containing
1406          STATEMENT_LIST.  */
1407       if (TREE_CODE (body) == STATEMENT_LIST
1408           && STATEMENT_LIST_HEAD (body) == NULL
1409           && !BIND_EXPR_BODY_BLOCK (stmt)
1410           && !BIND_EXPR_TRY_BLOCK (stmt))
1411         stmt = body;
1412       else
1413         BIND_EXPR_BODY (stmt) = body;
1414     }
1415   else if (STATEMENT_LIST_NO_SCOPE (stmt))
1416     stmt = pop_stmt_list (stmt);
1417   else
1418     {
1419       /* Destroy any ObjC "super" receivers that may have been
1420          created.  */
1421       objc_clear_super_receiver ();
1422
1423       stmt = do_poplevel (stmt);
1424     }
1425
1426   /* ??? See c_end_compound_stmt wrt statement expressions.  */
1427   add_stmt (stmt);
1428 }
1429
1430 /* Finish an asm-statement, whose components are a STRING, some
1431    OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1432    LABELS.  Also note whether the asm-statement should be
1433    considered volatile.  */
1434
1435 tree
1436 finish_asm_stmt (int volatile_p, tree string, tree output_operands,
1437                  tree input_operands, tree clobbers, tree labels)
1438 {
1439   tree r;
1440   tree t;
1441   int ninputs = list_length (input_operands);
1442   int noutputs = list_length (output_operands);
1443
1444   if (!processing_template_decl)
1445     {
1446       const char *constraint;
1447       const char **oconstraints;
1448       bool allows_mem, allows_reg, is_inout;
1449       tree operand;
1450       int i;
1451
1452       oconstraints = XALLOCAVEC (const char *, noutputs);
1453
1454       string = resolve_asm_operand_names (string, output_operands,
1455                                           input_operands, labels);
1456
1457       for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1458         {
1459           operand = TREE_VALUE (t);
1460
1461           /* ??? Really, this should not be here.  Users should be using a
1462              proper lvalue, dammit.  But there's a long history of using
1463              casts in the output operands.  In cases like longlong.h, this
1464              becomes a primitive form of typechecking -- if the cast can be
1465              removed, then the output operand had a type of the proper width;
1466              otherwise we'll get an error.  Gross, but ...  */
1467           STRIP_NOPS (operand);
1468
1469           operand = mark_lvalue_use (operand);
1470
1471           if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1472             operand = error_mark_node;
1473
1474           if (operand != error_mark_node
1475               && (TREE_READONLY (operand)
1476                   || CP_TYPE_CONST_P (TREE_TYPE (operand))
1477                   /* Functions are not modifiable, even though they are
1478                      lvalues.  */
1479                   || TREE_CODE (TREE_TYPE (operand)) == FUNCTION_TYPE
1480                   || TREE_CODE (TREE_TYPE (operand)) == METHOD_TYPE
1481                   /* If it's an aggregate and any field is const, then it is
1482                      effectively const.  */
1483                   || (CLASS_TYPE_P (TREE_TYPE (operand))
1484                       && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1485             cxx_readonly_error (operand, lv_asm);
1486
1487           constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1488           oconstraints[i] = constraint;
1489
1490           if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1491                                        &allows_mem, &allows_reg, &is_inout))
1492             {
1493               /* If the operand is going to end up in memory,
1494                  mark it addressable.  */
1495               if (!allows_reg && !cxx_mark_addressable (operand))
1496                 operand = error_mark_node;
1497             }
1498           else
1499             operand = error_mark_node;
1500
1501           TREE_VALUE (t) = operand;
1502         }
1503
1504       for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1505         {
1506           constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1507           bool constraint_parsed
1508             = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,   
1509                                       oconstraints, &allows_mem, &allows_reg);
1510           /* If the operand is going to end up in memory, don't call
1511              decay_conversion.  */
1512           if (constraint_parsed && !allows_reg && allows_mem)
1513             operand = mark_lvalue_use (TREE_VALUE (t));
1514           else
1515             operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1516
1517           /* If the type of the operand hasn't been determined (e.g.,
1518              because it involves an overloaded function), then issue
1519              an error message.  There's no context available to
1520              resolve the overloading.  */
1521           if (TREE_TYPE (operand) == unknown_type_node)
1522             {
1523               error ("type of asm operand %qE could not be determined",
1524                      TREE_VALUE (t));
1525               operand = error_mark_node;
1526             }
1527
1528           if (constraint_parsed)
1529             {
1530               /* If the operand is going to end up in memory,
1531                  mark it addressable.  */
1532               if (!allows_reg && allows_mem)
1533                 {
1534                   /* Strip the nops as we allow this case.  FIXME, this really
1535                      should be rejected or made deprecated.  */
1536                   STRIP_NOPS (operand);
1537                   if (!cxx_mark_addressable (operand))
1538                     operand = error_mark_node;
1539                 }
1540               else if (!allows_reg && !allows_mem)
1541                 {
1542                   /* If constraint allows neither register nor memory,
1543                      try harder to get a constant.  */
1544                   tree constop = maybe_constant_value (operand);
1545                   if (TREE_CONSTANT (constop))
1546                     operand = constop;
1547                 }
1548             }
1549           else
1550             operand = error_mark_node;
1551
1552           TREE_VALUE (t) = operand;
1553         }
1554     }
1555
1556   r = build_stmt (input_location, ASM_EXPR, string,
1557                   output_operands, input_operands,
1558                   clobbers, labels);
1559   ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1560   r = maybe_cleanup_point_expr_void (r);
1561   return add_stmt (r);
1562 }
1563
1564 /* Finish a label with the indicated NAME.  Returns the new label.  */
1565
1566 tree
1567 finish_label_stmt (tree name)
1568 {
1569   tree decl = define_label (input_location, name);
1570
1571   if (decl == error_mark_node)
1572     return error_mark_node;
1573
1574   add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
1575
1576   return decl;
1577 }
1578
1579 /* Finish a series of declarations for local labels.  G++ allows users
1580    to declare "local" labels, i.e., labels with scope.  This extension
1581    is useful when writing code involving statement-expressions.  */
1582
1583 void
1584 finish_label_decl (tree name)
1585 {
1586   if (!at_function_scope_p ())
1587     {
1588       error ("__label__ declarations are only allowed in function scopes");
1589       return;
1590     }
1591
1592   add_decl_expr (declare_local_label (name));
1593 }
1594
1595 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
1596
1597 void
1598 finish_decl_cleanup (tree decl, tree cleanup)
1599 {
1600   push_cleanup (decl, cleanup, false);
1601 }
1602
1603 /* If the current scope exits with an exception, run CLEANUP.  */
1604
1605 void
1606 finish_eh_cleanup (tree cleanup)
1607 {
1608   push_cleanup (NULL, cleanup, true);
1609 }
1610
1611 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1612    order they were written by the user.  Each node is as for
1613    emit_mem_initializers.  */
1614
1615 void
1616 finish_mem_initializers (tree mem_inits)
1617 {
1618   /* Reorder the MEM_INITS so that they are in the order they appeared
1619      in the source program.  */
1620   mem_inits = nreverse (mem_inits);
1621
1622   if (processing_template_decl)
1623     {
1624       tree mem;
1625
1626       for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
1627         {
1628           /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
1629              check for bare parameter packs in the TREE_VALUE, because
1630              any parameter packs in the TREE_VALUE have already been
1631              bound as part of the TREE_PURPOSE.  See
1632              make_pack_expansion for more information.  */
1633           if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
1634               && check_for_bare_parameter_packs (TREE_VALUE (mem)))
1635             TREE_VALUE (mem) = error_mark_node;
1636         }
1637
1638       add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
1639                                   CTOR_INITIALIZER, mem_inits));
1640     }
1641   else
1642     emit_mem_initializers (mem_inits);
1643 }
1644
1645 /* Obfuscate EXPR if it looks like an id-expression or member access so
1646    that the call to finish_decltype in do_auto_deduction will give the
1647    right result.  */
1648
1649 tree
1650 force_paren_expr (tree expr)
1651 {
1652   /* This is only needed for decltype(auto) in C++14.  */
1653   if (cxx_dialect < cxx14)
1654     return expr;
1655
1656   /* If we're in unevaluated context, we can't be deducing a
1657      return/initializer type, so we don't need to mess with this.  */
1658   if (cp_unevaluated_operand)
1659     return expr;
1660
1661   if (!DECL_P (expr) && TREE_CODE (expr) != COMPONENT_REF
1662       && TREE_CODE (expr) != SCOPE_REF)
1663     return expr;
1664
1665   if (TREE_CODE (expr) == COMPONENT_REF)
1666     REF_PARENTHESIZED_P (expr) = true;
1667   else if (type_dependent_expression_p (expr))
1668     expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
1669   else
1670     {
1671       cp_lvalue_kind kind = lvalue_kind (expr);
1672       if ((kind & ~clk_class) != clk_none)
1673         {
1674           tree type = unlowered_expr_type (expr);
1675           bool rval = !!(kind & clk_rvalueref);
1676           type = cp_build_reference_type (type, rval);
1677           /* This inhibits warnings in, eg, cxx_mark_addressable
1678              (c++/60955).  */
1679           warning_sentinel s (extra_warnings);
1680           expr = build_static_cast (type, expr, tf_error);
1681           if (expr != error_mark_node)
1682             REF_PARENTHESIZED_P (expr) = true;
1683         }
1684     }
1685
1686   return expr;
1687 }
1688
1689 /* Finish a parenthesized expression EXPR.  */
1690
1691 tree
1692 finish_parenthesized_expr (tree expr)
1693 {
1694   if (EXPR_P (expr))
1695     /* This inhibits warnings in c_common_truthvalue_conversion.  */
1696     TREE_NO_WARNING (expr) = 1;
1697
1698   if (TREE_CODE (expr) == OFFSET_REF
1699       || TREE_CODE (expr) == SCOPE_REF)
1700     /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1701        enclosed in parentheses.  */
1702     PTRMEM_OK_P (expr) = 0;
1703
1704   if (TREE_CODE (expr) == STRING_CST)
1705     PAREN_STRING_LITERAL_P (expr) = 1;
1706
1707   expr = force_paren_expr (expr);
1708
1709   return expr;
1710 }
1711
1712 /* Finish a reference to a non-static data member (DECL) that is not
1713    preceded by `.' or `->'.  */
1714
1715 tree
1716 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
1717 {
1718   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1719
1720   if (!object)
1721     {
1722       tree scope = qualifying_scope;
1723       if (scope == NULL_TREE)
1724         scope = context_for_name_lookup (decl);
1725       object = maybe_dummy_object (scope, NULL);
1726     }
1727
1728   object = maybe_resolve_dummy (object, true);
1729   if (object == error_mark_node)
1730     return error_mark_node;
1731
1732   /* DR 613/850: Can use non-static data members without an associated
1733      object in sizeof/decltype/alignof.  */
1734   if (is_dummy_object (object) && cp_unevaluated_operand == 0
1735       && (!processing_template_decl || !current_class_ref))
1736     {
1737       if (current_function_decl
1738           && DECL_STATIC_FUNCTION_P (current_function_decl))
1739         error ("invalid use of member %qD in static member function", decl);
1740       else
1741         error ("invalid use of non-static data member %qD", decl);
1742       inform (DECL_SOURCE_LOCATION (decl), "declared here");
1743
1744       return error_mark_node;
1745     }
1746
1747   if (current_class_ptr)
1748     TREE_USED (current_class_ptr) = 1;
1749   if (processing_template_decl && !qualifying_scope)
1750     {
1751       tree type = TREE_TYPE (decl);
1752
1753       if (TREE_CODE (type) == REFERENCE_TYPE)
1754         /* Quals on the object don't matter.  */;
1755       else if (PACK_EXPANSION_P (type))
1756         /* Don't bother trying to represent this.  */
1757         type = NULL_TREE;
1758       else
1759         {
1760           /* Set the cv qualifiers.  */
1761           int quals = cp_type_quals (TREE_TYPE (object));
1762
1763           if (DECL_MUTABLE_P (decl))
1764             quals &= ~TYPE_QUAL_CONST;
1765
1766           quals |= cp_type_quals (TREE_TYPE (decl));
1767           type = cp_build_qualified_type (type, quals);
1768         }
1769
1770       return (convert_from_reference
1771               (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
1772     }
1773   /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
1774      QUALIFYING_SCOPE is also non-null.  Wrap this in a SCOPE_REF
1775      for now.  */
1776   else if (processing_template_decl)
1777     return build_qualified_name (TREE_TYPE (decl),
1778                                  qualifying_scope,
1779                                  decl,
1780                                  /*template_p=*/false);
1781   else
1782     {
1783       tree access_type = TREE_TYPE (object);
1784
1785       perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
1786                                      decl, tf_warning_or_error);
1787
1788       /* If the data member was named `C::M', convert `*this' to `C'
1789          first.  */
1790       if (qualifying_scope)
1791         {
1792           tree binfo = NULL_TREE;
1793           object = build_scoped_ref (object, qualifying_scope,
1794                                      &binfo);
1795         }
1796
1797       return build_class_member_access_expr (object, decl,
1798                                              /*access_path=*/NULL_TREE,
1799                                              /*preserve_reference=*/false,
1800                                              tf_warning_or_error);
1801     }
1802 }
1803
1804 /* If we are currently parsing a template and we encountered a typedef
1805    TYPEDEF_DECL that is being accessed though CONTEXT, this function
1806    adds the typedef to a list tied to the current template.
1807    At template instantiation time, that list is walked and access check
1808    performed for each typedef.
1809    LOCATION is the location of the usage point of TYPEDEF_DECL.  */
1810
1811 void
1812 add_typedef_to_current_template_for_access_check (tree typedef_decl,
1813                                                   tree context,
1814                                                   location_t location)
1815 {
1816     tree template_info = NULL;
1817     tree cs = current_scope ();
1818
1819     if (!is_typedef_decl (typedef_decl)
1820         || !context
1821         || !CLASS_TYPE_P (context)
1822         || !cs)
1823       return;
1824
1825     if (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL)
1826       template_info = get_template_info (cs);
1827
1828     if (template_info
1829         && TI_TEMPLATE (template_info)
1830         && !currently_open_class (context))
1831       append_type_to_template_for_access_check (cs, typedef_decl,
1832                                                 context, location);
1833 }
1834
1835 /* DECL was the declaration to which a qualified-id resolved.  Issue
1836    an error message if it is not accessible.  If OBJECT_TYPE is
1837    non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
1838    type of `*x', or `x', respectively.  If the DECL was named as
1839    `A::B' then NESTED_NAME_SPECIFIER is `A'.  */
1840
1841 void
1842 check_accessibility_of_qualified_id (tree decl,
1843                                      tree object_type,
1844                                      tree nested_name_specifier)
1845 {
1846   tree scope;
1847   tree qualifying_type = NULL_TREE;
1848
1849   /* If we are parsing a template declaration and if decl is a typedef,
1850      add it to a list tied to the template.
1851      At template instantiation time, that list will be walked and
1852      access check performed.  */
1853   add_typedef_to_current_template_for_access_check (decl,
1854                                                     nested_name_specifier
1855                                                     ? nested_name_specifier
1856                                                     : DECL_CONTEXT (decl),
1857                                                     input_location);
1858
1859   /* If we're not checking, return immediately.  */
1860   if (deferred_access_no_check)
1861     return;
1862
1863   /* Determine the SCOPE of DECL.  */
1864   scope = context_for_name_lookup (decl);
1865   /* If the SCOPE is not a type, then DECL is not a member.  */
1866   if (!TYPE_P (scope))
1867     return;
1868   /* Compute the scope through which DECL is being accessed.  */
1869   if (object_type
1870       /* OBJECT_TYPE might not be a class type; consider:
1871
1872            class A { typedef int I; };
1873            I *p;
1874            p->A::I::~I();
1875
1876          In this case, we will have "A::I" as the DECL, but "I" as the
1877          OBJECT_TYPE.  */
1878       && CLASS_TYPE_P (object_type)
1879       && DERIVED_FROM_P (scope, object_type))
1880     /* If we are processing a `->' or `.' expression, use the type of the
1881        left-hand side.  */
1882     qualifying_type = object_type;
1883   else if (nested_name_specifier)
1884     {
1885       /* If the reference is to a non-static member of the
1886          current class, treat it as if it were referenced through
1887          `this'.  */
1888       tree ct;
1889       if (DECL_NONSTATIC_MEMBER_P (decl)
1890           && current_class_ptr
1891           && DERIVED_FROM_P (scope, ct = current_nonlambda_class_type ()))
1892         qualifying_type = ct;
1893       /* Otherwise, use the type indicated by the
1894          nested-name-specifier.  */
1895       else
1896         qualifying_type = nested_name_specifier;
1897     }
1898   else
1899     /* Otherwise, the name must be from the current class or one of
1900        its bases.  */
1901     qualifying_type = currently_open_derived_class (scope);
1902
1903   if (qualifying_type 
1904       /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
1905          or similar in a default argument value.  */
1906       && CLASS_TYPE_P (qualifying_type)
1907       && !dependent_type_p (qualifying_type))
1908     perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
1909                                    decl, tf_warning_or_error);
1910 }
1911
1912 /* EXPR is the result of a qualified-id.  The QUALIFYING_CLASS was the
1913    class named to the left of the "::" operator.  DONE is true if this
1914    expression is a complete postfix-expression; it is false if this
1915    expression is followed by '->', '[', '(', etc.  ADDRESS_P is true
1916    iff this expression is the operand of '&'.  TEMPLATE_P is true iff
1917    the qualified-id was of the form "A::template B".  TEMPLATE_ARG_P
1918    is true iff this qualified name appears as a template argument.  */
1919
1920 tree
1921 finish_qualified_id_expr (tree qualifying_class,
1922                           tree expr,
1923                           bool done,
1924                           bool address_p,
1925                           bool template_p,
1926                           bool template_arg_p,
1927                           tsubst_flags_t complain)
1928 {
1929   gcc_assert (TYPE_P (qualifying_class));
1930
1931   if (error_operand_p (expr))
1932     return error_mark_node;
1933
1934   if ((DECL_P (expr) || BASELINK_P (expr))
1935       && !mark_used (expr, complain))
1936     return error_mark_node;
1937
1938   if (template_p)
1939     check_template_keyword (expr);
1940
1941   /* If EXPR occurs as the operand of '&', use special handling that
1942      permits a pointer-to-member.  */
1943   if (address_p && done)
1944     {
1945       if (TREE_CODE (expr) == SCOPE_REF)
1946         expr = TREE_OPERAND (expr, 1);
1947       expr = build_offset_ref (qualifying_class, expr,
1948                                /*address_p=*/true, complain);
1949       return expr;
1950     }
1951
1952   /* No need to check access within an enum.  */
1953   if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE)
1954     return expr;
1955
1956   /* Within the scope of a class, turn references to non-static
1957      members into expression of the form "this->...".  */
1958   if (template_arg_p)
1959     /* But, within a template argument, we do not want make the
1960        transformation, as there is no "this" pointer.  */
1961     ;
1962   else if (TREE_CODE (expr) == FIELD_DECL)
1963     {
1964       push_deferring_access_checks (dk_no_check);
1965       expr = finish_non_static_data_member (expr, NULL_TREE,
1966                                             qualifying_class);
1967       pop_deferring_access_checks ();
1968     }
1969   else if (BASELINK_P (expr) && !processing_template_decl)
1970     {
1971       /* See if any of the functions are non-static members.  */
1972       /* If so, the expression may be relative to 'this'.  */
1973       if (!shared_member_p (expr)
1974           && current_class_ptr
1975           && DERIVED_FROM_P (qualifying_class,
1976                              current_nonlambda_class_type ()))
1977         expr = (build_class_member_access_expr
1978                 (maybe_dummy_object (qualifying_class, NULL),
1979                  expr,
1980                  BASELINK_ACCESS_BINFO (expr),
1981                  /*preserve_reference=*/false,
1982                  complain));
1983       else if (done)
1984         /* The expression is a qualified name whose address is not
1985            being taken.  */
1986         expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
1987                                  complain);
1988     }
1989   else if (BASELINK_P (expr))
1990     ;
1991   else
1992     {
1993       /* In a template, return a SCOPE_REF for most qualified-ids
1994          so that we can check access at instantiation time.  But if
1995          we're looking at a member of the current instantiation, we
1996          know we have access and building up the SCOPE_REF confuses
1997          non-type template argument handling.  */
1998       if (processing_template_decl
1999           && !currently_open_class (qualifying_class))
2000         expr = build_qualified_name (TREE_TYPE (expr),
2001                                      qualifying_class, expr,
2002                                      template_p);
2003
2004       expr = convert_from_reference (expr);
2005     }
2006
2007   return expr;
2008 }
2009
2010 /* Begin a statement-expression.  The value returned must be passed to
2011    finish_stmt_expr.  */
2012
2013 tree
2014 begin_stmt_expr (void)
2015 {
2016   return push_stmt_list ();
2017 }
2018
2019 /* Process the final expression of a statement expression. EXPR can be
2020    NULL, if the final expression is empty.  Return a STATEMENT_LIST
2021    containing all the statements in the statement-expression, or
2022    ERROR_MARK_NODE if there was an error.  */
2023
2024 tree
2025 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2026 {
2027   if (error_operand_p (expr))
2028     {
2029       /* The type of the statement-expression is the type of the last
2030          expression.  */
2031       TREE_TYPE (stmt_expr) = error_mark_node;
2032       return error_mark_node;
2033     }
2034
2035   /* If the last statement does not have "void" type, then the value
2036      of the last statement is the value of the entire expression.  */
2037   if (expr)
2038     {
2039       tree type = TREE_TYPE (expr);
2040
2041       if (processing_template_decl)
2042         {
2043           expr = build_stmt (input_location, EXPR_STMT, expr);
2044           expr = add_stmt (expr);
2045           /* Mark the last statement so that we can recognize it as such at
2046              template-instantiation time.  */
2047           EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2048         }
2049       else if (VOID_TYPE_P (type))
2050         {
2051           /* Just treat this like an ordinary statement.  */
2052           expr = finish_expr_stmt (expr);
2053         }
2054       else
2055         {
2056           /* It actually has a value we need to deal with.  First, force it
2057              to be an rvalue so that we won't need to build up a copy
2058              constructor call later when we try to assign it to something.  */
2059           expr = force_rvalue (expr, tf_warning_or_error);
2060           if (error_operand_p (expr))
2061             return error_mark_node;
2062
2063           /* Update for array-to-pointer decay.  */
2064           type = TREE_TYPE (expr);
2065
2066           /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2067              normal statement, but don't convert to void or actually add
2068              the EXPR_STMT.  */
2069           if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2070             expr = maybe_cleanup_point_expr (expr);
2071           add_stmt (expr);
2072         }
2073
2074       /* The type of the statement-expression is the type of the last
2075          expression.  */
2076       TREE_TYPE (stmt_expr) = type;
2077     }
2078
2079   return stmt_expr;
2080 }
2081
2082 /* Finish a statement-expression.  EXPR should be the value returned
2083    by the previous begin_stmt_expr.  Returns an expression
2084    representing the statement-expression.  */
2085
2086 tree
2087 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2088 {
2089   tree type;
2090   tree result;
2091
2092   if (error_operand_p (stmt_expr))
2093     {
2094       pop_stmt_list (stmt_expr);
2095       return error_mark_node;
2096     }
2097
2098   gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2099
2100   type = TREE_TYPE (stmt_expr);
2101   result = pop_stmt_list (stmt_expr);
2102   TREE_TYPE (result) = type;
2103
2104   if (processing_template_decl)
2105     {
2106       result = build_min (STMT_EXPR, type, result);
2107       TREE_SIDE_EFFECTS (result) = 1;
2108       STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2109     }
2110   else if (CLASS_TYPE_P (type))
2111     {
2112       /* Wrap the statement-expression in a TARGET_EXPR so that the
2113          temporary object created by the final expression is destroyed at
2114          the end of the full-expression containing the
2115          statement-expression.  */
2116       result = force_target_expr (type, result, tf_warning_or_error);
2117     }
2118
2119   return result;
2120 }
2121
2122 /* Returns the expression which provides the value of STMT_EXPR.  */
2123
2124 tree
2125 stmt_expr_value_expr (tree stmt_expr)
2126 {
2127   tree t = STMT_EXPR_STMT (stmt_expr);
2128
2129   if (TREE_CODE (t) == BIND_EXPR)
2130     t = BIND_EXPR_BODY (t);
2131
2132   if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2133     t = STATEMENT_LIST_TAIL (t)->stmt;
2134
2135   if (TREE_CODE (t) == EXPR_STMT)
2136     t = EXPR_STMT_EXPR (t);
2137
2138   return t;
2139 }
2140
2141 /* Return TRUE iff EXPR_STMT is an empty list of
2142    expression statements.  */
2143
2144 bool
2145 empty_expr_stmt_p (tree expr_stmt)
2146 {
2147   tree body = NULL_TREE;
2148
2149   if (expr_stmt == void_node)
2150     return true;
2151
2152   if (expr_stmt)
2153     {
2154       if (TREE_CODE (expr_stmt) == EXPR_STMT)
2155         body = EXPR_STMT_EXPR (expr_stmt);
2156       else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2157         body = expr_stmt;
2158     }
2159
2160   if (body)
2161     {
2162       if (TREE_CODE (body) == STATEMENT_LIST)
2163         return tsi_end_p (tsi_start (body));
2164       else
2165         return empty_expr_stmt_p (body);
2166     }
2167   return false;
2168 }
2169
2170 /* Perform Koenig lookup.  FN is the postfix-expression representing
2171    the function (or functions) to call; ARGS are the arguments to the
2172    call.  Returns the functions to be considered by overload resolution.  */
2173
2174 tree
2175 perform_koenig_lookup (tree fn, vec<tree, va_gc> *args,
2176                        tsubst_flags_t complain)
2177 {
2178   tree identifier = NULL_TREE;
2179   tree functions = NULL_TREE;
2180   tree tmpl_args = NULL_TREE;
2181   bool template_id = false;
2182
2183   if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2184     {
2185       /* Use a separate flag to handle null args.  */
2186       template_id = true;
2187       tmpl_args = TREE_OPERAND (fn, 1);
2188       fn = TREE_OPERAND (fn, 0);
2189     }
2190
2191   /* Find the name of the overloaded function.  */
2192   if (identifier_p (fn))
2193     identifier = fn;
2194   else if (is_overloaded_fn (fn))
2195     {
2196       functions = fn;
2197       identifier = DECL_NAME (get_first_fn (functions));
2198     }
2199   else if (DECL_P (fn))
2200     {
2201       functions = fn;
2202       identifier = DECL_NAME (fn);
2203     }
2204
2205   /* A call to a namespace-scope function using an unqualified name.
2206
2207      Do Koenig lookup -- unless any of the arguments are
2208      type-dependent.  */
2209   if (!any_type_dependent_arguments_p (args)
2210       && !any_dependent_template_arguments_p (tmpl_args))
2211     {
2212       fn = lookup_arg_dependent (identifier, functions, args);
2213       if (!fn)
2214         {
2215           /* The unqualified name could not be resolved.  */
2216           if (complain)
2217             fn = unqualified_fn_lookup_error (identifier);
2218           else
2219             fn = identifier;
2220         }
2221     }
2222
2223   if (fn && template_id)
2224     fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2225   
2226   return fn;
2227 }
2228
2229 /* Generate an expression for `FN (ARGS)'.  This may change the
2230    contents of ARGS.
2231
2232    If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2233    as a virtual call, even if FN is virtual.  (This flag is set when
2234    encountering an expression where the function name is explicitly
2235    qualified.  For example a call to `X::f' never generates a virtual
2236    call.)
2237
2238    Returns code for the call.  */
2239
2240 tree
2241 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2242                   bool koenig_p, tsubst_flags_t complain)
2243 {
2244   tree result;
2245   tree orig_fn;
2246   vec<tree, va_gc> *orig_args = NULL;
2247
2248   if (fn == error_mark_node)
2249     return error_mark_node;
2250
2251   gcc_assert (!TYPE_P (fn));
2252
2253   orig_fn = fn;
2254
2255   if (processing_template_decl)
2256     {
2257       /* If the call expression is dependent, build a CALL_EXPR node
2258          with no type; type_dependent_expression_p recognizes
2259          expressions with no type as being dependent.  */
2260       if (type_dependent_expression_p (fn)
2261           || any_type_dependent_arguments_p (*args)
2262           /* For a non-static member function that doesn't have an
2263              explicit object argument, we need to specifically
2264              test the type dependency of the "this" pointer because it
2265              is not included in *ARGS even though it is considered to
2266              be part of the list of arguments.  Note that this is
2267              related to CWG issues 515 and 1005.  */
2268           || (TREE_CODE (fn) != COMPONENT_REF
2269               && non_static_member_function_p (fn)
2270               && current_class_ref
2271               && type_dependent_expression_p (current_class_ref)))
2272         {
2273           result = build_nt_call_vec (fn, *args);
2274           SET_EXPR_LOCATION (result, EXPR_LOC_OR_LOC (fn, input_location));
2275           KOENIG_LOOKUP_P (result) = koenig_p;
2276           if (cfun)
2277             {
2278               do
2279                 {
2280                   tree fndecl = OVL_CURRENT (fn);
2281                   if (TREE_CODE (fndecl) != FUNCTION_DECL
2282                       || !TREE_THIS_VOLATILE (fndecl))
2283                     break;
2284                   fn = OVL_NEXT (fn);
2285                 }
2286               while (fn);
2287               if (!fn)
2288                 current_function_returns_abnormally = 1;
2289             }
2290           return result;
2291         }
2292       orig_args = make_tree_vector_copy (*args);
2293       if (!BASELINK_P (fn)
2294           && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2295           && TREE_TYPE (fn) != unknown_type_node)
2296         fn = build_non_dependent_expr (fn);
2297       make_args_non_dependent (*args);
2298     }
2299
2300   if (TREE_CODE (fn) == COMPONENT_REF)
2301     {
2302       tree member = TREE_OPERAND (fn, 1);
2303       if (BASELINK_P (member))
2304         {
2305           tree object = TREE_OPERAND (fn, 0);
2306           return build_new_method_call (object, member,
2307                                         args, NULL_TREE,
2308                                         (disallow_virtual
2309                                          ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2310                                          : LOOKUP_NORMAL),
2311                                         /*fn_p=*/NULL,
2312                                         complain);
2313         }
2314     }
2315
2316   /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'.  */
2317   if (TREE_CODE (fn) == ADDR_EXPR
2318       && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2319     fn = TREE_OPERAND (fn, 0);
2320
2321   if (is_overloaded_fn (fn))
2322     fn = baselink_for_fns (fn);
2323
2324   result = NULL_TREE;
2325   if (BASELINK_P (fn))
2326     {
2327       tree object;
2328
2329       /* A call to a member function.  From [over.call.func]:
2330
2331            If the keyword this is in scope and refers to the class of
2332            that member function, or a derived class thereof, then the
2333            function call is transformed into a qualified function call
2334            using (*this) as the postfix-expression to the left of the
2335            . operator.... [Otherwise] a contrived object of type T
2336            becomes the implied object argument.
2337
2338         In this situation:
2339
2340           struct A { void f(); };
2341           struct B : public A {};
2342           struct C : public A { void g() { B::f(); }};
2343
2344         "the class of that member function" refers to `A'.  But 11.2
2345         [class.access.base] says that we need to convert 'this' to B* as
2346         part of the access, so we pass 'B' to maybe_dummy_object.  */
2347
2348       object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2349                                    NULL);
2350
2351       if (processing_template_decl)
2352         {
2353           if (type_dependent_expression_p (object))
2354             {
2355               tree ret = build_nt_call_vec (orig_fn, orig_args);
2356               release_tree_vector (orig_args);
2357               return ret;
2358             }
2359           object = build_non_dependent_expr (object);
2360         }
2361
2362       result = build_new_method_call (object, fn, args, NULL_TREE,
2363                                       (disallow_virtual
2364                                        ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2365                                        : LOOKUP_NORMAL),
2366                                       /*fn_p=*/NULL,
2367                                       complain);
2368     }
2369   else if (is_overloaded_fn (fn))
2370     {
2371       /* If the function is an overloaded builtin, resolve it.  */
2372       if (TREE_CODE (fn) == FUNCTION_DECL
2373           && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2374               || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2375         result = resolve_overloaded_builtin (input_location, fn, *args);
2376
2377       if (!result)
2378         {
2379           if (warn_sizeof_pointer_memaccess
2380               && !vec_safe_is_empty (*args)
2381               && !processing_template_decl)
2382             {
2383               location_t sizeof_arg_loc[3];
2384               tree sizeof_arg[3];
2385               unsigned int i;
2386               for (i = 0; i < 3; i++)
2387                 {
2388                   tree t;
2389
2390                   sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2391                   sizeof_arg[i] = NULL_TREE;
2392                   if (i >= (*args)->length ())
2393                     continue;
2394                   t = (**args)[i];
2395                   if (TREE_CODE (t) != SIZEOF_EXPR)
2396                     continue;
2397                   if (SIZEOF_EXPR_TYPE_P (t))
2398                     sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2399                   else
2400                     sizeof_arg[i] = TREE_OPERAND (t, 0);
2401                   sizeof_arg_loc[i] = EXPR_LOCATION (t);
2402                 }
2403               sizeof_pointer_memaccess_warning
2404                 (sizeof_arg_loc, fn, *args,
2405                  sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2406             }
2407
2408           /* A call to a namespace-scope function.  */
2409           result = build_new_function_call (fn, args, koenig_p, complain);
2410         }
2411     }
2412   else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2413     {
2414       if (!vec_safe_is_empty (*args))
2415         error ("arguments to destructor are not allowed");
2416       /* Mark the pseudo-destructor call as having side-effects so
2417          that we do not issue warnings about its use.  */
2418       result = build1 (NOP_EXPR,
2419                        void_type_node,
2420                        TREE_OPERAND (fn, 0));
2421       TREE_SIDE_EFFECTS (result) = 1;
2422     }
2423   else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2424     /* If the "function" is really an object of class type, it might
2425        have an overloaded `operator ()'.  */
2426     result = build_op_call (fn, args, complain);
2427
2428   if (!result)
2429     /* A call where the function is unknown.  */
2430     result = cp_build_function_call_vec (fn, args, complain);
2431
2432   if (processing_template_decl && result != error_mark_node)
2433     {
2434       if (INDIRECT_REF_P (result))
2435         result = TREE_OPERAND (result, 0);
2436       result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2437       SET_EXPR_LOCATION (result, input_location);
2438       KOENIG_LOOKUP_P (result) = koenig_p;
2439       release_tree_vector (orig_args);
2440       result = convert_from_reference (result);
2441     }
2442
2443   if (koenig_p)
2444     {
2445       /* Free garbage OVERLOADs from arg-dependent lookup.  */
2446       tree next = NULL_TREE;
2447       for (fn = orig_fn;
2448            fn && TREE_CODE (fn) == OVERLOAD && OVL_ARG_DEPENDENT (fn);
2449            fn = next)
2450         {
2451           if (processing_template_decl)
2452             /* In a template, we'll re-use them at instantiation time.  */
2453             OVL_ARG_DEPENDENT (fn) = false;
2454           else
2455             {
2456               next = OVL_CHAIN (fn);
2457               ggc_free (fn);
2458             }
2459         }
2460     }
2461
2462   return result;
2463 }
2464
2465 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
2466    is indicated by CODE, which should be POSTINCREMENT_EXPR or
2467    POSTDECREMENT_EXPR.)  */
2468
2469 tree
2470 finish_increment_expr (tree expr, enum tree_code code)
2471 {
2472   return build_x_unary_op (input_location, code, expr, tf_warning_or_error);
2473 }
2474
2475 /* Finish a use of `this'.  Returns an expression for `this'.  */
2476
2477 tree
2478 finish_this_expr (void)
2479 {
2480   tree result = NULL_TREE;
2481
2482   if (current_class_ptr)
2483     {
2484       tree type = TREE_TYPE (current_class_ref);
2485
2486       /* In a lambda expression, 'this' refers to the captured 'this'.  */
2487       if (LAMBDA_TYPE_P (type))
2488         result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
2489       else
2490         result = current_class_ptr;
2491     }
2492
2493   if (result)
2494     /* The keyword 'this' is a prvalue expression.  */
2495     return rvalue (result);
2496
2497   tree fn = current_nonlambda_function ();
2498   if (fn && DECL_STATIC_FUNCTION_P (fn))
2499     error ("%<this%> is unavailable for static member functions");
2500   else if (fn)
2501     error ("invalid use of %<this%> in non-member function");
2502   else
2503     error ("invalid use of %<this%> at top level");
2504   return error_mark_node;
2505 }
2506
2507 /* Finish a pseudo-destructor expression.  If SCOPE is NULL, the
2508    expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
2509    the TYPE for the type given.  If SCOPE is non-NULL, the expression
2510    was of the form `OBJECT.SCOPE::~DESTRUCTOR'.  */
2511
2512 tree
2513 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
2514                                location_t loc)
2515 {
2516   if (object == error_mark_node || destructor == error_mark_node)
2517     return error_mark_node;
2518
2519   gcc_assert (TYPE_P (destructor));
2520
2521   if (!processing_template_decl)
2522     {
2523       if (scope == error_mark_node)
2524         {
2525           error_at (loc, "invalid qualifying scope in pseudo-destructor name");
2526           return error_mark_node;
2527         }
2528       if (is_auto (destructor))
2529         destructor = TREE_TYPE (object);
2530       if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
2531         {
2532           error_at (loc,
2533                     "qualified type %qT does not match destructor name ~%qT",
2534                     scope, destructor);
2535           return error_mark_node;
2536         }
2537
2538
2539       /* [expr.pseudo] says both:
2540
2541            The type designated by the pseudo-destructor-name shall be
2542            the same as the object type.
2543
2544          and:
2545
2546            The cv-unqualified versions of the object type and of the
2547            type designated by the pseudo-destructor-name shall be the
2548            same type.
2549
2550          We implement the more generous second sentence, since that is
2551          what most other compilers do.  */
2552       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
2553                                                       destructor))
2554         {
2555           error_at (loc, "%qE is not of type %qT", object, destructor);
2556           return error_mark_node;
2557         }
2558     }
2559
2560   return build3_loc (loc, PSEUDO_DTOR_EXPR, void_type_node, object,
2561                      scope, destructor);
2562 }
2563
2564 /* Finish an expression of the form CODE EXPR.  */
2565
2566 tree
2567 finish_unary_op_expr (location_t loc, enum tree_code code, tree expr,
2568                       tsubst_flags_t complain)
2569 {
2570   tree result = build_x_unary_op (loc, code, expr, complain);
2571   if ((complain & tf_warning)
2572       && TREE_OVERFLOW_P (result) && !TREE_OVERFLOW_P (expr))
2573     overflow_warning (input_location, result);
2574
2575   return result;
2576 }
2577
2578 /* Finish a compound-literal expression.  TYPE is the type to which
2579    the CONSTRUCTOR in COMPOUND_LITERAL is being cast.  */
2580
2581 tree
2582 finish_compound_literal (tree type, tree compound_literal,
2583                          tsubst_flags_t complain)
2584 {
2585   if (type == error_mark_node)
2586     return error_mark_node;
2587
2588   if (TREE_CODE (type) == REFERENCE_TYPE)
2589     {
2590       compound_literal
2591         = finish_compound_literal (TREE_TYPE (type), compound_literal,
2592                                    complain);
2593       return cp_build_c_cast (type, compound_literal, complain);
2594     }
2595
2596   if (!TYPE_OBJ_P (type))
2597     {
2598       if (complain & tf_error)
2599         error ("compound literal of non-object type %qT", type);
2600       return error_mark_node;
2601     }
2602
2603   if (processing_template_decl)
2604     {
2605       TREE_TYPE (compound_literal) = type;
2606       /* Mark the expression as a compound literal.  */
2607       TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
2608       return compound_literal;
2609     }
2610
2611   type = complete_type (type);
2612
2613   if (TYPE_NON_AGGREGATE_CLASS (type))
2614     {
2615       /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
2616          everywhere that deals with function arguments would be a pain, so
2617          just wrap it in a TREE_LIST.  The parser set a flag so we know
2618          that it came from T{} rather than T({}).  */
2619       CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
2620       compound_literal = build_tree_list (NULL_TREE, compound_literal);
2621       return build_functional_cast (type, compound_literal, complain);
2622     }
2623
2624   if (TREE_CODE (type) == ARRAY_TYPE
2625       && check_array_initializer (NULL_TREE, type, compound_literal))
2626     return error_mark_node;
2627   compound_literal = reshape_init (type, compound_literal, complain);
2628   if (SCALAR_TYPE_P (type)
2629       && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
2630       && !check_narrowing (type, compound_literal, complain))
2631     return error_mark_node;
2632   if (TREE_CODE (type) == ARRAY_TYPE
2633       && TYPE_DOMAIN (type) == NULL_TREE)
2634     {
2635       cp_complete_array_type_or_error (&type, compound_literal,
2636                                        false, complain);
2637       if (type == error_mark_node)
2638         return error_mark_node;
2639     }
2640   compound_literal = digest_init (type, compound_literal, complain);
2641   if (TREE_CODE (compound_literal) == CONSTRUCTOR)
2642     TREE_HAS_CONSTRUCTOR (compound_literal) = true;
2643   /* Put static/constant array temporaries in static variables, but always
2644      represent class temporaries with TARGET_EXPR so we elide copies.  */
2645   if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
2646       && TREE_CODE (type) == ARRAY_TYPE
2647       && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2648       && initializer_constant_valid_p (compound_literal, type))
2649     {
2650       tree decl = create_temporary_var (type);
2651       DECL_INITIAL (decl) = compound_literal;
2652       TREE_STATIC (decl) = 1;
2653       if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
2654         {
2655           /* 5.19 says that a constant expression can include an
2656              lvalue-rvalue conversion applied to "a glvalue of literal type
2657              that refers to a non-volatile temporary object initialized
2658              with a constant expression".  Rather than try to communicate
2659              that this VAR_DECL is a temporary, just mark it constexpr.  */
2660           DECL_DECLARED_CONSTEXPR_P (decl) = true;
2661           DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
2662           TREE_CONSTANT (decl) = true;
2663         }
2664       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
2665       decl = pushdecl_top_level (decl);
2666       DECL_NAME (decl) = make_anon_name ();
2667       SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
2668       /* Make sure the destructor is callable.  */
2669       tree clean = cxx_maybe_build_cleanup (decl, complain);
2670       if (clean == error_mark_node)
2671         return error_mark_node;
2672       return decl;
2673     }
2674   else
2675     return get_target_expr_sfinae (compound_literal, complain);
2676 }
2677
2678 /* Return the declaration for the function-name variable indicated by
2679    ID.  */
2680
2681 tree
2682 finish_fname (tree id)
2683 {
2684   tree decl;
2685
2686   decl = fname_decl (input_location, C_RID_CODE (id), id);
2687   if (processing_template_decl && current_function_decl
2688       && decl != error_mark_node)
2689     decl = DECL_NAME (decl);
2690   return decl;
2691 }
2692
2693 /* Finish a translation unit.  */
2694
2695 void
2696 finish_translation_unit (void)
2697 {
2698   /* In case there were missing closebraces,
2699      get us back to the global binding level.  */
2700   pop_everything ();
2701   while (current_namespace != global_namespace)
2702     pop_namespace ();
2703
2704   /* Do file scope __FUNCTION__ et al.  */
2705   finish_fname_decls ();
2706 }
2707
2708 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
2709    Returns the parameter.  */
2710
2711 tree
2712 finish_template_type_parm (tree aggr, tree identifier)
2713 {
2714   if (aggr != class_type_node)
2715     {
2716       permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
2717       aggr = class_type_node;
2718     }
2719
2720   return build_tree_list (aggr, identifier);
2721 }
2722
2723 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
2724    Returns the parameter.  */
2725
2726 tree
2727 finish_template_template_parm (tree aggr, tree identifier)
2728 {
2729   tree decl = build_decl (input_location,
2730                           TYPE_DECL, identifier, NULL_TREE);
2731   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
2732   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
2733   DECL_TEMPLATE_RESULT (tmpl) = decl;
2734   DECL_ARTIFICIAL (decl) = 1;
2735   end_template_decl ();
2736
2737   gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
2738
2739   check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl), 
2740                            /*is_primary=*/true, /*is_partial=*/false,
2741                            /*is_friend=*/0);
2742
2743   return finish_template_type_parm (aggr, tmpl);
2744 }
2745
2746 /* ARGUMENT is the default-argument value for a template template
2747    parameter.  If ARGUMENT is invalid, issue error messages and return
2748    the ERROR_MARK_NODE.  Otherwise, ARGUMENT itself is returned.  */
2749
2750 tree
2751 check_template_template_default_arg (tree argument)
2752 {
2753   if (TREE_CODE (argument) != TEMPLATE_DECL
2754       && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
2755       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
2756     {
2757       if (TREE_CODE (argument) == TYPE_DECL)
2758         error ("invalid use of type %qT as a default value for a template "
2759                "template-parameter", TREE_TYPE (argument));
2760       else
2761         error ("invalid default argument for a template template parameter");
2762       return error_mark_node;
2763     }
2764
2765   return argument;
2766 }
2767
2768 /* Begin a class definition, as indicated by T.  */
2769
2770 tree
2771 begin_class_definition (tree t)
2772 {
2773   if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
2774     return error_mark_node;
2775
2776   if (processing_template_parmlist)
2777     {
2778       error ("definition of %q#T inside template parameter list", t);
2779       return error_mark_node;
2780     }
2781
2782   /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
2783      are passed the same as decimal scalar types.  */
2784   if (TREE_CODE (t) == RECORD_TYPE
2785       && !processing_template_decl)
2786     {
2787       tree ns = TYPE_CONTEXT (t);
2788       if (ns && TREE_CODE (ns) == NAMESPACE_DECL
2789           && DECL_CONTEXT (ns) == std_node
2790           && DECL_NAME (ns)
2791           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (ns)), "decimal"))
2792         {
2793           const char *n = TYPE_NAME_STRING (t);
2794           if ((strcmp (n, "decimal32") == 0)
2795               || (strcmp (n, "decimal64") == 0)
2796               || (strcmp (n, "decimal128") == 0))
2797             TYPE_TRANSPARENT_AGGR (t) = 1;
2798         }
2799     }
2800
2801   /* A non-implicit typename comes from code like:
2802
2803        template <typename T> struct A {
2804          template <typename U> struct A<T>::B ...
2805
2806      This is erroneous.  */
2807   else if (TREE_CODE (t) == TYPENAME_TYPE)
2808     {
2809       error ("invalid definition of qualified type %qT", t);
2810       t = error_mark_node;
2811     }
2812
2813   if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
2814     {
2815       t = make_class_type (RECORD_TYPE);
2816       pushtag (make_anon_name (), t, /*tag_scope=*/ts_current);
2817     }
2818
2819   if (TYPE_BEING_DEFINED (t))
2820     {
2821       t = make_class_type (TREE_CODE (t));
2822       pushtag (TYPE_IDENTIFIER (t), t, /*tag_scope=*/ts_current);
2823     }
2824   maybe_process_partial_specialization (t);
2825   pushclass (t);
2826   TYPE_BEING_DEFINED (t) = 1;
2827   class_binding_level->defining_class_p = 1;
2828
2829   if (flag_pack_struct)
2830     {
2831       tree v;
2832       TYPE_PACKED (t) = 1;
2833       /* Even though the type is being defined for the first time
2834          here, there might have been a forward declaration, so there
2835          might be cv-qualified variants of T.  */
2836       for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2837         TYPE_PACKED (v) = 1;
2838     }
2839   /* Reset the interface data, at the earliest possible
2840      moment, as it might have been set via a class foo;
2841      before.  */
2842   if (! TYPE_ANONYMOUS_P (t))
2843     {
2844       struct c_fileinfo *finfo = \
2845         get_fileinfo (LOCATION_FILE (input_location));
2846       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
2847       SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2848         (t, finfo->interface_unknown);
2849     }
2850   reset_specialization();
2851
2852   /* Make a declaration for this class in its own scope.  */
2853   build_self_reference ();
2854
2855   return t;
2856 }
2857
2858 /* Finish the member declaration given by DECL.  */
2859
2860 void
2861 finish_member_declaration (tree decl)
2862 {
2863   if (decl == error_mark_node || decl == NULL_TREE)
2864     return;
2865
2866   if (decl == void_type_node)
2867     /* The COMPONENT was a friend, not a member, and so there's
2868        nothing for us to do.  */
2869     return;
2870
2871   /* We should see only one DECL at a time.  */
2872   gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
2873
2874   /* Set up access control for DECL.  */
2875   TREE_PRIVATE (decl)
2876     = (current_access_specifier == access_private_node);
2877   TREE_PROTECTED (decl)
2878     = (current_access_specifier == access_protected_node);
2879   if (TREE_CODE (decl) == TEMPLATE_DECL)
2880     {
2881       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
2882       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
2883     }
2884
2885   /* Mark the DECL as a member of the current class, unless it's
2886      a member of an enumeration.  */
2887   if (TREE_CODE (decl) != CONST_DECL)
2888     DECL_CONTEXT (decl) = current_class_type;
2889
2890   /* Check for bare parameter packs in the member variable declaration.  */
2891   if (TREE_CODE (decl) == FIELD_DECL)
2892     {
2893       if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
2894         TREE_TYPE (decl) = error_mark_node;
2895       if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
2896         DECL_ATTRIBUTES (decl) = NULL_TREE;
2897     }
2898
2899   /* [dcl.link]
2900
2901      A C language linkage is ignored for the names of class members
2902      and the member function type of class member functions.  */
2903   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
2904     SET_DECL_LANGUAGE (decl, lang_cplusplus);
2905
2906   /* Put functions on the TYPE_METHODS list and everything else on the
2907      TYPE_FIELDS list.  Note that these are built up in reverse order.
2908      We reverse them (to obtain declaration order) in finish_struct.  */
2909   if (DECL_DECLARES_FUNCTION_P (decl))
2910     {
2911       /* We also need to add this function to the
2912          CLASSTYPE_METHOD_VEC.  */
2913       if (add_method (current_class_type, decl, NULL_TREE))
2914         {
2915           DECL_CHAIN (decl) = TYPE_METHODS (current_class_type);
2916           TYPE_METHODS (current_class_type) = decl;
2917
2918           maybe_add_class_template_decl_list (current_class_type, decl,
2919                                               /*friend_p=*/0);
2920         }
2921     }
2922   /* Enter the DECL into the scope of the class, if the class
2923      isn't a closure (whose fields are supposed to be unnamed).  */
2924   else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
2925            || pushdecl_class_level (decl))
2926     {
2927       if (TREE_CODE (decl) == USING_DECL)
2928         {
2929           /* For now, ignore class-scope USING_DECLS, so that
2930              debugging backends do not see them. */
2931           DECL_IGNORED_P (decl) = 1;
2932         }
2933
2934       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
2935          go at the beginning.  The reason is that lookup_field_1
2936          searches the list in order, and we want a field name to
2937          override a type name so that the "struct stat hack" will
2938          work.  In particular:
2939
2940            struct S { enum E { }; int E } s;
2941            s.E = 3;
2942
2943          is valid.  In addition, the FIELD_DECLs must be maintained in
2944          declaration order so that class layout works as expected.
2945          However, we don't need that order until class layout, so we
2946          save a little time by putting FIELD_DECLs on in reverse order
2947          here, and then reversing them in finish_struct_1.  (We could
2948          also keep a pointer to the correct insertion points in the
2949          list.)  */
2950
2951       if (TREE_CODE (decl) == TYPE_DECL)
2952         TYPE_FIELDS (current_class_type)
2953           = chainon (TYPE_FIELDS (current_class_type), decl);
2954       else
2955         {
2956           DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
2957           TYPE_FIELDS (current_class_type) = decl;
2958         }
2959
2960       maybe_add_class_template_decl_list (current_class_type, decl,
2961                                           /*friend_p=*/0);
2962     }
2963
2964   if (pch_file)
2965     note_decl_for_pch (decl);
2966 }
2967
2968 /* DECL has been declared while we are building a PCH file.  Perform
2969    actions that we might normally undertake lazily, but which can be
2970    performed now so that they do not have to be performed in
2971    translation units which include the PCH file.  */
2972
2973 void
2974 note_decl_for_pch (tree decl)
2975 {
2976   gcc_assert (pch_file);
2977
2978   /* There's a good chance that we'll have to mangle names at some
2979      point, even if only for emission in debugging information.  */
2980   if (VAR_OR_FUNCTION_DECL_P (decl)
2981       && !processing_template_decl)
2982     mangle_decl (decl);
2983 }
2984
2985 /* Finish processing a complete template declaration.  The PARMS are
2986    the template parameters.  */
2987
2988 void
2989 finish_template_decl (tree parms)
2990 {
2991   if (parms)
2992     end_template_decl ();
2993   else
2994     end_specialization ();
2995 }
2996
2997 /* Finish processing a template-id (which names a type) of the form
2998    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
2999    template-id.  If ENTERING_SCOPE is nonzero we are about to enter
3000    the scope of template-id indicated.  */
3001
3002 tree
3003 finish_template_type (tree name, tree args, int entering_scope)
3004 {
3005   tree type;
3006
3007   type = lookup_template_class (name, args,
3008                                 NULL_TREE, NULL_TREE, entering_scope,
3009                                 tf_warning_or_error | tf_user);
3010   if (type == error_mark_node)
3011     return type;
3012   else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3013     return TYPE_STUB_DECL (type);
3014   else
3015     return TYPE_NAME (type);
3016 }
3017
3018 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3019    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3020    BASE_CLASS, or NULL_TREE if an error occurred.  The
3021    ACCESS_SPECIFIER is one of
3022    access_{default,public,protected_private}_node.  For a virtual base
3023    we set TREE_TYPE.  */
3024
3025 tree
3026 finish_base_specifier (tree base, tree access, bool virtual_p)
3027 {
3028   tree result;
3029
3030   if (base == error_mark_node)
3031     {
3032       error ("invalid base-class specification");
3033       result = NULL_TREE;
3034     }
3035   else if (! MAYBE_CLASS_TYPE_P (base))
3036     {
3037       error ("%qT is not a class type", base);
3038       result = NULL_TREE;
3039     }
3040   else
3041     {
3042       if (cp_type_quals (base) != 0)
3043         {
3044           /* DR 484: Can a base-specifier name a cv-qualified
3045              class type?  */
3046           base = TYPE_MAIN_VARIANT (base);
3047         }
3048       result = build_tree_list (access, base);
3049       if (virtual_p)
3050         TREE_TYPE (result) = integer_type_node;
3051     }
3052
3053   return result;
3054 }
3055
3056 /* If FNS is a member function, a set of member functions, or a
3057    template-id referring to one or more member functions, return a
3058    BASELINK for FNS, incorporating the current access context.
3059    Otherwise, return FNS unchanged.  */
3060
3061 tree
3062 baselink_for_fns (tree fns)
3063 {
3064   tree scope;
3065   tree cl;
3066
3067   if (BASELINK_P (fns) 
3068       || error_operand_p (fns))
3069     return fns;
3070
3071   scope = ovl_scope (fns);
3072   if (!CLASS_TYPE_P (scope))
3073     return fns;
3074
3075   cl = currently_open_derived_class (scope);
3076   if (!cl)
3077     cl = scope;
3078   cl = TYPE_BINFO (cl);
3079   return build_baselink (cl, cl, fns, /*optype=*/NULL_TREE);
3080 }
3081
3082 /* Returns true iff DECL is a variable from a function outside
3083    the current one.  */
3084
3085 static bool
3086 outer_var_p (tree decl)
3087 {
3088   return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3089           && DECL_FUNCTION_SCOPE_P (decl)
3090           && (DECL_CONTEXT (decl) != current_function_decl
3091               || parsing_nsdmi ()));
3092 }
3093
3094 /* As above, but also checks that DECL is automatic.  */
3095
3096 bool
3097 outer_automatic_var_p (tree decl)
3098 {
3099   return (outer_var_p (decl)
3100           && !TREE_STATIC (decl));
3101 }
3102
3103 /* DECL satisfies outer_automatic_var_p.  Possibly complain about it or
3104    rewrite it for lambda capture.  */
3105
3106 tree
3107 process_outer_var_ref (tree decl, tsubst_flags_t complain)
3108 {
3109   if (cp_unevaluated_operand)
3110     /* It's not a use (3.2) if we're in an unevaluated context.  */
3111     return decl;
3112   if (decl == error_mark_node)
3113     return decl;
3114
3115   tree context = DECL_CONTEXT (decl);
3116   tree containing_function = current_function_decl;
3117   tree lambda_stack = NULL_TREE;
3118   tree lambda_expr = NULL_TREE;
3119   tree initializer = convert_from_reference (decl);
3120
3121   /* Mark it as used now even if the use is ill-formed.  */
3122   mark_used (decl);
3123
3124   /* Core issue 696: "[At the July 2009 meeting] the CWG expressed
3125      support for an approach in which a reference to a local
3126      [constant] automatic variable in a nested class or lambda body
3127      would enter the expression as an rvalue, which would reduce
3128      the complexity of the problem"
3129
3130      FIXME update for final resolution of core issue 696.  */
3131   if (decl_maybe_constant_var_p (decl))
3132     {
3133       if (processing_template_decl)
3134         /* In a template, the constant value may not be in a usable
3135            form, so wait until instantiation time.  */
3136         return decl;
3137       else if (decl_constant_var_p (decl))
3138         {
3139           tree t = maybe_constant_value (convert_from_reference (decl));
3140           if (TREE_CONSTANT (t))
3141             return t;
3142         }
3143     }
3144
3145   if (parsing_nsdmi ())
3146     containing_function = NULL_TREE;
3147   else
3148     /* If we are in a lambda function, we can move out until we hit
3149        1. the context,
3150        2. a non-lambda function, or
3151        3. a non-default capturing lambda function.  */
3152     while (context != containing_function
3153            && LAMBDA_FUNCTION_P (containing_function))
3154       {
3155         tree closure = DECL_CONTEXT (containing_function);
3156         lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3157
3158         if (TYPE_CLASS_SCOPE_P (closure))
3159           /* A lambda in an NSDMI (c++/64496).  */
3160           break;
3161
3162         if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3163             == CPLD_NONE)
3164           break;
3165
3166         lambda_stack = tree_cons (NULL_TREE,
3167                                   lambda_expr,
3168                                   lambda_stack);
3169
3170         containing_function
3171           = decl_function_context (containing_function);
3172       }
3173
3174   if (lambda_expr && TREE_CODE (decl) == VAR_DECL
3175       && DECL_ANON_UNION_VAR_P (decl))
3176     {
3177       if (complain & tf_error)
3178         error ("cannot capture member %qD of anonymous union", decl);
3179       return error_mark_node;
3180     }
3181   if (context == containing_function)
3182     {
3183       decl = add_default_capture (lambda_stack,
3184                                   /*id=*/DECL_NAME (decl),
3185                                   initializer);
3186     }
3187   else if (lambda_expr)
3188     {
3189       if (complain & tf_error)
3190         {
3191           error ("%qD is not captured", decl);
3192           tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
3193           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr)
3194               == CPLD_NONE)
3195             inform (location_of (closure),
3196                     "the lambda has no capture-default");
3197           else if (TYPE_CLASS_SCOPE_P (closure))
3198             inform (0, "lambda in local class %q+T cannot "
3199                     "capture variables from the enclosing context",
3200                     TYPE_CONTEXT (closure));
3201           inform (input_location, "%q+#D declared here", decl);
3202         }
3203       return error_mark_node;
3204     }
3205   else
3206     {
3207       if (complain & tf_error)
3208         error (VAR_P (decl)
3209                ? G_("use of local variable with automatic storage from containing function")
3210                : G_("use of parameter from containing function"));
3211       inform (input_location, "%q+#D declared here", decl);
3212       return error_mark_node;
3213     }
3214   return decl;
3215 }
3216
3217 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
3218    id-expression.  (See cp_parser_id_expression for details.)  SCOPE,
3219    if non-NULL, is the type or namespace used to explicitly qualify
3220    ID_EXPRESSION.  DECL is the entity to which that name has been
3221    resolved.
3222
3223    *CONSTANT_EXPRESSION_P is true if we are presently parsing a
3224    constant-expression.  In that case, *NON_CONSTANT_EXPRESSION_P will
3225    be set to true if this expression isn't permitted in a
3226    constant-expression, but it is otherwise not set by this function.
3227    *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
3228    constant-expression, but a non-constant expression is also
3229    permissible.
3230
3231    DONE is true if this expression is a complete postfix-expression;
3232    it is false if this expression is followed by '->', '[', '(', etc.
3233    ADDRESS_P is true iff this expression is the operand of '&'.
3234    TEMPLATE_P is true iff the qualified-id was of the form
3235    "A::template B".  TEMPLATE_ARG_P is true iff this qualified name
3236    appears as a template argument.
3237
3238    If an error occurs, and it is the kind of error that might cause
3239    the parser to abort a tentative parse, *ERROR_MSG is filled in.  It
3240    is the caller's responsibility to issue the message.  *ERROR_MSG
3241    will be a string with static storage duration, so the caller need
3242    not "free" it.
3243
3244    Return an expression for the entity, after issuing appropriate
3245    diagnostics.  This function is also responsible for transforming a
3246    reference to a non-static member into a COMPONENT_REF that makes
3247    the use of "this" explicit.
3248
3249    Upon return, *IDK will be filled in appropriately.  */
3250 tree
3251 finish_id_expression (tree id_expression,
3252                       tree decl,
3253                       tree scope,
3254                       cp_id_kind *idk,
3255                       bool integral_constant_expression_p,
3256                       bool allow_non_integral_constant_expression_p,
3257                       bool *non_integral_constant_expression_p,
3258                       bool template_p,
3259                       bool done,
3260                       bool address_p,
3261                       bool template_arg_p,
3262                       const char **error_msg,
3263                       location_t location)
3264 {
3265   decl = strip_using_decl (decl);
3266
3267   /* Initialize the output parameters.  */
3268   *idk = CP_ID_KIND_NONE;
3269   *error_msg = NULL;
3270
3271   if (id_expression == error_mark_node)
3272     return error_mark_node;
3273   /* If we have a template-id, then no further lookup is
3274      required.  If the template-id was for a template-class, we
3275      will sometimes have a TYPE_DECL at this point.  */
3276   else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3277            || TREE_CODE (decl) == TYPE_DECL)
3278     ;
3279   /* Look up the name.  */
3280   else
3281     {
3282       if (decl == error_mark_node)
3283         {
3284           /* Name lookup failed.  */
3285           if (scope
3286               && (!TYPE_P (scope)
3287                   || (!dependent_type_p (scope)
3288                       && !(identifier_p (id_expression)
3289                            && IDENTIFIER_TYPENAME_P (id_expression)
3290                            && dependent_type_p (TREE_TYPE (id_expression))))))
3291             {
3292               /* If the qualifying type is non-dependent (and the name
3293                  does not name a conversion operator to a dependent
3294                  type), issue an error.  */
3295               qualified_name_lookup_error (scope, id_expression, decl, location);
3296               return error_mark_node;
3297             }
3298           else if (!scope)
3299             {
3300               /* It may be resolved via Koenig lookup.  */
3301               *idk = CP_ID_KIND_UNQUALIFIED;
3302               return id_expression;
3303             }
3304           else
3305             decl = id_expression;
3306         }
3307       /* If DECL is a variable that would be out of scope under
3308          ANSI/ISO rules, but in scope in the ARM, name lookup
3309          will succeed.  Issue a diagnostic here.  */
3310       else
3311         decl = check_for_out_of_scope_variable (decl);
3312
3313       /* Remember that the name was used in the definition of
3314          the current class so that we can check later to see if
3315          the meaning would have been different after the class
3316          was entirely defined.  */
3317       if (!scope && decl != error_mark_node && identifier_p (id_expression))
3318         maybe_note_name_used_in_class (id_expression, decl);
3319
3320       /* Disallow uses of local variables from containing functions, except
3321          within lambda-expressions.  */
3322       if (outer_automatic_var_p (decl))
3323         {
3324           decl = process_outer_var_ref (decl, tf_warning_or_error);
3325           if (decl == error_mark_node)
3326             return error_mark_node;
3327         }
3328
3329       /* Also disallow uses of function parameters outside the function
3330          body, except inside an unevaluated context (i.e. decltype).  */
3331       if (TREE_CODE (decl) == PARM_DECL
3332           && DECL_CONTEXT (decl) == NULL_TREE
3333           && !cp_unevaluated_operand)
3334         {
3335           *error_msg = "use of parameter outside function body";
3336           return error_mark_node;
3337         }
3338     }
3339
3340   /* If we didn't find anything, or what we found was a type,
3341      then this wasn't really an id-expression.  */
3342   if (TREE_CODE (decl) == TEMPLATE_DECL
3343       && !DECL_FUNCTION_TEMPLATE_P (decl))
3344     {
3345       *error_msg = "missing template arguments";
3346       return error_mark_node;
3347     }
3348   else if (TREE_CODE (decl) == TYPE_DECL
3349            || TREE_CODE (decl) == NAMESPACE_DECL)
3350     {
3351       *error_msg = "expected primary-expression";
3352       return error_mark_node;
3353     }
3354
3355   /* If the name resolved to a template parameter, there is no
3356      need to look it up again later.  */
3357   if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
3358       || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3359     {
3360       tree r;
3361
3362       *idk = CP_ID_KIND_NONE;
3363       if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
3364         decl = TEMPLATE_PARM_DECL (decl);
3365       r = convert_from_reference (DECL_INITIAL (decl));
3366
3367       if (integral_constant_expression_p
3368           && !dependent_type_p (TREE_TYPE (decl))
3369           && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
3370         {
3371           if (!allow_non_integral_constant_expression_p)
3372             error ("template parameter %qD of type %qT is not allowed in "
3373                    "an integral constant expression because it is not of "
3374                    "integral or enumeration type", decl, TREE_TYPE (decl));
3375           *non_integral_constant_expression_p = true;
3376         }
3377       return r;
3378     }
3379   else
3380     {
3381       bool dependent_p;
3382
3383       /* If the declaration was explicitly qualified indicate
3384          that.  The semantics of `A::f(3)' are different than
3385          `f(3)' if `f' is virtual.  */
3386       *idk = (scope
3387               ? CP_ID_KIND_QUALIFIED
3388               : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3389                  ? CP_ID_KIND_TEMPLATE_ID
3390                  : CP_ID_KIND_UNQUALIFIED));
3391
3392
3393       /* [temp.dep.expr]
3394
3395          An id-expression is type-dependent if it contains an
3396          identifier that was declared with a dependent type.
3397
3398          The standard is not very specific about an id-expression that
3399          names a set of overloaded functions.  What if some of them
3400          have dependent types and some of them do not?  Presumably,
3401          such a name should be treated as a dependent name.  */
3402       /* Assume the name is not dependent.  */
3403       dependent_p = false;
3404       if (!processing_template_decl)
3405         /* No names are dependent outside a template.  */
3406         ;
3407       else if (TREE_CODE (decl) == CONST_DECL)
3408         /* We don't want to treat enumerators as dependent.  */
3409         ;
3410       /* A template-id where the name of the template was not resolved
3411          is definitely dependent.  */
3412       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3413                && (identifier_p (TREE_OPERAND (decl, 0))))
3414         dependent_p = true;
3415       /* For anything except an overloaded function, just check its
3416          type.  */
3417       else if (!is_overloaded_fn (decl))
3418         dependent_p
3419           = dependent_type_p (TREE_TYPE (decl));
3420       /* For a set of overloaded functions, check each of the
3421          functions.  */
3422       else
3423         {
3424           tree fns = decl;
3425
3426           if (BASELINK_P (fns))
3427             fns = BASELINK_FUNCTIONS (fns);
3428
3429           /* For a template-id, check to see if the template
3430              arguments are dependent.  */
3431           if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3432             {
3433               tree args = TREE_OPERAND (fns, 1);
3434               dependent_p = any_dependent_template_arguments_p (args);
3435               /* The functions are those referred to by the
3436                  template-id.  */
3437               fns = TREE_OPERAND (fns, 0);
3438             }
3439
3440           /* If there are no dependent template arguments, go through
3441              the overloaded functions.  */
3442           while (fns && !dependent_p)
3443             {
3444               tree fn = OVL_CURRENT (fns);
3445
3446               /* Member functions of dependent classes are
3447                  dependent.  */
3448               if (TREE_CODE (fn) == FUNCTION_DECL
3449                   && type_dependent_expression_p (fn))
3450                 dependent_p = true;
3451               else if (TREE_CODE (fn) == TEMPLATE_DECL
3452                        && dependent_template_p (fn))
3453                 dependent_p = true;
3454
3455               fns = OVL_NEXT (fns);
3456             }
3457         }
3458
3459       /* If the name was dependent on a template parameter, we will
3460          resolve the name at instantiation time.  */
3461       if (dependent_p)
3462         {
3463           /* Create a SCOPE_REF for qualified names, if the scope is
3464              dependent.  */
3465           if (scope)
3466             {
3467               if (TYPE_P (scope))
3468                 {
3469                   if (address_p && done)
3470                     decl = finish_qualified_id_expr (scope, decl,
3471                                                      done, address_p,
3472                                                      template_p,
3473                                                      template_arg_p,
3474                                                      tf_warning_or_error);
3475                   else
3476                     {
3477                       tree type = NULL_TREE;
3478                       if (DECL_P (decl) && !dependent_scope_p (scope))
3479                         type = TREE_TYPE (decl);
3480                       decl = build_qualified_name (type,
3481                                                    scope,
3482                                                    id_expression,
3483                                                    template_p);
3484                     }
3485                 }
3486               if (TREE_TYPE (decl))
3487                 decl = convert_from_reference (decl);
3488               return decl;
3489             }
3490           /* A TEMPLATE_ID already contains all the information we
3491              need.  */
3492           if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3493             return id_expression;
3494           *idk = CP_ID_KIND_UNQUALIFIED_DEPENDENT;
3495           /* If we found a variable, then name lookup during the
3496              instantiation will always resolve to the same VAR_DECL
3497              (or an instantiation thereof).  */
3498           if (VAR_P (decl)
3499               || TREE_CODE (decl) == PARM_DECL)
3500             {
3501               mark_used (decl);
3502               return convert_from_reference (decl);
3503             }
3504           /* The same is true for FIELD_DECL, but we also need to
3505              make sure that the syntax is correct.  */
3506           else if (TREE_CODE (decl) == FIELD_DECL)
3507             {
3508               /* Since SCOPE is NULL here, this is an unqualified name.
3509                  Access checking has been performed during name lookup
3510                  already.  Turn off checking to avoid duplicate errors.  */
3511               push_deferring_access_checks (dk_no_check);
3512               decl = finish_non_static_data_member
3513                        (decl, NULL_TREE,
3514                         /*qualifying_scope=*/NULL_TREE);
3515               pop_deferring_access_checks ();
3516               return decl;
3517             }
3518           return id_expression;
3519         }
3520
3521       if (TREE_CODE (decl) == NAMESPACE_DECL)
3522         {
3523           error ("use of namespace %qD as expression", decl);
3524           return error_mark_node;
3525         }
3526       else if (DECL_CLASS_TEMPLATE_P (decl))
3527         {
3528           error ("use of class template %qT as expression", decl);
3529           return error_mark_node;
3530         }
3531       else if (TREE_CODE (decl) == TREE_LIST)
3532         {
3533           /* Ambiguous reference to base members.  */
3534           error ("request for member %qD is ambiguous in "
3535                  "multiple inheritance lattice", id_expression);
3536           print_candidates (decl);
3537           return error_mark_node;
3538         }
3539
3540       /* Mark variable-like entities as used.  Functions are similarly
3541          marked either below or after overload resolution.  */
3542       if ((VAR_P (decl)
3543            || TREE_CODE (decl) == PARM_DECL
3544            || TREE_CODE (decl) == CONST_DECL
3545            || TREE_CODE (decl) == RESULT_DECL)
3546           && !mark_used (decl))
3547         return error_mark_node;
3548
3549       /* Only certain kinds of names are allowed in constant
3550          expression.  Template parameters have already
3551          been handled above.  */
3552       if (! error_operand_p (decl)
3553           && integral_constant_expression_p
3554           && ! decl_constant_var_p (decl)
3555           && TREE_CODE (decl) != CONST_DECL
3556           && ! builtin_valid_in_constant_expr_p (decl))
3557         {
3558           if (!allow_non_integral_constant_expression_p)
3559             {
3560               error ("%qD cannot appear in a constant-expression", decl);
3561               return error_mark_node;
3562             }
3563           *non_integral_constant_expression_p = true;
3564         }
3565
3566       tree wrap;
3567       if (VAR_P (decl)
3568           && !cp_unevaluated_operand
3569           && !processing_template_decl
3570           && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3571           && DECL_THREAD_LOCAL_P (decl)
3572           && (wrap = get_tls_wrapper_fn (decl)))
3573         {
3574           /* Replace an evaluated use of the thread_local variable with
3575              a call to its wrapper.  */
3576           decl = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
3577         }
3578       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
3579                && variable_template_p (TREE_OPERAND (decl, 0)))
3580         {
3581           decl = finish_template_variable (decl);
3582           mark_used (decl);
3583           decl = convert_from_reference (decl);
3584         }
3585       else if (scope)
3586         {
3587           decl = (adjust_result_of_qualified_name_lookup
3588                   (decl, scope, current_nonlambda_class_type()));
3589
3590           if (TREE_CODE (decl) == FUNCTION_DECL)
3591             mark_used (decl);
3592
3593           if (TYPE_P (scope))
3594             decl = finish_qualified_id_expr (scope,
3595                                              decl,
3596                                              done,
3597                                              address_p,
3598                                              template_p,
3599                                              template_arg_p,
3600                                              tf_warning_or_error);
3601           else
3602             decl = convert_from_reference (decl);
3603         }
3604       else if (TREE_CODE (decl) == FIELD_DECL)
3605         {
3606           /* Since SCOPE is NULL here, this is an unqualified name.
3607              Access checking has been performed during name lookup
3608              already.  Turn off checking to avoid duplicate errors.  */
3609           push_deferring_access_checks (dk_no_check);
3610           decl = finish_non_static_data_member (decl, NULL_TREE,
3611                                                 /*qualifying_scope=*/NULL_TREE);
3612           pop_deferring_access_checks ();
3613         }
3614       else if (is_overloaded_fn (decl))
3615         {
3616           tree first_fn;
3617
3618           first_fn = get_first_fn (decl);
3619           if (TREE_CODE (first_fn) == TEMPLATE_DECL)
3620             first_fn = DECL_TEMPLATE_RESULT (first_fn);
3621
3622           if (!really_overloaded_fn (decl)
3623               && !mark_used (first_fn))
3624             return error_mark_node;
3625
3626           if (!template_arg_p
3627               && TREE_CODE (first_fn) == FUNCTION_DECL
3628               && DECL_FUNCTION_MEMBER_P (first_fn)
3629               && !shared_member_p (decl))
3630             {
3631               /* A set of member functions.  */
3632               decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
3633               return finish_class_member_access_expr (decl, id_expression,
3634                                                       /*template_p=*/false,
3635                                                       tf_warning_or_error);
3636             }
3637
3638           decl = baselink_for_fns (decl);
3639         }
3640       else
3641         {
3642           if (DECL_P (decl) && DECL_NONLOCAL (decl)
3643               && DECL_CLASS_SCOPE_P (decl))
3644             {
3645               tree context = context_for_name_lookup (decl); 
3646               if (context != current_class_type)
3647                 {
3648                   tree path = currently_open_derived_class (context);
3649                   perform_or_defer_access_check (TYPE_BINFO (path),
3650                                                  decl, decl,
3651                                                  tf_warning_or_error);
3652                 }
3653             }
3654
3655           decl = convert_from_reference (decl);
3656         }
3657     }
3658
3659   /* Handle references (c++/56130).  */
3660   tree t = REFERENCE_REF_P (decl) ? TREE_OPERAND (decl, 0) : decl;
3661   if (TREE_DEPRECATED (t))
3662     warn_deprecated_use (t, NULL_TREE);
3663
3664   return decl;
3665 }
3666
3667 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
3668    use as a type-specifier.  */
3669
3670 tree
3671 finish_typeof (tree expr)
3672 {
3673   tree type;
3674
3675   if (type_dependent_expression_p (expr))
3676     {
3677       type = cxx_make_type (TYPEOF_TYPE);
3678       TYPEOF_TYPE_EXPR (type) = expr;
3679       SET_TYPE_STRUCTURAL_EQUALITY (type);
3680
3681       return type;
3682     }
3683
3684   expr = mark_type_use (expr);
3685
3686   type = unlowered_expr_type (expr);
3687
3688   if (!type || type == unknown_type_node)
3689     {
3690       error ("type of %qE is unknown", expr);
3691       return error_mark_node;
3692     }
3693
3694   return type;
3695 }
3696
3697 /* Implement the __underlying_type keyword: Return the underlying
3698    type of TYPE, suitable for use as a type-specifier.  */
3699
3700 tree
3701 finish_underlying_type (tree type)
3702 {
3703   tree underlying_type;
3704
3705   if (processing_template_decl)
3706     {
3707       underlying_type = cxx_make_type (UNDERLYING_TYPE);
3708       UNDERLYING_TYPE_TYPE (underlying_type) = type;
3709       SET_TYPE_STRUCTURAL_EQUALITY (underlying_type);
3710
3711       return underlying_type;
3712     }
3713
3714   complete_type (type);
3715
3716   if (TREE_CODE (type) != ENUMERAL_TYPE)
3717     {
3718       error ("%qT is not an enumeration type", type);
3719       return error_mark_node;
3720     }
3721
3722   underlying_type = ENUM_UNDERLYING_TYPE (type);
3723
3724   /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
3725      includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
3726      See finish_enum_value_list for details.  */
3727   if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
3728     underlying_type
3729       = c_common_type_for_mode (TYPE_MODE (underlying_type),
3730                                 TYPE_UNSIGNED (underlying_type));
3731
3732   return underlying_type;
3733 }
3734
3735 /* Implement the __direct_bases keyword: Return the direct base classes
3736    of type */
3737
3738 tree
3739 calculate_direct_bases (tree type)
3740 {
3741   vec<tree, va_gc> *vector = make_tree_vector();
3742   tree bases_vec = NULL_TREE;
3743   vec<tree, va_gc> *base_binfos;
3744   tree binfo;
3745   unsigned i;
3746
3747   complete_type (type);
3748
3749   if (!NON_UNION_CLASS_TYPE_P (type))
3750     return make_tree_vec (0);
3751
3752   base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
3753
3754   /* Virtual bases are initialized first */
3755   for (i = 0; base_binfos->iterate (i, &binfo); i++)
3756     {
3757       if (BINFO_VIRTUAL_P (binfo))
3758        {
3759          vec_safe_push (vector, binfo);
3760        }
3761     }
3762
3763   /* Now non-virtuals */
3764   for (i = 0; base_binfos->iterate (i, &binfo); i++)
3765     {
3766       if (!BINFO_VIRTUAL_P (binfo))
3767        {
3768          vec_safe_push (vector, binfo);
3769        }
3770     }
3771
3772
3773   bases_vec = make_tree_vec (vector->length ());
3774
3775   for (i = 0; i < vector->length (); ++i)
3776     {
3777       TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
3778     }
3779   return bases_vec;
3780 }
3781
3782 /* Implement the __bases keyword: Return the base classes
3783    of type */
3784
3785 /* Find morally non-virtual base classes by walking binfo hierarchy */
3786 /* Virtual base classes are handled separately in finish_bases */
3787
3788 static tree
3789 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
3790 {
3791   /* Don't walk bases of virtual bases */
3792   return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
3793 }
3794
3795 static tree
3796 dfs_calculate_bases_post (tree binfo, void *data_)
3797 {
3798   vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
3799   if (!BINFO_VIRTUAL_P (binfo))
3800     {
3801       vec_safe_push (*data, BINFO_TYPE (binfo));
3802     }
3803   return NULL_TREE;
3804 }
3805
3806 /* Calculates the morally non-virtual base classes of a class */
3807 static vec<tree, va_gc> *
3808 calculate_bases_helper (tree type)
3809 {
3810   vec<tree, va_gc> *vector = make_tree_vector();
3811
3812   /* Now add non-virtual base classes in order of construction */
3813   dfs_walk_all (TYPE_BINFO (type),
3814                 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
3815   return vector;
3816 }
3817
3818 tree
3819 calculate_bases (tree type)
3820 {
3821   vec<tree, va_gc> *vector = make_tree_vector();
3822   tree bases_vec = NULL_TREE;
3823   unsigned i;
3824   vec<tree, va_gc> *vbases;
3825   vec<tree, va_gc> *nonvbases;
3826   tree binfo;
3827
3828   complete_type (type);
3829
3830   if (!NON_UNION_CLASS_TYPE_P (type))
3831     return make_tree_vec (0);
3832
3833   /* First go through virtual base classes */
3834   for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
3835        vec_safe_iterate (vbases, i, &binfo); i++)
3836     {
3837       vec<tree, va_gc> *vbase_bases;
3838       vbase_bases = calculate_bases_helper (BINFO_TYPE (binfo));
3839       vec_safe_splice (vector, vbase_bases);
3840       release_tree_vector (vbase_bases);
3841     }
3842
3843   /* Now for the non-virtual bases */
3844   nonvbases = calculate_bases_helper (type);
3845   vec_safe_splice (vector, nonvbases);
3846   release_tree_vector (nonvbases);
3847
3848   /* Last element is entire class, so don't copy */
3849   bases_vec = make_tree_vec (vector->length () - 1);
3850
3851   for (i = 0; i < vector->length () - 1; ++i)
3852     {
3853       TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
3854     }
3855   release_tree_vector (vector);
3856   return bases_vec;
3857 }
3858
3859 tree
3860 finish_bases (tree type, bool direct)
3861 {
3862   tree bases = NULL_TREE;
3863
3864   if (!processing_template_decl)
3865     {
3866       /* Parameter packs can only be used in templates */
3867       error ("Parameter pack __bases only valid in template declaration");
3868       return error_mark_node;
3869     }
3870
3871   bases = cxx_make_type (BASES);
3872   BASES_TYPE (bases) = type;
3873   BASES_DIRECT (bases) = direct;
3874   SET_TYPE_STRUCTURAL_EQUALITY (bases);
3875
3876   return bases;
3877 }
3878
3879 /* Perform C++-specific checks for __builtin_offsetof before calling
3880    fold_offsetof.  */
3881
3882 tree
3883 finish_offsetof (tree expr, location_t loc)
3884 {
3885   /* If we're processing a template, we can't finish the semantics yet.
3886      Otherwise we can fold the entire expression now.  */
3887   if (processing_template_decl)
3888     {
3889       expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
3890       SET_EXPR_LOCATION (expr, loc);
3891       return expr;
3892     }
3893
3894   if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
3895     {
3896       error ("cannot apply %<offsetof%> to destructor %<~%T%>",
3897               TREE_OPERAND (expr, 2));
3898       return error_mark_node;
3899     }
3900   if (TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
3901       || TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE
3902       || TREE_TYPE (expr) == unknown_type_node)
3903     {
3904       if (INDIRECT_REF_P (expr))
3905         error ("second operand of %<offsetof%> is neither a single "
3906                "identifier nor a sequence of member accesses and "
3907                "array references");
3908       else
3909         {
3910           if (TREE_CODE (expr) == COMPONENT_REF
3911               || TREE_CODE (expr) == COMPOUND_EXPR)
3912             expr = TREE_OPERAND (expr, 1);
3913           error ("cannot apply %<offsetof%> to member function %qD", expr);
3914         }
3915       return error_mark_node;
3916     }
3917   if (REFERENCE_REF_P (expr))
3918     expr = TREE_OPERAND (expr, 0);
3919   if (TREE_CODE (expr) == COMPONENT_REF)
3920     {
3921       tree object = TREE_OPERAND (expr, 0);
3922       if (!complete_type_or_else (TREE_TYPE (object), object))
3923         return error_mark_node;
3924       if (warn_invalid_offsetof
3925           && CLASS_TYPE_P (TREE_TYPE (object))
3926           && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (object))
3927           && cp_unevaluated_operand == 0)
3928         pedwarn (loc, OPT_Winvalid_offsetof,
3929                  "offsetof within non-standard-layout type %qT is undefined",
3930                  TREE_TYPE (object));
3931     }
3932   return fold_offsetof (expr);
3933 }
3934
3935 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR.  This
3936    function is broken out from the above for the benefit of the tree-ssa
3937    project.  */
3938
3939 void
3940 simplify_aggr_init_expr (tree *tp)
3941 {
3942   tree aggr_init_expr = *tp;
3943
3944   /* Form an appropriate CALL_EXPR.  */
3945   tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
3946   tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
3947   tree type = TREE_TYPE (slot);
3948
3949   tree call_expr;
3950   enum style_t { ctor, arg, pcc } style;
3951
3952   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
3953     style = ctor;
3954 #ifdef PCC_STATIC_STRUCT_RETURN
3955   else if (1)
3956     style = pcc;
3957 #endif
3958   else
3959     {
3960       gcc_assert (TREE_ADDRESSABLE (type));
3961       style = arg;
3962     }
3963
3964   call_expr = build_call_array_loc (input_location,
3965                                     TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
3966                                     fn,
3967                                     aggr_init_expr_nargs (aggr_init_expr),
3968                                     AGGR_INIT_EXPR_ARGP (aggr_init_expr));
3969   TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
3970   CALL_EXPR_LIST_INIT_P (call_expr) = CALL_EXPR_LIST_INIT_P (aggr_init_expr);
3971
3972   if (style == ctor)
3973     {
3974       /* Replace the first argument to the ctor with the address of the
3975          slot.  */
3976       cxx_mark_addressable (slot);
3977       CALL_EXPR_ARG (call_expr, 0) =
3978         build1 (ADDR_EXPR, build_pointer_type (type), slot);
3979     }
3980   else if (style == arg)
3981     {
3982       /* Just mark it addressable here, and leave the rest to
3983          expand_call{,_inline}.  */
3984       cxx_mark_addressable (slot);
3985       CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
3986       call_expr = build2 (INIT_EXPR, TREE_TYPE (call_expr), slot, call_expr);
3987     }
3988   else if (style == pcc)
3989     {
3990       /* If we're using the non-reentrant PCC calling convention, then we
3991          need to copy the returned value out of the static buffer into the
3992          SLOT.  */
3993       push_deferring_access_checks (dk_no_check);
3994       call_expr = build_aggr_init (slot, call_expr,
3995                                    DIRECT_BIND | LOOKUP_ONLYCONVERTING,
3996                                    tf_warning_or_error);
3997       pop_deferring_access_checks ();
3998       call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
3999     }
4000
4001   if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4002     {
4003       tree init = build_zero_init (type, NULL_TREE,
4004                                    /*static_storage_p=*/false);
4005       init = build2 (INIT_EXPR, void_type_node, slot, init);
4006       call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4007                           init, call_expr);
4008     }
4009
4010   *tp = call_expr;
4011 }
4012
4013 /* Emit all thunks to FN that should be emitted when FN is emitted.  */
4014
4015 void
4016 emit_associated_thunks (tree fn)
4017 {
4018   /* When we use vcall offsets, we emit thunks with the virtual
4019      functions to which they thunk. The whole point of vcall offsets
4020      is so that you can know statically the entire set of thunks that
4021      will ever be needed for a given virtual function, thereby
4022      enabling you to output all the thunks with the function itself.  */
4023   if (DECL_VIRTUAL_P (fn)
4024       /* Do not emit thunks for extern template instantiations.  */
4025       && ! DECL_REALLY_EXTERN (fn))
4026     {
4027       tree thunk;
4028
4029       for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4030         {
4031           if (!THUNK_ALIAS (thunk))
4032             {
4033               use_thunk (thunk, /*emit_p=*/1);
4034               if (DECL_RESULT_THUNK_P (thunk))
4035                 {
4036                   tree probe;
4037
4038                   for (probe = DECL_THUNKS (thunk);
4039                        probe; probe = DECL_CHAIN (probe))
4040                     use_thunk (probe, /*emit_p=*/1);
4041                 }
4042             }
4043           else
4044             gcc_assert (!DECL_THUNKS (thunk));
4045         }
4046     }
4047 }
4048
4049 /* Generate RTL for FN.  */
4050
4051 bool
4052 expand_or_defer_fn_1 (tree fn)
4053 {
4054   /* When the parser calls us after finishing the body of a template
4055      function, we don't really want to expand the body.  */
4056   if (processing_template_decl)
4057     {
4058       /* Normally, collection only occurs in rest_of_compilation.  So,
4059          if we don't collect here, we never collect junk generated
4060          during the processing of templates until we hit a
4061          non-template function.  It's not safe to do this inside a
4062          nested class, though, as the parser may have local state that
4063          is not a GC root.  */
4064       if (!function_depth)
4065         ggc_collect ();
4066       return false;
4067     }
4068
4069   gcc_assert (DECL_SAVED_TREE (fn));
4070
4071   /* We make a decision about linkage for these functions at the end
4072      of the compilation.  Until that point, we do not want the back
4073      end to output them -- but we do want it to see the bodies of
4074      these functions so that it can inline them as appropriate.  */
4075   if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4076     {
4077       if (DECL_INTERFACE_KNOWN (fn))
4078         /* We've already made a decision as to how this function will
4079            be handled.  */;
4080       else if (!at_eof)
4081         tentative_decl_linkage (fn);
4082       else
4083         import_export_decl (fn);
4084
4085       /* If the user wants us to keep all inline functions, then mark
4086          this function as needed so that finish_file will make sure to
4087          output it later.  Similarly, all dllexport'd functions must
4088          be emitted; there may be callers in other DLLs.  */
4089       if (DECL_DECLARED_INLINE_P (fn)
4090           && !DECL_REALLY_EXTERN (fn)
4091           && (flag_keep_inline_functions
4092               || (flag_keep_inline_dllexport
4093                   && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4094         {
4095           mark_needed (fn);
4096           DECL_EXTERNAL (fn) = 0;
4097         }
4098     }
4099
4100   /* If this is a constructor or destructor body, we have to clone
4101      it.  */
4102   if (maybe_clone_body (fn))
4103     {
4104       /* We don't want to process FN again, so pretend we've written
4105          it out, even though we haven't.  */
4106       TREE_ASM_WRITTEN (fn) = 1;
4107       /* If this is an instantiation of a constexpr function, keep
4108          DECL_SAVED_TREE for explain_invalid_constexpr_fn.  */
4109       if (!is_instantiation_of_constexpr (fn))
4110         DECL_SAVED_TREE (fn) = NULL_TREE;
4111       return false;
4112     }
4113
4114   /* There's no reason to do any of the work here if we're only doing
4115      semantic analysis; this code just generates RTL.  */
4116   if (flag_syntax_only)
4117     return false;
4118
4119   return true;
4120 }
4121
4122 void
4123 expand_or_defer_fn (tree fn)
4124 {
4125   if (expand_or_defer_fn_1 (fn))
4126     {
4127       function_depth++;
4128
4129       /* Expand or defer, at the whim of the compilation unit manager.  */
4130       cgraph_node::finalize_function (fn, function_depth > 1);
4131       emit_associated_thunks (fn);
4132
4133       function_depth--;
4134     }
4135 }
4136
4137 struct nrv_data
4138 {
4139   nrv_data () : visited (37) {}
4140
4141   tree var;
4142   tree result;
4143   hash_table<pointer_hash <tree_node> > visited;
4144 };
4145
4146 /* Helper function for walk_tree, used by finalize_nrv below.  */
4147
4148 static tree
4149 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4150 {
4151   struct nrv_data *dp = (struct nrv_data *)data;
4152   tree_node **slot;
4153
4154   /* No need to walk into types.  There wouldn't be any need to walk into
4155      non-statements, except that we have to consider STMT_EXPRs.  */
4156   if (TYPE_P (*tp))
4157     *walk_subtrees = 0;
4158   /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4159      but differs from using NULL_TREE in that it indicates that we care
4160      about the value of the RESULT_DECL.  */
4161   else if (TREE_CODE (*tp) == RETURN_EXPR)
4162     TREE_OPERAND (*tp, 0) = dp->result;
4163   /* Change all cleanups for the NRV to only run when an exception is
4164      thrown.  */
4165   else if (TREE_CODE (*tp) == CLEANUP_STMT
4166            && CLEANUP_DECL (*tp) == dp->var)
4167     CLEANUP_EH_ONLY (*tp) = 1;
4168   /* Replace the DECL_EXPR for the NRV with an initialization of the
4169      RESULT_DECL, if needed.  */
4170   else if (TREE_CODE (*tp) == DECL_EXPR
4171            && DECL_EXPR_DECL (*tp) == dp->var)
4172     {
4173       tree init;
4174       if (DECL_INITIAL (dp->var)
4175           && DECL_INITIAL (dp->var) != error_mark_node)
4176         init = build2 (INIT_EXPR, void_type_node, dp->result,
4177                        DECL_INITIAL (dp->var));
4178       else
4179         init = build_empty_stmt (EXPR_LOCATION (*tp));
4180       DECL_INITIAL (dp->var) = NULL_TREE;
4181       SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4182       *tp = init;
4183     }
4184   /* And replace all uses of the NRV with the RESULT_DECL.  */
4185   else if (*tp == dp->var)
4186     *tp = dp->result;
4187
4188   /* Avoid walking into the same tree more than once.  Unfortunately, we
4189      can't just use walk_tree_without duplicates because it would only call
4190      us for the first occurrence of dp->var in the function body.  */
4191   slot = dp->visited.find_slot (*tp, INSERT);
4192   if (*slot)
4193     *walk_subtrees = 0;
4194   else
4195     *slot = *tp;
4196
4197   /* Keep iterating.  */
4198   return NULL_TREE;
4199 }
4200
4201 /* Called from finish_function to implement the named return value
4202    optimization by overriding all the RETURN_EXPRs and pertinent
4203    CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
4204    RESULT_DECL for the function.  */
4205
4206 void
4207 finalize_nrv (tree *tp, tree var, tree result)
4208 {
4209   struct nrv_data data;
4210
4211   /* Copy name from VAR to RESULT.  */
4212   DECL_NAME (result) = DECL_NAME (var);
4213   /* Don't forget that we take its address.  */
4214   TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
4215   /* Finally set DECL_VALUE_EXPR to avoid assigning
4216      a stack slot at -O0 for the original var and debug info
4217      uses RESULT location for VAR.  */
4218   SET_DECL_VALUE_EXPR (var, result);
4219   DECL_HAS_VALUE_EXPR_P (var) = 1;
4220
4221   data.var = var;
4222   data.result = result;
4223   cp_walk_tree (tp, finalize_nrv_r, &data, 0);
4224 }
4225 \f
4226 /* Create CP_OMP_CLAUSE_INFO for clause C.  Returns true if it is invalid.  */
4227
4228 bool
4229 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
4230                             bool need_copy_ctor, bool need_copy_assignment,
4231                             bool need_dtor)
4232 {
4233   int save_errorcount = errorcount;
4234   tree info, t;
4235
4236   /* Always allocate 3 elements for simplicity.  These are the
4237      function decls for the ctor, dtor, and assignment op.
4238      This layout is known to the three lang hooks,
4239      cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
4240      and cxx_omp_clause_assign_op.  */
4241   info = make_tree_vec (3);
4242   CP_OMP_CLAUSE_INFO (c) = info;
4243
4244   if (need_default_ctor || need_copy_ctor)
4245     {
4246       if (need_default_ctor)
4247         t = get_default_ctor (type);
4248       else
4249         t = get_copy_ctor (type, tf_warning_or_error);
4250
4251       if (t && !trivial_fn_p (t))
4252         TREE_VEC_ELT (info, 0) = t;
4253     }
4254
4255   if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4256     TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
4257
4258   if (need_copy_assignment)
4259     {
4260       t = get_copy_assign (type);
4261
4262       if (t && !trivial_fn_p (t))
4263         TREE_VEC_ELT (info, 2) = t;
4264     }
4265
4266   return errorcount != save_errorcount;
4267 }
4268
4269 /* Helper function for handle_omp_array_sections.  Called recursively
4270    to handle multiple array-section-subscripts.  C is the clause,
4271    T current expression (initially OMP_CLAUSE_DECL), which is either
4272    a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
4273    expression if specified, TREE_VALUE length expression if specified,
4274    TREE_CHAIN is what it has been specified after, or some decl.
4275    TYPES vector is populated with array section types, MAYBE_ZERO_LEN
4276    set to true if any of the array-section-subscript could have length
4277    of zero (explicit or implicit), FIRST_NON_ONE is the index of the
4278    first array-section-subscript which is known not to have length
4279    of one.  Given say:
4280    map(a[:b][2:1][:c][:2][:d][e:f][2:5])
4281    FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
4282    all are or may have length of 1, array-section-subscript [:2] is the
4283    first one knonwn not to have length 1.  For array-section-subscript
4284    <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
4285    0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
4286    can if MAYBE_ZERO_LEN is false.  MAYBE_ZERO_LEN will be true in the above
4287    case though, as some lengths could be zero.  */
4288
4289 static tree
4290 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
4291                              bool &maybe_zero_len, unsigned int &first_non_one)
4292 {
4293   tree ret, low_bound, length, type;
4294   if (TREE_CODE (t) != TREE_LIST)
4295     {
4296       if (error_operand_p (t))
4297         return error_mark_node;
4298       if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
4299         {
4300           if (processing_template_decl)
4301             return NULL_TREE;
4302           if (DECL_P (t))
4303             error_at (OMP_CLAUSE_LOCATION (c),
4304                       "%qD is not a variable in %qs clause", t,
4305                       omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4306           else
4307             error_at (OMP_CLAUSE_LOCATION (c),
4308                       "%qE is not a variable in %qs clause", t,
4309                       omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4310           return error_mark_node;
4311         }
4312       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4313                && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
4314         {
4315           error_at (OMP_CLAUSE_LOCATION (c),
4316                     "%qD is threadprivate variable in %qs clause", t,
4317                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4318           return error_mark_node;
4319         }
4320       if (type_dependent_expression_p (t))
4321         return NULL_TREE;
4322       t = convert_from_reference (t);
4323       return t;
4324     }
4325
4326   ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
4327                                      maybe_zero_len, first_non_one);
4328   if (ret == error_mark_node || ret == NULL_TREE)
4329     return ret;
4330
4331   type = TREE_TYPE (ret);
4332   low_bound = TREE_PURPOSE (t);
4333   length = TREE_VALUE (t);
4334   if ((low_bound && type_dependent_expression_p (low_bound))
4335       || (length && type_dependent_expression_p (length)))
4336     return NULL_TREE;
4337
4338   if (low_bound == error_mark_node || length == error_mark_node)
4339     return error_mark_node;
4340
4341   if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
4342     {
4343       error_at (OMP_CLAUSE_LOCATION (c),
4344                 "low bound %qE of array section does not have integral type",
4345                 low_bound);
4346       return error_mark_node;
4347     }
4348   if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
4349     {
4350       error_at (OMP_CLAUSE_LOCATION (c),
4351                 "length %qE of array section does not have integral type",
4352                 length);
4353       return error_mark_node;
4354     }
4355   if (low_bound)
4356     low_bound = mark_rvalue_use (low_bound);
4357   if (length)
4358     length = mark_rvalue_use (length);
4359   if (low_bound
4360       && TREE_CODE (low_bound) == INTEGER_CST
4361       && TYPE_PRECISION (TREE_TYPE (low_bound))
4362          > TYPE_PRECISION (sizetype))
4363     low_bound = fold_convert (sizetype, low_bound);
4364   if (length
4365       && TREE_CODE (length) == INTEGER_CST
4366       && TYPE_PRECISION (TREE_TYPE (length))
4367          > TYPE_PRECISION (sizetype))
4368     length = fold_convert (sizetype, length);
4369   if (low_bound == NULL_TREE)
4370     low_bound = integer_zero_node;
4371
4372   if (length != NULL_TREE)
4373     {
4374       if (!integer_nonzerop (length))
4375         maybe_zero_len = true;
4376       if (first_non_one == types.length ()
4377           && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
4378         first_non_one++;
4379     }
4380   if (TREE_CODE (type) == ARRAY_TYPE)
4381     {
4382       if (length == NULL_TREE
4383           && (TYPE_DOMAIN (type) == NULL_TREE
4384               || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
4385         {
4386           error_at (OMP_CLAUSE_LOCATION (c),
4387                     "for unknown bound array type length expression must "
4388                     "be specified");
4389           return error_mark_node;
4390         }
4391       if (TREE_CODE (low_bound) == INTEGER_CST
4392           && tree_int_cst_sgn (low_bound) == -1)
4393         {
4394           error_at (OMP_CLAUSE_LOCATION (c),
4395                     "negative low bound in array section in %qs clause",
4396                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4397           return error_mark_node;
4398         }
4399       if (length != NULL_TREE
4400           && TREE_CODE (length) == INTEGER_CST
4401           && tree_int_cst_sgn (length) == -1)
4402         {
4403           error_at (OMP_CLAUSE_LOCATION (c),
4404                     "negative length in array section in %qs clause",
4405                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4406           return error_mark_node;
4407         }
4408       if (TYPE_DOMAIN (type)
4409           && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
4410           && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
4411                         == INTEGER_CST)
4412         {
4413           tree size = size_binop (PLUS_EXPR,
4414                                   TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4415                                   size_one_node);
4416           if (TREE_CODE (low_bound) == INTEGER_CST)
4417             {
4418               if (tree_int_cst_lt (size, low_bound))
4419                 {
4420                   error_at (OMP_CLAUSE_LOCATION (c),
4421                             "low bound %qE above array section size "
4422                             "in %qs clause", low_bound,
4423                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4424                   return error_mark_node;
4425                 }
4426               if (tree_int_cst_equal (size, low_bound))
4427                 maybe_zero_len = true;
4428               else if (length == NULL_TREE
4429                        && first_non_one == types.length ()
4430                        && tree_int_cst_equal
4431                             (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4432                              low_bound))
4433                 first_non_one++;
4434             }
4435           else if (length == NULL_TREE)
4436             {
4437               maybe_zero_len = true;
4438               if (first_non_one == types.length ())
4439                 first_non_one++;
4440             }
4441           if (length && TREE_CODE (length) == INTEGER_CST)
4442             {
4443               if (tree_int_cst_lt (size, length))
4444                 {
4445                   error_at (OMP_CLAUSE_LOCATION (c),
4446                             "length %qE above array section size "
4447                             "in %qs clause", length,
4448                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4449                   return error_mark_node;
4450                 }
4451               if (TREE_CODE (low_bound) == INTEGER_CST)
4452                 {
4453                   tree lbpluslen
4454                     = size_binop (PLUS_EXPR,
4455                                   fold_convert (sizetype, low_bound),
4456                                   fold_convert (sizetype, length));
4457                   if (TREE_CODE (lbpluslen) == INTEGER_CST
4458                       && tree_int_cst_lt (size, lbpluslen))
4459                     {
4460                       error_at (OMP_CLAUSE_LOCATION (c),
4461                                 "high bound %qE above array section size "
4462                                 "in %qs clause", lbpluslen,
4463                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4464                       return error_mark_node;
4465                     }
4466                 }
4467             }
4468         }
4469       else if (length == NULL_TREE)
4470         {
4471           maybe_zero_len = true;
4472           if (first_non_one == types.length ())
4473             first_non_one++;
4474         }
4475
4476       /* For [lb:] we will need to evaluate lb more than once.  */
4477       if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4478         {
4479           tree lb = cp_save_expr (low_bound);
4480           if (lb != low_bound)
4481             {
4482               TREE_PURPOSE (t) = lb;
4483               low_bound = lb;
4484             }
4485         }
4486     }
4487   else if (TREE_CODE (type) == POINTER_TYPE)
4488     {
4489       if (length == NULL_TREE)
4490         {
4491           error_at (OMP_CLAUSE_LOCATION (c),
4492                     "for pointer type length expression must be specified");
4493           return error_mark_node;
4494         }
4495       /* If there is a pointer type anywhere but in the very first
4496          array-section-subscript, the array section can't be contiguous.  */
4497       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
4498           && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
4499         {
4500           error_at (OMP_CLAUSE_LOCATION (c),
4501                     "array section is not contiguous in %qs clause",
4502                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4503           return error_mark_node;
4504         }
4505     }
4506   else
4507     {
4508       error_at (OMP_CLAUSE_LOCATION (c),
4509                 "%qE does not have pointer or array type", ret);
4510       return error_mark_node;
4511     }
4512   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
4513     types.safe_push (TREE_TYPE (ret));
4514   /* We will need to evaluate lb more than once.  */
4515   tree lb = cp_save_expr (low_bound);
4516   if (lb != low_bound)
4517     {
4518       TREE_PURPOSE (t) = lb;
4519       low_bound = lb;
4520     }
4521   ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, false);
4522   return ret;
4523 }
4524
4525 /* Handle array sections for clause C.  */
4526
4527 static bool
4528 handle_omp_array_sections (tree c)
4529 {
4530   bool maybe_zero_len = false;
4531   unsigned int first_non_one = 0;
4532   auto_vec<tree> types;
4533   tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
4534                                             maybe_zero_len, first_non_one);
4535   if (first == error_mark_node)
4536     return true;
4537   if (first == NULL_TREE)
4538     return false;
4539   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
4540     {
4541       tree t = OMP_CLAUSE_DECL (c);
4542       tree tem = NULL_TREE;
4543       if (processing_template_decl)
4544         return false;
4545       /* Need to evaluate side effects in the length expressions
4546          if any.  */
4547       while (TREE_CODE (t) == TREE_LIST)
4548         {
4549           if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
4550             {
4551               if (tem == NULL_TREE)
4552                 tem = TREE_VALUE (t);
4553               else
4554                 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
4555                               TREE_VALUE (t), tem);
4556             }
4557           t = TREE_CHAIN (t);
4558         }
4559       if (tem)
4560         first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
4561       OMP_CLAUSE_DECL (c) = first;
4562     }
4563   else
4564     {
4565       unsigned int num = types.length (), i;
4566       tree t, side_effects = NULL_TREE, size = NULL_TREE;
4567       tree condition = NULL_TREE;
4568
4569       if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
4570         maybe_zero_len = true;
4571       if (processing_template_decl && maybe_zero_len)
4572         return false;
4573
4574       for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
4575            t = TREE_CHAIN (t))
4576         {
4577           tree low_bound = TREE_PURPOSE (t);
4578           tree length = TREE_VALUE (t);
4579
4580           i--;
4581           if (low_bound
4582               && TREE_CODE (low_bound) == INTEGER_CST
4583               && TYPE_PRECISION (TREE_TYPE (low_bound))
4584                  > TYPE_PRECISION (sizetype))
4585             low_bound = fold_convert (sizetype, low_bound);
4586           if (length
4587               && TREE_CODE (length) == INTEGER_CST
4588               && TYPE_PRECISION (TREE_TYPE (length))
4589                  > TYPE_PRECISION (sizetype))
4590             length = fold_convert (sizetype, length);
4591           if (low_bound == NULL_TREE)
4592             low_bound = integer_zero_node;
4593           if (!maybe_zero_len && i > first_non_one)
4594             {
4595               if (integer_nonzerop (low_bound))
4596                 goto do_warn_noncontiguous;
4597               if (length != NULL_TREE
4598                   && TREE_CODE (length) == INTEGER_CST
4599                   && TYPE_DOMAIN (types[i])
4600                   && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
4601                   && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
4602                      == INTEGER_CST)
4603                 {
4604                   tree size;
4605                   size = size_binop (PLUS_EXPR,
4606                                      TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4607                                      size_one_node);
4608                   if (!tree_int_cst_equal (length, size))
4609                     {
4610                      do_warn_noncontiguous:
4611                       error_at (OMP_CLAUSE_LOCATION (c),
4612                                 "array section is not contiguous in %qs "
4613                                 "clause",
4614                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
4615                       return true;
4616                     }
4617                 }
4618               if (!processing_template_decl
4619                   && length != NULL_TREE
4620                   && TREE_SIDE_EFFECTS (length))
4621                 {
4622                   if (side_effects == NULL_TREE)
4623                     side_effects = length;
4624                   else
4625                     side_effects = build2 (COMPOUND_EXPR,
4626                                            TREE_TYPE (side_effects),
4627                                            length, side_effects);
4628                 }
4629             }
4630           else if (processing_template_decl)
4631             continue;
4632           else
4633             {
4634               tree l;
4635
4636               if (i > first_non_one && length && integer_nonzerop (length))
4637                 continue;
4638               if (length)
4639                 l = fold_convert (sizetype, length);
4640               else
4641                 {
4642                   l = size_binop (PLUS_EXPR,
4643                                   TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
4644                                   size_one_node);
4645                   l = size_binop (MINUS_EXPR, l,
4646                                   fold_convert (sizetype, low_bound));
4647                 }
4648               if (i > first_non_one)
4649                 {
4650                   l = fold_build2 (NE_EXPR, boolean_type_node, l,
4651                                    size_zero_node);
4652                   if (condition == NULL_TREE)
4653                     condition = l;
4654                   else
4655                     condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
4656                                              l, condition);
4657                 }
4658               else if (size == NULL_TREE)
4659                 {
4660                   size = size_in_bytes (TREE_TYPE (types[i]));
4661                   size = size_binop (MULT_EXPR, size, l);
4662                   if (condition)
4663                     size = fold_build3 (COND_EXPR, sizetype, condition,
4664                                         size, size_zero_node);
4665                 }
4666               else
4667                 size = size_binop (MULT_EXPR, size, l);
4668             }
4669         }
4670       if (!processing_template_decl)
4671         {
4672           if (side_effects)
4673             size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
4674           OMP_CLAUSE_DECL (c) = first;
4675           OMP_CLAUSE_SIZE (c) = size;
4676           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
4677             return false;
4678           tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4679                                       OMP_CLAUSE_MAP);
4680           OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
4681           if (!cxx_mark_addressable (t))
4682             return false;
4683           OMP_CLAUSE_DECL (c2) = t;
4684           t = build_fold_addr_expr (first);
4685           t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4686                                 ptrdiff_type_node, t);
4687           tree ptr = OMP_CLAUSE_DECL (c2);
4688           ptr = convert_from_reference (ptr);
4689           if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
4690             ptr = build_fold_addr_expr (ptr);
4691           t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
4692                                ptrdiff_type_node, t,
4693                                fold_convert_loc (OMP_CLAUSE_LOCATION (c),
4694                                                  ptrdiff_type_node, ptr));
4695           OMP_CLAUSE_SIZE (c2) = t;
4696           OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
4697           OMP_CLAUSE_CHAIN (c) = c2;
4698           ptr = OMP_CLAUSE_DECL (c2);
4699           if (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
4700               && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
4701             {
4702               tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
4703                                           OMP_CLAUSE_MAP);
4704               OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_POINTER);
4705               OMP_CLAUSE_DECL (c3) = ptr;
4706               OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
4707               OMP_CLAUSE_SIZE (c3) = size_zero_node;
4708               OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
4709               OMP_CLAUSE_CHAIN (c2) = c3;
4710             }
4711         }
4712     }
4713   return false;
4714 }
4715
4716 /* Return identifier to look up for omp declare reduction.  */
4717
4718 tree
4719 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
4720 {
4721   const char *p = NULL;
4722   const char *m = NULL;
4723   switch (reduction_code)
4724     {
4725     case PLUS_EXPR:
4726     case MULT_EXPR:
4727     case MINUS_EXPR:
4728     case BIT_AND_EXPR:
4729     case BIT_XOR_EXPR:
4730     case BIT_IOR_EXPR:
4731     case TRUTH_ANDIF_EXPR:
4732     case TRUTH_ORIF_EXPR:
4733       reduction_id = ansi_opname (reduction_code);
4734       break;
4735     case MIN_EXPR:
4736       p = "min";
4737       break;
4738     case MAX_EXPR:
4739       p = "max";
4740       break;
4741     default:
4742       break;
4743     }
4744
4745   if (p == NULL)
4746     {
4747       if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
4748         return error_mark_node;
4749       p = IDENTIFIER_POINTER (reduction_id);
4750     }
4751
4752   if (type != NULL_TREE)
4753     m = mangle_type_string (TYPE_MAIN_VARIANT (type));
4754
4755   const char prefix[] = "omp declare reduction ";
4756   size_t lenp = sizeof (prefix);
4757   if (strncmp (p, prefix, lenp - 1) == 0)
4758     lenp = 1;
4759   size_t len = strlen (p);
4760   size_t lenm = m ? strlen (m) + 1 : 0;
4761   char *name = XALLOCAVEC (char, lenp + len + lenm);
4762   if (lenp > 1)
4763     memcpy (name, prefix, lenp - 1);
4764   memcpy (name + lenp - 1, p, len + 1);
4765   if (m)
4766     {
4767       name[lenp + len - 1] = '~';
4768       memcpy (name + lenp + len, m, lenm);
4769     }
4770   return get_identifier (name);
4771 }
4772
4773 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
4774    FUNCTION_DECL or NULL_TREE if not found.  */
4775
4776 static tree
4777 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
4778                       vec<tree> *ambiguousp)
4779 {
4780   tree orig_id = id;
4781   tree baselink = NULL_TREE;
4782   if (identifier_p (id))
4783     {
4784       cp_id_kind idk;
4785       bool nonint_cst_expression_p;
4786       const char *error_msg;
4787       id = omp_reduction_id (ERROR_MARK, id, type);
4788       tree decl = lookup_name (id);
4789       if (decl == NULL_TREE)
4790         decl = error_mark_node;
4791       id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
4792                                  &nonint_cst_expression_p, false, true, false,
4793                                  false, &error_msg, loc);
4794       if (idk == CP_ID_KIND_UNQUALIFIED
4795           && identifier_p (id))
4796         {
4797           vec<tree, va_gc> *args = NULL;
4798           vec_safe_push (args, build_reference_type (type));
4799           id = perform_koenig_lookup (id, args, tf_none);
4800         }
4801     }
4802   else if (TREE_CODE (id) == SCOPE_REF)
4803     id = lookup_qualified_name (TREE_OPERAND (id, 0),
4804                                 omp_reduction_id (ERROR_MARK,
4805                                                   TREE_OPERAND (id, 1),
4806                                                   type),
4807                                 false, false);
4808   tree fns = id;
4809   if (id && is_overloaded_fn (id))
4810     id = get_fns (id);
4811   for (; id; id = OVL_NEXT (id))
4812     {
4813       tree fndecl = OVL_CURRENT (id);
4814       if (TREE_CODE (fndecl) == FUNCTION_DECL)
4815         {
4816           tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
4817           if (same_type_p (TREE_TYPE (argtype), type))
4818             break;
4819         }
4820     }
4821   if (id && BASELINK_P (fns))
4822     {
4823       if (baselinkp)
4824         *baselinkp = fns;
4825       else
4826         baselink = fns;
4827     }
4828   if (id == NULL_TREE && CLASS_TYPE_P (type) && TYPE_BINFO (type))
4829     {
4830       vec<tree> ambiguous = vNULL;
4831       tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
4832       unsigned int ix;
4833       if (ambiguousp == NULL)
4834         ambiguousp = &ambiguous;
4835       for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
4836         {
4837           id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
4838                                      baselinkp ? baselinkp : &baselink,
4839                                      ambiguousp);
4840           if (id == NULL_TREE)
4841             continue;
4842           if (!ambiguousp->is_empty ())
4843             ambiguousp->safe_push (id);
4844           else if (ret != NULL_TREE)
4845             {
4846               ambiguousp->safe_push (ret);
4847               ambiguousp->safe_push (id);
4848               ret = NULL_TREE;
4849             }
4850           else
4851             ret = id;
4852         }
4853       if (ambiguousp != &ambiguous)
4854         return ret;
4855       if (!ambiguous.is_empty ())
4856         {
4857           const char *str = _("candidates are:");
4858           unsigned int idx;
4859           tree udr;
4860           error_at (loc, "user defined reduction lookup is ambiguous");
4861           FOR_EACH_VEC_ELT (ambiguous, idx, udr)
4862             {
4863               inform (DECL_SOURCE_LOCATION (udr), "%s %#D", str, udr);
4864               if (idx == 0)
4865                 str = get_spaces (str);
4866             }
4867           ambiguous.release ();
4868           ret = error_mark_node;
4869           baselink = NULL_TREE;
4870         }
4871       id = ret;
4872     }
4873   if (id && baselink)
4874     perform_or_defer_access_check (BASELINK_BINFO (baselink),
4875                                    id, id, tf_warning_or_error);
4876   return id;
4877 }
4878
4879 /* Helper function for cp_parser_omp_declare_reduction_exprs
4880    and tsubst_omp_udr.
4881    Remove CLEANUP_STMT for data (omp_priv variable).
4882    Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
4883    DECL_EXPR.  */
4884
4885 tree
4886 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
4887 {
4888   if (TYPE_P (*tp))
4889     *walk_subtrees = 0;
4890   else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
4891     *tp = CLEANUP_BODY (*tp);
4892   else if (TREE_CODE (*tp) == DECL_EXPR)
4893     {
4894       tree decl = DECL_EXPR_DECL (*tp);
4895       if (!processing_template_decl
4896           && decl == (tree) data
4897           && DECL_INITIAL (decl)
4898           && DECL_INITIAL (decl) != error_mark_node)
4899         {
4900           tree list = NULL_TREE;
4901           append_to_statement_list_force (*tp, &list);
4902           tree init_expr = build2 (INIT_EXPR, void_type_node,
4903                                    decl, DECL_INITIAL (decl));
4904           DECL_INITIAL (decl) = NULL_TREE;
4905           append_to_statement_list_force (init_expr, &list);
4906           *tp = list;
4907         }
4908     }
4909   return NULL_TREE;
4910 }
4911
4912 /* Data passed from cp_check_omp_declare_reduction to
4913    cp_check_omp_declare_reduction_r.  */
4914
4915 struct cp_check_omp_declare_reduction_data
4916 {
4917   location_t loc;
4918   tree stmts[7];
4919   bool combiner_p;
4920 };
4921
4922 /* Helper function for cp_check_omp_declare_reduction, called via
4923    cp_walk_tree.  */
4924
4925 static tree
4926 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
4927 {
4928   struct cp_check_omp_declare_reduction_data *udr_data
4929     = (struct cp_check_omp_declare_reduction_data *) data;
4930   if (SSA_VAR_P (*tp)
4931       && !DECL_ARTIFICIAL (*tp)
4932       && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
4933       && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
4934     {
4935       location_t loc = udr_data->loc;
4936       if (udr_data->combiner_p)
4937         error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
4938                        "variable %qD which is not %<omp_out%> nor %<omp_in%>",
4939                   *tp);
4940       else
4941         error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
4942                        "to variable %qD which is not %<omp_priv%> nor "
4943                        "%<omp_orig%>",
4944                   *tp);
4945       return *tp;
4946     }
4947   return NULL_TREE;
4948 }
4949
4950 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions.  */
4951
4952 void
4953 cp_check_omp_declare_reduction (tree udr)
4954 {
4955   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
4956   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
4957   type = TREE_TYPE (type);
4958   int i;
4959   location_t loc = DECL_SOURCE_LOCATION (udr);
4960
4961   if (type == error_mark_node)
4962     return;
4963   if (ARITHMETIC_TYPE_P (type))
4964     {
4965       static enum tree_code predef_codes[]
4966         = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
4967             BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
4968       for (i = 0; i < 8; i++)
4969         {
4970           tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
4971           const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
4972           const char *n2 = IDENTIFIER_POINTER (id);
4973           if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
4974               && (n1[IDENTIFIER_LENGTH (id)] == '~'
4975                   || n1[IDENTIFIER_LENGTH (id)] == '\0'))
4976             break;
4977         }
4978
4979       if (i == 8
4980           && TREE_CODE (type) != COMPLEX_EXPR)
4981         {
4982           const char prefix_minmax[] = "omp declare reduction m";
4983           size_t prefix_size = sizeof (prefix_minmax) - 1;
4984           const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
4985           if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
4986                        prefix_minmax, prefix_size) == 0
4987               && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
4988                   || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
4989               && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
4990             i = 0;
4991         }
4992       if (i < 8)
4993         {
4994           error_at (loc, "predeclared arithmetic type %qT in "
4995                          "%<#pragma omp declare reduction%>", type);
4996           return;
4997         }
4998     }
4999   else if (TREE_CODE (type) == FUNCTION_TYPE
5000            || TREE_CODE (type) == METHOD_TYPE
5001            || TREE_CODE (type) == ARRAY_TYPE)
5002     {
5003       error_at (loc, "function or array type %qT in "
5004                      "%<#pragma omp declare reduction%>", type);
5005       return;
5006     }
5007   else if (TREE_CODE (type) == REFERENCE_TYPE)
5008     {
5009       error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
5010                 type);
5011       return;
5012     }
5013   else if (TYPE_QUALS_NO_ADDR_SPACE (type))
5014     {
5015       error_at (loc, "const, volatile or __restrict qualified type %qT in "
5016                      "%<#pragma omp declare reduction%>", type);
5017       return;
5018     }
5019
5020   tree body = DECL_SAVED_TREE (udr);
5021   if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
5022     return;
5023
5024   tree_stmt_iterator tsi;
5025   struct cp_check_omp_declare_reduction_data data;
5026   memset (data.stmts, 0, sizeof data.stmts);
5027   for (i = 0, tsi = tsi_start (body);
5028        i < 7 && !tsi_end_p (tsi);
5029        i++, tsi_next (&tsi))
5030     data.stmts[i] = tsi_stmt (tsi);
5031   data.loc = loc;
5032   gcc_assert (tsi_end_p (tsi));
5033   if (i >= 3)
5034     {
5035       gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
5036                   && TREE_CODE (data.stmts[1]) == DECL_EXPR);
5037       if (TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])))
5038         return;
5039       data.combiner_p = true;
5040       if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
5041                         &data, NULL))
5042         TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5043     }
5044   if (i >= 6)
5045     {
5046       gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
5047                   && TREE_CODE (data.stmts[4]) == DECL_EXPR);
5048       data.combiner_p = false;
5049       if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
5050                         &data, NULL)
5051           || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
5052                            cp_check_omp_declare_reduction_r, &data, NULL))
5053         TREE_NO_WARNING (DECL_EXPR_DECL (data.stmts[0])) = 1;
5054       if (i == 7)
5055         gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
5056     }
5057 }
5058
5059 /* Helper function of finish_omp_clauses.  Clone STMT as if we were making
5060    an inline call.  But, remap
5061    the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
5062    and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL.  */
5063
5064 static tree
5065 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
5066                tree decl, tree placeholder)
5067 {
5068   copy_body_data id;
5069   hash_map<tree, tree> decl_map;
5070
5071   decl_map.put (omp_decl1, placeholder);
5072   decl_map.put (omp_decl2, decl);
5073   memset (&id, 0, sizeof (id));
5074   id.src_fn = DECL_CONTEXT (omp_decl1);
5075   id.dst_fn = current_function_decl;
5076   id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
5077   id.decl_map = &decl_map;
5078
5079   id.copy_decl = copy_decl_no_change;
5080   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
5081   id.transform_new_cfg = true;
5082   id.transform_return_to_modify = false;
5083   id.transform_lang_insert_block = NULL;
5084   id.eh_lp_nr = 0;
5085   walk_tree (&stmt, copy_tree_body_r, &id, NULL);
5086   return stmt;
5087 }
5088
5089 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
5090    Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP.  */
5091
5092 static tree
5093 find_omp_placeholder_r (tree *tp, int *, void *data)
5094 {
5095   if (*tp == (tree) data)
5096     return *tp;
5097   return NULL_TREE;
5098 }
5099
5100 /* Helper function of finish_omp_clauses.  Handle OMP_CLAUSE_REDUCTION C.
5101    Return true if there is some error and the clause should be removed.  */
5102
5103 static bool
5104 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
5105 {
5106   tree t = OMP_CLAUSE_DECL (c);
5107   bool predefined = false;
5108   tree type = TREE_TYPE (t);
5109   if (TREE_CODE (type) == REFERENCE_TYPE)
5110     type = TREE_TYPE (type);
5111   if (type == error_mark_node)
5112     return true;
5113   else if (ARITHMETIC_TYPE_P (type))
5114     switch (OMP_CLAUSE_REDUCTION_CODE (c))
5115       {
5116       case PLUS_EXPR:
5117       case MULT_EXPR:
5118       case MINUS_EXPR:
5119         predefined = true;
5120         break;
5121       case MIN_EXPR:
5122       case MAX_EXPR:
5123         if (TREE_CODE (type) == COMPLEX_TYPE)
5124           break;
5125         predefined = true;
5126         break;
5127       case BIT_AND_EXPR:
5128       case BIT_IOR_EXPR:
5129       case BIT_XOR_EXPR:
5130         if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
5131           break;
5132         predefined = true;
5133         break;
5134       case TRUTH_ANDIF_EXPR:
5135       case TRUTH_ORIF_EXPR:
5136         if (FLOAT_TYPE_P (type))
5137           break;
5138         predefined = true;
5139         break;
5140       default:
5141         break;
5142       }
5143   else if (TREE_CODE (type) == ARRAY_TYPE || TYPE_READONLY (type))
5144     {
5145       error ("%qE has invalid type for %<reduction%>", t);
5146       return true;
5147     }
5148   else if (!processing_template_decl)
5149     {
5150       t = require_complete_type (t);
5151       if (t == error_mark_node)
5152         return true;
5153       OMP_CLAUSE_DECL (c) = t;
5154     }
5155
5156   if (predefined)
5157     {
5158       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5159       return false;
5160     }
5161   else if (processing_template_decl)
5162     return false;
5163
5164   tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
5165
5166   type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
5167   if (TREE_CODE (type) == REFERENCE_TYPE)
5168     type = TREE_TYPE (type);
5169   OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
5170   if (id == NULL_TREE)
5171     id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
5172                            NULL_TREE, NULL_TREE);
5173   id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
5174   if (id)
5175     {
5176       if (id == error_mark_node)
5177         return true;
5178       id = OVL_CURRENT (id);
5179       mark_used (id);
5180       tree body = DECL_SAVED_TREE (id);
5181       if (!body)
5182         return true;
5183       if (TREE_CODE (body) == STATEMENT_LIST)
5184         {
5185           tree_stmt_iterator tsi;
5186           tree placeholder = NULL_TREE;
5187           int i;
5188           tree stmts[7];
5189           tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
5190           atype = TREE_TYPE (atype);
5191           bool need_static_cast = !same_type_p (type, atype);
5192           memset (stmts, 0, sizeof stmts);
5193           for (i = 0, tsi = tsi_start (body);
5194                i < 7 && !tsi_end_p (tsi);
5195                i++, tsi_next (&tsi))
5196             stmts[i] = tsi_stmt (tsi);
5197           gcc_assert (tsi_end_p (tsi));
5198
5199           if (i >= 3)
5200             {
5201               gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
5202                           && TREE_CODE (stmts[1]) == DECL_EXPR);
5203               placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
5204               DECL_ARTIFICIAL (placeholder) = 1;
5205               DECL_IGNORED_P (placeholder) = 1;
5206               OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
5207               if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
5208                 cxx_mark_addressable (placeholder);
5209               if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
5210                   && TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5211                      != REFERENCE_TYPE)
5212                 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5213               tree omp_out = placeholder;
5214               tree omp_in = convert_from_reference (OMP_CLAUSE_DECL (c));
5215               if (need_static_cast)
5216                 {
5217                   tree rtype = build_reference_type (atype);
5218                   omp_out = build_static_cast (rtype, omp_out,
5219                                                tf_warning_or_error);
5220                   omp_in = build_static_cast (rtype, omp_in,
5221                                               tf_warning_or_error);
5222                   if (omp_out == error_mark_node || omp_in == error_mark_node)
5223                     return true;
5224                   omp_out = convert_from_reference (omp_out);
5225                   omp_in = convert_from_reference (omp_in);
5226                 }
5227               OMP_CLAUSE_REDUCTION_MERGE (c)
5228                 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
5229                                  DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
5230             }
5231           if (i >= 6)
5232             {
5233               gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
5234                           && TREE_CODE (stmts[4]) == DECL_EXPR);
5235               if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3])))
5236                 cxx_mark_addressable (OMP_CLAUSE_DECL (c));
5237               if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
5238                 cxx_mark_addressable (placeholder);
5239               tree omp_priv = convert_from_reference (OMP_CLAUSE_DECL (c));
5240               tree omp_orig = placeholder;
5241               if (need_static_cast)
5242                 {
5243                   if (i == 7)
5244                     {
5245                       error_at (OMP_CLAUSE_LOCATION (c),
5246                                 "user defined reduction with constructor "
5247                                 "initializer for base class %qT", atype);
5248                       return true;
5249                     }
5250                   tree rtype = build_reference_type (atype);
5251                   omp_priv = build_static_cast (rtype, omp_priv,
5252                                                 tf_warning_or_error);
5253                   omp_orig = build_static_cast (rtype, omp_orig,
5254                                                 tf_warning_or_error);
5255                   if (omp_priv == error_mark_node
5256                       || omp_orig == error_mark_node)
5257                     return true;
5258                   omp_priv = convert_from_reference (omp_priv);
5259                   omp_orig = convert_from_reference (omp_orig);
5260                 }
5261               if (i == 6)
5262                 *need_default_ctor = true;
5263               OMP_CLAUSE_REDUCTION_INIT (c)
5264                 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
5265                                  DECL_EXPR_DECL (stmts[3]),
5266                                  omp_priv, omp_orig);
5267               if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
5268                                 find_omp_placeholder_r, placeholder, NULL))
5269                 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
5270             }
5271           else if (i >= 3)
5272             {
5273               if (CLASS_TYPE_P (type) && !pod_type_p (type))
5274                 *need_default_ctor = true;
5275               else
5276                 {
5277                   tree init;
5278                   tree v = convert_from_reference (t);
5279                   if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
5280                     init = build_constructor (TREE_TYPE (v), NULL);
5281                   else
5282                     init = fold_convert (TREE_TYPE (v), integer_zero_node);
5283                   OMP_CLAUSE_REDUCTION_INIT (c)
5284                     = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
5285                 }
5286             }
5287         }
5288     }
5289   if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
5290     *need_dtor = true;
5291   else
5292     {
5293       error ("user defined reduction not found for %qD", t);
5294       return true;
5295     }
5296   return false;
5297 }
5298
5299 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
5300    Remove any elements from the list that are invalid.  */
5301
5302 tree
5303 finish_omp_clauses (tree clauses)
5304 {
5305   bitmap_head generic_head, firstprivate_head, lastprivate_head;
5306   bitmap_head aligned_head;
5307   tree c, t, *pc;
5308   bool branch_seen = false;
5309   bool copyprivate_seen = false;
5310
5311   bitmap_obstack_initialize (NULL);
5312   bitmap_initialize (&generic_head, &bitmap_default_obstack);
5313   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
5314   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
5315   bitmap_initialize (&aligned_head, &bitmap_default_obstack);
5316
5317   for (pc = &clauses, c = clauses; c ; c = *pc)
5318     {
5319       bool remove = false;
5320
5321       switch (OMP_CLAUSE_CODE (c))
5322         {
5323         case OMP_CLAUSE_SHARED:
5324           goto check_dup_generic;
5325         case OMP_CLAUSE_PRIVATE:
5326           goto check_dup_generic;
5327         case OMP_CLAUSE_REDUCTION:
5328           goto check_dup_generic;
5329         case OMP_CLAUSE_COPYPRIVATE:
5330           copyprivate_seen = true;
5331           goto check_dup_generic;
5332         case OMP_CLAUSE_COPYIN:
5333           goto check_dup_generic;
5334         case OMP_CLAUSE_LINEAR:
5335           t = OMP_CLAUSE_DECL (c);
5336           if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
5337               && !type_dependent_expression_p (t)
5338               && !INTEGRAL_TYPE_P (TREE_TYPE (t))
5339               && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
5340             {
5341               error ("linear clause applied to non-integral non-pointer "
5342                      "variable with %qT type", TREE_TYPE (t));
5343               remove = true;
5344               break;
5345             }
5346           t = OMP_CLAUSE_LINEAR_STEP (c);
5347           if (t == NULL_TREE)
5348             t = integer_one_node;
5349           if (t == error_mark_node)
5350             {
5351               remove = true;
5352               break;
5353             }
5354           else if (!type_dependent_expression_p (t)
5355                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5356             {
5357               error ("linear step expression must be integral");
5358               remove = true;
5359               break;
5360             }
5361           else
5362             {
5363               t = mark_rvalue_use (t);
5364               if (!processing_template_decl
5365                   && (VAR_P (OMP_CLAUSE_DECL (c))
5366                       || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
5367                 {
5368                   if (TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL)
5369                     t = maybe_constant_value (t);
5370                   t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5371                   if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c)))
5372                       == POINTER_TYPE)
5373                     {
5374                       t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
5375                                            OMP_CLAUSE_DECL (c), t);
5376                       t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
5377                                            MINUS_EXPR, sizetype, t,
5378                                            OMP_CLAUSE_DECL (c));
5379                       if (t == error_mark_node)
5380                         {
5381                           remove = true;
5382                           break;
5383                         }
5384                     }
5385                   else
5386                     t = fold_convert (TREE_TYPE (OMP_CLAUSE_DECL (c)), t);
5387                 }
5388               OMP_CLAUSE_LINEAR_STEP (c) = t;
5389             }
5390           goto check_dup_generic;
5391         check_dup_generic:
5392           t = OMP_CLAUSE_DECL (c);
5393           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5394             {
5395               if (processing_template_decl)
5396                 break;
5397               if (DECL_P (t))
5398                 error ("%qD is not a variable in clause %qs", t,
5399                        omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5400               else
5401                 error ("%qE is not a variable in clause %qs", t,
5402                        omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5403               remove = true;
5404             }
5405           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5406                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
5407                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5408             {
5409               error ("%qD appears more than once in data clauses", t);
5410               remove = true;
5411             }
5412           else
5413             bitmap_set_bit (&generic_head, DECL_UID (t));
5414           break;
5415
5416         case OMP_CLAUSE_FIRSTPRIVATE:
5417           t = OMP_CLAUSE_DECL (c);
5418           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5419             {
5420               if (processing_template_decl)
5421                 break;
5422               if (DECL_P (t))
5423                 error ("%qD is not a variable in clause %<firstprivate%>", t);
5424               else
5425                 error ("%qE is not a variable in clause %<firstprivate%>", t);
5426               remove = true;
5427             }
5428           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5429                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5430             {
5431               error ("%qD appears more than once in data clauses", t);
5432               remove = true;
5433             }
5434           else
5435             bitmap_set_bit (&firstprivate_head, DECL_UID (t));
5436           break;
5437
5438         case OMP_CLAUSE_LASTPRIVATE:
5439           t = OMP_CLAUSE_DECL (c);
5440           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5441             {
5442               if (processing_template_decl)
5443                 break;
5444               if (DECL_P (t))
5445                 error ("%qD is not a variable in clause %<lastprivate%>", t);
5446               else
5447                 error ("%qE is not a variable in clause %<lastprivate%>", t);
5448               remove = true;
5449             }
5450           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
5451                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
5452             {
5453               error ("%qD appears more than once in data clauses", t);
5454               remove = true;
5455             }
5456           else
5457             bitmap_set_bit (&lastprivate_head, DECL_UID (t));
5458           break;
5459
5460         case OMP_CLAUSE_IF:
5461           t = OMP_CLAUSE_IF_EXPR (c);
5462           t = maybe_convert_cond (t);
5463           if (t == error_mark_node)
5464             remove = true;
5465           else if (!processing_template_decl)
5466             t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5467           OMP_CLAUSE_IF_EXPR (c) = t;
5468           break;
5469
5470         case OMP_CLAUSE_FINAL:
5471           t = OMP_CLAUSE_FINAL_EXPR (c);
5472           t = maybe_convert_cond (t);
5473           if (t == error_mark_node)
5474             remove = true;
5475           else if (!processing_template_decl)
5476             t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5477           OMP_CLAUSE_FINAL_EXPR (c) = t;
5478           break;
5479
5480         case OMP_CLAUSE_NUM_THREADS:
5481           t = OMP_CLAUSE_NUM_THREADS_EXPR (c);
5482           if (t == error_mark_node)
5483             remove = true;
5484           else if (!type_dependent_expression_p (t)
5485                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5486             {
5487               error ("num_threads expression must be integral");
5488               remove = true;
5489             }
5490           else
5491             {
5492               t = mark_rvalue_use (t);
5493               if (!processing_template_decl)
5494                 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5495               OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
5496             }
5497           break;
5498
5499         case OMP_CLAUSE_SCHEDULE:
5500           t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
5501           if (t == NULL)
5502             ;
5503           else if (t == error_mark_node)
5504             remove = true;
5505           else if (!type_dependent_expression_p (t)
5506                    && (OMP_CLAUSE_SCHEDULE_KIND (c)
5507                        != OMP_CLAUSE_SCHEDULE_CILKFOR)
5508                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5509             {
5510               error ("schedule chunk size expression must be integral");
5511               remove = true;
5512             }
5513           else
5514             {
5515               t = mark_rvalue_use (t);
5516               if (!processing_template_decl)
5517                 {
5518                   if (OMP_CLAUSE_SCHEDULE_KIND (c)
5519                       == OMP_CLAUSE_SCHEDULE_CILKFOR)
5520                     {
5521                       t = convert_to_integer (long_integer_type_node, t);
5522                       if (t == error_mark_node)
5523                         {
5524                           remove = true;
5525                           break;
5526                         }
5527                     }
5528                   t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5529                 }
5530               OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
5531             }
5532           break;
5533
5534         case OMP_CLAUSE_SIMDLEN:
5535         case OMP_CLAUSE_SAFELEN:
5536           t = OMP_CLAUSE_OPERAND (c, 0);
5537           if (t == error_mark_node)
5538             remove = true;
5539           else if (!type_dependent_expression_p (t)
5540                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5541             {
5542               error ("%qs length expression must be integral",
5543                      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5544               remove = true;
5545             }
5546           else
5547             {
5548               t = mark_rvalue_use (t);
5549               t = maybe_constant_value (t);
5550               if (!processing_template_decl)
5551                 {
5552                   if (TREE_CODE (t) != INTEGER_CST
5553                       || tree_int_cst_sgn (t) != 1)
5554                     {
5555                       error ("%qs length expression must be positive constant"
5556                              " integer expression",
5557                              omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5558                       remove = true;
5559                     }
5560                 }
5561               OMP_CLAUSE_OPERAND (c, 0) = t;
5562             }
5563           break;
5564
5565         case OMP_CLAUSE_NUM_TEAMS:
5566           t = OMP_CLAUSE_NUM_TEAMS_EXPR (c);
5567           if (t == error_mark_node)
5568             remove = true;
5569           else if (!type_dependent_expression_p (t)
5570                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5571             {
5572               error ("%<num_teams%> expression must be integral");
5573               remove = true;
5574             }
5575           else
5576             {
5577               t = mark_rvalue_use (t);
5578               if (!processing_template_decl)
5579                 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5580               OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
5581             }
5582           break;
5583
5584         case OMP_CLAUSE_ASYNC:
5585           t = OMP_CLAUSE_ASYNC_EXPR (c);
5586           if (t == error_mark_node)
5587             remove = true;
5588           else if (!type_dependent_expression_p (t)
5589                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5590             {
5591               error ("%<async%> expression must be integral");
5592               remove = true;
5593             }
5594           else
5595             {
5596               t = mark_rvalue_use (t);
5597               if (!processing_template_decl)
5598                 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5599               OMP_CLAUSE_ASYNC_EXPR (c) = t;
5600             }
5601           break;
5602
5603         case OMP_CLAUSE_VECTOR_LENGTH:
5604           t = OMP_CLAUSE_VECTOR_LENGTH_EXPR (c);
5605           t = maybe_convert_cond (t);
5606           if (t == error_mark_node)
5607             remove = true;
5608           else if (!processing_template_decl)
5609             t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5610           OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
5611           break;
5612
5613         case OMP_CLAUSE_WAIT:
5614           t = OMP_CLAUSE_WAIT_EXPR (c);
5615           if (t == error_mark_node)
5616             remove = true;
5617           else if (!processing_template_decl)
5618             t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5619           OMP_CLAUSE_WAIT_EXPR (c) = t;
5620           break;
5621
5622         case OMP_CLAUSE_THREAD_LIMIT:
5623           t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
5624           if (t == error_mark_node)
5625             remove = true;
5626           else if (!type_dependent_expression_p (t)
5627                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5628             {
5629               error ("%<thread_limit%> expression must be integral");
5630               remove = true;
5631             }
5632           else
5633             {
5634               t = mark_rvalue_use (t);
5635               if (!processing_template_decl)
5636                 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5637               OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
5638             }
5639           break;
5640
5641         case OMP_CLAUSE_DEVICE:
5642           t = OMP_CLAUSE_DEVICE_ID (c);
5643           if (t == error_mark_node)
5644             remove = true;
5645           else if (!type_dependent_expression_p (t)
5646                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5647             {
5648               error ("%<device%> id must be integral");
5649               remove = true;
5650             }
5651           else
5652             {
5653               t = mark_rvalue_use (t);
5654               if (!processing_template_decl)
5655                 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5656               OMP_CLAUSE_DEVICE_ID (c) = t;
5657             }
5658           break;
5659
5660         case OMP_CLAUSE_DIST_SCHEDULE:
5661           t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
5662           if (t == NULL)
5663             ;
5664           else if (t == error_mark_node)
5665             remove = true;
5666           else if (!type_dependent_expression_p (t)
5667                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5668             {
5669               error ("%<dist_schedule%> chunk size expression must be "
5670                      "integral");
5671               remove = true;
5672             }
5673           else
5674             {
5675               t = mark_rvalue_use (t);
5676               if (!processing_template_decl)
5677                 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
5678               OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
5679             }
5680           break;
5681
5682         case OMP_CLAUSE_ALIGNED:
5683           t = OMP_CLAUSE_DECL (c);
5684           if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5685             {
5686               if (processing_template_decl)
5687                 break;
5688               if (DECL_P (t))
5689                 error ("%qD is not a variable in %<aligned%> clause", t);
5690               else
5691                 error ("%qE is not a variable in %<aligned%> clause", t);
5692               remove = true;
5693             }
5694           else if (!type_dependent_expression_p (t)
5695                    && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
5696                    && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5697                    && (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5698                        || (!POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
5699                            && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
5700                                != ARRAY_TYPE))))
5701             {
5702               error_at (OMP_CLAUSE_LOCATION (c),
5703                         "%qE in %<aligned%> clause is neither a pointer nor "
5704                         "an array nor a reference to pointer or array", t);
5705               remove = true;
5706             }
5707           else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
5708             {
5709               error ("%qD appears more than once in %<aligned%> clauses", t);
5710               remove = true;
5711             }
5712           else
5713             bitmap_set_bit (&aligned_head, DECL_UID (t));
5714           t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
5715           if (t == error_mark_node)
5716             remove = true;
5717           else if (t == NULL_TREE)
5718             break;
5719           else if (!type_dependent_expression_p (t)
5720                    && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5721             {
5722               error ("%<aligned%> clause alignment expression must "
5723                      "be integral");
5724               remove = true;
5725             }
5726           else
5727             {
5728               t = mark_rvalue_use (t);
5729               t = maybe_constant_value (t);
5730               if (!processing_template_decl)
5731                 {
5732                   if (TREE_CODE (t) != INTEGER_CST
5733                       || tree_int_cst_sgn (t) != 1)
5734                     {
5735                       error ("%<aligned%> clause alignment expression must be "
5736                              "positive constant integer expression");
5737                       remove = true;
5738                     }
5739                 }
5740               OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
5741             }
5742           break;
5743
5744         case OMP_CLAUSE_DEPEND:
5745           t = OMP_CLAUSE_DECL (c);
5746           if (TREE_CODE (t) == TREE_LIST)
5747             {
5748               if (handle_omp_array_sections (c))
5749                 remove = true;
5750               break;
5751             }
5752           if (t == error_mark_node)
5753             remove = true;
5754           else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5755             {
5756               if (processing_template_decl)
5757                 break;
5758               if (DECL_P (t))
5759                 error ("%qD is not a variable in %<depend%> clause", t);
5760               else
5761                 error ("%qE is not a variable in %<depend%> clause", t);
5762               remove = true;
5763             }
5764           else if (!processing_template_decl
5765                    && !cxx_mark_addressable (t))
5766             remove = true;
5767           break;
5768
5769         case OMP_CLAUSE_MAP:
5770         case OMP_CLAUSE_TO:
5771         case OMP_CLAUSE_FROM:
5772         case OMP_CLAUSE__CACHE_:
5773           t = OMP_CLAUSE_DECL (c);
5774           if (TREE_CODE (t) == TREE_LIST)
5775             {
5776               if (handle_omp_array_sections (c))
5777                 remove = true;
5778               else
5779                 {
5780                   t = OMP_CLAUSE_DECL (c);
5781                   if (TREE_CODE (t) != TREE_LIST
5782                       && !type_dependent_expression_p (t)
5783                       && !cp_omp_mappable_type (TREE_TYPE (t)))
5784                     {
5785                       error_at (OMP_CLAUSE_LOCATION (c),
5786                                 "array section does not have mappable type "
5787                                 "in %qs clause",
5788                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5789                       remove = true;
5790                     }
5791                 }
5792               break;
5793             }
5794           if (t == error_mark_node)
5795             remove = true;
5796           else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
5797             {
5798               if (processing_template_decl)
5799                 break;
5800               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5801                   && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5802                 break;
5803               if (DECL_P (t))
5804                 error ("%qD is not a variable in %qs clause", t,
5805                        omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5806               else
5807                 error ("%qE is not a variable in %qs clause", t,
5808                        omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5809               remove = true;
5810             }
5811           else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
5812             {
5813               error ("%qD is threadprivate variable in %qs clause", t,
5814                      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5815               remove = true;
5816             }
5817           else if (!processing_template_decl
5818                    && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE
5819                    && !cxx_mark_addressable (t))
5820             remove = true;
5821           else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5822                      && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
5823                    && !type_dependent_expression_p (t)
5824                    && !cp_omp_mappable_type ((TREE_CODE (TREE_TYPE (t))
5825                                               == REFERENCE_TYPE)
5826                                              ? TREE_TYPE (TREE_TYPE (t))
5827                                              : TREE_TYPE (t)))
5828             {
5829               error_at (OMP_CLAUSE_LOCATION (c),
5830                         "%qD does not have a mappable type in %qs clause", t,
5831                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5832               remove = true;
5833             }
5834           else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
5835             {
5836               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
5837                 error ("%qD appears more than once in motion clauses", t);
5838               else
5839                 error ("%qD appears more than once in map clauses", t);
5840               remove = true;
5841             }
5842           else
5843             bitmap_set_bit (&generic_head, DECL_UID (t));
5844           break;
5845
5846         case OMP_CLAUSE_UNIFORM:
5847           t = OMP_CLAUSE_DECL (c);
5848           if (TREE_CODE (t) != PARM_DECL)
5849             {
5850               if (processing_template_decl)
5851                 break;
5852               if (DECL_P (t))
5853                 error ("%qD is not an argument in %<uniform%> clause", t);
5854               else
5855                 error ("%qE is not an argument in %<uniform%> clause", t);
5856               remove = true;
5857               break;
5858             }
5859           goto check_dup_generic;
5860
5861         case OMP_CLAUSE_NOWAIT:
5862         case OMP_CLAUSE_ORDERED:
5863         case OMP_CLAUSE_DEFAULT:
5864         case OMP_CLAUSE_UNTIED:
5865         case OMP_CLAUSE_COLLAPSE:
5866         case OMP_CLAUSE_MERGEABLE:
5867         case OMP_CLAUSE_PARALLEL:
5868         case OMP_CLAUSE_FOR:
5869         case OMP_CLAUSE_SECTIONS:
5870         case OMP_CLAUSE_TASKGROUP:
5871         case OMP_CLAUSE_PROC_BIND:
5872         case OMP_CLAUSE__CILK_FOR_COUNT_:
5873           break;
5874
5875         case OMP_CLAUSE_INBRANCH:
5876         case OMP_CLAUSE_NOTINBRANCH:
5877           if (branch_seen)
5878             {
5879               error ("%<inbranch%> clause is incompatible with "
5880                      "%<notinbranch%>");
5881               remove = true;
5882             }
5883           branch_seen = true;
5884           break;
5885
5886         default:
5887           gcc_unreachable ();
5888         }
5889
5890       if (remove)
5891         *pc = OMP_CLAUSE_CHAIN (c);
5892       else
5893         pc = &OMP_CLAUSE_CHAIN (c);
5894     }
5895
5896   for (pc = &clauses, c = clauses; c ; c = *pc)
5897     {
5898       enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
5899       bool remove = false;
5900       bool need_complete_non_reference = false;
5901       bool need_default_ctor = false;
5902       bool need_copy_ctor = false;
5903       bool need_copy_assignment = false;
5904       bool need_implicitly_determined = false;
5905       bool need_dtor = false;
5906       tree type, inner_type;
5907
5908       switch (c_kind)
5909         {
5910         case OMP_CLAUSE_SHARED:
5911           need_implicitly_determined = true;
5912           break;
5913         case OMP_CLAUSE_PRIVATE:
5914           need_complete_non_reference = true;
5915           need_default_ctor = true;
5916           need_dtor = true;
5917           need_implicitly_determined = true;
5918           break;
5919         case OMP_CLAUSE_FIRSTPRIVATE:
5920           need_complete_non_reference = true;
5921           need_copy_ctor = true;
5922           need_dtor = true;
5923           need_implicitly_determined = true;
5924           break;
5925         case OMP_CLAUSE_LASTPRIVATE:
5926           need_complete_non_reference = true;
5927           need_copy_assignment = true;
5928           need_implicitly_determined = true;
5929           break;
5930         case OMP_CLAUSE_REDUCTION:
5931           need_implicitly_determined = true;
5932           break;
5933         case OMP_CLAUSE_COPYPRIVATE:
5934           need_copy_assignment = true;
5935           break;
5936         case OMP_CLAUSE_COPYIN:
5937           need_copy_assignment = true;
5938           break;
5939         case OMP_CLAUSE_NOWAIT:
5940           if (copyprivate_seen)
5941             {
5942               error_at (OMP_CLAUSE_LOCATION (c),
5943                         "%<nowait%> clause must not be used together "
5944                         "with %<copyprivate%>");
5945               *pc = OMP_CLAUSE_CHAIN (c);
5946               continue;
5947             }
5948           /* FALLTHRU */
5949         default:
5950           pc = &OMP_CLAUSE_CHAIN (c);
5951           continue;
5952         }
5953
5954       t = OMP_CLAUSE_DECL (c);
5955       if (processing_template_decl
5956           && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5957         {
5958           pc = &OMP_CLAUSE_CHAIN (c);
5959           continue;
5960         }
5961
5962       switch (c_kind)
5963         {
5964         case OMP_CLAUSE_LASTPRIVATE:
5965           if (!bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
5966             {
5967               need_default_ctor = true;
5968               need_dtor = true;
5969             }
5970           break;
5971
5972         case OMP_CLAUSE_REDUCTION:
5973           if (finish_omp_reduction_clause (c, &need_default_ctor,
5974                                            &need_dtor))
5975             remove = true;
5976           else
5977             t = OMP_CLAUSE_DECL (c);
5978           break;
5979
5980         case OMP_CLAUSE_COPYIN:
5981           if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
5982             {
5983               error ("%qE must be %<threadprivate%> for %<copyin%>", t);
5984               remove = true;
5985             }
5986           break;
5987
5988         default:
5989           break;
5990         }
5991
5992       if (need_complete_non_reference || need_copy_assignment)
5993         {
5994           t = require_complete_type (t);
5995           if (t == error_mark_node)
5996             remove = true;
5997           else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
5998                    && need_complete_non_reference)
5999             {
6000               error ("%qE has reference type for %qs", t,
6001                      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6002               remove = true;
6003             }
6004         }
6005       if (need_implicitly_determined)
6006         {
6007           const char *share_name = NULL;
6008
6009           if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
6010             share_name = "threadprivate";
6011           else switch (cxx_omp_predetermined_sharing (t))
6012             {
6013             case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
6014               break;
6015             case OMP_CLAUSE_DEFAULT_SHARED:
6016               /* const vars may be specified in firstprivate clause.  */
6017               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
6018                   && cxx_omp_const_qual_no_mutable (t))
6019                 break;
6020               share_name = "shared";
6021               break;
6022             case OMP_CLAUSE_DEFAULT_PRIVATE:
6023               share_name = "private";
6024               break;
6025             default:
6026               gcc_unreachable ();
6027             }
6028           if (share_name)
6029             {
6030               error ("%qE is predetermined %qs for %qs",
6031                      t, share_name, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6032               remove = true;
6033             }
6034         }
6035
6036       /* We're interested in the base element, not arrays.  */
6037       inner_type = type = TREE_TYPE (t);
6038       while (TREE_CODE (inner_type) == ARRAY_TYPE)
6039         inner_type = TREE_TYPE (inner_type);
6040
6041       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6042           && TREE_CODE (inner_type) == REFERENCE_TYPE)
6043         inner_type = TREE_TYPE (inner_type);
6044
6045       /* Check for special function availability by building a call to one.
6046          Save the results, because later we won't be in the right context
6047          for making these queries.  */
6048       if (CLASS_TYPE_P (inner_type)
6049           && COMPLETE_TYPE_P (inner_type)
6050           && (need_default_ctor || need_copy_ctor
6051               || need_copy_assignment || need_dtor)
6052           && !type_dependent_expression_p (t)
6053           && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
6054                                          need_copy_ctor, need_copy_assignment,
6055                                          need_dtor))
6056         remove = true;
6057
6058       if (remove)
6059         *pc = OMP_CLAUSE_CHAIN (c);
6060       else
6061         pc = &OMP_CLAUSE_CHAIN (c);
6062     }
6063
6064   bitmap_obstack_release (NULL);
6065   return clauses;
6066 }
6067
6068 /* For all variables in the tree_list VARS, mark them as thread local.  */
6069
6070 void
6071 finish_omp_threadprivate (tree vars)
6072 {
6073   tree t;
6074
6075   /* Mark every variable in VARS to be assigned thread local storage.  */
6076   for (t = vars; t; t = TREE_CHAIN (t))
6077     {
6078       tree v = TREE_PURPOSE (t);
6079
6080       if (error_operand_p (v))
6081         ;
6082       else if (!VAR_P (v))
6083         error ("%<threadprivate%> %qD is not file, namespace "
6084                "or block scope variable", v);
6085       /* If V had already been marked threadprivate, it doesn't matter
6086          whether it had been used prior to this point.  */
6087       else if (TREE_USED (v)
6088           && (DECL_LANG_SPECIFIC (v) == NULL
6089               || !CP_DECL_THREADPRIVATE_P (v)))
6090         error ("%qE declared %<threadprivate%> after first use", v);
6091       else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
6092         error ("automatic variable %qE cannot be %<threadprivate%>", v);
6093       else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
6094         error ("%<threadprivate%> %qE has incomplete type", v);
6095       else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
6096                && CP_DECL_CONTEXT (v) != current_class_type)
6097         error ("%<threadprivate%> %qE directive not "
6098                "in %qT definition", v, CP_DECL_CONTEXT (v));
6099       else
6100         {
6101           /* Allocate a LANG_SPECIFIC structure for V, if needed.  */
6102           if (DECL_LANG_SPECIFIC (v) == NULL)
6103             {
6104               retrofit_lang_decl (v);
6105
6106               /* Make sure that DECL_DISCRIMINATOR_P continues to be true
6107                  after the allocation of the lang_decl structure.  */
6108               if (DECL_DISCRIMINATOR_P (v))
6109                 DECL_LANG_SPECIFIC (v)->u.base.u2sel = 1;
6110             }
6111
6112           if (! DECL_THREAD_LOCAL_P (v))
6113             {
6114               set_decl_tls_model (v, decl_default_tls_model (v));
6115               /* If rtl has been already set for this var, call
6116                  make_decl_rtl once again, so that encode_section_info
6117                  has a chance to look at the new decl flags.  */
6118               if (DECL_RTL_SET_P (v))
6119                 make_decl_rtl (v);
6120             }
6121           CP_DECL_THREADPRIVATE_P (v) = 1;
6122         }
6123     }
6124 }
6125
6126 /* Build an OpenMP structured block.  */
6127
6128 tree
6129 begin_omp_structured_block (void)
6130 {
6131   return do_pushlevel (sk_omp);
6132 }
6133
6134 tree
6135 finish_omp_structured_block (tree block)
6136 {
6137   return do_poplevel (block);
6138 }
6139
6140 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
6141    statement.  LOC is the location of the OACC_DATA.  */
6142
6143 tree
6144 finish_oacc_data (tree clauses, tree block)
6145 {
6146   tree stmt;
6147
6148   block = finish_omp_structured_block (block);
6149
6150   stmt = make_node (OACC_DATA);
6151   TREE_TYPE (stmt) = void_type_node;
6152   OACC_DATA_CLAUSES (stmt) = clauses;
6153   OACC_DATA_BODY (stmt) = block;
6154
6155   return add_stmt (stmt);
6156 }
6157
6158 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
6159    statement.  LOC is the location of the OACC_KERNELS.  */
6160
6161 tree
6162 finish_oacc_kernels (tree clauses, tree block)
6163 {
6164   tree stmt;
6165
6166   block = finish_omp_structured_block (block);
6167
6168   stmt = make_node (OACC_KERNELS);
6169   TREE_TYPE (stmt) = void_type_node;
6170   OACC_KERNELS_CLAUSES (stmt) = clauses;
6171   OACC_KERNELS_BODY (stmt) = block;
6172
6173   return add_stmt (stmt);
6174 }
6175
6176 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
6177    statement.  LOC is the location of the OACC_PARALLEL.  */
6178
6179 tree
6180 finish_oacc_parallel (tree clauses, tree block)
6181 {
6182   tree stmt;
6183
6184   block = finish_omp_structured_block (block);
6185
6186   stmt = make_node (OACC_PARALLEL);
6187   TREE_TYPE (stmt) = void_type_node;
6188   OACC_PARALLEL_CLAUSES (stmt) = clauses;
6189   OACC_PARALLEL_BODY (stmt) = block;
6190
6191   return add_stmt (stmt);
6192 }
6193
6194 /* Similarly, except force the retention of the BLOCK.  */
6195
6196 tree
6197 begin_omp_parallel (void)
6198 {
6199   keep_next_level (true);
6200   return begin_omp_structured_block ();
6201 }
6202
6203 tree
6204 finish_omp_parallel (tree clauses, tree body)
6205 {
6206   tree stmt;
6207
6208   body = finish_omp_structured_block (body);
6209
6210   stmt = make_node (OMP_PARALLEL);
6211   TREE_TYPE (stmt) = void_type_node;
6212   OMP_PARALLEL_CLAUSES (stmt) = clauses;
6213   OMP_PARALLEL_BODY (stmt) = body;
6214
6215   return add_stmt (stmt);
6216 }
6217
6218 tree
6219 begin_omp_task (void)
6220 {
6221   keep_next_level (true);
6222   return begin_omp_structured_block ();
6223 }
6224
6225 tree
6226 finish_omp_task (tree clauses, tree body)
6227 {
6228   tree stmt;
6229
6230   body = finish_omp_structured_block (body);
6231
6232   stmt = make_node (OMP_TASK);
6233   TREE_TYPE (stmt) = void_type_node;
6234   OMP_TASK_CLAUSES (stmt) = clauses;
6235   OMP_TASK_BODY (stmt) = body;
6236
6237   return add_stmt (stmt);
6238 }
6239
6240 /* Helper function for finish_omp_for.  Convert Ith random access iterator
6241    into integral iterator.  Return FALSE if successful.  */
6242
6243 static bool
6244 handle_omp_for_class_iterator (int i, location_t locus, tree declv, tree initv,
6245                                tree condv, tree incrv, tree *body,
6246                                tree *pre_body, tree clauses, tree *lastp)
6247 {
6248   tree diff, iter_init, iter_incr = NULL, last;
6249   tree incr_var = NULL, orig_pre_body, orig_body, c;
6250   tree decl = TREE_VEC_ELT (declv, i);
6251   tree init = TREE_VEC_ELT (initv, i);
6252   tree cond = TREE_VEC_ELT (condv, i);
6253   tree incr = TREE_VEC_ELT (incrv, i);
6254   tree iter = decl;
6255   location_t elocus = locus;
6256
6257   if (init && EXPR_HAS_LOCATION (init))
6258     elocus = EXPR_LOCATION (init);
6259
6260   switch (TREE_CODE (cond))
6261     {
6262     case GT_EXPR:
6263     case GE_EXPR:
6264     case LT_EXPR:
6265     case LE_EXPR:
6266     case NE_EXPR:
6267       if (TREE_OPERAND (cond, 1) == iter)
6268         cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
6269                        TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
6270       if (TREE_OPERAND (cond, 0) != iter)
6271         cond = error_mark_node;
6272       else
6273         {
6274           tree tem = build_x_binary_op (EXPR_LOCATION (cond),
6275                                         TREE_CODE (cond),
6276                                         iter, ERROR_MARK,
6277                                         TREE_OPERAND (cond, 1), ERROR_MARK,
6278                                         NULL, tf_warning_or_error);
6279           if (error_operand_p (tem))
6280             return true;
6281         }
6282       break;
6283     default:
6284       cond = error_mark_node;
6285       break;
6286     }
6287   if (cond == error_mark_node)
6288     {
6289       error_at (elocus, "invalid controlling predicate");
6290       return true;
6291     }
6292   diff = build_x_binary_op (elocus, MINUS_EXPR, TREE_OPERAND (cond, 1),
6293                             ERROR_MARK, iter, ERROR_MARK, NULL,
6294                             tf_warning_or_error);
6295   if (error_operand_p (diff))
6296     return true;
6297   if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
6298     {
6299       error_at (elocus, "difference between %qE and %qD does not have integer type",
6300                 TREE_OPERAND (cond, 1), iter);
6301       return true;
6302     }
6303
6304   switch (TREE_CODE (incr))
6305     {
6306     case PREINCREMENT_EXPR:
6307     case PREDECREMENT_EXPR:
6308     case POSTINCREMENT_EXPR:
6309     case POSTDECREMENT_EXPR:
6310       if (TREE_OPERAND (incr, 0) != iter)
6311         {
6312           incr = error_mark_node;
6313           break;
6314         }
6315       iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
6316                                     TREE_CODE (incr), iter,
6317                                     tf_warning_or_error);
6318       if (error_operand_p (iter_incr))
6319         return true;
6320       else if (TREE_CODE (incr) == PREINCREMENT_EXPR
6321                || TREE_CODE (incr) == POSTINCREMENT_EXPR)
6322         incr = integer_one_node;
6323       else
6324         incr = integer_minus_one_node;
6325       break;
6326     case MODIFY_EXPR:
6327       if (TREE_OPERAND (incr, 0) != iter)
6328         incr = error_mark_node;
6329       else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
6330                || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
6331         {
6332           tree rhs = TREE_OPERAND (incr, 1);
6333           if (TREE_OPERAND (rhs, 0) == iter)
6334             {
6335               if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
6336                   != INTEGER_TYPE)
6337                 incr = error_mark_node;
6338               else
6339                 {
6340                   iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6341                                                    iter, TREE_CODE (rhs),
6342                                                    TREE_OPERAND (rhs, 1),
6343                                                    tf_warning_or_error);
6344                   if (error_operand_p (iter_incr))
6345                     return true;
6346                   incr = TREE_OPERAND (rhs, 1);
6347                   incr = cp_convert (TREE_TYPE (diff), incr,
6348                                      tf_warning_or_error);
6349                   if (TREE_CODE (rhs) == MINUS_EXPR)
6350                     {
6351                       incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
6352                       incr = fold_if_not_in_template (incr);
6353                     }
6354                   if (TREE_CODE (incr) != INTEGER_CST
6355                       && (TREE_CODE (incr) != NOP_EXPR
6356                           || (TREE_CODE (TREE_OPERAND (incr, 0))
6357                               != INTEGER_CST)))
6358                     iter_incr = NULL;
6359                 }
6360             }
6361           else if (TREE_OPERAND (rhs, 1) == iter)
6362             {
6363               if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
6364                   || TREE_CODE (rhs) != PLUS_EXPR)
6365                 incr = error_mark_node;
6366               else
6367                 {
6368                   iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
6369                                                  PLUS_EXPR,
6370                                                  TREE_OPERAND (rhs, 0),
6371                                                  ERROR_MARK, iter,
6372                                                  ERROR_MARK, NULL,
6373                                                  tf_warning_or_error);
6374                   if (error_operand_p (iter_incr))
6375                     return true;
6376                   iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
6377                                                    iter, NOP_EXPR,
6378                                                    iter_incr,
6379                                                    tf_warning_or_error);
6380                   if (error_operand_p (iter_incr))
6381                     return true;
6382                   incr = TREE_OPERAND (rhs, 0);
6383                   iter_incr = NULL;
6384                 }
6385             }
6386           else
6387             incr = error_mark_node;
6388         }
6389       else
6390         incr = error_mark_node;
6391       break;
6392     default:
6393       incr = error_mark_node;
6394       break;
6395     }
6396
6397   if (incr == error_mark_node)
6398     {
6399       error_at (elocus, "invalid increment expression");
6400       return true;
6401     }
6402
6403   incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
6404   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6405     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
6406         && OMP_CLAUSE_DECL (c) == iter)
6407       break;
6408
6409   decl = create_temporary_var (TREE_TYPE (diff));
6410   pushdecl (decl);
6411   add_decl_expr (decl);
6412   last = create_temporary_var (TREE_TYPE (diff));
6413   pushdecl (last);
6414   add_decl_expr (last);
6415   if (c && iter_incr == NULL)
6416     {
6417       incr_var = create_temporary_var (TREE_TYPE (diff));
6418       pushdecl (incr_var);
6419       add_decl_expr (incr_var);
6420     }
6421   gcc_assert (stmts_are_full_exprs_p ());
6422
6423   orig_pre_body = *pre_body;
6424   *pre_body = push_stmt_list ();
6425   if (orig_pre_body)
6426     add_stmt (orig_pre_body);
6427   if (init != NULL)
6428     finish_expr_stmt (build_x_modify_expr (elocus,
6429                                            iter, NOP_EXPR, init,
6430                                            tf_warning_or_error));
6431   init = build_int_cst (TREE_TYPE (diff), 0);
6432   if (c && iter_incr == NULL)
6433     {
6434       finish_expr_stmt (build_x_modify_expr (elocus,
6435                                              incr_var, NOP_EXPR,
6436                                              incr, tf_warning_or_error));
6437       incr = incr_var;
6438       iter_incr = build_x_modify_expr (elocus,
6439                                        iter, PLUS_EXPR, incr,
6440                                        tf_warning_or_error);
6441     }
6442   finish_expr_stmt (build_x_modify_expr (elocus,
6443                                          last, NOP_EXPR, init,
6444                                          tf_warning_or_error));
6445   *pre_body = pop_stmt_list (*pre_body);
6446
6447   cond = cp_build_binary_op (elocus,
6448                              TREE_CODE (cond), decl, diff,
6449                              tf_warning_or_error);
6450   incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
6451                             elocus, incr, NULL_TREE);
6452
6453   orig_body = *body;
6454   *body = push_stmt_list ();
6455   iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
6456   iter_init = build_x_modify_expr (elocus,
6457                                    iter, PLUS_EXPR, iter_init,
6458                                    tf_warning_or_error);
6459   if (iter_init != error_mark_node)
6460     iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
6461   finish_expr_stmt (iter_init);
6462   finish_expr_stmt (build_x_modify_expr (elocus,
6463                                          last, NOP_EXPR, decl,
6464                                          tf_warning_or_error));
6465   add_stmt (orig_body);
6466   *body = pop_stmt_list (*body);
6467
6468   if (c)
6469     {
6470       OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
6471       finish_expr_stmt (iter_incr);
6472       OMP_CLAUSE_LASTPRIVATE_STMT (c)
6473         = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
6474     }
6475
6476   TREE_VEC_ELT (declv, i) = decl;
6477   TREE_VEC_ELT (initv, i) = init;
6478   TREE_VEC_ELT (condv, i) = cond;
6479   TREE_VEC_ELT (incrv, i) = incr;
6480   *lastp = last;
6481
6482   return false;
6483 }
6484
6485 /* Build and validate an OMP_FOR statement.  CLAUSES, BODY, COND, INCR
6486    are directly for their associated operands in the statement.  DECL
6487    and INIT are a combo; if DECL is NULL then INIT ought to be a
6488    MODIFY_EXPR, and the DECL should be extracted.  PRE_BODY are
6489    optional statements that need to go before the loop into its
6490    sk_omp scope.  */
6491
6492 tree
6493 finish_omp_for (location_t locus, enum tree_code code, tree declv, tree initv,
6494                 tree condv, tree incrv, tree body, tree pre_body, tree clauses)
6495 {
6496   tree omp_for = NULL, orig_incr = NULL;
6497   tree decl = NULL, init, cond, incr, orig_decl = NULL_TREE, block = NULL_TREE;
6498   tree last = NULL_TREE;
6499   location_t elocus;
6500   int i;
6501
6502   gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
6503   gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
6504   gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
6505   for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6506     {
6507       decl = TREE_VEC_ELT (declv, i);
6508       init = TREE_VEC_ELT (initv, i);
6509       cond = TREE_VEC_ELT (condv, i);
6510       incr = TREE_VEC_ELT (incrv, i);
6511       elocus = locus;
6512
6513       if (decl == NULL)
6514         {
6515           if (init != NULL)
6516             switch (TREE_CODE (init))
6517               {
6518               case MODIFY_EXPR:
6519                 decl = TREE_OPERAND (init, 0);
6520                 init = TREE_OPERAND (init, 1);
6521                 break;
6522               case MODOP_EXPR:
6523                 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
6524                   {
6525                     decl = TREE_OPERAND (init, 0);
6526                     init = TREE_OPERAND (init, 2);
6527                   }
6528                 break;
6529               default:
6530                 break;
6531               }
6532
6533           if (decl == NULL)
6534             {
6535               error_at (locus,
6536                         "expected iteration declaration or initialization");
6537               return NULL;
6538             }
6539         }
6540
6541       if (init && EXPR_HAS_LOCATION (init))
6542         elocus = EXPR_LOCATION (init);
6543
6544       if (cond == NULL)
6545         {
6546           error_at (elocus, "missing controlling predicate");
6547           return NULL;
6548         }
6549
6550       if (incr == NULL)
6551         {
6552           error_at (elocus, "missing increment expression");
6553           return NULL;
6554         }
6555
6556       TREE_VEC_ELT (declv, i) = decl;
6557       TREE_VEC_ELT (initv, i) = init;
6558     }
6559
6560   if (dependent_omp_for_p (declv, initv, condv, incrv))
6561     {
6562       tree stmt;
6563
6564       stmt = make_node (code);
6565
6566       for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
6567         {
6568           /* This is really just a place-holder.  We'll be decomposing this
6569              again and going through the cp_build_modify_expr path below when
6570              we instantiate the thing.  */
6571           TREE_VEC_ELT (initv, i)
6572             = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
6573                       TREE_VEC_ELT (initv, i));
6574         }
6575
6576       TREE_TYPE (stmt) = void_type_node;
6577       OMP_FOR_INIT (stmt) = initv;
6578       OMP_FOR_COND (stmt) = condv;
6579       OMP_FOR_INCR (stmt) = incrv;
6580       OMP_FOR_BODY (stmt) = body;
6581       OMP_FOR_PRE_BODY (stmt) = pre_body;
6582       OMP_FOR_CLAUSES (stmt) = clauses;
6583
6584       SET_EXPR_LOCATION (stmt, locus);
6585       return add_stmt (stmt);
6586     }
6587
6588   if (processing_template_decl)
6589     orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
6590
6591   for (i = 0; i < TREE_VEC_LENGTH (declv); )
6592     {
6593       decl = TREE_VEC_ELT (declv, i);
6594       init = TREE_VEC_ELT (initv, i);
6595       cond = TREE_VEC_ELT (condv, i);
6596       incr = TREE_VEC_ELT (incrv, i);
6597       if (orig_incr)
6598         TREE_VEC_ELT (orig_incr, i) = incr;
6599       elocus = locus;
6600
6601       if (init && EXPR_HAS_LOCATION (init))
6602         elocus = EXPR_LOCATION (init);
6603
6604       if (!DECL_P (decl))
6605         {
6606           error_at (elocus, "expected iteration declaration or initialization");
6607           return NULL;
6608         }
6609
6610       if (incr && TREE_CODE (incr) == MODOP_EXPR)
6611         {
6612           if (orig_incr)
6613             TREE_VEC_ELT (orig_incr, i) = incr;
6614           incr = cp_build_modify_expr (TREE_OPERAND (incr, 0),
6615                                        TREE_CODE (TREE_OPERAND (incr, 1)),
6616                                        TREE_OPERAND (incr, 2),
6617                                        tf_warning_or_error);
6618         }
6619
6620       if (CLASS_TYPE_P (TREE_TYPE (decl)))
6621         {
6622           if (code == OMP_SIMD)
6623             {
6624               error_at (elocus, "%<#pragma omp simd%> used with class "
6625                                 "iteration variable %qE", decl);
6626               return NULL;
6627             }
6628           if (code == CILK_FOR && i == 0)
6629             orig_decl = decl;
6630           if (handle_omp_for_class_iterator (i, locus, declv, initv, condv,
6631                                              incrv, &body, &pre_body,
6632                                              clauses, &last))
6633             return NULL;
6634           continue;
6635         }
6636
6637       if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
6638           && !TYPE_PTR_P (TREE_TYPE (decl)))
6639         {
6640           error_at (elocus, "invalid type for iteration variable %qE", decl);
6641           return NULL;
6642         }
6643
6644       if (!processing_template_decl)
6645         {
6646           init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
6647           init = cp_build_modify_expr (decl, NOP_EXPR, init, tf_warning_or_error);
6648         }
6649       else
6650         init = build2 (MODIFY_EXPR, void_type_node, decl, init);
6651       if (cond
6652           && TREE_SIDE_EFFECTS (cond)
6653           && COMPARISON_CLASS_P (cond)
6654           && !processing_template_decl)
6655         {
6656           tree t = TREE_OPERAND (cond, 0);
6657           if (TREE_SIDE_EFFECTS (t)
6658               && t != decl
6659               && (TREE_CODE (t) != NOP_EXPR
6660                   || TREE_OPERAND (t, 0) != decl))
6661             TREE_OPERAND (cond, 0)
6662               = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6663
6664           t = TREE_OPERAND (cond, 1);
6665           if (TREE_SIDE_EFFECTS (t)
6666               && t != decl
6667               && (TREE_CODE (t) != NOP_EXPR
6668                   || TREE_OPERAND (t, 0) != decl))
6669             TREE_OPERAND (cond, 1)
6670               = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6671         }
6672       if (decl == error_mark_node || init == error_mark_node)
6673         return NULL;
6674
6675       TREE_VEC_ELT (declv, i) = decl;
6676       TREE_VEC_ELT (initv, i) = init;
6677       TREE_VEC_ELT (condv, i) = cond;
6678       TREE_VEC_ELT (incrv, i) = incr;
6679       i++;
6680     }
6681
6682   if (IS_EMPTY_STMT (pre_body))
6683     pre_body = NULL;
6684
6685   if (code == CILK_FOR && !processing_template_decl)
6686     block = push_stmt_list ();
6687
6688   omp_for = c_finish_omp_for (locus, code, declv, initv, condv, incrv,
6689                               body, pre_body);
6690
6691   if (omp_for == NULL)
6692     {
6693       if (block)
6694         pop_stmt_list (block);
6695       return NULL;
6696     }
6697
6698   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
6699     {
6700       decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i), 0);
6701       incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
6702
6703       if (TREE_CODE (incr) != MODIFY_EXPR)
6704         continue;
6705
6706       if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
6707           && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
6708           && !processing_template_decl)
6709         {
6710           tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
6711           if (TREE_SIDE_EFFECTS (t)
6712               && t != decl
6713               && (TREE_CODE (t) != NOP_EXPR
6714                   || TREE_OPERAND (t, 0) != decl))
6715             TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
6716               = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6717
6718           t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6719           if (TREE_SIDE_EFFECTS (t)
6720               && t != decl
6721               && (TREE_CODE (t) != NOP_EXPR
6722                   || TREE_OPERAND (t, 0) != decl))
6723             TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6724               = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
6725         }
6726
6727       if (orig_incr)
6728         TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
6729     }
6730   OMP_FOR_CLAUSES (omp_for) = clauses;
6731
6732   if (block)
6733     {
6734       tree omp_par = make_node (OMP_PARALLEL);
6735       TREE_TYPE (omp_par) = void_type_node;
6736       OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
6737       tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6738       TREE_SIDE_EFFECTS (bind) = 1;
6739       BIND_EXPR_BODY (bind) = pop_stmt_list (block);
6740       OMP_PARALLEL_BODY (omp_par) = bind;
6741       if (OMP_FOR_PRE_BODY (omp_for))
6742         {
6743           add_stmt (OMP_FOR_PRE_BODY (omp_for));
6744           OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
6745         }
6746       init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
6747       decl = TREE_OPERAND (init, 0);
6748       cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
6749       incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
6750       tree t = TREE_OPERAND (cond, 1), c, clauses, *pc;
6751       clauses = OMP_FOR_CLAUSES (omp_for);
6752       OMP_FOR_CLAUSES (omp_for) = NULL_TREE;
6753       for (pc = &clauses; *pc; )
6754         if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_SCHEDULE)
6755           {
6756             gcc_assert (OMP_FOR_CLAUSES (omp_for) == NULL_TREE);
6757             OMP_FOR_CLAUSES (omp_for) = *pc;
6758             *pc = OMP_CLAUSE_CHAIN (*pc);
6759             OMP_CLAUSE_CHAIN (OMP_FOR_CLAUSES (omp_for)) = NULL_TREE;
6760           }
6761         else
6762           {
6763             gcc_assert (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE);
6764             pc = &OMP_CLAUSE_CHAIN (*pc);
6765           }
6766       if (TREE_CODE (t) != INTEGER_CST)
6767         {
6768           TREE_OPERAND (cond, 1) = get_temp_regvar (TREE_TYPE (t), t);
6769           c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6770           OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
6771           OMP_CLAUSE_CHAIN (c) = clauses;
6772           clauses = c;
6773         }
6774       if (TREE_CODE (incr) == MODIFY_EXPR)
6775         {
6776           t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6777           if (TREE_CODE (t) != INTEGER_CST)
6778             {
6779               TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
6780                 = get_temp_regvar (TREE_TYPE (t), t);
6781               c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6782               OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
6783               OMP_CLAUSE_CHAIN (c) = clauses;
6784               clauses = c;
6785             }
6786         }
6787       t = TREE_OPERAND (init, 1);
6788       if (TREE_CODE (t) != INTEGER_CST)
6789         {
6790           TREE_OPERAND (init, 1) = get_temp_regvar (TREE_TYPE (t), t);
6791           c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6792           OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
6793           OMP_CLAUSE_CHAIN (c) = clauses;
6794           clauses = c;
6795         }
6796       if (orig_decl && orig_decl != decl)
6797         {
6798           c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6799           OMP_CLAUSE_DECL (c) = orig_decl;
6800           OMP_CLAUSE_CHAIN (c) = clauses;
6801           clauses = c;
6802         }
6803       if (last)
6804         {
6805           c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6806           OMP_CLAUSE_DECL (c) = last;
6807           OMP_CLAUSE_CHAIN (c) = clauses;
6808           clauses = c;
6809         }
6810       c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
6811       OMP_CLAUSE_DECL (c) = decl;
6812       OMP_CLAUSE_CHAIN (c) = clauses;
6813       clauses = c;
6814       c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
6815       OMP_CLAUSE_OPERAND (c, 0)
6816         = cilk_for_number_of_iterations (omp_for);
6817       OMP_CLAUSE_CHAIN (c) = clauses;
6818       OMP_PARALLEL_CLAUSES (omp_par) = finish_omp_clauses (c);
6819       add_stmt (omp_par);
6820       return omp_par;
6821     }
6822   else if (code == CILK_FOR && processing_template_decl)
6823     {
6824       tree c, clauses = OMP_FOR_CLAUSES (omp_for);
6825       if (orig_decl && orig_decl != decl)
6826         {
6827           c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6828           OMP_CLAUSE_DECL (c) = orig_decl;
6829           OMP_CLAUSE_CHAIN (c) = clauses;
6830           clauses = c;
6831         }
6832       if (last)
6833         {
6834           c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
6835           OMP_CLAUSE_DECL (c) = last;
6836           OMP_CLAUSE_CHAIN (c) = clauses;
6837           clauses = c;
6838         }
6839       OMP_FOR_CLAUSES (omp_for) = clauses;
6840     }
6841   return omp_for;
6842 }
6843
6844 void
6845 finish_omp_atomic (enum tree_code code, enum tree_code opcode, tree lhs,
6846                    tree rhs, tree v, tree lhs1, tree rhs1, bool seq_cst)
6847 {
6848   tree orig_lhs;
6849   tree orig_rhs;
6850   tree orig_v;
6851   tree orig_lhs1;
6852   tree orig_rhs1;
6853   bool dependent_p;
6854   tree stmt;
6855
6856   orig_lhs = lhs;
6857   orig_rhs = rhs;
6858   orig_v = v;
6859   orig_lhs1 = lhs1;
6860   orig_rhs1 = rhs1;
6861   dependent_p = false;
6862   stmt = NULL_TREE;
6863
6864   /* Even in a template, we can detect invalid uses of the atomic
6865      pragma if neither LHS nor RHS is type-dependent.  */
6866   if (processing_template_decl)
6867     {
6868       dependent_p = (type_dependent_expression_p (lhs)
6869                      || (rhs && type_dependent_expression_p (rhs))
6870                      || (v && type_dependent_expression_p (v))
6871                      || (lhs1 && type_dependent_expression_p (lhs1))
6872                      || (rhs1 && type_dependent_expression_p (rhs1)));
6873       if (!dependent_p)
6874         {
6875           lhs = build_non_dependent_expr (lhs);
6876           if (rhs)
6877             rhs = build_non_dependent_expr (rhs);
6878           if (v)
6879             v = build_non_dependent_expr (v);
6880           if (lhs1)
6881             lhs1 = build_non_dependent_expr (lhs1);
6882           if (rhs1)
6883             rhs1 = build_non_dependent_expr (rhs1);
6884         }
6885     }
6886   if (!dependent_p)
6887     {
6888       bool swapped = false;
6889       if (rhs1 && cp_tree_equal (lhs, rhs))
6890         {
6891           tree tem = rhs;
6892           rhs = rhs1;
6893           rhs1 = tem;
6894           swapped = !commutative_tree_code (opcode);
6895         }
6896       if (rhs1 && !cp_tree_equal (lhs, rhs1))
6897         {
6898           if (code == OMP_ATOMIC)
6899             error ("%<#pragma omp atomic update%> uses two different "
6900                    "expressions for memory");
6901           else
6902             error ("%<#pragma omp atomic capture%> uses two different "
6903                    "expressions for memory");
6904           return;
6905         }
6906       if (lhs1 && !cp_tree_equal (lhs, lhs1))
6907         {
6908           if (code == OMP_ATOMIC)
6909             error ("%<#pragma omp atomic update%> uses two different "
6910                    "expressions for memory");
6911           else
6912             error ("%<#pragma omp atomic capture%> uses two different "
6913                    "expressions for memory");
6914           return;
6915         }
6916       stmt = c_finish_omp_atomic (input_location, code, opcode, lhs, rhs,
6917                                   v, lhs1, rhs1, swapped, seq_cst);
6918       if (stmt == error_mark_node)
6919         return;
6920     }
6921   if (processing_template_decl)
6922     {
6923       if (code == OMP_ATOMIC_READ)
6924         {
6925           stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs),
6926                                    OMP_ATOMIC_READ, orig_lhs);
6927           OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6928           stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6929         }
6930       else
6931         {
6932           if (opcode == NOP_EXPR)
6933             stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
6934           else 
6935             stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
6936           if (orig_rhs1)
6937             stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
6938                                      COMPOUND_EXPR, orig_rhs1, stmt);
6939           if (code != OMP_ATOMIC)
6940             {
6941               stmt = build_min_nt_loc (EXPR_LOCATION (orig_lhs1),
6942                                        code, orig_lhs1, stmt);
6943               OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6944               stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
6945             }
6946         }
6947       stmt = build2 (OMP_ATOMIC, void_type_node, integer_zero_node, stmt);
6948       OMP_ATOMIC_SEQ_CST (stmt) = seq_cst;
6949     }
6950   finish_expr_stmt (stmt);
6951 }
6952
6953 void
6954 finish_omp_barrier (void)
6955 {
6956   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
6957   vec<tree, va_gc> *vec = make_tree_vector ();
6958   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6959   release_tree_vector (vec);
6960   finish_expr_stmt (stmt);
6961 }
6962
6963 void
6964 finish_omp_flush (void)
6965 {
6966   tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
6967   vec<tree, va_gc> *vec = make_tree_vector ();
6968   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6969   release_tree_vector (vec);
6970   finish_expr_stmt (stmt);
6971 }
6972
6973 void
6974 finish_omp_taskwait (void)
6975 {
6976   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
6977   vec<tree, va_gc> *vec = make_tree_vector ();
6978   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6979   release_tree_vector (vec);
6980   finish_expr_stmt (stmt);
6981 }
6982
6983 void
6984 finish_omp_taskyield (void)
6985 {
6986   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
6987   vec<tree, va_gc> *vec = make_tree_vector ();
6988   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
6989   release_tree_vector (vec);
6990   finish_expr_stmt (stmt);
6991 }
6992
6993 void
6994 finish_omp_cancel (tree clauses)
6995 {
6996   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
6997   int mask = 0;
6998   if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
6999     mask = 1;
7000   else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7001     mask = 2;
7002   else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7003     mask = 4;
7004   else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7005     mask = 8;
7006   else
7007     {
7008       error ("%<#pragma omp cancel must specify one of "
7009              "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7010       return;
7011     }
7012   vec<tree, va_gc> *vec = make_tree_vector ();
7013   tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
7014   if (ifc != NULL_TREE)
7015     {
7016       tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
7017       ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
7018                              boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
7019                              build_zero_cst (type));
7020     }
7021   else
7022     ifc = boolean_true_node;
7023   vec->quick_push (build_int_cst (integer_type_node, mask));
7024   vec->quick_push (ifc);
7025   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7026   release_tree_vector (vec);
7027   finish_expr_stmt (stmt);
7028 }
7029
7030 void
7031 finish_omp_cancellation_point (tree clauses)
7032 {
7033   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
7034   int mask = 0;
7035   if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
7036     mask = 1;
7037   else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
7038     mask = 2;
7039   else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
7040     mask = 4;
7041   else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
7042     mask = 8;
7043   else
7044     {
7045       error ("%<#pragma omp cancellation point must specify one of "
7046              "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
7047       return;
7048     }
7049   vec<tree, va_gc> *vec
7050     = make_tree_vector_single (build_int_cst (integer_type_node, mask));
7051   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
7052   release_tree_vector (vec);
7053   finish_expr_stmt (stmt);
7054 }
7055 \f
7056 /* Begin a __transaction_atomic or __transaction_relaxed statement.
7057    If PCOMPOUND is non-null, this is for a function-transaction-block, and we
7058    should create an extra compound stmt.  */
7059
7060 tree
7061 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
7062 {
7063   tree r;
7064
7065   if (pcompound)
7066     *pcompound = begin_compound_stmt (0);
7067
7068   r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
7069
7070   /* Only add the statement to the function if support enabled.  */
7071   if (flag_tm)
7072     add_stmt (r);
7073   else
7074     error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
7075                     ? G_("%<__transaction_relaxed%> without "
7076                          "transactional memory support enabled")
7077                     : G_("%<__transaction_atomic%> without "
7078                          "transactional memory support enabled")));
7079
7080   TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
7081   TREE_SIDE_EFFECTS (r) = 1;
7082   return r;
7083 }
7084
7085 /* End a __transaction_atomic or __transaction_relaxed statement.
7086    If COMPOUND_STMT is non-null, this is for a function-transaction-block,
7087    and we should end the compound.  If NOEX is non-NULL, we wrap the body in
7088    a MUST_NOT_THROW_EXPR with NOEX as condition.  */
7089
7090 void
7091 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
7092 {
7093   TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
7094   TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
7095   TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
7096   TRANSACTION_EXPR_IS_STMT (stmt) = 1;
7097
7098   /* noexcept specifications are not allowed for function transactions.  */
7099   gcc_assert (!(noex && compound_stmt));
7100   if (noex)
7101     {
7102       tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
7103                                              noex);
7104       /* This may not be true when the STATEMENT_LIST is empty.  */
7105       if (EXPR_P (body))
7106         SET_EXPR_LOCATION (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
7107       TREE_SIDE_EFFECTS (body) = 1;
7108       TRANSACTION_EXPR_BODY (stmt) = body;
7109     }
7110
7111   if (compound_stmt)
7112     finish_compound_stmt (compound_stmt);
7113 }
7114
7115 /* Build a __transaction_atomic or __transaction_relaxed expression.  If
7116    NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
7117    condition.  */
7118
7119 tree
7120 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
7121 {
7122   tree ret;
7123   if (noex)
7124     {
7125       expr = build_must_not_throw_expr (expr, noex);
7126       if (EXPR_P (expr))
7127         SET_EXPR_LOCATION (expr, loc);
7128       TREE_SIDE_EFFECTS (expr) = 1;
7129     }
7130   ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
7131   if (flags & TM_STMT_ATTR_RELAXED)
7132         TRANSACTION_EXPR_RELAXED (ret) = 1;
7133   TREE_SIDE_EFFECTS (ret) = 1;
7134   SET_EXPR_LOCATION (ret, loc);
7135   return ret;
7136 }
7137 \f
7138 void
7139 init_cp_semantics (void)
7140 {
7141 }
7142 \f
7143 /* Build a STATIC_ASSERT for a static assertion with the condition
7144    CONDITION and the message text MESSAGE.  LOCATION is the location
7145    of the static assertion in the source code.  When MEMBER_P, this
7146    static assertion is a member of a class.  */
7147 void 
7148 finish_static_assert (tree condition, tree message, location_t location, 
7149                       bool member_p)
7150 {
7151   if (message == NULL_TREE
7152       || message == error_mark_node
7153       || condition == NULL_TREE
7154       || condition == error_mark_node)
7155     return;
7156
7157   if (check_for_bare_parameter_packs (condition))
7158     condition = error_mark_node;
7159
7160   if (type_dependent_expression_p (condition) 
7161       || value_dependent_expression_p (condition))
7162     {
7163       /* We're in a template; build a STATIC_ASSERT and put it in
7164          the right place. */
7165       tree assertion;
7166
7167       assertion = make_node (STATIC_ASSERT);
7168       STATIC_ASSERT_CONDITION (assertion) = condition;
7169       STATIC_ASSERT_MESSAGE (assertion) = message;
7170       STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
7171
7172       if (member_p)
7173         maybe_add_class_template_decl_list (current_class_type, 
7174                                             assertion,
7175                                             /*friend_p=*/0);
7176       else
7177         add_stmt (assertion);
7178
7179       return;
7180     }
7181
7182   /* Fold the expression and convert it to a boolean value. */
7183   condition = instantiate_non_dependent_expr (condition);
7184   condition = cp_convert (boolean_type_node, condition, tf_warning_or_error);
7185   condition = maybe_constant_value (condition);
7186
7187   if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
7188     /* Do nothing; the condition is satisfied. */
7189     ;
7190   else 
7191     {
7192       location_t saved_loc = input_location;
7193
7194       input_location = location;
7195       if (TREE_CODE (condition) == INTEGER_CST 
7196           && integer_zerop (condition))
7197         /* Report the error. */
7198         error ("static assertion failed: %s", TREE_STRING_POINTER (message));
7199       else if (condition && condition != error_mark_node)
7200         {
7201           error ("non-constant condition for static assertion");
7202           if (require_potential_rvalue_constant_expression (condition))
7203             cxx_constant_value (condition);
7204         }
7205       input_location = saved_loc;
7206     }
7207 }
7208 \f
7209 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
7210    suitable for use as a type-specifier.
7211
7212    ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
7213    id-expression or a class member access, FALSE when it was parsed as
7214    a full expression.  */
7215
7216 tree
7217 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
7218                       tsubst_flags_t complain)
7219 {
7220   tree type = NULL_TREE;
7221
7222   if (!expr || error_operand_p (expr))
7223     return error_mark_node;
7224
7225   if (TYPE_P (expr)
7226       || TREE_CODE (expr) == TYPE_DECL
7227       || (TREE_CODE (expr) == BIT_NOT_EXPR
7228           && TYPE_P (TREE_OPERAND (expr, 0))))
7229     {
7230       if (complain & tf_error)
7231         error ("argument to decltype must be an expression");
7232       return error_mark_node;
7233     }
7234
7235   /* Depending on the resolution of DR 1172, we may later need to distinguish
7236      instantiation-dependent but not type-dependent expressions so that, say,
7237      A<decltype(sizeof(T))>::U doesn't require 'typename'.  */
7238   if (instantiation_dependent_expression_p (expr))
7239     {
7240       type = cxx_make_type (DECLTYPE_TYPE);
7241       DECLTYPE_TYPE_EXPR (type) = expr;
7242       DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
7243         = id_expression_or_member_access_p;
7244       SET_TYPE_STRUCTURAL_EQUALITY (type);
7245
7246       return type;
7247     }
7248
7249   /* The type denoted by decltype(e) is defined as follows:  */
7250
7251   expr = resolve_nondeduced_context (expr);
7252
7253   if (invalid_nonstatic_memfn_p (expr, complain))
7254     return error_mark_node;
7255
7256   if (type_unknown_p (expr))
7257     {
7258       if (complain & tf_error)
7259         error ("decltype cannot resolve address of overloaded function");
7260       return error_mark_node;
7261     }
7262
7263   /* To get the size of a static data member declared as an array of
7264      unknown bound, we need to instantiate it.  */
7265   if (VAR_P (expr)
7266       && VAR_HAD_UNKNOWN_BOUND (expr)
7267       && DECL_TEMPLATE_INSTANTIATION (expr))
7268     instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
7269
7270   if (id_expression_or_member_access_p)
7271     {
7272       /* If e is an id-expression or a class member access (5.2.5
7273          [expr.ref]), decltype(e) is defined as the type of the entity
7274          named by e. If there is no such entity, or e names a set of
7275          overloaded functions, the program is ill-formed.  */
7276       if (identifier_p (expr))
7277         expr = lookup_name (expr);
7278
7279       if (INDIRECT_REF_P (expr))
7280         /* This can happen when the expression is, e.g., "a.b". Just
7281            look at the underlying operand.  */
7282         expr = TREE_OPERAND (expr, 0);
7283
7284       if (TREE_CODE (expr) == OFFSET_REF
7285           || TREE_CODE (expr) == MEMBER_REF
7286           || TREE_CODE (expr) == SCOPE_REF)
7287         /* We're only interested in the field itself. If it is a
7288            BASELINK, we will need to see through it in the next
7289            step.  */
7290         expr = TREE_OPERAND (expr, 1);
7291
7292       if (BASELINK_P (expr))
7293         /* See through BASELINK nodes to the underlying function.  */
7294         expr = BASELINK_FUNCTIONS (expr);
7295
7296       switch (TREE_CODE (expr))
7297         {
7298         case FIELD_DECL:
7299           if (DECL_BIT_FIELD_TYPE (expr))
7300             {
7301               type = DECL_BIT_FIELD_TYPE (expr);
7302               break;
7303             }
7304           /* Fall through for fields that aren't bitfields.  */
7305
7306         case FUNCTION_DECL:
7307         case VAR_DECL:
7308         case CONST_DECL:
7309         case PARM_DECL:
7310         case RESULT_DECL:
7311         case TEMPLATE_PARM_INDEX:
7312           expr = mark_type_use (expr);
7313           type = TREE_TYPE (expr);
7314           break;
7315
7316         case ERROR_MARK:
7317           type = error_mark_node;
7318           break;
7319
7320         case COMPONENT_REF:
7321         case COMPOUND_EXPR:
7322           mark_type_use (expr);
7323           type = is_bitfield_expr_with_lowered_type (expr);
7324           if (!type)
7325             type = TREE_TYPE (TREE_OPERAND (expr, 1));
7326           break;
7327
7328         case BIT_FIELD_REF:
7329           gcc_unreachable ();
7330
7331         case INTEGER_CST:
7332         case PTRMEM_CST:
7333           /* We can get here when the id-expression refers to an
7334              enumerator or non-type template parameter.  */
7335           type = TREE_TYPE (expr);
7336           break;
7337
7338         default:
7339           /* Handle instantiated template non-type arguments.  */
7340           type = TREE_TYPE (expr);
7341           break;
7342         }
7343     }
7344   else
7345     {
7346       /* Within a lambda-expression:
7347
7348          Every occurrence of decltype((x)) where x is a possibly
7349          parenthesized id-expression that names an entity of
7350          automatic storage duration is treated as if x were
7351          transformed into an access to a corresponding data member
7352          of the closure type that would have been declared if x
7353          were a use of the denoted entity.  */
7354       if (outer_automatic_var_p (expr)
7355           && current_function_decl
7356           && LAMBDA_FUNCTION_P (current_function_decl))
7357         type = capture_decltype (expr);
7358       else if (error_operand_p (expr))
7359         type = error_mark_node;
7360       else if (expr == current_class_ptr)
7361         /* If the expression is just "this", we want the
7362            cv-unqualified pointer for the "this" type.  */
7363         type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
7364       else
7365         {
7366           /* Otherwise, where T is the type of e, if e is an lvalue,
7367              decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
7368           cp_lvalue_kind clk = lvalue_kind (expr);
7369           type = unlowered_expr_type (expr);
7370           gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
7371
7372           /* For vector types, pick a non-opaque variant.  */
7373           if (TREE_CODE (type) == VECTOR_TYPE)
7374             type = strip_typedefs (type);
7375
7376           if (clk != clk_none && !(clk & clk_class))
7377             type = cp_build_reference_type (type, (clk & clk_rvalueref));
7378         }
7379     }
7380
7381   return type;
7382 }
7383
7384 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or 
7385    __has_nothrow_copy, depending on assign_p.  */
7386
7387 static bool
7388 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
7389 {
7390   tree fns;
7391
7392   if (assign_p)
7393     {
7394       int ix;
7395       ix = lookup_fnfields_1 (type, ansi_assopname (NOP_EXPR));
7396       if (ix < 0)
7397         return false;
7398       fns = (*CLASSTYPE_METHOD_VEC (type))[ix];
7399     } 
7400   else if (TYPE_HAS_COPY_CTOR (type))
7401     {
7402       /* If construction of the copy constructor was postponed, create
7403          it now.  */
7404       if (CLASSTYPE_LAZY_COPY_CTOR (type))
7405         lazily_declare_fn (sfk_copy_constructor, type);
7406       if (CLASSTYPE_LAZY_MOVE_CTOR (type))
7407         lazily_declare_fn (sfk_move_constructor, type);
7408       fns = CLASSTYPE_CONSTRUCTORS (type);
7409     }
7410   else
7411     return false;
7412
7413   for (; fns; fns = OVL_NEXT (fns))
7414     {
7415       tree fn = OVL_CURRENT (fns);
7416  
7417       if (assign_p)
7418         {
7419           if (copy_fn_p (fn) == 0)
7420             continue;
7421         }
7422       else if (copy_fn_p (fn) <= 0)
7423         continue;
7424
7425       maybe_instantiate_noexcept (fn);
7426       if (!TYPE_NOTHROW_P (TREE_TYPE (fn)))
7427         return false;
7428     }
7429
7430   return true;
7431 }
7432
7433 /* Actually evaluates the trait.  */
7434
7435 static bool
7436 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
7437 {
7438   enum tree_code type_code1;
7439   tree t;
7440
7441   type_code1 = TREE_CODE (type1);
7442
7443   switch (kind)
7444     {
7445     case CPTK_HAS_NOTHROW_ASSIGN:
7446       type1 = strip_array_types (type1);
7447       return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7448               && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
7449                   || (CLASS_TYPE_P (type1)
7450                       && classtype_has_nothrow_assign_or_copy_p (type1,
7451                                                                  true))));
7452
7453     case CPTK_HAS_TRIVIAL_ASSIGN:
7454       /* ??? The standard seems to be missing the "or array of such a class
7455          type" wording for this trait.  */
7456       type1 = strip_array_types (type1);
7457       return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
7458               && (trivial_type_p (type1)
7459                     || (CLASS_TYPE_P (type1)
7460                         && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
7461
7462     case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7463       type1 = strip_array_types (type1);
7464       return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2) 
7465               || (CLASS_TYPE_P (type1)
7466                   && (t = locate_ctor (type1))
7467                   && (maybe_instantiate_noexcept (t),
7468                       TYPE_NOTHROW_P (TREE_TYPE (t)))));
7469
7470     case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7471       type1 = strip_array_types (type1);
7472       return (trivial_type_p (type1)
7473               || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
7474
7475     case CPTK_HAS_NOTHROW_COPY:
7476       type1 = strip_array_types (type1);
7477       return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
7478               || (CLASS_TYPE_P (type1)
7479                   && classtype_has_nothrow_assign_or_copy_p (type1, false)));
7480
7481     case CPTK_HAS_TRIVIAL_COPY:
7482       /* ??? The standard seems to be missing the "or array of such a class
7483          type" wording for this trait.  */
7484       type1 = strip_array_types (type1);
7485       return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7486               || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
7487
7488     case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7489       type1 = strip_array_types (type1);
7490       return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
7491               || (CLASS_TYPE_P (type1)
7492                   && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
7493
7494     case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7495       return type_has_virtual_destructor (type1);
7496
7497     case CPTK_IS_ABSTRACT:
7498       return (ABSTRACT_CLASS_TYPE_P (type1));
7499
7500     case CPTK_IS_BASE_OF:
7501       return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7502               && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
7503                   || DERIVED_FROM_P (type1, type2)));
7504
7505     case CPTK_IS_CLASS:
7506       return (NON_UNION_CLASS_TYPE_P (type1));
7507
7508     case CPTK_IS_EMPTY:
7509       return (NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1));
7510
7511     case CPTK_IS_ENUM:
7512       return (type_code1 == ENUMERAL_TYPE);
7513
7514     case CPTK_IS_FINAL:
7515       return (CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1));
7516
7517     case CPTK_IS_LITERAL_TYPE:
7518       return (literal_type_p (type1));
7519
7520     case CPTK_IS_POD:
7521       return (pod_type_p (type1));
7522
7523     case CPTK_IS_POLYMORPHIC:
7524       return (CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1));
7525
7526     case CPTK_IS_STD_LAYOUT:
7527       return (std_layout_type_p (type1));
7528
7529     case CPTK_IS_TRIVIAL:
7530       return (trivial_type_p (type1));
7531
7532     case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7533       return is_trivially_xible (MODIFY_EXPR, type1, type2);
7534
7535     case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7536       return is_trivially_xible (INIT_EXPR, type1, type2);
7537
7538     case CPTK_IS_TRIVIALLY_COPYABLE:
7539       return (trivially_copyable_p (type1));
7540
7541     case CPTK_IS_UNION:
7542       return (type_code1 == UNION_TYPE);
7543
7544     default:
7545       gcc_unreachable ();
7546       return false;
7547     }
7548 }
7549
7550 /* If TYPE is an array of unknown bound, or (possibly cv-qualified)
7551    void, or a complete type, returns true, otherwise false.  */
7552
7553 static bool
7554 check_trait_type (tree type)
7555 {
7556   if (type == NULL_TREE)
7557     return true;
7558
7559   if (TREE_CODE (type) == TREE_LIST)
7560     return (check_trait_type (TREE_VALUE (type))
7561             && check_trait_type (TREE_CHAIN (type)));
7562
7563   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
7564       && COMPLETE_TYPE_P (TREE_TYPE (type)))
7565     return true;
7566
7567   if (VOID_TYPE_P (type))
7568     return true;
7569
7570   return !!complete_type_or_else (strip_array_types (type), NULL_TREE);
7571 }
7572
7573 /* Process a trait expression.  */
7574
7575 tree
7576 finish_trait_expr (cp_trait_kind kind, tree type1, tree type2)
7577 {
7578   if (type1 == error_mark_node
7579       || type2 == error_mark_node)
7580     return error_mark_node;
7581
7582   if (processing_template_decl)
7583     {
7584       tree trait_expr = make_node (TRAIT_EXPR);
7585       TREE_TYPE (trait_expr) = boolean_type_node;
7586       TRAIT_EXPR_TYPE1 (trait_expr) = type1;
7587       TRAIT_EXPR_TYPE2 (trait_expr) = type2;
7588       TRAIT_EXPR_KIND (trait_expr) = kind;
7589       return trait_expr;
7590     }
7591
7592   switch (kind)
7593     {
7594     case CPTK_HAS_NOTHROW_ASSIGN:
7595     case CPTK_HAS_TRIVIAL_ASSIGN:
7596     case CPTK_HAS_NOTHROW_CONSTRUCTOR:
7597     case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
7598     case CPTK_HAS_NOTHROW_COPY:
7599     case CPTK_HAS_TRIVIAL_COPY:
7600     case CPTK_HAS_TRIVIAL_DESTRUCTOR:
7601     case CPTK_HAS_VIRTUAL_DESTRUCTOR:
7602     case CPTK_IS_ABSTRACT:
7603     case CPTK_IS_EMPTY:
7604     case CPTK_IS_FINAL:
7605     case CPTK_IS_LITERAL_TYPE:
7606     case CPTK_IS_POD:
7607     case CPTK_IS_POLYMORPHIC:
7608     case CPTK_IS_STD_LAYOUT:
7609     case CPTK_IS_TRIVIAL:
7610     case CPTK_IS_TRIVIALLY_COPYABLE:
7611       if (!check_trait_type (type1))
7612         return error_mark_node;
7613       break;
7614
7615     case CPTK_IS_TRIVIALLY_ASSIGNABLE:
7616     case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
7617       if (!check_trait_type (type1)
7618           || !check_trait_type (type2))
7619         return error_mark_node;
7620       break;
7621
7622     case CPTK_IS_BASE_OF:
7623       if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
7624           && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
7625           && !complete_type_or_else (type2, NULL_TREE))
7626         /* We already issued an error.  */
7627         return error_mark_node;
7628       break;
7629
7630     case CPTK_IS_CLASS:
7631     case CPTK_IS_ENUM:
7632     case CPTK_IS_UNION:
7633       break;
7634     
7635     default:
7636       gcc_unreachable ();
7637     }
7638
7639   return (trait_expr_value (kind, type1, type2)
7640           ? boolean_true_node : boolean_false_node);
7641 }
7642
7643 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
7644    which is ignored for C++.  */
7645
7646 void
7647 set_float_const_decimal64 (void)
7648 {
7649 }
7650
7651 void
7652 clear_float_const_decimal64 (void)
7653 {
7654 }
7655
7656 bool
7657 float_const_decimal64_p (void)
7658 {
7659   return 0;
7660 }
7661
7662 \f
7663 /* Return true if T designates the implied `this' parameter.  */
7664
7665 bool
7666 is_this_parameter (tree t)
7667 {
7668   if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
7669     return false;
7670   gcc_assert (TREE_CODE (t) == PARM_DECL || is_capture_proxy (t));
7671   return true;
7672 }
7673
7674 /* Insert the deduced return type for an auto function.  */
7675
7676 void
7677 apply_deduced_return_type (tree fco, tree return_type)
7678 {
7679   tree result;
7680
7681   if (return_type == error_mark_node)
7682     return;
7683
7684   if (LAMBDA_FUNCTION_P (fco))
7685     {
7686       tree lambda = CLASSTYPE_LAMBDA_EXPR (current_class_type);
7687       LAMBDA_EXPR_RETURN_TYPE (lambda) = return_type;
7688     }
7689
7690   if (DECL_CONV_FN_P (fco))
7691     DECL_NAME (fco) = mangle_conv_op_name_for_type (return_type);
7692
7693   TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
7694
7695   result = DECL_RESULT (fco);
7696   if (result == NULL_TREE)
7697     return;
7698   if (TREE_TYPE (result) == return_type)
7699     return;
7700
7701   /* We already have a DECL_RESULT from start_preparsed_function.
7702      Now we need to redo the work it and allocate_struct_function
7703      did to reflect the new type.  */
7704   gcc_assert (current_function_decl == fco);
7705   result = build_decl (input_location, RESULT_DECL, NULL_TREE,
7706                        TYPE_MAIN_VARIANT (return_type));
7707   DECL_ARTIFICIAL (result) = 1;
7708   DECL_IGNORED_P (result) = 1;
7709   cp_apply_type_quals_to_decl (cp_type_quals (return_type),
7710                                result);
7711
7712   DECL_RESULT (fco) = result;
7713
7714   if (!processing_template_decl)
7715     {
7716       if (!VOID_TYPE_P (TREE_TYPE (result)))
7717         complete_type_or_else (TREE_TYPE (result), NULL_TREE);
7718       bool aggr = aggregate_value_p (result, fco);
7719 #ifdef PCC_STATIC_STRUCT_RETURN
7720       cfun->returns_pcc_struct = aggr;
7721 #endif
7722       cfun->returns_struct = aggr;
7723     }
7724
7725 }
7726
7727 /* DECL is a local variable or parameter from the surrounding scope of a
7728    lambda-expression.  Returns the decltype for a use of the capture field
7729    for DECL even if it hasn't been captured yet.  */
7730
7731 static tree
7732 capture_decltype (tree decl)
7733 {
7734   tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
7735   /* FIXME do lookup instead of list walk? */
7736   tree cap = value_member (decl, LAMBDA_EXPR_CAPTURE_LIST (lam));
7737   tree type;
7738
7739   if (cap)
7740     type = TREE_TYPE (TREE_PURPOSE (cap));
7741   else
7742     switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
7743       {
7744       case CPLD_NONE:
7745         error ("%qD is not captured", decl);
7746         return error_mark_node;
7747
7748       case CPLD_COPY:
7749         type = TREE_TYPE (decl);
7750         if (TREE_CODE (type) == REFERENCE_TYPE
7751             && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
7752           type = TREE_TYPE (type);
7753         break;
7754
7755       case CPLD_REFERENCE:
7756         type = TREE_TYPE (decl);
7757         if (TREE_CODE (type) != REFERENCE_TYPE)
7758           type = build_reference_type (TREE_TYPE (decl));
7759         break;
7760
7761       default:
7762         gcc_unreachable ();
7763       }
7764
7765   if (TREE_CODE (type) != REFERENCE_TYPE)
7766     {
7767       if (!LAMBDA_EXPR_MUTABLE_P (lam))
7768         type = cp_build_qualified_type (type, (cp_type_quals (type)
7769                                                |TYPE_QUAL_CONST));
7770       type = build_reference_type (type);
7771     }
7772   return type;
7773 }
7774
7775 #include "gt-cp-semantics.h"