[SampleProfileLoader] Fix integer overflow in generateMDProfMetadata (#90217)
[llvm-project.git] / mlir / lib / TableGen / Property.cpp
blobe61d2fd2480fd5338eb33cf460fb7baa6aadbb2e
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.
12 //===----------------------------------------------------------------------===//
14 #include "mlir/TableGen/Property.h"
15 #include "mlir/TableGen/Format.h"
16 #include "mlir/TableGen/Operator.h"
17 #include "llvm/TableGen/Record.h"
19 using namespace mlir;
20 using namespace mlir::tblgen;
22 using llvm::DefInit;
23 using llvm::Init;
24 using llvm::Record;
25 using llvm::StringInit;
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 {};
35 Property::Property(const Record *def)
36 : Property(getValueAsString(def->getValueInit("storageType")),
37 getValueAsString(def->getValueInit("interfaceType")),
38 getValueAsString(def->getValueInit("convertFromStorage")),
39 getValueAsString(def->getValueInit("assignToStorage")),
40 getValueAsString(def->getValueInit("convertToAttribute")),
41 getValueAsString(def->getValueInit("convertFromAttribute")),
42 getValueAsString(def->getValueInit("readFromMlirBytecode")),
43 getValueAsString(def->getValueInit("writeToMlirBytecode")),
44 getValueAsString(def->getValueInit("hashProperty")),
45 getValueAsString(def->getValueInit("defaultValue"))) {
46 this->def = def;
47 assert((def->isSubClassOf("Property") || def->isSubClassOf("Attr")) &&
48 "must be subclass of TableGen 'Property' class");
51 Property::Property(const DefInit *init) : Property(init->getDef()) {}
53 Property::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";