1 //===- Interfaces.cpp - Interface classes ---------------------------------===// 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 #include "mlir/TableGen/Interfaces.h" 10 #include "llvm/ADT/StringExtras.h" 11 #include "llvm/Support/FormatVariadic.h" 12 #include "llvm/TableGen/Error.h" 13 #include "llvm/TableGen/Record.h" 14 15 using namespace mlir; 16 using namespace mlir::tblgen; 17 18 //===----------------------------------------------------------------------===// 19 // InterfaceMethod 20 //===----------------------------------------------------------------------===// 21 InterfaceMethod(const llvm::Record * def)22InterfaceMethod::InterfaceMethod(const llvm::Record *def) : def(def) { 23 llvm::DagInit *args = def->getValueAsDag("arguments"); 24 for (unsigned i = 0, e = args->getNumArgs(); i != e; ++i) { 25 arguments.push_back( 26 {llvm::cast<llvm::StringInit>(args->getArg(i))->getValue(), 27 args->getArgNameStr(i)}); 28 } 29 } 30 getReturnType() const31StringRef InterfaceMethod::getReturnType() const { 32 return def->getValueAsString("returnType"); 33 } 34 35 // Return the name of this method. getName() const36StringRef InterfaceMethod::getName() const { 37 return def->getValueAsString("name"); 38 } 39 40 // Return if this method is static. isStatic() const41bool InterfaceMethod::isStatic() const { 42 return def->isSubClassOf("StaticInterfaceMethod"); 43 } 44 45 // Return the body for this method if it has one. getBody() const46llvm::Optional<StringRef> InterfaceMethod::getBody() const { 47 auto value = def->getValueAsString("body"); 48 return value.empty() ? llvm::Optional<StringRef>() : value; 49 } 50 51 // Return the default implementation for this method if it has one. getDefaultImplementation() const52llvm::Optional<StringRef> InterfaceMethod::getDefaultImplementation() const { 53 auto value = def->getValueAsString("defaultBody"); 54 return value.empty() ? llvm::Optional<StringRef>() : value; 55 } 56 57 // Return the description of this method if it has one. getDescription() const58llvm::Optional<StringRef> InterfaceMethod::getDescription() const { 59 auto value = def->getValueAsString("description"); 60 return value.empty() ? llvm::Optional<StringRef>() : value; 61 } 62 getArguments() const63ArrayRef<InterfaceMethod::Argument> InterfaceMethod::getArguments() const { 64 return arguments; 65 } 66 arg_empty() const67bool InterfaceMethod::arg_empty() const { return arguments.empty(); } 68 69 //===----------------------------------------------------------------------===// 70 // Interface 71 //===----------------------------------------------------------------------===// 72 Interface(const llvm::Record * def)73Interface::Interface(const llvm::Record *def) : def(def) { 74 assert(def->isSubClassOf("Interface") && 75 "must be subclass of TableGen 'Interface' class"); 76 77 auto *listInit = dyn_cast<llvm::ListInit>(def->getValueInit("methods")); 78 for (llvm::Init *init : listInit->getValues()) 79 methods.emplace_back(cast<llvm::DefInit>(init)->getDef()); 80 } 81 82 // Return the name of this interface. getName() const83StringRef Interface::getName() const { 84 return def->getValueAsString("cppClassName"); 85 } 86 87 // Return the C++ namespace of this interface. getCppNamespace() const88StringRef Interface::getCppNamespace() const { 89 return def->getValueAsString("cppNamespace"); 90 } 91 92 // Return the methods of this interface. getMethods() const93ArrayRef<InterfaceMethod> Interface::getMethods() const { return methods; } 94 95 // Return the description of this method if it has one. getDescription() const96llvm::Optional<StringRef> Interface::getDescription() const { 97 auto value = def->getValueAsString("description"); 98 return value.empty() ? llvm::Optional<StringRef>() : value; 99 } 100 101 // Return the interfaces extra class declaration code. getExtraClassDeclaration() const102llvm::Optional<StringRef> Interface::getExtraClassDeclaration() const { 103 auto value = def->getValueAsString("extraClassDeclaration"); 104 return value.empty() ? llvm::Optional<StringRef>() : value; 105 } 106 107 // Return the traits extra class declaration code. getExtraTraitClassDeclaration() const108llvm::Optional<StringRef> Interface::getExtraTraitClassDeclaration() const { 109 auto value = def->getValueAsString("extraTraitClassDeclaration"); 110 return value.empty() ? llvm::Optional<StringRef>() : value; 111 } 112 113 // Return the body for this method if it has one. getVerify() const114llvm::Optional<StringRef> Interface::getVerify() const { 115 // Only OpInterface supports the verify method. 116 if (!isa<OpInterface>(this)) 117 return llvm::None; 118 auto value = def->getValueAsString("verify"); 119 return value.empty() ? llvm::Optional<StringRef>() : value; 120 } 121 122 //===----------------------------------------------------------------------===// 123 // AttrInterface 124 //===----------------------------------------------------------------------===// 125 classof(const Interface * interface)126bool AttrInterface::classof(const Interface *interface) { 127 return interface->getDef().isSubClassOf("AttrInterface"); 128 } 129 130 //===----------------------------------------------------------------------===// 131 // OpInterface 132 //===----------------------------------------------------------------------===// 133 classof(const Interface * interface)134bool OpInterface::classof(const Interface *interface) { 135 return interface->getDef().isSubClassOf("OpInterface"); 136 } 137 138 //===----------------------------------------------------------------------===// 139 // TypeInterface 140 //===----------------------------------------------------------------------===// 141 classof(const Interface * interface)142bool TypeInterface::classof(const Interface *interface) { 143 return interface->getDef().isSubClassOf("TypeInterface"); 144 } 145