MLIR  20.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"
17 #include "llvm/TableGen/Record.h"
18 
19 using namespace mlir;
20 using namespace mlir::tblgen;
21 
22 using llvm::DefInit;
23 using llvm::Init;
24 using llvm::Record;
25 using 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.
29 static StringRef getValueAsString(const Init *init) {
30  if (const auto *str = dyn_cast<StringInit>(init))
31  return str->getValue().trim();
32  return {};
33 }
34 
35 Property::Property(const Record *def)
36  : Property(
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"))) {
53  this->def = def;
54  assert((def->isSubClassOf("Property") || def->isSubClassOf("Attr")) &&
55  "must be subclass of TableGen 'Property' class");
56 }
57 
58 Property::Property(const DefInit *init) : Property(init->getDef()) {}
59 
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";
85 }
86 
87 StringRef Property::getPropertyDefName() const {
88  if (def->isAnonymous()) {
89  return getBaseProperty().def->getName();
90  }
91  return def->getName();
92 }
93 
95  if (const auto *defInit =
96  llvm::dyn_cast<llvm::DefInit>(def->getValueInit("baseProperty"))) {
97  return Property(defInit).getBaseProperty();
98  }
99  return *this;
100 }
static StringRef getValueAsString(const Init *init)
Definition: Property.cpp:29
StringRef getPropertyDefName() const
Definition: Property.cpp:87
Property(const llvm::Record *record)
Property getBaseProperty() const
Definition: Property.cpp:94
Include the generated interface declarations.