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