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