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

How to generate ir for embedded struct? #232

Open
wiltchamberian opened this issue Dec 19, 2023 · 6 comments
Open

How to generate ir for embedded struct? #232

wiltchamberian opened this issue Dec 19, 2023 · 6 comments
Labels

Comments

@wiltchamberian
Copy link

I didn't find about anything to process embedded struct, such as
type A struct{
value int
}
type B struct{
a A
}

what should I fill in " b := types.NewStruct(/what to fill here?/) "

@dannypsnl
Copy link
Member

From my understanding, embedded struct is a field that got an implicit name, so you insert reference to A and give a generated name.

@wiltchamberian
Copy link
Author

yes, I understand, but how to write the code there, it is a type of types.Var, so what if I have a types.Struct?

@dannypsnl
Copy link
Member

I believe you can use the "variable" in Go as instance of "type"

@wiltchamberian
Copy link
Author

in fact, not that easy. the variable in Go has type *types.Struct but the input needs to be a *types.Var, types.Var includes types.object and types.object include golang "Type", but not a types.Struct, so I have no idea about how to input the parameters. Or if you can write down the code.

@dannypsnl
Copy link
Member

	m := ir.NewModule()

	foo := m.NewTypeDef("foo", types.NewStruct(types.I32))
	_ = m.NewTypeDef("bar", types.NewStruct(foo))

	main := m.NewFunc("main", types.I32)

	{
		entry := main.NewBlock("")
		entry.NewRet(constant.NewInt(types.I32, 0))
	}

	fmt.Println(m)

output

%foo = type { i32 }
%bar = type { %foo }

define i32 @main() {
0:
	ret i32 0
}

@dannypsnl
Copy link
Member

You can also use types.NewPointer(foo) to get:

%foo = type { i32 }
%bar = type { %foo* }

define i32 @main() {
0:
	ret i32 0
}

@mewmew mewmew added the howto label Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants