GCC Newspaper
JULY 29, 2026
gcc Proposed

Proposal for tagged_by attribute to describe union discriminators

Introduce tagged_by attribute for discriminated unions, impacting layout and debug info.

Eric Botcazou proposes the ‘tagged_by’ attribute to describe union discriminators in GCC. This attribute is used for driving layout and debug info generation, and it differs from UNION_TYPE by enabling runtime checks and having a dynamic size based on the active member. The predicate for this attribute references the tag via PLACEHOLDER_EXPRs.

In the Thread 1 participant
  1. Eric Botcazou <botcazou@adacore.com> proposer

    Explains that QUAL_UNION_TYPE, unlike UNION_TYPE, has dynamic size and is used for layout and debug info generation. The 'tagged_by' attribute's predicate references the discriminant via PLACEHOLDER_EXPRs.

In Details

This RFC introduces a new GCC attribute, likely __attribute__((tagged_by(...))), to annotate discriminated unions. This mechanism, used in Ada, allows the compiler to track which member of a union is active based on a separate discriminant field. This affects code generation for memory layout (stor-layout.cc) and debugging information (dwarf2out.cc), with QUAL_UNION_TYPE implying dynamic sizing.

For Context
discriminated union
A union where a separate field, the discriminant, indicates which member of the union is currently active. This allows for type safety and specific access patterns.
QUAL_UNION_TYPE
A GCC internal type representing a discriminated union with dynamic size, whose active member can be determined at runtime. This contrasts with UNION_TYPE, which has a fixed size.
stor-layout.cc
A GCC source file responsible for determining the memory layout of language constructs, including unions and their members.
dwarf2out.cc
A GCC source file involved in generating DWARF debugging information, which describes the compiled program's structure to debuggers.
PLACEHOLDER_EXPRs
An internal GCC expression type used to represent references to declarations that might not yet be fully resolved or that are used in specific contexts like attribute qualifiers.
Filed Under: gccattributeuniontype systemdebug info