-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Reopening or sharing LibC
types properly
#13472
Comments
Maybe we could always represent all lib structs as unnamed structs internally, so all lib structs with the same types in the same positions are compatible to each other? |
@BlobCodes My first thought went into that direction as well. But then this also poses some problems when types are legitimately represented in different ways. Maybe even that could potentially cause conflict in some cases, for example with flexible array members. So I'm wondering if it even makes much sense to validate the compatibility of different bindings to the same symbol. If the fun bindings have different names I think it might be fine to just accept anything and not require any validation for compatibility with other definitions. Overriding a fun inside the same lib is a somewhat different story about redefinitions of lib bindings (related: #7205, #8422). |
Some other examples:
|
I am starting to think there should be a public shard for the complete To ensure that nothing can ever require the same bindings multiple times, the filenames between the shard and stdlib should match exactly, so that, assuming the default The standard library then distributes only the subset of the bindings it needs. The files might look like: # shard: src/lib_c/sys/stat.cr
lib LibC
struct Stat
# ...
end
fun stat(pathname : Char*, statbuf : Stat*) : Int
fun fstat(fd : Int, statbuf : Stat*) : Int
fun lstat(pathname : Char*, statbuf : Stat*) : Int
end
# stdlib: src/lib_c/sys/stat.cr
lib LibC
struct Stat
# identical fields
end
fun stat(pathname : Char*, statbuf : Stat*) : Int
end
# stdlib: src/crystal/system/unix/file.cr
require "lib_c/sys/stat" The above omits handling of platform-specific directories for brevity. We should think about whether the current directory layout immediately under With Actually generating The tight coupling between the shard |
Having a shard for C bindings sounds good. Not sure how useful it would be though. In general, most common functions of C's standard library should be already covered in Crystal's standard library. The only use for C bindings I see are system-specific features that are not implemented in Crystal's stdlib. I'm not sure about integration with stdlib bindings. Why do they need to be the same namespace? The C bindings for stdlib could just be completely encapsulated in |
Do you think this outweighs the benefits of having compile-time checks for inconsistent signatures? For the record, in very special cases the same lib fun can indeed be "overloaded" even in the same library. The prime example is |
Probably? But mainly based on the observation that it's not very common that you would have different bindings to the same function. But in the rare case that this happens, I think the compiler should not throw stones in your way. Especially if the other definition is somewhere in code that you do not own and cannot change (such as stdlib internals, or 3rd party shards). |
I am thinking if, instead of allowing arbitrary lib fun redeclarations, we only allow them if they have the same arity and the corresponding parameter types produce roughly the same LLVM types, to strike a balance between soundness and flexibility. We define layout compatibility as something like below:
Then for LLVM codegen, Crystal should be able to pick any declaration as the primary one, and use the appropriate casts for calls via the remaining declarations. |
It is possible to bind to the same C symbol from two lib funs, as long as their signatures are compatible:
But as long as structs are involved, two different libraries must choose to declare the same bindings in their own libs:
or in the same lib:
Normally this shouldn't happen, but it does when:
PCRE
MARK
verb #11320, Add bindings/API for LibXML SAX parsing #9048)LibC
bindings, the other "shard" is the C runtime or Win32 bindings in the standard libraryMy main concern is that as soon as Windows support becomes official, there will surely be a huge influx of new bindings for Win32 APIs, for example via https://github.com/microsoft/win32metadata. There is no guarantee that our own bindings are compatible with those shards. So let's see what a shard might do to circumvent those limitations:
Reopening
LibC
directly:Using a different lib:
Both approaches encourage people to read
LibC
's definitions and require a lot of macros just to get it right. This cannot be fixed by simply moving the C runtime or Win32 APIs in ourLibC
to another private lib.The text was updated successfully, but these errors were encountered: