1 //===- Trait.cpp ----------------------------------------------------------===// 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 // Trait wrapper to simplify using TableGen Record defining a MLIR Trait. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/TableGen/Trait.h" 14 #include "mlir/TableGen/Interfaces.h" 15 #include "mlir/TableGen/Predicate.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/Support/FormatVariadic.h" 18 #include "llvm/TableGen/Error.h" 19 #include "llvm/TableGen/Record.h" 20 21 using namespace mlir; 22 using namespace mlir::tblgen; 23 24 //===----------------------------------------------------------------------===// 25 // Trait 26 //===----------------------------------------------------------------------===// 27 28 Trait Trait::create(const llvm::Init *init) { 29 auto def = cast<llvm::DefInit>(init)->getDef(); 30 if (def->isSubClassOf("PredTrait")) 31 return Trait(Kind::Pred, def); 32 if (def->isSubClassOf("GenInternalTrait")) 33 return Trait(Kind::Internal, def); 34 if (def->isSubClassOf("InterfaceTrait")) 35 return Trait(Kind::Interface, def); 36 assert(def->isSubClassOf("NativeTrait")); 37 return Trait(Kind::Native, def); 38 } 39 40 Trait::Trait(Kind kind, const llvm::Record *def) : def(def), kind(kind) {} 41 42 //===----------------------------------------------------------------------===// 43 // NativeTrait 44 //===----------------------------------------------------------------------===// 45 46 std::string NativeTrait::getFullyQualifiedTraitName() const { 47 llvm::StringRef trait = def->getValueAsString("trait"); 48 llvm::StringRef cppNamespace = def->getValueAsString("cppNamespace"); 49 return cppNamespace.empty() ? trait.str() 50 : (cppNamespace + "::" + trait).str(); 51 } 52 53 //===----------------------------------------------------------------------===// 54 // InternalTrait 55 //===----------------------------------------------------------------------===// 56 57 llvm::StringRef InternalTrait::getFullyQualifiedTraitName() const { 58 return def->getValueAsString("trait"); 59 } 60 61 //===----------------------------------------------------------------------===// 62 // PredTrait 63 //===----------------------------------------------------------------------===// 64 65 std::string PredTrait::getPredTemplate() const { 66 auto pred = Pred(def->getValueInit("predicate")); 67 return pred.getCondition(); 68 } 69 70 llvm::StringRef PredTrait::getSummary() const { 71 return def->getValueAsString("summary"); 72 } 73 74 //===----------------------------------------------------------------------===// 75 // InterfaceTrait 76 //===----------------------------------------------------------------------===// 77 78 Interface InterfaceTrait::getInterface() const { return Interface(def); } 79 80 std::string InterfaceTrait::getFullyQualifiedTraitName() const { 81 llvm::StringRef trait = def->getValueAsString("trait"); 82 llvm::StringRef cppNamespace = def->getValueAsString("cppNamespace"); 83 return cppNamespace.empty() ? trait.str() 84 : (cppNamespace + "::" + trait).str(); 85 } 86 87 bool InterfaceTrait::shouldDeclareMethods() const { 88 return def->isSubClassOf("DeclareInterfaceMethods"); 89 } 90 91 std::vector<StringRef> InterfaceTrait::getAlwaysDeclaredMethods() const { 92 return def->getValueAsListOfStrings("alwaysOverriddenMethods"); 93 } 94