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