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