-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow writing mutators with lambdas #8877
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
|
If we like this direction, we can make a version for visitors, too. |
|
We can also consider adding |
I went ahead and did this -- clang-format decides to add a line, but I prefer how the functional form reads. |
|
Failures look like an upstream WASM breakage inside one of the rfactor cases -- not related to this PR |
This follows the implementation of
std::visit, using a struct to accumulate function overloads and then dispatching to the correct one at compile time (usingstd::is_invocable_v). The result is the ability to define mutators that capture local state. I convert a few visitors in the codebase as a means to achieve some test coverage.For example, this is the code for
expand_exprinside StorageFlattening.cpp:We need the pointer to the mutator as the second argument for be able to call
self->visit_base, which grants the lambdas access to theIRMutator::visitfunction.We also introduce helpers
mutate_withandvisit_with. Both accept aStmtorExpras the first argument. The remaining arguments are a list of lambdas from which to build a mutator or visitor. Both accept lists ofvisitoverloads. Themutate_withhelper additionally accepts a list ofmutateoverloads (which it detects via anis_invocablecheck onconst Expr &orconst Stmt &-- mixing mutate and visit overloads is unsupported).