Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/TableGen/Type.cpp
//===- Type.cpp - Type class ----------------------------------------------===// | //===- Type.cpp - Type class ----------------------------------------------===// | ||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// | // | ||||
// Type wrapper to simplify using TableGen Record defining a MLIR Type. | // Type wrapper to simplify using TableGen Record defining a MLIR Type. | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "mlir/TableGen/Type.h" | #include "mlir/TableGen/Type.h" | ||||
#include "mlir/TableGen/Dialect.h" | |||||
#include "llvm/ADT/Twine.h" | |||||
#include "llvm/ADT/TypeSwitch.h" | #include "llvm/ADT/TypeSwitch.h" | ||||
#include "llvm/TableGen/Record.h" | #include "llvm/TableGen/Record.h" | ||||
using namespace mlir; | using namespace mlir; | ||||
using namespace mlir::tblgen; | using namespace mlir::tblgen; | ||||
TypeConstraint::TypeConstraint(const llvm::Record *record) | TypeConstraint::TypeConstraint(const llvm::Record *record) | ||||
: Constraint(Constraint::CK_Type, record) { | : Constraint(Constraint::CK_Type, record) { | ||||
Show All 27 Lines | return TypeSwitch<llvm::Init *, Optional<StringRef>>(builderCall->getValue()) | ||||
.Case<llvm::StringInit>([&](auto *init) { | .Case<llvm::StringInit>([&](auto *init) { | ||||
StringRef value = init->getValue(); | StringRef value = init->getValue(); | ||||
return value.empty() ? Optional<StringRef>() : value; | return value.empty() ? Optional<StringRef>() : value; | ||||
}) | }) | ||||
.Default([](auto *) { return llvm::None; }); | .Default([](auto *) { return llvm::None; }); | ||||
} | } | ||||
// Return the C++ class name for this type (which may just be ::mlir::Type). | // Return the C++ class name for this type (which may just be ::mlir::Type). | ||||
StringRef TypeConstraint::getCPPClassName() const { | std::string TypeConstraint::getCPPClassName() const { | ||||
return def->getValueAsString("cppClassName"); | StringRef className = def->getValueAsString("cppClassName"); | ||||
// If the class name is already namespace resolved, use it. | |||||
if (className.contains("::")) | |||||
return className.str(); | |||||
// Otherwise, check to see if there is a namespace from a dialect to prepend. | |||||
if (const llvm::RecordVal *value = def->getValue("dialect")) { | |||||
Dialect dialect(cast<const llvm::DefInit>(value->getValue())->getDef()); | |||||
return (dialect.getCppNamespace() + "::" + className).str(); | |||||
} | |||||
return className.str(); | |||||
} | } | ||||
Type::Type(const llvm::Record *record) : TypeConstraint(record) {} | Type::Type(const llvm::Record *record) : TypeConstraint(record) {} | ||||
StringRef Type::getDescription() const { | StringRef Type::getDescription() const { | ||||
return def->getValueAsString("description"); | return def->getValueAsString("description"); | ||||
} | } | ||||
Dialect Type::getDialect() const { | Dialect Type::getDialect() const { | ||||
return Dialect(def->getValueAsDef("dialect")); | return Dialect(def->getValueAsDef("dialect")); | ||||
} | } |