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