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 
create(const llvm::Init * init)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 
Trait(Kind kind,const llvm::Record * def)40 Trait::Trait(Kind kind, const llvm::Record *def) : def(def), kind(kind) {}
41 
42 //===----------------------------------------------------------------------===//
43 // NativeTrait
44 //===----------------------------------------------------------------------===//
45 
getFullyQualifiedTraitName() const46 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();
51 }
52 
isStructuralOpTrait() const53 bool NativeTrait::isStructuralOpTrait() const {
54   return def->isSubClassOf("StructuralOpTrait");
55 }
56 
57 //===----------------------------------------------------------------------===//
58 // InternalTrait
59 //===----------------------------------------------------------------------===//
60 
getFullyQualifiedTraitName() const61 llvm::StringRef InternalTrait::getFullyQualifiedTraitName() const {
62   return def->getValueAsString("trait");
63 }
64 
65 //===----------------------------------------------------------------------===//
66 // PredTrait
67 //===----------------------------------------------------------------------===//
68 
getPredTemplate() const69 std::string PredTrait::getPredTemplate() const {
70   auto pred = Pred(def->getValueInit("predicate"));
71   return pred.getCondition();
72 }
73 
getSummary() const74 llvm::StringRef PredTrait::getSummary() const {
75   return def->getValueAsString("summary");
76 }
77 
78 //===----------------------------------------------------------------------===//
79 // InterfaceTrait
80 //===----------------------------------------------------------------------===//
81 
getInterface() const82 Interface InterfaceTrait::getInterface() const { return Interface(def); }
83 
getFullyQualifiedTraitName() const84 std::string InterfaceTrait::getFullyQualifiedTraitName() const {
85   llvm::StringRef trait = def->getValueAsString("trait");
86   llvm::StringRef cppNamespace = def->getValueAsString("cppNamespace");
87   return cppNamespace.empty() ? trait.str()
88                               : (cppNamespace + "::" + trait).str();
89 }
90 
shouldDeclareMethods() const91 bool InterfaceTrait::shouldDeclareMethods() const {
92   return def->isSubClassOf("DeclareInterfaceMethods");
93 }
94 
getAlwaysDeclaredMethods() const95 std::vector<StringRef> InterfaceTrait::getAlwaysDeclaredMethods() const {
96   return def->getValueAsListOfStrings("alwaysOverriddenMethods");
97 }
98