Support size pragma with generic type parameters #25403
Open
+441
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements deferred pragma expressions for the
sizepragma, and establishes infrastructure for deferred evaluation of expressions for other pragmas in generic types.Summary
DeferredPragmaExprtype and accessors for storing pragma expressions that reference generic type parameterssizepragma now accepts expressions likesizeof(T)that are evaluated during generic instantiationdefined(nimHasDeferredPragmas)to check for this featureUse Cases
Proper C++ Template Type Wrapping
The primary motivation: correctly wrapping C++ template types whose size depends on template parameters.
Platform-Independent FFI
Cross-platform bindings where size may vary by architecture:
Implementation
AST extensions (
astdef.nim,ast.nim):DeferredPragmaExprtype pairs pragma word with unevaluated expressiondeferredExprsImplfield onTTypeandTSymsfHasDeferredPragmas/tfHasDeferredPragmasflagssetDeferredExpr,clearDeferredExpr,deferredPragmasiteratorPragma processing (
pragmas.nim):containsUnresolvedIdent: Detects expressions with unresolved generic identifiersdeferOrEvaluate: Generic helper for defer-or-evaluate decisionwSizehandler to defer when expression contains generic paramsInstantiation (
semtypinst.nim):replaceIdentsWithTypes: Substitutes generic param names with concrete typesapplyDeferredPragma: Evaluates deferred pragmas after instantiationevaluateDeferredPragmas: Orchestrates evaluation of all deferred type pragmasIC serialization (
ic/enum2nif.nim):sfHasDeferredPragmasandtfHasDeferredPragmasflagsTests
tests/pragmas/tdeferred_size.nim- Size pragma withsizeof(T)expressionsPrior Art
PR #24204 addresses the same problem for the
sizepragma. Key differences in our approach:sizepragmasizepragma (with extensible infrastructure)deferredExprsImplfield with typed accessorstfHasMetaflagcontainsUnresolvedIdentThis PR uses a dedicated storage mechanism rather than mining symbol AST (which the #24204 author noted "seems dubious"). The
DeferredPragmaExprtype and accessor API make it straightforward to add deferred support to additional pragmas in follow-up PRs.Related Work
I have submtited a follow-up PRs to support
alignpragma with generic expressions (#25404).If the changeset is accepted, I also have branches ready for PR to support:
importc/importcpppragmas with generic expressionsheaderpragma with generic expressions