xref: /llvm-project/mlir/lib/TableGen/Property.cpp (revision d8399d5dd6a5a7025621eddd97fc0fa1f494bad8)
1 //===- Property.cpp - Property wrapper class ----------------------------===//
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 // Property wrapper to simplify using TableGen Record defining a MLIR
10 // Property.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/TableGen/Property.h"
15 #include "mlir/TableGen/Format.h"
16 #include "mlir/TableGen/Operator.h"
17 #include "mlir/TableGen/Predicate.h"
18 #include "llvm/TableGen/Record.h"
19 
20 using namespace mlir;
21 using namespace mlir::tblgen;
22 
23 using llvm::DefInit;
24 using llvm::Init;
25 using llvm::Record;
26 using llvm::StringInit;
27 
28 // Returns the initializer's value as string if the given TableGen initializer
29 // is a code or string initializer. Returns the empty StringRef otherwise.
30 static StringRef getValueAsString(const Init *init) {
31   if (const auto *str = dyn_cast<StringInit>(init))
32     return str->getValue().trim();
33   return {};
34 }
35 
36 Property::Property(const Record *def)
37     : Property(
38           getValueAsString(def->getValueInit("summary")),
39           getValueAsString(def->getValueInit("description")),
40           getValueAsString(def->getValueInit("storageType")),
41           getValueAsString(def->getValueInit("interfaceType")),
42           getValueAsString(def->getValueInit("convertFromStorage")),
43           getValueAsString(def->getValueInit("assignToStorage")),
44           getValueAsString(def->getValueInit("convertToAttribute")),
45           getValueAsString(def->getValueInit("convertFromAttribute")),
46           getValueAsString(def->getValueInit("parser")),
47           getValueAsString(def->getValueInit("optionalParser")),
48           getValueAsString(def->getValueInit("printer")),
49           getValueAsString(def->getValueInit("readFromMlirBytecode")),
50           getValueAsString(def->getValueInit("writeToMlirBytecode")),
51           getValueAsString(def->getValueInit("hashProperty")),
52           getValueAsString(def->getValueInit("defaultValue")),
53           getValueAsString(def->getValueInit("storageTypeValueOverride"))) {
54   this->def = def;
55   assert((def->isSubClassOf("Property") || def->isSubClassOf("Attr")) &&
56          "must be subclass of TableGen 'Property' class");
57 }
58 
59 Property::Property(const DefInit *init) : Property(init->getDef()) {}
60 
61 Property::Property(StringRef summary, StringRef description,
62                    StringRef storageType, StringRef interfaceType,
63                    StringRef convertFromStorageCall,
64                    StringRef assignToStorageCall,
65                    StringRef convertToAttributeCall,
66                    StringRef convertFromAttributeCall, StringRef parserCall,
67                    StringRef optionalParserCall, StringRef printerCall,
68                    StringRef readFromMlirBytecodeCall,
69                    StringRef writeToMlirBytecodeCall,
70                    StringRef hashPropertyCall, StringRef defaultValue,
71                    StringRef storageTypeValueOverride)
72     : def(nullptr), summary(summary), description(description),
73       storageType(storageType), interfaceType(interfaceType),
74       convertFromStorageCall(convertFromStorageCall),
75       assignToStorageCall(assignToStorageCall),
76       convertToAttributeCall(convertToAttributeCall),
77       convertFromAttributeCall(convertFromAttributeCall),
78       parserCall(parserCall), optionalParserCall(optionalParserCall),
79       printerCall(printerCall),
80       readFromMlirBytecodeCall(readFromMlirBytecodeCall),
81       writeToMlirBytecodeCall(writeToMlirBytecodeCall),
82       hashPropertyCall(hashPropertyCall), defaultValue(defaultValue),
83       storageTypeValueOverride(storageTypeValueOverride) {
84   if (storageType.empty())
85     storageType = "Property";
86 }
87 
88 StringRef Property::getPropertyDefName() const {
89   if (def->isAnonymous()) {
90     return getBaseProperty().def->getName();
91   }
92   return def->getName();
93 }
94 
95 Pred Property::getPredicate() const {
96   if (!def)
97     return Pred();
98   const llvm::RecordVal *maybePred = def->getValue("predicate");
99   if (!maybePred || !maybePred->getValue())
100     return Pred();
101   return Pred(maybePred->getValue());
102 }
103 
104 Property Property::getBaseProperty() const {
105   if (const auto *defInit =
106           llvm::dyn_cast<llvm::DefInit>(def->getValueInit("baseProperty"))) {
107     return Property(defInit).getBaseProperty();
108   }
109   return *this;
110 }
111