On Thu, 29 Sep 2022, Jason Merrill wrote:
On 9/29/22 11:05, Patrick Palka wrote:
Adding a new builtin trait currently involves some boilerplate (as can
be seen in r13-2956-g9ca147154074a0) of defining corresponding RID_ and
CPTK_ enumerators and adding them to various switch statements across
many files. The exact switch statements we need to change is determined
by whether the proposed trait yields a type or an expression.
This RFC patch attempts to streamline this process via a centralized
cp-trait.def file for declaring the important parts about a builtin
trait
(whether it yields a type or an expression, its code, its spelling and
its arity) and using this file to automate away the switch statement
addition boilerplate. It also converts 9 traits to use this approach
by way of example (we can convert all the traits once the design is
settled).
After this change, the process of adding a new builtin trait is just
(modulo tests): declare it in cp-trait.def, define its behavior in
finish_trait_type/expr, and handle it in diagnose_trait_expr if it's
an expression-yielding trait (this last step is unfortunate but since
the switch has no default case, we'll at least get a diagnostic if we
forget to do it).
Does this look like a good approach?
OK.
Thanks a lot, I committed the following (which migrates all the
C++-specific traits to the new approach):
-- >8 --
Subject: [PATCH] c++: streamline built-in trait addition process
Adding a new built-in trait currently involves manual boilerplate
consisting of defining an rid enumerator for the identifier as well as a
corresponding cp_trait_kind enumerator and handling them in various switch
statements, the exact set of which depends on whether the proposed trait
yields (and thus is recognized as) a type or an expression.
To streamline the process, this patch adds a central cp-trait.def file
that tabulates the essential details about each built-in trait (whether
it yields a type or an expression, its code, its spelling and its arity)
and uses this file to automate away the manual boilerplate. It also
migrates all the existing C++-specific built-in traits to use this
approach.
After this change, adding a new built-in trait just entails declaring
it in cp-trait.def and defining its behavior in finish_trait_expr/type
(and handling it in diagnose_trait_expr, if it's an expression-yielding
trait).
gcc/c-family/ChangeLog:
* c-common.cc (c_common_reswords): Use cp/cp-trait.def to handle
C++ traits.
* c-common.h (enum rid): Likewise.
gcc/cp/ChangeLog:
* constraint.cc (diagnose_trait_expr): Likewise.
* cp-objcp-common.cc (names_builtin_p): Likewise.
* cp-tree.h (enum cp_trait_kind): Likewise.
* cxx-pretty-print.cc (pp_cxx_trait): Likewise.
* parser.cc (cp_keyword_starts_decl_specifier_p): Likewise.
(cp_parser_primary_expression): Likewise.
(cp_parser_trait): Likewise.
(cp_parser_simple_type_specifier): Likewise.
* cp-trait.def: New file.
---
gcc/c-family/c-common.cc | 54 ++-------
gcc/c-family/c-common.h | 33 ++----
gcc/cp/constraint.cc | 12 +-
gcc/cp/cp-objcp-common.cc | 44 +-------
gcc/cp/cp-trait.def | 106 ++++++++++++++++++
gcc/cp/cp-tree.h | 46 +-------
gcc/cp/cxx-pretty-print.cc | 126 +--------------------
gcc/cp/parser.cc | 217 ++++---------------------------------
8 files changed, 161 insertions(+), 477 deletions(-)
create mode 100644 gcc/cp/cp-trait.def
diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index 6e0af863a49..3c60a89bfe2 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -378,7 +378,6 @@ const struct c_common_resword c_common_reswords[] =
{ "__attribute", RID_ATTRIBUTE, 0 },
{ "__attribute__", RID_ATTRIBUTE, 0 },
{ "__auto_type", RID_AUTO_TYPE, D_CONLY },
- { "__bases", RID_BASES, D_CXXONLY },
{ "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
{ "__builtin_bit_cast", RID_BUILTIN_BIT_CAST, D_CXXONLY },
{ "__builtin_call_with_static_chain",
@@ -401,44 +400,12 @@ const struct c_common_resword c_common_reswords[] =
{ "__const__", RID_CONST, 0 },
{ "__constinit", RID_CONSTINIT, D_CXXONLY },
{ "__decltype", RID_DECLTYPE, D_CXXONLY },
- { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
{ "__extension__", RID_EXTENSION, 0 },
{ "__func__", RID_C99_FUNCTION_NAME, 0 },
- { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
- { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
- { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
- { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
- { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
- { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
- { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
- { "__has_unique_object_representations",
RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
- D_CXXONLY },
- { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
{ "__imag", RID_IMAGPART, 0 },
{ "__imag__", RID_IMAGPART, 0 },
{ "__inline", RID_INLINE, 0 },
{ "__inline__", RID_INLINE, 0 },
- { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
- { "__is_aggregate", RID_IS_AGGREGATE, D_CXXONLY },
- { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
- { "__is_class", RID_IS_CLASS, D_CXXONLY },
- { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
- { "__is_enum", RID_IS_ENUM, D_CXXONLY },
- { "__is_final", RID_IS_FINAL, D_CXXONLY },
- { "__is_layout_compatible", RID_IS_LAYOUT_COMPATIBLE, D_CXXONLY },
- { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
- { "__is_pointer_interconvertible_base_of",
- RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF, D_CXXONLY },
- { "__is_pod", RID_IS_POD, D_CXXONLY },
- { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
- { "__is_same", RID_IS_SAME_AS, D_CXXONLY },
- { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
- { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
- { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
- { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
- { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE,
D_CXXONLY },
- { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
- { "__is_union", RID_IS_UNION, D_CXXONLY },
{ "__label__", RID_LABEL, 0 },
{ "__null", RID_NULL, 0 },
{ "__real", RID_REALPART, 0 },
@@ -453,7 +420,6 @@ const struct c_common_resword c_common_reswords[] =
{ "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
{ "__typeof", RID_TYPEOF, 0 },
{ "__typeof__", RID_TYPEOF, 0 },
- { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
{ "__volatile", RID_VOLATILE, 0 },
{ "__volatile__", RID_VOLATILE, 0 },
{ "__GIMPLE", RID_GIMPLE, D_CONLY },
@@ -537,19 +503,13 @@ const struct c_common_resword c_common_reswords[] =
{ "volatile", RID_VOLATILE, 0 },
{ "wchar_t", RID_WCHAR, D_CXXONLY },
{ "while", RID_WHILE, 0 },
- { "__is_assignable", RID_IS_ASSIGNABLE, D_CXXONLY },
- { "__is_constructible", RID_IS_CONSTRUCTIBLE, D_CXXONLY },
- { "__is_nothrow_assignable", RID_IS_NOTHROW_ASSIGNABLE, D_CXXONLY },
- { "__is_nothrow_constructible", RID_IS_NOTHROW_CONSTRUCTIBLE, D_CXXONLY
},
- { "__is_convertible", RID_IS_CONVERTIBLE, D_CXXONLY },
- { "__is_nothrow_convertible", RID_IS_NOTHROW_CONVERTIBLE, D_CXXONLY },
- { "__reference_constructs_from_temporary",
RID_REF_CONSTRUCTS_FROM_TEMPORARY,
- D_CXXONLY },
- { "__reference_converts_from_temporary", RID_REF_CONVERTS_FROM_TEMPORARY,
- D_CXXONLY },
- { "__remove_cv", RID_REMOVE_CV, D_CXXONLY },
- { "__remove_reference", RID_REMOVE_REFERENCE, D_CXXONLY },
- { "__remove_cvref", RID_REMOVE_CVREF, D_CXXONLY },
+
+#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
+ { NAME, RID_##CODE, D_CXXONLY },
+#include "cp/cp-trait.def"
+#undef DEFTRAIT
+ /* An alias for __is_same. */
+ { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
/* C++ transactional memory. */
{ "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index d5c98d306ce..5f470d94f4a 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -163,31 +163,14 @@ enum rid
RID_CONSTCAST, RID_DYNCAST, RID_REINTCAST, RID_STATCAST,
/* C++ extensions */
- RID_ADDRESSOF, RID_BASES,
- RID_BUILTIN_LAUNDER, RID_DIRECT_BASES,
- RID_HAS_NOTHROW_ASSIGN, RID_HAS_NOTHROW_CONSTRUCTOR,
- RID_HAS_NOTHROW_COPY, RID_HAS_TRIVIAL_ASSIGN,
- RID_HAS_TRIVIAL_CONSTRUCTOR, RID_HAS_TRIVIAL_COPY,
- RID_HAS_TRIVIAL_DESTRUCTOR, RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
- RID_HAS_VIRTUAL_DESTRUCTOR, RID_BUILTIN_BIT_CAST,
- RID_IS_ABSTRACT, RID_IS_AGGREGATE,
- RID_IS_BASE_OF, RID_IS_CLASS,
- RID_IS_EMPTY, RID_IS_ENUM,
- RID_IS_FINAL, RID_IS_LAYOUT_COMPATIBLE,
- RID_IS_LITERAL_TYPE,
- RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF,
- RID_IS_POD, RID_IS_POLYMORPHIC,
- RID_IS_SAME_AS,
- RID_IS_STD_LAYOUT, RID_IS_TRIVIAL,
- RID_IS_TRIVIALLY_ASSIGNABLE, RID_IS_TRIVIALLY_CONSTRUCTIBLE,
- RID_IS_TRIVIALLY_COPYABLE,
- RID_IS_UNION, RID_UNDERLYING_TYPE,
- RID_IS_ASSIGNABLE, RID_IS_CONSTRUCTIBLE,
- RID_IS_NOTHROW_ASSIGNABLE, RID_IS_NOTHROW_CONSTRUCTIBLE,
- RID_IS_CONVERTIBLE, RID_IS_NOTHROW_CONVERTIBLE,
- RID_REF_CONSTRUCTS_FROM_TEMPORARY,
- RID_REF_CONVERTS_FROM_TEMPORARY,
- RID_REMOVE_CV, RID_REMOVE_REFERENCE, RID_REMOVE_CVREF,
+ RID_ADDRESSOF,
+ RID_BUILTIN_LAUNDER,
+ RID_BUILTIN_BIT_CAST,
+
+#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
+ RID_##CODE,
+#include "cp/cp-trait.def"
+#undef DEFTRAIT
/* C++11 */
RID_CONSTEXPR, RID_DECLTYPE, RID_NOEXCEPT, RID_NULLPTR,
RID_STATIC_ASSERT,
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index ca73aff3f38..f4145571d92 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3711,13 +3711,11 @@ diagnose_trait_expr (tree expr, tree args)
inform (loc, " %qT is not a reference that binds to a temporary "
"object of type %qT (copy-initialization)", t1, t2);
break;
- case CPTK_BASES:
- case CPTK_DIRECT_BASES:
- case CPTK_UNDERLYING_TYPE:
- case CPTK_REMOVE_CV:
- case CPTK_REMOVE_REFERENCE:
- case CPTK_REMOVE_CVREF:
- /* We shouldn't see these non-expression traits. */
+#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
+ case CPTK_##CODE:
+#include "cp-trait.def"
+#undef DEFTRAIT_TYPE
+ /* Type-yielding traits aren't expressions. */
gcc_unreachable ();
/* We deliberately omit the default case so that when adding a new
trait we'll get reminded (by way of a warning) to handle it here.
*/
diff --git a/gcc/cp/cp-objcp-common.cc b/gcc/cp/cp-objcp-common.cc
index 2d3f206b530..e4df30d9720 100644
--- a/gcc/cp/cp-objcp-common.cc
+++ b/gcc/cp/cp-objcp-common.cc
@@ -430,46 +430,10 @@ names_builtin_p (const char *name)
case RID_BUILTIN_ASSOC_BARRIER:
case RID_BUILTIN_BIT_CAST:
case RID_OFFSETOF:
- case RID_HAS_NOTHROW_ASSIGN:
- case RID_HAS_NOTHROW_CONSTRUCTOR:
- case RID_HAS_NOTHROW_COPY:
- case RID_HAS_TRIVIAL_ASSIGN:
- case RID_HAS_TRIVIAL_CONSTRUCTOR:
- case RID_HAS_TRIVIAL_COPY:
- case RID_HAS_TRIVIAL_DESTRUCTOR:
- case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
- case RID_HAS_VIRTUAL_DESTRUCTOR:
- case RID_IS_ABSTRACT:
- case RID_IS_AGGREGATE:
- case RID_IS_BASE_OF:
- case RID_IS_CLASS:
- case RID_IS_EMPTY:
- case RID_IS_ENUM:
- case RID_IS_FINAL:
- case RID_IS_LAYOUT_COMPATIBLE:
- case RID_IS_LITERAL_TYPE:
- case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
- case RID_IS_POD:
- case RID_IS_POLYMORPHIC:
- case RID_IS_SAME_AS:
- case RID_IS_STD_LAYOUT:
- case RID_IS_TRIVIAL:
- case RID_IS_TRIVIALLY_ASSIGNABLE:
- case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
- case RID_IS_TRIVIALLY_COPYABLE:
- case RID_IS_UNION:
- case RID_IS_ASSIGNABLE:
- case RID_IS_CONSTRUCTIBLE:
- case RID_IS_NOTHROW_ASSIGNABLE:
- case RID_IS_NOTHROW_CONSTRUCTIBLE:
- case RID_UNDERLYING_TYPE:
- case RID_IS_CONVERTIBLE:
- case RID_IS_NOTHROW_CONVERTIBLE:
- case RID_REF_CONSTRUCTS_FROM_TEMPORARY:
- case RID_REF_CONVERTS_FROM_TEMPORARY:
- case RID_REMOVE_CV:
- case RID_REMOVE_REFERENCE:
- case RID_REMOVE_CVREF:
+#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
+ case RID_##CODE:
+#include "cp-trait.def"
+#undef DEFTRAIT
return true;
default:
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
new file mode 100644
index 00000000000..922348a1659
--- /dev/null
+++ b/gcc/cp/cp-trait.def
@@ -0,0 +1,106 @@
+/* This file contains the definitions for C++-specific built-in traits.
+
+ Copyright The GNU Toolchain Authors.
+
+ 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/>. */
+
+/* Add a DEFTRAIT_EXPR (CODE, NAME, N) line to this file to define an
+ expression-yielding built-in trait that has internal code name CODE, is
+ spelled as NAME and takes N type arguments (where N is either 1, 2, or
+ the special value -1 which denotes that it takes at least one argument).
+ Such traits are represented as TRAIT_EXPR tree whose TRAIT_EXPR_KIND is
+ CPTK_CODE. Define the behavior of the trait in finish_trait_expr. */
+
+/* Add a DEFTRAIT_TYPE (CODE, NAME, N) line to this file to define a
+ type-yielding built-in trait as described above. Such traits are
+ generally represented as a TRAIT_TYPE tree whose TRAIT_TYPE_KIND is
+ CPTK_CODE (exceptions are BASES and DIRECT_BASES below). Define the
+ behavior of the trait in finish_trait_type. */
+
+#ifdef DEFTRAIT
+#define DEFTRAIT_EXPR(CODE, NAME, ARITY) DEFTRAIT(tcc_expression, CODE,
NAME, ARITY)
+#define DEFTRAIT_TYPE(CODE, NAME, ARITY) DEFTRAIT(tcc_type, CODE, NAME,
ARITY)
+#define DEFTRAIT_EXPR_DEFAULTED
+#define DEFTRAIT_TYPE_DEFAULTED
+#endif
+
+#ifndef DEFTRAIT_EXPR
+#define DEFTRAIT_EXPR(CODE, NAME, ARITY)
+#define DEFTRAIT_EXPR_DEFAULTED
+#endif
+
+#ifndef DEFTRAIT_TYPE
+#define DEFTRAIT_TYPE(CODE, NAME, ARITY)
+#define DEFTRAIT_TYPE_DEFAULTED
+#endif
+
+DEFTRAIT_EXPR (HAS_NOTHROW_ASSIGN, "__has_nothrow_assign", 1)
+DEFTRAIT_EXPR (HAS_NOTHROW_CONSTRUCTOR, "__has_nothrow_constructor", 1)
+DEFTRAIT_EXPR (HAS_NOTHROW_COPY, "__has_nothrow_copy", 1)
+DEFTRAIT_EXPR (HAS_TRIVIAL_ASSIGN, "__has_trivial_assign", 1)
+DEFTRAIT_EXPR (HAS_TRIVIAL_CONSTRUCTOR, "__has_trivial_constructor", 1)
+DEFTRAIT_EXPR (HAS_TRIVIAL_COPY, "__has_trivial_copy", 1)
+DEFTRAIT_EXPR (HAS_TRIVIAL_DESTRUCTOR, "__has_trivial_destructor", 1)
+DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS,
"__has_unique_object_representations", 1)
+DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
+DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
+DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
+DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
+DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
+DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
+DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1)
+DEFTRAIT_EXPR (IS_CONVERTIBLE, "__is_convertible", 2)
+DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
+DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
+DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
+DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
+DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
+DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
+DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
+DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
+DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF,
"__is_pointer_interconvertible_base_of", 2)
+DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
+DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
+DEFTRAIT_EXPR (IS_SAME_AS, "__is_same", 2)