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. 27 class InterfaceMethod { 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. 56 ArrayRef<Argument> getArguments() const; 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); Interface(const Interface & rhs)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. 91 ArrayRef<InterfaceMethod> getMethods() const; 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. getBaseInterfaces()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. getDef()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. 129 SmallVector<InterfaceMethod, 8> methods; 130 131 // The base interfaces of this interface. 132 SmallVector<std::unique_ptr<Interface>> baseInterfaces; 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_ 157