[C++,coroutines,1/7] Common code and base definitions.

Message ID 92319BBC-8CC8-49F0-9DCE-1CB91F145D11@sandoe.co.uk
State New
Headers show
Series
  • [C++,coroutines,1/7] Common code and base definitions.
Related show

Commit Message

Iain Sandoe Jan. 9, 2020, 12:36 p.m.
Jeff Law <law@redhat.com> wrote:

> On 11/17/19 3:24 AM, Iain Sandoe wrote:

>> This part of the patch series provides the gating flag, the keywords,

>> cpp defines etc.

>> 

>> gcc/ChangeLog:

>> 

>> 2019-11-17  Iain Sandoe  <iain@sandoe.co.uk>

>> 

>> 	* doc/invoke.texi: Document the fcoroutines command line

>> 	switch.

>> 

>> gcc/c-family/ChangeLog:

>> 

>> 2019-11-17  Iain Sandoe  <iain@sandoe.co.uk>

>> 

>> 	* c-common.c (co_await, co_yield, co_return): New.

>> 	* c-common.h (RID_CO_AWAIT, RID_CO_YIELD,

>> 	RID_CO_RETURN): New enumeration values.

>> 	(D_CXX_COROUTINES): Bit to identify coroutines are active.

>> 	(D_CXX_COROUTINES_FLAGS): Guard for coroutine keywords.

>> 	* c-cppbuiltin.c (__cpp_coroutines): New cpp define.

>> 	* c.opt (fcoroutines): New command-line switch.

>> 

>> gcc/cp/ChangeLog:

>> 

>> 2019-11-17  Iain Sandoe  <iain@sandoe.co.uk>

>> 

>> 	* cp-tree.h (lang_decl-fn): coroutine_p, new bit.

>> 	* lex.c (init_reswords): Enable keywords when the coroutine flag

>> 	is set,

>> 	* operators.def (co_await): New operator.

> Looks quite reasonable to me.  If you need minor twiddling due to

> reviewer feedback elsewhere those are pre-approved as well.


Here is the updated patch (this is actually unchanged apart from dates).
Iain

--

Patch

=====

This part of the patch series provides the gating flag, the keywords,
cpp defines etc.

gcc/ChangeLog:

2020-01-09  Iain Sandoe  <iain@sandoe.co.uk>

	* doc/invoke.texi: Document the fcoroutines command line
	switch.

gcc/c-family/ChangeLog:

2020-01-09  Iain Sandoe  <iain@sandoe.co.uk>

	* c-common.c (co_await, co_yield, co_return): New.
	* c-common.h (RID_CO_AWAIT, RID_CO_YIELD,
	RID_CO_RETURN): New enumeration values.
	(D_CXX_COROUTINES): Bit to identify coroutines are active.
	(D_CXX_COROUTINES_FLAGS): Guard for coroutine keywords.
	* c-cppbuiltin.c (__cpp_coroutines): New cpp define.
	* c.opt (fcoroutines): New command-line switch.

gcc/cp/ChangeLog:

2020-01-09  Iain Sandoe  <iain@sandoe.co.uk>

	* cp-tree.h (lang_decl-fn): coroutine_p, new bit.
	(DECL_COROUTINE_P): New.
	* lex.c (init_reswords): Enable keywords when the coroutine flag
	is set,
	* operators.def (co_await): New operator.
---
 gcc/c-family/c-common.c     |  5 +++++
 gcc/c-family/c-common.h     |  5 +++++
 gcc/c-family/c-cppbuiltin.c |  2 ++
 gcc/c-family/c.opt          |  4 ++++
 gcc/cp/cp-tree.h            | 17 ++++++++++++++++-
 gcc/cp/lex.c                |  2 ++
 gcc/cp/operators.def        |  1 +
 gcc/doc/invoke.texi         |  4 ++++
 8 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 832d8a5dce..26a94a1535 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -537,6 +537,11 @@  const struct c_common_resword c_common_reswords[] =
   { "concept",		RID_CONCEPT,	D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
   { "requires", 	RID_REQUIRES,	D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
 
+  /* Coroutines-related keywords */
+  { "co_await",		RID_CO_AWAIT,	D_CXX_COROUTINES_FLAGS | D_CXXWARN },
+  { "co_yield",		RID_CO_YIELD,	D_CXX_COROUTINES_FLAGS | D_CXXWARN },
+  { "co_return", 	RID_CO_RETURN,	D_CXX_COROUTINES_FLAGS | D_CXXWARN },
+
   /* These Objective-C keywords are recognized only immediately after
      an '@'.  */
   { "compatibility_alias", RID_AT_ALIAS,	D_OBJC },
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 37b0594995..59d4aaf443 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -189,6 +189,9 @@  enum rid
   /* C++ concepts */
   RID_CONCEPT, RID_REQUIRES,
 
+  /* C++ coroutines */
+  RID_CO_AWAIT, RID_CO_YIELD, RID_CO_RETURN,
+
   /* C++ transactional memory.  */
   RID_ATOMIC_NOEXCEPT, RID_ATOMIC_CANCEL, RID_SYNCHRONIZED,
 
@@ -433,9 +436,11 @@  extern machine_mode c_default_pointer_mode;
 #define D_TRANSMEM	0X0800	/* C++ transactional memory TS.  */
 #define D_CXX_CHAR8_T	0X1000	/* In C++, only with -fchar8_t.  */
 #define D_CXX20		0x2000  /* In C++, C++20 only.  */
+#define D_CXX_COROUTINES 0x4000  /* In C++, only with coroutines.  */
 
 #define D_CXX_CONCEPTS_FLAGS D_CXXONLY | D_CXX_CONCEPTS
 #define D_CXX_CHAR8_T_FLAGS D_CXXONLY | D_CXX_CHAR8_T
+#define D_CXX_COROUTINES_FLAGS (D_CXXONLY | D_CXX_COROUTINES)
 
 /* The reserved keyword table.  */
 extern const struct c_common_resword c_common_reswords[];
diff --git a/gcc/c-family/c-cppbuiltin.c b/gcc/c-family/c-cppbuiltin.c
index cb86941516..a6308921dc 100644
--- a/gcc/c-family/c-cppbuiltin.c
+++ b/gcc/c-family/c-cppbuiltin.c
@@ -1017,6 +1017,8 @@  c_cpp_builtins (cpp_reader *pfile)
           else
             cpp_define (pfile, "__cpp_concepts=201507L");
         }
+      if (flag_coroutines)
+	cpp_define (pfile, "__cpp_coroutines=201902L"); /* n4835, C++20 CD */
       if (flag_tm)
 	/* Use a value smaller than the 201505 specified in
 	   the TS, since we don't yet support atomic_cancel.  */
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 230b76387b..aa0fa5deae 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1477,6 +1477,10 @@  fconstexpr-ops-limit=
 C++ ObjC++ Joined RejectNegative Host_Wide_Int Var(constexpr_ops_limit) Init(33554432)
 -fconstexpr-ops-limit=<number>	Specify maximum number of constexpr operations during a single constexpr evaluation.
 
+fcoroutines
+C++ LTO Var(flag_coroutines)
+Enable C++ coroutines (experimental).
+
 fdebug-cpp
 C ObjC C++ ObjC++
 Emit debug annotations during preprocessing.
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 495ed6b9f5..553e447434 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -2701,7 +2701,9 @@  struct GTY(()) lang_decl_fn {
   unsigned has_dependent_explicit_spec_p : 1;
   unsigned immediate_fn_p : 1;
   unsigned maybe_deleted : 1;
-  unsigned spare : 10;
+  unsigned coroutine_p : 1;
+
+  unsigned spare : 9;
 
   /* 32-bits padding on 64-bit host.  */
 
@@ -4992,6 +4994,13 @@  more_aggr_init_expr_args_p (const aggr_init_expr_arg_iterator *iter)
 #define QUALIFIED_NAME_IS_TEMPLATE(NODE) \
   (TREE_LANG_FLAG_1 (SCOPE_REF_CHECK (NODE)))
 
+/* [coroutines]
+*/
+
+/* True if NODE is a co-routine FUNCTION_DECL.  */
+#define DECL_COROUTINE_P(NODE) \
+  (LANG_DECL_FN_CHECK (DECL_COMMON_CHECK (NODE))->coroutine_p)
+
 /* True for an OMP_ATOMIC that has dependent parameters.  These are stored
    as an expr in operand 1, and integer_zero_node or clauses in operand 0.  */
 #define OMP_ATOMIC_DEPENDENT_P(NODE) \
@@ -7931,6 +7940,12 @@  extern tree cp_ubsan_maybe_instrument_downcast	(location_t, tree, tree, tree);
 extern tree cp_ubsan_maybe_instrument_cast_to_vbase (location_t, tree, tree);
 extern void cp_ubsan_maybe_initialize_vtbl_ptrs (tree);
 
+/* In coroutines.cc */
+extern tree finish_co_return_stmt		(location_t, tree);
+extern tree finish_co_await_expr		(location_t, tree);
+extern tree finish_co_yield_expr		(location_t, tree);
+extern bool morph_fn_to_coro			(tree, tree *, tree *);
+
 /* Inline bodies.  */
 
 inline tree
diff --git a/gcc/cp/lex.c b/gcc/cp/lex.c
index 11b872936a..37282d5697 100644
--- a/gcc/cp/lex.c
+++ b/gcc/cp/lex.c
@@ -233,6 +233,8 @@  init_reswords (void)
     mask |= D_CXX20;
   if (!flag_concepts)
     mask |= D_CXX_CONCEPTS;
+  if (!flag_coroutines)
+    mask |= D_CXX_COROUTINES;
   if (!flag_tm)
     mask |= D_TRANSMEM;
   if (!flag_char8_t)
diff --git a/gcc/cp/operators.def b/gcc/cp/operators.def
index c0a659381b..d2395355af 100644
--- a/gcc/cp/operators.def
+++ b/gcc/cp/operators.def
@@ -87,6 +87,7 @@  DEF_OPERATOR ("++", PREINCREMENT_EXPR, "pp", OVL_OP_FLAG_UNARY)
 DEF_OPERATOR ("--", PREDECREMENT_EXPR, "mm", OVL_OP_FLAG_UNARY)
 DEF_OPERATOR ("->", COMPONENT_REF, "pt", OVL_OP_FLAG_UNARY)
 DEF_OPERATOR ("sizeof", SIZEOF_EXPR, "sz", OVL_OP_FLAG_UNARY)
+DEF_OPERATOR ("co_await", CO_AWAIT_EXPR, "aw", OVL_OP_FLAG_UNARY)
 
 /* These are extensions.  */
 DEF_OPERATOR ("alignof", ALIGNOF_EXPR, "az", OVL_OP_FLAG_UNARY)
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 2b07c09def..abcab9515e 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -2569,6 +2569,10 @@  of a loop too many expressions need to be evaluated, the resulting constexpr
 evaluation might take too long.
 The default is 33554432 (1<<25).
 
+@item -fcoroutines
+@opindex fcoroutines
+Enable support for the C++ coroutines extension (experimental).
+
 @item -fno-elide-constructors
 @opindex fno-elide-constructors
 @opindex felide-constructors