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