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