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