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