Add a new utility class TypeSwitch to ADT.
authorRiver Riddle <riverriddle@google.com>
Tue, 17 Dec 2019 18:07:26 +0000 (10:07 -0800)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Tue, 17 Dec 2019 18:08:06 +0000 (10:08 -0800)
commitf44cf23297089dd4beb6f81a7fdda4e59466dcdb
tree91fd982330235a7073376d589f9106a4a84522c4
parent6e581e29a47b3005b14d8c1dac29bd9cd9c48381
Add a new utility class TypeSwitch to ADT.

This class provides a simplified mechanism for defining a switch over a set of types using llvm casting functionality. More specifically, this allows for defining a switch over a value of type T where each case corresponds to a type(CaseT) that can be used with dyn_cast<CaseT>(...). An example is shown below:

// Traditional piece of code:
Operation *op = ...;
if (auto constant = dyn_cast<ConstantOp>(op))
  ...;
else if (auto return = dyn_cast<ReturnOp>(op))
  ...;
else
  ...;

// New piece of code:
Operation *op = ...;
TypeSwitch<Operation *>(op)
  .Case<ConstantOp>([](ConstantOp constant) { ... })
  .Case<ReturnOp>([](ReturnOp return) { ... })
  .Default([](Operation *op) { ... });

Aside from the above, TypeSwitch supports return values, void return, multiple types per case, etc. The usability is intended to be very similar to StringSwitch.

(Using c++14 template lambdas makes everything even nicer)
More complex example of how this makes certain things easier:
LogicalResult process(Constant op);
LogicalResult process(ReturnOp op);
LogicalResult process(FuncOp op);

TypeSwitch<Operation *, LogicalResult>(op)
  .Case<ConstantOp, ReturnOp, FuncOp>([](auto op) { return process(op); })
  .Default([](Operation *op) { return op->emitError() << "could not be processed"; });

PiperOrigin-RevId: 286003613
mlir/include/mlir/ADT/TypeSwitch.h [new file with mode: 0644]
mlir/include/mlir/Support/STLExtras.h
mlir/unittests/ADT/CMakeLists.txt [new file with mode: 0644]
mlir/unittests/ADT/TypeSwitchTest.cpp [new file with mode: 0644]
mlir/unittests/CMakeLists.txt