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