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 	pedwarn (input_location, OPT_Wc__23_extensions,
   3566 		 "%<auto{x}%> only available with "
   3567 		 "%<-std=c++2b%> or %<-std=gnu++2b%>");
   3568       type = do_auto_deduction (type, compound_literal, type, complain,
   3569 				adc_variable_type);
   3570       if (type == error_mark_node)
   3571 	return error_mark_node;
   3572     }
   3573 
   3574   /* Used to hold a copy of the compound literal in a template.  */
   3575   tree orig_cl = NULL_TREE;
   3576 
   3577   if (processing_template_decl)
   3578     {
   3579       const bool dependent_p
   3580 	= (instantiation_dependent_expression_p (compound_literal)
   3581 	   || dependent_type_p (type));
   3582       if (dependent_p)
   3583 	/* We're about to return, no need to copy.  */
   3584 	orig_cl = compound_literal;
   3585       else
   3586 	/* We're going to need a copy.  */
   3587 	orig_cl = unshare_constructor (compound_literal);
   3588       TREE_TYPE (orig_cl) = type;
   3589       /* Mark the expression as a compound literal.  */
   3590       TREE_HAS_CONSTRUCTOR (orig_cl) = 1;
   3591       /* And as instantiation-dependent.  */
   3592       CONSTRUCTOR_IS_DEPENDENT (orig_cl) = dependent_p;
   3593       if (fcl_context == fcl_c99)
   3594 	CONSTRUCTOR_C99_COMPOUND_LITERAL (orig_cl) = 1;
   3595       /* If the compound literal is dependent, we're done for now.  */
   3596       if (dependent_p)
   3597 	return orig_cl;
   3598       /* Otherwise, do go on to e.g. check narrowing.  */
   3599     }
   3600 
   3601   type = complete_type (type);
   3602 
   3603   if (TYPE_NON_AGGREGATE_CLASS (type))
   3604     {
   3605       /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
   3606 	 everywhere that deals with function arguments would be a pain, so
   3607 	 just wrap it in a TREE_LIST.  The parser set a flag so we know
   3608 	 that it came from T{} rather than T({}).  */
   3609       CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
   3610       compound_literal = build_tree_list (NULL_TREE, compound_literal);
   3611       return build_functional_cast (input_location, type,
   3612 				    compound_literal, complain);
   3613     }
   3614 
   3615   if (TREE_CODE (type) == ARRAY_TYPE
   3616       && check_array_initializer (NULL_TREE, type, compound_literal))
   3617     return error_mark_node;
   3618   compound_literal = reshape_init (type, compound_literal, complain);
   3619   if (SCALAR_TYPE_P (type)
   3620       && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
   3621       && !check_narrowing (type, compound_literal, complain))
   3622     return error_mark_node;
   3623   if (TREE_CODE (type) == ARRAY_TYPE
   3624       && TYPE_DOMAIN (type) == NULL_TREE)
   3625     {
   3626       cp_complete_array_type_or_error (&type, compound_literal,
   3627 				       false, complain);
   3628       if (type == error_mark_node)
   3629 	return error_mark_node;
   3630     }
   3631   compound_literal = digest_init_flags (type, compound_literal,
   3632 					LOOKUP_NORMAL | LOOKUP_NO_NARROWING,
   3633 					complain);
   3634   if (compound_literal == error_mark_node)
   3635     return error_mark_node;
   3636 
   3637   /* If we're in a template, return the original compound literal.  */
   3638   if (orig_cl)
   3639     return orig_cl;
   3640 
   3641   if (TREE_CODE (compound_literal) == CONSTRUCTOR)
   3642     {
   3643       TREE_HAS_CONSTRUCTOR (compound_literal) = true;
   3644       if (fcl_context == fcl_c99)
   3645 	CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
   3646     }
   3647 
   3648   /* Put static/constant array temporaries in static variables.  */
   3649   /* FIXME all C99 compound literals should be variables rather than C++
   3650      temporaries, unless they are used as an aggregate initializer.  */
   3651   if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
   3652       && fcl_context == fcl_c99
   3653       && TREE_CODE (type) == ARRAY_TYPE
   3654       && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   3655       && initializer_constant_valid_p (compound_literal, type))
   3656     {
   3657       tree decl = create_temporary_var (type);
   3658       DECL_CONTEXT (decl) = NULL_TREE;
   3659       DECL_INITIAL (decl) = compound_literal;
   3660       TREE_STATIC (decl) = 1;
   3661       if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
   3662 	{
   3663 	  /* 5.19 says that a constant expression can include an
   3664 	     lvalue-rvalue conversion applied to "a glvalue of literal type
   3665 	     that refers to a non-volatile temporary object initialized
   3666 	     with a constant expression".  Rather than try to communicate
   3667 	     that this VAR_DECL is a temporary, just mark it constexpr.  */
   3668 	  DECL_DECLARED_CONSTEXPR_P (decl) = true;
   3669 	  DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   3670 	  TREE_CONSTANT (decl) = true;
   3671 	}
   3672       cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
   3673       decl = pushdecl_top_level (decl);
   3674       DECL_NAME (decl) = make_anon_name ();
   3675       SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
   3676       /* Make sure the destructor is callable.  */
   3677       tree clean = cxx_maybe_build_cleanup (decl, complain);
   3678       if (clean == error_mark_node)
   3679 	return error_mark_node;
   3680       return decl;
   3681     }
   3682 
   3683   /* Represent other compound literals with TARGET_EXPR so we produce
   3684      a prvalue, and can elide copies.  */
   3685   if (!VECTOR_TYPE_P (type)
   3686       && (TREE_CODE (compound_literal) == CONSTRUCTOR
   3687 	  || TREE_CODE (compound_literal) == VEC_INIT_EXPR))
   3688     {
   3689       /* The CONSTRUCTOR is now an initializer, not a compound literal.  */
   3690       if (TREE_CODE (compound_literal) == CONSTRUCTOR)
   3691 	TREE_HAS_CONSTRUCTOR (compound_literal) = false;
   3692       compound_literal = get_target_expr (compound_literal, complain);
   3693     }
   3694   else
   3695     /* For e.g. int{42} just make sure it's a prvalue.  */
   3696     compound_literal = rvalue (compound_literal);
   3697 
   3698   return compound_literal;
   3699 }
   3700 
   3701 /* Return the declaration for the function-name variable indicated by
   3702    ID.  */
   3703 
   3704 tree
   3705 finish_fname (tree id)
   3706 {
   3707   tree decl;
   3708 
   3709   decl = fname_decl (input_location, C_RID_CODE (id), id);
   3710   if (processing_template_decl && current_function_decl
   3711       && decl != error_mark_node)
   3712     decl = DECL_NAME (decl);
   3713   return decl;
   3714 }
   3715 
   3716 /* Finish a translation unit.  */
   3717 
   3718 void
   3719 finish_translation_unit (void)
   3720 {
   3721   /* In case there were missing closebraces,
   3722      get us back to the global binding level.  */
   3723   pop_everything ();
   3724   while (current_namespace != global_namespace)
   3725     pop_namespace ();
   3726 
   3727   /* Do file scope __FUNCTION__ et al.  */
   3728   finish_fname_decls ();
   3729 
   3730   if (vec_safe_length (scope_chain->omp_declare_target_attribute))
   3731     {
   3732       cp_omp_declare_target_attr
   3733 	a = scope_chain->omp_declare_target_attribute->pop ();
   3734       if (!errorcount)
   3735 	error ("%qs without corresponding %qs",
   3736 	       a.device_type >= 0 ? "#pragma omp begin declare target"
   3737 				  : "#pragma omp declare target",
   3738 	       "#pragma omp end declare target");
   3739       vec_safe_truncate (scope_chain->omp_declare_target_attribute, 0);
   3740     }
   3741   if (vec_safe_length (scope_chain->omp_begin_assumes))
   3742     {
   3743       if (!errorcount)
   3744 	error ("%qs without corresponding %qs",
   3745 	       "#pragma omp begin assumes", "#pragma omp end assumes");
   3746       vec_safe_truncate (scope_chain->omp_begin_assumes, 0);
   3747     }
   3748 }
   3749 
   3750 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
   3751    Returns the parameter.  */
   3752 
   3753 tree
   3754 finish_template_type_parm (tree aggr, tree identifier)
   3755 {
   3756   if (aggr != class_type_node)
   3757     {
   3758       permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
   3759       aggr = class_type_node;
   3760     }
   3761 
   3762   return build_tree_list (aggr, identifier);
   3763 }
   3764 
   3765 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
   3766    Returns the parameter.  */
   3767 
   3768 tree
   3769 finish_template_template_parm (tree aggr, tree identifier)
   3770 {
   3771   tree decl = build_decl (input_location,
   3772 			  TYPE_DECL, identifier, NULL_TREE);
   3773 
   3774   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
   3775   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
   3776   DECL_TEMPLATE_RESULT (tmpl) = decl;
   3777   DECL_ARTIFICIAL (decl) = 1;
   3778 
   3779   /* Associate the constraints with the underlying declaration,
   3780      not the template.  */
   3781   tree constr = current_template_constraints ();
   3782   set_constraints (decl, constr);
   3783 
   3784   end_template_decl ();
   3785 
   3786   gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
   3787 
   3788   check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
   3789 			   /*is_primary=*/true, /*is_partial=*/false,
   3790 			   /*is_friend=*/0);
   3791 
   3792   return finish_template_type_parm (aggr, tmpl);
   3793 }
   3794 
   3795 /* ARGUMENT is the default-argument value for a template template
   3796    parameter.  If ARGUMENT is invalid, issue error messages and return
   3797    the ERROR_MARK_NODE.  Otherwise, ARGUMENT itself is returned.  */
   3798 
   3799 tree
   3800 check_template_template_default_arg (tree argument)
   3801 {
   3802   if (TREE_CODE (argument) != TEMPLATE_DECL
   3803       && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
   3804       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
   3805     {
   3806       if (TREE_CODE (argument) == TYPE_DECL)
   3807 	{
   3808 	  if (tree t = maybe_get_template_decl_from_type_decl (argument))
   3809 	    if (TREE_CODE (t) == TEMPLATE_DECL)
   3810 	      return t;
   3811 	  error ("invalid use of type %qT as a default value for a template "
   3812 		 "template-parameter", TREE_TYPE (argument));
   3813 	}
   3814       else
   3815 	error ("invalid default argument for a template template parameter");
   3816       return error_mark_node;
   3817     }
   3818 
   3819   return argument;
   3820 }
   3821 
   3822 /* Begin a class definition, as indicated by T.  */
   3823 
   3824 tree
   3825 begin_class_definition (tree t)
   3826 {
   3827   if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
   3828     return error_mark_node;
   3829 
   3830   if (processing_template_parmlist && !LAMBDA_TYPE_P (t))
   3831     {
   3832       error ("definition of %q#T inside template parameter list", t);
   3833       return error_mark_node;
   3834     }
   3835 
   3836   /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
   3837      are passed the same as decimal scalar types.  */
   3838   if (TREE_CODE (t) == RECORD_TYPE
   3839       && !processing_template_decl)
   3840     {
   3841       tree ns = TYPE_CONTEXT (t);
   3842       if (ns && TREE_CODE (ns) == NAMESPACE_DECL
   3843 	  && DECL_CONTEXT (ns) == std_node
   3844 	  && DECL_NAME (ns)
   3845 	  && id_equal (DECL_NAME (ns), "decimal"))
   3846 	{
   3847 	  const char *n = TYPE_NAME_STRING (t);
   3848 	  if ((strcmp (n, "decimal32") == 0)
   3849 	      || (strcmp (n, "decimal64") == 0)
   3850 	      || (strcmp (n, "decimal128") == 0))
   3851 	    TYPE_TRANSPARENT_AGGR (t) = 1;
   3852 	}
   3853     }
   3854 
   3855   /* A non-implicit typename comes from code like:
   3856 
   3857        template <typename T> struct A {
   3858 	 template <typename U> struct A<T>::B ...
   3859 
   3860      This is erroneous.  */
   3861   else if (TREE_CODE (t) == TYPENAME_TYPE)
   3862     {
   3863       error ("invalid definition of qualified type %qT", t);
   3864       t = error_mark_node;
   3865     }
   3866 
   3867   if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
   3868     {
   3869       t = make_class_type (RECORD_TYPE);
   3870       pushtag (make_anon_name (), t);
   3871     }
   3872 
   3873   if (TYPE_BEING_DEFINED (t))
   3874     {
   3875       t = make_class_type (TREE_CODE (t));
   3876       pushtag (TYPE_IDENTIFIER (t), t);
   3877     }
   3878 
   3879   if (modules_p ())
   3880     {
   3881       if (!module_may_redeclare (TYPE_NAME (t)))
   3882 	return error_mark_node;
   3883       set_instantiating_module (TYPE_NAME (t));
   3884       set_defining_module (TYPE_NAME (t));
   3885     }
   3886 
   3887   maybe_process_partial_specialization (t);
   3888   pushclass (t);
   3889   TYPE_BEING_DEFINED (t) = 1;
   3890   class_binding_level->defining_class_p = 1;
   3891 
   3892   if (flag_pack_struct)
   3893     {
   3894       tree v;
   3895       TYPE_PACKED (t) = 1;
   3896       /* Even though the type is being defined for the first time
   3897 	 here, there might have been a forward declaration, so there
   3898 	 might be cv-qualified variants of T.  */
   3899       for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
   3900 	TYPE_PACKED (v) = 1;
   3901     }
   3902   /* Reset the interface data, at the earliest possible
   3903      moment, as it might have been set via a class foo;
   3904      before.  */
   3905   if (! TYPE_UNNAMED_P (t))
   3906     {
   3907       struct c_fileinfo *finfo = \
   3908 	get_fileinfo (LOCATION_FILE (input_location));
   3909       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
   3910       SET_CLASSTYPE_INTERFACE_UNKNOWN_X
   3911 	(t, finfo->interface_unknown);
   3912     }
   3913   reset_specialization ();
   3914 
   3915   /* Make a declaration for this class in its own scope.  */
   3916   build_self_reference ();
   3917 
   3918   return t;
   3919 }
   3920 
   3921 /* Finish the member declaration given by DECL.  */
   3922 
   3923 void
   3924 finish_member_declaration (tree decl)
   3925 {
   3926   if (decl == error_mark_node || decl == NULL_TREE)
   3927     return;
   3928 
   3929   if (decl == void_type_node)
   3930     /* The COMPONENT was a friend, not a member, and so there's
   3931        nothing for us to do.  */
   3932     return;
   3933 
   3934   /* We should see only one DECL at a time.  */
   3935   gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
   3936 
   3937   /* Don't add decls after definition.  */
   3938   gcc_assert (TYPE_BEING_DEFINED (current_class_type)
   3939 	      /* We can add lambda types when late parsing default
   3940 		 arguments.  */
   3941 	      || LAMBDA_TYPE_P (TREE_TYPE (decl)));
   3942 
   3943   /* Set up access control for DECL.  */
   3944   TREE_PRIVATE (decl)
   3945     = (current_access_specifier == access_private_node);
   3946   TREE_PROTECTED (decl)
   3947     = (current_access_specifier == access_protected_node);
   3948   if (TREE_CODE (decl) == TEMPLATE_DECL)
   3949     {
   3950       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
   3951       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
   3952     }
   3953 
   3954   /* Mark the DECL as a member of the current class, unless it's
   3955      a member of an enumeration.  */
   3956   if (TREE_CODE (decl) != CONST_DECL)
   3957     DECL_CONTEXT (decl) = current_class_type;
   3958 
   3959   /* Remember the single FIELD_DECL an anonymous aggregate type is used for.  */
   3960   if (TREE_CODE (decl) == FIELD_DECL
   3961       && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   3962     {
   3963       gcc_assert (!ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))));
   3964       ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))) = decl;
   3965     }
   3966 
   3967   if (TREE_CODE (decl) == USING_DECL)
   3968     /* Avoid debug info for class-scope USING_DECLS for now, we'll
   3969        call cp_emit_debug_info_for_using later. */
   3970     DECL_IGNORED_P (decl) = 1;
   3971 
   3972   /* Check for bare parameter packs in the non-static data member
   3973      declaration.  */
   3974   if (TREE_CODE (decl) == FIELD_DECL)
   3975     {
   3976       if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
   3977         TREE_TYPE (decl) = error_mark_node;
   3978       if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
   3979         DECL_ATTRIBUTES (decl) = NULL_TREE;
   3980     }
   3981 
   3982   /* [dcl.link]
   3983 
   3984      A C language linkage is ignored for the names of class members
   3985      and the member function type of class member functions.  */
   3986   if (DECL_LANG_SPECIFIC (decl))
   3987     SET_DECL_LANGUAGE (decl, lang_cplusplus);
   3988 
   3989   bool add = false;
   3990 
   3991   /* Functions and non-functions are added differently.  */
   3992   if (DECL_DECLARES_FUNCTION_P (decl))
   3993     add = add_method (current_class_type, decl, false);
   3994   /* Enter the DECL into the scope of the class, if the class
   3995      isn't a closure (whose fields are supposed to be unnamed).  */
   3996   else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
   3997 	   || maybe_push_used_methods (decl)
   3998 	   || pushdecl_class_level (decl))
   3999     add = true;
   4000 
   4001   if (add)
   4002     {
   4003       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
   4004 	 go at the beginning.  The reason is that
   4005 	 legacy_nonfn_member_lookup searches the list in order, and we
   4006 	 want a field name to override a type name so that the "struct
   4007 	 stat hack" will work.  In particular:
   4008 
   4009 	   struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
   4010 
   4011 	 is valid.  */
   4012 
   4013       if (TREE_CODE (decl) == TYPE_DECL)
   4014 	TYPE_FIELDS (current_class_type)
   4015 	  = chainon (TYPE_FIELDS (current_class_type), decl);
   4016       else
   4017 	{
   4018 	  DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
   4019 	  TYPE_FIELDS (current_class_type) = decl;
   4020 	}
   4021 
   4022       maybe_add_class_template_decl_list (current_class_type, decl,
   4023 					  /*friend_p=*/0);
   4024     }
   4025 }
   4026 
   4027 /* Finish processing a complete template declaration.  The PARMS are
   4028    the template parameters.  */
   4029 
   4030 void
   4031 finish_template_decl (tree parms)
   4032 {
   4033   if (parms)
   4034     end_template_decl ();
   4035   else
   4036     end_specialization ();
   4037 }
   4038 
   4039 // Returns the template type of the class scope being entered. If we're
   4040 // entering a constrained class scope. TYPE is the class template
   4041 // scope being entered and we may need to match the intended type with
   4042 // a constrained specialization. For example:
   4043 //
   4044 //    template<Object T>
   4045 //      struct S { void f(); }; #1
   4046 //
   4047 //    template<Object T>
   4048 //      void S<T>::f() { }      #2
   4049 //
   4050 // We check, in #2, that S<T> refers precisely to the type declared by
   4051 // #1 (i.e., that the constraints match). Note that the following should
   4052 // be an error since there is no specialization of S<T> that is
   4053 // unconstrained, but this is not diagnosed here.
   4054 //
   4055 //    template<typename T>
   4056 //      void S<T>::f() { }
   4057 //
   4058 // We cannot diagnose this problem here since this function also matches
   4059 // qualified template names that are not part of a definition. For example:
   4060 //
   4061 //    template<Integral T, Floating_point U>
   4062 //      typename pair<T, U>::first_type void f(T, U);
   4063 //
   4064 // Here, it is unlikely that there is a partial specialization of
   4065 // pair constrained for Integral and Floating_point arguments.
   4066 //
   4067 // The general rule is: if a constrained specialization with matching
   4068 // constraints is found return that type. Also note that if TYPE is not a
   4069 // class-type (e.g. a typename type), then no fixup is needed.
   4070 
   4071 static tree
   4072 fixup_template_type (tree type)
   4073 {
   4074   // Find the template parameter list at the a depth appropriate to
   4075   // the scope we're trying to enter.
   4076   tree parms = current_template_parms;
   4077   int depth = template_class_depth (type);
   4078   for (int n = current_template_depth; n > depth && parms; --n)
   4079     parms = TREE_CHAIN (parms);
   4080   if (!parms)
   4081     return type;
   4082   tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
   4083   tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
   4084 
   4085   // Search for a specialization whose type and constraints match.
   4086   tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
   4087   tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   4088   while (specs)
   4089     {
   4090       tree spec_constr = get_constraints (TREE_VALUE (specs));
   4091 
   4092       // If the type and constraints match a specialization, then we
   4093       // are entering that type.
   4094       if (same_type_p (type, TREE_TYPE (specs))
   4095 	  && equivalent_constraints (cur_constr, spec_constr))
   4096         return TREE_TYPE (specs);
   4097       specs = TREE_CHAIN (specs);
   4098     }
   4099 
   4100   // If no specialization matches, then must return the type
   4101   // previously found.
   4102   return type;
   4103 }
   4104 
   4105 /* Finish processing a template-id (which names a type) of the form
   4106    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
   4107    template-id.  If ENTERING_SCOPE is nonzero we are about to enter
   4108    the scope of template-id indicated.  */
   4109 
   4110 tree
   4111 finish_template_type (tree name, tree args, int entering_scope)
   4112 {
   4113   tree type;
   4114 
   4115   type = lookup_template_class (name, args,
   4116 				NULL_TREE, NULL_TREE, entering_scope,
   4117 				tf_warning_or_error | tf_user);
   4118 
   4119   /* If we might be entering the scope of a partial specialization,
   4120      find the one with the right constraints.  */
   4121   if (flag_concepts
   4122       && entering_scope
   4123       && CLASS_TYPE_P (type)
   4124       && CLASSTYPE_TEMPLATE_INFO (type)
   4125       && dependent_type_p (type)
   4126       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
   4127     type = fixup_template_type (type);
   4128 
   4129   if (type == error_mark_node)
   4130     return type;
   4131   else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
   4132     return TYPE_STUB_DECL (type);
   4133   else
   4134     return TYPE_NAME (type);
   4135 }
   4136 
   4137 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
   4138    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
   4139    BASE_CLASS, or NULL_TREE if an error occurred.  The
   4140    ACCESS_SPECIFIER is one of
   4141    access_{default,public,protected_private}_node.  For a virtual base
   4142    we set TREE_TYPE.  */
   4143 
   4144 tree
   4145 finish_base_specifier (tree base, tree access, bool virtual_p)
   4146 {
   4147   tree result;
   4148 
   4149   if (base == error_mark_node)
   4150     {
   4151       error ("invalid base-class specification");
   4152       result = NULL_TREE;
   4153     }
   4154   else if (! MAYBE_CLASS_TYPE_P (base))
   4155     {
   4156       error ("%qT is not a class type", base);
   4157       result = NULL_TREE;
   4158     }
   4159   else
   4160     {
   4161       if (cp_type_quals (base) != 0)
   4162 	{
   4163 	  /* DR 484: Can a base-specifier name a cv-qualified
   4164 	     class type?  */
   4165 	  base = TYPE_MAIN_VARIANT (base);
   4166 	}
   4167       result = build_tree_list (access, base);
   4168       if (virtual_p)
   4169 	TREE_TYPE (result) = integer_type_node;
   4170     }
   4171 
   4172   return result;
   4173 }
   4174 
   4175 /* If FNS is a member function, a set of member functions, or a
   4176    template-id referring to one or more member functions, return a
   4177    BASELINK for FNS, incorporating the current access context.
   4178    Otherwise, return FNS unchanged.  */
   4179 
   4180 tree
   4181 baselink_for_fns (tree fns)
   4182 {
   4183   tree scope;
   4184   tree cl;
   4185 
   4186   if (BASELINK_P (fns)
   4187       || error_operand_p (fns))
   4188     return fns;
   4189 
   4190   scope = ovl_scope (fns);
   4191   if (!CLASS_TYPE_P (scope))
   4192     return fns;
   4193 
   4194   cl = currently_open_derived_class (scope);
   4195   if (!cl)
   4196     cl = scope;
   4197   tree access_path = TYPE_BINFO (cl);
   4198   tree conv_path = (cl == scope ? access_path
   4199 		    : lookup_base (cl, scope, ba_any, NULL, tf_none));
   4200   return build_baselink (conv_path, access_path, fns, /*optype=*/NULL_TREE);
   4201 }
   4202 
   4203 /* Returns true iff DECL is a variable from a function outside
   4204    the current one.  */
   4205 
   4206 static bool
   4207 outer_var_p (tree decl)
   4208 {
   4209   /* These should have been stripped or otherwise handled by the caller.  */
   4210   gcc_checking_assert (!REFERENCE_REF_P (decl));
   4211 
   4212   return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
   4213 	  && DECL_FUNCTION_SCOPE_P (decl)
   4214 	  /* Don't get confused by temporaries.  */
   4215 	  && DECL_NAME (decl)
   4216 	  && (DECL_CONTEXT (decl) != current_function_decl
   4217 	      || parsing_nsdmi ()));
   4218 }
   4219 
   4220 /* As above, but also checks that DECL is automatic.  */
   4221 
   4222 bool
   4223 outer_automatic_var_p (tree decl)
   4224 {
   4225   return (outer_var_p (decl)
   4226 	  && !TREE_STATIC (decl));
   4227 }
   4228 
   4229 /* DECL satisfies outer_automatic_var_p.  Possibly complain about it or
   4230    rewrite it for lambda capture.
   4231 
   4232    If ODR_USE is true, we're being called from mark_use, and we complain about
   4233    use of constant variables.  If ODR_USE is false, we're being called for the
   4234    id-expression, and we do lambda capture.  */
   4235 
   4236 tree
   4237 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
   4238 {
   4239   if (cp_unevaluated_operand)
   4240     {
   4241       tree type = TREE_TYPE (decl);
   4242       if (!dependent_type_p (type)
   4243 	  && variably_modified_type_p (type, NULL_TREE))
   4244 	/* VLAs are used even in unevaluated context.  */;
   4245       else
   4246 	/* It's not a use (3.2) if we're in an unevaluated context.  */
   4247 	return decl;
   4248     }
   4249   if (decl == error_mark_node)
   4250     return decl;
   4251 
   4252   tree context = DECL_CONTEXT (decl);
   4253   tree containing_function = current_function_decl;
   4254   tree lambda_stack = NULL_TREE;
   4255   tree lambda_expr = NULL_TREE;
   4256   tree initializer = convert_from_reference (decl);
   4257 
   4258   /* Mark it as used now even if the use is ill-formed.  */
   4259   if (!mark_used (decl, complain))
   4260     return error_mark_node;
   4261 
   4262   if (parsing_nsdmi ())
   4263     containing_function = NULL_TREE;
   4264 
   4265   if (containing_function && LAMBDA_FUNCTION_P (containing_function))
   4266     {
   4267       /* Check whether we've already built a proxy.  */
   4268       tree var = decl;
   4269       while (is_normal_capture_proxy (var))
   4270 	var = DECL_CAPTURED_VARIABLE (var);
   4271       tree d = retrieve_local_specialization (var);
   4272 
   4273       if (d && d != decl && is_capture_proxy (d))
   4274 	{
   4275 	  if (DECL_CONTEXT (d) == containing_function)
   4276 	    /* We already have an inner proxy.  */
   4277 	    return d;
   4278 	  else
   4279 	    /* We need to capture an outer proxy.  */
   4280 	    return process_outer_var_ref (d, complain, odr_use);
   4281 	}
   4282     }
   4283 
   4284   /* If we are in a lambda function, we can move out until we hit
   4285      1. the context,
   4286      2. a non-lambda function, or
   4287      3. a non-default capturing lambda function.  */
   4288   while (context != containing_function
   4289 	 /* containing_function can be null with invalid generic lambdas.  */
   4290 	 && containing_function
   4291 	 && LAMBDA_FUNCTION_P (containing_function))
   4292     {
   4293       tree closure = DECL_CONTEXT (containing_function);
   4294       lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
   4295 
   4296       if (TYPE_CLASS_SCOPE_P (closure))
   4297 	/* A lambda in an NSDMI (c++/64496).  */
   4298 	break;
   4299 
   4300       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
   4301 	break;
   4302 
   4303       lambda_stack = tree_cons (NULL_TREE, lambda_expr, lambda_stack);
   4304 
   4305       containing_function = decl_function_context (containing_function);
   4306     }
   4307 
   4308   /* In a lambda within a template, wait until instantiation time to implicitly
   4309      capture a parameter pack.  We want to wait because we don't know if we're
   4310      capturing the whole pack or a single element, and it's OK to wait because
   4311      find_parameter_packs_r walks into the lambda body.  */
   4312   if (context == containing_function
   4313       && DECL_PACK_P (decl))
   4314     return decl;
   4315 
   4316   if (lambda_expr && VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))
   4317     {
   4318       if (complain & tf_error)
   4319 	error ("cannot capture member %qD of anonymous union", decl);
   4320       return error_mark_node;
   4321     }
   4322   /* Do lambda capture when processing the id-expression, not when
   4323      odr-using a variable.  */
   4324   if (!odr_use && context == containing_function)
   4325     decl = add_default_capture (lambda_stack,
   4326 				/*id=*/DECL_NAME (decl), initializer);
   4327   /* Only an odr-use of an outer automatic variable causes an
   4328      error, and a constant variable can decay to a prvalue
   4329      constant without odr-use.  So don't complain yet.  */
   4330   else if (!odr_use && decl_constant_var_p (decl))
   4331     return decl;
   4332   else if (lambda_expr)
   4333     {
   4334       if (complain & tf_error)
   4335 	{
   4336 	  error ("%qD is not captured", decl);
   4337 	  tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
   4338 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
   4339 	    inform (location_of (closure),
   4340 		    "the lambda has no capture-default");
   4341 	  else if (TYPE_CLASS_SCOPE_P (closure))
   4342 	    inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
   4343 		    "capture variables from the enclosing context",
   4344 		    TYPE_CONTEXT (closure));
   4345 	  inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
   4346 	}
   4347       return error_mark_node;
   4348     }
   4349   else if (processing_contract_condition && (TREE_CODE (decl) == PARM_DECL))
   4350     /* Use of a parameter in a contract condition is fine.  */
   4351     return decl;
   4352   else
   4353     {
   4354       if (complain & tf_error)
   4355 	{
   4356 	  error (VAR_P (decl)
   4357 		 ? G_("use of local variable with automatic storage from "
   4358 		      "containing function")
   4359 		 : G_("use of parameter from containing function"));
   4360 	  inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
   4361 	}
   4362       return error_mark_node;
   4363     }
   4364   return decl;
   4365 }
   4366 
   4367 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
   4368    id-expression.  (See cp_parser_id_expression for details.)  SCOPE,
   4369    if non-NULL, is the type or namespace used to explicitly qualify
   4370    ID_EXPRESSION.  DECL is the entity to which that name has been
   4371    resolved.
   4372 
   4373    *CONSTANT_EXPRESSION_P is true if we are presently parsing a
   4374    constant-expression.  In that case, *NON_CONSTANT_EXPRESSION_P will
   4375    be set to true if this expression isn't permitted in a
   4376    constant-expression, but it is otherwise not set by this function.
   4377    *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
   4378    constant-expression, but a non-constant expression is also
   4379    permissible.
   4380 
   4381    DONE is true if this expression is a complete postfix-expression;
   4382    it is false if this expression is followed by '->', '[', '(', etc.
   4383    ADDRESS_P is true iff this expression is the operand of '&'.
   4384    TEMPLATE_P is true iff the qualified-id was of the form
   4385    "A::template B".  TEMPLATE_ARG_P is true iff this qualified name
   4386    appears as a template argument.
   4387 
   4388    If an error occurs, and it is the kind of error that might cause
   4389    the parser to abort a tentative parse, *ERROR_MSG is filled in.  It
   4390    is the caller's responsibility to issue the message.  *ERROR_MSG
   4391    will be a string with static storage duration, so the caller need
   4392    not "free" it.
   4393 
   4394    Return an expression for the entity, after issuing appropriate
   4395    diagnostics.  This function is also responsible for transforming a
   4396    reference to a non-static member into a COMPONENT_REF that makes
   4397    the use of "this" explicit.
   4398 
   4399    Upon return, *IDK will be filled in appropriately.  */
   4400 static cp_expr
   4401 finish_id_expression_1 (tree id_expression,
   4402 			tree decl,
   4403 			tree scope,
   4404 			cp_id_kind *idk,
   4405 			bool integral_constant_expression_p,
   4406 			bool allow_non_integral_constant_expression_p,
   4407 			bool *non_integral_constant_expression_p,
   4408 			bool template_p,
   4409 			bool done,
   4410 			bool address_p,
   4411 			bool template_arg_p,
   4412 			const char **error_msg,
   4413 			location_t location)
   4414 {
   4415   decl = strip_using_decl (decl);
   4416 
   4417   /* Initialize the output parameters.  */
   4418   *idk = CP_ID_KIND_NONE;
   4419   *error_msg = NULL;
   4420 
   4421   if (id_expression == error_mark_node)
   4422     return error_mark_node;
   4423   /* If we have a template-id, then no further lookup is
   4424      required.  If the template-id was for a template-class, we
   4425      will sometimes have a TYPE_DECL at this point.  */
   4426   else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
   4427 	   || TREE_CODE (decl) == TYPE_DECL)
   4428     ;
   4429   /* Look up the name.  */
   4430   else
   4431     {
   4432       if (decl == error_mark_node)
   4433 	{
   4434 	  /* Name lookup failed.  */
   4435 	  if (scope
   4436 	      && (!TYPE_P (scope)
   4437 		  || (!dependent_type_p (scope)
   4438 		      && !(identifier_p (id_expression)
   4439 			   && IDENTIFIER_CONV_OP_P (id_expression)
   4440 			   && dependent_type_p (TREE_TYPE (id_expression))))))
   4441 	    {
   4442 	      /* If the qualifying type is non-dependent (and the name
   4443 		 does not name a conversion operator to a dependent
   4444 		 type), issue an error.  */
   4445 	      qualified_name_lookup_error (scope, id_expression, decl, location);
   4446 	      return error_mark_node;
   4447 	    }
   4448 	  else if (!scope)
   4449 	    {
   4450 	      /* It may be resolved via Koenig lookup.  */
   4451 	      *idk = CP_ID_KIND_UNQUALIFIED;
   4452 	      return id_expression;
   4453 	    }
   4454 	  else
   4455 	    decl = id_expression;
   4456 	}
   4457 
   4458       /* Remember that the name was used in the definition of
   4459 	 the current class so that we can check later to see if
   4460 	 the meaning would have been different after the class
   4461 	 was entirely defined.  */
   4462       if (!scope && decl != error_mark_node && identifier_p (id_expression))
   4463 	maybe_note_name_used_in_class (id_expression, decl);
   4464 
   4465       /* A use in unevaluated operand might not be instantiated appropriately
   4466 	 if tsubst_copy builds a dummy parm, or if we never instantiate a
   4467 	 generic lambda, so mark it now.  */
   4468       if (processing_template_decl && cp_unevaluated_operand)
   4469 	mark_type_use (decl);
   4470 
   4471       /* Disallow uses of local variables from containing functions, except
   4472 	 within lambda-expressions.  */
   4473       if (outer_automatic_var_p (decl))
   4474 	{
   4475 	  decl = process_outer_var_ref (decl, tf_warning_or_error);
   4476 	  if (decl == error_mark_node)
   4477 	    return error_mark_node;
   4478 	}
   4479 
   4480       /* Also disallow uses of function parameters outside the function
   4481 	 body, except inside an unevaluated context (i.e. decltype).  */
   4482       if (TREE_CODE (decl) == PARM_DECL
   4483 	  && DECL_CONTEXT (decl) == NULL_TREE
   4484 	  && !CONSTRAINT_VAR_P (decl)
   4485 	  && !cp_unevaluated_operand
   4486 	  && !processing_contract_condition)
   4487 	{
   4488 	  *error_msg = G_("use of parameter outside function body");
   4489 	  return error_mark_node;
   4490 	}
   4491     }
   4492 
   4493   /* If we didn't find anything, or what we found was a type,
   4494      then this wasn't really an id-expression.  */
   4495   if (TREE_CODE (decl) == TEMPLATE_DECL
   4496       && !DECL_FUNCTION_TEMPLATE_P (decl))
   4497     {
   4498       *error_msg = G_("missing template arguments");
   4499       return error_mark_node;
   4500     }
   4501   else if (TREE_CODE (decl) == TYPE_DECL
   4502 	   || TREE_CODE (decl) == NAMESPACE_DECL)
   4503     {
   4504       *error_msg = G_("expected primary-expression");
   4505       return error_mark_node;
   4506     }
   4507 
   4508   /* If the name resolved to a template parameter, there is no
   4509      need to look it up again later.  */
   4510   if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
   4511       || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
   4512     {
   4513       tree r;
   4514 
   4515       *idk = CP_ID_KIND_NONE;
   4516       if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
   4517 	decl = TEMPLATE_PARM_DECL (decl);
   4518       r = DECL_INITIAL (decl);
   4519       if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
   4520 	{
   4521 	  /* If the entity is a template parameter object for a template
   4522 	     parameter of type T, the type of the expression is const T.  */
   4523 	  tree ctype = TREE_TYPE (r);
   4524 	  ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
   4525 						   | TYPE_QUAL_CONST));
   4526 	  r = build1 (VIEW_CONVERT_EXPR, ctype, r);
   4527 	}
   4528       r = convert_from_reference (r);
   4529       if (integral_constant_expression_p
   4530 	  && !dependent_type_p (TREE_TYPE (decl))
   4531 	  && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
   4532 	{
   4533 	  if (!allow_non_integral_constant_expression_p)
   4534 	    error ("template parameter %qD of type %qT is not allowed in "
   4535 		   "an integral constant expression because it is not of "
   4536 		   "integral or enumeration type", decl, TREE_TYPE (decl));
   4537 	  *non_integral_constant_expression_p = true;
   4538 	}
   4539       return r;
   4540     }
   4541   else if (TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE)
   4542     {
   4543       gcc_checking_assert (scope);
   4544       *idk = CP_ID_KIND_QUALIFIED;
   4545       cp_warn_deprecated_use_scopes (scope);
   4546       decl = finish_qualified_id_expr (scope, decl, done, address_p,
   4547 				       template_p, template_arg_p,
   4548 				       tf_warning_or_error);
   4549     }
   4550   else
   4551     {
   4552       if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
   4553 	  && variable_template_p (TREE_OPERAND (decl, 0))
   4554 	  && !concept_check_p (decl))
   4555 	/* Try resolving this variable TEMPLATE_ID_EXPR (which is always
   4556 	   considered type-dependent) now, so that the dependence test that
   4557 	   follows gives us the right answer: if it represents a non-dependent
   4558 	   variable template-id then finish_template_variable will yield the
   4559 	   corresponding non-dependent VAR_DECL.  */
   4560 	decl = finish_template_variable (decl);
   4561 
   4562       bool dependent_p = type_dependent_expression_p (decl);
   4563 
   4564       /* If the declaration was explicitly qualified indicate
   4565 	 that.  The semantics of `A::f(3)' are different than
   4566 	 `f(3)' if `f' is virtual.  */
   4567       *idk = (scope
   4568 	      ? CP_ID_KIND_QUALIFIED
   4569 	      : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
   4570 		 ? CP_ID_KIND_TEMPLATE_ID
   4571 		 : (dependent_p
   4572 		    ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
   4573 		    : CP_ID_KIND_UNQUALIFIED)));
   4574 
   4575       if (dependent_p
   4576 	  && !scope
   4577 	  && DECL_P (decl)
   4578 	  && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
   4579 	/* Dependent type attributes on the decl mean that the TREE_TYPE is
   4580 	   wrong, so just return the identifier.  */
   4581 	return id_expression;
   4582 
   4583       if (DECL_CLASS_TEMPLATE_P (decl))
   4584 	{
   4585 	  error ("use of class template %qT as expression", decl);
   4586 	  return error_mark_node;
   4587 	}
   4588 
   4589       if (TREE_CODE (decl) == TREE_LIST)
   4590 	{
   4591 	  /* Ambiguous reference to base members.  */
   4592 	  error ("request for member %qD is ambiguous in "
   4593 		 "multiple inheritance lattice", id_expression);
   4594 	  print_candidates (decl);
   4595 	  return error_mark_node;
   4596 	}
   4597 
   4598       /* Mark variable-like entities as used.  Functions are similarly
   4599 	 marked either below or after overload resolution.  */
   4600       if ((VAR_P (decl)
   4601 	   || TREE_CODE (decl) == PARM_DECL
   4602 	   || TREE_CODE (decl) == CONST_DECL
   4603 	   || TREE_CODE (decl) == RESULT_DECL)
   4604 	  && !mark_used (decl))
   4605 	return error_mark_node;
   4606 
   4607       /* Only certain kinds of names are allowed in constant
   4608 	 expression.  Template parameters have already
   4609 	 been handled above.  */
   4610       if (! error_operand_p (decl)
   4611 	  && !dependent_p
   4612 	  && integral_constant_expression_p
   4613 	  && !decl_constant_var_p (decl)
   4614 	  && TREE_CODE (decl) != CONST_DECL
   4615 	  && !builtin_valid_in_constant_expr_p (decl)
   4616 	  && !concept_check_p (decl))
   4617 	{
   4618 	  if (!allow_non_integral_constant_expression_p)
   4619 	    {
   4620 	      error ("%qD cannot appear in a constant-expression", decl);
   4621 	      return error_mark_node;
   4622 	    }
   4623 	  *non_integral_constant_expression_p = true;
   4624 	}
   4625 
   4626       if (tree wrap = maybe_get_tls_wrapper_call (decl))
   4627 	/* Replace an evaluated use of the thread_local variable with
   4628 	   a call to its wrapper.  */
   4629 	decl = wrap;
   4630       else if (concept_check_p (decl))
   4631 	{
   4632 	  /* Nothing more to do. All of the analysis for concept checks
   4633 	     is done by build_conept_id, called from the parser.  */
   4634 	}
   4635       else if (scope)
   4636 	{
   4637 	  if (TREE_CODE (decl) == SCOPE_REF)
   4638 	    {
   4639 	      gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
   4640 	      decl = TREE_OPERAND (decl, 1);
   4641 	    }
   4642 
   4643 	  decl = (adjust_result_of_qualified_name_lookup
   4644 		  (decl, scope, current_nonlambda_class_type()));
   4645 
   4646 	  cp_warn_deprecated_use_scopes (scope);
   4647 
   4648 	  if (TYPE_P (scope))
   4649 	    decl = finish_qualified_id_expr (scope,
   4650 					     decl,
   4651 					     done,
   4652 					     address_p,
   4653 					     template_p,
   4654 					     template_arg_p,
   4655 					     tf_warning_or_error);
   4656 	  else
   4657 	    decl = convert_from_reference (decl);
   4658 	}
   4659       else if (TREE_CODE (decl) == FIELD_DECL)
   4660 	{
   4661 	  /* Since SCOPE is NULL here, this is an unqualified name.
   4662 	     Access checking has been performed during name lookup
   4663 	     already.  Turn off checking to avoid duplicate errors.  */
   4664 	  push_deferring_access_checks (dk_no_check);
   4665 	  decl = finish_non_static_data_member (decl, NULL_TREE,
   4666 						/*qualifying_scope=*/NULL_TREE);
   4667 	  pop_deferring_access_checks ();
   4668 	}
   4669       else if (is_overloaded_fn (decl))
   4670 	{
   4671 	  /* We only need to look at the first function,
   4672 	     because all the fns share the attribute we're
   4673 	     concerned with (all member fns or all non-members).  */
   4674 	  tree first_fn = get_first_fn (decl);
   4675 	  first_fn = STRIP_TEMPLATE (first_fn);
   4676 
   4677 	  if (!template_arg_p
   4678 	      && (TREE_CODE (first_fn) == USING_DECL
   4679 		  || (TREE_CODE (first_fn) == FUNCTION_DECL
   4680 		      && DECL_FUNCTION_MEMBER_P (first_fn)
   4681 		      && !shared_member_p (decl))))
   4682 	    {
   4683 	      /* A set of member functions.  */
   4684 	      decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
   4685 	      return finish_class_member_access_expr (decl, id_expression,
   4686 						      /*template_p=*/false,
   4687 						      tf_warning_or_error);
   4688 	    }
   4689 
   4690 	  decl = baselink_for_fns (decl);
   4691 	}
   4692       else
   4693 	{
   4694 	  if (DECL_P (decl) && DECL_NONLOCAL (decl)
   4695 	      && DECL_CLASS_SCOPE_P (decl))
   4696 	    {
   4697 	      tree context = context_for_name_lookup (decl);
   4698 	      if (context != current_class_type)
   4699 		{
   4700 		  tree path = currently_open_derived_class (context);
   4701 		  if (!path)
   4702 		    /* PATH can be null for using an enum of an unrelated
   4703 		       class; we checked its access in lookup_using_decl.
   4704 
   4705 		       ??? Should this case make a clone instead, like
   4706 		       handle_using_decl?  */
   4707 		    gcc_assert (TREE_CODE (decl) == CONST_DECL);
   4708 		  else
   4709 		    perform_or_defer_access_check (TYPE_BINFO (path),
   4710 						   decl, decl,
   4711 						   tf_warning_or_error);
   4712 		}
   4713 	    }
   4714 
   4715 	  decl = convert_from_reference (decl);
   4716 	}
   4717     }
   4718 
   4719   return cp_expr (decl, location);
   4720 }
   4721 
   4722 /* As per finish_id_expression_1, but adding a wrapper node
   4723    around the result if needed to express LOCATION.  */
   4724 
   4725 cp_expr
   4726 finish_id_expression (tree id_expression,
   4727 		      tree decl,
   4728 		      tree scope,
   4729 		      cp_id_kind *idk,
   4730 		      bool integral_constant_expression_p,
   4731 		      bool allow_non_integral_constant_expression_p,
   4732 		      bool *non_integral_constant_expression_p,
   4733 		      bool template_p,
   4734 		      bool done,
   4735 		      bool address_p,
   4736 		      bool template_arg_p,
   4737 		      const char **error_msg,
   4738 		      location_t location)
   4739 {
   4740   cp_expr result
   4741     = finish_id_expression_1 (id_expression, decl, scope, idk,
   4742 			      integral_constant_expression_p,
   4743 			      allow_non_integral_constant_expression_p,
   4744 			      non_integral_constant_expression_p,
   4745 			      template_p, done, address_p, template_arg_p,
   4746 			      error_msg, location);
   4747   return result.maybe_add_location_wrapper ();
   4748 }
   4749 
   4750 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
   4751    use as a type-specifier.  */
   4752 
   4753 tree
   4754 finish_typeof (tree expr)
   4755 {
   4756   tree type;
   4757 
   4758   if (type_dependent_expression_p (expr))
   4759     {
   4760       type = cxx_make_type (TYPEOF_TYPE);
   4761       TYPEOF_TYPE_EXPR (type) = expr;
   4762       SET_TYPE_STRUCTURAL_EQUALITY (type);
   4763 
   4764       return type;
   4765     }
   4766 
   4767   expr = mark_type_use (expr);
   4768 
   4769   type = unlowered_expr_type (expr);
   4770 
   4771   if (!type || type == unknown_type_node)
   4772     {
   4773       error ("type of %qE is unknown", expr);
   4774       return error_mark_node;
   4775     }
   4776 
   4777   return type;
   4778 }
   4779 
   4780 /* Implement the __underlying_type keyword: Return the underlying
   4781    type of TYPE, suitable for use as a type-specifier.  */
   4782 
   4783 tree
   4784 finish_underlying_type (tree type)
   4785 {
   4786   if (!complete_type_or_else (type, NULL_TREE))
   4787     return error_mark_node;
   4788 
   4789   if (TREE_CODE (type) != ENUMERAL_TYPE)
   4790     {
   4791       error ("%qT is not an enumeration type", type);
   4792       return error_mark_node;
   4793     }
   4794 
   4795   tree underlying_type = ENUM_UNDERLYING_TYPE (type);
   4796 
   4797   /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
   4798      includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
   4799      See finish_enum_value_list for details.  */
   4800   if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
   4801     underlying_type
   4802       = c_common_type_for_mode (TYPE_MODE (underlying_type),
   4803 				TYPE_UNSIGNED (underlying_type));
   4804 
   4805   return underlying_type;
   4806 }
   4807 
   4808 /* Implement the __type_pack_element keyword: Return the type
   4809    at index IDX within TYPES.  */
   4810 
   4811 static tree
   4812 finish_type_pack_element (tree idx, tree types, tsubst_flags_t complain)
   4813 {
   4814   idx = maybe_constant_value (idx);
   4815   if (TREE_CODE (idx) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (idx)))
   4816     {
   4817       if (complain & tf_error)
   4818 	error ("%<__type_pack_element%> index is not an integral constant");
   4819       return error_mark_node;
   4820     }
   4821   if (tree_int_cst_sgn (idx) < 0)
   4822     {
   4823       if (complain & tf_error)
   4824 	error ("%<__type_pack_element%> index is negative");
   4825       return error_mark_node;
   4826     }
   4827   if (wi::to_widest (idx) >= TREE_VEC_LENGTH (types))
   4828     {
   4829       if (complain & tf_error)
   4830 	error ("%<__type_pack_element%> index is out of range");
   4831       return error_mark_node;
   4832     }
   4833   return TREE_VEC_ELT (types, tree_to_shwi (idx));
   4834 }
   4835 
   4836 /* Implement the __direct_bases keyword: Return the direct base classes
   4837    of type.  */
   4838 
   4839 tree
   4840 calculate_direct_bases (tree type, tsubst_flags_t complain)
   4841 {
   4842   if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
   4843       || !NON_UNION_CLASS_TYPE_P (type))
   4844     return make_tree_vec (0);
   4845 
   4846   releasing_vec vector;
   4847   vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
   4848   tree binfo;
   4849   unsigned i;
   4850 
   4851   /* Virtual bases are initialized first */
   4852   for (i = 0; base_binfos->iterate (i, &binfo); i++)
   4853     if (BINFO_VIRTUAL_P (binfo))
   4854       vec_safe_push (vector, binfo);
   4855 
   4856   /* Now non-virtuals */
   4857   for (i = 0; base_binfos->iterate (i, &binfo); i++)
   4858     if (!BINFO_VIRTUAL_P (binfo))
   4859       vec_safe_push (vector, binfo);
   4860 
   4861   tree bases_vec = make_tree_vec (vector->length ());
   4862 
   4863   for (i = 0; i < vector->length (); ++i)
   4864     TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
   4865 
   4866   return bases_vec;
   4867 }
   4868 
   4869 /* Implement the __bases keyword: Return the base classes
   4870    of type */
   4871 
   4872 /* Find morally non-virtual base classes by walking binfo hierarchy */
   4873 /* Virtual base classes are handled separately in finish_bases */
   4874 
   4875 static tree
   4876 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
   4877 {
   4878   /* Don't walk bases of virtual bases */
   4879   return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
   4880 }
   4881 
   4882 static tree
   4883 dfs_calculate_bases_post (tree binfo, void *data_)
   4884 {
   4885   vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
   4886   if (!BINFO_VIRTUAL_P (binfo))
   4887     vec_safe_push (*data, BINFO_TYPE (binfo));
   4888   return NULL_TREE;
   4889 }
   4890 
   4891 /* Calculates the morally non-virtual base classes of a class */
   4892 static vec<tree, va_gc> *
   4893 calculate_bases_helper (tree type)
   4894 {
   4895   vec<tree, va_gc> *vector = make_tree_vector ();
   4896 
   4897   /* Now add non-virtual base classes in order of construction */
   4898   if (TYPE_BINFO (type))
   4899     dfs_walk_all (TYPE_BINFO (type),
   4900 		  dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
   4901   return vector;
   4902 }
   4903 
   4904 tree
   4905 calculate_bases (tree type, tsubst_flags_t complain)
   4906 {
   4907   if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
   4908       || !NON_UNION_CLASS_TYPE_P (type))
   4909     return make_tree_vec (0);
   4910 
   4911   releasing_vec vector;
   4912   tree bases_vec = NULL_TREE;
   4913   unsigned i;
   4914   vec<tree, va_gc> *vbases;
   4915   tree binfo;
   4916 
   4917   /* First go through virtual base classes */
   4918   for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
   4919        vec_safe_iterate (vbases, i, &binfo); i++)
   4920     {
   4921       releasing_vec vbase_bases
   4922 	= calculate_bases_helper (BINFO_TYPE (binfo));
   4923       vec_safe_splice (vector, vbase_bases);
   4924     }
   4925 
   4926   /* Now for the non-virtual bases */
   4927   releasing_vec nonvbases = calculate_bases_helper (type);
   4928   vec_safe_splice (vector, nonvbases);
   4929 
   4930   /* Note that during error recovery vector->length can even be zero.  */
   4931   if (vector->length () > 1)
   4932     {
   4933       /* Last element is entire class, so don't copy */
   4934       bases_vec = make_tree_vec (vector->length () - 1);
   4935 
   4936       for (i = 0; i < vector->length () - 1; ++i)
   4937 	TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
   4938     }
   4939   else
   4940     bases_vec = make_tree_vec (0);
   4941 
   4942   return bases_vec;
   4943 }
   4944 
   4945 tree
   4946 finish_bases (tree type, bool direct)
   4947 {
   4948   tree bases = NULL_TREE;
   4949 
   4950   if (!processing_template_decl)
   4951     {
   4952       /* Parameter packs can only be used in templates */
   4953       error ("parameter pack %<__bases%> only valid in template declaration");
   4954       return error_mark_node;
   4955     }
   4956 
   4957   bases = cxx_make_type (BASES);
   4958   BASES_TYPE (bases) = type;
   4959   BASES_DIRECT (bases) = direct;
   4960   SET_TYPE_STRUCTURAL_EQUALITY (bases);
   4961 
   4962   return bases;
   4963 }
   4964 
   4965 /* Perform C++-specific checks for __builtin_offsetof before calling
   4966    fold_offsetof.  */
   4967 
   4968 tree
   4969 finish_offsetof (tree object_ptr, tree expr, location_t loc)
   4970 {
   4971   /* If we're processing a template, we can't finish the semantics yet.
   4972      Otherwise we can fold the entire expression now.  */
   4973   if (processing_template_decl)
   4974     {
   4975       expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
   4976       SET_EXPR_LOCATION (expr, loc);
   4977       return expr;
   4978     }
   4979 
   4980   if (expr == error_mark_node)
   4981     return error_mark_node;
   4982 
   4983   if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
   4984     {
   4985       error ("cannot apply %<offsetof%> to destructor %<~%T%>",
   4986 	      TREE_OPERAND (expr, 2));
   4987       return error_mark_node;
   4988     }
   4989   if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr))
   4990       || TREE_TYPE (expr) == unknown_type_node)
   4991     {
   4992       while (TREE_CODE (expr) == COMPONENT_REF
   4993 	     || TREE_CODE (expr) == COMPOUND_EXPR)
   4994 	expr = TREE_OPERAND (expr, 1);
   4995 
   4996       if (DECL_P (expr))
   4997 	{
   4998 	  error ("cannot apply %<offsetof%> to member function %qD", expr);
   4999 	  inform (DECL_SOURCE_LOCATION (expr), "declared here");
   5000 	}
   5001       else
   5002 	error ("cannot apply %<offsetof%> to member function");
   5003       return error_mark_node;
   5004     }
   5005   if (TREE_CODE (expr) == CONST_DECL)
   5006     {
   5007       error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
   5008       return error_mark_node;
   5009     }
   5010   if (REFERENCE_REF_P (expr))
   5011     expr = TREE_OPERAND (expr, 0);
   5012   if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
   5013     return error_mark_node;
   5014   if (warn_invalid_offsetof
   5015       && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
   5016       && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
   5017       && cp_unevaluated_operand == 0)
   5018     warning_at (loc, OPT_Winvalid_offsetof, "%<offsetof%> within "
   5019 		"non-standard-layout type %qT is conditionally-supported",
   5020 		TREE_TYPE (TREE_TYPE (object_ptr)));
   5021   return fold_offsetof (expr);
   5022 }
   5023 
   5024 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR.  This
   5025    function is broken out from the above for the benefit of the tree-ssa
   5026    project.  */
   5027 
   5028 void
   5029 simplify_aggr_init_expr (tree *tp)
   5030 {
   5031   tree aggr_init_expr = *tp;
   5032 
   5033   /* Form an appropriate CALL_EXPR.  */
   5034   tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
   5035   tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
   5036   tree type = TREE_TYPE (slot);
   5037 
   5038   tree call_expr;
   5039   enum style_t { ctor, arg, pcc } style;
   5040 
   5041   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
   5042     style = ctor;
   5043 #ifdef PCC_STATIC_STRUCT_RETURN
   5044   else if (1)
   5045     style = pcc;
   5046 #endif
   5047   else
   5048     {
   5049       gcc_assert (TREE_ADDRESSABLE (type));
   5050       style = arg;
   5051     }
   5052 
   5053   call_expr = build_call_array_loc (input_location,
   5054 				    TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
   5055 				    fn,
   5056 				    aggr_init_expr_nargs (aggr_init_expr),
   5057 				    AGGR_INIT_EXPR_ARGP (aggr_init_expr));
   5058   TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
   5059   CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
   5060   CALL_EXPR_OPERATOR_SYNTAX (call_expr)
   5061     = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
   5062   CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
   5063   CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
   5064 
   5065   if (style == ctor)
   5066     {
   5067       /* Replace the first argument to the ctor with the address of the
   5068 	 slot.  */
   5069       cxx_mark_addressable (slot);
   5070       CALL_EXPR_ARG (call_expr, 0) =
   5071 	build1 (ADDR_EXPR, build_pointer_type (type), slot);
   5072     }
   5073   else if (style == arg)
   5074     {
   5075       /* Just mark it addressable here, and leave the rest to
   5076 	 expand_call{,_inline}.  */
   5077       cxx_mark_addressable (slot);
   5078       CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
   5079       call_expr = cp_build_init_expr (slot, call_expr);
   5080     }
   5081   else if (style == pcc)
   5082     {
   5083       /* If we're using the non-reentrant PCC calling convention, then we
   5084 	 need to copy the returned value out of the static buffer into the
   5085 	 SLOT.  */
   5086       push_deferring_access_checks (dk_no_check);
   5087       call_expr = build_aggr_init (slot, call_expr,
   5088 				   DIRECT_BIND | LOOKUP_ONLYCONVERTING,
   5089                                    tf_warning_or_error);
   5090       pop_deferring_access_checks ();
   5091       call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
   5092     }
   5093 
   5094   if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
   5095     {
   5096       tree init = build_zero_init (type, NULL_TREE,
   5097 				   /*static_storage_p=*/false);
   5098       init = cp_build_init_expr (slot, init);
   5099       call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
   5100 			  init, call_expr);
   5101     }
   5102 
   5103   *tp = call_expr;
   5104 }
   5105 
   5106 /* Emit all thunks to FN that should be emitted when FN is emitted.  */
   5107 
   5108 void
   5109 emit_associated_thunks (tree fn)
   5110 {
   5111   /* When we use vcall offsets, we emit thunks with the virtual
   5112      functions to which they thunk. The whole point of vcall offsets
   5113      is so that you can know statically the entire set of thunks that
   5114      will ever be needed for a given virtual function, thereby
   5115      enabling you to output all the thunks with the function itself.  */
   5116   if (DECL_VIRTUAL_P (fn)
   5117       /* Do not emit thunks for extern template instantiations.  */
   5118       && ! DECL_REALLY_EXTERN (fn)
   5119       /* Do not emit thunks for tentative decls, those will be processed
   5120 	 again at_eof if really needed.  */
   5121       && (DECL_INTERFACE_KNOWN (fn) || !DECL_DEFER_OUTPUT (fn)))
   5122     {
   5123       tree thunk;
   5124 
   5125       for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
   5126 	{
   5127 	  if (!THUNK_ALIAS (thunk))
   5128 	    {
   5129 	      use_thunk (thunk, /*emit_p=*/1);
   5130 	      if (DECL_RESULT_THUNK_P (thunk))
   5131 		{
   5132 		  tree probe;
   5133 
   5134 		  for (probe = DECL_THUNKS (thunk);
   5135 		       probe; probe = DECL_CHAIN (probe))
   5136 		    use_thunk (probe, /*emit_p=*/1);
   5137 		}
   5138 	    }
   5139 	  else
   5140 	    gcc_assert (!DECL_THUNKS (thunk));
   5141 	}
   5142     }
   5143 }
   5144 
   5145 /* Generate RTL for FN.  */
   5146 
   5147 bool
   5148 expand_or_defer_fn_1 (tree fn)
   5149 {
   5150   /* When the parser calls us after finishing the body of a template
   5151      function, we don't really want to expand the body.  */
   5152   if (processing_template_decl)
   5153     {
   5154       /* Normally, collection only occurs in rest_of_compilation.  So,
   5155 	 if we don't collect here, we never collect junk generated
   5156 	 during the processing of templates until we hit a
   5157 	 non-template function.  It's not safe to do this inside a
   5158 	 nested class, though, as the parser may have local state that
   5159 	 is not a GC root.  */
   5160       if (!function_depth)
   5161 	ggc_collect ();
   5162       return false;
   5163     }
   5164 
   5165   gcc_assert (DECL_SAVED_TREE (fn));
   5166 
   5167   /* We make a decision about linkage for these functions at the end
   5168      of the compilation.  Until that point, we do not want the back
   5169      end to output them -- but we do want it to see the bodies of
   5170      these functions so that it can inline them as appropriate.  */
   5171   if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
   5172     {
   5173       if (DECL_INTERFACE_KNOWN (fn))
   5174 	/* We've already made a decision as to how this function will
   5175 	   be handled.  */;
   5176       else if (!at_eof
   5177 	       || DECL_IMMEDIATE_FUNCTION_P (fn)
   5178 	       || DECL_OMP_DECLARE_REDUCTION_P (fn))
   5179 	tentative_decl_linkage (fn);
   5180       else
   5181 	import_export_decl (fn);
   5182 
   5183       /* If the user wants us to keep all inline functions, then mark
   5184 	 this function as needed so that finish_file will make sure to
   5185 	 output it later.  Similarly, all dllexport'd functions must
   5186 	 be emitted; there may be callers in other DLLs.  */
   5187       if (DECL_DECLARED_INLINE_P (fn)
   5188 	  && !DECL_REALLY_EXTERN (fn)
   5189 	  && !DECL_IMMEDIATE_FUNCTION_P (fn)
   5190 	  && !DECL_OMP_DECLARE_REDUCTION_P (fn)
   5191 	  && (flag_keep_inline_functions
   5192 	      || (flag_keep_inline_dllexport
   5193 		  && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
   5194 	{
   5195 	  mark_needed (fn);
   5196 	  DECL_EXTERNAL (fn) = 0;
   5197 	}
   5198     }
   5199 
   5200   /* If this is a constructor or destructor body, we have to clone
   5201      it.  */
   5202   if (maybe_clone_body (fn))
   5203     {
   5204       /* We don't want to process FN again, so pretend we've written
   5205 	 it out, even though we haven't.  */
   5206       TREE_ASM_WRITTEN (fn) = 1;
   5207       /* If this is a constexpr function we still need the body to be
   5208 	 able to evaluate it.  Similarly, with modules we only stream
   5209 	 the maybe-in-charge cdtor and regenerate the clones from it on
   5210 	 demand, so we also need to keep the body.  Otherwise we don't
   5211 	 need it anymore.  */
   5212       if (!DECL_DECLARED_CONSTEXPR_P (fn)
   5213 	  && !(modules_p () && vague_linkage_p (fn)))
   5214 	DECL_SAVED_TREE (fn) = NULL_TREE;
   5215       return false;
   5216     }
   5217 
   5218   /* There's no reason to do any of the work here if we're only doing
   5219      semantic analysis; this code just generates RTL.  */
   5220   if (flag_syntax_only)
   5221     {
   5222       /* Pretend that this function has been written out so that we don't try
   5223 	 to expand it again.  */
   5224       TREE_ASM_WRITTEN (fn) = 1;
   5225       return false;
   5226     }
   5227 
   5228   if (DECL_OMP_DECLARE_REDUCTION_P (fn))
   5229     return false;
   5230 
   5231   return true;
   5232 }
   5233 
   5234 void
   5235 expand_or_defer_fn (tree fn)
   5236 {
   5237   if (expand_or_defer_fn_1 (fn))
   5238     {
   5239       function_depth++;
   5240 
   5241       /* Expand or defer, at the whim of the compilation unit manager.  */
   5242       cgraph_node::finalize_function (fn, function_depth > 1);
   5243       emit_associated_thunks (fn);
   5244 
   5245       function_depth--;
   5246 
   5247       if (DECL_IMMEDIATE_FUNCTION_P (fn))
   5248 	{
   5249 	  if (cgraph_node *node = cgraph_node::get (fn))
   5250 	    {
   5251 	      node->body_removed = true;
   5252 	      node->analyzed = false;
   5253 	      node->definition = false;
   5254 	      node->force_output = false;
   5255 	    }
   5256 	}
   5257     }
   5258 }
   5259 
   5260 class nrv_data
   5261 {
   5262 public:
   5263   nrv_data () : visited (37) {}
   5264 
   5265   tree var;
   5266   tree result;
   5267   hash_set<tree> visited;
   5268   bool simple;
   5269   bool in_nrv_cleanup;
   5270 };
   5271 
   5272 /* Helper function for walk_tree, used by finalize_nrv below.  */
   5273 
   5274 static tree
   5275 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
   5276 {
   5277   class nrv_data *dp = (class nrv_data *)data;
   5278 
   5279   /* No need to walk into types.  There wouldn't be any need to walk into
   5280      non-statements, except that we have to consider STMT_EXPRs.  */
   5281   if (TYPE_P (*tp))
   5282     *walk_subtrees = 0;
   5283 
   5284   /* Replace all uses of the NRV with the RESULT_DECL.  */
   5285   else if (*tp == dp->var)
   5286     *tp = dp->result;
   5287 
   5288   /* Avoid walking into the same tree more than once.  Unfortunately, we
   5289      can't just use walk_tree_without duplicates because it would only call
   5290      us for the first occurrence of dp->var in the function body.  */
   5291   else if (dp->visited.add (*tp))
   5292     *walk_subtrees = 0;
   5293 
   5294   /* If there's a label, we might need to destroy the NRV on goto (92407).  */
   5295   else if (TREE_CODE (*tp) == LABEL_EXPR && !dp->in_nrv_cleanup)
   5296     dp->simple = false;
   5297   /* Change NRV returns to just refer to the RESULT_DECL; this is a nop,
   5298      but differs from using NULL_TREE in that it indicates that we care
   5299      about the value of the RESULT_DECL.  But preserve anything appended
   5300      by check_return_expr.  */
   5301   else if (TREE_CODE (*tp) == RETURN_EXPR)
   5302     {
   5303       tree *p = &TREE_OPERAND (*tp, 0);
   5304       while (TREE_CODE (*p) == COMPOUND_EXPR)
   5305 	p = &TREE_OPERAND (*p, 0);
   5306       if (TREE_CODE (*p) == INIT_EXPR
   5307 	  && INIT_EXPR_NRV_P (*p))
   5308 	*p = dp->result;
   5309     }
   5310   /* Change all cleanups for the NRV to only run when not returning.  */
   5311   else if (TREE_CODE (*tp) == CLEANUP_STMT
   5312 	   && CLEANUP_DECL (*tp) == dp->var)
   5313     {
   5314       dp->in_nrv_cleanup = true;
   5315       cp_walk_tree (&CLEANUP_BODY (*tp), finalize_nrv_r, data, 0);
   5316       dp->in_nrv_cleanup = false;
   5317       cp_walk_tree (&CLEANUP_EXPR (*tp), finalize_nrv_r, data, 0);
   5318       *walk_subtrees = 0;
   5319 
   5320       if (dp->simple)
   5321 	/* For a simple NRV, just run it on the EH path.  */
   5322 	CLEANUP_EH_ONLY (*tp) = true;
   5323       else
   5324 	{
   5325 	  /* Not simple, we need to check current_retval_sentinel to decide
   5326 	     whether to run it.  If it's set, we're returning normally and
   5327 	     don't want to destroy the NRV.  If the sentinel is not set, we're
   5328 	     leaving scope some other way, either by flowing off the end of its
   5329 	     scope or throwing an exception.  */
   5330 	  tree cond = build3 (COND_EXPR, void_type_node,
   5331 			      current_retval_sentinel,
   5332 			      void_node, CLEANUP_EXPR (*tp));
   5333 	  CLEANUP_EXPR (*tp) = cond;
   5334 	}
   5335 
   5336       /* If a cleanup might throw, we need to clear current_retval_sentinel on
   5337 	 the exception path, both so the check above succeeds and so an outer
   5338 	 cleanup added by maybe_splice_retval_cleanup doesn't run.  */
   5339       if (cp_function_chain->throwing_cleanup)
   5340 	{
   5341 	  tree clear = build2 (MODIFY_EXPR, boolean_type_node,
   5342 			       current_retval_sentinel,
   5343 			       boolean_false_node);
   5344 	  if (dp->simple)
   5345 	    {
   5346 	      /* We're already only on the EH path, just prepend it.  */
   5347 	      tree &exp = CLEANUP_EXPR (*tp);
   5348 	      exp = build2 (COMPOUND_EXPR, void_type_node, clear, exp);
   5349 	    }
   5350 	  else
   5351 	    {
   5352 	      /* The cleanup runs on both normal and EH paths, we need another
   5353 		 CLEANUP_STMT to clear the flag only on the EH path.  */
   5354 	      tree &bod = CLEANUP_BODY (*tp);
   5355 	      bod = build_stmt (EXPR_LOCATION (*tp), CLEANUP_STMT,
   5356 				bod, clear, current_retval_sentinel);
   5357 	      CLEANUP_EH_ONLY (bod) = true;
   5358 	    }
   5359 	}
   5360     }
   5361   /* Disable maybe_splice_retval_cleanup within the NRV cleanup scope, we don't
   5362      want to destroy the retval before the variable goes out of scope.  */
   5363   else if (TREE_CODE (*tp) == CLEANUP_STMT
   5364 	   && dp->in_nrv_cleanup
   5365 	   && CLEANUP_DECL (*tp) == dp->result)
   5366     CLEANUP_EXPR (*tp) = void_node;
   5367   /* Replace the DECL_EXPR for the NRV with an initialization of the
   5368      RESULT_DECL, if needed.  */
   5369   else if (TREE_CODE (*tp) == DECL_EXPR
   5370 	   && DECL_EXPR_DECL (*tp) == dp->var)
   5371     {
   5372       tree init;
   5373       if (DECL_INITIAL (dp->var)
   5374 	  && DECL_INITIAL (dp->var) != error_mark_node)
   5375 	init = cp_build_init_expr (dp->result,
   5376 		       DECL_INITIAL (dp->var));
   5377       else
   5378 	init = build_empty_stmt (EXPR_LOCATION (*tp));
   5379       DECL_INITIAL (dp->var) = NULL_TREE;
   5380       SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
   5381       *tp = init;
   5382     }
   5383 
   5384   /* Keep iterating.  */
   5385   return NULL_TREE;
   5386 }
   5387 
   5388 /* Called from finish_function to implement the named return value
   5389    optimization by overriding all the RETURN_EXPRs and pertinent
   5390    CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
   5391    RESULT_DECL for the function.  */
   5392 
   5393 void
   5394 finalize_nrv (tree fndecl, tree var)
   5395 {
   5396   class nrv_data data;
   5397   tree result = DECL_RESULT (fndecl);
   5398 
   5399   /* Copy name from VAR to RESULT.  */
   5400   DECL_NAME (result) = DECL_NAME (var);
   5401   /* Don't forget that we take its address.  */
   5402   TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
   5403   /* Finally set DECL_VALUE_EXPR to avoid assigning
   5404      a stack slot at -O0 for the original var and debug info
   5405      uses RESULT location for VAR.  */
   5406   SET_DECL_VALUE_EXPR (var, result);
   5407   DECL_HAS_VALUE_EXPR_P (var) = 1;
   5408 
   5409   data.var = var;
   5410   data.result = result;
   5411   data.in_nrv_cleanup = false;
   5412 
   5413   /* This is simpler for variables declared in the outer scope of
   5414      the function so we know that their lifetime always ends with a
   5415      return; see g++.dg/opt/nrv6.C.  */
   5416   tree outer = outer_curly_brace_block (fndecl);
   5417   data.simple = chain_member (var, BLOCK_VARS (outer));
   5418 
   5419   cp_walk_tree (&DECL_SAVED_TREE (fndecl), finalize_nrv_r, &data, 0);
   5420 }
   5421 
   5422 /* Create CP_OMP_CLAUSE_INFO for clause C.  Returns true if it is invalid.  */
   5424 
   5425 bool
   5426 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
   5427 			    bool need_copy_ctor, bool need_copy_assignment,
   5428 			    bool need_dtor)
   5429 {
   5430   int save_errorcount = errorcount;
   5431   tree info, t;
   5432 
   5433   /* Always allocate 3 elements for simplicity.  These are the
   5434      function decls for the ctor, dtor, and assignment op.
   5435      This layout is known to the three lang hooks,
   5436      cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
   5437      and cxx_omp_clause_assign_op.  */
   5438   info = make_tree_vec (3);
   5439   CP_OMP_CLAUSE_INFO (c) = info;
   5440 
   5441   if (need_default_ctor || need_copy_ctor)
   5442     {
   5443       if (need_default_ctor)
   5444 	t = get_default_ctor (type);
   5445       else
   5446 	t = get_copy_ctor (type, tf_warning_or_error);
   5447 
   5448       if (t && !trivial_fn_p (t))
   5449 	TREE_VEC_ELT (info, 0) = t;
   5450     }
   5451 
   5452   if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
   5453     TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
   5454 
   5455   if (need_copy_assignment)
   5456     {
   5457       t = get_copy_assign (type);
   5458 
   5459       if (t && !trivial_fn_p (t))
   5460 	TREE_VEC_ELT (info, 2) = t;
   5461     }
   5462 
   5463   return errorcount != save_errorcount;
   5464 }
   5465 
   5466 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
   5467    FIELD_DECL, otherwise return DECL itself.  */
   5468 
   5469 static tree
   5470 omp_clause_decl_field (tree decl)
   5471 {
   5472   if (VAR_P (decl)
   5473       && DECL_HAS_VALUE_EXPR_P (decl)
   5474       && DECL_ARTIFICIAL (decl)
   5475       && DECL_LANG_SPECIFIC (decl)
   5476       && DECL_OMP_PRIVATIZED_MEMBER (decl))
   5477     {
   5478       tree f = DECL_VALUE_EXPR (decl);
   5479       if (INDIRECT_REF_P (f))
   5480 	f = TREE_OPERAND (f, 0);
   5481       if (TREE_CODE (f) == COMPONENT_REF)
   5482 	{
   5483 	  f = TREE_OPERAND (f, 1);
   5484 	  gcc_assert (TREE_CODE (f) == FIELD_DECL);
   5485 	  return f;
   5486 	}
   5487     }
   5488   return NULL_TREE;
   5489 }
   5490 
   5491 /* Adjust DECL if needed for printing using %qE.  */
   5492 
   5493 static tree
   5494 omp_clause_printable_decl (tree decl)
   5495 {
   5496   tree t = omp_clause_decl_field (decl);
   5497   if (t)
   5498     return t;
   5499   return decl;
   5500 }
   5501 
   5502 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
   5503    VAR_DECL T that doesn't need a DECL_EXPR added, record it for
   5504    privatization.  */
   5505 
   5506 static void
   5507 omp_note_field_privatization (tree f, tree t)
   5508 {
   5509   if (!omp_private_member_map)
   5510     omp_private_member_map = new hash_map<tree, tree>;
   5511   tree &v = omp_private_member_map->get_or_insert (f);
   5512   if (v == NULL_TREE)
   5513     {
   5514       v = t;
   5515       omp_private_member_vec.safe_push (f);
   5516       /* Signal that we don't want to create DECL_EXPR for this dummy var.  */
   5517       omp_private_member_vec.safe_push (integer_zero_node);
   5518     }
   5519 }
   5520 
   5521 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
   5522    dummy VAR_DECL.  */
   5523 
   5524 tree
   5525 omp_privatize_field (tree t, bool shared)
   5526 {
   5527   tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
   5528   if (m == error_mark_node)
   5529     return error_mark_node;
   5530   if (!omp_private_member_map && !shared)
   5531     omp_private_member_map = new hash_map<tree, tree>;
   5532   if (TYPE_REF_P (TREE_TYPE (t)))
   5533     {
   5534       gcc_assert (INDIRECT_REF_P (m));
   5535       m = TREE_OPERAND (m, 0);
   5536     }
   5537   tree vb = NULL_TREE;
   5538   tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
   5539   if (v == NULL_TREE)
   5540     {
   5541       v = create_temporary_var (TREE_TYPE (m));
   5542       retrofit_lang_decl (v);
   5543       DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
   5544       SET_DECL_VALUE_EXPR (v, m);
   5545       DECL_HAS_VALUE_EXPR_P (v) = 1;
   5546       if (!shared)
   5547 	omp_private_member_vec.safe_push (t);
   5548     }
   5549   return v;
   5550 }
   5551 
   5552 /* C++ specialisation of the c_omp_address_inspector class.  */
   5553 
   5554 class cp_omp_address_inspector : public c_omp_address_inspector
   5555 {
   5556 public:
   5557   cp_omp_address_inspector (location_t loc, tree t)
   5558     : c_omp_address_inspector (loc, t)
   5559     {
   5560     }
   5561 
   5562   ~cp_omp_address_inspector ()
   5563     {
   5564     }
   5565 
   5566   bool processing_template_decl_p ()
   5567     {
   5568       return processing_template_decl;
   5569     }
   5570 
   5571   void emit_unmappable_type_notes (tree t)
   5572     {
   5573       if (TREE_TYPE (t) != error_mark_node
   5574 	  && !COMPLETE_TYPE_P (TREE_TYPE (t)))
   5575 	cxx_incomplete_type_inform (TREE_TYPE (t));
   5576     }
   5577 
   5578   tree convert_from_reference (tree x)
   5579     {
   5580       return ::convert_from_reference (x);
   5581     }
   5582 
   5583   tree build_array_ref (location_t loc, tree arr, tree idx)
   5584     {
   5585       return ::build_array_ref (loc, arr, idx);
   5586     }
   5587 
   5588   bool check_clause (tree clause)
   5589     {
   5590       if (TREE_CODE (orig) == COMPONENT_REF
   5591 	  && invalid_nonstatic_memfn_p (EXPR_LOCATION (orig), orig,
   5592 					tf_warning_or_error))
   5593 	return false;
   5594       if (!c_omp_address_inspector::check_clause (clause))
   5595 	return false;
   5596       return true;
   5597     }
   5598 };
   5599 
   5600 /* Helper function for handle_omp_array_sections.  Called recursively
   5601    to handle multiple array-section-subscripts.  C is the clause,
   5602    T current expression (initially OMP_CLAUSE_DECL), which is either
   5603    a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
   5604    expression if specified, TREE_VALUE length expression if specified,
   5605    TREE_CHAIN is what it has been specified after, or some decl.
   5606    TYPES vector is populated with array section types, MAYBE_ZERO_LEN
   5607    set to true if any of the array-section-subscript could have length
   5608    of zero (explicit or implicit), FIRST_NON_ONE is the index of the
   5609    first array-section-subscript which is known not to have length
   5610    of one.  Given say:
   5611    map(a[:b][2:1][:c][:2][:d][e:f][2:5])
   5612    FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
   5613    all are or may have length of 1, array-section-subscript [:2] is the
   5614    first one known not to have length 1.  For array-section-subscript
   5615    <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
   5616    0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
   5617    can if MAYBE_ZERO_LEN is false.  MAYBE_ZERO_LEN will be true in the above
   5618    case though, as some lengths could be zero.  */
   5619 
   5620 static tree
   5621 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
   5622 			     bool &maybe_zero_len, unsigned int &first_non_one,
   5623 			     enum c_omp_region_type ort)
   5624 {
   5625   tree ret, low_bound, length, type;
   5626   bool openacc = (ort & C_ORT_ACC) != 0;
   5627   if (TREE_CODE (t) != OMP_ARRAY_SECTION)
   5628     {
   5629       if (error_operand_p (t))
   5630 	return error_mark_node;
   5631 
   5632       cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
   5633       tree t_refto = ai.maybe_unconvert_ref (t);
   5634 
   5635       if (!ai.check_clause (c))
   5636 	return error_mark_node;
   5637       else if (ai.component_access_p ()
   5638 	       && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   5639 		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
   5640 		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
   5641 	t = ai.get_root_term (true);
   5642       else
   5643 	t = ai.unconverted_ref_origin ();
   5644       if (t == error_mark_node)
   5645 	return error_mark_node;
   5646       ret = t_refto;
   5647       if (TREE_CODE (t) == FIELD_DECL)
   5648 	ret = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
   5649       else if (!VAR_P (t)
   5650 	       && (openacc || !EXPR_P (t))
   5651 	       && TREE_CODE (t) != PARM_DECL)
   5652 	{
   5653 	  if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   5654 	    return NULL_TREE;
   5655 	  if (DECL_P (t))
   5656 	    error_at (OMP_CLAUSE_LOCATION (c),
   5657 		      "%qD is not a variable in %qs clause", t,
   5658 		      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5659 	  else
   5660 	    error_at (OMP_CLAUSE_LOCATION (c),
   5661 		      "%qE is not a variable in %qs clause", t,
   5662 		      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5663 	  return error_mark_node;
   5664 	}
   5665       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
   5666 	       && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
   5667 	       && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
   5668 	{
   5669 	  error_at (OMP_CLAUSE_LOCATION (c),
   5670 		    "%qD is threadprivate variable in %qs clause", t,
   5671 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5672 	  return error_mark_node;
   5673 	}
   5674       if (type_dependent_expression_p (ret))
   5675 	return NULL_TREE;
   5676       ret = convert_from_reference (ret);
   5677       return ret;
   5678     }
   5679 
   5680   if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
   5681       && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   5682 	  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   5683 	  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
   5684       && TREE_CODE (TREE_OPERAND (t, 0)) == FIELD_DECL)
   5685     TREE_OPERAND (t, 0) = omp_privatize_field (TREE_OPERAND (t, 0), false);
   5686   ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
   5687 				     maybe_zero_len, first_non_one, ort);
   5688   if (ret == error_mark_node || ret == NULL_TREE)
   5689     return ret;
   5690 
   5691   type = TREE_TYPE (ret);
   5692   low_bound = TREE_OPERAND (t, 1);
   5693   length = TREE_OPERAND (t, 2);
   5694   if ((low_bound && type_dependent_expression_p (low_bound))
   5695       || (length && type_dependent_expression_p (length)))
   5696     return NULL_TREE;
   5697 
   5698   if (low_bound == error_mark_node || length == error_mark_node)
   5699     return error_mark_node;
   5700 
   5701   if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
   5702     {
   5703       error_at (OMP_CLAUSE_LOCATION (c),
   5704 		"low bound %qE of array section does not have integral type",
   5705 		low_bound);
   5706       return error_mark_node;
   5707     }
   5708   if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
   5709     {
   5710       error_at (OMP_CLAUSE_LOCATION (c),
   5711 		"length %qE of array section does not have integral type",
   5712 		length);
   5713       return error_mark_node;
   5714     }
   5715   if (low_bound)
   5716     low_bound = mark_rvalue_use (low_bound);
   5717   if (length)
   5718     length = mark_rvalue_use (length);
   5719   /* We need to reduce to real constant-values for checks below.  */
   5720   if (length)
   5721     length = fold_simple (length);
   5722   if (low_bound)
   5723     low_bound = fold_simple (low_bound);
   5724   if (low_bound
   5725       && TREE_CODE (low_bound) == INTEGER_CST
   5726       && TYPE_PRECISION (TREE_TYPE (low_bound))
   5727 	 > TYPE_PRECISION (sizetype))
   5728     low_bound = fold_convert (sizetype, low_bound);
   5729   if (length
   5730       && TREE_CODE (length) == INTEGER_CST
   5731       && TYPE_PRECISION (TREE_TYPE (length))
   5732 	 > TYPE_PRECISION (sizetype))
   5733     length = fold_convert (sizetype, length);
   5734   if (low_bound == NULL_TREE)
   5735     low_bound = integer_zero_node;
   5736 
   5737   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   5738       && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   5739 	  || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
   5740     {
   5741       if (length != integer_one_node)
   5742 	{
   5743 	  error_at (OMP_CLAUSE_LOCATION (c),
   5744 		    "expected single pointer in %qs clause",
   5745 		    user_omp_clause_code_name (c, openacc));
   5746 	  return error_mark_node;
   5747 	}
   5748     }
   5749   if (length != NULL_TREE)
   5750     {
   5751       if (!integer_nonzerop (length))
   5752 	{
   5753 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
   5754 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   5755 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   5756 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   5757 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
   5758 	    {
   5759 	      if (integer_zerop (length))
   5760 		{
   5761 		  error_at (OMP_CLAUSE_LOCATION (c),
   5762 			    "zero length array section in %qs clause",
   5763 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5764 		  return error_mark_node;
   5765 		}
   5766 	    }
   5767 	  else
   5768 	    maybe_zero_len = true;
   5769 	}
   5770       if (first_non_one == types.length ()
   5771 	  && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
   5772 	first_non_one++;
   5773     }
   5774   if (TREE_CODE (type) == ARRAY_TYPE)
   5775     {
   5776       if (length == NULL_TREE
   5777 	  && (TYPE_DOMAIN (type) == NULL_TREE
   5778 	      || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
   5779 	{
   5780 	  error_at (OMP_CLAUSE_LOCATION (c),
   5781 		    "for unknown bound array type length expression must "
   5782 		    "be specified");
   5783 	  return error_mark_node;
   5784 	}
   5785       if (TREE_CODE (low_bound) == INTEGER_CST
   5786 	  && tree_int_cst_sgn (low_bound) == -1)
   5787 	{
   5788 	  error_at (OMP_CLAUSE_LOCATION (c),
   5789 		    "negative low bound in array section in %qs clause",
   5790 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5791 	  return error_mark_node;
   5792 	}
   5793       if (length != NULL_TREE
   5794 	  && TREE_CODE (length) == INTEGER_CST
   5795 	  && tree_int_cst_sgn (length) == -1)
   5796 	{
   5797 	  error_at (OMP_CLAUSE_LOCATION (c),
   5798 		    "negative length in array section in %qs clause",
   5799 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5800 	  return error_mark_node;
   5801 	}
   5802       if (TYPE_DOMAIN (type)
   5803 	  && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
   5804 	  && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
   5805 			== INTEGER_CST)
   5806 	{
   5807 	  tree size
   5808 	    = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
   5809 	  size = size_binop (PLUS_EXPR, size, size_one_node);
   5810 	  if (TREE_CODE (low_bound) == INTEGER_CST)
   5811 	    {
   5812 	      if (tree_int_cst_lt (size, low_bound))
   5813 		{
   5814 		  error_at (OMP_CLAUSE_LOCATION (c),
   5815 			    "low bound %qE above array section size "
   5816 			    "in %qs clause", low_bound,
   5817 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5818 		  return error_mark_node;
   5819 		}
   5820 	      if (tree_int_cst_equal (size, low_bound))
   5821 		{
   5822 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
   5823 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   5824 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   5825 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   5826 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
   5827 		    {
   5828 		      error_at (OMP_CLAUSE_LOCATION (c),
   5829 				"zero length array section in %qs clause",
   5830 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5831 		      return error_mark_node;
   5832 		    }
   5833 		  maybe_zero_len = true;
   5834 		}
   5835 	      else if (length == NULL_TREE
   5836 		       && first_non_one == types.length ()
   5837 		       && tree_int_cst_equal
   5838 			    (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
   5839 			     low_bound))
   5840 		first_non_one++;
   5841 	    }
   5842 	  else if (length == NULL_TREE)
   5843 	    {
   5844 	      if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
   5845 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
   5846 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
   5847 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
   5848 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
   5849 		maybe_zero_len = true;
   5850 	      if (first_non_one == types.length ())
   5851 		first_non_one++;
   5852 	    }
   5853 	  if (length && TREE_CODE (length) == INTEGER_CST)
   5854 	    {
   5855 	      if (tree_int_cst_lt (size, length))
   5856 		{
   5857 		  error_at (OMP_CLAUSE_LOCATION (c),
   5858 			    "length %qE above array section size "
   5859 			    "in %qs clause", length,
   5860 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5861 		  return error_mark_node;
   5862 		}
   5863 	      if (TREE_CODE (low_bound) == INTEGER_CST)
   5864 		{
   5865 		  tree lbpluslen
   5866 		    = size_binop (PLUS_EXPR,
   5867 				  fold_convert (sizetype, low_bound),
   5868 				  fold_convert (sizetype, length));
   5869 		  if (TREE_CODE (lbpluslen) == INTEGER_CST
   5870 		      && tree_int_cst_lt (size, lbpluslen))
   5871 		    {
   5872 		      error_at (OMP_CLAUSE_LOCATION (c),
   5873 				"high bound %qE above array section size "
   5874 				"in %qs clause", lbpluslen,
   5875 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5876 		      return error_mark_node;
   5877 		    }
   5878 		}
   5879 	    }
   5880 	}
   5881       else if (length == NULL_TREE)
   5882 	{
   5883 	  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
   5884 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
   5885 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
   5886 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
   5887 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
   5888 	    maybe_zero_len = true;
   5889 	  if (first_non_one == types.length ())
   5890 	    first_non_one++;
   5891 	}
   5892 
   5893       /* For [lb:] we will need to evaluate lb more than once.  */
   5894       if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
   5895 	{
   5896 	  tree lb = cp_save_expr (low_bound);
   5897 	  if (lb != low_bound)
   5898 	    {
   5899 	      TREE_OPERAND (t, 1) = lb;
   5900 	      low_bound = lb;
   5901 	    }
   5902 	}
   5903     }
   5904   else if (TYPE_PTR_P (type))
   5905     {
   5906       if (length == NULL_TREE)
   5907 	{
   5908 	  if (TREE_CODE (ret) == PARM_DECL && DECL_ARRAY_PARAMETER_P (ret))
   5909 	    error_at (OMP_CLAUSE_LOCATION (c),
   5910 		      "for array function parameter length expression "
   5911 		      "must be specified");
   5912 	  else
   5913 	    error_at (OMP_CLAUSE_LOCATION (c),
   5914 		      "for pointer type length expression must be specified");
   5915 	  return error_mark_node;
   5916 	}
   5917       if (length != NULL_TREE
   5918 	  && TREE_CODE (length) == INTEGER_CST
   5919 	  && tree_int_cst_sgn (length) == -1)
   5920 	{
   5921 	  error_at (OMP_CLAUSE_LOCATION (c),
   5922 		    "negative length in array section in %qs clause",
   5923 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5924 	  return error_mark_node;
   5925 	}
   5926       /* If there is a pointer type anywhere but in the very first
   5927 	 array-section-subscript, the array section could be non-contiguous.  */
   5928       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
   5929 	  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
   5930 	  && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
   5931 	{
   5932 	  /* If any prior dimension has a non-one length, then deem this
   5933 	     array section as non-contiguous.  */
   5934 	  for (tree d = TREE_OPERAND (t, 0); TREE_CODE (d) == OMP_ARRAY_SECTION;
   5935 	       d = TREE_OPERAND (d, 0))
   5936 	    {
   5937 	      tree d_length = TREE_OPERAND (d, 2);
   5938 	      if (d_length == NULL_TREE || !integer_onep (d_length))
   5939 		{
   5940 		  error_at (OMP_CLAUSE_LOCATION (c),
   5941 			    "array section is not contiguous in %qs clause",
   5942 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   5943 		  return error_mark_node;
   5944 		}
   5945 	    }
   5946 	}
   5947     }
   5948   else
   5949     {
   5950       error_at (OMP_CLAUSE_LOCATION (c),
   5951 		"%qE does not have pointer or array type", ret);
   5952       return error_mark_node;
   5953     }
   5954   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
   5955     types.safe_push (TREE_TYPE (ret));
   5956   /* We will need to evaluate lb more than once.  */
   5957   tree lb = cp_save_expr (low_bound);
   5958   if (lb != low_bound)
   5959     {
   5960       TREE_OPERAND (t, 1) = lb;
   5961       low_bound = lb;
   5962     }
   5963   /* Temporarily disable -fstrong-eval-order for array reductions.
   5964      The SAVE_EXPR and COMPOUND_EXPR added if low_bound has side-effects
   5965      is something the middle-end can't cope with and more importantly,
   5966      it needs to be the actual base variable that is privatized, not some
   5967      temporary assigned previous value of it.  That, together with OpenMP
   5968      saying how many times the side-effects are evaluated is unspecified,
   5969      makes int *a, *b; ... reduction(+:a[a = b, 3:10]) really unspecified.  */
   5970   warning_sentinel s (flag_strong_eval_order,
   5971 		      OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   5972 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   5973 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION);
   5974   ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, NULL,
   5975 			 tf_warning_or_error);
   5976   return ret;
   5977 }
   5978 
   5979 /* Handle array sections for clause C.  */
   5980 
   5981 static bool
   5982 handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
   5983 {
   5984   bool maybe_zero_len = false;
   5985   unsigned int first_non_one = 0;
   5986   auto_vec<tree, 10> types;
   5987   tree *tp = &OMP_CLAUSE_DECL (c);
   5988   if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   5989        || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
   5990       && TREE_CODE (*tp) == TREE_LIST
   5991       && TREE_PURPOSE (*tp)
   5992       && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
   5993     tp = &TREE_VALUE (*tp);
   5994   tree first = handle_omp_array_sections_1 (c, *tp, types,
   5995 					    maybe_zero_len, first_non_one,
   5996 					    ort);
   5997   if (first == error_mark_node)
   5998     return true;
   5999   if (first == NULL_TREE)
   6000     return false;
   6001   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   6002       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
   6003     {
   6004       tree t = *tp;
   6005       tree tem = NULL_TREE;
   6006       if (processing_template_decl)
   6007 	return false;
   6008       /* Need to evaluate side effects in the length expressions
   6009 	 if any.  */
   6010       while (TREE_CODE (t) == TREE_LIST)
   6011 	{
   6012 	  if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
   6013 	    {
   6014 	      if (tem == NULL_TREE)
   6015 		tem = TREE_VALUE (t);
   6016 	      else
   6017 		tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
   6018 			      TREE_VALUE (t), tem);
   6019 	    }
   6020 	  t = TREE_CHAIN (t);
   6021 	}
   6022       if (tem)
   6023 	first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
   6024       *tp = first;
   6025     }
   6026   else
   6027     {
   6028       unsigned int num = types.length (), i;
   6029       tree t, side_effects = NULL_TREE, size = NULL_TREE;
   6030       tree condition = NULL_TREE;
   6031 
   6032       if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
   6033 	maybe_zero_len = true;
   6034       if (processing_template_decl && maybe_zero_len)
   6035 	return false;
   6036 
   6037       for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
   6038 	   t = TREE_OPERAND (t, 0))
   6039 	{
   6040 	  gcc_assert (TREE_CODE (t) == OMP_ARRAY_SECTION);
   6041 
   6042 	  tree low_bound = TREE_OPERAND (t, 1);
   6043 	  tree length = TREE_OPERAND (t, 2);
   6044 
   6045 	  i--;
   6046 	  if (low_bound
   6047 	      && TREE_CODE (low_bound) == INTEGER_CST
   6048 	      && TYPE_PRECISION (TREE_TYPE (low_bound))
   6049 		 > TYPE_PRECISION (sizetype))
   6050 	    low_bound = fold_convert (sizetype, low_bound);
   6051 	  if (length
   6052 	      && TREE_CODE (length) == INTEGER_CST
   6053 	      && TYPE_PRECISION (TREE_TYPE (length))
   6054 		 > TYPE_PRECISION (sizetype))
   6055 	    length = fold_convert (sizetype, length);
   6056 	  if (low_bound == NULL_TREE)
   6057 	    low_bound = integer_zero_node;
   6058 	  if (!maybe_zero_len && i > first_non_one)
   6059 	    {
   6060 	      if (integer_nonzerop (low_bound))
   6061 		goto do_warn_noncontiguous;
   6062 	      if (length != NULL_TREE
   6063 		  && TREE_CODE (length) == INTEGER_CST
   6064 		  && TYPE_DOMAIN (types[i])
   6065 		  && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
   6066 		  && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
   6067 		     == INTEGER_CST)
   6068 		{
   6069 		  tree size;
   6070 		  size = size_binop (PLUS_EXPR,
   6071 				     TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
   6072 				     size_one_node);
   6073 		  if (!tree_int_cst_equal (length, size))
   6074 		    {
   6075 		     do_warn_noncontiguous:
   6076 		      error_at (OMP_CLAUSE_LOCATION (c),
   6077 				"array section is not contiguous in %qs "
   6078 				"clause",
   6079 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   6080 		      return true;
   6081 		    }
   6082 		}
   6083 	      if (!processing_template_decl
   6084 		  && length != NULL_TREE
   6085 		  && TREE_SIDE_EFFECTS (length))
   6086 		{
   6087 		  if (side_effects == NULL_TREE)
   6088 		    side_effects = length;
   6089 		  else
   6090 		    side_effects = build2 (COMPOUND_EXPR,
   6091 					   TREE_TYPE (side_effects),
   6092 					   length, side_effects);
   6093 		}
   6094 	    }
   6095 	  else if (processing_template_decl)
   6096 	    continue;
   6097 	  else
   6098 	    {
   6099 	      tree l;
   6100 
   6101 	      if (i > first_non_one
   6102 		  && ((length && integer_nonzerop (length))
   6103 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   6104 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   6105 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
   6106 		continue;
   6107 	      if (length)
   6108 		l = fold_convert (sizetype, length);
   6109 	      else
   6110 		{
   6111 		  l = size_binop (PLUS_EXPR,
   6112 				  TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
   6113 				  size_one_node);
   6114 		  l = size_binop (MINUS_EXPR, l,
   6115 				  fold_convert (sizetype, low_bound));
   6116 		}
   6117 	      if (i > first_non_one)
   6118 		{
   6119 		  l = fold_build2 (NE_EXPR, boolean_type_node, l,
   6120 				   size_zero_node);
   6121 		  if (condition == NULL_TREE)
   6122 		    condition = l;
   6123 		  else
   6124 		    condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
   6125 					     l, condition);
   6126 		}
   6127 	      else if (size == NULL_TREE)
   6128 		{
   6129 		  size = size_in_bytes (TREE_TYPE (types[i]));
   6130 		  tree eltype = TREE_TYPE (types[num - 1]);
   6131 		  while (TREE_CODE (eltype) == ARRAY_TYPE)
   6132 		    eltype = TREE_TYPE (eltype);
   6133 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   6134 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   6135 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
   6136 		    size = size_binop (EXACT_DIV_EXPR, size,
   6137 				       size_in_bytes (eltype));
   6138 		  size = size_binop (MULT_EXPR, size, l);
   6139 		  if (condition)
   6140 		    size = fold_build3 (COND_EXPR, sizetype, condition,
   6141 					size, size_zero_node);
   6142 		}
   6143 	      else
   6144 		size = size_binop (MULT_EXPR, size, l);
   6145 	    }
   6146 	}
   6147       if (!processing_template_decl)
   6148 	{
   6149 	  if (side_effects)
   6150 	    size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
   6151 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   6152 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   6153 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
   6154 	    {
   6155 	      size = size_binop (MINUS_EXPR, size, size_one_node);
   6156 	      size = save_expr (size);
   6157 	      tree index_type = build_index_type (size);
   6158 	      tree eltype = TREE_TYPE (first);
   6159 	      while (TREE_CODE (eltype) == ARRAY_TYPE)
   6160 		eltype = TREE_TYPE (eltype);
   6161 	      tree type = build_array_type (eltype, index_type);
   6162 	      tree ptype = build_pointer_type (eltype);
   6163 	      if (TYPE_REF_P (TREE_TYPE (t))
   6164 		  && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
   6165 		t = convert_from_reference (t);
   6166 	      else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
   6167 		t = build_fold_addr_expr (t);
   6168 	      tree t2 = build_fold_addr_expr (first);
   6169 	      t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
   6170 				     ptrdiff_type_node, t2);
   6171 	      t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
   6172 				    ptrdiff_type_node, t2,
   6173 				    fold_convert_loc (OMP_CLAUSE_LOCATION (c),
   6174 						      ptrdiff_type_node, t));
   6175 	      if (tree_fits_shwi_p (t2))
   6176 		t = build2 (MEM_REF, type, t,
   6177 			    build_int_cst (ptype, tree_to_shwi (t2)));
   6178 	      else
   6179 		{
   6180 		  t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
   6181 					 sizetype, t2);
   6182 		  t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
   6183 				  TREE_TYPE (t), t, t2);
   6184 		  t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
   6185 		}
   6186 	      OMP_CLAUSE_DECL (c) = t;
   6187 	      return false;
   6188 	    }
   6189 	  OMP_CLAUSE_DECL (c) = first;
   6190 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
   6191 	    return false;
   6192 	  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
   6193 	      || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
   6194 		  && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
   6195 		  && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
   6196 	    OMP_CLAUSE_SIZE (c) = size;
   6197 	  if (TREE_CODE (t) == FIELD_DECL)
   6198 	    t = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
   6199 
   6200 	  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
   6201 	    return false;
   6202 
   6203 	  if (TREE_CODE (first) == INDIRECT_REF)
   6204 	    {
   6205 	      /* Detect and skip adding extra nodes for pointer-to-member
   6206 		 mappings.  These are unsupported for now.  */
   6207 	      tree tmp = TREE_OPERAND (first, 0);
   6208 
   6209 	      if (TREE_CODE (tmp) == NON_LVALUE_EXPR)
   6210 		tmp = TREE_OPERAND (tmp, 0);
   6211 
   6212 	      if (TREE_CODE (tmp) == INDIRECT_REF)
   6213 		tmp = TREE_OPERAND (tmp, 0);
   6214 
   6215 	      if (TREE_CODE (tmp) == POINTER_PLUS_EXPR)
   6216 		{
   6217 		  tree offset = TREE_OPERAND (tmp, 1);
   6218 		  STRIP_NOPS (offset);
   6219 		  if (TYPE_PTRMEM_P (TREE_TYPE (offset)))
   6220 		    {
   6221 		      sorry_at (OMP_CLAUSE_LOCATION (c),
   6222 				"pointer-to-member mapping %qE not supported",
   6223 				OMP_CLAUSE_DECL (c));
   6224 		      return true;
   6225 		    }
   6226 		}
   6227 	    }
   6228 
   6229 	  /* FIRST represents the first item of data that we are mapping.
   6230 	     E.g. if we're mapping an array, FIRST might resemble
   6231 	     "foo.bar.myarray[0]".  */
   6232 
   6233 	  auto_vec<omp_addr_token *, 10> addr_tokens;
   6234 
   6235 	  if (!omp_parse_expr (addr_tokens, first))
   6236 	    return true;
   6237 
   6238 	  cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
   6239 
   6240 	  tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
   6241 	  if (nc != error_mark_node)
   6242 	    {
   6243 	      using namespace omp_addr_tokenizer;
   6244 
   6245 	      if (ai.maybe_zero_length_array_section (c))
   6246 		OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
   6247 
   6248 	      /* !!! If we're accessing a base decl via chained access
   6249 		 methods (e.g. multiple indirections), duplicate clause
   6250 		 detection won't work properly.  Skip it in that case.  */
   6251 	      if ((addr_tokens[0]->type == STRUCTURE_BASE
   6252 		   || addr_tokens[0]->type == ARRAY_BASE)
   6253 		  && addr_tokens[0]->u.structure_base_kind == BASE_DECL
   6254 		  && addr_tokens[1]->type == ACCESS_METHOD
   6255 		  && omp_access_chain_p (addr_tokens, 1))
   6256 		c = nc;
   6257 
   6258 	      return false;
   6259 	    }
   6260 	}
   6261     }
   6262   return false;
   6263 }
   6264 
   6265 /* Return identifier to look up for omp declare reduction.  */
   6266 
   6267 tree
   6268 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
   6269 {
   6270   const char *p = NULL;
   6271   const char *m = NULL;
   6272   switch (reduction_code)
   6273     {
   6274     case PLUS_EXPR:
   6275     case MULT_EXPR:
   6276     case MINUS_EXPR:
   6277     case BIT_AND_EXPR:
   6278     case BIT_XOR_EXPR:
   6279     case BIT_IOR_EXPR:
   6280     case TRUTH_ANDIF_EXPR:
   6281     case TRUTH_ORIF_EXPR:
   6282       reduction_id = ovl_op_identifier (false, reduction_code);
   6283       break;
   6284     case MIN_EXPR:
   6285       p = "min";
   6286       break;
   6287     case MAX_EXPR:
   6288       p = "max";
   6289       break;
   6290     default:
   6291       break;
   6292     }
   6293 
   6294   if (p == NULL)
   6295     {
   6296       if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
   6297 	return error_mark_node;
   6298       p = IDENTIFIER_POINTER (reduction_id);
   6299     }
   6300 
   6301   if (type != NULL_TREE)
   6302     m = mangle_type_string (TYPE_MAIN_VARIANT (type));
   6303 
   6304   const char prefix[] = "omp declare reduction ";
   6305   size_t lenp = sizeof (prefix);
   6306   if (strncmp (p, prefix, lenp - 1) == 0)
   6307     lenp = 1;
   6308   size_t len = strlen (p);
   6309   size_t lenm = m ? strlen (m) + 1 : 0;
   6310   char *name = XALLOCAVEC (char, lenp + len + lenm);
   6311   if (lenp > 1)
   6312     memcpy (name, prefix, lenp - 1);
   6313   memcpy (name + lenp - 1, p, len + 1);
   6314   if (m)
   6315     {
   6316       name[lenp + len - 1] = '~';
   6317       memcpy (name + lenp + len, m, lenm);
   6318     }
   6319   return get_identifier (name);
   6320 }
   6321 
   6322 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
   6323    FUNCTION_DECL or NULL_TREE if not found.  */
   6324 
   6325 static tree
   6326 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
   6327 		      vec<tree> *ambiguousp)
   6328 {
   6329   tree orig_id = id;
   6330   tree baselink = NULL_TREE;
   6331   if (identifier_p (id))
   6332     {
   6333       cp_id_kind idk;
   6334       bool nonint_cst_expression_p;
   6335       const char *error_msg;
   6336       id = omp_reduction_id (ERROR_MARK, id, type);
   6337       tree decl = lookup_name (id);
   6338       if (decl == NULL_TREE)
   6339 	decl = error_mark_node;
   6340       id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
   6341 				 &nonint_cst_expression_p, false, true, false,
   6342 				 false, &error_msg, loc);
   6343       if (idk == CP_ID_KIND_UNQUALIFIED
   6344 	  && identifier_p (id))
   6345 	{
   6346 	  vec<tree, va_gc> *args = NULL;
   6347 	  vec_safe_push (args, build_reference_type (type));
   6348 	  id = perform_koenig_lookup (id, args, tf_none);
   6349 	}
   6350     }
   6351   else if (TREE_CODE (id) == SCOPE_REF)
   6352     id = lookup_qualified_name (TREE_OPERAND (id, 0),
   6353 				omp_reduction_id (ERROR_MARK,
   6354 						  TREE_OPERAND (id, 1),
   6355 						  type),
   6356 				LOOK_want::NORMAL, false);
   6357   tree fns = id;
   6358   id = NULL_TREE;
   6359   if (fns && is_overloaded_fn (fns))
   6360     {
   6361       for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
   6362 	{
   6363 	  tree fndecl = *iter;
   6364 	  if (TREE_CODE (fndecl) == FUNCTION_DECL)
   6365 	    {
   6366 	      tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
   6367 	      if (same_type_p (TREE_TYPE (argtype), type))
   6368 		{
   6369 		  id = fndecl;
   6370 		  break;
   6371 		}
   6372 	    }
   6373 	}
   6374 
   6375       if (id && BASELINK_P (fns))
   6376 	{
   6377 	  if (baselinkp)
   6378 	    *baselinkp = fns;
   6379 	  else
   6380 	    baselink = fns;
   6381 	}
   6382     }
   6383 
   6384   if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
   6385     {
   6386       auto_vec<tree> ambiguous;
   6387       tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
   6388       unsigned int ix;
   6389       if (ambiguousp == NULL)
   6390 	ambiguousp = &ambiguous;
   6391       for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
   6392 	{
   6393 	  id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
   6394 				     baselinkp ? baselinkp : &baselink,
   6395 				     ambiguousp);
   6396 	  if (id == NULL_TREE)
   6397 	    continue;
   6398 	  if (!ambiguousp->is_empty ())
   6399 	    ambiguousp->safe_push (id);
   6400 	  else if (ret != NULL_TREE)
   6401 	    {
   6402 	      ambiguousp->safe_push (ret);
   6403 	      ambiguousp->safe_push (id);
   6404 	      ret = NULL_TREE;
   6405 	    }
   6406 	  else
   6407 	    ret = id;
   6408 	}
   6409       if (ambiguousp != &ambiguous)
   6410 	return ret;
   6411       if (!ambiguous.is_empty ())
   6412 	{
   6413 	  const char *str = _("candidates are:");
   6414 	  unsigned int idx;
   6415 	  tree udr;
   6416 	  error_at (loc, "user defined reduction lookup is ambiguous");
   6417 	  FOR_EACH_VEC_ELT (ambiguous, idx, udr)
   6418 	    {
   6419 	      inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
   6420 	      if (idx == 0)
   6421 		str = get_spaces (str);
   6422 	    }
   6423 	  ret = error_mark_node;
   6424 	  baselink = NULL_TREE;
   6425 	}
   6426       id = ret;
   6427     }
   6428   if (id && baselink)
   6429     perform_or_defer_access_check (BASELINK_BINFO (baselink),
   6430 				   id, id, tf_warning_or_error);
   6431   return id;
   6432 }
   6433 
   6434 /* Helper function for cp_parser_omp_declare_reduction_exprs
   6435    and tsubst_omp_udr.
   6436    Remove CLEANUP_STMT for data (omp_priv variable).
   6437    Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
   6438    DECL_EXPR.  */
   6439 
   6440 tree
   6441 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
   6442 {
   6443   if (TYPE_P (*tp))
   6444     *walk_subtrees = 0;
   6445   else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
   6446     *tp = CLEANUP_BODY (*tp);
   6447   else if (TREE_CODE (*tp) == DECL_EXPR)
   6448     {
   6449       tree decl = DECL_EXPR_DECL (*tp);
   6450       if (!processing_template_decl
   6451 	  && decl == (tree) data
   6452 	  && DECL_INITIAL (decl)
   6453 	  && DECL_INITIAL (decl) != error_mark_node)
   6454 	{
   6455 	  tree list = NULL_TREE;
   6456 	  append_to_statement_list_force (*tp, &list);
   6457 	  tree init_expr = build2 (INIT_EXPR, void_type_node,
   6458 				   decl, DECL_INITIAL (decl));
   6459 	  DECL_INITIAL (decl) = NULL_TREE;
   6460 	  append_to_statement_list_force (init_expr, &list);
   6461 	  *tp = list;
   6462 	}
   6463     }
   6464   return NULL_TREE;
   6465 }
   6466 
   6467 /* Data passed from cp_check_omp_declare_reduction to
   6468    cp_check_omp_declare_reduction_r.  */
   6469 
   6470 struct cp_check_omp_declare_reduction_data
   6471 {
   6472   location_t loc;
   6473   tree stmts[7];
   6474   bool combiner_p;
   6475 };
   6476 
   6477 /* Helper function for cp_check_omp_declare_reduction, called via
   6478    cp_walk_tree.  */
   6479 
   6480 static tree
   6481 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
   6482 {
   6483   struct cp_check_omp_declare_reduction_data *udr_data
   6484     = (struct cp_check_omp_declare_reduction_data *) data;
   6485   if (SSA_VAR_P (*tp)
   6486       && !DECL_ARTIFICIAL (*tp)
   6487       && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
   6488       && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
   6489     {
   6490       location_t loc = udr_data->loc;
   6491       if (udr_data->combiner_p)
   6492 	error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
   6493 		       "variable %qD which is not %<omp_out%> nor %<omp_in%>",
   6494 		  *tp);
   6495       else
   6496 	error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
   6497 		       "to variable %qD which is not %<omp_priv%> nor "
   6498 		       "%<omp_orig%>",
   6499 		  *tp);
   6500       return *tp;
   6501     }
   6502   return NULL_TREE;
   6503 }
   6504 
   6505 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions.  */
   6506 
   6507 bool
   6508 cp_check_omp_declare_reduction (tree udr)
   6509 {
   6510   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
   6511   gcc_assert (TYPE_REF_P (type));
   6512   type = TREE_TYPE (type);
   6513   int i;
   6514   location_t loc = DECL_SOURCE_LOCATION (udr);
   6515 
   6516   if (type == error_mark_node)
   6517     return false;
   6518   if (ARITHMETIC_TYPE_P (type))
   6519     {
   6520       static enum tree_code predef_codes[]
   6521 	= { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
   6522 	    BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
   6523       for (i = 0; i < 8; i++)
   6524 	{
   6525 	  tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
   6526 	  const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
   6527 	  const char *n2 = IDENTIFIER_POINTER (id);
   6528 	  if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
   6529 	      && (n1[IDENTIFIER_LENGTH (id)] == '~'
   6530 		  || n1[IDENTIFIER_LENGTH (id)] == '\0'))
   6531 	    break;
   6532 	}
   6533 
   6534       if (i == 8
   6535 	  && TREE_CODE (type) != COMPLEX_EXPR)
   6536 	{
   6537 	  const char prefix_minmax[] = "omp declare reduction m";
   6538 	  size_t prefix_size = sizeof (prefix_minmax) - 1;
   6539 	  const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
   6540 	  if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
   6541 		       prefix_minmax, prefix_size) == 0
   6542 	      && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
   6543 		  || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
   6544 	      && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
   6545 	    i = 0;
   6546 	}
   6547       if (i < 8)
   6548 	{
   6549 	  error_at (loc, "predeclared arithmetic type %qT in "
   6550 			 "%<#pragma omp declare reduction%>", type);
   6551 	  return false;
   6552 	}
   6553     }
   6554   else if (FUNC_OR_METHOD_TYPE_P (type)
   6555 	   || TREE_CODE (type) == ARRAY_TYPE)
   6556     {
   6557       error_at (loc, "function or array type %qT in "
   6558 		     "%<#pragma omp declare reduction%>", type);
   6559       return false;
   6560     }
   6561   else if (TYPE_REF_P (type))
   6562     {
   6563       error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
   6564 		type);
   6565       return false;
   6566     }
   6567   else if (TYPE_QUALS_NO_ADDR_SPACE (type))
   6568     {
   6569       error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
   6570 		"type %qT in %<#pragma omp declare reduction%>", type);
   6571       return false;
   6572     }
   6573 
   6574   tree body = DECL_SAVED_TREE (udr);
   6575   if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
   6576     return true;
   6577 
   6578   tree_stmt_iterator tsi;
   6579   struct cp_check_omp_declare_reduction_data data;
   6580   memset (data.stmts, 0, sizeof data.stmts);
   6581   for (i = 0, tsi = tsi_start (body);
   6582        i < 7 && !tsi_end_p (tsi);
   6583        i++, tsi_next (&tsi))
   6584     data.stmts[i] = tsi_stmt (tsi);
   6585   data.loc = loc;
   6586   gcc_assert (tsi_end_p (tsi));
   6587   if (i >= 3)
   6588     {
   6589       gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
   6590 		  && TREE_CODE (data.stmts[1]) == DECL_EXPR);
   6591       if (warning_suppressed_p (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */))
   6592 	return true;
   6593       data.combiner_p = true;
   6594       if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
   6595 			&data, NULL))
   6596 	suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */);
   6597     }
   6598   if (i >= 6)
   6599     {
   6600       gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
   6601 		  && TREE_CODE (data.stmts[4]) == DECL_EXPR);
   6602       data.combiner_p = false;
   6603       if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
   6604 			&data, NULL)
   6605 	  || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
   6606 			   cp_check_omp_declare_reduction_r, &data, NULL))
   6607 	suppress_warning (DECL_EXPR_DECL (data.stmts[0])  /* Wat warning? */);
   6608       if (i == 7)
   6609 	gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
   6610     }
   6611   return true;
   6612 }
   6613 
   6614 /* Helper function of finish_omp_clauses.  Clone STMT as if we were making
   6615    an inline call.  But, remap
   6616    the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
   6617    and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL.  */
   6618 
   6619 static tree
   6620 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
   6621 	       tree decl, tree placeholder)
   6622 {
   6623   copy_body_data id;
   6624   hash_map<tree, tree> decl_map;
   6625 
   6626   decl_map.put (omp_decl1, placeholder);
   6627   decl_map.put (omp_decl2, decl);
   6628   memset (&id, 0, sizeof (id));
   6629   id.src_fn = DECL_CONTEXT (omp_decl1);
   6630   id.dst_fn = current_function_decl;
   6631   id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
   6632   id.decl_map = &decl_map;
   6633 
   6634   id.copy_decl = copy_decl_no_change;
   6635   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
   6636   id.transform_new_cfg = true;
   6637   id.transform_return_to_modify = false;
   6638   id.eh_lp_nr = 0;
   6639   walk_tree (&stmt, copy_tree_body_r, &id, NULL);
   6640   return stmt;
   6641 }
   6642 
   6643 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
   6644    Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP.  */
   6645 
   6646 static tree
   6647 find_omp_placeholder_r (tree *tp, int *, void *data)
   6648 {
   6649   if (*tp == (tree) data)
   6650     return *tp;
   6651   return NULL_TREE;
   6652 }
   6653 
   6654 /* Helper function of finish_omp_clauses.  Handle OMP_CLAUSE_REDUCTION C.
   6655    Return true if there is some error and the clause should be removed.  */
   6656 
   6657 static bool
   6658 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
   6659 {
   6660   tree t = OMP_CLAUSE_DECL (c);
   6661   bool predefined = false;
   6662   if (TREE_CODE (t) == TREE_LIST)
   6663     {
   6664       gcc_assert (processing_template_decl);
   6665       return false;
   6666     }
   6667   tree type = TREE_TYPE (t);
   6668   if (TREE_CODE (t) == MEM_REF)
   6669     type = TREE_TYPE (type);
   6670   if (TYPE_REF_P (type))
   6671     type = TREE_TYPE (type);
   6672   if (TREE_CODE (type) == ARRAY_TYPE)
   6673     {
   6674       tree oatype = type;
   6675       gcc_assert (TREE_CODE (t) != MEM_REF);
   6676       while (TREE_CODE (type) == ARRAY_TYPE)
   6677 	type = TREE_TYPE (type);
   6678       if (!processing_template_decl)
   6679 	{
   6680 	  t = require_complete_type (t);
   6681 	  if (t == error_mark_node
   6682 	      || !complete_type_or_else (oatype, NULL_TREE))
   6683 	    return true;
   6684 	  tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
   6685 				  TYPE_SIZE_UNIT (type));
   6686 	  if (integer_zerop (size))
   6687 	    {
   6688 	      error_at (OMP_CLAUSE_LOCATION (c),
   6689 			"%qE in %<reduction%> clause is a zero size array",
   6690 			omp_clause_printable_decl (t));
   6691 	      return true;
   6692 	    }
   6693 	  size = size_binop (MINUS_EXPR, size, size_one_node);
   6694 	  size = save_expr (size);
   6695 	  tree index_type = build_index_type (size);
   6696 	  tree atype = build_array_type (type, index_type);
   6697 	  tree ptype = build_pointer_type (type);
   6698 	  if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
   6699 	    t = build_fold_addr_expr (t);
   6700 	  t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
   6701 	  OMP_CLAUSE_DECL (c) = t;
   6702 	}
   6703     }
   6704   if (type == error_mark_node)
   6705     return true;
   6706   else if (ARITHMETIC_TYPE_P (type))
   6707     switch (OMP_CLAUSE_REDUCTION_CODE (c))
   6708       {
   6709       case PLUS_EXPR:
   6710       case MULT_EXPR:
   6711       case MINUS_EXPR:
   6712       case TRUTH_ANDIF_EXPR:
   6713       case TRUTH_ORIF_EXPR:
   6714 	predefined = true;
   6715 	break;
   6716       case MIN_EXPR:
   6717       case MAX_EXPR:
   6718 	if (TREE_CODE (type) == COMPLEX_TYPE)
   6719 	  break;
   6720 	predefined = true;
   6721 	break;
   6722       case BIT_AND_EXPR:
   6723       case BIT_IOR_EXPR:
   6724       case BIT_XOR_EXPR:
   6725 	if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
   6726 	  break;
   6727 	predefined = true;
   6728 	break;
   6729       default:
   6730 	break;
   6731       }
   6732   else if (TYPE_READONLY (type))
   6733     {
   6734       error_at (OMP_CLAUSE_LOCATION (c),
   6735 		"%qE has const type for %<reduction%>",
   6736 		omp_clause_printable_decl (t));
   6737       return true;
   6738     }
   6739   else if (!processing_template_decl)
   6740     {
   6741       t = require_complete_type (t);
   6742       if (t == error_mark_node)
   6743 	return true;
   6744       OMP_CLAUSE_DECL (c) = t;
   6745     }
   6746 
   6747   if (predefined)
   6748     {
   6749       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
   6750       return false;
   6751     }
   6752   else if (processing_template_decl)
   6753     {
   6754       if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
   6755 	return true;
   6756       return false;
   6757     }
   6758 
   6759   tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
   6760 
   6761   type = TYPE_MAIN_VARIANT (type);
   6762   OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
   6763   if (id == NULL_TREE)
   6764     id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
   6765 			   NULL_TREE, NULL_TREE);
   6766   id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
   6767   if (id)
   6768     {
   6769       if (id == error_mark_node)
   6770 	return true;
   6771       mark_used (id);
   6772       tree body = DECL_SAVED_TREE (id);
   6773       if (!body)
   6774 	return true;
   6775       if (TREE_CODE (body) == STATEMENT_LIST)
   6776 	{
   6777 	  tree_stmt_iterator tsi;
   6778 	  tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
   6779 	  int i;
   6780 	  tree stmts[7];
   6781 	  tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
   6782 	  atype = TREE_TYPE (atype);
   6783 	  bool need_static_cast = !same_type_p (type, atype);
   6784 	  memset (stmts, 0, sizeof stmts);
   6785 	  for (i = 0, tsi = tsi_start (body);
   6786 	       i < 7 && !tsi_end_p (tsi);
   6787 	       i++, tsi_next (&tsi))
   6788 	    stmts[i] = tsi_stmt (tsi);
   6789 	  gcc_assert (tsi_end_p (tsi));
   6790 
   6791 	  if (i >= 3)
   6792 	    {
   6793 	      gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
   6794 			  && TREE_CODE (stmts[1]) == DECL_EXPR);
   6795 	      placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
   6796 	      DECL_ARTIFICIAL (placeholder) = 1;
   6797 	      DECL_IGNORED_P (placeholder) = 1;
   6798 	      OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
   6799 	      if (TREE_CODE (t) == MEM_REF)
   6800 		{
   6801 		  decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
   6802 						      type);
   6803 		  DECL_ARTIFICIAL (decl_placeholder) = 1;
   6804 		  DECL_IGNORED_P (decl_placeholder) = 1;
   6805 		  OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
   6806 		}
   6807 	      if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
   6808 		cxx_mark_addressable (placeholder);
   6809 	      if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
   6810 		  && (decl_placeholder
   6811 		      || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
   6812 		cxx_mark_addressable (decl_placeholder ? decl_placeholder
   6813 				      : OMP_CLAUSE_DECL (c));
   6814 	      tree omp_out = placeholder;
   6815 	      tree omp_in = decl_placeholder ? decl_placeholder
   6816 			    : convert_from_reference (OMP_CLAUSE_DECL (c));
   6817 	      if (need_static_cast)
   6818 		{
   6819 		  tree rtype = build_reference_type (atype);
   6820 		  omp_out = build_static_cast (input_location,
   6821 					       rtype, omp_out,
   6822 					       tf_warning_or_error);
   6823 		  omp_in = build_static_cast (input_location,
   6824 					      rtype, omp_in,
   6825 					      tf_warning_or_error);
   6826 		  if (omp_out == error_mark_node || omp_in == error_mark_node)
   6827 		    return true;
   6828 		  omp_out = convert_from_reference (omp_out);
   6829 		  omp_in = convert_from_reference (omp_in);
   6830 		}
   6831 	      OMP_CLAUSE_REDUCTION_MERGE (c)
   6832 		= clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
   6833 				 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
   6834 	    }
   6835 	  if (i >= 6)
   6836 	    {
   6837 	      gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
   6838 			  && TREE_CODE (stmts[4]) == DECL_EXPR);
   6839 	      if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
   6840 		  && (decl_placeholder
   6841 		      || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
   6842 		cxx_mark_addressable (decl_placeholder ? decl_placeholder
   6843 				      : OMP_CLAUSE_DECL (c));
   6844 	      if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
   6845 		cxx_mark_addressable (placeholder);
   6846 	      tree omp_priv = decl_placeholder ? decl_placeholder
   6847 			      : convert_from_reference (OMP_CLAUSE_DECL (c));
   6848 	      tree omp_orig = placeholder;
   6849 	      if (need_static_cast)
   6850 		{
   6851 		  if (i == 7)
   6852 		    {
   6853 		      error_at (OMP_CLAUSE_LOCATION (c),
   6854 				"user defined reduction with constructor "
   6855 				"initializer for base class %qT", atype);
   6856 		      return true;
   6857 		    }
   6858 		  tree rtype = build_reference_type (atype);
   6859 		  omp_priv = build_static_cast (input_location,
   6860 						rtype, omp_priv,
   6861 						tf_warning_or_error);
   6862 		  omp_orig = build_static_cast (input_location,
   6863 						rtype, omp_orig,
   6864 						tf_warning_or_error);
   6865 		  if (omp_priv == error_mark_node
   6866 		      || omp_orig == error_mark_node)
   6867 		    return true;
   6868 		  omp_priv = convert_from_reference (omp_priv);
   6869 		  omp_orig = convert_from_reference (omp_orig);
   6870 		}
   6871 	      if (i == 6)
   6872 		*need_default_ctor = true;
   6873 	      OMP_CLAUSE_REDUCTION_INIT (c)
   6874 		= clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
   6875 				 DECL_EXPR_DECL (stmts[3]),
   6876 				 omp_priv, omp_orig);
   6877 	      if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
   6878 				find_omp_placeholder_r, placeholder, NULL))
   6879 		OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
   6880 	    }
   6881 	  else if (i >= 3)
   6882 	    {
   6883 	      if (CLASS_TYPE_P (type) && !pod_type_p (type))
   6884 		*need_default_ctor = true;
   6885 	      else
   6886 		{
   6887 		  tree init;
   6888 		  tree v = decl_placeholder ? decl_placeholder
   6889 			   : convert_from_reference (t);
   6890 		  if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
   6891 		    init = build_constructor (TREE_TYPE (v), NULL);
   6892 		  else
   6893 		    init = fold_convert (TREE_TYPE (v), integer_zero_node);
   6894 		  OMP_CLAUSE_REDUCTION_INIT (c)
   6895 		    = cp_build_init_expr (v, init);
   6896 		}
   6897 	    }
   6898 	}
   6899     }
   6900   if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
   6901     *need_dtor = true;
   6902   else
   6903     {
   6904       error_at (OMP_CLAUSE_LOCATION (c),
   6905 		"user defined reduction not found for %qE",
   6906 		omp_clause_printable_decl (t));
   6907       return true;
   6908     }
   6909   if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
   6910     gcc_assert (TYPE_SIZE_UNIT (type)
   6911 		&& TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
   6912   return false;
   6913 }
   6914 
   6915 /* Called from finish_struct_1.  linear(this) or linear(this:step)
   6916    clauses might not be finalized yet because the class has been incomplete
   6917    when parsing #pragma omp declare simd methods.  Fix those up now.  */
   6918 
   6919 void
   6920 finish_omp_declare_simd_methods (tree t)
   6921 {
   6922   if (processing_template_decl)
   6923     return;
   6924 
   6925   for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
   6926     {
   6927       if (TREE_CODE (x) == USING_DECL
   6928 	  || !DECL_IOBJ_MEMBER_FUNCTION_P (x))
   6929 	continue;
   6930       tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
   6931       if (!ods || !TREE_VALUE (ods))
   6932 	continue;
   6933       for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
   6934 	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
   6935 	    && integer_zerop (OMP_CLAUSE_DECL (c))
   6936 	    && OMP_CLAUSE_LINEAR_STEP (c)
   6937 	    && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
   6938 	  {
   6939 	    tree s = OMP_CLAUSE_LINEAR_STEP (c);
   6940 	    s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
   6941 	    s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
   6942 				 sizetype, s, TYPE_SIZE_UNIT (t));
   6943 	    OMP_CLAUSE_LINEAR_STEP (c) = s;
   6944 	  }
   6945     }
   6946 }
   6947 
   6948 /* Adjust sink depend/doacross clause to take into account pointer offsets.
   6949 
   6950    Return TRUE if there was a problem processing the offset, and the
   6951    whole clause should be removed.  */
   6952 
   6953 static bool
   6954 cp_finish_omp_clause_doacross_sink (tree sink_clause)
   6955 {
   6956   tree t = OMP_CLAUSE_DECL (sink_clause);
   6957   gcc_assert (TREE_CODE (t) == TREE_LIST);
   6958 
   6959   /* Make sure we don't adjust things twice for templates.  */
   6960   if (processing_template_decl)
   6961     return false;
   6962 
   6963   for (; t; t = TREE_CHAIN (t))
   6964     {
   6965       tree decl = TREE_VALUE (t);
   6966       if (TYPE_PTR_P (TREE_TYPE (decl)))
   6967 	{
   6968 	  tree offset = TREE_PURPOSE (t);
   6969 	  bool neg = wi::neg_p (wi::to_wide (offset));
   6970 	  offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
   6971 	  decl = mark_rvalue_use (decl);
   6972 	  decl = convert_from_reference (decl);
   6973 	  tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
   6974 				     neg ? MINUS_EXPR : PLUS_EXPR,
   6975 				     decl, offset);
   6976 	  t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
   6977 				MINUS_EXPR, sizetype,
   6978 				fold_convert (sizetype, t2),
   6979 				fold_convert (sizetype, decl));
   6980 	  if (t2 == error_mark_node)
   6981 	    return true;
   6982 	  TREE_PURPOSE (t) = t2;
   6983 	}
   6984     }
   6985   return false;
   6986 }
   6987 
   6988 /* Finish OpenMP iterators ITER.  Return true if they are errorneous
   6989    and clauses containing them should be removed.  */
   6990 
   6991 static bool
   6992 cp_omp_finish_iterators (tree iter)
   6993 {
   6994   bool ret = false;
   6995   for (tree it = iter; it; it = TREE_CHAIN (it))
   6996     {
   6997       tree var = TREE_VEC_ELT (it, 0);
   6998       tree begin = TREE_VEC_ELT (it, 1);
   6999       tree end = TREE_VEC_ELT (it, 2);
   7000       tree step = TREE_VEC_ELT (it, 3);
   7001       tree orig_step;
   7002       tree type = TREE_TYPE (var);
   7003       location_t loc = DECL_SOURCE_LOCATION (var);
   7004       if (type == error_mark_node)
   7005 	{
   7006 	  ret = true;
   7007 	  continue;
   7008 	}
   7009       if (type_dependent_expression_p (var))
   7010 	continue;
   7011       if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
   7012 	{
   7013 	  error_at (loc, "iterator %qD has neither integral nor pointer type",
   7014 		    var);
   7015 	  ret = true;
   7016 	  continue;
   7017 	}
   7018       else if (TYPE_READONLY (type))
   7019 	{
   7020 	  error_at (loc, "iterator %qD has const qualified type", var);
   7021 	  ret = true;
   7022 	  continue;
   7023 	}
   7024       if (type_dependent_expression_p (begin)
   7025 	  || type_dependent_expression_p (end)
   7026 	  || type_dependent_expression_p (step))
   7027 	continue;
   7028       else if (error_operand_p (step))
   7029 	{
   7030 	  ret = true;
   7031 	  continue;
   7032 	}
   7033       else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
   7034 	{
   7035 	  error_at (EXPR_LOC_OR_LOC (step, loc),
   7036 		    "iterator step with non-integral type");
   7037 	  ret = true;
   7038 	  continue;
   7039 	}
   7040 
   7041       begin = mark_rvalue_use (begin);
   7042       end = mark_rvalue_use (end);
   7043       step = mark_rvalue_use (step);
   7044       begin = cp_build_c_cast (input_location, type, begin,
   7045 			       tf_warning_or_error);
   7046       end = cp_build_c_cast (input_location, type, end,
   7047 			     tf_warning_or_error);
   7048       orig_step = step;
   7049       if (!processing_template_decl)
   7050 	step = orig_step = save_expr (step);
   7051       tree stype = POINTER_TYPE_P (type) ? sizetype : type;
   7052       step = cp_build_c_cast (input_location, stype, step,
   7053 			      tf_warning_or_error);
   7054       if (POINTER_TYPE_P (type) && !processing_template_decl)
   7055 	{
   7056 	  begin = save_expr (begin);
   7057 	  step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
   7058 	  step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
   7059 				  fold_convert (sizetype, step),
   7060 				  fold_convert (sizetype, begin));
   7061 	  step = fold_convert (ssizetype, step);
   7062 	}
   7063       if (!processing_template_decl)
   7064 	{
   7065 	  begin = maybe_constant_value (begin);
   7066 	  end = maybe_constant_value (end);
   7067 	  step = maybe_constant_value (step);
   7068 	  orig_step = maybe_constant_value (orig_step);
   7069 	}
   7070       if (integer_zerop (step))
   7071 	{
   7072 	  error_at (loc, "iterator %qD has zero step", var);
   7073 	  ret = true;
   7074 	  continue;
   7075 	}
   7076 
   7077       if (begin == error_mark_node
   7078 	  || end == error_mark_node
   7079 	  || step == error_mark_node
   7080 	  || orig_step == error_mark_node)
   7081 	{
   7082 	  ret = true;
   7083 	  continue;
   7084 	}
   7085 
   7086       if (!processing_template_decl)
   7087 	{
   7088 	  begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
   7089 	  end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
   7090 	  step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
   7091 	  orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
   7092 						     orig_step);
   7093 	}
   7094       hash_set<tree> pset;
   7095       tree it2;
   7096       for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
   7097 	{
   7098 	  tree var2 = TREE_VEC_ELT (it2, 0);
   7099 	  tree begin2 = TREE_VEC_ELT (it2, 1);
   7100 	  tree end2 = TREE_VEC_ELT (it2, 2);
   7101 	  tree step2 = TREE_VEC_ELT (it2, 3);
   7102 	  location_t loc2 = DECL_SOURCE_LOCATION (var2);
   7103 	  if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
   7104 	    {
   7105 	      error_at (EXPR_LOC_OR_LOC (begin2, loc2),
   7106 			"begin expression refers to outer iterator %qD", var);
   7107 	      break;
   7108 	    }
   7109 	  else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
   7110 	    {
   7111 	      error_at (EXPR_LOC_OR_LOC (end2, loc2),
   7112 			"end expression refers to outer iterator %qD", var);
   7113 	      break;
   7114 	    }
   7115 	  else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
   7116 	    {
   7117 	      error_at (EXPR_LOC_OR_LOC (step2, loc2),
   7118 			"step expression refers to outer iterator %qD", var);
   7119 	      break;
   7120 	    }
   7121 	}
   7122       if (it2)
   7123 	{
   7124 	  ret = true;
   7125 	  continue;
   7126 	}
   7127       TREE_VEC_ELT (it, 1) = begin;
   7128       TREE_VEC_ELT (it, 2) = end;
   7129       if (processing_template_decl)
   7130 	TREE_VEC_ELT (it, 3) = orig_step;
   7131       else
   7132 	{
   7133 	  TREE_VEC_ELT (it, 3) = step;
   7134 	  TREE_VEC_ELT (it, 4) = orig_step;
   7135 	}
   7136     }
   7137   return ret;
   7138 }
   7139 
   7140 /* Ensure that pointers are used in OpenACC attach and detach clauses.
   7141    Return true if an error has been detected.  */
   7142 
   7143 static bool
   7144 cp_oacc_check_attachments (tree c)
   7145 {
   7146   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
   7147     return false;
   7148 
   7149   /* OpenACC attach / detach clauses must be pointers.  */
   7150   if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   7151       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
   7152     {
   7153       tree t = OMP_CLAUSE_DECL (c);
   7154       tree type;
   7155 
   7156       while (TREE_CODE (t) == OMP_ARRAY_SECTION)
   7157 	t = TREE_OPERAND (t, 0);
   7158 
   7159       type = TREE_TYPE (t);
   7160 
   7161       if (TREE_CODE (type) == REFERENCE_TYPE)
   7162 	type = TREE_TYPE (type);
   7163 
   7164       if (TREE_CODE (type) != POINTER_TYPE)
   7165 	{
   7166 	  error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
   7167 		    user_omp_clause_code_name (c, true));
   7168 	  return true;
   7169 	}
   7170     }
   7171 
   7172   return false;
   7173 }
   7174 
   7175 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
   7176    Remove any elements from the list that are invalid.  */
   7177 
   7178 tree
   7179 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
   7180 {
   7181   bitmap_head generic_head, firstprivate_head, lastprivate_head;
   7182   bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
   7183   bitmap_head oacc_reduction_head, is_on_device_head;
   7184   tree c, t, *pc;
   7185   tree safelen = NULL_TREE;
   7186   bool openacc = (ort & C_ORT_ACC) != 0;
   7187   bool branch_seen = false;
   7188   bool copyprivate_seen = false;
   7189   bool ordered_seen = false;
   7190   bool order_seen = false;
   7191   bool schedule_seen = false;
   7192   bool oacc_async = false;
   7193   bool indir_component_ref_p = false;
   7194   tree last_iterators = NULL_TREE;
   7195   bool last_iterators_remove = false;
   7196   /* 1 if normal/task reduction has been seen, -1 if inscan reduction
   7197      has been seen, -2 if mixed inscan/normal reduction diagnosed.  */
   7198   int reduction_seen = 0;
   7199   bool allocate_seen = false;
   7200   tree detach_seen = NULL_TREE;
   7201   bool mergeable_seen = false;
   7202   bool implicit_moved = false;
   7203   bool target_in_reduction_seen = false;
   7204 
   7205   bitmap_obstack_initialize (NULL);
   7206   bitmap_initialize (&generic_head, &bitmap_default_obstack);
   7207   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
   7208   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
   7209   bitmap_initialize (&aligned_head, &bitmap_default_obstack);
   7210   /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead.  */
   7211   bitmap_initialize (&map_head, &bitmap_default_obstack);
   7212   bitmap_initialize (&map_field_head, &bitmap_default_obstack);
   7213   bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
   7214   /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
   7215      instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head.  */
   7216   bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
   7217   bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
   7218 
   7219   if (openacc)
   7220     for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
   7221       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
   7222 	{
   7223 	  oacc_async = true;
   7224 	  break;
   7225 	}
   7226 
   7227   tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
   7228 
   7229   for (pc = &clauses, c = clauses; c ; c = *pc)
   7230     {
   7231       bool remove = false;
   7232       bool field_ok = false;
   7233 
   7234       /* We've reached the end of a list of expanded nodes.  Reset the group
   7235 	 start pointer.  */
   7236       if (c == grp_sentinel)
   7237 	grp_start_p = NULL;
   7238 
   7239       switch (OMP_CLAUSE_CODE (c))
   7240 	{
   7241 	case OMP_CLAUSE_SHARED:
   7242 	  field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
   7243 	  goto check_dup_generic;
   7244 	case OMP_CLAUSE_PRIVATE:
   7245 	  field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
   7246 	  goto check_dup_generic;
   7247 	case OMP_CLAUSE_REDUCTION:
   7248 	  if (reduction_seen == 0)
   7249 	    reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
   7250 	  else if (reduction_seen != -2
   7251 		   && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
   7252 					 ? -1 : 1))
   7253 	    {
   7254 	      error_at (OMP_CLAUSE_LOCATION (c),
   7255 			"%<inscan%> and non-%<inscan%> %<reduction%> clauses "
   7256 			"on the same construct");
   7257 	      reduction_seen = -2;
   7258 	    }
   7259 	  /* FALLTHRU */
   7260 	case OMP_CLAUSE_IN_REDUCTION:
   7261 	case OMP_CLAUSE_TASK_REDUCTION:
   7262 	  field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
   7263 	  t = OMP_CLAUSE_DECL (c);
   7264 	  if (TREE_CODE (t) == OMP_ARRAY_SECTION)
   7265 	    {
   7266 	      if (handle_omp_array_sections (c, ort))
   7267 		{
   7268 		  remove = true;
   7269 		  break;
   7270 		}
   7271 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   7272 		  && OMP_CLAUSE_REDUCTION_INSCAN (c))
   7273 		{
   7274 		  error_at (OMP_CLAUSE_LOCATION (c),
   7275 			    "%<inscan%> %<reduction%> clause with array "
   7276 			    "section");
   7277 		  remove = true;
   7278 		  break;
   7279 		}
   7280 	      if (TREE_CODE (t) == OMP_ARRAY_SECTION)
   7281 		{
   7282 		  while (TREE_CODE (t) == OMP_ARRAY_SECTION)
   7283 		    t = TREE_OPERAND (t, 0);
   7284 		}
   7285 	      else
   7286 		{
   7287 		  gcc_assert (TREE_CODE (t) == MEM_REF);
   7288 		  t = TREE_OPERAND (t, 0);
   7289 		  if (TREE_CODE (t) == POINTER_PLUS_EXPR)
   7290 		    t = TREE_OPERAND (t, 0);
   7291 		  if (TREE_CODE (t) == ADDR_EXPR
   7292 		      || INDIRECT_REF_P (t))
   7293 		    t = TREE_OPERAND (t, 0);
   7294 		}
   7295 	      tree n = omp_clause_decl_field (t);
   7296 	      if (n)
   7297 		t = n;
   7298 	      goto check_dup_generic_t;
   7299 	    }
   7300 	  if (oacc_async)
   7301 	    cxx_mark_addressable (t);
   7302 	  goto check_dup_generic;
   7303 	case OMP_CLAUSE_COPYPRIVATE:
   7304 	  copyprivate_seen = true;
   7305 	  field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
   7306 	  goto check_dup_generic;
   7307 	case OMP_CLAUSE_COPYIN:
   7308 	  goto check_dup_generic;
   7309 	case OMP_CLAUSE_LINEAR:
   7310 	  field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
   7311 	  t = OMP_CLAUSE_DECL (c);
   7312 	  if (ort != C_ORT_OMP_DECLARE_SIMD
   7313 	      && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
   7314 	    {
   7315 	      if (OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
   7316 		{
   7317 		  error_at (OMP_CLAUSE_LOCATION (c),
   7318 			    "modifier should not be specified in %<linear%> "
   7319 			    "clause on %<simd%> or %<for%> constructs when "
   7320 			    "not using OpenMP 5.2 modifiers");
   7321 		  OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
   7322 		}
   7323 	      else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_VAL)
   7324 		{
   7325 		  error_at (OMP_CLAUSE_LOCATION (c),
   7326 			    "modifier other than %<val%> specified in "
   7327 			    "%<linear%> clause on %<simd%> or %<for%> "
   7328 			    "constructs when using OpenMP 5.2 modifiers");
   7329 		  OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
   7330 		}
   7331 	    }
   7332 	  if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
   7333 	      && !type_dependent_expression_p (t))
   7334 	    {
   7335 	      tree type = TREE_TYPE (t);
   7336 	      if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
   7337 		   || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
   7338 		  && !TYPE_REF_P (type))
   7339 		{
   7340 		  error_at (OMP_CLAUSE_LOCATION (c),
   7341 			    "linear clause with %qs modifier applied to "
   7342 			    "non-reference variable with %qT type",
   7343 			    OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
   7344 			    ? "ref" : "uval", TREE_TYPE (t));
   7345 		  remove = true;
   7346 		  break;
   7347 		}
   7348 	      if (TYPE_REF_P (type))
   7349 		type = TREE_TYPE (type);
   7350 	      if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
   7351 		{
   7352 		  if (!INTEGRAL_TYPE_P (type)
   7353 		      && !TYPE_PTR_P (type))
   7354 		    {
   7355 		      error_at (OMP_CLAUSE_LOCATION (c),
   7356 				"linear clause applied to non-integral "
   7357 				"non-pointer variable with %qT type",
   7358 				TREE_TYPE (t));
   7359 		      remove = true;
   7360 		      break;
   7361 		    }
   7362 		}
   7363 	    }
   7364 	  t = OMP_CLAUSE_LINEAR_STEP (c);
   7365 	  if (t == NULL_TREE)
   7366 	    t = integer_one_node;
   7367 	  if (t == error_mark_node)
   7368 	    {
   7369 	      remove = true;
   7370 	      break;
   7371 	    }
   7372 	  else if (!type_dependent_expression_p (t)
   7373 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t))
   7374 		   && (ort != C_ORT_OMP_DECLARE_SIMD
   7375 		       || TREE_CODE (t) != PARM_DECL
   7376 		       || !TYPE_REF_P (TREE_TYPE (t))
   7377 		       || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
   7378 	    {
   7379 	      error_at (OMP_CLAUSE_LOCATION (c),
   7380 			"linear step expression must be integral");
   7381 	      remove = true;
   7382 	      break;
   7383 	    }
   7384 	  else
   7385 	    {
   7386 	      t = mark_rvalue_use (t);
   7387 	      if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
   7388 		{
   7389 		  OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
   7390 		  goto check_dup_generic;
   7391 		}
   7392 	      if (!processing_template_decl
   7393 		  && (VAR_P (OMP_CLAUSE_DECL (c))
   7394 		      || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
   7395 		{
   7396 		  if (ort == C_ORT_OMP_DECLARE_SIMD)
   7397 		    {
   7398 		      t = maybe_constant_value (t);
   7399 		      if (TREE_CODE (t) != INTEGER_CST)
   7400 			{
   7401 			  error_at (OMP_CLAUSE_LOCATION (c),
   7402 				    "%<linear%> clause step %qE is neither "
   7403 				     "constant nor a parameter", t);
   7404 			  remove = true;
   7405 			  break;
   7406 			}
   7407 		    }
   7408 		  t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7409 		  tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
   7410 		  if (TYPE_REF_P (type))
   7411 		    type = TREE_TYPE (type);
   7412 		  if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
   7413 		    {
   7414 		      type = build_pointer_type (type);
   7415 		      tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
   7416 		      t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
   7417 					   d, t);
   7418 		      t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
   7419 					   MINUS_EXPR, sizetype,
   7420 					   fold_convert (sizetype, t),
   7421 					   fold_convert (sizetype, d));
   7422 		      if (t == error_mark_node)
   7423 			{
   7424 			  remove = true;
   7425 			  break;
   7426 			}
   7427 		    }
   7428 		  else if (TYPE_PTR_P (type)
   7429 			   /* Can't multiply the step yet if *this
   7430 			      is still incomplete type.  */
   7431 			   && (ort != C_ORT_OMP_DECLARE_SIMD
   7432 			       || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
   7433 			       || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
   7434 			       || DECL_NAME (OMP_CLAUSE_DECL (c))
   7435 				  != this_identifier
   7436 			       || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
   7437 		    {
   7438 		      tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
   7439 		      t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
   7440 					   d, t);
   7441 		      t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
   7442 					   MINUS_EXPR, sizetype,
   7443 					   fold_convert (sizetype, t),
   7444 					   fold_convert (sizetype, d));
   7445 		      if (t == error_mark_node)
   7446 			{
   7447 			  remove = true;
   7448 			  break;
   7449 			}
   7450 		    }
   7451 		  else
   7452 		    t = fold_convert (type, t);
   7453 		}
   7454 	      OMP_CLAUSE_LINEAR_STEP (c) = t;
   7455 	    }
   7456 	  goto check_dup_generic;
   7457 	check_dup_generic:
   7458 	  t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
   7459 	  if (t)
   7460 	    {
   7461 	      if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
   7462 		omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
   7463 	    }
   7464 	  else
   7465 	    t = OMP_CLAUSE_DECL (c);
   7466 	check_dup_generic_t:
   7467 	  if (t == current_class_ptr
   7468 	      && ((ort != C_ORT_OMP_DECLARE_SIMD && !openacc)
   7469 		  || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
   7470 		      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
   7471 	    {
   7472 	      error_at (OMP_CLAUSE_LOCATION (c),
   7473 			"%<this%> allowed in OpenMP only in %<declare simd%>"
   7474 			" clauses");
   7475 	      remove = true;
   7476 	      break;
   7477 	    }
   7478 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
   7479 	      && (!field_ok || TREE_CODE (t) != FIELD_DECL))
   7480 	    {
   7481 	      if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   7482 		break;
   7483 	      if (DECL_P (t))
   7484 		error_at (OMP_CLAUSE_LOCATION (c),
   7485 			  "%qD is not a variable in clause %qs", t,
   7486 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   7487 	      else
   7488 		error_at (OMP_CLAUSE_LOCATION (c),
   7489 			  "%qE is not a variable in clause %qs", t,
   7490 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   7491 	      remove = true;
   7492 	    }
   7493 	  else if ((openacc
   7494 		    && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
   7495 		   || (ort == C_ORT_OMP
   7496 		       && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
   7497 			   || (OMP_CLAUSE_CODE (c)
   7498 			       == OMP_CLAUSE_USE_DEVICE_ADDR)))
   7499 		   || (ort == C_ORT_OMP_TARGET
   7500 		       && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
   7501 	    {
   7502 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   7503 		  && (bitmap_bit_p (&generic_head, DECL_UID (t))
   7504 		      || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
   7505 		{
   7506 		  error_at (OMP_CLAUSE_LOCATION (c),
   7507 			    "%qD appears more than once in data-sharing "
   7508 			    "clauses", t);
   7509 		  remove = true;
   7510 		  break;
   7511 		}
   7512 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
   7513 		target_in_reduction_seen = true;
   7514 	      if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
   7515 		{
   7516 		  error_at (OMP_CLAUSE_LOCATION (c),
   7517 			    openacc
   7518 			    ? "%qD appears more than once in reduction clauses"
   7519 			    : "%qD appears more than once in data clauses",
   7520 			    t);
   7521 		  remove = true;
   7522 		}
   7523 	      else
   7524 		bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
   7525 	    }
   7526 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
   7527 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
   7528 		   || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
   7529 		   || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
   7530 	    {
   7531 	      error_at (OMP_CLAUSE_LOCATION (c),
   7532 			"%qD appears more than once in data clauses", t);
   7533 	      remove = true;
   7534 	    }
   7535 	  else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
   7536 		    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
   7537 		    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
   7538 		   && bitmap_bit_p (&map_head, DECL_UID (t)))
   7539 	    {
   7540 	      if (openacc)
   7541 		error_at (OMP_CLAUSE_LOCATION (c),
   7542 			  "%qD appears more than once in data clauses", t);
   7543 	      else
   7544 		error_at (OMP_CLAUSE_LOCATION (c),
   7545 			  "%qD appears both in data and map clauses", t);
   7546 	      remove = true;
   7547 	    }
   7548 	  else
   7549 	    bitmap_set_bit (&generic_head, DECL_UID (t));
   7550 	  if (!field_ok)
   7551 	    break;
   7552 	handle_field_decl:
   7553 	  if (!remove
   7554 	      && TREE_CODE (t) == FIELD_DECL
   7555 	      && t == OMP_CLAUSE_DECL (c))
   7556 	    {
   7557 	      OMP_CLAUSE_DECL (c)
   7558 		= omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
   7559 					   == OMP_CLAUSE_SHARED));
   7560 	      if (OMP_CLAUSE_DECL (c) == error_mark_node)
   7561 		remove = true;
   7562 	    }
   7563 	  break;
   7564 
   7565 	case OMP_CLAUSE_FIRSTPRIVATE:
   7566 	  if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
   7567 	    {
   7568 	    move_implicit:
   7569 	      implicit_moved = true;
   7570 	      /* Move firstprivate and map clauses with
   7571 		 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
   7572 		 clauses chain.  */
   7573 	      tree cl1 = NULL_TREE, cl2 = NULL_TREE;
   7574 	      tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
   7575 	      while (*pc1)
   7576 		if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
   7577 		    && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
   7578 		  {
   7579 		    *pc3 = *pc1;
   7580 		    pc3 = &OMP_CLAUSE_CHAIN (*pc3);
   7581 		    *pc1 = OMP_CLAUSE_CHAIN (*pc1);
   7582 		  }
   7583 		else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
   7584 			 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
   7585 		  {
   7586 		    *pc2 = *pc1;
   7587 		    pc2 = &OMP_CLAUSE_CHAIN (*pc2);
   7588 		    *pc1 = OMP_CLAUSE_CHAIN (*pc1);
   7589 		  }
   7590 		else
   7591 		  pc1 = &OMP_CLAUSE_CHAIN (*pc1);
   7592 	      *pc3 = NULL;
   7593 	      *pc2 = cl2;
   7594 	      *pc1 = cl1;
   7595 	      continue;
   7596 	    }
   7597 	  t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
   7598 	  if (t)
   7599 	    omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
   7600 	  else
   7601 	    t = OMP_CLAUSE_DECL (c);
   7602 	  if (!openacc && t == current_class_ptr)
   7603 	    {
   7604 	      error_at (OMP_CLAUSE_LOCATION (c),
   7605 			"%<this%> allowed in OpenMP only in %<declare simd%>"
   7606 			" clauses");
   7607 	      remove = true;
   7608 	      break;
   7609 	    }
   7610 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
   7611 	      && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
   7612 		  || TREE_CODE (t) != FIELD_DECL))
   7613 	    {
   7614 	      if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   7615 		break;
   7616 	      if (DECL_P (t))
   7617 		error_at (OMP_CLAUSE_LOCATION (c),
   7618 			  "%qD is not a variable in clause %<firstprivate%>",
   7619 			  t);
   7620 	      else
   7621 		error_at (OMP_CLAUSE_LOCATION (c),
   7622 			  "%qE is not a variable in clause %<firstprivate%>",
   7623 			  t);
   7624 	      remove = true;
   7625 	    }
   7626 	  else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
   7627 		   && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
   7628 		   && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
   7629 	    remove = true;
   7630 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
   7631 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
   7632 		   || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
   7633 	    {
   7634 	      error_at (OMP_CLAUSE_LOCATION (c),
   7635 			"%qD appears more than once in data clauses", t);
   7636 	      remove = true;
   7637 	    }
   7638 	  else if (bitmap_bit_p (&map_head, DECL_UID (t))
   7639 		   || bitmap_bit_p (&map_field_head, DECL_UID (t)))
   7640 	    {
   7641 	      if (openacc)
   7642 		error_at (OMP_CLAUSE_LOCATION (c),
   7643 			  "%qD appears more than once in data clauses", t);
   7644 	      else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
   7645 		       && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
   7646 		/* Silently drop the clause.  */;
   7647 	      else
   7648 		error_at (OMP_CLAUSE_LOCATION (c),
   7649 			  "%qD appears both in data and map clauses", t);
   7650 	      remove = true;
   7651 	    }
   7652 	  else
   7653 	    bitmap_set_bit (&firstprivate_head, DECL_UID (t));
   7654 	  goto handle_field_decl;
   7655 
   7656 	case OMP_CLAUSE_LASTPRIVATE:
   7657 	  t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
   7658 	  if (t)
   7659 	    omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
   7660 	  else
   7661 	    t = OMP_CLAUSE_DECL (c);
   7662 	  if (!openacc && t == current_class_ptr)
   7663 	    {
   7664 	      error_at (OMP_CLAUSE_LOCATION (c),
   7665 			"%<this%> allowed in OpenMP only in %<declare simd%>"
   7666 			" clauses");
   7667 	      remove = true;
   7668 	      break;
   7669 	    }
   7670 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
   7671 	      && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
   7672 		  || TREE_CODE (t) != FIELD_DECL))
   7673 	    {
   7674 	      if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   7675 		break;
   7676 	      if (DECL_P (t))
   7677 		error_at (OMP_CLAUSE_LOCATION (c),
   7678 			  "%qD is not a variable in clause %<lastprivate%>",
   7679 			  t);
   7680 	      else
   7681 		error_at (OMP_CLAUSE_LOCATION (c),
   7682 			  "%qE is not a variable in clause %<lastprivate%>",
   7683 			  t);
   7684 	      remove = true;
   7685 	    }
   7686 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
   7687 		   || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
   7688 	    {
   7689 	      error_at (OMP_CLAUSE_LOCATION (c),
   7690 			"%qD appears more than once in data clauses", t);
   7691 	      remove = true;
   7692 	    }
   7693 	  else
   7694 	    bitmap_set_bit (&lastprivate_head, DECL_UID (t));
   7695 	  goto handle_field_decl;
   7696 
   7697 	case OMP_CLAUSE_IF:
   7698 	case OMP_CLAUSE_SELF:
   7699 	  t = OMP_CLAUSE_OPERAND (c, 0);
   7700 	  t = maybe_convert_cond (t);
   7701 	  if (t == error_mark_node)
   7702 	    remove = true;
   7703 	  else if (!processing_template_decl)
   7704 	    t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7705 	  OMP_CLAUSE_OPERAND (c, 0) = t;
   7706 	  break;
   7707 
   7708 	case OMP_CLAUSE_FINAL:
   7709 	  t = OMP_CLAUSE_FINAL_EXPR (c);
   7710 	  t = maybe_convert_cond (t);
   7711 	  if (t == error_mark_node)
   7712 	    remove = true;
   7713 	  else if (!processing_template_decl)
   7714 	    t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7715 	  OMP_CLAUSE_FINAL_EXPR (c) = t;
   7716 	  break;
   7717 
   7718 	case OMP_CLAUSE_GANG:
   7719 	  /* Operand 1 is the gang static: argument.  */
   7720 	  t = OMP_CLAUSE_OPERAND (c, 1);
   7721 	  if (t != NULL_TREE)
   7722 	    {
   7723 	      if (t == error_mark_node)
   7724 		remove = true;
   7725 	      else if (!type_dependent_expression_p (t)
   7726 		       && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   7727 		{
   7728 		  error_at (OMP_CLAUSE_LOCATION (c),
   7729 			    "%<gang%> static expression must be integral");
   7730 		  remove = true;
   7731 		}
   7732 	      else
   7733 		{
   7734 		  t = mark_rvalue_use (t);
   7735 		  if (!processing_template_decl)
   7736 		    {
   7737 		      t = maybe_constant_value (t);
   7738 		      if (TREE_CODE (t) == INTEGER_CST
   7739 			  && tree_int_cst_sgn (t) != 1
   7740 			  && t != integer_minus_one_node)
   7741 			{
   7742 			  warning_at (OMP_CLAUSE_LOCATION (c), 0,
   7743 				      "%<gang%> static value must be "
   7744 				      "positive");
   7745 			  t = integer_one_node;
   7746 			}
   7747 		      t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7748 		    }
   7749 		}
   7750 	      OMP_CLAUSE_OPERAND (c, 1) = t;
   7751 	    }
   7752 	  /* Check operand 0, the num argument.  */
   7753 	  /* FALLTHRU */
   7754 
   7755 	case OMP_CLAUSE_WORKER:
   7756 	case OMP_CLAUSE_VECTOR:
   7757 	  if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
   7758 	    break;
   7759 	  /* FALLTHRU */
   7760 
   7761 	case OMP_CLAUSE_NUM_TASKS:
   7762 	case OMP_CLAUSE_NUM_TEAMS:
   7763 	case OMP_CLAUSE_NUM_THREADS:
   7764 	case OMP_CLAUSE_NUM_GANGS:
   7765 	case OMP_CLAUSE_NUM_WORKERS:
   7766 	case OMP_CLAUSE_VECTOR_LENGTH:
   7767 	  t = OMP_CLAUSE_OPERAND (c, 0);
   7768 	  if (t == error_mark_node)
   7769 	    remove = true;
   7770 	  else if (!type_dependent_expression_p (t)
   7771 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   7772 	    {
   7773 	     switch (OMP_CLAUSE_CODE (c))
   7774 		{
   7775 		case OMP_CLAUSE_GANG:
   7776 		  error_at (OMP_CLAUSE_LOCATION (c),
   7777 			    "%<gang%> num expression must be integral"); break;
   7778 		case OMP_CLAUSE_VECTOR:
   7779 		  error_at (OMP_CLAUSE_LOCATION (c),
   7780 			    "%<vector%> length expression must be integral");
   7781 		  break;
   7782 		case OMP_CLAUSE_WORKER:
   7783 		  error_at (OMP_CLAUSE_LOCATION (c),
   7784 			    "%<worker%> num expression must be integral");
   7785 		  break;
   7786 		default:
   7787 		  error_at (OMP_CLAUSE_LOCATION (c),
   7788 			    "%qs expression must be integral",
   7789 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   7790 		}
   7791 	      remove = true;
   7792 	    }
   7793 	  else
   7794 	    {
   7795 	      t = mark_rvalue_use (t);
   7796 	      if (!processing_template_decl)
   7797 		{
   7798 		  t = maybe_constant_value (t);
   7799 		  if (TREE_CODE (t) == INTEGER_CST
   7800 		      && tree_int_cst_sgn (t) != 1)
   7801 		    {
   7802 		      switch (OMP_CLAUSE_CODE (c))
   7803 			{
   7804 			case OMP_CLAUSE_GANG:
   7805 			  warning_at (OMP_CLAUSE_LOCATION (c), 0,
   7806 				      "%<gang%> num value must be positive");
   7807 			  break;
   7808 			case OMP_CLAUSE_VECTOR:
   7809 			  warning_at (OMP_CLAUSE_LOCATION (c), 0,
   7810 				      "%<vector%> length value must be "
   7811 				      "positive");
   7812 			  break;
   7813 			case OMP_CLAUSE_WORKER:
   7814 			  warning_at (OMP_CLAUSE_LOCATION (c), 0,
   7815 				      "%<worker%> num value must be "
   7816 				      "positive");
   7817 			  break;
   7818 			default:
   7819 			  warning_at (OMP_CLAUSE_LOCATION (c),
   7820 				      (flag_openmp || flag_openmp_simd)
   7821 				      ? OPT_Wopenmp : 0,
   7822 				      "%qs value must be positive",
   7823 				      omp_clause_code_name
   7824 				      [OMP_CLAUSE_CODE (c)]);
   7825 			}
   7826 		      t = integer_one_node;
   7827 		    }
   7828 		  t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7829 		}
   7830 	      OMP_CLAUSE_OPERAND (c, 0) = t;
   7831 	    }
   7832 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
   7833 	      && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
   7834 	      && !remove)
   7835 	    {
   7836 	      t = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
   7837 	      if (t == error_mark_node)
   7838 		remove = true;
   7839 	      else if (!type_dependent_expression_p (t)
   7840 		       && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   7841 		{
   7842 		  error_at (OMP_CLAUSE_LOCATION (c),
   7843 			    "%qs expression must be integral",
   7844 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   7845 		  remove = true;
   7846 		}
   7847 	      else
   7848 		{
   7849 		  t = mark_rvalue_use (t);
   7850 		  if (!processing_template_decl)
   7851 		    {
   7852 		      t = maybe_constant_value (t);
   7853 		      if (TREE_CODE (t) == INTEGER_CST
   7854 			  && tree_int_cst_sgn (t) != 1)
   7855 			{
   7856 			  warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   7857 				      "%qs value must be positive",
   7858 				      omp_clause_code_name
   7859 				      [OMP_CLAUSE_CODE (c)]);
   7860 			  t = NULL_TREE;
   7861 			}
   7862 		      else
   7863 			t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7864 		      tree upper = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
   7865 		      if (t
   7866 			  && TREE_CODE (t) == INTEGER_CST
   7867 			  && TREE_CODE (upper) == INTEGER_CST
   7868 			  && tree_int_cst_lt (upper, t))
   7869 			{
   7870 			  warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   7871 				      "%<num_teams%> lower bound %qE bigger "
   7872 				      "than upper bound %qE", t, upper);
   7873 			  t = NULL_TREE;
   7874 			}
   7875 		    }
   7876 		  OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = t;
   7877 		}
   7878 	    }
   7879 	  break;
   7880 
   7881 	case OMP_CLAUSE_SCHEDULE:
   7882 	  t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
   7883 	  if (t == NULL)
   7884 	    ;
   7885 	  else if (t == error_mark_node)
   7886 	    remove = true;
   7887 	  else if (!type_dependent_expression_p (t)
   7888 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   7889 	    {
   7890 	      error_at (OMP_CLAUSE_LOCATION (c),
   7891 			"schedule chunk size expression must be integral");
   7892 	      remove = true;
   7893 	    }
   7894 	  else
   7895 	    {
   7896 	      t = mark_rvalue_use (t);
   7897 	      if (!processing_template_decl)
   7898 		{
   7899 		  t = maybe_constant_value (t);
   7900 		  if (TREE_CODE (t) == INTEGER_CST
   7901 		      && tree_int_cst_sgn (t) != 1)
   7902 		  {
   7903 		    warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   7904 			      "chunk size value must be positive");
   7905 		    t = integer_one_node;
   7906 		  }
   7907 		  t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7908 		}
   7909 	      OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
   7910 	    }
   7911 	  if (!remove)
   7912 	    schedule_seen = true;
   7913 	  break;
   7914 
   7915 	case OMP_CLAUSE_SIMDLEN:
   7916 	case OMP_CLAUSE_SAFELEN:
   7917 	  t = OMP_CLAUSE_OPERAND (c, 0);
   7918 	  if (t == error_mark_node)
   7919 	    remove = true;
   7920 	  else if (!type_dependent_expression_p (t)
   7921 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   7922 	    {
   7923 	      error_at (OMP_CLAUSE_LOCATION (c),
   7924 			"%qs length expression must be integral",
   7925 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   7926 	      remove = true;
   7927 	    }
   7928 	  else
   7929 	    {
   7930 	      t = mark_rvalue_use (t);
   7931 	      if (!processing_template_decl)
   7932 		{
   7933 		  t = maybe_constant_value (t);
   7934 		  if (TREE_CODE (t) != INTEGER_CST
   7935 		      || tree_int_cst_sgn (t) != 1)
   7936 		    {
   7937 		      error_at (OMP_CLAUSE_LOCATION (c),
   7938 				"%qs length expression must be positive "
   7939 				"constant integer expression",
   7940 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   7941 		      remove = true;
   7942 		    }
   7943 		}
   7944 	      OMP_CLAUSE_OPERAND (c, 0) = t;
   7945 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
   7946 		safelen = c;
   7947 	    }
   7948 	  break;
   7949 
   7950 	case OMP_CLAUSE_ASYNC:
   7951 	  t = OMP_CLAUSE_ASYNC_EXPR (c);
   7952 	  if (t == error_mark_node)
   7953 	    remove = true;
   7954 	  else if (!type_dependent_expression_p (t)
   7955 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   7956 	    {
   7957 	      error_at (OMP_CLAUSE_LOCATION (c),
   7958 			"%<async%> expression must be integral");
   7959 	      remove = true;
   7960 	    }
   7961 	  else
   7962 	    {
   7963 	      t = mark_rvalue_use (t);
   7964 	      if (!processing_template_decl)
   7965 		t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7966 	      OMP_CLAUSE_ASYNC_EXPR (c) = t;
   7967 	    }
   7968 	  break;
   7969 
   7970 	case OMP_CLAUSE_WAIT:
   7971 	  t = OMP_CLAUSE_WAIT_EXPR (c);
   7972 	  if (t == error_mark_node)
   7973 	    remove = true;
   7974 	  else if (!processing_template_decl)
   7975 	    t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   7976 	  OMP_CLAUSE_WAIT_EXPR (c) = t;
   7977 	  break;
   7978 
   7979 	case OMP_CLAUSE_THREAD_LIMIT:
   7980 	  t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
   7981 	  if (t == error_mark_node)
   7982 	    remove = true;
   7983 	  else if (!type_dependent_expression_p (t)
   7984 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   7985 	    {
   7986 	      error_at (OMP_CLAUSE_LOCATION (c),
   7987 			"%<thread_limit%> expression must be integral");
   7988 	      remove = true;
   7989 	    }
   7990 	  else
   7991 	    {
   7992 	      t = mark_rvalue_use (t);
   7993 	      if (!processing_template_decl)
   7994 		{
   7995 		  t = maybe_constant_value (t);
   7996 		  if (TREE_CODE (t) == INTEGER_CST
   7997 		      && tree_int_cst_sgn (t) != 1)
   7998 		    {
   7999 		      warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   8000 				  "%<thread_limit%> value must be positive");
   8001 		      t = integer_one_node;
   8002 		    }
   8003 		  t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   8004 		}
   8005 	      OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
   8006 	    }
   8007 	  break;
   8008 
   8009 	case OMP_CLAUSE_DEVICE:
   8010 	  t = OMP_CLAUSE_DEVICE_ID (c);
   8011 	  if (t == error_mark_node)
   8012 	    remove = true;
   8013 	  else if (!type_dependent_expression_p (t)
   8014 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   8015 	    {
   8016 	      error_at (OMP_CLAUSE_LOCATION (c),
   8017 			"%<device%> id must be integral");
   8018 	      remove = true;
   8019 	    }
   8020 	  else if (OMP_CLAUSE_DEVICE_ANCESTOR (c)
   8021 		   && TREE_CODE (t) == INTEGER_CST
   8022 		   && !integer_onep (t))
   8023 	    {
   8024 	      error_at (OMP_CLAUSE_LOCATION (c),
   8025 			"the %<device%> clause expression must evaluate to "
   8026 			"%<1%>");
   8027 	      remove = true;
   8028 	    }
   8029 	  else
   8030 	    {
   8031 	      t = mark_rvalue_use (t);
   8032 	      if (!processing_template_decl)
   8033 		t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   8034 	      OMP_CLAUSE_DEVICE_ID (c) = t;
   8035 	    }
   8036 	  break;
   8037 
   8038 	case OMP_CLAUSE_DIST_SCHEDULE:
   8039 	  t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
   8040 	  if (t == NULL)
   8041 	    ;
   8042 	  else if (t == error_mark_node)
   8043 	    remove = true;
   8044 	  else if (!type_dependent_expression_p (t)
   8045 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   8046 	    {
   8047 	      error_at (OMP_CLAUSE_LOCATION (c),
   8048 			"%<dist_schedule%> chunk size expression must be "
   8049 			"integral");
   8050 	      remove = true;
   8051 	    }
   8052 	  else
   8053 	    {
   8054 	      t = mark_rvalue_use (t);
   8055  	      if (!processing_template_decl)
   8056 		t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   8057 	      OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
   8058 	    }
   8059 	  break;
   8060 
   8061 	case OMP_CLAUSE_ALIGNED:
   8062 	  t = OMP_CLAUSE_DECL (c);
   8063 	  if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
   8064 	    {
   8065 	      error_at (OMP_CLAUSE_LOCATION (c),
   8066 			"%<this%> allowed in OpenMP only in %<declare simd%>"
   8067 			" clauses");
   8068 	      remove = true;
   8069 	      break;
   8070 	    }
   8071 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
   8072 	    {
   8073 	      if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   8074 		break;
   8075 	      if (DECL_P (t))
   8076 		error_at (OMP_CLAUSE_LOCATION (c),
   8077 			  "%qD is not a variable in %<aligned%> clause", t);
   8078 	      else
   8079 		error_at (OMP_CLAUSE_LOCATION (c),
   8080 			  "%qE is not a variable in %<aligned%> clause", t);
   8081 	      remove = true;
   8082 	    }
   8083 	  else if (!type_dependent_expression_p (t)
   8084 		   && !TYPE_PTR_P (TREE_TYPE (t))
   8085 		   && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
   8086 		   && (!TYPE_REF_P (TREE_TYPE (t))
   8087 		       || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
   8088 			   && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
   8089 			       != ARRAY_TYPE))))
   8090 	    {
   8091 	      error_at (OMP_CLAUSE_LOCATION (c),
   8092 			"%qE in %<aligned%> clause is neither a pointer nor "
   8093 			"an array nor a reference to pointer or array", t);
   8094 	      remove = true;
   8095 	    }
   8096 	  else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
   8097 	    {
   8098 	      error_at (OMP_CLAUSE_LOCATION (c),
   8099 			"%qD appears more than once in %<aligned%> clauses",
   8100 			t);
   8101 	      remove = true;
   8102 	    }
   8103 	  else
   8104 	    bitmap_set_bit (&aligned_head, DECL_UID (t));
   8105 	  t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
   8106 	  if (t == error_mark_node)
   8107 	    remove = true;
   8108 	  else if (t == NULL_TREE)
   8109 	    break;
   8110 	  else if (!type_dependent_expression_p (t)
   8111 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   8112 	    {
   8113 	      error_at (OMP_CLAUSE_LOCATION (c),
   8114 			"%<aligned%> clause alignment expression must "
   8115 			"be integral");
   8116 	      remove = true;
   8117 	    }
   8118 	  else
   8119 	    {
   8120 	      t = mark_rvalue_use (t);
   8121 	      if (!processing_template_decl)
   8122 		{
   8123 		  t = maybe_constant_value (t);
   8124 		  if (TREE_CODE (t) != INTEGER_CST
   8125 		      || tree_int_cst_sgn (t) != 1)
   8126 		    {
   8127 		      error_at (OMP_CLAUSE_LOCATION (c),
   8128 				"%<aligned%> clause alignment expression must "
   8129 				"be positive constant integer expression");
   8130 		      remove = true;
   8131 		    }
   8132 		  else
   8133 		    t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   8134 		}
   8135 	      OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
   8136 	    }
   8137 	  break;
   8138 
   8139 	case OMP_CLAUSE_NONTEMPORAL:
   8140 	  t = OMP_CLAUSE_DECL (c);
   8141 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
   8142 	    {
   8143 	      if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   8144 		break;
   8145 	      if (DECL_P (t))
   8146 		error_at (OMP_CLAUSE_LOCATION (c),
   8147 			  "%qD is not a variable in %<nontemporal%> clause",
   8148 			  t);
   8149 	      else
   8150 		error_at (OMP_CLAUSE_LOCATION (c),
   8151 			  "%qE is not a variable in %<nontemporal%> clause",
   8152 			  t);
   8153 	      remove = true;
   8154 	    }
   8155 	  else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
   8156 	    {
   8157 	      error_at (OMP_CLAUSE_LOCATION (c),
   8158 			"%qD appears more than once in %<nontemporal%> "
   8159 			"clauses", t);
   8160 	      remove = true;
   8161 	    }
   8162 	  else
   8163 	    bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
   8164 	  break;
   8165 
   8166 	case OMP_CLAUSE_ALLOCATE:
   8167 	  t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
   8168 	  if (t)
   8169 	    omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
   8170 	  else
   8171 	    t = OMP_CLAUSE_DECL (c);
   8172 	  if (t == current_class_ptr)
   8173 	    {
   8174 	      error_at (OMP_CLAUSE_LOCATION (c),
   8175 			"%<this%> not allowed in %<allocate%> clause");
   8176 	      remove = true;
   8177 	      break;
   8178 	    }
   8179 	  if (!VAR_P (t)
   8180 	      && TREE_CODE (t) != PARM_DECL
   8181 	      && TREE_CODE (t) != FIELD_DECL)
   8182 	    {
   8183 	      if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   8184 		break;
   8185 	      if (DECL_P (t))
   8186 		error_at (OMP_CLAUSE_LOCATION (c),
   8187 			  "%qD is not a variable in %<allocate%> clause", t);
   8188 	      else
   8189 		error_at (OMP_CLAUSE_LOCATION (c),
   8190 			  "%qE is not a variable in %<allocate%> clause", t);
   8191 	      remove = true;
   8192 	    }
   8193 	  else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
   8194 	    {
   8195 	      warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   8196 			"%qD appears more than once in %<allocate%> clauses",
   8197 			t);
   8198 	      remove = true;
   8199 	    }
   8200 	  else
   8201 	    {
   8202 	      bitmap_set_bit (&aligned_head, DECL_UID (t));
   8203 	      allocate_seen = true;
   8204 	    }
   8205 	  tree allocator, align;
   8206 	  align = OMP_CLAUSE_ALLOCATE_ALIGN (c);
   8207 	  if (error_operand_p (align))
   8208 	    {
   8209 	      remove = true;
   8210 	      break;
   8211 	    }
   8212 	  if (align)
   8213 	    {
   8214 	      if (!type_dependent_expression_p (align)
   8215 		  && !INTEGRAL_TYPE_P (TREE_TYPE (align)))
   8216 		{
   8217 		  error_at (OMP_CLAUSE_LOCATION (c),
   8218 			    "%<allocate%> clause %<align%> modifier "
   8219 			    "argument needs to be positive constant "
   8220 			    "power of two integer expression");
   8221 		  remove = true;
   8222 		}
   8223 	      else
   8224 		{
   8225 		  align = mark_rvalue_use (align);
   8226 		  if (!processing_template_decl)
   8227 		    {
   8228 		      align = maybe_constant_value (align);
   8229 		      if (TREE_CODE (align) != INTEGER_CST
   8230 			  || !tree_fits_uhwi_p (align)
   8231 			  || !integer_pow2p (align))
   8232 			{
   8233 			  error_at (OMP_CLAUSE_LOCATION (c),
   8234 				    "%<allocate%> clause %<align%> modifier "
   8235 				    "argument needs to be positive constant "
   8236 				    "power of two integer expression");
   8237 			  remove = true;
   8238 			}
   8239 		    }
   8240 		}
   8241 	      OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
   8242 	    }
   8243 	  allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
   8244 	  if (error_operand_p (allocator))
   8245 	    {
   8246 	      remove = true;
   8247 	      break;
   8248 	    }
   8249 	  if (allocator == NULL_TREE)
   8250 	    goto handle_field_decl;
   8251 	  tree allocatort;
   8252 	  allocatort = TYPE_MAIN_VARIANT (TREE_TYPE (allocator));
   8253 	  if (!type_dependent_expression_p (allocator)
   8254 	      && (TREE_CODE (allocatort) != ENUMERAL_TYPE
   8255 		  || TYPE_NAME (allocatort) == NULL_TREE
   8256 		  || TREE_CODE (TYPE_NAME (allocatort)) != TYPE_DECL
   8257 		  || (DECL_NAME (TYPE_NAME (allocatort))
   8258 		      != get_identifier ("omp_allocator_handle_t"))
   8259 		  || (TYPE_CONTEXT (allocatort)
   8260 		      != DECL_CONTEXT (global_namespace))))
   8261 	    {
   8262 	      error_at (OMP_CLAUSE_LOCATION (c),
   8263 			"%<allocate%> clause allocator expression has "
   8264 			"type %qT rather than %<omp_allocator_handle_t%>",
   8265 			TREE_TYPE (allocator));
   8266 	      remove = true;
   8267 	      break;
   8268 	    }
   8269 	  else
   8270 	    {
   8271 	      allocator = mark_rvalue_use (allocator);
   8272 	      if (!processing_template_decl)
   8273 		allocator = maybe_constant_value (allocator);
   8274 	      OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
   8275 	    }
   8276 	  goto handle_field_decl;
   8277 
   8278 	case OMP_CLAUSE_DOACROSS:
   8279 	  t = OMP_CLAUSE_DECL (c);
   8280 	  if (t == NULL_TREE)
   8281 	    break;
   8282 	  if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
   8283 	    {
   8284 	      if (cp_finish_omp_clause_doacross_sink (c))
   8285 		remove = true;
   8286 	      break;
   8287 	    }
   8288 	  gcc_unreachable ();
   8289 	case OMP_CLAUSE_DEPEND:
   8290 	case OMP_CLAUSE_AFFINITY:
   8291 	  t = OMP_CLAUSE_DECL (c);
   8292 	  if (TREE_CODE (t) == TREE_LIST
   8293 	      && TREE_PURPOSE (t)
   8294 	      && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
   8295 	    {
   8296 	      if (TREE_PURPOSE (t) != last_iterators)
   8297 		last_iterators_remove
   8298 		  = cp_omp_finish_iterators (TREE_PURPOSE (t));
   8299 	      last_iterators = TREE_PURPOSE (t);
   8300 	      t = TREE_VALUE (t);
   8301 	      if (last_iterators_remove)
   8302 		t = error_mark_node;
   8303 	    }
   8304 	  else
   8305 	    last_iterators = NULL_TREE;
   8306 
   8307 	  if (TREE_CODE (t) == OMP_ARRAY_SECTION)
   8308 	    {
   8309 	      if (handle_omp_array_sections (c, ort))
   8310 		remove = true;
   8311 	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   8312 		       && (OMP_CLAUSE_DEPEND_KIND (c)
   8313 			   == OMP_CLAUSE_DEPEND_DEPOBJ))
   8314 		{
   8315 		  error_at (OMP_CLAUSE_LOCATION (c),
   8316 			    "%<depend%> clause with %<depobj%> dependence "
   8317 			    "type on array section");
   8318 		  remove = true;
   8319 		}
   8320 	      break;
   8321 	    }
   8322 	  if (t == error_mark_node)
   8323 	    remove = true;
   8324 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   8325 		   && t == ridpointers[RID_OMP_ALL_MEMORY])
   8326 	    {
   8327 	      if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
   8328 		  && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
   8329 		{
   8330 		  error_at (OMP_CLAUSE_LOCATION (c),
   8331 			    "%<omp_all_memory%> used with %<depend%> kind "
   8332 			    "other than %<out%> or %<inout%>");
   8333 		  remove = true;
   8334 		}
   8335 	      if (processing_template_decl)
   8336 		break;
   8337 	    }
   8338 	  else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   8339 	    break;
   8340 	  else if (!lvalue_p (t))
   8341 	    {
   8342 	      if (DECL_P (t))
   8343 		error_at (OMP_CLAUSE_LOCATION (c),
   8344 			  "%qD is not lvalue expression nor array section "
   8345 			  "in %qs clause", t,
   8346 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   8347 	      else
   8348 		error_at (OMP_CLAUSE_LOCATION (c),
   8349 			  "%qE is not lvalue expression nor array section "
   8350 			  "in %qs clause", t,
   8351 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   8352 	      remove = true;
   8353 	    }
   8354 	  else if (TREE_CODE (t) == COMPONENT_REF
   8355 		   && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
   8356 		   && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
   8357 	    {
   8358 	      error_at (OMP_CLAUSE_LOCATION (c),
   8359 			"bit-field %qE in %qs clause", t,
   8360 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   8361 	      remove = true;
   8362 	    }
   8363 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   8364 		   && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
   8365 	    {
   8366 	      if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
   8367 				     ? TREE_TYPE (TREE_TYPE (t))
   8368 				     : TREE_TYPE (t)))
   8369 		{
   8370 		  error_at (OMP_CLAUSE_LOCATION (c),
   8371 			    "%qE does not have %<omp_depend_t%> type in "
   8372 			    "%<depend%> clause with %<depobj%> dependence "
   8373 			    "type", t);
   8374 		  remove = true;
   8375 		}
   8376 	    }
   8377 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
   8378 		   && c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
   8379 					? TREE_TYPE (TREE_TYPE (t))
   8380 					: TREE_TYPE (t)))
   8381 	    {
   8382 	      error_at (OMP_CLAUSE_LOCATION (c),
   8383 			"%qE should not have %<omp_depend_t%> type in "
   8384 			"%<depend%> clause with dependence type other than "
   8385 			"%<depobj%>", t);
   8386 	      remove = true;
   8387 	    }
   8388 	  if (!remove)
   8389 	    {
   8390 	      if (t == ridpointers[RID_OMP_ALL_MEMORY])
   8391 		t = null_pointer_node;
   8392 	      else
   8393 		{
   8394 		  tree addr = cp_build_addr_expr (t, tf_warning_or_error);
   8395 		  if (addr == error_mark_node)
   8396 		    {
   8397 		      remove = true;
   8398 		      break;
   8399 		    }
   8400 		  t = cp_build_indirect_ref (OMP_CLAUSE_LOCATION (c),
   8401 					     addr, RO_UNARY_STAR,
   8402 					     tf_warning_or_error);
   8403 		  if (t == error_mark_node)
   8404 		    {
   8405 		      remove = true;
   8406 		      break;
   8407 		    }
   8408 		}
   8409 	      if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
   8410 		  && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
   8411 		  && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
   8412 		      == TREE_VEC))
   8413 		TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
   8414 	      else
   8415 		OMP_CLAUSE_DECL (c) = t;
   8416 	    }
   8417 	  break;
   8418 	case OMP_CLAUSE_DETACH:
   8419 	  t = OMP_CLAUSE_DECL (c);
   8420 	  if (detach_seen)
   8421 	    {
   8422 	      error_at (OMP_CLAUSE_LOCATION (c),
   8423 			"too many %qs clauses on a task construct",
   8424 			"detach");
   8425 	      remove = true;
   8426 	      break;
   8427 	    }
   8428 	  else if (error_operand_p (t))
   8429 	    {
   8430 	      remove = true;
   8431 	      break;
   8432 	    }
   8433 	  else
   8434 	    {
   8435 	      tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
   8436 	      if (!type_dependent_expression_p (t)
   8437 		  && (!INTEGRAL_TYPE_P (type)
   8438 		      || TREE_CODE (type) != ENUMERAL_TYPE
   8439 		      || TYPE_NAME (type) == NULL_TREE
   8440 		      || (DECL_NAME (TYPE_NAME (type))
   8441 			  != get_identifier ("omp_event_handle_t"))))
   8442 		{
   8443 		  error_at (OMP_CLAUSE_LOCATION (c),
   8444 			    "%<detach%> clause event handle "
   8445 			    "has type %qT rather than "
   8446 			    "%<omp_event_handle_t%>",
   8447 			    type);
   8448 		  remove = true;
   8449 		}
   8450 	      detach_seen = c;
   8451 	      cxx_mark_addressable (t);
   8452 	    }
   8453 	  break;
   8454 
   8455 	case OMP_CLAUSE_MAP:
   8456 	  if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
   8457 	    goto move_implicit;
   8458 	  /* FALLTHRU */
   8459 	case OMP_CLAUSE_TO:
   8460 	case OMP_CLAUSE_FROM:
   8461 	case OMP_CLAUSE__CACHE_:
   8462 	  {
   8463 	    using namespace omp_addr_tokenizer;
   8464 	    auto_vec<omp_addr_token *, 10> addr_tokens;
   8465 
   8466 	    t = OMP_CLAUSE_DECL (c);
   8467 	    if (TREE_CODE (t) == OMP_ARRAY_SECTION)
   8468 	      {
   8469 		grp_start_p = pc;
   8470 		grp_sentinel = OMP_CLAUSE_CHAIN (c);
   8471 
   8472 		if (handle_omp_array_sections (c, ort))
   8473 		  remove = true;
   8474 		else
   8475 		  {
   8476 		    t = OMP_CLAUSE_DECL (c);
   8477 		    if (TREE_CODE (t) != OMP_ARRAY_SECTION
   8478 			&& !type_dependent_expression_p (t)
   8479 			&& !omp_mappable_type (TREE_TYPE (t)))
   8480 		      {
   8481 			error_at (OMP_CLAUSE_LOCATION (c),
   8482 				  "array section does not have mappable type "
   8483 				  "in %qs clause",
   8484 				  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   8485 			if (TREE_TYPE (t) != error_mark_node
   8486 			    && !COMPLETE_TYPE_P (TREE_TYPE (t)))
   8487 			  cxx_incomplete_type_inform (TREE_TYPE (t));
   8488 			remove = true;
   8489 		      }
   8490 		    while (TREE_CODE (t) == ARRAY_REF)
   8491 		      t = TREE_OPERAND (t, 0);
   8492 
   8493 		    if (type_dependent_expression_p (t))
   8494 		      break;
   8495 
   8496 		    cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
   8497 
   8498 		    if (!ai.map_supported_p ()
   8499 			|| !omp_parse_expr (addr_tokens, t))
   8500 		      {
   8501 			sorry_at (OMP_CLAUSE_LOCATION (c),
   8502 				  "unsupported map expression %qE",
   8503 				  OMP_CLAUSE_DECL (c));
   8504 			remove = true;
   8505 			break;
   8506 		      }
   8507 
   8508 		    /* This check is to determine if this will be the only map
   8509 		       node created for this clause.  Otherwise, we'll check
   8510 		       the following FIRSTPRIVATE_POINTER,
   8511 		       FIRSTPRIVATE_REFERENCE or ATTACH_DETACH node on the next
   8512 		       iteration(s) of the loop.   */
   8513 		    if (addr_tokens.length () >= 4
   8514 			&& addr_tokens[0]->type == STRUCTURE_BASE
   8515 			&& addr_tokens[0]->u.structure_base_kind == BASE_DECL
   8516 			&& addr_tokens[1]->type == ACCESS_METHOD
   8517 			&& addr_tokens[2]->type == COMPONENT_SELECTOR
   8518 			&& addr_tokens[3]->type == ACCESS_METHOD
   8519 			&& (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
   8520 			    || (addr_tokens[3]->u.access_kind
   8521 				== ACCESS_INDEXED_ARRAY)))
   8522 		      {
   8523 			tree rt = addr_tokens[1]->expr;
   8524 
   8525 			gcc_assert (DECL_P (rt));
   8526 
   8527 			if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8528 			    && OMP_CLAUSE_MAP_IMPLICIT (c)
   8529 			    && (bitmap_bit_p (&map_head, DECL_UID (rt))
   8530 				|| bitmap_bit_p (&map_field_head, DECL_UID (rt))
   8531 				|| bitmap_bit_p (&map_firstprivate_head,
   8532 						 DECL_UID (rt))))
   8533 			  {
   8534 			    remove = true;
   8535 			    break;
   8536 			  }
   8537 			if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
   8538 			  break;
   8539 			if (bitmap_bit_p (&map_head, DECL_UID (rt)))
   8540 			  {
   8541 			    if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
   8542 			      error_at (OMP_CLAUSE_LOCATION (c),
   8543 					"%qD appears more than once in motion"
   8544 					" clauses", rt);
   8545 			    else if (openacc)
   8546 			      error_at (OMP_CLAUSE_LOCATION (c),
   8547 					"%qD appears more than once in data"
   8548 					" clauses", rt);
   8549 			    else
   8550 			      error_at (OMP_CLAUSE_LOCATION (c),
   8551 					"%qD appears more than once in map"
   8552 					" clauses", rt);
   8553 			    remove = true;
   8554 			  }
   8555 			else
   8556 			  {
   8557 			    bitmap_set_bit (&map_head, DECL_UID (rt));
   8558 			    bitmap_set_bit (&map_field_head, DECL_UID (rt));
   8559 			  }
   8560 		      }
   8561 		  }
   8562 		if (cp_oacc_check_attachments (c))
   8563 		  remove = true;
   8564 		if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8565 		    && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   8566 			|| OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
   8567 		    && !OMP_CLAUSE_SIZE (c))
   8568 		  /* In this case, we have a single array element which is a
   8569 		     pointer, and we already set OMP_CLAUSE_SIZE in
   8570 		     handle_omp_array_sections above.  For attach/detach
   8571 		     clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
   8572 		     to zero here.  */
   8573 		  OMP_CLAUSE_SIZE (c) = size_zero_node;
   8574 		break;
   8575 	      }
   8576 	    else if (type_dependent_expression_p (t))
   8577 	      break;
   8578 	    else if (!omp_parse_expr (addr_tokens, t))
   8579 	      {
   8580 		sorry_at (OMP_CLAUSE_LOCATION (c),
   8581 			  "unsupported map expression %qE",
   8582 			  OMP_CLAUSE_DECL (c));
   8583 		remove = true;
   8584 		break;
   8585 	      }
   8586 	    if (t == error_mark_node)
   8587 	      {
   8588 		remove = true;
   8589 		break;
   8590 	      }
   8591 	    /* OpenACC attach / detach clauses must be pointers.  */
   8592 	    if (cp_oacc_check_attachments (c))
   8593 	      {
   8594 		remove = true;
   8595 		break;
   8596 	      }
   8597 	    if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8598 		&& (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
   8599 		    || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
   8600 		&& !OMP_CLAUSE_SIZE (c))
   8601 	      /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
   8602 		 bias) to zero here, so it is not set erroneously to the
   8603 		 pointer size later on in gimplify.cc.  */
   8604 	      OMP_CLAUSE_SIZE (c) = size_zero_node;
   8605 
   8606 	    cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
   8607 
   8608 	    if (!ai.check_clause (c))
   8609 	      {
   8610 		remove = true;
   8611 		break;
   8612 	      }
   8613 
   8614 	    if (!ai.map_supported_p ())
   8615 	      {
   8616 		sorry_at (OMP_CLAUSE_LOCATION (c),
   8617 			  "unsupported map expression %qE",
   8618 			  OMP_CLAUSE_DECL (c));
   8619 		remove = true;
   8620 		break;
   8621 	      }
   8622 
   8623 	    gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
   8624 			 || addr_tokens[0]->type == STRUCTURE_BASE)
   8625 			&& addr_tokens[1]->type == ACCESS_METHOD);
   8626 
   8627 	    t = addr_tokens[1]->expr;
   8628 
   8629 	    /* This is used to prevent cxx_mark_addressable from being called
   8630 	       on 'this' for expressions like 'this->a', i.e. typical member
   8631 	       accesses.  */
   8632 	    indir_component_ref_p
   8633 	      = (addr_tokens[0]->type == STRUCTURE_BASE
   8634 		 && addr_tokens[1]->u.access_kind != ACCESS_DIRECT);
   8635 
   8636 	    if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
   8637 	      goto skip_decl_checks;
   8638 
   8639 	    /* For OpenMP, we can access a struct "t" and "t.d" on the same
   8640 	       mapping.  OpenACC allows multiple fields of the same structure
   8641 	       to be written.  */
   8642 	    if (addr_tokens[0]->type == STRUCTURE_BASE
   8643 		&& (bitmap_bit_p (&map_field_head, DECL_UID (t))
   8644 		    || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
   8645 	      goto skip_decl_checks;
   8646 
   8647 	    if (!processing_template_decl && TREE_CODE (t) == FIELD_DECL)
   8648 	      {
   8649 		OMP_CLAUSE_DECL (c)
   8650 		  = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
   8651 		break;
   8652 	      }
   8653 	    if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
   8654 	      {
   8655 		if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   8656 		  break;
   8657 		if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8658 		    && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
   8659 			|| OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
   8660 			|| OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH
   8661 			|| (!openacc && EXPR_P (t))))
   8662 		  break;
   8663 		if (DECL_P (t))
   8664 		  error_at (OMP_CLAUSE_LOCATION (c),
   8665 			    "%qD is not a variable in %qs clause", t,
   8666 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   8667 		else
   8668 		  error_at (OMP_CLAUSE_LOCATION (c),
   8669 			    "%qE is not a variable in %qs clause", t,
   8670 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   8671 		remove = true;
   8672 	      }
   8673 	    else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
   8674 	      {
   8675 		error_at (OMP_CLAUSE_LOCATION (c),
   8676 			  "%qD is threadprivate variable in %qs clause", t,
   8677 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   8678 		remove = true;
   8679 	      }
   8680 	    else if (!processing_template_decl
   8681 		     && !TYPE_REF_P (TREE_TYPE (t))
   8682 		     && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
   8683 			 || (OMP_CLAUSE_MAP_KIND (c)
   8684 			     != GOMP_MAP_FIRSTPRIVATE_POINTER))
   8685 		     && !indir_component_ref_p
   8686 		     && (t != current_class_ptr
   8687 			 || OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
   8688 			 || OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH)
   8689 		     && !cxx_mark_addressable (t))
   8690 	      remove = true;
   8691 	    else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8692 		       && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
   8693 			   || (OMP_CLAUSE_MAP_KIND (c)
   8694 			       == GOMP_MAP_FIRSTPRIVATE_POINTER)
   8695 			   || (OMP_CLAUSE_MAP_KIND (c)
   8696 			       == GOMP_MAP_ATTACH_DETACH)))
   8697 		     && t == OMP_CLAUSE_DECL (c)
   8698 		     && !type_dependent_expression_p (t)
   8699 		     && !omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
   8700 					    ? TREE_TYPE (TREE_TYPE (t))
   8701 					    : TREE_TYPE (t)))
   8702 	      {
   8703 		error_at (OMP_CLAUSE_LOCATION (c),
   8704 			  "%qD does not have a mappable type in %qs clause", t,
   8705 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   8706 		if (TREE_TYPE (t) != error_mark_node
   8707 		    && !COMPLETE_TYPE_P (TREE_TYPE (t)))
   8708 		  cxx_incomplete_type_inform (TREE_TYPE (t));
   8709 		remove = true;
   8710 	      }
   8711 	    else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8712 		     && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
   8713 		     && !type_dependent_expression_p (t)
   8714 		     && !INDIRECT_TYPE_P (TREE_TYPE (t)))
   8715 	      {
   8716 		error_at (OMP_CLAUSE_LOCATION (c),
   8717 			  "%qD is not a pointer variable", t);
   8718 		remove = true;
   8719 	      }
   8720 	    else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8721 		     && OMP_CLAUSE_MAP_IMPLICIT (c)
   8722 		     && (bitmap_bit_p (&map_head, DECL_UID (t))
   8723 			 || bitmap_bit_p (&map_field_head, DECL_UID (t))
   8724 			 || bitmap_bit_p (&map_firstprivate_head,
   8725 					  DECL_UID (t))))
   8726 	      remove = true;
   8727 	    else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8728 		     && (OMP_CLAUSE_MAP_KIND (c)
   8729 			 == GOMP_MAP_FIRSTPRIVATE_POINTER))
   8730 	      {
   8731 		if (bitmap_bit_p (&generic_head, DECL_UID (t))
   8732 		    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
   8733 		    || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
   8734 		  {
   8735 		    error_at (OMP_CLAUSE_LOCATION (c),
   8736 			      "%qD appears more than once in data clauses", t);
   8737 		    remove = true;
   8738 		  }
   8739 		else if (bitmap_bit_p (&map_head, DECL_UID (t))
   8740 			 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
   8741 			 && openacc)
   8742 		  {
   8743 		    error_at (OMP_CLAUSE_LOCATION (c),
   8744 			      "%qD appears more than once in data clauses", t);
   8745 		    remove = true;
   8746 		  }
   8747 		else
   8748 		  bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
   8749 	      }
   8750 	    else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
   8751 		     && (OMP_CLAUSE_MAP_KIND (c)
   8752 			 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
   8753 	      bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
   8754 	    else if (bitmap_bit_p (&map_head, DECL_UID (t))
   8755 		     && !bitmap_bit_p (&map_field_head, DECL_UID (t))
   8756 		     && ort != C_ORT_OMP
   8757 		     && ort != C_ORT_OMP_EXIT_DATA)
   8758 	      {
   8759 		if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
   8760 		  error_at (OMP_CLAUSE_LOCATION (c),
   8761 			    "%qD appears more than once in motion clauses", t);
   8762 		else if (openacc)
   8763 		  error_at (OMP_CLAUSE_LOCATION (c),
   8764 			    "%qD appears more than once in data clauses", t);
   8765 		else
   8766 		  error_at (OMP_CLAUSE_LOCATION (c),
   8767 			    "%qD appears more than once in map clauses", t);
   8768 		remove = true;
   8769 	      }
   8770 	    else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
   8771 	      {
   8772 		error_at (OMP_CLAUSE_LOCATION (c),
   8773 			  "%qD appears more than once in data clauses", t);
   8774 		remove = true;
   8775 	      }
   8776 	    else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
   8777 		     || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
   8778 	      {
   8779 		if (openacc)
   8780 		  error_at (OMP_CLAUSE_LOCATION (c),
   8781 			    "%qD appears more than once in data clauses", t);
   8782 		else
   8783 		  error_at (OMP_CLAUSE_LOCATION (c),
   8784 			    "%qD appears both in data and map clauses", t);
   8785 		remove = true;
   8786 	      }
   8787 	    else if (!omp_access_chain_p (addr_tokens, 1))
   8788 	      {
   8789 		bitmap_set_bit (&map_head, DECL_UID (t));
   8790 
   8791 		tree decl = OMP_CLAUSE_DECL (c);
   8792 		if (t != decl
   8793 		    && (TREE_CODE (decl) == COMPONENT_REF
   8794 			|| (INDIRECT_REF_P (decl)
   8795 			    && (TREE_CODE (TREE_OPERAND (decl, 0))
   8796 				== COMPONENT_REF)
   8797 			    && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (decl,
   8798 								    0))))))
   8799 		  bitmap_set_bit (&map_field_head, DECL_UID (t));
   8800 	      }
   8801 
   8802 	  skip_decl_checks:
   8803 	    /* If we call ai.expand_map_clause in handle_omp_array_sections,
   8804 	       the containing loop (here) iterates through the new nodes
   8805 	       created by that expansion.  Avoid expanding those again (just
   8806 	       by checking the node type).  */
   8807 	    if (!remove
   8808 		&& !processing_template_decl
   8809 		&& ort != C_ORT_DECLARE_SIMD
   8810 		&& (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
   8811 		    || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
   8812 			&& (OMP_CLAUSE_MAP_KIND (c)
   8813 			    != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
   8814 			&& OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
   8815 			&& OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
   8816 			&& OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
   8817 			&& OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
   8818 	      {
   8819 		grp_start_p = pc;
   8820 		grp_sentinel = OMP_CLAUSE_CHAIN (c);
   8821 		tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
   8822 						addr_tokens, ort);
   8823 		if (nc != error_mark_node)
   8824 		  c = nc;
   8825 	      }
   8826 	  }
   8827 	  break;
   8828 
   8829 	case OMP_CLAUSE_ENTER:
   8830 	case OMP_CLAUSE_LINK:
   8831 	  t = OMP_CLAUSE_DECL (c);
   8832 	  const char *cname;
   8833 	  cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
   8834 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
   8835 	      && OMP_CLAUSE_ENTER_TO (c))
   8836 	    cname = "to";
   8837 	  if (TREE_CODE (t) == FUNCTION_DECL
   8838 	      && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
   8839 	    ;
   8840 	  else if (!VAR_P (t))
   8841 	    {
   8842 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
   8843 		{
   8844 		  if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
   8845 		    error_at (OMP_CLAUSE_LOCATION (c),
   8846 			      "template %qE in clause %qs", t, cname);
   8847 		  else if (really_overloaded_fn (t))
   8848 		    error_at (OMP_CLAUSE_LOCATION (c),
   8849 			      "overloaded function name %qE in clause %qs", t,
   8850 			      cname);
   8851 		  else
   8852 		    error_at (OMP_CLAUSE_LOCATION (c),
   8853 			      "%qE is neither a variable nor a function name "
   8854 			      "in clause %qs", t, cname);
   8855 		}
   8856 	      else
   8857 		error_at (OMP_CLAUSE_LOCATION (c),
   8858 			  "%qE is not a variable in clause %qs", t, cname);
   8859 	      remove = true;
   8860 	    }
   8861 	  else if (DECL_THREAD_LOCAL_P (t))
   8862 	    {
   8863 	      error_at (OMP_CLAUSE_LOCATION (c),
   8864 			"%qD is threadprivate variable in %qs clause", t,
   8865 			cname);
   8866 	      remove = true;
   8867 	    }
   8868 	  else if (!omp_mappable_type (TREE_TYPE (t)))
   8869 	    {
   8870 	      error_at (OMP_CLAUSE_LOCATION (c),
   8871 			"%qD does not have a mappable type in %qs clause", t,
   8872 			cname);
   8873 	      if (TREE_TYPE (t) != error_mark_node
   8874 		  && !COMPLETE_TYPE_P (TREE_TYPE (t)))
   8875 		cxx_incomplete_type_inform (TREE_TYPE (t));
   8876 	      remove = true;
   8877 	    }
   8878 	  if (remove)
   8879 	    break;
   8880 	  if (bitmap_bit_p (&generic_head, DECL_UID (t)))
   8881 	    {
   8882 	      error_at (OMP_CLAUSE_LOCATION (c),
   8883 			"%qE appears more than once on the same "
   8884 			"%<declare target%> directive", t);
   8885 	      remove = true;
   8886 	    }
   8887 	  else
   8888 	    bitmap_set_bit (&generic_head, DECL_UID (t));
   8889 	  break;
   8890 
   8891 	case OMP_CLAUSE_UNIFORM:
   8892 	  t = OMP_CLAUSE_DECL (c);
   8893 	  if (TREE_CODE (t) != PARM_DECL)
   8894 	    {
   8895 	      if (processing_template_decl)
   8896 		break;
   8897 	      if (DECL_P (t))
   8898 		error_at (OMP_CLAUSE_LOCATION (c),
   8899 			  "%qD is not an argument in %<uniform%> clause", t);
   8900 	      else
   8901 		error_at (OMP_CLAUSE_LOCATION (c),
   8902 			  "%qE is not an argument in %<uniform%> clause", t);
   8903 	      remove = true;
   8904 	      break;
   8905 	    }
   8906 	  /* map_head bitmap is used as uniform_head if declare_simd.  */
   8907 	  bitmap_set_bit (&map_head, DECL_UID (t));
   8908 	  goto check_dup_generic;
   8909 
   8910 	case OMP_CLAUSE_GRAINSIZE:
   8911 	  t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
   8912 	  if (t == error_mark_node)
   8913 	    remove = true;
   8914 	  else if (!type_dependent_expression_p (t)
   8915 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   8916 	    {
   8917 	      error_at (OMP_CLAUSE_LOCATION (c),
   8918 			"%<grainsize%> expression must be integral");
   8919 	      remove = true;
   8920 	    }
   8921 	  else
   8922 	    {
   8923 	      t = mark_rvalue_use (t);
   8924 	      if (!processing_template_decl)
   8925 		{
   8926 		  t = maybe_constant_value (t);
   8927 		  if (TREE_CODE (t) == INTEGER_CST
   8928 		      && tree_int_cst_sgn (t) != 1)
   8929 		    {
   8930 		      warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   8931 				  "%<grainsize%> value must be positive");
   8932 		      t = integer_one_node;
   8933 		    }
   8934 		  t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   8935 		}
   8936 	      OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
   8937 	    }
   8938 	  break;
   8939 
   8940 	case OMP_CLAUSE_PRIORITY:
   8941 	  t = OMP_CLAUSE_PRIORITY_EXPR (c);
   8942 	  if (t == error_mark_node)
   8943 	    remove = true;
   8944 	  else if (!type_dependent_expression_p (t)
   8945 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   8946 	    {
   8947 	      error_at (OMP_CLAUSE_LOCATION (c),
   8948 			"%<priority%> expression must be integral");
   8949 	      remove = true;
   8950 	    }
   8951 	  else
   8952 	    {
   8953 	      t = mark_rvalue_use (t);
   8954 	      if (!processing_template_decl)
   8955 		{
   8956 		  t = maybe_constant_value (t);
   8957 		  if (TREE_CODE (t) == INTEGER_CST
   8958 		      && tree_int_cst_sgn (t) == -1)
   8959 		    {
   8960 		      warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
   8961 				  "%<priority%> value must be non-negative");
   8962 		      t = integer_one_node;
   8963 		    }
   8964 		  t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   8965 		}
   8966 	      OMP_CLAUSE_PRIORITY_EXPR (c) = t;
   8967 	    }
   8968 	  break;
   8969 
   8970 	case OMP_CLAUSE_HINT:
   8971 	  t = OMP_CLAUSE_HINT_EXPR (c);
   8972 	  if (t == error_mark_node)
   8973 	    remove = true;
   8974 	  else if (!type_dependent_expression_p (t)
   8975 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   8976 	    {
   8977 	      error_at (OMP_CLAUSE_LOCATION (c),
   8978 			"%<hint%> expression must be integral");
   8979 	      remove = true;
   8980 	    }
   8981 	  else
   8982 	    {
   8983 	      t = mark_rvalue_use (t);
   8984 	      if (!processing_template_decl)
   8985 		{
   8986 		  t = maybe_constant_value (t);
   8987 		  t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   8988 		  if (TREE_CODE (t) != INTEGER_CST)
   8989 		    {
   8990 		      error_at (OMP_CLAUSE_LOCATION (c),
   8991 				"%<hint%> expression must be constant integer "
   8992 				"expression");
   8993 		      remove = true;
   8994 		    }
   8995 		}
   8996 	      OMP_CLAUSE_HINT_EXPR (c) = t;
   8997 	    }
   8998 	  break;
   8999 
   9000 	case OMP_CLAUSE_FILTER:
   9001 	  t = OMP_CLAUSE_FILTER_EXPR (c);
   9002 	  if (t == error_mark_node)
   9003 	    remove = true;
   9004 	  else if (!type_dependent_expression_p (t)
   9005 		   && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   9006 	    {
   9007 	      error_at (OMP_CLAUSE_LOCATION (c),
   9008 			"%<filter%> expression must be integral");
   9009 	      remove = true;
   9010 	    }
   9011 	  else
   9012 	    {
   9013 	      t = mark_rvalue_use (t);
   9014 	      if (!processing_template_decl)
   9015 		{
   9016 		  t = maybe_constant_value (t);
   9017 		  t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   9018 		}
   9019 	      OMP_CLAUSE_FILTER_EXPR (c) = t;
   9020 	    }
   9021 	  break;
   9022 
   9023 	case OMP_CLAUSE_IS_DEVICE_PTR:
   9024 	case OMP_CLAUSE_USE_DEVICE_PTR:
   9025 	  field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
   9026 	  t = OMP_CLAUSE_DECL (c);
   9027 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
   9028 	    bitmap_set_bit (&is_on_device_head, DECL_UID (t));
   9029 	  if (!type_dependent_expression_p (t))
   9030 	    {
   9031 	      tree type = TREE_TYPE (t);
   9032 	      if (!TYPE_PTR_P (type)
   9033 		  && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
   9034 		{
   9035 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
   9036 		      && ort == C_ORT_OMP)
   9037 		    {
   9038 		      error_at (OMP_CLAUSE_LOCATION (c),
   9039 				"%qs variable is neither a pointer "
   9040 				"nor reference to pointer",
   9041 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   9042 		      remove = true;
   9043 		    }
   9044 		  else if (TREE_CODE (type) != ARRAY_TYPE
   9045 			   && (!TYPE_REF_P (type)
   9046 			       || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
   9047 		    {
   9048 		      error_at (OMP_CLAUSE_LOCATION (c),
   9049 				"%qs variable is neither a pointer, nor an "
   9050 				"array nor reference to pointer or array",
   9051 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   9052 		      remove = true;
   9053 		    }
   9054 		}
   9055 	    }
   9056 	  goto check_dup_generic;
   9057 
   9058 	case OMP_CLAUSE_HAS_DEVICE_ADDR:
   9059 	  t = OMP_CLAUSE_DECL (c);
   9060 	  if (TREE_CODE (t) == OMP_ARRAY_SECTION)
   9061 	    {
   9062 	      if (handle_omp_array_sections (c, ort))
   9063 		remove = true;
   9064 	      else
   9065 		{
   9066 		  t = OMP_CLAUSE_DECL (c);
   9067 		  while (TREE_CODE (t) == OMP_ARRAY_SECTION)
   9068 		    t = TREE_OPERAND (t, 0);
   9069 		  while (INDIRECT_REF_P (t)
   9070 			 || TREE_CODE (t) == ARRAY_REF)
   9071 		    t = TREE_OPERAND (t, 0);
   9072 		}
   9073 	    }
   9074 	  if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
   9075 	    {
   9076 	      bitmap_set_bit (&is_on_device_head, DECL_UID (t));
   9077 	      if (!processing_template_decl
   9078 		  && !cxx_mark_addressable (t))
   9079 		remove = true;
   9080 	    }
   9081 	  goto check_dup_generic_t;
   9082 
   9083 	case OMP_CLAUSE_USE_DEVICE_ADDR:
   9084 	  field_ok = true;
   9085 	  t = OMP_CLAUSE_DECL (c);
   9086 	  if (!processing_template_decl
   9087 	      && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
   9088 	      && !TYPE_REF_P (TREE_TYPE (t))
   9089 	      && !cxx_mark_addressable (t))
   9090 	    remove = true;
   9091 	  goto check_dup_generic;
   9092 
   9093 	case OMP_CLAUSE_NOWAIT:
   9094 	case OMP_CLAUSE_DEFAULT:
   9095 	case OMP_CLAUSE_UNTIED:
   9096 	case OMP_CLAUSE_COLLAPSE:
   9097 	case OMP_CLAUSE_PARALLEL:
   9098 	case OMP_CLAUSE_FOR:
   9099 	case OMP_CLAUSE_SECTIONS:
   9100 	case OMP_CLAUSE_TASKGROUP:
   9101 	case OMP_CLAUSE_PROC_BIND:
   9102 	case OMP_CLAUSE_DEVICE_TYPE:
   9103 	case OMP_CLAUSE_NOGROUP:
   9104 	case OMP_CLAUSE_THREADS:
   9105 	case OMP_CLAUSE_SIMD:
   9106 	case OMP_CLAUSE_DEFAULTMAP:
   9107 	case OMP_CLAUSE_BIND:
   9108 	case OMP_CLAUSE_AUTO:
   9109 	case OMP_CLAUSE_INDEPENDENT:
   9110 	case OMP_CLAUSE_SEQ:
   9111 	case OMP_CLAUSE_IF_PRESENT:
   9112 	case OMP_CLAUSE_FINALIZE:
   9113 	case OMP_CLAUSE_NOHOST:
   9114 	case OMP_CLAUSE_INDIRECT:
   9115 	  break;
   9116 
   9117 	case OMP_CLAUSE_MERGEABLE:
   9118 	  mergeable_seen = true;
   9119 	  break;
   9120 
   9121 	case OMP_CLAUSE_TILE:
   9122 	  for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
   9123 	       list = TREE_CHAIN (list))
   9124 	    {
   9125 	      t = TREE_VALUE (list);
   9126 
   9127 	      if (t == error_mark_node)
   9128 		remove = true;
   9129 	      else if (!type_dependent_expression_p (t)
   9130 		       && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
   9131 		{
   9132 		  error_at (OMP_CLAUSE_LOCATION (c),
   9133 			    "%<tile%> argument needs integral type");
   9134 		  remove = true;
   9135 		}
   9136 	      else
   9137 		{
   9138 		  t = mark_rvalue_use (t);
   9139 		  if (!processing_template_decl)
   9140 		    {
   9141 		      /* Zero is used to indicate '*', we permit you
   9142 			 to get there via an ICE of value zero.  */
   9143 		      t = maybe_constant_value (t);
   9144 		      if (!tree_fits_shwi_p (t)
   9145 			  || tree_to_shwi (t) < 0)
   9146 			{
   9147 			  error_at (OMP_CLAUSE_LOCATION (c),
   9148 				    "%<tile%> argument needs positive "
   9149 				    "integral constant");
   9150 			  remove = true;
   9151 			}
   9152 		      t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   9153 		    }
   9154 		}
   9155 
   9156 		/* Update list item.  */
   9157 	      TREE_VALUE (list) = t;
   9158 	    }
   9159 	  break;
   9160 
   9161 	case OMP_CLAUSE_ORDERED:
   9162 	  ordered_seen = true;
   9163 	  break;
   9164 
   9165 	case OMP_CLAUSE_ORDER:
   9166 	  if (order_seen)
   9167 	    remove = true;
   9168 	  else
   9169 	    order_seen = true;
   9170 	  break;
   9171 
   9172 	case OMP_CLAUSE_INBRANCH:
   9173 	case OMP_CLAUSE_NOTINBRANCH:
   9174 	  if (branch_seen)
   9175 	    {
   9176 	      error_at (OMP_CLAUSE_LOCATION (c),
   9177 			"%<inbranch%> clause is incompatible with "
   9178 			"%<notinbranch%>");
   9179 	      remove = true;
   9180 	    }
   9181 	  branch_seen = true;
   9182 	  break;
   9183 
   9184 	case OMP_CLAUSE_INCLUSIVE:
   9185 	case OMP_CLAUSE_EXCLUSIVE:
   9186 	  t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
   9187 	  if (!t)
   9188 	    t = OMP_CLAUSE_DECL (c);
   9189 	  if (t == current_class_ptr)
   9190 	    {
   9191 	      error_at (OMP_CLAUSE_LOCATION (c),
   9192 			"%<this%> allowed in OpenMP only in %<declare simd%>"
   9193 			" clauses");
   9194 	      remove = true;
   9195 	      break;
   9196 	    }
   9197 	  if (!VAR_P (t)
   9198 	      && TREE_CODE (t) != PARM_DECL
   9199 	      && TREE_CODE (t) != FIELD_DECL)
   9200 	    {
   9201 	      if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
   9202 		break;
   9203 	      if (DECL_P (t))
   9204 		error_at (OMP_CLAUSE_LOCATION (c),
   9205 			  "%qD is not a variable in clause %qs", t,
   9206 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   9207 	      else
   9208 		error_at (OMP_CLAUSE_LOCATION (c),
   9209 			  "%qE is not a variable in clause %qs", t,
   9210 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   9211 	      remove = true;
   9212 	    }
   9213 	  break;
   9214 
   9215 	default:
   9216 	  gcc_unreachable ();
   9217 	}
   9218 
   9219       if (remove)
   9220 	{
   9221 	  if (grp_start_p)
   9222 	    {
   9223 	      /* If we found a clause to remove, we want to remove the whole
   9224 		 expanded group, otherwise gimplify
   9225 		 (omp_resolve_clause_dependencies) can get confused.  */
   9226 	      *grp_start_p = grp_sentinel;
   9227 	      pc = grp_start_p;
   9228 	      grp_start_p = NULL;
   9229 	    }
   9230 	  else
   9231 	    *pc = OMP_CLAUSE_CHAIN (c);
   9232 	}
   9233       else
   9234 	pc = &OMP_CLAUSE_CHAIN (c);
   9235     }
   9236 
   9237   if (reduction_seen < 0 && (ordered_seen || schedule_seen))
   9238     reduction_seen = -2;
   9239 
   9240   for (pc = &clauses, c = clauses; c ; c = *pc)
   9241     {
   9242       enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
   9243       bool remove = false;
   9244       bool need_complete_type = false;
   9245       bool need_default_ctor = false;
   9246       bool need_copy_ctor = false;
   9247       bool need_copy_assignment = false;
   9248       bool need_implicitly_determined = false;
   9249       bool need_dtor = false;
   9250       tree type, inner_type;
   9251 
   9252       switch (c_kind)
   9253 	{
   9254 	case OMP_CLAUSE_SHARED:
   9255 	  need_implicitly_determined = true;
   9256 	  break;
   9257 	case OMP_CLAUSE_PRIVATE:
   9258 	  need_complete_type = true;
   9259 	  need_default_ctor = true;
   9260 	  need_dtor = true;
   9261 	  need_implicitly_determined = true;
   9262 	  break;
   9263 	case OMP_CLAUSE_FIRSTPRIVATE:
   9264 	  need_complete_type = true;
   9265 	  need_copy_ctor = true;
   9266 	  need_dtor = true;
   9267 	  need_implicitly_determined = true;
   9268 	  break;
   9269 	case OMP_CLAUSE_LASTPRIVATE:
   9270 	  need_complete_type = true;
   9271 	  need_copy_assignment = true;
   9272 	  need_implicitly_determined = true;
   9273 	  break;
   9274 	case OMP_CLAUSE_REDUCTION:
   9275 	  if (reduction_seen == -2)
   9276 	    OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
   9277 	  if (OMP_CLAUSE_REDUCTION_INSCAN (c))
   9278 	    need_copy_assignment = true;
   9279 	  need_implicitly_determined = true;
   9280 	  break;
   9281 	case OMP_CLAUSE_IN_REDUCTION:
   9282 	case OMP_CLAUSE_TASK_REDUCTION:
   9283 	case OMP_CLAUSE_INCLUSIVE:
   9284 	case OMP_CLAUSE_EXCLUSIVE:
   9285 	  need_implicitly_determined = true;
   9286 	  break;
   9287 	case OMP_CLAUSE_LINEAR:
   9288 	  if (ort != C_ORT_OMP_DECLARE_SIMD)
   9289 	    need_implicitly_determined = true;
   9290 	  else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
   9291 		   && !bitmap_bit_p (&map_head,
   9292 				     DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
   9293 	    {
   9294 	      error_at (OMP_CLAUSE_LOCATION (c),
   9295 			"%<linear%> clause step is a parameter %qD not "
   9296 			"specified in %<uniform%> clause",
   9297 			OMP_CLAUSE_LINEAR_STEP (c));
   9298 	      *pc = OMP_CLAUSE_CHAIN (c);
   9299 	      continue;
   9300 	    }
   9301 	  break;
   9302 	case OMP_CLAUSE_COPYPRIVATE:
   9303 	  need_copy_assignment = true;
   9304 	  break;
   9305 	case OMP_CLAUSE_COPYIN:
   9306 	  need_copy_assignment = true;
   9307 	  break;
   9308 	case OMP_CLAUSE_SIMDLEN:
   9309 	  if (safelen
   9310 	      && !processing_template_decl
   9311 	      && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
   9312 				  OMP_CLAUSE_SIMDLEN_EXPR (c)))
   9313 	    {
   9314 	      error_at (OMP_CLAUSE_LOCATION (c),
   9315 			"%<simdlen%> clause value is bigger than "
   9316 			"%<safelen%> clause value");
   9317 	      OMP_CLAUSE_SIMDLEN_EXPR (c)
   9318 		= OMP_CLAUSE_SAFELEN_EXPR (safelen);
   9319 	    }
   9320 	  pc = &OMP_CLAUSE_CHAIN (c);
   9321 	  continue;
   9322 	case OMP_CLAUSE_SCHEDULE:
   9323 	  if (ordered_seen
   9324 	      && (OMP_CLAUSE_SCHEDULE_KIND (c)
   9325 		  & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
   9326 	    {
   9327 	      error_at (OMP_CLAUSE_LOCATION (c),
   9328 			"%<nonmonotonic%> schedule modifier specified "
   9329 			"together with %<ordered%> clause");
   9330 	      OMP_CLAUSE_SCHEDULE_KIND (c)
   9331 		= (enum omp_clause_schedule_kind)
   9332 		  (OMP_CLAUSE_SCHEDULE_KIND (c)
   9333 		   & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
   9334 	    }
   9335 	  if (reduction_seen == -2)
   9336 	    error_at (OMP_CLAUSE_LOCATION (c),
   9337 		      "%qs clause specified together with %<inscan%> "
   9338 		      "%<reduction%> clause", "schedule");
   9339 	  pc = &OMP_CLAUSE_CHAIN (c);
   9340 	  continue;
   9341 	case OMP_CLAUSE_NOGROUP:
   9342 	  if (reduction_seen)
   9343 	    {
   9344 	      error_at (OMP_CLAUSE_LOCATION (c),
   9345 			"%<nogroup%> clause must not be used together with "
   9346 			"%<reduction%> clause");
   9347 	      *pc = OMP_CLAUSE_CHAIN (c);
   9348 	      continue;
   9349 	    }
   9350 	  pc = &OMP_CLAUSE_CHAIN (c);
   9351 	  continue;
   9352 	case OMP_CLAUSE_ORDERED:
   9353 	  if (reduction_seen == -2)
   9354 	    error_at (OMP_CLAUSE_LOCATION (c),
   9355 		      "%qs clause specified together with %<inscan%> "
   9356 		      "%<reduction%> clause", "ordered");
   9357 	  pc = &OMP_CLAUSE_CHAIN (c);
   9358 	  continue;
   9359 	case OMP_CLAUSE_ORDER:
   9360 	  if (ordered_seen)
   9361 	    {
   9362 	      error_at (OMP_CLAUSE_LOCATION (c),
   9363 			"%<order%> clause must not be used together "
   9364 			"with %<ordered%>");
   9365 	      *pc = OMP_CLAUSE_CHAIN (c);
   9366 	      continue;
   9367 	    }
   9368 	  pc = &OMP_CLAUSE_CHAIN (c);
   9369 	  continue;
   9370 	case OMP_CLAUSE_DETACH:
   9371 	  if (mergeable_seen)
   9372 	    {
   9373 	      error_at (OMP_CLAUSE_LOCATION (c),
   9374 			"%<detach%> clause must not be used together with "
   9375 			"%<mergeable%> clause");
   9376 	      *pc = OMP_CLAUSE_CHAIN (c);
   9377 	      continue;
   9378 	    }
   9379 	  pc = &OMP_CLAUSE_CHAIN (c);
   9380 	  continue;
   9381 	case OMP_CLAUSE_MAP:
   9382 	  if (target_in_reduction_seen && !processing_template_decl)
   9383 	    {
   9384 	      t = OMP_CLAUSE_DECL (c);
   9385 	      while (handled_component_p (t)
   9386 		     || INDIRECT_REF_P (t)
   9387 		     || TREE_CODE (t) == ADDR_EXPR
   9388 		     || TREE_CODE (t) == MEM_REF
   9389 		     || TREE_CODE (t) == NON_LVALUE_EXPR)
   9390 		t = TREE_OPERAND (t, 0);
   9391 	      if (DECL_P (t)
   9392 		  && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
   9393 		OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
   9394 	    }
   9395 	  pc = &OMP_CLAUSE_CHAIN (c);
   9396 	  continue;
   9397 	case OMP_CLAUSE_NOWAIT:
   9398 	  if (copyprivate_seen)
   9399 	    {
   9400 	      error_at (OMP_CLAUSE_LOCATION (c),
   9401 			"%<nowait%> clause must not be used together "
   9402 			"with %<copyprivate%>");
   9403 	      *pc = OMP_CLAUSE_CHAIN (c);
   9404 	      continue;
   9405 	    }
   9406 	  /* FALLTHRU */
   9407 	default:
   9408 	  pc = &OMP_CLAUSE_CHAIN (c);
   9409 	  continue;
   9410 	}
   9411 
   9412       t = OMP_CLAUSE_DECL (c);
   9413       switch (c_kind)
   9414 	{
   9415 	case OMP_CLAUSE_LASTPRIVATE:
   9416 	  if (DECL_P (t)
   9417 	      && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
   9418 	    {
   9419 	      need_default_ctor = true;
   9420 	      need_dtor = true;
   9421 	    }
   9422 	  break;
   9423 
   9424 	case OMP_CLAUSE_REDUCTION:
   9425 	case OMP_CLAUSE_IN_REDUCTION:
   9426 	case OMP_CLAUSE_TASK_REDUCTION:
   9427 	  if (allocate_seen)
   9428 	    {
   9429 	      if (TREE_CODE (t) == MEM_REF)
   9430 		{
   9431 		  t = TREE_OPERAND (t, 0);
   9432 		  if (TREE_CODE (t) == POINTER_PLUS_EXPR)
   9433 		    t = TREE_OPERAND (t, 0);
   9434 		  if (TREE_CODE (t) == ADDR_EXPR
   9435 		      || INDIRECT_REF_P (t))
   9436 		    t = TREE_OPERAND (t, 0);
   9437 		  if (DECL_P (t))
   9438 		    bitmap_clear_bit (&aligned_head, DECL_UID (t));
   9439 		}
   9440 	      else if (TREE_CODE (t) == OMP_ARRAY_SECTION)
   9441 		{
   9442 		  while (TREE_CODE (t) == OMP_ARRAY_SECTION)
   9443 		    t = TREE_OPERAND (t, 0);
   9444 		  if (DECL_P (t))
   9445 		    bitmap_clear_bit (&aligned_head, DECL_UID (t));
   9446 		  t = OMP_CLAUSE_DECL (c);
   9447 		}
   9448 	      else if (DECL_P (t))
   9449 		bitmap_clear_bit (&aligned_head, DECL_UID (t));
   9450 	      t = OMP_CLAUSE_DECL (c);
   9451 	    }
   9452 	  if (processing_template_decl
   9453 	      && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
   9454 	    break;
   9455 	  if (finish_omp_reduction_clause (c, &need_default_ctor,
   9456 					   &need_dtor))
   9457 	    remove = true;
   9458 	  else
   9459 	    t = OMP_CLAUSE_DECL (c);
   9460 	  break;
   9461 
   9462 	case OMP_CLAUSE_COPYIN:
   9463 	  if (processing_template_decl
   9464 	      && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
   9465 	    break;
   9466 	  if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
   9467 	    {
   9468 	      error_at (OMP_CLAUSE_LOCATION (c),
   9469 			"%qE must be %<threadprivate%> for %<copyin%>", t);
   9470 	      remove = true;
   9471 	    }
   9472 	  break;
   9473 
   9474 	default:
   9475 	  break;
   9476 	}
   9477 
   9478       if (processing_template_decl
   9479 	  && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
   9480 	{
   9481 	  pc = &OMP_CLAUSE_CHAIN (c);
   9482 	  continue;
   9483 	}
   9484 
   9485       if (need_complete_type || need_copy_assignment)
   9486 	{
   9487 	  t = require_complete_type (t);
   9488 	  if (t == error_mark_node)
   9489 	    remove = true;
   9490 	  else if (!processing_template_decl
   9491 		   && TYPE_REF_P (TREE_TYPE (t))
   9492 		   && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
   9493 	    remove = true;
   9494 	}
   9495       if (need_implicitly_determined)
   9496 	{
   9497 	  const char *share_name = NULL;
   9498 
   9499 	  if (allocate_seen
   9500 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
   9501 	      && DECL_P (t))
   9502 	    bitmap_clear_bit (&aligned_head, DECL_UID (t));
   9503 
   9504 	  if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
   9505 	    share_name = "threadprivate";
   9506 	  else switch (cxx_omp_predetermined_sharing_1 (t))
   9507 	    {
   9508 	    case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
   9509 	      break;
   9510 	    case OMP_CLAUSE_DEFAULT_SHARED:
   9511 	      if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
   9512 		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
   9513 		  && c_omp_predefined_variable (t))
   9514 		/* The __func__ variable and similar function-local predefined
   9515 		   variables may be listed in a shared or firstprivate
   9516 		   clause.  */
   9517 		break;
   9518 	      if (VAR_P (t)
   9519 		  && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
   9520 		  && TREE_STATIC (t)
   9521 		  && cxx_omp_const_qual_no_mutable (t))
   9522 		{
   9523 		  tree ctx = CP_DECL_CONTEXT (t);
   9524 		  /* const qualified static data members without mutable
   9525 		     member may be specified in firstprivate clause.  */
   9526 		  if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
   9527 		    break;
   9528 		}
   9529 	      share_name = "shared";
   9530 	      break;
   9531 	    case OMP_CLAUSE_DEFAULT_PRIVATE:
   9532 	      share_name = "private";
   9533 	      break;
   9534 	    default:
   9535 	      gcc_unreachable ();
   9536 	    }
   9537 	  if (share_name)
   9538 	    {
   9539 	      error_at (OMP_CLAUSE_LOCATION (c),
   9540 			"%qE is predetermined %qs for %qs",
   9541 			omp_clause_printable_decl (t), share_name,
   9542 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
   9543 	      remove = true;
   9544 	    }
   9545 	  else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
   9546 		   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
   9547 		   && cxx_omp_const_qual_no_mutable (t))
   9548 	    {
   9549 	      error_at (OMP_CLAUSE_LOCATION (c),
   9550 			"%<const%> qualified %qE without %<mutable%> member "
   9551 			"may appear only in %<shared%> or %<firstprivate%> "
   9552 			"clauses", omp_clause_printable_decl (t));
   9553 	      remove = true;
   9554 	    }
   9555 	}
   9556 
   9557       if (detach_seen
   9558 	  && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
   9559 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
   9560 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
   9561 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
   9562 	  && OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (detach_seen))
   9563 	{
   9564 	  error_at (OMP_CLAUSE_LOCATION (c),
   9565 		    "the event handle of a %<detach%> clause "
   9566 		    "should not be in a data-sharing clause");
   9567 	  remove = true;
   9568 	}
   9569 
   9570       /* We're interested in the base element, not arrays.  */
   9571       inner_type = type = TREE_TYPE (t);
   9572       if ((need_complete_type
   9573 	   || need_copy_assignment
   9574 	   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
   9575 	   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
   9576 	   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
   9577 	  && TYPE_REF_P (inner_type))
   9578 	inner_type = TREE_TYPE (inner_type);
   9579       while (TREE_CODE (inner_type) == ARRAY_TYPE)
   9580 	inner_type = TREE_TYPE (inner_type);
   9581 
   9582       /* Check for special function availability by building a call to one.
   9583 	 Save the results, because later we won't be in the right context
   9584 	 for making these queries.  */
   9585       if (CLASS_TYPE_P (inner_type)
   9586 	  && COMPLETE_TYPE_P (inner_type)
   9587 	  && (need_default_ctor || need_copy_ctor
   9588 	      || need_copy_assignment || need_dtor)
   9589 	  && !type_dependent_expression_p (t)
   9590 	  && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
   9591 					 need_copy_ctor, need_copy_assignment,
   9592 					 need_dtor))
   9593 	remove = true;
   9594 
   9595       if (!remove
   9596 	  && c_kind == OMP_CLAUSE_SHARED
   9597 	  && processing_template_decl)
   9598 	{
   9599 	  t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
   9600 	  if (t)
   9601 	    OMP_CLAUSE_DECL (c) = t;
   9602 	}
   9603 
   9604       if (remove)
   9605 	*pc = OMP_CLAUSE_CHAIN (c);
   9606       else
   9607 	pc = &OMP_CLAUSE_CHAIN (c);
   9608     }
   9609 
   9610   if (allocate_seen)
   9611     for (pc = &clauses, c = clauses; c ; c = *pc)
   9612       {
   9613 	bool remove = false;
   9614 	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
   9615 	    && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
   9616 	    && DECL_P (OMP_CLAUSE_DECL (c))
   9617 	    && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
   9618 	  {
   9619 	    error_at (OMP_CLAUSE_LOCATION (c),
   9620 		      "%qD specified in %<allocate%> clause but not in "
   9621 		      "an explicit privatization clause", OMP_CLAUSE_DECL (c));
   9622 	    remove = true;
   9623 	  }
   9624 	if (remove)
   9625 	  *pc = OMP_CLAUSE_CHAIN (c);
   9626 	else
   9627 	  pc = &OMP_CLAUSE_CHAIN (c);
   9628       }
   9629 
   9630   bitmap_obstack_release (NULL);
   9631   return clauses;
   9632 }
   9633 
   9634 /* Start processing OpenMP clauses that can include any
   9635    privatization clauses for non-static data members.  */
   9636 
   9637 tree
   9638 push_omp_privatization_clauses (bool ignore_next)
   9639 {
   9640   if (omp_private_member_ignore_next)
   9641     {
   9642       omp_private_member_ignore_next = ignore_next;
   9643       return NULL_TREE;
   9644     }
   9645   omp_private_member_ignore_next = ignore_next;
   9646   if (omp_private_member_map)
   9647     omp_private_member_vec.safe_push (error_mark_node);
   9648   return push_stmt_list ();
   9649 }
   9650 
   9651 /* Revert remapping of any non-static data members since
   9652    the last push_omp_privatization_clauses () call.  */
   9653 
   9654 void
   9655 pop_omp_privatization_clauses (tree stmt)
   9656 {
   9657   if (stmt == NULL_TREE)
   9658     return;
   9659   stmt = pop_stmt_list (stmt);
   9660   if (omp_private_member_map)
   9661     {
   9662       while (!omp_private_member_vec.is_empty ())
   9663 	{
   9664 	  tree t = omp_private_member_vec.pop ();
   9665 	  if (t == error_mark_node)
   9666 	    {
   9667 	      add_stmt (stmt);
   9668 	      return;
   9669 	    }
   9670 	  bool no_decl_expr = t == integer_zero_node;
   9671 	  if (no_decl_expr)
   9672 	    t = omp_private_member_vec.pop ();
   9673 	  tree *v = omp_private_member_map->get (t);
   9674 	  gcc_assert (v);
   9675 	  if (!no_decl_expr)
   9676 	    add_decl_expr (*v);
   9677 	  omp_private_member_map->remove (t);
   9678 	}
   9679       delete omp_private_member_map;
   9680       omp_private_member_map = NULL;
   9681     }
   9682   add_stmt (stmt);
   9683 }
   9684 
   9685 /* Remember OpenMP privatization clauses mapping and clear it.
   9686    Used for lambdas.  */
   9687 
   9688 void
   9689 save_omp_privatization_clauses (vec<tree> &save)
   9690 {
   9691   save = vNULL;
   9692   if (omp_private_member_ignore_next)
   9693     save.safe_push (integer_one_node);
   9694   omp_private_member_ignore_next = false;
   9695   if (!omp_private_member_map)
   9696     return;
   9697 
   9698   while (!omp_private_member_vec.is_empty ())
   9699     {
   9700       tree t = omp_private_member_vec.pop ();
   9701       if (t == error_mark_node)
   9702 	{
   9703 	  save.safe_push (t);
   9704 	  continue;
   9705 	}
   9706       tree n = t;
   9707       if (t == integer_zero_node)
   9708 	t = omp_private_member_vec.pop ();
   9709       tree *v = omp_private_member_map->get (t);
   9710       gcc_assert (v);
   9711       save.safe_push (*v);
   9712       save.safe_push (t);
   9713       if (n != t)
   9714 	save.safe_push (n);
   9715     }
   9716   delete omp_private_member_map;
   9717   omp_private_member_map = NULL;
   9718 }
   9719 
   9720 /* Restore OpenMP privatization clauses mapping saved by the
   9721    above function.  */
   9722 
   9723 void
   9724 restore_omp_privatization_clauses (vec<tree> &save)
   9725 {
   9726   gcc_assert (omp_private_member_vec.is_empty ());
   9727   omp_private_member_ignore_next = false;
   9728   if (save.is_empty ())
   9729     return;
   9730   if (save.length () == 1 && save[0] == integer_one_node)
   9731     {
   9732       omp_private_member_ignore_next = true;
   9733       save.release ();
   9734       return;
   9735     }
   9736 
   9737   omp_private_member_map = new hash_map <tree, tree>;
   9738   while (!save.is_empty ())
   9739     {
   9740       tree t = save.pop ();
   9741       tree n = t;
   9742       if (t != error_mark_node)
   9743 	{
   9744 	  if (t == integer_one_node)
   9745 	    {
   9746 	      omp_private_member_ignore_next = true;
   9747 	      gcc_assert (save.is_empty ());
   9748 	      break;
   9749 	    }
   9750 	  if (t == integer_zero_node)
   9751 	    t = save.pop ();
   9752 	  tree &v = omp_private_member_map->get_or_insert (t);
   9753 	  v = save.pop ();
   9754 	}
   9755       omp_private_member_vec.safe_push (t);
   9756       if (n != t)
   9757 	omp_private_member_vec.safe_push (n);
   9758     }
   9759   save.release ();
   9760 }
   9761 
   9762 /* For all variables in the tree_list VARS, mark them as thread local.  */
   9763 
   9764 void
   9765 finish_omp_threadprivate (tree vars)
   9766 {
   9767   tree t;
   9768 
   9769   /* Mark every variable in VARS to be assigned thread local storage.  */
   9770   for (t = vars; t; t = TREE_CHAIN (t))
   9771     {
   9772       tree v = TREE_PURPOSE (t);
   9773 
   9774       if (error_operand_p (v))
   9775 	;
   9776       else if (!VAR_P (v))
   9777 	error ("%<threadprivate%> %qD is not file, namespace "
   9778 	       "or block scope variable", v);
   9779       /* If V had already been marked threadprivate, it doesn't matter
   9780 	 whether it had been used prior to this point.  */
   9781       else if (TREE_USED (v)
   9782 	  && (DECL_LANG_SPECIFIC (v) == NULL
   9783 	      || !CP_DECL_THREADPRIVATE_P (v)))
   9784 	error ("%qE declared %<threadprivate%> after first use", v);
   9785       else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
   9786 	error ("automatic variable %qE cannot be %<threadprivate%>", v);
   9787       else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
   9788 	error ("%<threadprivate%> %qE has incomplete type", v);
   9789       else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
   9790 	       && CP_DECL_CONTEXT (v) != current_class_type)
   9791 	error ("%<threadprivate%> %qE directive not "
   9792 	       "in %qT definition", v, CP_DECL_CONTEXT (v));
   9793       else
   9794 	{
   9795 	  /* Allocate a LANG_SPECIFIC structure for V, if needed.  */
   9796 	  if (DECL_LANG_SPECIFIC (v) == NULL)
   9797 	    retrofit_lang_decl (v);
   9798 
   9799 	  if (! CP_DECL_THREAD_LOCAL_P (v))
   9800 	    {
   9801 	      CP_DECL_THREAD_LOCAL_P (v) = true;
   9802 	      set_decl_tls_model (v, decl_default_tls_model (v));
   9803 	      /* If rtl has been already set for this var, call
   9804 		 make_decl_rtl once again, so that encode_section_info
   9805 		 has a chance to look at the new decl flags.  */
   9806 	      if (DECL_RTL_SET_P (v))
   9807 		make_decl_rtl (v);
   9808 	    }
   9809 	  CP_DECL_THREADPRIVATE_P (v) = 1;
   9810 	}
   9811     }
   9812 }
   9813 
   9814 /* Build an OpenMP structured block.  */
   9815 
   9816 tree
   9817 begin_omp_structured_block (void)
   9818 {
   9819   return do_pushlevel (sk_omp);
   9820 }
   9821 
   9822 tree
   9823 finish_omp_structured_block (tree block)
   9824 {
   9825   return do_poplevel (block);
   9826 }
   9827 
   9828 /* Similarly, except force the retention of the BLOCK.  */
   9829 
   9830 tree
   9831 begin_omp_parallel (void)
   9832 {
   9833   keep_next_level (true);
   9834   return begin_omp_structured_block ();
   9835 }
   9836 
   9837 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
   9838    statement.  */
   9839 
   9840 tree
   9841 finish_oacc_data (tree clauses, tree block)
   9842 {
   9843   tree stmt;
   9844 
   9845   block = finish_omp_structured_block (block);
   9846 
   9847   stmt = make_node (OACC_DATA);
   9848   TREE_TYPE (stmt) = void_type_node;
   9849   OACC_DATA_CLAUSES (stmt) = clauses;
   9850   OACC_DATA_BODY (stmt) = block;
   9851 
   9852   return add_stmt (stmt);
   9853 }
   9854 
   9855 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
   9856    statement.  */
   9857 
   9858 tree
   9859 finish_oacc_host_data (tree clauses, tree block)
   9860 {
   9861   tree stmt;
   9862 
   9863   block = finish_omp_structured_block (block);
   9864 
   9865   stmt = make_node (OACC_HOST_DATA);
   9866   TREE_TYPE (stmt) = void_type_node;
   9867   OACC_HOST_DATA_CLAUSES (stmt) = clauses;
   9868   OACC_HOST_DATA_BODY (stmt) = block;
   9869 
   9870   return add_stmt (stmt);
   9871 }
   9872 
   9873 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
   9874    statement.  */
   9875 
   9876 tree
   9877 finish_omp_construct (enum tree_code code, tree body, tree clauses)
   9878 {
   9879   body = finish_omp_structured_block (body);
   9880 
   9881   tree stmt = make_node (code);
   9882   TREE_TYPE (stmt) = void_type_node;
   9883   OMP_BODY (stmt) = body;
   9884   OMP_CLAUSES (stmt) = clauses;
   9885 
   9886   return add_stmt (stmt);
   9887 }
   9888 
   9889 /* Used to walk OpenMP target directive body.  */
   9890 
   9891 struct omp_target_walk_data
   9892 {
   9893   /* Holds the 'this' expression found in current function.  */
   9894   tree current_object;
   9895 
   9896   /* True if the 'this' expression was accessed in the target body.  */
   9897   bool this_expr_accessed;
   9898 
   9899   /* For non-static functions, record which pointer-typed members were
   9900      accessed, and the whole expression.  */
   9901   hash_map<tree, tree> ptr_members_accessed;
   9902 
   9903   /* Record which lambda objects were accessed in target body.  */
   9904   hash_set<tree> lambda_objects_accessed;
   9905 
   9906   /* For lambda functions, the __closure object expression of the current
   9907      function, and the set of captured variables accessed in target body.  */
   9908   tree current_closure;
   9909   hash_set<tree> closure_vars_accessed;
   9910 
   9911   /* Local variables declared inside a BIND_EXPR, used to filter out such
   9912      variables when recording lambda_objects_accessed.  */
   9913   hash_set<tree> local_decls;
   9914 };
   9915 
   9916 /* Helper function of finish_omp_target_clauses, called via
   9917    cp_walk_tree_without_duplicates.  Traverse body of OpenMP target
   9918    directive *TP, and fill out omp_target_walk_data passed in *PTR.  */
   9919 
   9920 static tree
   9921 finish_omp_target_clauses_r (tree *tp, int *walk_subtrees, void *ptr)
   9922 {
   9923   tree t = *tp;
   9924   struct omp_target_walk_data *data = (struct omp_target_walk_data *) ptr;
   9925   tree current_object = data->current_object;
   9926   tree current_closure = data->current_closure;
   9927 
   9928   /* References inside of these expression codes shouldn't incur any
   9929      form of mapping, so return early.  */
   9930   if (TREE_CODE (t) == SIZEOF_EXPR
   9931       || TREE_CODE (t) == ALIGNOF_EXPR)
   9932     {
   9933       *walk_subtrees = 0;
   9934       return NULL_TREE;
   9935     }
   9936 
   9937   if (TREE_CODE (t) == OMP_CLAUSE)
   9938     return NULL_TREE;
   9939 
   9940   if (current_object)
   9941     {
   9942       tree this_expr = TREE_OPERAND (current_object, 0);
   9943 
   9944       if (operand_equal_p (t, this_expr))
   9945 	{
   9946 	  data->this_expr_accessed = true;
   9947 	  *walk_subtrees = 0;
   9948 	  return NULL_TREE;
   9949 	}
   9950 
   9951       if (TREE_CODE (t) == COMPONENT_REF
   9952 	  && POINTER_TYPE_P (TREE_TYPE (t))
   9953 	  && operand_equal_p (TREE_OPERAND (t, 0), current_object)
   9954 	  && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL)
   9955 	{
   9956 	  data->this_expr_accessed = true;
   9957 	  tree fld = TREE_OPERAND (t, 1);
   9958 	  if (data->ptr_members_accessed.get (fld) == NULL)
   9959 	    {
   9960 	      if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
   9961 		t = convert_from_reference (t);
   9962 	      data->ptr_members_accessed.put (fld, t);
   9963 	    }
   9964 	  *walk_subtrees = 0;
   9965 	  return NULL_TREE;
   9966 	}
   9967     }
   9968 
   9969   /* When the current_function_decl is a lambda function, the closure object
   9970      argument's type seems to not yet have fields layed out, so a recording
   9971      of DECL_VALUE_EXPRs during the target body walk seems the only way to
   9972      find them.  */
   9973   if (current_closure
   9974       && (VAR_P (t)
   9975 	  || TREE_CODE (t) == PARM_DECL
   9976 	  || TREE_CODE (t) == RESULT_DECL)
   9977       && DECL_HAS_VALUE_EXPR_P (t)
   9978       && TREE_CODE (DECL_VALUE_EXPR (t)) == COMPONENT_REF
   9979       && operand_equal_p (current_closure,
   9980 			  TREE_OPERAND (DECL_VALUE_EXPR (t), 0)))
   9981     {
   9982       if (!data->closure_vars_accessed.contains (t))
   9983 	data->closure_vars_accessed.add (t);
   9984       *walk_subtrees = 0;
   9985       return NULL_TREE;
   9986     }
   9987 
   9988   if (TREE_CODE (t) == BIND_EXPR)
   9989     {
   9990       tree block = BIND_EXPR_BLOCK (t);
   9991       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   9992 	if (!data->local_decls.contains (var))
   9993 	  data->local_decls.add (var);
   9994       return NULL_TREE;
   9995     }
   9996 
   9997   if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
   9998     {
   9999       tree lt = TREE_TYPE (t);
   10000       gcc_assert (CLASS_TYPE_P (lt));
   10001 
   10002       if (!data->lambda_objects_accessed.contains (t)
   10003 	  /* Do not prepare to create target maps for locally declared
   10004 	     lambdas or anonymous ones.  */
   10005 	  && !data->local_decls.contains (t)
   10006 	  && TREE_CODE (t) != TARGET_EXPR)
   10007 	data->lambda_objects_accessed.add (t);
   10008       *walk_subtrees = 0;
   10009       return NULL_TREE;
   10010     }
   10011 
   10012   return NULL_TREE;
   10013 }
   10014 
   10015 /* Helper function for finish_omp_target, and also from tsubst_expr.
   10016    Create additional clauses for mapping of non-static members, lambda objects,
   10017    etc.  */
   10018 
   10019 void
   10020 finish_omp_target_clauses (location_t loc, tree body, tree *clauses_ptr)
   10021 {
   10022   omp_target_walk_data data;
   10023   data.this_expr_accessed = false;
   10024   data.current_object = NULL_TREE;
   10025 
   10026   if (DECL_NONSTATIC_MEMBER_P (current_function_decl) && current_class_ptr)
   10027     if (tree ct = current_nonlambda_class_type ())
   10028       {
   10029 	tree object = maybe_dummy_object (ct, NULL);
   10030 	object = maybe_resolve_dummy (object, true);
   10031 	data.current_object = object;
   10032       }
   10033 
   10034   if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
   10035     {
   10036       tree closure = DECL_ARGUMENTS (current_function_decl);
   10037       data.current_closure = build_indirect_ref (loc, closure, RO_UNARY_STAR);
   10038     }
   10039   else
   10040     data.current_closure = NULL_TREE;
   10041 
   10042   cp_walk_tree_without_duplicates (&body, finish_omp_target_clauses_r, &data);
   10043 
   10044   auto_vec<tree, 16> new_clauses;
   10045 
   10046   tree omp_target_this_expr = NULL_TREE;
   10047   tree *explicit_this_deref_map = NULL;
   10048   if (data.this_expr_accessed)
   10049     {
   10050       omp_target_this_expr = TREE_OPERAND (data.current_object, 0);
   10051 
   10052       /* See if explicit user-specified map(this[:]) clause already exists.
   10053 	 If not, we create an implicit map(tofrom:this[:1]) clause.  */
   10054       for (tree *cp = clauses_ptr; *cp; cp = &OMP_CLAUSE_CHAIN (*cp))
   10055 	if (OMP_CLAUSE_CODE (*cp) == OMP_CLAUSE_MAP
   10056 	    && (TREE_CODE (OMP_CLAUSE_DECL (*cp)) == INDIRECT_REF
   10057 		|| TREE_CODE (OMP_CLAUSE_DECL (*cp)) == MEM_REF)
   10058 	    && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*cp), 0),
   10059 				omp_target_this_expr))
   10060 	  {
   10061 	    explicit_this_deref_map = cp;
   10062 	    break;
   10063 	  }
   10064     }
   10065 
   10066   if (DECL_LAMBDA_FUNCTION_P (current_function_decl)
   10067       && (data.this_expr_accessed
   10068 	  || !data.closure_vars_accessed.is_empty ()))
   10069     {
   10070       /* For lambda functions, we need to first create a copy of the
   10071 	 __closure object.  */
   10072       tree closure = DECL_ARGUMENTS (current_function_decl);
   10073       tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10074       OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
   10075       OMP_CLAUSE_DECL (c)
   10076 	= build_indirect_ref (loc, closure, RO_UNARY_STAR);
   10077       OMP_CLAUSE_SIZE (c)
   10078 	= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure)));
   10079       new_clauses.safe_push (c);
   10080 
   10081       tree closure_obj = OMP_CLAUSE_DECL (c);
   10082       tree closure_type = TREE_TYPE (closure_obj);
   10083 
   10084       gcc_assert (LAMBDA_TYPE_P (closure_type)
   10085 		  && CLASS_TYPE_P (closure_type));
   10086 
   10087       tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10088       OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
   10089       OMP_CLAUSE_DECL (c2) = closure;
   10090       OMP_CLAUSE_SIZE (c2) = size_zero_node;
   10091       new_clauses.safe_push (c2);
   10092     }
   10093 
   10094   if (data.this_expr_accessed)
   10095     {
   10096       /* If the this-expr was accessed, create a map(*this) clause.  */
   10097       enum gomp_map_kind kind = GOMP_MAP_TOFROM;
   10098       if (explicit_this_deref_map)
   10099 	{
   10100 	  tree this_map = *explicit_this_deref_map;
   10101 	  tree nc = OMP_CLAUSE_CHAIN (this_map);
   10102 	  gcc_assert (nc != NULL_TREE
   10103 		      && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
   10104 		      && (OMP_CLAUSE_MAP_KIND (nc)
   10105 			  == GOMP_MAP_FIRSTPRIVATE_POINTER));
   10106 	  kind = OMP_CLAUSE_MAP_KIND (this_map);
   10107 	  /* Remove the original 'map(*this) map(firstprivate_ptr:this)'
   10108 	     two-map sequence away from the chain.  */
   10109 	  *explicit_this_deref_map = OMP_CLAUSE_CHAIN (nc);
   10110 	}
   10111       tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10112       OMP_CLAUSE_SET_MAP_KIND (c, kind);
   10113       OMP_CLAUSE_DECL (c)
   10114 	= build_indirect_ref (loc, omp_target_this_expr, RO_UNARY_STAR);
   10115       OMP_CLAUSE_SIZE (c)
   10116 	= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (omp_target_this_expr)));
   10117       new_clauses.safe_push (c);
   10118 
   10119       /* If we're in a lambda function, the this-pointer will actually be
   10120 	 '__closure->this', a mapped member of __closure, hence always_pointer.
   10121 	 Otherwise it's a firstprivate pointer.  */
   10122       enum gomp_map_kind ptr_kind
   10123 	= (DECL_LAMBDA_FUNCTION_P (current_function_decl)
   10124 	   ? GOMP_MAP_ALWAYS_POINTER
   10125 	   : GOMP_MAP_FIRSTPRIVATE_POINTER);
   10126       c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10127       OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
   10128       OMP_CLAUSE_DECL (c) = omp_target_this_expr;
   10129       OMP_CLAUSE_SIZE (c) = size_zero_node;
   10130       new_clauses.safe_push (c);
   10131     }
   10132 
   10133   if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
   10134     {
   10135       if (omp_target_this_expr)
   10136 	{
   10137 	  STRIP_NOPS (omp_target_this_expr);
   10138 	  gcc_assert (DECL_HAS_VALUE_EXPR_P (omp_target_this_expr));
   10139 	  omp_target_this_expr = DECL_VALUE_EXPR (omp_target_this_expr);
   10140 	}
   10141 
   10142       for (hash_set<tree>::iterator i = data.closure_vars_accessed.begin ();
   10143 	   i != data.closure_vars_accessed.end (); ++i)
   10144 	{
   10145 	  tree orig_decl = *i;
   10146 	  tree closure_expr = DECL_VALUE_EXPR (orig_decl);
   10147 
   10148 	  if (TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE
   10149 	      || TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE)
   10150 	    {
   10151 	      /* this-pointer is processed above, outside this loop.  */
   10152 	      if (omp_target_this_expr
   10153 		  && operand_equal_p (closure_expr, omp_target_this_expr))
   10154 		continue;
   10155 
   10156 	      bool ptr_p = TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE;
   10157 	      enum gomp_map_kind kind, ptr_kind, nc_kind;
   10158 	      tree size;
   10159 
   10160 	      if (ptr_p)
   10161 		{
   10162 		  /* For pointers, default mapped as zero-length array
   10163 		     section.  */
   10164 		  kind = GOMP_MAP_ALLOC;
   10165 		  nc_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
   10166 		  ptr_kind = GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
   10167 		  size = size_zero_node;
   10168 		}
   10169 	      else
   10170 		{
   10171 		  /* For references, default mapped as appearing on map
   10172 		     clause.  */
   10173 		  kind = GOMP_MAP_TOFROM;
   10174 		  nc_kind = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
   10175 		  ptr_kind = GOMP_MAP_ALWAYS_POINTER;
   10176 		  size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure_expr)));
   10177 		}
   10178 
   10179 	      for (tree *p = clauses_ptr; *p; p = &OMP_CLAUSE_CHAIN (*p))
   10180 		if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_MAP
   10181 		    && (TREE_CODE (OMP_CLAUSE_DECL (*p)) == INDIRECT_REF
   10182 			|| TREE_CODE (OMP_CLAUSE_DECL (*p)) == MEM_REF)
   10183 		    && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*p), 0),
   10184 					orig_decl))
   10185 		  {
   10186 		    /* If this was already specified by user as a map,
   10187 		       save the user specified map kind, delete the
   10188 		       "map(*ptr/ref), map(firstprivate ptr/ref)" sequence,
   10189 		       and insert our own sequence:
   10190 		       "map(*__closure->ptr/ref), map(<ptr_kind>:__closure->ref"
   10191 		    */
   10192 		    tree nc = OMP_CLAUSE_CHAIN (*p);
   10193 		    gcc_assert (nc != NULL_TREE
   10194 				&& OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
   10195 				&& OMP_CLAUSE_MAP_KIND (nc) == nc_kind);
   10196 		    /* Update with user specified kind and size.  */
   10197 		    kind = OMP_CLAUSE_MAP_KIND (*p);
   10198 		    size = OMP_CLAUSE_SIZE (*p);
   10199 		    *p = OMP_CLAUSE_CHAIN (nc);
   10200 		    break;
   10201 		  }
   10202 
   10203 	      tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10204 	      OMP_CLAUSE_SET_MAP_KIND (c, kind);
   10205 	      OMP_CLAUSE_DECL (c)
   10206 		= build_indirect_ref (loc, closure_expr, RO_UNARY_STAR);
   10207 	      OMP_CLAUSE_SIZE (c) = size;
   10208 	      if (ptr_p)
   10209 		OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
   10210 	      new_clauses.safe_push (c);
   10211 
   10212 	      c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10213 	      OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
   10214 	      OMP_CLAUSE_DECL (c) = closure_expr;
   10215 	      OMP_CLAUSE_SIZE (c) = size_zero_node;
   10216 	      new_clauses.safe_push (c);
   10217 	    }
   10218 	}
   10219     }
   10220 
   10221   if (!data.ptr_members_accessed.is_empty ())
   10222     for (hash_map<tree, tree>::iterator i = data.ptr_members_accessed.begin ();
   10223 	 i != data.ptr_members_accessed.end (); ++i)
   10224       {
   10225 	/* For each referenced member that is of pointer or reference-to-pointer
   10226 	   type, create the equivalent of map(alloc:this->ptr[:0]).  */
   10227 	tree field_decl = (*i).first;
   10228 	tree ptr_member = (*i).second;
   10229 
   10230 	for (tree c = *clauses_ptr; c; c = OMP_CLAUSE_CHAIN (c))
   10231 	  {
   10232 	    if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
   10233 	      continue;
   10234 	    /* If map(this->ptr[:N]) already exists, avoid creating another
   10235 	       such map.  */
   10236 	    tree decl = OMP_CLAUSE_DECL (c);
   10237 	    if ((TREE_CODE (decl) == INDIRECT_REF
   10238 		 || TREE_CODE (decl) == MEM_REF)
   10239 		&& operand_equal_p (TREE_OPERAND (decl, 0), ptr_member))
   10240 	      goto next_ptr_member;
   10241 	  }
   10242 
   10243 	if (!cxx_mark_addressable (ptr_member))
   10244 	  gcc_unreachable ();
   10245 
   10246 	if (TREE_CODE (TREE_TYPE (field_decl)) == REFERENCE_TYPE)
   10247 	  {
   10248 	    /* For reference to pointers, we need to map the referenced
   10249 	       pointer first for things to be correct.  */
   10250 	    tree ptr_member_type = TREE_TYPE (ptr_member);
   10251 
   10252 	    /* Map pointer target as zero-length array section.  */
   10253 	    tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10254 	    OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
   10255 	    OMP_CLAUSE_DECL (c)
   10256 	      = build1 (INDIRECT_REF, TREE_TYPE (ptr_member_type), ptr_member);
   10257 	    OMP_CLAUSE_SIZE (c) = size_zero_node;
   10258 	    OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
   10259 
   10260 	    /* Map pointer to zero-length array section.  */
   10261 	    tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10262 	    OMP_CLAUSE_SET_MAP_KIND
   10263 	      (c2, GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION);
   10264 	    OMP_CLAUSE_DECL (c2) = ptr_member;
   10265 	    OMP_CLAUSE_SIZE (c2) = size_zero_node;
   10266 
   10267 	    /* Attach reference-to-pointer field to pointer.  */
   10268 	    tree c3 = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10269 	    OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_ATTACH);
   10270 	    OMP_CLAUSE_DECL (c3) = TREE_OPERAND (ptr_member, 0);
   10271 	    OMP_CLAUSE_SIZE (c3) = size_zero_node;
   10272 
   10273 	    new_clauses.safe_push (c);
   10274 	    new_clauses.safe_push (c2);
   10275 	    new_clauses.safe_push (c3);
   10276 	  }
   10277 	else if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
   10278 	  {
   10279 	    /* Map pointer target as zero-length array section.  */
   10280 	    tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10281 	    OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
   10282 	    OMP_CLAUSE_DECL (c) = build_indirect_ref (loc, ptr_member,
   10283 						      RO_UNARY_STAR);
   10284 	    OMP_CLAUSE_SIZE (c) = size_zero_node;
   10285 	    OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
   10286 
   10287 	    /* Attach zero-length array section to pointer.  */
   10288 	    tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10289 	    OMP_CLAUSE_SET_MAP_KIND
   10290 	      (c2, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
   10291 	    OMP_CLAUSE_DECL (c2) = ptr_member;
   10292 	    OMP_CLAUSE_SIZE (c2) = size_zero_node;
   10293 
   10294 	    new_clauses.safe_push (c);
   10295 	    new_clauses.safe_push (c2);
   10296 	  }
   10297 	else
   10298 	  gcc_unreachable ();
   10299 
   10300       next_ptr_member:
   10301 	;
   10302       }
   10303 
   10304   for (hash_set<tree>::iterator i = data.lambda_objects_accessed.begin ();
   10305        i != data.lambda_objects_accessed.end (); ++i)
   10306     {
   10307       tree lobj = *i;
   10308       if (TREE_CODE (lobj) == TARGET_EXPR)
   10309 	lobj = TREE_OPERAND (lobj, 0);
   10310 
   10311       tree lt = TREE_TYPE (lobj);
   10312       gcc_assert (LAMBDA_TYPE_P (lt) && CLASS_TYPE_P (lt));
   10313 
   10314       tree lc = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10315       OMP_CLAUSE_SET_MAP_KIND (lc, GOMP_MAP_TO);
   10316       OMP_CLAUSE_DECL (lc) = lobj;
   10317       OMP_CLAUSE_SIZE (lc) = TYPE_SIZE_UNIT (lt);
   10318       new_clauses.safe_push (lc);
   10319 
   10320       for (tree fld = TYPE_FIELDS (lt); fld; fld = DECL_CHAIN (fld))
   10321 	{
   10322 	  if (TREE_CODE (TREE_TYPE (fld)) == POINTER_TYPE)
   10323 	    {
   10324 	      tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
   10325 				 lobj, fld, NULL_TREE);
   10326 	      tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10327 	      OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
   10328 	      OMP_CLAUSE_DECL (c)
   10329 		= build_indirect_ref (loc, exp, RO_UNARY_STAR);
   10330 	      OMP_CLAUSE_SIZE (c) = size_zero_node;
   10331 	      OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
   10332 	      new_clauses.safe_push (c);
   10333 
   10334 	      c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10335 	      OMP_CLAUSE_SET_MAP_KIND
   10336 		(c, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
   10337 	      OMP_CLAUSE_DECL (c) = exp;
   10338 	      OMP_CLAUSE_SIZE (c) = size_zero_node;
   10339 	      new_clauses.safe_push (c);
   10340 	    }
   10341 	  else if (TREE_CODE (TREE_TYPE (fld)) == REFERENCE_TYPE)
   10342 	    {
   10343 	      tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
   10344 				 lobj, fld, NULL_TREE);
   10345 	      tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10346 	      OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
   10347 	      OMP_CLAUSE_DECL (c)
   10348 		= build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
   10349 	      OMP_CLAUSE_SIZE (c)
   10350 		= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (exp)));
   10351 	      new_clauses.safe_push (c);
   10352 
   10353 	      c = build_omp_clause (loc, OMP_CLAUSE_MAP);
   10354 	      OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
   10355 	      OMP_CLAUSE_DECL (c) = exp;
   10356 	      OMP_CLAUSE_SIZE (c) = size_zero_node;
   10357 	      new_clauses.safe_push (c);
   10358 	    }
   10359 	}
   10360     }
   10361 
   10362   tree c = *clauses_ptr;
   10363   for (int i = new_clauses.length () - 1; i >= 0; i--)
   10364     {
   10365       OMP_CLAUSE_CHAIN (new_clauses[i]) = c;
   10366       c = new_clauses[i];
   10367     }
   10368   *clauses_ptr = c;
   10369 }
   10370 
   10371 /* Called from cp_parser_omp_target.  Create additional implicit clauses for
   10372    OpenMP target directives, and do sanity checks.  */
   10373 
   10374 tree
   10375 finish_omp_target (location_t loc, tree clauses, tree body, bool combined_p)
   10376 {
   10377   if (!processing_template_decl)
   10378     finish_omp_target_clauses (loc, body, &clauses);
   10379 
   10380   tree stmt = make_node (OMP_TARGET);
   10381   TREE_TYPE (stmt) = void_type_node;
   10382   OMP_TARGET_CLAUSES (stmt) = clauses;
   10383   OMP_TARGET_BODY (stmt) = body;
   10384   OMP_TARGET_COMBINED (stmt) = combined_p;
   10385   SET_EXPR_LOCATION (stmt, loc);
   10386 
   10387   tree c = clauses;
   10388   while (c)
   10389     {
   10390       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
   10391 	switch (OMP_CLAUSE_MAP_KIND (c))
   10392 	  {
   10393 	  case GOMP_MAP_TO:
   10394 	  case GOMP_MAP_ALWAYS_TO:
   10395 	  case GOMP_MAP_PRESENT_TO:
   10396 	  case GOMP_MAP_ALWAYS_PRESENT_TO:
   10397 	  case GOMP_MAP_FROM:
   10398 	  case GOMP_MAP_ALWAYS_FROM:
   10399 	  case GOMP_MAP_PRESENT_FROM:
   10400 	  case GOMP_MAP_ALWAYS_PRESENT_FROM:
   10401 	  case GOMP_MAP_TOFROM:
   10402 	  case GOMP_MAP_ALWAYS_TOFROM:
   10403 	  case GOMP_MAP_PRESENT_TOFROM:
   10404 	  case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
   10405 	  case GOMP_MAP_ALLOC:
   10406 	  case GOMP_MAP_PRESENT_ALLOC:
   10407 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
   10408 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
   10409 	  case GOMP_MAP_ALWAYS_POINTER:
   10410 	  case GOMP_MAP_ATTACH_DETACH:
   10411 	  case GOMP_MAP_ATTACH:
   10412 	  case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
   10413 	  case GOMP_MAP_POINTER:
   10414 	  case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
   10415 	    break;
   10416 	  default:
   10417 	    error_at (OMP_CLAUSE_LOCATION (c),
   10418 		      "%<#pragma omp target%> with map-type other "
   10419 		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
   10420 		      "on %<map%> clause");
   10421 	    break;
   10422 	  }
   10423       c = OMP_CLAUSE_CHAIN (c);
   10424     }
   10425   return add_stmt (stmt);
   10426 }
   10427 
   10428 tree
   10429 finish_omp_parallel (tree clauses, tree body)
   10430 {
   10431   tree stmt;
   10432 
   10433   body = finish_omp_structured_block (body);
   10434 
   10435   stmt = make_node (OMP_PARALLEL);
   10436   TREE_TYPE (stmt) = void_type_node;
   10437   OMP_PARALLEL_CLAUSES (stmt) = clauses;
   10438   OMP_PARALLEL_BODY (stmt) = body;
   10439 
   10440   return add_stmt (stmt);
   10441 }
   10442 
   10443 tree
   10444 begin_omp_task (void)
   10445 {
   10446   keep_next_level (true);
   10447   return begin_omp_structured_block ();
   10448 }
   10449 
   10450 tree
   10451 finish_omp_task (tree clauses, tree body)
   10452 {
   10453   tree stmt;
   10454 
   10455   body = finish_omp_structured_block (body);
   10456 
   10457   stmt = make_node (OMP_TASK);
   10458   TREE_TYPE (stmt) = void_type_node;
   10459   OMP_TASK_CLAUSES (stmt) = clauses;
   10460   OMP_TASK_BODY (stmt) = body;
   10461 
   10462   return add_stmt (stmt);
   10463 }
   10464 
   10465 /* Helper function for finish_omp_for.  Convert Ith random access iterator
   10466    into integral iterator.  Return FALSE if successful.  */
   10467 
   10468 static bool
   10469 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
   10470 			       tree declv, tree orig_declv, tree initv,
   10471 			       tree condv, tree incrv, tree *body,
   10472 			       tree *pre_body, tree &clauses,
   10473 			       int collapse, int ordered)
   10474 {
   10475   tree diff, iter_init, iter_incr = NULL, last;
   10476   tree incr_var = NULL, orig_pre_body, orig_body, c;
   10477   tree decl = TREE_VEC_ELT (declv, i);
   10478   tree init = TREE_VEC_ELT (initv, i);
   10479   tree cond = TREE_VEC_ELT (condv, i);
   10480   tree incr = TREE_VEC_ELT (incrv, i);
   10481   tree iter = decl;
   10482   location_t elocus = locus;
   10483 
   10484   if (init && EXPR_HAS_LOCATION (init))
   10485     elocus = EXPR_LOCATION (init);
   10486 
   10487   switch (TREE_CODE (cond))
   10488     {
   10489     case GT_EXPR:
   10490     case GE_EXPR:
   10491     case LT_EXPR:
   10492     case LE_EXPR:
   10493     case NE_EXPR:
   10494       if (TREE_OPERAND (cond, 1) == iter)
   10495 	cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
   10496 		       TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
   10497       if (TREE_OPERAND (cond, 0) != iter)
   10498 	cond = error_mark_node;
   10499       else
   10500 	{
   10501 	  tree tem = build_x_binary_op (EXPR_LOCATION (cond),
   10502 					TREE_CODE (cond),
   10503 					iter, ERROR_MARK,
   10504 					TREE_OPERAND (cond, 1), ERROR_MARK,
   10505 					NULL_TREE, NULL, tf_warning_or_error);
   10506 	  if (error_operand_p (tem))
   10507 	    return true;
   10508 	}
   10509       break;
   10510     default:
   10511       cond = error_mark_node;
   10512       break;
   10513     }
   10514   if (cond == error_mark_node)
   10515     {
   10516       error_at (elocus, "invalid controlling predicate");
   10517       return true;
   10518     }
   10519   diff = build_x_binary_op (elocus, MINUS_EXPR,
   10520 			    TREE_OPERAND (cond, 1), ERROR_MARK,
   10521 			    iter, ERROR_MARK,
   10522 			    NULL_TREE, NULL, tf_warning_or_error);
   10523   diff = cp_fully_fold (diff);
   10524   if (error_operand_p (diff))
   10525     return true;
   10526   if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
   10527     {
   10528       error_at (elocus, "difference between %qE and %qD does not have integer type",
   10529 		TREE_OPERAND (cond, 1), iter);
   10530       return true;
   10531     }
   10532   if (!c_omp_check_loop_iv_exprs (locus, code, orig_declv, i,
   10533 				  TREE_VEC_ELT (declv, i), NULL_TREE,
   10534 				  cond, cp_walk_subtrees))
   10535     return true;
   10536 
   10537   switch (TREE_CODE (incr))
   10538     {
   10539     case PREINCREMENT_EXPR:
   10540     case PREDECREMENT_EXPR:
   10541     case POSTINCREMENT_EXPR:
   10542     case POSTDECREMENT_EXPR:
   10543       if (TREE_OPERAND (incr, 0) != iter)
   10544 	{
   10545 	  incr = error_mark_node;
   10546 	  break;
   10547 	}
   10548       iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
   10549 				    TREE_CODE (incr), iter,
   10550 				    NULL_TREE, tf_warning_or_error);
   10551       if (error_operand_p (iter_incr))
   10552 	return true;
   10553       else if (TREE_CODE (incr) == PREINCREMENT_EXPR
   10554 	       || TREE_CODE (incr) == POSTINCREMENT_EXPR)
   10555 	incr = integer_one_node;
   10556       else
   10557 	incr = integer_minus_one_node;
   10558       break;
   10559     case MODIFY_EXPR:
   10560       if (TREE_OPERAND (incr, 0) != iter)
   10561 	incr = error_mark_node;
   10562       else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   10563 	       || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   10564 	{
   10565 	  tree rhs = TREE_OPERAND (incr, 1);
   10566 	  if (TREE_OPERAND (rhs, 0) == iter)
   10567 	    {
   10568 	      if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
   10569 		  != INTEGER_TYPE)
   10570 		incr = error_mark_node;
   10571 	      else
   10572 		{
   10573 		  iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
   10574 						   iter, TREE_CODE (rhs),
   10575 						   TREE_OPERAND (rhs, 1),
   10576 						   NULL_TREE,
   10577 						   tf_warning_or_error);
   10578 		  if (error_operand_p (iter_incr))
   10579 		    return true;
   10580 		  incr = TREE_OPERAND (rhs, 1);
   10581 		  incr = cp_convert (TREE_TYPE (diff), incr,
   10582 				     tf_warning_or_error);
   10583 		  if (TREE_CODE (rhs) == MINUS_EXPR)
   10584 		    {
   10585 		      incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
   10586 		      incr = fold_simple (incr);
   10587 		    }
   10588 		  if (TREE_CODE (incr) != INTEGER_CST
   10589 		      && (TREE_CODE (incr) != NOP_EXPR
   10590 			  || (TREE_CODE (TREE_OPERAND (incr, 0))
   10591 			      != INTEGER_CST)))
   10592 		    iter_incr = NULL;
   10593 		}
   10594 	    }
   10595 	  else if (TREE_OPERAND (rhs, 1) == iter)
   10596 	    {
   10597 	      if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
   10598 		  || TREE_CODE (rhs) != PLUS_EXPR)
   10599 		incr = error_mark_node;
   10600 	      else
   10601 		{
   10602 		  iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
   10603 						 PLUS_EXPR,
   10604 						 TREE_OPERAND (rhs, 0),
   10605 						 ERROR_MARK, iter,
   10606 						 ERROR_MARK, NULL_TREE, NULL,
   10607 						 tf_warning_or_error);
   10608 		  if (error_operand_p (iter_incr))
   10609 		    return true;
   10610 		  iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
   10611 						   iter, NOP_EXPR,
   10612 						   iter_incr, NULL_TREE,
   10613 						   tf_warning_or_error);
   10614 		  if (error_operand_p (iter_incr))
   10615 		    return true;
   10616 		  incr = TREE_OPERAND (rhs, 0);
   10617 		  iter_incr = NULL;
   10618 		}
   10619 	    }
   10620 	  else
   10621 	    incr = error_mark_node;
   10622 	}
   10623       else
   10624 	incr = error_mark_node;
   10625       break;
   10626     default:
   10627       incr = error_mark_node;
   10628       break;
   10629     }
   10630 
   10631   if (incr == error_mark_node)
   10632     {
   10633       error_at (elocus, "invalid increment expression");
   10634       return true;
   10635     }
   10636 
   10637   incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
   10638   incr = cp_fully_fold (incr);
   10639   tree loop_iv_seen = NULL_TREE;
   10640   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
   10641     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
   10642 	&& OMP_CLAUSE_DECL (c) == iter)
   10643       {
   10644 	if (code == OMP_TASKLOOP || code == OMP_LOOP)
   10645 	  {
   10646 	    loop_iv_seen = c;
   10647 	    OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
   10648 	  }
   10649 	break;
   10650       }
   10651     else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
   10652 	     && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
   10653 	     && OMP_CLAUSE_DECL (c) == iter)
   10654       {
   10655 	loop_iv_seen = c;
   10656 	if (code == OMP_TASKLOOP)
   10657 	  OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
   10658       }
   10659 
   10660   decl = create_temporary_var (TREE_TYPE (diff));
   10661   pushdecl (decl);
   10662   add_decl_expr (decl);
   10663   last = create_temporary_var (TREE_TYPE (diff));
   10664   pushdecl (last);
   10665   add_decl_expr (last);
   10666   if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
   10667       && (!ordered || (i < collapse && collapse > 1)))
   10668     {
   10669       incr_var = create_temporary_var (TREE_TYPE (diff));
   10670       pushdecl (incr_var);
   10671       add_decl_expr (incr_var);
   10672     }
   10673   gcc_assert (stmts_are_full_exprs_p ());
   10674   tree diffvar = NULL_TREE;
   10675   if (code == OMP_TASKLOOP)
   10676     {
   10677       if (!loop_iv_seen)
   10678 	{
   10679 	  tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
   10680 	  OMP_CLAUSE_DECL (ivc) = iter;
   10681 	  cxx_omp_finish_clause (ivc, NULL, false);
   10682 	  OMP_CLAUSE_CHAIN (ivc) = clauses;
   10683 	  clauses = ivc;
   10684 	}
   10685       tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
   10686       OMP_CLAUSE_DECL (lvc) = last;
   10687       OMP_CLAUSE_CHAIN (lvc) = clauses;
   10688       clauses = lvc;
   10689       diffvar = create_temporary_var (TREE_TYPE (diff));
   10690       pushdecl (diffvar);
   10691       add_decl_expr (diffvar);
   10692     }
   10693   else if (code == OMP_LOOP)
   10694     {
   10695       if (!loop_iv_seen)
   10696 	{
   10697 	  /* While iterators on the loop construct are predetermined
   10698 	     lastprivate, if the decl is not declared inside of the
   10699 	     loop, OMP_CLAUSE_LASTPRIVATE should have been added
   10700 	     already.  */
   10701 	  loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
   10702 	  OMP_CLAUSE_DECL (loop_iv_seen) = iter;
   10703 	  OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
   10704 	  clauses = loop_iv_seen;
   10705 	}
   10706       else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
   10707 	{
   10708 	  OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
   10709 	  OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
   10710 	  OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
   10711 	}
   10712       if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
   10713 	cxx_omp_finish_clause (loop_iv_seen, NULL, false);
   10714     }
   10715 
   10716   orig_pre_body = *pre_body;
   10717   *pre_body = push_stmt_list ();
   10718   if (orig_pre_body)
   10719     add_stmt (orig_pre_body);
   10720   if (init != NULL)
   10721     finish_expr_stmt (build_x_modify_expr (elocus,
   10722 					   iter, NOP_EXPR, init,
   10723 					   NULL_TREE, tf_warning_or_error));
   10724   init = build_int_cst (TREE_TYPE (diff), 0);
   10725   if (c && iter_incr == NULL
   10726       && (!ordered || (i < collapse && collapse > 1)))
   10727     {
   10728       if (incr_var)
   10729 	{
   10730 	  finish_expr_stmt (build_x_modify_expr (elocus,
   10731 						 incr_var, NOP_EXPR,
   10732 						 incr, NULL_TREE,
   10733 						 tf_warning_or_error));
   10734 	  incr = incr_var;
   10735 	}
   10736       iter_incr = build_x_modify_expr (elocus,
   10737 				       iter, PLUS_EXPR, incr,
   10738 				       NULL_TREE, tf_warning_or_error);
   10739     }
   10740   if (c && ordered && i < collapse && collapse > 1)
   10741     iter_incr = incr;
   10742   finish_expr_stmt (build_x_modify_expr (elocus,
   10743 					 last, NOP_EXPR, init,
   10744 					 NULL_TREE, tf_warning_or_error));
   10745   if (diffvar)
   10746     {
   10747       finish_expr_stmt (build_x_modify_expr (elocus,
   10748 					     diffvar, NOP_EXPR,
   10749 					     diff, NULL_TREE, tf_warning_or_error));
   10750       diff = diffvar;
   10751     }
   10752   *pre_body = pop_stmt_list (*pre_body);
   10753 
   10754   cond = cp_build_binary_op (elocus,
   10755 			     TREE_CODE (cond), decl, diff,
   10756 			     tf_warning_or_error);
   10757   incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
   10758 			    elocus, incr, NULL_TREE);
   10759 
   10760   orig_body = *body;
   10761   *body = push_stmt_list ();
   10762   iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
   10763   iter_init = build_x_modify_expr (elocus,
   10764 				   iter, PLUS_EXPR, iter_init,
   10765 				   NULL_TREE, tf_warning_or_error);
   10766   if (iter_init != error_mark_node)
   10767     iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
   10768   finish_expr_stmt (iter_init);
   10769   finish_expr_stmt (build_x_modify_expr (elocus,
   10770 					 last, NOP_EXPR, decl,
   10771 					 NULL_TREE, tf_warning_or_error));
   10772   add_stmt (orig_body);
   10773   *body = pop_stmt_list (*body);
   10774 
   10775   if (c)
   10776     {
   10777       OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
   10778       if (!ordered)
   10779 	finish_expr_stmt (iter_incr);
   10780       else
   10781 	{
   10782 	  iter_init = decl;
   10783 	  if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
   10784 	    iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
   10785 				iter_init, iter_incr);
   10786 	  iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
   10787 	  iter_init = build_x_modify_expr (elocus,
   10788 					   iter, PLUS_EXPR, iter_init,
   10789 					   NULL_TREE, tf_warning_or_error);
   10790 	  if (iter_init != error_mark_node)
   10791 	    iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
   10792 	  finish_expr_stmt (iter_init);
   10793 	}
   10794       OMP_CLAUSE_LASTPRIVATE_STMT (c)
   10795 	= pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
   10796     }
   10797 
   10798   if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
   10799     {
   10800       tree t = TREE_VEC_ELT (orig_declv, i);
   10801       gcc_assert (TREE_PURPOSE (t) == NULL_TREE
   10802 		  && TREE_VALUE (t) == NULL_TREE
   10803 		  && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
   10804       TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
   10805       TREE_VALUE (t) = last;
   10806     }
   10807   else
   10808     TREE_VEC_ELT (orig_declv, i)
   10809       = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
   10810   TREE_VEC_ELT (declv, i) = decl;
   10811   TREE_VEC_ELT (initv, i) = init;
   10812   TREE_VEC_ELT (condv, i) = cond;
   10813   TREE_VEC_ELT (incrv, i) = incr;
   10814 
   10815   return false;
   10816 }
   10817 
   10818 /* Build and validate an OMP_FOR statement.  CLAUSES, BODY, COND, INCR
   10819    are directly for their associated operands in the statement.  DECL
   10820    and INIT are a combo; if DECL is NULL then INIT ought to be a
   10821    MODIFY_EXPR, and the DECL should be extracted.  PRE_BODY are
   10822    optional statements that need to go before the loop into its
   10823    sk_omp scope.  */
   10824 
   10825 tree
   10826 finish_omp_for (location_t locus, enum tree_code code, tree declv,
   10827 		tree orig_declv, tree initv, tree condv, tree incrv,
   10828 		tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
   10829 {
   10830   tree omp_for = NULL, orig_incr = NULL;
   10831   tree decl = NULL, init, cond, incr;
   10832   location_t elocus;
   10833   int i;
   10834   int collapse = 1;
   10835   int ordered = 0;
   10836   auto_vec<location_t> init_locv;
   10837 
   10838   gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
   10839   gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
   10840   gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
   10841   if (TREE_VEC_LENGTH (declv) > 1)
   10842     {
   10843       tree c;
   10844 
   10845       c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
   10846       if (c)
   10847 	collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
   10848       else
   10849 	{
   10850 	  c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
   10851 	  if (c)
   10852 	    collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
   10853 	  if (collapse != TREE_VEC_LENGTH (declv))
   10854 	    ordered = TREE_VEC_LENGTH (declv);
   10855 	}
   10856     }
   10857   for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
   10858     {
   10859       decl = TREE_VEC_ELT (declv, i);
   10860       init = TREE_VEC_ELT (initv, i);
   10861       cond = TREE_VEC_ELT (condv, i);
   10862       incr = TREE_VEC_ELT (incrv, i);
   10863       elocus = locus;
   10864 
   10865       /* We are going to throw out the init's original MODIFY_EXPR or
   10866 	 MODOP_EXPR below.  Save its location so we can use it when
   10867 	 reconstructing the expression farther down.  Alternatively, if the
   10868 	 initializer is a binding of the iteration variable, save
   10869 	 that location.  Any of these locations in the initialization clause
   10870 	 for the current nested loop are better than using the argument locus,
   10871 	 that points to the "for" of the outermost loop in the nest.  */
   10872       if (init && EXPR_HAS_LOCATION (init))
   10873 	elocus = EXPR_LOCATION (init);
   10874       else if (decl && INDIRECT_REF_P (decl) && EXPR_HAS_LOCATION (decl))
   10875 	/* This can happen for class iterators.  */
   10876 	elocus = EXPR_LOCATION (decl);
   10877       else if (decl && DECL_P (decl))
   10878 	{
   10879 	  if (DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION)
   10880 	    elocus = DECL_SOURCE_LOCATION (decl);
   10881 	  else if (DECL_INITIAL (decl)
   10882 		   && EXPR_HAS_LOCATION (DECL_INITIAL (decl)))
   10883 	    elocus = EXPR_LOCATION (DECL_INITIAL (decl));
   10884 	}
   10885       init_locv.safe_push (elocus);
   10886 
   10887       if (decl == NULL)
   10888 	{
   10889 	  if (init != NULL)
   10890 	    switch (TREE_CODE (init))
   10891 	      {
   10892 	      case MODIFY_EXPR:
   10893 		decl = TREE_OPERAND (init, 0);
   10894 		init = TREE_OPERAND (init, 1);
   10895 		break;
   10896 	      case MODOP_EXPR:
   10897 		if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
   10898 		  {
   10899 		    decl = TREE_OPERAND (init, 0);
   10900 		    init = TREE_OPERAND (init, 2);
   10901 		  }
   10902 		break;
   10903 	      default:
   10904 		break;
   10905 	      }
   10906 
   10907 	  if (decl == NULL)
   10908 	    {
   10909 	      error_at (locus,
   10910 			"expected iteration declaration or initialization");
   10911 	      return NULL;
   10912 	    }
   10913 	}
   10914 
   10915       if (cond == global_namespace)
   10916 	continue;
   10917 
   10918       if (cond == NULL)
   10919 	{
   10920 	  error_at (elocus, "missing controlling predicate");
   10921 	  return NULL;
   10922 	}
   10923 
   10924       if (incr == NULL)
   10925 	{
   10926 	  error_at (elocus, "missing increment expression");
   10927 	  return NULL;
   10928 	}
   10929 
   10930       TREE_VEC_ELT (declv, i) = decl;
   10931       TREE_VEC_ELT (initv, i) = init;
   10932     }
   10933 
   10934   if (orig_inits)
   10935     {
   10936       bool fail = false;
   10937       tree orig_init;
   10938       FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
   10939 	if (orig_init
   10940 	    && !c_omp_check_loop_iv_exprs (locus, code,
   10941 					   orig_declv ? orig_declv : declv, i,
   10942 					   TREE_VEC_ELT (declv, i), orig_init,
   10943 					   NULL_TREE, cp_walk_subtrees))
   10944 	  fail = true;
   10945       if (fail)
   10946 	return NULL;
   10947     }
   10948 
   10949   if (dependent_omp_for_p (declv, initv, condv, incrv))
   10950     {
   10951       tree stmt;
   10952 
   10953       stmt = make_node (code);
   10954 
   10955       for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
   10956 	{
   10957 	  /* This is really just a place-holder.  We'll be decomposing this
   10958 	     again and going through the cp_build_modify_expr path below when
   10959 	     we instantiate the thing.  */
   10960 	  TREE_VEC_ELT (initv, i)
   10961 	    = build2_loc (init_locv[i], MODIFY_EXPR, void_type_node,
   10962 			  TREE_VEC_ELT (declv, i), TREE_VEC_ELT (initv, i));
   10963 	}
   10964 
   10965       TREE_TYPE (stmt) = void_type_node;
   10966       OMP_FOR_INIT (stmt) = initv;
   10967       OMP_FOR_COND (stmt) = condv;
   10968       OMP_FOR_INCR (stmt) = incrv;
   10969       OMP_FOR_BODY (stmt) = body;
   10970       OMP_FOR_PRE_BODY (stmt) = pre_body;
   10971       OMP_FOR_CLAUSES (stmt) = clauses;
   10972 
   10973       SET_EXPR_LOCATION (stmt, locus);
   10974       return add_stmt (stmt);
   10975     }
   10976 
   10977   if (!orig_declv)
   10978     orig_declv = copy_node (declv);
   10979 
   10980   if (processing_template_decl)
   10981     orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
   10982 
   10983   for (i = 0; i < TREE_VEC_LENGTH (declv); )
   10984     {
   10985       decl = TREE_VEC_ELT (declv, i);
   10986       init = TREE_VEC_ELT (initv, i);
   10987       cond = TREE_VEC_ELT (condv, i);
   10988       incr = TREE_VEC_ELT (incrv, i);
   10989       if (orig_incr)
   10990 	TREE_VEC_ELT (orig_incr, i) = incr;
   10991       elocus = init_locv[i];
   10992 
   10993       if (!DECL_P (decl))
   10994 	{
   10995 	  error_at (elocus, "expected iteration declaration or initialization");
   10996 	  return NULL;
   10997 	}
   10998 
   10999       if (incr && TREE_CODE (incr) == MODOP_EXPR)
   11000 	{
   11001 	  if (orig_incr)
   11002 	    TREE_VEC_ELT (orig_incr, i) = incr;
   11003 	  incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
   11004 				       TREE_CODE (TREE_OPERAND (incr, 1)),
   11005 				       TREE_OPERAND (incr, 2),
   11006 				       tf_warning_or_error);
   11007 	}
   11008 
   11009       if (CLASS_TYPE_P (TREE_TYPE (decl)))
   11010 	{
   11011 	  if (code == OMP_SIMD)
   11012 	    {
   11013 	      error_at (elocus, "%<#pragma omp simd%> used with class "
   11014 				"iteration variable %qE", decl);
   11015 	      return NULL;
   11016 	    }
   11017 	  if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
   11018 					     initv, condv, incrv, &body,
   11019 					     &pre_body, clauses,
   11020 					     collapse, ordered))
   11021 	    return NULL;
   11022 	  continue;
   11023 	}
   11024 
   11025       if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
   11026 	  && !TYPE_PTR_P (TREE_TYPE (decl)))
   11027 	{
   11028 	  error_at (elocus, "invalid type for iteration variable %qE", decl);
   11029 	  return NULL;
   11030 	}
   11031 
   11032       if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
   11033 	init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
   11034 				     tf_warning_or_error);
   11035       else
   11036 	init = build2_loc (elocus, MODIFY_EXPR, void_type_node, decl, init);
   11037       if (decl == error_mark_node || init == error_mark_node)
   11038 	return NULL;
   11039 
   11040       TREE_VEC_ELT (declv, i) = decl;
   11041       TREE_VEC_ELT (initv, i) = init;
   11042       TREE_VEC_ELT (condv, i) = cond;
   11043       TREE_VEC_ELT (incrv, i) = incr;
   11044       i++;
   11045     }
   11046 
   11047   if (pre_body && IS_EMPTY_STMT (pre_body))
   11048     pre_body = NULL;
   11049 
   11050   omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
   11051 			      incrv, body, pre_body,
   11052 			      !processing_template_decl);
   11053 
   11054   /* Check for iterators appearing in lb, b or incr expressions.  */
   11055   if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
   11056     omp_for = NULL_TREE;
   11057 
   11058   if (omp_for == NULL)
   11059     return NULL;
   11060 
   11061   add_stmt (omp_for);
   11062 
   11063   for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
   11064     {
   11065       init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
   11066       decl = TREE_OPERAND (init, 0);
   11067       cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
   11068       incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
   11069 
   11070       if (!processing_template_decl)
   11071 	{
   11072 	  if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
   11073 	    {
   11074 	      tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
   11075 	      TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
   11076 		= fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   11077 	      t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
   11078 	      TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
   11079 		= fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   11080 	    }
   11081 	  else
   11082 	    {
   11083 	      tree t = TREE_OPERAND (init, 1);
   11084 	      TREE_OPERAND (init, 1)
   11085 		= fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   11086 	    }
   11087 	  if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
   11088 	    {
   11089 	      tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
   11090 	      TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
   11091 		= fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   11092 	      t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
   11093 	      TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
   11094 		= fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   11095 	    }
   11096 	  else
   11097 	    {
   11098 	      tree t = TREE_OPERAND (cond, 1);
   11099 	      TREE_OPERAND (cond, 1)
   11100 		= fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   11101 	    }
   11102 	}
   11103 
   11104       if (TREE_CODE (incr) != MODIFY_EXPR)
   11105 	continue;
   11106 
   11107       if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
   11108 	  && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
   11109 	  && !processing_template_decl)
   11110 	{
   11111 	  tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
   11112 	  if (TREE_SIDE_EFFECTS (t)
   11113 	      && t != decl
   11114 	      && (TREE_CODE (t) != NOP_EXPR
   11115 		  || TREE_OPERAND (t, 0) != decl))
   11116 	    TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
   11117 	      = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   11118 
   11119 	  t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
   11120 	  if (TREE_SIDE_EFFECTS (t)
   11121 	      && t != decl
   11122 	      && (TREE_CODE (t) != NOP_EXPR
   11123 		  || TREE_OPERAND (t, 0) != decl))
   11124 	    TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
   11125 	      = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   11126 	}
   11127 
   11128       if (orig_incr)
   11129 	TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
   11130     }
   11131   OMP_FOR_CLAUSES (omp_for) = clauses;
   11132 
   11133   /* For simd loops with non-static data member iterators, we could have added
   11134      OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP.  As we know the
   11135      step at this point, fill it in.  */
   11136   if (code == OMP_SIMD && !processing_template_decl
   11137       && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
   11138     for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
   11139 	 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
   11140       if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
   11141 	{
   11142 	  decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
   11143 	  gcc_assert (decl == OMP_CLAUSE_DECL (c));
   11144 	  incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
   11145 	  tree step, stept;
   11146 	  switch (TREE_CODE (incr))
   11147 	    {
   11148 	    case PREINCREMENT_EXPR:
   11149 	    case POSTINCREMENT_EXPR:
   11150 	      /* c_omp_for_incr_canonicalize_ptr() should have been
   11151 		 called to massage things appropriately.  */
   11152 	      gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
   11153 	      OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
   11154 	      break;
   11155 	    case PREDECREMENT_EXPR:
   11156 	    case POSTDECREMENT_EXPR:
   11157 	      /* c_omp_for_incr_canonicalize_ptr() should have been
   11158 		 called to massage things appropriately.  */
   11159 	      gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
   11160 	      OMP_CLAUSE_LINEAR_STEP (c)
   11161 		= build_int_cst (TREE_TYPE (decl), -1);
   11162 	      break;
   11163 	    case MODIFY_EXPR:
   11164 	      gcc_assert (TREE_OPERAND (incr, 0) == decl);
   11165 	      incr = TREE_OPERAND (incr, 1);
   11166 	      switch (TREE_CODE (incr))
   11167 		{
   11168 		case PLUS_EXPR:
   11169 		  if (TREE_OPERAND (incr, 1) == decl)
   11170 		    step = TREE_OPERAND (incr, 0);
   11171 		  else
   11172 		    step = TREE_OPERAND (incr, 1);
   11173 		  break;
   11174 		case MINUS_EXPR:
   11175 		case POINTER_PLUS_EXPR:
   11176 		  gcc_assert (TREE_OPERAND (incr, 0) == decl);
   11177 		  step = TREE_OPERAND (incr, 1);
   11178 		  break;
   11179 		default:
   11180 		  gcc_unreachable ();
   11181 		}
   11182 	      stept = TREE_TYPE (decl);
   11183 	      if (INDIRECT_TYPE_P (stept))
   11184 		stept = sizetype;
   11185 	      step = fold_convert (stept, step);
   11186 	      if (TREE_CODE (incr) == MINUS_EXPR)
   11187 		step = fold_build1 (NEGATE_EXPR, stept, step);
   11188 	      OMP_CLAUSE_LINEAR_STEP (c) = step;
   11189 	      break;
   11190 	    default:
   11191 	      gcc_unreachable ();
   11192 	    }
   11193 	}
   11194   /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
   11195      clauses, we need copy ctor for those rather than default ctor,
   11196      plus as for other lastprivates assignment op and dtor.  */
   11197   if (code == OMP_LOOP && !processing_template_decl)
   11198     for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
   11199       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
   11200 	  && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
   11201 	  && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
   11202 					 false, true, true, true))
   11203 	CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
   11204 
   11205   return omp_for;
   11206 }
   11207 
   11208 /* Code walker for finish_omp_for_block: extract binding of DP->var
   11209    from its current block and move it to a new BIND_EXPR DP->b
   11210    surrounding the body of DP->omp_for.  */
   11211 
   11212 struct fofb_data {
   11213   tree var;
   11214   tree b;
   11215   tree omp_for;
   11216 };
   11217 
   11218 static tree
   11219 finish_omp_for_block_walker (tree *tp, int *walk_subtrees, void *dp)
   11220 {
   11221   struct fofb_data *fofb = (struct fofb_data *)dp;
   11222   if (TREE_CODE (*tp) == BIND_EXPR)
   11223     for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
   11224       {
   11225 	if (*p == fofb->var)
   11226 	  {
   11227 	    *p = DECL_CHAIN (*p);
   11228 	    if (fofb->b == NULL_TREE)
   11229 	      {
   11230 		fofb->b = make_node (BLOCK);
   11231 		fofb->b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
   11232 			    OMP_FOR_BODY (fofb->omp_for), fofb->b);
   11233 		TREE_SIDE_EFFECTS (fofb->b) = 1;
   11234 		OMP_FOR_BODY (fofb->omp_for) = fofb->b;
   11235 	      }
   11236 	    DECL_CHAIN (fofb->var) = BIND_EXPR_VARS (fofb->b);
   11237 	    BIND_EXPR_VARS (fofb->b) = fofb->var;
   11238 	    BLOCK_VARS (BIND_EXPR_BLOCK (fofb->b)) = fofb->var;
   11239 	    BLOCK_VARS (BIND_EXPR_BLOCK (*tp)) = BIND_EXPR_VARS (*tp);
   11240 	    return *tp;
   11241 	  }
   11242       }
   11243   if (TREE_CODE (*tp) != BIND_EXPR && TREE_CODE (*tp) != STATEMENT_LIST)
   11244     *walk_subtrees = false;
   11245   return NULL_TREE;
   11246 }
   11247 
   11248 /* Fix up range for decls.  Those decls were pushed into BIND's
   11249    BIND_EXPR_VARS, or that of a nested BIND_EXPR inside its body,
   11250    and need to be moved into a new BIND_EXPR surrounding OMP_FOR's body
   11251    so that processing of combined loop directives can find them.  */
   11252 tree
   11253 finish_omp_for_block (tree bind, tree omp_for)
   11254 {
   11255   if (omp_for == NULL_TREE
   11256       || !OMP_FOR_ORIG_DECLS (omp_for)
   11257       || bind == NULL_TREE)
   11258     return bind;
   11259   struct fofb_data fofb;
   11260   fofb.b = NULL_TREE;
   11261   fofb.omp_for = omp_for;
   11262   for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
   11263     if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
   11264 	&& TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
   11265       {
   11266 	tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
   11267 	for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
   11268 	  {
   11269 	    fofb.var = TREE_VEC_ELT (v, j);
   11270 	    cp_walk_tree (&bind, finish_omp_for_block_walker,
   11271 			  (void *)&fofb, NULL);
   11272 	  }
   11273       }
   11274   return bind;
   11275 }
   11276 
   11277 void
   11278 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
   11279 		   tree lhs, tree rhs, tree v, tree lhs1, tree rhs1, tree r,
   11280 		   tree clauses, enum omp_memory_order mo, bool weak)
   11281 {
   11282   tree orig_lhs;
   11283   tree orig_rhs;
   11284   tree orig_v;
   11285   tree orig_lhs1;
   11286   tree orig_rhs1;
   11287   tree orig_r;
   11288   bool dependent_p;
   11289   tree stmt;
   11290 
   11291   orig_lhs = lhs;
   11292   orig_rhs = rhs;
   11293   orig_v = v;
   11294   orig_lhs1 = lhs1;
   11295   orig_rhs1 = rhs1;
   11296   orig_r = r;
   11297   dependent_p = false;
   11298   stmt = NULL_TREE;
   11299 
   11300   /* Even in a template, we can detect invalid uses of the atomic
   11301      pragma if neither LHS nor RHS is type-dependent.  */
   11302   if (processing_template_decl)
   11303     {
   11304       dependent_p = (type_dependent_expression_p (lhs)
   11305 		     || (rhs && type_dependent_expression_p (rhs))
   11306 		     || (v && type_dependent_expression_p (v))
   11307 		     || (lhs1 && type_dependent_expression_p (lhs1))
   11308 		     || (rhs1 && type_dependent_expression_p (rhs1))
   11309 		     || (r
   11310 			 && r != void_list_node
   11311 			 && type_dependent_expression_p (r)));
   11312       if (clauses)
   11313 	{
   11314 	  gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
   11315 		      && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
   11316 		      && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
   11317 	  if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
   11318 	      || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
   11319 	    dependent_p = true;
   11320 	}
   11321     }
   11322   if (!dependent_p)
   11323     {
   11324       bool swapped = false;
   11325       if (rhs1 && opcode != COND_EXPR && cp_tree_equal (lhs, rhs))
   11326 	{
   11327 	  std::swap (rhs, rhs1);
   11328 	  swapped = !commutative_tree_code (opcode);
   11329 	}
   11330       if (rhs1 && opcode != COND_EXPR && !cp_tree_equal (lhs, rhs1))
   11331 	{
   11332 	  if (code == OMP_ATOMIC)
   11333 	    error ("%<#pragma omp atomic update%> uses two different "
   11334 		   "expressions for memory");
   11335 	  else
   11336 	    error ("%<#pragma omp atomic capture%> uses two different "
   11337 		   "expressions for memory");
   11338 	  return;
   11339 	}
   11340       if (lhs1 && !cp_tree_equal (lhs, lhs1))
   11341 	{
   11342 	  if (code == OMP_ATOMIC)
   11343 	    error ("%<#pragma omp atomic update%> uses two different "
   11344 		   "expressions for memory");
   11345 	  else
   11346 	    error ("%<#pragma omp atomic capture%> uses two different "
   11347 		   "expressions for memory");
   11348 	  return;
   11349 	}
   11350       stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
   11351 				  v, lhs1, rhs1, r, swapped, mo, weak,
   11352 				  processing_template_decl != 0);
   11353       if (stmt == error_mark_node)
   11354 	return;
   11355     }
   11356   if (processing_template_decl)
   11357     {
   11358       if (code == OMP_ATOMIC_READ)
   11359 	{
   11360 	  stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
   11361 	  OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
   11362 	  stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
   11363 	}
   11364       else
   11365 	{
   11366 	  if (opcode == NOP_EXPR)
   11367 	    stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
   11368 	  else if (opcode == COND_EXPR)
   11369 	    {
   11370 	      stmt = build2 (EQ_EXPR, boolean_type_node, orig_lhs, orig_rhs);
   11371 	      if (orig_r)
   11372 		stmt = build2 (MODIFY_EXPR, boolean_type_node, orig_r,
   11373 			       stmt);
   11374 	      stmt = build3 (COND_EXPR, void_type_node, stmt, orig_rhs1,
   11375 			     orig_lhs);
   11376 	      orig_rhs1 = NULL_TREE;
   11377 	    }
   11378 	  else
   11379 	    stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
   11380 	  if (orig_rhs1)
   11381 	    stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
   11382 				     COMPOUND_EXPR, orig_rhs1, stmt);
   11383 	  if (code != OMP_ATOMIC)
   11384 	    {
   11385 	      stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
   11386 	      OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
   11387 	      OMP_ATOMIC_WEAK (stmt) = weak;
   11388 	      stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
   11389 	    }
   11390 	}
   11391       stmt = build2 (OMP_ATOMIC, void_type_node,
   11392 		     clauses ? clauses : integer_zero_node, stmt);
   11393       OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
   11394       OMP_ATOMIC_WEAK (stmt) = weak;
   11395       SET_EXPR_LOCATION (stmt, loc);
   11396     }
   11397 
   11398   /* Avoid -Wunused-value warnings here, the whole construct has side-effects
   11399      and even if it might be wrapped from fold-const.cc or c-omp.cc wrapped
   11400      in some tree that appears to be unused, the value is not unused.  */
   11401   warning_sentinel w (warn_unused_value);
   11402   finish_expr_stmt (stmt);
   11403 }
   11404 
   11405 void
   11406 finish_omp_barrier (void)
   11407 {
   11408   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
   11409   releasing_vec vec;
   11410   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
   11411   finish_expr_stmt (stmt);
   11412 }
   11413 
   11414 void
   11415 finish_omp_depobj (location_t loc, tree depobj,
   11416 		   enum omp_clause_depend_kind kind, tree clause)
   11417 {
   11418   if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
   11419     {
   11420       if (!lvalue_p (depobj))
   11421 	{
   11422 	  error_at (EXPR_LOC_OR_LOC (depobj, loc),
   11423 		    "%<depobj%> expression is not lvalue expression");
   11424 	  depobj = error_mark_node;
   11425 	}
   11426     }
   11427 
   11428   if (processing_template_decl)
   11429     {
   11430       if (clause == NULL_TREE)
   11431 	clause = build_int_cst (integer_type_node, kind);
   11432       add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
   11433       return;
   11434     }
   11435 
   11436   if (!error_operand_p (depobj))
   11437     {
   11438       tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
   11439       if (addr == error_mark_node)
   11440 	depobj = error_mark_node;
   11441       else
   11442 	depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
   11443 					tf_warning_or_error);
   11444     }
   11445 
   11446   c_finish_omp_depobj (loc, depobj, kind, clause);
   11447 }
   11448 
   11449 void
   11450 finish_omp_flush (int mo)
   11451 {
   11452   tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
   11453   releasing_vec vec;
   11454   if (mo != MEMMODEL_LAST && mo != MEMMODEL_SEQ_CST)
   11455     {
   11456       fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
   11457       vec->quick_push (build_int_cst (integer_type_node, mo));
   11458     }
   11459   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
   11460   finish_expr_stmt (stmt);
   11461 }
   11462 
   11463 void
   11464 finish_omp_taskwait (void)
   11465 {
   11466   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
   11467   releasing_vec vec;
   11468   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
   11469   finish_expr_stmt (stmt);
   11470 }
   11471 
   11472 void
   11473 finish_omp_taskyield (void)
   11474 {
   11475   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
   11476   releasing_vec vec;
   11477   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
   11478   finish_expr_stmt (stmt);
   11479 }
   11480 
   11481 void
   11482 finish_omp_cancel (tree clauses)
   11483 {
   11484   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
   11485   int mask = 0;
   11486   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
   11487     mask = 1;
   11488   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
   11489     mask = 2;
   11490   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
   11491     mask = 4;
   11492   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
   11493     mask = 8;
   11494   else
   11495     {
   11496       error ("%<#pragma omp cancel%> must specify one of "
   11497 	     "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
   11498       return;
   11499     }
   11500   releasing_vec vec;
   11501   tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
   11502   if (ifc != NULL_TREE)
   11503     {
   11504       if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
   11505 	  && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
   11506 	error_at (OMP_CLAUSE_LOCATION (ifc),
   11507 		  "expected %<cancel%> %<if%> clause modifier");
   11508       else
   11509 	{
   11510 	  tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
   11511 	  if (ifc2 != NULL_TREE)
   11512 	    {
   11513 	      gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
   11514 			  && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
   11515 			  && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
   11516 	      error_at (OMP_CLAUSE_LOCATION (ifc2),
   11517 			"expected %<cancel%> %<if%> clause modifier");
   11518 	    }
   11519 	}
   11520 
   11521       if (!processing_template_decl)
   11522 	ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
   11523       else
   11524 	ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
   11525 				 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
   11526 				 integer_zero_node, ERROR_MARK,
   11527 				 NULL_TREE, NULL, tf_warning_or_error);
   11528     }
   11529   else
   11530     ifc = boolean_true_node;
   11531   vec->quick_push (build_int_cst (integer_type_node, mask));
   11532   vec->quick_push (ifc);
   11533   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
   11534   finish_expr_stmt (stmt);
   11535 }
   11536 
   11537 void
   11538 finish_omp_cancellation_point (tree clauses)
   11539 {
   11540   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
   11541   int mask = 0;
   11542   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
   11543     mask = 1;
   11544   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
   11545     mask = 2;
   11546   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
   11547     mask = 4;
   11548   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
   11549     mask = 8;
   11550   else
   11551     {
   11552       error ("%<#pragma omp cancellation point%> must specify one of "
   11553 	     "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
   11554       return;
   11555     }
   11556   releasing_vec vec
   11557     = make_tree_vector_single (build_int_cst (integer_type_node, mask));
   11558   tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
   11559   finish_expr_stmt (stmt);
   11560 }
   11561 
   11562 /* Begin a __transaction_atomic or __transaction_relaxed statement.
   11564    If PCOMPOUND is non-null, this is for a function-transaction-block, and we
   11565    should create an extra compound stmt.  */
   11566 
   11567 tree
   11568 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
   11569 {
   11570   tree r;
   11571 
   11572   if (pcompound)
   11573     *pcompound = begin_compound_stmt (0);
   11574 
   11575   r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
   11576 
   11577   /* Only add the statement to the function if support enabled.  */
   11578   if (flag_tm)
   11579     add_stmt (r);
   11580   else
   11581     error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
   11582 		    ? G_("%<__transaction_relaxed%> without "
   11583 			 "transactional memory support enabled")
   11584 		    : G_("%<__transaction_atomic%> without "
   11585 			 "transactional memory support enabled")));
   11586 
   11587   TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
   11588   TREE_SIDE_EFFECTS (r) = 1;
   11589   return r;
   11590 }
   11591 
   11592 /* End a __transaction_atomic or __transaction_relaxed statement.
   11593    If COMPOUND_STMT is non-null, this is for a function-transaction-block,
   11594    and we should end the compound.  If NOEX is non-NULL, we wrap the body in
   11595    a MUST_NOT_THROW_EXPR with NOEX as condition.  */
   11596 
   11597 void
   11598 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
   11599 {
   11600   TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
   11601   TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
   11602   TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
   11603   TRANSACTION_EXPR_IS_STMT (stmt) = 1;
   11604 
   11605   /* noexcept specifications are not allowed for function transactions.  */
   11606   gcc_assert (!(noex && compound_stmt));
   11607   if (noex)
   11608     {
   11609       tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
   11610 					     noex);
   11611       protected_set_expr_location
   11612 	(body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
   11613       TREE_SIDE_EFFECTS (body) = 1;
   11614       TRANSACTION_EXPR_BODY (stmt) = body;
   11615     }
   11616 
   11617   if (compound_stmt)
   11618     finish_compound_stmt (compound_stmt);
   11619 }
   11620 
   11621 /* Build a __transaction_atomic or __transaction_relaxed expression.  If
   11622    NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
   11623    condition.  */
   11624 
   11625 tree
   11626 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
   11627 {
   11628   tree ret;
   11629   if (noex)
   11630     {
   11631       expr = build_must_not_throw_expr (expr, noex);
   11632       protected_set_expr_location (expr, loc);
   11633       TREE_SIDE_EFFECTS (expr) = 1;
   11634     }
   11635   ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
   11636   if (flags & TM_STMT_ATTR_RELAXED)
   11637 	TRANSACTION_EXPR_RELAXED (ret) = 1;
   11638   TREE_SIDE_EFFECTS (ret) = 1;
   11639   SET_EXPR_LOCATION (ret, loc);
   11640   return ret;
   11641 }
   11642 
   11643 void
   11645 init_cp_semantics (void)
   11646 {
   11647 }
   11648 
   11649 
   11651 /* Build a STATIC_ASSERT for a static assertion with the condition
   11652    CONDITION and the message text MESSAGE.  LOCATION is the location
   11653    of the static assertion in the source code.  When MEMBER_P, this
   11654    static assertion is a member of a class.  If SHOW_EXPR_P is true,
   11655    print the condition (because it was instantiation-dependent).  */
   11656 
   11657 void
   11658 finish_static_assert (tree condition, tree message, location_t location,
   11659 		      bool member_p, bool show_expr_p)
   11660 {
   11661   tsubst_flags_t complain = tf_warning_or_error;
   11662   tree message_sz = NULL_TREE, message_data = NULL_TREE;
   11663 
   11664   if (message == NULL_TREE
   11665       || message == error_mark_node
   11666       || condition == NULL_TREE
   11667       || condition == error_mark_node)
   11668     return;
   11669 
   11670   if (check_for_bare_parameter_packs (condition)
   11671       || check_for_bare_parameter_packs (message))
   11672     return;
   11673 
   11674   if (TREE_CODE (message) != STRING_CST
   11675       && !type_dependent_expression_p (message))
   11676     {
   11677       message_sz
   11678 	= finish_class_member_access_expr (message,
   11679 					   get_identifier ("size"),
   11680 					   false, complain);
   11681       if (message_sz != error_mark_node)
   11682 	message_data
   11683 	  = finish_class_member_access_expr (message,
   11684 					     get_identifier ("data"),
   11685 					     false, complain);
   11686       if (message_sz == error_mark_node || message_data == error_mark_node)
   11687 	{
   11688 	  error_at (location, "%<static_assert%> message must be a string "
   11689 			      "literal or object with %<size%> and "
   11690 			      "%<data%> members");
   11691 	  return;
   11692 	}
   11693       releasing_vec size_args, data_args;
   11694       message_sz = finish_call_expr (message_sz, &size_args, false, false,
   11695 				     complain);
   11696       message_data = finish_call_expr (message_data, &data_args, false, false,
   11697 				       complain);
   11698       if (message_sz == error_mark_node || message_data == error_mark_node)
   11699 	return;
   11700       message_sz = build_converted_constant_expr (size_type_node, message_sz,
   11701 						  complain);
   11702       if (message_sz == error_mark_node)
   11703 	{
   11704 	  error_at (location, "%<static_assert%> message %<size()%> "
   11705 			      "must be implicitly convertible to "
   11706 			      "%<std::size_t%>");
   11707 	  return;
   11708 	}
   11709       message_data = build_converted_constant_expr (const_string_type_node,
   11710 						    message_data, complain);
   11711       if (message_data == error_mark_node)
   11712 	{
   11713 	  error_at (location, "%<static_assert%> message %<data()%> "
   11714 			      "must be implicitly convertible to "
   11715 			      "%<const char*%>");
   11716 	  return;
   11717 	}
   11718     }
   11719 
   11720   /* Save the condition in case it was a concept check.  */
   11721   tree orig_condition = condition;
   11722 
   11723   if (instantiation_dependent_expression_p (condition)
   11724       || instantiation_dependent_expression_p (message))
   11725     {
   11726       /* We're in a template; build a STATIC_ASSERT and put it in
   11727          the right place. */
   11728     defer:
   11729       tree assertion = make_node (STATIC_ASSERT);
   11730       STATIC_ASSERT_CONDITION (assertion) = orig_condition;
   11731       STATIC_ASSERT_MESSAGE (assertion) = message;
   11732       STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
   11733 
   11734       if (member_p)
   11735         maybe_add_class_template_decl_list (current_class_type,
   11736                                             assertion,
   11737                                             /*friend_p=*/0);
   11738       else
   11739         add_stmt (assertion);
   11740 
   11741       return;
   11742     }
   11743 
   11744   /* Fold the expression and convert it to a boolean value. */
   11745   condition = contextual_conv_bool (condition, complain);
   11746   condition = fold_non_dependent_expr (condition, complain,
   11747 				       /*manifestly_const_eval=*/true);
   11748 
   11749   if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
   11750     /* Do nothing; the condition is satisfied. */
   11751     ;
   11752   else
   11753     {
   11754       iloc_sentinel ils (location);
   11755 
   11756       if (integer_zerop (condition))
   11757 	{
   11758 	  /* CWG2518: static_assert failure in a template is not IFNDR.  */
   11759 	  if (processing_template_decl)
   11760 	    goto defer;
   11761 
   11762 	  int len;
   11763 	  const char *msg = NULL;
   11764 	  char *buf = NULL;
   11765 	  if (message_sz && message_data)
   11766 	    {
   11767 	      tree msz = cxx_constant_value (message_sz, NULL_TREE, complain);
   11768 	      if (!tree_fits_uhwi_p (msz))
   11769 		{
   11770 		  error_at (location,
   11771 			    "%<static_assert%> message %<size()%> "
   11772 			    "must be a constant expression");
   11773 		  return;
   11774 		}
   11775 	      else if ((unsigned HOST_WIDE_INT) (int) tree_to_uhwi (msz)
   11776 		       != tree_to_uhwi (msz))
   11777 		{
   11778 		  error_at (location,
   11779 			    "%<static_assert%> message %<size()%> "
   11780 			    "%qE too large", msz);
   11781 		  return;
   11782 		}
   11783 	      len = tree_to_uhwi (msz);
   11784 	      tree data = maybe_constant_value (message_data, NULL_TREE,
   11785 						mce_true);
   11786 	      if (!reduced_constant_expression_p (data))
   11787 		data = NULL_TREE;
   11788 	      if (len)
   11789 		{
   11790 		  if (data)
   11791 		    msg = c_getstr (data);
   11792 		  if (msg == NULL)
   11793 		    buf = XNEWVEC (char, len);
   11794 		  for (int i = 0; i < len; ++i)
   11795 		    {
   11796 		      tree t = message_data;
   11797 		      if (i)
   11798 			t = build2 (POINTER_PLUS_EXPR,
   11799 				    TREE_TYPE (message_data), message_data,
   11800 				    size_int (i));
   11801 		      t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
   11802 		      tree t2 = cxx_constant_value (t, NULL_TREE, complain);
   11803 		      if (!tree_fits_shwi_p (t2))
   11804 			{
   11805 			  error_at (location,
   11806 				    "%<static_assert%> message %<data()[%d]%> "
   11807 				    "must be a constant expression", i);
   11808 			  XDELETEVEC (buf);
   11809 			  return;
   11810 			}
   11811 		      if (msg == NULL)
   11812 			buf[i] = tree_to_shwi (t2);
   11813 		      /* If c_getstr worked, just verify the first and
   11814 			 last characters using constant evaluation.  */
   11815 		      else if (len > 2 && i == 0)
   11816 			i = len - 2;
   11817 		    }
   11818 		  if (msg == NULL)
   11819 		    msg = buf;
   11820 		}
   11821 	      else if (!data)
   11822 		{
   11823 		  /* We don't have any function to test whether some
   11824 		     expression is a core constant expression.  So, instead
   11825 		     test whether (message.data (), 0) is a constant
   11826 		     expression.  */
   11827 		  data = build2 (COMPOUND_EXPR, integer_type_node,
   11828 				 message_data, integer_zero_node);
   11829 		  tree t = cxx_constant_value (data, NULL_TREE, complain);
   11830 		  if (!integer_zerop (t))
   11831 		    {
   11832 		      error_at (location,
   11833 				"%<static_assert%> message %<data()%> "
   11834 				"must be a core constant expression");
   11835 		      return;
   11836 		    }
   11837 		}
   11838 	    }
   11839 	  else
   11840 	    {
   11841 	      tree eltype = TREE_TYPE (TREE_TYPE (message));
   11842 	      int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (eltype));
   11843 	      msg = TREE_STRING_POINTER (message);
   11844 	      len = TREE_STRING_LENGTH (message) / sz - 1;
   11845 	    }
   11846 
   11847 	  /* See if we can find which clause was failing (for logical AND).  */
   11848 	  tree bad = find_failing_clause (NULL, orig_condition);
   11849 	  /* If not, or its location is unusable, fall back to the previous
   11850 	     location.  */
   11851 	  location_t cloc = cp_expr_loc_or_loc (bad, location);
   11852 
   11853 	  auto_diagnostic_group d;
   11854 
   11855 	  /* Report the error. */
   11856 	  if (len == 0)
   11857 	    error_at (cloc, "static assertion failed");
   11858 	  else
   11859 	    error_at (cloc, "static assertion failed: %.*s", len, msg);
   11860 
   11861 	  XDELETEVEC (buf);
   11862 
   11863 	  diagnose_failing_condition (bad, cloc, show_expr_p);
   11864 	}
   11865       else if (condition && condition != error_mark_node)
   11866 	{
   11867 	  error ("non-constant condition for static assertion");
   11868 	  if (require_rvalue_constant_expression (condition))
   11869 	    cxx_constant_value (condition);
   11870 	}
   11871     }
   11872 }
   11873 
   11874 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
   11876    suitable for use as a type-specifier.
   11877 
   11878    ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
   11879    id-expression or a class member access, FALSE when it was parsed as
   11880    a full expression.  */
   11881 
   11882 tree
   11883 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
   11884 		      tsubst_flags_t complain)
   11885 {
   11886   tree type = NULL_TREE;
   11887 
   11888   if (!expr || error_operand_p (expr))
   11889     return error_mark_node;
   11890 
   11891   if (TYPE_P (expr)
   11892       || TREE_CODE (expr) == TYPE_DECL
   11893       || (TREE_CODE (expr) == BIT_NOT_EXPR
   11894 	  && TYPE_P (TREE_OPERAND (expr, 0))))
   11895     {
   11896       if (complain & tf_error)
   11897 	error ("argument to %<decltype%> must be an expression");
   11898       return error_mark_node;
   11899     }
   11900 
   11901   /* decltype is an unevaluated context.  */
   11902   cp_unevaluated u;
   11903 
   11904   processing_template_decl_sentinel ptds (/*reset=*/false);
   11905 
   11906   /* Depending on the resolution of DR 1172, we may later need to distinguish
   11907      instantiation-dependent but not type-dependent expressions so that, say,
   11908      A<decltype(sizeof(T))>::U doesn't require 'typename'.  */
   11909   if (instantiation_dependent_uneval_expression_p (expr))
   11910     {
   11911     dependent:
   11912       type = cxx_make_type (DECLTYPE_TYPE);
   11913       DECLTYPE_TYPE_EXPR (type) = expr;
   11914       DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
   11915         = id_expression_or_member_access_p;
   11916       SET_TYPE_STRUCTURAL_EQUALITY (type);
   11917 
   11918       return type;
   11919     }
   11920   else if (processing_template_decl)
   11921     {
   11922       expr = instantiate_non_dependent_expr (expr, complain|tf_decltype);
   11923       if (expr == error_mark_node)
   11924 	return error_mark_node;
   11925       /* Keep processing_template_decl cleared for the rest of the function
   11926 	 (for sake of the call to lvalue_kind below, which handles templated
   11927 	 and non-templated COND_EXPR differently).  */
   11928       processing_template_decl = 0;
   11929     }
   11930 
   11931   /* The type denoted by decltype(e) is defined as follows:  */
   11932 
   11933   expr = resolve_nondeduced_context (expr, complain);
   11934   if (!mark_single_function (expr, complain))
   11935     return error_mark_node;
   11936 
   11937   if (invalid_nonstatic_memfn_p (input_location, expr, complain))
   11938     return error_mark_node;
   11939 
   11940   if (type_unknown_p (expr))
   11941     {
   11942       if (complain & tf_error)
   11943 	error ("%<decltype%> cannot resolve address of overloaded function");
   11944       return error_mark_node;
   11945     }
   11946 
   11947   if (id_expression_or_member_access_p)
   11948     {
   11949       /* If e is an id-expression or a class member access (5.2.5
   11950          [expr.ref]), decltype(e) is defined as the type of the entity
   11951          named by e. If there is no such entity, or e names a set of
   11952          overloaded functions, the program is ill-formed.  */
   11953       if (identifier_p (expr))
   11954         expr = lookup_name (expr);
   11955 
   11956       if (INDIRECT_REF_P (expr)
   11957 	  || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
   11958         /* This can happen when the expression is, e.g., "a.b". Just
   11959            look at the underlying operand.  */
   11960         expr = TREE_OPERAND (expr, 0);
   11961 
   11962       if (TREE_CODE (expr) == OFFSET_REF
   11963           || TREE_CODE (expr) == MEMBER_REF
   11964 	  || TREE_CODE (expr) == SCOPE_REF)
   11965         /* We're only interested in the field itself. If it is a
   11966            BASELINK, we will need to see through it in the next
   11967            step.  */
   11968         expr = TREE_OPERAND (expr, 1);
   11969 
   11970       if (BASELINK_P (expr))
   11971         /* See through BASELINK nodes to the underlying function.  */
   11972         expr = BASELINK_FUNCTIONS (expr);
   11973 
   11974       /* decltype of a decomposition name drops references in the tuple case
   11975 	 (unlike decltype of a normal variable) and keeps cv-qualifiers from
   11976 	 the containing object in the other cases (unlike decltype of a member
   11977 	 access expression).  */
   11978       if (DECL_DECOMPOSITION_P (expr))
   11979 	{
   11980 	  if (ptds.saved)
   11981 	    {
   11982 	      gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (expr));
   11983 	      /* DECL_HAS_VALUE_EXPR_P is always set if
   11984 		 processing_template_decl.  If lookup_decomp_type
   11985 		 returns non-NULL, it is the tuple case.  */
   11986 	      if (tree ret = lookup_decomp_type (expr))
   11987 		return ret;
   11988 	    }
   11989 	  if (DECL_HAS_VALUE_EXPR_P (expr))
   11990 	    /* Expr is an array or struct subobject proxy, handle
   11991 	       bit-fields properly.  */
   11992 	    return unlowered_expr_type (expr);
   11993 	  else
   11994 	    /* Expr is a reference variable for the tuple case.  */
   11995 	    return lookup_decomp_type (expr);
   11996 	}
   11997 
   11998       switch (TREE_CODE (expr))
   11999         {
   12000         case FIELD_DECL:
   12001           if (DECL_BIT_FIELD_TYPE (expr))
   12002             {
   12003               type = DECL_BIT_FIELD_TYPE (expr);
   12004               break;
   12005             }
   12006           /* Fall through for fields that aren't bitfields.  */
   12007 	  gcc_fallthrough ();
   12008 
   12009         case VAR_DECL:
   12010 	  if (is_capture_proxy (expr))
   12011 	    {
   12012 	      if (is_normal_capture_proxy (expr))
   12013 		{
   12014 		  expr = DECL_CAPTURED_VARIABLE (expr);
   12015 		  type = TREE_TYPE (expr);
   12016 		}
   12017 	      else
   12018 		{
   12019 		  expr = DECL_VALUE_EXPR (expr);
   12020 		  gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
   12021 		  expr = TREE_OPERAND (expr, 1);
   12022 		  type = TREE_TYPE (expr);
   12023 		}
   12024 	      break;
   12025 	    }
   12026 	  /* Fall through for variables that aren't capture proxies.  */
   12027 	  gcc_fallthrough ();
   12028 
   12029 	case FUNCTION_DECL:
   12030         case CONST_DECL:
   12031         case PARM_DECL:
   12032         case RESULT_DECL:
   12033         case TEMPLATE_PARM_INDEX:
   12034 	  expr = mark_type_use (expr);
   12035           type = TREE_TYPE (expr);
   12036 	  if (VAR_P (expr) && DECL_NTTP_OBJECT_P (expr))
   12037 	    {
   12038 	      /* decltype of an NTTP object is the type of the template
   12039 		 parameter, which is the object type modulo cv-quals.  */
   12040 	      int quals = cp_type_quals (type);
   12041 	      gcc_checking_assert (quals & TYPE_QUAL_CONST);
   12042 	      type = cv_unqualified (type);
   12043 	    }
   12044           break;
   12045 
   12046         case ERROR_MARK:
   12047           type = error_mark_node;
   12048           break;
   12049 
   12050         case COMPONENT_REF:
   12051 	case COMPOUND_EXPR:
   12052 	  mark_type_use (expr);
   12053           type = is_bitfield_expr_with_lowered_type (expr);
   12054           if (!type)
   12055             type = TREE_TYPE (TREE_OPERAND (expr, 1));
   12056           break;
   12057 
   12058         case BIT_FIELD_REF:
   12059           gcc_unreachable ();
   12060 
   12061         case INTEGER_CST:
   12062 	case PTRMEM_CST:
   12063           /* We can get here when the id-expression refers to an
   12064              enumerator or non-type template parameter.  */
   12065           type = TREE_TYPE (expr);
   12066           break;
   12067 
   12068         default:
   12069 	  /* Handle instantiated template non-type arguments.  */
   12070 	  type = TREE_TYPE (expr);
   12071           break;
   12072         }
   12073     }
   12074   else
   12075     {
   12076       if (outer_automatic_var_p (STRIP_REFERENCE_REF (expr))
   12077 	  && current_function_decl
   12078 	  && LAMBDA_FUNCTION_P (current_function_decl))
   12079 	{
   12080 	  /* [expr.prim.id.unqual]/3: If naming the entity from outside of an
   12081 	     unevaluated operand within S would refer to an entity captured by
   12082 	     copy in some intervening lambda-expression, then let E be the
   12083 	     innermost such lambda-expression.
   12084 
   12085 	     If there is such a lambda-expression and if P is in E's function
   12086 	     parameter scope but not its parameter-declaration-clause, then the
   12087 	     type of the expression is the type of a class member access
   12088 	     expression naming the non-static data member that would be declared
   12089 	     for such a capture in the object parameter of the function call
   12090 	     operator of E."  */
   12091 	  /* FIXME: This transformation needs to happen for all uses of an outer
   12092 	     local variable inside decltype, not just decltype((x)) (PR83167).
   12093 	     And we don't handle nested lambdas properly, where we need to
   12094 	     consider the outer lambdas as well (PR112926). */
   12095 	  tree decl = STRIP_REFERENCE_REF (expr);
   12096 	  tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
   12097 	  tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
   12098 				  LOOK_want::HIDDEN_LAMBDA);
   12099 
   12100 	  if (cap && is_capture_proxy (cap))
   12101 	    type = TREE_TYPE (cap);
   12102 	  else if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_COPY)
   12103 	    {
   12104 	      type = TREE_TYPE (decl);
   12105 	      if (TYPE_REF_P (type)
   12106 		  && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
   12107 		type = TREE_TYPE (type);
   12108 	    }
   12109 
   12110 	  if (type && !TYPE_REF_P (type))
   12111 	    {
   12112 	      tree obtype = TREE_TYPE (DECL_ARGUMENTS (current_function_decl));
   12113 	      if (WILDCARD_TYPE_P (non_reference (obtype)))
   12114 		/* We don't know what the eventual obtype quals will be.  */
   12115 		goto dependent;
   12116 	      auto direct_type = [](tree t){
   12117 		  if (INDIRECT_TYPE_P (t))
   12118 		    return TREE_TYPE (t);
   12119 		  return t;
   12120 	       };
   12121 	      int const quals = cp_type_quals (type)
   12122 			      | cp_type_quals (direct_type (obtype));
   12123 	      type = cp_build_qualified_type (type, quals);
   12124 	      type = build_reference_type (type);
   12125 	    }
   12126 	}
   12127       else if (error_operand_p (expr))
   12128 	type = error_mark_node;
   12129       else if (expr == current_class_ptr)
   12130 	/* If the expression is just "this", we want the
   12131 	   cv-unqualified pointer for the "this" type.  */
   12132 	type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
   12133 
   12134       if (!type)
   12135 	{
   12136 	  /* Otherwise, where T is the type of e, if e is an lvalue,
   12137 	     decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
   12138 	  cp_lvalue_kind clk = lvalue_kind (expr);
   12139 	  type = unlowered_expr_type (expr);
   12140 	  gcc_assert (!TYPE_REF_P (type));
   12141 
   12142 	  /* For vector types, pick a non-opaque variant.  */
   12143 	  if (VECTOR_TYPE_P (type))
   12144 	    type = strip_typedefs (type);
   12145 
   12146 	  if (clk != clk_none && !(clk & clk_class))
   12147 	    type = cp_build_reference_type (type, (clk & clk_rvalueref));
   12148 	}
   12149     }
   12150 
   12151   return type;
   12152 }
   12153 
   12154 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
   12155    __has_nothrow_copy, depending on assign_p.  Returns true iff all
   12156    the copy {ctor,assign} fns are nothrow.  */
   12157 
   12158 static bool
   12159 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
   12160 {
   12161   tree fns = NULL_TREE;
   12162 
   12163   if (assign_p || TYPE_HAS_COPY_CTOR (type))
   12164     fns = get_class_binding (type, assign_p ? assign_op_identifier
   12165 			     : ctor_identifier);
   12166 
   12167   bool saw_copy = false;
   12168   for (ovl_iterator iter (fns); iter; ++iter)
   12169     {
   12170       tree fn = *iter;
   12171 
   12172       if (copy_fn_p (fn) > 0)
   12173 	{
   12174 	  saw_copy = true;
   12175 	  if (!maybe_instantiate_noexcept (fn)
   12176 	      || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
   12177 	    return false;
   12178 	}
   12179     }
   12180 
   12181   return saw_copy;
   12182 }
   12183 
   12184 /* Return true if DERIVED is pointer interconvertible base of BASE.  */
   12185 
   12186 static bool
   12187 pointer_interconvertible_base_of_p (tree base, tree derived)
   12188 {
   12189   if (base == error_mark_node || derived == error_mark_node)
   12190     return false;
   12191   base = TYPE_MAIN_VARIANT (base);
   12192   derived = TYPE_MAIN_VARIANT (derived);
   12193   if (!NON_UNION_CLASS_TYPE_P (base)
   12194       || !NON_UNION_CLASS_TYPE_P (derived))
   12195     return false;
   12196 
   12197   if (same_type_p (base, derived))
   12198     return true;
   12199 
   12200   if (!std_layout_type_p (derived))
   12201     return false;
   12202 
   12203   return uniquely_derived_from_p (base, derived);
   12204 }
   12205 
   12206 /* Helper function for fold_builtin_is_pointer_inverconvertible_with_class,
   12207    return true if MEMBERTYPE is the type of the first non-static data member
   12208    of TYPE or for unions of any members.  */
   12209 static bool
   12210 first_nonstatic_data_member_p (tree type, tree membertype)
   12211 {
   12212   for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
   12213     {
   12214       if (TREE_CODE (field) != FIELD_DECL)
   12215 	continue;
   12216       if (DECL_FIELD_IS_BASE (field) && is_empty_field (field))
   12217 	continue;
   12218       if (DECL_FIELD_IS_BASE (field))
   12219 	return first_nonstatic_data_member_p (TREE_TYPE (field), membertype);
   12220       if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
   12221 	{
   12222 	  if ((TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
   12223 	       || std_layout_type_p (TREE_TYPE (field)))
   12224 	      && first_nonstatic_data_member_p (TREE_TYPE (field), membertype))
   12225 	    return true;
   12226 	}
   12227       else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
   12228 							  membertype))
   12229 	return true;
   12230       if (TREE_CODE (type) != UNION_TYPE)
   12231 	return false;
   12232     }
   12233   return false;
   12234 }
   12235 
   12236 /* Fold __builtin_is_pointer_interconvertible_with_class call.  */
   12237 
   12238 tree
   12239 fold_builtin_is_pointer_inverconvertible_with_class (location_t loc, int nargs,
   12240 						     tree *args)
   12241 {
   12242   /* Unless users call the builtin directly, the following 3 checks should be
   12243      ensured from std::is_pointer_interconvertible_with_class function
   12244      template.  */
   12245   if (nargs != 1)
   12246     {
   12247       error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
   12248 		     "needs a single argument");
   12249       return boolean_false_node;
   12250     }
   12251   tree arg = args[0];
   12252   if (error_operand_p (arg))
   12253     return boolean_false_node;
   12254   if (!TYPE_PTRMEM_P (TREE_TYPE (arg)))
   12255     {
   12256       error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
   12257 		     "argument is not pointer to member");
   12258       return boolean_false_node;
   12259     }
   12260 
   12261   if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg)))
   12262     return boolean_false_node;
   12263 
   12264   tree membertype = TREE_TYPE (TREE_TYPE (arg));
   12265   tree basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
   12266   if (!complete_type_or_else (basetype, NULL_TREE))
   12267     return boolean_false_node;
   12268 
   12269   if (TREE_CODE (basetype) != UNION_TYPE
   12270       && !std_layout_type_p (basetype))
   12271     return boolean_false_node;
   12272 
   12273   if (!first_nonstatic_data_member_p (basetype, membertype))
   12274     return boolean_false_node;
   12275 
   12276   if (TREE_CODE (arg) == PTRMEM_CST)
   12277     arg = cplus_expand_constant (arg);
   12278 
   12279   if (integer_nonzerop (arg))
   12280     return boolean_false_node;
   12281   if (integer_zerop (arg))
   12282     return boolean_true_node;
   12283 
   12284   return fold_build2 (EQ_EXPR, boolean_type_node, arg,
   12285 		      build_zero_cst (TREE_TYPE (arg)));
   12286 }
   12287 
   12288 /* Helper function for is_corresponding_member_aggr.  Return true if
   12289    MEMBERTYPE pointer-to-data-member ARG can be found in anonymous
   12290    union or structure BASETYPE.  */
   12291 
   12292 static bool
   12293 is_corresponding_member_union (tree basetype, tree membertype, tree arg)
   12294 {
   12295   for (tree field = TYPE_FIELDS (basetype); field; field = DECL_CHAIN (field))
   12296     if (TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD_TYPE (field))
   12297       continue;
   12298     else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
   12299 							membertype))
   12300       {
   12301 	if (TREE_CODE (arg) != INTEGER_CST
   12302 	    || tree_int_cst_equal (arg, byte_position (field)))
   12303 	  return true;
   12304       }
   12305     else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
   12306       {
   12307 	tree narg = arg;
   12308 	if (TREE_CODE (basetype) != UNION_TYPE
   12309 	    && TREE_CODE (narg) == INTEGER_CST)
   12310 	  narg = size_binop (MINUS_EXPR, arg, byte_position (field));
   12311 	if (is_corresponding_member_union (TREE_TYPE (field),
   12312 					   membertype, narg))
   12313 	  return true;
   12314       }
   12315   return false;
   12316 }
   12317 
   12318 /* Helper function for fold_builtin_is_corresponding_member call.
   12319    Return boolean_false_node if MEMBERTYPE1 BASETYPE1::*ARG1 and
   12320    MEMBERTYPE2 BASETYPE2::*ARG2 aren't corresponding members,
   12321    boolean_true_node if they are corresponding members, or for
   12322    non-constant ARG2 the highest member offset for corresponding
   12323    members.  */
   12324 
   12325 static tree
   12326 is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
   12327 			      tree arg1, tree basetype2, tree membertype2,
   12328 			      tree arg2)
   12329 {
   12330   tree field1 = TYPE_FIELDS (basetype1);
   12331   tree field2 = TYPE_FIELDS (basetype2);
   12332   tree ret = boolean_false_node;
   12333   while (1)
   12334     {
   12335       bool r = next_common_initial_sequence (field1, field2);
   12336       if (field1 == NULL_TREE || field2 == NULL_TREE)
   12337 	break;
   12338       if (r
   12339 	  && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field1),
   12340 							membertype1)
   12341 	  && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field2),
   12342 							membertype2))
   12343 	{
   12344 	  tree pos = byte_position (field1);
   12345 	  if (TREE_CODE (arg1) == INTEGER_CST
   12346 	      && tree_int_cst_equal (arg1, pos))
   12347 	    {
   12348 	      if (TREE_CODE (arg2) == INTEGER_CST)
   12349 		return boolean_true_node;
   12350 	      return pos;
   12351 	    }
   12352 	  else if (TREE_CODE (arg1) != INTEGER_CST)
   12353 	    ret = pos;
   12354 	}
   12355       else if (ANON_AGGR_TYPE_P (TREE_TYPE (field1))
   12356 	       && ANON_AGGR_TYPE_P (TREE_TYPE (field2)))
   12357 	{
   12358 	  if ((!lookup_attribute ("no_unique_address",
   12359 				  DECL_ATTRIBUTES (field1)))
   12360 	      != !lookup_attribute ("no_unique_address",
   12361 				    DECL_ATTRIBUTES (field2)))
   12362 	    break;
   12363 	  if (!tree_int_cst_equal (bit_position (field1),
   12364 				   bit_position (field2)))
   12365 	    break;
   12366 	  bool overlap = true;
   12367 	  tree pos = byte_position (field1);
   12368 	  if (TREE_CODE (arg1) == INTEGER_CST)
   12369 	    {
   12370 	      tree off1 = fold_convert (sizetype, arg1);
   12371 	      tree sz1 = TYPE_SIZE_UNIT (TREE_TYPE (field1));
   12372 	      if (tree_int_cst_lt (off1, pos)
   12373 		  || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz1), off1))
   12374 		overlap = false;
   12375 	    }
   12376 	  if (TREE_CODE (arg2) == INTEGER_CST)
   12377 	    {
   12378 	      tree off2 = fold_convert (sizetype, arg2);
   12379 	      tree sz2 = TYPE_SIZE_UNIT (TREE_TYPE (field2));
   12380 	      if (tree_int_cst_lt (off2, pos)
   12381 		  || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz2), off2))
   12382 		overlap = false;
   12383 	    }
   12384 	  if (overlap
   12385 	      && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field1))
   12386 	      && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field2)))
   12387 	    {
   12388 	      tree narg1 = arg1;
   12389 	      if (TREE_CODE (arg1) == INTEGER_CST)
   12390 		narg1 = size_binop (MINUS_EXPR,
   12391 				    fold_convert (sizetype, arg1), pos);
   12392 	      tree narg2 = arg2;
   12393 	      if (TREE_CODE (arg2) == INTEGER_CST)
   12394 		narg2 = size_binop (MINUS_EXPR,
   12395 				    fold_convert (sizetype, arg2), pos);
   12396 	      tree t1 = TREE_TYPE (field1);
   12397 	      tree t2 = TREE_TYPE (field2);
   12398 	      tree nret = is_corresponding_member_aggr (loc, t1, membertype1,
   12399 							narg1, t2, membertype2,
   12400 							narg2);
   12401 	      if (nret != boolean_false_node)
   12402 		{
   12403 		  if (nret == boolean_true_node)
   12404 		    return nret;
   12405 		  if (TREE_CODE (arg1) == INTEGER_CST)
   12406 		    return size_binop (PLUS_EXPR, nret, pos);
   12407 		  ret = size_binop (PLUS_EXPR, nret, pos);
   12408 		}
   12409 	    }
   12410 	  else if (overlap
   12411 		   && TREE_CODE (TREE_TYPE (field1)) == UNION_TYPE
   12412 		   && TREE_CODE (TREE_TYPE (field2)) == UNION_TYPE)
   12413 	    {
   12414 	      tree narg1 = arg1;
   12415 	      if (TREE_CODE (arg1) == INTEGER_CST)
   12416 		narg1 = size_binop (MINUS_EXPR,
   12417 				    fold_convert (sizetype, arg1), pos);
   12418 	      tree narg2 = arg2;
   12419 	      if (TREE_CODE (arg2) == INTEGER_CST)
   12420 		narg2 = size_binop (MINUS_EXPR,
   12421 				    fold_convert (sizetype, arg2), pos);
   12422 	      if (is_corresponding_member_union (TREE_TYPE (field1),
   12423 						 membertype1, narg1)
   12424 		  && is_corresponding_member_union (TREE_TYPE (field2),
   12425 						    membertype2, narg2))
   12426 		{
   12427 		  sorry_at (loc, "%<__builtin_is_corresponding_member%> "
   12428 				 "not well defined for anonymous unions");
   12429 		  return boolean_false_node;
   12430 		}
   12431 	    }
   12432 	}
   12433       if (!r)
   12434 	break;
   12435       field1 = DECL_CHAIN (field1);
   12436       field2 = DECL_CHAIN (field2);
   12437     }
   12438   return ret;
   12439 }
   12440 
   12441 /* Fold __builtin_is_corresponding_member call.  */
   12442 
   12443 tree
   12444 fold_builtin_is_corresponding_member (location_t loc, int nargs,
   12445 				      tree *args)
   12446 {
   12447   /* Unless users call the builtin directly, the following 3 checks should be
   12448      ensured from std::is_corresponding_member function template.  */
   12449   if (nargs != 2)
   12450     {
   12451       error_at (loc, "%<__builtin_is_corresponding_member%> "
   12452 		     "needs two arguments");
   12453       return boolean_false_node;
   12454     }
   12455   tree arg1 = args[0];
   12456   tree arg2 = args[1];
   12457   if (error_operand_p (arg1) || error_operand_p (arg2))
   12458     return boolean_false_node;
   12459   if (!TYPE_PTRMEM_P (TREE_TYPE (arg1))
   12460       || !TYPE_PTRMEM_P (TREE_TYPE (arg2)))
   12461     {
   12462       error_at (loc, "%<__builtin_is_corresponding_member%> "
   12463 		     "argument is not pointer to member");
   12464       return boolean_false_node;
   12465     }
   12466 
   12467   if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg1))
   12468       || !TYPE_PTRDATAMEM_P (TREE_TYPE (arg2)))
   12469     return boolean_false_node;
   12470 
   12471   tree membertype1 = TREE_TYPE (TREE_TYPE (arg1));
   12472   tree basetype1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg1));
   12473   if (!complete_type_or_else (basetype1, NULL_TREE))
   12474     return boolean_false_node;
   12475 
   12476   tree membertype2 = TREE_TYPE (TREE_TYPE (arg2));
   12477   tree basetype2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg2));
   12478   if (!complete_type_or_else (basetype2, NULL_TREE))
   12479     return boolean_false_node;
   12480 
   12481   if (!NON_UNION_CLASS_TYPE_P (basetype1)
   12482       || !NON_UNION_CLASS_TYPE_P (basetype2)
   12483       || !std_layout_type_p (basetype1)
   12484       || !std_layout_type_p (basetype2))
   12485     return boolean_false_node;
   12486 
   12487   /* If the member types aren't layout compatible, then they
   12488      can't be corresponding members.  */
   12489   if (!layout_compatible_type_p (membertype1, membertype2))
   12490     return boolean_false_node;
   12491 
   12492   if (TREE_CODE (arg1) == PTRMEM_CST)
   12493     arg1 = cplus_expand_constant (arg1);
   12494   if (TREE_CODE (arg2) == PTRMEM_CST)
   12495     arg2 = cplus_expand_constant (arg2);
   12496 
   12497   if (null_member_pointer_value_p (arg1)
   12498       || null_member_pointer_value_p (arg2))
   12499     return boolean_false_node;
   12500 
   12501   if (TREE_CODE (arg1) == INTEGER_CST
   12502       && TREE_CODE (arg2) == INTEGER_CST
   12503       && !tree_int_cst_equal (arg1, arg2))
   12504     return boolean_false_node;
   12505 
   12506   if (TREE_CODE (arg2) == INTEGER_CST
   12507       && TREE_CODE (arg1) != INTEGER_CST)
   12508     {
   12509       std::swap (arg1, arg2);
   12510       std::swap (membertype1, membertype2);
   12511       std::swap (basetype1, basetype2);
   12512     }
   12513 
   12514   tree ret = is_corresponding_member_aggr (loc, basetype1, membertype1, arg1,
   12515 					   basetype2, membertype2, arg2);
   12516   if (TREE_TYPE (ret) == boolean_type_node)
   12517     return ret;
   12518   /* If both arg1 and arg2 are INTEGER_CSTs, is_corresponding_member_aggr
   12519      already returns boolean_{true,false}_node whether those particular
   12520      members are corresponding members or not.  Otherwise, if only
   12521      one of them is INTEGER_CST (canonicalized to first being INTEGER_CST
   12522      above), it returns boolean_false_node if it is certainly not a
   12523      corresponding member and otherwise we need to do a runtime check that
   12524      those two OFFSET_TYPE offsets are equal.
   12525      If neither of the operands is INTEGER_CST, is_corresponding_member_aggr
   12526      returns the largest offset at which the members would be corresponding
   12527      members, so perform arg1 <= ret && arg1 == arg2 runtime check.  */
   12528   gcc_assert (TREE_CODE (arg2) != INTEGER_CST);
   12529   if (TREE_CODE (arg1) == INTEGER_CST)
   12530     return fold_build2 (EQ_EXPR, boolean_type_node, arg1,
   12531 			fold_convert (TREE_TYPE (arg1), arg2));
   12532   ret = fold_build2 (LE_EXPR, boolean_type_node,
   12533 		     fold_convert (pointer_sized_int_node, arg1),
   12534 		     fold_convert (pointer_sized_int_node, ret));
   12535   return fold_build2 (TRUTH_AND_EXPR, boolean_type_node, ret,
   12536 		      fold_build2 (EQ_EXPR, boolean_type_node, arg1,
   12537 				   fold_convert (TREE_TYPE (arg1), arg2)));
   12538 }
   12539 
   12540 /* Actually evaluates the trait.  */
   12541 
   12542 static bool
   12543 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
   12544 {
   12545   enum tree_code type_code1;
   12546   tree t;
   12547 
   12548   type_code1 = TREE_CODE (type1);
   12549 
   12550   switch (kind)
   12551     {
   12552     case CPTK_HAS_NOTHROW_ASSIGN:
   12553       type1 = strip_array_types (type1);
   12554       return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
   12555 	      && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
   12556 		  || (CLASS_TYPE_P (type1)
   12557 		      && classtype_has_nothrow_assign_or_copy_p (type1,
   12558 								 true))));
   12559 
   12560     case CPTK_HAS_NOTHROW_CONSTRUCTOR:
   12561       type1 = strip_array_types (type1);
   12562       return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
   12563 	      || (CLASS_TYPE_P (type1)
   12564 		  && (t = locate_ctor (type1))
   12565 		  && maybe_instantiate_noexcept (t)
   12566 		  && TYPE_NOTHROW_P (TREE_TYPE (t))));
   12567 
   12568     case CPTK_HAS_NOTHROW_COPY:
   12569       type1 = strip_array_types (type1);
   12570       return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
   12571 	      || (CLASS_TYPE_P (type1)
   12572 		  && classtype_has_nothrow_assign_or_copy_p (type1, false)));
   12573 
   12574     case CPTK_HAS_TRIVIAL_ASSIGN:
   12575       /* ??? The standard seems to be missing the "or array of such a class
   12576 	 type" wording for this trait.  */
   12577       type1 = strip_array_types (type1);
   12578       return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
   12579 	      && (trivial_type_p (type1)
   12580 		    || (CLASS_TYPE_P (type1)
   12581 			&& TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
   12582 
   12583     case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
   12584       type1 = strip_array_types (type1);
   12585       return (trivial_type_p (type1)
   12586 	      || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
   12587 
   12588     case CPTK_HAS_TRIVIAL_COPY:
   12589       /* ??? The standard seems to be missing the "or array of such a class
   12590 	 type" wording for this trait.  */
   12591       type1 = strip_array_types (type1);
   12592       return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
   12593 	      || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
   12594 
   12595     case CPTK_HAS_TRIVIAL_DESTRUCTOR:
   12596       type1 = strip_array_types (type1);
   12597       return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
   12598 	      || (CLASS_TYPE_P (type1)
   12599 		  && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
   12600 
   12601     case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
   12602       return type_has_unique_obj_representations (type1);
   12603 
   12604     case CPTK_HAS_VIRTUAL_DESTRUCTOR:
   12605       return type_has_virtual_destructor (type1);
   12606 
   12607     case CPTK_IS_ABSTRACT:
   12608       return ABSTRACT_CLASS_TYPE_P (type1);
   12609 
   12610     case CPTK_IS_AGGREGATE:
   12611       return CP_AGGREGATE_TYPE_P (type1);
   12612 
   12613     case CPTK_IS_ARRAY:
   12614       return (type_code1 == ARRAY_TYPE
   12615 	      /* We don't want to report T[0] as being an array type.
   12616 		 This is for compatibility with an implementation of
   12617 		 std::is_array by template argument deduction, because
   12618 		 compute_array_index_type_loc rejects a zero-size array
   12619 		 in SFINAE context.  */
   12620 	      && !(TYPE_SIZE (type1) && integer_zerop (TYPE_SIZE (type1))));
   12621 
   12622     case CPTK_IS_ASSIGNABLE:
   12623       return is_xible (MODIFY_EXPR, type1, type2);
   12624 
   12625     case CPTK_IS_BASE_OF:
   12626       return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
   12627 	      && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
   12628 		  || DERIVED_FROM_P (type1, type2)));
   12629 
   12630     case CPTK_IS_BOUNDED_ARRAY:
   12631       return (type_code1 == ARRAY_TYPE
   12632 	      && TYPE_DOMAIN (type1)
   12633 	      /* We don't want to report T[0] as being a bounded array type.
   12634 		 This is for compatibility with an implementation of
   12635 		 std::is_bounded_array by template argument deduction, because
   12636 		 compute_array_index_type_loc rejects a zero-size array
   12637 		 in SFINAE context.  */
   12638 	      && !(TYPE_SIZE (type1) && integer_zerop (TYPE_SIZE (type1))));
   12639 
   12640     case CPTK_IS_CLASS:
   12641       return NON_UNION_CLASS_TYPE_P (type1);
   12642 
   12643     case CPTK_IS_CONSTRUCTIBLE:
   12644       return is_xible (INIT_EXPR, type1, type2);
   12645 
   12646     case CPTK_IS_CONVERTIBLE:
   12647       return is_convertible (type1, type2);
   12648 
   12649     case CPTK_IS_EMPTY:
   12650       return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
   12651 
   12652     case CPTK_IS_ENUM:
   12653       return type_code1 == ENUMERAL_TYPE;
   12654 
   12655     case CPTK_IS_FINAL:
   12656       return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
   12657 
   12658     case CPTK_IS_FUNCTION:
   12659       return type_code1 == FUNCTION_TYPE;
   12660 
   12661     case CPTK_IS_LAYOUT_COMPATIBLE:
   12662       return layout_compatible_type_p (type1, type2);
   12663 
   12664     case CPTK_IS_LITERAL_TYPE:
   12665       return literal_type_p (type1);
   12666 
   12667     case CPTK_IS_MEMBER_FUNCTION_POINTER:
   12668       return TYPE_PTRMEMFUNC_P (type1);
   12669 
   12670     case CPTK_IS_MEMBER_OBJECT_POINTER:
   12671       return TYPE_PTRDATAMEM_P (type1);
   12672 
   12673     case CPTK_IS_MEMBER_POINTER:
   12674       return TYPE_PTRMEM_P (type1);
   12675 
   12676     case CPTK_IS_NOTHROW_ASSIGNABLE:
   12677       return is_nothrow_xible (MODIFY_EXPR, type1, type2);
   12678 
   12679     case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
   12680       return is_nothrow_xible (INIT_EXPR, type1, type2);
   12681 
   12682     case CPTK_IS_NOTHROW_CONVERTIBLE:
   12683       return is_nothrow_convertible (type1, type2);
   12684 
   12685     case CPTK_IS_OBJECT:
   12686       return (type_code1 != FUNCTION_TYPE
   12687 	      && type_code1 != REFERENCE_TYPE
   12688 	      && type_code1 != VOID_TYPE);
   12689 
   12690     case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
   12691       return pointer_interconvertible_base_of_p (type1, type2);
   12692 
   12693     case CPTK_IS_POD:
   12694       return pod_type_p (type1);
   12695 
   12696     case CPTK_IS_POLYMORPHIC:
   12697       return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
   12698 
   12699     case CPTK_IS_REFERENCE:
   12700       return type_code1 == REFERENCE_TYPE;
   12701 
   12702     case CPTK_IS_SAME:
   12703       return same_type_p (type1, type2);
   12704 
   12705     case CPTK_IS_SCOPED_ENUM:
   12706       return SCOPED_ENUM_P (type1);
   12707 
   12708     case CPTK_IS_STD_LAYOUT:
   12709       return std_layout_type_p (type1);
   12710 
   12711     case CPTK_IS_TRIVIAL:
   12712       return trivial_type_p (type1);
   12713 
   12714     case CPTK_IS_TRIVIALLY_ASSIGNABLE:
   12715       return is_trivially_xible (MODIFY_EXPR, type1, type2);
   12716 
   12717     case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
   12718       return is_trivially_xible (INIT_EXPR, type1, type2);
   12719 
   12720     case CPTK_IS_TRIVIALLY_COPYABLE:
   12721       return trivially_copyable_p (type1);
   12722 
   12723     case CPTK_IS_UNION:
   12724       return type_code1 == UNION_TYPE;
   12725 
   12726     case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
   12727       return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
   12728 
   12729     case CPTK_REF_CONVERTS_FROM_TEMPORARY:
   12730       return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
   12731 
   12732     case CPTK_IS_DEDUCIBLE:
   12733       return type_targs_deducible_from (type1, type2);
   12734 
   12735 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
   12736     case CPTK_##CODE:
   12737 #include "cp-trait.def"
   12738 #undef DEFTRAIT_TYPE
   12739       /* Type-yielding traits are handled in finish_trait_type.  */
   12740       break;
   12741     }
   12742 
   12743   gcc_unreachable ();
   12744 }
   12745 
   12746 /* Returns true if TYPE meets the requirements for the specified KIND,
   12747    false otherwise.
   12748 
   12749    When KIND == 1, TYPE must be an array of unknown bound,
   12750    or (possibly cv-qualified) void, or a complete type.
   12751 
   12752    When KIND == 2, TYPE must be a complete type, or array of complete type,
   12753    or (possibly cv-qualified) void.
   12754 
   12755    When KIND == 3:
   12756    If TYPE is a non-union class type, it must be complete.
   12757 
   12758    When KIND == 4:
   12759    If TYPE is a class type, it must be complete.  */
   12760 
   12761 static bool
   12762 check_trait_type (tree type, int kind = 1)
   12763 {
   12764   if (type == NULL_TREE)
   12765     return true;
   12766 
   12767   if (TREE_CODE (type) == TREE_VEC)
   12768     {
   12769       for (tree arg : tree_vec_range (type))
   12770 	if (!check_trait_type (arg, kind))
   12771 	  return false;
   12772       return true;
   12773     }
   12774 
   12775   if (kind == 1 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
   12776     return true; // Array of unknown bound. Don't care about completeness.
   12777 
   12778   if (kind == 3 && !NON_UNION_CLASS_TYPE_P (type))
   12779     return true; // Not a non-union class type. Don't care about completeness.
   12780 
   12781   if (kind == 4 && TREE_CODE (type) == ARRAY_TYPE)
   12782     return true; // Not a class type. Don't care about completeness.
   12783 
   12784   if (VOID_TYPE_P (type))
   12785     return true;
   12786 
   12787   type = complete_type (strip_array_types (type));
   12788   if (!COMPLETE_TYPE_P (type)
   12789       && cxx_incomplete_type_diagnostic (NULL_TREE, type, DK_PERMERROR)
   12790       && !flag_permissive)
   12791     return false;
   12792   return true;
   12793 }
   12794 
   12795 /* True iff the conversion (if any) would be a direct reference
   12796    binding, not requiring complete types.  This is LWG2939.  */
   12797 
   12798 static bool
   12799 same_type_ref_bind_p (cp_trait_kind kind, tree type1, tree type2)
   12800 {
   12801   tree from, to;
   12802   switch (kind)
   12803     {
   12804       /* These put the target type first.  */
   12805     case CPTK_IS_CONSTRUCTIBLE:
   12806     case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
   12807     case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
   12808     case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
   12809     case CPTK_REF_CONVERTS_FROM_TEMPORARY:
   12810       to = type1;
   12811       from = type2;
   12812       break;
   12813 
   12814       /* These put it second.  */
   12815     case CPTK_IS_CONVERTIBLE:
   12816     case CPTK_IS_NOTHROW_CONVERTIBLE:
   12817       to = type2;
   12818       from = type1;
   12819       break;
   12820 
   12821     default:
   12822       gcc_unreachable ();
   12823     }
   12824 
   12825   if (TREE_CODE (to) != REFERENCE_TYPE || !from)
   12826     return false;
   12827   if (TREE_CODE (from) == TREE_VEC && TREE_VEC_LENGTH (from) == 1)
   12828     from = TREE_VEC_ELT (from, 0);
   12829   return (TYPE_P (from)
   12830 	  && (same_type_ignoring_top_level_qualifiers_p
   12831 	      (non_reference (to), non_reference (from))));
   12832 }
   12833 
   12834 /* Process a trait expression.  */
   12835 
   12836 tree
   12837 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
   12838 {
   12839   if (type1 == error_mark_node
   12840       || type2 == error_mark_node)
   12841     return error_mark_node;
   12842 
   12843   if (processing_template_decl)
   12844     {
   12845       tree trait_expr = make_node (TRAIT_EXPR);
   12846       TREE_TYPE (trait_expr) = boolean_type_node;
   12847       TRAIT_EXPR_TYPE1 (trait_expr) = type1;
   12848       TRAIT_EXPR_TYPE2 (trait_expr) = type2;
   12849       TRAIT_EXPR_KIND (trait_expr) = kind;
   12850       TRAIT_EXPR_LOCATION (trait_expr) = loc;
   12851       return trait_expr;
   12852     }
   12853 
   12854   switch (kind)
   12855     {
   12856     case CPTK_HAS_NOTHROW_ASSIGN:
   12857     case CPTK_HAS_TRIVIAL_ASSIGN:
   12858     case CPTK_HAS_NOTHROW_CONSTRUCTOR:
   12859     case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
   12860     case CPTK_HAS_NOTHROW_COPY:
   12861     case CPTK_HAS_TRIVIAL_COPY:
   12862     case CPTK_HAS_TRIVIAL_DESTRUCTOR:
   12863       if (!check_trait_type (type1))
   12864 	return error_mark_node;
   12865       break;
   12866 
   12867     case CPTK_IS_LITERAL_TYPE:
   12868     case CPTK_IS_POD:
   12869     case CPTK_IS_STD_LAYOUT:
   12870     case CPTK_IS_TRIVIAL:
   12871     case CPTK_IS_TRIVIALLY_COPYABLE:
   12872     case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
   12873       if (!check_trait_type (type1, /* kind = */ 2))
   12874 	return error_mark_node;
   12875       break;
   12876 
   12877     case CPTK_IS_ABSTRACT:
   12878     case CPTK_IS_EMPTY:
   12879     case CPTK_IS_POLYMORPHIC:
   12880     case CPTK_HAS_VIRTUAL_DESTRUCTOR:
   12881       if (!check_trait_type (type1, /* kind = */ 3))
   12882 	return error_mark_node;
   12883       break;
   12884 
   12885     /* N.B. std::is_aggregate is kind=2 but we don't need a complete element
   12886        type to know whether an array is an aggregate, so use kind=4 here.  */
   12887     case CPTK_IS_AGGREGATE:
   12888     case CPTK_IS_FINAL:
   12889       if (!check_trait_type (type1, /* kind = */ 4))
   12890 	return error_mark_node;
   12891       break;
   12892 
   12893     case CPTK_IS_CONSTRUCTIBLE:
   12894     case CPTK_IS_CONVERTIBLE:
   12895     case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
   12896     case CPTK_IS_NOTHROW_CONVERTIBLE:
   12897     case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
   12898     case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
   12899     case CPTK_REF_CONVERTS_FROM_TEMPORARY:
   12900       /* Don't check completeness for direct reference binding.  */;
   12901       if (same_type_ref_bind_p (kind, type1, type2))
   12902 	break;
   12903       gcc_fallthrough ();
   12904 
   12905     case CPTK_IS_ASSIGNABLE:
   12906     case CPTK_IS_NOTHROW_ASSIGNABLE:
   12907     case CPTK_IS_TRIVIALLY_ASSIGNABLE:
   12908       if (!check_trait_type (type1)
   12909 	  || !check_trait_type (type2))
   12910 	return error_mark_node;
   12911       break;
   12912 
   12913     case CPTK_IS_BASE_OF:
   12914     case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
   12915       if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
   12916 	  && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
   12917 	  && !complete_type_or_else (type2, NULL_TREE))
   12918 	/* We already issued an error.  */
   12919 	return error_mark_node;
   12920       break;
   12921 
   12922     case CPTK_IS_ARRAY:
   12923     case CPTK_IS_BOUNDED_ARRAY:
   12924     case CPTK_IS_CLASS:
   12925     case CPTK_IS_ENUM:
   12926     case CPTK_IS_FUNCTION:
   12927     case CPTK_IS_MEMBER_FUNCTION_POINTER:
   12928     case CPTK_IS_MEMBER_OBJECT_POINTER:
   12929     case CPTK_IS_MEMBER_POINTER:
   12930     case CPTK_IS_OBJECT:
   12931     case CPTK_IS_REFERENCE:
   12932     case CPTK_IS_SAME:
   12933     case CPTK_IS_SCOPED_ENUM:
   12934     case CPTK_IS_UNION:
   12935       break;
   12936 
   12937     case CPTK_IS_LAYOUT_COMPATIBLE:
   12938       if (!array_of_unknown_bound_p (type1)
   12939 	  && TREE_CODE (type1) != VOID_TYPE
   12940 	  && !complete_type_or_else (type1, NULL_TREE))
   12941 	/* We already issued an error.  */
   12942 	return error_mark_node;
   12943       if (!array_of_unknown_bound_p (type2)
   12944 	  && TREE_CODE (type2) != VOID_TYPE
   12945 	  && !complete_type_or_else (type2, NULL_TREE))
   12946 	/* We already issued an error.  */
   12947 	return error_mark_node;
   12948       break;
   12949 
   12950     case CPTK_IS_DEDUCIBLE:
   12951       if (!DECL_TYPE_TEMPLATE_P (type1))
   12952 	{
   12953 	  error ("%qD is not a class or alias template", type1);
   12954 	  return error_mark_node;
   12955 	}
   12956       break;
   12957 
   12958 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
   12959     case CPTK_##CODE:
   12960 #include "cp-trait.def"
   12961 #undef DEFTRAIT_TYPE
   12962       /* Type-yielding traits are handled in finish_trait_type.  */
   12963       gcc_unreachable ();
   12964     }
   12965 
   12966   tree val = (trait_expr_value (kind, type1, type2)
   12967 	      ? boolean_true_node : boolean_false_node);
   12968   return maybe_wrap_with_location (val, loc);
   12969 }
   12970 
   12971 /* Process a trait type.  */
   12972 
   12973 tree
   12974 finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
   12975 		   tsubst_flags_t complain)
   12976 {
   12977   if (type1 == error_mark_node
   12978       || type2 == error_mark_node)
   12979     return error_mark_node;
   12980 
   12981   if (processing_template_decl)
   12982     {
   12983       tree type = cxx_make_type (TRAIT_TYPE);
   12984       TRAIT_TYPE_TYPE1 (type) = type1;
   12985       TRAIT_TYPE_TYPE2 (type) = type2;
   12986       TRAIT_TYPE_KIND_RAW (type) = build_int_cstu (integer_type_node, kind);
   12987       /* These traits are intended to be used in the definition of the ::type
   12988 	 member of the corresponding standard library type trait and aren't
   12989 	 mangleable (and thus won't appear directly in template signatures),
   12990 	 so structural equality should suffice.  */
   12991       SET_TYPE_STRUCTURAL_EQUALITY (type);
   12992       return type;
   12993     }
   12994 
   12995   switch (kind)
   12996     {
   12997     case CPTK_REMOVE_CV:
   12998       return cv_unqualified (type1);
   12999 
   13000     case CPTK_REMOVE_CVREF:
   13001       if (TYPE_REF_P (type1))
   13002 	type1 = TREE_TYPE (type1);
   13003       return cv_unqualified (type1);
   13004 
   13005     case CPTK_REMOVE_POINTER:
   13006       if (TYPE_PTR_P (type1))
   13007 	type1 = TREE_TYPE (type1);
   13008       return type1;
   13009 
   13010     case CPTK_REMOVE_REFERENCE:
   13011       if (TYPE_REF_P (type1))
   13012 	type1 = TREE_TYPE (type1);
   13013       return type1;
   13014 
   13015     case CPTK_TYPE_PACK_ELEMENT:
   13016       return finish_type_pack_element (type1, type2, complain);
   13017 
   13018     case CPTK_UNDERLYING_TYPE:
   13019       return finish_underlying_type (type1);
   13020 
   13021 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
   13022     case CPTK_##CODE:
   13023 #include "cp-trait.def"
   13024 #undef DEFTRAIT_EXPR
   13025       /* Expression-yielding traits are handled in finish_trait_expr.  */
   13026     case CPTK_BASES:
   13027     case CPTK_DIRECT_BASES:
   13028       /* BASES and DIRECT_BASES are handled in finish_bases.  */
   13029       break;
   13030     }
   13031 
   13032   gcc_unreachable ();
   13033 }
   13034 
   13035 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
   13036    which is ignored for C++.  */
   13037 
   13038 void
   13039 set_float_const_decimal64 (void)
   13040 {
   13041 }
   13042 
   13043 void
   13044 clear_float_const_decimal64 (void)
   13045 {
   13046 }
   13047 
   13048 bool
   13049 float_const_decimal64_p (void)
   13050 {
   13051   return 0;
   13052 }
   13053 
   13054 
   13055 /* Return true if T designates the implied `this' parameter.  */
   13057 
   13058 bool
   13059 is_this_parameter (tree t)
   13060 {
   13061   if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
   13062     return false;
   13063   gcc_assert (TREE_CODE (t) == PARM_DECL
   13064 	      || (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
   13065 	      || (cp_binding_oracle && VAR_P (t)));
   13066   return true;
   13067 }
   13068 
   13069 /* As above, or a C++23 explicit object parameter.  */
   13070 
   13071 bool
   13072 is_object_parameter (tree t)
   13073 {
   13074   if (is_this_parameter (t))
   13075     return true;
   13076   if (TREE_CODE (t) != PARM_DECL)
   13077     return false;
   13078   tree ctx = DECL_CONTEXT (t);
   13079   return (ctx && DECL_XOBJ_MEMBER_FUNCTION_P (ctx)
   13080 	  && t == DECL_ARGUMENTS (ctx));
   13081 }
   13082 
   13083 /* Insert the deduced return type for an auto function.  */
   13084 
   13085 void
   13086 apply_deduced_return_type (tree fco, tree return_type)
   13087 {
   13088   tree result;
   13089 
   13090   if (return_type == error_mark_node)
   13091     return;
   13092 
   13093   if (DECL_CONV_FN_P (fco))
   13094     DECL_NAME (fco) = make_conv_op_name (return_type);
   13095 
   13096   TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
   13097 
   13098   maybe_update_postconditions (fco);
   13099 
   13100   /* Apply the type to the result object.  */
   13101 
   13102   result = DECL_RESULT (fco);
   13103   if (result == NULL_TREE)
   13104     return;
   13105   if (TREE_TYPE (result) == return_type)
   13106     return;
   13107 
   13108   if (!processing_template_decl && !VOID_TYPE_P (return_type)
   13109       && !complete_type_or_else (return_type, NULL_TREE))
   13110     return;
   13111 
   13112   /* We already have a DECL_RESULT from start_preparsed_function.
   13113      Now we need to redo the work it and allocate_struct_function
   13114      did to reflect the new type.  */
   13115   result = build_decl (DECL_SOURCE_LOCATION (result), RESULT_DECL, NULL_TREE,
   13116 		       TYPE_MAIN_VARIANT (return_type));
   13117   DECL_ARTIFICIAL (result) = 1;
   13118   DECL_IGNORED_P (result) = 1;
   13119   cp_apply_type_quals_to_decl (cp_type_quals (return_type),
   13120                                result);
   13121   DECL_RESULT (fco) = result;
   13122 
   13123   if (!processing_template_decl)
   13124     if (function *fun = DECL_STRUCT_FUNCTION (fco))
   13125       {
   13126 	bool aggr = aggregate_value_p (result, fco);
   13127 #ifdef PCC_STATIC_STRUCT_RETURN
   13128 	fun->returns_pcc_struct = aggr;
   13129 #endif
   13130 	fun->returns_struct = aggr;
   13131       }
   13132 }
   13133 
   13134 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
   13135    this is a right unary fold. Otherwise it is a left unary fold. */
   13136 
   13137 static tree
   13138 finish_unary_fold_expr (location_t loc, tree expr, int op, tree_code dir)
   13139 {
   13140   /* Build a pack expansion (assuming expr has pack type).  */
   13141   if (!uses_parameter_packs (expr))
   13142     {
   13143       error_at (location_of (expr), "operand of fold expression has no "
   13144 		"unexpanded parameter packs");
   13145       return error_mark_node;
   13146     }
   13147   tree pack = make_pack_expansion (expr);
   13148 
   13149   /* Build the fold expression.  */
   13150   tree code = build_int_cstu (integer_type_node, abs (op));
   13151   tree fold = build_min_nt_loc (loc, dir, code, pack);
   13152   FOLD_EXPR_MODIFY_P (fold) = (op < 0);
   13153   TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
   13154 						    FOLD_EXPR_OP (fold),
   13155 						    FOLD_EXPR_MODIFY_P (fold));
   13156   return fold;
   13157 }
   13158 
   13159 tree
   13160 finish_left_unary_fold_expr (location_t loc, tree expr, int op)
   13161 {
   13162   return finish_unary_fold_expr (loc, expr, op, UNARY_LEFT_FOLD_EXPR);
   13163 }
   13164 
   13165 tree
   13166 finish_right_unary_fold_expr (location_t loc, tree expr, int op)
   13167 {
   13168   return finish_unary_fold_expr (loc, expr, op, UNARY_RIGHT_FOLD_EXPR);
   13169 }
   13170 
   13171 /* Build a binary fold expression over EXPR1 and EXPR2. The
   13172    associativity of the fold is determined by EXPR1 and EXPR2 (whichever
   13173    has an unexpanded parameter pack). */
   13174 
   13175 static tree
   13176 finish_binary_fold_expr (location_t loc, tree pack, tree init,
   13177 			 int op, tree_code dir)
   13178 {
   13179   pack = make_pack_expansion (pack);
   13180   tree code = build_int_cstu (integer_type_node, abs (op));
   13181   tree fold = build_min_nt_loc (loc, dir, code, pack, init);
   13182   FOLD_EXPR_MODIFY_P (fold) = (op < 0);
   13183   TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
   13184 						    FOLD_EXPR_OP (fold),
   13185 						    FOLD_EXPR_MODIFY_P (fold));
   13186   return fold;
   13187 }
   13188 
   13189 tree
   13190 finish_binary_fold_expr (location_t loc, tree expr1, tree expr2, int op)
   13191 {
   13192   // Determine which expr has an unexpanded parameter pack and
   13193   // set the pack and initial term.
   13194   bool pack1 = uses_parameter_packs (expr1);
   13195   bool pack2 = uses_parameter_packs (expr2);
   13196   if (pack1 && !pack2)
   13197     return finish_binary_fold_expr (loc, expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
   13198   else if (pack2 && !pack1)
   13199     return finish_binary_fold_expr (loc, expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
   13200   else
   13201     {
   13202       if (pack1)
   13203         error ("both arguments in binary fold have unexpanded parameter packs");
   13204       else
   13205         error ("no unexpanded parameter packs in binary fold");
   13206     }
   13207   return error_mark_node;
   13208 }
   13209 
   13210 /* Finish __builtin_launder (arg).  */
   13211 
   13212 tree
   13213 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
   13214 {
   13215   tree orig_arg = arg;
   13216   if (!type_dependent_expression_p (arg))
   13217     arg = decay_conversion (arg, complain);
   13218   if (error_operand_p (arg))
   13219     return error_mark_node;
   13220   if (!type_dependent_expression_p (arg)
   13221       && !TYPE_PTR_P (TREE_TYPE (arg)))
   13222     {
   13223       error_at (loc, "non-pointer argument to %<__builtin_launder%>");
   13224       return error_mark_node;
   13225     }
   13226   if (processing_template_decl)
   13227     arg = orig_arg;
   13228   return build_call_expr_internal_loc (loc, IFN_LAUNDER,
   13229 				       TREE_TYPE (arg), 1, arg);
   13230 }
   13231 
   13232 /* Finish __builtin_convertvector (arg, type).  */
   13233 
   13234 tree
   13235 cp_build_vec_convert (tree arg, location_t loc, tree type,
   13236 		      tsubst_flags_t complain)
   13237 {
   13238   if (error_operand_p (type))
   13239     return error_mark_node;
   13240   if (error_operand_p (arg))
   13241     return error_mark_node;
   13242 
   13243   tree ret = NULL_TREE;
   13244   if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
   13245     ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
   13246 			       decay_conversion (arg, complain),
   13247 			       loc, type, (complain & tf_error) != 0);
   13248 
   13249   if (!processing_template_decl)
   13250     return ret;
   13251 
   13252   return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
   13253 }
   13254 
   13255 /* Finish __builtin_bit_cast (type, arg).  */
   13256 
   13257 tree
   13258 cp_build_bit_cast (location_t loc, tree type, tree arg,
   13259 		   tsubst_flags_t complain)
   13260 {
   13261   if (error_operand_p (type))
   13262     return error_mark_node;
   13263   if (!dependent_type_p (type))
   13264     {
   13265       if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
   13266 	return error_mark_node;
   13267       if (TREE_CODE (type) == ARRAY_TYPE)
   13268 	{
   13269 	  /* std::bit_cast for destination ARRAY_TYPE is not possible,
   13270 	     as functions may not return an array, so don't bother trying
   13271 	     to support this (and then deal with VLAs etc.).  */
   13272 	  error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
   13273 			 "is an array type", type);
   13274 	  return error_mark_node;
   13275 	}
   13276       if (!trivially_copyable_p (type))
   13277 	{
   13278 	  error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
   13279 			 "is not trivially copyable", type);
   13280 	  return error_mark_node;
   13281 	}
   13282     }
   13283 
   13284   if (error_operand_p (arg))
   13285     return error_mark_node;
   13286 
   13287   if (!type_dependent_expression_p (arg))
   13288     {
   13289       if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
   13290 	{
   13291 	  /* Don't perform array-to-pointer conversion.  */
   13292 	  arg = mark_rvalue_use (arg, loc, true);
   13293 	  if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
   13294 	    return error_mark_node;
   13295 	}
   13296       else
   13297 	arg = decay_conversion (arg, complain);
   13298 
   13299       if (error_operand_p (arg))
   13300 	return error_mark_node;
   13301 
   13302       if (!trivially_copyable_p (TREE_TYPE (arg)))
   13303 	{
   13304 	  error_at (cp_expr_loc_or_loc (arg, loc),
   13305 		    "%<__builtin_bit_cast%> source type %qT "
   13306 		    "is not trivially copyable", TREE_TYPE (arg));
   13307 	  return error_mark_node;
   13308 	}
   13309       if (!dependent_type_p (type)
   13310 	  && !cp_tree_equal (TYPE_SIZE_UNIT (type),
   13311 			     TYPE_SIZE_UNIT (TREE_TYPE (arg))))
   13312 	{
   13313 	  error_at (loc, "%<__builtin_bit_cast%> source size %qE "
   13314 			 "not equal to destination type size %qE",
   13315 			 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
   13316 			 TYPE_SIZE_UNIT (type));
   13317 	  return error_mark_node;
   13318 	}
   13319     }
   13320 
   13321   tree ret = build_min (BIT_CAST_EXPR, type, arg);
   13322   SET_EXPR_LOCATION (ret, loc);
   13323 
   13324   if (!processing_template_decl && CLASS_TYPE_P (type))
   13325     ret = get_target_expr (ret, complain);
   13326 
   13327   return ret;
   13328 }
   13329 
   13330 /* Diagnose invalid #pragma GCC unroll argument and adjust
   13331    it if needed.  */
   13332 
   13333 tree
   13334 cp_check_pragma_unroll (location_t loc, tree unroll)
   13335 {
   13336   HOST_WIDE_INT lunroll = 0;
   13337   if (type_dependent_expression_p (unroll))
   13338     ;
   13339   else if (!INTEGRAL_TYPE_P (TREE_TYPE (unroll))
   13340 	   || (!value_dependent_expression_p (unroll)
   13341 	       && (!tree_fits_shwi_p (unroll)
   13342 		   || (lunroll = tree_to_shwi (unroll)) < 0
   13343 		   || lunroll >= USHRT_MAX)))
   13344     {
   13345       error_at (loc, "%<#pragma GCC unroll%> requires an"
   13346 		" assignment-expression that evaluates to a non-negative"
   13347 		" integral constant less than %u", USHRT_MAX);
   13348       unroll = integer_one_node;
   13349     }
   13350   else if (TREE_CODE (unroll) == INTEGER_CST)
   13351     {
   13352       unroll = fold_convert (integer_type_node, unroll);
   13353       if (integer_zerop (unroll))
   13354 	unroll = integer_one_node;
   13355     }
   13356   return unroll;
   13357 }
   13358 
   13359 #include "gt-cp-semantics.h"
   13360