Excerpts from Richard Sandiford via Gcc-patches's message of September 8, 2023
6:29 pm:
> Currently there are four static sources of attributes:
>
> - LANG_HOOKS_ATTRIBUTE_TABLE
> - LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
> - LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
> - TARGET_ATTRIBUTE_TABLE
>
> All of the attributes in these tables go in the "gnu" namespace.
> This means that they can use the traditional GNU __attribute__((...))
> syntax and the standard [[gnu::...]] syntax.
>
> Standard attributes are registered dynamically with a null namespace.
> There are no supported attributes in other namespaces (clang, vendor
> namespaces, etc.).
>
> This patch tries to generalise things by making the namespace
> part of the attribute specification.
>
> It's usual for multiple attributes to be defined in the same namespace,
> so rather than adding the namespace to each individual definition,
> it seemed better to group attributes in the same namespace together.
> This would also allow us to reuse the same table for clang attributes
> that are written with the GNU syntax, or other similar situations
> where the attribute can be accessed via multiple "spellings".
>
> The patch therefore adds a scoped_attribute_specs that contains
> a namespace and a list of attributes in that namespace.
>
Changes to the D front-end in this patch look reasonable to me.
Regards,
Iain.
>
>
> gcc/d/
> * d-tree.h (d_langhook_attribute_table): Replace with...
> (d_langhook_gnu_attribute_table): ...this.
> (d_langhook_common_attribute_table): Change type to
> scoped_attribute_specs.
> * d-attribs.cc (d_langhook_common_attribute_table): Change type to
> scoped_attribute_specs, using...
> (d_langhook_common_attributes): ...this as the underlying array.
> (d_langhook_attribute_table): Replace with...
> (d_langhook_gnu_attributes, d_langhook_gnu_attribute_table): ...these
> new globals.
> (uda_attribute_p): Update accordingly, and update for new
> targetm.attribute_table type.
> * d-lang.cc (d_langhook_attribute_table): New global.
> (LANG_HOOKS_COMMON_ATTRIBUTE_TABLE): Delete.
>
> ---
> gcc/d/d-attribs.cc | 35 ++---
> gcc/d/d-lang.cc | 8 +-
> gcc/d/d-tree.h | 4 +-
>
> diff --git a/gcc/d/d-attribs.cc b/gcc/d/d-attribs.cc
> index cc46220ddc2..78215bc88bc 100644
> --- a/gcc/d/d-attribs.cc
> +++ b/gcc/d/d-attribs.cc
> @@ -162,7 +162,7 @@ extern const struct attribute_spec::exclusions
> attr_cold_hot_exclusions[] =
>
> /* Table of machine-independent attributes.
> For internal use (marking of built-ins) only. */
> -const attribute_spec d_langhook_common_attribute_table[] =
> +static const attribute_spec d_langhook_common_attributes[] =
> {
> ATTR_SPEC ("noreturn", 0, 0, true, false, false, false,
> handle_noreturn_attribute, attr_noreturn_exclusions),
> @@ -190,11 +190,15 @@ const attribute_spec
> d_langhook_common_attribute_table[] =
> handle_fnspec_attribute, NULL),
> ATTR_SPEC ("omp declare simd", 0, -1, true, false, false, false,
> handle_omp_declare_simd_attribute, NULL),
> - ATTR_SPEC (NULL, 0, 0, false, false, false, false, NULL, NULL),
> +};
> +
> +const scoped_attribute_specs d_langhook_common_attribute_table =
> +{
> + "gnu", d_langhook_common_attributes
> };
>
> /* Table of D language attributes exposed by `gcc.attribute' UDAs. */
> -const attribute_spec d_langhook_attribute_table[] =
> +static const attribute_spec d_langhook_gnu_attributes[] =
> {
> ATTR_SPEC ("noinline", 0, 0, true, false, false, false,
> d_handle_noinline_attribute, attr_noinline_exclusions),
> @@ -238,9 +242,12 @@ const attribute_spec d_langhook_attribute_table[] =
> d_handle_used_attribute, NULL),
> ATTR_SPEC ("visibility", 1, 1, false, false, false, false,
> d_handle_visibility_attribute, NULL),
> - ATTR_SPEC (NULL, 0, 0, false, false, false, false, NULL, NULL),
> };
>
> +const scoped_attribute_specs d_langhook_gnu_attribute_table =
> +{
> + "gnu", d_langhook_gnu_attributes
> +};
>
> /* Insert the type attribute ATTRNAME with value VALUE into TYPE.
> Returns a new variant of the original type declaration. */
> @@ -283,20 +290,14 @@ uda_attribute_p (const char *name)
>
> /* Search both our language, and target attribute tables.
> Common and format attributes are kept internal. */
> - for (const attribute_spec *p = d_langhook_attribute_table; p->name; p++)
> - {
> - if (get_identifier (p->name) == ident)
> - return true;
> - }
> + for (const attribute_spec &p : d_langhook_gnu_attributes)
> + if (get_identifier (p.name) == ident)
> + return true;
>
> - if (targetm.attribute_table)
> - {
> - for (const attribute_spec *p = targetm.attribute_table; p->name; p++)
> - {
> - if (get_identifier (p->name) == ident)
> - return true;
> - }
> - }
> + for (auto scoped_attributes : targetm.attribute_table)
> + for (const attribute_spec &p : scoped_attributes->attributes)
> + if (get_identifier (p.name) == ident)
> + return true;
>
> return false;
> }
> diff --git a/gcc/d/d-lang.cc b/gcc/d/d-lang.cc
> index 10b9000119e..90699f6d36b 100644
> --- a/gcc/d/d-lang.cc
> +++ b/gcc/d/d-lang.cc
> @@ -1919,6 +1919,12 @@ d_get_sarif_source_language (const char *)
> return "d";
> }
>
> +const scoped_attribute_specs *const d_langhook_attribute_table[] =
> +{
> + &d_langhook_gnu_attribute_table,
> + &d_langhook_common_attribute_table,
> +};
> +
> /* Definitions for our language-specific hooks. */
>
> #undef LANG_HOOKS_NAME
> @@ -1930,7 +1936,6 @@ d_get_sarif_source_language (const char *)
> #undef LANG_HOOKS_HANDLE_OPTION
> #undef LANG_HOOKS_POST_OPTIONS
> #undef LANG_HOOKS_PARSE_FILE
> -#undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
> #undef LANG_HOOKS_ATTRIBUTE_TABLE
> #undef LANG_HOOKS_GET_ALIAS_SET
> #undef LANG_HOOKS_TYPES_COMPATIBLE_P
> @@ -1963,7 +1968,6 @@ d_get_sarif_source_language (const char *)
> #define LANG_HOOKS_HANDLE_OPTION d_handle_option
> #define LANG_HOOKS_POST_OPTIONS d_post_options
> #define LANG_HOOKS_PARSE_FILE d_parse_file
> -#define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE d_langhook_common_attribute_table
> #define LANG_HOOKS_ATTRIBUTE_TABLE d_langhook_attribute_table
> #define LANG_HOOKS_GET_ALIAS_SET d_get_alias_set
> #define LANG_HOOKS_TYPES_COMPATIBLE_P d_types_compatible_p
> diff --git a/gcc/d/d-tree.h b/gcc/d/d-tree.h
> index b64a6fb46f9..021d3409b0a 100644
> --- a/gcc/d/d-tree.h
> +++ b/gcc/d/d-tree.h
> @@ -508,8 +508,8 @@ extern tree insert_decl_attribute (tree, const char *,
> tree = NULL_TREE);
> extern void apply_user_attributes (Dsymbol *, tree);
>
> /* In d-builtins.cc. */
> -extern const attribute_spec d_langhook_attribute_table[];
> -extern const attribute_spec d_langhook_common_attribute_table[];
> +extern const struct scoped_attribute_specs d_langhook_gnu_attribute_table;
> +extern const struct scoped_attribute_specs d_langhook_common_attribute_table;
> extern Type *build_frontend_type (tree);
>
> extern tree d_builtin_function (tree);
> --
> 2.25.1
>
>