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