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