Skip to content
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

make @cImport take comptime []u8 instead of an expression #9332

Closed
ontes opened this issue Jul 8, 2021 · 5 comments
Closed

make @cImport take comptime []u8 instead of an expression #9332

ontes opened this issue Jul 8, 2021 · 5 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@ontes
Copy link

ontes commented Jul 8, 2021

I'm learning Zig and I noticed that @cImport takes some arbitrary type expression, which seems quite confusing for me.
As an expansion of #2070 I propose that @cImport should take comptime []u8 c code as an argument.

This will produce to syntax like this:

// one line syntax
const c1 = @cImport("#include <header1.h>");
// multi-line string
const c2 = @cImport(
    \\#define INCLUDE_PROTOTYPES
    \\#include <header2.h>
);
// zig embedding
const c3 = @cImport(@embedFile("header3.h"));
// arbitrary code
const c4 = @cImport(
    \\extern int myFunction(void);
);

@cInclude, @cDefine and @cUndef could be removed.

@Vexu Vexu added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jul 8, 2021
@Vexu Vexu added this to the 0.9.0 milestone Jul 8, 2021
@zigazeljko
Copy link
Contributor

Would @cImport then accept arbitrary comptime expressions or just string literals (like #2206)?

@ehaas
Copy link
Contributor

ehaas commented Jul 9, 2021

This makes the common-case simpler but also makes it uglier to use compile-time computations to control the C code that is compiled (you'd have to explicitly concat the strings yourself) - e.g. the second example here https://ziglang.org/documentation/0.8.0/#Import-from-C-Header-File

I think I prefer the proposal in #2070 for achieving the goal of this proposal.

I do agree that @cImport is a bit confusing at first, because it reuses function call syntax even though you're not really calling a function and passing it a value; you're starting a new C source buffer and telling the compiler how to build that buffer at compile-time using code that is lexically inside the "function call"

@ifreund
Copy link
Member

ifreund commented Jul 10, 2021

The 3 functions you proposed removing are critical for Zig's C interop. Please read the documentation before making such large proposals.

@nektro the proposed changes would not remove any functionality, they map directly to c preprocessor directives which could be used in the proposed @cImport(). In fact, the implementation of the status quo set of builtins works by generating a snippet of C source code based on the contents of the @cImport() then running translate-c on that.

@ontes
Copy link
Author

ontes commented Jul 10, 2021

This makes the common-case simpler but also makes it uglier to use compile-time computations to control the C code that is compiled (you'd have to explicitly concat the strings yourself) - e.g. the second example here https://ziglang.org/documentation/0.8.0/#Import-from-C-Header-File

@ehaas Yeah, you are right. It may actually be uglier in some cases. But it would still be possible. (And maybe we could add library functions to make it easier.) For me it still seems like much cleaner way of doing it, because we would use comptime language features instead of compiler magic.

Would @cImport then accept arbitrary comptime expressions or just string literals (like #2206)?

@zigazeljko Full comptime expressions would be necessary to keep the example mentioned by @ehaas possible.

@andrewrk
Copy link
Member

Duplicate of #2070

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

6 participants