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

unions and structs are not translated correctly if typedef is not used #190

Open
dy-tea opened this issue Dec 26, 2024 · 5 comments
Open

Comments

@dy-tea
Copy link

dy-tea commented Dec 26, 2024

Here is some C code that uses unions:

#include <stdio.h>

int main() {
  union {
    int i;
    int j;
  } u;

  u.i = 3;
  u.j = 5;

  printf("%d, %d\n", u.i, u.j);

  return 0;
}

I run v translate test.c and it outputs the below test.v:

@[translated]
module main

fn main() {
	u.i = 3
	u.j = 5
	C.printf(c'%d, %d\n', u.i, u.j)
	return
}

The above code will not compile successfully, as u is not defined:

test.v:5:2: error: undefined ident: `u`
    3 | 
    4 | fn main() {
    5 |     u.i = 3
      |     ^
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
test.v:5:4: error: `u` does not return a value
    3 | 
    4 | fn main() {
    5 |     u.i = 3
      |       ^
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
test.v:5:4: error: unexpected symbol `void`
    3 | 
    4 | fn main() {
    5 |     u.i = 3
      |       ^
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
test.v:6:2: error: undefined ident: `u`
    4 | fn main() {
    5 |     u.i = 3
    6 |     u.j = 5
      |     ^
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
    8 |     return
test.v:6:4: error: `u` does not return a value
    4 | fn main() {
    5 |     u.i = 3
    6 |     u.j = 5
      |       ^
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
    8 |     return
test.v:6:4: error: unexpected symbol `void`
    4 | fn main() {
    5 |     u.i = 3
    6 |     u.j = 5
      |       ^
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
    8 |     return
test.v:7:24: error: undefined ident: `u`
    5 |     u.i = 3
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
      |                           ^
    8 |     return
    9 | }
test.v:7:26: error: `u` does not return a value
    5 |     u.i = 3
    6 |     u.j = 5
    7 |     C.printf(c'%d, %d\n', u.i, u.j)
      |                             ^
    8 |     return
    9 | }
@JalonSolov
Copy link
Contributor

Looks like it just skipped the union def altogether.

@dy-tea
Copy link
Author

dy-tea commented Dec 27, 2024

Another example is with the input code:

union MyUnion {
  int a;
  int b;
};

int main() {
  union MyUnion u;
  u.a = 10;
  u.b = 100;
  return 0;
}

The below code is generated:

@[translated]
module main

union MyUnion {
	a int
	b int
}

fn main() {
	u := Union
	MyUnion{}
	u.a = 10
	u.b = 100
	return
}

Which incorrectly sets u to Union instead of MyUnion{}.

The case which does work correctly is with:

typedef union {
  int a;
  int b;
}MyUnion;

int main() {
  MyUnion u;
  u.a = 10;
  u.b = 100;
  return 0;
}

Which correctly translates to:

@[translated]
module main

union MyUnion {
	a int
	b int
}

fn main() {
	u := MyUnion{}
	u.a = 10
	u.b = 100
	return
}

@JalonSolov JalonSolov changed the title unions are not translated correctly unions are not translated correctly if typedef is not used Dec 27, 2024
@JalonSolov
Copy link
Contributor

So the missing typedef is the problem. Changed the title accordingly.

@dy-tea
Copy link
Author

dy-tea commented Dec 27, 2024

Seems to also affect structs.

Here is some C code that has a struct inside and outside main:

struct Outside {
  int a;
  int b;
};

int main() {
  struct Outside o;
  o.a = 1;
  o.b = 2;

  struct Inside {
    int a;
    int b;
  };
  struct Inside i;
  i.a = 3;
  i.b = 4;
}

The struct outside main is handled correctly, but the struct definition inside main is removed:

@[translated]
module main

struct Outside {
	a int
	b int
}

fn main() {
	o := Outside{}
	o.a = 1
	o.b = 2

	i := Inside{}
	i.a = 3
	i.b = 4
}

@JalonSolov JalonSolov changed the title unions are not translated correctly if typedef is not used unions and structs are not translated correctly if typedef is not used Dec 27, 2024
@JalonSolov
Copy link
Contributor

Title updated to include both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants