72 lines · cpp
1//===- Type.cpp - Type 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// Type wrapper to simplify using TableGen Record defining a MLIR Type.10//11//===----------------------------------------------------------------------===//12 13#include "mlir/TableGen/Type.h"14#include "mlir/TableGen/Dialect.h"15#include "llvm/ADT/TypeSwitch.h"16#include "llvm/TableGen/Record.h"17 18using namespace mlir;19using namespace mlir::tblgen;20using llvm::Record;21 22TypeConstraint::TypeConstraint(const llvm::DefInit *init)23 : TypeConstraint(init->getDef()) {}24 25bool TypeConstraint::isOptional() const {26 return def->isSubClassOf("Optional");27}28 29bool TypeConstraint::isVariadic() const {30 return def->isSubClassOf("Variadic");31}32 33bool TypeConstraint::isVariadicOfVariadic() const {34 return def->isSubClassOf("VariadicOfVariadic");35}36 37StringRef TypeConstraint::getVariadicOfVariadicSegmentSizeAttr() const {38 assert(isVariadicOfVariadic());39 return def->getValueAsString("segmentAttrName");40}41 42// Returns the builder call for this constraint if this is a buildable type,43// returns std::nullopt otherwise.44std::optional<StringRef> TypeConstraint::getBuilderCall() const {45 const Record *baseType = def;46 if (isVariableLength())47 baseType = baseType->getValueAsDef("baseType");48 49 // Check to see if this type constraint has a builder call.50 const llvm::RecordVal *builderCall = baseType->getValue("builderCall");51 if (!builderCall || !builderCall->getValue())52 return std::nullopt;53 return TypeSwitch<const llvm::Init *, std::optional<StringRef>>(54 builderCall->getValue())55 .Case<llvm::StringInit>([&](auto *init) {56 StringRef value = init->getValue();57 return value.empty() ? std::optional<StringRef>() : value;58 })59 .Default(std::nullopt);60}61 62// Return the C++ type for this type (which may just be ::mlir::Type).63StringRef TypeConstraint::getCppType() const {64 return def->getValueAsString("cppType");65}66 67Type::Type(const Record *record) : TypeConstraint(record) {}68 69Dialect Type::getDialect() const {70 return Dialect(def->getValueAsDef("dialect"));71}72