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