1 //===- Dialect.cpp - Dialect 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 // Dialect wrapper to simplify using TableGen Record defining a MLIR dialect. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/TableGen/Dialect.h" 14 #include "llvm/TableGen/Record.h" 15 16 namespace mlir { 17 namespace tblgen { 18 19 StringRef tblgen::Dialect::getName() const { 20 return def->getValueAsString("name"); 21 } 22 23 StringRef tblgen::Dialect::getCppNamespace() const { 24 return def->getValueAsString("cppNamespace"); 25 } 26 27 std::string tblgen::Dialect::getCppClassName() const { 28 // Simply use the name and remove any '_' tokens. 29 std::string cppName = def->getName().str(); 30 llvm::erase_if(cppName, [](char c) { return c == '_'; }); 31 return cppName; 32 } 33 34 static StringRef getAsStringOrEmpty(const llvm::Record &record, 35 StringRef fieldName) { 36 if (auto valueInit = record.getValueInit(fieldName)) { 37 if (llvm::isa<llvm::CodeInit, llvm::StringInit>(valueInit)) 38 return record.getValueAsString(fieldName); 39 } 40 return ""; 41 } 42 43 StringRef tblgen::Dialect::getSummary() const { 44 return getAsStringOrEmpty(*def, "summary"); 45 } 46 47 StringRef tblgen::Dialect::getDescription() const { 48 return getAsStringOrEmpty(*def, "description"); 49 } 50 51 llvm::Optional<StringRef> tblgen::Dialect::getExtraClassDeclaration() const { 52 auto value = def->getValueAsString("extraClassDeclaration"); 53 return value.empty() ? llvm::Optional<StringRef>() : value; 54 } 55 56 bool tblgen::Dialect::hasConstantMaterializer() const { 57 return def->getValueAsBit("hasConstantMaterializer"); 58 } 59 60 bool tblgen::Dialect::hasOperationAttrVerify() const { 61 return def->getValueAsBit("hasOperationAttrVerify"); 62 } 63 64 bool tblgen::Dialect::hasRegionArgAttrVerify() const { 65 return def->getValueAsBit("hasRegionArgAttrVerify"); 66 } 67 68 bool tblgen::Dialect::hasRegionResultAttrVerify() const { 69 return def->getValueAsBit("hasRegionResultAttrVerify"); 70 } 71 72 bool Dialect::operator==(const Dialect &other) const { 73 return def == other.def; 74 } 75 76 bool Dialect::operator<(const Dialect &other) const { 77 return getName() < other.getName(); 78 } 79 80 } // end namespace tblgen 81 } // end namespace mlir 82