[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

GitOrigin-RevId: 706c9c5ce0382644d4e693741f5d885be7c20e46
diff --git a/include/llvm/ADT/TypeSwitch.h b/include/llvm/ADT/TypeSwitch.h
index bfcb206..815b9a4 100644
--- a/include/llvm/ADT/TypeSwitch.h
+++ b/include/llvm/ADT/TypeSwitch.h
@@ -124,6 +124,12 @@
       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 fde423d..442ac19 100644
--- a/unittests/ADT/TypeSwitchTest.cpp
+++ b/unittests/ADT/TypeSwitchTest.cpp
@@ -47,7 +47,7 @@
     return TypeSwitch<Base *, int>(&value)
         .Case<DerivedA, DerivedB, DerivedD>([](auto *) { return 0; })
         .Case([](DerivedC *) { return 1; })
-        .Default([](Base *) { return -1; });
+        .Default(-1);
   };
   EXPECT_EQ(0, translate(DerivedA()));
   EXPECT_EQ(0, translate(DerivedB()));