Re: [PATCH v16 02/39] c-family, c++: Look up built-in traits through gperf

2023-10-11 Thread Ken Matsui
On Wed, Oct 11, 2023 at 1:09 PM Patrick Palka  wrote:
>
> On Tue, 10 Oct 2023, Ken Matsui wrote:
>
> > Since RID_MAX soon reaches 255 and all traits are used approximately once in
> > a C++ translation unit, this patch instead uses only RID_TRAIT_EXPR and
> > RID_TRAIT_TYPE for all traits and uses gperf to look up the specific trait.
>
> Nice!  This looks good to me, but I wonder what the corresponding
> ridpointers entry should be for RID_TRAIT_TYPE and RID_TRAIT_EXPR?  It
> seems we currently assume every rid code has a unique canonical spelling
> which we keep in ridpointers[RID_FOO], but that's of course not the case
> for RID_TRAIT_TYPE and RID_TRAIT_EXPR.  Maybe we should make
> init_reswords() keep the ridpointers entry empty for RID_TRAIT_EXPR and
> RID_TRAIT_TYPE?
>

That makes sense. I will update this patch. Thank you for your review!

> >
> > gcc/c-family/ChangeLog:
> >
> >   * c-common.cc (c_common_reswords): Map all traits to RID_TRAIT_EXPR
> >   and RID_TRAIT_TYPE instead.
> >   * c-common.h (enum rid): Remove all existing RID values for traits.
> >   Use RID_TRAIT_EXPR and RID_TRAIT_TYPE instead.
> >
> > gcc/cp/ChangeLog:
> >
> >   * Make-lang.in: Add targets to generate cp-trait.gperf and
> >   cp-trait.h.
> >   * cp-objcp-common.cc (names_builtin_p): Remove all existing RID values
> >   for traits.  Use RID_TRAIT_EXPR and RID_TRAIT_TYPE instead.
> >   * parser.cc (cp_keyword_starts_decl_specifier_p): Likewise, for
> >   type-yielding traits.  Use RID_TRAIT_TYPE instead.
> >   (cp_parser_simple_type_specifier): Likewise.
> >   (cp_parser_primary_expression): Likewise, for expression-yielding
> >   traits.  Use RID_TRAIT_EXPR instead.
> >   (cp_parser_trait): Look up traits through gperf instead of enum rid.
> >   * cp-trait-head.in: New file.
> >   * cp-trait.gperf: New file.
> >   * cp-trait.h: New file.
> >
> > Signed-off-by: Ken Matsui 
> > ---
> >  gcc/c-family/c-common.cc  |  12 +-
> >  gcc/c-family/c-common.h   |   7 +-
> >  gcc/cp/Make-lang.in   |  24 
> >  gcc/cp/cp-objcp-common.cc |   6 +-
> >  gcc/cp/cp-trait-head.in   |  30 +
> >  gcc/cp/cp-trait.gperf |  74 
> >  gcc/cp/cp-trait.h | 247 ++
> >  gcc/cp/parser.cc  |  70 ---
> >  8 files changed, 412 insertions(+), 58 deletions(-)
> >  create mode 100644 gcc/cp/cp-trait-head.in
> >  create mode 100644 gcc/cp/cp-trait.gperf
> >  create mode 100644 gcc/cp/cp-trait.h
> >
> > diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
> > index f044db5b797..f219ccd29e5 100644
> > --- a/gcc/c-family/c-common.cc
> > +++ b/gcc/c-family/c-common.cc
> > @@ -508,12 +508,16 @@ const struct c_common_resword c_common_reswords[] =
> >{ "wchar_t",   RID_WCHAR,  D_CXXONLY },
> >{ "while", RID_WHILE,  0 },
> >
> > -#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
> > -  { NAME,RID_##CODE, D_CXXONLY },
> > +#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
> > +  { NAME,RID_TRAIT_EXPR, D_CXXONLY },
> >  #include "cp/cp-trait.def"
> > -#undef DEFTRAIT
> > +#undef DEFTRAIT_EXPR
> >/* An alias for __is_same.  */
> > -  { "__is_same_as",  RID_IS_SAME,D_CXXONLY },
> > +  { "__is_same_as",  RID_TRAIT_EXPR, D_CXXONLY },
> > +#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
> > +  { NAME,RID_TRAIT_TYPE, D_CXXONLY },
> > +#include "cp/cp-trait.def"
> > +#undef DEFTRAIT_TYPE
> >
> >/* 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 1fdba7ef3ea..a1a641f4175 100644
> > --- a/gcc/c-family/c-common.h
> > +++ b/gcc/c-family/c-common.h
> > @@ -168,10 +168,9 @@ enum rid
> >RID_BUILTIN_LAUNDER,
> >RID_BUILTIN_BIT_CAST,
> >
> > -#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
> > -  RID_##CODE,
> > -#include "cp/cp-trait.def"
> > -#undef DEFTRAIT
> > +  /* C++ traits, defined in cp-trait.def.  */
> > +  RID_TRAIT_EXPR,
> > +  RID_TRAIT_TYPE,
> >
> >/* C++11 */
> >RID_CONSTEXPR, RID_DECLTYPE, RID_NOEXCEPT, RID_NULLPTR, 
> > RID_STATIC_ASSERT,
> > diff --git a/gcc/cp/Make-lang.in b/gcc/cp/Make-lang.in
> > index 2727fb7f8cc..8d4e3a1f594 100644
> > --- a/gcc/cp/Make-lang.in
> > +++ b/gcc/cp/Make-lang.in
> > @@ -34,6 +34,8 @@
> >  # - the compiler proper (eg: cc1plus)
> >  # - define the names for selecting the language in LANGUAGES.
> >
> > +AWK = @AWK@
> > +
> >  # Actual names to use when installing a native compiler.
> >  CXX_INSTALL_NAME := $(shell echo c++|sed '$(program_transform_name)')
> >  GXX_INSTALL_NAME := $(shell echo g++|sed '$(program_transform_name)')
> > @@ -186,6 +188,28 @@ endif
> >  # This is the file that depends on the generated header file.
> >  cp/name-lookup.o: $(srcdir)/cp/std-name-hint.h
> >
> > +# We always need the dependency on the .gperf file because it itself is 
> > 

Re: [PATCH v16 02/39] c-family, c++: Look up built-in traits through gperf

2023-10-11 Thread Patrick Palka
On Tue, 10 Oct 2023, Ken Matsui wrote:

> Since RID_MAX soon reaches 255 and all traits are used approximately once in
> a C++ translation unit, this patch instead uses only RID_TRAIT_EXPR and
> RID_TRAIT_TYPE for all traits and uses gperf to look up the specific trait.

Nice!  This looks good to me, but I wonder what the corresponding
ridpointers entry should be for RID_TRAIT_TYPE and RID_TRAIT_EXPR?  It
seems we currently assume every rid code has a unique canonical spelling
which we keep in ridpointers[RID_FOO], but that's of course not the case
for RID_TRAIT_TYPE and RID_TRAIT_EXPR.  Maybe we should make
init_reswords() keep the ridpointers entry empty for RID_TRAIT_EXPR and
RID_TRAIT_TYPE?

> 
> gcc/c-family/ChangeLog:
> 
>   * c-common.cc (c_common_reswords): Map all traits to RID_TRAIT_EXPR
>   and RID_TRAIT_TYPE instead.
>   * c-common.h (enum rid): Remove all existing RID values for traits.
>   Use RID_TRAIT_EXPR and RID_TRAIT_TYPE instead.
> 
> gcc/cp/ChangeLog:
> 
>   * Make-lang.in: Add targets to generate cp-trait.gperf and
>   cp-trait.h.
>   * cp-objcp-common.cc (names_builtin_p): Remove all existing RID values
>   for traits.  Use RID_TRAIT_EXPR and RID_TRAIT_TYPE instead.
>   * parser.cc (cp_keyword_starts_decl_specifier_p): Likewise, for
>   type-yielding traits.  Use RID_TRAIT_TYPE instead.
>   (cp_parser_simple_type_specifier): Likewise.
>   (cp_parser_primary_expression): Likewise, for expression-yielding
>   traits.  Use RID_TRAIT_EXPR instead.
>   (cp_parser_trait): Look up traits through gperf instead of enum rid.
>   * cp-trait-head.in: New file.
>   * cp-trait.gperf: New file.
>   * cp-trait.h: New file.
> 
> Signed-off-by: Ken Matsui 
> ---
>  gcc/c-family/c-common.cc  |  12 +-
>  gcc/c-family/c-common.h   |   7 +-
>  gcc/cp/Make-lang.in   |  24 
>  gcc/cp/cp-objcp-common.cc |   6 +-
>  gcc/cp/cp-trait-head.in   |  30 +
>  gcc/cp/cp-trait.gperf |  74 
>  gcc/cp/cp-trait.h | 247 ++
>  gcc/cp/parser.cc  |  70 ---
>  8 files changed, 412 insertions(+), 58 deletions(-)
>  create mode 100644 gcc/cp/cp-trait-head.in
>  create mode 100644 gcc/cp/cp-trait.gperf
>  create mode 100644 gcc/cp/cp-trait.h
> 
> diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
> index f044db5b797..f219ccd29e5 100644
> --- a/gcc/c-family/c-common.cc
> +++ b/gcc/c-family/c-common.cc
> @@ -508,12 +508,16 @@ const struct c_common_resword c_common_reswords[] =
>{ "wchar_t",   RID_WCHAR,  D_CXXONLY },
>{ "while", RID_WHILE,  0 },
>  
> -#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
> -  { NAME,RID_##CODE, D_CXXONLY },
> +#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
> +  { NAME,RID_TRAIT_EXPR, D_CXXONLY },
>  #include "cp/cp-trait.def"
> -#undef DEFTRAIT
> +#undef DEFTRAIT_EXPR
>/* An alias for __is_same.  */
> -  { "__is_same_as",  RID_IS_SAME,D_CXXONLY },
> +  { "__is_same_as",  RID_TRAIT_EXPR, D_CXXONLY },
> +#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
> +  { NAME,RID_TRAIT_TYPE, D_CXXONLY },
> +#include "cp/cp-trait.def"
> +#undef DEFTRAIT_TYPE
>  
>/* 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 1fdba7ef3ea..a1a641f4175 100644
> --- a/gcc/c-family/c-common.h
> +++ b/gcc/c-family/c-common.h
> @@ -168,10 +168,9 @@ enum rid
>RID_BUILTIN_LAUNDER,
>RID_BUILTIN_BIT_CAST,
>  
> -#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
> -  RID_##CODE,
> -#include "cp/cp-trait.def"
> -#undef DEFTRAIT
> +  /* C++ traits, defined in cp-trait.def.  */
> +  RID_TRAIT_EXPR,
> +  RID_TRAIT_TYPE,
>  
>/* C++11 */
>RID_CONSTEXPR, RID_DECLTYPE, RID_NOEXCEPT, RID_NULLPTR, RID_STATIC_ASSERT,
> diff --git a/gcc/cp/Make-lang.in b/gcc/cp/Make-lang.in
> index 2727fb7f8cc..8d4e3a1f594 100644
> --- a/gcc/cp/Make-lang.in
> +++ b/gcc/cp/Make-lang.in
> @@ -34,6 +34,8 @@
>  # - the compiler proper (eg: cc1plus)
>  # - define the names for selecting the language in LANGUAGES.
>  
> +AWK = @AWK@
> +
>  # Actual names to use when installing a native compiler.
>  CXX_INSTALL_NAME := $(shell echo c++|sed '$(program_transform_name)')
>  GXX_INSTALL_NAME := $(shell echo g++|sed '$(program_transform_name)')
> @@ -186,6 +188,28 @@ endif
>  # This is the file that depends on the generated header file.
>  cp/name-lookup.o: $(srcdir)/cp/std-name-hint.h
>  
> +# We always need the dependency on the .gperf file because it itself is 
> generated.
> +ifeq ($(ENABLE_MAINTAINER_RULES), true)
> +$(srcdir)/cp/cp-trait.h: $(srcdir)/cp/cp-trait.gperf
> +else
> +$(srcdir)/cp/cp-trait.h: | $(srcdir)/cp/cp-trait.gperf
> +endif
> + gperf -o -C -E -k '8' -D -N 'find' -L C++ \
> + $(srcdir)/cp/cp-trait.gperf --output-file 
> $(srcdir)/cp/cp-trait.h
> +
> +# The cp-trait.gperf 

[PATCH v16 02/39] c-family, c++: Look up built-in traits through gperf

2023-10-10 Thread Ken Matsui
Since RID_MAX soon reaches 255 and all traits are used approximately once in
a C++ translation unit, this patch instead uses only RID_TRAIT_EXPR and
RID_TRAIT_TYPE for all traits and uses gperf to look up the specific trait.

gcc/c-family/ChangeLog:

* c-common.cc (c_common_reswords): Map all traits to RID_TRAIT_EXPR
and RID_TRAIT_TYPE instead.
* c-common.h (enum rid): Remove all existing RID values for traits.
Use RID_TRAIT_EXPR and RID_TRAIT_TYPE instead.

gcc/cp/ChangeLog:

* Make-lang.in: Add targets to generate cp-trait.gperf and
cp-trait.h.
* cp-objcp-common.cc (names_builtin_p): Remove all existing RID values
for traits.  Use RID_TRAIT_EXPR and RID_TRAIT_TYPE instead.
* parser.cc (cp_keyword_starts_decl_specifier_p): Likewise, for
type-yielding traits.  Use RID_TRAIT_TYPE instead.
(cp_parser_simple_type_specifier): Likewise.
(cp_parser_primary_expression): Likewise, for expression-yielding
traits.  Use RID_TRAIT_EXPR instead.
(cp_parser_trait): Look up traits through gperf instead of enum rid.
* cp-trait-head.in: New file.
* cp-trait.gperf: New file.
* cp-trait.h: New file.

Signed-off-by: Ken Matsui 
---
 gcc/c-family/c-common.cc  |  12 +-
 gcc/c-family/c-common.h   |   7 +-
 gcc/cp/Make-lang.in   |  24 
 gcc/cp/cp-objcp-common.cc |   6 +-
 gcc/cp/cp-trait-head.in   |  30 +
 gcc/cp/cp-trait.gperf |  74 
 gcc/cp/cp-trait.h | 247 ++
 gcc/cp/parser.cc  |  70 ---
 8 files changed, 412 insertions(+), 58 deletions(-)
 create mode 100644 gcc/cp/cp-trait-head.in
 create mode 100644 gcc/cp/cp-trait.gperf
 create mode 100644 gcc/cp/cp-trait.h

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index f044db5b797..f219ccd29e5 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -508,12 +508,16 @@ const struct c_common_resword c_common_reswords[] =
   { "wchar_t", RID_WCHAR,  D_CXXONLY },
   { "while",   RID_WHILE,  0 },
 
-#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
-  { NAME,  RID_##CODE, D_CXXONLY },
+#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
+  { NAME,  RID_TRAIT_EXPR, D_CXXONLY },
 #include "cp/cp-trait.def"
-#undef DEFTRAIT
+#undef DEFTRAIT_EXPR
   /* An alias for __is_same.  */
-  { "__is_same_as",RID_IS_SAME,D_CXXONLY },
+  { "__is_same_as",RID_TRAIT_EXPR, D_CXXONLY },
+#define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
+  { NAME,  RID_TRAIT_TYPE, D_CXXONLY },
+#include "cp/cp-trait.def"
+#undef DEFTRAIT_TYPE
 
   /* 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 1fdba7ef3ea..a1a641f4175 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -168,10 +168,9 @@ enum rid
   RID_BUILTIN_LAUNDER,
   RID_BUILTIN_BIT_CAST,
 
-#define DEFTRAIT(TCC, CODE, NAME, ARITY) \
-  RID_##CODE,
-#include "cp/cp-trait.def"
-#undef DEFTRAIT
+  /* C++ traits, defined in cp-trait.def.  */
+  RID_TRAIT_EXPR,
+  RID_TRAIT_TYPE,
 
   /* C++11 */
   RID_CONSTEXPR, RID_DECLTYPE, RID_NOEXCEPT, RID_NULLPTR, RID_STATIC_ASSERT,
diff --git a/gcc/cp/Make-lang.in b/gcc/cp/Make-lang.in
index 2727fb7f8cc..8d4e3a1f594 100644
--- a/gcc/cp/Make-lang.in
+++ b/gcc/cp/Make-lang.in
@@ -34,6 +34,8 @@
 # - the compiler proper (eg: cc1plus)
 # - define the names for selecting the language in LANGUAGES.
 
+AWK = @AWK@
+
 # Actual names to use when installing a native compiler.
 CXX_INSTALL_NAME := $(shell echo c++|sed '$(program_transform_name)')
 GXX_INSTALL_NAME := $(shell echo g++|sed '$(program_transform_name)')
@@ -186,6 +188,28 @@ endif
 # This is the file that depends on the generated header file.
 cp/name-lookup.o: $(srcdir)/cp/std-name-hint.h
 
+# We always need the dependency on the .gperf file because it itself is 
generated.
+ifeq ($(ENABLE_MAINTAINER_RULES), true)
+$(srcdir)/cp/cp-trait.h: $(srcdir)/cp/cp-trait.gperf
+else
+$(srcdir)/cp/cp-trait.h: | $(srcdir)/cp/cp-trait.gperf
+endif
+   gperf -o -C -E -k '8' -D -N 'find' -L C++ \
+   $(srcdir)/cp/cp-trait.gperf --output-file 
$(srcdir)/cp/cp-trait.h
+
+# The cp-trait.gperf file itself is generated from a cp-trait.def file.
+$(srcdir)/cp/cp-trait.gperf: $(srcdir)/cp/cp-trait.def 
$(srcdir)/cp/cp-trait-head.in
+   cat $(srcdir)/cp/cp-trait-head.in > $@
+   $(AWK) -F', *' '/^DEFTRAIT_/ { \
+   type = (index($$1, "DEFTRAIT_TYPE") != 0 ? "true" : "false"); \
+   gsub(/DEFTRAIT_(EXPR|TYPE) \(/, "", $$1); \
+   gsub(/\)/, "", $$3); \
+   print $$2", CPTK_" $$1", "$$3", "type; \
+   }' $(srcdir)/cp/cp-trait.def >> $@
+
+# This is the file that depends on the generated header file.
+cp/parser.o: $(srcdir)/cp/cp-trait.h
+
 components_in_prev = "bfd opcodes binutils