[C++,coroutines,1/6] Common code and base definitions.
diff mbox series

Message ID 285E6AA6-17E6-4E7F-9F37-852707896DA1@sandoe.co.uk
State New
Headers show
Series
  • Implement C++ coroutines.
Related show

Commit Message

Iain Sandoe Nov. 17, 2019, 10:24 a.m. UTC
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.
---
 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(-)

Comments

Jeff Law Nov. 17, 2019, 3:49 p.m. UTC | #1
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.

jeff

Patch
diff mbox series

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 4881199..8be92a6 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -536,6 +536,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 80a8c9f..6ec0910 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 cf3d437..6299d47 100644
--- a/gcc/c-family/c-cppbuiltin.c
+++ b/gcc/c-family/c-cppbuiltin.c
@@ -1000,6 +1000,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 914a2f0..62bf4f1 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1469,6 +1469,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 adc021b..6fb99d8 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -2696,7 +2696,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.  */
 
@@ -4982,6 +4984,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) \
@@ -7892,6 +7901,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 25529e7..cd8dd80 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 ee0a4c1..3e5bd1f 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 00eb7e7..7be3bfb 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -2567,6 +2567,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