mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
coroutines.cc
5196 lines (4551 loc) · 178 KB
/
coroutines.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* coroutine-specific state, expansions and tests.
Copyright (C) 2018-2024 Free Software Foundation, Inc.
Contributed by Iain Sandoe <iain@sandoe.co.uk> under contract to Facebook.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "cp-tree.h"
#include "stringpool.h"
#include "stmt.h"
#include "stor-layout.h"
#include "tree-iterator.h"
#include "tree.h"
#include "gcc-rich-location.h"
#include "hash-map.h"
static bool coro_promise_type_found_p (tree, location_t);
/* GCC C++ coroutines implementation.
The user authors a function that becomes a coroutine (lazily) by
making use of any of the co_await, co_yield or co_return keywords.
Unlike a regular function, where the activation record is placed on the
stack, and is destroyed on function exit, a coroutine has some state that
persists between calls - the coroutine frame (analogous to a stack frame).
We transform the user's function into three pieces:
1. A so-called ramp function, that establishes the coroutine frame and
begins execution of the coroutine.
2. An actor function that contains the state machine corresponding to the
user's suspend/resume structure.
3. A stub function that calls the actor function in 'destroy' mode.
The actor function is executed:
* from "resume point 0" by the ramp.
* from resume point N ( > 0 ) for handle.resume() calls.
* from the destroy stub for destroy point N for handle.destroy() calls.
The functions in this file carry out the necessary analysis of, and
transforms to, the AST to perform this.
The C++ coroutine design makes use of some helper functions that are
authored in a so-called "promise" class provided by the user.
At parse time (or post substitution) the type of the coroutine promise
will be determined. At that point, we can look up the required promise
class methods and issue diagnostics if they are missing or incorrect. To
avoid repeating these actions at code-gen time, we make use of temporary
'proxy' variables for the coroutine handle and the promise - which will
eventually be instantiated in the coroutine frame.
Each of the keywords will expand to a code sequence (although co_yield is
just syntactic sugar for a co_await).
We defer the analysis and transformation until template expansion is
complete so that we have complete types at that time. */
/* The state that we collect during parsing (and template expansion) for
a coroutine. */
struct GTY((for_user)) coroutine_info
{
tree function_decl; /* The original function decl. */
tree actor_decl; /* The synthesized actor function. */
tree destroy_decl; /* The synthesized destroy function. */
tree promise_type; /* The cached promise type for this function. */
tree handle_type; /* The cached coroutine handle for this function. */
tree self_h_proxy; /* A handle instance that is used as the proxy for the
one that will eventually be allocated in the coroutine
frame. */
tree promise_proxy; /* Likewise, a proxy promise instance. */
tree return_void; /* The expression for p.return_void() if it exists. */
location_t first_coro_keyword; /* The location of the keyword that made this
function into a coroutine. */
/* Flags to avoid repeated errors for per-function issues. */
bool coro_ret_type_error_emitted;
bool coro_promise_error_emitted;
bool coro_co_return_error_emitted;
};
struct coroutine_info_hasher : ggc_ptr_hash<coroutine_info>
{
typedef tree compare_type; /* We only compare the function decl. */
static inline hashval_t hash (coroutine_info *);
static inline hashval_t hash (const compare_type &);
static inline bool equal (coroutine_info *, coroutine_info *);
static inline bool equal (coroutine_info *, const compare_type &);
};
/* This table holds all the collected coroutine state for coroutines in
the current translation unit. */
static GTY (()) hash_table<coroutine_info_hasher> *coroutine_info_table;
/* We will initialize state lazily. */
static bool coro_initialized = false;
/* Return a hash value for the entry pointed to by INFO.
The compare type is a tree, but the only trees we are going use are
function decls. We use the DECL_UID as the hash value since that is
stable across PCH. */
hashval_t
coroutine_info_hasher::hash (coroutine_info *info)
{
return DECL_UID (info->function_decl);
}
/* Return a hash value for the compare value COMP. */
hashval_t
coroutine_info_hasher::hash (const compare_type& comp)
{
return DECL_UID (comp);
}
/* Return true if the entries pointed to by LHS and RHS are for the
same coroutine. */
bool
coroutine_info_hasher::equal (coroutine_info *lhs, coroutine_info *rhs)
{
return lhs->function_decl == rhs->function_decl;
}
bool
coroutine_info_hasher::equal (coroutine_info *lhs, const compare_type& rhs)
{
return lhs->function_decl == rhs;
}
/* Get the existing coroutine_info for FN_DECL, or insert a new one if the
entry does not yet exist. */
coroutine_info *
get_or_insert_coroutine_info (tree fn_decl)
{
gcc_checking_assert (coroutine_info_table != NULL);
coroutine_info **slot = coroutine_info_table->find_slot_with_hash
(fn_decl, coroutine_info_hasher::hash (fn_decl), INSERT);
if (*slot == NULL)
{
*slot = new (ggc_cleared_alloc<coroutine_info> ()) coroutine_info ();
(*slot)->function_decl = fn_decl;
}
return *slot;
}
/* Get the existing coroutine_info for FN_DECL, fail if it doesn't exist. */
coroutine_info *
get_coroutine_info (tree fn_decl)
{
if (coroutine_info_table == NULL)
return NULL;
coroutine_info **slot = coroutine_info_table->find_slot_with_hash
(fn_decl, coroutine_info_hasher::hash (fn_decl), NO_INSERT);
if (slot)
return *slot;
return NULL;
}
/* We will lazily create all the identifiers that are used by coroutines
on the first attempt to lookup the traits. */
/* Identifiers that are used by all coroutines. */
static GTY(()) tree coro_traits_identifier;
static GTY(()) tree coro_handle_identifier;
static GTY(()) tree coro_promise_type_identifier;
/* Required promise method name identifiers. */
static GTY(()) tree coro_await_transform_identifier;
static GTY(()) tree coro_initial_suspend_identifier;
static GTY(()) tree coro_final_suspend_identifier;
static GTY(()) tree coro_return_void_identifier;
static GTY(()) tree coro_return_value_identifier;
static GTY(()) tree coro_yield_value_identifier;
static GTY(()) tree coro_resume_identifier;
static GTY(()) tree coro_address_identifier;
static GTY(()) tree coro_from_address_identifier;
static GTY(()) tree coro_get_return_object_identifier;
static GTY(()) tree coro_gro_on_allocation_fail_identifier;
static GTY(()) tree coro_unhandled_exception_identifier;
/* Awaitable methods. */
static GTY(()) tree coro_await_ready_identifier;
static GTY(()) tree coro_await_suspend_identifier;
static GTY(()) tree coro_await_resume_identifier;
/* Accessors for the coroutine frame state used by the implementation. */
static GTY(()) tree coro_resume_fn_id;
static GTY(()) tree coro_destroy_fn_id;
static GTY(()) tree coro_promise_id;
static GTY(()) tree coro_frame_needs_free_id;
static GTY(()) tree coro_resume_index_id;
static GTY(()) tree coro_self_handle_id;
static GTY(()) tree coro_actor_continue_id;
static GTY(()) tree coro_frame_i_a_r_c_id;
/* Create the identifiers used by the coroutines library interfaces and
the implementation frame state. */
static void
coro_init_identifiers ()
{
coro_traits_identifier = get_identifier ("coroutine_traits");
coro_handle_identifier = get_identifier ("coroutine_handle");
coro_promise_type_identifier = get_identifier ("promise_type");
coro_await_transform_identifier = get_identifier ("await_transform");
coro_initial_suspend_identifier = get_identifier ("initial_suspend");
coro_final_suspend_identifier = get_identifier ("final_suspend");
coro_return_void_identifier = get_identifier ("return_void");
coro_return_value_identifier = get_identifier ("return_value");
coro_yield_value_identifier = get_identifier ("yield_value");
coro_resume_identifier = get_identifier ("resume");
coro_address_identifier = get_identifier ("address");
coro_from_address_identifier = get_identifier ("from_address");
coro_get_return_object_identifier = get_identifier ("get_return_object");
coro_gro_on_allocation_fail_identifier =
get_identifier ("get_return_object_on_allocation_failure");
coro_unhandled_exception_identifier = get_identifier ("unhandled_exception");
coro_await_ready_identifier = get_identifier ("await_ready");
coro_await_suspend_identifier = get_identifier ("await_suspend");
coro_await_resume_identifier = get_identifier ("await_resume");
/* Coroutine state frame field accessors. */
coro_resume_fn_id = get_identifier ("_Coro_resume_fn");
coro_destroy_fn_id = get_identifier ("_Coro_destroy_fn");
coro_promise_id = get_identifier ("_Coro_promise");
coro_frame_needs_free_id = get_identifier ("_Coro_frame_needs_free");
coro_frame_i_a_r_c_id = get_identifier ("_Coro_initial_await_resume_called");
coro_resume_index_id = get_identifier ("_Coro_resume_index");
coro_self_handle_id = get_identifier ("_Coro_self_handle");
coro_actor_continue_id = get_identifier ("_Coro_actor_continue");
}
/* Trees we only need to set up once. */
static GTY(()) tree coro_traits_templ;
static GTY(()) tree coro_handle_templ;
static GTY(()) tree void_coro_handle_type;
/* ================= Parse, Semantics and Type checking ================= */
/* This initial set of routines are helper for the parsing and template
expansion phases.
At the completion of this, we will have completed trees for each of the
keywords, but making use of proxy variables for the self-handle and the
promise class instance. */
/* [coroutine.traits]
Lookup the coroutine_traits template decl. */
static tree
find_coro_traits_template_decl (location_t kw)
{
/* If we are missing fundamental information, such as the traits, (or the
declaration found is not a type template), then don't emit an error for
every keyword in a TU, just do it once. */
static bool traits_error_emitted = false;
tree traits_decl = lookup_qualified_name (std_node, coro_traits_identifier,
LOOK_want::NORMAL,
/*complain=*/!traits_error_emitted);
if (traits_decl == error_mark_node
|| !DECL_TYPE_TEMPLATE_P (traits_decl))
{
if (!traits_error_emitted)
{
gcc_rich_location richloc (kw);
error_at (&richloc, "coroutines require a traits template; cannot"
" find %<%E::%E%>", std_node, coro_traits_identifier);
inform (&richloc, "perhaps %<#include <coroutine>%> is missing");
traits_error_emitted = true;
}
return NULL_TREE;
}
else
return traits_decl;
}
/* Instantiate Coroutine traits for the function signature. */
static tree
instantiate_coro_traits (tree fndecl, location_t kw)
{
/* [coroutine.traits.primary]
So now build up a type list for the template <typename _R, typename...>.
The types are the function's arg types and _R is the function return
type. */
tree functyp = TREE_TYPE (fndecl);
tree arg = DECL_ARGUMENTS (fndecl);
tree arg_node = TYPE_ARG_TYPES (functyp);
tree argtypes = make_tree_vec (list_length (arg_node)-1);
unsigned p = 0;
while (arg_node != NULL_TREE && !VOID_TYPE_P (TREE_VALUE (arg_node)))
{
if (is_this_parameter (arg)
|| DECL_NAME (arg) == closure_identifier)
{
/* We pass a reference to *this to the param preview. */
tree ct = TREE_TYPE (TREE_TYPE (arg));
TREE_VEC_ELT (argtypes, p++) = cp_build_reference_type (ct, false);
}
else
TREE_VEC_ELT (argtypes, p++) = TREE_VALUE (arg_node);
arg_node = TREE_CHAIN (arg_node);
arg = DECL_CHAIN (arg);
}
tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
ARGUMENT_PACK_ARGS (argtypepack) = argtypes;
tree targ = make_tree_vec (2);
TREE_VEC_ELT (targ, 0) = TREE_TYPE (functyp);
TREE_VEC_ELT (targ, 1) = argtypepack;
tree traits_class
= lookup_template_class (coro_traits_templ, targ,
/*in_decl=*/NULL_TREE, /*context=*/NULL_TREE,
tf_warning_or_error);
if (traits_class == error_mark_node)
{
error_at (kw, "cannot instantiate %<coroutine traits%>");
return NULL_TREE;
}
return traits_class;
}
/* [coroutine.handle] */
static tree
find_coro_handle_template_decl (location_t kw)
{
/* As for the coroutine traits, this error is per TU, so only emit
it once. */
static bool coro_handle_error_emitted = false;
tree handle_decl = lookup_qualified_name (std_node, coro_handle_identifier,
LOOK_want::NORMAL,
!coro_handle_error_emitted);
if (handle_decl == error_mark_node
|| !DECL_CLASS_TEMPLATE_P (handle_decl))
{
if (!coro_handle_error_emitted)
error_at (kw, "coroutines require a handle class template;"
" cannot find %<%E::%E%>", std_node, coro_handle_identifier);
coro_handle_error_emitted = true;
return NULL_TREE;
}
else
return handle_decl;
}
/* Instantiate the handle template for a given promise type. */
static tree
instantiate_coro_handle_for_promise_type (location_t kw, tree promise_type)
{
/* So now build up a type list for the template, one entry, the promise. */
tree targ = make_tree_vec (1);
TREE_VEC_ELT (targ, 0) = promise_type;
tree handle_type
= lookup_template_class (coro_handle_identifier, targ,
/* in_decl=*/NULL_TREE,
/* context=*/std_node,
tf_warning_or_error);
if (handle_type == error_mark_node)
{
error_at (kw, "cannot instantiate a %<coroutine handle%> for"
" promise type %qT", promise_type);
return NULL_TREE;
}
return handle_type;
}
/* Look for the promise_type in the instantiated traits. */
static tree
find_promise_type (tree traits_class)
{
tree promise_type
= lookup_member (traits_class, coro_promise_type_identifier,
/* protect=*/1, /*want_type=*/true, tf_warning_or_error);
if (promise_type)
promise_type
= complete_type_or_else (TREE_TYPE (promise_type), promise_type);
/* NULL_TREE on fail. */
return promise_type;
}
static bool
coro_promise_type_found_p (tree fndecl, location_t loc)
{
gcc_assert (fndecl != NULL_TREE);
if (!coro_initialized)
{
/* Trees we only need to create once.
Set up the identifiers we will use. */
coro_init_identifiers ();
/* Coroutine traits template. */
coro_traits_templ = find_coro_traits_template_decl (loc);
if (coro_traits_templ == NULL_TREE)
return false;
/* coroutine_handle<> template. */
coro_handle_templ = find_coro_handle_template_decl (loc);
if (coro_handle_templ == NULL_TREE)
return false;
/* We can also instantiate the void coroutine_handle<> */
void_coro_handle_type =
instantiate_coro_handle_for_promise_type (loc, NULL_TREE);
if (void_coro_handle_type == NULL_TREE)
return false;
/* A table to hold the state, per coroutine decl. */
gcc_checking_assert (coroutine_info_table == NULL);
coroutine_info_table =
hash_table<coroutine_info_hasher>::create_ggc (11);
if (coroutine_info_table == NULL)
return false;
coro_initialized = true;
}
/* Save the coroutine data on the side to avoid the overhead on every
function decl tree. */
coroutine_info *coro_info = get_or_insert_coroutine_info (fndecl);
/* Without this, we cannot really proceed. */
gcc_checking_assert (coro_info);
/* If we don't already have a current promise type, try to look it up. */
if (coro_info->promise_type == NULL_TREE)
{
/* Get the coroutine traits template class instance for the function
signature we have - coroutine_traits <R, ...> */
tree templ_class = instantiate_coro_traits (fndecl, loc);
/* Find the promise type for that. */
coro_info->promise_type = find_promise_type (templ_class);
/* If we don't find it, punt on the rest. */
if (coro_info->promise_type == NULL_TREE)
{
if (!coro_info->coro_promise_error_emitted)
error_at (loc, "unable to find the promise type for"
" this coroutine");
coro_info->coro_promise_error_emitted = true;
return false;
}
/* Test for errors in the promise type that can be determined now. */
tree has_ret_void = lookup_member (coro_info->promise_type,
coro_return_void_identifier,
/*protect=*/1, /*want_type=*/0,
tf_none);
tree has_ret_val = lookup_member (coro_info->promise_type,
coro_return_value_identifier,
/*protect=*/1, /*want_type=*/0,
tf_none);
if (has_ret_void && has_ret_val)
{
location_t ploc = DECL_SOURCE_LOCATION (fndecl);
if (!coro_info->coro_co_return_error_emitted)
error_at (ploc, "the coroutine promise type %qT declares both"
" %<return_value%> and %<return_void%>",
coro_info->promise_type);
inform (DECL_SOURCE_LOCATION (BASELINK_FUNCTIONS (has_ret_void)),
"%<return_void%> declared here");
has_ret_val = BASELINK_FUNCTIONS (has_ret_val);
const char *message = "%<return_value%> declared here";
if (TREE_CODE (has_ret_val) == OVERLOAD)
{
has_ret_val = OVL_FIRST (has_ret_val);
message = "%<return_value%> first declared here";
}
inform (DECL_SOURCE_LOCATION (has_ret_val), message);
coro_info->coro_co_return_error_emitted = true;
return false;
}
/* Try to find the handle type for the promise. */
tree handle_type =
instantiate_coro_handle_for_promise_type (loc, coro_info->promise_type);
if (handle_type == NULL_TREE)
return false;
/* Complete this, we're going to use it. */
coro_info->handle_type = complete_type_or_else (handle_type, fndecl);
/* Diagnostic would be emitted by complete_type_or_else. */
if (!coro_info->handle_type)
return false;
/* Build a proxy for a handle to "self" as the param to
await_suspend() calls. */
coro_info->self_h_proxy
= build_lang_decl (VAR_DECL, coro_self_handle_id,
coro_info->handle_type);
/* Build a proxy for the promise so that we can perform lookups. */
coro_info->promise_proxy
= build_lang_decl (VAR_DECL, coro_promise_id,
coro_info->promise_type);
/* Note where we first saw a coroutine keyword. */
coro_info->first_coro_keyword = loc;
}
return true;
}
/* Map from actor or destroyer to ramp. */
static GTY(()) hash_map<tree, tree> *to_ramp;
/* Given a tree that is an actor or destroy, find the ramp function. */
tree
coro_get_ramp_function (tree decl)
{
if (!to_ramp)
return NULL_TREE;
tree *p = to_ramp->get (decl);
if (p)
return *p;
return NULL_TREE;
}
/* Given the DECL for a ramp function (the user's original declaration) return
the actor function if it has been defined. */
tree
coro_get_actor_function (tree decl)
{
if (coroutine_info *info = get_coroutine_info (decl))
return info->actor_decl;
return NULL_TREE;
}
/* Given the DECL for a ramp function (the user's original declaration) return
the destroy function if it has been defined. */
tree
coro_get_destroy_function (tree decl)
{
if (coroutine_info *info = get_coroutine_info (decl))
return info->destroy_decl;
return NULL_TREE;
}
/* These functions assumes that the caller has verified that the state for
the decl has been initialized, we try to minimize work here. */
static tree
get_coroutine_promise_type (tree decl)
{
if (coroutine_info *info = get_coroutine_info (decl))
return info->promise_type;
return NULL_TREE;
}
static tree
get_coroutine_handle_type (tree decl)
{
if (coroutine_info *info = get_coroutine_info (decl))
return info->handle_type;
return NULL_TREE;
}
static tree
get_coroutine_self_handle_proxy (tree decl)
{
if (coroutine_info *info = get_coroutine_info (decl))
return info->self_h_proxy;
return NULL_TREE;
}
static tree
get_coroutine_promise_proxy (tree decl)
{
if (coroutine_info *info = get_coroutine_info (decl))
return info->promise_proxy;
return NULL_TREE;
}
static tree
lookup_promise_method (tree fndecl, tree member_id, location_t loc,
bool musthave)
{
tree promise = get_coroutine_promise_type (fndecl);
tree pm_memb
= lookup_member (promise, member_id,
/*protect=*/1, /*want_type=*/0, tf_warning_or_error);
if (musthave && pm_memb == NULL_TREE)
{
error_at (loc, "no member named %qE in %qT", member_id, promise);
return error_mark_node;
}
return pm_memb;
}
/* Build an expression of the form p.method (args) where the p is a promise
object for the current coroutine.
OBJECT is the promise object instance to use, it may be NULL, in which case
we will use the promise_proxy instance for this coroutine.
ARGS may be NULL, for empty parm lists. */
static tree
coro_build_promise_expression (tree fn, tree promise_obj, tree member_id,
location_t loc, vec<tree, va_gc> **args,
bool musthave)
{
tree meth = lookup_promise_method (fn, member_id, loc, musthave);
if (meth == error_mark_node)
return error_mark_node;
/* If we don't find it, and it isn't needed, an empty return is OK. */
if (!meth)
return NULL_TREE;
tree promise
= promise_obj ? promise_obj
: get_coroutine_promise_proxy (current_function_decl);
tree expr;
if (BASELINK_P (meth))
expr = build_new_method_call (promise, meth, args, NULL_TREE,
LOOKUP_NORMAL, NULL, tf_warning_or_error);
else
{
expr = build_class_member_access_expr (promise, meth, NULL_TREE,
true, tf_warning_or_error);
vec<tree, va_gc> *real_args;
if (!args)
real_args = make_tree_vector ();
else
real_args = *args;
expr = build_op_call (expr, &real_args, tf_warning_or_error);
}
return expr;
}
/* Caching get for the expression p.return_void (). */
static tree
get_coroutine_return_void_expr (tree decl, location_t loc, bool musthave)
{
if (coroutine_info *info = get_coroutine_info (decl))
{
/* If we don't have it try to build it. */
if (!info->return_void)
info->return_void
= coro_build_promise_expression (current_function_decl, NULL,
coro_return_void_identifier,
loc, NULL, musthave);
/* Don't return an error if it's an optional call. */
if (!musthave && info->return_void == error_mark_node)
return NULL_TREE;
return info->return_void;
}
return musthave ? error_mark_node : NULL_TREE;
}
/* Lookup an Awaitable member, which should be await_ready, await_suspend
or await_resume. */
static tree
lookup_awaitable_member (tree await_type, tree member_id, location_t loc)
{
tree aw_memb
= lookup_member (await_type, member_id,
/*protect=*/1, /*want_type=*/0, tf_warning_or_error);
if (aw_memb == NULL_TREE)
{
error_at (loc, "no member named %qE in %qT", member_id, await_type);
return error_mark_node;
}
return aw_memb;
}
/* Here we check the constraints that are common to all keywords (since the
presence of a coroutine keyword makes the function into a coroutine). */
static bool
coro_common_keyword_context_valid_p (tree fndecl, location_t kw_loc,
const char *kw_name)
{
if (fndecl == NULL_TREE)
{
error_at (kw_loc, "%qs cannot be used outside a function", kw_name);
return false;
}
/* This is arranged in order of prohibitions in the std. */
if (DECL_MAIN_P (fndecl))
{
/* [basic.start.main] 3. The function main shall not be a coroutine. */
error_at (kw_loc, "%qs cannot be used in the %<main%> function",
kw_name);
return false;
}
if (DECL_DECLARED_CONSTEXPR_P (fndecl))
{
cp_function_chain->invalid_constexpr = true;
if (!is_instantiation_of_constexpr (fndecl))
{
/* [dcl.constexpr] 3.3 it shall not be a coroutine. */
error_at (kw_loc, "%qs cannot be used in a %<constexpr%> function",
kw_name);
return false;
}
}
if (FNDECL_USED_AUTO (fndecl))
{
/* [dcl.spec.auto] 15. A function declared with a return type that uses
a placeholder type shall not be a coroutine. */
error_at (kw_loc,
"%qs cannot be used in a function with a deduced return type",
kw_name);
return false;
}
if (varargs_function_p (fndecl))
{
/* [dcl.fct.def.coroutine] The parameter-declaration-clause of the
coroutine shall not terminate with an ellipsis that is not part
of a parameter-declaration. */
error_at (kw_loc,
"%qs cannot be used in a varargs function", kw_name);
return false;
}
if (DECL_CONSTRUCTOR_P (fndecl))
{
/* [class.ctor] 7. a constructor shall not be a coroutine. */
error_at (kw_loc, "%qs cannot be used in a constructor", kw_name);
return false;
}
if (DECL_DESTRUCTOR_P (fndecl))
{
/* [class.dtor] 21. a destructor shall not be a coroutine. */
error_at (kw_loc, "%qs cannot be used in a destructor", kw_name);
return false;
}
return true;
}
/* Here we check the constraints that are not per keyword. */
static bool
coro_function_valid_p (tree fndecl)
{
location_t f_loc = DECL_SOURCE_LOCATION (fndecl);
/* For cases where fundamental information cannot be found, e.g. the
coroutine traits are missing, we need to punt early. */
if (!coro_promise_type_found_p (fndecl, f_loc))
return false;
/* Since we think the function is a coroutine, that implies we parsed
a keyword that triggered this. Keywords check promise validity for
their context and thus the promise type should be known at this point. */
if (get_coroutine_handle_type (fndecl) == NULL_TREE
|| get_coroutine_promise_type (fndecl) == NULL_TREE)
return false;
if (current_function_returns_value || current_function_returns_null)
{
/* TODO: record or extract positions of returns (and the first coro
keyword) so that we can add notes to the diagnostic about where
the bad keyword is and what made the function into a coro. */
error_at (f_loc, "a %<return%> statement is not allowed in coroutine;"
" did you mean %<co_return%>?");
return false;
}
return true;
}
enum suspend_point_kind {
CO_AWAIT_SUSPEND_POINT = 0,
CO_YIELD_SUSPEND_POINT,
INITIAL_SUSPEND_POINT,
FINAL_SUSPEND_POINT
};
/* Helper function to build a named variable for the temps we use for each
await point. The root of the name is determined by SUSPEND_KIND, and
the variable is of type V_TYPE. The awaitable number is reset each time
we encounter a final suspend. */
static tree
get_awaitable_var (suspend_point_kind suspend_kind, tree v_type)
{
static int awn = 0;
char *buf;
switch (suspend_kind)
{
default: buf = xasprintf ("Aw%d", awn++); break;
case CO_YIELD_SUSPEND_POINT: buf = xasprintf ("Yd%d", awn++); break;
case INITIAL_SUSPEND_POINT: buf = xasprintf ("Is"); break;
case FINAL_SUSPEND_POINT: buf = xasprintf ("Fs"); awn = 0; break;
}
tree ret = get_identifier (buf);
free (buf);
ret = build_lang_decl (VAR_DECL, ret, v_type);
DECL_ARTIFICIAL (ret) = true;
return ret;
}
/* Helpers to diagnose missing noexcept on final await expressions. */
static bool
coro_diagnose_throwing_fn (tree fndecl)
{
if (!TYPE_NOTHROW_P (TREE_TYPE (fndecl)))
{
location_t f_loc = cp_expr_loc_or_loc (fndecl,
DECL_SOURCE_LOCATION (fndecl));
error_at (f_loc, "the expression %qE is required to be non-throwing",
fndecl);
inform (f_loc, "must be declared with %<noexcept(true)%>");
return true;
}
return false;
}
static bool
coro_diagnose_throwing_final_aw_expr (tree expr)
{
if (TREE_CODE (expr) == TARGET_EXPR)
expr = TARGET_EXPR_INITIAL (expr);
tree fn = NULL_TREE;
if (TREE_CODE (expr) == CALL_EXPR)
fn = CALL_EXPR_FN (expr);
else if (TREE_CODE (expr) == AGGR_INIT_EXPR)
fn = AGGR_INIT_EXPR_FN (expr);
else if (TREE_CODE (expr) == CONSTRUCTOR)
return false;
else
{
gcc_checking_assert (0 && "unhandled expression type");
return false;
}
fn = TREE_OPERAND (fn, 0);
return coro_diagnose_throwing_fn (fn);
}
/* This performs [expr.await] bullet 3.3 and validates the interface obtained.
It is also used to build the initial and final suspend points.
'a', 'o' and 'e' are used as per the description in the section noted.
A, the original yield/await expr, is found at source location LOC.
We will be constructing a CO_AWAIT_EXPR for a suspend point of one of
the four suspend_point_kind kinds. This is indicated by SUSPEND_KIND. */
static tree
build_co_await (location_t loc, tree a, suspend_point_kind suspend_kind)
{
/* Try and overload of operator co_await, .... */
tree o;
if (MAYBE_CLASS_TYPE_P (TREE_TYPE (a)))
{
o = build_new_op (loc, CO_AWAIT_EXPR, LOOKUP_NORMAL, a, NULL_TREE,
NULL_TREE, NULL_TREE, NULL, tf_warning_or_error);
/* If no viable functions are found, o is a. */
if (!o || o == error_mark_node)
o = a;
else if (flag_exceptions && suspend_kind == FINAL_SUSPEND_POINT)
{
/* We found an overload for co_await(), diagnose throwing cases. */
if (TREE_CODE (o) == TARGET_EXPR
&& coro_diagnose_throwing_final_aw_expr (o))
return error_mark_node;
/* We now know that the final suspend object is distinct from the
final awaiter, so check for a non-throwing DTOR where needed. */
if (tree dummy = cxx_maybe_build_cleanup (a, tf_none))
{
if (CONVERT_EXPR_P (dummy))
dummy = TREE_OPERAND (dummy, 0);
dummy = TREE_OPERAND (CALL_EXPR_FN (dummy), 0);
if (coro_diagnose_throwing_fn (dummy))
return error_mark_node;
}
}
}
else
o = a; /* This is most likely about to fail anyway. */
tree o_type = TREE_TYPE (o);
if (o_type && !VOID_TYPE_P (o_type))
o_type = complete_type_or_else (o_type, o);
if (!o_type)
return error_mark_node;
if (TREE_CODE (o_type) != RECORD_TYPE)
{
error_at (loc, "awaitable type %qT is not a structure",
o_type);
return error_mark_node;
}
/* Check for required awaitable members and their types. */
tree awrd_meth
= lookup_awaitable_member (o_type, coro_await_ready_identifier, loc);
if (!awrd_meth || awrd_meth == error_mark_node)
return error_mark_node;
tree awsp_meth
= lookup_awaitable_member (o_type, coro_await_suspend_identifier, loc);
if (!awsp_meth || awsp_meth == error_mark_node)
return error_mark_node;
/* The type of the co_await is the return type of the awaitable's
await_resume, so we need to look that up. */
tree awrs_meth
= lookup_awaitable_member (o_type, coro_await_resume_identifier, loc);
if (!awrs_meth || awrs_meth == error_mark_node)
return error_mark_node;
/* To complete the lookups, we need an instance of 'e' which is built from
'o' according to [expr.await] 3.4.
If we need to materialize this as a temporary, then that will have to be
'promoted' to a coroutine frame var. However, if the awaitable is a
user variable, parameter or comes from a scope outside this function,
then we must use it directly - or we will see unnecessary copies.
If o is a variable, find the underlying var. */
tree e_proxy = STRIP_NOPS (o);
if (INDIRECT_REF_P (e_proxy))
e_proxy = TREE_OPERAND (e_proxy, 0);
while (TREE_CODE (e_proxy) == COMPONENT_REF)
{
e_proxy = TREE_OPERAND (e_proxy, 0);
if (INDIRECT_REF_P (e_proxy))
e_proxy = TREE_OPERAND (e_proxy, 0);
if (TREE_CODE (e_proxy) == CALL_EXPR)
{
/* We could have operator-> here too. */
tree op = TREE_OPERAND (CALL_EXPR_FN (e_proxy), 0);
if (DECL_OVERLOADED_OPERATOR_P (op)