MLIR  22.0.0git
Interfaces.h
Go to the documentation of this file.
1 //===- Interfaces.h - Interface wrapper 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 #ifndef MLIR_TABLEGEN_INTERFACES_H_
10 #define MLIR_TABLEGEN_INTERFACES_H_
11 
12 #include "mlir/Support/LLVM.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/iterator.h"
16 
17 namespace llvm {
18 class Init;
19 class Record;
20 } // namespace llvm
21 
22 namespace mlir {
23 namespace tblgen {
24 
25 // Wrapper class with helper methods for accessing InterfaceMethod defined
26 // in TableGen.
28 public:
29  // This struct represents a single method argument.
30  struct Argument {
31  StringRef type;
32  StringRef name;
33  };
34 
35  explicit InterfaceMethod(const llvm::Record *def, std::string uniqueName);
36 
37  // Return the return type of this method.
38  StringRef getReturnType() const;
39 
40  // Return the name of this method.
41  StringRef getName() const;
42 
43  // Return the dedup name of this method.
44  StringRef getUniqueName() const;
45 
46  // Return if this method is static.
47  bool isStatic() const;
48 
49  // Return the body for this method if it has one.
50  std::optional<StringRef> getBody() const;
51 
52  // Return the default implementation for this method if it has one.
53  std::optional<StringRef> getDefaultImplementation() const;
54 
55  // Return the description of this method if it has one.
56  std::optional<StringRef> getDescription() const;
57 
58  // Arguments.
60  bool arg_empty() const;
61 
62 private:
63  // The TableGen definition of this method.
64  const llvm::Record *def;
65 
66  // The arguments of this method.
67  SmallVector<Argument, 2> arguments;
68 
69  // The unique name of this method, to distinguish it from other methods with
70  // the same name (overloaded methods)
71  std::string uniqueName;
72 };
73 
74 //===----------------------------------------------------------------------===//
75 // Interface
76 //===----------------------------------------------------------------------===//
77 
78 // Wrapper class with helper methods for accessing Interfaces defined in
79 // TableGen.
80 class Interface {
81 public:
82  explicit Interface(const llvm::Record *def);
83  Interface(const Interface &rhs) : def(rhs.def), methods(rhs.methods) {
84  for (auto &base : rhs.baseInterfaces)
85  baseInterfaces.push_back(std::make_unique<Interface>(*base));
86  }
87 
88  // Return the name of this interface.
89  StringRef getName() const;
90 
91  // Returns this interface's name prefixed with namespaces.
92  std::string getFullyQualifiedName() const;
93 
94  // Return the C++ namespace of this interface.
95  StringRef getCppNamespace() const;
96 
97  // Return the methods of this interface.
99 
100  // Return the description of this method if it has one.
101  std::optional<StringRef> getDescription() const;
102 
103  // Return the interfaces extra class declaration code.
104  std::optional<StringRef> getExtraClassDeclaration() const;
105 
106  // Return the traits extra class declaration code.
107  std::optional<StringRef> getExtraTraitClassDeclaration() const;
108 
109  // Return the extra class declaration code shared between the interface and
110  // trait classes.
111  std::optional<StringRef> getExtraSharedClassDeclaration() const;
112 
113  // Return the extra classof method code.
114  std::optional<StringRef> getExtraClassOf() const;
115 
116  // Return the verify method body if it has one.
117  std::optional<StringRef> getVerify() const;
118 
119  // Return the base interfaces of this interface.
120  auto getBaseInterfaces() const {
121  return llvm::make_pointee_range(baseInterfaces);
122  }
123 
124  // If there's a verify method, return if it needs to access the ops in the
125  // regions.
126  bool verifyWithRegions() const;
127 
128  // Returns the Tablegen definition this interface was constructed from.
129  const llvm::Record &getDef() const { return *def; }
130 
131 private:
132  // The TableGen definition of this interface.
133  const llvm::Record *def;
134 
135  // The methods of this interface.
137 
138  // The base interfaces of this interface.
140 };
141 
142 // An interface that is registered to an Attribute.
143 struct AttrInterface : public Interface {
144  using Interface::Interface;
145 
146  static bool classof(const Interface *interface);
147 };
148 // An interface that is registered to an Operation.
149 struct OpInterface : public Interface {
150  using Interface::Interface;
151 
152  static bool classof(const Interface *interface);
153 };
154 // An interface that is registered to a Type.
155 struct TypeInterface : public Interface {
156  using Interface::Interface;
157 
158  static bool classof(const Interface *interface);
159 };
160 } // namespace tblgen
161 } // namespace mlir
162 
163 #endif // MLIR_TABLEGEN_INTERFACES_H_
StringRef getReturnType() const
Definition: Interfaces.cpp:37
std::optional< StringRef > getDefaultImplementation() const
Definition: Interfaces.cpp:62
ArrayRef< Argument > getArguments() const
Definition: Interfaces.cpp:74
InterfaceMethod(const llvm::Record *def, std::string uniqueName)
Definition: Interfaces.cpp:28
std::optional< StringRef > getBody() const
Definition: Interfaces.cpp:55
StringRef getUniqueName() const
Definition: Interfaces.cpp:47
StringRef getName() const
Definition: Interfaces.cpp:42
std::optional< StringRef > getDescription() const
Definition: Interfaces.cpp:69
Interface(const llvm::Record *def)
std::optional< StringRef > getExtraClassOf() const
Definition: Interfaces.cpp:170
std::optional< StringRef > getDescription() const
Definition: Interfaces.cpp:147
std::optional< StringRef > getExtraClassDeclaration() const
Definition: Interfaces.cpp:153
std::optional< StringRef > getExtraSharedClassDeclaration() const
Definition: Interfaces.cpp:165
Interface(const Interface &rhs)
Definition: Interfaces.h:83
ArrayRef< InterfaceMethod > getMethods() const
Definition: Interfaces.cpp:144
std::optional< StringRef > getExtraTraitClassDeclaration() const
Definition: Interfaces.cpp:159
bool verifyWithRegions() const
Definition: Interfaces.cpp:184
std::optional< StringRef > getVerify() const
Definition: Interfaces.cpp:176
auto getBaseInterfaces() const
Definition: Interfaces.h:120
std::string getFullyQualifiedName() const
Definition: Interfaces.cpp:130
StringRef getCppNamespace() const
Definition: Interfaces.cpp:139
const llvm::Record & getDef() const
Definition: Interfaces.h:129
StringRef getName() const
Definition: Interfaces.cpp:125
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
Include the generated interface declarations.
static bool classof(const Interface *interface)
Definition: Interfaces.cpp:192
static bool classof(const Interface *interface)
Definition: Interfaces.cpp:200
static bool classof(const Interface *interface)
Definition: Interfaces.cpp:208