From d5c6604f42034e36b149f899c5ddb93025a645b4 Mon Sep 17 00:00:00 2001 From: Richard Sandiford Date: Tue, 2 Nov 2021 10:51:23 +0000 Subject: Add a simulate_record_decl lang hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- gcc/c/c-decl.c | 30 ++++++++++++++++++++++++++++++ gcc/c/c-objc-common.h | 2 ++ gcc/c/c-tree.h | 2 ++ 3 files changed, 34 insertions(+) (limited to 'gcc/c') diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 771efa3..186fa16 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -9436,6 +9436,36 @@ c_simulate_enum_decl (location_t loc, const char *name, input_location = saved_loc; return enumtype; } + +/* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */ + +tree +c_simulate_record_decl (location_t loc, const char *name, + array_slice fields) +{ + location_t saved_loc = input_location; + input_location = loc; + + class c_struct_parse_info *struct_info; + tree ident = get_identifier (name); + tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info); + + for (unsigned int i = 0; i < fields.size (); ++i) + { + DECL_FIELD_CONTEXT (fields[i]) = type; + if (i > 0) + DECL_CHAIN (fields[i - 1]) = fields[i]; + } + + finish_struct (loc, type, fields[0], NULL_TREE, struct_info); + + tree decl = build_decl (loc, TYPE_DECL, ident, type); + set_underlying_type (decl); + lang_hooks.decls.pushdecl (decl); + + input_location = saved_loc; + return type; +} /* Create the FUNCTION_DECL for a function definition. DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of diff --git a/gcc/c/c-objc-common.h b/gcc/c/c-objc-common.h index 7d35a06..f4e8271 100644 --- a/gcc/c/c-objc-common.h +++ b/gcc/c/c-objc-common.h @@ -81,6 +81,8 @@ along with GCC; see the file COPYING3. If not see #undef LANG_HOOKS_SIMULATE_ENUM_DECL #define LANG_HOOKS_SIMULATE_ENUM_DECL c_simulate_enum_decl +#undef LANG_HOOKS_SIMULATE_RECORD_DECL +#define LANG_HOOKS_SIMULATE_RECORD_DECL c_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/c/c-tree.h b/gcc/c/c-tree.h index a046c6b..f1dbbd5 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -598,6 +598,8 @@ extern tree finish_struct (location_t, tree, tree, tree, class c_struct_parse_info *); extern tree c_simulate_enum_decl (location_t, const char *, vec *); +extern tree c_simulate_record_decl (location_t, const char *, + array_slice); extern struct c_arg_info *build_arg_info (void); extern struct c_arg_info *get_parm_info (bool, tree); extern tree grokfield (location_t, struct c_declarator *, -- cgit v1.1