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 22 InterfaceMethod::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 31 StringRef InterfaceMethod::getReturnType() const { 32 return def->getValueAsString("returnType"); 33 } 34 35 // Return the name of this method. 36 StringRef InterfaceMethod::getName() const { 37 return def->getValueAsString("name"); 38 } 39 40 // Return if this method is static. 41 bool InterfaceMethod::isStatic() const { 42 return def->isSubClassOf("StaticInterfaceMethod"); 43 } 44 45 // Return the body for this method if it has one. 46 llvm::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. 52 llvm::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. 58 llvm::Optional<StringRef> InterfaceMethod::getDescription() const { 59 auto value = def->getValueAsString("description"); 60 return value.empty() ? llvm::Optional<StringRef>() : value; 61 } 62 63 ArrayRef<InterfaceMethod::Argument> InterfaceMethod::getArguments() const { 64 return arguments; 65 } 66 67 bool InterfaceMethod::arg_empty() const { return arguments.empty(); } 68 69 //===----------------------------------------------------------------------===// 70 // Interface 71 //===----------------------------------------------------------------------===// 72 73 Interface::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. 83 StringRef Interface::getName() const { 84 return def->getValueAsString("cppClassName"); 85 } 86 87 // Return the methods of this interface. 88 ArrayRef<InterfaceMethod> Interface::getMethods() const { return methods; } 89 90 // Return the description of this method if it has one. 91 llvm::Optional<StringRef> Interface::getDescription() const { 92 auto value = def->getValueAsString("description"); 93 return value.empty() ? llvm::Optional<StringRef>() : value; 94 } 95 96 // Return the interfaces extra class declaration code. 97 llvm::Optional<StringRef> Interface::getExtraClassDeclaration() const { 98 auto value = def->getValueAsString("extraClassDeclaration"); 99 return value.empty() ? llvm::Optional<StringRef>() : value; 100 } 101 102 // Return the traits extra class declaration code. 103 llvm::Optional<StringRef> Interface::getExtraTraitClassDeclaration() const { 104 auto value = def->getValueAsString("extraTraitClassDeclaration"); 105 return value.empty() ? llvm::Optional<StringRef>() : value; 106 } 107 108 // Return the body for this method if it has one. 109 llvm::Optional<StringRef> Interface::getVerify() const { 110 // Only OpInterface supports the verify method. 111 if (!isa<OpInterface>(this)) 112 return llvm::None; 113 auto value = def->getValueAsString("verify"); 114 return value.empty() ? llvm::Optional<StringRef>() : value; 115 } 116 117 //===----------------------------------------------------------------------===// 118 // AttrInterface 119 //===----------------------------------------------------------------------===// 120 121 bool AttrInterface::classof(const Interface *interface) { 122 return interface->getDef().isSubClassOf("AttrInterface"); 123 } 124 125 //===----------------------------------------------------------------------===// 126 // OpInterface 127 //===----------------------------------------------------------------------===// 128 129 bool OpInterface::classof(const Interface *interface) { 130 return interface->getDef().isSubClassOf("OpInterface"); 131 } 132 133 //===----------------------------------------------------------------------===// 134 // TypeInterface 135 //===----------------------------------------------------------------------===// 136 137 bool TypeInterface::classof(const Interface *interface) { 138 return interface->getDef().isSubClassOf("TypeInterface"); 139 } 140