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