mirror of
https://github.com/intel/llvm.git
synced 2026-01-13 19:08:21 +08:00
[NFC][MLIR][TableGen] Adopt NamespaceEmitter more widely (#163289)
Adopt NamespaceEmitter more widely in MLIR TableGen.
This commit is contained in:
@@ -50,19 +50,21 @@ private:
|
||||
};
|
||||
|
||||
// Simple RAII helper for emitting namespace scope. Name can be a single
|
||||
// namespace (empty for anonymous namespace) or nested namespace.
|
||||
// namespace or nested namespace. If the name is empty, will not generate any
|
||||
// namespace scope.
|
||||
class NamespaceEmitter {
|
||||
public:
|
||||
NamespaceEmitter(raw_ostream &OS, StringRef Name)
|
||||
: Name(trim(Name).str()), OS(OS) {
|
||||
OS << "namespace " << this->Name << " {\n";
|
||||
NamespaceEmitter(raw_ostream &OS, StringRef NameUntrimmed)
|
||||
: Name(trim(NameUntrimmed).str()), OS(OS) {
|
||||
if (!Name.empty())
|
||||
OS << "namespace " << Name << " {\n";
|
||||
}
|
||||
|
||||
~NamespaceEmitter() { close(); }
|
||||
|
||||
// Explicit function to close the namespace scopes.
|
||||
void close() {
|
||||
if (!Closed)
|
||||
if (!Closed && !Name.empty())
|
||||
OS << "} // namespace " << Name << "\n";
|
||||
Closed = true;
|
||||
}
|
||||
|
||||
@@ -96,17 +96,14 @@ def EncodingTrait : AttrInterface<"EncodingTrait"> {
|
||||
}];
|
||||
let methods = [
|
||||
];
|
||||
// ATTR-INTERFACE: namespace mlir
|
||||
// ATTR-INTERFACE-NEXT: namespace a
|
||||
// ATTR-INTERFACE-NEXT: namespace traits
|
||||
// ATTR-INTERFACE: namespace mlir::a::traits {
|
||||
// ATTR-INTERFACE-NEXT: /// Common trait for all layouts.
|
||||
// ATTR-INTERFACE-NEXT: class EncodingTrait;
|
||||
}
|
||||
|
||||
def SimpleEncodingTrait : AttrInterface<"SimpleEncodingTrait"> {
|
||||
let cppNamespace = "a::traits";
|
||||
// ATTR-INTERFACE: namespace a {
|
||||
// ATTR-INTERFACE-NEXT: namespace traits {
|
||||
// ATTR-INTERFACE: namespace a::traits {
|
||||
// ATTR-INTERFACE-NEXT: class SimpleEncodingTrait;
|
||||
}
|
||||
|
||||
@@ -116,8 +113,7 @@ def SimpleOpInterface : OpInterface<"SimpleOpInterface"> {
|
||||
|
||||
Simple Op Interface description
|
||||
}];
|
||||
// OP-INTERFACE: namespace a {
|
||||
// OP-INTERFACE-NEXT: namespace traits {
|
||||
// OP-INTERFACE: namespace a::traits {
|
||||
// OP-INTERFACE-NEXT: /// Simple Op Interface description
|
||||
// OP-INTERFACE-NEXT: class SimpleOpInterface;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
#include "llvm/Support/FormatVariadic.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "llvm/TableGen/CodeGenHelpers.h"
|
||||
#include "llvm/TableGen/Error.h"
|
||||
#include "llvm/TableGen/Record.h"
|
||||
#include "llvm/TableGen/TableGenBackend.h"
|
||||
@@ -701,11 +702,7 @@ static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
|
||||
StringRef underlyingToSymFnName = enumInfo.getUnderlyingToSymbolFnName();
|
||||
auto enumerants = enumInfo.getAllCases();
|
||||
|
||||
SmallVector<StringRef, 2> namespaces;
|
||||
llvm::SplitString(cppNamespace, namespaces, "::");
|
||||
|
||||
for (auto ns : namespaces)
|
||||
os << "namespace " << ns << " {\n";
|
||||
llvm::NamespaceEmitter ns(os, cppNamespace);
|
||||
|
||||
// Emit the enum class definition
|
||||
emitEnumClass(enumDef, enumName, underlyingType, description, enumerants, os);
|
||||
@@ -766,8 +763,7 @@ public:
|
||||
os << formatv(attrClassDecl, enumName, attrClassName, baseAttrClassName);
|
||||
}
|
||||
|
||||
for (auto ns : llvm::reverse(namespaces))
|
||||
os << "} // namespace " << ns << "\n";
|
||||
ns.close();
|
||||
|
||||
// Generate a generic parser and printer for the enum.
|
||||
std::string qualName =
|
||||
@@ -790,13 +786,8 @@ static bool emitEnumDecls(const RecordKeeper &records, raw_ostream &os) {
|
||||
|
||||
static void emitEnumDef(const Record &enumDef, raw_ostream &os) {
|
||||
EnumInfo enumInfo(enumDef);
|
||||
StringRef cppNamespace = enumInfo.getCppNamespace();
|
||||
|
||||
SmallVector<StringRef, 2> namespaces;
|
||||
llvm::SplitString(cppNamespace, namespaces, "::");
|
||||
|
||||
for (auto ns : namespaces)
|
||||
os << "namespace " << ns << " {\n";
|
||||
llvm::NamespaceEmitter ns(os, enumInfo.getCppNamespace());
|
||||
|
||||
if (enumInfo.isBitEnum()) {
|
||||
emitSymToStrFnForBitEnum(enumDef, os);
|
||||
@@ -810,10 +801,6 @@ static void emitEnumDef(const Record &enumDef, raw_ostream &os) {
|
||||
|
||||
if (enumInfo.genSpecializedAttr())
|
||||
emitSpecializedAttrDef(enumDef, os);
|
||||
|
||||
for (auto ns : llvm::reverse(namespaces))
|
||||
os << "} // namespace " << ns << "\n";
|
||||
os << "\n";
|
||||
}
|
||||
|
||||
static bool emitEnumDefs(const RecordKeeper &records, raw_ostream &os) {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
#include "llvm/Support/FormatVariadic.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "llvm/TableGen/CodeGenHelpers.h"
|
||||
#include "llvm/TableGen/Error.h"
|
||||
#include "llvm/TableGen/Record.h"
|
||||
#include "llvm/TableGen/TableGenBackend.h"
|
||||
@@ -342,11 +343,7 @@ void InterfaceGenerator::emitModelDecl(const Interface &interface) {
|
||||
}
|
||||
|
||||
void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
|
||||
llvm::SmallVector<StringRef, 2> namespaces;
|
||||
llvm::SplitString(interface.getCppNamespace(), namespaces, "::");
|
||||
for (StringRef ns : namespaces)
|
||||
os << "namespace " << ns << " {\n";
|
||||
|
||||
llvm::NamespaceEmitter ns(os, interface.getCppNamespace());
|
||||
for (auto &method : interface.getMethods()) {
|
||||
os << "template<typename " << valueTemplate << ">\n";
|
||||
emitCPPType(method.getReturnType(), os);
|
||||
@@ -442,18 +439,11 @@ void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
|
||||
method.isStatic() ? &ctx : &nonStaticMethodFmt);
|
||||
os << "\n}\n";
|
||||
}
|
||||
|
||||
for (StringRef ns : llvm::reverse(namespaces))
|
||||
os << "} // namespace " << ns << "\n";
|
||||
}
|
||||
|
||||
void InterfaceGenerator::emitInterfaceTraitDecl(const Interface &interface) {
|
||||
llvm::SmallVector<StringRef, 2> namespaces;
|
||||
llvm::SplitString(interface.getCppNamespace(), namespaces, "::");
|
||||
for (StringRef ns : namespaces)
|
||||
os << "namespace " << ns << " {\n";
|
||||
|
||||
os << "namespace detail {\n";
|
||||
auto cppNamespace = (interface.getCppNamespace() + "::detail").str();
|
||||
llvm::NamespaceEmitter ns(os, cppNamespace);
|
||||
|
||||
StringRef interfaceName = interface.getName();
|
||||
auto interfaceTraitsName = (interfaceName + "InterfaceTraits").str();
|
||||
@@ -504,10 +494,6 @@ void InterfaceGenerator::emitInterfaceTraitDecl(const Interface &interface) {
|
||||
os << tblgen::tgfmt(*extraTraitDecls, &traitMethodFmt) << "\n";
|
||||
|
||||
os << " };\n";
|
||||
os << "}// namespace detail\n";
|
||||
|
||||
for (StringRef ns : llvm::reverse(namespaces))
|
||||
os << "} // namespace " << ns << "\n";
|
||||
}
|
||||
|
||||
static void emitInterfaceDeclMethods(const Interface &interface,
|
||||
@@ -533,10 +519,7 @@ static void emitInterfaceDeclMethods(const Interface &interface,
|
||||
}
|
||||
|
||||
void InterfaceGenerator::forwardDeclareInterface(const Interface &interface) {
|
||||
llvm::SmallVector<StringRef, 2> namespaces;
|
||||
llvm::SplitString(interface.getCppNamespace(), namespaces, "::");
|
||||
for (StringRef ns : namespaces)
|
||||
os << "namespace " << ns << " {\n";
|
||||
llvm::NamespaceEmitter ns(os, interface.getCppNamespace());
|
||||
|
||||
// Emit a forward declaration of the interface class so that it becomes usable
|
||||
// in the signature of its methods.
|
||||
@@ -545,16 +528,10 @@ void InterfaceGenerator::forwardDeclareInterface(const Interface &interface) {
|
||||
|
||||
StringRef interfaceName = interface.getName();
|
||||
os << "class " << interfaceName << ";\n";
|
||||
|
||||
for (StringRef ns : llvm::reverse(namespaces))
|
||||
os << "} // namespace " << ns << "\n";
|
||||
}
|
||||
|
||||
void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
|
||||
llvm::SmallVector<StringRef, 2> namespaces;
|
||||
llvm::SplitString(interface.getCppNamespace(), namespaces, "::");
|
||||
for (StringRef ns : namespaces)
|
||||
os << "namespace " << ns << " {\n";
|
||||
llvm::NamespaceEmitter ns(os, interface.getCppNamespace());
|
||||
|
||||
StringRef interfaceName = interface.getName();
|
||||
auto interfaceTraitsName = (interfaceName + "InterfaceTraits").str();
|
||||
@@ -631,9 +608,6 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
|
||||
}
|
||||
|
||||
os << "};\n";
|
||||
|
||||
for (StringRef ns : llvm::reverse(namespaces))
|
||||
os << "} // namespace " << ns << "\n";
|
||||
}
|
||||
|
||||
bool InterfaceGenerator::emitInterfaceDecls() {
|
||||
|
||||
@@ -259,8 +259,8 @@ static void emitInterfaceDecl(const Availability &availability,
|
||||
std::string interfaceTraitsName =
|
||||
std::string(formatv("{0}Traits", interfaceName));
|
||||
|
||||
StringRef cppNamespace = availability.getInterfaceClassNamespace();
|
||||
llvm::NamespaceEmitter nsEmitter(os, cppNamespace);
|
||||
llvm::NamespaceEmitter nsEmitter(os,
|
||||
availability.getInterfaceClassNamespace());
|
||||
os << "class " << interfaceName << ";\n\n";
|
||||
|
||||
// Emit the traits struct containing the concept and model declarations.
|
||||
@@ -418,15 +418,9 @@ static void emitAvailabilityQueryForBitEnum(const Record &enumDef,
|
||||
static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
|
||||
EnumInfo enumInfo(enumDef);
|
||||
StringRef enumName = enumInfo.getEnumClassName();
|
||||
StringRef cppNamespace = enumInfo.getCppNamespace();
|
||||
auto enumerants = enumInfo.getAllCases();
|
||||
|
||||
llvm::SmallVector<StringRef, 2> namespaces;
|
||||
llvm::SplitString(cppNamespace, namespaces, "::");
|
||||
|
||||
for (auto ns : namespaces)
|
||||
os << "namespace " << ns << " {\n";
|
||||
|
||||
llvm::NamespaceEmitter ns(os, enumInfo.getCppNamespace());
|
||||
llvm::StringSet<> handledClasses;
|
||||
|
||||
// Place all availability specifications to their corresponding
|
||||
@@ -441,9 +435,6 @@ static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
|
||||
enumName);
|
||||
handledClasses.insert(className);
|
||||
}
|
||||
|
||||
for (auto ns : llvm::reverse(namespaces))
|
||||
os << "} // namespace " << ns << "\n";
|
||||
}
|
||||
|
||||
static bool emitEnumDecls(const RecordKeeper &records, raw_ostream &os) {
|
||||
@@ -459,31 +450,19 @@ static bool emitEnumDecls(const RecordKeeper &records, raw_ostream &os) {
|
||||
|
||||
static void emitEnumDef(const Record &enumDef, raw_ostream &os) {
|
||||
EnumInfo enumInfo(enumDef);
|
||||
StringRef cppNamespace = enumInfo.getCppNamespace();
|
||||
llvm::NamespaceEmitter ns(os, enumInfo.getCppNamespace());
|
||||
|
||||
llvm::SmallVector<StringRef, 2> namespaces;
|
||||
llvm::SplitString(cppNamespace, namespaces, "::");
|
||||
|
||||
for (auto ns : namespaces)
|
||||
os << "namespace " << ns << " {\n";
|
||||
|
||||
if (enumInfo.isBitEnum()) {
|
||||
if (enumInfo.isBitEnum())
|
||||
emitAvailabilityQueryForBitEnum(enumDef, os);
|
||||
} else {
|
||||
else
|
||||
emitAvailabilityQueryForIntEnum(enumDef, os);
|
||||
}
|
||||
|
||||
for (auto ns : llvm::reverse(namespaces))
|
||||
os << "} // namespace " << ns << "\n";
|
||||
os << "\n";
|
||||
}
|
||||
|
||||
static bool emitEnumDefs(const RecordKeeper &records, raw_ostream &os) {
|
||||
llvm::emitSourceFileHeader("SPIR-V Enum Availability Definitions", os,
|
||||
records);
|
||||
|
||||
auto defs = records.getAllDerivedDefinitions("EnumInfo");
|
||||
for (const auto *def : defs)
|
||||
for (const Record *def : records.getAllDerivedDefinitions("EnumInfo"))
|
||||
emitEnumDef(*def, os);
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user