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