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