MLIR  21.0.0git
Property.cpp
Go to the documentation of this file.
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"
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 
37  return getValueAsString(def->getValueInit("interfaceType"));
38 }
39 
40 Property::Property(const Record *def)
41  : Property(
42  def, getValueAsString(def->getValueInit("summary")),
43  getValueAsString(def->getValueInit("description")),
44  getValueAsString(def->getValueInit("storageType")),
45  getValueAsString(def->getValueInit("interfaceType")),
46  getValueAsString(def->getValueInit("convertFromStorage")),
47  getValueAsString(def->getValueInit("assignToStorage")),
48  getValueAsString(def->getValueInit("convertToAttribute")),
49  getValueAsString(def->getValueInit("convertFromAttribute")),
50  getValueAsString(def->getValueInit("parser")),
51  getValueAsString(def->getValueInit("optionalParser")),
52  getValueAsString(def->getValueInit("printer")),
53  getValueAsString(def->getValueInit("readFromMlirBytecode")),
54  getValueAsString(def->getValueInit("writeToMlirBytecode")),
55  getValueAsString(def->getValueInit("hashProperty")),
56  getValueAsString(def->getValueInit("defaultValue")),
57  getValueAsString(def->getValueInit("storageTypeValueOverride"))) {
58  assert((def->isSubClassOf("Property") || def->isSubClassOf("Attr")) &&
59  "must be subclass of TableGen 'Property' class");
60 }
61 
62 Property::Property(const DefInit *init) : Property(init->getDef()) {}
63 
64 Property::Property(const llvm::Record *maybeDef, StringRef summary,
65  StringRef description, StringRef storageType,
66  StringRef interfaceType, StringRef convertFromStorageCall,
67  StringRef assignToStorageCall,
68  StringRef convertToAttributeCall,
69  StringRef convertFromAttributeCall, StringRef parserCall,
70  StringRef optionalParserCall, StringRef printerCall,
71  StringRef readFromMlirBytecodeCall,
72  StringRef writeToMlirBytecodeCall,
73  StringRef hashPropertyCall, StringRef defaultValue,
74  StringRef storageTypeValueOverride)
75  : PropConstraint(maybeDef, Constraint::CK_Prop), summary(summary),
76  description(description), storageType(storageType),
77  interfaceType(interfaceType),
78  convertFromStorageCall(convertFromStorageCall),
79  assignToStorageCall(assignToStorageCall),
80  convertToAttributeCall(convertToAttributeCall),
81  convertFromAttributeCall(convertFromAttributeCall),
82  parserCall(parserCall), optionalParserCall(optionalParserCall),
83  printerCall(printerCall),
84  readFromMlirBytecodeCall(readFromMlirBytecodeCall),
85  writeToMlirBytecodeCall(writeToMlirBytecodeCall),
86  hashPropertyCall(hashPropertyCall), defaultValue(defaultValue),
87  storageTypeValueOverride(storageTypeValueOverride) {
88  if (storageType.empty())
89  storageType = "Property";
90 }
91 
92 StringRef Property::getPropertyDefName() const {
93  if (def->isAnonymous()) {
94  return getBaseProperty().def->getName();
95  }
96  return def->getName();
97 }
98 
100  if (!def)
101  return Pred();
102  const llvm::RecordVal *maybePred = def->getValue("predicate");
103  if (!maybePred || !maybePred->getValue())
104  return Pred();
105  return Pred(maybePred->getValue());
106 }
107 
109  if (const auto *defInit =
110  llvm::dyn_cast<llvm::DefInit>(def->getValueInit("baseProperty"))) {
111  return Property(defInit).getBaseProperty();
112  }
113  return *this;
114 }
static StringRef getValueAsString(const Init *init)
Definition: Property.cpp:30
const llvm::Record * def
Definition: Constraint.h:90
StringRef getInterfaceType() const
Definition: Property.cpp:36
StringRef getPropertyDefName() const
Definition: Property.cpp:92
Pred getPredicate() const
Definition: Property.cpp:99
Property(const llvm::Record *def)
Property getBaseProperty() const
Definition: Property.cpp:108
Include the generated interface declarations.