• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 namespace llvm {
17 class RecordKeeper;
18 } // end namespace llvm
19 
20 namespace mlir {
21 
22 /// Generator function to invoke.
23 using GenFunction = std::function<bool(const llvm::RecordKeeper &recordKeeper,
24                                        raw_ostream &os)>;
25 
26 /// Structure to group information about a generator (argument to invoke via
27 /// mlir-tblgen, description, and generator function).
28 class GenInfo {
29 public:
30   /// GenInfo constructor should not be invoked directly, instead use
31   /// GenRegistration or registerGen.
GenInfo(StringRef arg,StringRef description,GenFunction generator)32   GenInfo(StringRef arg, StringRef description, GenFunction generator)
33       : arg(arg), description(description), generator(generator) {}
34 
35   /// Invokes the generator and returns whether the generator failed.
invoke(const llvm::RecordKeeper & recordKeeper,raw_ostream & os)36   bool invoke(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) const {
37     assert(generator && "Cannot call generator with null generator");
38     return generator(recordKeeper, os);
39   }
40 
41   /// Returns the command line option that may be passed to 'mlir-tblgen' to
42   /// invoke this generator.
getGenArgument()43   StringRef getGenArgument() const { return arg; }
44 
45   /// Returns a description for the generator.
getGenDescription()46   StringRef getGenDescription() const { return description; }
47 
48 private:
49   // The argument with which to invoke the generator via mlir-tblgen.
50   StringRef arg;
51 
52   // Description of the generator.
53   StringRef description;
54 
55   // Generator function.
56   GenFunction generator;
57 };
58 
59 /// GenRegistration provides a global initializer that registers a generator
60 /// function.
61 ///
62 /// Usage:
63 ///
64 ///   // At namespace scope.
65 ///   static GenRegistration Print("print", "Print records", [](...){...});
66 struct GenRegistration {
67   GenRegistration(StringRef arg, StringRef description, GenFunction function);
68 };
69 
70 } // end namespace mlir
71 
72 #endif // MLIR_TABLEGEN_GENINFO_H_
73