Skip to content

Instantly share code, notes, and snippets.

@abadams
Created December 20, 2017 22:33
Show Gist options
  • Save abadams/3f1543bc70a72868d0a8df422ec6f74f to your computer and use it in GitHub Desktop.
Save abadams/3f1543bc70a72868d0a8df422ec6f74f to your computer and use it in GitHub Desktop.
// This sucks. foo, bar, and baz are mentioned way too many times, mostly just to capture references to them.
bool my_function(Expr e) {
Foo foo = ...;
Bar bar = ...;
Baz baz = ...;
class CheckForSpecialNode : public IRVisitor {
using IRVisitor::visit;
void visit(const Add *op) override {
result |= check_property(op, foo, bar, baz);
}
void visit(const Sub *op) override {
result |= check_property(op, foo, bar, baz);
}
const Foo &foo;
const Bar &bar;
const Baz &baz;
public:
CheckForSpecialNode(const Foo &foo, const Bar &bar, const Baz &baz) : foo(foo), bar(bar), baz(baz) {}
bool result = false;
} checker(foo, bar, baz);
e.accept(&checker);
return checker.result;
}
// This would be better:
bool my_function(Expr e) {
Foo foo = ...;
Bar bar = ...;
Baz baz = ...;
bool result = false;
e.accept([&](const Add *op) {result |= check_property(op, foo, bar, baz);},
[&](const Sub *op) {result |= check_property(op, foo, bar, baz);});
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment