Hi!
As mentioned on the PR, we ICE on the following testcase and if members_of
isn't called on a class with e.g. typedef struct { int d; } D;, we don't
handle it correctly, e.g. we say ^^C::D is not an type alias or for
members_of in a namespace that there aren't two entities, the struct itself
and the type alias for it.
This is because name_unnamed_type handles the naming of an unnamed type
through typedef for linkage purposes (where we originally have
a TYPE_DECL with IDENTIFIER_ANON_P DECL_NAME for the type) by replacing
all occurrences of TYPE_NAME on the type from the old TYPE_DECL to the new
TYPE_DECL with the user provided name.
The ICE for members_of (^^C, uctx) is then because we see two TYPE_DECLs
(one with IDENTIFIER_ANON_P, one with user name) with the same TREE_TYPE
and enter the same thing twice into what we want to return and ICE in the
comparison routine. Anyway, for is_type_alias purposes, there is no
is_typedef_decl and can't be because the same TYPE_DECL is used as TYPE_NAME
of both the type proper and its alias. Without reflection we didn't care
about the difference.
So, the following patch changes name_unnamed_type to do things differently,
but only for -freflection, because 1) I don't want to break stuff late in
stage4 2) without reflection we don't really need it and don't need to
pay the extra memory cost by having another type which is the type alias.
The change is that instead of
TYPE_DECL .anon_NN
| TREE_TYPE
v
type <----------+
| TYPE_NAME |
v |
TYPE_DECL D |
| TREE_TYPE |
+-------------+
where for class context both TYPE_DECLs are in TYPE_FIELDS and for
namespace context only the latter one is (as pushdecl ignores the
IDENTIFIER_ANON_P one) we have
TYPE_DECL D TYPE_DECL D --- DECL_ORIGINAL_TYPE
| TREE_TYPE | TREE_TYPE |
v v |
type variant_type |
^-------------------------------+
which is except for the same DECL_NAME on both TYPE_DECLs exactly what
is used for typedef struct D_ { int d; } D;
Various spots have been testing for the typedef name for linkage purposes
cases and were using tests like:
OVERLOAD_TYPE_P (TREE_TYPE (value))
&& value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value)))
So that this can be tested, this patch introduces a new decl_flag on
the TYPE_DECLs and marks for -freflection both of these TYPE_DECLs
(and for -fno-reflection the one without IDENTIFIER_ANON_P name).
It is easy to differentiate between the two, the first one is also
DECL_IMPLICIT_TYPEDEF_P, the latter is not (and on the other side
has DECL_ORIGINAL_TYPE non-NULL).
For name lookup in namespaces, nothing special needs to be done,
because the originally IDENTIFIER_ANON_P TYPE_DECL wasn't added
to the bindings, at block scope I had to deal with it in pop_local_binding
because it was unhappy that it got renamed. And finally for class
scopes, we need to arrange for the latter TYPE_DECL to be found, but
currently it is the second one. The patch currently skips the first one for
name lookup in fields_linear_search and arranges for count_class_fields
and member_vec_append_class_fields to also ignore the first one. Wonder if
the latter two shouldn't also ignore any other IDENTIFIER_ANON_P TYPE_FIELDS
chain decls, or do we ever perform name lookup for the anon identifiers?
Another option for fields_linear_search would be try to swap the order of
the two TYPE_DECLs in TYPE_FIELDS chain somewhere in grokfield.
Anyway, the changes result in minor emitted DWARF changes, say for
g++.dg/debug/dwarf2/typedef1.C without -freflection there is
.uleb128 0x4 # (DIE (0x46) DW_TAG_enumeration_type)
.long .LASF6 # DW_AT_name: "typedef foo<1>::type type"
.byte 0x7 # DW_AT_encoding
.byte 0x4 # DW_AT_byte_size
.long 0x70 # DW_AT_type
.byte 0x1 # DW_AT_decl_file (typedef1.C)
.byte 0x18 # DW_AT_decl_line
.byte 0x12 # DW_AT_decl_column
.long .LASF7 # DW_AT_MIPS_linkage_name: "N3fooILj1EE4typeE"
...
and no typedef, while with -freflection there is
.uleb128 0x3 # (DIE (0x3a) DW_TAG_enumeration_type)
.long .LASF5 # DW_AT_name: "type"
.byte 0x7 # DW_AT_encoding
.byte 0x4 # DW_AT_byte_size
.long 0x6c # DW_AT_type
.byte 0x1 # DW_AT_decl_file (typedef1.C)
.byte 0x18 # DW_AT_decl_line
.byte 0x12 # DW_AT_decl_column
...
.uleb128 0x5 # (DIE (0x57) DW_TAG_typedef)
.long .LASF5 # DW_AT_name: "type"
.byte 0x1 # DW_AT_decl_file (typedef1.C)
.byte 0x18 # DW_AT_decl_line
.byte 0x1d # DW_AT_decl_column
.long 0x3a # DW_AT_type
so, different DW_AT_name on the DW_TAG_enumeration_type, missing
DW_AT_MIPS_linkage_name and an extra DW_TAG_typedef. While in theory
I could work harder to hide that detail, I actually think it is a good
thing to have it the latter way because it represents more exactly
what is going on.
Another slight change is different locations in some diagnostics
on g++.dg/lto/odr-3 test (location of the unnamed struct vs. locations
of the typedef name given to it without -freflection), and a module
issue which Nathan has some WIP patch for in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123810#c11
In any case, none of those differences show up in normal testsuite runs
currently (as those tests aren't compiled with -freflection), if/when
-freflection becomes the default for -std=c++26 we can deal with the
DWARF one as well as different locations in odr-3 and for modules I was
hoping it could be handled incrementally. I'm not even sure what should
happen if one TU has struct D { int d; }; and another one has
typedef struct { int d; } D;, shall that be some kind of error? Though
right now typedef struct { int d; } D; in both results in an error too
and that definitely needs to be handled.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
BTW, also wonder about attributes/annotations on the typedef, say for
typedef struct { int d; } D [[deprecated]];
or
typedef struct [[=3, =4]] { int d; } D [[=1, =2]];
We were running cplus_decl_attributes on the D TYPE_DECL (which was
and without -freflection still is the TYPE_NAME of the type) with
ATTR_FLAG_TYPE_IN_PLACE flags (not sure what exactly it does when we
actually don't call it on the type but the TYPE_DECL), now with -freflection
it is called on the second D TYPE_DECL (i.e. the one later on for the
set_underlying_type but set_underlying_type is called only later on).
eval_annotations_of right now does maybe_strip_typedefs and for types
uses TYPE_ATTRIBUTES, so it will never return annotations on type aliases,
guess we'll need to figure out what exactly it should do for cases like
that.
2026-02-13 Jakub Jelinek <[email protected]>
PR c++/123810
* cp-tree.h (TYPE_DECL_FOR_LINKAGE_PURPOSES_P): Define.
(TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P): Likewise.
(TYPE_WAS_UNNAMED): Also check TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P.
* decl.cc (start_decl): Use TYPE_DECL_FOR_LINKAGE_PURPOSES_P.
(maybe_diagnose_non_c_class_typedef_for_l): If t == type, use
DECL_SOURCE_LOCATION (orig) instead of
DECL_SOURCE_LOCATION (TYPE_NAME (t)).
(name_unnamed_type): Set TYPE_DECL_FOR_LINKAGE_PURPOSES_P
on decl. For -freflection don't change TYPE_NAME from
orig to decl, but instead change DECL_NAME (orig) to
DECL_NAME (decl) and set TYPE_DECL_FOR_LINKAGE_PURPOSES_P on
orig too.
* decl2.cc (grokfield): Use TYPE_DECL_FOR_LINKAGE_PURPOSES_P.
* name-lookup.cc (fields_linear_search): Ignore
TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P decls.
(count_class_fields): Likewise.
(member_vec_append_class_fields): Likewise.
(pop_local_binding): Likewise.
* reflect.cc (namespace_members_of): For TYPE_DECL with
TYPE_DECL_FOR_LINKAGE_PURPOSES_P set also append
reflection of strip_typedefs (m).
* class.cc (find_flexarrays): Handle TYPE_DECLs with
TYPE_DECL_FOR_LINKAGE_PURPOSES_P like the ones with IDENTIFIER_ANON_P
name.
* g++.dg/reflect/members_of8.C: New test.
* g++.dg/cpp2a/typedef1.C: Expect one message on a different line.
--- gcc/cp/cp-tree.h.jj 2026-02-12 12:33:55.510613450 +0100
+++ gcc/cp/cp-tree.h 2026-02-12 14:40:12.649547905 +0100
@@ -590,6 +590,7 @@ extern GTY(()) tree cp_global_trees[CPTI
7: DECL_THUNK_P (in a member FUNCTION_DECL)
DECL_NORMAL_CAPTURE_P (in FIELD_DECL)
DECL_DECLARED_CONSTINIT_P (in VAR_DECL)
+ TYPE_DECL_FOR_LINKAGE_PURPOSES_P (in TYPE_DECL)
8: DECL_DECLARED_CONSTEXPR_P (in VAR_DECL, FUNCTION_DECL)
Usage of language-independent fields in a language-dependent manner:
@@ -3999,6 +4000,20 @@ struct GTY(()) lang_decl {
&& TREE_CODE (TYPE_NAME (NODE)) == TYPE_DECL \
&& TYPE_DECL_ALIAS_P (TYPE_NAME (NODE)))
+/* Nonzero for typedef name for linkage purposes. For -freflection
+ set also on the originally unnamed TYPE_DECL. */
+#define TYPE_DECL_FOR_LINKAGE_PURPOSES_P(NODE) \
+ DECL_LANG_FLAG_7 (TYPE_DECL_CHECK (NODE))
+
+/* Nonzero for TYPE_DECL originally with IDENTIFIER_ANON_P DECL_NAME
+ later on named by a typedef name for linkage purposes in the
+ -freflection case (otherwise the TYPE_DECL keeps IDENTIFIER_ANON_P
+ DECL_NAME). */
+#define TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P(NODE) \
+ (TREE_CODE (NODE) == TYPE_DECL \
+ && TYPE_DECL_FOR_LINKAGE_PURPOSES_P (NODE) \
+ && DECL_IMPLICIT_TYPEDEF_P (NODE))
+
/* If non-NULL for a VAR_DECL, FUNCTION_DECL, TYPE_DECL, TEMPLATE_DECL,
or CONCEPT_DECL, the entity is either a template specialization (if
DECL_USE_TEMPLATE is nonzero) or the abstract instance of the
@@ -5365,10 +5380,13 @@ get_vec_init_expr (tree t)
/* True if TYPE is an unnamed structured type with a typedef for
linkage purposes. In that case TYPE_NAME and TYPE_STUB_DECL of the
- MAIN-VARIANT are different. */
+ MAIN-VARIANT are different or TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P
+ is true for the TYPE_NAME. */
#define TYPE_WAS_UNNAMED(NODE) \
(TYPE_NAME (TYPE_MAIN_VARIANT (NODE)) \
- != TYPE_STUB_DECL (TYPE_MAIN_VARIANT (NODE)))
+ != TYPE_STUB_DECL (TYPE_MAIN_VARIANT (NODE)) \
+ || TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P \
+ (TYPE_NAME (TYPE_MAIN_VARIANT (NODE))))
/* C++: all of these are overloaded! These apply only to TYPE_DECLs. */
--- gcc/cp/decl.cc.jj 2026-02-12 10:32:24.647331121 +0100
+++ gcc/cp/decl.cc 2026-02-12 14:21:23.854541467 +0100
@@ -6585,8 +6585,7 @@ start_decl (const cp_declarator *declara
/* If this is a typedef that names the class for linkage purposes
(7.1.3p8), apply any attributes directly to the type. */
if (TREE_CODE (decl) == TYPE_DECL
- && OVERLOAD_TYPE_P (TREE_TYPE (decl))
- && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
+ && TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl))
flags = ATTR_FLAG_TYPE_IN_PLACE;
else
flags = 0;
@@ -13698,7 +13697,8 @@ maybe_diagnose_non_c_class_typedef_for_l
{
auto_diagnostic_group d;
if (diagnose_non_c_class_typedef_for_linkage (type, orig))
- inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)),
+ inform (type == t ? DECL_SOURCE_LOCATION (orig)
+ : DECL_SOURCE_LOCATION (TYPE_NAME (t)),
"type is not C-compatible because it has a base class");
return true;
}
@@ -13764,12 +13764,22 @@ name_unnamed_type (tree type, tree decl)
gcc_assert (TYPE_UNNAMED_P (type)
|| enum_with_enumerator_for_linkage_p (type));
- /* Replace the anonymous decl with the real decl. Be careful not to
- rename other typedefs (such as the self-reference) of type. */
tree orig = TYPE_NAME (type);
- for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
- if (TYPE_NAME (t) == orig)
- TYPE_NAME (t) = decl;
+ if (flag_reflection)
+ {
+ /* For -freflection for typedef struct { ... } S; ^^S needs to be
+ a reflection of a type alias. So, TREE_TYPE (DECL) can't be
+ TYPE. Instead of what we do below, override DECL_NAME (orig). */
+ DECL_NAME (orig) = DECL_NAME (decl);
+ TYPE_DECL_FOR_LINKAGE_PURPOSES_P (orig) = 1;
+ }
+ else
+ /* Replace the anonymous decl with the real decl. Be careful not to
+ rename other typedefs (such as the self-reference) of type. */
+ for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
+ if (TYPE_NAME (t) == orig)
+ TYPE_NAME (t) = decl;
+ TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl) = 1;
/* If this is a typedef within a template class, the nested
type is a (non-primary) template. The name for the
--- gcc/cp/decl2.cc.jj 2026-02-12 10:32:24.656330969 +0100
+++ gcc/cp/decl2.cc 2026-02-12 12:39:29.271487328 +0100
@@ -1303,8 +1303,7 @@ grokfield (const cp_declarator *declarat
/* If this is a typedef that names the class for linkage purposes
(7.1.3p8), apply any attributes directly to the type. */
- if (OVERLOAD_TYPE_P (TREE_TYPE (value))
- && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))))
+ if (TYPE_DECL_FOR_LINKAGE_PURPOSES_P (value))
attrflags = ATTR_FLAG_TYPE_IN_PLACE;
cplus_decl_attributes (&value, attrlist, attrflags);
--- gcc/cp/name-lookup.cc.jj 2026-02-12 10:32:24.663330851 +0100
+++ gcc/cp/name-lookup.cc 2026-02-12 13:38:47.498316544 +0100
@@ -1893,6 +1893,11 @@ fields_linear_search (tree klass, tree n
continue;
}
+ if (TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P (decl))
+ /* Ignore TYPE_DECLs formerly unnamed, there is a TYPE_DECL
+ for linkage purposes later on in the chain. */
+ continue;
+
if (DECL_DECLARES_FUNCTION_P (decl))
/* Functions are found separately. */
continue;
@@ -2369,7 +2374,13 @@ count_class_fields (tree klass)
&& ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
n_fields += count_class_fields (TREE_TYPE (fields));
else if (DECL_NAME (fields))
- n_fields += 1;
+ {
+ if (TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P (fields))
+ /* Ignore TYPE_DECLs formerly unnamed, there is a TYPE_DECL
+ for linkage purposes later on in the chain. */
+ continue;
+ n_fields += 1;
+ }
return n_fields;
}
@@ -2393,6 +2404,10 @@ member_vec_append_class_fields (vec<tree
if (TREE_CODE (field) == USING_DECL
&& IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
field = ovl_make (conv_op_marker, field);
+ else if (TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P (field))
+ /* Ignore TYPE_DECLs formerly unnamed, there is a TYPE_DECL
+ for linkage purposes later on in the chain. */
+ continue;
member_vec->quick_push (field);
}
}
@@ -2724,7 +2739,10 @@ pop_local_binding (tree id, tree decl)
binding->value = NULL_TREE;
else if (binding->type == decl)
binding->type = NULL_TREE;
- else
+ /* Ignore TYPE_DECL renamed in name_unnamed_type for -freflection,
+ as there will be another TYPE_DECL with the same DECL_NAME for
+ the typedef. */
+ else if (!TYPE_DECL_NAMED_FOR_LINKAGE_PURPOSES_P (decl))
{
/* Name-independent variable was found after at least one declaration
with the same name. */
--- gcc/cp/reflect.cc.jj 2026-02-12 10:32:24.668330767 +0100
+++ gcc/cp/reflect.cc 2026-02-12 12:39:29.272806505 +0100
@@ -6528,6 +6528,11 @@ namespace_members_of (location_t loc, tr
so don't bother calling it here. */
CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
get_reflection_raw (loc, m));
+ if (TREE_CODE (b) == TYPE_DECL
+ && TYPE_DECL_FOR_LINKAGE_PURPOSES_P (b))
+ CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
+ get_reflection_raw (loc,
+ strip_typedefs (m)));
}
}
delete seen;
--- gcc/cp/class.cc.jj 2026-02-12 10:32:29.075256573 +0100
+++ gcc/cp/class.cc 2026-02-12 14:11:58.067039044 +0100
@@ -7608,7 +7608,8 @@ find_flexarrays (tree t, flexmems_t *fme
if (TREE_CODE (fld) == TYPE_DECL
&& DECL_IMPLICIT_TYPEDEF_P (fld)
&& CLASS_TYPE_P (TREE_TYPE (fld))
- && IDENTIFIER_ANON_P (DECL_NAME (fld)))
+ && (IDENTIFIER_ANON_P (DECL_NAME (fld))
+ || TYPE_DECL_FOR_LINKAGE_PURPOSES_P (fld)))
{
/* Check the nested unnamed type referenced via a typedef
independently of FMEM (since it's not a data member of
--- gcc/testsuite/g++.dg/reflect/members_of8.C.jj 2026-02-12
12:39:29.273275302 +0100
+++ gcc/testsuite/g++.dg/reflect/members_of8.C 2026-02-12 12:39:29.273275302
+0100
@@ -0,0 +1,56 @@
+// PR c++/123810
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+#include <meta>
+
+namespace A {
+ typedef struct { int b; } B;
+}
+struct C {
+ typedef struct { int d; } D;
+};
+struct F {
+ typedef struct { int g; } G;
+ typedef struct { int h; } H;
+ typedef struct { int i; } I;
+ typedef struct { int j; } J;
+ typedef struct { int k; } K;
+ typedef struct { int l; } L;
+ typedef struct { int m; } M;
+ typedef struct { int n; } N;
+};
+void
+foo ()
+{
+ typedef struct { int e; } E;
+ static_assert (is_type_alias (^^E));
+}
+
+static_assert (is_type_alias (^^A::B));
+static_assert (is_type_alias (^^C::D));
+static_assert (is_type_alias (^^F::G));
+static_assert (is_type_alias (^^F::N));
+constexpr auto uctx = std::meta::access_context::unchecked ();
+static_assert (members_of (^^C, uctx)[0] == dealias (^^C::D));
+static_assert (members_of (^^C, uctx)[1] == ^^C::D);
+static_assert (members_of (^^F, uctx)[0] == dealias (^^F::G));
+static_assert (members_of (^^F, uctx)[1] == ^^F::G);
+static_assert (members_of (^^F, uctx)[2] == dealias (^^F::H));
+static_assert (members_of (^^F, uctx)[3] == ^^F::H);
+static_assert (members_of (^^F, uctx)[4] == dealias (^^F::I));
+static_assert (members_of (^^F, uctx)[5] == ^^F::I);
+static_assert (members_of (^^F, uctx)[6] == dealias (^^F::J));
+static_assert (members_of (^^F, uctx)[7] == ^^F::J);
+static_assert (members_of (^^F, uctx)[8] == dealias (^^F::K));
+static_assert (members_of (^^F, uctx)[9] == ^^F::K);
+static_assert (members_of (^^F, uctx)[10] == dealias (^^F::L));
+static_assert (members_of (^^F, uctx)[11] == ^^F::L);
+static_assert (members_of (^^F, uctx)[12] == dealias (^^F::M));
+static_assert (members_of (^^F, uctx)[13] == ^^F::M);
+static_assert (members_of (^^F, uctx)[14] == dealias (^^F::N));
+static_assert (members_of (^^F, uctx)[15] == ^^F::N);
+static_assert ((members_of (^^A, uctx)[0] == dealias (^^A::B)
+ && members_of (^^A, uctx)[1] == ^^A::B)
+ || ((members_of (^^A, uctx)[0] == ^^A::B)
+ && members_of (^^A, uctx)[1] == dealias (^^A::B)));
--- gcc/testsuite/g++.dg/cpp2a/typedef1.C.jj 2025-08-15 22:36:04.669612779
+0200
+++ gcc/testsuite/g++.dg/cpp2a/typedef1.C 2026-02-12 14:44:27.499255018
+0100
@@ -33,8 +33,8 @@ typedef struct { // {
dg-message "un
static int a; // { dg-error
"static data member '<unnamed struct>::a' in unnamed class" }
} B;
typedef struct : public A { // { dg-error
"anonymous non-C-compatible type given name for linkage purposes by 'typedef'
declaration" }
- int a;
-} C; // { dg-message "type
is not C-compatible because it has a base class" }
+ int a; // { dg-message "type
is not C-compatible because it has a base class" "" { target *-*-* } .-1 }
+} C;
#if __cplusplus >= 201103L
typedef struct { // { dg-error
"anonymous non-C-compatible type given name for linkage purposes by 'typedef'
declaration" "" { target c++11 } }
int b = 42; // { dg-message "type
is not C-compatible because 'D::b' has default member initializer" "" { target
c++11 } }
Jakub