xref: /llvm-project/mlir/include/mlir/TableGen/Builder.h (revision 0f827ee0366922c16fe987c75dad459e717b4f6e)
1 //===- Builder.h - Builder classes ------------------------------*- 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 // Builder wrapper to simplify using TableGen Record for building
10 // operations/types/etc.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TABLEGEN_BUILDER_H_
15 #define MLIR_TABLEGEN_BUILDER_H_
16 
17 #include "mlir/Support/LLVM.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 
22 namespace llvm {
23 class Init;
24 class Record;
25 class SMLoc;
26 } // namespace llvm
27 
28 namespace mlir {
29 namespace tblgen {
30 
31 /// Wrapper class with helper methods for accessing Builders defined in
32 /// TableGen.
33 class Builder {
34 public:
35   /// This class represents a single parameter to a builder method.
36   class Parameter {
37   public:
38     /// Return a string containing the C++ type of this parameter.
39     StringRef getCppType() const;
40 
41     /// Return an optional string containing the name of this parameter. If
42     /// std::nullopt, no name was specified for this parameter by the user.
getName()43     std::optional<StringRef> getName() const { return name; }
44 
45     /// Return an optional string containing the default value to use for this
46     /// parameter.
47     std::optional<StringRef> getDefaultValue() const;
48 
49   private:
Parameter(std::optional<StringRef> name,const llvm::Init * def)50     Parameter(std::optional<StringRef> name, const llvm::Init *def)
51         : name(name), def(def) {}
52 
53     /// The optional name of the parameter.
54     std::optional<StringRef> name;
55 
56     /// The tablegen definition of the parameter. This is either a StringInit,
57     /// or a CArg DefInit.
58     const llvm::Init *def;
59 
60     // Allow access to the constructor.
61     friend Builder;
62   };
63 
64   /// Construct a builder from the given Record instance.
65   Builder(const llvm::Record *record, ArrayRef<SMLoc> loc);
66 
67   /// Return a list of parameters used in this build method.
getParameters()68   ArrayRef<Parameter> getParameters() const { return parameters; }
69 
70   /// Return an optional string containing the body of the builder.
71   std::optional<StringRef> getBody() const;
72 
73   /// Return the deprecation message of the builder.
74   /// Empty optional if the builder is not deprecated.
75   std::optional<StringRef> getDeprecatedMessage() const;
76 
77 protected:
78   /// The TableGen definition of this builder.
79   const llvm::Record *def;
80 
81 private:
82   /// A collection of parameters to the builder.
83   SmallVector<Parameter> parameters;
84 };
85 
86 } // namespace tblgen
87 } // namespace mlir
88 
89 #endif // MLIR_TABLEGEN_BUILDER_H_
90