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