diff options
author | Richard Sandiford <richard.sandiford@arm.com> | 2021-11-02 10:51:23 +0000 |
---|---|---|
committer | Richard Sandiford <richard.sandiford@arm.com> | 2021-11-02 10:51:23 +0000 |
commit | d5c6604f42034e36b149f899c5ddb93025a645b4 (patch) | |
tree | 3febb7400753ff374b3d69fb256da9e0a581a6a7 /gcc/cp | |
parent | d8fe39f417bebd34498ea60a3815c2f420116ecf (diff) | |
download | gcc-d5c6604f42034e36b149f899c5ddb93025a645b4.zip gcc-d5c6604f42034e36b149f899c5ddb93025a645b4.tar.gz gcc-d5c6604f42034e36b149f899c5ddb93025a645b4.tar.bz2 |
Add a simulate_record_decl lang hook
This patch adds a lang hook for defining a struct/RECORD_TYPE
“as if” it had appeared directly in the source code. It follows
the similar existing hook for enums.
It's the caller's responsibility to create the fields
(as FIELD_DECLs) but the hook's responsibility to create
and declare the associated RECORD_TYPE.
For now the hook is hard-coded to do the equivalent of:
typedef struct NAME { FIELDS } NAME;
but this could be controlled by an extra parameter if some callers
want a different behaviour in future.
The motivating use case is to allow the long list of struct
definitions in arm_neon.h to be provided by the compiler,
which in turn unblocks various arm_neon.h optimisations.
gcc/
* langhooks.h (lang_hooks_for_types::simulate_record_decl): New hook.
* langhooks-def.h (lhd_simulate_record_decl): Declare.
(LANG_HOOKS_SIMULATE_RECORD_DECL): Define.
(LANG_HOOKS_FOR_TYPES_INITIALIZER): Include it.
* langhooks.c (lhd_simulate_record_decl): New function.
gcc/c/
* c-tree.h (c_simulate_record_decl): Declare.
* c-objc-common.h (LANG_HOOKS_SIMULATE_RECORD_DECL): Override.
* c-decl.c (c_simulate_record_decl): New function.
gcc/cp/
* decl.c: Include langhooks-def.h.
(cxx_simulate_record_decl): New function.
* cp-objcp-common.h (cxx_simulate_record_decl): Declare.
(LANG_HOOKS_SIMULATE_RECORD_DECL): Override.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/cp-objcp-common.h | 4 | ||||
-rw-r--r-- | gcc/cp/decl.c | 37 |
2 files changed, 41 insertions, 0 deletions
diff --git a/gcc/cp/cp-objcp-common.h b/gcc/cp/cp-objcp-common.h index f1704aa..d585940 100644 --- a/gcc/cp/cp-objcp-common.h +++ b/gcc/cp/cp-objcp-common.h @@ -39,6 +39,8 @@ extern bool cp_handle_option (size_t, const char *, HOST_WIDE_INT, int, extern tree cxx_make_type_hook (tree_code); extern tree cxx_simulate_enum_decl (location_t, const char *, vec<string_int_pair> *); +extern tree cxx_simulate_record_decl (location_t, const char *, + array_slice<const tree>); /* Lang hooks that are shared between C++ and ObjC++ are defined here. Hooks specific to C++ or ObjC++ go in cp/cp-lang.c and objcp/objcp-lang.c, @@ -139,6 +141,8 @@ extern tree cxx_simulate_enum_decl (location_t, const char *, #define LANG_HOOKS_MAKE_TYPE cxx_make_type_hook #undef LANG_HOOKS_SIMULATE_ENUM_DECL #define LANG_HOOKS_SIMULATE_ENUM_DECL cxx_simulate_enum_decl +#undef LANG_HOOKS_SIMULATE_RECORD_DECL +#define LANG_HOOKS_SIMULATE_RECORD_DECL cxx_simulate_record_decl #undef LANG_HOOKS_TYPE_FOR_MODE #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode #undef LANG_HOOKS_TYPE_FOR_SIZE diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 8a45411..7c2a134 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -57,6 +57,7 @@ along with GCC; see the file COPYING3. If not see #include "omp-general.h" #include "omp-offload.h" /* For offload_vars. */ #include "opts.h" +#include "langhooks-def.h" /* For lhd_simulate_record_decl */ /* Possible cases of bad specifiers type used by bad_specifiers. */ enum bad_spec_place { @@ -16602,6 +16603,42 @@ cxx_simulate_enum_decl (location_t loc, const char *name, input_location = saved_loc; return enumtype; } + +/* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */ + +tree +cxx_simulate_record_decl (location_t loc, const char *name, + array_slice<const tree> fields) +{ + iloc_sentinel ils (loc); + + tree ident = get_identifier (name); + tree type = xref_tag (/*tag_code=*/record_type, ident); + if (type != error_mark_node + && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type))) + { + error ("redefinition of %q#T", type); + type = error_mark_node; + } + if (type == error_mark_node) + return lhd_simulate_record_decl (loc, name, fields); + + xref_basetypes (type, NULL_TREE); + type = begin_class_definition (type); + if (type == error_mark_node) + return lhd_simulate_record_decl (loc, name, fields); + + for (tree field : fields) + finish_member_declaration (field); + + type = finish_struct (type, NULL_TREE); + + tree decl = build_decl (loc, TYPE_DECL, ident, type); + set_underlying_type (decl); + lang_hooks.decls.pushdecl (decl); + + return type; +} /* We're defining DECL. Make sure that its type is OK. */ |