diff --git a/llvm/include/llvm/ADT/Visitor.h b/llvm/include/llvm/ADT/Visitor.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ADT/Visitor.h @@ -0,0 +1,42 @@ +//===- llvm/ADT/Visitor.h - Visitors constructed from lambdas. --*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines the Visitor class, which is a utility that constructs +// visitors from lambdas. Can be used for e.g. visiting PointerUnion. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ADT_VISITOR_H +#define LLVM_ADT_VISITOR_H + +namespace llvm { + +template struct Visitor; + +template +struct Visitor : Head, Visitor { + explicit Visitor(Head head, Tail... tail) + : Head(head), Visitor(tail...) {} + + using Head::operator(); + using Visitor::operator(); +}; + +template struct Visitor : Head { + explicit Visitor(Head head) : Head(head) {} + + using Head::operator(); +}; + +template auto makeVisitor(Lambdas... lambdas) { + return Visitor(lambdas...); +} + +} // namespace llvm + +#endif diff --git a/llvm/unittests/ADT/VisitorTest.cpp b/llvm/unittests/ADT/VisitorTest.cpp new file mode 100644 --- /dev/null +++ b/llvm/unittests/ADT/VisitorTest.cpp @@ -0,0 +1,26 @@ +//===- VisitorTest.cpp - Unit tests for a sequence abstraciton -----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/Visitor.h" +#include "gtest/gtest.h" + +#include + +using namespace llvm; + +namespace { + +TEST(VisitorTest, Basic) { + auto visitor = llvm::makeVisitor([](int a) { return "int"; }, + [](std::string a) { return "str"; }); + + EXPECT_EQ("int", visitor(42)); + EXPECT_EQ("str", visitor("foo")); +} + +} // anonymous namespace