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
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
35Property::Property(const Record *def)
36 : Property(getValueAsString(init: def->getValueInit(FieldName: "storageType")),
37 getValueAsString(init: def->getValueInit(FieldName: "interfaceType")),
38 getValueAsString(init: def->getValueInit(FieldName: "convertFromStorage")),
39 getValueAsString(init: def->getValueInit(FieldName: "assignToStorage")),
40 getValueAsString(init: def->getValueInit(FieldName: "convertToAttribute")),
41 getValueAsString(init: def->getValueInit(FieldName: "convertFromAttribute")),
42 getValueAsString(init: def->getValueInit(FieldName: "readFromMlirBytecode")),
43 getValueAsString(init: def->getValueInit(FieldName: "writeToMlirBytecode")),
44 getValueAsString(init: def->getValueInit(FieldName: "hashProperty")),
45 getValueAsString(init: def->getValueInit(FieldName: "defaultValue"))) {
46 this->def = def;
47 assert((def->isSubClassOf("Property") || def->isSubClassOf("Attr")) &&
48 "must be subclass of TableGen 'Property' class");
49}
50
51Property::Property(const DefInit *init) : Property(init->getDef()) {}
52
53Property::Property(StringRef storageType, StringRef interfaceType,
54 StringRef convertFromStorageCall,
55 StringRef assignToStorageCall,
56 StringRef convertToAttributeCall,
57 StringRef convertFromAttributeCall,
58 StringRef readFromMlirBytecodeCall,
59 StringRef writeToMlirBytecodeCall,
60 StringRef hashPropertyCall, StringRef defaultValue)
61 : storageType(storageType), interfaceType(interfaceType),
62 convertFromStorageCall(convertFromStorageCall),
63 assignToStorageCall(assignToStorageCall),
64 convertToAttributeCall(convertToAttributeCall),
65 convertFromAttributeCall(convertFromAttributeCall),
66 readFromMlirBytecodeCall(readFromMlirBytecodeCall),
67 writeToMlirBytecodeCall(writeToMlirBytecodeCall),
68 hashPropertyCall(hashPropertyCall), defaultValue(defaultValue) {
69 if (storageType.empty())
70 storageType = "Property";
71}
72

source code of mlir/lib/TableGen/Property.cpp