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