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