1 //===- GenInfo.h - Generator info -------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef MLIR_TABLEGEN_GENINFO_H_ 10 #define MLIR_TABLEGEN_GENINFO_H_ 11 12 #include "mlir/Support/LLVM.h" 13 #include "llvm/ADT/StringRef.h" 14 #include <functional> 15 #include <utility> 16 17 namespace llvm { 18 class RecordKeeper; 19 } // namespace llvm 20 21 namespace mlir { 22 23 /// Generator function to invoke. 24 using GenFunction = 25 std::function<bool(const llvm::RecordKeeper &records, raw_ostream &os)>; 26 27 /// Structure to group information about a generator (argument to invoke via 28 /// mlir-tblgen, description, and generator function). 29 class GenInfo { 30 public: 31 /// GenInfo constructor should not be invoked directly, instead use 32 /// GenRegistration or registerGen. 33 GenInfo(StringRef arg, StringRef description, GenFunction generator) 34 : arg(arg), description(description), generator(std::move(generator)) {} 35 36 /// Invokes the generator and returns whether the generator failed. 37 bool invoke(const llvm::RecordKeeper &records, raw_ostream &os) const { 38 assert(generator && "Cannot call generator with null generator"); 39 return generator(records, os); 40 } 41 42 /// Returns the command line option that may be passed to 'mlir-tblgen' to 43 /// invoke this generator. 44 StringRef getGenArgument() const { return arg; } 45 46 /// Returns a description for the generator. 47 StringRef getGenDescription() const { return description; } 48 49 private: 50 // The argument with which to invoke the generator via mlir-tblgen. 51 StringRef arg; 52 53 // Description of the generator. 54 StringRef description; 55 56 // Generator function. 57 GenFunction generator; 58 }; 59 60 /// GenRegistration provides a global initializer that registers a generator 61 /// function. 62 /// 63 /// Usage: 64 /// 65 /// // At namespace scope. 66 /// static GenRegistration Print("print", "Print records", [](...){...}); 67 struct GenRegistration { 68 GenRegistration(StringRef arg, StringRef description, 69 const GenFunction &function); 70 }; 71 72 } // namespace mlir 73 74 #endif // MLIR_TABLEGEN_GENINFO_H_ 75