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

Full blown reflection for aggregate structs in clang #532

Merged
merged 42 commits into from
Nov 24, 2023
Merged

Conversation

stephenberry
Copy link
Owner

@stephenberry stephenberry commented Nov 21, 2023

This uses clang's __builtin_dump_struct to enable fully featured (no macros, no user code) reflection for aggregate initializable structs. Only works on clang, no gcc or msvc.

@stephenberry
Copy link
Owner Author

@justend29, @jbbjarnason

I'm not sure if you guys use clang, but check out this black magic: tests/reflection_test/reflection_test.cpp

This branch enables real reflection, no macros, no user code, for clang. If a glz::meta is provided, that will override this behavior, but otherwise you can just read/write structs to/from JSON without a glz::meta.

What are your thoughts on using __builtin_dump_struct in this way? I want glaze to play nice with future C++ reflection, so this was a good test case for that. The beauty of this approach is that we still get nearly the same compile time optimization using this reflection. The only overhead is needing to copy pointers due to the loss of member variable pointers from the structured binding transform.

@stephenberry
Copy link
Owner Author

This works in Xcode 15 and later versions of clang, but I'm seeing if I can get it to work with Xcode 14.2, which is used for these actions.

@jbbjarnason
Copy link
Collaborator

jbbjarnason commented Nov 21, 2023

This is awesome, I really hope that the c++ committee makes this as a standard, that is reflection of members.

What are your thoughts on using __builtin_dump_struct in this way?

Looking at the docs: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-dump-struct it is said to be for debugging purposes, so I would suggest to mention it in docs.

Can we put an #if for the feature flag __has_builtin(__builtin_dump_struct) in reflect.hpp ?

Would it be possible to implement to_tuple with tuple_cat ?

@stephenberry
Copy link
Owner Author

Yeah, it looks like we'll be getting real reflection in C++26 (see Herb Sutter's blog), but in the meantime this could be nice and we could invisibly replace this behavior with C++26 reflection when available.

Can we put an #if for the feature flag __has_builtin(__builtin_dump_struct) in reflect.hpp ?

Great idea!

Would it be possible to implement to_tuple with tuple_cat?

No, to_tuple cannot be built with tuple_cat because we can't convert a struct directly to a tuple, instead we need to use structured bindings. But, this isn't a big problem, we just write out a large enough to_tuple function.

@justend29
Copy link
Collaborator

This seems great, guys. Especially since it can be "invisibly replaced with C++26 reflection." I've been following the standards committee on the reflection proposals, and it all looks promising. I'm not sure I'll have time to experiment with this clang-specific addition in the next couple days, so I'll have to leave it to you two. Great find.

@stephenberry stephenberry merged commit 60a19f3 into main Nov 24, 2023
12 checks passed
@stephenberry stephenberry deleted the clang branch November 24, 2023 15:37
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

Successfully merging this pull request may close these issues.

3 participants