From 13a7aadf73f97d953897d6b94157412ff130d3c2 Mon Sep 17 00:00:00 2001 From: River Riddle Date: Thu, 15 Apr 2021 16:09:08 -0700 Subject: [PATCH] [mlir] Add support for walking locations similarly to Operations This allows for walking all nested locations of a given location, and is generally useful when processing locations. Differential Revision: https://reviews.llvm.org/D100437 --- include/llvm/ADT/TypeSwitch.h | 6 ++++++ unittests/ADT/TypeSwitchTest.cpp | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/llvm/ADT/TypeSwitch.h b/include/llvm/ADT/TypeSwitch.h index bfcb2064301..815b9a40afa 100644 --- a/include/llvm/ADT/TypeSwitch.h +++ b/include/llvm/ADT/TypeSwitch.h @@ -124,6 +124,12 @@ public: return std::move(*result); return defaultFn(this->value); } + /// As a default, return the given value. + LLVM_NODISCARD ResultT Default(ResultT defaultResult) { + if (result) + return std::move(*result); + return defaultResult; + } LLVM_NODISCARD operator ResultT() { diff --git a/unittests/ADT/TypeSwitchTest.cpp b/unittests/ADT/TypeSwitchTest.cpp index fde423d011b..442ac1910a0 100644 --- a/unittests/ADT/TypeSwitchTest.cpp +++ b/unittests/ADT/TypeSwitchTest.cpp @@ -47,7 +47,7 @@ TEST(TypeSwitchTest, CasesResult) { return TypeSwitch(&value) .Case([](auto *) { return 0; }) .Case([](DerivedC *) { return 1; }) - .Default([](Base *) { return -1; }); + .Default(-1); }; EXPECT_EQ(0, translate(DerivedA())); EXPECT_EQ(0, translate(DerivedB()));