MLIR  20.0.0git
Trait.cpp
Go to the documentation of this file.
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"
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 
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 
54  return def->isSubClassOf("StructuralOpTrait");
55 }
56 
58  return def->getValueAsString("extraConcreteClassDeclaration");
59 }
60 
62  return def->getValueAsString("extraConcreteClassDefinition");
63 }
64 
65 //===----------------------------------------------------------------------===//
66 // InternalTrait
67 //===----------------------------------------------------------------------===//
68 
70  return def->getValueAsString("trait");
71 }
72 
73 //===----------------------------------------------------------------------===//
74 // PredTrait
75 //===----------------------------------------------------------------------===//
76 
77 std::string PredTrait::getPredTemplate() const {
78  auto pred = Pred(def->getValueInit("predicate"));
79  return pred.getCondition();
80 }
81 
82 llvm::StringRef PredTrait::getSummary() const {
83  return def->getValueAsString("summary");
84 }
85 
86 //===----------------------------------------------------------------------===//
87 // InterfaceTrait
88 //===----------------------------------------------------------------------===//
89 
91 
93  llvm::StringRef trait = def->getValueAsString("trait");
94  llvm::StringRef cppNamespace = def->getValueAsString("cppNamespace");
95  return cppNamespace.empty() ? trait.str()
96  : (cppNamespace + "::" + trait).str();
97 }
98 
100  return def->isSubClassOf("DeclareInterfaceMethods");
101 }
102 
103 std::vector<StringRef> InterfaceTrait::getAlwaysDeclaredMethods() const {
104  return def->getValueAsListOfStrings("alwaysOverriddenMethods");
105 }
std::vector< StringRef > getAlwaysDeclaredMethods() const
Definition: Trait.cpp:103
std::string getFullyQualifiedTraitName() const
Definition: Trait.cpp:92
Interface getInterface() const
Definition: Trait.cpp:90
bool shouldDeclareMethods() const
Definition: Trait.cpp:99
StringRef getFullyQualifiedTraitName() const
Definition: Trait.cpp:69
bool isStructuralOpTrait() const
Definition: Trait.cpp:53
std::string getFullyQualifiedTraitName() const
Definition: Trait.cpp:46
StringRef getExtraConcreteClassDeclaration() const
Definition: Trait.cpp:57
StringRef getExtraConcreteClassDefinition() const
Definition: Trait.cpp:61
StringRef getSummary() const
Definition: Trait.cpp:82
std::string getPredTemplate() const
Definition: Trait.cpp:77
const llvm::Record * def
Definition: Trait.h:58
Trait(Kind kind, const llvm::Record *def)
Definition: Trait.cpp:40
static Trait create(const llvm::Init *init)
Definition: Trait.cpp:28
Include the generated interface declarations.