[utils]: Add TINT_DEFER()

Runs the statement(s) at the end of the lexical scope

Change-Id: I74de02b7204b56016c7bbe71fee4ece498d3570d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51923
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7bde86a..3d2d8d6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -666,6 +666,7 @@
     sem/type_manager_test.cc
     sem/u32_type_test.cc
     sem/vector_type_test.cc
+    utils/defer_test.cc
     utils/enum_set_test.cc
     utils/get_or_create_test.cc
     utils/hash_test.cc
diff --git a/src/utils/concat.h b/src/utils/concat.h
new file mode 100644
index 0000000..54a4e75
--- /dev/null
+++ b/src/utils/concat.h
@@ -0,0 +1,22 @@
+
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_UTILS_CONCAT_H_
+#define SRC_UTILS_CONCAT_H_
+
+#define TINT_CONCAT_2(a, b) a##b
+#define TINT_CONCAT(a, b) TINT_CONCAT_2(a, b)
+
+#endif  //  SRC_UTILS_CONCAT_H_
diff --git a/src/utils/defer.h b/src/utils/defer.h
new file mode 100644
index 0000000..781e5c9
--- /dev/null
+++ b/src/utils/defer.h
@@ -0,0 +1,63 @@
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_UTILS_DEFER_H_
+#define SRC_UTILS_DEFER_H_
+
+#include <utility>
+
+#include "src/utils/concat.h"
+
+namespace tint {
+namespace utils {
+
+/// Defer executes a function or function like object when it is destructed.
+template <typename F>
+class Defer {
+ public:
+  /// Constructor
+  /// @param f the function to call when the Defer is destructed
+  explicit Defer(F&& f) : f_(std::move(f)) {}
+
+  /// Move constructor
+  Defer(Defer&&) = default;
+
+  /// Destructor
+  /// Calls the deferred function
+  ~Defer() { f_(); }
+
+ private:
+  Defer(const Defer&) = delete;
+  Defer& operator=(const Defer&) = delete;
+
+  F f_;
+};
+
+/// Constructor
+/// @param f the function to call when the Defer is destructed
+template <typename F>
+inline Defer<F> MakeDefer(F&& f) {
+  return Defer<F>(std::forward<F>(f));
+}
+
+}  // namespace utils
+}  // namespace tint
+
+/// TINT_DEFER(S) executes the statement(s) `S` when exiting the current lexical
+/// scope.
+#define TINT_DEFER(S)                          \
+  auto TINT_CONCAT(tint_defer_, __COUNTER__) = \
+      ::tint::utils::MakeDefer([&] { S; })
+
+#endif  //  SRC_UTILS_DEFER_H_
diff --git a/src/utils/defer_test.cc b/src/utils/defer_test.cc
new file mode 100644
index 0000000..4f93b15
--- /dev/null
+++ b/src/utils/defer_test.cc
@@ -0,0 +1,44 @@
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/utils/defer.h"
+
+#include "gtest/gtest.h"
+
+namespace tint {
+namespace utils {
+namespace {
+
+TEST(DeferTest, Basic) {
+  bool deferCalled = false;
+  { TINT_DEFER(deferCalled = true); }
+  ASSERT_TRUE(deferCalled);
+}
+
+TEST(DeferTest, DeferOrder) {
+  int counter = 0;
+  int a = 0, b = 0, c = 0;
+  {
+    TINT_DEFER(a = ++counter);
+    TINT_DEFER(b = ++counter);
+    TINT_DEFER(c = ++counter);
+  }
+  ASSERT_EQ(a, 3);
+  ASSERT_EQ(b, 2);
+  ASSERT_EQ(c, 1);
+}
+
+}  // namespace
+}  // namespace utils
+}  // namespace tint
diff --git a/src/utils/scoped_assignment.h b/src/utils/scoped_assignment.h
index 58e7cdd..99f8ba4 100644
--- a/src/utils/scoped_assignment.h
+++ b/src/utils/scoped_assignment.h
@@ -1,3 +1,4 @@
+
 // Copyright 2021 The Tint Authors.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +18,8 @@
 
 #include <type_traits>
 
+#include "src/utils/concat.h"
+
 namespace tint {
 namespace utils {
 
@@ -50,9 +53,6 @@
 }  // namespace utils
 }  // namespace tint
 
-#define TINT_CONCAT_2(a, b) a##b
-#define TINT_CONCAT(a, b) TINT_CONCAT_2(a, b)
-
 /// TINT_SCOPED_ASSIGNMENT(var, val) assigns `val` to `var`, and automatically
 /// restores the original value of `var` when exiting the current lexical scope.
 #define TINT_SCOPED_ASSIGNMENT(var, val)                                  \
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 0431fa9..1e1d989 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -299,6 +299,7 @@
     "../src/transform/vertex_pulling_test.cc",
     "../src/transform/wrap_arrays_in_structs_test.cc",
     "../src/transform/zero_init_workgroup_memory_test.cc",
+    "../src/utils/defer_test.cc",
     "../src/utils/enum_set_test.cc",
     "../src/utils/get_or_create_test.cc",
     "../src/utils/hash_test.cc",