-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importc types: CT sizeof, $
, {.sizeof.}, const CVAR {.importc.}: int
#205
Comments
$
, {.sizeof.}, NIM_STATIC_ASSERT$
, {.sizeof.}, const CVAR {.importc.}: int
You propose 5 things to solve which problem? |
updated top section with a summary |
Proposal 1: we already have pragma |
The size of an object is pretty much worthless for sizeof computations, if you don't also know the alignment. |
Proposal 6: Introduce a pragma |
how is that different from proposal 4 besides the name change
what about |
well currently Then I don't like the reuse of the name I don't like neither type stat {.importc: "stat", header = "<stat.h>", sizeof.} =
foo1: cint
_: array[4, byte]
foo2: cstring
_: array[16, byte]
No. If those fields should be inspectable, give them the correct name of the C declaration. Then it can be used properly. After all we already have an export marker, we don't need yet another level of exportedness.
You must add static asserts for all field offsets as well and the object alignment as well. Really you don't want a single size/alignment computation to be wrong and stay undetected. Last but not least. The hidden fields should not be listed with the fields iterator, nor should they be part of the ast that is returned from
That part is underspecified. What is |
/cc @cooldome
indeed, Also, /cc @krux02
That's basically what proposal 1 is about; allow
same exact same thing would apply with
you can't for ABI compatibility reasons. But you should still be able to access the values (implicitly casted to specified type) transparently: the cgen inserts something like
yes, and it's cheap to do.
s2 is any expression that can be computed at CT and is of type const N = when defined(osx): 40 else: 48 # just an example; in practice we can automate
type cvMat {.importc, sizeof: N, alignof: cint.alignof.} = object
rows*, cols*: cint
data*: pointer
# other fields that are skipped for various reasons (eg not needed, laziness, or ABI issues etc) Now cvMat.{sizeof,alignof} can be used at CT, which in turns mean any type involving it can be used at CT. Note also that my original idea was to introduce |
No, it would not. All fields with their true names will be visualized correctly in gdb and other debuggers, they use the debug information that comes from the C(++) compiler, with correct field names. |
I'm talking about getting/setting those fields from nim code (eg: serialization), not from a debugger. Of course a debugger has access to those with debug info. |
Give them the correct name of the C declaration, then you can access those members from Nim properly. |
see nim-lang/Nim#13926 which implements proposal 1 |
proposal 1 would allow CT sizeof for specified importc types
proposal 2 is just cleanup (related to proposal 1)
proposal 4 would fix
$
andrepr
for importc typesproposal 5 would avoid having to hardcode macro constants defined in C headers (which is brittle and not cross platform), and make them available at CT in a reliable way
proposal 3 is just sugar
proposal 1: introduce {.sizeof.} for importc types
{.sizeof.}
pragma for importc types, with following meaning:Note: there should be zero additional code to compute CT sizeof for importc types, it's just the regular CT sizeof except that we remove a CT error depending on presence of
{.sizeof.}
we add a CT check in codegen to check for errors (eg if some header is updated and fields are added), using
NIM_STATIC_ASSERT
C/C++ macro introduced in fix some codegen bugs: NIM_BOOL, NIM_STATIC_ASSERT, --passc:-std=... (etc) Nim#13798:codegen inserts
NIM_STATIC_ASSERT(sizeof(Foo1) == computedSizeofFoo1)
right after the header where it's define (or right after its declaration if no header).proposal 2: deprecate incompleteStruct
[EDIT: this needs to be double checked]
deprecate
{.incompleteStruct.}
(introduced in 2012), it's the wrong default, it's not observed (AFAIK) as evidenced by face only few structs are annotated with it. It's also un-safe as a lack of annotation would mean a struct is complete, which is often wrong.proposal 3: {.importc.} for ref/ptr object types
introduce
type Foo {.importc.} = ref object
, with the meaning that theimportc
applies to the object, not the ref, to avoid the common boilerplate:where
TFoo
doesn't need to be declared.ditto with
ptr
.I don't think there's any use case otherwise for
{.importc.}
applying to theref
orptr
directly, since a pointer is just a pointer.proposal 4:
{.hidden.}
for importc fields that shouldn't be referenced by name in codegen(previous version of this proposal called it
{.importc:"?".}
)this is an alternative approach to nim-lang/Nim#13783 for cases where some field names (eg
pad
,reserved
etc) are un-reliable because they're OS-specific, and not part of stable ABI. eg:$
implementation can query whether a field has a pragma{.hidden.}
(and either skip it or refer to it in codegen viaoffsetof
)repr
should probably not skip it but useoffsetof
to dump its valueproposal 5:
const CVAR {.importc.}: int
the idea is to replace hardcoded values obtained from reading header files (which may be unreliable, not cross platform, and even wrong if user passes some other
--passc
option affecting some symbols) by simply calling preprocessor:by:
I've implemented this in a POC and it seems to work, but no PR yet.
It is efficient since I'm caching header files and preprocessor output.
EDIT: yet another example use case: nim-lang/Nim#16459 (comment)
Note: regardless of whether we use
const CVAR {.importc.}: int
, codegen could validate the value we get viaNIM_STATIC_ASSERT
Note: this doesn't require libffi
The text was updated successfully, but these errors were encountered: