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/Operator.h"
16#include "mlir/TableGen/Predicate.h"
17#include "llvm/TableGen/Record.h"
18
19using namespace mlir;
20using namespace mlir::tblgen;
21
22using llvm::DefInit;
23using llvm::Init;
24using llvm::Record;
25using llvm::StringInit;
26
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.
29static StringRef getValueAsString(const Init *init) {
30 if (const auto *str = dyn_cast<StringInit>(Val: init))
31 return str->getValue().trim();
32 return {};
33}
34
35StringRef PropConstraint::getInterfaceType() const {
36 return getValueAsString(init: def->getValueInit(FieldName: "interfaceType"));
37}
38
39Property::Property(const Record *def)
40 : Property(
41 def, getValueAsString(init: def->getValueInit(FieldName: "summary")),
42 getValueAsString(init: def->getValueInit(FieldName: "description")),
43 getValueAsString(init: def->getValueInit(FieldName: "storageType")),
44 getValueAsString(init: def->getValueInit(FieldName: "interfaceType")),
45 getValueAsString(init: def->getValueInit(FieldName: "convertFromStorage")),
46 getValueAsString(init: def->getValueInit(FieldName: "assignToStorage")),
47 getValueAsString(init: def->getValueInit(FieldName: "convertToAttribute")),
48 getValueAsString(init: def->getValueInit(FieldName: "convertFromAttribute")),
49 getValueAsString(init: def->getValueInit(FieldName: "parser")),
50 getValueAsString(init: def->getValueInit(FieldName: "optionalParser")),
51 getValueAsString(init: def->getValueInit(FieldName: "printer")),
52 getValueAsString(init: def->getValueInit(FieldName: "readFromMlirBytecode")),
53 getValueAsString(init: def->getValueInit(FieldName: "writeToMlirBytecode")),
54 getValueAsString(init: def->getValueInit(FieldName: "hashProperty")),
55 getValueAsString(init: def->getValueInit(FieldName: "defaultValue")),
56 getValueAsString(init: def->getValueInit(FieldName: "storageTypeValueOverride"))) {
57 assert((def->isSubClassOf("Property") || def->isSubClassOf("Attr")) &&
58 "must be subclass of TableGen 'Property' class");
59}
60
61Property::Property(const DefInit *init) : Property(init->getDef()) {}
62
63Property::Property(const llvm::Record *maybeDef, StringRef summary,
64 StringRef description, StringRef storageType,
65 StringRef interfaceType, StringRef convertFromStorageCall,
66 StringRef assignToStorageCall,
67 StringRef convertToAttributeCall,
68 StringRef convertFromAttributeCall, StringRef parserCall,
69 StringRef optionalParserCall, StringRef printerCall,
70 StringRef readFromMlirBytecodeCall,
71 StringRef writeToMlirBytecodeCall,
72 StringRef hashPropertyCall, StringRef defaultValue,
73 StringRef storageTypeValueOverride)
74 : PropConstraint(maybeDef, Constraint::CK_Prop), summary(summary),
75 description(description), storageType(storageType),
76 interfaceType(interfaceType),
77 convertFromStorageCall(convertFromStorageCall),
78 assignToStorageCall(assignToStorageCall),
79 convertToAttributeCall(convertToAttributeCall),
80 convertFromAttributeCall(convertFromAttributeCall),
81 parserCall(parserCall), optionalParserCall(optionalParserCall),
82 printerCall(printerCall),
83 readFromMlirBytecodeCall(readFromMlirBytecodeCall),
84 writeToMlirBytecodeCall(writeToMlirBytecodeCall),
85 hashPropertyCall(hashPropertyCall), defaultValue(defaultValue),
86 storageTypeValueOverride(storageTypeValueOverride) {
87 if (storageType.empty())
88 storageType = "Property";
89}
90
91StringRef Property::getPropertyDefName() const {
92 if (def->isAnonymous()) {
93 return getBaseProperty().def->getName();
94 }
95 return def->getName();
96}
97
98Pred Property::getPredicate() const {
99 if (!def)
100 return Pred();
101 const llvm::RecordVal *maybePred = def->getValue(Name: "predicate");
102 if (!maybePred)
103 return Pred();
104 return Pred(maybePred->getValue());
105}
106
107bool Property::usesDefaultParser() const {
108 const Record *propertyClass = def->getRecords().getClass(Name: "Property");
109 if (const auto *baseInit =
110 llvm::dyn_cast<DefInit>(Val: def->getValueInit(FieldName: "baseProperty"))) {
111 Property baseProperty(baseInit);
112 if (getParserCall() == baseProperty.getParserCall())
113 return baseProperty.usesDefaultParser();
114 }
115 // RecordVal retains the source location of the initializer that supplied a
116 // field. An inherited parser therefore points at Property::parser, while an
117 // explicit `let parser` points at the override without inspecting its text.
118 return def->getValue(Name: "parser")->getLoc().getPointer() ==
119 propertyClass->getValue(Name: "parser")->getLoc().getPointer();
120}
121
122Property Property::getBaseProperty() const {
123 if (const auto *defInit =
124 llvm::dyn_cast<llvm::DefInit>(Val: def->getValueInit(FieldName: "baseProperty"))) {
125 return Property(defInit).getBaseProperty();
126 }
127 return *this;
128}
129
130bool Property::isSubClassOf(StringRef className) const {
131 return def && def->isSubClassOf(Name: className);
132}
133
134StringRef ConstantProp::getValue() const {
135 return def->getValueAsString(FieldName: "value");
136}
137