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