[PATCH] D99560: Utility to construct visitors from lambdas.

Bakhtiyar Neyman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 29 21:13:01 PDT 2021


bakhtiyarneyman created this revision.
Herald added a subscriber: dexonsmith.
bakhtiyarneyman requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99560

Files:
  llvm/include/llvm/ADT/Visitor.h


Index: llvm/include/llvm/ADT/Visitor.h
===================================================================
--- /dev/null
+++ 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 <class... Fs> struct Visitor;
+
+template <class HEAD, class... TAIL>
+struct Visitor<HEAD, TAIL...> : HEAD, Visitor<TAIL...> {
+  explicit Visitor(HEAD head, TAIL... tail)
+      : HEAD(head), Visitor<TAIL...>(tail...) {}
+
+  using HEAD::operator();
+  using Visitor<TAIL...>::operator();
+};
+
+template <class HEAD> struct Visitor<HEAD> : HEAD {
+  explicit Visitor(HEAD f0) : HEAD(f0) {}
+
+  using HEAD::operator();
+};
+
+template <class... LAMBDAS> auto makeVisitor(LAMBDAS... lambdas) {
+  return Visitor<LAMBDAS...>(lambdas...);
+}
+
+} // namespace llvm
+
+#endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99560.334045.patch
Type: text/x-patch
Size: 1452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210330/99dfbfbe/attachment.bin>


More information about the llvm-commits mailing list