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