1 //===- Trait.cpp ----------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Trait wrapper to simplify using TableGen Record defining a MLIR Trait.
11 //===----------------------------------------------------------------------===//
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"
22 using namespace mlir::tblgen
;
24 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
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
);
40 Trait::Trait(Kind kind
, const llvm::Record
*def
) : def(def
), kind(kind
) {}
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
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();
53 bool NativeTrait::isStructuralOpTrait() const {
54 return def
->isSubClassOf("StructuralOpTrait");
57 StringRef
NativeTrait::getExtraConcreteClassDeclaration() const {
58 return def
->getValueAsString("extraConcreteClassDeclaration");
61 StringRef
NativeTrait::getExtraConcreteClassDefinition() const {
62 return def
->getValueAsString("extraConcreteClassDefinition");
65 //===----------------------------------------------------------------------===//
67 //===----------------------------------------------------------------------===//
69 llvm::StringRef
InternalTrait::getFullyQualifiedTraitName() const {
70 return def
->getValueAsString("trait");
73 //===----------------------------------------------------------------------===//
75 //===----------------------------------------------------------------------===//
77 std::string
PredTrait::getPredTemplate() const {
78 auto pred
= Pred(def
->getValueInit("predicate"));
79 return pred
.getCondition();
82 llvm::StringRef
PredTrait::getSummary() const {
83 return def
->getValueAsString("summary");
86 //===----------------------------------------------------------------------===//
88 //===----------------------------------------------------------------------===//
90 Interface
InterfaceTrait::getInterface() const { return Interface(def
); }
92 std::string
InterfaceTrait::getFullyQualifiedTraitName() const {
93 llvm::StringRef trait
= def
->getValueAsString("trait");
94 llvm::StringRef cppNamespace
= def
->getValueAsString("cppNamespace");
95 return cppNamespace
.empty() ? trait
.str()
96 : (cppNamespace
+ "::" + trait
).str();
99 bool InterfaceTrait::shouldDeclareMethods() const {
100 return def
->isSubClassOf("DeclareInterfaceMethods");
103 std::vector
<StringRef
> InterfaceTrait::getAlwaysDeclaredMethods() const {
104 return def
->getValueAsListOfStrings("alwaysOverriddenMethods");