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