MLIR  20.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);
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 if this method is static.
44  bool isStatic() const;
45 
46  // Return the body for this method if it has one.
47  std::optional<StringRef> getBody() const;
48 
49  // Return the default implementation for this method if it has one.
50  std::optional<StringRef> getDefaultImplementation() const;
51 
52  // Return the description of this method if it has one.
53  std::optional<StringRef> getDescription() const;
54 
55  // Arguments.
57  bool arg_empty() const;
58 
59 private:
60  // The TableGen definition of this method.
61  const llvm::Record *def;
62 
63  // The arguments of this method.
64  SmallVector<Argument, 2> arguments;
65 };
66 
67 //===----------------------------------------------------------------------===//
68 // Interface
69 //===----------------------------------------------------------------------===//
70 
71 // Wrapper class with helper methods for accessing Interfaces defined in
72 // TableGen.
73 class Interface {
74 public:
75  explicit Interface(const llvm::Record *def);
76  Interface(const Interface &rhs) : def(rhs.def), methods(rhs.methods) {
77  for (auto &base : rhs.baseInterfaces)
78  baseInterfaces.push_back(std::make_unique<Interface>(*base));
79  }
80 
81  // Return the name of this interface.
82  StringRef getName() const;
83 
84  // Returns this interface's name prefixed with namespaces.
85  std::string getFullyQualifiedName() const;
86 
87  // Return the C++ namespace of this interface.
88  StringRef getCppNamespace() const;
89 
90  // Return the methods of this interface.
92 
93  // Return the description of this method if it has one.
94  std::optional<StringRef> getDescription() const;
95 
96  // Return the interfaces extra class declaration code.
97  std::optional<StringRef> getExtraClassDeclaration() const;
98 
99  // Return the traits extra class declaration code.
100  std::optional<StringRef> getExtraTraitClassDeclaration() const;
101 
102  // Return the extra class declaration code shared between the interface and
103  // trait classes.
104  std::optional<StringRef> getExtraSharedClassDeclaration() const;
105 
106  // Return the extra classof method code.
107  std::optional<StringRef> getExtraClassOf() const;
108 
109  // Return the verify method body if it has one.
110  std::optional<StringRef> getVerify() const;
111 
112  // Return the base interfaces of this interface.
113  auto getBaseInterfaces() const {
114  return llvm::make_pointee_range(baseInterfaces);
115  }
116 
117  // If there's a verify method, return if it needs to access the ops in the
118  // regions.
119  bool verifyWithRegions() const;
120 
121  // Returns the Tablegen definition this interface was constructed from.
122  const llvm::Record &getDef() const { return *def; }
123 
124 private:
125  // The TableGen definition of this interface.
126  const llvm::Record *def;
127 
128  // The methods of this interface.
130 
131  // The base interfaces of this interface.
133 };
134 
135 // An interface that is registered to an Attribute.
136 struct AttrInterface : public Interface {
137  using Interface::Interface;
138 
139  static bool classof(const Interface *interface);
140 };
141 // An interface that is registered to an Operation.
142 struct OpInterface : public Interface {
143  using Interface::Interface;
144 
145  static bool classof(const Interface *interface);
146 };
147 // An interface that is registered to a Type.
148 struct TypeInterface : public Interface {
149  using Interface::Interface;
150 
151  static bool classof(const Interface *interface);
152 };
153 } // namespace tblgen
154 } // namespace mlir
155 
156 #endif // MLIR_TABLEGEN_INTERFACES_H_
InterfaceMethod(const llvm::Record *def)
Definition: Interfaces.cpp:30
StringRef getReturnType() const
Definition: Interfaces.cpp:38
std::optional< StringRef > getDefaultImplementation() const
Definition: Interfaces.cpp:59
ArrayRef< Argument > getArguments() const
Definition: Interfaces.cpp:70
std::optional< StringRef > getBody() const
Definition: Interfaces.cpp:53
StringRef getName() const
Definition: Interfaces.cpp:43
std::optional< StringRef > getDescription() const
Definition: Interfaces.cpp:65
Interface(const llvm::Record *def)
std::optional< StringRef > getExtraClassOf() const
Definition: Interfaces.cpp:155
std::optional< StringRef > getDescription() const
Definition: Interfaces.cpp:132
std::optional< StringRef > getExtraClassDeclaration() const
Definition: Interfaces.cpp:138
std::optional< StringRef > getExtraSharedClassDeclaration() const
Definition: Interfaces.cpp:150
Interface(const Interface &rhs)
Definition: Interfaces.h:76
ArrayRef< InterfaceMethod > getMethods() const
Definition: Interfaces.cpp:129
std::optional< StringRef > getExtraTraitClassDeclaration() const
Definition: Interfaces.cpp:144
bool verifyWithRegions() const
Definition: Interfaces.cpp:169
std::optional< StringRef > getVerify() const
Definition: Interfaces.cpp:161
auto getBaseInterfaces() const
Definition: Interfaces.h:113
std::string getFullyQualifiedName() const
Definition: Interfaces.cpp:115
StringRef getCppNamespace() const
Definition: Interfaces.cpp:124
const llvm::Record & getDef() const
Definition: Interfaces.h:122
StringRef getName() const
Definition: Interfaces.cpp:110
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:177
static bool classof(const Interface *interface)
Definition: Interfaces.cpp:185
static bool classof(const Interface *interface)
Definition: Interfaces.cpp:193