Skip to content

ExprParser: no dynamic_cast, instantiate node templates once - #65

Merged
tkittel merged 1 commit into
mctools:mainfrom
tkittel:exprparser-rtti
Nov 8, 2026
Merged

tkittel merged 1 commit into
mctools:mainfrom
tkittel:exprparser-rtti

Conversation

@tkittel

@tkittel tkittel commented Nov 8, 2026

Copy link
Copy Markdown
Member

Removes the remaining risk of the cross-module type information issue in
the expression parser.

The expression nodes are created in G4ExprParser and MCPLExprParser, but
type-checked in ExprParser. The header-only class templates ExprEntity
and ExprEntityConstantValue had their vtables and type information
emitted in each library using them, and a dynamic_cast between libraries
can fail on platforms which compare type information by address (e.g.
Intel macOS).

Changes:

  • No more dynamic_cast in ExprParser:
    • _ensure_type relies on returnType() alone. It is final in
      ExprEntity, so it already identifies the type.
    • A new virtual isConstantValue() replaces the cast which checked for
      constant value nodes during optimisation.
    • makeobj uses a static_cast for the upcast. Before, debug builds used
      a dynamic_cast.
  • The templates are explicitly instantiated for the three value types in
    the ExprParser library, with extern template declarations in
    ASTNode.hh. Their vtables and type information now only exist in
    libExprParser. G4ExprParser and MCPLExprParser no longer contain copies
    (checked with nm).

assisted-by: claude

The expression nodes are created in G4ExprParser and MCPLExprParser and
type-checked in ExprParser. The header-only class templates ExprEntity<T> and
ExprEntityConstantValue<T> had their vtables and type information emitted in
each library using them, and dynamic_cast between them across libraries can
fail on platforms comparing type information by address (e.g. Intel macOS).

* Remove all dynamic_cast's: returnType() (final in ExprEntity<T>) is enough
  to check the type, a new virtual isConstantValue() replaces the check for
  constant value nodes, and makeobj uses static_cast for the upcast.
* Explicitly instantiate the templates for the three value types in the
  ExprParser library (extern template declarations in ASTNode.hh), so their
  vtables and type information only exist there.

Assisted-by: Claude
@tkittel
tkittel merged commit c78e3da into mctools:main Nov 8, 2026
24 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant