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

translate-c generates Zig code that does not compile. #4479

Closed
JesseRMeyer opened this issue Feb 16, 2020 · 3 comments · Fixed by #8242
Closed

translate-c generates Zig code that does not compile. #4479

JesseRMeyer opened this issue Feb 16, 2020 · 3 comments · Fixed by #8242
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@JesseRMeyer
Copy link

Consider:

typedef long HRESULT;
#define _HRESULT_TYPEDEF_(_sc) ((HRESULT)_sc)
#define DXGI_ERROR_NOT_FOUND _HRESULT_TYPEDEF_(0x887A0002L)

translate-c:

pub inline fn _HRESULT_TYPEDEF_(_sc: var) @TypeOf(if (@typeId(@TypeOf(_sc)) == .Pointer) @ptrCast(HRESULT, _sc) else if (@typeId(@TypeOf(_sc)) == .Int) @intToPtr(HRESULT, _sc) else @as(HRESULT, _sc)) {
    return if (@typeId(@TypeOf(_sc)) == .Pointer) @ptrCast(HRESULT, _sc) else if (@typeId(@TypeOf(_sc)) == .Int) @intToPtr(HRESULT, _sc) else @as(HRESULT, _sc);
}
const DXGI_ERROR_NOT_FOUND = _HRESULT_TYPEDEF_(@as(c_long, 0x887A0002));

However:

error: integer value 2289696770 cannot be coerced to type 'c_long'
pub const DXGI_ERROR_NOT_FOUND = _HRESULT_TYPEDEF_(@as(c_long, 0x887A0002));

Somewhat confusingly, in VS, the hex value mysteriously gets converted to UL:
ULorL

I don't know if that is a function of the UI, and what the compiler ends up seeing.

What should translate-c do with C code that will not compile as Zig code?

@andrewrk andrewrk added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. translate-c C to Zig source translation feature (@cImport) labels Feb 18, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Feb 18, 2020
@andrewrk
Copy link
Member

Macros are translated on a heuristic basis. Not all macros can be translated.

This one looks like it can be, but it needs a more complicated Zig expression to express what the C code is doing.

@JustinRyanH
Copy link

@andrewrk is this a scenario where we need to identify when for signed integers and it's one bigger than maxInt in the pre_processor we translate it to minInt

@JesseRMeyer
Copy link
Author

Macros are translated on a heuristic basis. Not all macros can be translated.

I'm not actually expecting for every macro can be translatable. I'm expecting that every translated macro be valid Zig.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants