Support size pragma with generic type parameters#25403
Closed
elijahr wants to merge 2 commits intonim-lang:develfrom
Closed
Support size pragma with generic type parameters#25403elijahr wants to merge 2 commits intonim-lang:develfrom
elijahr wants to merge 2 commits intonim-lang:develfrom
Conversation
Enables the size pragma to accept expressions involving type parameters
for generic imported types. The expression is deferred and evaluated
when the generic type is instantiated.
Example:
type
CppAtomic[T] {.importcpp, size: sizeof(T).} = object
Member
|
Sorry for this late feedback. The feature itself is fine but I don't like the fact that now every symbol/type takes up more memory (16 bytes if I'm not mistaken). We need a more general mechanism for symbol/type extensions, maybe based on a in-memory representation of NIF, maybe something else. |
Contributor
Author
|
Closing in favor of #25480 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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