| 1 |
/* coroutine-specific state, expansions and tests. |
| 2 |
|
| 3 |
Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
Contributed by Iain Sandoe <[email protected]> under contract to Facebook. |
| 6 |
|
| 7 |
This file is part of GCC. |
| 8 |
|
| 9 |
GCC is free software; you can redistribute it and/or modify it under |
| 10 |
the terms of the GNU General Public License as published by the Free |
| 11 |
Software Foundation; either version 3, or (at your option) any later |
| 12 |
version. |
| 13 |
|
| 14 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 15 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 |
for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with GCC; see the file COPYING3. If not see |
| 21 |
<http://www.gnu.org/licenses/>. */ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include "system.h" |
| 25 |
#include "coretypes.h" |
| 26 |
#include "target.h" |
| 27 |
#include "cp-tree.h" |
| 28 |
#include "stringpool.h" |
| 29 |
#include "stmt.h" |
| 30 |
#include "stor-layout.h" |
| 31 |
#include "tree-iterator.h" |
| 32 |
#include "tree.h" |
| 33 |
#include "gcc-rich-location.h" |
| 34 |
#include "hash-map.h" |
| 35 |
#include "coroutines.h" |
| 36 |
|
| 37 |
/* ================= Debug. ================= */ |
| 38 |
|
| 39 |
#include "langhooks.h" |
| 40 |
#include "cxx-pretty-print.h" |
| 41 |
|
| 42 |
/* Walk through the fields of the type TYP and print them to the pretty printer |
| 43 |
PP. */ |
| 44 |
|
| 45 |
static void |
| 46 |
dump_record_fields (cxx_pretty_printer *pp, tree typ) |
| 47 |
{ |
| 48 |
pp->type_id (typ); |
| 49 |
pp_newline_and_indent (pp, 2); |
| 50 |
pp_left_brace (pp); |
| 51 |
pp_indentation (pp) += 2; |
| 52 |
|
| 53 |
/* We'll be on a new line, we don't need padding. */ |
| 54 |
pp->set_padding (pp_none); |
| 55 |
|
| 56 |
for (tree tmp = TYPE_FIELDS (typ); tmp; tmp = DECL_CHAIN (tmp)) |
| 57 |
{ |
| 58 |
pp_newline_and_indent (pp, 0); |
| 59 |
pp->declaration (tmp); |
| 60 |
} |
| 61 |
|
| 62 |
pp_newline_and_indent (pp, -2); |
| 63 |
pp_right_brace (pp); |
| 64 |
pp_newline_and_indent (pp, -2); |
| 65 |
} |
| 66 |
|
| 67 |
/* The lang-coro stream. */ |
| 68 |
static FILE *dmp_str = NULL; |
| 69 |
|
| 70 |
/* ID of the lang-coro dump. */ |
| 71 |
int coro_dump_id; |
| 72 |
|
| 73 |
/* Flags passed to the lang-coro dump. */ |
| 74 |
static dump_flags_t coro_dump_flags; |
| 75 |
|
| 76 |
/* Pretty print the function FNDECL, which ought to be a coroutine before |
| 77 |
co_await expansion, into the lang-coro dump, if it is enabled. */ |
| 78 |
|
| 79 |
static void |
| 80 |
coro_maybe_dump_initial_function (tree fndecl) |
| 81 |
{ |
| 82 |
if (!dmp_str) |
| 83 |
return; |
| 84 |
|
| 85 |
bool lambda_p = LAMBDA_TYPE_P (DECL_CONTEXT (fndecl)); |
| 86 |
fprintf (dmp_str, "%s %s original:\n", |
| 87 |
(lambda_p ? "Lambda" : "Function"), |
| 88 |
lang_hooks.decl_printable_name (fndecl, 2)); |
| 89 |
|
| 90 |
cxx_pretty_printer pp; |
| 91 |
pp.set_output_stream (dmp_str); |
| 92 |
pp.flags = coro_dump_flags; |
| 93 |
pp.declaration (fndecl); |
| 94 |
pp_newline_and_flush (&pp); |
| 95 |
} |
| 96 |
|
| 97 |
/* Pretty print the RAMP function to the lang-coro dump, if it is enabled. */ |
| 98 |
|
| 99 |
static void |
| 100 |
coro_maybe_dump_ramp (tree ramp) |
| 101 |
{ |
| 102 |
if (!dmp_str) |
| 103 |
return; |
| 104 |
|
| 105 |
cxx_pretty_printer pp; |
| 106 |
pp.set_output_stream (dmp_str); |
| 107 |
pp.flags = coro_dump_flags; |
| 108 |
|
| 109 |
pp_string (&pp, "Ramp function:"); |
| 110 |
pp_newline_and_indent (&pp, 0); |
| 111 |
pp.declaration (ramp); |
| 112 |
pp_newline_and_flush (&pp); |
| 113 |
} |
| 114 |
|
| 115 |
/* For a given ACTOR and DESTROY, if lang-coro dumping is enabled, pretty-print |
| 116 |
their contents to the lang-coro dump. */ |
| 117 |
|
| 118 |
static void |
| 119 |
coro_maybe_dump_transformed_functions (tree actor, tree destroy) |
| 120 |
{ |
| 121 |
if (!dmp_str) |
| 122 |
return; |
| 123 |
|
| 124 |
cxx_pretty_printer pp; |
| 125 |
pp.set_output_stream (dmp_str); |
| 126 |
pp.flags = coro_dump_flags; |
| 127 |
|
| 128 |
if (!actor || actor == error_mark_node) |
| 129 |
{ |
| 130 |
pp_string (&pp, "Transform failed"); |
| 131 |
pp_newline_and_flush (&pp); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
tree frame = TREE_TYPE (TREE_TYPE (DECL_ARGUMENTS (actor))); |
| 136 |
pp_string (&pp, "Frame type:"); |
| 137 |
pp_newline (&pp); |
| 138 |
dump_record_fields (&pp, frame); |
| 139 |
pp_newline_and_flush (&pp); |
| 140 |
|
| 141 |
pp_string (&pp, "Actor/resumer:"); |
| 142 |
pp_newline (&pp); |
| 143 |
pp.declaration (actor); |
| 144 |
pp_newline_and_flush (&pp); |
| 145 |
|
| 146 |
pp_string (&pp, "Destroyer:"); |
| 147 |
pp_newline (&pp); |
| 148 |
pp.declaration (destroy); |
| 149 |
pp_newline_and_flush (&pp); |
| 150 |
} |
| 151 |
|
| 152 |
/* ================= END Debug. ================= */ |
| 153 |
|
| 154 |
static bool coro_promise_type_found_p (tree, location_t); |
| 155 |
|
| 156 |
/* GCC C++ coroutines implementation. |
| 157 |
|
| 158 |
The user authors a function that becomes a coroutine (lazily) by |
| 159 |
making use of any of the co_await, co_yield or co_return keywords. |
| 160 |
|
| 161 |
Unlike a regular function, where the activation record is placed on the |
| 162 |
stack, and is destroyed on function exit, a coroutine has some state that |
| 163 |
persists between calls - the coroutine frame (analogous to a stack frame). |
| 164 |
|
| 165 |
We transform the user's function into three pieces: |
| 166 |
1. A so-called ramp function, that establishes the coroutine frame and |
| 167 |
begins execution of the coroutine. |
| 168 |
2. An actor function that contains the state machine corresponding to the |
| 169 |
user's suspend/resume structure. |
| 170 |
3. A stub function that calls the actor function in 'destroy' mode. |
| 171 |
|
| 172 |
The actor function is executed: |
| 173 |
* from "resume point 0" by the ramp. |
| 174 |
* from resume point N ( > 0 ) for handle.resume() calls. |
| 175 |
* from the destroy stub for destroy point N for handle.destroy() calls. |
| 176 |
|
| 177 |
The functions in this file carry out the necessary analysis of, and |
| 178 |
transforms to, the AST to perform this. |
| 179 |
|
| 180 |
The C++ coroutine design makes use of some helper functions that are |
| 181 |
authored in a so-called "promise" class provided by the user. |
| 182 |
|
| 183 |
At parse time (or post substitution) the type of the coroutine promise |
| 184 |
will be determined. At that point, we can look up the required promise |
| 185 |
class methods and issue diagnostics if they are missing or incorrect. To |
| 186 |
avoid repeating these actions at code-gen time, we make use of temporary |
| 187 |
'proxy' variables for the coroutine handle and the promise - which will |
| 188 |
eventually be instantiated in the coroutine frame. |
| 189 |
|
| 190 |
Each of the keywords will expand to a code sequence (although co_yield is |
| 191 |
just syntactic sugar for a co_await). |
| 192 |
|
| 193 |
We defer the analysis and transformation until template expansion is |
| 194 |
complete so that we have complete types at that time. |
| 195 |
|
| 196 |
--------------------------------------------------------------------------- |
| 197 |
|
| 198 |
Coroutine state, and responsibility for its release. |
| 199 |
|
| 200 |
As noted above, a coroutine has some state that persists across suspensions. |
| 201 |
|
| 202 |
The state has two components: |
| 203 |
* State that is specified by the standard and persists for the entire |
| 204 |
life of the coroutine. |
| 205 |
* Local state that is constructed/destructed as scopes in the original |
| 206 |
function body are entered/exited. The destruction of local state is |
| 207 |
always the responsibility of the body code. |
| 208 |
|
| 209 |
The persistent state (and the overall storage for the state) must be |
| 210 |
managed in two places: |
| 211 |
* The ramp function (which allocates and builds this - and can, in some |
| 212 |
cases, be responsible for destroying it) |
| 213 |
* The re-written function body which can destroy it when that body |
| 214 |
completes its final suspend - or when the handle.destroy () is called. |
| 215 |
|
| 216 |
In all cases the ramp holds responsibility for constructing the standard- |
| 217 |
mandated persistent state. |
| 218 |
|
| 219 |
There are four ways in which the ramp might be re-entered after starting |
| 220 |
the function body: |
| 221 |
A The body could suspend (one might expect that to be the 'normal' case |
| 222 |
for most coroutines). |
| 223 |
B The body might complete either synchronously or via continuations. |
| 224 |
C An exception might be thrown during the setup of the initial await |
| 225 |
expression, before the initial awaiter resumes. |
| 226 |
D An exception might be processed by promise.unhandled_exception () and |
| 227 |
that, in turn, might re-throw it (or throw something else). In this |
| 228 |
case, the coroutine is considered suspended at the final suspension |
| 229 |
point. |
| 230 |
|
| 231 |
Until the ramp return value has been constructed, the ramp is considered |
| 232 |
to have a use of the state. |
| 233 |
|
| 234 |
To manage these interacting conditions we allocate a reference counter |
| 235 |
for the frame state. This is initialised to 1 by the ramp as part of its |
| 236 |
startup (note that failures/exceptions in the startup code are handled |
| 237 |
locally to the ramp). |
| 238 |
|
| 239 |
When the body returns (either normally, or by exception) the ramp releases |
| 240 |
its use. |
| 241 |
|
| 242 |
Once the rewritten coroutine body is started, the body is considered to |
| 243 |
have a use of the frame. This use (potentially) needs to be released if |
| 244 |
an exception is thrown from the body. We implement this using an eh-only |
| 245 |
cleanup around the initial await and function body. If we have the case |
| 246 |
D above, then we do not release the use. |
| 247 |
|
| 248 |
In case: |
| 249 |
|
| 250 |
A, typically the ramp would be re-entered with the body holding a use, |
| 251 |
and therefore the ramp should not destroy the state. |
| 252 |
|
| 253 |
B, both the body and ramp will have released their uses, and the ramp |
| 254 |
should destroy the state. |
| 255 |
|
| 256 |
C, we must arrange for the body to release its use, because we require |
| 257 |
the ramp to cleanup in this circumstance. |
| 258 |
|
| 259 |
D is an outlier, since the responsibility for destruction of the state |
| 260 |
now rests with the user's code (via a handle.destroy() call). |
| 261 |
|
| 262 |
NOTE: In the case that the body has never suspended before such an |
| 263 |
exception occurs, the only reasonable way for the user code to obtain the |
| 264 |
necessary handle is if unhandled_exception() throws the handle or some |
| 265 |
object that contains the handle. That is outside of the designs here - |
| 266 |
if the user code might need this corner-case, then such provision will |
| 267 |
have to be made. |
| 268 |
|
| 269 |
In the ramp, we implement destruction for the persistent frame state by |
| 270 |
means of cleanups. These are run conditionally when the reference count |
| 271 |
is 0 signalling that both the body and the ramp have completed. |
| 272 |
|
| 273 |
In the body, once we pass the final suspend, then we test the use and |
| 274 |
delete the state if the use is 0. */ |
| 275 |
|
| 276 |
/* The state that we collect during parsing (and template expansion) for |
| 277 |
a coroutine. */ |
| 278 |
|
| 279 |
struct GTY((for_user)) coroutine_info |
| 280 |
{ |
| 281 |
tree function_decl; /* The original function decl. */ |
| 282 |
tree actor_decl; /* The synthesized actor function. */ |
| 283 |
tree destroy_decl; /* The synthesized destroy function. */ |
| 284 |
tree promise_type; /* The cached promise type for this function. */ |
| 285 |
tree traits_type; /* The cached traits type for this function. */ |
| 286 |
tree handle_type; /* The cached coroutine handle for this function. */ |
| 287 |
tree self_h_proxy; /* A handle instance that is used as the proxy for the |
| 288 |
one that will eventually be built in lowering. */ |
| 289 |
tree promise_proxy; /* Likewise, a proxy promise instance. */ |
| 290 |
tree from_address; /* handle_type from_address() function. */ |
| 291 |
tree return_void; /* The expression for p.return_void(), if it exists. */ |
| 292 |
location_t first_coro_keyword; /* The location of the keyword that made this |
| 293 |
function into a coroutine. */ |
| 294 |
|
| 295 |
/* Temporary variable number assigned by get_awaitable_var. */ |
| 296 |
int awaitable_number = 0; |
| 297 |
|
| 298 |
/* Flags to avoid repeated errors for per-function issues. */ |
| 299 |
bool coro_ret_type_error_emitted; |
| 300 |
bool coro_promise_error_emitted; |
| 301 |
bool coro_co_return_error_emitted; |
| 302 |
}; |
| 303 |
|
| 304 |
struct coroutine_info_hasher : ggc_ptr_hash<coroutine_info> |
| 305 |
{ |
| 306 |
typedef tree compare_type; /* We only compare the function decl. */ |
| 307 |
static inline hashval_t hash (coroutine_info *); |
| 308 |
static inline hashval_t hash (const compare_type &); |
| 309 |
static inline bool equal (coroutine_info *, coroutine_info *); |
| 310 |
static inline bool equal (coroutine_info *, const compare_type &); |
| 311 |
}; |
| 312 |
|
| 313 |
/* This table holds all the collected coroutine state for coroutines in |
| 314 |
the current translation unit. */ |
| 315 |
|
| 316 |
static GTY (()) hash_table<coroutine_info_hasher> *coroutine_info_table; |
| 317 |
|
| 318 |
/* We will initialize state lazily. */ |
| 319 |
static bool coro_initialized = false; |
| 320 |
|
| 321 |
/* Return a hash value for the entry pointed to by INFO. |
| 322 |
The compare type is a tree, but the only trees we are going use are |
| 323 |
function decls. We use the DECL_UID as the hash value since that is |
| 324 |
stable across PCH. */ |
| 325 |
|
| 326 |
hashval_t |
| 327 |
coroutine_info_hasher::hash (coroutine_info *info) |
| 328 |
{ |
| 329 |
return DECL_UID (info->function_decl); |
| 330 |
} |
| 331 |
|
| 332 |
/* Return a hash value for the compare value COMP. */ |
| 333 |
|
| 334 |
hashval_t |
| 335 |
coroutine_info_hasher::hash (const compare_type& comp) |
| 336 |
{ |
| 337 |
return DECL_UID (comp); |
| 338 |
} |
| 339 |
|
| 340 |
/* Return true if the entries pointed to by LHS and RHS are for the |
| 341 |
same coroutine. */ |
| 342 |
|
| 343 |
bool |
| 344 |
coroutine_info_hasher::equal (coroutine_info *lhs, coroutine_info *rhs) |
| 345 |
{ |
| 346 |
return lhs->function_decl == rhs->function_decl; |
| 347 |
} |
| 348 |
|
| 349 |
bool |
| 350 |
coroutine_info_hasher::equal (coroutine_info *lhs, const compare_type& rhs) |
| 351 |
{ |
| 352 |
return lhs->function_decl == rhs; |
| 353 |
} |
| 354 |
|
| 355 |
/* Get the existing coroutine_info for FN_DECL, or insert a new one if the |
| 356 |
entry does not yet exist. */ |
| 357 |
|
| 358 |
coroutine_info * |
| 359 |
get_or_insert_coroutine_info (tree fn_decl) |
| 360 |
{ |
| 361 |
gcc_checking_assert (coroutine_info_table != NULL); |
| 362 |
|
| 363 |
coroutine_info **slot = coroutine_info_table->find_slot_with_hash |
| 364 |
(fn_decl, coroutine_info_hasher::hash (fn_decl), INSERT); |
| 365 |
|
| 366 |
if (*slot == NULL) |
| 367 |
{ |
| 368 |
*slot = new (ggc_cleared_alloc<coroutine_info> ()) coroutine_info (); |
| 369 |
(*slot)->function_decl = fn_decl; |
| 370 |
} |
| 371 |
|
| 372 |
return *slot; |
| 373 |
} |
| 374 |
|
| 375 |
/* Get the existing coroutine_info for FN_DECL, fail if it doesn't exist. */ |
| 376 |
|
| 377 |
coroutine_info * |
| 378 |
get_coroutine_info (tree fn_decl) |
| 379 |
{ |
| 380 |
if (coroutine_info_table == NULL) |
| 381 |
return NULL; |
| 382 |
|
| 383 |
coroutine_info **slot = coroutine_info_table->find_slot_with_hash |
| 384 |
(fn_decl, coroutine_info_hasher::hash (fn_decl), NO_INSERT); |
| 385 |
if (slot) |
| 386 |
return *slot; |
| 387 |
return NULL; |
| 388 |
} |
| 389 |
|
| 390 |
/* We will lazily create all the identifiers that are used by coroutines |
| 391 |
on the first attempt to lookup the traits. */ |
| 392 |
|
| 393 |
/* Identifiers that are used by all coroutines. */ |
| 394 |
|
| 395 |
static GTY(()) tree coro_traits_identifier; |
| 396 |
static GTY(()) tree coro_handle_identifier; |
| 397 |
static GTY(()) tree coro_promise_type_identifier; |
| 398 |
|
| 399 |
/* Required promise method name identifiers. */ |
| 400 |
|
| 401 |
static GTY(()) tree coro_await_transform_identifier; |
| 402 |
static GTY(()) tree coro_initial_suspend_identifier; |
| 403 |
static GTY(()) tree coro_final_suspend_identifier; |
| 404 |
static GTY(()) tree coro_return_void_identifier; |
| 405 |
static GTY(()) tree coro_return_value_identifier; |
| 406 |
static GTY(()) tree coro_yield_value_identifier; |
| 407 |
static GTY(()) tree coro_address_identifier; |
| 408 |
static GTY(()) tree coro_from_address_identifier; |
| 409 |
static GTY(()) tree coro_get_return_object_identifier; |
| 410 |
static GTY(()) tree coro_gro_on_allocation_fail_identifier; |
| 411 |
static GTY(()) tree coro_unhandled_exception_identifier; |
| 412 |
|
| 413 |
/* Awaitable methods. */ |
| 414 |
|
| 415 |
static GTY(()) tree coro_await_ready_identifier; |
| 416 |
static GTY(()) tree coro_await_suspend_identifier; |
| 417 |
static GTY(()) tree coro_await_resume_identifier; |
| 418 |
|
| 419 |
/* Accessors for the coroutine frame state used by the implementation. */ |
| 420 |
|
| 421 |
static GTY(()) tree coro_resume_fn_id; |
| 422 |
static GTY(()) tree coro_destroy_fn_id; |
| 423 |
static GTY(()) tree coro_promise_id; |
| 424 |
static GTY(()) tree coro_frame_needs_free_id; |
| 425 |
static GTY(()) tree coro_resume_index_id; |
| 426 |
static GTY(()) tree coro_self_handle_id; |
| 427 |
static GTY(()) tree coro_actor_continue_id; |
| 428 |
static GTY(()) tree coro_frame_i_a_r_c_id; |
| 429 |
static GTY(()) tree coro_frame_refcount_id; |
| 430 |
|
| 431 |
/* Create the identifiers used by the coroutines library interfaces and |
| 432 |
the implementation frame state. */ |
| 433 |
|
| 434 |
static void |
| 435 |
coro_init_identifiers () |
| 436 |
{ |
| 437 |
coro_traits_identifier = get_identifier ("coroutine_traits"); |
| 438 |
coro_handle_identifier = get_identifier ("coroutine_handle"); |
| 439 |
coro_promise_type_identifier = get_identifier ("promise_type"); |
| 440 |
|
| 441 |
coro_await_transform_identifier = get_identifier ("await_transform"); |
| 442 |
coro_initial_suspend_identifier = get_identifier ("initial_suspend"); |
| 443 |
coro_final_suspend_identifier = get_identifier ("final_suspend"); |
| 444 |
coro_return_void_identifier = get_identifier ("return_void"); |
| 445 |
coro_return_value_identifier = get_identifier ("return_value"); |
| 446 |
coro_yield_value_identifier = get_identifier ("yield_value"); |
| 447 |
coro_address_identifier = get_identifier ("address"); |
| 448 |
coro_from_address_identifier = get_identifier ("from_address"); |
| 449 |
coro_get_return_object_identifier = get_identifier ("get_return_object"); |
| 450 |
coro_gro_on_allocation_fail_identifier = |
| 451 |
get_identifier ("get_return_object_on_allocation_failure"); |
| 452 |
coro_unhandled_exception_identifier = get_identifier ("unhandled_exception"); |
| 453 |
|
| 454 |
coro_await_ready_identifier = get_identifier ("await_ready"); |
| 455 |
coro_await_suspend_identifier = get_identifier ("await_suspend"); |
| 456 |
coro_await_resume_identifier = get_identifier ("await_resume"); |
| 457 |
|
| 458 |
/* Coroutine state frame field accessors. */ |
| 459 |
coro_resume_fn_id = get_identifier ("_Coro_resume_fn"); |
| 460 |
coro_destroy_fn_id = get_identifier ("_Coro_destroy_fn"); |
| 461 |
coro_promise_id = get_identifier ("_Coro_promise"); |
| 462 |
coro_frame_needs_free_id = get_identifier ("_Coro_frame_needs_free"); |
| 463 |
coro_frame_i_a_r_c_id = get_identifier ("_Coro_initial_await_resume_called"); |
| 464 |
coro_resume_index_id = get_identifier ("_Coro_resume_index"); |
| 465 |
coro_self_handle_id = get_identifier ("_Coro_self_handle"); |
| 466 |
coro_actor_continue_id = get_identifier ("_Coro_actor_continue"); |
| 467 |
coro_frame_refcount_id = get_identifier ("_Coro_frame_refcount"); |
| 468 |
} |
| 469 |
|
| 470 |
/* Trees we only need to set up once. */ |
| 471 |
|
| 472 |
static GTY(()) tree coro_traits_templ; |
| 473 |
static GTY(()) tree coro_handle_templ; |
| 474 |
static GTY(()) tree void_coro_handle_type; |
| 475 |
static GTY(()) tree void_coro_handle_address; |
| 476 |
|
| 477 |
/* ================= Parse, Semantics and Type checking ================= */ |
| 478 |
|
| 479 |
/* This initial set of routines are helper for the parsing and template |
| 480 |
expansion phases. |
| 481 |
|
| 482 |
At the completion of this, we will have completed trees for each of the |
| 483 |
keywords, but making use of proxy variables for the self-handle and the |
| 484 |
promise class instance. */ |
| 485 |
|
| 486 |
/* [coroutine.traits] |
| 487 |
Lookup the coroutine_traits template decl. */ |
| 488 |
|
| 489 |
static tree |
| 490 |
find_coro_traits_template_decl (location_t kw) |
| 491 |
{ |
| 492 |
/* If we are missing fundamental information, such as the traits, (or the |
| 493 |
declaration found is not a type template), then don't emit an error for |
| 494 |
every keyword in a TU, just do it once. */ |
| 495 |
static bool traits_error_emitted = false; |
| 496 |
|
| 497 |
tree traits_decl = lookup_qualified_name (std_node, coro_traits_identifier, |
| 498 |
LOOK_want::NORMAL, |
| 499 |
/*complain=*/!traits_error_emitted); |
| 500 |
if (traits_decl == error_mark_node |
| 501 |
|| !DECL_TYPE_TEMPLATE_P (traits_decl)) |
| 502 |
{ |
| 503 |
if (!traits_error_emitted) |
| 504 |
{ |
| 505 |
auto_diagnostic_group d; |
| 506 |
gcc_rich_location richloc (kw); |
| 507 |
error_at (&richloc, "coroutines require a traits template; cannot" |
| 508 |
" find %<%E::%E%>", std_node, coro_traits_identifier); |
| 509 |
inform (&richloc, "perhaps %<#include <coroutine>%> is missing"); |
| 510 |
traits_error_emitted = true; |
| 511 |
} |
| 512 |
return NULL_TREE; |
| 513 |
} |
| 514 |
else |
| 515 |
return traits_decl; |
| 516 |
} |
| 517 |
|
| 518 |
/* Instantiate Coroutine traits for the function signature. */ |
| 519 |
|
| 520 |
static tree |
| 521 |
instantiate_coro_traits (tree fndecl, location_t kw) |
| 522 |
{ |
| 523 |
/* [coroutine.traits.primary] |
| 524 |
So now build up a type list for the template <typename _R, typename...>. |
| 525 |
The types are the function's arg types and _R is the function return |
| 526 |
type. */ |
| 527 |
|
| 528 |
tree functyp = TREE_TYPE (fndecl); |
| 529 |
tree arg = DECL_ARGUMENTS (fndecl); |
| 530 |
tree arg_node = TYPE_ARG_TYPES (functyp); |
| 531 |
tree argtypes = make_tree_vec (list_length (arg_node)-1); |
| 532 |
unsigned p = 0; |
| 533 |
|
| 534 |
while (arg_node != NULL_TREE && !VOID_TYPE_P (TREE_VALUE (arg_node))) |
| 535 |
{ |
| 536 |
if (is_this_parameter (arg) |
| 537 |
|| DECL_NAME (arg) == closure_identifier) |
| 538 |
{ |
| 539 |
/* We pass a reference to *this to the param preview. */ |
| 540 |
tree ct = TREE_TYPE (TREE_TYPE (arg)); |
| 541 |
TREE_VEC_ELT (argtypes, p++) = cp_build_reference_type (ct, false); |
| 542 |
} |
| 543 |
else |
| 544 |
TREE_VEC_ELT (argtypes, p++) = TREE_VALUE (arg_node); |
| 545 |
|
| 546 |
arg_node = TREE_CHAIN (arg_node); |
| 547 |
arg = DECL_CHAIN (arg); |
| 548 |
} |
| 549 |
|
| 550 |
tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK); |
| 551 |
ARGUMENT_PACK_ARGS (argtypepack) = argtypes; |
| 552 |
|
| 553 |
tree targ = make_tree_vec (2); |
| 554 |
TREE_VEC_ELT (targ, 0) = TREE_TYPE (functyp); |
| 555 |
TREE_VEC_ELT (targ, 1) = argtypepack; |
| 556 |
|
| 557 |
tree traits_class |
| 558 |
= lookup_template_class (coro_traits_templ, targ, |
| 559 |
/*in_decl=*/NULL_TREE, /*context=*/NULL_TREE, |
| 560 |
tf_warning_or_error); |
| 561 |
|
| 562 |
if (traits_class == error_mark_node) |
| 563 |
{ |
| 564 |
error_at (kw, "cannot instantiate %<coroutine traits%>"); |
| 565 |
return NULL_TREE; |
| 566 |
} |
| 567 |
|
| 568 |
return traits_class; |
| 569 |
} |
| 570 |
|
| 571 |
/* [coroutine.handle] */ |
| 572 |
|
| 573 |
static tree |
| 574 |
find_coro_handle_template_decl (location_t kw) |
| 575 |
{ |
| 576 |
/* As for the coroutine traits, this error is per TU, so only emit |
| 577 |
it once. */ |
| 578 |
static bool coro_handle_error_emitted = false; |
| 579 |
tree handle_decl = lookup_qualified_name (std_node, coro_handle_identifier, |
| 580 |
LOOK_want::NORMAL, |
| 581 |
!coro_handle_error_emitted); |
| 582 |
if (handle_decl == error_mark_node |
| 583 |
|| !DECL_CLASS_TEMPLATE_P (handle_decl)) |
| 584 |
{ |
| 585 |
if (!coro_handle_error_emitted) |
| 586 |
error_at (kw, "coroutines require a handle class template;" |
| 587 |
" cannot find %<%E::%E%>", std_node, coro_handle_identifier); |
| 588 |
coro_handle_error_emitted = true; |
| 589 |
return NULL_TREE; |
| 590 |
} |
| 591 |
else |
| 592 |
return handle_decl; |
| 593 |
} |
| 594 |
|
| 595 |
/* Get and validate HANDLE_TYPE::address. The resulting function, if any, will |
| 596 |
be a non-overloaded member function that takes no arguments and returns |
| 597 |
void*. If that is not the case, signals an error and returns NULL_TREE. */ |
| 598 |
|
| 599 |
static tree |
| 600 |
get_handle_type_address (location_t kw, tree handle_type) |
| 601 |
{ |
| 602 |
tree addr_getter = lookup_member (handle_type, coro_address_identifier, 1, |
| 603 |
0, tf_warning_or_error); |
| 604 |
if (!addr_getter || addr_getter == error_mark_node) |
| 605 |
{ |
| 606 |
qualified_name_lookup_error (handle_type, coro_address_identifier, |
| 607 |
error_mark_node, kw); |
| 608 |
return NULL_TREE; |
| 609 |
} |
| 610 |
|
| 611 |
if (!BASELINK_P (addr_getter) |
| 612 |
|| TREE_CODE (TREE_TYPE (addr_getter)) != METHOD_TYPE) |
| 613 |
{ |
| 614 |
error_at (kw, "%qE must be a non-overloaded method", addr_getter); |
| 615 |
return NULL_TREE; |
| 616 |
} |
| 617 |
|
| 618 |
tree fn_t = TREE_TYPE (addr_getter); |
| 619 |
tree arg = TYPE_ARG_TYPES (fn_t); |
| 620 |
|
| 621 |
/* Skip the 'this' pointer. */ |
| 622 |
arg = TREE_CHAIN (arg); |
| 623 |
|
| 624 |
/* Check that from_addr has the argument list (). */ |
| 625 |
if (arg != void_list_node) |
| 626 |
{ |
| 627 |
error_at (kw, "%qE must take no arguments", addr_getter); |
| 628 |
return NULL_TREE; |
| 629 |
} |
| 630 |
|
| 631 |
tree ret_t = TREE_TYPE (fn_t); |
| 632 |
if (!same_type_p (ret_t, ptr_type_node)) |
| 633 |
{ |
| 634 |
error_at (kw, "%qE must return %qT, not %qT", |
| 635 |
addr_getter, ptr_type_node, ret_t); |
| 636 |
return NULL_TREE; |
| 637 |
} |
| 638 |
|
| 639 |
return addr_getter; |
| 640 |
} |
| 641 |
|
| 642 |
/* Get and validate HANDLE_TYPE::from_address. The resulting function, if |
| 643 |
any, will be a non-overloaded static function that takes a single void* and |
| 644 |
returns HANDLE_TYPE. If that is not the case, signals an error and returns |
| 645 |
NULL_TREE. */ |
| 646 |
|
| 647 |
static tree |
| 648 |
get_handle_type_from_address (location_t kw, tree handle_type) |
| 649 |
{ |
| 650 |
tree from_addr = lookup_member (handle_type, coro_from_address_identifier, 1, |
| 651 |
0, tf_warning_or_error); |
| 652 |
if (!from_addr || from_addr == error_mark_node) |
| 653 |
{ |
| 654 |
qualified_name_lookup_error (handle_type, coro_from_address_identifier, |
| 655 |
error_mark_node, kw); |
| 656 |
return NULL_TREE; |
| 657 |
} |
| 658 |
if (!BASELINK_P (from_addr) |
| 659 |
|| TREE_CODE (TREE_TYPE (from_addr)) != FUNCTION_TYPE) |
| 660 |
{ |
| 661 |
error_at (kw, "%qE must be a non-overloaded static function", from_addr); |
| 662 |
return NULL_TREE; |
| 663 |
} |
| 664 |
|
| 665 |
tree fn_t = TREE_TYPE (from_addr); |
| 666 |
tree arg = TYPE_ARG_TYPES (fn_t); |
| 667 |
/* Check that from_addr has the argument list (void*). */ |
| 668 |
if (!arg |
| 669 |
|| !same_type_p (TREE_VALUE (arg), ptr_type_node) |
| 670 |
|| TREE_CHAIN (arg) != void_list_node) |
| 671 |
{ |
| 672 |
error_at (kw, "%qE must take a single %qT", from_addr, ptr_type_node); |
| 673 |
return NULL_TREE; |
| 674 |
} |
| 675 |
|
| 676 |
tree ret_t = TREE_TYPE (fn_t); |
| 677 |
if (!same_type_p (ret_t, handle_type)) |
| 678 |
{ |
| 679 |
error_at (kw, "%qE must return %qT, not %qT", |
| 680 |
from_addr, handle_type, ret_t); |
| 681 |
return NULL_TREE; |
| 682 |
} |
| 683 |
|
| 684 |
return from_addr; |
| 685 |
} |
| 686 |
|
| 687 |
static tree |
| 688 |
instantiate_coro_handle_for_promise_type (location_t kw, tree promise_type) |
| 689 |
{ |
| 690 |
/* So now build up a type list for the template, one entry, the promise. */ |
| 691 |
tree targ = make_tree_vec (1); |
| 692 |
TREE_VEC_ELT (targ, 0) = promise_type; |
| 693 |
tree handle_type |
| 694 |
= lookup_template_class (coro_handle_identifier, targ, |
| 695 |
/* in_decl=*/NULL_TREE, |
| 696 |
/* context=*/std_node, |
| 697 |
tf_warning_or_error); |
| 698 |
|
| 699 |
if (handle_type == error_mark_node) |
| 700 |
{ |
| 701 |
error_at (kw, "cannot instantiate a %<coroutine handle%> for" |
| 702 |
" promise type %qT", promise_type); |
| 703 |
return NULL_TREE; |
| 704 |
} |
| 705 |
|
| 706 |
return handle_type; |
| 707 |
} |
| 708 |
|
| 709 |
/* Look for the promise_type in the instantiated traits. */ |
| 710 |
|
| 711 |
static tree |
| 712 |
find_promise_type (tree traits_class) |
| 713 |
{ |
| 714 |
tree promise_type |
| 715 |
= lookup_member (traits_class, coro_promise_type_identifier, |
| 716 |
/* protect=*/1, /*want_type=*/true, tf_warning_or_error); |
| 717 |
|
| 718 |
if (promise_type) |
| 719 |
promise_type |
| 720 |
= complete_type_or_else (TREE_TYPE (promise_type), promise_type); |
| 721 |
|
| 722 |
/* NULL_TREE on fail. */ |
| 723 |
return promise_type; |
| 724 |
} |
| 725 |
|
| 726 |
/* Perform initialization of the coroutine processor state, if not done |
| 727 |
before. */ |
| 728 |
|
| 729 |
static bool |
| 730 |
ensure_coro_initialized (location_t loc) |
| 731 |
{ |
| 732 |
if (!coro_initialized) |
| 733 |
{ |
| 734 |
/* Trees we only need to create once. |
| 735 |
Set up the identifiers we will use. */ |
| 736 |
coro_init_identifiers (); |
| 737 |
|
| 738 |
/* Coroutine traits template. */ |
| 739 |
coro_traits_templ = find_coro_traits_template_decl (loc); |
| 740 |
if (coro_traits_templ == NULL_TREE) |
| 741 |
return false; |
| 742 |
|
| 743 |
/* coroutine_handle<> template. */ |
| 744 |
coro_handle_templ = find_coro_handle_template_decl (loc); |
| 745 |
if (coro_handle_templ == NULL_TREE) |
| 746 |
return false; |
| 747 |
|
| 748 |
/* We can also instantiate the void coroutine_handle<> */ |
| 749 |
void_coro_handle_type |
| 750 |
= instantiate_coro_handle_for_promise_type (loc, void_type_node); |
| 751 |
if (void_coro_handle_type == NULL_TREE) |
| 752 |
return false; |
| 753 |
|
| 754 |
void_coro_handle_address |
| 755 |
= get_handle_type_address (loc, void_coro_handle_type); |
| 756 |
if (!void_coro_handle_address) |
| 757 |
return false; |
| 758 |
|
| 759 |
/* A table to hold the state, per coroutine decl. */ |
| 760 |
gcc_checking_assert (coroutine_info_table == NULL); |
| 761 |
coroutine_info_table = |
| 762 |
hash_table<coroutine_info_hasher>::create_ggc (11); |
| 763 |
|
| 764 |
if (coroutine_info_table == NULL) |
| 765 |
return false; |
| 766 |
|
| 767 |
coro_initialized = true; |
| 768 |
} |
| 769 |
return true; |
| 770 |
} |
| 771 |
|
| 772 |
/* Try to get the coroutine traits class. */ |
| 773 |
static tree |
| 774 |
coro_get_traits_class (tree fndecl, location_t loc) |
| 775 |
{ |
| 776 |
gcc_assert (fndecl != NULL_TREE); |
| 777 |
gcc_assert (coro_initialized); |
| 778 |
|
| 779 |
coroutine_info *coro_info = get_or_insert_coroutine_info (fndecl); |
| 780 |
auto& traits_type = coro_info->traits_type; |
| 781 |
if (!traits_type) |
| 782 |
traits_type = instantiate_coro_traits (fndecl, loc); |
| 783 |
return traits_type; |
| 784 |
} |
| 785 |
|
| 786 |
static bool |
| 787 |
coro_promise_type_found_p (tree fndecl, location_t loc) |
| 788 |
{ |
| 789 |
gcc_assert (fndecl != NULL_TREE); |
| 790 |
|
| 791 |
if (!ensure_coro_initialized (loc)) |
| 792 |
return false; |
| 793 |
|
| 794 |
/* Save the coroutine data on the side to avoid the overhead on every |
| 795 |
function decl tree. */ |
| 796 |
|
| 797 |
coroutine_info *coro_info = get_or_insert_coroutine_info (fndecl); |
| 798 |
/* Without this, we cannot really proceed. */ |
| 799 |
gcc_checking_assert (coro_info); |
| 800 |
|
| 801 |
/* If we don't already have a current promise type, try to look it up. */ |
| 802 |
if (coro_info->promise_type == NULL_TREE) |
| 803 |
{ |
| 804 |
/* Get the coroutine traits template class instance for the function |
| 805 |
signature we have - coroutine_traits <R, ...> */ |
| 806 |
|
| 807 |
tree templ_class = coro_get_traits_class (fndecl, loc); |
| 808 |
|
| 809 |
/* Find the promise type for that. */ |
| 810 |
coro_info->promise_type = find_promise_type (templ_class); |
| 811 |
|
| 812 |
/* If we don't find it, punt on the rest. */ |
| 813 |
if (coro_info->promise_type == NULL_TREE) |
| 814 |
{ |
| 815 |
if (!coro_info->coro_promise_error_emitted) |
| 816 |
error_at (loc, "unable to find the promise type for" |
| 817 |
" this coroutine"); |
| 818 |
coro_info->coro_promise_error_emitted = true; |
| 819 |
return false; |
| 820 |
} |
| 821 |
|
| 822 |
/* Test for errors in the promise type that can be determined now. */ |
| 823 |
tree has_ret_void = lookup_member (coro_info->promise_type, |
| 824 |
coro_return_void_identifier, |
| 825 |
/*protect=*/1, /*want_type=*/0, |
| 826 |
tf_none); |
| 827 |
tree has_ret_val = lookup_member (coro_info->promise_type, |
| 828 |
coro_return_value_identifier, |
| 829 |
/*protect=*/1, /*want_type=*/0, |
| 830 |
tf_none); |
| 831 |
if (has_ret_void && has_ret_val) |
| 832 |
{ |
| 833 |
auto_diagnostic_group d; |
| 834 |
location_t ploc = DECL_SOURCE_LOCATION (fndecl); |
| 835 |
if (!coro_info->coro_co_return_error_emitted) |
| 836 |
error_at (ploc, "the coroutine promise type %qT declares both" |
| 837 |
" %<return_value%> and %<return_void%>", |
| 838 |
coro_info->promise_type); |
| 839 |
inform (DECL_SOURCE_LOCATION (BASELINK_FUNCTIONS (has_ret_void)), |
| 840 |
"%<return_void%> declared here"); |
| 841 |
has_ret_val = BASELINK_FUNCTIONS (has_ret_val); |
| 842 |
const char *message = "%<return_value%> declared here"; |
| 843 |
if (TREE_CODE (has_ret_val) == OVERLOAD) |
| 844 |
{ |
| 845 |
has_ret_val = OVL_FIRST (has_ret_val); |
| 846 |
message = "%<return_value%> first declared here"; |
| 847 |
} |
| 848 |
inform (DECL_SOURCE_LOCATION (has_ret_val), message); |
| 849 |
coro_info->coro_co_return_error_emitted = true; |
| 850 |
return false; |
| 851 |
} |
| 852 |
|
| 853 |
/* Try to find the handle type for the promise. */ |
| 854 |
tree handle_type |
| 855 |
= instantiate_coro_handle_for_promise_type (loc, coro_info->promise_type); |
| 856 |
if (handle_type == NULL_TREE) |
| 857 |
return false; |
| 858 |
tree from_address = get_handle_type_from_address (loc, handle_type); |
| 859 |
if (from_address == NULL_TREE) |
| 860 |
return false; |
| 861 |
|
| 862 |
/* Complete this, we're going to use it. */ |
| 863 |
coro_info->handle_type = complete_type_or_else (handle_type, fndecl); |
| 864 |
coro_info->from_address = from_address; |
| 865 |
|
| 866 |
/* Diagnostic would be emitted by complete_type_or_else. */ |
| 867 |
if (!coro_info->handle_type) |
| 868 |
return false; |
| 869 |
|
| 870 |
/* Build a proxy for a handle to "self" as the param to |
| 871 |
await_suspend() calls. */ |
| 872 |
coro_info->self_h_proxy |
| 873 |
= build_lang_decl (VAR_DECL, coro_self_handle_id, |
| 874 |
coro_info->handle_type); |
| 875 |
|
| 876 |
/* Build a proxy for the promise so that we can perform lookups. */ |
| 877 |
coro_info->promise_proxy |
| 878 |
= build_lang_decl (VAR_DECL, coro_promise_id, |
| 879 |
coro_info->promise_type); |
| 880 |
|
| 881 |
/* Note where we first saw a coroutine keyword. */ |
| 882 |
coro_info->first_coro_keyword = loc; |
| 883 |
} |
| 884 |
|
| 885 |
return true; |
| 886 |
} |
| 887 |
|
| 888 |
/* Map from actor or destroyer to ramp. */ |
| 889 |
static GTY(()) hash_map<tree, tree> *to_ramp; |
| 890 |
|
| 891 |
/* Given a tree that is an actor or destroy, find the ramp function. */ |
| 892 |
|
| 893 |
tree |
| 894 |
coro_get_ramp_function (tree decl) |
| 895 |
{ |
| 896 |
if (!to_ramp) |
| 897 |
return NULL_TREE; |
| 898 |
tree *p = to_ramp->get (decl); |
| 899 |
if (p) |
| 900 |
return *p; |
| 901 |
return NULL_TREE; |
| 902 |
} |
| 903 |
|
| 904 |
/* Given the DECL for a ramp function (the user's original declaration) return |
| 905 |
the actor function if it has been defined. */ |
| 906 |
|
| 907 |
tree |
| 908 |
coro_get_actor_function (tree decl) |
| 909 |
{ |
| 910 |
if (coroutine_info *info = get_coroutine_info (decl)) |
| 911 |
return info->actor_decl; |
| 912 |
|
| 913 |
return NULL_TREE; |
| 914 |
} |
| 915 |
|
| 916 |
/* Given the DECL for a ramp function (the user's original declaration) return |
| 917 |
the destroy function if it has been defined. */ |
| 918 |
|
| 919 |
tree |
| 920 |
coro_get_destroy_function (tree decl) |
| 921 |
{ |
| 922 |
if (coroutine_info *info = get_coroutine_info (decl)) |
| 923 |
return info->destroy_decl; |
| 924 |
|
| 925 |
return NULL_TREE; |
| 926 |
} |
| 927 |
|
| 928 |
/* Given a CO_AWAIT_EXPR AWAIT_EXPR, return its resume call. */ |
| 929 |
|
| 930 |
tree |
| 931 |
co_await_get_resume_call (tree await_expr) |
| 932 |
{ |
| 933 |
gcc_checking_assert (TREE_CODE (await_expr) == CO_AWAIT_EXPR); |
| 934 |
tree vec = TREE_OPERAND (await_expr, 3); |
| 935 |
if (!vec) |
| 936 |
return nullptr; |
| 937 |
return TREE_VEC_ELT (vec, 2); |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
/* These functions assumes that the caller has verified that the state for |
| 942 |
the decl has been initialized, we try to minimize work here. */ |
| 943 |
|
| 944 |
static tree |
| 945 |
get_coroutine_promise_type (tree decl) |
| 946 |
{ |
| 947 |
if (coroutine_info *info = get_coroutine_info (decl)) |
| 948 |
return info->promise_type; |
| 949 |
|
| 950 |
return NULL_TREE; |
| 951 |
} |
| 952 |
|
| 953 |
static tree |
| 954 |
get_coroutine_handle_type (tree decl) |
| 955 |
{ |
| 956 |
if (coroutine_info *info = get_coroutine_info (decl)) |
| 957 |
return info->handle_type; |
| 958 |
|
| 959 |
return NULL_TREE; |
| 960 |
} |
| 961 |
|
| 962 |
static tree |
| 963 |
get_coroutine_self_handle_proxy (tree decl) |
| 964 |
{ |
| 965 |
if (coroutine_info *info = get_coroutine_info (decl)) |
| 966 |
return info->self_h_proxy; |
| 967 |
|
| 968 |
return NULL_TREE; |
| 969 |
} |
| 970 |
|
| 971 |
static tree |
| 972 |
get_coroutine_promise_proxy (tree decl) |
| 973 |
{ |
| 974 |
if (coroutine_info *info = get_coroutine_info (decl)) |
| 975 |
return info->promise_proxy; |
| 976 |
|
| 977 |
return NULL_TREE; |
| 978 |
} |
| 979 |
|
| 980 |
static tree |
| 981 |
get_coroutine_from_address (tree decl) |
| 982 |
{ |
| 983 |
if (coroutine_info *info = get_coroutine_info (decl)) |
| 984 |
return info->from_address; |
| 985 |
|
| 986 |
return NULL_TREE; |
| 987 |
} |
| 988 |
|
| 989 |
static tree |
| 990 |
lookup_promise_method (tree fndecl, tree member_id, location_t loc, |
| 991 |
bool musthave) |
| 992 |
{ |
| 993 |
tree promise = get_coroutine_promise_type (fndecl); |
| 994 |
tree pm_memb |
| 995 |
= lookup_member (promise, member_id, |
| 996 |
/*protect=*/1, /*want_type=*/0, tf_warning_or_error); |
| 997 |
if (musthave && pm_memb == NULL_TREE) |
| 998 |
{ |
| 999 |
error_at (loc, "no member named %qE in %qT", member_id, promise); |
| 1000 |
return error_mark_node; |
| 1001 |
} |
| 1002 |
return pm_memb; |
| 1003 |
} |
| 1004 |
|
| 1005 |
/* Build an expression of the form p.method (args) where the p is a promise |
| 1006 |
object for the current coroutine. |
| 1007 |
OBJECT is the promise object instance to use, it may be NULL, in which case |
| 1008 |
we will use the promise_proxy instance for this coroutine. |
| 1009 |
ARGS may be NULL, for empty parm lists. */ |
| 1010 |
|
| 1011 |
static tree |
| 1012 |
coro_build_promise_expression (tree fn, tree promise_obj, tree member_id, |
| 1013 |
location_t loc, vec<tree, va_gc> **args, |
| 1014 |
bool musthave) |
| 1015 |
{ |
| 1016 |
tree meth = lookup_promise_method (fn, member_id, loc, musthave); |
| 1017 |
if (meth == error_mark_node) |
| 1018 |
return error_mark_node; |
| 1019 |
|
| 1020 |
/* If we don't find it, and it isn't needed, an empty return is OK. */ |
| 1021 |
if (!meth) |
| 1022 |
return NULL_TREE; |
| 1023 |
|
| 1024 |
tree promise |
| 1025 |
= promise_obj ? promise_obj |
| 1026 |
: get_coroutine_promise_proxy (current_function_decl); |
| 1027 |
tree expr; |
| 1028 |
if (BASELINK_P (meth)) |
| 1029 |
expr = build_new_method_call (promise, meth, args, NULL_TREE, |
| 1030 |
LOOKUP_NORMAL, NULL, tf_warning_or_error); |
| 1031 |
else |
| 1032 |
{ |
| 1033 |
expr = build_class_member_access_expr (promise, meth, NULL_TREE, |
| 1034 |
true, tf_warning_or_error); |
| 1035 |
vec<tree, va_gc> *real_args; |
| 1036 |
if (!args) |
| 1037 |
real_args = make_tree_vector (); |
| 1038 |
else |
| 1039 |
real_args = *args; |
| 1040 |
expr = build_op_call (expr, &real_args, tf_warning_or_error); |
| 1041 |
} |
| 1042 |
return expr; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* Caching get for the expression p.return_void (). */ |
| 1046 |
|
| 1047 |
static tree |
| 1048 |
get_coroutine_return_void_expr (tree decl, location_t loc, bool musthave) |
| 1049 |
{ |
| 1050 |
if (coroutine_info *info = get_coroutine_info (decl)) |
| 1051 |
{ |
| 1052 |
/* If we don't have it try to build it. */ |
| 1053 |
if (!info->return_void) |
| 1054 |
info->return_void |
| 1055 |
= coro_build_promise_expression (current_function_decl, NULL, |
| 1056 |
coro_return_void_identifier, |
| 1057 |
loc, NULL, musthave); |
| 1058 |
/* Don't return an error if it's an optional call. */ |
| 1059 |
if (!musthave && info->return_void == error_mark_node) |
| 1060 |
return NULL_TREE; |
| 1061 |
return info->return_void; |
| 1062 |
} |
| 1063 |
return musthave ? error_mark_node : NULL_TREE; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/* Lookup an Awaitable member, which should be await_ready, await_suspend |
| 1067 |
or await_resume. */ |
| 1068 |
|
| 1069 |
static tree |
| 1070 |
lookup_awaitable_member (tree await_type, tree member_id, location_t loc) |
| 1071 |
{ |
| 1072 |
tree aw_memb |
| 1073 |
= lookup_member (await_type, member_id, |
| 1074 |
/*protect=*/1, /*want_type=*/0, tf_warning_or_error); |
| 1075 |
if (aw_memb == NULL_TREE) |
| 1076 |
{ |
| 1077 |
error_at (loc, "no member named %qE in %qT", member_id, await_type); |
| 1078 |
return error_mark_node; |
| 1079 |
} |
| 1080 |
return aw_memb; |
| 1081 |
} |
| 1082 |
|
| 1083 |
/* Here we check the constraints that are common to all keywords (since the |
| 1084 |
presence of a coroutine keyword makes the function into a coroutine). */ |
| 1085 |
|
| 1086 |
static bool |
| 1087 |
coro_common_keyword_context_valid_p (tree fndecl, location_t kw_loc, |
| 1088 |
const char *kw_name) |
| 1089 |
{ |
| 1090 |
if (fndecl == NULL_TREE) |
| 1091 |
{ |
| 1092 |
error_at (kw_loc, "%qs cannot be used outside a function", kw_name); |
| 1093 |
return false; |
| 1094 |
} |
| 1095 |
|
| 1096 |
/* This is arranged in order of prohibitions in the std. */ |
| 1097 |
if (DECL_MAIN_P (fndecl)) |
| 1098 |
{ |
| 1099 |
/* [basic.start.main] 3. The function main shall not be a coroutine. */ |
| 1100 |
error_at (kw_loc, "%qs cannot be used in the %<main%> function", |
| 1101 |
kw_name); |
| 1102 |
return false; |
| 1103 |
} |
| 1104 |
|
| 1105 |
if (DECL_DECLARED_CONSTEXPR_P (fndecl)) |
| 1106 |
{ |
| 1107 |
cp_function_chain->invalid_constexpr = true; |
| 1108 |
if (!is_instantiation_of_constexpr (fndecl)) |
| 1109 |
{ |
| 1110 |
/* [dcl.constexpr] 3.3 it shall not be a coroutine. */ |
| 1111 |
error_at (kw_loc, "%qs cannot be used in a %<constexpr%> function", |
| 1112 |
kw_name); |
| 1113 |
return false; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (FNDECL_USED_AUTO (fndecl)) |
| 1118 |
{ |
| 1119 |
/* [dcl.spec.auto] 15. A function declared with a return type that uses |
| 1120 |
a placeholder type shall not be a coroutine. */ |
| 1121 |
error_at (kw_loc, |
| 1122 |
"%qs cannot be used in a function with a deduced return type", |
| 1123 |
kw_name); |
| 1124 |
return false; |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (varargs_function_p (fndecl)) |
| 1128 |
{ |
| 1129 |
/* [dcl.fct.def.coroutine] The parameter-declaration-clause of the |
| 1130 |
coroutine shall not terminate with an ellipsis that is not part |
| 1131 |
of a parameter-declaration. */ |
| 1132 |
error_at (kw_loc, |
| 1133 |
"%qs cannot be used in a varargs function", kw_name); |
| 1134 |
return false; |
| 1135 |
} |
| 1136 |
|
| 1137 |
if (DECL_CONSTRUCTOR_P (fndecl)) |
| 1138 |
{ |
| 1139 |
/* [class.ctor] 7. a constructor shall not be a coroutine. */ |
| 1140 |
error_at (kw_loc, "%qs cannot be used in a constructor", kw_name); |
| 1141 |
return false; |
| 1142 |
} |
| 1143 |
|
| 1144 |
if (DECL_DESTRUCTOR_P (fndecl)) |
| 1145 |
{ |
| 1146 |
/* [class.dtor] 21. a destructor shall not be a coroutine. */ |
| 1147 |
error_at (kw_loc, "%qs cannot be used in a destructor", kw_name); |
| 1148 |
return false; |
| 1149 |
} |
| 1150 |
|
| 1151 |
return true; |
| 1152 |
} |
| 1153 |
|
| 1154 |
/* Here we check the constraints that are not per keyword. */ |
| 1155 |
|
| 1156 |
static bool |
| 1157 |
coro_function_valid_p (tree fndecl) |
| 1158 |
{ |
| 1159 |
location_t f_loc = DECL_SOURCE_LOCATION (fndecl); |
| 1160 |
|
| 1161 |
/* For cases where fundamental information cannot be found, e.g. the |
| 1162 |
coroutine traits are missing, we need to punt early. */ |
| 1163 |
if (!coro_promise_type_found_p (fndecl, f_loc)) |
| 1164 |
return false; |
| 1165 |
|
| 1166 |
/* Since we think the function is a coroutine, that implies we parsed |
| 1167 |
a keyword that triggered this. Keywords check promise validity for |
| 1168 |
their context and thus the promise type should be known at this point. */ |
| 1169 |
if (get_coroutine_handle_type (fndecl) == NULL_TREE |
| 1170 |
|| get_coroutine_promise_type (fndecl) == NULL_TREE) |
| 1171 |
return false; |
| 1172 |
|
| 1173 |
if (current_function_returns_value || current_function_returns_null) |
| 1174 |
{ |
| 1175 |
/* TODO: record or extract positions of returns (and the first coro |
| 1176 |
keyword) so that we can add notes to the diagnostic about where |
| 1177 |
the bad keyword is and what made the function into a coro. */ |
| 1178 |
error_at (f_loc, "a %<return%> statement is not allowed in coroutine;" |
| 1179 |
" did you mean %<co_return%>?"); |
| 1180 |
return false; |
| 1181 |
} |
| 1182 |
|
| 1183 |
return true; |
| 1184 |
} |
| 1185 |
|
| 1186 |
enum suspend_point_kind { |
| 1187 |
CO_AWAIT_SUSPEND_POINT = 0, |
| 1188 |
CO_YIELD_SUSPEND_POINT, |
| 1189 |
INITIAL_SUSPEND_POINT, |
| 1190 |
FINAL_SUSPEND_POINT |
| 1191 |
}; |
| 1192 |
|
| 1193 |
/* Helper function to build a named variable for the temps we use for each |
| 1194 |
await point. The root of the name is determined by SUSPEND_KIND, and |
| 1195 |
the variable is of type V_TYPE. The awaitable number is reset each time |
| 1196 |
we encounter a final suspend. */ |
| 1197 |
|
| 1198 |
static tree |
| 1199 |
get_awaitable_var (suspend_point_kind suspend_kind, tree v_type) |
| 1200 |
{ |
| 1201 |
auto cinfo = get_coroutine_info (current_function_decl); |
| 1202 |
gcc_checking_assert (cinfo); |
| 1203 |
char *buf; |
| 1204 |
switch (suspend_kind) |
| 1205 |
{ |
| 1206 |
default: buf = xasprintf ("Aw%d", cinfo->awaitable_number++); break; |
| 1207 |
case CO_YIELD_SUSPEND_POINT: |
| 1208 |
buf = xasprintf ("Yd%d", cinfo->awaitable_number++); |
| 1209 |
break; |
| 1210 |
case INITIAL_SUSPEND_POINT: buf = xasprintf ("Is"); break; |
| 1211 |
case FINAL_SUSPEND_POINT: buf = xasprintf ("Fs"); break; |
| 1212 |
} |
| 1213 |
tree ret = get_identifier (buf); |
| 1214 |
free (buf); |
| 1215 |
ret = build_lang_decl (VAR_DECL, ret, v_type); |
| 1216 |
DECL_ARTIFICIAL (ret) = true; |
| 1217 |
return ret; |
| 1218 |
} |
| 1219 |
|
| 1220 |
/* Helpers to diagnose missing noexcept on final await expressions. */ |
| 1221 |
|
| 1222 |
static bool |
| 1223 |
coro_diagnose_throwing_fn (tree fndecl) |
| 1224 |
{ |
| 1225 |
if (!TYPE_NOTHROW_P (TREE_TYPE (fndecl))) |
| 1226 |
{ |
| 1227 |
auto_diagnostic_group d; |
| 1228 |
location_t f_loc = cp_expr_loc_or_loc (fndecl, |
| 1229 |
DECL_SOURCE_LOCATION (fndecl)); |
| 1230 |
error_at (f_loc, "the expression %qE is required to be non-throwing", |
| 1231 |
fndecl); |
| 1232 |
inform (f_loc, "must be declared with %<noexcept(true)%>"); |
| 1233 |
return true; |
| 1234 |
} |
| 1235 |
return false; |
| 1236 |
} |
| 1237 |
|
| 1238 |
static bool |
| 1239 |
coro_diagnose_throwing_final_aw_expr (tree expr) |
| 1240 |
{ |
| 1241 |
if (TREE_CODE (expr) == TARGET_EXPR) |
| 1242 |
expr = TARGET_EXPR_INITIAL (expr); |
| 1243 |
tree fn = NULL_TREE; |
| 1244 |
if (TREE_CODE (expr) == CALL_EXPR) |
| 1245 |
fn = CALL_EXPR_FN (expr); |
| 1246 |
else if (TREE_CODE (expr) == AGGR_INIT_EXPR) |
| 1247 |
fn = AGGR_INIT_EXPR_FN (expr); |
| 1248 |
else if (TREE_CODE (expr) == CONSTRUCTOR) |
| 1249 |
return false; |
| 1250 |
else |
| 1251 |
{ |
| 1252 |
gcc_checking_assert (0 && "unhandled expression type"); |
| 1253 |
return false; |
| 1254 |
} |
| 1255 |
fn = TREE_OPERAND (fn, 0); |
| 1256 |
return coro_diagnose_throwing_fn (fn); |
| 1257 |
} |
| 1258 |
|
| 1259 |
/* Build a co_await suitable for later expansion. */ |
| 1260 |
|
| 1261 |
tree |
| 1262 |
build_template_co_await_expr (location_t kw, tree type, tree expr, tree kind) |
| 1263 |
{ |
| 1264 |
tree aw_expr = build5_loc (kw, CO_AWAIT_EXPR, type, expr, |
| 1265 |
NULL_TREE, NULL_TREE, NULL_TREE, |
| 1266 |
kind); |
| 1267 |
TREE_SIDE_EFFECTS (aw_expr) = true; |
| 1268 |
return aw_expr; |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* Is EXPR an lvalue that will refer to the same object after a resume? |
| 1272 |
|
| 1273 |
This is close to asking tree_invariant_p of its address, but that doesn't |
| 1274 |
distinguish temporaries from other variables. */ |
| 1275 |
|
| 1276 |
static bool |
| 1277 |
is_stable_lvalue (tree expr) |
| 1278 |
{ |
| 1279 |
if (TREE_SIDE_EFFECTS (expr)) |
| 1280 |
return false; |
| 1281 |
|
| 1282 |
for (; handled_component_p (expr); |
| 1283 |
expr = TREE_OPERAND (expr, 0)) |
| 1284 |
{ |
| 1285 |
if (TREE_CODE (expr) == ARRAY_REF |
| 1286 |
&& !TREE_CONSTANT (TREE_OPERAND (expr, 1))) |
| 1287 |
return false; |
| 1288 |
} |
| 1289 |
|
| 1290 |
return (TREE_CODE (expr) == PARM_DECL |
| 1291 |
|| (VAR_P (expr) && !is_local_temp (expr))); |
| 1292 |
} |
| 1293 |
|
| 1294 |
/* This performs [expr.await] bullet 3.3 and validates the interface obtained. |
| 1295 |
It is also used to build the initial and final suspend points. |
| 1296 |
|
| 1297 |
'a', 'o' and 'e' are used as per the description in the section noted. |
| 1298 |
|
| 1299 |
A, the original yield/await expr, is found at source location LOC. |
| 1300 |
|
| 1301 |
We will be constructing a CO_AWAIT_EXPR for a suspend point of one of |
| 1302 |
the four suspend_point_kind kinds. This is indicated by SUSPEND_KIND. |
| 1303 |
|
| 1304 |
In the case that we're processing a template declaration, we can't save |
| 1305 |
actual awaiter expressions as the frame type will differ between |
| 1306 |
instantiations, but we can generate them to type-check them and compute the |
| 1307 |
resulting expression type. In those cases, we will return a |
| 1308 |
template-appropriate CO_AWAIT_EXPR and throw away the rest of the results. |
| 1309 |
Such an expression requires the original co_await operand unaltered. Pass |
| 1310 |
it as ORIG_OPERAND. If it is the same as 'a', you can pass NULL_TREE (the |
| 1311 |
default) to use 'a' as the value. */ |
| 1312 |
|
| 1313 |
static tree |
| 1314 |
build_co_await (location_t loc, tree a, suspend_point_kind suspend_kind, |
| 1315 |
tree orig_operand = NULL_TREE) |
| 1316 |
{ |
| 1317 |
if (orig_operand == NULL_TREE) |
| 1318 |
orig_operand = a; |
| 1319 |
|
| 1320 |
/* Try and overload of operator co_await, .... */ |
| 1321 |
tree o; |
| 1322 |
if (MAYBE_CLASS_TYPE_P (TREE_TYPE (a))) |
| 1323 |
{ |
| 1324 |
o = build_new_op (loc, CO_AWAIT_EXPR, LOOKUP_NORMAL, a, NULL_TREE, |
| 1325 |
NULL_TREE, NULL_TREE, NULL, tf_warning_or_error); |
| 1326 |
/* If no viable functions are found, o is a. */ |
| 1327 |
if (!o || o == error_mark_node) |
| 1328 |
o = a; |
| 1329 |
else if (flag_exceptions && suspend_kind == FINAL_SUSPEND_POINT) |
| 1330 |
{ |
| 1331 |
/* We found an overload for co_await(), diagnose throwing cases. */ |
| 1332 |
if (TREE_CODE (o) == TARGET_EXPR |
| 1333 |
&& coro_diagnose_throwing_final_aw_expr (o)) |
| 1334 |
return error_mark_node; |
| 1335 |
|
| 1336 |
/* We now know that the final suspend object is distinct from the |
| 1337 |
final awaiter, so check for a non-throwing DTOR where needed. */ |
| 1338 |
if (tree dummy = cxx_maybe_build_cleanup (a, tf_none)) |
| 1339 |
{ |
| 1340 |
if (CONVERT_EXPR_P (dummy)) |
| 1341 |
dummy = TREE_OPERAND (dummy, 0); |
| 1342 |
dummy = TREE_OPERAND (CALL_EXPR_FN (dummy), 0); |
| 1343 |
if (coro_diagnose_throwing_fn (dummy)) |
| 1344 |
return error_mark_node; |
| 1345 |
} |
| 1346 |
} |
| 1347 |
} |
| 1348 |
else |
| 1349 |
o = a; /* This is most likely about to fail anyway. */ |
| 1350 |
|
| 1351 |
tree o_type = TREE_TYPE (o); |
| 1352 |
if (o_type && !VOID_TYPE_P (o_type)) |
| 1353 |
o_type = complete_type_or_else (o_type, o); |
| 1354 |
|
| 1355 |
if (!o_type || o_type == error_mark_node) |
| 1356 |
return error_mark_node; |
| 1357 |
|
| 1358 |
if (TREE_CODE (o_type) != RECORD_TYPE) |
| 1359 |
{ |
| 1360 |
if (suspend_kind == FINAL_SUSPEND_POINT) |
| 1361 |
error_at (loc, "%qs awaitable type %qT is not a structure", |
| 1362 |
"final_suspend()", o_type); |
| 1363 |
else if (suspend_kind == INITIAL_SUSPEND_POINT) |
| 1364 |
error_at (loc, "%qs awaitable type %qT is not a structure", |
| 1365 |
"initial_suspend()", o_type); |
| 1366 |
else |
| 1367 |
error_at (loc, "awaitable type %qT is not a structure", o_type); |
| 1368 |
return error_mark_node; |
| 1369 |
} |
| 1370 |
|
| 1371 |
/* Check for required awaitable members and their types. */ |
| 1372 |
tree awrd_meth |
| 1373 |
= lookup_awaitable_member (o_type, coro_await_ready_identifier, loc); |
| 1374 |
if (!awrd_meth || awrd_meth == error_mark_node) |
| 1375 |
return error_mark_node; |
| 1376 |
tree awsp_meth |
| 1377 |
= lookup_awaitable_member (o_type, coro_await_suspend_identifier, loc); |
| 1378 |
if (!awsp_meth || awsp_meth == error_mark_node) |
| 1379 |
return error_mark_node; |
| 1380 |
|
| 1381 |
/* The type of the co_await is the return type of the awaitable's |
| 1382 |
await_resume, so we need to look that up. */ |
| 1383 |
tree awrs_meth |
| 1384 |
= lookup_awaitable_member (o_type, coro_await_resume_identifier, loc); |
| 1385 |
if (!awrs_meth || awrs_meth == error_mark_node) |
| 1386 |
return error_mark_node; |
| 1387 |
|
| 1388 |
/* [expr.await]/3.3 If o would be a prvalue, the temporary |
| 1389 |
materialization conversion ([conv.rval]) is applied. */ |
| 1390 |
if (!glvalue_p (o)) |
| 1391 |
o = get_target_expr (o, tf_warning_or_error); |
| 1392 |
|
| 1393 |
/* [expr.await]/3.4 e is an lvalue referring to the result of evaluating the |
| 1394 |
(possibly-converted) o. |
| 1395 |
|
| 1396 |
So, either reuse an existing stable lvalue such as a variable or |
| 1397 |
COMPONENT_REF thereof, or create a new a coroutine state frame variable |
| 1398 |
for the awaiter, since it must persist across suspension. */ |
| 1399 |
tree e_var = NULL_TREE; |
| 1400 |
tree e_proxy = o; |
| 1401 |
if (is_stable_lvalue (o)) |
| 1402 |
o = NULL_TREE; /* Use the existing entity. */ |
| 1403 |
else /* We need a non-temp var. */ |
| 1404 |
{ |
| 1405 |
tree p_type = TREE_TYPE (o); |
| 1406 |
tree o_a = o; |
| 1407 |
if (glvalue_p (o)) |
| 1408 |
{ |
| 1409 |
/* Build a reference variable for a non-stable lvalue o. */ |
| 1410 |
p_type = cp_build_reference_type (p_type, xvalue_p (o)); |
| 1411 |
o_a = build_address (o); |
| 1412 |
o_a = cp_fold_convert (p_type, o_a); |
| 1413 |
} |
| 1414 |
e_var = get_awaitable_var (suspend_kind, p_type); |
| 1415 |
o = cp_build_init_expr (loc, e_var, o_a); |
| 1416 |
e_proxy = convert_from_reference (e_var); |
| 1417 |
} |
| 1418 |
|
| 1419 |
/* I suppose we could check that this is contextually convertible to bool. */ |
| 1420 |
tree awrd_func = NULL_TREE; |
| 1421 |
tree awrd_call |
| 1422 |
= build_new_method_call (e_proxy, awrd_meth, NULL, NULL_TREE, LOOKUP_NORMAL, |
| 1423 |
&awrd_func, tf_warning_or_error); |
| 1424 |
|
| 1425 |
if (!awrd_func || !awrd_call || awrd_call == error_mark_node) |
| 1426 |
return error_mark_node; |
| 1427 |
|
| 1428 |
/* The suspend method may return one of three types: |
| 1429 |
1. void (no special action needed). |
| 1430 |
2. bool (if true, we don't need to suspend). |
| 1431 |
3. a coroutine handle, we execute the handle.resume() call. */ |
| 1432 |
tree awsp_func = NULL_TREE; |
| 1433 |
tree h_proxy = get_coroutine_self_handle_proxy (current_function_decl); |
| 1434 |
/* expand_one_await_expression will replace the argument with a prvalue call |
| 1435 |
to from_address, so pass an rvalue now as well. */ |
| 1436 |
h_proxy = move (h_proxy); |
| 1437 |
vec<tree, va_gc> *args = make_tree_vector_single (h_proxy); |
| 1438 |
tree awsp_call |
| 1439 |
= build_new_method_call (e_proxy, awsp_meth, &args, NULL_TREE, |
| 1440 |
LOOKUP_NORMAL, &awsp_func, tf_warning_or_error); |
| 1441 |
|
| 1442 |
release_tree_vector (args); |
| 1443 |
if (!awsp_func || !awsp_call || awsp_call == error_mark_node) |
| 1444 |
return error_mark_node; |
| 1445 |
|
| 1446 |
bool ok = false; |
| 1447 |
tree susp_return_type = TREE_TYPE (TREE_TYPE (awsp_func)); |
| 1448 |
if (same_type_p (susp_return_type, void_type_node)) |
| 1449 |
ok = true; |
| 1450 |
else if (same_type_p (susp_return_type, boolean_type_node)) |
| 1451 |
ok = true; |
| 1452 |
else if (TREE_CODE (susp_return_type) == RECORD_TYPE |
| 1453 |
&& CLASS_TYPE_P (susp_return_type) |
| 1454 |
&& CLASSTYPE_TEMPLATE_INFO (susp_return_type)) |
| 1455 |
{ |
| 1456 |
tree tt = CLASSTYPE_TI_TEMPLATE (susp_return_type); |
| 1457 |
if (tt == coro_handle_templ) |
| 1458 |
ok = true; |
| 1459 |
} |
| 1460 |
|
| 1461 |
if (!ok) |
| 1462 |
{ |
| 1463 |
error_at (loc, "%<await_suspend%> must return %<void%>, %<bool%> or" |
| 1464 |
" a coroutine handle"); |
| 1465 |
return error_mark_node; |
| 1466 |
} |
| 1467 |
|
| 1468 |
/* Finally, the type of e.await_resume() is the co_await's type. */ |
| 1469 |
tree awrs_func = NULL_TREE; |
| 1470 |
tree awrs_call |
| 1471 |
= build_new_method_call (e_proxy, awrs_meth, NULL, NULL_TREE, LOOKUP_NORMAL, |
| 1472 |
&awrs_func, tf_warning_or_error); |
| 1473 |
|
| 1474 |
if (!awrs_func || !awrs_call || awrs_call == error_mark_node) |
| 1475 |
return error_mark_node; |
| 1476 |
|
| 1477 |
if (flag_exceptions && suspend_kind == FINAL_SUSPEND_POINT) |
| 1478 |
{ |
| 1479 |
if (coro_diagnose_throwing_fn (awrd_func)) |
| 1480 |
return error_mark_node; |
| 1481 |
if (coro_diagnose_throwing_fn (awsp_func)) |
| 1482 |
return error_mark_node; |
| 1483 |
if (coro_diagnose_throwing_fn (awrs_func)) |
| 1484 |
return error_mark_node; |
| 1485 |
if (tree dummy = cxx_maybe_build_cleanup (e_var, tf_none)) |
| 1486 |
{ |
| 1487 |
if (CONVERT_EXPR_P (dummy)) |
| 1488 |
dummy = TREE_OPERAND (dummy, 0); |
| 1489 |
dummy = TREE_OPERAND (CALL_EXPR_FN (dummy), 0); |
| 1490 |
if (coro_diagnose_throwing_fn (dummy)) |
| 1491 |
return error_mark_node; |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
/* We now have three call expressions, in terms of the promise, handle and |
| 1496 |
'e' proxy expression. Save them in the await expression for later |
| 1497 |
expansion. */ |
| 1498 |
|
| 1499 |
tree awaiter_calls = make_tree_vec (3); |
| 1500 |
TREE_VEC_ELT (awaiter_calls, 0) = awrd_call; /* await_ready(). */ |
| 1501 |
TREE_VEC_ELT (awaiter_calls, 1) = awsp_call; /* await_suspend(). */ |
| 1502 |
tree te = NULL_TREE; |
| 1503 |
if (TREE_CODE (awrs_call) == TARGET_EXPR) |
| 1504 |
{ |
| 1505 |
te = awrs_call; |
| 1506 |
awrs_call = TARGET_EXPR_INITIAL (awrs_call); |
| 1507 |
} |
| 1508 |
TREE_VEC_ELT (awaiter_calls, 2) = awrs_call; /* await_resume(). */ |
| 1509 |
|
| 1510 |
if (e_var) |
| 1511 |
e_proxy = e_var; |
| 1512 |
|
| 1513 |
tree awrs_type = TREE_TYPE (TREE_TYPE (awrs_func)); |
| 1514 |
tree suspend_kind_cst = build_int_cst (integer_type_node, |
| 1515 |
(int) suspend_kind); |
| 1516 |
tree await_expr = build5_loc (loc, CO_AWAIT_EXPR, |
| 1517 |
awrs_type, |
| 1518 |
a, e_proxy, o, awaiter_calls, |
| 1519 |
suspend_kind_cst); |
| 1520 |
TREE_SIDE_EFFECTS (await_expr) = true; |
| 1521 |
if (te) |
| 1522 |
{ |
| 1523 |
TREE_OPERAND (te, 1) = await_expr; |
| 1524 |
TREE_SIDE_EFFECTS (te) = true; |
| 1525 |
await_expr = te; |
| 1526 |
} |
| 1527 |
SET_EXPR_LOCATION (await_expr, loc); |
| 1528 |
|
| 1529 |
if (processing_template_decl) |
| 1530 |
return build_template_co_await_expr (loc, awrs_type, orig_operand, |
| 1531 |
suspend_kind_cst); |
| 1532 |
return convert_from_reference (await_expr); |
| 1533 |
} |
| 1534 |
|
| 1535 |
/* Returns true iff EXPR or the TRAITS_CLASS, which should be a |
| 1536 |
coroutine_traits instance, are dependent. In those cases, we can't decide |
| 1537 |
what the types of our co_{await,yield,return} expressions are, so we defer |
| 1538 |
expansion entirely. */ |
| 1539 |
|
| 1540 |
static bool |
| 1541 |
coro_dependent_p (tree expr, tree traits_class) |
| 1542 |
{ |
| 1543 |
return type_dependent_expression_p (expr) |
| 1544 |
|| dependent_type_p (traits_class); |
| 1545 |
} |
| 1546 |
|
| 1547 |
tree |
| 1548 |
finish_co_await_expr (location_t kw, tree expr) |
| 1549 |
{ |
| 1550 |
if (!expr || error_operand_p (expr)) |
| 1551 |
return error_mark_node; |
| 1552 |
|
| 1553 |
if (cp_unevaluated_operand) |
| 1554 |
{ |
| 1555 |
error_at (kw, "%qs cannot be used in an unevaluated context","co_await"); |
| 1556 |
return error_mark_node; |
| 1557 |
} |
| 1558 |
|
| 1559 |
if (!coro_common_keyword_context_valid_p (current_function_decl, kw, |
| 1560 |
"co_await")) |
| 1561 |
return error_mark_node; |
| 1562 |
|
| 1563 |
/* The current function has now become a coroutine, if it wasn't already. */ |
| 1564 |
DECL_COROUTINE_P (current_function_decl) = 1; |
| 1565 |
|
| 1566 |
/* This function will appear to have no return statement, even if it |
| 1567 |
is declared to return non-void (most likely). This is correct - we |
| 1568 |
synthesize the return for the ramp in the compiler. So suppress any |
| 1569 |
extraneous warnings during substitution. */ |
| 1570 |
suppress_warning (current_function_decl, OPT_Wreturn_type); |
| 1571 |
|
| 1572 |
/* Prepare for coroutine transformations. */ |
| 1573 |
if (!ensure_coro_initialized (kw)) |
| 1574 |
return error_mark_node; |
| 1575 |
|
| 1576 |
/* Get our traits class. */ |
| 1577 |
tree traits_class = coro_get_traits_class (current_function_decl, kw); |
| 1578 |
|
| 1579 |
/* Defer expansion when we are processing a template, unless the traits type |
| 1580 |
and expression would not be dependent. In the case that the types are |
| 1581 |
not dependent but we are processing a template declaration, we will do |
| 1582 |
most of the computation but throw away the results (except for the |
| 1583 |
await_resume type). Otherwise, if our co_await is type-dependent |
| 1584 |
(i.e. the promise type or operand type is dependent), we can't do much, |
| 1585 |
and just return early with a NULL_TREE type (indicating that we cannot |
| 1586 |
compute the type yet). */ |
| 1587 |
if (coro_dependent_p (expr, traits_class)) |
| 1588 |
return build_template_co_await_expr (kw, NULL_TREE, expr, |
| 1589 |
integer_zero_node); |
| 1590 |
|
| 1591 |
/* We must be able to look up the "await_transform" method in the scope of |
| 1592 |
the promise type, and obtain its return type. */ |
| 1593 |
if (!coro_promise_type_found_p (current_function_decl, kw)) |
| 1594 |
return error_mark_node; |
| 1595 |
|
| 1596 |
/* [expr.await] 3.2 |
| 1597 |
The incoming cast expression might be transformed by a promise |
| 1598 |
'await_transform()'. */ |
| 1599 |
tree at_meth |
| 1600 |
= lookup_promise_method (current_function_decl, |
| 1601 |
coro_await_transform_identifier, kw, |
| 1602 |
/*musthave=*/false); |
| 1603 |
if (at_meth == error_mark_node) |
| 1604 |
return error_mark_node; |
| 1605 |
|
| 1606 |
tree a = expr; |
| 1607 |
if (at_meth) |
| 1608 |
{ |
| 1609 |
/* try to build a = p.await_transform (e). */ |
| 1610 |
vec<tree, va_gc> *args = make_tree_vector_single (expr); |
| 1611 |
a = build_new_method_call (get_coroutine_promise_proxy ( |
| 1612 |
current_function_decl), |
| 1613 |
at_meth, &args, NULL_TREE, LOOKUP_NORMAL, |
| 1614 |
NULL, tf_warning_or_error); |
| 1615 |
|
| 1616 |
/* As I read the section. |
| 1617 |
We saw an await_transform method, so it's mandatory that we replace |
| 1618 |
expr with p.await_transform (expr), therefore if the method call fails |
| 1619 |
(presumably, we don't have suitable arguments) then this part of the |
| 1620 |
process fails. */ |
| 1621 |
if (a == error_mark_node) |
| 1622 |
return error_mark_node; |
| 1623 |
} |
| 1624 |
|
| 1625 |
/* Now we want to build co_await a. */ |
| 1626 |
return build_co_await (kw, a, CO_AWAIT_SUSPEND_POINT, expr); |
| 1627 |
} |
| 1628 |
|
| 1629 |
/* Take the EXPR given and attempt to build: |
| 1630 |
co_await p.yield_value (expr); |
| 1631 |
per [expr.yield] para 1. */ |
| 1632 |
|
| 1633 |
tree |
| 1634 |
finish_co_yield_expr (location_t kw, tree expr) |
| 1635 |
{ |
| 1636 |
if (!expr || error_operand_p (expr)) |
| 1637 |
return error_mark_node; |
| 1638 |
|
| 1639 |
if (cp_unevaluated_operand) |
| 1640 |
{ |
| 1641 |
error_at (kw, "%qs cannot be used in an unevaluated context","co_yield"); |
| 1642 |
return error_mark_node; |
| 1643 |
} |
| 1644 |
|
| 1645 |
/* Check the general requirements and simple syntax errors. */ |
| 1646 |
if (!coro_common_keyword_context_valid_p (current_function_decl, kw, |
| 1647 |
"co_yield")) |
| 1648 |
return error_mark_node; |
| 1649 |
|
| 1650 |
/* The current function has now become a coroutine, if it wasn't already. */ |
| 1651 |
DECL_COROUTINE_P (current_function_decl) = 1; |
| 1652 |
|
| 1653 |
/* This function will appear to have no return statement, even if it |
| 1654 |
is declared to return non-void (most likely). This is correct - we |
| 1655 |
synthesize the return for the ramp in the compiler. So suppress any |
| 1656 |
extraneous warnings during substitution. */ |
| 1657 |
suppress_warning (current_function_decl, OPT_Wreturn_type); |
| 1658 |
|
| 1659 |
/* Prepare for coroutine transformations. */ |
| 1660 |
if (!ensure_coro_initialized (kw)) |
| 1661 |
return error_mark_node; |
| 1662 |
|
| 1663 |
/* Get our traits class. */ |
| 1664 |
tree traits_class = coro_get_traits_class (current_function_decl, kw); |
| 1665 |
|
| 1666 |
/* Defer expansion when we are processing a template; see note in |
| 1667 |
finish_co_await_expr. Also note that a yield is equivalent to |
| 1668 |
|
| 1669 |
co_await p.yield_value(EXPR) |
| 1670 |
|
| 1671 |
If either p or EXPR are type-dependent, then the whole expression is |
| 1672 |
certainly type-dependent, and we can't proceed. */ |
| 1673 |
if (coro_dependent_p (expr, traits_class)) |
| 1674 |
return build2_loc (kw, CO_YIELD_EXPR, NULL_TREE, expr, NULL_TREE); |
| 1675 |
|
| 1676 |
if (!coro_promise_type_found_p (current_function_decl, kw)) |
| 1677 |
/* We must be able to look up the "yield_value" method in the scope of |
| 1678 |
the promise type, and obtain its return type. */ |
| 1679 |
return error_mark_node; |
| 1680 |
|
| 1681 |
/* [expr.yield] / 1 |
| 1682 |
Let e be the operand of the yield-expression and p be an lvalue naming |
| 1683 |
the promise object of the enclosing coroutine, then the yield-expression |
| 1684 |
is equivalent to the expression co_await p.yield_value(e). |
| 1685 |
build p.yield_value(e): */ |
| 1686 |
vec<tree, va_gc> *args = make_tree_vector_single (expr); |
| 1687 |
tree yield_call |
| 1688 |
= coro_build_promise_expression (current_function_decl, NULL, |
| 1689 |
coro_yield_value_identifier, kw, |
| 1690 |
&args, /*musthave=*/true); |
| 1691 |
release_tree_vector (args); |
| 1692 |
|
| 1693 |
/* Now build co_await p.yield_value (e). |
| 1694 |
Noting that for co_yield, there is no evaluation of any potential |
| 1695 |
promise transform_await(), so we call build_co_await directly. */ |
| 1696 |
|
| 1697 |
tree op = build_co_await (kw, yield_call, CO_YIELD_SUSPEND_POINT); |
| 1698 |
if (op != error_mark_node) |
| 1699 |
{ |
| 1700 |
if (REFERENCE_REF_P (op)) |
| 1701 |
op = TREE_OPERAND (op, 0); |
| 1702 |
/* If the await expression is wrapped in a TARGET_EXPR, then transfer |
| 1703 |
that wrapper to the CO_YIELD_EXPR, since this is just a proxy for |
| 1704 |
its contained await. Otherwise, just build the CO_YIELD_EXPR. */ |
| 1705 |
if (TREE_CODE (op) == TARGET_EXPR) |
| 1706 |
{ |
| 1707 |
tree t = TARGET_EXPR_INITIAL (op); |
| 1708 |
t = build2_loc (kw, CO_YIELD_EXPR, TREE_TYPE (t), expr, t); |
| 1709 |
TARGET_EXPR_INITIAL (op) = t; |
| 1710 |
} |
| 1711 |
else |
| 1712 |
op = build2_loc (kw, CO_YIELD_EXPR, TREE_TYPE (op), expr, op); |
| 1713 |
TREE_SIDE_EFFECTS (op) = 1; |
| 1714 |
op = convert_from_reference (op); |
| 1715 |
} |
| 1716 |
|
| 1717 |
return op; |
| 1718 |
} |
| 1719 |
|
| 1720 |
/* Check and build a co_return statement. |
| 1721 |
First that it's valid to have a co_return keyword here. |
| 1722 |
If it is, then check and build the p.return_{void(),value(expr)}. |
| 1723 |
These are built against a proxy for the promise, which will be filled |
| 1724 |
in with the actual frame version when the function is transformed. */ |
| 1725 |
|
| 1726 |
tree |
| 1727 |
finish_co_return_stmt (location_t kw, tree expr) |
| 1728 |
{ |
| 1729 |
if (expr) |
| 1730 |
STRIP_ANY_LOCATION_WRAPPER (expr); |
| 1731 |
|
| 1732 |
if (error_operand_p (expr)) |
| 1733 |
return error_mark_node; |
| 1734 |
|
| 1735 |
/* If it fails the following test, the function is not permitted to be a |
| 1736 |
coroutine, so the co_return statement is erroneous. */ |
| 1737 |
if (!coro_common_keyword_context_valid_p (current_function_decl, kw, |
| 1738 |
"co_return")) |
| 1739 |
return error_mark_node; |
| 1740 |
|
| 1741 |
/* The current function has now become a coroutine, if it wasn't |
| 1742 |
already. */ |
| 1743 |
DECL_COROUTINE_P (current_function_decl) = 1; |
| 1744 |
|
| 1745 |
/* This function will appear to have no return statement, even if it |
| 1746 |
is declared to return non-void (most likely). This is correct - we |
| 1747 |
synthesize the return for the ramp in the compiler. So suppress any |
| 1748 |
extraneous warnings during substitution. */ |
| 1749 |
suppress_warning (current_function_decl, OPT_Wreturn_type); |
| 1750 |
|
| 1751 |
/* Prepare for coroutine transformations. */ |
| 1752 |
if (!ensure_coro_initialized (kw)) |
| 1753 |
return error_mark_node; |
| 1754 |
|
| 1755 |
/* Get our traits class. */ |
| 1756 |
tree traits_class = coro_get_traits_class (current_function_decl, kw); |
| 1757 |
|
| 1758 |
if (processing_template_decl |
| 1759 |
&& check_for_bare_parameter_packs (expr)) |
| 1760 |
return error_mark_node; |
| 1761 |
|
| 1762 |
/* Defer expansion when we must and are processing a template; see note in |
| 1763 |
finish_co_await_expr. */ |
| 1764 |
if (coro_dependent_p (expr, traits_class)) |
| 1765 |
{ |
| 1766 |
/* co_return expressions are always void type, regardless of the |
| 1767 |
expression type. */ |
| 1768 |
expr = build2_loc (kw, CO_RETURN_EXPR, void_type_node, |
| 1769 |
expr, NULL_TREE); |
| 1770 |
expr = maybe_cleanup_point_expr_void (expr); |
| 1771 |
return add_stmt (expr); |
| 1772 |
} |
| 1773 |
|
| 1774 |
if (!coro_promise_type_found_p (current_function_decl, kw)) |
| 1775 |
return error_mark_node; |
| 1776 |
|
| 1777 |
/* Suppress -Wreturn-type for co_return, we need to check indirectly |
| 1778 |
whether the promise type has a suitable return_void/return_value. */ |
| 1779 |
suppress_warning (current_function_decl, OPT_Wreturn_type); |
| 1780 |
|
| 1781 |
if (!processing_template_decl && warn_sequence_point) |
| 1782 |
verify_sequence_points (expr); |
| 1783 |
|
| 1784 |
if (expr) |
| 1785 |
{ |
| 1786 |
/* If we had an id-expression obfuscated by force_paren_expr, we need |
| 1787 |
to undo it so we can try to treat it as an rvalue below. */ |
| 1788 |
expr = maybe_undo_parenthesized_ref (expr); |
| 1789 |
|
| 1790 |
if (error_operand_p (expr)) |
| 1791 |
return error_mark_node; |
| 1792 |
} |
| 1793 |
|
| 1794 |
/* If the promise object doesn't have the correct return call then |
| 1795 |
there's a mis-match between the co_return <expr> and this. */ |
| 1796 |
tree co_ret_call = error_mark_node; |
| 1797 |
if (expr == NULL_TREE || VOID_TYPE_P (TREE_TYPE (expr))) |
| 1798 |
co_ret_call |
| 1799 |
= get_coroutine_return_void_expr (current_function_decl, kw, true); |
| 1800 |
else |
| 1801 |
{ |
| 1802 |
/* [class.copy.elision] / 3. |
| 1803 |
An implicitly movable entity is a variable of automatic storage |
| 1804 |
duration that is either a non-volatile object or an rvalue reference |
| 1805 |
to a non-volatile object type. For such objects in the context of |
| 1806 |
the co_return, the overload resolution should be carried out first |
| 1807 |
treating the object as an rvalue, if that fails, then we fall back |
| 1808 |
to regular overload resolution. */ |
| 1809 |
|
| 1810 |
tree arg = expr; |
| 1811 |
if (tree moved = treat_lvalue_as_rvalue_p (expr, /*return*/true)) |
| 1812 |
arg = moved; |
| 1813 |
|
| 1814 |
releasing_vec args = make_tree_vector_single (arg); |
| 1815 |
co_ret_call |
| 1816 |
= coro_build_promise_expression (current_function_decl, NULL, |
| 1817 |
coro_return_value_identifier, kw, |
| 1818 |
&args, /*musthave=*/true); |
| 1819 |
} |
| 1820 |
|
| 1821 |
/* Makes no sense for a co-routine really. */ |
| 1822 |
if (TREE_THIS_VOLATILE (current_function_decl)) |
| 1823 |
warning_at (kw, 0, |
| 1824 |
"function declared %<noreturn%> has a" |
| 1825 |
" %<co_return%> statement"); |
| 1826 |
|
| 1827 |
expr = build2_loc (kw, CO_RETURN_EXPR, void_type_node, expr, co_ret_call); |
| 1828 |
expr = maybe_cleanup_point_expr_void (expr); |
| 1829 |
return add_stmt (expr); |
| 1830 |
} |
| 1831 |
|
| 1832 |
/* We need to validate the arguments to __builtin_coro_promise, since the |
| 1833 |
second two must be constant, and the builtins machinery doesn't seem to |
| 1834 |
deal with that properly. */ |
| 1835 |
|
| 1836 |
tree |
| 1837 |
coro_validate_builtin_call (tree call, tsubst_flags_t) |
| 1838 |
{ |
| 1839 |
tree fn = TREE_OPERAND (CALL_EXPR_FN (call), 0); |
| 1840 |
|
| 1841 |
gcc_checking_assert (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL); |
| 1842 |
switch (DECL_FUNCTION_CODE (fn)) |
| 1843 |
{ |
| 1844 |
default: |
| 1845 |
return call; |
| 1846 |
|
| 1847 |
case BUILT_IN_CORO_PROMISE: |
| 1848 |
{ |
| 1849 |
/* Argument 0 is already checked by the normal built-in machinery |
| 1850 |
Argument 1 must be a constant of size type. It probably makes |
| 1851 |
little sense if it's not a power of 2, but that isn't specified |
| 1852 |
formally. */ |
| 1853 |
tree arg = CALL_EXPR_ARG (call, 1); |
| 1854 |
location_t loc = EXPR_LOCATION (arg); |
| 1855 |
|
| 1856 |
/* We expect alignof expressions in templates. */ |
| 1857 |
if (TREE_CODE (arg) == ALIGNOF_EXPR) |
| 1858 |
; |
| 1859 |
else if (!TREE_CONSTANT (arg)) |
| 1860 |
{ |
| 1861 |
error_at (loc, "the align argument to %<__builtin_coro_promise%>" |
| 1862 |
" must be a constant"); |
| 1863 |
return error_mark_node; |
| 1864 |
} |
| 1865 |
/* Argument 2 is the direction - to / from handle address to promise |
| 1866 |
address. */ |
| 1867 |
arg = CALL_EXPR_ARG (call, 2); |
| 1868 |
loc = EXPR_LOCATION (arg); |
| 1869 |
if (!TREE_CONSTANT (arg)) |
| 1870 |
{ |
| 1871 |
error_at (loc, "the direction argument to" |
| 1872 |
" %<__builtin_coro_promise%> must be a constant"); |
| 1873 |
return error_mark_node; |
| 1874 |
} |
| 1875 |
return call; |
| 1876 |
break; |
| 1877 |
} |
| 1878 |
} |
| 1879 |
} |
| 1880 |
|
| 1881 |
/* ================= Morph and Expand. ================= |
| 1882 |
|
| 1883 |
The entry point here is morph_fn_to_coro () which is called from |
| 1884 |
finish_function () when we have completed any template expansion. |
| 1885 |
|
| 1886 |
This is preceded by helper functions that implement the phases below. |
| 1887 |
|
| 1888 |
The process proceeds in four phases. |
| 1889 |
|
| 1890 |
A Initial framing. |
| 1891 |
The user's function body is wrapped in the initial and final suspend |
| 1892 |
points and we begin building the coroutine frame. |
| 1893 |
We build empty decls for the actor and destroyer functions at this |
| 1894 |
time too. |
| 1895 |
When exceptions are enabled, the user's function body will also be |
| 1896 |
wrapped in a try-catch block with the catch invoking the promise |
| 1897 |
class 'unhandled_exception' method. |
| 1898 |
|
| 1899 |
B Analysis. |
| 1900 |
The user's function body is analyzed to determine the suspend points, |
| 1901 |
if any, and to capture local variables that might persist across such |
| 1902 |
suspensions. In most cases, it is not necessary to capture compiler |
| 1903 |
temporaries, since the tree-lowering nests the suspensions correctly. |
| 1904 |
However, in the case of a captured reference, there is a lifetime |
| 1905 |
extension to the end of the full expression - which can mean across a |
| 1906 |
suspend point in which case it must be promoted to a frame variable. |
| 1907 |
|
| 1908 |
At the conclusion of analysis, we have a conservative frame layout and |
| 1909 |
maps of the local variables to their frame entry points. |
| 1910 |
|
| 1911 |
C Build the ramp function. |
| 1912 |
Carry out the allocation for the coroutine frame (NOTE; the actual size |
| 1913 |
computation is deferred until late in the middle end to allow for future |
| 1914 |
optimizations that will be allowed to elide unused frame entries). |
| 1915 |
We build the return object. |
| 1916 |
|
| 1917 |
D Build and expand the actor and destroyer function bodies. |
| 1918 |
The destroyer is a trivial shim that sets a bit to indicate that the |
| 1919 |
destroy dispatcher should be used and then calls into the actor. |
| 1920 |
|
| 1921 |
The actor function is the implementation of the user's state machine. |
| 1922 |
The current suspend point is noted in an index. |
| 1923 |
Each suspend point is encoded as a pair of internal functions, one in |
| 1924 |
the relevant dispatcher, and one representing the suspend point. |
| 1925 |
|
| 1926 |
During this process, the user's local variables and the proxies for the |
| 1927 |
self-handle and the promise class instance are re-written to their |
| 1928 |
coroutine frame equivalents. |
| 1929 |
|
| 1930 |
The complete bodies for the ramp, actor and destroy function are passed |
| 1931 |
back to finish_function for folding and gimplification. */ |
| 1932 |
|
| 1933 |
/* Helper to build a coroutine state frame access expression |
| 1934 |
CORO_FP is a frame pointer |
| 1935 |
MEMBER_ID is an identifier for a frame field |
| 1936 |
PRESERVE_REF is true, the expression returned will have REFERENCE_TYPE if |
| 1937 |
the MEMBER does. |
| 1938 |
COMPLAIN is passed to the underlying functions. */ |
| 1939 |
|
| 1940 |
static tree |
| 1941 |
coro_build_frame_access_expr (tree coro_ref, tree member_id, bool preserve_ref, |
| 1942 |
tsubst_flags_t complain) |
| 1943 |
{ |
| 1944 |
gcc_checking_assert (INDIRECT_REF_P (coro_ref)); |
| 1945 |
tree fr_type = TREE_TYPE (coro_ref); |
| 1946 |
tree mb = lookup_member (fr_type, member_id, /*protect=*/1, /*want_type=*/0, |
| 1947 |
complain); |
| 1948 |
if (!mb || mb == error_mark_node) |
| 1949 |
return error_mark_node; |
| 1950 |
tree expr |
| 1951 |
= build_class_member_access_expr (coro_ref, mb, NULL_TREE, |
| 1952 |
preserve_ref, complain); |
| 1953 |
return expr; |
| 1954 |
} |
| 1955 |
|
| 1956 |
/* Helpers to build EXPR_STMT and void-cast EXPR_STMT, common ops. */ |
| 1957 |
|
| 1958 |
static tree |
| 1959 |
coro_build_expr_stmt (tree expr, location_t loc) |
| 1960 |
{ |
| 1961 |
return maybe_cleanup_point_expr_void (build_stmt (loc, EXPR_STMT, expr)); |
| 1962 |
} |
| 1963 |
|
| 1964 |
static tree |
| 1965 |
coro_build_cvt_void_expr_stmt (tree expr, location_t loc) |
| 1966 |
{ |
| 1967 |
tree t = build1 (CONVERT_EXPR, void_type_node, expr); |
| 1968 |
return coro_build_expr_stmt (t, loc); |
| 1969 |
} |
| 1970 |
|
| 1971 |
/* Helpers to build an artificial var, with location LOC, NAME and TYPE, in |
| 1972 |
CTX, and with initializer INIT. */ |
| 1973 |
|
| 1974 |
static tree |
| 1975 |
coro_build_artificial_var (location_t loc, tree name, tree type, tree ctx, |
| 1976 |
tree init) |
| 1977 |
{ |
| 1978 |
tree res = build_lang_decl (VAR_DECL, name, type); |
| 1979 |
DECL_SOURCE_LOCATION (res) = loc; |
| 1980 |
DECL_CONTEXT (res) = ctx; |
| 1981 |
DECL_ARTIFICIAL (res) = true; |
| 1982 |
DECL_INITIAL (res) = init; |
| 1983 |
return res; |
| 1984 |
} |
| 1985 |
|
| 1986 |
/* As above, except allowing the name to be a string. */ |
| 1987 |
|
| 1988 |
static tree |
| 1989 |
coro_build_artificial_var (location_t loc, const char *name, tree type, |
| 1990 |
tree ctx, tree init) |
| 1991 |
{ |
| 1992 |
return coro_build_artificial_var (loc, get_identifier (name), |
| 1993 |
type, ctx, init); |
| 1994 |
} |
| 1995 |
|
| 1996 |
/* As for coro_build_artificial_var, except that we push this decl into |
| 1997 |
the current binding scope and add the decl_expr. */ |
| 1998 |
|
| 1999 |
static tree |
| 2000 |
coro_build_and_push_artificial_var (location_t loc, const char *name, |
| 2001 |
tree type, tree ctx, tree init) |
| 2002 |
{ |
| 2003 |
tree v = coro_build_artificial_var (loc, name, type, ctx, init); |
| 2004 |
v = pushdecl (v); |
| 2005 |
add_decl_expr (v); |
| 2006 |
return v; |
| 2007 |
} |
| 2008 |
|
| 2009 |
/* Build a var NAME of TYPE in CTX and with INIT; add a DECL_VALUE_EXPR that |
| 2010 |
refers to BASE.FIELD. */ |
| 2011 |
|
| 2012 |
static tree |
| 2013 |
coro_build_artificial_var_with_dve (location_t loc, tree name, tree type, |
| 2014 |
tree ctx, tree init, tree base, |
| 2015 |
tree field = NULL_TREE) |
| 2016 |
{ |
| 2017 |
tree v = coro_build_artificial_var (loc, name, type, ctx, init); |
| 2018 |
if (field == NULL_TREE) |
| 2019 |
field = name; |
| 2020 |
tree dve = coro_build_frame_access_expr (base, field, true, |
| 2021 |
tf_warning_or_error); |
| 2022 |
SET_DECL_VALUE_EXPR (v, dve); |
| 2023 |
DECL_HAS_VALUE_EXPR_P (v) = true; |
| 2024 |
return v; |
| 2025 |
} |
| 2026 |
|
| 2027 |
/* As for coro_build_artificial_var_with_dve, but push into the current binding |
| 2028 |
scope and add the decl_expr. */ |
| 2029 |
|
| 2030 |
static tree |
| 2031 |
coro_build_and_push_artificial_var_with_dve (location_t loc, tree name, |
| 2032 |
tree type, tree ctx, tree init, |
| 2033 |
tree base, tree field = NULL_TREE) |
| 2034 |
{ |
| 2035 |
tree v = coro_build_artificial_var_with_dve (loc, name, type, ctx, init, |
| 2036 |
base, field); |
| 2037 |
v = pushdecl (v); |
| 2038 |
add_decl_expr (v); |
| 2039 |
return v; |
| 2040 |
} |
| 2041 |
|
| 2042 |
/* 2. Create a named label in the specified context. */ |
| 2043 |
|
| 2044 |
static tree |
| 2045 |
create_named_label_with_ctx (location_t loc, const char *name, tree ctx) |
| 2046 |
{ |
| 2047 |
tree lab_id = get_identifier (name); |
| 2048 |
tree lab = define_label (loc, lab_id); |
| 2049 |
DECL_CONTEXT (lab) = ctx; |
| 2050 |
DECL_ARTIFICIAL (lab) = true; |
| 2051 |
TREE_USED (lab) = true; |
| 2052 |
return lab; |
| 2053 |
} |
| 2054 |
|
| 2055 |
struct proxy_replace |
| 2056 |
{ |
| 2057 |
tree from, to; |
| 2058 |
}; |
| 2059 |
|
| 2060 |
static tree |
| 2061 |
replace_proxy (tree *here, int *do_subtree, void *d) |
| 2062 |
{ |
| 2063 |
proxy_replace *data = (proxy_replace *) d; |
| 2064 |
|
| 2065 |
if (*here == data->from) |
| 2066 |
{ |
| 2067 |
*here = data->to; |
| 2068 |
*do_subtree = 0; |
| 2069 |
} |
| 2070 |
else |
| 2071 |
*do_subtree = 1; |
| 2072 |
return NULL_TREE; |
| 2073 |
} |
| 2074 |
|
| 2075 |
/* Support for expansion of co_await statements. */ |
| 2076 |
|
| 2077 |
struct coro_aw_data |
| 2078 |
{ |
| 2079 |
tree actor_fn; /* Decl for context. */ |
| 2080 |
tree coro_fp; /* Frame pointer var. */ |
| 2081 |
tree resume_idx; /* This is the index var in the frame. */ |
| 2082 |
tree i_a_r_c; /* initial suspend await_resume() was called if true. */ |
| 2083 |
tree cleanup; /* This is where to go once we complete local destroy. */ |
| 2084 |
tree cororet; /* This is where to go if we suspend. */ |
| 2085 |
tree corocont; /* This is where to go if we continue. */ |
| 2086 |
tree dispatch; /* This is where we go if we restart the dispatch. */ |
| 2087 |
tree conthand; /* This is the handle for a continuation. */ |
| 2088 |
tree handle_type; /* Handle type for this coroutine... */ |
| 2089 |
tree hfa_m; /* ... and handle.from_address() for this. */ |
| 2090 |
unsigned index; /* This is our current resume index. */ |
| 2091 |
}; |
| 2092 |
|
| 2093 |
/* Lightweight search for the first await expression in tree-walk order. |
| 2094 |
returns: |
| 2095 |
The first await expression found in STMT. |
| 2096 |
NULL_TREE if there are none. |
| 2097 |
So can be used to determine if the statement needs to be processed for |
| 2098 |
awaits. */ |
| 2099 |
|
| 2100 |
static tree |
| 2101 |
co_await_find_in_subtree (tree *stmt, int *, void *d) |
| 2102 |
{ |
| 2103 |
tree **p = (tree **) d; |
| 2104 |
if (TREE_CODE (*stmt) == CO_AWAIT_EXPR) |
| 2105 |
{ |
| 2106 |
*p = stmt; |
| 2107 |
return *stmt; |
| 2108 |
} |
| 2109 |
return NULL_TREE; |
| 2110 |
} |
| 2111 |
|
| 2112 |
/* Starting with a statement: |
| 2113 |
|
| 2114 |
stmt => some tree containing one or more await expressions. |
| 2115 |
|
| 2116 |
We replace the statement with: |
| 2117 |
<STATEMENT_LIST> { |
| 2118 |
initialize awaitable |
| 2119 |
if (!ready) |
| 2120 |
{ |
| 2121 |
suspension context. |
| 2122 |
} |
| 2123 |
resume: |
| 2124 |
revised statement with one await expression rewritten to its |
| 2125 |
await_resume() return value. |
| 2126 |
} |
| 2127 |
|
| 2128 |
We then recurse into the initializer and the revised statement |
| 2129 |
repeating this replacement until there are no more await expressions |
| 2130 |
in either. */ |
| 2131 |
|
| 2132 |
static tree * |
| 2133 |
expand_one_await_expression (tree *expr, tree *await_expr, void *d) |
| 2134 |
{ |
| 2135 |
coro_aw_data *data = (coro_aw_data *) d; |
| 2136 |
|
| 2137 |
tree saved_statement = *expr; |
| 2138 |
tree saved_co_await = *await_expr; |
| 2139 |
|
| 2140 |
tree actor = data->actor_fn; |
| 2141 |
location_t loc = EXPR_LOCATION (*expr); |
| 2142 |
tree var = TREE_OPERAND (saved_co_await, 1); /* frame slot. */ |
| 2143 |
tree init_expr = TREE_OPERAND (saved_co_await, 2); /* initializer. */ |
| 2144 |
tree awaiter_calls = TREE_OPERAND (saved_co_await, 3); |
| 2145 |
|
| 2146 |
tree source = TREE_OPERAND (saved_co_await, 4); |
| 2147 |
bool is_final |
| 2148 |
= (source && TREE_INT_CST_LOW (source) == (int) FINAL_SUSPEND_POINT); |
| 2149 |
bool is_initial |
| 2150 |
= (source && TREE_INT_CST_LOW (source) == (int) INITIAL_SUSPEND_POINT); |
| 2151 |
|
| 2152 |
/* Build labels for the destinations of the control flow when we are resuming |
| 2153 |
or destroying. */ |
| 2154 |
int resume_point = data->index; |
| 2155 |
char *buf = xasprintf ("destroy.%d", resume_point); |
| 2156 |
tree destroy_label = create_named_label_with_ctx (loc, buf, actor); |
| 2157 |
free (buf); |
| 2158 |
buf = xasprintf ("resume.%d", resume_point); |
| 2159 |
tree resume_label = create_named_label_with_ctx (loc, buf, actor); |
| 2160 |
free (buf); |
| 2161 |
|
| 2162 |
/* This will contain our expanded expression and replace the original |
| 2163 |
expression. */ |
| 2164 |
tree stmt_list = push_stmt_list (); |
| 2165 |
tree *await_init = NULL; |
| 2166 |
|
| 2167 |
bool needs_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var)); |
| 2168 |
if (!init_expr) |
| 2169 |
needs_dtor = false; /* No need, the var's lifetime is managed elsewhere. */ |
| 2170 |
else |
| 2171 |
{ |
| 2172 |
finish_expr_stmt (init_expr); |
| 2173 |
/* We have an initializer, which might itself contain await exprs. */ |
| 2174 |
await_init = tsi_stmt_ptr (tsi_last (stmt_list)); |
| 2175 |
} |
| 2176 |
|
| 2177 |
/* Use the await_ready() call to test if we need to suspend. */ |
| 2178 |
tree ready_cond = TREE_VEC_ELT (awaiter_calls, 0); /* await_ready(). */ |
| 2179 |
|
| 2180 |
/* We will resume (or continue) at the following index. */ |
| 2181 |
tree resume_idx = build_int_cst (short_unsigned_type_node, data->index); |
| 2182 |
tree r = cp_build_init_expr (data->resume_idx, resume_idx); |
| 2183 |
finish_expr_stmt (r); |
| 2184 |
|
| 2185 |
/* Convert to bool, if necessary. */ |
| 2186 |
if (TREE_CODE (TREE_TYPE (ready_cond)) != BOOLEAN_TYPE) |
| 2187 |
ready_cond = cp_convert (boolean_type_node, ready_cond, |
| 2188 |
tf_warning_or_error); |
| 2189 |
tree susp_if = begin_if_stmt (); |
| 2190 |
/* Be aggressive in folding here, since there are a significant number of |
| 2191 |
cases where the ready condition is constant. */ |
| 2192 |
ready_cond = invert_truthvalue_loc (loc, ready_cond); |
| 2193 |
finish_if_stmt_cond (ready_cond, susp_if); |
| 2194 |
|
| 2195 |
/* Find out what we have to do with the awaiter's suspend method. |
| 2196 |
[expr.await] |
| 2197 |
(5.1) If the result of await-ready is false, the coroutine is considered |
| 2198 |
suspended. Then: |
| 2199 |
(5.1.1) If the type of await-suspend is std::coroutine_handle<Z>, |
| 2200 |
await-suspend.resume() is evaluated. |
| 2201 |
(5.1.2) if the type of await-suspend is bool, await-suspend is evaluated, |
| 2202 |
and the coroutine is resumed if the result is false. |
| 2203 |
(5.1.3) Otherwise, await-suspend is evaluated. */ |
| 2204 |
|
| 2205 |
tree suspend = TREE_VEC_ELT (awaiter_calls, 1); /* await_suspend(). */ |
| 2206 |
tree susp_type = TREE_TYPE (suspend); |
| 2207 |
tree susp_call = suspend; |
| 2208 |
if (TREE_CODE (suspend) == TARGET_EXPR) |
| 2209 |
susp_call = TARGET_EXPR_INITIAL (suspend); |
| 2210 |
gcc_checking_assert (TREE_CODE (susp_call) == CALL_EXPR); |
| 2211 |
tree dummy_ch = build_dummy_object (data->handle_type); |
| 2212 |
r = fold_convert (build_pointer_type (void_type_node), data->coro_fp); |
| 2213 |
vec<tree, va_gc> *args = make_tree_vector_single (r); |
| 2214 |
tree hfa = cp_fold_rvalue ( |
| 2215 |
build_new_method_call (dummy_ch, data->hfa_m, &args, NULL_TREE, |
| 2216 |
LOOKUP_NORMAL, NULL, tf_warning_or_error)); |
| 2217 |
release_tree_vector (args); |
| 2218 |
{ |
| 2219 |
tree fn = get_callee_fndecl (susp_call); |
| 2220 |
int argno = 0 + DECL_OBJECT_MEMBER_FUNCTION_P (fn); |
| 2221 |
tree &arg = CALL_EXPR_ARG (susp_call, argno); |
| 2222 |
/* Handle await_suspend taking an unusual type (c++/123975). */ |
| 2223 |
tree type = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
| 2224 |
type = TREE_VALUE (chain_index (argno, type)); |
| 2225 |
hfa = perform_implicit_conversion (type, hfa, tf_warning_or_error); |
| 2226 |
hfa = convert_for_arg_passing (type, hfa, tf_warning_or_error); |
| 2227 |
gcc_checking_assert (same_type_p (TREE_TYPE (arg), TREE_TYPE (hfa))); |
| 2228 |
arg = hfa; |
| 2229 |
} |
| 2230 |
|
| 2231 |
bool is_cont = false; |
| 2232 |
/* NOTE: final suspend can't resume; the "resume" label in that case |
| 2233 |
corresponds to implicit destruction. */ |
| 2234 |
if (VOID_TYPE_P (susp_type)) |
| 2235 |
/* Void return - just proceed to suspend. */ |
| 2236 |
finish_expr_stmt (suspend); |
| 2237 |
else if (TREE_CODE (susp_type) == BOOLEAN_TYPE) |
| 2238 |
{ |
| 2239 |
/* Boolean return, "continue" if the call returns false. */ |
| 2240 |
tree restart_if = begin_if_stmt (); |
| 2241 |
suspend = invert_truthvalue_loc (loc, suspend); |
| 2242 |
finish_if_stmt_cond (suspend, restart_if); |
| 2243 |
/* Resume - so restart the dispatcher, since we do not know if this |
| 2244 |
coroutine was already resumed from within await_suspend. We must |
| 2245 |
exit this scope without cleanups. */ |
| 2246 |
r = build_call_expr_internal_loc (loc, IFN_CO_SUSPN, void_type_node, 1, |
| 2247 |
build_address (data->dispatch)); |
| 2248 |
/* This will eventually expand to 'goto coro.restart.dispatch'. */ |
| 2249 |
finish_expr_stmt (r); |
| 2250 |
finish_then_clause (restart_if); |
| 2251 |
finish_if_stmt (restart_if); |
| 2252 |
} |
| 2253 |
else |
| 2254 |
{ |
| 2255 |
/* Handle return, save it to the continuation. */ |
| 2256 |
r = suspend; |
| 2257 |
if (!same_type_ignoring_top_level_qualifiers_p (susp_type, |
| 2258 |
void_coro_handle_type)) |
| 2259 |
r = build1_loc (loc, VIEW_CONVERT_EXPR, void_coro_handle_type, r); |
| 2260 |
r = cp_build_init_expr (loc, data->conthand, r); |
| 2261 |
finish_expr_stmt (r); |
| 2262 |
is_cont = true; |
| 2263 |
} |
| 2264 |
|
| 2265 |
tree d_l = build_address (destroy_label); |
| 2266 |
tree r_l = build_address (resume_label); |
| 2267 |
tree susp = build_address (data->cororet); |
| 2268 |
tree cont = build_address (data->corocont); |
| 2269 |
tree final_susp = build_int_cst (integer_type_node, is_final ? 1 : 0); |
| 2270 |
|
| 2271 |
resume_idx = build_int_cst (integer_type_node, data->index); |
| 2272 |
|
| 2273 |
tree sw = begin_switch_stmt (); |
| 2274 |
|
| 2275 |
r = build_call_expr_internal_loc (loc, IFN_CO_YIELD, integer_type_node, 5, |
| 2276 |
resume_idx, final_susp, r_l, d_l, |
| 2277 |
data->coro_fp); |
| 2278 |
finish_switch_cond (r, sw); |
| 2279 |
finish_case_label (loc, integer_zero_node, NULL_TREE); /* case 0: */ |
| 2280 |
/* Implement the suspend, a scope exit without clean ups. */ |
| 2281 |
r = build_call_expr_internal_loc (loc, IFN_CO_SUSPN, void_type_node, 1, |
| 2282 |
is_cont ? cont : susp); |
| 2283 |
finish_expr_stmt (r); /* This will eventually expand to 'goto return'. */ |
| 2284 |
finish_case_label (loc, integer_one_node, NULL_TREE); /* case 1: */ |
| 2285 |
add_stmt (build_stmt (loc, GOTO_EXPR, resume_label)); /* goto resume; */ |
| 2286 |
finish_case_label (loc, NULL_TREE, NULL_TREE); /* default:; */ |
| 2287 |
add_stmt (build_stmt (loc, GOTO_EXPR, destroy_label)); /* goto destroy; */ |
| 2288 |
|
| 2289 |
finish_switch_stmt (sw); |
| 2290 |
add_stmt (build_stmt (loc, LABEL_EXPR, destroy_label)); |
| 2291 |
if (needs_dtor) |
| 2292 |
finish_expr_stmt (build_cleanup (var)); |
| 2293 |
add_stmt (build_stmt (loc, GOTO_EXPR, data->cleanup)); |
| 2294 |
|
| 2295 |
finish_then_clause (susp_if); |
| 2296 |
finish_if_stmt (susp_if); |
| 2297 |
|
| 2298 |
/* Resume point. */ |
| 2299 |
add_stmt (build_stmt (loc, LABEL_EXPR, resume_label)); |
| 2300 |
|
| 2301 |
if (is_initial && data->i_a_r_c) |
| 2302 |
{ |
| 2303 |
r = cp_build_modify_expr (loc, data->i_a_r_c, NOP_EXPR, boolean_true_node, |
| 2304 |
tf_warning_or_error); |
| 2305 |
finish_expr_stmt (r); |
| 2306 |
} |
| 2307 |
|
| 2308 |
/* This will produce the value (if one is provided) from the co_await |
| 2309 |
expression. */ |
| 2310 |
tree resume_call = TREE_VEC_ELT (awaiter_calls, 2); /* await_resume(). */ |
| 2311 |
if (REFERENCE_REF_P (resume_call)) |
| 2312 |
/* Sink to await_resume call_expr. */ |
| 2313 |
resume_call = TREE_OPERAND (resume_call, 0); |
| 2314 |
|
| 2315 |
*await_expr = resume_call; /* Replace the co_await expr with its result. */ |
| 2316 |
append_to_statement_list_force (saved_statement, &stmt_list); |
| 2317 |
/* Get a pointer to the revised statement. */ |
| 2318 |
tree *revised = tsi_stmt_ptr (tsi_last (stmt_list)); |
| 2319 |
if (needs_dtor) |
| 2320 |
finish_expr_stmt (build_cleanup (var)); |
| 2321 |
data->index += 2; |
| 2322 |
|
| 2323 |
/* Replace the original expression with the expansion. */ |
| 2324 |
*expr = pop_stmt_list (stmt_list); |
| 2325 |
|
| 2326 |
/* Now, if the awaitable had an initializer, expand any awaits that might |
| 2327 |
be embedded in it. */ |
| 2328 |
tree *aw_expr_ptr; |
| 2329 |
if (await_init && |
| 2330 |
cp_walk_tree (await_init, co_await_find_in_subtree, &aw_expr_ptr, NULL)) |
| 2331 |
expand_one_await_expression (await_init, aw_expr_ptr, d); |
| 2332 |
|
| 2333 |
/* Expand any more await expressions in the original statement. */ |
| 2334 |
if (cp_walk_tree (revised, co_await_find_in_subtree, &aw_expr_ptr, NULL)) |
| 2335 |
expand_one_await_expression (revised, aw_expr_ptr, d); |
| 2336 |
|
| 2337 |
return NULL; |
| 2338 |
} |
| 2339 |
|
| 2340 |
/* Check to see if a statement contains at least one await expression, if |
| 2341 |
so, then process that. */ |
| 2342 |
|
| 2343 |
static tree |
| 2344 |
process_one_statement (tree *stmt, void *d) |
| 2345 |
{ |
| 2346 |
tree *aw_expr_ptr; |
| 2347 |
if (cp_walk_tree (stmt, co_await_find_in_subtree, &aw_expr_ptr, NULL)) |
| 2348 |
expand_one_await_expression (stmt, aw_expr_ptr, d); |
| 2349 |
return NULL_TREE; |
| 2350 |
} |
| 2351 |
|
| 2352 |
static tree |
| 2353 |
await_statement_expander (tree *stmt, int *do_subtree, void *d) |
| 2354 |
{ |
| 2355 |
tree res = NULL_TREE; |
| 2356 |
|
| 2357 |
/* Process a statement at a time. */ |
| 2358 |
if (STATEMENT_CLASS_P (*stmt) |
| 2359 |
|| TREE_CODE (*stmt) == BIND_EXPR |
| 2360 |
|| TREE_CODE (*stmt) == CLEANUP_POINT_EXPR) |
| 2361 |
return NULL_TREE; /* Just process the sub-trees. */ |
| 2362 |
else if (TREE_CODE (*stmt) == STATEMENT_LIST) |
| 2363 |
{ |
| 2364 |
for (tree &s : tsi_range (*stmt)) |
| 2365 |
{ |
| 2366 |
res = cp_walk_tree (&s, await_statement_expander, |
| 2367 |
d, NULL); |
| 2368 |
if (res) |
| 2369 |
return res; |
| 2370 |
} |
| 2371 |
*do_subtree = 0; /* Done subtrees. */ |
| 2372 |
} |
| 2373 |
else if (EXPR_P (*stmt)) |
| 2374 |
{ |
| 2375 |
process_one_statement (stmt, d); |
| 2376 |
*do_subtree = 0; /* Done subtrees. */ |
| 2377 |
} |
| 2378 |
|
| 2379 |
/* Continue statement walk, where required. */ |
| 2380 |
return res; |
| 2381 |
} |
| 2382 |
|
| 2383 |
struct await_xform_data |
| 2384 |
{ |
| 2385 |
tree actor_fn; /* Decl for context. */ |
| 2386 |
tree actor_frame; |
| 2387 |
hash_map<tree, suspend_point_info> *suspend_points; |
| 2388 |
}; |
| 2389 |
|
| 2390 |
/* When we built the await expressions, we didn't know the coro frame |
| 2391 |
layout, therefore no idea where to find the promise or where to put |
| 2392 |
the awaitables. Now we know these things, fill them in. */ |
| 2393 |
|
| 2394 |
static tree |
| 2395 |
transform_await_expr (tree await_expr, await_xform_data *xform) |
| 2396 |
{ |
| 2397 |
suspend_point_info *si = xform->suspend_points->get (await_expr); |
| 2398 |
location_t loc = EXPR_LOCATION (await_expr); |
| 2399 |
if (!si) |
| 2400 |
{ |
| 2401 |
error_at (loc, "no suspend point info for %qD", await_expr); |
| 2402 |
return error_mark_node; |
| 2403 |
} |
| 2404 |
|
| 2405 |
/* So, on entry, we have: |
| 2406 |
in : CO_AWAIT_EXPR (a, e_proxy, o, awr_call_vector, mode) |
| 2407 |
We no longer need a [it had diagnostic value, maybe?] |
| 2408 |
We need to replace the e_proxy in the awr_call. */ |
| 2409 |
|
| 2410 |
/* If we have a frame var for the awaitable, get a reference to it. */ |
| 2411 |
proxy_replace data; |
| 2412 |
if (si->await_field_id) |
| 2413 |
{ |
| 2414 |
tree as |
| 2415 |
= coro_build_frame_access_expr (xform->actor_frame, si->await_field_id, |
| 2416 |
true, tf_warning_or_error); |
| 2417 |
/* Replace references to the instance proxy with the frame entry now |
| 2418 |
computed. */ |
| 2419 |
data.from = TREE_OPERAND (await_expr, 1); |
| 2420 |
data.to = as; |
| 2421 |
cp_walk_tree (&await_expr, replace_proxy, &data, NULL); |
| 2422 |
|
| 2423 |
/* .. and replace. */ |
| 2424 |
TREE_OPERAND (await_expr, 1) = as; |
| 2425 |
} |
| 2426 |
|
| 2427 |
return await_expr; |
| 2428 |
} |
| 2429 |
|
| 2430 |
/* A wrapper for the transform_await_expr function so that it can be a |
| 2431 |
callback from cp_walk_tree. */ |
| 2432 |
|
| 2433 |
static tree |
| 2434 |
transform_await_wrapper (tree *stmt, int *do_subtree, void *d) |
| 2435 |
{ |
| 2436 |
/* Set actor function as new DECL_CONTEXT of label_decl. */ |
| 2437 |
struct await_xform_data *xform = (struct await_xform_data *) d; |
| 2438 |
if (TREE_CODE (*stmt) == LABEL_DECL |
| 2439 |
&& DECL_CONTEXT (*stmt) != xform->actor_fn) |
| 2440 |
DECL_CONTEXT (*stmt) = xform->actor_fn; |
| 2441 |
|
| 2442 |
/* We should have already lowered co_yields to their co_await. */ |
| 2443 |
gcc_checking_assert (TREE_CODE (*stmt) != CO_YIELD_EXPR); |
| 2444 |
if (TREE_CODE (*stmt) != CO_AWAIT_EXPR) |
| 2445 |
return NULL_TREE; |
| 2446 |
|
| 2447 |
tree await_expr = *stmt; |
| 2448 |
*stmt = transform_await_expr (await_expr, xform); |
| 2449 |
if (*stmt == error_mark_node) |
| 2450 |
*do_subtree = 0; |
| 2451 |
return NULL_TREE; |
| 2452 |
} |
| 2453 |
|
| 2454 |
/* For figuring out what local variable usage we have. */ |
| 2455 |
struct local_vars_transform |
| 2456 |
{ |
| 2457 |
tree context; |
| 2458 |
tree actor_frame; |
| 2459 |
tree coro_frame_type; |
| 2460 |
location_t loc; |
| 2461 |
hash_map<tree, local_var_info> *local_var_uses; |
| 2462 |
}; |
| 2463 |
|
| 2464 |
static tree |
| 2465 |
transform_local_var_uses (tree *stmt, int *do_subtree, void *d) |
| 2466 |
{ |
| 2467 |
local_vars_transform *lvd = (local_vars_transform *) d; |
| 2468 |
|
| 2469 |
/* For each var in this bind expr (that has a frame id, which means it was |
| 2470 |
accessed), build a frame reference and add it as the DECL_VALUE_EXPR. */ |
| 2471 |
|
| 2472 |
if (TREE_CODE (*stmt) == BIND_EXPR) |
| 2473 |
{ |
| 2474 |
tree lvar; |
| 2475 |
for (lvar = BIND_EXPR_VARS (*stmt); lvar != NULL; |
| 2476 |
lvar = DECL_CHAIN (lvar)) |
| 2477 |
{ |
| 2478 |
bool existed; |
| 2479 |
local_var_info &local_var |
| 2480 |
= lvd->local_var_uses->get_or_insert (lvar, &existed); |
| 2481 |
gcc_checking_assert (existed); |
| 2482 |
|
| 2483 |
/* Re-write the variable's context to be in the actor func. */ |
| 2484 |
DECL_CONTEXT (lvar) = lvd->context; |
| 2485 |
|
| 2486 |
/* For capture proxies, this could include the decl value expr. */ |
| 2487 |
if (local_var.is_lambda_capture || local_var.has_value_expr_p) |
| 2488 |
continue; /* No frame entry for this. */ |
| 2489 |
|
| 2490 |
/* TODO: implement selective generation of fields when vars are |
| 2491 |
known not-used. */ |
| 2492 |
if (local_var.field_id == NULL_TREE) |
| 2493 |
continue; /* Wasn't used. */ |
| 2494 |
tree fld_idx |
| 2495 |
= coro_build_frame_access_expr (lvd->actor_frame, |
| 2496 |
local_var.field_id, true, |
| 2497 |
tf_warning_or_error); |
| 2498 |
local_var.field_idx = fld_idx; |
| 2499 |
SET_DECL_VALUE_EXPR (lvar, fld_idx); |
| 2500 |
DECL_HAS_VALUE_EXPR_P (lvar) = true; |
| 2501 |
} |
| 2502 |
cp_walk_tree (&BIND_EXPR_BODY (*stmt), transform_local_var_uses, d, NULL); |
| 2503 |
*do_subtree = 0; /* We've done the body already. */ |
| 2504 |
return NULL_TREE; |
| 2505 |
} |
| 2506 |
return NULL_TREE; |
| 2507 |
} |
| 2508 |
|
| 2509 |
/* A helper to build the frame DTOR. |
| 2510 |
[dcl.fct.def.coroutine] / 12 |
| 2511 |
The deallocation function’s name is looked up in the scope of the promise |
| 2512 |
type. If this lookup fails, the deallocation function’s name is looked up |
| 2513 |
in the global scope. If deallocation function lookup finds both a usual |
| 2514 |
deallocation function with only a pointer parameter and a usual |
| 2515 |
deallocation function with both a pointer parameter and a size parameter, |
| 2516 |
then the selected deallocation function shall be the one with two |
| 2517 |
parameters. Otherwise, the selected deallocation function shall be the |
| 2518 |
function with one parameter. If no usual deallocation function is found |
| 2519 |
the program is ill-formed. The selected deallocation function shall be |
| 2520 |
called with the address of the block of storage to be reclaimed as its |
| 2521 |
first argument. If a deallocation function with a parameter of type |
| 2522 |
std::size_t is used, the size of the block is passed as the corresponding |
| 2523 |
argument. */ |
| 2524 |
|
| 2525 |
static tree |
| 2526 |
build_coroutine_frame_delete_expr (tree, tree, tree, location_t); |
| 2527 |
|
| 2528 |
/* The actor transform. */ |
| 2529 |
|
| 2530 |
static void |
| 2531 |
build_actor_fn (location_t loc, tree coro_frame_type, tree actor, tree fnbody, |
| 2532 |
tree orig, hash_map<tree, local_var_info> *local_var_uses, |
| 2533 |
hash_map<tree, suspend_point_info> *suspend_points, |
| 2534 |
vec<tree> *param_dtor_list, |
| 2535 |
tree resume_idx_var, unsigned body_count, tree frame_size, |
| 2536 |
bool inline_p) |
| 2537 |
{ |
| 2538 |
verify_stmt_tree (fnbody); |
| 2539 |
/* Some things we inherit from the original function. */ |
| 2540 |
tree promise_type = get_coroutine_promise_type (orig); |
| 2541 |
tree promise_proxy = get_coroutine_promise_proxy (orig); |
| 2542 |
|
| 2543 |
/* One param, the coro frame pointer. */ |
| 2544 |
tree actor_fp = DECL_ARGUMENTS (actor); |
| 2545 |
|
| 2546 |
bool spf = start_preparsed_function (actor, NULL_TREE, SF_PRE_PARSED); |
| 2547 |
gcc_checking_assert (spf); |
| 2548 |
gcc_checking_assert (cfun && current_function_decl && TREE_STATIC (actor)); |
| 2549 |
if (flag_exceptions) |
| 2550 |
/* We, unconditionally, add a try/catch and rethrow. |
| 2551 |
TODO: Determine if the combination of initial suspend and the original |
| 2552 |
body cannot throw, and elide these additions. */ |
| 2553 |
cp_function_chain->can_throw = true; |
| 2554 |
tree stmt = begin_function_body (); |
| 2555 |
|
| 2556 |
tree actor_bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); |
| 2557 |
tree top_block = make_node (BLOCK); |
| 2558 |
BIND_EXPR_BLOCK (actor_bind) = top_block; |
| 2559 |
|
| 2560 |
tree continuation = coro_build_artificial_var (loc, coro_actor_continue_id, |
| 2561 |
void_coro_handle_type, actor, |
| 2562 |
NULL_TREE); |
| 2563 |
|
| 2564 |
BIND_EXPR_VARS (actor_bind) = continuation; |
| 2565 |
BLOCK_VARS (top_block) = BIND_EXPR_VARS (actor_bind) ; |
| 2566 |
|
| 2567 |
/* Link in the block associated with the outer scope of the re-written |
| 2568 |
function body. */ |
| 2569 |
tree first = expr_first (fnbody); |
| 2570 |
gcc_checking_assert (first && TREE_CODE (first) == BIND_EXPR); |
| 2571 |
tree block = BIND_EXPR_BLOCK (first); |
| 2572 |
gcc_checking_assert (BLOCK_SUPERCONTEXT (block) == NULL_TREE); |
| 2573 |
gcc_checking_assert (BLOCK_CHAIN (block) == NULL_TREE); |
| 2574 |
BLOCK_SUPERCONTEXT (block) = top_block; |
| 2575 |
BLOCK_SUBBLOCKS (top_block) = block; |
| 2576 |
|
| 2577 |
add_stmt (actor_bind); |
| 2578 |
tree actor_body = push_stmt_list (); |
| 2579 |
|
| 2580 |
/* The entry point for the actor code from the ramp. */ |
| 2581 |
tree actor_begin_label |
| 2582 |
= create_named_label_with_ctx (loc, "actor.begin", actor); |
| 2583 |
tree actor_frame = build1_loc (loc, INDIRECT_REF, coro_frame_type, actor_fp); |
| 2584 |
|
| 2585 |
/* Declare the continuation handle. */ |
| 2586 |
add_decl_expr (continuation); |
| 2587 |
|
| 2588 |
/* Re-write local vars, similarly. */ |
| 2589 |
local_vars_transform xform_vars_data |
| 2590 |
= {actor, actor_frame, coro_frame_type, loc, local_var_uses}; |
| 2591 |
cp_walk_tree (&fnbody, transform_local_var_uses, &xform_vars_data, NULL); |
| 2592 |
tree rat = coro_build_frame_access_expr (actor_frame, coro_resume_index_id, |
| 2593 |
false, tf_warning_or_error); |
| 2594 |
tree ret_label |
| 2595 |
= create_named_label_with_ctx (loc, "actor.suspend.ret", actor); |
| 2596 |
|
| 2597 |
tree continue_label |
| 2598 |
= create_named_label_with_ctx (loc, "actor.continue.ret", actor); |
| 2599 |
|
| 2600 |
/* Build the dispatcher; for each await expression there is an odd entry |
| 2601 |
corresponding to the destruction action and an even entry for the resume |
| 2602 |
one: |
| 2603 |
if (resume index is odd) |
| 2604 |
{ |
| 2605 |
switch (resume index) |
| 2606 |
case 1: |
| 2607 |
goto cleanup. |
| 2608 |
case ... odd suspension point number |
| 2609 |
.CO_ACTOR (... odd suspension point number) |
| 2610 |
break; |
| 2611 |
default: |
| 2612 |
break; |
| 2613 |
} |
| 2614 |
else |
| 2615 |
{ |
| 2616 |
coro.restart.dispatch: |
| 2617 |
case 0: |
| 2618 |
goto start. |
| 2619 |
case ... even suspension point number |
| 2620 |
.CO_ACTOR (... even suspension point number) |
| 2621 |
break; |
| 2622 |
default: |
| 2623 |
break; |
| 2624 |
} |
| 2625 |
we should not get here unless something is broken badly. |
| 2626 |
__builtin_trap (); |
| 2627 |
*/ |
| 2628 |
tree lsb_if = begin_if_stmt (); |
| 2629 |
tree chkb0 = build2 (BIT_AND_EXPR, short_unsigned_type_node, rat, |
| 2630 |
build_int_cst (short_unsigned_type_node, 1)); |
| 2631 |
chkb0 = build2 (NE_EXPR, short_unsigned_type_node, chkb0, |
| 2632 |
build_int_cst (short_unsigned_type_node, 0)); |
| 2633 |
finish_if_stmt_cond (chkb0, lsb_if); |
| 2634 |
|
| 2635 |
tree destroy_dispatcher = begin_switch_stmt (); |
| 2636 |
finish_switch_cond (rat, destroy_dispatcher); |
| 2637 |
|
| 2638 |
/* The destroy point numbered #1 is special, in that it is reached from a |
| 2639 |
coroutine that is suspended after re-throwing from unhandled_exception(). |
| 2640 |
This label just invokes the cleanup of promise, param copies and the |
| 2641 |
frame itself, if the ramp isn't still keeping them alive. */ |
| 2642 |
tree del_promise_label |
| 2643 |
= create_named_label_with_ctx (loc, "coro.delete.promise", actor); |
| 2644 |
finish_case_label (loc, build_int_cst (short_unsigned_type_node, 1), |
| 2645 |
NULL_TREE); |
| 2646 |
add_stmt (build_stmt (loc, GOTO_EXPR, del_promise_label)); |
| 2647 |
|
| 2648 |
short unsigned lab_num = 3; |
| 2649 |
for (unsigned destr_pt = 0; destr_pt < body_count; destr_pt++) |
| 2650 |
{ |
| 2651 |
tree l_num = build_int_cst (short_unsigned_type_node, lab_num); |
| 2652 |
finish_case_label (loc, l_num, NULL_TREE); |
| 2653 |
tree c = build_call_expr_internal_loc (loc, IFN_CO_ACTOR, void_type_node, |
| 2654 |
1, l_num); |
| 2655 |
finish_expr_stmt (c); |
| 2656 |
finish_break_stmt (); |
| 2657 |
lab_num += 2; |
| 2658 |
} |
| 2659 |
finish_case_label (loc, NULL_TREE, NULL_TREE); |
| 2660 |
finish_break_stmt (); |
| 2661 |
|
| 2662 |
/* Finish the destroy dispatcher. */ |
| 2663 |
finish_switch_stmt (destroy_dispatcher); |
| 2664 |
|
| 2665 |
finish_then_clause (lsb_if); |
| 2666 |
begin_else_clause (lsb_if); |
| 2667 |
|
| 2668 |
/* For the case of a boolean await_resume () that returns 'true' we should |
| 2669 |
restart the dispatch, since we cannot know if additional resumes were |
| 2670 |
executed from within the await_suspend function. */ |
| 2671 |
tree restart_dispatch_label |
| 2672 |
= create_named_label_with_ctx (loc, "coro.restart.dispatch", actor); |
| 2673 |
add_stmt (build_stmt (loc, LABEL_EXPR, restart_dispatch_label)); |
| 2674 |
|
| 2675 |
tree dispatcher = begin_switch_stmt (); |
| 2676 |
finish_switch_cond (rat, dispatcher); |
| 2677 |
finish_case_label (loc, build_int_cst (short_unsigned_type_node, 0), |
| 2678 |
NULL_TREE); |
| 2679 |
add_stmt (build_stmt (loc, GOTO_EXPR, actor_begin_label)); |
| 2680 |
|
| 2681 |
lab_num = 2; |
| 2682 |
/* The final resume should be made to hit the default (trap, UB) entry |
| 2683 |
although it will be unreachable via the normal entry point, since that |
| 2684 |
is set to NULL on reaching final suspend. */ |
| 2685 |
for (unsigned resu_pt = 0; resu_pt < body_count; resu_pt++) |
| 2686 |
{ |
| 2687 |
tree l_num = build_int_cst (short_unsigned_type_node, lab_num); |
| 2688 |
finish_case_label (loc, l_num, NULL_TREE); |
| 2689 |
tree c = build_call_expr_internal_loc (loc, IFN_CO_ACTOR, void_type_node, |
| 2690 |
1, l_num); |
| 2691 |
finish_expr_stmt (c); |
| 2692 |
finish_break_stmt (); |
| 2693 |
lab_num += 2; |
| 2694 |
} |
| 2695 |
finish_case_label (loc, NULL_TREE, NULL_TREE); |
| 2696 |
finish_break_stmt (); |
| 2697 |
|
| 2698 |
/* Finish the resume dispatcher. */ |
| 2699 |
finish_switch_stmt (dispatcher); |
| 2700 |
|
| 2701 |
finish_else_clause (lsb_if); |
| 2702 |
finish_if_stmt (lsb_if); |
| 2703 |
|
| 2704 |
/* If we reach here then we've hit UB. */ |
| 2705 |
tree t = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0); |
| 2706 |
finish_expr_stmt (t); |
| 2707 |
|
| 2708 |
/* Now we start building the rewritten function body. */ |
| 2709 |
add_stmt (build_stmt (loc, LABEL_EXPR, actor_begin_label)); |
| 2710 |
|
| 2711 |
tree i_a_r_c = NULL_TREE; |
| 2712 |
if (flag_exceptions) |
| 2713 |
{ |
| 2714 |
i_a_r_c |
| 2715 |
= coro_build_frame_access_expr (actor_frame, coro_frame_i_a_r_c_id, |
| 2716 |
false, tf_warning_or_error); |
| 2717 |
tree m = cp_build_modify_expr (loc, i_a_r_c, NOP_EXPR, |
| 2718 |
boolean_false_node, tf_warning_or_error); |
| 2719 |
finish_expr_stmt (m); |
| 2720 |
} |
| 2721 |
|
| 2722 |
/* Now we know the real promise, and enough about the frame layout to |
| 2723 |
decide where to put things. */ |
| 2724 |
|
| 2725 |
await_xform_data xform = {actor, actor_frame, suspend_points}; |
| 2726 |
|
| 2727 |
/* Transform the await expressions in the function body. Only do each |
| 2728 |
await tree once! */ |
| 2729 |
hash_set<tree> pset; |
| 2730 |
cp_walk_tree (&fnbody, transform_await_wrapper, &xform, &pset); |
| 2731 |
|
| 2732 |
/* Add in our function body with the co_returns rewritten to final form. */ |
| 2733 |
add_stmt (fnbody); |
| 2734 |
|
| 2735 |
tree r = build_stmt (loc, LABEL_EXPR, del_promise_label); |
| 2736 |
add_stmt (r); |
| 2737 |
|
| 2738 |
/* We are done with the frame, but if the ramp still has a hold on it |
| 2739 |
we should not cleanup. So decrement the refcount and then return to |
| 2740 |
the ramp if it is > 0. */ |
| 2741 |
tree coro_frame_refcount |
| 2742 |
= coro_build_frame_access_expr (actor_frame, coro_frame_refcount_id, |
| 2743 |
false, tf_warning_or_error); |
| 2744 |
tree released = build2_loc (loc, MINUS_EXPR, short_unsigned_type_node, |
| 2745 |
coro_frame_refcount, |
| 2746 |
build_int_cst (short_unsigned_type_node, 1)); |
| 2747 |
r = cp_build_modify_expr (loc, coro_frame_refcount, NOP_EXPR, released, |
| 2748 |
tf_warning_or_error); |
| 2749 |
finish_expr_stmt (r); |
| 2750 |
tree cond = build2_loc (loc, NE_EXPR, short_unsigned_type_node, |
| 2751 |
coro_frame_refcount, |
| 2752 |
build_int_cst (short_unsigned_type_node, 0)); |
| 2753 |
tree ramp_cu_if = begin_if_stmt (); |
| 2754 |
finish_if_stmt_cond (cond, ramp_cu_if); |
| 2755 |
finish_return_stmt (NULL_TREE); |
| 2756 |
finish_then_clause (ramp_cu_if); |
| 2757 |
finish_if_stmt (ramp_cu_if); |
| 2758 |
|
| 2759 |
/* Otherwise, do the tail of the function; first cleanups. */ |
| 2760 |
|
| 2761 |
/* Destructors for the things we built explicitly. |
| 2762 |
promise... */ |
| 2763 |
if (tree c = cxx_maybe_build_cleanup (promise_proxy, tf_warning_or_error)) |
| 2764 |
finish_expr_stmt (c); |
| 2765 |
|
| 2766 |
/* Argument copies ... */ |
| 2767 |
while (!param_dtor_list->is_empty ()) |
| 2768 |
{ |
| 2769 |
tree parm_id = param_dtor_list->pop (); |
| 2770 |
tree a = coro_build_frame_access_expr (actor_frame, parm_id, false, |
| 2771 |
tf_warning_or_error); |
| 2772 |
if (tree dtor = cxx_maybe_build_cleanup (a, tf_warning_or_error)) |
| 2773 |
finish_expr_stmt (dtor); |
| 2774 |
} |
| 2775 |
|
| 2776 |
/* Here deallocate the frame (if we allocated it), which we will have at |
| 2777 |
present. */ |
| 2778 |
tree fnf2_x |
| 2779 |
= coro_build_frame_access_expr (actor_frame, coro_frame_needs_free_id, |
| 2780 |
false, tf_warning_or_error); |
| 2781 |
tree need_free_if = begin_if_stmt (); |
| 2782 |
finish_if_stmt_cond (fnf2_x, need_free_if); |
| 2783 |
|
| 2784 |
/* Build the frame DTOR. */ |
| 2785 |
tree del_coro_fr |
| 2786 |
= build_coroutine_frame_delete_expr (actor_fp, frame_size, |
| 2787 |
promise_type, loc); |
| 2788 |
finish_expr_stmt (del_coro_fr); |
| 2789 |
finish_then_clause (need_free_if); |
| 2790 |
finish_if_stmt (need_free_if); |
| 2791 |
|
| 2792 |
/* Done. */ |
| 2793 |
finish_return_stmt (NULL_TREE); |
| 2794 |
|
| 2795 |
/* This is the suspend return point. */ |
| 2796 |
add_stmt (build_stmt (loc, LABEL_EXPR, ret_label)); |
| 2797 |
|
| 2798 |
finish_return_stmt (NULL_TREE); |
| 2799 |
|
| 2800 |
/* This is the 'continuation' return point. For such a case we have a coro |
| 2801 |
handle (from the await_suspend() call) and we want handle.resume() to |
| 2802 |
execute as a tailcall allowing arbitrary chaining of coroutines. */ |
| 2803 |
add_stmt (build_stmt (loc, LABEL_EXPR, continue_label)); |
| 2804 |
|
| 2805 |
/* Should have been set earlier by the coro_initialized code. */ |
| 2806 |
gcc_assert (void_coro_handle_address); |
| 2807 |
|
| 2808 |
/* We want to force a tail-call even for O0/1, so this expands the resume |
| 2809 |
call into its underlying implementation. */ |
| 2810 |
tree addr = build_new_method_call (continuation, void_coro_handle_address, |
| 2811 |
NULL, NULL_TREE, LOOKUP_NORMAL, NULL, |
| 2812 |
tf_warning_or_error); |
| 2813 |
tree resume = build_call_expr_loc |
| 2814 |
(loc, builtin_decl_explicit (BUILT_IN_CORO_RESUME), 1, addr); |
| 2815 |
|
| 2816 |
/* In order to support an arbitrary number of coroutine continuations, |
| 2817 |
we must tail call them. However, some targets do not support indirect |
| 2818 |
tail calls to arbitrary callees. See PR94359. */ |
| 2819 |
CALL_EXPR_TAILCALL (resume) = true; |
| 2820 |
resume = coro_build_cvt_void_expr_stmt (resume, loc); |
| 2821 |
add_stmt (resume); |
| 2822 |
|
| 2823 |
r = build_stmt (loc, RETURN_EXPR, NULL); |
| 2824 |
gcc_checking_assert (maybe_cleanup_point_expr_void (r) == r); |
| 2825 |
add_stmt (r); |
| 2826 |
|
| 2827 |
/* How to construct the handle for this coroutine from the frame address. */ |
| 2828 |
tree hfa_m = get_coroutine_from_address (orig); |
| 2829 |
/* Should have been set earlier by coro_promise_type_found_p. */ |
| 2830 |
gcc_assert (hfa_m); |
| 2831 |
tree handle_type = TREE_TYPE (get_coroutine_self_handle_proxy (orig)); |
| 2832 |
|
| 2833 |
/* We've now rewritten the tree and added the initial and final |
| 2834 |
co_awaits. Now pass over the tree and expand the co_awaits. */ |
| 2835 |
|
| 2836 |
coro_aw_data data = {actor, actor_fp, resume_idx_var, i_a_r_c, |
| 2837 |
del_promise_label, ret_label, |
| 2838 |
continue_label, restart_dispatch_label, continuation, |
| 2839 |
handle_type, hfa_m, 2}; |
| 2840 |
cp_walk_tree (&actor_body, await_statement_expander, &data, NULL); |
| 2841 |
|
| 2842 |
BIND_EXPR_BODY (actor_bind) = pop_stmt_list (actor_body); |
| 2843 |
TREE_SIDE_EFFECTS (actor_bind) = true; |
| 2844 |
|
| 2845 |
cfun->coroutine_component = 1; |
| 2846 |
finish_function_body (stmt); |
| 2847 |
finish_function (inline_p); |
| 2848 |
} |
| 2849 |
|
| 2850 |
/* The prototype 'destroy' function : |
| 2851 |
frame->__Coro_resume_index |= 1; |
| 2852 |
actor (frame); */ |
| 2853 |
|
| 2854 |
static void |
| 2855 |
build_destroy_fn (location_t loc, tree coro_frame_type, tree destroy, |
| 2856 |
tree actor, bool inline_p) |
| 2857 |
{ |
| 2858 |
/* One param, the coro frame pointer. */ |
| 2859 |
tree destr_fp = DECL_ARGUMENTS (destroy); |
| 2860 |
gcc_checking_assert (POINTER_TYPE_P (TREE_TYPE (destr_fp)) |
| 2861 |
&& same_type_p (coro_frame_type, |
| 2862 |
TREE_TYPE (TREE_TYPE (destr_fp)))); |
| 2863 |
|
| 2864 |
bool spf = start_preparsed_function (destroy, NULL_TREE, SF_PRE_PARSED); |
| 2865 |
gcc_checking_assert (spf); |
| 2866 |
tree dstr_stmt = begin_function_body (); |
| 2867 |
|
| 2868 |
tree destr_frame |
| 2869 |
= cp_build_indirect_ref (loc, destr_fp, RO_UNARY_STAR, |
| 2870 |
tf_warning_or_error); |
| 2871 |
|
| 2872 |
tree rat = coro_build_frame_access_expr (destr_frame, coro_resume_index_id, |
| 2873 |
false, tf_warning_or_error); |
| 2874 |
|
| 2875 |
/* _resume_at |= 1 */ |
| 2876 |
tree dstr_idx |
| 2877 |
= build2_loc (loc, BIT_IOR_EXPR, short_unsigned_type_node, rat, |
| 2878 |
build_int_cst (short_unsigned_type_node, 1)); |
| 2879 |
tree r = cp_build_modify_expr (loc, rat, NOP_EXPR, dstr_idx, |
| 2880 |
tf_warning_or_error); |
| 2881 |
finish_expr_stmt (r); |
| 2882 |
|
| 2883 |
/* So .. call the actor .. */ |
| 2884 |
finish_expr_stmt (build_call_expr_loc (loc, actor, 1, destr_fp)); |
| 2885 |
|
| 2886 |
/* done. */ |
| 2887 |
finish_return_stmt (NULL_TREE); |
| 2888 |
|
| 2889 |
gcc_checking_assert (cfun && current_function_decl); |
| 2890 |
cfun->coroutine_component = 1; |
| 2891 |
finish_function_body (dstr_stmt); |
| 2892 |
finish_function (inline_p); |
| 2893 |
} |
| 2894 |
|
| 2895 |
/* Helper that returns an identifier for an appended extension to the |
| 2896 |
current un-mangled function name. */ |
| 2897 |
|
| 2898 |
static tree |
| 2899 |
get_fn_local_identifier (tree orig, const char *append) |
| 2900 |
{ |
| 2901 |
/* Figure out the bits we need to generate names for the outlined things |
| 2902 |
For consistency, this needs to behave the same way as |
| 2903 |
ASM_FORMAT_PRIVATE_NAME does. */ |
| 2904 |
tree nm = DECL_NAME (orig); |
| 2905 |
const char *sep, *pfx = ""; |
| 2906 |
#ifndef NO_DOT_IN_LABEL |
| 2907 |
sep = "."; |
| 2908 |
#else |
| 2909 |
#ifndef NO_DOLLAR_IN_LABEL |
| 2910 |
sep = "$"; |
| 2911 |
#else |
| 2912 |
sep = "_"; |
| 2913 |
pfx = "__"; |
| 2914 |
#endif |
| 2915 |
#endif |
| 2916 |
|
| 2917 |
char *an; |
| 2918 |
if (DECL_ASSEMBLER_NAME (orig)) |
| 2919 |
an = ACONCAT ((IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (orig)), sep, append, |
| 2920 |
(char *) 0)); |
| 2921 |
else if (DECL_USE_TEMPLATE (orig) && DECL_TEMPLATE_INFO (orig) |
| 2922 |
&& DECL_TI_ARGS (orig)) |
| 2923 |
{ |
| 2924 |
tree tpl_args = DECL_TI_ARGS (orig); |
| 2925 |
an = ACONCAT ((pfx, IDENTIFIER_POINTER (nm), (char *) 0)); |
| 2926 |
for (int i = 0; i < TREE_VEC_LENGTH (tpl_args); ++i) |
| 2927 |
{ |
| 2928 |
tree typ = DECL_NAME (TYPE_NAME (TREE_VEC_ELT (tpl_args, i))); |
| 2929 |
an = ACONCAT ((an, sep, IDENTIFIER_POINTER (typ), (char *) 0)); |
| 2930 |
} |
| 2931 |
an = ACONCAT ((an, sep, append, (char *) 0)); |
| 2932 |
} |
| 2933 |
else |
| 2934 |
an = ACONCAT ((pfx, IDENTIFIER_POINTER (nm), sep, append, (char *) 0)); |
| 2935 |
|
| 2936 |
return get_identifier (an); |
| 2937 |
} |
| 2938 |
|
| 2939 |
/* Build an initial or final await initialized from the promise |
| 2940 |
initial_suspend or final_suspend expression. */ |
| 2941 |
|
| 2942 |
static tree |
| 2943 |
build_init_or_final_await (location_t loc, bool is_final) |
| 2944 |
{ |
| 2945 |
tree suspend_alt = is_final ? coro_final_suspend_identifier |
| 2946 |
: coro_initial_suspend_identifier; |
| 2947 |
|
| 2948 |
tree setup_call |
| 2949 |
= coro_build_promise_expression (current_function_decl, NULL, suspend_alt, |
| 2950 |
loc, NULL, /*musthave=*/true); |
| 2951 |
|
| 2952 |
/* Check for noexcept on the final_suspend call. */ |
| 2953 |
if (flag_exceptions && is_final && setup_call != error_mark_node |
| 2954 |
&& coro_diagnose_throwing_final_aw_expr (setup_call)) |
| 2955 |
return error_mark_node; |
| 2956 |
|
| 2957 |
/* So build the co_await for this */ |
| 2958 |
/* For initial/final suspends the call is "a" per [expr.await] 3.2. */ |
| 2959 |
return build_co_await (loc, setup_call, (is_final ? FINAL_SUSPEND_POINT |
| 2960 |
: INITIAL_SUSPEND_POINT)); |
| 2961 |
} |
| 2962 |
|
| 2963 |
/* Callback to record the essential data for each await point found in the |
| 2964 |
function. */ |
| 2965 |
|
| 2966 |
static bool |
| 2967 |
register_await_info (tree await_expr, tree aw_type, tree aw_nam, |
| 2968 |
hash_map<tree, suspend_point_info> *suspend_points) |
| 2969 |
{ |
| 2970 |
bool seen; |
| 2971 |
suspend_point_info &s |
| 2972 |
= suspend_points->get_or_insert (await_expr, &seen); |
| 2973 |
if (seen) |
| 2974 |
{ |
| 2975 |
warning_at (EXPR_LOCATION (await_expr), 0, "duplicate info for %qE", |
| 2976 |
await_expr); |
| 2977 |
return false; |
| 2978 |
} |
| 2979 |
s.awaitable_type = aw_type; |
| 2980 |
s.await_field_id = aw_nam; |
| 2981 |
return true; |
| 2982 |
} |
| 2983 |
|
| 2984 |
/* If this is an await expression, then count it (both uniquely within the |
| 2985 |
function and locally within a single statement). */ |
| 2986 |
|
| 2987 |
static tree |
| 2988 |
register_awaits (tree *stmt, int *, void *d) |
| 2989 |
{ |
| 2990 |
tree aw_expr = *stmt; |
| 2991 |
|
| 2992 |
/* We should have already lowered co_yields to their co_await. */ |
| 2993 |
gcc_checking_assert (TREE_CODE (aw_expr) != CO_YIELD_EXPR); |
| 2994 |
|
| 2995 |
if (TREE_CODE (aw_expr) != CO_AWAIT_EXPR) |
| 2996 |
return NULL_TREE; |
| 2997 |
|
| 2998 |
/* Count how many awaits the current expression contains. */ |
| 2999 |
susp_frame_data *data = (susp_frame_data *) d; |
| 3000 |
data->saw_awaits++; |
| 3001 |
/* Each await suspend context is unique, this is a function-wide value. */ |
| 3002 |
data->await_number++; |
| 3003 |
|
| 3004 |
/* Awaitables should either be user-locals or promoted to coroutine frame |
| 3005 |
entries at this point, and their initializers should have been broken |
| 3006 |
out. */ |
| 3007 |
tree aw = TREE_OPERAND (aw_expr, 1); |
| 3008 |
gcc_checking_assert (!TREE_OPERAND (aw_expr, 2)); |
| 3009 |
|
| 3010 |
tree aw_field_type = TREE_TYPE (aw); |
| 3011 |
tree aw_field_nam = NULL_TREE; |
| 3012 |
register_await_info (aw_expr, aw_field_type, aw_field_nam, data->suspend_points); |
| 3013 |
|
| 3014 |
/* Rewrite target expressions on the await_suspend () to remove extraneous |
| 3015 |
cleanups for the awaitables, which are now promoted to frame vars and |
| 3016 |
managed via that. */ |
| 3017 |
tree v = TREE_OPERAND (aw_expr, 3); |
| 3018 |
tree o = TREE_VEC_ELT (v, 1); |
| 3019 |
if (TREE_CODE (o) == TARGET_EXPR) |
| 3020 |
TREE_VEC_ELT (v, 1) = get_target_expr (TARGET_EXPR_INITIAL (o)); |
| 3021 |
return NULL_TREE; |
| 3022 |
} |
| 3023 |
|
| 3024 |
/* There are cases where any await expression is relevant. */ |
| 3025 |
static tree |
| 3026 |
find_any_await (tree *stmt, int *dosub, void *d) |
| 3027 |
{ |
| 3028 |
if (TREE_CODE (*stmt) == CO_AWAIT_EXPR) |
| 3029 |
{ |
| 3030 |
*dosub = 0; /* We don't need to consider this any further. */ |
| 3031 |
if (d) |
| 3032 |
*(tree **)d = stmt; |
| 3033 |
return *stmt; |
| 3034 |
} |
| 3035 |
return NULL_TREE; |
| 3036 |
} |
| 3037 |
|
| 3038 |
static bool |
| 3039 |
tmp_target_expr_p (tree t) |
| 3040 |
{ |
| 3041 |
if (TREE_CODE (t) != TARGET_EXPR) |
| 3042 |
return false; |
| 3043 |
tree v = TARGET_EXPR_SLOT (t); |
| 3044 |
if (!DECL_ARTIFICIAL (v)) |
| 3045 |
return false; |
| 3046 |
if (DECL_NAME (v)) |
| 3047 |
return false; |
| 3048 |
return true; |
| 3049 |
} |
| 3050 |
|
| 3051 |
/* Structure to record sub-expressions that need to be handled by the |
| 3052 |
statement flattener. */ |
| 3053 |
|
| 3054 |
struct coro_interesting_subtree |
| 3055 |
{ |
| 3056 |
tree* entry; |
| 3057 |
hash_set<tree> *temps_used; |
| 3058 |
}; |
| 3059 |
|
| 3060 |
/* tree-walk callback that returns the first encountered sub-expression of |
| 3061 |
a kind that needs to be handled specifically by the statement flattener. */ |
| 3062 |
|
| 3063 |
static tree |
| 3064 |
find_interesting_subtree (tree *expr_p, int *dosub, void *d) |
| 3065 |
{ |
| 3066 |
tree expr = *expr_p; |
| 3067 |
coro_interesting_subtree *p = (coro_interesting_subtree *)d; |
| 3068 |
if (TREE_CODE (expr) == CO_AWAIT_EXPR) |
| 3069 |
{ |
| 3070 |
*dosub = 0; /* We don't need to consider this any further. */ |
| 3071 |
if (TREE_OPERAND (expr, 2)) |
| 3072 |
{ |
| 3073 |
p->entry = expr_p; |
| 3074 |
return expr; |
| 3075 |
} |
| 3076 |
} |
| 3077 |
else if (tmp_target_expr_p (expr) |
| 3078 |
&& !TARGET_EXPR_ELIDING_P (expr) |
| 3079 |
&& !p->temps_used->contains (expr)) |
| 3080 |
{ |
| 3081 |
p->entry = expr_p; |
| 3082 |
return expr; |
| 3083 |
} |
| 3084 |
|
| 3085 |
return NULL_TREE; |
| 3086 |
} |
| 3087 |
|
| 3088 |
/* Node for a doubly-linked list of promoted variables and their |
| 3089 |
initializers. When the initializer is a conditional expression |
| 3090 |
the 'then' and 'else' clauses are represented by a linked list |
| 3091 |
attached to then_cl and else_cl respectively. */ |
| 3092 |
|
| 3093 |
struct var_nest_node |
| 3094 |
{ |
| 3095 |
var_nest_node () = default; |
| 3096 |
var_nest_node (tree v, tree i, var_nest_node *p, var_nest_node *n) |
| 3097 |
: var(v), init(i), prev(p), next(n), then_cl (NULL), else_cl (NULL) |
| 3098 |
{ |
| 3099 |
if (p) |
| 3100 |
p->next = this; |
| 3101 |
if (n) |
| 3102 |
n->prev = this; |
| 3103 |
} |
| 3104 |
tree var; |
| 3105 |
tree init; |
| 3106 |
var_nest_node *prev; |
| 3107 |
var_nest_node *next; |
| 3108 |
var_nest_node *then_cl; |
| 3109 |
var_nest_node *else_cl; |
| 3110 |
}; |
| 3111 |
|
| 3112 |
/* This is called for single statements from the co-await statement walker. |
| 3113 |
It checks to see if the statement contains any initializers for awaitables |
| 3114 |
and if any of these capture items by reference. */ |
| 3115 |
|
| 3116 |
static void |
| 3117 |
flatten_await_stmt (var_nest_node *n, hash_set<tree> *promoted, |
| 3118 |
hash_set<tree> *temps_used, tree *replace_in) |
| 3119 |
{ |
| 3120 |
bool init_expr = false; |
| 3121 |
switch (TREE_CODE (n->init)) |
| 3122 |
{ |
| 3123 |
default: break; |
| 3124 |
/* Compound expressions must be flattened specifically. */ |
| 3125 |
case COMPOUND_EXPR: |
| 3126 |
{ |
| 3127 |
tree first = TREE_OPERAND (n->init, 0); |
| 3128 |
n->init = TREE_OPERAND (n->init, 1); |
| 3129 |
var_nest_node *ins |
| 3130 |
= new var_nest_node(NULL_TREE, first, n->prev, n); |
| 3131 |
/* The compiler (but not the user) can generate temporaries with |
| 3132 |
uses in the second arm of a compound expr. */ |
| 3133 |
flatten_await_stmt (ins, promoted, temps_used, &n->init); |
| 3134 |
flatten_await_stmt (n, promoted, temps_used, NULL); |
| 3135 |
/* The two arms have been processed separately. */ |
| 3136 |
return; |
| 3137 |
} |
| 3138 |
break; |
| 3139 |
/* Handle conditional expressions. */ |
| 3140 |
case INIT_EXPR: |
| 3141 |
init_expr = true; |
| 3142 |
/* FALLTHROUGH */ |
| 3143 |
case MODIFY_EXPR: |
| 3144 |
{ |
| 3145 |
tree old_expr = TREE_OPERAND (n->init, 1); |
| 3146 |
if (TREE_CODE (old_expr) == COMPOUND_EXPR) |
| 3147 |
{ |
| 3148 |
tree first = TREE_OPERAND (old_expr, 0); |
| 3149 |
TREE_OPERAND (n->init, 1) = TREE_OPERAND (old_expr, 1); |
| 3150 |
var_nest_node *ins |
| 3151 |
= new var_nest_node(NULL_TREE, first, n->prev, n); |
| 3152 |
flatten_await_stmt (ins, promoted, temps_used, |
| 3153 |
&TREE_OPERAND (n->init, 1)); |
| 3154 |
flatten_await_stmt (n, promoted, temps_used, NULL); |
| 3155 |
return; |
| 3156 |
} |
| 3157 |
if (TREE_CODE (old_expr) != COND_EXPR) |
| 3158 |
break; |
| 3159 |
/* Reconstruct x = t ? y : z; |
| 3160 |
as (void) t ? x = y : x = z; */ |
| 3161 |
tree var = TREE_OPERAND (n->init, 0); |
| 3162 |
tree var_type = TREE_TYPE (var); |
| 3163 |
tree cond = COND_EXPR_COND (old_expr); |
| 3164 |
/* We are allowed a void type throw in one or both of the cond |
| 3165 |
expr arms. */ |
| 3166 |
tree then_cl = COND_EXPR_THEN (old_expr); |
| 3167 |
if (!VOID_TYPE_P (TREE_TYPE (then_cl))) |
| 3168 |
{ |
| 3169 |
gcc_checking_assert (TREE_CODE (then_cl) != STATEMENT_LIST); |
| 3170 |
if (init_expr) |
| 3171 |
then_cl = cp_build_init_expr (var, then_cl); |
| 3172 |
else |
| 3173 |
then_cl = build2 (MODIFY_EXPR, var_type, var, then_cl); |
| 3174 |
} |
| 3175 |
tree else_cl = COND_EXPR_ELSE (old_expr); |
| 3176 |
if (!VOID_TYPE_P (TREE_TYPE (else_cl))) |
| 3177 |
{ |
| 3178 |
gcc_checking_assert (TREE_CODE (else_cl) != STATEMENT_LIST); |
| 3179 |
if (init_expr) |
| 3180 |
else_cl = cp_build_init_expr (var, else_cl); |
| 3181 |
else |
| 3182 |
else_cl = build2 (MODIFY_EXPR, var_type, var, else_cl); |
| 3183 |
} |
| 3184 |
n->init = build3 (COND_EXPR, var_type, cond, then_cl, else_cl); |
| 3185 |
} |
| 3186 |
/* FALLTHROUGH */ |
| 3187 |
case COND_EXPR: |
| 3188 |
{ |
| 3189 |
tree *found; |
| 3190 |
tree cond = COND_EXPR_COND (n->init); |
| 3191 |
/* If the condition contains an await expression, then we need to |
| 3192 |
set that first and use a separate var. */ |
| 3193 |
if (cp_walk_tree (&cond, find_any_await, &found, NULL)) |
| 3194 |
{ |
| 3195 |
tree cond_type = TREE_TYPE (cond); |
| 3196 |
tree cond_var = build_lang_decl (VAR_DECL, NULL_TREE, cond_type); |
| 3197 |
DECL_ARTIFICIAL (cond_var) = true; |
| 3198 |
layout_decl (cond_var, 0); |
| 3199 |
gcc_checking_assert (!TYPE_NEEDS_CONSTRUCTING (cond_type)); |
| 3200 |
cond = cp_build_init_expr (cond_var, cond); |
| 3201 |
var_nest_node *ins |
| 3202 |
= new var_nest_node (cond_var, cond, n->prev, n); |
| 3203 |
COND_EXPR_COND (n->init) = cond_var; |
| 3204 |
flatten_await_stmt (ins, promoted, temps_used, NULL); |
| 3205 |
} |
| 3206 |
|
| 3207 |
n->then_cl |
| 3208 |
= new var_nest_node (n->var, COND_EXPR_THEN (n->init), NULL, NULL); |
| 3209 |
n->else_cl |
| 3210 |
= new var_nest_node (n->var, COND_EXPR_ELSE (n->init), NULL, NULL); |
| 3211 |
flatten_await_stmt (n->then_cl, promoted, temps_used, NULL); |
| 3212 |
/* Point to the start of the flattened code. */ |
| 3213 |
while (n->then_cl->prev) |
| 3214 |
n->then_cl = n->then_cl->prev; |
| 3215 |
flatten_await_stmt (n->else_cl, promoted, temps_used, NULL); |
| 3216 |
while (n->else_cl->prev) |
| 3217 |
n->else_cl = n->else_cl->prev; |
| 3218 |
return; |
| 3219 |
} |
| 3220 |
break; |
| 3221 |
} |
| 3222 |
coro_interesting_subtree v = { NULL, temps_used }; |
| 3223 |
tree t = cp_walk_tree (&n->init, find_interesting_subtree, (void *)&v, NULL); |
| 3224 |
if (!t) |
| 3225 |
return; |
| 3226 |
switch (TREE_CODE (t)) |
| 3227 |
{ |
| 3228 |
default: break; |
| 3229 |
case CO_AWAIT_EXPR: |
| 3230 |
{ |
| 3231 |
/* Await expressions with initializers have a compiler-temporary |
| 3232 |
as the awaitable. 'promote' this. */ |
| 3233 |
tree var = TREE_OPERAND (t, 1); |
| 3234 |
bool already_present = promoted->add (var); |
| 3235 |
gcc_checking_assert (!already_present); |
| 3236 |
tree init = TREE_OPERAND (t, 2); |
| 3237 |
switch (TREE_CODE (init)) |
| 3238 |
{ |
| 3239 |
default: break; |
| 3240 |
case INIT_EXPR: |
| 3241 |
case MODIFY_EXPR: |
| 3242 |
{ |
| 3243 |
tree inner = TREE_OPERAND (init, 1); |
| 3244 |
/* We can have non-lvalue-expressions here, but when we see |
| 3245 |
a target expression, mark it as already used. */ |
| 3246 |
if (TREE_CODE (inner) == TARGET_EXPR) |
| 3247 |
{ |
| 3248 |
temps_used->add (inner); |
| 3249 |
gcc_checking_assert |
| 3250 |
(TREE_CODE (TARGET_EXPR_INITIAL (inner)) != COND_EXPR); |
| 3251 |
} |
| 3252 |
} |
| 3253 |
break; |
| 3254 |
case CALL_EXPR: |
| 3255 |
/* If this is a call and not a CTOR, then we didn't expect it. */ |
| 3256 |
gcc_checking_assert |
| 3257 |
(DECL_CONSTRUCTOR_P (TREE_OPERAND (CALL_EXPR_FN (init), 0))); |
| 3258 |
break; |
| 3259 |
} |
| 3260 |
var_nest_node *ins = new var_nest_node (var, init, n->prev, n); |
| 3261 |
TREE_OPERAND (t, 2) = NULL_TREE; |
| 3262 |
flatten_await_stmt (ins, promoted, temps_used, NULL); |
| 3263 |
flatten_await_stmt (n, promoted, temps_used, NULL); |
| 3264 |
return; |
| 3265 |
} |
| 3266 |
break; |
| 3267 |
case TARGET_EXPR: |
| 3268 |
{ |
| 3269 |
/* We have a temporary; promote it, but allow for the idiom in code |
| 3270 |
generated by the compiler like |
| 3271 |
a = (target_expr produces temp, op uses temp). */ |
| 3272 |
tree init = t; |
| 3273 |
temps_used->add (init); |
| 3274 |
tree var_type = TREE_TYPE (init); |
| 3275 |
char *buf = xasprintf ("T%03u", (unsigned) temps_used->elements ()); |
| 3276 |
tree var = build_lang_decl (VAR_DECL, get_identifier (buf), var_type); |
| 3277 |
DECL_ARTIFICIAL (var) = true; |
| 3278 |
free (buf); |
| 3279 |
bool already_present = promoted->add (var); |
| 3280 |
gcc_checking_assert (!already_present); |
| 3281 |
tree inner = TARGET_EXPR_INITIAL (init); |
| 3282 |
gcc_checking_assert |
| 3283 |
(TREE_CODE (inner) != COND_EXPR |
| 3284 |
|| !cp_walk_tree (&inner, find_any_await, nullptr, nullptr)); |
| 3285 |
init = cp_build_modify_expr (input_location, var, INIT_EXPR, init, |
| 3286 |
tf_warning_or_error); |
| 3287 |
/* Simplify for the case that we have an init containing the temp |
| 3288 |
alone. */ |
| 3289 |
if (t == n->init && n->var == NULL_TREE) |
| 3290 |
{ |
| 3291 |
n->var = var; |
| 3292 |
proxy_replace pr = {TARGET_EXPR_SLOT (t), var}; |
| 3293 |
cp_walk_tree (&init, replace_proxy, &pr, NULL); |
| 3294 |
n->init = init; |
| 3295 |
if (replace_in) |
| 3296 |
cp_walk_tree (replace_in, replace_proxy, &pr, NULL); |
| 3297 |
flatten_await_stmt (n, promoted, temps_used, NULL); |
| 3298 |
} |
| 3299 |
else |
| 3300 |
{ |
| 3301 |
var_nest_node *ins |
| 3302 |
= new var_nest_node (var, init, n->prev, n); |
| 3303 |
/* We have to replace the target expr... */ |
| 3304 |
*v.entry = var; |
| 3305 |
/* ... and any uses of its var. */ |
| 3306 |
proxy_replace pr = {TARGET_EXPR_SLOT (t), var}; |
| 3307 |
cp_walk_tree (&n->init, replace_proxy, &pr, NULL); |
| 3308 |
/* Compiler-generated temporaries can also have uses in |
| 3309 |
following arms of compound expressions, which will be listed |
| 3310 |
in 'replace_in' if present. */ |
| 3311 |
if (replace_in) |
| 3312 |
cp_walk_tree (replace_in, replace_proxy, &pr, NULL); |
| 3313 |
flatten_await_stmt (ins, promoted, temps_used, NULL); |
| 3314 |
flatten_await_stmt (n, promoted, temps_used, NULL); |
| 3315 |
} |
| 3316 |
return; |
| 3317 |
} |
| 3318 |
break; |
| 3319 |
} |
| 3320 |
} |
| 3321 |
|
| 3322 |
/* Helper for 'process_conditional' that handles recursion into nested |
| 3323 |
conditionals. */ |
| 3324 |
|
| 3325 |
static void |
| 3326 |
handle_nested_conditionals (var_nest_node *n, vec<tree>& list, |
| 3327 |
hash_map<tree, tree>& map) |
| 3328 |
{ |
| 3329 |
do |
| 3330 |
{ |
| 3331 |
if (n->var && DECL_NAME (n->var)) |
| 3332 |
{ |
| 3333 |
list.safe_push (n->var); |
| 3334 |
if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (n->var))) |
| 3335 |
{ |
| 3336 |
bool existed; |
| 3337 |
tree& flag = map.get_or_insert (n->var, &existed); |
| 3338 |
if (!existed) |
| 3339 |
{ |
| 3340 |
/* We didn't see this var before and it needs a DTOR, so |
| 3341 |
build a guard variable for it. */ |
| 3342 |
char *nam |
| 3343 |
= xasprintf ("%s_guard", |
| 3344 |
IDENTIFIER_POINTER (DECL_NAME (n->var))); |
| 3345 |
flag = build_lang_decl (VAR_DECL, get_identifier (nam), |
| 3346 |
boolean_type_node); |
| 3347 |
free (nam); |
| 3348 |
DECL_ARTIFICIAL (flag) = true; |
| 3349 |
} |
| 3350 |
|
| 3351 |
/* The initializer for this variable is replaced by a compound |
| 3352 |
expression that performs the init and then records that the |
| 3353 |
variable is live (and the DTOR should be run at the scope |
| 3354 |
exit. */ |
| 3355 |
tree set_flag = cp_build_init_expr (flag, boolean_true_node); |
| 3356 |
n->init |
| 3357 |
= build2 (COMPOUND_EXPR, boolean_type_node, n->init, set_flag); |
| 3358 |
} |
| 3359 |
} |
| 3360 |
if (TREE_CODE (n->init) == COND_EXPR) |
| 3361 |
{ |
| 3362 |
tree new_then = push_stmt_list (); |
| 3363 |
handle_nested_conditionals (n->then_cl, list, map); |
| 3364 |
new_then = pop_stmt_list (new_then); |
| 3365 |
tree new_else = push_stmt_list (); |
| 3366 |
handle_nested_conditionals (n->else_cl, list, map); |
| 3367 |
new_else = pop_stmt_list (new_else); |
| 3368 |
tree new_if |
| 3369 |
= build4 (IF_STMT, void_type_node, COND_EXPR_COND (n->init), |
| 3370 |
new_then, new_else, NULL_TREE); |
| 3371 |
add_stmt (new_if); |
| 3372 |
} |
| 3373 |
else |
| 3374 |
finish_expr_stmt (n->init); |
| 3375 |
n = n->next; |
| 3376 |
} while (n); |
| 3377 |
} |
| 3378 |
|
| 3379 |
/* helper for 'maybe_promote_temps'. |
| 3380 |
|
| 3381 |
When we have a conditional expression which might embed await expressions |
| 3382 |
and/or promoted variables, we need to handle it appropriately. |
| 3383 |
|
| 3384 |
The linked lists for the 'then' and 'else' clauses in a conditional node |
| 3385 |
identify the promoted variables (but these cannot be wrapped in a regular |
| 3386 |
cleanup). |
| 3387 |
|
| 3388 |
So recurse through the lists and build up a composite list of captured vars. |
| 3389 |
Declare these and any guard variables needed to decide if a DTOR should be |
| 3390 |
run. Then embed the conditional into a try-finally expression that handles |
| 3391 |
running each DTOR conditionally on its guard variable. */ |
| 3392 |
|
| 3393 |
static void |
| 3394 |
process_conditional (var_nest_node *n, tree& vlist) |
| 3395 |
{ |
| 3396 |
tree init = n->init; |
| 3397 |
hash_map<tree, tree> var_flags; |
| 3398 |
auto_vec<tree> var_list; |
| 3399 |
tree new_then = push_stmt_list (); |
| 3400 |
handle_nested_conditionals (n->then_cl, var_list, var_flags); |
| 3401 |
new_then = pop_stmt_list (new_then); |
| 3402 |
tree new_else = push_stmt_list (); |
| 3403 |
handle_nested_conditionals (n->else_cl, var_list, var_flags); |
| 3404 |
new_else = pop_stmt_list (new_else); |
| 3405 |
/* Declare the vars. There are two loops so that the boolean flags are |
| 3406 |
grouped in the frame. */ |
| 3407 |
for (unsigned i = 0; i < var_list.length(); i++) |
| 3408 |
{ |
| 3409 |
tree var = var_list[i]; |
| 3410 |
DECL_CHAIN (var) = vlist; |
| 3411 |
vlist = var; |
| 3412 |
add_decl_expr (var); |
| 3413 |
} |
| 3414 |
/* Define the guard flags for variables that need a DTOR. */ |
| 3415 |
for (unsigned i = 0; i < var_list.length(); i++) |
| 3416 |
{ |
| 3417 |
tree *flag = var_flags.get (var_list[i]); |
| 3418 |
if (flag) |
| 3419 |
{ |
| 3420 |
DECL_INITIAL (*flag) = boolean_false_node; |
| 3421 |
DECL_CHAIN (*flag) = vlist; |
| 3422 |
vlist = *flag; |
| 3423 |
add_decl_expr (*flag); |
| 3424 |
} |
| 3425 |
} |
| 3426 |
tree new_if |
| 3427 |
= build4 (IF_STMT, void_type_node, COND_EXPR_COND (init), |
| 3428 |
new_then, new_else, NULL_TREE); |
| 3429 |
/* Build a set of conditional DTORs. */ |
| 3430 |
tree final_actions = push_stmt_list (); |
| 3431 |
while (!var_list.is_empty()) |
| 3432 |
{ |
| 3433 |
tree var = var_list.pop (); |
| 3434 |
tree *flag = var_flags.get (var); |
| 3435 |
if (!flag) |
| 3436 |
continue; |
| 3437 |
if (tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error)) |
| 3438 |
{ |
| 3439 |
tree cond_cleanup = begin_if_stmt (); |
| 3440 |
finish_if_stmt_cond (*flag, cond_cleanup); |
| 3441 |
finish_expr_stmt (cleanup); |
| 3442 |
finish_then_clause (cond_cleanup); |
| 3443 |
finish_if_stmt (cond_cleanup); |
| 3444 |
} |
| 3445 |
} |
| 3446 |
final_actions = pop_stmt_list (final_actions); |
| 3447 |
tree try_finally |
| 3448 |
= build2 (TRY_FINALLY_EXPR, void_type_node, new_if, final_actions); |
| 3449 |
add_stmt (try_finally); |
| 3450 |
} |
| 3451 |
|
| 3452 |
/* Given *STMT, that contains at least one await expression. |
| 3453 |
|
| 3454 |
The full expression represented in the original source code will contain |
| 3455 |
suspension points, but it is still required that the lifetime of temporary |
| 3456 |
values extends to the end of the expression. |
| 3457 |
|
| 3458 |
We already have a mechanism to 'promote' user-authored local variables |
| 3459 |
to a coroutine frame counterpart (which allows explicit management of the |
| 3460 |
lifetime across suspensions). The transform here re-writes STMT into |
| 3461 |
a bind expression, promotes temporary values into local variables in that |
| 3462 |
and flattens the statement into a series of cleanups. |
| 3463 |
|
| 3464 |
Conditional expressions are re-written to regular 'if' statements. |
| 3465 |
The cleanups for variables initialized inside a conditional (including |
| 3466 |
nested cases) are wrapped in a try-finally clause, with guard variables |
| 3467 |
to determine which DTORs need to be run. */ |
| 3468 |
|
| 3469 |
static tree |
| 3470 |
maybe_promote_temps (tree *stmt, void *d) |
| 3471 |
{ |
| 3472 |
susp_frame_data *awpts = (susp_frame_data *) d; |
| 3473 |
|
| 3474 |
location_t sloc = EXPR_LOCATION (*stmt); |
| 3475 |
tree expr = *stmt; |
| 3476 |
/* Strip off uninteresting wrappers. */ |
| 3477 |
if (TREE_CODE (expr) == CLEANUP_POINT_EXPR) |
| 3478 |
expr = TREE_OPERAND (expr, 0); |
| 3479 |
if (TREE_CODE (expr) == EXPR_STMT) |
| 3480 |
expr = EXPR_STMT_EXPR (expr); |
| 3481 |
if (TREE_CODE (expr) == CONVERT_EXPR |
| 3482 |
&& VOID_TYPE_P (TREE_TYPE (expr))) |
| 3483 |
expr = TREE_OPERAND (expr, 0); |
| 3484 |
STRIP_NOPS (expr); |
| 3485 |
|
| 3486 |
/* We walk the statement trees, flattening it into an ordered list of |
| 3487 |
variables with initializers and fragments corresponding to compound |
| 3488 |
expressions, truth or/and if and ternary conditionals. Conditional |
| 3489 |
expressions carry a nested list of fragments for the then and else |
| 3490 |
clauses. We anchor to the 'bottom' of the fragment list; we will write |
| 3491 |
a cleanup nest with one shell for each variable initialized. */ |
| 3492 |
var_nest_node *root = new var_nest_node (NULL_TREE, expr, NULL, NULL); |
| 3493 |
/* Check to see we didn't promote one twice. */ |
| 3494 |
hash_set<tree> promoted_vars; |
| 3495 |
hash_set<tree> used_temps; |
| 3496 |
flatten_await_stmt (root, &promoted_vars, &used_temps, NULL); |
| 3497 |
|
| 3498 |
gcc_checking_assert (root->next == NULL); |
| 3499 |
tree vlist = NULL_TREE; |
| 3500 |
var_nest_node *t = root; |
| 3501 |
/* We build the bind scope expression from the bottom-up. |
| 3502 |
EXPR_LIST holds the inner expression nest at the current cleanup |
| 3503 |
level (becoming the final expression list when we've exhausted the |
| 3504 |
number of sub-expression fragments). */ |
| 3505 |
tree expr_list = NULL_TREE; |
| 3506 |
do |
| 3507 |
{ |
| 3508 |
tree new_list = push_stmt_list (); |
| 3509 |
/* When we have a promoted variable, then add that to the bind scope |
| 3510 |
and initialize it. When there's no promoted variable, we just need |
| 3511 |
to run the initializer. |
| 3512 |
If the initializer is a conditional expression, we need to collect |
| 3513 |
and declare any promoted variables nested within it. DTORs for such |
| 3514 |
variables must be run conditionally too. |
| 3515 |
|
| 3516 |
Since here we're synthetically processing code here, we've already |
| 3517 |
emitted any Wunused-result warnings. Below, however, we call |
| 3518 |
finish_expr_stmt, which will convert its operand to void, and could |
| 3519 |
result in such a diagnostic being emitted. To avoid that, convert to |
| 3520 |
void ahead of time. */ |
| 3521 |
if (t->var) |
| 3522 |
{ |
| 3523 |
tree var = t->var; |
| 3524 |
DECL_CHAIN (var) = vlist; |
| 3525 |
vlist = var; |
| 3526 |
add_decl_expr (var); |
| 3527 |
if (TREE_CODE (t->init) == COND_EXPR) |
| 3528 |
process_conditional (t, vlist); |
| 3529 |
else |
| 3530 |
finish_expr_stmt (convert_to_void (t->init, ICV_STATEMENT, tf_none)); |
| 3531 |
if (tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error)) |
| 3532 |
{ |
| 3533 |
tree cl = build_stmt (sloc, CLEANUP_STMT, expr_list, cleanup, var); |
| 3534 |
add_stmt (cl); /* push this onto the level above. */ |
| 3535 |
} |
| 3536 |
else if (expr_list) |
| 3537 |
{ |
| 3538 |
if (TREE_CODE (expr_list) != STATEMENT_LIST) |
| 3539 |
add_stmt (expr_list); |
| 3540 |
else if (!tsi_end_p (tsi_start (expr_list))) |
| 3541 |
add_stmt (expr_list); |
| 3542 |
} |
| 3543 |
} |
| 3544 |
else |
| 3545 |
{ |
| 3546 |
if (TREE_CODE (t->init) == COND_EXPR) |
| 3547 |
process_conditional (t, vlist); |
| 3548 |
else |
| 3549 |
finish_expr_stmt (convert_to_void (t->init, ICV_STATEMENT, tf_none)); |
| 3550 |
if (expr_list) |
| 3551 |
{ |
| 3552 |
if (TREE_CODE (expr_list) != STATEMENT_LIST) |
| 3553 |
add_stmt (expr_list); |
| 3554 |
else if (!tsi_end_p (tsi_start (expr_list))) |
| 3555 |
add_stmt (expr_list); |
| 3556 |
} |
| 3557 |
} |
| 3558 |
expr_list = pop_stmt_list (new_list); |
| 3559 |
var_nest_node *old = t; |
| 3560 |
t = t->prev; |
| 3561 |
delete old; |
| 3562 |
} while (t); |
| 3563 |
|
| 3564 |
/* Now produce the bind expression containing the 'promoted' temporaries |
| 3565 |
as its variable list, and the cleanup nest as the statement. */ |
| 3566 |
tree await_bind = build3_loc (sloc, BIND_EXPR, void_type_node, |
| 3567 |
NULL, NULL, NULL); |
| 3568 |
BIND_EXPR_BODY (await_bind) = expr_list; |
| 3569 |
BIND_EXPR_VARS (await_bind) = nreverse (vlist); |
| 3570 |
tree b_block = make_node (BLOCK); |
| 3571 |
if (!awpts->block_stack->is_empty ()) |
| 3572 |
{ |
| 3573 |
tree s_block = awpts->block_stack->last (); |
| 3574 |
if (s_block) |
| 3575 |
{ |
| 3576 |
BLOCK_SUPERCONTEXT (b_block) = s_block; |
| 3577 |
BLOCK_CHAIN (b_block) = BLOCK_SUBBLOCKS (s_block); |
| 3578 |
BLOCK_SUBBLOCKS (s_block) = b_block; |
| 3579 |
} |
| 3580 |
} |
| 3581 |
BLOCK_VARS (b_block) = BIND_EXPR_VARS (await_bind) ; |
| 3582 |
BIND_EXPR_BLOCK (await_bind) = b_block; |
| 3583 |
TREE_SIDE_EFFECTS (await_bind) = TREE_SIDE_EFFECTS (BIND_EXPR_BODY (await_bind)); |
| 3584 |
*stmt = await_bind; |
| 3585 |
hash_set<tree> visited; |
| 3586 |
return cp_walk_tree (stmt, register_awaits, d, &visited); |
| 3587 |
} |
| 3588 |
|
| 3589 |
/* Relatively lightweight callback to do initial assessment: |
| 3590 |
0) Rewrite some await expressions. |
| 3591 |
1) If the statement/expression contains any await expressions. |
| 3592 |
2) If the statement/expression potentially requires a re-write to handle |
| 3593 |
TRUTH_{AND,OR}IF_EXPRs since, in most cases, they will need expansion |
| 3594 |
so that the await expressions are not processed in the case of the |
| 3595 |
short-circuit arm. |
| 3596 |
|
| 3597 |
CO_YIELD expressions are re-written to their underlying co_await. */ |
| 3598 |
|
| 3599 |
static tree |
| 3600 |
analyze_expression_awaits (tree *stmt, int *do_subtree, void *d) |
| 3601 |
{ |
| 3602 |
susp_frame_data *awpts = (susp_frame_data *) d; |
| 3603 |
|
| 3604 |
switch (TREE_CODE (*stmt)) |
| 3605 |
{ |
| 3606 |
default: return NULL_TREE; |
| 3607 |
case CALL_EXPR: |
| 3608 |
{ |
| 3609 |
tree fn = cp_get_callee_fndecl_nofold (*stmt); |
| 3610 |
/* Special-cases where we want to re-write await expressions to some |
| 3611 |
other value before they are otherwise processed. */ |
| 3612 |
if (fn && DECL_IS_BUILTIN_CONSTANT_P (fn)) |
| 3613 |
{ |
| 3614 |
gcc_checking_assert (call_expr_nargs (*stmt) == 1); |
| 3615 |
tree expr = CALL_EXPR_ARG (*stmt, 0); |
| 3616 |
if (cp_walk_tree (&expr, find_any_await, nullptr, NULL)) |
| 3617 |
{ |
| 3618 |
if (TREE_CONSTANT (maybe_constant_value (expr))) |
| 3619 |
*stmt = integer_one_node; |
| 3620 |
else |
| 3621 |
*stmt = integer_zero_node; |
| 3622 |
} |
| 3623 |
*do_subtree = 0; |
| 3624 |
} |
| 3625 |
else if (!fn && CALL_EXPR_IFN (*stmt) == IFN_ASSUME) |
| 3626 |
{ |
| 3627 |
tree expr = CALL_EXPR_ARG (*stmt, 0); |
| 3628 |
if (TREE_SIDE_EFFECTS (expr)) |
| 3629 |
{ |
| 3630 |
location_t loc_e = cp_expr_location (expr); |
| 3631 |
location_t loc_s = cp_expr_location (*stmt); |
| 3632 |
location_t loc_n = make_location (loc_e, loc_s, loc_s); |
| 3633 |
warning_at (loc_n, OPT_Wattributes,"assumption ignored" |
| 3634 |
" because it contains an await-expression"); |
| 3635 |
*stmt = build_empty_stmt (loc_n); |
| 3636 |
} |
| 3637 |
} |
| 3638 |
} |
| 3639 |
break; |
| 3640 |
case CO_YIELD_EXPR: |
| 3641 |
/* co_yield is syntactic sugar, re-write it to co_await. */ |
| 3642 |
*stmt = TREE_OPERAND (*stmt, 1); |
| 3643 |
/* FALLTHROUGH */ |
| 3644 |
case CO_AWAIT_EXPR: |
| 3645 |
awpts->saw_awaits++; |
| 3646 |
/* A non-null initializer for the awaiter means we need to expand. */ |
| 3647 |
if (TREE_OPERAND (*stmt, 2)) |
| 3648 |
awpts->has_awaiter_init = true; |
| 3649 |
break; |
| 3650 |
case TRUTH_ANDIF_EXPR: |
| 3651 |
case TRUTH_ORIF_EXPR: |
| 3652 |
{ |
| 3653 |
/* We don't need special action for awaits in the always-executed |
| 3654 |
arm of a TRUTH_IF. */ |
| 3655 |
if (tree res = cp_walk_tree (&TREE_OPERAND (*stmt, 0), |
| 3656 |
analyze_expression_awaits, d, NULL)) |
| 3657 |
return res; |
| 3658 |
/* However, if there are await expressions on the conditionally |
| 3659 |
executed branch, we must expand the TRUTH_IF to ensure that the |
| 3660 |
expanded await expression control-flow is fully contained in the |
| 3661 |
conditionally executed code. */ |
| 3662 |
unsigned aw_count = awpts->saw_awaits; |
| 3663 |
if (tree res = cp_walk_tree (&TREE_OPERAND (*stmt, 1), |
| 3664 |
analyze_expression_awaits, d, NULL)) |
| 3665 |
return res; |
| 3666 |
if (awpts->saw_awaits > aw_count) |
| 3667 |
{ |
| 3668 |
awpts->truth_aoif_to_expand->add (*stmt); |
| 3669 |
awpts->needs_truth_if_exp = true; |
| 3670 |
} |
| 3671 |
/* We've done the sub-trees here. */ |
| 3672 |
*do_subtree = 0; |
| 3673 |
} |
| 3674 |
break; |
| 3675 |
} |
| 3676 |
|
| 3677 |
return NULL_TREE; /* Recurse until done. */ |
| 3678 |
} |
| 3679 |
|
| 3680 |
/* Given *EXPR |
| 3681 |
If EXPR contains a TRUTH_{AND,OR}IF_EXPR, TAOIE with an await expr on |
| 3682 |
the conditionally executed branch, change this in a ternary operator. |
| 3683 |
|
| 3684 |
bool not_expr = TAOIE == TRUTH_ORIF_EXPR ? NOT : NOP; |
| 3685 |
not_expr (always-exec expr) ? conditionally-exec expr : not_expr; |
| 3686 |
|
| 3687 |
Apply this recursively to the condition and the conditionally-exec |
| 3688 |
branch. */ |
| 3689 |
|
| 3690 |
struct truth_if_transform { |
| 3691 |
tree *orig_stmt; |
| 3692 |
tree scratch_var; |
| 3693 |
hash_set<tree> *truth_aoif_to_expand; |
| 3694 |
}; |
| 3695 |
|
| 3696 |
static tree |
| 3697 |
expand_one_truth_if (tree *expr, int *do_subtree, void *d) |
| 3698 |
{ |
| 3699 |
truth_if_transform *xform = (truth_if_transform *) d; |
| 3700 |
|
| 3701 |
bool needs_not = false; |
| 3702 |
switch (TREE_CODE (*expr)) |
| 3703 |
{ |
| 3704 |
default: break; |
| 3705 |
case TRUTH_ORIF_EXPR: |
| 3706 |
needs_not = true; |
| 3707 |
/* FALLTHROUGH */ |
| 3708 |
case TRUTH_ANDIF_EXPR: |
| 3709 |
{ |
| 3710 |
if (!xform->truth_aoif_to_expand->contains (*expr)) |
| 3711 |
break; |
| 3712 |
|
| 3713 |
location_t sloc = EXPR_LOCATION (*expr); |
| 3714 |
/* Transform truth expression into a cond expression with |
| 3715 |
* the always-executed arm as the condition. |
| 3716 |
* the conditionally-executed arm as the then clause. |
| 3717 |
* the 'else' clause is fixed: 'true' for ||,'false' for &&. */ |
| 3718 |
tree cond = TREE_OPERAND (*expr, 0); |
| 3719 |
tree test1 = TREE_OPERAND (*expr, 1); |
| 3720 |
tree fixed = needs_not ? boolean_true_node : boolean_false_node; |
| 3721 |
if (needs_not) |
| 3722 |
cond = build1 (TRUTH_NOT_EXPR, boolean_type_node, cond); |
| 3723 |
tree cond_expr |
| 3724 |
= build3_loc (sloc, COND_EXPR, boolean_type_node, |
| 3725 |
cond, test1, fixed); |
| 3726 |
*expr = cond_expr; |
| 3727 |
if (tree res = cp_walk_tree (&COND_EXPR_COND (*expr), |
| 3728 |
expand_one_truth_if, d, NULL)) |
| 3729 |
return res; |
| 3730 |
if (tree res = cp_walk_tree (&COND_EXPR_THEN (*expr), |
| 3731 |
expand_one_truth_if, d, NULL)) |
| 3732 |
return res; |
| 3733 |
/* We've manually processed necessary sub-trees here. */ |
| 3734 |
*do_subtree = 0; |
| 3735 |
} |
| 3736 |
break; |
| 3737 |
} |
| 3738 |
return NULL_TREE; |
| 3739 |
} |
| 3740 |
|
| 3741 |
/* Helper that adds a new variable of VAR_TYPE to a bind scope BIND, the |
| 3742 |
name is made up from NAM_ROOT, NAM_VERS. */ |
| 3743 |
|
| 3744 |
static tree |
| 3745 |
add_var_to_bind (tree& bind, tree var_type, |
| 3746 |
const char *nam_root, unsigned nam_vers) |
| 3747 |
{ |
| 3748 |
tree b_vars = BIND_EXPR_VARS (bind); |
| 3749 |
/* Build a variable to hold the condition, this will be included in the |
| 3750 |
frame as a local var. */ |
| 3751 |
char *nam = xasprintf ("__%s_%d", nam_root, nam_vers); |
| 3752 |
tree newvar = build_lang_decl (VAR_DECL, get_identifier (nam), var_type); |
| 3753 |
free (nam); |
| 3754 |
DECL_CHAIN (newvar) = b_vars; |
| 3755 |
BIND_EXPR_VARS (bind) = newvar; |
| 3756 |
return newvar; |
| 3757 |
} |
| 3758 |
|
| 3759 |
/* Helper to build and add if (!cond) break; */ |
| 3760 |
|
| 3761 |
static void |
| 3762 |
coro_build_add_if_not_cond_break (tree cond) |
| 3763 |
{ |
| 3764 |
tree if_stmt = begin_if_stmt (); |
| 3765 |
tree invert = build1 (TRUTH_NOT_EXPR, boolean_type_node, cond); |
| 3766 |
finish_if_stmt_cond (invert, if_stmt); |
| 3767 |
finish_break_stmt (); |
| 3768 |
finish_then_clause (if_stmt); |
| 3769 |
finish_if_stmt (if_stmt); |
| 3770 |
} |
| 3771 |
|
| 3772 |
/* Tree walk callback to replace continue statements with goto label. */ |
| 3773 |
static tree |
| 3774 |
replace_continue (tree *stmt, int *do_subtree, void *d) |
| 3775 |
{ |
| 3776 |
tree expr = *stmt; |
| 3777 |
if (TREE_CODE (expr) == CLEANUP_POINT_EXPR) |
| 3778 |
expr = TREE_OPERAND (expr, 0); |
| 3779 |
if (CONVERT_EXPR_P (expr) && VOID_TYPE_P (TREE_TYPE (expr))) |
| 3780 |
expr = TREE_OPERAND (expr, 0); |
| 3781 |
STRIP_NOPS (expr); |
| 3782 |
if (!STATEMENT_CLASS_P (expr)) |
| 3783 |
return NULL_TREE; |
| 3784 |
|
| 3785 |
switch (TREE_CODE (expr)) |
| 3786 |
{ |
| 3787 |
/* Unless it's a special case, just walk the subtrees as usual. */ |
| 3788 |
default: return NULL_TREE; |
| 3789 |
|
| 3790 |
case CONTINUE_STMT: |
| 3791 |
{ |
| 3792 |
tree *label = (tree *)d; |
| 3793 |
location_t loc = EXPR_LOCATION (expr); |
| 3794 |
/* re-write a continue to goto label. */ |
| 3795 |
*stmt = build_stmt (loc, GOTO_EXPR, *label); |
| 3796 |
*do_subtree = 0; |
| 3797 |
return NULL_TREE; |
| 3798 |
} |
| 3799 |
|
| 3800 |
/* Statements that do not require recursion. */ |
| 3801 |
case DECL_EXPR: |
| 3802 |
case BREAK_STMT: |
| 3803 |
case GOTO_EXPR: |
| 3804 |
case LABEL_EXPR: |
| 3805 |
case CASE_LABEL_EXPR: |
| 3806 |
case ASM_EXPR: |
| 3807 |
/* These must break recursion. */ |
| 3808 |
case FOR_STMT: |
| 3809 |
case WHILE_STMT: |
| 3810 |
case DO_STMT: |
| 3811 |
*do_subtree = 0; |
| 3812 |
return NULL_TREE; |
| 3813 |
} |
| 3814 |
} |
| 3815 |
|
| 3816 |
/* Tree walk callback to analyze, register and pre-process statements that |
| 3817 |
contain await expressions. */ |
| 3818 |
|
| 3819 |
static tree |
| 3820 |
await_statement_walker (tree *stmt, int *do_subtree, void *d) |
| 3821 |
{ |
| 3822 |
tree res = NULL_TREE; |
| 3823 |
susp_frame_data *awpts = (susp_frame_data *) d; |
| 3824 |
|
| 3825 |
/* Process a statement at a time. */ |
| 3826 |
if (TREE_CODE (*stmt) == BIND_EXPR) |
| 3827 |
{ |
| 3828 |
/* For conditional expressions, we might wish to add an artificial var |
| 3829 |
to their containing bind expr. */ |
| 3830 |
vec_safe_push (awpts->bind_stack, *stmt); |
| 3831 |
/* We might need to insert a new bind expression, and want to link it |
| 3832 |
into the correct scope, so keep a note of the current block scope. */ |
| 3833 |
tree blk = BIND_EXPR_BLOCK (*stmt); |
| 3834 |
vec_safe_push (awpts->block_stack, blk); |
| 3835 |
res = cp_walk_tree (&BIND_EXPR_BODY (*stmt), await_statement_walker, |
| 3836 |
d, NULL); |
| 3837 |
awpts->block_stack->pop (); |
| 3838 |
awpts->bind_stack->pop (); |
| 3839 |
*do_subtree = 0; /* Done subtrees. */ |
| 3840 |
return res; |
| 3841 |
} |
| 3842 |
else if (TREE_CODE (*stmt) == STATEMENT_LIST) |
| 3843 |
{ |
| 3844 |
for (tree &s : tsi_range (*stmt)) |
| 3845 |
{ |
| 3846 |
res = cp_walk_tree (&s, await_statement_walker, |
| 3847 |
d, NULL); |
| 3848 |
if (res) |
| 3849 |
return res; |
| 3850 |
} |
| 3851 |
*do_subtree = 0; /* Done subtrees. */ |
| 3852 |
return NULL_TREE; |
| 3853 |
} |
| 3854 |
|
| 3855 |
/* We have something to be handled as a single statement. We have to handle |
| 3856 |
a few statements specially where await statements have to be moved out of |
| 3857 |
constructs. */ |
| 3858 |
tree expr = *stmt; |
| 3859 |
if (TREE_CODE (*stmt) == CLEANUP_POINT_EXPR) |
| 3860 |
expr = TREE_OPERAND (expr, 0); |
| 3861 |
STRIP_NOPS (expr); |
| 3862 |
|
| 3863 |
if (STATEMENT_CLASS_P (expr)) |
| 3864 |
switch (TREE_CODE (expr)) |
| 3865 |
{ |
| 3866 |
/* Unless it's a special case, just walk the subtrees as usual. */ |
| 3867 |
default: return NULL_TREE; |
| 3868 |
|
| 3869 |
/* When we have a conditional expression, which contains one or more |
| 3870 |
await expressions, we have to break the condition out into a |
| 3871 |
regular statement so that the control flow introduced by the await |
| 3872 |
transforms can be implemented. */ |
| 3873 |
case IF_STMT: |
| 3874 |
{ |
| 3875 |
tree *await_ptr; |
| 3876 |
hash_set<tree> visited; |
| 3877 |
/* Transform 'if (cond with awaits) then stmt1 else stmt2' into |
| 3878 |
bool cond = cond with awaits. |
| 3879 |
if (cond) then stmt1 else stmt2. */ |
| 3880 |
tree if_stmt = *stmt; |
| 3881 |
/* We treat the condition as if it was a stand-alone statement, |
| 3882 |
to see if there are any await expressions which will be analyzed |
| 3883 |
and registered. */ |
| 3884 |
if (!(cp_walk_tree (&IF_COND (if_stmt), |
| 3885 |
find_any_await, &await_ptr, &visited))) |
| 3886 |
return NULL_TREE; /* Nothing special to do here. */ |
| 3887 |
|
| 3888 |
gcc_checking_assert (!awpts->bind_stack->is_empty()); |
| 3889 |
tree& bind_expr = awpts->bind_stack->last (); |
| 3890 |
tree newvar = add_var_to_bind (bind_expr, boolean_type_node, |
| 3891 |
"ifcd", awpts->cond_number++); |
| 3892 |
tree insert_list = push_stmt_list (); |
| 3893 |
tree cond_inner = IF_COND (if_stmt); |
| 3894 |
if (TREE_CODE (cond_inner) == CLEANUP_POINT_EXPR) |
| 3895 |
cond_inner = TREE_OPERAND (cond_inner, 0); |
| 3896 |
add_decl_expr (newvar); |
| 3897 |
location_t sloc = EXPR_LOCATION (IF_COND (if_stmt)); |
| 3898 |
/* We want to initialize the new variable with the expression |
| 3899 |
that contains the await(s) and potentially also needs to |
| 3900 |
have truth_if expressions expanded. */ |
| 3901 |
tree new_s = cp_build_init_expr (sloc, newvar, cond_inner); |
| 3902 |
finish_expr_stmt (new_s); |
| 3903 |
IF_COND (if_stmt) = newvar; |
| 3904 |
add_stmt (if_stmt); |
| 3905 |
*stmt = pop_stmt_list (insert_list); |
| 3906 |
/* So now walk the new statement list. */ |
| 3907 |
res = cp_walk_tree (stmt, await_statement_walker, d, NULL); |
| 3908 |
*do_subtree = 0; /* Done subtrees. */ |
| 3909 |
return res; |
| 3910 |
} |
| 3911 |
break; |
| 3912 |
case FOR_STMT: |
| 3913 |
{ |
| 3914 |
tree *await_ptr; |
| 3915 |
hash_set<tree> visited; |
| 3916 |
/* for loops only need special treatment if the condition or the |
| 3917 |
iteration expression contain a co_await. */ |
| 3918 |
tree for_stmt = *stmt; |
| 3919 |
/* At present, the FE always generates a separate initializer for |
| 3920 |
the FOR_INIT_STMT, when the expression has an await. Check that |
| 3921 |
this assumption holds in the future. */ |
| 3922 |
gcc_checking_assert |
| 3923 |
(!(cp_walk_tree (&FOR_INIT_STMT (for_stmt), find_any_await, |
| 3924 |
&await_ptr, &visited))); |
| 3925 |
|
| 3926 |
visited.empty (); |
| 3927 |
bool for_cond_await |
| 3928 |
= cp_walk_tree (&FOR_COND (for_stmt), find_any_await, |
| 3929 |
&await_ptr, &visited); |
| 3930 |
|
| 3931 |
visited.empty (); |
| 3932 |
bool for_expr_await |
| 3933 |
= cp_walk_tree (&FOR_EXPR (for_stmt), find_any_await, |
| 3934 |
&await_ptr, &visited); |
| 3935 |
|
| 3936 |
/* If the condition has an await, then we will need to rewrite the |
| 3937 |
loop as |
| 3938 |
for (init expression;true;iteration expression) { |
| 3939 |
condition = await expression; |
| 3940 |
if (condition) |
| 3941 |
break; |
| 3942 |
... |
| 3943 |
} |
| 3944 |
*/ |
| 3945 |
if (for_cond_await) |
| 3946 |
{ |
| 3947 |
tree insert_list = push_stmt_list (); |
| 3948 |
/* This will be expanded when the revised body is handled. */ |
| 3949 |
coro_build_add_if_not_cond_break (FOR_COND (for_stmt)); |
| 3950 |
/* .. add the original for body. */ |
| 3951 |
add_stmt (FOR_BODY (for_stmt)); |
| 3952 |
/* To make the new for body. */ |
| 3953 |
FOR_BODY (for_stmt) = pop_stmt_list (insert_list); |
| 3954 |
FOR_COND (for_stmt) = boolean_true_node; |
| 3955 |
} |
| 3956 |
/* If the iteration expression has an await, it's a bit more |
| 3957 |
tricky. |
| 3958 |
for (init expression;condition;) { |
| 3959 |
... |
| 3960 |
iteration_expr_label: |
| 3961 |
iteration expression with await; |
| 3962 |
} |
| 3963 |
but, then we will need to re-write any continue statements into |
| 3964 |
'goto iteration_expr_label:'. |
| 3965 |
*/ |
| 3966 |
if (for_expr_await) |
| 3967 |
{ |
| 3968 |
location_t sloc = EXPR_LOCATION (FOR_EXPR (for_stmt)); |
| 3969 |
tree insert_list = push_stmt_list (); |
| 3970 |
/* The original for body. */ |
| 3971 |
add_stmt (FOR_BODY (for_stmt)); |
| 3972 |
char *buf = xasprintf ("for.iter.expr.%u", awpts->cond_number++); |
| 3973 |
tree it_expr_label |
| 3974 |
= create_named_label_with_ctx (sloc, buf, NULL_TREE); |
| 3975 |
free (buf); |
| 3976 |
add_stmt (build_stmt (sloc, LABEL_EXPR, it_expr_label)); |
| 3977 |
tree for_expr = FOR_EXPR (for_stmt); |
| 3978 |
/* Present the iteration expression as a statement. */ |
| 3979 |
if (TREE_CODE (for_expr) == CLEANUP_POINT_EXPR) |
| 3980 |
for_expr = TREE_OPERAND (for_expr, 0); |
| 3981 |
STRIP_NOPS (for_expr); |
| 3982 |
finish_expr_stmt (for_expr); |
| 3983 |
FOR_EXPR (for_stmt) = NULL_TREE; |
| 3984 |
FOR_BODY (for_stmt) = pop_stmt_list (insert_list); |
| 3985 |
/* rewrite continue statements to goto label. */ |
| 3986 |
hash_set<tree> visited_continue; |
| 3987 |
if ((res = cp_walk_tree (&FOR_BODY (for_stmt), |
| 3988 |
replace_continue, &it_expr_label, &visited_continue))) |
| 3989 |
return res; |
| 3990 |
} |
| 3991 |
|
| 3992 |
/* So now walk the body statement (list), if there were no await |
| 3993 |
expressions, then this handles the original body - and either |
| 3994 |
way we will have finished with this statement. */ |
| 3995 |
res = cp_walk_tree (&FOR_BODY (for_stmt), |
| 3996 |
await_statement_walker, d, NULL); |
| 3997 |
*do_subtree = 0; /* Done subtrees. */ |
| 3998 |
return res; |
| 3999 |
} |
| 4000 |
break; |
| 4001 |
case WHILE_STMT: |
| 4002 |
{ |
| 4003 |
/* We turn 'while (cond with awaits) stmt' into |
| 4004 |
while (true) { |
| 4005 |
if (!(cond with awaits)) |
| 4006 |
break; |
| 4007 |
stmt.. |
| 4008 |
} */ |
| 4009 |
tree *await_ptr; |
| 4010 |
hash_set<tree> visited; |
| 4011 |
tree while_stmt = *stmt; |
| 4012 |
if (!(cp_walk_tree (&WHILE_COND (while_stmt), |
| 4013 |
find_any_await, &await_ptr, &visited))) |
| 4014 |
return NULL_TREE; /* Nothing special to do here. */ |
| 4015 |
|
| 4016 |
tree insert_list = push_stmt_list (); |
| 4017 |
coro_build_add_if_not_cond_break (WHILE_COND (while_stmt)); |
| 4018 |
/* The original while body. */ |
| 4019 |
add_stmt (WHILE_BODY (while_stmt)); |
| 4020 |
/* The new while body. */ |
| 4021 |
WHILE_BODY (while_stmt) = pop_stmt_list (insert_list); |
| 4022 |
WHILE_COND (while_stmt) = boolean_true_node; |
| 4023 |
/* So now walk the new statement list. */ |
| 4024 |
res = cp_walk_tree (&WHILE_BODY (while_stmt), |
| 4025 |
await_statement_walker, d, NULL); |
| 4026 |
*do_subtree = 0; /* Done subtrees. */ |
| 4027 |
return res; |
| 4028 |
} |
| 4029 |
break; |
| 4030 |
case DO_STMT: |
| 4031 |
{ |
| 4032 |
/* We turn do stmt while (cond with awaits) into: |
| 4033 |
do { |
| 4034 |
stmt.. |
| 4035 |
if (!(cond with awaits)) |
| 4036 |
break; |
| 4037 |
} while (true); */ |
| 4038 |
tree do_stmt = *stmt; |
| 4039 |
tree *await_ptr; |
| 4040 |
hash_set<tree> visited; |
| 4041 |
if (!(cp_walk_tree (&DO_COND (do_stmt), |
| 4042 |
find_any_await, &await_ptr, &visited))) |
| 4043 |
return NULL_TREE; /* Nothing special to do here. */ |
| 4044 |
|
| 4045 |
tree insert_list = push_stmt_list (); |
| 4046 |
/* The original do stmt body. */ |
| 4047 |
add_stmt (DO_BODY (do_stmt)); |
| 4048 |
coro_build_add_if_not_cond_break (DO_COND (do_stmt)); |
| 4049 |
/* The new while body. */ |
| 4050 |
DO_BODY (do_stmt) = pop_stmt_list (insert_list); |
| 4051 |
DO_COND (do_stmt) = boolean_true_node; |
| 4052 |
/* So now walk the new statement list. */ |
| 4053 |
res = cp_walk_tree (&DO_BODY (do_stmt), await_statement_walker, |
| 4054 |
d, NULL); |
| 4055 |
*do_subtree = 0; /* Done subtrees. */ |
| 4056 |
return res; |
| 4057 |
} |
| 4058 |
break; |
| 4059 |
case SWITCH_STMT: |
| 4060 |
{ |
| 4061 |
/* We turn 'switch (cond with awaits) stmt' into |
| 4062 |
switch_type cond = cond with awaits |
| 4063 |
switch (cond) stmt. */ |
| 4064 |
tree sw_stmt = *stmt; |
| 4065 |
tree *await_ptr; |
| 4066 |
hash_set<tree> visited; |
| 4067 |
if (!(cp_walk_tree (&SWITCH_STMT_COND (sw_stmt), |
| 4068 |
find_any_await, &await_ptr, &visited))) |
| 4069 |
return NULL_TREE; /* Nothing special to do here. */ |
| 4070 |
|
| 4071 |
gcc_checking_assert (!awpts->bind_stack->is_empty()); |
| 4072 |
/* Build a variable to hold the condition, this will be |
| 4073 |
included in the frame as a local var. */ |
| 4074 |
tree& bind_expr = awpts->bind_stack->last (); |
| 4075 |
tree sw_type = SWITCH_STMT_TYPE (sw_stmt); |
| 4076 |
tree newvar = add_var_to_bind (bind_expr, sw_type, "swch", |
| 4077 |
awpts->cond_number++); |
| 4078 |
tree insert_list = push_stmt_list (); |
| 4079 |
add_decl_expr (newvar); |
| 4080 |
|
| 4081 |
tree cond_inner = SWITCH_STMT_COND (sw_stmt); |
| 4082 |
if (TREE_CODE (cond_inner) == CLEANUP_POINT_EXPR) |
| 4083 |
cond_inner = TREE_OPERAND (cond_inner, 0); |
| 4084 |
location_t sloc = EXPR_LOCATION (SWITCH_STMT_COND (sw_stmt)); |
| 4085 |
tree new_s = cp_build_init_expr (sloc, newvar, |
| 4086 |
cond_inner); |
| 4087 |
finish_expr_stmt (new_s); |
| 4088 |
SWITCH_STMT_COND (sw_stmt) = newvar; |
| 4089 |
/* Now add the switch statement with the condition re- |
| 4090 |
written to use the local var. */ |
| 4091 |
add_stmt (sw_stmt); |
| 4092 |
*stmt = pop_stmt_list (insert_list); |
| 4093 |
/* Process the expanded list. */ |
| 4094 |
res = cp_walk_tree (stmt, await_statement_walker, |
| 4095 |
d, NULL); |
| 4096 |
*do_subtree = 0; /* Done subtrees. */ |
| 4097 |
return res; |
| 4098 |
} |
| 4099 |
break; |
| 4100 |
case CO_RETURN_EXPR: |
| 4101 |
{ |
| 4102 |
/* Expand the co_return as per [stmt.return.coroutine] |
| 4103 |
- for co_return; |
| 4104 |
{ p.return_void (); goto final_suspend; } |
| 4105 |
- for co_return [void expr]; |
| 4106 |
{ expr; p.return_void(); goto final_suspend;} |
| 4107 |
- for co_return [non void expr]; |
| 4108 |
{ p.return_value(expr); goto final_suspend; } */ |
| 4109 |
location_t loc = EXPR_LOCATION (expr); |
| 4110 |
tree call = TREE_OPERAND (expr, 1); |
| 4111 |
expr = TREE_OPERAND (expr, 0); |
| 4112 |
tree ret_list = push_stmt_list (); |
| 4113 |
/* [stmt.return.coroutine], 2.2 |
| 4114 |
If expr is present and void, it is placed immediately before |
| 4115 |
the call for return_void; */ |
| 4116 |
if (expr && VOID_TYPE_P (TREE_TYPE (expr))) |
| 4117 |
finish_expr_stmt (expr); |
| 4118 |
/* Insert p.return_{void,value(expr)}. */ |
| 4119 |
finish_expr_stmt (call); |
| 4120 |
TREE_USED (awpts->fs_label) = 1; |
| 4121 |
add_stmt (build_stmt (loc, GOTO_EXPR, awpts->fs_label)); |
| 4122 |
*stmt = pop_stmt_list (ret_list); |
| 4123 |
res = cp_walk_tree (stmt, await_statement_walker, d, NULL); |
| 4124 |
/* Once this is complete, we will have processed subtrees. */ |
| 4125 |
*do_subtree = 0; |
| 4126 |
return res; |
| 4127 |
} |
| 4128 |
break; |
| 4129 |
case HANDLER: |
| 4130 |
{ |
| 4131 |
/* [expr.await] An await-expression shall appear only in a |
| 4132 |
potentially-evaluated expression within the compound-statement |
| 4133 |
of a function-body outside of a handler. */ |
| 4134 |
tree *await_ptr; |
| 4135 |
hash_set<tree> visited; |
| 4136 |
if (!(cp_walk_tree (&HANDLER_BODY (expr), find_any_await, |
| 4137 |
&await_ptr, &visited))) |
| 4138 |
return NULL_TREE; /* All OK. */ |
| 4139 |
location_t loc = EXPR_LOCATION (*await_ptr); |
| 4140 |
error_at (loc, "await expressions are not permitted in handlers"); |
| 4141 |
return NULL_TREE; /* This is going to fail later anyway. */ |
| 4142 |
} |
| 4143 |
break; |
| 4144 |
} |
| 4145 |
else if (EXPR_P (expr)) |
| 4146 |
{ |
| 4147 |
hash_set<tree> visited; |
| 4148 |
tree *await_ptr; |
| 4149 |
if (!(cp_walk_tree (stmt, find_any_await, &await_ptr, &visited))) |
| 4150 |
return NULL_TREE; /* Nothing special to do here. */ |
| 4151 |
|
| 4152 |
visited.empty (); |
| 4153 |
awpts->saw_awaits = 0; |
| 4154 |
hash_set<tree> truth_aoif_to_expand; |
| 4155 |
awpts->truth_aoif_to_expand = &truth_aoif_to_expand; |
| 4156 |
awpts->needs_truth_if_exp = false; |
| 4157 |
awpts->has_awaiter_init = false; |
| 4158 |
if ((res = cp_walk_tree (stmt, analyze_expression_awaits, d, &visited))) |
| 4159 |
return res; |
| 4160 |
*do_subtree = 0; /* Done subtrees. */ |
| 4161 |
if (!awpts->saw_awaits) |
| 4162 |
return NULL_TREE; /* Nothing special to do here. */ |
| 4163 |
|
| 4164 |
if (awpts->needs_truth_if_exp) |
| 4165 |
{ |
| 4166 |
/* If a truth-and/or-if expression has an await expression in the |
| 4167 |
conditionally-taken branch, then it must be rewritten into a |
| 4168 |
regular conditional. */ |
| 4169 |
truth_if_transform xf = {stmt, NULL_TREE, &truth_aoif_to_expand}; |
| 4170 |
if ((res = cp_walk_tree (stmt, expand_one_truth_if, &xf, NULL))) |
| 4171 |
return res; |
| 4172 |
} |
| 4173 |
/* Process this statement, which contains at least one await expression |
| 4174 |
to 'promote' temporary values to a coroutine frame slot. */ |
| 4175 |
return maybe_promote_temps (stmt, d); |
| 4176 |
} |
| 4177 |
/* Continue recursion, if needed. */ |
| 4178 |
return res; |
| 4179 |
} |
| 4180 |
|
| 4181 |
/* For figuring out what param usage we have. */ |
| 4182 |
|
| 4183 |
struct param_frame_data |
| 4184 |
{ |
| 4185 |
tree *field_list; |
| 4186 |
hash_map<tree, param_info> *param_uses; |
| 4187 |
hash_set<tree *> *visited; |
| 4188 |
location_t loc; |
| 4189 |
bool param_seen; |
| 4190 |
}; |
| 4191 |
|
| 4192 |
/* A tree walk callback that rewrites each parm use to the local variable |
| 4193 |
that represents its copy in the frame. */ |
| 4194 |
|
| 4195 |
static tree |
| 4196 |
rewrite_param_uses (tree *stmt, int *do_subtree ATTRIBUTE_UNUSED, void *d) |
| 4197 |
{ |
| 4198 |
param_frame_data *data = (param_frame_data *) d; |
| 4199 |
|
| 4200 |
/* For lambda closure content, we have to look specifically. */ |
| 4201 |
if (VAR_P (*stmt) && DECL_HAS_VALUE_EXPR_P (*stmt)) |
| 4202 |
{ |
| 4203 |
tree t = DECL_VALUE_EXPR (*stmt); |
| 4204 |
return cp_walk_tree (&t, rewrite_param_uses, d, NULL); |
| 4205 |
} |
| 4206 |
|
| 4207 |
if (unevaluated_p (TREE_CODE (*stmt))) |
| 4208 |
{ |
| 4209 |
/* No odr-uses in unevaluated operands. */ |
| 4210 |
*do_subtree = 0; |
| 4211 |
return NULL_TREE; |
| 4212 |
} |
| 4213 |
|
| 4214 |
if (TREE_CODE (*stmt) != PARM_DECL) |
| 4215 |
return NULL_TREE; |
| 4216 |
|
| 4217 |
/* If we already saw the containing expression, then we're done. */ |
| 4218 |
if (data->visited->add (stmt)) |
| 4219 |
return NULL_TREE; |
| 4220 |
|
| 4221 |
bool existed; |
| 4222 |
param_info &parm = data->param_uses->get_or_insert (*stmt, &existed); |
| 4223 |
gcc_checking_assert (existed); |
| 4224 |
|
| 4225 |
*stmt = parm.copy_var; |
| 4226 |
return NULL_TREE; |
| 4227 |
} |
| 4228 |
|
| 4229 |
/* Build up a set of info that determines how each param copy will be |
| 4230 |
handled. We store this in a hash map so that we can access it from |
| 4231 |
a tree walk callback that re-writes the original parameters to their |
| 4232 |
copies. */ |
| 4233 |
|
| 4234 |
void |
| 4235 |
cp_coroutine_transform::analyze_fn_parms () |
| 4236 |
{ |
| 4237 |
if (!DECL_ARGUMENTS (orig_fn_decl)) |
| 4238 |
return; |
| 4239 |
|
| 4240 |
/* Build a hash map with an entry for each param. |
| 4241 |
The key is the param tree. |
| 4242 |
Then we have an entry for the frame field name. |
| 4243 |
Then a cache for the field ref when we come to use it. |
| 4244 |
Then a tree list of the uses. |
| 4245 |
The second two entries start out empty - and only get populated |
| 4246 |
when we see uses. */ |
| 4247 |
bool lambda_p = LAMBDA_FUNCTION_P (orig_fn_decl); |
| 4248 |
|
| 4249 |
/* Count the param copies from 1 as per the std. */ |
| 4250 |
unsigned parm_num = 1; |
| 4251 |
for (tree arg = DECL_ARGUMENTS (orig_fn_decl); arg != NULL; |
| 4252 |
++parm_num, arg = DECL_CHAIN (arg)) |
| 4253 |
{ |
| 4254 |
bool existed; |
| 4255 |
param_info &parm = param_uses.get_or_insert (arg, &existed); |
| 4256 |
gcc_checking_assert (!existed); |
| 4257 |
parm.body_uses = NULL; |
| 4258 |
tree actual_type = TREE_TYPE (arg); |
| 4259 |
actual_type = complete_type_or_else (actual_type, orig_fn_decl); |
| 4260 |
if (actual_type == NULL_TREE) |
| 4261 |
actual_type = error_mark_node; |
| 4262 |
parm.orig_type = actual_type; |
| 4263 |
parm.by_ref = parm.pt_ref = parm.rv_ref = false; |
| 4264 |
if (TREE_CODE (actual_type) == REFERENCE_TYPE) |
| 4265 |
{ |
| 4266 |
/* If the user passes by reference, then we will save the |
| 4267 |
pointer to the original. As noted in |
| 4268 |
[dcl.fct.def.coroutine] / 13, if the lifetime of the |
| 4269 |
referenced item ends and then the coroutine is resumed, |
| 4270 |
we have UB; well, the user asked for it. */ |
| 4271 |
if (TYPE_REF_IS_RVALUE (actual_type)) |
| 4272 |
parm.rv_ref = true; |
| 4273 |
else |
| 4274 |
parm.pt_ref = true; |
| 4275 |
} |
| 4276 |
else if (TYPE_REF_P (DECL_ARG_TYPE (arg))) |
| 4277 |
parm.by_ref = true; |
| 4278 |
|
| 4279 |
parm.frame_type = actual_type; |
| 4280 |
|
| 4281 |
parm.this_ptr = is_this_parameter (arg); |
| 4282 |
parm.lambda_cobj = lambda_p && DECL_NAME (arg) == closure_identifier; |
| 4283 |
|
| 4284 |
tree name = DECL_NAME (arg); |
| 4285 |
if (!name) |
| 4286 |
{ |
| 4287 |
char *buf = xasprintf ("_Coro_q%u___unnamed", parm_num); |
| 4288 |
name = get_identifier (buf); |
| 4289 |
free (buf); |
| 4290 |
} |
| 4291 |
parm.field_id = name; |
| 4292 |
if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (parm.frame_type)) |
| 4293 |
parm.trivial_dtor = false; |
| 4294 |
else |
| 4295 |
parm.trivial_dtor = true; |
| 4296 |
} |
| 4297 |
} |
| 4298 |
|
| 4299 |
/* Small helper for the repetitive task of adding a new field to the coro |
| 4300 |
frame type. */ |
| 4301 |
|
| 4302 |
static tree |
| 4303 |
coro_make_frame_entry (tree *field_list, const char *name, tree fld_type, |
| 4304 |
location_t loc) |
| 4305 |
{ |
| 4306 |
tree id = get_identifier (name); |
| 4307 |
tree decl = build_decl (loc, FIELD_DECL, id, fld_type); |
| 4308 |
DECL_CHAIN (decl) = *field_list; |
| 4309 |
*field_list = decl; |
| 4310 |
return id; |
| 4311 |
} |
| 4312 |
|
| 4313 |
/* A tree-walk callback that processes one bind expression noting local |
| 4314 |
variables, and making a coroutine frame slot available for those that |
| 4315 |
need it, so that they can be 'promoted' across suspension points. */ |
| 4316 |
|
| 4317 |
static tree |
| 4318 |
register_local_var_uses (tree *stmt, int *do_subtree, void *d) |
| 4319 |
{ |
| 4320 |
if (TREE_CODE (*stmt) != BIND_EXPR) |
| 4321 |
return NULL_TREE; |
| 4322 |
|
| 4323 |
local_vars_frame_data *lvd = (local_vars_frame_data *) d; |
| 4324 |
|
| 4325 |
/* As we enter a bind expression - record the vars there and then recurse. |
| 4326 |
As we exit drop the nest depth. |
| 4327 |
The bind index is a growing count of how many bind indices we've seen. |
| 4328 |
We build a space in the frame for each local var. */ |
| 4329 |
|
| 4330 |
tree lvar; |
| 4331 |
unsigned serial = 0; |
| 4332 |
for (lvar = BIND_EXPR_VARS (*stmt); lvar != NULL; lvar = DECL_CHAIN (lvar)) |
| 4333 |
{ |
| 4334 |
bool existed; |
| 4335 |
local_var_info &local_var |
| 4336 |
= lvd->local_var_uses->get_or_insert (lvar, &existed); |
| 4337 |
gcc_checking_assert (!existed); |
| 4338 |
local_var.def_loc = DECL_SOURCE_LOCATION (lvar); |
| 4339 |
tree lvtype = TREE_TYPE (lvar); |
| 4340 |
local_var.frame_type = lvtype; |
| 4341 |
local_var.field_idx = local_var.field_id = NULL_TREE; |
| 4342 |
|
| 4343 |
/* Make sure that we only present vars to the tests below. */ |
| 4344 |
if (TREE_CODE (lvar) != PARM_DECL |
| 4345 |
&& TREE_CODE (lvar) != VAR_DECL) |
| 4346 |
continue; |
| 4347 |
|
| 4348 |
/* We don't move static vars into the frame. */ |
| 4349 |
local_var.is_static = TREE_STATIC (lvar); |
| 4350 |
if (local_var.is_static) |
| 4351 |
continue; |
| 4352 |
|
| 4353 |
poly_uint64 size; |
| 4354 |
if (TREE_CODE (lvtype) == ARRAY_TYPE |
| 4355 |
&& !poly_int_tree_p (DECL_SIZE_UNIT (lvar), &size)) |
| 4356 |
{ |
| 4357 |
sorry_at (local_var.def_loc, "variable length arrays are not" |
| 4358 |
" yet supported in coroutines"); |
| 4359 |
/* Ignore it, this is broken anyway. */ |
| 4360 |
continue; |
| 4361 |
} |
| 4362 |
|
| 4363 |
lvd->local_var_seen = true; |
| 4364 |
/* If this var is a lambda capture proxy, we want to leave it alone, |
| 4365 |
and later rewrite the DECL_VALUE_EXPR to indirect through the |
| 4366 |
frame copy of the pointer to the lambda closure object. */ |
| 4367 |
local_var.is_lambda_capture = is_capture_proxy (lvar); |
| 4368 |
if (local_var.is_lambda_capture) |
| 4369 |
continue; |
| 4370 |
|
| 4371 |
/* If a variable has a value expression, then that's what needs |
| 4372 |
to be processed. */ |
| 4373 |
local_var.has_value_expr_p = DECL_HAS_VALUE_EXPR_P (lvar); |
| 4374 |
if (local_var.has_value_expr_p) |
| 4375 |
continue; |
| 4376 |
|
| 4377 |
/* Make names depth+index unique, so that we can support nested |
| 4378 |
scopes with identically named locals and still be able to |
| 4379 |
identify them in the coroutine frame. */ |
| 4380 |
tree lvname = DECL_NAME (lvar); |
| 4381 |
char *buf = NULL; |
| 4382 |
if (name_independent_decl_p (lvar)) |
| 4383 |
lvname = NULL_TREE; |
| 4384 |
|
| 4385 |
/* The outermost bind scope contains the artificial variables that |
| 4386 |
we inject to implement the coro state machine. We want to be able |
| 4387 |
to inspect these in debugging. */ |
| 4388 |
if (lvname != NULL_TREE && lvd->nest_depth == 0) |
| 4389 |
buf = xasprintf ("%s", IDENTIFIER_POINTER (lvname)); |
| 4390 |
else if (lvname != NULL_TREE) |
| 4391 |
buf = xasprintf ("%s_%u_%u", IDENTIFIER_POINTER (lvname), |
| 4392 |
lvd->nest_depth, lvd->bind_indx); |
| 4393 |
else |
| 4394 |
buf = xasprintf ("_D%u_%u_%u", lvd->nest_depth, lvd->bind_indx, |
| 4395 |
serial++); |
| 4396 |
|
| 4397 |
/* TODO: Figure out if we should build a local type that has any |
| 4398 |
excess alignment or size from the original decl. */ |
| 4399 |
local_var.field_id = coro_make_frame_entry (lvd->field_list, buf, |
| 4400 |
lvtype, lvd->loc); |
| 4401 |
free (buf); |
| 4402 |
/* We don't walk any of the local var sub-trees, they won't contain |
| 4403 |
any bind exprs. */ |
| 4404 |
} |
| 4405 |
lvd->bind_indx++; |
| 4406 |
lvd->nest_depth++; |
| 4407 |
/* Ensure we only visit each expression once. */ |
| 4408 |
cp_walk_tree_without_duplicates (&BIND_EXPR_BODY (*stmt), |
| 4409 |
register_local_var_uses, d); |
| 4410 |
*do_subtree = 0; /* We've done this. */ |
| 4411 |
lvd->nest_depth--; |
| 4412 |
return NULL_TREE; |
| 4413 |
} |
| 4414 |
|
| 4415 |
/* Build, return FUNCTION_DECL node based on ORIG with a type FN_TYPE which has |
| 4416 |
a single argument of type CORO_FRAME_PTR. Build the actor function if |
| 4417 |
ACTOR_P is true, otherwise the destroy. */ |
| 4418 |
|
| 4419 |
static tree |
| 4420 |
coro_build_actor_or_destroy_function (tree orig, tree fn_type, |
| 4421 |
tree coro_frame_ptr, bool actor_p) |
| 4422 |
{ |
| 4423 |
location_t loc = DECL_SOURCE_LOCATION (orig); |
| 4424 |
tree fn |
| 4425 |
= build_lang_decl (FUNCTION_DECL, copy_node (DECL_NAME (orig)), fn_type); |
| 4426 |
|
| 4427 |
/* Allow for locating the ramp (original) function from this one. */ |
| 4428 |
if (!to_ramp) |
| 4429 |
to_ramp = hash_map<tree, tree>::create_ggc (10); |
| 4430 |
to_ramp->put (fn, orig); |
| 4431 |
|
| 4432 |
DECL_CONTEXT (fn) = DECL_CONTEXT (orig); |
| 4433 |
DECL_SOURCE_LOCATION (fn) = loc; |
| 4434 |
DECL_ARTIFICIAL (fn) = true; |
| 4435 |
DECL_INITIAL (fn) = error_mark_node; |
| 4436 |
|
| 4437 |
tree id = get_identifier ("frame_ptr"); |
| 4438 |
tree fp = build_lang_decl (PARM_DECL, id, coro_frame_ptr); |
| 4439 |
DECL_ARTIFICIAL (fp) = true; |
| 4440 |
DECL_CONTEXT (fp) = fn; |
| 4441 |
DECL_ARG_TYPE (fp) = type_passed_as (coro_frame_ptr); |
| 4442 |
DECL_ARGUMENTS (fn) = fp; |
| 4443 |
|
| 4444 |
/* Copy selected attributes from the original function. */ |
| 4445 |
TREE_USED (fn) = TREE_USED (orig); |
| 4446 |
if (DECL_SECTION_NAME (orig)) |
| 4447 |
set_decl_section_name (fn, orig); |
| 4448 |
/* Copy any alignment that the FE added. */ |
| 4449 |
if (DECL_ALIGN (orig)) |
| 4450 |
SET_DECL_ALIGN (fn, DECL_ALIGN (orig)); |
| 4451 |
/* Copy any alignment the user added. */ |
| 4452 |
DECL_USER_ALIGN (fn) = DECL_USER_ALIGN (orig); |
| 4453 |
/* Apply attributes from the original fn. */ |
| 4454 |
DECL_ATTRIBUTES (fn) = copy_list (DECL_ATTRIBUTES (orig)); |
| 4455 |
/* but we do not want ones for contracts. */ |
| 4456 |
remove_contract_attributes (fn); |
| 4457 |
|
| 4458 |
/* A void return. */ |
| 4459 |
tree resdecl = build_decl (loc, RESULT_DECL, 0, void_type_node); |
| 4460 |
DECL_CONTEXT (resdecl) = fn; |
| 4461 |
DECL_ARTIFICIAL (resdecl) = 1; |
| 4462 |
DECL_IGNORED_P (resdecl) = 1; |
| 4463 |
DECL_RESULT (fn) = resdecl; |
| 4464 |
|
| 4465 |
/* Set up a means to find out if a decl is one of the helpers and, if so, |
| 4466 |
which one. */ |
| 4467 |
if (coroutine_info *info = get_coroutine_info (orig)) |
| 4468 |
{ |
| 4469 |
gcc_checking_assert ((actor_p && info->actor_decl == NULL_TREE) |
| 4470 |
|| info->destroy_decl == NULL_TREE); |
| 4471 |
if (actor_p) |
| 4472 |
info->actor_decl = fn; |
| 4473 |
else |
| 4474 |
info->destroy_decl = fn; |
| 4475 |
} |
| 4476 |
return fn; |
| 4477 |
} |
| 4478 |
|
| 4479 |
/* Re-write the body as per [dcl.fct.def.coroutine] / 5. */ |
| 4480 |
|
| 4481 |
void |
| 4482 |
cp_coroutine_transform::wrap_original_function_body () |
| 4483 |
{ |
| 4484 |
/* Avoid the code here attaching a location that makes the debugger jump. */ |
| 4485 |
iloc_sentinel stable_input_loc (fn_start); |
| 4486 |
location_t loc = fn_start; |
| 4487 |
|
| 4488 |
/* This will be our new outer scope. */ |
| 4489 |
tree update_body |
| 4490 |
= build3_loc (loc, BIND_EXPR, void_type_node, NULL, NULL, NULL); |
| 4491 |
tree top_block = make_node (BLOCK); |
| 4492 |
BIND_EXPR_BLOCK (update_body) = top_block; |
| 4493 |
BIND_EXPR_BODY (update_body) = push_stmt_list (); |
| 4494 |
|
| 4495 |
/* If the function has a top level bind expression, then connect that |
| 4496 |
after first making sure we give it a new block. */ |
| 4497 |
tree first = expr_first (coroutine_body); |
| 4498 |
if (first && TREE_CODE (first) == BIND_EXPR) |
| 4499 |
{ |
| 4500 |
tree block = BIND_EXPR_BLOCK (first); |
| 4501 |
gcc_checking_assert (block); |
| 4502 |
gcc_checking_assert (BLOCK_SUPERCONTEXT (block) == NULL_TREE); |
| 4503 |
gcc_checking_assert (BLOCK_CHAIN (block) == NULL_TREE); |
| 4504 |
/* Replace the top block to avoid issues with locations for args |
| 4505 |
appearing to be in a non-existent place. */ |
| 4506 |
tree replace_blk = make_node (BLOCK); |
| 4507 |
BLOCK_VARS (replace_blk) = BLOCK_VARS (block); |
| 4508 |
BLOCK_SUBBLOCKS (replace_blk) = BLOCK_SUBBLOCKS (block); |
| 4509 |
for (tree b = BLOCK_SUBBLOCKS (replace_blk); b; b = BLOCK_CHAIN (b)) |
| 4510 |
BLOCK_SUPERCONTEXT (b) = replace_blk; |
| 4511 |
BIND_EXPR_BLOCK (first) = replace_blk; |
| 4512 |
/* The top block has one child, so far, and we have now got a |
| 4513 |
superblock. */ |
| 4514 |
BLOCK_SUPERCONTEXT (replace_blk) = top_block; |
| 4515 |
BLOCK_SUBBLOCKS (top_block) = replace_blk; |
| 4516 |
} |
| 4517 |
else |
| 4518 |
{ |
| 4519 |
/* We are missing a top level BIND_EXPR. We need one to ensure that we |
| 4520 |
don't shuffle around the coroutine frame and corrupt it. */ |
| 4521 |
tree bind_wrap = build3_loc (loc, BIND_EXPR, void_type_node, |
| 4522 |
NULL, NULL, NULL); |
| 4523 |
BIND_EXPR_BODY (bind_wrap) = coroutine_body; |
| 4524 |
/* Ensure we have a block to connect up the scopes. */ |
| 4525 |
tree new_blk = make_node (BLOCK); |
| 4526 |
BIND_EXPR_BLOCK (bind_wrap) = new_blk; |
| 4527 |
BLOCK_SUBBLOCKS (top_block) = new_blk; |
| 4528 |
coroutine_body = bind_wrap; |
| 4529 |
} |
| 4530 |
|
| 4531 |
/* Wrap the function body in a try {} catch (...) {} block, if exceptions |
| 4532 |
are enabled. */ |
| 4533 |
tree var_list = NULL_TREE; |
| 4534 |
|
| 4535 |
/* [stmt.return.coroutine] / 3 |
| 4536 |
If p.return_void() is a valid expression, flowing off the end of a |
| 4537 |
coroutine is equivalent to a co_return with no operand; otherwise |
| 4538 |
flowing off the end of a coroutine results in undefined behavior. */ |
| 4539 |
tree return_void |
| 4540 |
= get_coroutine_return_void_expr (orig_fn_decl, loc, false); |
| 4541 |
|
| 4542 |
/* The pointer to the resume function. */ |
| 4543 |
tree resume_fn_ptr |
| 4544 |
= coro_build_artificial_var (loc, coro_resume_fn_id, |
| 4545 |
act_des_fn_ptr_type, orig_fn_decl, NULL_TREE); |
| 4546 |
DECL_CHAIN (resume_fn_ptr) = var_list; |
| 4547 |
var_list = resume_fn_ptr; |
| 4548 |
add_decl_expr (resume_fn_ptr); |
| 4549 |
|
| 4550 |
/* We will need to be able to set the resume function pointer to nullptr |
| 4551 |
to signal that the coroutine is 'done'. */ |
| 4552 |
tree zero_resume |
| 4553 |
= build1 (CONVERT_EXPR, act_des_fn_ptr_type, nullptr_node); |
| 4554 |
|
| 4555 |
/* The pointer to the destroy function. */ |
| 4556 |
tree var |
| 4557 |
= coro_build_artificial_var (loc, coro_destroy_fn_id, |
| 4558 |
act_des_fn_ptr_type, orig_fn_decl, NULL_TREE); |
| 4559 |
DECL_CHAIN (var) = var_list; |
| 4560 |
var_list = var; |
| 4561 |
add_decl_expr (var); |
| 4562 |
|
| 4563 |
/* The promise was created on demand when parsing we now link it into |
| 4564 |
our scope. */ |
| 4565 |
tree promise = get_coroutine_promise_proxy (orig_fn_decl); |
| 4566 |
DECL_CONTEXT (promise) = orig_fn_decl; |
| 4567 |
DECL_SOURCE_LOCATION (promise) = loc; |
| 4568 |
DECL_CHAIN (promise) = var_list; |
| 4569 |
var_list = promise; |
| 4570 |
add_decl_expr (promise); |
| 4571 |
|
| 4572 |
/* If we have function parms, then these will be copied to the coroutine |
| 4573 |
frame as per [dcl.fct.def.coroutine] / 13. |
| 4574 |
Here, we create a local (proxy) variable for each parm, since the original |
| 4575 |
parms will be out of scope once the ramp has finished. The proxy vars will |
| 4576 |
get DECL_VALUE_EXPRs pointing to the frame copies, so that we can interact |
| 4577 |
with them in the debugger. */ |
| 4578 |
if (DECL_ARGUMENTS (orig_fn_decl)) |
| 4579 |
{ |
| 4580 |
/* Add a local var for each parm. */ |
| 4581 |
for (tree arg = DECL_ARGUMENTS (orig_fn_decl); arg != NULL; |
| 4582 |
arg = DECL_CHAIN (arg)) |
| 4583 |
{ |
| 4584 |
param_info *parm_i = param_uses.get (arg); |
| 4585 |
gcc_checking_assert (parm_i); |
| 4586 |
parm_i->copy_var |
| 4587 |
= build_lang_decl (VAR_DECL, parm_i->field_id, TREE_TYPE (arg)); |
| 4588 |
DECL_SOURCE_LOCATION (parm_i->copy_var) = DECL_SOURCE_LOCATION (arg); |
| 4589 |
DECL_CONTEXT (parm_i->copy_var) = orig_fn_decl; |
| 4590 |
DECL_ARTIFICIAL (parm_i->copy_var) = true; |
| 4591 |
DECL_CHAIN (parm_i->copy_var) = var_list; |
| 4592 |
var_list = parm_i->copy_var; |
| 4593 |
add_decl_expr (parm_i->copy_var); |
| 4594 |
} |
| 4595 |
|
| 4596 |
/* Now replace all uses of the parms in the function body with the proxy |
| 4597 |
vars. We want to this to apply to every instance of param's use, so |
| 4598 |
don't include a 'visited' hash_set on the tree walk, however we will |
| 4599 |
arrange to visit each containing expression only once. */ |
| 4600 |
hash_set<tree *> visited; |
| 4601 |
param_frame_data param_data = {NULL, ¶m_uses, |
| 4602 |
&visited, loc, false}; |
| 4603 |
cp_walk_tree (&coroutine_body, rewrite_param_uses, ¶m_data, NULL); |
| 4604 |
} |
| 4605 |
|
| 4606 |
/* We create a resume index, this is initialized in the ramp. */ |
| 4607 |
resume_idx_var |
| 4608 |
= coro_build_artificial_var (loc, coro_resume_index_id, |
| 4609 |
short_unsigned_type_node, orig_fn_decl, |
| 4610 |
NULL_TREE); |
| 4611 |
DECL_CHAIN (resume_idx_var) = var_list; |
| 4612 |
var_list = resume_idx_var; |
| 4613 |
add_decl_expr (resume_idx_var); |
| 4614 |
|
| 4615 |
tree coro_frame_refcount |
| 4616 |
= coro_build_artificial_var (loc, coro_frame_refcount_id, |
| 4617 |
short_unsigned_type_node, orig_fn_decl, |
| 4618 |
NULL_TREE); |
| 4619 |
DECL_CHAIN (coro_frame_refcount) = var_list; |
| 4620 |
var_list = coro_frame_refcount; |
| 4621 |
add_decl_expr (coro_frame_refcount); |
| 4622 |
|
| 4623 |
/* If the coroutine has a frame that needs to be freed, this will be set by |
| 4624 |
the ramp. */ |
| 4625 |
var = coro_build_artificial_var (loc, coro_frame_needs_free_id, |
| 4626 |
boolean_type_node, orig_fn_decl, NULL_TREE); |
| 4627 |
DECL_CHAIN (var) = var_list; |
| 4628 |
var_list = var; |
| 4629 |
add_decl_expr (var); |
| 4630 |
|
| 4631 |
/* We consider that the body has a use of the frame once we start to process |
| 4632 |
the initial suspend expression. (the use might be relinquished if we |
| 4633 |
encounter an exception before the body is finished). */ |
| 4634 |
tree body_use |
| 4635 |
= build2_loc (loc, PLUS_EXPR, short_unsigned_type_node, coro_frame_refcount, |
| 4636 |
build_int_cst (short_unsigned_type_node, 1)); |
| 4637 |
body_use = cp_build_modify_expr (loc, coro_frame_refcount, NOP_EXPR, body_use, |
| 4638 |
tf_warning_or_error); |
| 4639 |
finish_expr_stmt (body_use); |
| 4640 |
if (flag_exceptions) |
| 4641 |
{ |
| 4642 |
/* Build promise.unhandled_exception(); */ |
| 4643 |
tree ueh |
| 4644 |
= coro_build_promise_expression (orig_fn_decl, promise, |
| 4645 |
coro_unhandled_exception_identifier, |
| 4646 |
loc, NULL, /*musthave=*/true); |
| 4647 |
/* Create and initialize the initial-await-resume-called variable per |
| 4648 |
[dcl.fct.def.coroutine] / 5.3. */ |
| 4649 |
tree i_a_r_c |
| 4650 |
= coro_build_artificial_var (loc, coro_frame_i_a_r_c_id, |
| 4651 |
boolean_type_node, orig_fn_decl, |
| 4652 |
NULL_TREE); |
| 4653 |
DECL_CHAIN (i_a_r_c) = var_list; |
| 4654 |
var_list = i_a_r_c; |
| 4655 |
add_decl_expr (i_a_r_c); |
| 4656 |
/* Start the try-catch. */ |
| 4657 |
tree tcb = build_stmt (loc, TRY_BLOCK, NULL_TREE, NULL_TREE); |
| 4658 |
add_stmt (tcb); |
| 4659 |
TRY_STMTS (tcb) = push_stmt_list (); |
| 4660 |
/* We need a new scope to handle the cleanup for the ramp use that is |
| 4661 |
needed for exceptions. */ |
| 4662 |
tree except_scope = begin_compound_stmt (0); |
| 4663 |
current_binding_level->artificial = 1; |
| 4664 |
tree release |
| 4665 |
= build2_loc (loc, MINUS_EXPR, short_unsigned_type_node, |
| 4666 |
coro_frame_refcount, build_int_cst (short_unsigned_type_node, 1)); |
| 4667 |
release = cp_build_modify_expr (loc, coro_frame_refcount, NOP_EXPR, |
| 4668 |
release, tf_warning_or_error); |
| 4669 |
/* Once we pass the initial await resume, the cleanup rules on exception |
| 4670 |
change so that the responsibility lies with the caller. */ |
| 4671 |
release = build3 (COND_EXPR, void_type_node, i_a_r_c, |
| 4672 |
build_empty_stmt (loc), release); |
| 4673 |
push_cleanup (NULL_TREE, release, /*ehonly*/true); |
| 4674 |
/* Add the initial await to the start of the user-authored function. */ |
| 4675 |
finish_expr_stmt (initial_await); |
| 4676 |
/* End the scope that handles the remove of frame-use on exception. */ |
| 4677 |
finish_compound_stmt (except_scope); |
| 4678 |
|
| 4679 |
/* Append the original function body. */ |
| 4680 |
add_stmt (coroutine_body); |
| 4681 |
|
| 4682 |
if (return_void) |
| 4683 |
add_stmt (return_void); |
| 4684 |
TRY_STMTS (tcb) = pop_stmt_list (TRY_STMTS (tcb)); |
| 4685 |
TRY_HANDLERS (tcb) = push_stmt_list (); |
| 4686 |
/* Mimic what the parser does for the catch. */ |
| 4687 |
tree handler = begin_handler (); |
| 4688 |
finish_handler_parms (NULL_TREE, handler); /* catch (...) */ |
| 4689 |
|
| 4690 |
/* Get the initial await resume called value. */ |
| 4691 |
tree not_iarc_if = begin_if_stmt (); |
| 4692 |
tree not_iarc = build1_loc (loc, TRUTH_NOT_EXPR, |
| 4693 |
boolean_type_node, i_a_r_c); |
| 4694 |
finish_if_stmt_cond (not_iarc, not_iarc_if); |
| 4695 |
/* If the initial await resume called value is false, rethrow... */ |
| 4696 |
tree rethrow = build_throw (loc, NULL_TREE, tf_warning_or_error); |
| 4697 |
suppress_warning (rethrow); |
| 4698 |
finish_expr_stmt (rethrow); |
| 4699 |
finish_then_clause (not_iarc_if); |
| 4700 |
finish_if_stmt (not_iarc_if); |
| 4701 |
/* ... else call the promise unhandled exception method |
| 4702 |
but first we set done = true and the resume index to 0. |
| 4703 |
If the unhandled exception method returns, then we continue |
| 4704 |
to the final await expression (which duplicates the clearing of |
| 4705 |
the field). */ |
| 4706 |
tree r = build2_loc (loc, MODIFY_EXPR, act_des_fn_ptr_type, resume_fn_ptr, |
| 4707 |
zero_resume); |
| 4708 |
finish_expr_stmt (r); |
| 4709 |
tree short_zero = build_int_cst (short_unsigned_type_node, 0); |
| 4710 |
r = build2 (MODIFY_EXPR, short_unsigned_type_node, resume_idx_var, |
| 4711 |
short_zero); |
| 4712 |
finish_expr_stmt (r); |
| 4713 |
finish_expr_stmt (ueh); |
| 4714 |
finish_handler (handler); |
| 4715 |
TRY_HANDLERS (tcb) = pop_stmt_list (TRY_HANDLERS (tcb)); |
| 4716 |
} |
| 4717 |
else |
| 4718 |
{ |
| 4719 |
if (pedantic) |
| 4720 |
{ |
| 4721 |
/* We still try to look for the promise method and warn if it's not |
| 4722 |
present. */ |
| 4723 |
tree ueh_meth |
| 4724 |
= lookup_promise_method (orig_fn_decl, |
| 4725 |
coro_unhandled_exception_identifier, |
| 4726 |
loc, /*musthave=*/false); |
| 4727 |
if (!ueh_meth || ueh_meth == error_mark_node) |
| 4728 |
warning_at (loc, 0, "no member named %qE in %qT", |
| 4729 |
coro_unhandled_exception_identifier, |
| 4730 |
get_coroutine_promise_type (orig_fn_decl)); |
| 4731 |
} |
| 4732 |
/* Else we don't check and don't care if the method is missing.. |
| 4733 |
just add the initial suspend, function and return. */ |
| 4734 |
finish_expr_stmt (initial_await); |
| 4735 |
/* Append the original function body. */ |
| 4736 |
add_stmt (coroutine_body); |
| 4737 |
if (return_void) |
| 4738 |
add_stmt (return_void); |
| 4739 |
} |
| 4740 |
|
| 4741 |
/* We are now doing actions associated with the end of the function, so |
| 4742 |
point to the closing brace. */ |
| 4743 |
input_location = loc = fn_end; |
| 4744 |
|
| 4745 |
/* co_return branches to the final_suspend label, so declare that now. */ |
| 4746 |
fs_label |
| 4747 |
= create_named_label_with_ctx (loc, "final.suspend", NULL_TREE); |
| 4748 |
add_stmt (build_stmt (loc, LABEL_EXPR, fs_label)); |
| 4749 |
|
| 4750 |
/* Before entering the final suspend point, we signal that this point has |
| 4751 |
been reached by setting the resume function pointer to zero (this is |
| 4752 |
what the 'done()' builtin tests) as per the current ABI. */ |
| 4753 |
zero_resume = build2_loc (loc, MODIFY_EXPR, act_des_fn_ptr_type, |
| 4754 |
resume_fn_ptr, zero_resume); |
| 4755 |
finish_expr_stmt (zero_resume); |
| 4756 |
finish_expr_stmt (final_await); |
| 4757 |
|
| 4758 |
BIND_EXPR_BODY (update_body) = pop_stmt_list (BIND_EXPR_BODY (update_body)); |
| 4759 |
BIND_EXPR_VARS (update_body) = nreverse (var_list); |
| 4760 |
BLOCK_VARS (top_block) = BIND_EXPR_VARS (update_body); |
| 4761 |
|
| 4762 |
coroutine_body = update_body; |
| 4763 |
} |
| 4764 |
|
| 4765 |
/* Extract the body of the function we are going to outline, leaving |
| 4766 |
to original function decl ready to build the ramp. */ |
| 4767 |
|
| 4768 |
static tree |
| 4769 |
split_coroutine_body_from_ramp (tree fndecl) |
| 4770 |
{ |
| 4771 |
/* Sanity-check and punt if we have a nonsense tree because of earlier |
| 4772 |
parse errors, perhaps. */ |
| 4773 |
if (!current_binding_level |
| 4774 |
|| current_binding_level->kind != sk_function_parms) |
| 4775 |
return NULL_TREE; |
| 4776 |
|
| 4777 |
/* Once we've tied off the original user-authored body in fn_body. |
| 4778 |
Start the replacement synthesized ramp body. */ |
| 4779 |
|
| 4780 |
tree body; |
| 4781 |
if (use_eh_spec_block (fndecl)) |
| 4782 |
{ |
| 4783 |
body = pop_stmt_list (TREE_OPERAND (current_eh_spec_block, 0)); |
| 4784 |
TREE_OPERAND (current_eh_spec_block, 0) = push_stmt_list (); |
| 4785 |
} |
| 4786 |
else |
| 4787 |
{ |
| 4788 |
body = pop_stmt_list (DECL_SAVED_TREE (fndecl)); |
| 4789 |
DECL_SAVED_TREE (fndecl) = push_stmt_list (); |
| 4790 |
} |
| 4791 |
|
| 4792 |
/* We can't validly get here with an empty statement list, since there's no |
| 4793 |
way for the FE to decide it's a coroutine in the absence of any code. */ |
| 4794 |
gcc_checking_assert (body != NULL_TREE); |
| 4795 |
|
| 4796 |
/* If we have an empty or erroneous function body, do not try to transform it |
| 4797 |
since that would potentially wrap errors. */ |
| 4798 |
tree body_start = expr_first (body); |
| 4799 |
if (body_start == NULL_TREE || body_start == error_mark_node) |
| 4800 |
{ |
| 4801 |
/* Restore the original state. */ |
| 4802 |
add_stmt (body); |
| 4803 |
return NULL_TREE; |
| 4804 |
} |
| 4805 |
return body; |
| 4806 |
} |
| 4807 |
|
| 4808 |
/* Build the expression to allocate the coroutine frame according to the |
| 4809 |
rules of [dcl.fct.def.coroutine] / 9. */ |
| 4810 |
|
| 4811 |
static tree |
| 4812 |
build_coroutine_frame_alloc_expr (tree promise_type, tree orig_fn_decl, |
| 4813 |
location_t fn_start, tree grooaf, |
| 4814 |
hash_map<tree, param_info> *param_uses, |
| 4815 |
tree frame_size) |
| 4816 |
{ |
| 4817 |
/* Allocate the frame, this has several possibilities: |
| 4818 |
[dcl.fct.def.coroutine] / 9 (part 1) |
| 4819 |
The allocation function’s name is looked up in the scope of the promise |
| 4820 |
type. It is not a failure for it to be absent see part 4, below. */ |
| 4821 |
|
| 4822 |
tree nwname = ovl_op_identifier (false, NEW_EXPR); |
| 4823 |
tree new_fn_call = NULL_TREE; |
| 4824 |
tree dummy_promise |
| 4825 |
= build_dummy_object (get_coroutine_promise_type (orig_fn_decl)); |
| 4826 |
|
| 4827 |
if (TYPE_HAS_NEW_OPERATOR (promise_type)) |
| 4828 |
{ |
| 4829 |
tree fns = lookup_promise_method (orig_fn_decl, nwname, fn_start, |
| 4830 |
/*musthave=*/true); |
| 4831 |
/* [dcl.fct.def.coroutine] / 9 (part 2) |
| 4832 |
If the lookup finds an allocation function in the scope of the promise |
| 4833 |
type, overload resolution is performed on a function call created by |
| 4834 |
assembling an argument list. The first argument is the amount of space |
| 4835 |
requested, and has type std::size_t. The lvalues p1...pn are the |
| 4836 |
succeeding arguments.. */ |
| 4837 |
vec<tree, va_gc> *args = make_tree_vector (); |
| 4838 |
vec_safe_push (args, frame_size); /* Space needed. */ |
| 4839 |
|
| 4840 |
for (tree arg = DECL_ARGUMENTS (orig_fn_decl); arg != NULL; |
| 4841 |
arg = DECL_CHAIN (arg)) |
| 4842 |
{ |
| 4843 |
param_info *parm_i = param_uses->get (arg); |
| 4844 |
gcc_checking_assert (parm_i); |
| 4845 |
if (parm_i->this_ptr || parm_i->lambda_cobj) |
| 4846 |
{ |
| 4847 |
/* We pass a reference to *this to the allocator lookup. */ |
| 4848 |
/* It's unsafe to use the cp_ version here since current_class_ref |
| 4849 |
might've gotten clobbered earlier during rewrite_param_uses. */ |
| 4850 |
tree this_ref = build_fold_indirect_ref (arg); |
| 4851 |
vec_safe_push (args, this_ref); |
| 4852 |
} |
| 4853 |
else |
| 4854 |
vec_safe_push (args, convert_from_reference (arg)); |
| 4855 |
} |
| 4856 |
|
| 4857 |
/* Note the function selected; we test to see if it's NOTHROW. */ |
| 4858 |
tree func; |
| 4859 |
/* Failure is not an error for this attempt. */ |
| 4860 |
new_fn_call = build_new_method_call (dummy_promise, fns, &args, NULL, |
| 4861 |
LOOKUP_NORMAL, &func, tf_none); |
| 4862 |
release_tree_vector (args); |
| 4863 |
|
| 4864 |
if (new_fn_call == error_mark_node) |
| 4865 |
{ |
| 4866 |
/* [dcl.fct.def.coroutine] / 9 (part 3) |
| 4867 |
If no viable function is found, overload resolution is performed |
| 4868 |
again on a function call created by passing just the amount of |
| 4869 |
space required as an argument of type std::size_t. */ |
| 4870 |
args = make_tree_vector_single (frame_size); /* Space needed. */ |
| 4871 |
new_fn_call = build_new_method_call (dummy_promise, fns, &args, |
| 4872 |
NULL_TREE, LOOKUP_NORMAL, &func, |
| 4873 |
tf_none); |
| 4874 |
release_tree_vector (args); |
| 4875 |
} |
| 4876 |
|
| 4877 |
/* However, if the promise provides an operator new, then one of these |
| 4878 |
two options must be available. */ |
| 4879 |
if (new_fn_call == error_mark_node) |
| 4880 |
{ |
| 4881 |
error_at (fn_start, "%qE is provided by %qT but is not usable with" |
| 4882 |
" the function signature %qD", nwname, promise_type, |
| 4883 |
orig_fn_decl); |
| 4884 |
return error_mark_node; |
| 4885 |
} |
| 4886 |
else if (grooaf && !TYPE_NOTHROW_P (TREE_TYPE (func))) |
| 4887 |
{ |
| 4888 |
error_at (fn_start, "%qE is provided by %qT but %qE is not marked" |
| 4889 |
" %<throw()%> or %<noexcept%>", grooaf, promise_type, nwname); |
| 4890 |
return error_mark_node; |
| 4891 |
} |
| 4892 |
else if (!grooaf && TYPE_NOTHROW_P (TREE_TYPE (func))) |
| 4893 |
warning_at (fn_start, 0, "%qE is marked %<throw()%> or %<noexcept%> but" |
| 4894 |
" no usable %<get_return_object_on_allocation_failure%>" |
| 4895 |
" is provided by %qT", nwname, promise_type); |
| 4896 |
} |
| 4897 |
else /* No operator new in the promise. */ |
| 4898 |
{ |
| 4899 |
/* [dcl.fct.def.coroutine] / 9 (part 4) |
| 4900 |
If this lookup fails, the allocation function’s name is looked up in |
| 4901 |
the global scope. */ |
| 4902 |
|
| 4903 |
vec<tree, va_gc> *args; |
| 4904 |
/* build_operator_new_call () will insert size needed as element 0 of |
| 4905 |
this, and we might need to append the std::nothrow constant. */ |
| 4906 |
vec_alloc (args, 2); |
| 4907 |
if (grooaf) |
| 4908 |
{ |
| 4909 |
/* [dcl.fct.def.coroutine] / 10 (part 2) |
| 4910 |
If any declarations (of the get return on allocation fail) are |
| 4911 |
found, then the result of a call to an allocation function used |
| 4912 |
to obtain storage for the coroutine state is assumed to return |
| 4913 |
nullptr if it fails to obtain storage and, if a global allocation |
| 4914 |
function is selected, the ::operator new(size_t, nothrow_t) form |
| 4915 |
is used. The allocation function used in this case shall have a |
| 4916 |
non-throwing noexcept-specification. So we need std::nothrow. */ |
| 4917 |
tree std_nt = lookup_qualified_name (std_node, |
| 4918 |
get_identifier ("nothrow"), |
| 4919 |
LOOK_want::NORMAL, |
| 4920 |
/*complain=*/true); |
| 4921 |
if (!std_nt || std_nt == error_mark_node) |
| 4922 |
{ |
| 4923 |
/* Something is seriously wrong, punt. */ |
| 4924 |
error_at (fn_start, "%qE is provided by %qT but %<std::nothrow%>" |
| 4925 |
" cannot be found", grooaf, promise_type); |
| 4926 |
return error_mark_node; |
| 4927 |
} |
| 4928 |
vec_safe_push (args, std_nt); |
| 4929 |
} |
| 4930 |
|
| 4931 |
/* If we get to this point, we must succeed in looking up the global |
| 4932 |
operator new for the params provided. Since we are not setting |
| 4933 |
size_check or cookie, we expect frame_size to be unaltered. */ |
| 4934 |
tree cookie = NULL; |
| 4935 |
new_fn_call = build_operator_new_call (nwname, &args, &frame_size, |
| 4936 |
&cookie, /*align_arg=*/NULL, |
| 4937 |
/*size_check=*/NULL, /*fn=*/NULL, |
| 4938 |
tf_warning_or_error); |
| 4939 |
release_tree_vector (args); |
| 4940 |
} |
| 4941 |
return new_fn_call; |
| 4942 |
} |
| 4943 |
|
| 4944 |
/* Build an expression to delete the coroutine state frame. */ |
| 4945 |
|
| 4946 |
static tree |
| 4947 |
build_coroutine_frame_delete_expr (tree coro_fp, tree frame_size, |
| 4948 |
tree promise_type, location_t loc) |
| 4949 |
{ |
| 4950 |
/* Cast the frame pointer to a pointer to promise so that the build op |
| 4951 |
delete call will search the promise. */ |
| 4952 |
tree pptr_type = build_pointer_type (promise_type); |
| 4953 |
tree frame_arg = build1_loc (loc, CONVERT_EXPR, pptr_type, coro_fp); |
| 4954 |
/* [dcl.fct.def.coroutine] / 12 sentence 3: |
| 4955 |
If both a usual deallocation function with only a pointer parameter and |
| 4956 |
a usual deallocation function with both a pointer parameter and a size |
| 4957 |
parameter are found, then the selected deallocation function shall be the |
| 4958 |
one with two parameters. */ |
| 4959 |
tree del_coro_fr |
| 4960 |
= build_coroutine_op_delete_call (DELETE_EXPR, frame_arg, frame_size, |
| 4961 |
/*global_p=*/false, /*placement=*/NULL, |
| 4962 |
/*alloc_fn=*/NULL, tf_warning_or_error); |
| 4963 |
if (!del_coro_fr || del_coro_fr == error_mark_node) |
| 4964 |
return error_mark_node; |
| 4965 |
return del_coro_fr; |
| 4966 |
} |
| 4967 |
|
| 4968 |
/* Build the ramp function. |
| 4969 |
Here we take the original function definition which has now had its body |
| 4970 |
removed, and use it as the declaration of the ramp which both replaces the |
| 4971 |
user's written function at call sites, and is responsible for starting |
| 4972 |
the coroutine it defined. |
| 4973 |
returns false on error. |
| 4974 |
|
| 4975 |
We should arrive here with the state of the compiler as if we had just |
| 4976 |
executed start_preparsed_function(). */ |
| 4977 |
|
| 4978 |
bool |
| 4979 |
cp_coroutine_transform::build_ramp_function () |
| 4980 |
{ |
| 4981 |
gcc_checking_assert (current_binding_level |
| 4982 |
&& current_binding_level->kind == sk_function_parms); |
| 4983 |
|
| 4984 |
/* This is completely synthetic code, if we find an issue then we have not |
| 4985 |
much chance to point at the most useful place in the user's code. In |
| 4986 |
lieu of this use the function start - so at least the diagnostic relates |
| 4987 |
to something that the user can inspect. */ |
| 4988 |
iloc_sentinel saved_position (fn_start); |
| 4989 |
location_t loc = fn_start; |
| 4990 |
|
| 4991 |
tree promise_type = get_coroutine_promise_type (orig_fn_decl); |
| 4992 |
tree fn_return_type = TREE_TYPE (TREE_TYPE (orig_fn_decl)); |
| 4993 |
bool void_ramp_p = VOID_TYPE_P (fn_return_type); |
| 4994 |
/* We know there was no return statement, that is intentional. */ |
| 4995 |
suppress_warning (orig_fn_decl, OPT_Wreturn_type); |
| 4996 |
|
| 4997 |
/* [dcl.fct.def.coroutine] / 10 (part1) |
| 4998 |
The unqualified-id get_return_object_on_allocation_failure is looked up |
| 4999 |
in the scope of the promise type by class member access lookup. */ |
| 5000 |
|
| 5001 |
/* We don't require this, but, if the lookup succeeds, then the function |
| 5002 |
must be usable, punt if it is not. */ |
| 5003 |
tree grooaf_meth |
| 5004 |
= lookup_promise_method (orig_fn_decl, |
| 5005 |
coro_gro_on_allocation_fail_identifier, loc, |
| 5006 |
/*musthave*/ false); |
| 5007 |
tree grooaf = NULL_TREE; |
| 5008 |
tree dummy_promise |
| 5009 |
= build_dummy_object (get_coroutine_promise_type (orig_fn_decl)); |
| 5010 |
if (grooaf_meth && grooaf_meth != error_mark_node) |
| 5011 |
{ |
| 5012 |
grooaf |
| 5013 |
= coro_build_promise_expression (orig_fn_decl, dummy_promise, |
| 5014 |
coro_gro_on_allocation_fail_identifier, |
| 5015 |
fn_start, NULL, /*musthave=*/false); |
| 5016 |
|
| 5017 |
/* That should succeed. */ |
| 5018 |
if (!grooaf || grooaf == error_mark_node) |
| 5019 |
{ |
| 5020 |
error_at (fn_start, "%qE is provided by %qT but is not usable with" |
| 5021 |
" the function %qD", coro_gro_on_allocation_fail_identifier, |
| 5022 |
promise_type, orig_fn_decl); |
| 5023 |
return false; |
| 5024 |
} |
| 5025 |
} |
| 5026 |
|
| 5027 |
/* Check early for usable allocator/deallocator, without which we cannot |
| 5028 |
build a useful ramp; early exit if they are not available or usable. */ |
| 5029 |
|
| 5030 |
frame_size = TYPE_SIZE_UNIT (frame_type); |
| 5031 |
|
| 5032 |
/* Make a var to represent the frame pointer early. */ |
| 5033 |
tree coro_fp = coro_build_artificial_var (loc, "_Coro_frameptr", |
| 5034 |
frame_ptr_type, orig_fn_decl, |
| 5035 |
NULL_TREE); |
| 5036 |
|
| 5037 |
tree new_fn_call |
| 5038 |
= build_coroutine_frame_alloc_expr (promise_type, orig_fn_decl, fn_start, |
| 5039 |
grooaf, ¶m_uses, frame_size); |
| 5040 |
|
| 5041 |
/* We must have a useable allocator to proceed. */ |
| 5042 |
if (!new_fn_call || new_fn_call == error_mark_node) |
| 5043 |
return false; |
| 5044 |
|
| 5045 |
/* Likewise, we need the DTOR to delete the frame. */ |
| 5046 |
tree delete_frame_call |
| 5047 |
= build_coroutine_frame_delete_expr (coro_fp, frame_size, promise_type, |
| 5048 |
fn_start); |
| 5049 |
if (!delete_frame_call || delete_frame_call == error_mark_node) |
| 5050 |
return false; |
| 5051 |
|
| 5052 |
/* At least verify we can lookup the get return object method. */ |
| 5053 |
tree get_ro_meth |
| 5054 |
= lookup_promise_method (orig_fn_decl, |
| 5055 |
coro_get_return_object_identifier, loc, |
| 5056 |
/*musthave*/ true); |
| 5057 |
if (!get_ro_meth || get_ro_meth == error_mark_node) |
| 5058 |
return false; |
| 5059 |
|
| 5060 |
/* So now construct the Ramp: */ |
| 5061 |
|
| 5062 |
tree ramp_fnbody = begin_compound_stmt (BCS_FN_BODY); |
| 5063 |
coro_fp = pushdecl (coro_fp); |
| 5064 |
add_decl_expr (coro_fp); |
| 5065 |
|
| 5066 |
/* Build the frame. */ |
| 5067 |
|
| 5068 |
/* The CO_FRAME internal function is a mechanism to allow the middle end |
| 5069 |
to adjust the allocation in response to optimizations. We provide the |
| 5070 |
current conservative estimate of the frame size (as per the current) |
| 5071 |
computed layout. */ |
| 5072 |
|
| 5073 |
tree resizeable |
| 5074 |
= build_call_expr_internal_loc (loc, IFN_CO_FRAME, size_type_node, 2, |
| 5075 |
frame_size, |
| 5076 |
build_zero_cst (frame_ptr_type)); |
| 5077 |
CALL_EXPR_ARG (new_fn_call, 0) = resizeable; |
| 5078 |
tree allocated = build1 (CONVERT_EXPR, frame_ptr_type, new_fn_call); |
| 5079 |
tree r = cp_build_init_expr (coro_fp, allocated); |
| 5080 |
finish_expr_stmt (r); |
| 5081 |
|
| 5082 |
/* If the user provided a method to return an object on alloc fail, then |
| 5083 |
check the returned pointer and call the func if it's null. |
| 5084 |
Otherwise, no check, and we fail for noexcept/fno-exceptions cases. */ |
| 5085 |
|
| 5086 |
tree grooaf_if_stmt = NULL_TREE; |
| 5087 |
tree alloc_ok_scope = NULL_TREE; |
| 5088 |
if (grooaf) |
| 5089 |
{ |
| 5090 |
/* [dcl.fct.def.coroutine] / 10 (part 3) |
| 5091 |
If the allocation function returns nullptr,the coroutine returns |
| 5092 |
control to the caller of the coroutine and the return value is |
| 5093 |
obtained by a call to T::get_return_object_on_allocation_failure(), |
| 5094 |
where T is the promise type. */ |
| 5095 |
tree cond = build1 (CONVERT_EXPR, frame_ptr_type, nullptr_node); |
| 5096 |
cond = build2 (NE_EXPR, boolean_type_node, coro_fp, cond); |
| 5097 |
grooaf_if_stmt = begin_if_stmt (); |
| 5098 |
finish_if_stmt_cond (cond, grooaf_if_stmt); |
| 5099 |
alloc_ok_scope = begin_compound_stmt (BCS_NORMAL); |
| 5100 |
} |
| 5101 |
|
| 5102 |
/* Dereference the frame pointer, to use in member access code. */ |
| 5103 |
tree deref_fp |
| 5104 |
= cp_build_indirect_ref (loc, coro_fp, RO_UNARY_STAR, tf_warning_or_error); |
| 5105 |
|
| 5106 |
/* For now, once allocation has succeeded we always assume that this needs |
| 5107 |
destruction, there's no impl. for frame allocation elision. */ |
| 5108 |
tree frame_needs_free |
| 5109 |
= coro_build_and_push_artificial_var_with_dve (loc, |
| 5110 |
coro_frame_needs_free_id, |
| 5111 |
boolean_type_node, |
| 5112 |
orig_fn_decl, |
| 5113 |
boolean_true_node, |
| 5114 |
deref_fp); |
| 5115 |
/* Although it appears to be unused here the frame entry is needed and we |
| 5116 |
just set it true. */ |
| 5117 |
TREE_USED (frame_needs_free) = true; |
| 5118 |
|
| 5119 |
tree coro_frame_refcount |
| 5120 |
= coro_build_and_push_artificial_var_with_dve (loc, coro_frame_refcount_id, |
| 5121 |
short_unsigned_type_node, |
| 5122 |
orig_fn_decl, NULL_TREE, |
| 5123 |
deref_fp); |
| 5124 |
/* Cleanup if both the ramp and the body have finished. */ |
| 5125 |
tree cond |
| 5126 |
= build2_loc (loc, EQ_EXPR, short_unsigned_type_node, coro_frame_refcount, |
| 5127 |
build_int_cst (short_unsigned_type_node, 0)); |
| 5128 |
r = build3 (COND_EXPR, void_type_node, cond, delete_frame_call, |
| 5129 |
build_empty_stmt (loc)); |
| 5130 |
push_cleanup (coro_fp, r, /*eh_only*/false); |
| 5131 |
|
| 5132 |
/* Put the resumer and destroyer functions in. */ |
| 5133 |
|
| 5134 |
tree actor_addr = build1 (ADDR_EXPR, act_des_fn_ptr_type, resumer); |
| 5135 |
coro_build_and_push_artificial_var_with_dve (loc, coro_resume_fn_id, |
| 5136 |
act_des_fn_ptr_type, |
| 5137 |
orig_fn_decl, |
| 5138 |
actor_addr, deref_fp); |
| 5139 |
|
| 5140 |
tree destroy_addr = build1 (ADDR_EXPR, act_des_fn_ptr_type, destroyer); |
| 5141 |
coro_build_and_push_artificial_var_with_dve (loc, coro_destroy_fn_id, |
| 5142 |
act_des_fn_ptr_type, |
| 5143 |
orig_fn_decl, |
| 5144 |
destroy_addr, deref_fp); |
| 5145 |
|
| 5146 |
/* [dcl.fct.def.coroutine] /13 |
| 5147 |
When a coroutine is invoked, a copy is created for each coroutine |
| 5148 |
parameter. Each such copy is an object with automatic storage duration |
| 5149 |
that is direct-initialized from an lvalue referring to the corresponding |
| 5150 |
parameter if the parameter is an lvalue reference, and from an xvalue |
| 5151 |
referring to it otherwise. A reference to a parameter in the function- |
| 5152 |
body of the coroutine and in the call to the coroutine promise |
| 5153 |
constructor is replaced by a reference to its copy. */ |
| 5154 |
|
| 5155 |
vec<tree, va_gc> *promise_args = NULL; /* So that we can adjust refs. */ |
| 5156 |
|
| 5157 |
/* The initialization and destruction of each parameter copy occurs in the |
| 5158 |
context of the called coroutine. Initializations of parameter copies are |
| 5159 |
sequenced before the call to the coroutine promise constructor and |
| 5160 |
indeterminately sequenced with respect to each other. The lifetime of |
| 5161 |
parameter copies ends immediately after the lifetime of the coroutine |
| 5162 |
promise object ends. */ |
| 5163 |
|
| 5164 |
if (DECL_ARGUMENTS (orig_fn_decl)) |
| 5165 |
{ |
| 5166 |
promise_args = make_tree_vector (); |
| 5167 |
for (tree arg = DECL_ARGUMENTS (orig_fn_decl); arg != NULL; |
| 5168 |
arg = DECL_CHAIN (arg)) |
| 5169 |
{ |
| 5170 |
bool existed; |
| 5171 |
param_info &parm = param_uses.get_or_insert (arg, &existed); |
| 5172 |
tree fld_idx |
| 5173 |
= coro_build_frame_access_expr (deref_fp, parm.field_id, |
| 5174 |
false, tf_warning_or_error); |
| 5175 |
|
| 5176 |
/* Add this to the promise CTOR arguments list, accounting for |
| 5177 |
refs and special handling for method this ptr. */ |
| 5178 |
if (parm.this_ptr || parm.lambda_cobj) |
| 5179 |
{ |
| 5180 |
/* We pass a reference to *this to the param preview. */ |
| 5181 |
/* It's unsafe to use the cp_ version here since current_class_ref |
| 5182 |
might've gotten clobbered earlier during rewrite_param_uses. */ |
| 5183 |
tree this_ref = build_fold_indirect_ref (arg); |
| 5184 |
vec_safe_push (promise_args, this_ref); |
| 5185 |
} |
| 5186 |
else if (parm.rv_ref) |
| 5187 |
vec_safe_push (promise_args, move (fld_idx)); |
| 5188 |
else |
| 5189 |
vec_safe_push (promise_args, fld_idx); |
| 5190 |
|
| 5191 |
if (parm.rv_ref || parm.pt_ref) |
| 5192 |
/* Initialise the frame reference field directly. */ |
| 5193 |
r = build2 (INIT_EXPR, TREE_TYPE (arg), |
| 5194 |
TREE_OPERAND (fld_idx, 0), arg); |
| 5195 |
else |
| 5196 |
{ |
| 5197 |
r = forward_parm (arg); |
| 5198 |
r = cp_build_modify_expr (loc, fld_idx, INIT_EXPR, r, |
| 5199 |
tf_warning_or_error); |
| 5200 |
} |
| 5201 |
finish_expr_stmt (r); |
| 5202 |
|
| 5203 |
/* Arrange for parm copies to be cleaned up when an exception is |
| 5204 |
thrown before initial await resume. */ |
| 5205 |
if (!parm.trivial_dtor) |
| 5206 |
{ |
| 5207 |
parm.fr_copy_dtor |
| 5208 |
= cxx_maybe_build_cleanup (fld_idx, tf_warning_or_error); |
| 5209 |
if (parm.fr_copy_dtor && parm.fr_copy_dtor != error_mark_node) |
| 5210 |
{ |
| 5211 |
param_dtor_list.safe_push (parm.field_id); |
| 5212 |
cond |
| 5213 |
= build2_loc (loc, EQ_EXPR, short_unsigned_type_node, |
| 5214 |
coro_frame_refcount, |
| 5215 |
build_int_cst (short_unsigned_type_node, 0)); |
| 5216 |
r = build3_loc (loc, COND_EXPR, void_type_node, cond, |
| 5217 |
parm.fr_copy_dtor, build_empty_stmt (loc)); |
| 5218 |
push_cleanup (fld_idx, r, /*eh_only*/false); |
| 5219 |
} |
| 5220 |
} |
| 5221 |
} |
| 5222 |
} |
| 5223 |
|
| 5224 |
/* Set up the promise. */ |
| 5225 |
tree p |
| 5226 |
= coro_build_and_push_artificial_var_with_dve (loc, coro_promise_id, |
| 5227 |
promise_type, orig_fn_decl, |
| 5228 |
NULL_TREE, deref_fp); |
| 5229 |
|
| 5230 |
if (type_build_ctor_call (promise_type)) |
| 5231 |
{ |
| 5232 |
/* Construct the promise object [dcl.fct.def.coroutine] / 5.7. |
| 5233 |
|
| 5234 |
First try to find a constructor with an argument list comprised of |
| 5235 |
the parameter copies. */ |
| 5236 |
|
| 5237 |
if (DECL_ARGUMENTS (orig_fn_decl)) |
| 5238 |
{ |
| 5239 |
r = build_special_member_call (p, complete_ctor_identifier, |
| 5240 |
&promise_args, promise_type, |
| 5241 |
LOOKUP_NORMAL, tf_none); |
| 5242 |
release_tree_vector (promise_args); |
| 5243 |
} |
| 5244 |
else |
| 5245 |
r = NULL_TREE; |
| 5246 |
|
| 5247 |
/* If that fails then the promise constructor argument list is empty. */ |
| 5248 |
if (r == NULL_TREE || r == error_mark_node) |
| 5249 |
r = build_special_member_call (p, complete_ctor_identifier, NULL, |
| 5250 |
promise_type, LOOKUP_NORMAL, |
| 5251 |
tf_warning_or_error); |
| 5252 |
|
| 5253 |
/* If type_build_ctor_call() encounters deprecated implicit CTORs it will |
| 5254 |
return true, and therefore we will execute this code path. However, |
| 5255 |
we might well not actually require a CTOR and under those conditions |
| 5256 |
the build call above will not return a call expression, but the |
| 5257 |
original instance object. Do not attempt to add the statement unless |
| 5258 |
it has side-effects. */ |
| 5259 |
if (r && r != error_mark_node && TREE_SIDE_EFFECTS (r)) |
| 5260 |
finish_expr_stmt (r); |
| 5261 |
} |
| 5262 |
|
| 5263 |
tree promise_dtor = cxx_maybe_build_cleanup (p, tf_warning_or_error); |
| 5264 |
/* If the promise is live, then run its dtor if that's available. */ |
| 5265 |
if (promise_dtor && promise_dtor != error_mark_node) |
| 5266 |
{ |
| 5267 |
cond = build2_loc (loc, EQ_EXPR, short_unsigned_type_node, |
| 5268 |
coro_frame_refcount, |
| 5269 |
build_int_cst (short_unsigned_type_node, 0)); |
| 5270 |
r = build3 (COND_EXPR, void_type_node, cond, promise_dtor, |
| 5271 |
build_empty_stmt (loc)); |
| 5272 |
push_cleanup (p, r, /*eh_only*/false); |
| 5273 |
} |
| 5274 |
|
| 5275 |
/* Now that we've constructed everything in the frame, consider it |
| 5276 |
used... */ |
| 5277 |
r = cp_build_modify_expr (loc, coro_frame_refcount, NOP_EXPR, |
| 5278 |
build_int_cst (short_unsigned_type_node, 1), |
| 5279 |
tf_warning_or_error); |
| 5280 |
finish_expr_stmt (r); |
| 5281 |
/* ... but when we finish we want to release that, and we want to do that |
| 5282 |
before the frame cleanups run. But after the gro cleanup, in case it |
| 5283 |
calls destroy (PR121961). */ |
| 5284 |
tree released |
| 5285 |
= build2_loc (loc, MINUS_EXPR, short_unsigned_type_node, |
| 5286 |
coro_frame_refcount, |
| 5287 |
build_int_cst (short_unsigned_type_node, 1)); |
| 5288 |
released = cp_build_modify_expr (loc, coro_frame_refcount, NOP_EXPR, released, |
| 5289 |
tf_warning_or_error); |
| 5290 |
push_cleanup (NULL_TREE, released, /*eh_only*/false); |
| 5291 |
|
| 5292 |
tree get_ro |
| 5293 |
= coro_build_promise_expression (orig_fn_decl, p, |
| 5294 |
coro_get_return_object_identifier, |
| 5295 |
fn_start, NULL, /*musthave=*/true); |
| 5296 |
|
| 5297 |
/* Without a return object we haven't got much clue what's going on. */ |
| 5298 |
if (!get_ro || get_ro == error_mark_node) |
| 5299 |
return false; |
| 5300 |
|
| 5301 |
/* Check for a bad get return object type. |
| 5302 |
[dcl.fct.def.coroutine] / 7 requires: |
| 5303 |
The expression promise.get_return_object() is used to initialize the |
| 5304 |
returned reference or prvalue result object ... |
| 5305 |
When we use a local to hold this, it is decltype(auto). */ |
| 5306 |
tree gro_type |
| 5307 |
= finish_decltype_type (get_ro, /*id_expression_or_member_access_p*/false, |
| 5308 |
tf_warning_or_error); |
| 5309 |
if (VOID_TYPE_P (gro_type) && !void_ramp_p) |
| 5310 |
{ |
| 5311 |
error_at (fn_start, "no viable conversion from %<void%> provided by" |
| 5312 |
" %<get_return_object%> to return type %qT", fn_return_type); |
| 5313 |
return false; |
| 5314 |
} |
| 5315 |
|
| 5316 |
/* Initialize the resume_idx_var to 0, meaning "not started". */ |
| 5317 |
coro_build_and_push_artificial_var_with_dve |
| 5318 |
(loc, coro_resume_index_id, short_unsigned_type_node, orig_fn_decl, |
| 5319 |
build_zero_cst (short_unsigned_type_node), deref_fp); |
| 5320 |
|
| 5321 |
/* We must manage the cleanups ourselves, with the exception of the g_r_o, |
| 5322 |
because the responsibility for them changes after the initial suspend. |
| 5323 |
However, any use of cxx_maybe_build_cleanup () in preceding code can |
| 5324 |
set the throwing_cleanup flag. */ |
| 5325 |
cp_function_chain->throwing_cleanup = false; |
| 5326 |
|
| 5327 |
/* [dcl.fct.def.coroutine] / 7 |
| 5328 |
The expression promise.get_return_object() is used to initialize the |
| 5329 |
glvalue result or prvalue result object of a call to a coroutine. */ |
| 5330 |
|
| 5331 |
tree coro_gro = NULL_TREE; |
| 5332 |
if (void_ramp_p) |
| 5333 |
/* We still want to call the method, even if the result is unused. */ |
| 5334 |
finish_expr_stmt (get_ro); |
| 5335 |
else |
| 5336 |
{ |
| 5337 |
/* Per CWG2563, we keep the result of promise.get_return_object () in |
| 5338 |
a temp which is then used to intialize the return object, including |
| 5339 |
NVRO. */ |
| 5340 |
|
| 5341 |
coro_gro |
| 5342 |
= coro_build_and_push_artificial_var (loc, "_Coro_gro", gro_type, |
| 5343 |
orig_fn_decl, NULL_TREE); |
| 5344 |
|
| 5345 |
r = cp_build_init_expr (coro_gro, STRIP_REFERENCE_REF (get_ro)); |
| 5346 |
finish_expr_stmt (r); |
| 5347 |
tree coro_gro_cleanup |
| 5348 |
= cxx_maybe_build_cleanup (coro_gro, tf_warning_or_error); |
| 5349 |
if (coro_gro_cleanup) |
| 5350 |
push_cleanup (coro_gro, coro_gro_cleanup, /*eh_only*/false); |
| 5351 |
} |
| 5352 |
|
| 5353 |
/* Start the coroutine body. */ |
| 5354 |
r = build_call_expr_loc (fn_start, resumer, 1, coro_fp); |
| 5355 |
finish_expr_stmt (r); |
| 5356 |
|
| 5357 |
/* The ramp is done, we just need the return statement, which we build from |
| 5358 |
the return object we constructed before we called the actor. */ |
| 5359 |
|
| 5360 |
/* This is our 'normal' exit. */ |
| 5361 |
r = void_ramp_p ? NULL_TREE : convert_from_reference (coro_gro); |
| 5362 |
finish_return_stmt (r); |
| 5363 |
|
| 5364 |
if (grooaf) |
| 5365 |
{ |
| 5366 |
finish_compound_stmt (alloc_ok_scope); |
| 5367 |
finish_then_clause (grooaf_if_stmt); |
| 5368 |
|
| 5369 |
begin_else_clause (grooaf_if_stmt); |
| 5370 |
/* We come here if the frame allocation failed. */ |
| 5371 |
r = NULL_TREE; |
| 5372 |
if (void_ramp_p) |
| 5373 |
/* Execute the get-return-object-on-alloc-fail call... */ |
| 5374 |
finish_expr_stmt (grooaf); |
| 5375 |
else |
| 5376 |
/* Get the fallback return object. */ |
| 5377 |
r = grooaf; |
| 5378 |
finish_return_stmt (r); |
| 5379 |
finish_if_stmt (grooaf_if_stmt); |
| 5380 |
} |
| 5381 |
|
| 5382 |
finish_compound_stmt (ramp_fnbody); |
| 5383 |
return true; |
| 5384 |
} |
| 5385 |
|
| 5386 |
/* ------- Encapsulate analysis of the couroutine -------- */ |
| 5387 |
|
| 5388 |
|
| 5389 |
cp_coroutine_transform::cp_coroutine_transform (tree _orig_fn, bool _inl) |
| 5390 |
: orig_fn_decl (_orig_fn), inline_p (_inl) |
| 5391 |
{ |
| 5392 |
/* We don't expect to be called with missing decl or e_m_n. */ |
| 5393 |
gcc_checking_assert (orig_fn_decl |
| 5394 |
&& TREE_CODE (orig_fn_decl) == FUNCTION_DECL); |
| 5395 |
if (!coro_function_valid_p (orig_fn_decl)) |
| 5396 |
{ |
| 5397 |
/* For early errors, we do not want a diagnostic about the missing |
| 5398 |
ramp return value, since the user cannot fix this - a 'return' is |
| 5399 |
not allowed in a coroutine. */ |
| 5400 |
suppress_warning (orig_fn_decl, OPT_Wreturn_type); |
| 5401 |
/* Discard the body, we can't process it further... */ |
| 5402 |
pop_stmt_list (DECL_SAVED_TREE (orig_fn_decl)); |
| 5403 |
/* ... and make an empty fn. */ |
| 5404 |
DECL_SAVED_TREE (orig_fn_decl) = push_stmt_list (); |
| 5405 |
/* Match the expected nesting when an eh block is in use. */ |
| 5406 |
if (use_eh_spec_block (orig_fn_decl)) |
| 5407 |
current_eh_spec_block = begin_eh_spec_block (); |
| 5408 |
valid_coroutine = false; |
| 5409 |
} |
| 5410 |
|
| 5411 |
/* We don't have the locus of the opening brace - it's filled in later (and |
| 5412 |
there doesn't really seem to be any easy way to get at it). */ |
| 5413 |
fn_start = DECL_SOURCE_LOCATION (orig_fn_decl); |
| 5414 |
/* The closing brace is assumed to be input_location. */ |
| 5415 |
fn_end = input_location; |
| 5416 |
|
| 5417 |
/* Build types we need. */ |
| 5418 |
tree fr_name = get_fn_local_identifier (orig_fn_decl, "Frame"); |
| 5419 |
frame_type = xref_tag (record_type, fr_name); |
| 5420 |
DECL_CONTEXT (TYPE_NAME (frame_type)) = DECL_CONTEXT (orig_fn_decl); |
| 5421 |
frame_ptr_type = build_pointer_type (frame_type); |
| 5422 |
act_des_fn_type |
| 5423 |
= build_function_type_list (void_type_node, frame_ptr_type, NULL_TREE); |
| 5424 |
act_des_fn_ptr_type = build_pointer_type (act_des_fn_type); |
| 5425 |
valid_coroutine = true; |
| 5426 |
} |
| 5427 |
|
| 5428 |
cp_coroutine_transform::~cp_coroutine_transform () |
| 5429 |
{ |
| 5430 |
} |
| 5431 |
|
| 5432 |
/* Here we: |
| 5433 |
a) Check that the function and promise type are valid for a |
| 5434 |
coroutine. |
| 5435 |
b) Carry out the initial morph to create the skeleton of the |
| 5436 |
coroutine ramp function and the rewritten body. |
| 5437 |
|
| 5438 |
Assumptions. |
| 5439 |
|
| 5440 |
1. We only hit this code once all dependencies are resolved. |
| 5441 |
2. The function body will be either a bind expr or a statement list |
| 5442 |
3. That cfun and current_function_decl are valid for the case we're |
| 5443 |
expanding. |
| 5444 |
4. 'input_location' will be of the final brace for the function. |
| 5445 |
|
| 5446 |
We do something like this: |
| 5447 |
declare a dummy coro frame. |
| 5448 |
struct _R_frame { |
| 5449 |
using handle_type = coro::coroutine_handle<coro1::promise_type>; |
| 5450 |
void (*_Coro_resume_fn)(_R_frame *); |
| 5451 |
void (*_Coro_destroy_fn)(_R_frame *); |
| 5452 |
coro1::promise_type _Coro_promise; |
| 5453 |
bool _Coro_frame_needs_free; free the coro frame mem if set. |
| 5454 |
bool _Coro_i_a_r_c; [dcl.fct.def.coroutine] / 5.3 |
| 5455 |
short _Coro_resume_index; |
| 5456 |
parameter copies (were required). |
| 5457 |
local variables saved (including awaitables) |
| 5458 |
(maybe) trailing space. |
| 5459 |
}; */ |
| 5460 |
|
| 5461 |
void |
| 5462 |
cp_coroutine_transform::apply_transforms () |
| 5463 |
{ |
| 5464 |
if (dmp_str == NULL) |
| 5465 |
dmp_str = dump_begin (coro_dump_id, &coro_dump_flags); |
| 5466 |
|
| 5467 |
coro_maybe_dump_initial_function (orig_fn_decl); |
| 5468 |
|
| 5469 |
coroutine_body |
| 5470 |
= split_coroutine_body_from_ramp (orig_fn_decl); |
| 5471 |
if (!coroutine_body) |
| 5472 |
{ |
| 5473 |
valid_coroutine = false; |
| 5474 |
return; |
| 5475 |
} |
| 5476 |
/* Keep the original function block tree to one side and reset. */ |
| 5477 |
body_blocks = current_binding_level->blocks; |
| 5478 |
current_binding_level->blocks = NULL_TREE; |
| 5479 |
|
| 5480 |
/* Collect information on the original function params and their use in the |
| 5481 |
function body. */ |
| 5482 |
analyze_fn_parms (); |
| 5483 |
|
| 5484 |
/* Declare the actor and destroyer functions, the following code needs to |
| 5485 |
see these. */ |
| 5486 |
resumer |
| 5487 |
= coro_build_actor_or_destroy_function (orig_fn_decl, act_des_fn_type, |
| 5488 |
frame_ptr_type, true); |
| 5489 |
destroyer |
| 5490 |
= coro_build_actor_or_destroy_function (orig_fn_decl, act_des_fn_type, |
| 5491 |
frame_ptr_type, false); |
| 5492 |
|
| 5493 |
/* Avoid repeating diagnostics about promise or awaiter fails. */ |
| 5494 |
if (!seen_error ()) |
| 5495 |
{ |
| 5496 |
iloc_sentinel stable_input_loc (fn_start); |
| 5497 |
initial_await = build_init_or_final_await (fn_start, false); |
| 5498 |
input_location = fn_end; |
| 5499 |
if (initial_await && initial_await != error_mark_node) |
| 5500 |
final_await = build_init_or_final_await (fn_end, true); |
| 5501 |
} |
| 5502 |
|
| 5503 |
/* Transform the function body as per [dcl.fct.def.coroutine] / 5. */ |
| 5504 |
wrap_original_function_body (); |
| 5505 |
|
| 5506 |
/* Analyze the body await expressions. */ |
| 5507 |
susp_frame_data body_aw_points (fs_label, &suspend_points); |
| 5508 |
cp_walk_tree (&coroutine_body, await_statement_walker, &body_aw_points, NULL); |
| 5509 |
await_count = body_aw_points.await_number; |
| 5510 |
|
| 5511 |
/* Determine the fields for the coroutine state. */ |
| 5512 |
tree field_list = NULL_TREE; |
| 5513 |
local_vars_frame_data local_vars_data (&field_list, &local_var_uses); |
| 5514 |
cp_walk_tree_without_duplicates (&coroutine_body, register_local_var_uses, |
| 5515 |
&local_vars_data); |
| 5516 |
|
| 5517 |
/* Conservative computation of the coroutine frame content. */ |
| 5518 |
frame_type = begin_class_definition (frame_type); |
| 5519 |
TYPE_FIELDS (frame_type) = field_list; |
| 5520 |
TYPE_BINFO (frame_type) = make_tree_binfo (0); |
| 5521 |
BINFO_OFFSET (TYPE_BINFO (frame_type)) = size_zero_node; |
| 5522 |
BINFO_TYPE (TYPE_BINFO (frame_type)) = frame_type; |
| 5523 |
frame_type = finish_struct (frame_type, NULL_TREE); |
| 5524 |
|
| 5525 |
valid_coroutine = build_ramp_function (); |
| 5526 |
coro_maybe_dump_ramp (orig_fn_decl); |
| 5527 |
} |
| 5528 |
|
| 5529 |
/* Having analysed and collected the necessary data we are now in a position |
| 5530 |
to build the outlined coroutine body and the destroyer shim. */ |
| 5531 |
|
| 5532 |
void |
| 5533 |
cp_coroutine_transform::finish_transforms () |
| 5534 |
{ |
| 5535 |
if (!valid_coroutine) |
| 5536 |
return; |
| 5537 |
|
| 5538 |
current_function_decl = resumer; |
| 5539 |
build_actor_fn (fn_start, frame_type, resumer, coroutine_body, orig_fn_decl, |
| 5540 |
&local_var_uses, &suspend_points, ¶m_dtor_list, |
| 5541 |
resume_idx_var, await_count, frame_size, inline_p); |
| 5542 |
|
| 5543 |
current_function_decl = destroyer; |
| 5544 |
build_destroy_fn (fn_start, frame_type, destroyer, resumer, inline_p); |
| 5545 |
|
| 5546 |
coro_maybe_dump_transformed_functions (resumer, destroyer); |
| 5547 |
} |
| 5548 |
|
| 5549 |
#include "gt-cp-coroutines.h" |
| 5550 |
|