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