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