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