GCC Newspaper
JULY 29, 2026
gcc Proposed

Gradual 'tree' typing and C++

Discussion on C++ template syntax for GCC's internal 'tree' types and its impact on code readability.

This discussion debates the merits of using C++ templates for GCC’s internal ‘tree’ type, specifically proposing syntax like foo(tree<one_of<FUNCTION_DECL, PLUS_EXPR>> x). While proponents see it as an improvement over assertions in function bodies, others argue it makes functions appear ‘weird’ and can lead to writing bad code. The core disagreement lies in whether explicit static typing of internal compiler data structures enhances or detracts from code clarity and design.

In the Thread 2 participants
  1. Joseph Myers <josmyers@redhat.com> proposer

    Agrees that most code expecting 'tree' nodes likely has a specific type and supports static typing for clarity.

    “So I think we should aim for static typing in the cases where we expect that the code is already quite close to having exactly one static type for each place using "tree"”
  2. Martin Uecker <martinu@google.com> reviewer

    Critiques the proposed gradual typing syntax, arguing that functions working on disparate 'tree' codes are inherently suspect and that explicit types improve readability.

    “foo(tree< one_of<FUNCTION_DECL, PLUS_EXPR> > x) May be amazing technology, but just tells me the function should not exist in this form. So it only helps writing bad code.”

Technical Tradeoffs

  • Using templates for 'tree' typing increases static analysis benefits and potentially catches errors earlier.
  • Overuse of complex template syntax can decrease readability for developers unfamiliar with advanced C++ features.
  • Explicitly typing 'tree' parameters might reveal poorly designed internal APIs that accept too wide a range of node types.

In Details

The conversation revolves around applying C++11 template metaprogramming features, specifically variadic templates and type traits, to GCC's internal compiler representation ('tree'). The proposal suggests using syntax like tree<ExpectedCode1, ExpectedCode2> to statically type function parameters that currently accept a generic tree type, replacing runtime assertions. The debate centers on whether this improves code clarity and maintainability or obscures the underlying AST/GIMPLE structure and signals poor API design.

For Context
tree
GCC's internal representation for abstract syntax trees (AST) and intermediate representation (IR) during compilation. It represents code structure and constructs.
tree codes
Specific identifiers within GCC's 'tree' structure that denote the type of construct, such as FUNCTION_DECL for function declarations or PLUS_EXPR for addition operations.
GIMPLE
A three-address intermediate representation used by GCC, derived from the 'tree' representation, that simplifies optimization passes.
Filed Under: compilergccC++templatesinternal representation