| 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 "mlir/TableGen/Predicate.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>(Val: init)) |
| 32 | return str->getValue().trim(); |
| 33 | return {}; |
| 34 | } |
| 35 | |
| 36 | StringRef PropConstraint::getInterfaceType() const { |
| 37 | return getValueAsString(init: def->getValueInit(FieldName: "interfaceType" )); |
| 38 | } |
| 39 | |
| 40 | Property::Property(const Record *def) |
| 41 | : Property( |
| 42 | def, getValueAsString(init: def->getValueInit(FieldName: "summary" )), |
| 43 | getValueAsString(init: def->getValueInit(FieldName: "description" )), |
| 44 | getValueAsString(init: def->getValueInit(FieldName: "storageType" )), |
| 45 | getValueAsString(init: def->getValueInit(FieldName: "interfaceType" )), |
| 46 | getValueAsString(init: def->getValueInit(FieldName: "convertFromStorage" )), |
| 47 | getValueAsString(init: def->getValueInit(FieldName: "assignToStorage" )), |
| 48 | getValueAsString(init: def->getValueInit(FieldName: "convertToAttribute" )), |
| 49 | getValueAsString(init: def->getValueInit(FieldName: "convertFromAttribute" )), |
| 50 | getValueAsString(init: def->getValueInit(FieldName: "parser" )), |
| 51 | getValueAsString(init: def->getValueInit(FieldName: "optionalParser" )), |
| 52 | getValueAsString(init: def->getValueInit(FieldName: "printer" )), |
| 53 | getValueAsString(init: def->getValueInit(FieldName: "readFromMlirBytecode" )), |
| 54 | getValueAsString(init: def->getValueInit(FieldName: "writeToMlirBytecode" )), |
| 55 | getValueAsString(init: def->getValueInit(FieldName: "hashProperty" )), |
| 56 | getValueAsString(init: def->getValueInit(FieldName: "defaultValue" )), |
| 57 | getValueAsString(init: def->getValueInit(FieldName: "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 | |
| 99 | Pred Property::getPredicate() const { |
| 100 | if (!def) |
| 101 | return Pred(); |
| 102 | const llvm::RecordVal *maybePred = def->getValue(Name: "predicate" ); |
| 103 | if (!maybePred || !maybePred->getValue()) |
| 104 | return Pred(); |
| 105 | return Pred(maybePred->getValue()); |
| 106 | } |
| 107 | |
| 108 | Property Property::getBaseProperty() const { |
| 109 | if (const auto *defInit = |
| 110 | llvm::dyn_cast<llvm::DefInit>(Val: def->getValueInit(FieldName: "baseProperty" ))) { |
| 111 | return Property(defInit).getBaseProperty(); |
| 112 | } |
| 113 | return *this; |
| 114 | } |
| 115 | |