122 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// Property wrapper to simplify using TableGen Record defining a MLIR10// 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 initializer28// 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>(init))31 return str->getValue().trim();32 return {};33}34 35StringRef PropConstraint::getInterfaceType() const {36 return getValueAsString(def->getValueInit("interfaceType"));37}38 39Property::Property(const Record *def)40 : Property(41 def, getValueAsString(def->getValueInit("summary")),42 getValueAsString(def->getValueInit("description")),43 getValueAsString(def->getValueInit("storageType")),44 getValueAsString(def->getValueInit("interfaceType")),45 getValueAsString(def->getValueInit("convertFromStorage")),46 getValueAsString(def->getValueInit("assignToStorage")),47 getValueAsString(def->getValueInit("convertToAttribute")),48 getValueAsString(def->getValueInit("convertFromAttribute")),49 getValueAsString(def->getValueInit("parser")),50 getValueAsString(def->getValueInit("optionalParser")),51 getValueAsString(def->getValueInit("printer")),52 getValueAsString(def->getValueInit("readFromMlirBytecode")),53 getValueAsString(def->getValueInit("writeToMlirBytecode")),54 getValueAsString(def->getValueInit("hashProperty")),55 getValueAsString(def->getValueInit("defaultValue")),56 getValueAsString(def->getValueInit("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("predicate");102 if (!maybePred || !maybePred->getValue())103 return Pred();104 return Pred(maybePred->getValue());105}106 107Property Property::getBaseProperty() const {108 if (const auto *defInit =109 llvm::dyn_cast<llvm::DefInit>(def->getValueInit("baseProperty"))) {110 return Property(defInit).getBaseProperty();111 }112 return *this;113}114 115bool Property::isSubClassOf(StringRef className) const {116 return def && def->isSubClassOf(className);117}118 119StringRef ConstantProp::getValue() const {120 return def->getValueAsString("value");121}122