-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
parser: fix C struct nested anon struct language #25789
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
Conversation
| typed_obj := if lang == .c { | ||
| '(${sym.cname}*)&(${obj})' | ||
| } else { | ||
| '*(${sym.cname}*)&(${obj})' | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this different for a C one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The V compiler handles C structs differently than V structs.
// V auto str functions:
static string SYSTEM_INFO_2_str(SYSTEM_INFO_2* it) { return indent_SYSTEM_INFO_2_str(it, 0);}
static string C___VAnonStruct1_str(C___VAnonStruct1* it) { return indent_C___VAnonStruct1_str(it, 0);}
static string C___VAnonStruct2_str(C___VAnonStruct2* it) { return indent_C___VAnonStruct2_str(it, 0);}// V auto str functions:
static string main__DeepStruct_str(main__DeepStruct it) { return indent_main__DeepStruct_str(it, 0);}
static string main___VAnonStruct1_str(main___VAnonStruct1 it) { return indent_main___VAnonStruct1_str(it, 0);}
static string main___VAnonStruct2_str(main___VAnonStruct2 it) { return indent_main___VAnonStruct2_str(it, 0);}As shown above, when the compiler encounters a C struct, it accesses the value via a pointer. In contrast, for a V struct, it passes the struct by value (making a copy).
To ensure auto_str works correctly with a V anonymous struct, a corresponding C___VAnonStruct1 instance must be created for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't direct access a C anon struct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be better in the long term, to pass the V struct for the generated auto str methods by a reference too, to keep things consistent 🤔.
spytheman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work.
Thanks @kbkpbot .
Fix issue #25807
force anon struct/union in C.struct keep the same language of outside.
To fix issue #25788 in another PR due to bootstrap problem.