1 //===- Property.cpp - Property wrapper class ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Property wrapper to simplify using TableGen Record defining a MLIR
12 //===----------------------------------------------------------------------===//
14 #include "mlir/TableGen/Property.h"
15 #include "mlir/TableGen/Format.h"
16 #include "mlir/TableGen/Operator.h"
17 #include "llvm/TableGen/Record.h"
20 using namespace mlir::tblgen
;
25 using llvm::StringInit
;
27 // Returns the initializer's value as string if the given TableGen initializer
28 // is a code or string initializer. Returns the empty StringRef otherwise.
29 static StringRef
getValueAsString(const Init
*init
) {
30 if (const auto *str
= dyn_cast
<StringInit
>(init
))
31 return str
->getValue().trim();
35 Property::Property(const Record
*def
)
37 getValueAsString(def
->getValueInit("summary")),
38 getValueAsString(def
->getValueInit("description")),
39 getValueAsString(def
->getValueInit("storageType")),
40 getValueAsString(def
->getValueInit("interfaceType")),
41 getValueAsString(def
->getValueInit("convertFromStorage")),
42 getValueAsString(def
->getValueInit("assignToStorage")),
43 getValueAsString(def
->getValueInit("convertToAttribute")),
44 getValueAsString(def
->getValueInit("convertFromAttribute")),
45 getValueAsString(def
->getValueInit("parser")),
46 getValueAsString(def
->getValueInit("optionalParser")),
47 getValueAsString(def
->getValueInit("printer")),
48 getValueAsString(def
->getValueInit("readFromMlirBytecode")),
49 getValueAsString(def
->getValueInit("writeToMlirBytecode")),
50 getValueAsString(def
->getValueInit("hashProperty")),
51 getValueAsString(def
->getValueInit("defaultValue")),
52 getValueAsString(def
->getValueInit("storageTypeValueOverride"))) {
54 assert((def
->isSubClassOf("Property") || def
->isSubClassOf("Attr")) &&
55 "must be subclass of TableGen 'Property' class");
58 Property::Property(const DefInit
*init
) : Property(init
->getDef()) {}
60 Property::Property(StringRef summary
, StringRef description
,
61 StringRef storageType
, StringRef interfaceType
,
62 StringRef convertFromStorageCall
,
63 StringRef assignToStorageCall
,
64 StringRef convertToAttributeCall
,
65 StringRef convertFromAttributeCall
, StringRef parserCall
,
66 StringRef optionalParserCall
, StringRef printerCall
,
67 StringRef readFromMlirBytecodeCall
,
68 StringRef writeToMlirBytecodeCall
,
69 StringRef hashPropertyCall
, StringRef defaultValue
,
70 StringRef storageTypeValueOverride
)
71 : summary(summary
), description(description
), storageType(storageType
),
72 interfaceType(interfaceType
),
73 convertFromStorageCall(convertFromStorageCall
),
74 assignToStorageCall(assignToStorageCall
),
75 convertToAttributeCall(convertToAttributeCall
),
76 convertFromAttributeCall(convertFromAttributeCall
),
77 parserCall(parserCall
), optionalParserCall(optionalParserCall
),
78 printerCall(printerCall
),
79 readFromMlirBytecodeCall(readFromMlirBytecodeCall
),
80 writeToMlirBytecodeCall(writeToMlirBytecodeCall
),
81 hashPropertyCall(hashPropertyCall
), defaultValue(defaultValue
),
82 storageTypeValueOverride(storageTypeValueOverride
) {
83 if (storageType
.empty())
84 storageType
= "Property";
87 StringRef
Property::getPropertyDefName() const {
88 if (def
->isAnonymous()) {
89 return getBaseProperty().def
->getName();
91 return def
->getName();
94 Property
Property::getBaseProperty() const {
95 if (const auto *defInit
=
96 llvm::dyn_cast
<llvm::DefInit
>(def
->getValueInit("baseProperty"))) {
97 return Property(defInit
).getBaseProperty();