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