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 doesn't understand automatic sized arrays. #4831

Closed
BarabasGitHub opened this issue Mar 27, 2020 · 2 comments · Fixed by #7849
Closed

translate-c doesn't understand automatic sized arrays. #4831

BarabasGitHub opened this issue Mar 27, 2020 · 2 comments · Fixed by #7849
Labels
translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@BarabasGitHub
Copy link
Contributor

Zig version: 0.5.0+aec4967f3

Trying to translate this C code

const uint32_t shader_data[] = {1, 2, 3, 4};

results in

pub export const data: [*c]const c_uint = [4]c_uint{
    @bitCast(c_uint, @as(c_int, 1)),
    @bitCast(c_uint, @as(c_int, 2)),
    @bitCast(c_uint, @as(c_int, 3)),
    @bitCast(c_uint, @as(c_int, 4)),
};

which fails to compile as zig, because it thinks data is a pointer while it's actually an array.

@andrewrk andrewrk added the translate-c C to Zig source translation feature (@cImport) label Mar 28, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Mar 28, 2020
@BarabasGitHub
Copy link
Contributor Author

I was trying to look into it and thought Zig used the std to do parse the @cImport-ed files, but it seems the whole c\parse.zig file is full of never compiled code??? I can remove all of it and still build zig and it doesn't seem to have any tests? So... that means it isn't called anywhere and since it doesn't have tests either it never actually gets compiled, right?

Or I'm just totally missing something.

@he-la
Copy link
Contributor

he-la commented Jul 28, 2020

See #4045 for discussion on std.c.parse; translate-c currently implements parsing (and rendering of the zig code) in src-self-hosted/translate_c.zig.

In particular, L672ff take care of rendering the type in zig, while L686ff are responsible for rendering the initializer, i.e. the value. Hence the type renderer is unaware of the initializer, even though the type apparently can depend on whether the variable is initialized or not. You would need to do some lookahead and inform transQualType whether to emit a c pointer or an inferred array size...

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
ehaas added a commit to ehaas/zig that referenced this issue Jan 21, 2021
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the
initializer size as the array size.

2. For arrays initialized with a string literal translate it as an array
of character literals instead of `[*c]const u8`

3. Don't crash if an empty initializer is used for an incomplete array.

4. Add a test for multi-character character constants

Additionally lay some groundwork for supporting wide string literals.

fixes ziglang#4831 ziglang#7832 ziglang#7842
ehaas added a commit to ehaas/zig that referenced this issue Jan 21, 2021
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the
initializer size as the array size.

2. For arrays initialized with a string literal translate it as an array
of character literals instead of `[*c]const u8`

3. Don't crash if an empty initializer is used for an incomplete array.

4. Add a test for multi-character character constants

Additionally lay some groundwork for supporting wide string literals.

fixes ziglang#4831 ziglang#7832 ziglang#7842
ehaas added a commit to ehaas/zig that referenced this issue Jan 21, 2021
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the
initializer size as the array size.

2. For arrays initialized with a string literal translate it as an array
of character literals instead of `[*c]const u8`

3. Don't crash if an empty initializer is used for an incomplete array.

4. Add a test for multi-character character constants

Additionally lay some groundwork for supporting wide string literals.

fixes ziglang#4831 ziglang#7832 ziglang#7842
ehaas added a commit to ehaas/zig that referenced this issue Jan 24, 2021
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the
initializer size as the array size.

2. For arrays initialized with a string literal translate it as an array
of character literals instead of `[*c]const u8`

3. Don't crash if an empty initializer is used for an incomplete array.

4. Add a test for multi-character character constants

Additionally lay some groundwork for supporting wide string literals.

fixes ziglang#4831 ziglang#7832 ziglang#7842
ehaas added a commit to ehaas/zig that referenced this issue Jan 24, 2021
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the
initializer size as the array size.

2. For arrays initialized with a string literal translate it as an array
of character literals instead of `[*c]const u8`

3. Don't crash if an empty initializer is used for an incomplete array.

4. Add a test for multi-character character constants

Additionally lay some groundwork for supporting wide string literals.

fixes ziglang#4831 ziglang#7832 ziglang#7842
ehaas added a commit to ehaas/zig that referenced this issue Jan 24, 2021
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the
initializer size as the array size.

2. For arrays initialized with a string literal translate it as an array
of character literals instead of `[*c]const u8`

3. Don't crash if an empty initializer is used for an incomplete array.

4. Add a test for multi-character character constants

Additionally lay some groundwork for supporting wide string literals.

fixes ziglang#4831 ziglang#7832 ziglang#7842
andrewrk pushed a commit that referenced this issue Jan 25, 2021
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the
initializer size as the array size.

2. For arrays initialized with a string literal translate it as an array
of character literals instead of `[*c]const u8`

3. Don't crash if an empty initializer is used for an incomplete array.

4. Add a test for multi-character character constants

Additionally lay some groundwork for supporting wide string literals.

fixes #4831 #7832 #7842
dgbuckley pushed a commit to dgbuckley/zig that referenced this issue Mar 9, 2021
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the
initializer size as the array size.

2. For arrays initialized with a string literal translate it as an array
of character literals instead of `[*c]const u8`

3. Don't crash if an empty initializer is used for an incomplete array.

4. Add a test for multi-character character constants

Additionally lay some groundwork for supporting wide string literals.

fixes ziglang#4831 ziglang#7832 ziglang#7842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants