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