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>(Val: init)->getDef(); |
30 | if (def->isSubClassOf(Name: "PredTrait" )) |
31 | return Trait(Kind::Pred, def); |
32 | if (def->isSubClassOf(Name: "GenInternalTrait" )) |
33 | return Trait(Kind::Internal, def); |
34 | if (def->isSubClassOf(Name: "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(FieldName: "trait" ); |
48 | llvm::StringRef cppNamespace = def->getValueAsString(FieldName: "cppNamespace" ); |
49 | return cppNamespace.empty() ? trait.str() |
50 | : (cppNamespace + "::" + trait).str(); |
51 | } |
52 | |
53 | bool NativeTrait::isStructuralOpTrait() const { |
54 | return def->isSubClassOf(Name: "StructuralOpTrait" ); |
55 | } |
56 | |
57 | StringRef NativeTrait::() const { |
58 | return def->getValueAsString(FieldName: "extraConcreteClassDeclaration" ); |
59 | } |
60 | |
61 | StringRef NativeTrait::() const { |
62 | return def->getValueAsString(FieldName: "extraConcreteClassDefinition" ); |
63 | } |
64 | |
65 | //===----------------------------------------------------------------------===// |
66 | // InternalTrait |
67 | //===----------------------------------------------------------------------===// |
68 | |
69 | llvm::StringRef InternalTrait::getFullyQualifiedTraitName() const { |
70 | return def->getValueAsString(FieldName: "trait" ); |
71 | } |
72 | |
73 | //===----------------------------------------------------------------------===// |
74 | // PredTrait |
75 | //===----------------------------------------------------------------------===// |
76 | |
77 | std::string PredTrait::getPredTemplate() const { |
78 | auto pred = Pred(def->getValueInit(FieldName: "predicate" )); |
79 | return pred.getCondition(); |
80 | } |
81 | |
82 | llvm::StringRef PredTrait::getSummary() const { |
83 | return def->getValueAsString(FieldName: "summary" ); |
84 | } |
85 | |
86 | //===----------------------------------------------------------------------===// |
87 | // InterfaceTrait |
88 | //===----------------------------------------------------------------------===// |
89 | |
90 | Interface InterfaceTrait::getInterface() const { return Interface(def); } |
91 | |
92 | std::string InterfaceTrait::getFullyQualifiedTraitName() const { |
93 | llvm::StringRef trait = def->getValueAsString(FieldName: "trait" ); |
94 | llvm::StringRef cppNamespace = def->getValueAsString(FieldName: "cppNamespace" ); |
95 | return cppNamespace.empty() ? trait.str() |
96 | : (cppNamespace + "::" + trait).str(); |
97 | } |
98 | |
99 | bool InterfaceTrait::shouldDeclareMethods() const { |
100 | return def->isSubClassOf(Name: "DeclareInterfaceMethods" ); |
101 | } |
102 | |
103 | std::vector<StringRef> InterfaceTrait::getAlwaysDeclaredMethods() const { |
104 | return def->getValueAsListOfStrings(FieldName: "alwaysOverriddenMethods" ); |
105 | } |
106 | |